Android kernel running custom kernel

There are two ways to obtain the Android kernel, one is to extract the kernel from the device, you can see my other article " Android kernel kernel extraction ", including the image file format introduction are in this article; another way It is to download the kernel source code from the official website, compile it and then flash it to the mobile phone. Here we talk about the second way.

My compilation environment is Mac10.10, android device is Nexus4, AOSP version is Android4.4.

1. Get the kernel source code

There are many ways to obtain the kernel source code, depending on who is responsible for the kernel of the device. Google provides a kernel git repository for AOSP-supported devices, and OEMs may use different methods to publish kernel source code. But no matter what, the kernel will definitely be released, because the Linux kernel follows the GPL agreement. Here we mainly talk about AOSP kernel source code.
First you go to http://source.android.com/source/building-kernels.html to find the git path of the kernel source code corresponding to the name of your device. How do you know the name of your device? Enter the following command at the command line of the device:
getprop ro.hardware
How to check the device chip model? Enter the following command on the device command line:
shell@mako:/data $ ls /dev/block/platform/                                     
msm_sdcc.1
My device is Nexus4, the device logo is mako, and the chip is msm, which is Qualcomm's chip.
In this way, the address of the kernel source code is found. Just enter the following command under your AOSP root directory:
BriansdeMacBook-Pro:Android4.4 brian$ mkdir kernel && cd $_
BriansdeMacBook-Pro:kernel brian$ git clone https://android.googlesource.com/kernel/msm
It will be done in a few minutes, but you will find that there are no files. This is because the AOSP kernel tree under the master branch is always empty. In the Git repository, the .git directory contains all the information needed to restore any working copy in the development history. And the kernel of your device is based on a certain commit version, you need to find this version and check it out. The way to find this commit version is to enter the following command in the command line of the device:
shell@mako:/data $ cat /proc/version                                           
Linux version 3.4.0-perf-ga6edd5e ([email protected]) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Apr 16 09:33:17 PDT 2014
In the above output, the 7 hexadecimal digits a6edd5e are the commit version, and then you can get the kernel source code with the command git checkout a6edd5e.

2. Compile the kernel source code

To compile the kernel source code, you must first build a compilation environment. Here it is assumed that you can already compile the AOSP source code correctly, then the basic things such as jdk and some cross-compilation tools have been installed correctly. Next, let's go to the root directory of AOSP and enter the following command:
BriansdeMacBook-Pro:Android4.4 brian$ source build/envsetup.sh 
including device/asus/deb/vendorsetup.sh
including device/asus/flo/vendorsetup.sh
including device/asus/grouper/vendorsetup.sh
including device/asus/tilapia/vendorsetup.sh
including device/generic/armv7-a-neon/vendorsetup.sh
including device/generic/mips/vendorsetup.sh
including device/generic/x86/vendorsetup.sh
including device/lge/hammerhead/vendorsetup.sh
including device/lge/mako/vendorsetup.sh
including device/samsung/manta/vendorsetup.sh
including sdk/bash_completion/adb.bash
BriansdeMacBook-Pro:Android4.4 brian$ lunch

You're building on Darwin

Lunch menu... pick a combo:
     1. aosp_arm-eng
     2. aosp_x86-eng
     3. aosp_mips-eng
     4. vbox_x86-eng
     5. aosp_deb-userdebug
     6. aosp_flo-userdebug
     7. aosp_grouper-userdebug
     8. aosp_tilapia-userdebug
     9. mini_armv7a_neon-userdebug
     10. mini_mips-userdebug
     11. mini_x86-userdebug
     12. aosp_hammerhead-userdebug
     13. aosp_mako-userdebug
     14. aosp_manta-userdebug

Which would you like? [aosp_arm-eng] aosp_mako-userdebug

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.4.4
TARGET_PRODUCT=aosp_mako
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a-neon
TARGET_CPU_VARIANT=krait
HOST_ARCH=x86
HOST_OS=darwin
HOST_OS_EXTRA=Darwin-14.5.0-x86_64-i386-64bit
HOST_BUILD_TYPE=release
BUILD_ID=KTU84P
OUT_DIR=out
============================================

The first is to execute build / envsetup.sh to initialize the compilation environment, set some environment variables, and then select the compilation target. In addition, three environment variables need to be set, otherwise an error will occur:
BriansdeMacBook-Pro:Android4.4 brian$ export CROSS_COMPILE=arm-eabi-
BriansdeMacBook-Pro:Android4.4 brian$ export SUBARCH=arm
BriansdeMacBook-Pro:Android4.4 brian$ export ARCH=arm
The arm-eabi compiler is used instead of the arm-linux-androideabi compiler. Incorrect use will cause the compilation to fail.

The kernel needs to be configured before compilation. This is because the Linux kernel supports many architectures and hardware components. In order to be able to compile the kernel image for any combination of configurations, the Linux kernel uses an extensible configuration subsystem. The Linux kernel also provides a variety of configuration interfaces for the configuration subsystem, including Opportunity QT's graphical user interface, text-based menus, and question and answer interfaces. The documentation on the Android developer site (http://source.android.com/devices/tech/config/kernel.html) describes the necessary and recommended configuration options in the Android kernel.
However, it is too much trouble to specify one by one, so the Linux kernel also provides a configuration template defconfig, which encapsulates the corresponding configuration template for different devices. You only need to specify the configuration template. Find the kernel source code in front There is a table at the beginning of the path page where you can find the configuration template corresponding to your device. After finding your configuration template, enter the root directory of the kernel source code, and then enter the following command to complete the configuration. My configuration template is mako_defconfig.
BriansdeMacBook-Pro:msm brian$ make mako_defconfig
After entering the above command, the kernel compilation system will first compile some dependencies used to process the configuration template file, then read the configuration template and write to the .config file.
Then enter the make command to complete the compilation of the kernel, get the kernel image file arch / arm / boot / zImage.

3. Make boot.img image file and install

Earlier we got the image file of the kernel, so how to flash it to the phone? If you have read the article " Kernel Extraction of Android Kernel ", you will know that the boot partition image file of the phone contains the kernel image file and the root partition image file, so we need to make a new boot.img for the kernel image we compiled The file is then swiped into the phone.
First export boot.img from your mobile phone to the computer, and then use the abootimg tool (the download address in the aforementioned article) to update our compiled kernel image file to boot.img:
BriansdeMacBook-Pro:boot brian$ abootimg -u boot.img -k zImage-new 
reading kernel from zImage-new
Writing Boot Image boot.img
If the kernel image becomes larger, abootimg will prompt a similar error "new-boot.img: update is too big for the Boot Image (4534272 vs 4505600 bytes)", at this time we need to decompress the entire boot.img file :
BriansdeMacBook-Pro:boot brian$ abootimg -x boot.img 
writing boot image config in bootimg.cfg
extracting kernel in zImage
extracting ramdisk in initrd.img
Then enter the following command:
BriansdeMacBook-Pro:boot brian$ abootimg --create new-boot.img -f bootimg.cfg -k zImage-new -r initrd.img -c "bootsize=4534272"
Just press Enter. Note that this size is the size of the entire boot.img. The usage of abootimg is not covered here, you can see the introduction on github.
At this point, the boot image file is made. To install, you first need to put the phone into fastboot mode and enter the following command:
BriansdeMacBook-Pro:boot brian$ adb reboot bootloader
The device will reboot and enter fastboot mode. There are two ways for the phone to execute the kernel we compiled, one is to start directly with the new boot.img, and the other is to write the new boot.img to the boot partition of the device. The advantage of the first method is that if it fails, it can be recovered only by restarting, but it is not persistent, and the second method is persistent.
First look at the first method, enter the command in the command line of the computer:
BriansdeMacBook-Pro:boot brian$ fastboot boot new-boot.img 

The second method, enter in the command line of the computer:
BriansdeMacBook-Pro:boot brian$ fastboot flash boot new-boot.img
BriansdeMacBook-Pro:boot brian$ fastboot reboot

Then it will restart, and then we need to verify whether it is actually swiped in, enter the command line of the phone and execute the following command:
shell@mako:/ $ cat /proc/version
Linux version 3.4.0-perf-gc7ee689-dirty ([email protected]) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Fri Nov 11 12:06:19 CST 2016
You can see it is different from the previous output.


At this point, we can successfully compile the kernel and flash it into the phone. You can add your own code to the kernel, or debug the kernel code, or even do whatever you want. Of course, you can also write the kernel module yourself, and then use insmod to load it directly into the kernel. For more in-depth content, I will introduce it later, this article is here ~.








Published 60 original articles · Like 44 · Visits 340,000+

Guess you like

Origin blog.csdn.net/beyond702/article/details/53175154
Recommended