Linux
  • Register

This article is not my work! I have copied and posted it, because the webpage was not working at that time (2020-02-21).
Original source should be: https://ataberkozen.me/how-to-port-kernel-source-for-android-device/
Copied from: http://webcache.googleusercontent.com/search?q=cache:https://ataberkozen.me/how-to-port-kernel-source-for-android-device/

Some OEMs violating GPL licenses and doesn’t publish linux kernel sources for their Android devices.
In this post I’ll explain how to port kernel source to any android device.

Tested on Snapdragon devices.

What do we need ?

  • A Computer which has Linux or Mac installation.
  • Device to test.

Let’s Start, Shell we ?

  • First of all we need to take dmesg of stock kernel using adb shell dmesg > dmesg.txt command. (I recommend you to take it while device is booting)
  • You got dmesg ? Nice. Open it with any text editor and search for “hardware” or “machine” and note the result to somewhere. By doing this we’ll get the name of device-tree-blobs.
  • Download android image kitchen and extract it to somewhere.
  • Now hold and move your device’s stock boot.img to unpackimg.(bat/sh)
  • Now your stock kernel got extracted to split_img/boot.img-zImage. After this line I will referance this file as “stock kernel”
  • Fetch split-appended-dtb using git clone https://github.com/MacTavishAO/split-appended-dtb-mac command. We’ll use this tool to extract dtb from stock kernel.
  • Copy stock kernel to split-appended-dtb-mac folder.
  • Now we will extract dtb(device-tree-blobs) from stock kernel. If you are on Linux use ./split-appended-dtb boot.img-zImage command for Mac use ./split-appended-dtb-mac boot.img-zImage command.
  • Now we have bunch of files named dtbdump_xx.dtb. We have to find out which one our device uses. Use grep -r <machine name that we found at step 2> . to find out which dtb our device using. (Write different parts of machine name to find out)
  • Install dtc using apt install device-tree-compiler command.
  • Let’s say it turns out dtbdump_21.dtb is right one. We have to decompile dtb file to dts using dtc -I dtb -O dts -o extracted.dts dtbdump_21.dtb command.
  • Now we have to extract defconfig from stock kernel.
  • Use wget https://github.com/torvalds/linux/raw/master/scripts/extract-ikconfig && chmod a+x extract-ikconfig command to download necessary tool.
  • Use ./extract-ikconfig boot.img-zImage > extracted_defconfig to get defconfig from stock kernel.
  • Now we got what we need, we need to download kernel source to start porting to our device.
  • After this point I’ll start to explain as porting to Qualcomm device but almost same on other system-on-chips.
  • Go to Settings -> About Phone -> and note “Kernel version”
  • We need to download soc manufacturer’s sources.From here find msm-X.X that matching with your phone and copy link of it.
  • And from here find tag matching your chipset and note it somewhere.
  • Use git clone <sources from 2 steps up> -b <tag>
  • Now enter to msm-X.X folder.
  • Copy extracted_defconfig to arch/<your device's architecture>/configs/ folder.
  • If your device is arm64 copy extracted.dts toarch/arm64/boot/dts/qcom/ if this directory doesn’t exist copy to arch/arm/boot/dts/qcom/ folder.
  • Open “Makefile” file in arch/arm(64)/boot/dts/qcom/ and add extracted.dts to the line matching with your chipset.
  • It’s time to import drivers. After this step I cannot help you because every devices’ hardware isn’t same but I can give some tactics.
  • To get driver names you can read dts file, install any device info app from Google Play Store etc.
  • Search these names and GitHub you can find files and commits which will help you to add drivers.
  • You did all of these without my help ? Excellent! Now it’s time to build kernel. Until I share kernel compilation guide Google is closest friend to search for how to compile android kernel.
  • After compiling kernel. Copy compiled kernel to split_img folder which mentioned at early of this tutorial. rename it to boot.img-zImage and double click repackimg.(bat.sh) and you have new file named image-new.img.
  • Boot it using fastboot boot image-new.img command and if everything is working you can use fastboot flash boot image-new.img to use it permanently.