[Android Software Development] ataberkozen:
Some OEMs violating GPL licenses and won't publish linux kernel sources for their Android devices.

On this post I’ll explain how to port kernel source to any android device.

Maybe you can port kernel sources to other SoCs with similiar ways but I've explained qcom way.

What do we need ?

A Computer which has Linux or Mac installation.

Device to test.

Let’s Start, Shall 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 the 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 write it down 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 to arch/arm64/boot/dts/qcom/ if this directory doesn’t exist copy to arch/arm/boot/dts/qcom/ folder. (If you have 3.x qcom device skip the arm64 part and copy it directly into arm)

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 or 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. I suggest you to use [MENTION=305206]natchan[/MENTION]chance 's kernel compilation guide.

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.

Leave a comment here about your questions. I'll try to reply all of them.

Planned to make a guide video about it but I don't have much time. Keep checking this thread may I post soon.