Compiling kernel for my Nexus 4

September 2014 ยท 2 minute read

I have used custom compiled kernels on desktop linux, but never tried to build one for android device.

First thing I learned is that Android’s kernel is fork of original kernel, and they have made changes such as wakelocks, ipc etc. So it is good to stick with google’s kernel source and as I have Nexus device, things are simpler than non-nexus devices.

Selecting the source

Currently I’m running Kitkat 4.4.4, so for that I’ll go for android-msm-mako-3.4-kitkat-mr2 branch.

Getting the source
git remote add google && git pull android-msm-mako-3.4-kitkat-mr2 --depth=1

I have selected depth 1 to save bandwidth, and later can be pulled using –unshallow.

Getting toolchain

There are 2 choices available

  1. Google’s arm toolchain
  2. Linaro toolchain

For starter I selected Google’s arm toolchain.

git clone

Specifying path parameter

export PATH=$PATH:~/android/arm-linux-androideabi-4.8/bin
Building the kernel
export ARCH=arm
export SUBARCH=arm
export CROSS_COMPILE=arm-linux-androideabi-

Currently applied only one patch, FIOPS IO scheduler

patch -p1 < ../patches/fiops.patch

Hunk will fail for block/Makefile, just append after test-iosched.

obj-$(CONFIG_IOSCHED_DEADLINE)  += deadline-iosched.o
 obj-$(CONFIG_IOSCHED_CFQ)  += cfq-iosched.o
 obj-$(CONFIG_IOSCHED_TEST) += test-iosched.o
obj-$(CONFIG_IOSCHED_FIOPS) += fiops-iosched.o

 obj-$(CONFIG_BLOCK_COMPAT) += compat_ioctl.o
 obj-$(CONFIG_BLK_DEV_INTEGRITY)    += blk-integrity.o

Now build

make -j9

kernel zImage should be in /arch/arm/boot/zImage if build has succeded.

Creating Repacking boot.img

mkbootimg and unpackbootimg are the tools for extracting and repacking boot.img.

I got initial boot.img from factory images available for nexus devices.

Unpack boot.img

mkdir boot
unpackbootimg -i boot.img -o boot

Repack with new zImage

mkbootimg --kernel ../arch/arm/boot/zImage \
 --ramdisk boot/boot.img.bk-ramdisk.gz \
--cmdline "console=ttyHSL0,115200,n8 androidboot.hardware=mako lpj=67677 user_debug=31" \
--pagesize 2048 \
--base 0x80200000 \
--ramdiskaddr 0x81800000 \
--output boot.img

Board config can be found at

ramdiskaddr = base + ramdisk_offset


reboot device to bootloader

adb reboot bootloader

try booting before flashing

fastboot boot boot.img

If working then flash

fastboot flash boot boot.img


Professional Linux Kernel Architecture 9780470343432
Linux Kernel Programming 9788177589566

contact -