天天看點

Building and booting Nexus 5 kernel

Date 06/05/2014 Tags ​​english​​​ / ​​master's

thesis​​​ / ​​linux​​

My description of building process will be strongly based on this article which was a great help in whole process:​​http://pete.akeo.ie/2013/10/compiling-and-running-your-own-android.html​​

Prerequisites:

  • 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 https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/

    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.      

-androideabi

$ vim run_this_android.sh      

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 run_this_android.sh
$ source run_this_android.sh      

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

$ git clone https://android.googlesource.com/kernel/msm.git

   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
     remotes/origin/android-msm-2.6.35
     remotes/origin/android-msm-3.9-usb-and-mmc-hacks
     remotes/origin/android-msm-flo-3.4-jb-mr2
     remotes/origin/android-msm-flo-3.4-kitkat-mr0
     remotes/origin/android-msm-flo-3.4-kitkat-mr1
     remotes/origin/android-msm-hammerhead-3.4-kitkat-mr1
     remotes/origin/android-msm-hammerhead-3.4-kk-fr1
     remotes/origin/android-msm-hammerhead-3.4-kk-fr2
     remotes/origin/android-msm-hammerhead-3.4-kk-r1
     remotes/origin/android-msm-mako-3.4-jb-mr1
     remotes/origin/android-msm-mako-3.4-jb-mr1-fr
     remotes/origin/android-msm-mako-3.4-jb-mr1-kgsl
     remotes/origin/android-msm-mako-3.4-jb-mr1.1
     remotes/origin/android-msm-mako-3.4-jb-mr2
     remotes/origin/android-msm-mako-3.4-kitkat-mr0
     remotes/origin/android-msm-mako-3.4-kitkat-mr1
     remotes/origin/android-msm-sony-cm-jb-3.0
     remotes/origin/master


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

General setup ---> Local version - append to kernel release

$ 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

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 run_this_android.sh. In the other case gcc will try to cross compile it for ARM architecture...

$ cd .. # if you was in msm directory
$ git clone https://github.com/pbatard/bootimg-tools.git

    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'      

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  run_this_android.sh      

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      

YOUR FLASH IS NOT TOUCHED IT'S 100% SAFE!

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! (: