【嵌入式移植】3、编译U-Boot

发布时间:2024年01月13日

0 U-Boot及本文所选硬件

Das U-Boot,全称 Universal Boot Loader,是遵循GPL条款的开放源码项目。U-Boot的作用是系统引导。U-Boot从FADSROM、8xxROM、PPCBOOT逐步发展演化而来。其源码目录、编译形式与Linux内核很相似,事实上,不少U-Boot源码就是根据相应的Linux内核源程序进行简化而形成的,尤其是一些设备的驱动程序,这从U-Boot源码的注释中能体现这一点。

本文对U-Boot进行移植,笔者目前有FriendlyElec NanoPi Duo2、NanoPi NEO2、NanoPi Fire3等硬件,考虑要玩比较新的体系(比如ARMv8),而且U-Boot又给出了Allwinner SoC based boards相关资料,因此选择了NanoPi NEO2这个硬件平台,采用全志64位四核A53处理器H5, 内置Mail450 GPU, 标配512M DDR3内存。

1 获取U-Boot源码

根据FriendlyElec提供的手册Building U-boot and Linux for H5/H3/H2+,选用u-boot-2017.11,源码默认配置中包含nanopi_neo2_defconfg。

2 获取工具链

本文在ubuntu环境下编译,可以使用上一篇文章制作的交叉编译工具链进行编译,也可以直接安装对应的交叉编译工具链

sudo apt install gcc-aarch64-linux-gnu

本文先使用安装的交叉编译工具链进行编译,后续再针对自己制作的工具链进行试用。

根据U-Boot文档安装可能需要的依赖项

sudo apt-get install bc bison build-essential coccinelle \
  device-tree-compiler dfu-util efitools flex gdisk graphviz imagemagick \
  liblz4-tool libgnutls28-dev libguestfs-tools libncurses-dev \
  libpython3-dev libsdl2-dev libssl-dev lz4 lzma lzma-alone openssl \
  pkg-config python3 python3-asteval python3-coverage python3-filelock \
  python3-pkg-resources python3-pycryptodome python3-pyelftools \
  python3-pytest python3-pytest-xdist python3-sphinxcontrib.apidoc \
  python3-sphinx-rtd-theme python3-subunit python3-testtools \
  python3-virtualenv swig uuid-dev

3 BL31

根据U-Boot的手册,对于使用基于 Allwinner ARM 的 SoC(“sunxi”)的主板,编译时需要bl31.bin这个文件,在硬件板子提供的u-boot-sunxi-v2017.x源码根目录下找到此文件,复制到u-boot-2017.11根目录下,并设置临时环境变量

export BL31=~/arm/u-boot/u-boot-2017.11/bl31.bin

4 编译

执行配置

make nanopi_neo2_defconfig

请添加图片描述
执行编译

make CROSS_COMPILE=aarch64-linux-gnu-     

发生报错 ̄□ ̄||
请添加图片描述
这里报错信息为multiple definition of `yylloc’,即yylloc重复定义了,报错位置位于scripts/dtc/dtc-lexer.lex.o,即scripts/dtc/dtc-lexer.lex.c文件中

4.1 yylloc

在u-boot-2017.11目录下进行查找(其实上述报错信息已经提示在scripts/dtc/dtc-lexer.lex.c文件中了),这里的-r选项表示递归地在指定目录(此处为.)及其子目录中进行搜索

cd ~/arm/u-boot-u-boot-2017.11
grep -r "yylloc" .

请添加图片描述
可知yylloc在dtc-parser.tab.c、dtc-lexer.lex.c均存在定义。

./scripts/dtc/dtc-parser.tab.c:YYLTYPE yylloc
....
./scripts/dtc/dtc-lexer.lex.c:YYLTYPE yylloc;
....

根据报错信息,应对dtc-lexer.lex.c文件中的定义进行修改。考虑其它文件中使用extern关键字,这里同样可以在dtc-lexer.lex.c文件中的yylloc定义前增加extern关键字,避免重复定义。使用vi修改dtc-lexer.lex.c文件

vi scripts/dtc/dtc-lexer.lex.c

使用鼠标滚轮向下翻找,使用方向键移动光标,找到对应代码,按i修改
请添加图片描述
修改完成后按ESC,输入 :wq 保存并退出,保险起见可使用

cat scripts/dtc/dtc-lexer.lex.c

查看文件修改完成,再次运行编译命令

make CROSS_COMPILE=aarch64-linux-gnu-     

4.2 u_boot_dtsi

又发生报错 (╬ ̄皿 ̄)=○请添加图片描述从报错信息来看,与scripts/Makefile.lib:319: arch/arm/dts/sun50i-h5-nanopi-neo2.dtb的编译有关(这里根据错误信息可知为arch/arm/dts/.sun50i-h5-nanopi-neo2.dtb.pre.tmp文件中第87行1~10列存在语法错误):

Error: arch/arm/dts/.sun50i-h5-nanopi-neo2.dtb.pre.tmp:87.1-10 syntax error
FATAL ERROR: Unable to parse input tree
make[2]: *** [scripts/Makefile.lib:319: arch/arm/dts/sun50i-h5-nanopi-neo2.dtb] Error 1
make[1]: *** [dts/Makefile:43: arch-dtbs] Error 2
make: *** [Makefile:876: dts/dt.dtb] Error 2

至于这个arch/arm/dts/.sun50i-h5-nanopi-neo2.dtb.pre.tmp文件是哪儿来的呢。

查看scripts/Makefile.lib第319行,这里先安装vim,然后设置vim显示行号(在~/.vimrc文件中输入set nu,按ESC,输入:wq保存并退出)

sudo apt install vim
vim ~/.vimrc

请添加图片描述

然后查看scripts/Makefile.lib,找到第319行(这里配色方案有点奇怪。。。先不管了)
请添加图片描述
307~322行内容复制出来如下

quiet_cmd_dtc = DTC     $@
# Modified for U-Boot
# Bring in any U-Boot-specific include at the end of the file
cmd_dtc = mkdir -p $(dir ${dtc-tmp}) ; \
	(cat $<; $(if $(u_boot_dtsi),echo '\#include "$(u_boot_dtsi)"')) > $(pre-tmp); \
	$(CPP) $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $(pre-tmp) ; \
	$(DTC) -O dtb -o $@ -b 0 \
		-i $(dir $<) $(DTC_FLAGS) \
		-d $(depfile).dtc.tmp $(dtc-tmp) ; \
	cat $(depfile).pre.tmp $(depfile).dtc.tmp > $(depfile)

$(obj)/%.dtb: $(src)/%.dts FORCE
	$(call if_changed_dep,dtc) 

pre-tmp = $(subst $(comma),_,$(dot-target).pre.tmp)
dtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp)

由于其中含有许多变量,具体内容不太好看,因此重新编译,增加参数V=1(查看完整的编译信息)

make CROSS_COMPILE=aarch64-linux-gnu- V=1

请添加图片描述
报错位置附近所执行的语句为(按分号分隔)

  mkdir -p arch/arm/dts/ ; 

这里是创建arch/arm/dts/目录,-p参数表示若无上层目录则同时创建上层目录。

  (cat arch/arm/dts/sun50i-h5-nanopi-neo2.dts; echo '\#include "sunxi-u-boot.dtsi"') > arch/arm/dts/.sun50i-h5-nanopi-neo2.dtb.pre.tmp; 

这一条语句是读取arch/arm/dts/sun50i-h5-nanopi-neo2.dts文件中的内容,并在最后增加一行

\#include "sunxi-u-boot.dtsi”

一起写入arch/arm/dts/.sun50i-h5-nanopi-neo2.dtb.pre.tmp文件中,查看此文件,其最后其一行确实相比arch/arm/dts/sun50i-h5-nanopi-neo2.dts文件增加了一行#include "sunxi-u-boot.dtsi”(此文件同时设置了用于输出u-boot启动过程信息的端口为uart0,波特率115200,对应引脚为PF5、PF6等相关信息)

/*
 * Copyright (C) 2017 Icenowy Zheng <icenowy@aosc.io>
 * Copyright (C) 2017 Jagan Teki <jteki@openedev.com>
 *
 * This file is dual-licensed: you can use it either under the terms
 * of the GPL or the X11 license, at your option. Note that this dual
 * licensing only applies to this file, and not this project as a
 * whole.
 *
 *  a) This library is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of the
 *     License, or (at your option) any later version.
 *
 *     This library is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 * Or, alternatively,
 *
 *  b) Permission is hereby granted, free of charge, to any person
 *     obtaining a copy of this software and associated documentation
 *     files (the "Software"), to deal in the Software without
 *     restriction, including without limitation the rights to use,
 *     copy, modify, merge, publish, distribute, sublicense, and/or
 *     sell copies of the Software, and to permit persons to whom the
 *     Software is furnished to do so, subject to the following
 *     conditions:
 *
 *     The above copyright notice and this permission notice shall be
 *     included in all copies or substantial portions of the Software.
 *
 *     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *     HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *     WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *     OTHER DEALINGS IN THE SOFTWARE.
 */

/dts-v1/;

#include "sun50i-h5.dtsi"

#include <dt-bindings/gpio/gpio.h>

/ {
	model = "FriendlyARM NanoPi NEO 2";
	compatible = "friendlyarm,nanopi-neo2", "allwinner,sun50i-h5";

	aliases {
		serial0 = &uart0;
	};

	chosen {
		stdout-path = "serial0:115200n8";
	};

	reg_vcc3v3: vcc3v3 {
		compatible = "regulator-fixed";
		regulator-name = "vcc3v3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
	};
};

&mmc0 {
	compatible = "allwinner,sun50i-h5-mmc",
		     "allwinner,sun50i-a64-mmc",
		     "allwinner,sun5i-a13-mmc";
	pinctrl-names = "default";
	pinctrl-0 = <&mmc0_pins_a>, <&mmc0_cd_pin>;
	vmmc-supply = <&reg_vcc3v3>;
	bus-width = <4>;
	cd-gpios = <&pio 5 6 GPIO_ACTIVE_LOW>; /* PF6 */
	status = "okay";
};

&uart0 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart0_pins_a>;
	status = "okay";
};
\#include "sunxi-u-boot.dtsi"

其实在这里根据前述的错误信息,可知是第87行,新增的\#include "sunxi-u-boot.dtsi"中\#include这部分存在语法错误。分析可知去掉前面的\即可。

修改scripts/Makefile.lib第311行

vim scripts/Makefile.lib

将311行中的\去掉

改之前:(cat $<; $(if $(u_boot_dtsi),echo '\#include "$(u_boot_dtsi)"')) > $(pre-tmp); \
改之后:(cat $<; $(if $(u_boot_dtsi),echo '#include "$(u_boot_dtsi)"')) > $(pre-tmp); \

改好之后按ESC,输入:wq保存并退出。再次执行编译

make CROSS_COMPILE=aarch64-linux-gnu-     

请添加图片描述

5 烧写

插入SD卡,通过fdisk命令查看设备名称,这里我的SD为/dev/sdb

sudo fdisk -l
....
Disk /dev/sda: 128 GiB, 137438953472 bytes, 268435456 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: ********-****-****-****-************

Device       Start       End   Sectors   Size Type
/dev/sda1     2048   1050623   1048576   512M EFI System
/dev/sda2  1050624 268433407 267382784 127.5G Linux filesystem

....
Disk /dev/sdb: 14.88 GiB, 15974006784 bytes, 31199232 sectors
Disk model: SD Card Reader  
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x********

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        2048 31197183 31195136 14.9G  7 HPFS/NTFS/exFAT

将编译生成的sunxi-spl.bin和u-boot.itb烧写到SD卡上,其中/dev/sdX为实际的SD设备名

sudo dd if=spl/sunxi-spl.bin of=/dev/sdX bs=1024 seek=8
sudo dd if=u-boot.itb of=/dev/sdX bs=1024 seek=40

6 上电验证

将SD卡插入nanopi neo2的插槽,将UART0接到电脑上,使用相关串口工具接收串口信息。从前述分析可知,波特率为115200,上电,u-boot成功启动,输出信息如下
请添加图片描述
输入help可以获取u-boot命令行的相关命令,这里就不做展示了

本章完结撒花??ヽ(°▽°)ノ?

文章来源:https://blog.csdn.net/Keep_moving_tzw/article/details/135466169
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。