Skip to content

ravindu644/Android-Kernel-Tutorials

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Beginner-Friendly Guide to Compiling Your First Android Kernel..!

What You'll Learn:

  • Downloading the kernel source for your device (Samsung only)
  • Understanding the kernel root & choosing the right compilers for compilation
  • Customizing the kernel
  • Remove Samsung's anti-root protections.
  • Implementing KernelSU (to-do)
  • Creating a signed boot image from the compiled kernel

What You'll Need:

  • A working 🧠
  • Ubuntu/Debian based PC/Server
  • Knowledge of basic commands in Linux, and Bash/Shell script knowledge
  • Understanding of English.
  • Patience

Additional Notes:

  • You can also use Gitpod if you don't want to install a Linux distro.
    • Keep in mind, though, that it might be more challenging for beginners who are not familiar with the command-line interface.
    • Access the terminal from Gitpod and its GUI using ravindu644/LinuxRDP.

Dependencies for compiling kernels : (Paste this in terminal.)

sudo apt update && sudo apt install -y git device-tree-compiler lz4 xz-utils zlib1g-dev openjdk-17-jdk gcc g++ python3 python-is-python3 p7zip-full android-sdk-libsparse-utils erofs-utils \
default-jdk git gnupg flex bison gperf build-essential zip curl libc6-dev libncurses-dev libx11-dev libreadline-dev libgl1 libgl1-mesa-dev \
python3 make sudo gcc g++ bc grep tofrodos python3-markdown libxml2-utils xsltproc zlib1g-dev python-is-python3 libc6-dev libtinfo6 \
make repo cpio kmod openssl libelf-dev pahole libssl-dev libarchive-tools zstd --fix-missing && wget http://security.ubuntu.com/ubuntu/pool/universe/n/ncurses/libtinfo5_6.3-2ubuntu0.1_amd64.deb && sudo dpkg -i libtinfo5_6.3-2ubuntu0.1_amd64.deb


❗The video Guide for this tutorial can be found here : Open in Telegram

Quick Links :

  1. Downloading Part (Only for samsung)
  2. Understanding the Kernel root
  3. Understanding non-GKI & GKI kernels
  4. Compiling Part (Universal for any device).
  5. (❗ Samsung Specific) How to disable kernel securities from the menuconfig..?
  6. How to make your kernel supports with APatch..?
  7. Compilation Process.
  8. (FINAL) How to put the compiled kernel, inside our boot.img..?

✅ Downloading Part. (Only for samsung)

01. Download the kernel source from the Samsung Opensource.

02. Extract the Kernel.tar.gz from the source zip, unarchive it using this command.

tar -xvf Kernel.tar.gz && rm Kernel.tar.gz

Note: It's a good idea to give the entire kernel directory 755 permissions to remove those 🔒 from the files and folders, preventing any issues when editing files and upstreaming the kernel.

Do it this way:

chmod +755 -R /path/to/extracted/kernel/

Before:

After:

The following video demonstrates all the steps mentioned above:

🎥 Extracting Samsung's Kernel.tar.gz & granting required permissions


  • ⚠️ For other devices, You can find them by your OEM's sites or from your OEM's official GitHub repos.

✅ Understanding non-GKI & GKI kernels

+-------------------------------------+
|       Android Kernel Versions       |
+-------------------------------------+
|       non-GKI       |      GKI      |
+---------------------+---------------+
|         3.10        |      5.4      |
|         3.18        |      5.10     |
|         4.4         |      5.15     |
|         4.9         |      6.1      |
|         4.14        |               |
|         4.19        |               |
+---------------------+---------------+

✅ Understanding the Kernel root

  • As you can see in the above screenshot, it's the Linux kernel source code.

  • It must have those folders, highlighted in blue in the terminal.

  • In GKI kernels, the kernel root is located in a folder named "common".

  • If you have a GKI Samsung kernel, you should use the "common" kernel instead of "msm-kernel" for building your kernel, and the build steps are the same as for a non-GKI kernel. Refer to this repo for an idea.

✅ Compiling Part (Universal for any device).

01. After downloading or cloning the Kernel Source, we must have a build script to compile our kernel.

  • Before creating a build script, we must determine the compatible compilers we will use to build our kernel.

  • Run make kernelversion inside the kernel root to check your kernel version.

  • In my case, the kernel version is 5.4, which is GKI.

  • You can find full information about choosing the correct compiler for your kernel version here (based on my experience, btw).

    • ❗Mediatek users, read this.
  • Next, go to build_scripts, choose the appropriate script, download it, and place it inside your kernel's root directory.

  • Keep in mind that you don't need to manually download any of these since my build scripts handle everything for you :)

  • ❗If your device is Samsung Exynos, it doesn't support compiling the kernel in a separated 'out' directory. So, edit your build script like this


Notes :

  • Replace your_defconfig to your current defconfig which is located in arch/arm64/configs In GKI kernels, it's normally gki_defconfig

  • But just in case, make sure to check arch/arm64/configs or arch/arm64/configs/vendor

  • If your defconfig is located in the arch/arm64/configs directory, just replace your_defconfig with the name of your defconfig.

  • If your defconfig is located in the arch/arm64/configs/vendor directory, replace your_defconfig like this:

    • vendor/name_of_the_defconfig
    • Example patch: here

02. Edit the Makefile.

  • If you find these variables: REAL_CC or CFP_CC in your "Makefile", remove them from the "Makefile", then Search for "wrapper" in your Makefile. If there's a line related to a Python file, remove that entire line/function as well.

    • Example patch of removing the wrapper: click here
  • Search CONFIG_CC_STACKPROTECTOR_STRONG and replace it with CONFIG_CC_STACKPROTECTOR_NONE

    • Example patch of fix -fstack-protector-strong not supported by compiler: click here

03. Now, grant the executable permissions to build_xxxx.sh using this command.

chmod +x build_xxxx.sh

04. Finally, run the build script using this command :

./build_xxxx.sh

  • When you run the script for the first time, it will begin to install all the necessary dependencies and start downloading the required toolchains, depending on your kernel version.

  • Make sure not to interrupt the first run. If it gets interrupted somehow, delete the toolchains folder from "~/" and try again: rm -rf ~/toolchains

After the initial run is completed, the kernel should start building, and the "menuconfig" should appear.

  • Additional notes : Press space bar to enable/disable or enable as a module .

👇👇👇 outdated guide from here \ 2025.03.17

✅ (❗ Samsung Specific) How to disable kernel securities from the menuconfig..?


01. → Kernel Features => Disable "Enable RKP (Realtime Kernel Protection) UH feature", "Enable LKM authentication by micro hypervisor", "Block LKM by micro hypervisor", "Enable micro hypervisor feature of Samsung" respectively.

  • Image :

02. → Kernel Features → Control Flow Protection => Disable "JOP Prevention", "ROP Prevention" and "JOPP and ROPP" Respectively.

  • Image :

Additional notes :

  • If you can't find them (01 and 02) in the "→ Kernel Features", they are located in the "→ Boot options".

❗In Android 14 and some Android 13 sources, they are located in → Hypervisor. Disable them ALL!

  • Image :

  • As I mentioned at the beginning of this guide, your must use your brain..! 🧠

03. → Security options => Disable "Integrity subsystem" and "Defex Support".

  • Image :


✅ How to make your kernel supports with APatch..?


01. Open → General setup → Local version - append to kernel release => Choose any string you like.

  • Image :

02. Open → Kernel hacking => Turn on the Kernel debugging ❗.

  • Image :

03. → General setup → Configure standard kernel features (expert users) => Enable everything except "sgetmask/ssetmask syscalls support and Sysctl syscall support"

  • Image :

04. → Enable loadable module support => Enable "Forced module loading", "Module unloading", "Forced module unloading", "Module versioning support" and disable others.

  • Image :



    ❗Additional Notes : To force load the vendor/system modules in some devices, use this commit (⚠️ This fixes various hardware related issues after installing a custom kernel) - Click here

05. → Boot options => enable "Build a concatenated Image.gz/dtb by default" and "Kernel compression method (Build compressed kernel image)" ---> "(X) Build compressed kernel image"

  • Image :

06. → File systems => Enable "<*> Overlay filesystem support".

  • Image :


✅ Compilation Process.


07. Exit and Save the config.

  • When you see "configuration written", stop the compilation process with Ctrl+C and replace the content of ".config" with your desired defconfig.

08. Compile using ./build.sh --> Skip the menuconfig and wait until the compilation finishes..!

  • ℹ️ The compiled kernel will be located at out/arch/arm64/boot.

Notes:

  • If you encounter errors during the compilation process, it's advisable to search for these errors on GitHub and find a solution.

✅ (FINAL) How to put the compiled kernel, inside our boot.img..?


01. Extract the boot.img from the stock ROM/ROM ZIP. If you are a Samsung user, I prefer https://github.com/ravindu644/Scamsung to do this online.

- Use exact build number to download the firmware.

02. Unpack the boot.img using AIK-Linux which can be found in here : https://github.com/ravindu644/AIK-Linux

  • Image :


How to check "Which kernel format should I use"..?

  • Kernel without GZIP compression :

  • Kernel with GZIP compression :



Notes :

  • If your split_img has a boot.img-dtb + Uncompressed Kernel => Use "Image".
  • If your split_img has a boot.img-dtb + GZIP compressed Kernel => Use "Image.gz".
  • If your split_img don't has a boot.img-dtb + uncompressed Kernel => Use "Image-dtb". (if your out/arm64/boot folder don't have such a file, use Image instead)
  • If your split_img don't has a boot.img-dtb + GZIP compressed Kernel => Use "Image.gz-dtb".

03. Choose the required kernel as I mentioned above > Rename it to "boot.img-kernel" and copy and replace it with the boot.img-kernel, which is in the split_img folder.

04. Repack --> rename "image-new.img" to "boot.img" and make a tar file using this command :

tar cvf "DEVICE NAME (APatch Support).tar" boot.img

05. Flash it using Fastboot/ODIN..!

06. DONE..!

  • Proof :


Written by @Ravindu_Deshan for @SamsungTweaks and @APatchChannel | Sharing this without proper credit is not allowed..❗

About

Kernel building tutorials for Any device for newbies..! 🎉

Resources

License

Stars

647 stars

Watchers

15 watching

Forks

Packages

 
 
 

Contributors