Giter VIP home page Giter VIP logo

android-kernel-tutorials's Introduction

How to Compile the Kernel for APatch for Unsupported Devices..?


What You'll Need: A Working 🧠, PC/RDP with any Linux GUI distro, Knowledge of basic commands in Linux, Understanding of English.

Additional Notes:

  • 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.

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

sudo apt update -y
sudo apt install default-jdk git-core gnupg flex bison gperf build-essential zip curl libc6-dev libncurses5-dev x11proto-core-dev libx11-dev libreadline6-dev libgl1-mesa-glx libgl1-mesa-dev python3 make sudo gcc g++ bc grep tofrodos python3-markdown libxml2-utils xsltproc zlib1g-dev libncurses5 python-is-python3 libc6-dev libtinfo5 ncurses-dev make python2 repo cpio kmod openssl libelf-dev pahole -y


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

Quick Links :

  1. βœ… Downloading Part. (Only for samsung)
  2. βœ… Compiling Part (Universal for any device).
  3. βœ… (❗ Samsung Specific) How to disable kernel securities from the menuconfig..?
  4. βœ… How to make your kernel supports with APatch..?
  5. βœ… Compilation Process.
  6. βœ… (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

Additional note : If your Kernel source's folders are locked like this, you can change the entire folder's permissions to read and write.

  • Problem :
  • Solution :

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

βœ… Compiling Part (Universal for any device).


01. After downloading or cloning the Kernel Source, We must make a Script to compile our kernel.

  • 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.

  • For an Example :

  • 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."

Notes :

  • 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.

02. Edit the Makefile.

  • if you found these variables : CROSS_COMPILE, REAL_CC or CC, 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 with CONFIG_CC_STACKPROTECTOR_NONE

Our build script must looks like this, after making the changes: (This is an example.)

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

Now we finished setting up the basic configurations for kernel compilation.

04. Now, grant the executable permissions to build.sh using this command.

chmod +x build.sh

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

./build.sh

After a couple of seconds, the "menuconfig" should appear.

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

βœ… (❗ 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..❗

android-kernel-tutorials's People

Contributors

ravindu644 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.