GuidesBuilding a Custom Kernel for Android

Building a Custom Kernel for Android

Let's build a kernel!

These instructions are written for Debian Bullseye, but should be straightforward to adapt to other Linux distributions.

Installation

To create a viable kernel for Android, first install the dependencies:

1
sudo apt-get install --no-install-recommends docker.io git patch tar

Then, clone the repository:

1
2
git clone https://android.googlesource.com/kernel/goldfish 
cd goldfish/ 

The next steps depend on the version of Android you're after.

Android 7 / 8

Change to the android-goldfish-3.18 branch:

1
2
git checkout android-goldfish-3.18 
export GOLDFISH_CHECKOUT=$(realpath .)

Download this patch and save it to $GOLDFISH_CHECKOUT:

android-goldfish-3.18.patch

Apply the patch with:

1
patch -p1 < android-goldfish-3.18.patch

Then, download this defconfig and save it to $GOLDFISH_CHECKOUT/arch/arm64/configs/corellium_defconfig:

corellium_defconfig-3.18

Android 9 / 10

Change to the android-goldfish-4.4-dev branch:

1
2
git checkout android-goldfish-4.4-dev 
export GOLDFISH_CHECKOUT=$(realpath .)

Once that's done, download this tarball and save it to $GOLDFISH_CHECKOUT.

goldfish.tar.gz

Once that's saved, replace the Goldfish platform drivers:

1
2
3
rm --force --recursive drivers/platform/goldfish 
mkdir drivers/platform/goldfish 
tar --extract --directory drivers/platform/goldfish --file goldfish.tar.gz --gunzip

Next, download this defconfig and save it to $GOLDFISH_CHECKOUT/arch/arm64/configs/corellium_defconfig:

corellium_defconfig

Build the Kernel

For portability, build the kernel inside a Docker container:

1
2
3
4
5
6
7
8
mkdir goldfish-build-environment/ 
cd goldfish-build-environment/ 
cat > Dockerfile << END 
FROM ubuntu:16.04
RUN apt-get update && apt-get upgrade --assume-yes && apt-get install --assume-yes --no-install-recommends bc bison flex gcc gcc-aarch64-linux-gnu libc6-dev make 
END
sudo docker build --tag=goldfish-build-environment . 
sudo docker run --interactive --tty --mount=type=bind,source=$GOLDFISH_CHECKOUT,target=/goldfish goldfish-build-environment bash -c 'cd /goldfish/ && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make corellium_defconfig && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make'

Using the Kernel Image

Once the build is finished, a usable kernel image is found in $GOLDFISH_CHECKOUT/arch/arm64/boot/Image.

You can now tweak the kernel as you like. After you’ve made your changes, run:

1
sudo docker run --interactive --tty --mount=type=bind,source=$GOLDFISH_CHECKOUT,target=/goldfish goldfish-build-environment bash -c 'cd /goldfish/ && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make'

to rebuild the kernel with your changes.