[Basic Tutorial]How to Build a Custom ARM64 Kernel #949
Coconutat
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
[Basic Tutorial]How to Build a Custom ARM64 Kernel
Firstly, there is a simple self narration. I am an amateur Android device player, so the things discussed here are the most basic. Experienced friends can ignore it.
Secondly, I hope to simplify this tutorial as much as possible to help beginners get through the trial and error period.
Due to the fact that most modern Android devices are based on the ARM64 architecture, this guide is specifically designed for ARM64 devices.
So, let's get started!
1.Basic preparation:
I hereby declare in advance that I will not teach you how to install Linux distributions or virtual machines. Because this is a tutorial on compiling the kernel, and installing the operating system is easy to obtain online tutorials.
2.Install Basic Dependency Environment:
I will follow Ubuntu 20.04 as an example to tell you how to do it:
Open your terminal and use the following command to install basic compilation dependencies:
If your system lacks these dependent environments, compilation will report errors. Of course, the above does not necessarily guarantee 100% that you do not lack a dependent environment at all. You need to make adjustments according to the kernel and system you want to compile.
Here is an example where the kernel of Huawei's early devices used Python 2 instead of Python 3. If you use Python 3, it will cause compilation errors.
3.Choose the appropriate kernel for the device
Firstly, you need to understand the Android version and Android kernel version of your device. This way, you can obtain the correct one from the open source code published by the manufacturer.
Because according to Google's requirements, a device can receive at least three major Android version updates.
During this period, the manufacturer may modify the kernel. So you need to know this information to obtain the correct kernel.
I will use two devices as examples:
Device 1:
With this information, you can go to Xiaomi's open-source kernel repository to download the appropriate kernel.
So the Xiaomi Open Source page will prompt you with information such as your model, Android version, and kernel branch.
By relying on these, you can filter out suitable kernels.
Device 2:
Knowing this, you can go to the Huawei open source page to download the appropriate kernel.
Huawei's open source website will prompt you with information such as model and system version for you to check the correct model.
Once you know the basic information, you can download the kernel to prepare for compilation.
4. Compilation preparation
First, you need to choose the appropriate cross-compiler.
What is a cross-compiler? See: Cross compiler
In simple terms, your computer may have an x86 architecture, while your phone has an ARM64 architecture. Binary files for different architectures are not interchangeable. You need to generate the appropriate binary files for the respective platforms.
So, let's get back to selecting the right cross-compiler.
First and foremost, it's important to note that most smartphones are currently compiled using the Clang compiler. However, some older phones may still use the GCC compiler.
Therefore, you need to find the suitable cross-compiler for your specific situation.
Unfortunately, finding the right cross-compiler can be a bit challenging, and it often requires some personal research.
Here, I can share my own experience:
Here are some cross-compilers you can consider:
Google's GCC Cross-Compiler:
Google GCC: aarch64-linux-android-4.9
When you open this page, you'll find a wide range of options. Please search according to your Android version, for example: android-q (Android 10).
Google's Clang:
Google Clang: Google Clang linux-x86
Similarly, when you open this page, you'll find various choices. Search based on your Android version, for example: android-13 (Android 13).
Recommended Third-Party Cross-Compilers:
Proton-Clang
Proton-Clang is a Clang-based cross-compiler developed by kdrag0n. For more details, refer to the project homepage.
The Linaro GCC 4.9-2017.01
Linaro GCC 4.9 is also an excellent alternative to Google GCC. For more details, visit the project homepage.
5.Start Compilation
In an appropriate empty folder, extract your kernel, and then in another empty folder, extract your cross-compiler. Finally, open the terminal in the source code root directory.
If using GCC:
Set the PATH environment variable:
XXXXX is the root directory of your cross-compiler's specific path. This command sets the PATH environment variable, and it's only effective in this terminal session. For example, if your cross-compiler path is:
Declare the name of the cross-compiler:
This command declares the name of the cross-compiler as aarch64-linux-gnu-, and please note that the - is required. The specific name depends on the cross-compiler you downloaded, so don't copy it directly.
Declare the architecture of the kernel you want to compile:
This command declares that you want to compile the kernel for ARM64 architecture.
Declare the defconfig for the kernel you want to compile:
This command generates a default configuration file for the specified kernel name (XXXXX) in the ARM64 architecture. The configuration file contains the necessary settings for compiling the kernel, such as enabled or disabled features and driver options. The defconfig file is usually located in the kernel's root directory under arch/arm64/configs/. Choose the defconfig that suits your device.
Start the compilation:
This command is used to compile the Linux kernel and simultaneously output the standard output and standard error to a file named kernel_log.log. This allows you to check for errors in case of compilation issues. The -jX parameter specifies the number of parallel compilation threads, where X should be replaced with an integer. Typically, X is set to the number of available CPU cores multiplied by 2 to maximize system resource utilization. For example, if your CPU has 4 cores, you can use -j8.
Wait for the compilation result.
There are only two possible outcomes: either successful compilation or failure. Fixing errors is beyond the scope of this tutorial, so you will need to learn how to troubleshoot them on your own.
If using Clang:
Set the PATH environment variable:
Similar to GCC, this command sets the PATH environment variable for your Clang cross-compiler. For example, if you are using Proton-Clang:
Declare the name of the cross-compiler and related variables:
This command declares Clang as the compiler and specifies the name for the cross-compiler (aarch64-linux-gnu- in this example). Please note that these specific names depend on the cross-compiler you downloaded, so don't copy them directly.
Declare the architecture of the kernel you want to compile:
This command declares that you want to compile the kernel for ARM64 architecture.
Declare the defconfig for the kernel you want to compile:
Similar to the GCC setup, this command generates a default configuration file for the specified kernel name (XXXXX) in the ARM64 architecture using Clang as the compiler.
Start the compilation:
This command compiles the Linux kernel using Clang and outputs the compilation logs to the kernel_log.log file. The -jX parameter specifies the number of parallel compilation threads, as explained earlier.
Wait for the compilation result, and as mentioned before, troubleshooting any errors is not covered in this tutorial.
6.End
We've reached the end of this guide. Kernel compilation essentially involves these few steps. This is my first attempt at creating a comprehensive tutorial, and I sincerely welcome your feedback and suggestions. If you have better solutions, please feel free to share them in the discussion forum. If there are any grammar or description errors, kindly bring them to my attention for correction.
Beta Was this translation helpful? Give feedback.
All reactions