Compile linux firmware(GPT)

Foreword

The SDK development environment is on Ubuntu system. We recommend using Ubuntu 16.04 system compiles. Other Linux version may need to adjust the package accordingly. In addition to the system requirements, it also need other hardware and software requirements.

Preparatory work

Hardware requirements:

A 64 - bit system, hard disk space is more than 40 g. Multiple build, if you will need a bigger hard disk space.

Software requirements: Compiling environment initialization

  • Ubuntu 14.04 software package installation:

$ sudo apt-get install git gnupg flex bison gperf build-essential \
zip tar curl libc6-dev libncurses5-dev:i386 x11proto-core-dev \
libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 \
libgl1-mesa-dev g++-multilib mingw32 cmake tofrodos \
python-markdown libxml2-utils xsltproc zlib1g-dev:i386 lzop lib32stdc++6
$ sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so
  • Ubuntu 16.04 software package installation:

sudo apt-get install git gcc-arm-linux-gnueabihf u-boot-tools device-tree-compiler mtools \
parted libudev-dev libusb-1.0-0-dev python-linaro-image-tools linaro-image-tools libssl-dev \
autotools-dev libsigsegv2 m4 libdrm-dev curl sed make binutils build-essential gcc g++ bash \
patch gzip bzip2 perl tar cpio python unzip rsync file bc wget libncurses5 libglib2.0-dev openssh-client lib32stdc++6
  • Install the software package related to ARM cross compiling tool chain and compiling kernel

$ sudo apt-get install gcc-arm-linux-gnueabihf \
gcc-aarch64-linux-gnu device-tree-compiler lzop libncurses5-dev \
libssl1.0.0 libssl-dev

Download Firefly_Linux_SDK

  • Download

The Firefly_Linux_SDK source package is relatively large, and the Firefly_Linux_SDK source package can be obtained as follows:Download link

After downloading, verify the MD5 code:

$ md5sum firefly-sdk-20200629.7z
d8c52272725ff8a2216fc2be7a92ffc4  firefly-sdk-20200629.7z
  • Unzip

After confirming that it is correct, you can unzip:

mkdir -p ~/proj/
cd ~/proj/
7z x /path/to/firefly-sdk-20200629.7z -r -o./
cd firefly-sdk
git reset --hard

Note: Be sure to update the SDK after decompression.

Update

The SDK update includes the following two parts:

  • update rk3328-linux-bundle

#1. Enter the SDK root directory
cd ~/proj/firefly-sdk

#2. Download remote bundle repository
git clone https://github.com/FireflyTeam/bundle.git -b rk3328-linux-bundle

#3. Update, and subsequent updates do not need to pull the remote warehouse again, just execute the following command
./bundle/update rk3328-linux-bundle

#4. Follow the prompts to update the content to FETCH_HEAD, synchronize FETCH_HEAD to the firefly branch
git rebase FETCH_HEAD
  • update common-linux-bundle

Download: common-linux-bundle

#1. Download and put common-linux-bundle in the root directory of SDK
cd ~/proj/firefly-sdk
mv common-linux-bundle/ .common-linux-bundle/

#2. Update, and you can check `Change_log.txt` for subsequent updates
chmod 755 .common-linux-bundle/update && .common-linux-bundle/update
git rebase FETCH_HEAD

Linux_SDK catalog

firefly-sdk/
├── app
├── buildroot                                              The compile directory for the buildroot
├── build.sh -> device/rockchip/common/build.sh            Fully automated compiled scripts
├── debian
├── device                                                 Compile-related configuration files
├── distro
├── docs                                                   Document
├── envsetup.sh -> buildroot/build/envsetup.sh
├── external
├── kernel                                                 Kernel
├── Makefile -> buildroot/build/Makefile
├── mkfirmware.sh -> device/rockchip/common/mkfirmware.sh  Rockdev links to the update script
├── prebuilts
├── rkbin
├── rkflash.sh -> device/rockchip/common/rkflash.sh        The upgrade script
├── tools                                                  Upgrade and package tools
├── u-boot                                                 Uboot
└── yocto

Build Linux-SDK:

Precompile Configuration:

Select the Configuration file for the Board in device/rockchip/rk3328/.

This article takes the compilation of Ubuntu firmware as an example: use the roc-rk3328-cc-ubuntu.mk configuration file.

Note: To compile the Buildroot firmware, use the roc-rk3328-cc-buildroot.mk configuration file.

The configuration file uses the source command to include other files, and some configuration options are in the included files.

Related configuration introduction:

# Target arch
export RK_ARCH=arm64
# Uboot defconfig
export RK_UBOOT_DEFCONFIG=firefly-rk3328
# Kernel defconfig
export RK_KERNEL_DEFCONFIG=firefly-rk3328_defconfig
# Kernel dts
export RK_KERNEL_DTS=rk3328-roc-cc
# parameter for GPT table
export RK_PARAMETER=parameter-ubuntu.txt
# packagefile for pack image files
export RK_PACKAGE_FILE=rk3328-ubuntu-package-file
# rootfs image path
export RK_ROOTFS_IMG=ubuntu_rootfs/rk3328_ubuntu_rootfs.img

Configure Rootfs:

If you compile Buildroot firmware, skip this step. If you compile Ubuntu firmware, you need to configure it through the following steps:

Take Ubuntu 18.04 as an example

  1. Download the root file system: Ubuntu18.04 root file system and put it under the SDK path.

  2. The file is a 7z compressed package, decompress the file.

    7z x rk3328-ubuntu1804-arm64-rootfs.7z
    
  3. After completing the above, get rk3328-ubuntu1804-arm64-rootfs.img, copy the root file system to the ubuntu_rootfs directory.

    mkdir ubuntu_rootfs
    cp rk3328-ubuntu1804-arm64-rootfs.img ubuntu_rootfs/
    
  4. In device/rockchip/rk3328/firefly-rk3328-ubuntu.mk, modify “# rootfs image path” to the following configuration options or in device/rockchip/rk3328/roc-rk3328-cc-ubuntu.mk Add this configuration option.

    export RK_ROOTFS_IMG=ubuntu_rootfs/rk3328-ubuntu1804-arm64-rootfs.img
    

Compile the configuration file

./build.sh roc-rk3328-cc-ubuntu.mk

After running the above script, in the device/rockchip/ directory, generate the .BoardConfig.mk soft link device/rockchip/rk3328/roc-rk3328-cc-ubuntu.mk.

Fully compiled

./build.sh

Note: If the root file system that the user needs is Debian or Ubuntu, please prepare the corresponding root file system first. Otherwise, when executing the build.sh script, errors will occur when arranging partition images and packaging firmware. This part of the operation is explained in detail in the “Compile rootfs” in the following “Partial compilation”, just follow the operation!

Part of compilation

Compile the u-boot

./build.sh uboot

Compile the kernel

./build.sh kernle

Compile the rootfs

The SDK supports three root file systems: buildroot, Debian, Ubuntu.

  • Buildroot:

Before compiling the Buildroot file system, please select roc-rk3328-cc-buildroot.mk as the configuration file in the configuration before compilation.

Compile Buildroot environment set up depends on package installation command is as follows:

sudo apt-get install repo git-core gitk git-gui gcc-arm-linux-gnueabihf u-boot-tools device-tree-compiler \
gcc-aarch64-linux-gnu mtools parted libudev-dev libusb-1.0-0-dev python-linaro-image-tools linaro-image-tools \
autoconf autotools-dev libsigsegv2 m4 intltool libdrm-dev curl sed make binutils build-essential gcc g++ bash \
patch gzip bzip2 perl tar cpio python unzip rsync file bc wget libncurses5 libqt4-dev libglib2.0-dev libgtk2.0-dev \
libglade2-dev cvs git mercurial rsync openssh-client subversion asciidoc w3m dblatex graphviz python-matplotlib \
libc6:i386 libssl-dev texinfo liblz4-tool genext2fs expect

After the completion of set up the environment, compile buildroot, perform the following commands:

./build.sh rootfs
  • Debian:

Compile Debian environment set up depends on package installation command is as follows:

sudo apt-get install repo git-core gitk git-gui gcc-arm-linux-gnueabihf u-boot-tools device-tree-compiler \
gcc-aarch64-linux-gnu mtools parted libudev-dev libusb-1.0-0-dev python-linaro-image-tools linaro-image-tools \
gcc-4.8-multilib-arm-linux-gnueabihf gcc-arm-linux-gnueabihf libssl-dev gcc-aarch64-linux-gnu g+conf autotools-dev \
libsigsegv2 m4 intltool libdrm-dev curl sed make binutils build-essential gcc g++ bash patch gzip bzip2 perl \
tar cpio python unzip rsync file bc wget libncurses5 libqt4-dev libglib2.0-dev libgtk2.0-dev libglade2-dev cvs \
git mercurial rsync openssh-client subversion asciidoc w3m dblatex graphviz python-matplotlib libc6:i386 \
libssl-dev texinfo liblz4-tool genext2fs

After the completion of set up the environment, compile the Debian, according to own needs, perform the following commands:

cd rootfs/

## Usage for 32bit Debian
Building a base debian system by ubuntu-build-service from linaro.

    sudo apt-get install binfmt-support qemu-user-static
    sudo dpkg -i ubuntu-build-service/packages/*
    sudo apt-get install -f
    RELEASE=stretch TARGET=desktop ARCH=armhf ./mk-base-debian.sh

Building the rk-debain rootfs with debug:

    VERSION=debug ARCH=armhf ./mk-rootfs-stretch.sh

Creating the ext4 image(linaro-rootfs.img):

    ./mk-image.sh
-----------------------------------------------------------------

## Usage for 64bit Debian
Building a base debian system by ubuntu-build-service from linaro.

    sudo apt-get install binfmt-support qemu-user-static
    sudo dpkg -i ubuntu-build-service/packages/*
    sudo apt-get install -f
    RELEASE=stretch TARGET=desktop ARCH=arm64 ./mk-base-debian.sh

Building the rk-debain rootfs with debug:

    VERSION=debug ARCH=arm64 ./mk-rootfs-stretch-arm64.sh

Creating the ext4 image(linaro-rootfs.img):

    ./mk-image.sh
  • Ubuntu

  1. Download the root file system: Ubuntu18.04 root file system and put it under the SDK path.

  2. The file is a 7z compressed package, decompress the file.

    7z x rk3328-ubuntu1804-arm64-rootfs.7z
    
  3. After completing the above, get rk3328-ubuntu1804-arm64-rootfs.img, copy the root file system to the ubuntu_rootfs directory.

    mkdir ubuntu_rootfs
    cp rk3328-ubuntu1804-arm64-rootfs.img ubuntu_rootfs/
    
  4. In device/rockchip/rk3328/firefly-rk3328-ubuntu.mk, modify “# rootfs image path” to the following configuration options or in device/rockchip/rk3328/roc-rk3328-cc-ubuntu.mk Add this configuration option.

    export RK_ROOTFS_IMG=ubuntu_rootfs/rk3328-ubuntu1804-arm64-rootfs.img
    

Compile the recovery

./build.sh recovery

Supplement

Usage: build.sh [OPTIONS]
Available options:
*.mk               -switch to specified board config
uboot              -build uboot
spl                -build spl
kernel             -build kernel
modules            -build kernel modules
toolchain          -build toolchain
extboot            -build extlinux boot.img, boot from EFI partition
rootfs             -build default rootfs, currently build buildroot as default
buildroot          -build buildroot rootfs
ramboot            -build ramboot image
multi-npu_boot     -build boot image for multi-npu board
yocto              -build yocto rootfs
debian             -build debian9 stretch rootfs
distro             -build debian10 buster rootfs
pcba               -build pcba
recovery           -build recovery
all                -build uboot, kernel, rootfs, recovery image
cleanall           -clean uboot, kernel, rootfs, recovery
firmware           -pack all the image we need to boot up system
sdupdateimg        -pack sdupdate image
updateimg [-p]     -pack update image; [-p] compress
sdbootimg [-p]     -pack sdboot image; [-p] compress
otapackage         -pack ab update otapackage image
save               -save images, patches, commands used to debug
allsave            -build all & firmware & updateimg & save

Default option is 'allsave'.

Pack the firmware

The parameter partition table

The parameter.txt file contains important information about the firmware,for example:

path: device/rockchip/rk3328/parameter-ubuntu.txt

FIRMWARE_VER: 8.1
MACHINE_MODEL:rk3328
MACHINE_ID:007
MANUFACTURER:RK3288
MAGIC: 0x5041524B
ATAG: 0x00200800
MACHINE: 3288
CHECK_MASK: 0x80
PWR_HLD: 0,0,A,0,1
TYPE: GPT
CMDLINE: mtdparts=rk29xxnand:0x00002000@0x00004000(uboot),0x00002000@0x00006000(trust),0x00002000@0x00008000(misc),0x00010000@0x0000a000(boot),0x00010000@0x0001a000(recovery),0x00010000@0x0002a000(backup),0x00600000@0x0005a000(rootfs),-@0x0065a000(userdata:grow)
uuid:rootfs=614e0000-0000-4b53-8000-1d28000054a9

The CMDLINE attribute is where we are concerned, using uboot as an example 0x00002000@0x00004000(uboot) as the starting position of the uboot partition 0x00002000 as the size of the partition, followed by the same. Users can add or subtract partition information according to their needs, but please keep at least the uboot,trust,boot and rootfs partition, which is the prerequisite for the machine to start normally.

  • Partition:

uboot : flashing uboot.img.
trust : flashing trust.img.
misc(omissible): flashing misc.img,responsible for starting the mode switch and first aid mode parameter transfer.
boot  : flashing boot.img,contain kernel.img and resource.img
recovery(omissible): flashing recovery.img.recovery mode image.
backup : Reserved
oem(omissible) : flashing oem.img,Used by the manufacturer to store the app or data. Read only. Mounted in the /oem directory.
rootfs : flashing rootfs.img.
userdata(omissible) : flashing userdata.img mount /userdata directory.

In the parameter.txt file, only five indispensable partitions are kept.

Note:If find rootfs partition size exceptional, execute the following command:

resize2fs /dev/mmcblk2p5

package-file

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

for example:path:tools/linux/Linux_Pack_Firmware/rockdev/rk3328-ubuntu-package-file

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

Pack

  • Arrange the partition image to the rockdev/ directory

./mkfirmware

Before each package firmware, ensure rockdev/ directory file link is correct:

$ tree -L 1 rockdev/
rockdev/
├── boot.img -> ../kernel/boot.img
├── MiniLoaderAll.bin -> ../u-boot/rk3328_loader_v1.16.250.bin
├── misc.img -> ../device/rockchip/rockimg/wipe_all-misc.img
├── oem.img
├── pack
├── parameter.txt -> ../device/rockchip/rk3328/parameter-buildroot.txt
├── recovery.img -> ../buildroot/output/rockchip_rk3328_recovery/images/recovery.img
├── rootfs.ext4 -> ../buildroot/output/rockchip_rk3328/images/rootfs.ext2
├── rootfs.img -> ../buildroot/output/rockchip_rk3328/images/rootfs.ext2
├── trust.img -> ../u-boot/trust.img
├── uboot.img -> ../u-boot/uboot.img
└── userdata.img
  • Pack the firmware

./build.sh updateimg

After entering this command, it will remind you whether you need to rename, enter y to rename the firmware, enter n to use the default firmware name, and generate the firmware in the rockdev/pack directory.

Note: Each time you pack firmware,you need to run mkfirmware.sh to update the partition images in rockdev/.

Upgrade firmware

Tools Download

Windows

Download AndroidTool.rar,then decompressing it , run the AndroidTool.exe , as following picture show: _images/linux_compile1.png

Premise: when the device upgrade firmware or partition images, it needs to be in Loader mode or Maskroom mode.

Upgrade firmware

Steps are as follows:

  1. Switch to the “Upgrade Firmware” page;

  2. Click “Firmware” to select the firmware you wanna burn;

  3. Click “Upgrade” to start burn,finish the upgrade,will restart device;

as following picture show: _images/linux_compile2.png

Note:

  1. If the upgrade fails, try clicking the “EraseFlash” to erase Flash , then upgrade again;

  2. If the version of the firmware loader you burned is not consistent with the original machine, do “erase Flash” before upgrading the firmware;

  3. If “erase Flash” run failed , try lower version of AndroidTool, then Burn firmware with AndroidTool_v2.58

as following picture show: _images/linux_compile3.png

Burn Partition Image

Steps are as follows:

  1. Switch to the “Download Image” page;

  2. Tick the partition you want to upgrade, you can choose more;

  3. Make sure the image file has the correct path. If necessary, click the blank table cells on the right side of the path to reselect;

  4. Click “Run” to start burn the partition image,finish it,will restart device;

as following picture show: _images/linux_compile1.png

Linux

Download Upgrade_tool1.34.zip , then unzip it , copy the upgrade_tool to /usr/local/bin/

  • Burn by Upgrade_tool

# Burn the firmware
sudo upgrade_tool uf update.img

# Burn the partition images
sudo upgrade_tool ul $LOADER
sudo upgrade_tool di -p $PARAMETER
sudo upgrade_tool di -uboot $UBOOT
sudo upgrade_tool di -trust $TRUST
sudo upgrade_tool di -b $BOOT
sudo upgrade_tool di -r $RECOVERY
sudo upgrade_tool di -m $MISC
sudo upgrade_tool di -oem $OEM
sudo upgrade_tool di -userdata $USERDATA
sudo upgrade_tool di -rootfs $ROOTFS
  • Burn by Script

# Burn all image
./rkflash.sh

# Burn the partition images
./rkflash.sh loader
./rkflash.sh parameter
./rkflash.sh uboot
./rkflash.sh trust
./rkflash.sh boot
./rkflash.sh recovery
./rkflash.sh misc
./rkflash.sh oem
./rkflash.sh userdata
./rkflash.sh rootfs

Note: rkflash.sh this script file works by using upgrade_tool