Uboot優美代碼賞析1:目錄結構和malkefile分析

Uboot優美代碼賞析1:目錄結構和malkefile分析html

關於Uboot本身選的版本是目前最新的2011.06,官方網址爲:http://www.denx.de/wiki/U-Boot/WebHome,下面的一些內容主要翻譯自頂層目錄的 README 。ios

U-Boot是一種基於PowerPC, ARM, MIPS 或者其餘處理器架構的嵌入式開發板的啓動引導程序(boot loader),boot loader是能夠被安裝在做爲引導的ROM上,實現初始化和測試硬件,和下載與運行應用的代碼。shell

U-Boot的開發與Linux緊密相連:部分代碼是取自Linux的源代碼,好比咱們使用共同的頭文件定義,同時特別提供了能夠用於啓動Linux內核的功能。api

DENX注重於去使這個軟件很容易去配置和拓展。好比,爲了便於用戶增長新的命令,因此的監視命令都是用一樣的接口和架構實現的。另外,不多使用的命令能夠動態地進行加載執行,好比硬件測試功能。網絡

下面介紹目錄結構:架構

/arch/存放特定架構的實現文件,系統的啓動代碼通常在這裏的start.S中app

  /arm/Arm架構框架

    /cpu/CPU相關文件ide

      /arm720t/ARM 720 CPUspost

      /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

  /libfdtLibrary files to support flattened device trees

  /lzmaLibrary files to support LZMA decompression

  /lzoLibrary 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交互

 

 

 
 
分類:  uboot源碼賞析
標籤:  uboot源碼賞析
相關文章
相關標籤/搜索