编译 Linux5.10 固件 编译环境搭建 本章介绍 Linux SDK 的编译环境搭建 注意: (1)推荐在 X86_64 Ubuntu 18.04 系统环境下进行开发,若使用其它系统版本 ,可能需要对编译环境做相应调整。 (2)使用普通用户进行编译,不要使用 root 用户权限进行编译。 获取 SDK 首先准备一个空文件夹用于存放 SDK,建议在 home 目录下,本文以"~/proj"为 例 不要在虚拟机共享文件夹以及非英文目录存放、解压SDK,避免产生不必要的错 误 获取 SDK 需要先安装: sudo apt update sudo apt install -y repo git python 方法一(推荐) 由于 Firefly_Linux_SDK 源码包比较大,部分用户电脑不支持4G以上文件或单 个文件网络传输较慢, 所以我们采用分卷压缩的方法来打包 SDK。用户可以通过 如下方式获取 Firefly_Linux_SDK 源码包: rk356x_linux5.10_release_20240319_v1.4.0b 下载完成后,提取 SDK: # 给脚本添加执行权限 cd /path/to/rk356x_linux5.10_release_20240319_v1.4.0b chmod +x ./sdk_tools.sh # 创建 SDK 目录 mkdir -p ~/proj/rk356x_sdk-linux5.10 # 校验并解压 ./sdk_tools.sh --unpack -C ~/proj/rk356x_sdk-linux5.10 # 还原工作目录 ./sdk_tools.sh --sync -C ~/proj/rk356x_sdk-linux5.10 方法二 通过 repo 拉取代码,此方法对网络要求较高,有条件可以使用 可选择获取完整 SDK 或者 BSP: # 创建 SDK 目录 mkdir ~/proj/rk356x_sdk-linux5.10 cd ~/proj/rk356x_sdk-linux5.10 ## 完整 SDK repo init --no-clone-bundle --repo-url https://gitlab.com/firefly-linux/git-repo.git --no-repo-verify -u https://gitlab.com/firefly-linux/manifests.git -b master -m rk356x_linux5.10_release.xml ## BSP ( 只包含基础仓库和编译工具 ) ## BSP 包括 device/rockchip 、docs 、 kernel 、 u-boot 、 rkbin 、 tools 和交叉编译链 repo init --no-clone-bundle --repo-url https://gitlab.com/firefly-linux/git-repo.git --no-repo-verify -u https://gitlab.com/firefly-linux/manifests.git -b master -m rk356x_linux5.10_bsp_release.xml 同步代码 执行如下命令同步代码: # 进入 SDK 根目录 cd ~/proj/rk356x_sdk-linux5.10 # 同步 .repo/repo/repo sync -c --no-tags .repo/repo/repo start firefly --all 后续可以使用以下命令更新 SDK: .repo/repo/repo sync -c --no-tags 因为网络环境等原因,".repo/repo/repo sync -c --no-tags" 命令更新代码可 能会失败,可多次反复执行。 目录结构 . ├── app ├── buildroot # Buildroot 根文件系统编译目录 ├── build.sh -> device/rockchip/common/build.sh # 编译脚本 ├── device # 编译相关配置文件 ├── docs # 文档 ├── envsetup.sh -> buildroot/build/envsetup.sh ├── external ├── kernel # Kernel ├── Makefile -> device/rockchip/common/Makefile ├── prebuilts # 交叉编译工具链 ├── rkbin ├── tools # 工具目录 └── u-boot # U-Boot 安装依赖 方法一: 在 PC 中自行安装环境: 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 \ unzip device-tree-compiler python-pip ncurses-dev python-pyelftools 方法二:使用 Docker 使用 dockerfile 创建容器,在容器中进行编译,完美解决编译环境问题,并且 与主机环境隔离,互不影响。 首先在主机中安装 docker,请参考: 安装教程 创建一个目录作为 docker 工作目录,例如"~/docker/",在其中创建文件 "dockerfile",内容如下: FROM ubuntu:18.04 MAINTAINER firefly "service@t-firefly.com" ENV DEBIAN_FRONTEND=noninteractive RUN cp -a /etc/apt/sources.list /etc/apt/sources.list.bak RUN sed -i 's@http://.*ubuntu.com@http://repo.huaweicloud.com@g' /etc/apt/sources.list RUN apt update RUN apt install -y build-essential crossbuild-essential-arm64 \ bash-completion vim sudo locales time rsync bc python RUN apt install -y repo git ssh libssl-dev liblz4-tool lib32stdc++6 \ expect patchelf chrpath gawk texinfo diffstat binfmt-support \ qemu-user-static live-build bison flex fakeroot cmake \ unzip device-tree-compiler python-pip ncurses-dev python-pyelftools \ subversion asciidoc w3m dblatex graphviz python-matplotlib cpio \ libparse-yapp-perl default-jre patchutils swig expect-dev u-boot-tools RUN apt update && apt install -y -f # language support RUN locale-gen en_US.UTF-8 ENV LANG en_US.UTF-8 # switch to a no-root user RUN useradd -c 'firefly user' -m -d /home/firefly -s /bin/bash firefly RUN sed -i -e '/\%sudo/ c \%sudo ALL=(ALL) NOPASSWD: ALL' /etc/sudoers RUN usermod -a -G sudo firefly USER firefly WORKDIR /home/firefly 创建镜像 cd ~/docker docker build -t sdkcompiler . # sdkcompiler 是镜像名称,可随意更改,注意命令最后有一个‘.’ # 此过程需要一段时间,请耐心等待 镜像创建完毕后,创建容器并启动 # 此处将主机内 SDK 所在文件夹挂载到容器内,这样容器内就能访问主机中的 SDK 了 # source= 填 SDK 所在目录;target= 填容器内的一个目录,必须是空目录 # ubuntu18 是容器名,firefly 是容器 hostname,均可随意更改 # sdkcompiler 是上一步的镜像名 docker run --privileged --mount type=bind,source=/home/fierfly/proj,target=/home/firefly/proj --name="ubuntu18" -h firefly -it sdkcompiler 现在就可以在容器中进行 SDK 的编译了。 退出容器、重启容器的方法: # 在容器内输入 exit 即可退出 # 查看所有容器(包括已退出的) docker ps -a # 重启一个退出的容器并连接 docker start ubuntu18 # 容器名 docker attach ubuntu18 编译 Ubuntu 固件 本章介绍 Ubuntu 固件的编译流程,推荐在 Ubuntu 18.04 系统环境下进行开发 ,若使用其它系统版本,可能需要对编译环境做相应调整。 Ubuntu 固件简单介绍 什么是 Ubuntu Minimal ? 什么是 Ubuntu Desktop ? 编译 SDK 编译前配置 不同板型的配置文件存放在"device/rockchip/rk3566_rk3568/"目录下 回到 SDK 根目录执行"build.sh"选择配置文件: ./build.sh firefly_rk3568_roc-rk3568-pc-se_ubuntu_defconfig 配置文件会链接到 "output/defconfig",检查该文件可以验证是否配置成功。 相关配置介绍: # 预编译的文件系统位置 PREBUILT_ROOTFS_IMG="prebuilt_rootfs/rk356x_ubuntu_rootfs.img" # 蓝牙用到的 UART RK_WIFIBT_TTY="ttyS8" # 内核配置文件 RK_KERNEL_CFG="firefly_linux_defconfig" # 设备树名称 RK_KERNEL_DTS_NAME="rk3568-firefly-roc-pc" # ramdisk 镜像 RK_RAMDISK_IMG="ramdisk.img" # boot 分区镜像的 Image tree source RK_BOOT_FIT_ITS="bootramdisk.its" # 预编译的 recovery 镜像 RK_RECOVERY_RAMDISK="rk356x-recovery-arm64.cpio.gz" # 分区表 RK_PARAMETER="parameter-ubuntu-fit.txt" # 使用 FIT 格式镜像 RK_USE_FIT_IMG=y # 使用 extlinux 方式引导系统 USE_EXTBOOT=y 下载 Ubuntu 根文件系统 下载根文件系统:Ubuntu 根文件系统(64位),放到 SDK 路径下 解压文件 # 假设下载的压缩包叫 Ubuntu20.04-xxx_RK3568_KERNEL-5.10_xxx.7z 7z x Ubuntu20.04-xxx_RK3568_KERNEL-5.10_xxx.7z 将解压出的根文件系统放到 prebuilt_rootfs/ 目录下 mkdir prebuilt_rootfs # 假设解压出的文件系统叫做 Ubuntu20.04-xxx_RK3568_KERNEL-5.10_xxx.img mv Ubuntu20.04-xxx_RK3568_KERNEL-5.10_xxx.img prebuilt_rootfs/ cd prebuilt_rootfs ln -sf Ubuntu20.04-xxx_RK3568_KERNEL-5.10_xxx.img rk356x_ubuntu_rootfs.img 全自动编译 全自动编译会执行所有编译、打包操作,直接生成 RK 固件。 ./build.sh all 完整固件会保存到 "output/update/" 目录。 部分编译 编译 u-boot ./build.sh uboot 生成的镜像为 u-boot/uboot.img 编译 kernel 注意:Firefly kernel 没有开启全部的内核功能,有需求请查看 Kernel 使用 ./build.sh extboot 生成的镜像为 kernel/extboot.img 打包固件 ./build.sh updateimg 打包固件,生成的完整固件会保存到 "output/update/" 目录。 分区说明 parameter 分区表 parameter.txt 文件中包含了固件的分区信息,以 parameter-ubuntu-fit.txt 为例: 路径:"device/rockchip/rk3566_rk3568/parameter-ubuntu-fit.txt" FIRMWARE_VER: 1.0 MACHINE_MODEL: RK3568 MACHINE_ID: 007 MANUFACTURER: RK3568 MAGIC: 0x5041524B ATAG: 0x00200800 MACHINE: 0xffffffff CHECK_MASK: 0x80 PWR_HLD: 0,0,A,0,1 TYPE: GPT GROW_ALIGN: 0 CMDLINE: mtdparts=rk29xxnand:0x00002000@0x00004000(uboot),0x00002000@0x00006000(misc),0x00040000@0x00008000(boot:bootable),0x00020000@0x00048000(recovery),0x00010000@0x00068000(backup),0x00c00000@0x00078000(rootfs),-@0x00c78000(userdata:grow) uuid:rootfs=614e0000-0000-4b53-8000-1d28000054a9 CMDLINE 属性是我们关注的地方,以 uboot 为例, 0x00002000@0x00004000(uboot) 中 0x00004000 为uboot 分区的起始位置, 0x00002000 为分区的大小,以此类推。单位是块,每块 512 字节。 编译 Buildroot 固件 本章介绍 Buildroot 固件的编译流程,推荐在 Ubuntu 18.04 系统环境下进行 开发,若使用其它系统版本,可能需要对编译环境做相应调整。 编译 SDK 编译前配置 不同板型的配置文件存放在"device/rockchip/rk3566_rk3568/"目录下 回到 SDK 根目录执行"build.sh"选择配置文件: ./build.sh firefly_rk3568_roc-rk3568-pc-se_buildroot_defconfig 配置文件会链接到 "output/defconfig",检查该文件可以验证是否配置成功。 相关配置介绍: # 设备树名称 RK_KERNEL_DTS_NAME="rk3568-firefly-roc-pc" # 内核配置文件 RK_KERNEL_CFG="firefly_linux_defconfig" # 使用 FIT 格式镜像 RK_USE_FIT_IMG=y # 蓝牙用到的 UART RK_WIFIBT_TTY="ttyS8" # 分区表 RK_PARAMETER="parameter-buildroot-fit.txt" # boot 分区镜像的 Image tree source RK_BOOT_FIT_ITS="bootramdisk.its" # ramdisk 镜像 RK_RAMDISK_IMG="ramdisk.img" # 使用 extlinux 的方式引导系统 USE_EXTBOOT=y 全自动编译 全自动编译会执行所有编译、打包操作,生成完整固件。 ./build.sh all 完整固件会保存到 "output/update/" 目录。 部分编译 编译 u-boot ./build.sh uboot 生成的镜像为 u-boot/uboot.img 编译 kernel 注意:Firefly kernel 没有开启全部的内核功能,有需求请查看 Kernel 使用 ./build.sh extboot 生成的镜像为 kernel/extboot.img 编译 recovery ./build.sh recovery 生成结果在 output/recovery 下 编译 Buildroot 根文件系统 ./build.sh buildroot # 注:确保作为普通用户编译 Buildroot 根文件系统,避免不必要的错误。 编译目录在 buildroot/output/ 下 打包固件 ./build.sh updateimg 打包固件,生成的完整固件会保存到 "output/update/" 目录。 分区说明 parameter 分区表 parameter.txt 文件中包含了固件的分区信息,以 "parameter-buildroot- fit.txt" 为例: 路径:"device/rockchip/rk3566_rk3568/parameter-buildroot-fit.txt" FIRMWARE_VER: 1.0 MACHINE_MODEL: RK3568 MACHINE_ID: 007 MANUFACTURER: RK3568 MAGIC: 0x5041524B ATAG: 0x00200800 MACHINE: 0xffffffff CHECK_MASK: 0x80 PWR_HLD: 0,0,A,0,1 TYPE: GPT GROW_ALIGN: 0 CMDLINE: mtdparts=:0x00002000@0x00004000(uboot),0x00002000@0x00006000(misc),0x00040000@0x00008000(boot:bootable),0x00020000@0x00048000(recovery),0x00010000@0x00068000(backup),0x00040000@0x00078000(oem),0x00c00000@0x000b8000(rootfs),-@0x00cb8000(userdata:grow) uuid:rootfs=614e0000-0000-4b53-8000-1d28000054a9 CMDLINE 属性是我们关注的地方,以 uboot 为例, 0x00002000@0x00004000(uboot) 中 0x00004000 为uboot 分区的起始位置, 0x00002000 为分区的大小,以此类推。单位是块,每块 512 字节。