Building Android Kernel for the Nexus 5 — AOSP(6.0.1)

posted in: Android | 7

Useful links:
http://softwarebakery.com/building-the-android-kernel-on-linux
https://source.android.com/source/building-kernels.html
https://android.googlesource.com/kernel/msm

*** UPDATED Jan/18/2016 ***

[[ STAGE SEPARATED ]]

-In this article we are going to build a kernel and join it with the AOSP or build it standalone.
-The Kernel was not updated from android-5.1.0_r1->android-5.1.0_r3. It’s the same.
-Both Hardware and Software talk to each other seamlessly via the kernel.
-There are a few reasons you would want to build your own kernel.
1.) Adding features that are not available in stock.
b.) Yes that means editing source to your liking if that is your wish.
c.) Even merging with other master branches.
d.) Or you simply just want to make it your own.

[[ STAGE ONE ]]

2.) Install the following below:
b.) ‘build-essential‘ includes ‘dpkg-dev‘, ‘g++‘, ‘gcc‘, ‘libc6-dev‘, ‘make‘.
-Some we have and some we do not. Issue:
$ sudo apt-get install android-tools-adb android-tools-fastboot
$ sudo apt-get install build-essential abootimg git

3.) Check this page out for a low down of which kernel to build. Link
b.) Ours is for hammerhead device, binary location:device/lge/hammerhead-kernel, source location:kernel/msm, build config:hammerhead_defconfig

c.) From our previous AOSP build, the kernel was located in
workingDir/device/lge/hammerhead-kernel‘. The file is ‘zImage-dtb

d.) Checking your version of kernel: copy line below
dd if=zImage-dtb bs=1 skip=$(LC_ALL=C od -Ad -x -w2 zImage-dtb | grep 8b1f | cut -d ‘ ‘ -f1 | head -1) | zgrep -a ‘Linux version’

4.) Here is the android source kernel location. Here
b.) Let’s close in a bit. Straight to the recent release. Here
c.) As of this writing, 5.1.0_r1 was updated to 5.1.0_r3(3/25/2015) but the kernel is still the same -mr1.
d.) Remember your ‘workingDir‘ from previous AOSP build?
-Back out of it until you hit your $HOME directory. OR:
$ cd $HOME

f.) You could also grab other kernels here… say Nexus 6, Nexus 7 (grouper), Nexus 10.
g.) Grab the kernel source: Put it inside ‘kernel‘ folder.
$ git clone https://android.googlesource.com/kernel/msm \
––branch android-msm-hammerhead-3.4-marshmallow-mr1 kernel

Now we wait for it to complete.

[[ STAGE TWO ]]

1.) Downloading a prebuilt gcc.
2.) Check Google’s gcc instructions. Link
3. You are still in $HOME: Issue this:
$ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6
b.) Once its done, a new folder named ‘arm-eabi-4.6‘ would have been created.

4.) Time to add the prebuilt toolchain to our path.
a.) You can also add these 4 lines into your .bashrc file.
b.) Issue this:
$ export PATH=~/arm-eabi-4.6/bin:$PATH

c.) Then issue these three:
$ export ARCH=arm
$ export SUBARCH=arm
$ export CROSS_COMPILE=arm-eabi-

d.) If you added these files into your .bashrc, reload it. ** DOT **
$ . ~/.bashrc

5.) Building the hammerhead kernel. Build from top or jump in ‘kernel‘ and ‘make hammerhead_config‘.
$ make -C kernel hammerhead_defconfig
$ make -C kernel

b.) Now you wait.
c.) It should be like the image below when its done.
kernelmakedone
d.) The kernel should be in ‘kernel/arch/arm/boot/‘ named ‘zImage-dtb‘.

[[ STAGE THREE ]]

1.) Google Android source says:
The kernel binary is output as: `arch/arm/boot/zImage` It can be copied into the Android source tree in order to build the matching boot image.

2.) We can deal with the kernel two ways. 1) Include with new AOSP, 2) Standalone boot.img file.

b.) We include with a new AOSP build at folder: ‘workingDir/device/lge/hammerhead-kernel
c.) Make a folder(OldKernel), move your old zImage-dtb inside there ^^^ to -> ‘~/OldKernel

3.) First let’s move the old zImage-dtb. While in ‘workingDir/device/lge/hammerhead-kernel
$ mv zImage-dtb ~/OldKernel/zImage-dtbOLDER
b.) Then move the newly compiled kernel it its place. ** Notice the *space then *single dot. **
$ cp ~/kernel/arch/arm/boot/zImage-dtb .
$ chmod 775 zImage-dtb

4.) Back out to ~/workingDir
$ cd ~/workingDir

5.) Remember ‘make clean‘ from previous article? Your out/ folder will be wiped.
$ make clean << ===== THIS IS NOT NECESSARY. You can skip if you recently built aosp.
$ . build/envsetup.sh
$ lunch aosp_hammerhead-userdebug

6.) Build again – Remember N=# of cpus picked for VM.
$ make -jN

myownkernelbuild

Look at the screenshot below. Notice the kernel version: it matches my terminal.
Screenshot_2015-03-26-19-26-33

7.) The Second way is to have ‘boot.img‘ flashed via ‘fastboot‘ by itself without redoing entire AOSP build.
b.) We are going to utilize the existing Hammerhead factory images to build our new boot image.
c.) Grab factory image from Google. I’m assuming you’re in ‘workingDir‘ folder.
$ wget https://dl.google.com/dl/android/aosp/hammerhead-lmy47d-factory-6c1ad81e.tgz

d.) We are after .zip file within .tgz file.
$ tar -xzf hammerhead-lmy47d-factory-6c1ad81e.tgz
$ cd hammerhead-lmy47d
$ unzip image-hammerhead-lmy47d.zip

Change folders to the output of the AOSP Build. $ cd ~/workingDir/out/target/product/hammerhead
If you did not build aosp, just extract the boot.img from a Google Nexus Image archive. Link

e.) Now extract the files within boot.img
$ abootimg -x boot.img
– Couple files were spit out. bootimg.cfg, zImage, initrd.img.

f.) You should still be under ‘hammerhead-lmy47d‘ folder.
– Remember that kernel we built from source? Let’s pull it to our local folder. ** Notice the single dot **
$ cp ~/kernel/arch/arm/boot/zImage-dtb .

g.) Let’s edit bootimg.cfg file.
$ gedit bootimg.cfg
-See the first line? bootsize = 0x…… – Delete it and bring everything up one line.
-Save file then exit.

h.) Time to create your own myboot.img file.
$ abootimg ––create myboot.img -f bootimg.cfg -k zImage-dtb -r initrd.img
-We pulled the kernel locally, the other two files were extracted by abootimg.
-After the command above you should have a myboot.img file inside hammerhead folder folder.
bootimgcreated

8.) Boot into fastboot then try the new myboot.img file.
$ adb reboot-bootloader <—- If you were still at home screen.
$ fastboot boot myboot.img <—- Pay close attention, there is no flash mode. You’re simply booting to test myboot.img
-Simply reboot and you’re back to your old boot.img.

9.) Test some calls, send some messages, visit the web. If it looks functional then flash it.
$ fastboot flash boot myboot.img <—- Now flash makes it stick.
-This time myboot.img is permanent until you flash another.

— We are done — One or two images might not match but the point is there.
*** UPDATED Jan/18/2016 ***
————————————————-

[[ STAGE SEPARATING ]]

 

Google Apps — The Basics — Android.mk


 




7 Responses

  1. garynych

    im use :export CROSS_COMPILE=/home/garynych/android/android-ndk-r10d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-
    and after build is end, in log i see error

    WARNING: vmlinux.o(.data+0x8434): Section mismatch in reference from the variable msm_mpm_debug_mask to the function .init.text:mpm_irq_domain_linear_size()
    The variable msm_mpm_debug_mask references
    the function __init mpm_irq_domain_linear_size()
    If the reference is valid then annotate the
    variable with __init* or __refdata (see linux/init.h) or name the variable:
    *_template, *_timer, *_sht, *_ops, *_probe, *_probe_one, *_console

    WARNING: vmlinux.o(.data+0x844c): Section mismatch in reference from the variable msm_mpm_debug_mask to the function .init.text:mpm_irq_domain_legacy_size()
    The variable msm_mpm_debug_mask references
    the function __init mpm_irq_domain_legacy_size()
    If the reference is valid then annotate the
    variable with __init* or __refdata (see linux/init.h) or name the variable:
    *_template, *_timer, *_sht, *_ops, *_probe, *_probe_one, *_console

    how to fix?

  2. fireNice

    Make sure you have all those exports above in your path. Usually it’s in .bashrc so changes still stick after a reboot of the system. You can apply them then compile but as soon as you reboot you lose the paths.

  3. fireNice

    Another thing too. Try this: make -j1 or -j2

    :- if it breaks then try this:
    make -j$NUMBEROFCPUS CONFIG_NO_ERROR_ON_MISMATCH=y

  4. garynych

    this way I know it boosts these errors, and the kernel is ready 🙂
    make -j$NUMBEROFCPUS CONFIG_NO_ERROR_ON_MISMATCH=y zImage-dtb

    but I would like to do to avoid these mistakes at all…

    and if ( Try this: make-j1 or-j2) does not help

    inclined to believe that the problem in the toolchains 4.8

    but strange,I saw a lot of kernels for nexus 5, compile it
    although it may confuse those linaro kernel were made

  5. fireNice

    If you followed the tutorial to the letter then this should be working. I’ll try the tutorial on a new system and see if this holds up and builds without errors.

  6. garynych

    GCC 4.7 , no this errors and kernel ready
    GCC 4.8,4.9 have this error and error №2 Unknown or unsupported ARM architecture
    need patch for ( /kernel/arch/arm/boot/compressed/Makefile)

    ok,thanks

Leave a Reply

Your email address will not be published. Required fields are marked *