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
Repo
https://android.googlesource.com/kernel/msm

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 https://android.googlesource.com/kernel/msm && 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.

Repo
https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.8
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.8

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

FIOPS
https://lkml.org/lkml/2012/1/30/29
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.

https://developers.google.com/android/nexus/images

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

https://android.googlesource.com/device/lge/mako/+/master/BoardConfig.mk

ramdiskaddr = base + ramdisk_offset

Testing

reboot device to bootloader

adb reboot bootloader

try booting before flashing

fastboot boot boot.img

If working then flash

fastboot flash boot boot.img

References

https://source.android.com/source/building-kernels.html https://github.com/CyanogenMod/lge-kernel-mako

Source
https://github.com/kiriapurv/kiriyard-mako-kernel
Book ISBN
Professional Linux Kernel Architecture 9780470343432
Linux Kernel Programming 9788177589566

contact - social@apurv.me