Wednesday, 12 July 2023

Build Android Kernel and load your module in Emulator

Hi Guys!!!Hope you are doing well !!!. 
Today I am going to shade some light on Android kernel and module building.

In my previous 3 post I have given details on 
For above 3 post we need kernel module invstr to get build and loaded once our Emulator/device is ready.

Step 1  Download Android kernel 

Why we need to download Android Kernel separately ?
The AOSP contains only pre-built kernel binaries. When AOSP system image is build, it copies the prebuilt kernel image to the output folder. Hence we need to download and build it with our changes.

you can use android official page to download kernel  as per your choice.
I have downloaded following Android kernel . 
Android Common Kernel ---- common-android13-5.15
I have downloaded it because I am using Android 13 for my previous  3 blogs .
Note :- Different Android kernel version configured build differently

Step 2 Build the Android common kernel
Android Common Kernels is used to run on Emulator. For real hardware devices, SoC companies (MTK, Qualcomm) will provide their customized kernel and build instruction.

Android 11 introduced GKI, which separates the kernel into a Google-maintained kernel image and vendor maintained-modules, which are built separately.

List of few Android kernel branch for Emulator

Android 11
    • common-android11-5.4
Android 12
    • common-android12-5.4
    • common-android12-5.10
Android 13
    • common-android13-5.10
    • common-android13-5.15
Legacy dessert kernel branches
Legacy dessert kernels were created to guarantee that new feature development didn’t interfere with merging from the Android Common Kernel

Android 10
    • android-4.9-q
    • android-4.14-q
    • android-4.19-q
Android 11
    • android-4.14-stable
    • android-4.19-stable
Building 
Install libs for building kernel
sudo apt install libssl-dev libelf-dev
A common kernels are generic, customizable kernels and therefore don’t define a default configuration
We have to set some environment settings:
Environment variableDescriptionExample
BUILD_CONFIGBuild config file from where you initialize the build environment. The location must be defined relative to the Repo root directory. Defaults to build.config. Mandatory for common kernels.BUILD_CONFIG=common/build.config.<target>.x86_64
CCOverride compiler to be used. Falls back to the default compiler defined by build.config.CC=clang
DIST_DIRBase output directory for the kernel distribution.DIST_DIR=/path/to/my/dist
OUT_DIRBase output directory for the kernel build.OUT_DIR=/path/to/my/out
SKIP_DEFCONFIGSkip make defconfigSKIP_DEFCONFIG=1
SKIP_MRPROPERSkip make mrproperSKIP_MRPROPER=1

Build Config for Android common kernel 
I am going to build common-android13-5.15 hence using below configuration
BUILD_CONFIG=common-modules/virtual-device/build.config.cuttlefish.x86_64 
build/build.sh
Then run below command for Emulator
BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64
build/build.sh

After build complete, check the kernel file
/home/saurabh/kernel-common-android13-5.15/out/android13-5.15/dist/bzImage

If you run file command  for this bzImages yoy will get below output



Fast re-build
By default, Kernel is always built with mrproper target which removes all generated files + config + various backup files to create a clean build.
When developing on one or a few modules, we can skip the some initial steps and start re-build immediately:
BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64 
LTO=none 
FAST_BUILD=1 
SKIP_MRPROPER=1
SKIP_DEFCONFIG=1
build/build.sh
Step 3 Include custom kernel in AOSP Build System
Edit the kernel make file that generally lies in your device folder. For Emulator path is
device/generic/goldfish/x86_64-kernel.mk










Step 4 Build Complete AOSP and Run Emulator
After modifying the Kernel make file , you need to rebuild AOSP completely to get Kernel image replaced.
make -j8

After successful build Run the emulator with below Command
emulator -verbose -show-kernel -selinux permissive -writable-system

Once Emulator get launched you can check your kernel version.


In next post Will explain how to load module in kernel and build.

Thanks
Saurabh
Happy Coding!!!!

6 comments:

  1. Can you make an example for android 14 ? trying to do it but failing bad.

    ReplyDelete
  2. Happy with your example
    please visit my site for more info:Best android app development

    ReplyDelete
  3. Thank you for information. Honey Web Solutions, is the first and foremost Best Online Marketing and Website designing and web development Company based out of Tirupati, India to be qualified and certified as Google Partner Company. Honey Web Solutions is one of The Best Web Designing Company in Tirupati , Digital Marketing Company Tirupati. We offer Advanced Digital Marketing and Web Designing Services., is the first and foremost Best Online Marketing and Website designing and web development Company based out of Tirupati, India to be qualified and certified as Google Partner Company. Honey Web Solutions is one of The Best Web Designing Company in Tirupati , Digital Marketing Company Tirupati. We offer Advanced Digital Marketing and Web Designing Services.

    ReplyDelete
  4. This blog provides a comprehensive guide on building a custom kernel module for Android, offering step-by-step instructions and insights into the process. Great resource for developers diving into kernel-level development for Android devices!

    Software Development Sydney

    ReplyDelete

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...