Note : This guide is outdated af. I'm planning to write a new guide at the end of the year 2024.
Screw APatch..π€‘ Learn something new from here..β€οΈ
What You'll Need: A Working π§ , PC/RDP with any Linux GUI distro, Knowledge of basic commands in Linux, Understanding of English.
- If you're feeling a bit lazy to install a Linux distro, you can also use Gitpod. Keep in mind, though, it might be more challenging.
- I'm not sure compiled kernels will work on MediaTek devices because their kernel sources are missing some drivers for MTK.
- Also, if you have a most recent Samsung device, building should be hard because of the new GKI kernels.
sudo apt update -y
sudo apt install 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 -y
βThe video Guide for this tutorial can be found here : Open in YouTube
- β Downloading Part. (Only for samsung)
- β 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
Additional note : If your Kernel source's folders are locked like this, you can change the entire folder's permissions to read and write.
β οΈ For other devices, You can find them by your OEM's sites or from your OEM's official GitHub repos.
- Before making a build script, we must download compilers to build the kernel.
- if you own a Snapdragon/Mediatek device, I recommend you to use Google's Compilers.
- You can clone them using this command : (βonly for every Snapdragon/Mediatek devices from any OEM)
cd ~
git clone https://github.com/ravindu644/Toolchains_by_Google.git
- Or, If you prefer Qualcomm's own compilers rather than Google's, you can clone them using this command :
cd ~
git clone https://github.com/ravindu644/Toolchains_for_Snapdragon.git
- Or, If you prefer Proton clang to compile the kernel (Qualcomm and Mediatek), Jump to this Guide : Click here
Notes : (Samsung exynos specific) If your device is exynos, Open the "README_Kernel.txt" and download the toolchains by Googling the values for "CC
" and "CROSS_COMPILE
". You can find them easily from the Google Opensource or github.
-
Now, Open the terminal from your Kernel source's folder and create a new file called
build.sh
using this command :
touch build.sh
- Open the newly created
build.sh
and make that file looks like this : (β οΈ This code is only for Snapdragon and Mediatek devices and not for Exynos) - The reason for that is, Snapdragon and Mediatek kernel sources only supported to compile to a seperated directory called "out".
- Here's the code : β¬οΈ
#!/bin/bash
clear
export ARCH=arm64
export PLATFORM_VERSION=13
export ANDROID_MAJOR_VERSION=t
ln -s /usr/bin/python2.7 $HOME/python
export PATH=$HOME/:$PATH
mkdir out
ARGS='
CC=$HOME/Toolchains_by_Google/clang-10.0/bin/clang
CROSS_COMPILE=$HOME/Toolchains_by_Google/aarch64-4.9/bin/aarch64-linux-android-
CLANG_TRIPLE=aarch64-linux-gnu-
ARCH=arm64
'
make -C $(pwd) O=$(pwd)/out ${ARGS} clean && make -C $(pwd) O=$(pwd)/out ${ARGS} mrproper #to clean the source
make -C $(pwd) O=$(pwd)/out ${ARGS} YOUR_DEFCONFIG #making your current kernel config
make -C $(pwd) O=$(pwd)/out ${ARGS} menuconfig #editing the kernel config via gui
make -C $(pwd) O=$(pwd)/out ${ARGS} -j$(nproc) #to compile the kernel
#to copy all the kernel modules (.ko) to "modules" folder.
mkdir -p modules
find . -type f -name "*.ko" -exec cp -n {} modules \;
echo "Module files copied to the 'modules' folder."
- βIf your Device is Samsung Exynos, It does't supports the compiling kernel in a separate directory. So, the code must be like this :
#!/bin/bash
clear
export ARCH=arm64
export PLATFORM_VERSION=13
export ANDROID_MAJOR_VERSION=t
ln -s /usr/bin/python2.7 $HOME/python
export PATH=$HOME/:$PATH
ARGS='
CC=$HOME/path/to/clang/bin/clang
CROSS_COMPILE=$HOME/path/to/gcc/bin/aarch64-linux-android-
CLANG_TRIPLE=aarch64-linux-gnu-
ARCH=arm64
'
make ${ARGS} clean && make ${ARGS} mrproper #to clean the source
make ${ARGS} YOUR_DEFCONFIG #making your current kernel config
make ${ARGS} menuconfig #editing the kernel config via gui
make ${ARGS} -j$(nproc) #to compile the kernel
#to copy all the kernel modules (.ko) to "modules" folder.
mkdir -p modules
find . -type f -name "*.ko" -exec cp -n {} modules \;
echo "Module files copied to the 'modules' folder."
- Edit the
PLATFORM_VERSION
value to your Android version. - Edit the
ANDROID_MAJOR_VERSION
value to your Android version's codename. - Edit
CC
value with the path to the clang's [bin folder]/clang. - Edit
CROSS_COMPILE
value with the path to the GCC's [bin folder]/aarch64-linux-android- - Replace
YOUR_DEFCONFIG
to your current defconfig which is located in arch/arm64/configs.
- if you found these variables :
CROSS_COMPILE
,REAL_CC
orCC
,CFP_CC
in your "makefile" ; remove them from the "Makefile" - search "wrapper" in your makefile. If there's a line related to a python file, remove that entire line/function too.
- Search
CONFIG_CC_STACKPROTECTOR_STRONG
and replace it withCONFIG_CC_STACKPROTECTOR_NONE
03.β Bug fixing in the Kernel Source. (These are the universal errors for all the snapdragon kernel sources)
- To fix the "symbol versioning failure for gsi_write_channel_scratch" error : Click here
- Also, you will faced an error called
scripts/gcc-version.sh: line 25: aarch64-linux-gnu-gcc: command not found
if you used the Google's compilers, You can fix them too using this commit : Click here - To force load the vendor modules in some devices (Universal)
β οΈ (This fixes various hardware related issues after installing a custom kernel) - Click here
chmod +x build.sh
./build.sh
- 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
".
01. Open β General setup β Local version - append to kernel release
=> Choose any string you like.
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+C
and 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