3. Uboot的修改与编译

3.1. 安装编译工具和依赖

使用apt工具安装以下工具和依赖:

1
sudo apt install make git gcc-arm-none-eabi gcc bison flex libssl-dev dpkg-dev lzop libncurses5-dev

3.2. 获取uboot源码

3.2.1. 下载源代码

uboot的源代码有多种方式可下载,但各自又有不同,野火uboot是根据NXP官方提供的uboot进行板卡的修改与功能的添加,NXP官方的uboot是以uboot官方版本进行芯片的适配。

官方uboot GitHub:

https://github.com/u-boot/u-boot

NXP提供的uboot下载链接:

https://github.com/Freescale/u-boot-fslc

野火提供的uboot下载链接:

https://gitee.com/Embedfire/ebf_linux_uboot

或者

https://github.com/Embedfire/ebf_linux_uboot

注意:本教程以野火提供的uboot为分析样本,也十分建议使用野火适配好的。

3.2.2. 查看切换分支

通常一个uboot仓库往往维护着不同分支的uboot,进入仓库目录下可通过命令查看及切换uboot分支, 例如imx6ull buildroot的镜像中使用的uboot分支为 ebf_v2020_10_imx

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
git clone https://gitee.com/Embedfire/ebf_linux_uboot.git

#查看uboot分支
git branch -a

#打印消息如下,默认为master主分支。
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/ebf_v2017_09_rk3328
  remotes/origin/ebf_v2018.11_star
  remotes/origin/ebf_v2020_10_imx
  remotes/origin/imx_v2019.04_ls1043ardb
  remotes/origin/imx_v2020.04_5.4.47_2.2.0
  remotes/origin/master


#切换ebf_v2020_10_imx分支
git checkout ebf_v2020_10_imx

#打印消息如下
分支 'ebf_v2020_10_imx' 设置为跟踪来自 'origin' 的远程分支 'ebf_v2020_10_imx'。
切换到一个新分支 'ebf_v2020_10_imx'

#重新查看当前分支
git branch
* ebf_v2020_10_imx
  master

也可以在下载时指定分支并且减少拉取以往的提交记录,如下所示:(建议使用以下操作进行拉取,拉取体积小)

1
2
3
4
5
#使用-b参数指定ebf_v2020_10_imx分支,使用--depth指定拉取最新的10次提交
git clone -b ebf_v2020_10_imx --depth=10 https://gitee.com/Embedfire/ebf_linux_uboot.git

#或者
git clone -b ebf_v2020_10_imx --depth=10 https://github.com/Embedfire/ebf_linux_uboot.git

如需下载NXP的uboot可以使用如下命令

1
2
3
4
  #获取默认的分支,再切换为需要的分支
  git clone https://github.com/Freescale/u-boot-fslc.git
  #或者-b参数指定需要的分支
  git clone -b 2020.10+fslc https://github.com/Freescale/u-boot-fslc.git

3.2.3. uboot工程结构分析

学习一个软件,尤其是开源软件,首先应该从分析软件的工程结构开始。一个好的软件有良好的工程结构,对于读者学习和理解软件的架构以及工作流程都有很好的帮助。

uboot的源代码布局和我们后面讲的Linux类似,使用了按照模块划分的结构,并且充分考虑了体系结构和跨平台问题,其源代码树结构请参考下图

uboot目录

目录/文件

说明

目录/文件

说明

api

通用的API函数相关目录

arch

与芯片架构相关目录

board

板级相关信息目录

cmd

uboot命令相关目录

common

通用代码目录

configs

boot配置文件目录

disk

磁盘相关内容目录

doc

说明文档

drivers

驱动代码相关目录

dtoverlay

dts

设备树相关目录

env

uboot环境相关

examples

示例代码目录

fs

文件系统相关目录

include

头文件相关目录

lib

lib库文件目录

Licenses

许可证相关目录

net

网络相关代码目录

post

上电自检相关目录

scripts

相关脚本目录

test

测试代码目录

tools

uboot构建工具相关目录

Kconfig

图形配置界面相关文件

Makefile

Makefile文件

3.3. uboot编译

野火提供的imx6ull uboot分为nand版本和eMMC/SD版本,可在uboot源码顶层目录执行以下命令编译不同版本。

  • eMMC/SD版本配置文件为:mx6ull_fire_mmc_lite_defconfig

  • nand版本配置文件为:mx6ull_fire_nand_lite_defconfig

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#编译eMMC/SD版本
#清除编译输出
make distclean
#加载配置文件
make ARCH=arm CROSS_COMPILE=arm-none-eabi- mx6ull_fire_mmc_lite_defconfig
#编译
make ARCH=arm CROSS_COMPILE=arm-none-eabi- -j16


#编译nand版本
#清除编译输出
make distclean
#加载配置文件
make ARCH=arm CROSS_COMPILE=arm-none-eabi- mx6ull_fire_nand_lite_defconfig
#编译
make ARCH=arm CROSS_COMPILE=arm-none-eabi- -j16

3.3.1. 编译产物分析

编译成功后,将打印以下信息。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
LD      u-boot
OBJCOPY u-boot-nodtb.bin
OBJCOPY u-boot.srec
SYM     u-boot.sym
DTC     arch/arm/dts/imx6ull-14x14-evk.dtb
DTC     arch/arm/dts/imx6ull-fire-mmc.dtb
DTC     arch/arm/dts/imx6ull-fire-nand.dtb
DTC     arch/arm/dts/imx6ull-colibri.dtb
DTC     arch/arm/dts/imx6ull-myir-mys-6ulx-eval.dtb
DTC     arch/arm/dts/imx6ull-phytec-segin-ff-rdk-emmc.dtb
DTC     arch/arm/dts/imx6ull-dart-6ul.dtb
DTC     arch/arm/dts/imx6ull-somlabs-visionsom.dtb
DTC     arch/arm/dts/imx6ulz-14x14-evk.dtb
DTC     arch/arm/dts/imx6-apalis.dtb
DTC     arch/arm/dts/imx6-colibri.dtb
SHIPPED dts/dt.dtb
FDTGREP dts/dt-spl.dtb
CAT     u-boot-dtb.bin
COPY    u-boot.dtb
COPY    u-boot.bin
CFGS    u-boot-dtb.cfgout
MKIMAGE u-boot-dtb.imx
  • u-boot:初步链接后得到的uboot文件

  • u-boot-nodtb.bin:是在u-boot的基础上,经过objcopy去除符号表信息之后的可执行程序,具体代码如下

1
arm-none-eabi-objcopy --gap-fill=0xff  -j .text -j .secure_text -j .secure_data -j .rodata -j .hash -j .data -j .got -j .got.plt -j .u_boot_list -j .rel.dyn -j .binman_sym_table -j .text_rest -j .dtb.init.rodata -j .efi_runtime -j .efi_runtime_rel -O binary   u-boot u-boot-nodtb.bin
  • u-boot.dtb:uboot的设备树,是由arm-none-eabi-gcc和dtc编译出来的,详细的可以分析编译过程,这里就不展开分析了

  • u-boot.bin:是在u-boot-nodtb.bin后追加了u-boot.dtb形成的

  • u-boot-dtb.imx:是u-boot.bin 添加了3KB头部信息和尾部信息(结尾添加了1298字节的00,00实际没什么作用)组成的镜像

编译生成的 u-boot-dtb.imx 文件就是我们想要文件。

为了区分eMMC/SD和nand版本,需要重命名 u-boot-dtb.imx ,在后续烧录工具或者打包工具中对于不同版本作了具体名字的指定。

  • eMMC/SD版本重命名为: u-boot-mmc.imx

  • nand版本重命名为: u-boot-nand.imx

3.4. uboot修改

一般来说,要适配一块新的板卡,在uboot中主要设计三部分的修改和添加, 分别是board目录下涉及板卡初始化的部分、configs目录下对应板卡的配置文件、arch目录下与板级外设相关的文件。

3.4.1. uboot配置文件

如果我们要修改uboot配置文件的话,可以借助menuconfig工具来修改。

  • eMMC/SD版本配置文件为:mx6ull_fire_mmc_lite_defconfig

  • nand版本配置文件为:mx6ull_fire_nand_lite_defconfig

首先在uboot根目录下执行以下操作加载配置文件:

1
2
3
4
5
6
7
#eMMC/SD执行
#应用配置文件
make mx6ull_fire_mmc_lite_defconfig

#nand执行
#应用配置文件
make mx6ull_fire_nand_lite_defconfig

然后打开menuconfig界面:

1
2
# 使用menuconfig来管理修改配置文件
make menuconfig
../../_images/uboot_build_0.jpg

修改完成之后按ESC按键退出,并保存

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 保存defconfig文件
make savedefconfig

# eMMC/SD执行
# 覆盖原来的配置文件
cp -f defconfig configs/mx6ull_fire_mmc_lite_defconfig

# nand执行
# 覆盖原来的配置文件
cp -f defconfig configs/mx6ull_fire_nand_lite_defconfig

3.4.2. uboot设备树

设备树文件是板级设备的描述文件,系统通过设备树文件得知板卡上有哪些外设,从而加载相应的驱动使外设正常工作。

原生的U-Boot只支持使用U-Boot自己的DTB,NXP平台增加了kernel DTB机制的支持, 即使用kernel DTB去初始化外设。 主要目的是为了兼容外设板级差异,如:power、clock、display等。

二者的作用:

  • U-Boot DTB:负责初始化存储、打印串口等设备;

  • Kernel DTB:负责初始化存储、打印串口以外的设备;

U-Boot初始化时先用U-Boot DTB完成存储、打印串口初始化, 然后从存储上加载Kernel DTB并转而使用这份DTB继续初始化其余外设。

开发者一般不需要修改 U-Boot DTB(除非更换打印串口),NXP系列处理器使用的defconfig都已启用kernel DTB机制。 所以通常对于外设的DTS修改,用户应该修改kernel DTB。

关于U-Boot DTB目录:

  • eMMC/SD设备树:ebf_linux_uboot/arch/arm/dts/imx6ull-fire-mmc.dts

  • nand设备树:ebf_linux_uboot/arch/arm/dts/imx6ull-fire-nand.dts

3.4.3. uboot板级文件

本教程使用的板级配置位于:

  • ebf_linux_uboot/board/freescale/mx6ullfire_lite

目录结构如下:

1
2
3
4
5
6
7
8
.
├── imximage.cfg
├── Kconfig
├── MAINTAINERS
├── Makefile
├── mx6ullfire_lite.c
├── plugin.S
└── README
  • mx6ullfire_lite.c :写了一些uboot启动代码和对板子引脚读取的代码。

  • plugin.S :用汇编写的设置ddr和时钟,一般不需要更改。

  • Makefile :里面修改C文件编译后的o文件,有多少个就写多少个o文件,注意与c文件名字相同。

  • Kconfig :U-Boot配置选项文件,定义了编译时需要配置哪些选项用于生成适用于该开发板的U-Boot程序。

如果需要适配新的板卡,可以参考此提交: 添加imx6ull lite板级配置,用于快速启动

3.5. 参考资料

官方uboot下载链接: http://www.denx.de/wiki/uboot/WebHome

官方uboot GitHub: https://github.com/u-boot/u-boot

NXP提供的uboot下载链接: https://github.com/Freescale/u-boot-fslc

uboot wiki: http://www.denx.de/wiki/uboot/WebHome