Porting basics

B2G OS uses a kernel derived from Android, with a Gecko-based user interface on top of it. This article provides a basic guide to how to go about porting the operating system to new devices.

This guide assumes you're porting to a new device that already runs Android; if you're porting to another device, the job is going to be more involved.

Note: You can find help on porting on the #fxos IRC channel and on Mozilla Discourse.

Set up your build system

The first step is to configure your build system; you can follow the guide in B2G OS build prerequisites.

Create a local backup of the original Android system

Next, you should back up your Android device before you start nuking it with your test builds of B2G. In addition, some of these bits will be needed by the build and install process.  When picking the device id name, we recommend using '_' instead of '-' .  For the rationale behind this recommendation, see bug 1243349.

mkdir my_device_backup
cd my_device_backup
adb pull /system system

Clone the B2G repositories

The first step is to clone the B2G repository as well as the repository with the manifests.

git clone https://github.com/mozilla-b2g/B2G.git
git clone https://github.com/mozilla-b2g/b2g-manifest.git

Add a new device to config.sh

The next step is to add a new device to config.sh in the B2G repository;  you can use the existing ones as a template. This basically involves providing the instructions for fetching the correct files to do the build.

Create a manifest for the new device

Now you need to add a manifest file for the new device. Refer to one of the existing manifests for a template. You can use the hamachi manifest a reference. Once done, you should add and commit your new manifest to your local b2g-manifest repository:

git add my-new-device.xml
git commit

Next, you will want the config.sh file to use your local b2g-manifest repository instead of the official one. To achieve this, change the values of the GITREPO and BRANCH variables in the config.sh file to the location of your local repository and desired branch, for example:

GITREPO=${GITREPO:-"file:///home/yourname/b2g-manifest"}
BRANCH=${BRANCH:-master}

Create a configuration tree for the new device

Create a new configuration tree for the new device. This should be at device/<manufacturer>/<device_id>. This tree should include, at least:

  • AndroidBoard.mk
  • AndroidProducts.mk
  • BoardConfig.mk
  • extract-files.sh
  • full_<device_id>.mk
  • idc files for touchscreen
  • init files (init.rc, init.<target>.rc, uevent.rc, ...)

The content here may differ a great deal from one device to another. In particular, BoardConfig.mk and extract-files.sh may differ significantly. This part requires a lot of hacking, testing, and debugging to figure out which binary blobs should be extracted. To get a better idea what is supposed to be contained there, take a look at the configuration for the hamachi device. Remember to correctly reference your own configuration tree from the manifest you created for your new device.

Note: If you can find an existing reference on CyanogenMod for your device, this information will speed up the porting process. the XDA Forum is another good place to discuss and check for resources.

Rebuild boot.img

Once you have all that done, you need to rebuild the boot image. This isn't usually needed for the kernel itself, but to pick up the changes to init.rc.

Changes to init.rc

The init.rc you use is not the one provided by B2G; instead, you need to pull it from the device.

The main things you'll need to modify are:

Import init.b2g.rc

Add the following lines to import init.b2g.rc:

on early-init
    start ueventd
    import /init.b2g.rc

Fix permissions

Correct the permissions on the files /system/b2g/b2g, /system/b2g/updater, /system/b2g/plugin-container; this should be done after the lines that mount the filesystem read/write:

chmod 0755 /system/b2g/b2g
chmod 0755 /system/b2g/updater
chmod 0755 /system/b2g/plugin-container

You might want to start by modifying the init.rc from the new device instead of using the init.rc provided by the build system; if so, you need to remember to set TARGET_PROVIDES_INIT_RC in BoardConfig.mk.

Prebuilt kernel vs. building the kernel from source

You can use a prebuilt kernel, or you may build the kernel from source. To build the kernel from source, add AndroidKernel.mk and the kernel config to the device configuration tree.

The maguro on the old build system is an example that builds the kernel from source.

Extracting and modifying an existing boot image

It is possible to recover the boot image of a phone by dumping the contents of the /dev/mtd/mtd1 or /dev/mtd/mtd2 devices, the resulting image file can then be easily recovered:

adb shell 'cat /dev/mtd/mtd1 > /sdcard/boot.img'
adb pull /sdcard/boot.img

Once the boot image file has been obtained it can be unpacked via a helper tool such as unmkbootimg. The tool will extract both the kernel image (zImage) and the ramdisk (initramfs.cpio.gz) as well as printing out a command to rebuild the image with the same parameters of the original one, for example:

$ unmkbootimg boot.img 
Kernel size 3872576
Kernel address 0x208000
Ramdisk size 265102
Ramdisk address 0x1500000
Secondary size 0
Secondary address 0x1100000
Kernel tags address 0x200100
Flash page size 2048
Board name is ""
Command line "androidboot.hardware=aphone"
Extracting kernel to file zImage ...
Extracting root filesystem to file initramfs.cpio.gz ...
All done.
---------------
To recompile this image, use:
  mkbootimg --kernel zImage --ramdisk initramfs.cpio.gz --base 0x200000 --cmdline 'androidboot.hardware=aphone' -o new_boot.img
---------------

To modify the ramdisk file create an output directory and extract it there:

mkdir initramfs_dir
cd initramfs_dir
gunzip -c ../initramfs.cpio.gz | cpio -i

Make all the required changes (such as modifying init.rc) and repack the ramdisk using mkbootfs, be sure to use the version that has been built with the B2G host tools:

/path/to/your/B2G/out/host/linux-x86/bin/mkbootfs . | gzip > ../newinitramfs.cpio.gz

Finally go back to the top-level directory and re-pack the boot image using the mkbootimg, also make sure you're using the version built with the other B2G host tools:

/path/to/your/B2G/out/host/linux-x86/bin/mkbootimg --kernel zImage --ramdisk newinitramfs.cpio.gz --base 0x200000 --cmdline 'androidboot.hardware=aphone' -o newboot.img

If you now copy the new boot image under out/target/product/$DEVICE/boot.img (where $DEVICE is your device name) it will be automatically flashed when invoking flash.sh. Alternatively you can flash it by hand with the following commands:

adb reboot bootloader
fastboot flash boot newboot.img
fastboot reboot

Add the new device to flash.sh

Add the new device to flash.sh; the specifics of how to do this will depend on what tools need to be used to flash the new device.

Configure, build, and flash the new device

Now you can try building for and flashing to your new device:

ANDROIDFS_DIR=my_device_backup ./config.sh <device_id> '../b2g-manifest/default.xml'
./build.sh
./flash.sh

Test and debug

We need some details added here; indeed, this entire article could use some help.

See also

Document Tags and Contributors

Tags: 
 Last updated by: chrisdavidmills,