DRAFT: Kernel Compilation

Building and flashing a Linux kernel for Open Devices AOSP.

Sony link: How to build and flash a Linux Kernel for AOSP

Unlock bootloader

See Unlock Bootloader on sony.com

Download cross compiler

Linaro or clang

Clang:

git clone https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86 -b android-10.0.0_r39 clang-linux-x86

Linaro GCC: Download

gcc-arm-9.2-2019.12-mingw-w64-i686-arm-none-linux-gnueabihf.tar.xz and gcc-arm-9.2-2019.12-mingw-w64-i686-aarch64-none-linux-gnu.tar.xz from the ARM website, extract the files.

Export cross compiler

Linaro GCC:

export CROSS_COMPILE=<android-dir>/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-
export CROSS_COMPILE_ARM32=<android-dir>/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-

For clang:

export CC=clang-linux-x86/clang-r353983c1/bin
export CLANG_TRIPLE=aarch64-linux-gnu

Download kernel source

git clone https://github.com/sonyxperiadev/kernel
cd kernel

Also techpack/audio, data-kernel, wifi modules

Identify defconfig

The defconfig name is constructed using this schema: aosp_<platform>_<codename>_defconfig.

As for the codename, search the Sony Devices overview for the device Code name, e.g. kagura for the Xperia XZ.

For platform, use the “board” name, e.g. tone for Xperia XZ.

So, Xperia XZ becomes aosp_tone_kagura_defconfig.

For easier reference, here’s an overview:

Device name defconfig DTBO
Xperia X aosp_loire_suzu_defconfig false
Xperia X Compact aosp_loire_kugo_defconfig false
Xperia Touch aosp_loire_blanc_defconfig false
Xperia X Performance aosp_tone_dora_defconfig false
Xperia XZ aosp_tone_kagura_defconfig false
Xperia XZs aosp_tone_keyaki_defconfig false
Xperia XZ Premium aosp_yoshino_maple_defconfig false
Xperia XZ1 aosp_yoshino_poplar_defconfig false
Xperia XZ1 Compact aosp_yoshino_lilac_defconfig false
Xperia XA2 aosp_nile_pioneer_defconfig false
Xperia XA2 Ultra aosp_nile_discovery_defconfig false
Xperia XA2 Plus aosp_nile_voyager_defconfig false
Xperia XZ2 aosp_tama_akari_defconfig true
Xperia XZ2 Compact aosp_tama_apollo_defconfig true
Xperia XZ3 aosp_tama_akatsuki_defconfig true
Xperia 10 aosp_ganges_kirin_defconfig true
Xperia 10 Plus aosp_ganges_mermaid_defconfig true
Xperia 1 aosp_kumano_griffin_defconfig true
Xperia 5 aosp_kumano_bahamut_defconfig true
Xperia 10 II aosp_seine_pdx201_defconfig true

Configure kernel

make ARCH=arm64 CROSS_COMPILE=$CROSS_COMPILE CROSS_COMPILE_ARM32=$CROSS_COMPILE_ARM32 <device_defconfig>

For clang:

make ARCH=arm64 CC=$CC CLANG_TRIPLE=$CLANG_TRIPLE CROSS_COMPILE=$CROSS_COMPILE CROSS_COMPILE_ARM32=$CROSS_COMPILE_ARM32 <device_defconfig>

Build kernel

make \
  ARCH=arm64 \
  CROSS_COMPILE=$CROSS_COMPILE \
  CROSS_COMPILE_ARM32=$CROSS_COMPILE_ARM32 \
  -j $(nproc)

For clang:

make \
  ARCH=arm64 \
  CC=$CC \
  CLANG_TRIPLE=$CLANG_TRIPLE \
  CROSS_COMPILE=$CROSS_COMPILE \
  CROSS_COMPILE_ARM32=$CROSS_COMPILE_ARM32 \
  -j $(nproc)

Copy kernel and dtbo

Devices with appended DTB

Copy <kernel>/arch/arm64/boot/Image.gz-dtb to <android-dir>/kernel/sony/msm-4.14/common-kernel/kernel-dtb-<device>.

DTBO devices

Copy <kernel>/arch/arm64/boot/Image.gz to <android-dir>/kernel/sony/msm-4.14/common-kernel/kernel-dtb-<device>.

Then, build mkdtimg from inside your Android tree and run it:

export MKDTIMG=<android-dir>/out/host/linux-x86/bin/mkdtimg
$MKDTIMG create <android-dir>kernel/sony/msm-4.14/common-kernel/dtbo-<device>.img \
  `find <kernel-dir>/arch/arm64/boot/dts -name "*.dtbo"`

Build bootimage and dtboimage

Navigate into top-level Android directory.

source build/envsetup.sh && lunch
make -j $(nproc) bootimage

The DTBO image will be generated automatically when building boot image.

See also: Marijn’s oot script.

See also: Handling Android Images.

Download fastboot

On Linux: Install fastboot package

On Windows: Link to drivers

Flash via fastboot

Make sure the current slot is correct:

$ fastboot getvar current-slot
-> "a"

Else, set it:

fastboot set_active a

Then, flash images:

fastboot flash boot out/target/product/<device>/boot.img
# For DTBO devices:
fastboot flash dtbo out/target/product/<device>/dtbo.img

Finally:

fastboot reboot

Edit source on Github