编写:编译uboot的过程分析

This commit is contained in:
weidongshan
2023-04-23 16:11:34 +08:00
parent b744d4e95d
commit 79a6a28a54
4 changed files with 531 additions and 17 deletions

View File

@@ -26,7 +26,7 @@ make过程
## 5.2 详细分析
## 5.2 配置过程详细分析
参考资料https://www.cnblogs.com/jianhua1992/archive/2022/11/02/16852774.html
@@ -361,28 +361,156 @@ endif
### 5.2.4 编译过程
## 5.3 编译过程详细分析(IMX6ULL)
```shell
book@100ask:~/100ask_imx6ull-sdk/Uboot-2017.03$ grep "head-y" * -nr
arch/arm/Makefile:85:head-y := arch/arm/cpu/$(CPU)/start.o
arch/arm/Makefile:89:head-y := $(CONFIG_SPL_START_S_PATH:"%"=%)/start.o
```
### 5.3.1 结论
![image-20230416110253414](pic/11_arch_makefile.png)
以IMX6ULL为例先说结论
* 执行make命令时要编译得到哪些文件由`ALL-y`决定
* 规则如下
```shell
include config.mk # 里面会包含arch/arm/config.mk(含有ALL-y += u-boot-dtb.imx)
_all : all
# Always append ALL so that arch config.mk's can add custom ones
ALL-y += u-boot.srec u-boot.bin u-boot.sym System.map binary_size_check
ALL-$(CONFIG_OF_SEPARATE) += u-boot.dtb
all: $(ALL-y)
```
* `ALL-y`的值为:
```shell
checkarmreloc u-boot-dtb.imx u-boot.srec u-boot.bin u-boot.sym System.map binary_size_check u-boot.dtb
```
* u-boot-dtb.imx依赖于u-boot-dtb.binu-boot-dtb.bin又依赖于u-boot-nodtb.bin和dts/dt.dtb
* 各类u-boot文件都依赖于u-boot先编译得到u-boot它由`u-boot-init`、`u-boot-main`两部分链接而成
* u-boot-init为arch/arm/cpu/armv7/start.o
* u-boot-main为lib/built-in.o fs/built-in.o net/built-in.o 等等
* 如何编译各个built-in.o以lib/built-in.o为例
* 执行: ``make -f ./scripts/Makefile.build obj=lib`
* 会包含lib/Makefile里面定义了`obj-y`
* 使用Makefile.build的规则把各个obj-y编译、链接为built-in.o
* 使用如下的命令链接得到u-boot
```shell
quiet_cmd_u-boot__ ?= LD $@
cmd_u-boot__ ?= $(LD) $(LDFLAGS) $(LDFLAGS_u-boot) -o $@ \
-T u-boot.lds $(u-boot-init) \
--start-group $(u-boot-main) --end-group \
$(PLATFORM_LIBS) -Map u-boot.map
```
* 再编译得到设备树文件: `make -f ./scripts/Makefile.build obj=dts dtbs`
* 顶层Makefile如下
```shell
dts/dt.dtb: checkdtc u-boot
$(Q)$(MAKE) $(build)=dts dtbs
```
* scripts/Makefile.build中会包含 dts/Makefile
```shell
DTB := arch/$(ARCH)/dts/$(DEVICE_TREE).dtb
$(DTB): arch-dtbs
$(obj)/dt.dtb: $(DTB) FORCE
$(call if_changed,shipped)
targets += dt.dtb
$(DTB): arch-dtbs
$(Q)test -e $@ || ( \
echo >&2; \
echo >&2 "Device Tree Source is not correctly specified."; \
echo >&2 "Please define 'CONFIG_DEFAULT_DEVICE_TREE'"; \
echo >&2 "or build with 'DEVICE_TREE=<device_tree>' argument"; \
echo >&2; \
/bin/false)
arch-dtbs:
$(Q)$(MAKE) $(build)=arch/$(ARCH)/dts dtbs
.SECONDARY: $(obj)/dt.dtb.S
obj-$(CONFIG_OF_EMBED) := dt.dtb.o
dtbs: $(obj)/dt.dtb
@:
```
* 最后把u-boot、设备树打包: `make -f ./scripts/Makefile.build obj=arch/arm/imx-common u-boot-dtb.imx`
```shell
include config.mk # 里面会包含arch/arm/config.mk(含有ALL-y += u-boot-dtb.imx)
%.imx: %.bin
$(Q)$(MAKE) $(build)=arch/arm/imx-common $@
```
### 5.3.2 顶层Makefile
顶层Makefile里指定了第1个文件head-y、指定了要编译哪些子目录libs-y
从顶层Makefile开始分析
```shell
include config.mk
include arch/$(ARCH)/Makefile # arch/arm/Makefile
include config.mk # 里面会包含arch/arm/config.mk(含有ALL-y += u-boot-dtb.imx)
include arch/$(ARCH)/Makefile # arch/arm/Makefile, 里面含有head-y libs-y
libs-y += lib/
libs-$(HAVE_VENDOR_COMMON_LIB) += board/$(VENDOR)/common/
libs-$(CONFIG_OF_EMBED) += dts/
libs-y += fs/
libs-y += net/
libs-y += disk/
libs-y += drivers/
libs-y += drivers/dma/
libs-y += drivers/gpio/
libs-y += drivers/i2c/
libs-y += drivers/mmc/
libs-y += $(if $(BOARDDIR),board/$(BOARDDIR)/)
libs-y := $(sort $(libs-y))
u-boot-dirs := $(patsubst %/,%,$(filter %/, $(libs-y))) tools examples
u-boot-alldirs := $(sort $(u-boot-dirs) $(patsubst %/,%,$(filter %/, $(libs-))))
libs-y := $(patsubst %/, %/built-in.o, $(libs-y))
u-boot-init := $(head-y)
u-boot-main := $(libs-y)
ALL-y += u-boot.srec u-boot.bin u-boot.sym System.map binary_size_check
```
arch/arm/Makefile
对于`arch/arm/Makefile`
```shell
head-y := arch/arm/cpu/$(CPU)/start.o
@@ -394,7 +522,15 @@ libs-y += arch/arm/lib/
继续回到顶层Makefile它会进入每一个libs-y目录执行里面的Makefile
所以,顶层Makefile包含了`arch/arm/Makefile`确定了head-y为`arch/arm/cpu/armv7/start.o`。
还定义了libs-y变量它里面含有多个目录。
### 5.3.3 编译子目录
顶层Makefile里定义了libs-y会进入里面每一目录使用它的Makefile进行编译
```shell
u-boot-main := $(libs-y)
@@ -406,29 +542,407 @@ u-boot-dirs := $(patsubst %/,%,$(filter %/, $(libs-y))) tools examples
$(u-boot-dirs): prepare scripts
# make -f $(srctree)/scripts/Makefile.build obj=arch/arm/cpu/arm7
$(Q)$(MAKE) $(build)=$@
```
比如u-boot-main里面含有一个目录"arch/arm/cpu/arm7/"
比如u-boot-main里面含有一个目录"arch/arm/cpu/armv7/"在u-boot-dirs中它末尾的"/"被取消就变成arch/arm/cpu/armv7。
u-boot-dirs就变成arch/arm/cpu/arm7
然后使用scripts/Makefile.build进行处理$(Q)$(MAKE) $(build)=$@ ,展开就是:
```shell
make -f /scripts/Makefile.build obj=arch/arm/cpu/armv7/
```
进一步分析scripts/Makefile.build它是编译u-boot源码的核心
scripts/Makefile.build是编译u-boot源码的关键
```
src := $(patsubst $(prefix)/%,%,$(obj)) # obj=arch/arm/cpu/arm7/, src=arch/arm/cpu/armv7
# 第1个目标
__build:
# The filename Kbuild has precedence over Makefile
kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile)
include $(kbuild-file) # 包含 arch/arm/cpu/arm7/Makefile
ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
lib-target := $(obj)/lib.a # 如果包含的arch/arm/cpu/armv7/Makefile里定义了lib-y
endif
ifneq ($(strip $(obj-y) $(obj-m) $(obj-) $(subdir-m) $(lib-target)),)
builtin-target := $(obj)/built-in.o # 如果包含的arch/arm/cpu/armv7/Makefile里定义了obj-y
endif
__build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
$(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
$(subdir-ym) $(always)
@:
# Built-in and composite module parts
$(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
$(call cmd,force_checksrc)
$(call if_changed_rule,cc_o_c)
ifdef builtin-target
quiet_cmd_link_o_target = LD $@
# If the list of objects to link is empty, just create an empty built-in.o
cmd_link_o_target = $(if $(strip $(obj-y)),\
$(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \
$(cmd_secanalysis),\
rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@)
$(builtin-target): $(obj-y) FORCE
$(call if_changed,link_o_target)
targets += $(builtin-target)
endif # builtin-target
```
### 5.3.4 链接得到u-boot
顶层Makefile里
```shell
libs-y := $(patsubst %/, %/built-in.o, $(libs-y))
u-boot-init := $(head-y)
u-boot-main := $(libs-y)
# Rule to link u-boot
# May be overridden by arch/$(ARCH)/config.mk
quiet_cmd_u-boot__ ?= LD $@
cmd_u-boot__ ?= $(LD) $(LDFLAGS) $(LDFLAGS_u-boot) -o $@ \
-T u-boot.lds $(u-boot-init) \
--start-group $(u-boot-main) --end-group \
$(PLATFORM_LIBS) -Map u-boot.map
quiet_cmd_smap = GEN common/system_map.o
cmd_smap = \
smap=`$(call SYSTEM_MAP,u-boot) | \
awk '$$2 ~ /[tTwW]/ {printf $$1 $$3 "\\\\000"}'` ; \
$(CC) $(c_flags) -DSYSTEM_MAP="\"$${smap}\"" \
-c $(srctree)/common/system_map.c -o common/system_map.o
u-boot: $(u-boot-init) $(u-boot-main) u-boot.lds FORCE
$(call if_changed,u-boot__)
ifeq ($(CONFIG_KALLSYMS),y)
$(call cmd,smap)
$(call cmd,u-boot__) common/system_map.o
endif
```
就是把head-y和各个libs-y下的built-in.o链接得到u-boot。
### 5.3.5 映像文件的依赖
对于IMX6ULL要使用的是u-boot-dtb.imx它含有u-boot和设备树。
在顶层Makefile里
```shell
include config.mk # 里面会包含arch/arm/config.mk(含有ALL-y += u-boot-dtb.imx)
%.imx: %.bin
$(Q)$(MAKE) $(build)=arch/arm/imx-common $@
```
u-boot-dtb.imx依赖于u-boot-dtb.binu-boot-dtb.bin又依赖于u-boot-nodtb.bin和dts/dt.dtb。
在顶层Makefile里
```shell
u-boot-dtb.bin: u-boot-nodtb.bin dts/dt.dtb FORCE
$(call if_changed,cat)
u-boot-nodtb.bin: u-boot FORCE
$(call if_changed,objcopy)
$(call DO_STATIC_RELA,$<,$@,$(CONFIG_SYS_TEXT_BASE))
$(BOARD_SIZE_CHECK)
```
所以编译出u-boot后会先制作u-boot-nodtb.bin然后编译dts/dt.dtb最后生成u-boot-dtb.bin、imx文件
![image-20230423141920708](pic/12_create_imx.png)
### 5.3.6 编译设备树
顶层Makefile里
```shell
dts/dt.dtb: checkdtc u-boot
$(Q)$(MAKE) $(build)=dts dtbs
```
需要用到scripts/Makefile.build中它会包含 dts/Makefile dts/Makefile中含有dtbs目标
```shell
DEVICE_TREE ?= $(CONFIG_DEFAULT_DEVICE_TREE:"%"=%)
DTB := arch/$(ARCH)/dts/$(DEVICE_TREE).dtb # 就是 arch/arm/dts/imx6ull-14x14-evk.dtb
$(obj)/dt.dtb: $(DTB) FORCE
$(call if_changed,shipped)
targets += dt.dtb
$(DTB): arch-dtbs
$(Q)test -e $@ || ( \
echo >&2; \
echo >&2 "Device Tree Source is not correctly specified."; \
echo >&2 "Please define 'CONFIG_DEFAULT_DEVICE_TREE'"; \
echo >&2 "or build with 'DEVICE_TREE=<device_tree>' argument"; \
echo >&2; \
/bin/false)
arch-dtbs:
$(Q)$(MAKE) $(build)=arch/$(ARCH)/dts dtbs
.SECONDARY: $(obj)/dt.dtb.S
obj-$(CONFIG_OF_EMBED) := dt.dtb.o
dtbs: $(obj)/dt.dtb
@:
```
最后会使用`$(Q)$(MAKE) $(build)=arch/$(ARCH)/dts dtbs`来处理进入使用arch/arm/dts/Makefile里面定了dtb-y
```shell
dtb-$(CONFIG_MX6) += imx6ul-14x14-ddr3-arm2.dtb \
imx6ul-14x14-ddr3-arm2-emmc.dtb \
imx6ul-14x14-ddr3-arm2-gpmi-weim.dtb \
imx6ul-14x14-lpddr2-arm2.dtb \
imx6ul-14x14-evk.dtb \
imx6ul-14x14-evk-emmc.dtb \
PHONY += dtbs
dtbs: $(addprefix $(obj)/, $(dtb-y))
@:
```
要编译出各类dtb文件用到scripts/Makefile.lib规则如下
```shell
quiet_cmd_dtc = DTC $@
# Modified for U-Boot
# Bring in any U-Boot-specific include after the '/dts-v1/;' header
cmd_dtc = mkdir -p $(dir ${dtc-tmp}) ; \
cat $< $(if $(u_boot_dtsi),\
| sed '/^\/ {$$/{x;s%$$%\#include \"$(u_boot_dtsi)\"%;G;}') | \
$(CPP) $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-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)
```
### 5.3.7 制作映像文件
当制作出设备树文件后在顶层Makefile里有如下规则进而制作出u-boot-dtb.bin
```shell
u-boot-dtb.bin: u-boot-nodtb.bin dts/dt.dtb FORCE
$(call if_changed,cat)
```
回到顶层Makefile看看如何制作imx映像文件
```shell
include config.mk # 里面会包含arch/arm/config.mk(含有ALL-y += u-boot-dtb.imx)
%.imx: %.bin
$(Q)$(MAKE) $(build)=arch/arm/imx-common $@
```
现在u-boot-dtb.imx的依赖文件u-boot-dtb.bin已经生成了将会使用arch/arm/imx-common/Makefile来生产imx文件
```shell
u-boot.imx: u-boot.bin $(IMX_CONFIG) $(PLUGIN).bin FORCE
$(call if_changed,mkimage)
```
最后会使用如下命令生产imx映像文件
```shell
./tools/mkimage -n board/freescale/mx6ullevk/imximage.cfg.cfgtmp -T imximage -e 0x87800000 -d u-boot-dtb.bin u-boot-dtb.imx
```
## 5.4 配置编译过程(STM32MP157)
使用如下命令配置、编译STM32MP157的u-boot
```shell
make stm32mp15_trusted_defconfig
make
```
所涉及的配置过程、编译过程跟前面分析的IMX6ULL几乎一样。
### 5.4.1 配置过程总结
![](pic/13_u-boot_stm32mp157_cfg.png)
### 5.4.2 编译过程总结
![image-20230423160914936](pic/14_create_stm32.png)
结论:
* 执行make命令时要编译得到哪些文件由`ALL-y`决定
* 规则如下
```shell
include config.mk # 里面会包含arch/arm/config.mk
# arch/arm/Makefile又包含$(machdirs)/config.mk
# 就是包含 arch/arm/mach-stm32mp/config.mk
# 里面有 ALL-y += u-boot.stm32
_all : all
# Always append ALL so that arch config.mk's can add custom ones
ALL-y += u-boot.srec u-boot.bin u-boot.sym System.map binary_size_check
ALL-$(CONFIG_OF_SEPARATE) += u-boot.dtb
all: $(ALL-y)
```
* `ALL-y`的值为:
```shell
checkarmreloc u-boot.stm32 u-boot.srec u-boot.bin u-boot.sym System.map binary_size_check u-boot.dtb
```
* u-boot.stm32依赖于u-boot.bin
* u-boot.bin依赖于u-boot-dtb.bin
* u-boot-dtb.bin依赖于u-boot-nodtb.bin和dts/dt.dtb
* 各类u-boot文件都依赖于u-boot先编译得到u-boot它由`u-boot-init`、`u-boot-main`两部分链接而成
* u-boot-init为arch/arm/cpu/armv7/start.o
* u-boot-main为lib/built-in.o fs/built-in.o net/built-in.o 等等
* 如何编译各个built-in.o以lib/built-in.o为例
* 执行: ``make -f ./scripts/Makefile.build obj=lib`
* 会包含lib/Makefile里面定义了`obj-y`
* 使用Makefile.build的规则把各个obj-y编译、链接为built-in.o
* 使用如下的命令链接得到u-boot
```shell
quiet_cmd_u-boot__ ?= LD $@
cmd_u-boot__ ?= $(LD) $(LDFLAGS) $(LDFLAGS_u-boot) -o $@ \
-T u-boot.lds $(u-boot-init) \
--start-group $(u-boot-main) --end-group \
$(PLATFORM_LIBS) -Map u-boot.map
```
* 再编译得到设备树文件: `make -f ./scripts/Makefile.build obj=dts dtbs`
* 顶层Makefile如下
```shell
dts/dt.dtb: checkdtc u-boot
$(Q)$(MAKE) $(build)=dts dtbs
```
* scripts/Makefile.build中会包含 dts/Makefile
```shell
DTB := arch/$(ARCH)/dts/$(DEVICE_TREE).dtb
$(DTB): arch-dtbs
$(obj)/dt.dtb: $(DTB) FORCE
$(call if_changed,shipped)
targets += dt.dtb
$(DTB): arch-dtbs
$(Q)test -e $@ || ( \
echo >&2; \
echo >&2 "Device Tree Source is not correctly specified."; \
echo >&2 "Please define 'CONFIG_DEFAULT_DEVICE_TREE'"; \
echo >&2 "or build with 'DEVICE_TREE=<device_tree>' argument"; \
echo >&2; \
/bin/false)
arch-dtbs:
$(Q)$(MAKE) $(build)=arch/$(ARCH)/dts dtbs
.SECONDARY: $(obj)/dt.dtb.S
obj-$(CONFIG_OF_EMBED) := dt.dtb.o
dtbs: $(obj)/dt.dtb
@:
```
* 最后制作u-boot.stm32在arch/arm/mach-stm32mp/config.mk中有如下规则
```shell
u-boot.stm32: u-boot.bin FORCE
$(call if_changed,mkimage)
```
* if_changed函数在scripts/Kbuild.include中定义
```shell
if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
@set -e; \
$(echo-cmd) $(cmd_$(1)); \
printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
```
* 展开`$(call if_changed,mkimage)`,就是执行如下命令:
```shell
cmd_mkimage
```
* cmd_mkimage命令在顶层Makefile中定义
```shell
cmd_mkimage = $(objtree)/tools/mkimage $(MKIMAGEFLAGS_$(@F)) -d $< $@ \
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 KiB