[GUIDE]A Noob Guide On Building Your Own Custom Kernel (ARM & ARM64 & MTK)

What does the thread need ?


  • Total voters
    41
  • Poll closed .

pvineeth97

Senior Member
Nov 1, 2015
1,251
933
0
St. Louis
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 devices (mediatek support soon thanks @kirito9 and maybe arm64)
- Add feature
- Basic use of git
I feel happy to see these guides until now! There are books available on Embedded Android. Its better to look at them for more options and completion than half cooked information.
 
Last edited:

Dlind

Senior Member
May 10, 2016
336
95
0
Port orchard, WA
Love the tutorial its simple for my noon brain to understand and I want to try this but don't have a pc, and I don't really want to make and publish kernels, I just want to use a published kernel and edit it for my own use, any advice anyone, is there a linux rom out there or some sort of app that let's me unpack boot .img files and edit them? Yeah I know the risk and I do understand custom kernels and I have been doing this since 2012.
 

3lambda

Recognized Contributor
Dec 6, 2012
1,751
1,457
133
Clermont-Ferrand
Okay guys these days i have been really busy
I'll update op + add some information that people told and requested (of course I'll credit them ;) )
Also I'll add mtk compiling support
And i need someone to tell me about arm64 compiling
I guess that's not as different as arm but i need to be sure :)
Also as mentioned do people want me to try to make a video tutorial series ? Although i have a bad English accent for that
And thanks a lot for all your feedback it'll encourage and help to improve this guide :)
Stay tuned !
 

F4uzan

Inactive Recognized Contributor
Jul 20, 2012
3,573
3,165
0
Mind if I tune in a bit and have some suggestions up and running?

- A good system specifications isn't really needed for kernel building. The only requirements that I felt is actually required are 64 bit CPU / processor and accompanied by 64 bit Linux (or heck, macOS) distribution, plus a working build tools (gcc, make, etc)
- Cross-compiler / toolchain matters a lot, please add a notice that not every single device kernel will boot (or even compiles) with older or newer GCC
- You don't have to strictly do a make clean and mrproper every time you restart a build, or you're rebuilding the kernel. It's recommended to do, but not a strict requirement.
- It'll be great if there's a notice that the dash (" - ") on the end of the CROSS_COMPILER path and variable is VERY important, if there's no dash, the compiler simply won't work
- Job parameter (-j) can be reduced, it doesn't necessarily means that if you have 4 cores, it has to be 5. Anything less is accepted (and is great for debugging), and more than 5 is also okay (but is severely not recommended)
- The minimum space for a kernel source (and its compiled code) is about 2 GB
- Not all kernel builds zImage, sometimes it builds only Image (not zImage), and sometimes other forms of compressed image

This is not all, but some that I can think of. I appreciate the effort into making the tutorial, however, and this is not a sore criticism but rather a suggestion to improve the tutorial :)

Ps. I can write more for ARM64 if you're interested
 
N

notauseranymore

Guest
I've been learning how to build and modify kernels from source too, the steps here are very similar to what is used in XDA University

I wanna contribute something to your guide too, specifically on pushing commits. Every once in a while I see a commit that goes:
Code:
Fixed build error
compared to

Code:
ALSA: pcm : Call kill_fasync() in stream lock
Currently kill_fasync() is called outside the stream lock in
snd_pcm_period_elapsed().  This is potentially racy, since the stream
may get released even during the irq handler is running.  Although
snd_pcm_release_substream() calls snd_pcm_drop(), this doesn't
guarantee that the irq handler finishes, thus the kill_fasync() call
outside the stream spin lock may be invoked after the substream is
detached, as recently reported by KASAN.

As a quick workaround, move kill_fasync() call inside the stream
lock.  The fasync is rarely used interface, so this shouldn't have a
big impact from the performance POV.

Ideally, we should implement some sync mechanism for the proper finish
of stream and irq handler.  But this oneliner should suffice for most
cases, so far.

Change-Id: I5dbc5260abe527261b4b8c4699400b317af8451e
Reported-by: Baozeng Ding <[email protected]>
Signed-off-by: Takashi Iwai <[email protected]>
A proper documentation will go a long way for both new and experienced devs, since Github is in Google's search results. That's how all of the build errors I found are solved, since my device has no devs.

Also, cherry-picking commits are the fastest way to implement features into your kernel. The code will be merged automagically and will inform you if there are conflicts.
 

DroidThug

Senior Member
Jul 6, 2015
53
101
0
Bangalore, India
droidthug.me
Great thread! Really neat and descriptive!
A small note.
1. INTRODUCTION:​
Quote:
- #!/bin/bash: In the script all lines starting with # are description (so nothing of importance here)
- export ARCH=arm: Defining which kernel architecture type it is (For example arm64 etc)
- from build.sh explanation
The #!/bin/bash is a convention so the *nix shell knows what kind of interpreter to run. Not just description/comment
 
  • Like
Reactions: 3lambda

3lambda

Recognized Contributor
Dec 6, 2012
1,751
1,457
133
Clermont-Ferrand
I would appreciate it a ton if you could show us how to download and use a different toolchain (especially linaro) to compile the kernel. You did amazing work on this guide!
Yap @F4uzan have mentionned this, will correct and explain this :)

Thanks for the awesome guide. I really wish to see a similar guide before few month so that I was able to save a lot of time :p.
Btw, few more tips for adding kernel name from my side...
1. Add the name after "EXTRAVERSION" (e.g: EXTRAVERSION = -XenomTW-3.2.6) in the 'Makefile' (line 4) which will be found in the root folder..
2. Edit the last two sections ("const char linux_banner[] =" and "const char linux_proc_banner[] =") of 'init/version.c".
e.g. Change this to

some thing like this
Super nice tips ! I'll add it and credits you for this ;) thanks

I'm keenly waiting for the mtk release, i have a mtk6582 and really need a custom kernel
It's coming stay tuned :)

Correction?

#!/bin/sh is called shebang line.
It is meant to all command is running in a shell command, not python, or either else.

Nice thread btw ;)
Thanks for the pointing out this will correct that :)

can you add some screenshots, please?
Hmm I'll try if I can't make videos :)

Mind if I tune in a bit and have some suggestions up and running?

- A good system specifications isn't really needed for kernel building. The only requirements that I felt is actually required are 64 bit CPU / processor and accompanied by 64 bit Linux (or heck, macOS) distribution, plus a working build tools (gcc, make, etc)
- Cross-compiler / toolchain matters a lot, please add a notice that not every single device kernel will boot (or even compiles) with older or newer GCC
- You don't have to strictly do a make clean and mrproper every time you restart a build, or you're rebuilding the kernel. It's recommended to do, but not a strict requirement.
- It'll be great if there's a notice that the dash (" - ") on the end of the CROSS_COMPILER path and variable is VERY important, if there's no dash, the compiler simply won't work
- Job parameter (-j) can be reduced, it doesn't necessarily means that if you have 4 cores, it has to be 5. Anything less is accepted (and is great for debugging), and more than 5 is also okay (but is severely not recommended)
- The minimum space for a kernel source (and its compiled code) is about 2 GB
- Not all kernel builds zImage, sometimes it builds only Image (not zImage), and sometimes other forms of compressed image

This is not all, but some that I can think of. I appreciate the effort into making the tutorial, however, and this is not a sore criticism but rather a suggestion to improve the tutorial :)

Ps. I can write more for ARM64 if you're interested
I sent you a PM Huge thanks for all these precision ;)

I've been learning how to build and modify kernels from source too, the steps here are very similar to what is used in XDA University

I wanna contribute something to your guide too, specifically on pushing commits. Every once in a while I see a commit that goes:
Code:
Fixed build error
compared to

Code:
ALSA: pcm : Call kill_fasync() in stream lock
Currently kill_fasync() is called outside the stream lock in
snd_pcm_period_elapsed().  This is potentially racy, since the stream
may get released even during the irq handler is running.  Although
snd_pcm_release_substream() calls snd_pcm_drop(), this doesn't
guarantee that the irq handler finishes, thus the kill_fasync() call
outside the stream spin lock may be invoked after the substream is
detached, as recently reported by KASAN.

As a quick workaround, move kill_fasync() call inside the stream
lock.  The fasync is rarely used interface, so this shouldn't have a
big impact from the performance POV.

Ideally, we should implement some sync mechanism for the proper finish
of stream and irq handler.  But this oneliner should suffice for most
cases, so far.

Change-Id: I5dbc5260abe527261b4b8c4699400b317af8451e
Reported-by: Baozeng Ding <[email protected]>
Signed-off-by: Takashi Iwai <[email protected]>
A proper documentation will go a long way for both new and experienced devs, since Github is in Google's search results. That's how all of the build errors I found are solved, since my device has no devs.

Also, cherry-picking commits are the fastest way to implement features into your kernel. The code will be merged automagically and will inform you if there are conflicts.
I'll look deeper when I'll have time :) thanks for this ;)

Great thread! Really neat and descriptive!
A small note.

- from build.sh explanation
The #!/bin/bash is a convention so the *nix shell knows what kind of interpreter to run. Not just description/comment
Yap someone pointed out this earlier, BTW thanks for pointing out this :)

I'll update op tomorrow, stay tuned !
 

Has.007

Senior Member
Sep 13, 2015
1,091
4,370
0
Curepipe
It's really nice to see everyone helping & contributing to this!

Oh lol thanks :)
Will correct that in op
No worries. ;) Also, you can add pwd to be used in toolchain's directory so the users can get the path of toolchain easily. Pwd outputs current path or directory you're working in.

I would appreciate it a ton if you could show us how to download and use a different toolchain (especially linaro) to compile the kernel. You did amazing work on this guide!
I would suggest you to go with your default compiler. You'll have to check the .txt files in your kernel source where you can see the default toolchain to be used. Other, newer or older toolchains will usually not compile the kernel nd throw errors which you'll have to fix nd it's a long work.

Thanks for the awesome guide. I really wish to see a similar guide before few month so that I was able to save a lot of time :p.
Btw, few more tips for adding kernel name from my side...
1. Add the name after "EXTRAVERSION" (e.g: EXTRAVERSION = -XenomTW-3.2.6) in the 'Makefile' (line 4) which will be found in the root folder..
2. Edit the last two sections ("const char linux_banner[] =" and "const char linux_proc_banner[] =") of 'init/version.c".
e.g. Change this to
some thing like this
True that! :p
Btw, for newbies it'll be hard to follow the syntax accurately nd it might cause errors if they misplace something.
I would rather suggest to go with mkcompile_h for Linux nd Host.
For Extraversion its fine nd easy so new peeps would be able to follow! ;)

Love the tutorial its simple for my noon brain to understand and I want to try this but don't have a pc, and I don't really want to make and publish kernels, I just want to use a published kernel and edit it for my own use, any advice anyone, is there a linux rom out there or some sort of app that let's me unpack boot .img files and edit them? Yeah I know the risk and I do understand custom kernels and I have been doing this since 2012.
No. What you can do is. Get a server nd then you can use the command line from your Android device to clone nd build kernels.
 

i'mrunningoutofusernames

Senior Member
Mar 22, 2015
239
49
0
I would suggest you to go with your default compiler. You'll have to check the .txt files in your kernel source where you can see the default toolchain to be used. Other, newer or older toolchains will usually not compile the kernel nd throw errors which you'll have to fix nd it's a long work.
Hey thank you so much for the tip! Any idea what the name of this text file would be? The kernel that i am trying to compile is here https://github.com/Team-M8/android_kernel_htc_msm8974 if you need the source to know this.
 

3lambda

Recognized Contributor
Dec 6, 2012
1,751
1,457
133
Clermont-Ferrand
Hey thank you so much for the tip! Any idea what the name of this text file would be? The kernel that i am trying to compile is here https://github.com/Team-M8/android_kernel_htc_msm8974 if you need the source to know this.
In this kernel source there isn't text file that show you the toolchain to use
Try with 4.8 or 4.9 GCC
(I'm also a m8 user although i didn't tried to build a kernel yet for this device )

Envoyé de mon m8 en utilisant Tapatalk
 

i'mrunningoutofusernames

Senior Member
Mar 22, 2015
239
49
0
In this kernel source there isn't text file that show you the toolchain to use
Try with 4.8 or 4.9 GCC
(I'm also a m8 user although i didn't tried to build a kernel yet for this device )

Envoyé de mon m8 en utilisant Tapatalk
i am currently using google's gcc 4.9 from https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9 and it works fine. i had tried using a couple of linaro toolchain versions but the compilation failed halfway with one (4.9.4) and it didnt seem to start at all with the 5.2 version (i probably didnt set it up correctly). I wanted to know which one would be supported by this kernel. I havent tried the toolchain on your github though, i will when i have more time since i am a nub at this stuff. (btw more development in m8's aosp dev scene is always appreciated wink wink)
thank you guys for all the help! :)
 

noob4598

Senior Member
Jun 26, 2015
670
316
0
34
Timmonsville
No worries. ;) Also, you can add pwd to be used in toolchain's directory so the users can get the path of toolchain easily. Pwd outputs current path or directory you're working in.


I would suggest you to go with your default compiler. You'll have to check the .txt files in your kernel source where you can see the default toolchain to be used. Other, newer or older toolchains will usually not compile the kernel nd throw errors which you'll have to fix nd it's a long work.


True that! :p
Btw, for newbies it'll be hard to follow the syntax accurately nd it might cause errors if they misplace something.
I would rather suggest to go with mkcompile_h for Linux nd Host.
For Extraversion its fine nd easy so new peeps would be able to follow! ;)


No. What you can do is. Get a server nd then you can use the command line from your Android device to clone nd build kernels.
I would love some more info on compiling a kernel with target or maybe if you know a decent place for me to start you could link it up. I have been looking for a solid guide for this for close to a year. I have termux, gnuroot, cctools, and c4droid installed with all the plugins but could never figured them out. I always have my phone with me but never my computer so it would be very convenient. I have a Tmobile note 4 and from what I have gathered it's powerful enough for the task. BTW....this is a great guide....probably the easiest to understand of any and I have read them all. Thanks