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
- 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.
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
- ✅ Downloading Part (Only for samsung)
- ✅ Understanding the Kernel root
- ✅ Understanding non-GKI & GKI kernels
- ✅ Compiling Part (Universal for any device).
- ✅ (❗ Samsung Specific) How to disable kernel securities from the menuconfig..?
- ✅ How to make your kernel supports with APatch..?
- ✅ Compilation Process.
- ✅ (FINAL) How to put the compiled kernel, inside our boot.img..?
01. Download the kernel source from the Samsung Opensource.
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/
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.
+-------------------------------------+
| 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 | |
+---------------------+---------------+
-
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.
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 kernelversioninside 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
-
Replace
your_defconfigto your current defconfig which is located inarch/arm64/configsIn GKI kernels, it's normallygki_defconfig -
But just in case, make sure to check
arch/arm64/configsorarch/arm64/configs/vendor -
If your defconfig is located in the
arch/arm64/configsdirectory, just replaceyour_defconfigwith the name of your defconfig. -
If your defconfig is located in the
arch/arm64/configs/vendordirectory, replaceyour_defconfiglike this:vendor/name_of_the_defconfig- Example patch: here
-
If you find these variables:
REAL_CCorCFP_CCin 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_STRONGand replace it withCONFIG_CC_STACKPROTECTOR_NONE- Example patch of fix -fstack-protector-strong not supported by compiler: click here
chmod +x build_xxxx.sh
./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
toolchainsfolder 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 .
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.
02. → Kernel Features → Control Flow Protection => Disable "JOP Prevention", "ROP Prevention" and "JOPP and ROPP" Respectively.
- If you can't find them (01 and 02) in the "
→ Kernel Features", they are located in the "→ Boot options".
03. → General setup → Configure standard kernel features (expert users) => Enable everything except "sgetmask/ssetmask syscalls support and Sysctl syscall support"
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"
- When you see "
configuration written", stop the compilation process withCtrl+Cand replace the content of ".config" with your desired defconfig.
- ℹ️ The compiled kernel will be located at out/arch/arm64/boot.
- If you encounter errors during the compilation process, it's advisable to search for these errors on GitHub and find a solution.
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
- 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.
tar cvf "DEVICE NAME (APatch Support).tar" boot.img









