Introduction to Android Kernel Development

Android Common Kernels

Android is an operating system based on Linux kernel designed primarily for touchscreen mobile devices such as smartphones and tablets.

However the linux kernel used on Android is quite different than kernel.org mainstream kernels, it uses modified version of linux kernel called as "Android Common Kernel" or ACK.

Android Common Kernels (also known as ACK or AOSP Common Kernel) is a downstream fork of kernel.org kernels with Android-specific patches from Google that aren't merged in Linux mainline or LTS (Long Term Supported) Kernels. These patches generally include:

  • Cherry-picks or backports from upstream kernel forks/mainline inorder to port/add more features.

  • Newer features/optimizations.

  • SoC Vendor/OEM specific features that are merged into ACK (eg: erofs)

  • Patches of various Android-related CVE fixes etc.

Android Mainline

Android Mainline, is the most active & primary development branch for Android features. Whenever Linus Torvalds releases a new version of the Linux kernel or an RC (Release Candidate) branch, the Linux mainline is merged into the Android mainline and is tested by Google on their Android targets. Previously, before 2019, Google used to take the latest LTS kernel as a base and then work over it to port Android-specific patches, however, this resulted in devices shipping with older kernel versions (Note that some devices released with Android 10 shipped with Linux 4.9 which was already a quite old release). Hence this changed in 2019, since then android-mainline is used to branch a new Android Common Kernel, this new model allowed OEMs to ship a lot newer & mainline kernel on their devices.

Once Google declares Android mainline upstream as a new LTS kernel, Android Partners (ie, soc vendors, ODMs & OEMs) take ACK as their base and port their hardware/software specific features into the kernel & then ship on their devices.

The kernel which is ported by Android Partners for their devices is then referred as BSP Kernel.

Android GKI

The Android GKI or also known as Android Generic Kernel Image is a project from Google that targets to address kernel fragmentation by unifying the core kernel-image across all Android targets, the SoC, OEM & Board specific drivers are moved out of core kernel-image to loadable vendor modules( or kernel objects/ *.ko files), these loadable vendor_modules are present in vendor_boot partition of an Android device. GKI presents KMI (Kernel Module Interface) for loadable vendor modules, which means that the core kernel-image & modules can be updated seperately.

However. The Android vendor-modules existed before GKI implemented too, but instead of vendor_boot partition, they were loaded from vendor/lib/modules.

There are two major versions of GKI, GKI v1 & GKI v2:

  • GKI v1: Kernel 5.4+ w/Android 11 Release

  • GKI v2: Kernel 5.10+ w/Android 12 Release

You can download GKI artifacts from https://ci.android.com/builds/branches/aosp-master/grid?

qGKI vs mGKI

qGKi stands for Qualcomm Generic Kernel Image, qGKI is developed by Qualcomm for their soc-specific features and qti perf boost enhancements, on the other hand, mGKI (also known as MediaTek Generic Kernel Image) is developed by MediaTek Inc. for their latest platforms, it consists of mtk specific features like mtk sched, mtk task_turbo etc.

Android OEMs further use qGKI/mGKIs to port it out for their devices.

We can depict the GKI Kernel & KMI Implementation using the below Figure

GKI architecture

DLKM

Dynamically Loadable Kernel Modules or DLKM are kernel modules that are present in vendor_boot, and can be dynamically loaded during device boot, as per the needs of the device.

Android Legacy kernels

The older Android release kernels are maintained by Google to provide backports of patches cited in ASB (Android Security Bulletin). They're depreciated when their support lifetime ends or the associated Linux Kernel is no longer supported by Linux.org.

Every month when Google publishes ASB (Android Security Bulletin) it lists Security issues or CVEs, the fixes of these CVEs are merged into these kernels as well but they do not receive the LTS Patches hence the minor version number never changes.

The Soc-vendor or OEM-related patches are not backported, these are backported by the Android Partners themselves.

All kernel-4.19 & older kernels fall in this category.

Android Common Kernel Hierarchy

The top level of the common kernel hierarchy is shown in Figure 1.

Creating common kernels from android-mainline kernel

Figure 1. Android Common Kernel Hierarchy (cited from source.android.com)

Compiling Android Kernel

  • Clone the kernel source from OEM open-source git repository.

  • Clone Clang and GCC compiles are per your choice.

  • You're ready to execute make $target_defconfig O=out you can further apply -j$cores arg as well.

  • Now run make again, you can find kernel image in out/arch/arm64/boot after successful compilation.

ACK - https://android.googlesource.com/kernel/common/

OEM opensource

Android Partners are liable to release kernel sources used in their products as per GPL Licensing, here are some OEM open-source git/websites.

https://github.com/OnePlusOSS

https://github.com/realme-kernel-opensource

https://github.com/oppo-source

https://github.com/nothingoss

https://github.com/micode

https://github.com/Teracube-Inc

https://opensource.samsung.com/main