How to build kernel for Android

Revision as of 07:26, 21 August 2019 by Registered User

This article explains how to build the kernel binaries (including the kernel image, the modules and the device trees). It is intended for Distribution Package users

The Android Linux Common kernel is used in the distribution package.

1. Prerequisites[edit source]

The environment must be installed using the Distribution Package adapted to your selected microprocessor device. See the list of Android Distribution Package.

To be able to execute following instructions you need to work from your distribution root directory and initialise your environment:

 source build/envsetup.sh
 bspsetup
 lunch aosp-<BoardId>-userdebug


Info white.png Information
The bspsetup command needs to be run only one time for the distribution

The distribution includes prebuilt kernel binaries used to generate the partition images (boot, dt and vendor). These binaries are available in the device/stm/<SocId>-kernel/prebuilt directory.

2. Partition layout[edit source]

The Linux kernel binary is part of the boot partition.

The Linux kernel drivers built as loadable modules (.ko files) are part of the vendor partition.

The device tree binary file is part of the dt partition.

For the complete partition layout please see STM32MP15 Flash mapping for Android.

3. Load the kernel sources[edit source]

By default, the kernel sources are not part of the STM32MPU distribution for Android. To load the sources, execute the following command:

 load_kernel

This will load the Android™ Linux common kernel sources. These can then be found in the device/stm/<SocId>-kernel/linux-<SocId> directory.

Info white.png Information

The load_kernel script uses configuration file available in the /device/stm/<SocId>-kernel/source/patch/<kernel version>/ directory.

4. Build the kernel[edit source]

After retrieving the kernel sources you can build it using:

 build_kernel

The output can be found in: out-bsp/<SocId>/KERNEL_OBJ

This command generate kernel, DT and gpu module. It is possible to generate them one by one:

 build_kernel dtb
 build_kernel gpu
 build_kernel vmlinux

For more details please check the command helper:

 build_kernel --help
Usage: build_kernel [Options] [Command]

  This script allows building the Linux kernel source

Options:
  -h/--help: print this message
  -i/--install: update prebuilt images
  -v/--version: get script version
  --verbose: enable verbosity
Command: only one command at a time supported
  dtb: build only device-tree binaries
  gpu: build only gpu module (kernel is build if not already performed)
  defaultconfig: build only .config based on default defconfig files and fragments
  menuconfig: display standard Linux kernel menuconfig interface
  modules: build only kernel modules binaries (kernel is build if not already performed)
  mrproper: execute make mrproper on targeted kernel
  vmlinux: build only kernel vmlinux binary

5. Integrate the kernel[edit source]

To integrate the new kernel, modules or/and device tree within your distribution, you have first to update the prebuilt images:

 build_kernel -i

or

 build_kernel -i dtb
 build_kernel -i modules
 build_kernel -i vmlinux

And then build the distribution to generate output images.

 make -j

Then you can update associated partitions on your device. Please refer to Flashing the built image

6. Kernel configuration[edit source]

The kernel configuration file used is merged between the following files (this operation is performed at the beginning of the kernel build process):

  • The Android configuration files named android-base.config and android-recommended.config files available in the device/stm/<SocId>-kernel/source/kconfig/<kernel version>/ directory.
  • The android-soc.config file available in the device/stm/<SocId>-kernel/source/kconfig/<kernel version>/ directory.

The kernel device tree used is set in the dt.mk file available in the device/stm/<SocId>/build/tasks/ directory. For more information please see Device tree.

For more details information on kernel configuration, please see How to customize the Linux kernel for Android