[Android General] allesnietser:
About this guide

I have been building kernels for many machine types and have been fiddling around with custom android kernels for my own phone but to my great frustration the rules changed quite a lot for my new phone. It seems every kernel after 2017 is A LOT harder to build and needs quite some fiddling around te get it to boot. The documentation is terrible the least on the steps and all howtos are all outdated by now. This guide shares with you the secrets to build yourself a modern post 2017 kernel from source. I only own a op7 at this moment but I am pretty sure the rules apply to all devices.

So why do we need to build our own kernel? Well that's simple. I have scrolled through many custom kernels missing a simple module that I need. In my case binfmt. Perhaps you need a specific device driver compiled into your kernel or you want to hack in/patch in some special functions that are only needed by you. Or you love a certain kernel but need that one little adjustment in the configs that this kernel doesn’t have and so on.

Lets get through it step by step but I do assume you know something about Linux, you know how to use the terminal and that you have some knowledge on building binaries from source. Ready? Ok lets get started.

These steps are required to be followed to be successful:

1. Set up a building environment

2. Obtain the kernel source

3. Download the proper tool-chain

4. Adjust configuration files to match your needs

5. Build the actual kernel image

6. Backup your current boot.img!!!

7. Trow the image into a anykernel zip and flash it.

8. Close your eyes prey and run the kernel.

Prerequisites

You will need:

A fairly fast computer with lets say 4 gigs of RAM and a decent CPU.

A workable linux distribution that you can boot into (I prefer ubuntu bionic at this moment).

The kernel sources for your device.

ROOT!!

Lots of spare time.

A attitude that makes you unstoppable.

1. Setting up the building environment

Ok lets first open a terminal. You will need the terminal a lot. As a matter of fact it will be your friend through this guide so better get used to it. Assuming you are on a ubuntu like distribution lets fetch the important packages first

$sudo apt install build-essential bizon flex git

This command will install some of the prerequisites you need to do anything. More packages will need to be installed on the way but at least you will be able to type $make

Now lets create a working directory. Go into your home folder and create a working directory which will later on contain your kernel sources and tool chain.

$cd
$mkdir [COLOR="MediumTurquoise"]name-of-your-working-folder[/COLOR]

2. Obtain the kernel source

This one is tricky as my experience have learned me the kernel sources supplied by my own vendor are usually badly documented and contain a few bugs here and there resulting in either build errors or a unbootable kernel. So usually I grab a custom kernel that has minimal changes to the stock kernel and boots by only using a single kernel file without extra kernel modules attached. The custom kernels usually have the nasty building bugs flawed out and thus your chances for success are higher. I used the exkernel in my case and out of respect for the hard work of flang2 I bought the app that came along.

To get the sources google around and find the sources on for example git and click the download or clone button there and copy the link of the depository.

Then simply clone the depository to your working directory so after changing to your working directory type

$git clone link-you-just-copied

Git will now automatically clone the entire kernel source into the source sub-directory. You can change the name of this directory to make you life easier.

3. Download the proper tool-chain

This is where I lost many days and weeks of my life. Where do we find the proper tool-chain for our kernel?

You basically need 3 compilers

clang

GCC

device-tree-compiler

There are many versions of clang and GCC and you will need to find the one that matches your kernel and builds for your cpu architecture. I will assume in this guide that your CPU is aarch64 or arm64 (both names are used at the same time).

To obtain clang head over to the following address:

"https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/"

now in the tags and branches you can find the version of android you are looking for. If your kernel is for android 9 then use the android 9 tag. Click on it and then click tgz which will provide you with a huge tarball.

Create a folder in your working directory named toolchain en extract the contents of the tarball into the sub-folder clang.

You can try any subversion of clang there is but I recommend to simply peek into the build.config files located in the root of your kernel source directory which usually specify the exact subversion of clang used.

Time to get GCC which is easier since there is only one version right now.

Head over to

"https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/"

and use the correct version here as well. In my case I picked the latest build. Then do the same as for clang but name the sub-folder GCC or whatever you please to do.

And then finally get yourself the device-tree-compiler

Head over to

"https://github.com/dgibson/dtc/tree/v1.4.4"

and clone this depository into ~/your-working-directory/toolchain/

and navigate to the subdir you cloned it into

then build by typing

$make

any missing package errors will need you to install them using

$sudo apt install missing-package-name

and do make again until the build completes.

If all went well your tool-chain and working directory will look like this:

/[COLOR="mediumturquoise"]current-working-dir[/COLOR]/[COLOR="mediumturquoise"]kernel-source-dir[/COLOR]/
|
/toolchain/
|
/clang/
/gcc/
/dtc/

4. Adjust configuration files to match your needs

Lets first grab the correct defconfig file and setup the build directory. This defconfig file contains the default kernel configuration parameters which you propably want to adjust.

Head over to ~/working-directory/kernel-source-dir/arch/arm64/configs/

see what you can find there. I found the elementalX_defconfig file which is the file I needed to build the Exkernel from scratch. If you are trying a stock kernel dive a little bit deeper into the vendor folder and see if you can find a good config file there.

Alternatively sometimes the config is hidden on your device itself. Have a look at

/proc/config.gz

the file inside the archive contains a valid defconfig for your device but beware your may need to open it with menuconfig first and save it again to make it workable for the compiler script.

Place the defconfig file you like to use into the configs folder.

(Skip the following step if your are going to build your kernel for the first time because we first want to see it work.)

Now head over to your kernel source dir and type the following command

$PATH="~/[COLOR="mediumturquoise"]working-dir[/COLOR]/toolchain/clang/[COLOR="mediumturquoise"]clang-r353983c[/COLOR]/bin:~/[COLOR="mediumturquoise"]working-dir[/COLOR]/toolchain/gcc/a[COLOR="mediumturquoise"]arch64-linux-android-4.9[/COLOR]/bin:${PATH}" \
make menuconfig O=out \
ARCH=arm64 \
CC=clang \
CLANG_TRIPLE=aarch64-linux-gnu- \
CROSS_COMPILE=aarch64-linux-android-

now remember to use the correct paths to your tool-chain. In this command I use clang-r353983c but you may use a different version. The same applies for linux-android-4.9.

I would like to thank Nathan Chance for supplying documentation on the clang tool-chain on his github.

If all well you will be presented with a menu in which you can adjust things. First open the defconfig file you wish to use and then save the file after you are done. Now REMEMBER! With this command the menuconfig will save your new file into the /kernel-source-dir/out folder.

(end of the skipped step)

Now it is time to prepare our kernel build using the following command (do this from your kernel source dir) and remember that this command will look in the /kernel-source-dir/arch/arm64/configs/ directory

$make O=out ARCH=arm64 name-of-your-defconfig

If all goes well you will see something like configuration written to .config

Everything is up and ready to go. Your hart will start pounding from this moment on ;)

5. Build the actual kernel image

This is where things get complicated. I hope you have many years to live because this part consumes time, a lot of it.

type

$PATH="~/[COLOR="mediumturquoise"]working-dir[/COLOR]/toolchain/clang/[COLOR="mediumturquoise"]clang-r353983c[/COLOR]/bin:~/[COLOR="mediumturquoise"]working-dir[/COLOR]/toolchain/gcc/[COLOR="mediumturquoise"]aarch64-linux-android-4.9[/COLOR]/bin:${PATH}" \
make -j$(nproc --all) O=out \
ARCH=arm64 \
CC=clang \
DTC=~/[COLOR="mediumturquoise"]working-directory[/COLOR]/toolchain/dtc/dtc \
CLANG_TRIPLE=aarch64-linux-gnu- \
CROSS_COMPILE=aarch64-linux-android-

ready set GO!

This will depending on your machine take between forever and eternally. You will witness many warnings on the DTC build seems normal these days and a few warnings on the CC part. Most important is that no errors are thrown at you.

If all goes well you will see a normal exit status and you will have a “working” kernel image.

“Error?: Well that happens. Try a different build of clang check your command line. And if all that fails try to find out what is wrong in the source and that means digging through thousands of forum pages until you find out whats wrong. But using the google tools usually goes well.”

Almost there go and check

kernel-source-dir/out/arch/arm64/boot

and there should be a image-dtb or image.gz-dtb file depending on you settings.

That is your kernel image right there. The difference in size between image and image-dtb should not be huge. 10Megs in difference usually means your dtb is not good but trying is the only way to find out if it works.

6. Backup your current boot.img!!!

You know what to do here right? Do not skip this step unless you like bricked devices or want to reflash and lose your data and all that kind of stuff. Not sure what you are doing stop here or backup your entire device including system vendor etc.

7. Trow the image into a anykernel zip and flash it.

Ok something changed in the last few years. Unpacking repacking and booting using fastboot somehow gives me problems. Dm-verity errors and all kind of red screens. Signing the boot image results into new errors. Well this is how I did it.

Get yourself a anykernel zip file. I used the Exkernel.zip because it only contains a kernel image which I like. Open the zip in a good zip tool (I used ark) and replace the image-dtb file with the one you created. Place this new zip you created on a memory stick and then….

Flash it using twrp or any tool of choice.

8. Close your eyes prey and run the kernel.

Two things can happen.

1. Blank screen nothing happens. Only god can help you, repeat all the steps.

2. Your android starts booting. Start crying of joy

Check in your android if this is indeed the kernel you build. If so time to make some adjustments to your configs or happily enjoy your boosted phone.

Now please remember. If you plan to distribute your kernel that you do the correct steps of accrediting the original programmer and trow the source online. If you use a already custom kernel please respect the hard work of the maker and communicate your plans with him/her.