How to build your own linux kernel for your android device

Requirements

ATTENTION

If you have a custom recovery manager installed on your device, you should create a backup! If you have none, have a look at Clockworkmod. ;-)

Build your own kernel for your android device

1. You need the kernelsources matching the currently running kernel. For information how do determine your kernel version have a look at Figure out correct kernel version.

2. You need the config for currently running kernel

  1. Custom images like CyanogenMod have enabled config export through the proc-filesystem. You could obtain it by:

    • adb pull /proc/config.gz .

    • gunzip config.gz

    • cp config /path/to/your/kernel/.config

    b. If you run a stock image, have a look at documentation/kernel-tree.txt. It lists a set of Android device their kernel trees and the matching default config.

3. Set the CROSS_COMPILE variable.

For the Android prebuilt toolchain this step may look like:

4. First, clearify your configuration is up-to-date

Set all needed options for SystemTap descriped in [7]

5. Don't forget to build the kernel itself ;-)

The compiled kernel image is located in arch/arm/boot/.

6. Now you need the current boot image, because it contains the initialramdisk which we will recylce. First, determine the boot partition.

Otherwise you have to determine the kind of flashdriver used and print the partitiontable. Two examples are given below:

If you have determined the correct boot partition, dump it and copy the dump to your computer:

7. Unpack the boot image with the tool unpackbootimg. Its sourcecode is located in tools/bootimg.

The output may look like this:

$ unpackbootimg ./boot.img
Magic value:ANDROID!
Kernel: size:3691628 addr:0x80008000
Ramdisk: size:223281 addr:0x81000000
Second: size:0 addr:0x80f00000
Page size: 2048
Name:
Cmd:
Calc file size:3919872 stat size:3919872
Extracting kernel (3691628 bytes)...
Extracting ramdisk (223281 bytes)...
No secondary image found.

Pay attention on the kernel loadaddress. Here it is 0x80008000, which means the baseaddress is 0x80000000. Keep that in mind! You have to copy the displayed commandline arguments as well.

8. To create the new boot image just run one command and push it to the device:

9. Finally, you have to flash the new boot image. On some firmware images the tool flash_kernel, not on aosp images, is available. I prefer to use this tool. Otherwise you must do it with dd.

Sources

[1] http://wiki.cyanogenmod.org/wiki/Howto:_Install_Kernels

[2] http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack%2C_Edit%2C_and_Re-Pack_Boot_Images

[3] http://source.android.com/source/downloading.html

[4] http://sourceware.org/systemtap/wiki/SystemTapWithSelfBuiltKernel

[5] http://developer.android.com/sdk/index.html

[6] http://wiki.cyanogenmod.org/wiki/Building_Kernel_from_source