Uboot优美代码赏析1:目录结构和malkefile分析
关于Uboot自己选的版本是目前最新的2011.06,官方网址为:http://www.denx.de/wiki/U-Boot/WebHome,下面的一些内容主要翻译自顶层目录的 README(对不怎么爱看英语的我,我很喜欢) 。
U-Boot是一种基于PowerPC, ARM, MIPS 或者其他处理器架构的嵌入式开发板的启动引导程序(boot loader),boot loader是可以被安装在作为引导的ROM上,实现初始化和测试硬件,和下载与运行应用的代码。
U-Boot的开发与Linux紧密相连:部分代码是取自Linux的源代码,比如我们使用共同的头文件定义,同时特别提供了可以用于启动Linux内核的功能。
DENX注重于去使这个软件很容易去配置和拓展。比如,为了便于用户增加新的命令,所以的监视命令都是用同样的接口和架构实现的。另外,很少使用的命令可以动态地进行加载执行,比如硬件测试功能。
下面介绍目录结构:
/arch /存放特定架构的实现文件,系统的启动代码一般在这里的start.S中
/arm /Arm架构
/cpu /CPU相关文件
/arm720t /ARM 720 CPUs
/arm920t /ARM 920 CPUs
/at91rm9200 /Atmel AT91RM9200 CPU
/imx /Freescale MC9328 i.MX CPUs
/s3c24x0 /Samsung S3C24X0 CPUs
/arm925t /ARM 925 CPUs
/arm926ejs /ARM 926 CPUs
/arm1136 /ARM 1136 CPUs
/ixp /Intel XScale IXP CPUs
/pxa /Intel XScale PXA CPUs
/s3c44b0 /Samsung S3C44B0 CPUs
/sa1100 /Intel StrongARM SA1100 CPUs
/lib /CPU library files
/avr32 /AVR32架构
/cpu /CPU specific files
/lib /CPU library files
/blackfin /Blackfin架构
/x86 /x86 架构
/m68k /m68k 架构
/microblaze /microblaze 架构
/mips /MIPS 架构
/nios2 /NIOS2 架构
/powerpc /PowerPC 架构
/sh /SH 架构
/sparc /SPARC 架构
/api /为拓展应用准备的,与硬件和架构无关的接口定义文件
/board /常用主板的文件,我们增加的主板文件也需要在这里新增目录后进行开发
/common /与架构无关的通用接口
/disk /实现磁盘分区的接口
/doc /常见功能和问题的说明文档
/drivers /常用的设备驱动程序
/examples /Demo Example code
/fs /文件系统(cramfs, ext2, jffs2, etc.)
/include /全局需要的头文件定义在这儿
/lib /所以架构通用的ib
/libfdt Library files to support flattened device trees
/lzma Library files to support LZMA decompression
/lzo Library files to support LZO decompression
/net /网络相关代码
/post /Power On Self Test 开机自检
/rtc /实时时钟驱动
/tools /Tools to build S-Record or U-Boot images, etc.
README再往下就是介绍各个功能宏,便于我们进行裁剪。
下面通过smdk2410的编译来分析MAKEFILE关系。
我们知道编译smdk2410要先执行make smdk2410_config,然后执行make,生成该主板的u-boot.bin,
如果不先进行配置,直接make会报错:System not configured - see README
所以主makefile有如下大的框架:
ifeq ($(obj)include/config.mk,$(wildcard $(obj)include/config.mk)) #line 145 include/config.mk文件存在 wildcard : 扩展通配符,找到所有通配的文件并返回
else # !config.mk #line 530 include/config.mk文件不存在
@echo "System not configured - see README" >&2
@ exit 1
所以我们先看如何执行make smdk2410_config,要先找到目标smdk2410_config定义的地方,makefile中定义很多配置的目标,但没有这个目标,所以我们注意到
%_config:: unconfig
@$(MKCONFIG) -A $(@:_config=)
sinclude $(obj).boards.depend
$(obj).boards.depend: boards.cfg #读取boards.cfg,生成所有XXXX_config,SMDK2410_Config就在这里产生
awk '(NF && $$1 !~ /^#/) { print $$1 ": " $$1 "_config; $$(MAKE)" }' $< > $@
注意到主目录下的.boards.depend文件和boards.cfg文件,MKCONFIG由
MKCONFIG := $(SRCTREE)/mkconfig
export MKCONFIG
定义
所以主目录下mkconfig文件来生成include下config.mk和config.h,注意里面有一个 cd ./include,来进入include文件夹的。
进行到此,例如生成了如下config.mk:
ARCH = arm
CPU = arm920t
BOARD = smdk2410
VENDOR = samsung
SOC = s3c24x0
生成了如下config.h:
/* Automatically generated - do not edit */
#define CONFIG_BOADDIR board/samsung/smdk2410 // BOARDDIR=${vendor}/${board}
#include <config_cmd_defaults.h>
#include <config_default.h>
#include <configs/smdk2410.h>
#include <asm/config.h> // ln -s ${SRCTREE}/arch/${arch}/include/asm asm
config.h约束了全局使用到的功能。
下面分析make命令:
先创建两个实目标(非.PHONY ):
TIMESTAMP_FILE = $(obj)include/timestamp_autogenerated.h
$(TIMESTAMP_FILE):
@LC_ALL=C date +'#define U_BOOT_DATE "%b %d %C%y"' > $@
@LC_ALL=C date +'#define U_BOOT_TIME "%T"' >> $@
VERSION_FILE = $(obj)include/version_autogenerated.h
$(VERSION_FILE):
@( localvers='$(shell $(TOPDIR)/tools/setlocalversion $(TOPDIR))' ; \
printf '#define PLAIN_VERSION "%s%s"\n' \
"$(U_BOOT_VERSION)" "$${localvers}" ; \
printf '#define U_BOOT_VERSION "U-Boot %s%s"\n' \
"$(U_BOOT_VERSION)" "$${localvers}" ; \
) > $@.tmp
@( printf '#define CC_VERSION_STRING "%s"\n' \
'$(shell $(CC) --version | head -n 1)' )>> $@.tmp
@( printf '#define LD_VERSION_STRING "%s"\n' \
'$(shell $(LD) -v | head -n 1)' )>> $@.tmp
@cmp -s $@ $@.tmp && rm -f $@.tmp || mv -f $@.tmp $@
然后执行第一个目标:
all: $(ALL)
ALL目标又依赖与:
# Always append ALL so that arch config.mk's can add custom ones
ALL += $(obj)u-boot.srec $(obj)u-boot.bin $(obj)System.map
ifeq ($(CONFIG_NAND_U_BOOT),y)
ALL += $(obj)u-boot-nand.bin
endif
ifeq ($(CONFIG_ONENAND_U_BOOT),y)
ALL += $(obj)u-boot-onenand.bin
ONENAND_BIN ?= $(obj)onenand_ipl/onenand-ipl-2k.bin
endif
ifeq ($(CONFIG_MMC_U_BOOT),y)
ALL += $(obj)mmc_spl/u-boot-mmc-spl.bin
endif
各目标又依赖与:
$(obj)u-boot.hex: $(obj)u-boot
$(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@
$(obj)u-boot.srec: $(obj)u-boot
$(OBJCOPY) -O srec $< $@
$(obj)u-boot.bin: $(obj)u-boot
$(OBJCOPY) ${OBJCFLAGS} -O binary $< $@
$(BOARD_SIZE_CHECK)
$(obj)u-boot.ldr: $(obj)u-boot
$(CREATE_LDR_ENV)
$(LDR) -T $(CONFIG_BFIN_CPU) -c $@ $< $(LDR_FLAGS)
$(BOARD_SIZE_CHECK)
$(obj)u-boot.ldr.hex: $(obj)u-boot.ldr
$(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@ -I binary
$(obj)u-boot.ldr.srec: $(obj)u-boot.ldr
$(OBJCOPY) ${OBJCFLAGS} -O srec $< $@ -I binary
$(obj)u-boot.img: $(obj)u-boot.bin
$(obj)tools/mkimage -A $(ARCH) -T firmware -C none \
-a $(CONFIG_SYS_TEXT_BASE) -e 0 \
-n $(shell sed -n -e 's/.*U_BOOT_VERSION//p' $(VERSION_FILE) | \
sed -e 's/"[ ]*$$/ for $(BOARD) board"/') \
-d $< $@
$(obj)u-boot.imx: $(obj)u-boot.bin
$(obj)tools/mkimage -n $(CONFIG_IMX_CONFIG) -T imximage \
-e $(CONFIG_SYS_TEXT_BASE) -d $< $@
$(obj)u-boot.kwb: $(obj)u-boot.bin
$(obj)tools/mkimage -n $(CONFIG_SYS_KWD_CONFIG) -T kwbimage \
-a $(CONFIG_SYS_TEXT_BASE) -e $(CONFIG_SYS_TEXT_BASE) -d $< $@
$(obj)u-boot.sha1: $(obj)u-boot.bin
$(obj)tools/ubsha1 $(obj)u-boot.bin
$(obj)u-boot.dis: $(obj)u-boot
$(OBJDUMP) -d $< > $@
$(obj)u-boot目标又依赖与:
$(obj)u-boot: depend \
$(SUBDIRS) $(OBJS) $(LIBBOARD) $(LIBS) $(LDSCRIPT) $(obj)u-boot.lds
$(GEN_UBOOT)
SUBDIRS目标:
# The "tools" are needed early, so put this first
# Don't include stuff already done in $(LIBS)
SUBDIRS = tools \
examples/standalone \
examples/api
$(SUBDIRS): depend
$(MAKE) -C $@ all
OBJS目标:
OBJS = $(CPUDIR)/start.o #这个是系统总入口
ifeq ($(CPU),x86)
OBJS += $(CPUDIR)/start16.o
OBJS += $(CPUDIR)/resetvec.o
endif
ifeq ($(CPU),ppc4xx)
OBJS += $(CPUDIR)/resetvec.o
endif
ifeq ($(CPU),mpc85xx)
OBJS += $(CPUDIR)/resetvec.o
endif
OBJS := $(addprefix $(obj),$(OBJS))
$(OBJS): depend
$(MAKE) -C $(CPUDIR) $(if $(REMOTE_BUILD),$@,$(notdir $@)) #编译notdir OBJS各项
LIBBOARD目标:
LIBBOARD = board/$(BOARDDIR)/lib$(BOARD).o
LIBBOARD := $(addprefix $(obj),$(LIBBOARD))
$(LIBBOARD): depend $(LIBS)
$(MAKE) -C $(dir $(subst $(obj),,$@)) #将$(obj)和LIBBOARD联合后取目录编译
LIBS目标:
LIBS = lib/libgeneric.o
LIBS += lib/lzma/liblzma.o
LIBS += lib/lzo/liblzo.o
LIBS += lib/zlib/libz.o
LIBS += $(shell if [ -f board/$(VENDOR)/common/Makefile ]; then echo \
"board/$(VENDOR)/common/lib$(VENDOR).o"; fi)
LIBS += $(CPUDIR)/lib$(CPU).o
ifdef SOC
LIBS += $(CPUDIR)/$(SOC)/lib$(SOC).o
endif
ifeq ($(CPU),ixp)
LIBS += arch/arm/cpu/ixp/npe/libnpe.o
endif
LIBS += arch/$(ARCH)/lib/lib$(ARCH).o
LIBS += fs/cramfs/libcramfs.o fs/fat/libfat.o fs/fdos/libfdos.o fs/jffs2/libjffs2.o \
fs/reiserfs/libreiserfs.o fs/ext2/libext2fs.o fs/yaffs2/libyaffs2.o \
fs/ubifs/libubifs.o
LIBS += net/libnet.o
LIBS += disk/libdisk.o
LIBS += drivers/bios_emulator/libatibiosemu.o
LIBS += drivers/block/libblock.o
LIBS += drivers/dma/libdma.o
LIBS += drivers/fpga/libfpga.o
LIBS += drivers/gpio/libgpio.o
LIBS += drivers/hwmon/libhwmon.o
LIBS += drivers/i2c/libi2c.o
LIBS += drivers/input/libinput.o
LIBS += drivers/misc/libmisc.o
LIBS += drivers/mmc/libmmc.o
LIBS += drivers/mtd/libmtd.o
LIBS += drivers/mtd/nand/libnand.o
LIBS += drivers/mtd/onenand/libonenand.o
LIBS += drivers/mtd/ubi/libubi.o
LIBS += drivers/mtd/spi/libspi_flash.o
LIBS += drivers/net/libnet.o
LIBS += drivers/net/phy/libphy.o
LIBS += drivers/pci/libpci.o
LIBS += drivers/pcmcia/libpcmcia.o
LIBS += drivers/power/libpower.o
LIBS += drivers/spi/libspi.o
ifeq ($(CPU),mpc83xx)
LIBS += drivers/qe/libqe.o
LIBS += arch/powerpc/cpu/mpc8xxx/lib8xxx.o
endif
ifeq ($(CPU),mpc85xx)
LIBS += drivers/qe/libqe.o
LIBS += arch/powerpc/cpu/mpc8xxx/ddr/libddr.o
LIBS += arch/powerpc/cpu/mpc8xxx/lib8xxx.o
endif
ifeq ($(CPU),mpc86xx)
LIBS += arch/powerpc/cpu/mpc8xxx/ddr/libddr.o
LIBS += arch/powerpc/cpu/mpc8xxx/lib8xxx.o
endif
LIBS += drivers/rtc/librtc.o
LIBS += drivers/serial/libserial.o
LIBS += drivers/twserial/libtws.o
LIBS += drivers/usb/eth/libusb_eth.o
LIBS += drivers/usb/gadget/libusb_gadget.o
LIBS += drivers/usb/host/libusb_host.o
LIBS += drivers/usb/musb/libusb_musb.o
LIBS += drivers/usb/phy/libusb_phy.o
LIBS += drivers/video/libvideo.o
LIBS += drivers/watchdog/libwatchdog.o
LIBS += common/libcommon.o
LIBS += lib/libfdt/libfdt.o
LIBS += api/libapi.o
LIBS += post/libpost.o
ifeq ($(SOC),omap3)
LIBS += $(CPUDIR)/omap-common/libomap-common.o
endif
ifeq ($(SOC),omap4)
LIBS += $(CPUDIR)/omap-common/libomap-common.o
endif
ifeq ($(SOC),s5pc1xx)
LIBS += $(CPUDIR)/s5p-common/libs5p-common.o
endif
ifeq ($(SOC),s5pc2xx)
LIBS += $(CPUDIR)/s5p-common/libs5p-common.o
endif
LIBS := $(addprefix $(obj),$(sort $(LIBS)))
$(LIBS): depend $(SUBDIRS)
$(MAKE) -C $(dir $(subst $(obj),,$@)) #将$(obj)和LIBS联合后取目录编译
其他的目标比较好找。
依此往上,最后生成u-boot.bin
后续希望自己能完成:
Uboot硬件启动 u-boot-2011.06\arch\arm\cpu\arm920t\start.S
Uboot系统各模块初始化
各模块Command交互