[Android Software Development] 3lambda:
[GUIDE]A Noob Guide On Building Your Own Custom Kernel on WIN10 (ARM & ARM64 & MTK)

1. INTRODUCTION:​

This is a guide to build your own custom kernel. Although I'm still a "noob" at this,

I've struggled a lot to build one as all the guides which I followed were not very clear.

So I hope this will be clear enough and as noob friendly as possible!​

You will learn how to:

- Build a kernel for arm and mediatek devices on windows 10

- Add feature

- Basic use of git

Prerequise :

- Updated windows 10 64bits (falls creators update)

- A decent computer with a decent internet speed

- Space on your HDD The minimum space for a kernel source (and its compiled code) is about 2 GB

- Minimal linux knowledge (Terminal, Commands etc)

- Your Brain

- And finally patience

2.Setting UP ENVIRONMENT:​

Installing ubuntu :

1 - Go in Settings -> Update and Security -> For developers and turn on developers mode then

2 - go in Control Panel > Programs > Turn Windows Features On Or Off and enable Windows subsystem for linux

3 - Reboot your computer

4 - launch linux subsystem now and let it download all it need and set up your password (remember it ! you'll need this password later)

5 - Go in microsoft app store and download Ubunutu by canonical group limited

6 - Open ubuntu (a windows with your name and computer name wil appear), congrats you installed ubuntu on windows 10 !

Seting up you environment :

1 - Type "apt-get update" (will update all repo for apps and dependencies)

From here it is nearly the same as my previous guide, but be careful there is some little changes

2 - Type "sudo apt-get install -y build-essential kernel-package libncurses5-dev bzip2" (will install all dependencies to build kernel)

3 - Check if dependencies are correctly installed :

- Then type "gcc"

If "gcc" is already installed, you should see "gcc : fatal error : no input file"

- Then type "make"

If "make" is already installed, you should see "make: *** no target specified and no makefile found. stop."

- Then type "git"

If "git" is already installed, you should see bunch of basic git commands

Now you're almost ready to start building your kernel!

Toolchains:

There are several types of toolchains (GCC, Linaro and few custom made ones)

Warning : Not every single device kernel will boot (or even compiles) with older or newer GCC

- For ARM:

We'll be using GCC 4.7 in this tutorial (link : https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/ )

-Open terminal and type: "mkdir kernel"(Type the name you want, I used "kernel")

-Then type "cd kernel" (the name which you used above)

-Then type "git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7"

-Wait till it finishes.

- For ARM 64:

For ARM 64 you need a 64 bit kernel compiler (there's "aarch64" in the name for telling that's a 64 bit compiler)

Exemple : https://releases.linaro.org/archive/13.07/components/toolchain/binaries/gcc-linaro-aarch64-linux-gnu-4.8-2013.07-1_linux.tar.xz

3.DOWNLOADING SOURCE FILES FOR YOUR DEVICE:​

Now you have to find a github that contains your kernel source.

Search on Google or XDA to find a kernel github repo for your device.

A kernel github looks like this: "https://github.com/atxoxx/android_kernel_samsung_msm8974/tree/xenomTW"

On the upper left side you can see branch: completed by a name

These are the different versions of the kernel/project (generally can be for testing, android version etc)

QUICK EXPLANATION OF FILES/FOLDERS:

- /arch/arm/configs : contains the config files for device (where you add option like new governors, features etc)

- /output/arch/arm/boot/ : Where zimage is stored (will explain that later)

- build.sh : Script to make the building much easier (will explain how it works later)

- /arm-cortex-linux-gnueabi-linaro_5.2-2015.11-2 : I put the toolchain in my kernel source making it easier to find (your kernel's toolchain name may be different)

If you don't have your kernel source yet, you need to download it.

Open terminal and make sure that you are in "kernel" folder (the one you previously created)

Then type in terminal : "git clone "URL of the github kernel" -b "name of the branch" "

For Example : "git clone https://github.com/atxoxx/android_kernel_samsung_msm8974 -b xenomTW"

Good! Now you have your kernel source!

4.BUILDING:​

For an easier way you can go to the location using your file explorer to : "/home/"name of your session"/kernel"

You'll see two folders (The Toolchain and The Kernel Source)

Go into your kernel source folder.

- For ARM:

Copy paste this:

Explaination:

Example :

- For ARM 64:

Copy paste this:

Explaination:

Example :

- For Mediatek:

Copy paste this:

Explaination:

Example :

When these step are done make sure you are in kernel folder in terminal and type "sudo bash build.sh" then type your password you set up in first launch of linux subsytem

(sudo is important, windows 10 ubuntu seems to handle permission differently than native ubuntu)

The compilation have started

If it compiles without any problems:

Wait till it finishes (it'll say something like "zimage is ready")

If you followed arm and arm64:

If you followed mediatek:

Caution : Not all kernel build Zimage, it can build image or other compressed image

If in case you have any errors:

Check and see what it says, generally it'll tell you where the error is.

If the text is going too fast reduce the -j number as explained above.

For reference I compile with an AMD Phenom X4 3.4GHz,Samsung HDD and 8GB of RAM and it takes around 10min to build

It is recommanded to type in the terminal "make clean" and "make mrproper" before compiling again​

5.MAKING THE KERNEL BOOT:​

You have 2 solutions here:

1) You can use [MENTION=4544860]osm0sis[/MENTION] anykernel method, which is explainded here: "https://xdaforums.com/showthread.php?t=2670512" (A huge shoutout to him!)

OR

2) You can unpack the boot.img (from the same rom (CM, touchwizz,sense etc) and android version) and swap Zimage in it explained here : "https://xdaforums.com/showthread.php?t=2073775" (thanks again to [MENTION=4544860]osm0sis[/MENTION] !)

Before flashing the kernel which you've made, backup your "stock" boot.img and Then flash your kernel and see if it boots!

6.HOW TO ADD FEATURE TO KERNEL WORK:​

Here starts the most interesting part! Now let's see how it works:

Basically you can add: Governors, IO Schedulers, Overclock the CPU & Many Tweaks...

Checkout the github section (Section 7) to see how to add them properly.

Here's an exemple for adding a governor (this one is called Intellimm) : https://github.com/gugu0das/android...mmit/7186ee60c171b06ea3a027e8383be392d3186bb1

The text in the blue box is the commit description (generally tells you about the changelog, general information and who originally made the commit)

The other text boxes tell you about where and which files have been modified/changed.

Everything in green indicates what has been added.

Everything in red indicates what has been deleted.

We can see in the first 2 text boxes that in "arch/arm/configs/" "msm8974_sec_defconfig" and "cm_msm8974_sec_defconfig" have been modified.

Between the lines 140 and 141 of this files this text has been added : "CONFIG_CPU_FREQ_GOV_INTELLIMM=y"

(This line is for enabling Intellimm when you're compiling your kernel)

Same technique applies to the other text boxes (what has been added and deleted and it's location)

Depending on the features you add, more or less files can be modified, added or deleted.

So to sum it up, a Commit let's you see all the changes which have been made and everything else!

7.GUIDE TO GITHUB:​

For this, I'll direct you over to this awsome guide made by [MENTION=3544509]eagleeyetom[/MENTION] !

8.GPL (IMPORTANT !!!):​

THANKS :​

- [MENTION=2735826]ravish_919[/MENTION] : For testing and correcting this guide :P

- [MENTION=5150446]karkasss[/MENTION] : As my friend and support ;)

- [MENTION=7385747]gugu0das[/MENTION] : For helping me a lot when I tried to build my kernel ;)

- [MENTION=3544509]eagleeyetom[/MENTION] : For his awsome github guide :)

- [MENTION=4544860]osm0sis[/MENTION] For his aswsome anykernel solution

- [MENTION=5515628]kirito9[/MENTION] : Huge thanks to him for providing mediatek guide ! ;)

- [MENTION=4751327]F4uzan[/MENTION] : Huge thanks to him for giving me a lot of useful information to fill this guide ! ;)

- [MENTION=7060205]sunilpaulmathew[/MENTION] : For providing an advanced method to rename your kernel ! :D (again)

- [MENTION=6842057]nathanchance[/MENTION] : For a proper kernel naming method

- [MENTION=6052432]RendyAK[/MENTION] and [MENTION=6808706]DroidThug[/MENTION] : For correcting me about "#!/bin/bash" ;)

- [MENTION=7003469]ahmed.ismael[/MENTION] : For helping me, giving feedback and his huge support ! ;)

- Microsoft and canonical for the windows linux subsystem documentation

- All the developers for their hard work !

- XDA and The Community!