My description of building process will be strongly based on this article which was a great help in whole process:


  • You should own a Nexus 5 device, with unlocked bootloader (and for best experience rooted)
  • Android SDK tools in your PATH (adb and fastboot needed)
  • Decent internet connection
  • git installed, with other classical tools needed for kernel building

These are the things I'll not cover in the article, as they are basic and not the most important in the whole process.

You should really start with official Google guide here . I'll describe the process from my own perspective i.e. Ubuntu 14.04 x64 machine. So let's start with the tools. I assume you are working in some new empty folder. First is the toolchain:

$ git clone

    Cloning into 'arm-eabi-4.7'...
    remote: Sending approximately 23.63 MiB ...
    remote: Counting objects: 22, done
    remote: Finding sources: 100% (22/22)
    remote: Total 201 (delta 89), reused 201 (delta 89)
    Receiving objects: 100% (201/201), 23.63 MiB | 585.00 KiB/s, done.
    Resolving deltas: 100% (89/89), done.
    Checking connectivity... done.

The newer 4.8 compilers suite seems to not work with Nexus 5 kernel sources at this moment. The same applies to 4.7 -androideabi type. Now you'll create a file with environment variables which are going to help us with simplifying the process.

$ vim

Set them to:

export CC=$(pwd)/arm-eabi-4.7/bin/arm-eabi-
export CROSS_COMPILE=$(pwd)/arm-eabi-4.7/bin/arm-eabi-

export ARCH=arm
export SUBARCH=arm

export PATH=$PATH:$(pwd)/andorid_boot_tools_bin

Make it executable and source to current terminal window.

$ chmod +x
$ source

Now it's time to clone the actual kernel sources (it will take some time).

$ git clone

   Cloning into 'msm'...
   remote: Sending approximately 753.86 MiB ...
   remote: Counting objects: 52377, done
   remote: Finding sources: 100% (4982/4982)
   remote: Total 3288741 (delta 2746117), reused 3287961 (delta 2746117)
   Receiving objects: 100% (3288741/3288741), 754.56 MiB | 601.00 KiB/s, done.
   Resolving deltas: 100% (2753582/2753582), done.
   Checking connectivity... done.

Next thing you'll do is change branch to the proper one (to get the best result you should checkout a commit in your current kernel version 3.4.0-gXXXXXXX, where XXXXXXX is short of a commit SHA-1).

 $ cd msm/
 $ git branch -a

   * master
     remotes/origin/HEAD -> origin/master

$ git checkout origin/android-msm-hammerhead-3.4-kitkat-mr1

Now you should have the code on which you can work :) So compile it, and check if it works for you. In menuconfig General setup ---> Local version - append to kernel release you can append some string that you'll know that it's your kernel.

$ make hammerhead_defconfig
$ make menuconfig
$ make -j4

Make some break while it's compiling. You should adjust the number -jX to the number of cores in your CPU. If all went fine, at the end of output you'll find something like this:

OBJCOPY arch/arm/boot/zImage
Kernel: arch/arm/boot/zImage is ready
CAT     arch/arm/boot/zImage-dtb
Kernel: arch/arm/boot/zImage-dtb is ready

You have your kernel ready. On most embedded systems that will be the end of your work. Usually you'll copy the kernel to SD card or NFS location, and the board will boot. But on Android it's different. You need to prepare special boot partition which then you can boot using fastboot.

So you need to start from downloading the Android image for your phone from Google sites. Go to Nexus Factory Images site and download the image that matches to Android version that's on your phone. In my case it's 4.4.2. Unpack it, then go inside the created directory and unpack the .zip archive. You need to get the boot.img file. Copy the extracted file to a new folder, inside the directory where you earlier downloaded the toolchain and the kernel. Eg. mkidr boot_img there.

Next thing you'll do is preparing some special image crafting tools that Pete Batard made for us and published on his github. I've made a copy of them on mine too.

Build this on a different terminal window, if you've before sourced In the other case gcc will try to cross compile it for ARM architecture...

$ cd .. # if you was in msm directory
$ git clone

    Cloning into 'bootimg-tools'...
    remote: Reusing existing pack: 49, done.
    remote: Total 49 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (49/49), done.
    Checking connectivity... done.

$ cd bootimg-tools/
$ make

    cc -Wall -Wextra -Wno-unused-parameter -pedantic -pipe -std=c99 -D_GNU_SOURCE -Iinclude   -c -o libmincrypt/sha.o libmincrypt/sha.c
    cc -Wall -Wextra -Wno-unused-parameter -pedantic -pipe -std=c99 -D_GNU_SOURCE -Iinclude   -c -o libmincrypt/rsa.o libmincrypt/rsa.c
    cc -Wall -Wextra -Wno-unused-parameter -pedantic -pipe -std=c99 -D_GNU_SOURCE -Iinclude   -c -o libmincrypt/sha256.o libmincrypt/sha256.c
    cc -Wall -Wextra -Wno-unused-parameter -pedantic -pipe -std=c99 -D_GNU_SOURCE -Iinclude   -c -o mkbootimg/mkbootimg.o mkbootimg/mkbootimg.c
    cc -o mkbootimg/unmkbootimg mkbootimg/unmkbootimg.o

$ cd cpio/
$ gcc mkbootfs.c  -o mkbootfs -I../include

$ cd ../..
$ mkdir andorid_boot_tools_bin
$ cd andorid_boot_tools_bin/
$ cp ../bootimg-tools/mkbootimg/mkbootimg .
$ cp ../bootimg-tools/mkbootimg/unmkbootimg .
$ cp ../bootimg-tools/cpio/mkbootfs .
$ cd ..

It’s high time to unpack the boot partition from original image and prepare your own. So let's start.

$ unmkbootimg -i boot_img/boot.img

    kernel written to 'kernel' (8331496 bytes)
    ramdisk written to 'ramdisk.cpio.gz' (498796 bytes)

    To rebuild this boot image, you can use the command:
    mkbootimg --base 0 --pagesize 2048 --kernel_offset 0x00008000 --ramdisk_offset 0x02900000 --second_offset 0x00f00000 --tags_offset 0x02700000 --cmdline 'console=ttyHSL0,115200,n8 androidboot.hardware=hammerhead  user_debug=31 maxcpus=2 msm_watchdog_v2.enable=1' --kernel kernel --ramdisk ramdisk.cpio.gz -o boot_img/boot.img

Change the kernel to the one that you've compiled.

$ cp msm/arch/arm/boot/zImage-dtb kernel
$ mkbootimg --base 0 --pagesize 2048 --kernel_offset 0x00008000 --ramdisk_offset 0x02900000 --second_offset 0x00f00000 --tags_offset 0x02700000 --cmdline 'console=ttyHSL0,115200,n8 androidboot.hardware=hammerhead  user_debug=31 maxcpus=2 msm_watchdog_v2.enable=1' --kernel kernel --ramdisk ramdisk.cpio.gz -o boot.img
$ ls
    andorid_boot_tools_bin  arm-eabi-4.7  boot_img  boot.img  bootimg-tools  kernel  msm  ramdisk.cpio.gz

In output of ls command you should see the boot.img file. If you have it, you've done :) So let's try and check if it works or not. Connect your phone using USB cable to your PC, be sure that you have USB debugging enabled.

$ adb start-server
    * daemon not running. starting it now on port 5037 *
    * daemon started successfully *
$ adb reboot bootloader
$ sudo fastboot boot boot.img

During this commands your phone will reboot to bootloader mode, next using fastboot command you'll copy the new boot image to RAM of the phone and then boot it. YOUR FLASH IS NOT TOUCHED IT'S 100% SAFE! Now just check in settings, what is version of your kernel. When you've done with hacking, and want to have the original just reboot your phone. Happy hacking!

"My settings in Android"

I'm really thankful to Pete Batard for his original article and the tools he prepared. It save me ~20 GB of download the AOSP and much of frustration. Thank you! (:


comments powered by Disqus