FORUMS
Remove All Ads from XDA

[ULTIMATE GUIDE][Noob Friendly]Compile your own android kernel from source

285 posts
Thanks Meter: 623
 
Post Reply Email Thread
Hey, Guys
Today I'm going to show you a nice tutorial on how to compile your own android kernel from source, this tutorial will be a little bit focused on Sony devices as i had an experience with 'em, but that is not going to prevent that this way can be good for other manufacturers as all of them are Android based (Same programing language)
I have included screenshots and examples of codes to accompany each step of the way.
On XDA Portal:


Chapters:
Quote:

1.Installing Ubuntu (Alongside with Windows OS)
2.Downloading the source code
3.Installing libraries

4.Preparing the toolchain
5.Adding Features to the Kernel
6.Compiling
7.Trying out your kernel
8.Additional Tips

Quote:

Requirments:
Ubuntu 12.04 LTS or Newer
Manufacturer sources (Links located down for most of the manufacturers)
Linaro Toolchain
Simple background about executing Linux terminals

1.Installing Ubuntu
As we all know, Ubuntu is free open source OS that allows you to use it alongside your already installed OS without any problems, you will be promoted at every startup to choose whether your Default OS or Ubuntu OS
So, don't get freaked guys no partitions are going to be formatted or no data loss is going to happen at all.

1)Get the Ubuntu ISO
Go to Ubuntu site (Downloads section)
Choose whether you are on 32-bit or 64-bit (We'll be assuming that we are on 32-bit)

Download the ISO then burn the ISO to DVD then boot it (or you can write install your ubuntu via usb by using this tutorial

Choose "install Ubuntu alongside your windows OS version"
Manage the partitions yourself

You got your Ubuntu ready. Time for some business

2.Downloading the Source Code
Motorola: opensource.motorola.com/
LG: http://opensource.lge.com/osList/list?m=Mc001&s=Sc002
Huawei: http://emui.huawei.com/en/plugin.php?id=hwdownload
Sony: http://developer.sonymobile.com/down...urce-archives/
HTC: http://www.htcdev.com/
Samsung: http://opensource.samsung.com/

Download the TAR Source file that suits your firmware version for e.g (11.2.A.0.31)
To find your firmware version go to settings then about phone. You will see your firmware version there
The Following 87 Users Say Thank You to Eliminator79 For This Useful Post: [ View ] Gift Eliminator79 Ad-Free
 
 
8th September 2014, 05:18 PM |#2  
Eliminator79's Avatar
OP Senior Member
Flag Alexandria
Thanks Meter: 623
 
More
Reserved
3.Installing Libraries and Preparing the Environment
As you know guys there are several libraries needed for any software to terminate properly so, if you want to succeed in compiling you have to get these libraries that we're gonna write through the Linux terminal

1)Open the Linux Terminal
2)Write these commands
Code:
sudo apt-get install -y build-essential kernel-package libncurses5-dev bzip2
Code:
sudo apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0
Code:
mkdir android
3)Copy the source code archive into the folder android (that has been created throughout the “mkdir android” command
4)Run these commands
Code:
cd android
Code:
tar -xjvf Source Name
Replace Source Name with the Archive name


4.Preparing the toolchain
(You should download the whole toolchain of DoomLord, But to save our time I decided not to download the whole toolchain since that we're going to use only one folder of it)
The folder will be: arm-eabi-4.33 (Took from Linaro 4.6) (So you can consider your kernel is compiled using Linaro 4.6
Download arm-eabi-4.4.3
and extract it in the root of your home
And if you want to download the whole source run these commands
Code:
cd android
Code:
cd kernel
Code:
git clone git://github.com/DooMLoRD/android_prebuilt_toolchains.git toolchains
The Following 51 Users Say Thank You to Eliminator79 For This Useful Post: [ View ] Gift Eliminator79 Ad-Free
8th September 2014, 05:19 PM |#3  
Eliminator79's Avatar
OP Senior Member
Flag Alexandria
Thanks Meter: 623
 
More
Reserved
5.Adding Features to the Kernel

A)Automatic Way (Using the toolchain menu)
Code:
cd android
Code:
cd kernel
Code:
ARCH=arm CROSS_COMPILE=~/arm-eabi-4.4.3/bin/arm-eabi- make Device Name
Code:
make ARCH=arm CROSS_COMPILE=~/arm-eabi-4.4.3/bin/arm-eabi- menuconfig
Replace Device Name with the configuration file found at:
Source/arch/arm/configs
It's written in this way something_DeviceCodename_user_defconfig
A menu like this will appear
Choose the features you want.

B)Manual Way
1)Adding Governors
The CPU is just 1 C file (Can be found at sources of any of kernel developers)
Get the Governor file
1.1 If you cant get it through github just view it and use other stock governor file like this
my kernel has already got performance governor, you are going to take it from Kernel Source/drivers/cpufreq/cpufreq_performance.c
Copy cpufreq_performance.c from kernel source to desktop then open it with text editor
delete all the lines in it then copy the lines from the viewed governor on github and rename the whole file name

1.2
Copy the governor C file to KernelSource/drivers/cpufreq and paste it
Open Kconfig
Then Add these lines with proper replacment

Code:
config CPU_FREQ_GOV_GOVNAMEHERE
tristate "'gov_name_lowercase' cpufreq governor"
depends on CPU_FREQ
help
governor' - a custom governor!
For BadAss (For Example)

Code:
config CPU_FREQ_DEFAULT_GOV_BADASS
bool "badass"
select CPU_FREQ_GOV_BADASS
help
Use the CPUFreq governor 'BADASS' as default
Find endchoice
Add these lines below it


Code:
config CPU_FREQ_GOV_BADASS
tristate "'badass' cpufreq governor"
depends on CPU_FREQ
help
'badass' - This driver adds a dynamic cpufreq policy governor.
The governor does a periodic polling and
changes frequency based on the CPU utilization.
The support for this governor depends on CPU capability to
do fast frequency switching (i.e, very low latency frequency
transitions).

If in doubt, say N.
1.3
Open Makefile
Add the line according to the Governor
HTML Code:
obj-$(CONFIG_CPU_FREQ_GOV_BADASS) += cpufreq_badass.o
1.4

Open Kernel Source/include/linux.* now open cpufreq.h
Add these lines (With Proper Replacments)
Code:
#elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_badass)
extern struct cpufreq_governor cpufreq_gov_badass;
#define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_badass)

2)Adding I/O Schedulers
Get the I/O .c file as the same way you got the Governor File
Paste it to Kernel Source/block

2.1
Now Open Kconfig.iosched
Let's Assume that we now see SIO

HTML Code:
config IOSCHED_SIO
tristate "Simple I/O scheduler"
default y
---help---
The Simple I/O scheduler is an extremely simple scheduler,
based on noop and deadline, that relies on deadlines to
ensure fairness. The algorithm does not do any sorting but
basic merging, trying to keep a minimum overhead. It is aimed
mainly for aleatory access devices (eg: flash devices).
And we will add the I/O Scheduler VR Below it like:
We will put VR below it

HTML Code:
config IOSCHED_VR
tristate "V(R) I/O scheduler"
default y
---help---
Requests are chosen according to SSTF with a penalty of rev_penalty
for switching head direction.
Let's assume that we saw SIO (But in different way)
config DEFAULT_SIO
Code:
bool "sio" if IOSCHED_SIO=y
We will put VR below it

Code:
config DEFAULT_VR
               bool "V(R)" if IOSCHED_VR=y
Let's assume that we saw SIO (But in another different way)
Code:
default "sio" if DEFAULT_SIO
We will put VR below it
Code:
default "vr" if DEFAULT_VR
2.2
Open the Makefile at the same folder
Lets Assume that we saw SIO And we will put VR below it
Code:
obj-$(CONFIG_IOSCHED_SIO) += sio-iosched.o
Add VR Now
Code:
[obj-$(CONFIG_IOSCHED_VR) += vr-iosched.o
Done We have added the VR I/O Scheduler !

3)Overclocking
Unfortunately, overclocking can't be told the way i said the Governors and I/O Schedulers way because every device has its specific CPU frequencies
But, here is a little help
Go to KernelSource/arch/arm/mach-msm/acpuclock-XxXX.c
You will find files like these
acpuclock-7x30.c
acpuclock-8x50.c

You will see each one of them has only .c file
except one which will have .c and .o file
the one with both .c and .o files will be edited
Explanation will be added as soon as possible
The Following 44 Users Say Thank You to Eliminator79 For This Useful Post: [ View ] Gift Eliminator79 Ad-Free
8th September 2014, 05:19 PM |#4  
Eliminator79's Avatar
OP Senior Member
Flag Alexandria
Thanks Meter: 623
 
More
Last
6.Compiling
Part.A
Code:
cd android
Code:
cd kernel
Code:
export ARCH=arm
Code:
export CROSS_COMPILE=~/android/kernel/arm-eabi-4.4.3/bin/arm-eabi-
Part.B (Wanna do some modifications ?)
Code:
make <your_config_name>_defconfig
Replace <your_config_name> with the one found at Source/arch/arm/configs
Code:
make menuconfig
Code:
make -j<cpucore>
Replace <cpucore> with the number of cpu cores that the device has if you are on single core write 1 and if you are on dual core write 2[
Quad core=4
Octa core=8


Part.C
Code:
make clean
Code:
make oldconfig
Code:
make -j<cpucore>
Same as before.
The Following 31 Users Say Thank You to Eliminator79 For This Useful Post: [ View ] Gift Eliminator79 Ad-Free
8th September 2014, 05:36 PM |#5  
Eliminator79's Avatar
OP Senior Member
Flag Alexandria
Thanks Meter: 623
 
More
7.Trying out your kernel
Once you have got an output which will be kernel.elf
You have now to try it
Flash it using fastboot
Whether CMD or Androxyde Flashtool
You will have to install your fastboot drivers in both ways
CMD way
Code:
fastboot -i 0x0fce flash boot kernel.elf
Using Androxyde Flashtool (XPERIA Only)
Download it from here
Install the drivers found at (C:/Flashtool/drivers/Flashtool-drivers.exe)
Press on the thunder button above
Then Choose Fastboot mode
Connect your device in fastboot
After that choose the kernel output (.ELF)


Wanna my help fast ?
Mention me or PM me
8.Additional Tips
*To find the number of cores of your CPU, just open terminal and type the following:
Code:
cd android
cd kernel
cat /proc/cpuinfo
You will find the number of cores of CPU in a tag called "cpu cores" (Great Thanks to @#buzz)
*Some other useful packages from apt-get, especially on newer Qualcomm devices. (Might help you if you want to compile whole AOSP ROMs from source)

Code:
sudo apt-get install liblz4-dev liblz4-tool lzop device-tree-compiler
(Thanks bro @savoca)
Android & GPL
Once you have done your kernel and of course you used some parts from past kernel developers, you will also have to give your sources to other people "This which is called GPL Licence"
So, the best way for this process is "GIthub"
Quote:

The Rules as they apply on XDA

As XDA has no legal power to uphold the GPL (and frankly we want to stay as far away from doing so as possible), we can’t force any of our users to abide by the GPL. However it is in XDA’s interests as well as the interests of our developer-base to ensure all GPL-derived materials hosted or linked on XDA comply fully with the GPL.

GPL-derived materials that do not come with the complete sources used to compile the GPL components are considered warez, and will be treated as such under forum rule 6 and 9.
If you use GPL components, but do not make any modifications to them whatsoever, you should provide a link to the original source of your GPL code.
Sources accompanying a release should be complete, and contain all the necessary source code for any modules, scripts or definition files. Complete sources will be defined as those which compile correctly and completely against the platform for which the software is distributed, and which contain any and all modifications made to the released General Public Licenced code. The source code supplied should be the exact version for which the source code is being requested, complete with all modifications.

EXAMPLE: Here’s a bit of code that could be used as a template to post your releases

<Kernel Or Author Name> <Kernel Nr>:
<Source>|<ReadMe>|<Credits>|<Other>

The Very Quick Summary of General Public License (GPL)

The text of the GPL Licence itself will be used to reach any final conclusion regarding any disputes over GPL Licenced materials. The above is a summary of what XDA expects of members using GPL code, and the complete text can be read at the GNU website.



The GPL states that anyone who modifies GPL licenced code is required to make available the sources used to compile it. This is to further improve and encourage collaborative work, as well as to ensure that the best code possible is produced, and to encourage peer-review of all work. This benefits both developers and end users in numerous ways, including:

Allowing anyone to verify the code they are trusting with their data, and its authenticity
Encouraging community collaboration to produce faster fixes and updates, and better code
Helping bring new developments from other devices and fields to your own, letting you benefit from new code that wouldn’t have been available without this sharing.
The GPL imparts great freedom for GPL end users. It ensures innovation is never stifled and no project is dependent upon any single developer.

It is in everyone’s interest for the GPL to be adhered to, as it gives us all better ROMs, better transparency, and a better atmosphere for developers to work together to make great code.

Credits @TheWizardOfROMs @thewadegeek @DooMLoRD @SatrioDwiPrabowo @Haze028 @abcdjdj
XDA Universty
The Following 40 Users Say Thank You to Eliminator79 For This Useful Post: [ View ] Gift Eliminator79 Ad-Free
9th September 2014, 11:38 AM |#6  
DanielFlorin's Avatar
Senior Member
Flag Bucharest RO
Thanks Meter: 730
 
More
Good tutorial !
The Following 3 Users Say Thank You to DanielFlorin For This Useful Post: [ View ] Gift DanielFlorin Ad-Free
9th September 2014, 12:09 PM |#7  
Eliminator79's Avatar
OP Senior Member
Flag Alexandria
Thanks Meter: 623
 
More
Quote:
Originally Posted by DanielFlorin

Good tutorial !

@DanielFlorin
Thanks Bro.
10th September 2014, 05:07 AM |#8  
SatrioDwiPrabowo's Avatar
Senior Member
Flag Karawang Rock City - West Java INDONESIA
Thanks Meter: 439
 
More
Step 7 just for Sony Xperia Tamsui device like Miro , J , E , Tipo this is different with an other Xperia device so please justify and one more don't re-upload / mirror my files please use original link on my github thanks ....
The Following User Says Thank You to SatrioDwiPrabowo For This Useful Post: [ View ] Gift SatrioDwiPrabowo Ad-Free
21st September 2014, 06:22 PM |#9  
nicholaschum's Avatar
Recognized Developer / Recognized Themer
Flag University of Toronto St. George (Hometown: Hong Kong)
Thanks Meter: 13,278
 
Donate to Me
More
Truly amazing and descriptive guide. Was thinking about making my own kernel from source just the other day and now this is up!
The Following 3 Users Say Thank You to nicholaschum For This Useful Post: [ View ]
21st September 2014, 06:31 PM |#10  
vlc07's Avatar
Senior Member
Flag Haveri
Thanks Meter: 143
 
More
Nice guide well done
The Following 2 Users Say Thank You to vlc07 For This Useful Post: [ View ] Gift vlc07 Ad-Free
21st September 2014, 07:24 PM |#11  
Nikhil's Avatar
Senior Member
Flag Ahmedabad
Thanks Meter: 1,664
 
More
Hi,
Great tutorial. I'm going to try it for my device!
Kernel has more than just I/O, schedulers and governers.
Are you going to expand the tutorial gradually i.e. explain other features? (Would be great if Yes)
The Following User Says Thank You to Nikhil For This Useful Post: [ View ] Gift Nikhil Ad-Free
Post Reply Subscribe to Thread

Tags
adding features to kernel, compile kernel from source, kernel guide, sony source code

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes