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

Search This thread

LinuxPanda

Senior Member
May 24, 2017
679
513
India
Hi there,after compiling my kernel the wifi does not work , also there is no wlan.ko being compiled.So after some research
I found that i have to install prima module in my kernel source .So where can i find these modules ? My processor is msm8937 and I am building kernel (arm)for stock nougat (7.0) ROM.Also does 32 or 64 bit( arm or arm64) matter in prima module because if it does not can i insert prima module from lineage os 15.1 source for my device which is arm64?
Thank You

You can find it in github. I think its arch independent.
 

lolvatveo

Senior Member
Nov 1, 2020
190
7
please anyone point me where i can learn/study about android kernel. i know how to download the source code but don't know these files in it and how to edit to overclock, add governor, features...
 

lolvatveo

Senior Member
Nov 1, 2020
190
7
hello sir I just find out the new reason why my phone doesn't apply new kernel feature I added. That is I don't update dt.img into boot.img for phones, most modern phones need custom dt.img when using custom kernel. The dtb files are compiled at the same time as the kernel is compiled, I need to create, merge the dtb files to dt.img, but the important thing is I need to do it successfully. And it's a big mistake that most kernel compilation forums don't mention dtb and how to make dt.img or the tool to do it! sir how to merge dtb files and create dt.img?
 

Motoepower

Member
Feb 15, 2017
6
0
So basically I was trying to add driver support for CH341,I tried to add command in kernel config as CONFIG_USB_SERIAL_CH341=y and when i try to compile its giving me erros Please check image-
457wur2um15a1.png
 

Top Liked Posts

  • There are no posts matching your filters.
  • 93
    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. :good:
    On XDA Portal:
    BLWJJxR.png


    Chapters:
    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

    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)
    v3eiir.png

    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
    wmkt1x.png

    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/downloads/xperia-open-source-archives/
    HTC: http://www.htcdev.com/
    Samsung: http://opensource.samsung.com/
    313q5oo.png

    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
    61
    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 [COLOR="DarkSlateBlue"]Source Name[/COLOR]
    Replace Source Name with the Archive name
    25umrsz.png


    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
    47
    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.
    xbebg6.png

    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:
    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:
    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:
    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 ! :D

    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
    43
    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 :D
    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)
    xth-1.png


    Wanna my help fast ? :D
    Mention me or PM me :D
    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"
    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
    32
    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.