It is assumed you've already setup your build environment and cloned the repositories.
It is advisable to follow the instructions in the order presented.
Backing up your Galaxy S2's native OS
You'll want to back up your phone's factory image before attempting to build or flash anything to it. As it happens, the builds or flash process itself can be unstable and you don't want to end up with a brick.
These instructions assume that you have already installed heimdall.
From some other directory than where you have B2G installed:
$ cd /tmp
$ wget http://cmw.22aaf3.com/c1/recovery/re...4-galaxys2.tar
$ tar xvf recovery-clockwork-*tar
$ adb reboot download
When your phone is in "download" mode, it's ready to be flashed using Odin protocol.
To Flash Clockworked (and backup image):
With your phone in "download" mode, run the following:
$ heimdall flash --kernel zImage
Next, reboot the phone to recovery mode.
$ adb reboot recovery
With the phone in recovery mode, follow the on-screen instructions to make a backup image.
Building for Samsung Galaxy S2
Do NOT run any of the following commands as root! From the directory to where you've cloned your git repositories:
$ cd B2G
$ make sync
FIRST TIME ONLY: you should configure
$ sudo nano local.mk
and add the following line:
Next, you will setup your build directory with the binary blobs necessary for building for your device. With your device booted to the original Android OS, connected via USB and running:
$ make config-galaxy-s2
Now, build gonk backend and the system itself:
$ make gonk
Flashing to Samsung Galaxy S2
Making and flashing the kernel (first time only)
$ make kernel
$ adb reboot download
$ heimdall flash --kernel boot/kernel-android-galaxy-s2/arch/arm/boot/zImage
Building and flashing Gaia frontend
Assuming you're in the root of B2G repo, pull files from the remote repo and merge them to your local repo. Next install gaia (by copying files from remote repo to your device):
$ cd gaia
$ git pull origin master
$ cd ..
$ make install-gaia
$ make kill-b2g
Forcing GIT repo into clean state
Sometimes it's necessary to do this - but cloning a fresh repo and running
make sync takes too much time