2. Compile Linux Firmware (kernel-5.10)

2.1. Download Firefly_Linux_SDK

First prepare an empty folder to place SDK, better under home, here we use ~/proj as example.

Attention:

1. SDK uses cross-compile, so download SDK to your X86_64 PC, do not download SDK to arm64 board.

2. Build environment needs to be Ubuntu18.04 (real PC or docker container), other versions may cause build failure.

3. Do not put SDK under shared directory of VM or non-English path.

4. Please get/build SDK as a normal user, root privilege are neither allowed nor required (except installing sth. with apt)

2.1.1. Install Tools

sudo apt update
sudo apt install -y repo git python

2.1.2. Init Code Repository

  • Method One

Download via repo, you can choose to get full SDK or BSP:

mkdir ~/proj/rk3588_sdk/
cd ~/proj/rk3588_sdk/

## Full SDK
repo init --no-clone-bundle --repo-url https://gitlab.com/firefly-linux/git-repo.git -u https://gitlab.com/firefly-linux/manifests.git -b master -m rk3588_linux_release.xml

## BSP (Only include some basic repositories and compile tools)
## BSP includes device/rockchip, docs, kernel, u-boot, rkbin, tools and cross-compile toolchian
repo init --no-clone-bundle --repo-url https://gitlab.com/firefly-linux/git-repo.git -u https://gitlab.com/firefly-linux/manifests.git -b master -m rk3588_linux_bsp_release.xml
  • Method Two

Download Firefly_Linux_SDK sub-volume compressed package: Linux SDK

After downloading, verify the MD5 code:

$ md5sum rk3588_linux_release_20230114_v1.0.6c_0*
c3bcb3f92bd139f72551c89f75d39bfa  rk3588_linux_release_20230114_v1.0.6c_00
ebb658571a645d4af1e2b569709480b7  rk3588_linux_release_20230114_v1.0.6c_01
9761cc324e9f7133500b590c441b0307  rk3588_linux_release_20230114_v1.0.6c_02
7adc9fe2158d7681554dce1def238f49  rk3588_linux_release_20230114_v1.0.6c_03
3d9201e3849b8a523c05920bebe28b39  rk3588_linux_release_20230114_v1.0.6c_04
6faaee006fe60fc9be60a64a01506cb6  rk3588_linux_release_20230114_v1.0.6c_05

After confirming that it is correct, you can unzip:

mkdir -p ~/proj/rk3588_sdk
cd ~/proj/rk3588_sdk
cat path/to/rk3588_linux_release_20230114_v1.0.6c_0* | tar -xv

# export data
.repo/repo/repo sync -l

2.1.3. Sync Code

Execute the following command to synchronize the code:

# Enter the SDK root directory
cd ~/proj/rk3588_sdk

# Sync
.repo/repo/repo sync -c --no-tags
.repo/repo/repo start firefly --all

You can use the following command to update the SDK later:

.repo/repo/repo sync -c --no-tags

2.2. Linux SDK Configuration introduction

2.2.1. Directory

$ tree -L 1
.
├── app
├── buildroot # Buildroot root filesystem build directory
├── build.sh -> device/rockchip/common/build.sh # Compile script
├── device # Compile related configuration files
├── docs # Documentation
├── envsetup.sh -> buildroot/build/envsetup.sh
├── external
├── kernel
├── Makefile -> buildroot/build/Makefile
├── mkfirmware.sh -> device/rockchip/common/mkfirmware.sh # Link script
├── prebuilts # Cross compilation toolchain
├── rkbin
├── rkflash.sh -> device/rockchip/common/rkflash.sh # Flash script
├── tools # Tools directory
├── u-boot

2.2.2. Introduction to configuration files

In the device/rockchip/rk3588/ directory, there are configuration files (xxxx.mk) for different board types, which are used to manage the compilation configuration of each project of the SDK. The relevant configuration introduction:

# Target arch
export RK_ARCH=arm64
# Uboot defconfig
export RK_UBOOT_DEFCONFIG=xxxx_defconfig
# Kernel defconfig
export RK_KERNEL_DEFCONFIG=xxxx_defconfig
# Kernel defconfig fragment
export RK_KERNEL_DEFCONFIG_FRAGMENT=xxxx.config
# Kernel dts
export RK_KERNEL_DTS=aio-3588l.dts
# parameter for GPT table
export RK_PARAMETER=parameter-xxxx.txt
# rootfs image path
export RK_ROOTFS_IMG=ubuntu_rootfs/rootfs.img

2.2.3. Partition table

2.2.3.1. parameter

The parameter.txt file contains the partition information of the firmware. Take parameter-ubuntu-fit.txt as an example:

path: device/rockchip/rk3588/parameter-ubuntu-fit.txt

FIRMWARE_VER: 1.0
MACHINE_MODEL: RK3588
MACHINE_ID: 007
MANUFACTURER: RK3588
MAGIC: 0x5041524B
ATAG: 0x00200800
MACHINE: 0xffffffff
CHECK_MASK: 0x80
PWR_HLD: 0,0,A,0,1
TYPE: GPT
CMDLINE: mtdparts=rk29xxnand:0x00002000@0x00004000(uboot),0x00002000@0x00006000(misc),0x00020000@0x00008000(boot:bootable),0x00040000@0x00028000(recovery),0x00010000@0x00068000(backup),0x00c00000@0x00078000(rootfs),0x00040000@0x00c78000(oem),-@0x00cb8000(userdata:grow)
uuid:rootfs=614e0000-0000-4b53-8000-1d28000054a9

The CMDLINE attribute is where we are concerned. Take uboot as an example. In 0x00002000@0x00004000(uboot), 0x00004000 is the starting position of the uboot partition, 0x00002000 is the size of the partition, and so on.

2.2.3.2. package-file

The package-file is used to determine the required partition image and image path when packaging the firmware, and it needs to be consistent with the parameter.txt.

path: tools/linux/Linux_Pack_Firmware/rockdev/rk3588-package-file

# NAME          Relative path
#
#HWDEF          HWDEF
package-file    package-file
bootloader      Image/MiniLoaderAll.bin
parameter       Image/parameter.txt
uboot           Image/uboot.img
misc            Image/misc.img
boot            Image/boot.img
recovery        Image/recovery.img
rootfs          Image/rootfs.img
userdata        RESERVED
backup          RESERVED

2.3. Compile Ubuntu firmware

This chapter introduces the compilation process of Ubuntu firmware. It is recommended to develop under Ubuntu 18.04 system environment. If you use other system versions, you may need to adjust the compilation environment accordingly.

The compilation portion of this tutorial works with SDK versions above v1.0.6e

$ readlink -f .repo/manifest.xml
/home/daijh/p/rk3588/.repo/manifests/rk3588/rk3588_linux_release_20230301_v1.0.6e.xml

2.3.1. Preparatory work

2.3.1.1. Set up compilation environment

sudo apt-get install repo git ssh make gcc libssl-dev liblz4-tool \
expect g++ patchelf chrpath gawk texinfo chrpath diffstat binfmt-support \
qemu-user-static live-build bison flex fakeroot cmake gcc-multilib g++-multilib \
unzip \
device-tree-compiler ncurses-dev \

2.3.2. Compile SDK

2.3.2.1. Precompile Configuration

There are configuration files for different board in device/rockchip/rk3588/, select the configuration file:

./build.sh aio-3588l-ubuntu.mk

2.3.2.2. Build

2.3.2.2.1. Automatic compilation
7z x ubuntu-aarch64-rootfs.7z
mkdir ubuntu_rootfs
mv ubuntu-aarch64-rootfs.img ubuntu_rootfs/rootfs.img
  • start compiling

./build.sh

the firmware will be saved to the directory rockdev/pack/.

2.3.2.2.2. Partial compilation
  • u-boot

./build.sh uboot
  • kernel

./build.sh extboot
  • recovery

./build.sh recovery
7z x ubuntu-aarch64-rootfs.7z
mkdir ubuntu_rootfs
mv ubuntu-aarch64-rootfs.img ubuntu_rootfs/rootfs.img
  • Update each part of the .img link to the directory rockdev/:

./mkfirmware.sh
  • Pack the firmware, the firmware will be saved to the directory rockdev/pack/.

./build.sh updateimg

2.4. Compile Yocto firmware

2.4.1. Get SDK

repo init --no-clone-bundle --repo-url https://gitlab.com/firefly-linux/git-repo.git -u https://gitlab.com/firefly-linux/manifests.git -b master -m rk3588_yocto_kirkstone_release.xml
.repo/repo/repo sync -c

2.4.2. Compile

2.4.2.1. Select image

The Yocto project provides some images that can be used without layers. The following table lists currently supported build images and associated recipes.

Image name Target provided by layer
core-image-minimal A small image that only allows a device to boot Poky
core-image-minimal-xfce A XFCE minimal demo image meta-openembedded/meta-xfce
core-image-sato Image with Sato, a mobile environment and visual style for mobile devices. The image supports X11 with a Sato theme, Pimlico applications, and contains terminal, editor, and file manager Poky
core-image-weston A very basic Wayland image with a terminal Poky
core-image-x11 A very basic X11 image with a terminal Poky

2.4.3. Build image

The process of building with the bitbake command needs to ensure that the network connection is normal. If it is a customer in inland China, you need to ensure that it can ping the external network

  • Enter the directory <path/to/yocto/poky> and execute the following commands in sequence

# Install the required environment packages
# sudo apt install zstd
source oe-init-build-env

# add layer
bitbake-layers add-layer ../../meta-openembedded/meta-oe
bitbake-layers add-layer ../../meta-openembedded/meta-python
bitbake-layers add-layer ../../meta-openembedded/meta-networking
bitbake-layers add-layer ../../meta-openembedded/meta-multimedia
bitbake-layers add-layer ../../meta-openembedded/meta-gnome
bitbake-layers add-layer ../../meta-openembedded/meta-xfce
bitbake-layers add-layer ../../meta-lts-mixins
bitbake-layers add-layer ../../meta-clang
bitbake-layers add-layer ../../meta-browser/meta-chromium
bitbake-layers add-layer ../../meta-rockchip

Choose one of the commands to compile the complete core-image recipes. The following is an x11 based core-image.

MACHINE=aio-3588l bitbake core-image-minimal
MACHINE=aio-3588l bitbake core-image-minimal-xfce
MACHINE=aio-3588l bitbake core-image-x11
MACHINE=aio-3588l bitbake core-image-sato

The following is a core-image based on wayland. You need to modify DISPLAY_PLATFORM to wayland in /path/to/yocto/meta-rockchip/conf/machine/include/display.conf. Modify as follows:

DISPLAY_PLATFORM ?= "wayland"
# DISPLAY_PLATFORM ?= "x11"

After completing the above modifications, execute the command to compile core-image-weston

MACHINE=aio-3588l bitbake core-image-weston

Note: If you need to change the compiled core-image recipes after you have already compiled core-image once, you need to clean up the currently compiled core-image and then compile a new core-image.

For example: the currently compiled one is core-image-minimal. You need to change it to core-image-sato.

MACHINE=aio-3588l bitbake core-image-minimal -c clean
MACHINE=aio-3588l bitbake core-image-sato

If you want to compile some recipes separately, you can refer to the following:

# kernel
MACHINE=aio-3588l bitbake linux-rockchip
        
# u-boot
MACHINE=aio-3588l bitbake u-boot-rockchip
        
# rkmpp
MACHINE=aio-3588l bitbake rockchip-mpp
        
# rockchip-librga
MACHINE=aio-3588l bitbake rockchip-librga
        
# See more compilation objects
MACHINE=aio-3588l bitbake -s

2.4.4. Adjust compilation speed

Modify the BB_NUMBER_THREADS and PARALLEL_MAKE variables in the file /path/to/yocto/meta-rockchip/conf/machine/firefly-rk3588.conf. If the number of threads is set too large, the machine may run out of memory and cause compilation failure. Please set the compilation speed according to the configuration of the compilation machine.

BB_NUMBER_THREADS = "4"
PARALLEL_MAKE = "-j 4"
  • BB_NUMBER_THREADS: The maximum number of threads BitBake simultaneously executes.

  • BB_NUMBER_PARSE_THREADS: The number of threads BitBake uses during parsing.

  • PARALLEL_MAKE: Extra options passed to the make command during the do_compile task in order to specify parallel compilation on the local build host.

  • PARALLEL_MAKEINST: Extra options passed to the make command during the do_install task in order to specify parallel installation on the local build host.

2.4.5. More bitbake options

Fundamentally, BitBake is a generic task execution engine that allows shell and Python tasks to be run efficiently and in parallel while working within complex inter-task dependency constraints. One of BitBake’s main users, OpenEmbedded, takes this core and builds embedded Linux software stacks using a task-oriented approach.For more detailed usage, please check《bitbake-user-manual》

MACHINE=aio-3588l bitbake <target> <paramater>
# e.g
MACHINE=aio-3588l bitbake u-boot-rockchip -c clean
MACHINE=aio-3588l bitbake u-boot-rockchip
Bitbake paramater Description
-c fetch Fetches if the downloads state is not marked as done
-c clean Removes all output files for a target
-c cleanall Removes all output files, shared state cache, and downloaded source files for a target
-c compile -f It is not recommended that the source code under the temporary directory is changed directly, but if it is, the Yocto Project might not rebuild it unless this option is used. Use this option to force a recompile after the image is deployed.
-c listtasks Lists all defined tasks for a target

2.4.6. Partition firmware upgrade

The compiled firmware is located in the directory <path/to/yocto>/build/tmp/deploy/images/<board>/

$ sudo upgrade_tool di -boot boot.img
$ sudo upgrade_tool di -uboot uboot.img
$ sudo upgrade_tool di -misc misc.img
$ sudo upgrade_tool di -recovery recovery.img
  • Partition burning is suitable for debugging stage. For firmware verification, please use the unified firmware burning below.

  • Rootfs does not support separate burning. You need to pack the complete firmware before burning.

2.4.7. Unified firmware upgrade

The compiled firmware is located in the directory <path/to/yocto>/build/tmp/deploy/images/<board>/, the files to be downloaded are .wic and update.img, and after entering the loader mode, execute the following commands :

$ sudo upgrade_tool wl 0 <IMAGE NAME>.wic
$ sudo upgrade_tool uf update.img
  • The default login account of the firmware is: root, password: firefly. The firmware contains a common user account named firefly, and the password is firefly.

Note: If you are developing on a Windows PC, you can use RKdevtool to directly burn update.img, no need to burn <IMAGE NAME>.wic. However, please note that update.img is a link file, so you must select the actual file that the link file points to.

2.4.9. Introduction to Yocto Project Release layer

layer path priority(The higher the number, the higher the priority) describe
meta-oe meta-openembedded/meta-oe 6 contains a large amount of additional recipes
meta-python meta-openembedded/meta-python 7 Provide Python recipes
meta-qt5 meta-qt5 7 Provides QT5 recipes
meta-clang meta-clang 7 clang compiler
meta-rockchip meta-rockchip 9 Rockchip board level support available
meta meta 5 Contains the OpenEmbedded-Core metadata
meta-poky meta-poky 5 Holds the configuration for the Poky reference distribution
meta-yocto-bsp meta-yocto-bsp 5 Configuration for the Yocto Project reference hardware board support package.
meta-chromium meta-chromium 7 Provide chromium browser recipe

2.5. Compile Debian firmware

This chapter introduces the compilation process of Debian firmware. It is recommended to develop under Ubuntu 18.04 system environment. If you use other system versions, you may need to adjust the compilation environment accordingly.

The compilation portion of this tutorial works with SDK versions above v1.0.6e

$ readlink -f .repo/manifest.xml
/home/daijh/p/rk3588/.repo/manifests/rk3588/rk3588_linux_release_20230301_v1.0.6e.xml

2.5.1. Preparatory work

2.5.1.1. Set up compilation environment

sudo apt-get install repo git ssh make gcc libssl-dev liblz4-tool \
expect g++ patchelf chrpath gawk texinfo chrpath diffstat binfmt-support \
qemu-user-static live-build bison flex fakeroot cmake gcc-multilib g++-multilib \
unzip \
device-tree-compiler ncurses-dev \

### Compile SDK

#### Precompile Configuration

There are configuration files for different board in `device/rockchip/rk3588/`, select the configuration file:

```bash
./build.sh aio-3588l-debian.mk

2.5.1.2. Build

2.5.1.2.1. Automatic compilation
7z x debian_rk3588_rootfs_xxx.7z
mkdir debian
mv debianxx-rootfs.img debian/debian-rootfs.img
  • start compiling

./build.sh

the firmware will be saved to the directory rockdev/pack/.

2.5.1.2.2. Partial compilation
  • u-boot

./build.sh uboot
  • kernel

./build.sh extboot
  • recovery

./build.sh recovery
7z x debian_rk3588_rootfs_xxx.7z
mkdir debian
mv debianxx-rootfs.img debian/rootfs.img
  • Update each part of the .img link to the directory rockdev/:

./mkfirmware.sh
  • Pack the firmware, the firmware will be saved to the directory rockdev/pack/.

./build.sh updateimg

2.6. Compile Buildroot firmware

This chapter introduces the compilation process of Buildroot firmware. It is recommended to develop under Ubuntu 18.04 system environment. If you use other system versions, you may need to adjust the compilation environment accordingly.

The compilation portion of this tutorial works with SDK versions above v1.0.6e

$ readlink -f .repo/manifest.xml
/home/daijh/p/rk3588/.repo/manifests/rk3588/rk3588_linux_release_20230301_v1.0.6e.xml

2.6.1. Preparatory work

2.6.1.1. Set up compilation environment

sudo apt-get install repo git ssh make gcc libssl-dev liblz4-tool \
expect g++ patchelf chrpath gawk texinfo chrpath diffstat binfmt-support \
qemu-user-static live-build bison flex fakeroot cmake gcc-multilib g++-multilib \
unzip \
device-tree-compiler ncurses-dev \

2.6.2. Compile SDK

2.6.2.1. Precompile Configuration

There are configuration files for different board in device/rockchip/rk3588/, select the configuration file:

./build.sh aio-3588l-buildroot.mk

2.6.2.2. Build

2.6.2.2.1. Automatic compilation
  • start compiling

./build.sh

the firmware will be saved to the directory rockdev/pack/.

2.6.2.2.2. Partial compilation
  • u-boot

./build.sh uboot
  • kernel

./build.sh extboot
  • recovery

./build.sh recovery
  • buildroot

./build.sh rootfs
  • Update each part of the .img link to the directory rockdev/:

./mkfirmware.sh
  • Pack the firmware, the firmware will be saved to the directory rockdev/pack/.

./build.sh updateimg