u-boot分析二

u-boot分析

Posted by hades on April 27, 2018

u-boot分析二

继续分析编译过程 boot编译的方式是将各个目录下的源码编译成.o,然后通过mips-linux-uclibc-ar

命令创建成静态库, 
./lib_bootstrap/libbootstrap.a 
./cpu/mips/libmips.a 
./board/atheros/board955x/libboard955x.a 
./lib_mips/libmips.a 
./lib_generic/libgeneric.a 
./common/libcommon.a 
./drivers/libdrivers.a 
./rtc/librtc.a 
./net/libnet.a 
./post/libpost.a 
./post/cpu/libcpu.a 

到最后通过连接脚本将这些静态库链接成u-boot u-boot通过调用lzma压缩,再调用mkimage制作镜像。 mkimage主要是给压缩后的镜像添加一个头,这个头的结构如下

typedef struct image_header { 
  uint32_t ih_magic; /* Image Header Magic Number*/ 
  uint32_t ih_hcrc; /* Image Header CRC Checksum*/ 
  uint32_t ih_time; /* Image Creation Timestamp*/ 
  uint32_t ih_size; /* Image Data Size*/ 
  uint32_t ih_load; /* DataLoad Address*/ 
  uint32_t ih_ep; /* Entry Point Address*/ 
  uint32_t ih_dcrc; /* Image Data CRC Checksum*/ 
  uint8_t ih_os; /* Operating System*/ 
  uint8_t ih_arch; /* CPU architecture*/ 
  uint8_t ih_type; /* Image Type*/ 
  uint8_t ih_comp; /* Compression Type*/ 
  uint8_t ih_name[IH_NMLEN]; /* Image Name*/ 
} image_header_t;

其中 uint32_t ih_load; /* DataLoad Address*/ uint32_t ih_ep; /* Entry Point Address 是两个关键的地址,ih_load是解压uboot到的目的地址,ih_ep是程序的入口地址。 到此,uboot编译完成,生成的uboot.bin的结构如下:

++++++++++++++++++++++++++++++ 
|bootstrap.bin|image header|u-boot.lzimg | 
++++++++++++++++++++++++++++++

然后 开始上电初始化过程 汇编初始化 根据硬件特性程序从入口Entry(_start)开始执行,主要的操作在start_bootstrap.S中进行,主要的操作:

  • 初始化时钟,DDR,cache、禁止time中断
  • CPU PLL CONFIG
  • SDRM或 DDR initialization
  • Initialize caches(bal mips_cache_reset),建立暂时栈空间
  • Initialize GOT pointer//为调用函数建立的符号表指针
  • 进入第一个c函数bootstrap_board_init_f:

    la t9, bootstrap_board_init_f j t9 bootstrap_board_init_f先执行初始化函数指针数组init_sequence,然后开辟一个临时栈空间,在DDR的最高位置。然后计算临时栈的起始位置

    +++++++++++++++++++++++++++++++++++++++++++++++++ | |16B|128K uboot参数|gb_t|bd_t|malloc 128K|uboot的BSS&TEXT| 4K | +++++++++++++++++++++++++++++++++++++++++++++++++ 初始化一个临时栈空间,然后调用bootstrap_relocate_code,最后要清cache,然后跳转

    /* Jump to where we’ve relocated ourselves */ addi t0, a2, in_ram - _start_bootstrap j t0

然后转到in_ram,启动动bootstrap_board_init_r


	move a0, a1
	la   t9, bootstrap_board_init_r
	j    t9
	move a1, a2			/* Delay slot */

bootstrap_relocate_codestart_bootstrap.S中实现,将uboot的代码重定向到临时栈空间运行,进入函数bootstrap_board_init_r

bootstrap_board_init_r

bootstrap_board_init_r函数首先初始化128K的栈空间,然后将uboot的header取出来,检查magic number和crc32校验,通过后调用函数lzma_inflate解压uboot:

i = lzma_inflate ((unsigned char )data, len, (unsigned char)ntohl(hdr->ih_load), &destLen);  解压成功后,清除cache,跳入uboot的入口:`fn = ntohl(hdr->ih_load)`,既是0x80010000地址:
80010000 T _start 
80010030 T relocate_code 
80010094 t in_ram 
80010100 T ath_set_tuning_caps 
800101a0 T do_go 
800102c0 T print_image_hdr 
800106c8 T do_bootd 
80010718 T fake_image_header 
800107f0 T do_bootm 
800109c0 T flash_sect_erase 
80010bc8 T do_flerase 

_start函数在start.S中,,执行以下操作 清空cache 清空BSS段 正式进入C环境

la t9 board_init_r

board_init_r

board_init_r 主要执行以下的操作: 将前面临时栈一些数据(gd、bd等)copy到RAM中预留的对应位置 设置系统模式(Init_System_Mode),这里可以设置CPU时钟

/* Relocate environment function pointers etc. */
env_relocate();

.....
/* Leave this here (after malloc(), environment and PCI are working) */
/* Initialize devices */
devices_init();   //i2c、LCD、keyboard …… 

/* main_loop() can return to retry autoboot, if so just run it again */
for (;;)
	main_loop();

main_loop循环监听,无人为干预,启动系统(do_bootm),有人为干预初始化以太网其他相关操作。 main_loop中调用了parse_string_outer 通过执行命令bootm 0x9f010000 进入到函数do_bootmu-boot_mod/u-boot/common/main.c

/* Get boot command */
	bootcmd = getenv("bootcmd");

#if defined(CONFIG_BOOTCOMMAND)
	if (!bootcmd)
		setenv("bootcmd", CONFIG_BOOTCOMMAND);

	bootcmd = getenv("bootcmd");
#endif
do_bootm

do_bootm:解压缩kernel,调用do_bootm_linux

do_bootm_linux(cmdtp, flag, argc, argv);

主要功能 :

  • 复制Image(这里指的是uImage) 头到全局变量header;
  • 检查header的magic number是否正确,检查header和image各自的校验和是否正确;
  • 检查image的体系架构和类型(kernel or MULTI)
  • 将内核zImage搬移到image头中指定的内核加载地址ih_load
  • 调用u-boot_mod/lib_mips/mipslinux.c中的do_bootm_linux()函数,将传递给内核的参数存放在指定的内存位置,然后执行thekernel(0,…,…)跳转到内核入口点执行,控制权交给内核,u-boot执行完毕。
do_bootm_linux

do_bootm_linux:对kernel进行crc校验,并和header中的crc字段进行比较,设置环境变量,然后调用theKernel 进入kernel。