u-boot源码个别分析 

深入Bootloader系列

http://ftp.denx.de/pub/u-boot/
 

简介===>

1.U-Boot系统加载器

U-Boot是一个规模庞大的开源Bootloader软件,最初是由denx(www.denx.de发起。U-Boot的前身是PPCBoot,目前是SourceForge(www.sourceforge.net)的一个项目。

最初的U-Boot仅支持PowerPC架构的系统,称做PPCBoot。从0.3.2官方版本之后开始逐步支持多种架构的处理器,目前可以支持 PowerPC(MPC5xx、MPC8xx、MPC82xx、MPC7xx、MPC74xx)、ARM(ARM7、ARM9、StrongARM、 Xscale)、MIPS(4kc、5kc)、X86等处理器,支持的嵌入式操作系统有Linux、Vx-Works、NetBSD、QNX、 RTEMS、ARTOS、LynxOS等,是PowerPC、ARM9、Xscale、X86等系统通用的Boot方案。

U-Boot支持的处理器和操作系统很多,但是它对PowerPC系列处理器和Linux操作系统支持最好。U-Boot支持的功能也较多,对于嵌 入式开发常用的查看、修改内存,从网络下载操作系统镜像等功能都提供了很好的支持。U-Boot的项目更新较快,支持的目标板众多,是学习底层开发很好的 示例。

2.ViVi系统加载器

ViVi是韩国的mizi公司专门针对ARM9处理器设计的一款Bootloader。它的特点是操作简便,同时提供了完备的命令体系,目前在三星系列的ARM9处理器上ViVi也比较流行。

与U-Boot相比,由于ViVi支持的处理器单一,ViVi的代码也要小很多。同时,ViVi的软件架构和配置方法采用和Linux内核类似的风格,对于有过配置编译Linux内核经验的读者,ViVi更容易上手。

与其他的Bootloader一样,ViVi有两种工作模式:启动加载模式和下载模式。使用启动加载模式,在目标板上电后,ViVi会从预先配置好 的Flash分区读取Linux或者其他系统的镜像并且启动系统;使用下载模式,ViVi向用户提供了一个命令行接口,通过该接口用户可以使用ViVi提 供的命令。ViVi主要提供了5个命令如下:

Load:把二进制文件载入Flash或RAM。

Part:操作MTD分区信息。显示、增加、删除、复位、保存MTD分区。

Param:设置参数。

Boot:启动系统。

Flash:管理Flash,如删除Flash的数据。

与Linux内核的组织类似,ViVi的源代码主要包括arch、init、lib、drivers和include等几个目录,共200多个代码文件。各目录的具体功能请参考ViVi相关的信息。

=====================================================================》》》

=====================================================================》》》

=====================================================================》》》

 

基本目录分类:

common目录是与体系结构无关的文件,包括实现各种命令的C语言源代码文件。

cpu目录          存放与CPU相关的文件,每种CPU需要的代码文件存放在以CPU名称命名的子目录下,arm920t存放了arm920t为内核的 CPU相关的文件。在每个特定的子目录下都包括cpu.c、interrupt.c和start.S这3个文件,这3个文件是CPU初始化以及配置中断的 代码。U-Boot自带了很多CPU相关的代码,用户可以在现有CPU支持的基础上修改自己所需要的配置。

通用设备的驱动程序存放在drivers目录下。U-Boot自带了许多设备的驱动,包括显示芯片、网络接口控制器、USB控制器、I2C器件等,对于大多数用户而言已经够用,用户也可以按照自己的需求增加或者修改设备驱动。

fs                    存放支持的文件系统代码,U-Boot目前支持cramfs、ext2、fat、jffs、reiserfs、yaffs等多种常见的文件系统。

net目录         是与网络协议有关的代码,比如BOOTP协议、TFTP协议、RARP协议等。

post               存放与硬件自检有关的代码。

rtc目录          存放与硬件实时时钟相关的代码。

tools目录      存放U-Boot编译过程中用到的一些工具代码。  // 例如:mkimage

==========================================


hao:
start_armboot => bootm.c
向量表在_start开始阶段已经汇编搞定。

其实主要就是个寄存器和内存的基本处理。

===》》》
列出了U-Boot在ARM处理器启动过程中的几个关键点,
从图中看出U-Boot的启动代码分布在start.S、low_level_init.S、 board.c和main.c文件中。

start.S                    是U-Boot整个程序的入口,该文件使用汇编语言编写,不同体系结构的启动代码是不同 的;

low_level_init.S    特定开发板的设置代码

board.c                  包含开发板底层设备驱动

main.c是一个与平台无关的代码,U-Boot应用程序的入口在此文件中

取出CPSR寄存器的值,CPSR寄存器保存当前系统状态,

使用比特清除命令清空了CPSR寄存器的中断控制位,表示清除 中断。

设置了CPSR寄存器的处理器模式位为管理模式,然后在第117行写入 CPSR的值强制切换处理器为超级保护模式。

定义看门狗控制器有关的变量,

根据平台设置看门狗定时器。

设置时钟分频寄存器的值。

需要根据CONFIG_SKIP_LOWLEVEL_INIT宏的值是否跳转到cpu_init_crit标号执行

===========cpu_init_crit==========

                                                                cpu_init_crit标号处的代码初始化ARM处理器关键的寄存器


  1. 228 /*  
  2. 229  *****************************************************************  
  3. 230  *  
  4. 231  * CPU_init_critical registers  
  5. 232  *  
  6. 233  * setup important registers  
  7. 234  * setup memory timing  
  8. 235  *  
  9. 236  ******************************************************************  
  10. 237  */  
  11. 238   
  12. 239   
  13. 240 #ifndef CONFIG_SKIP_LOWLEVEL_INIT  
  14. 241 cpu_init_crit:  
  15. 242   /*  
  16. 243    * flush v4 I/D caches  
  17. 244    */  
  18. 245   mov r0, #0  
  19. 246   mcr p15, 0, r0, c7, c7, 0 /* flush v3/v4 cache */     // 1.刷新cache  
  20. 247   mcr p15, 0, r0, c8, c7, 0 /* flush v4 TLB */          // 2.刷新TLB  
  21. 248   
  22. 249   /*  
  23. 250    * disable MMU stuff and caches                       // 3.关闭MMU  
  24. 251    */  
  25. 252   mrc p15, 0, r0, c1, c0, 0  
  26. 253   bic r0, r0, #0x00002300 @ clear bits 13, 9:8 (--V- --RS)  
  27. 254   bic r0, r0, #0x00000087 @ clear bits 7, 2:0 (B--- -CAM)  
  28. 255   orr r0, r0, #0x00000002 @ set bit 2 (A) Align  
  29. 256   orr r0, r0, #0x00001000 @ set bit 12 (I) I-Cache  
  30. 257   mcr p15, 0, r0, c1, c0, 0  
  31. 258   
  32. 259   /*  
  33. 260    * before relocating, we have to setup RAM timing  
  34. 261    * because memory timing is board-dependend, you will  
  35. 262    * find a lowlevel_init.S in your board directory.  
  36. 263    */  
  37. 264   mov ip, lr  
  38. 265   bl  lowlevel_init   // 跳转到lowlevel_init  
  39. 266   mov lr, ip  
  40. 267   mov pc, lr  
  41. 268 #endif /* CONFIG_SKIP_LOWLEVEL_INIT */ 

 

 

==>2.

TLB的作用是在处理器访问内存数据的时候做地址转换。TLB的全称是Translation Lookaside Buffer,可以翻译做旁路缓冲。

TLB中存放了一些页表文件,文件中记录了虚拟地址和物理地址的映射关系。当应用程序访问一个虚拟地址的时候,会从 TLB中查询出对应的物理地址,然后访问物理地址。TLB通常是一个分层结构,使用与Cache类似的原理。处理器使用一定的算法把最常用的页表放在最先 访问的层次。

==>3.

程序第252~257行关闭MMU。MMU是内存管理单元(Memory Management Unit)的缩写。在现代计算机体系结构上,MMU被广泛应用。使用MMU技术可以向应用程序提供一个巨大的虚拟地址空间。在U-Boot初始化的时候, 程序看到的地址都是物理地址,无须使用MMU。

 

 


=========================lowlevel_init=========================  

                                                                                  位于board/smdk2410/lowlevel_init.S文件
                                         开发板相关的初始化配置

  1. 133 lowlevel_init:  
  2. 134   /* memory control configuration */  
  3. 135   /* make r0 relative the current location so that it */  
  4. 136   /* reads SMRDATA out of FLASH rather than memory ! */  
  5. 137   ldr      r0,   =SMRDATA                      // 读取SMRDATA变量地址  
  6. 138   ldr  r1,  _TEXT_BASE                        // 读取_TEXT_BASE变量地址  
  7. 139   sub    r0,  r0, r1                                 // 得出相对偏移
  8. 140   ldr      r1,  =BWSCON                        // 主要是了解BANK的位宽,16位
  9. 141   add    r2,     r0,  #13*4                      // 得到SMRDATA占用的大小,结尾处的偏移
  10. 142 0:  
  11. 143   ldr     r3, [r0], #4                               // 加载SMRDATA到内存  ,相当于一个while循环
  12. 144   str     r3, [r1], #4  
  13. 145   cmp     r2, r0  
  14. 146   bne     0b                                        // 循环,相当于while循环
  15. 147   
  16. 148   /* everything is fine now */  
  17. 149   mov pc, lr  
  18. 152 /* the literal pools origin */  
  19. 153   
  20. 154 SMRDATA:            // 定义SMRDATA值  
  21. 155     .word (0+(B1_BWSCON<<4)+(B2_BWSCON<<8)+(B3_BWSCON<<12)+(B4_BWSCON   
  22.         <<16)+(B5_BWSCON<<20)+(B6_BWSCON<<24)+(B7_BWSCON<<28))  
  23. 156     .word ((B0_Tacs<<13)+(B0_Tcos<<11)+(B0_Tacc<<8)+(B0_ Tcoh<<6)+  
  24.         (B0_Tah<<4)+(B0_Tacp<<2)+(B0_PMC))  
  25. 157     .word ((B1_Tacs<<13)+(B1_Tcos<<11)+(B1_Tacc<<8)+(B1_Tcoh<<6)+   
  26.         (B1_Tah<<4)+(B1_Tacp<<2)+(B1_PMC))  
  27. 158     .word ((B2_Tacs<<13)+(B2_Tcos<<11)+(B2_Tacc<<8)+(B2_Tcoh<<6)+   
  28.         (B2_Tah<<4)+(B2_Tacp<<2)+(B2_PMC))  
  29. 159     .word ((B3_Tacs<<13)+(B3_Tcos<<11)+(B3_Tacc<<8)+(B3_Tcoh<<6)+   
  30.         (B3_Tah<<4)+(B3_Tacp<<2)+(B3_PMC))  
  31. 160     .word ((B4_Tacs<<13)+(B4_Tcos<<11)+(B4_Tacc<<8)+(B4_Tcoh<<6)+   
  32.         (B4_Tah<<4)+(B4_Tacp<<2)+(B4_PMC))  
  33. 161     .word ((B5_Tacs<<13)+(B5_Tcos<<11)+(B5_Tacc<<8)+(B5_Tcoh<<6)+   
  34.         (B5_Tah<<4)+(B5_Tacp<<2)+(B5_PMC))  
  35. 162     .word ((B6_MT<<15)+(B6_Trcd<<2)+(B6_SCAN))  
  36. 163     .word ((B7_MT<<15)+(B7_Trcd<<2)+(B7_SCAN))  
  37. 164     .word ((REFEN<<23)+(TREFMD<<22)+(Trp<<20)+(Trc<<18)+   
  38.         (Tchr<<16)+REFCNT)  
  39. 165     .word 0x32  
  40. 166     .word 0x30  
  41. 167     .word 0x30 

程序第137~141行计算SMRDATA需要加载的内存地址和大小。首先在137行读取SMRDATA的变量地址,之后计算存放的内存地址并且记录在r0寄存器,然后根据总线宽度计算需要加载的SMRDATA大小,并且把加载结束地址存放在r2寄存器。

程序第142~146行复制SMRDATA到内存SMRDATA是开发板上内存映射的配置

正式开始了第二阶段:

relocate部分的代码负责把U-Boot Stage2的代码从Flash存储器加载到内存,代码如下:

  1. 163 #ifndef CONFIG_SKIP_RELOCATE_UBOOT  
  2. 164 relocate:       /* relocate U-Boot to RAM     */  
  3. 165   adr  r0,  _start     /* r0 <- current position of code   */    
  4.                            // 获取当前代码存放地址  00000000
  5. 166   ldr  r1, _TEXT_BASE    /* test if we run from flash or RAM */  
  6.                              // 获取内存存放代码地址  33f80000
  7. 167   cmp     r0,  r1        /* don't reloc during debug         */  
  8.                              //地址相同说明程序已经在内存中 则不需要加载  
  9. 168   beq     stack_setup  
  10. 169   //开始加载
  11. 170   ldr  r2, _armboot_start    // 获取stage2代码存放地址  
  12. 171   ldr r3,  _bss_start          // 获取内存代码段起始地址  
  13. 172   sub  r2,  r3, r2           /* r2 <- size of armboot  */    
  14.                                  // 不包括向量表,U-BOOT的整个大小 
  15. 173   add  r2,  r0,  r2    /* r2 <- source end address         */    
  16.                        33f80000 size         // 计算stage2代码结束地址 
  17. 174   
  18. 175 copy_loop:  
  19. 176   ldmia  r0!,  {r3-r10}   /* copy from source address [r0]    */  
  20.                               // 从Flash复制代码到内存  
  21. 177   stmia r1!,  {r3-r10}    /* copy to   target address [r1]    */  
  22. 178   cmp  r0,  r2            /* until source end addreee [r2]    */  
  23. 179   ble  copy_loop  
  24. 180 #endif   /* CONFIG_SKIP_RELOCATE_UBOOT */  
  25. 181   
  26. 182   /* Set up the stack */    
  27.       // 在内存中建立堆栈  
  28. 183 stack_setup:  
  29. 184   ldr r0, _TEXT_BASE               /* upper 128 KiB: relocated uboot   */  
  30. 185   sub r0, r0, #CFG_MALLOC_LEN      /* malloc area    */  // 分配内存区域  
  31. 186   sub r0, r0, #CFG_GBL_DATA_SIZE   /* bdinfo  */  
  32. 187 #ifdef CONFIG_USE_IRQ  
  33. 188   sub r0, r0, #(CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ)  
  34. 189 #endif  
  35. 190   sub sp, r0, #12      /* leave 3 words for abort-stack    */  
  36. 191   
  37. 192 clear_bss:                 // 初始化内存bss段内容为0  
  38. 193   ldr r0, _bss_start       /* find start of bss segment        */  
  39.                                // 查找bss段起始地址  
  40. 194   ldr  r1,  _bss_end       /* stop here      */  // 查找bss段结束地址  
  41. 195   mov   r2,  #0x00000000   /* clear   */ // 清空bss段内容  
  42. 196   
  43. 197  clbss_l: str r2, [r0]     /* clear loop...                    */  
  44. 198   add  r0,  r0,  #4  
  45. 199   cmp  r0,  r1  
  46. 200   ble  clbss_l  
  47. 223   ldr  pc,  _start_armboot     // 设置程序指针为start_armboot()函数地址  
  48. 224   
  49. 225 _start_armboot:
  50. .word  start_armboot //这里是个C的函数名字,也就是入口地址

代码解释:

    程序首先在165~168行检查当前是否在内存中执行代码,根据结果决定是否需要从Flash存储器加载代码。程序通过获取_start和_TEXT_BASE所在的地址比较,如果地址相同说明程序已经在内存中,无须加载。

    程序第170~173行计算要加载的Stage2代码起始地址和长度,然后在第176~179行循环复制Flash的数据到内存,每次可以复制8个字长的数据。

    Stage2程序复制完毕后,程序第184~190行设置系统堆栈,最后在第193~200行清空内存bss段的内容。

    relocate程序最后在223行设置程序指针寄存器为start_armboot()函数地址,程序跳转到Stage2部分执行。请注意第 225行的定义,_start_armboot全局变量的值是C语言函数start_armboot()函数的地址,使用这种方式可以在汇编中调用C语言 编写的函数。   

这里就是之前启动的C的函数:start_armboot()
<lib_arm/board.c>
start_armboot()函数主要初始化ARM系统的硬件和环境变量,包括Flash存储器、FrameBuffer、网卡等,最后进入U-Boot应用程序主循环

=========================================

  1. 236 void start_armboot (void)  
  2. 237 {  
  3. 238    init_fnc_t **init_fnc_ptr;  
  4. 239    char *s;  
  5. 240 #ifndef CFG_NO_FLASH  
  6. 241   ulong size;  
  7. 242 #endif  
  8. 243 #if defined(CONFIG_VFD) || defined(CONFIG_LCD)  
  9. 244    unsigned long addr;  
  10. 245 #endif  
  11. 246   
  12. 247   /* Pointer is writable since we allocated a register for it */  
  13. 248   gd = (gd_t*)(_armboot_start - CFG_MALLOC_LEN - sizeof(gd_t));  //全局的系统初始化参数
  14. 249   /* compiler optimization barrier needed for GCC >= 3.4 */  
  15. 250   __asm__ __volatile__("": : :"memory");  
  16. 251   
  17. 252   memset( (void*)gd,  0,  sizeof (gd_t) );  
  18. 253   gd->bd = (bd_t*)( (char*)gd  -  sizeof(bd_t) );  
  19. 254   memset ( gd->bd,  0,  sizeof (bd_t) );   //初始化了板子的参数
  20. 255   
  21. 256   monitor_flash_len = _bss_start - _armboot_start;  
  22. 257   
  23. 258   for (init_fnc_ptr = init_sequence;  *init_fnc_ptr;  ++init_fnc_ptr) {  
  24. 259      if ((*init_fnc_ptr)() != 0) {  
  25. 260        hang ();  
  26. 261     }  
  27. 262   }  
  28. 263   
  29. 264 #ifndef CFG_NO_FLASH  
  30. 265   /* configure available FLASH banks */  
  31. 266    size = flash_init ();          // 初始化 Flash存储器 配置  
  32. 267    display_flash_config (size);   // 显示 Flash存储器 配置  
  33. 268 #endif /* CFG_NO_FLASH */  
  34. 269   
  35. 270 #ifdef CONFIG_VFD  
  36. 271 # ifndef PAGE_SIZE  
  37. 272 #   define PAGE_SIZE 4096  
  38. 273 # endif  
  39. 274   /*  
  40. 275    * reserve memory for VFD display (always full pages)  
  41. 276    */  
  42. 277   /* bss_end is defined in the board-specific linker script */  
  43. 278   addr = (_bss_end + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);  
  44.                                             // 计算FrameBuffer 内存地址  
  45. 279   size = vfd_setmem (addr);             // 设置FrameBuffer 占用内存大小 
  46. 280   gd->fb_base = addr;                  // 设置FrameBuffer 内存起始地址  
  47. 281 #endif /* CONFIG_VFD */  
  48. 282   
  49. 283 #ifdef CONFIG_LCD  
  50. 284 # ifndef PAGE_SIZE  
  51. 285 #   define PAGE_SIZE 4096  
  52. 286 # endif  
  53. 287   /*  
  54. 288    * reserve memory for LCD display (always full pages)  
  55. 289    */  
  56. 290   /* bss_end is defined in the board-specific linker script */  
  57. 291   addr = (_bss_end + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);  
  58.                                             // 计算FrameBuffer内存地址  
  59. 292   size = lcd_setmem (addr);             // 设置FrameBuffer大小  
  60. 293   gd->fb_base = addr;                   // 设置FrameBuffer内存起始地址  
  61. 294 #endif /* CONFIG_LCD */  
  62. 295   
  63. 296   /* armboot_start is defined in the board-specific linker script */  
  64. 297   mem_malloc_init (_armboot_start - CFG_MALLOC_LEN);  
  65. 298   
  66. 299 #if (CONFIG_COMMANDS & CFG_CMD_NAND)  
  67. 300    puts ("NAND:  ");  
  68. 301   nand_init();    /* go init the NAND */     // 初始化NAND Flash存储器  
  69. 302 #endif  
  70. 303   
  71. 304 #ifdef CONFIG_HAS_DATAFLASH  
  72. 305   AT91F_DataflashInit();                 // 初始化Hash表  
  73. 306   dataflash_print_info();  
  74. 307 #endif  
  75. 308   
  76. 309   /* initialize environment */  
  77. 310   env_relocate ();                                   // 重新设置环境变量  
  78. 311       
  79. 312 #ifdef CONFIG_VFD  
  80. 313   /* must do this after the framebuffer is allocated */  
  81. 314   drv_vfd_init();                                    // 初始化虚拟显示设备  
  82. 315 #endif /* CONFIG_VFD */  
  83. 316   
  84. 317   /* IP Address */  
  85. 318   gd->bd->bi_ip_addr = getenv_IPaddr ("ipaddr");     // 设置网卡的IP地址  
  86. 319   
  87. 320   /* MAC Address */  
  88. 321   {  
  89. 322     int i;  
  90. 323     ulong reg;  
  91. 324     char *s, *e;  
  92. 325     char tmp[64];  
  93. 326   
  94. 327     i = getenv_r ("ethaddr", tmp, sizeof (tmp));      // 从网卡寄存器读取  MAC地址  
  95. 328     s = (i > 0) ? tmp : NULL;  
  96. 329   
  97. 330     for (reg = 0; reg < 6; ++reg) {  
  98. 331       gd->bd->bi_enetaddr[reg] = s ? simple_strtoul (s, &e, 16) : 0;  
  99. 332       if (s)  
  100. 333         s = (*e) ? e + 1 : e;  
  101. 334     }  
  102. 335   
  103. 336 #ifdef CONFIG_HAS_ETH1  
  104. 337     i = getenv_r ("eth1addr", tmp, sizeof (tmp));    // 读取Hash值  
  105. 338     s = (i > 0) ? tmp : NULL;  
  106. 339   
  107. 340     for (reg = 0; reg < 6; ++reg) {  
  108. 341       gd->bd->bi_enet1addr[reg] = s ? simple_strtoul (s, &e, 16) : 0;  
  109. 342       if (s)  
  110. 343         s = (*e) ? e + 1 : e;  
  111. 344     }  
  112. 345 #endif  
  113. 346   }  
  114. 347   
  115. 348   devices_init ();   /* get the devices list going. */  
  116.                                           // 初始化开发板上的设备  
  117. 349   
  118. 350 #ifdef CONFIG_CMC_PU2  
  119. 351   load_sernum_ethaddr ();  
  120. 352 #endif /* CONFIG_CMC_PU2 */  
  121. 353   
  122. 354   jumptable_init ();                 // 初始化跳转表  
  123. 355   
  124. 356   console_init_r ();   /* fully init console as a device */  
  125.                            // 初始化控制台  
  126. 357   
  127. 358 #if defined(CONFIG_MISC_INIT_R)  
  128. 359   /* miscellaneous platform dependent initialisations */  
  129. 360   misc_init_r ();                    // 初始化其他设备  
  130. 361 #endif  
  131. 362   
  132. 363   /* enable exceptions */  
  133. 364   enable_interrupts ();              // 打开中断  
  134. 365   
  135. 366   /* Perform network card initialisation if necessary */  
  136. 367 #ifdef CONFIG_DRIVER_CS8900  
  137. 368   cs8900_get_enetaddr (gd->bd->bi_enetaddr);    // 获取CS8900网卡MAC地址  
  138. 369 #endif  
  139. 370   
  140. 371 #if defined(CONFIG_DRIVER_SMC91111) || defined (CONFIG_DRIVER_   
  141. LAN91C96)  
  142. 372   if (getenv ("ethaddr")) {  
  143. 373     smc_set_mac_addr(gd->bd->bi_enetaddr);      // 设置SMC网卡MAC地址  
  144. 374   }  
  145. 375 #endif /* CONFIG_DRIVER_SMC91111 || CONFIG_DRIVER_LAN91C96 */  
  146. 376   
  147. 377   /* Initialize from environment */  
  148. 378   if ((s = getenv ("loadaddr")) != NULL) {  
  149. 379     load_addr = simple_strtoul (s, NULL, 16);  
  150. 380   }  
  151. 381 #if (CONFIG_COMMANDS & CFG_CMD_NET)  
  152. 382   if ((s = getenv ("bootfile")) != NULL) {  
  153. 383     copy_filename (BootFile, s, sizeof (BootFile));  // 保存FrameBuffer  
  154. 384   }  
  155. 385 #endif  /* CFG_CMD_NET */  
  156. 386   
  157. 387 #ifdef BOARD_LATE_INIT  
  158. 388   board_late_init ();                           // 开发板相关设备初始化  
  159. 389 #endif  
  160. 390 #if (CONFIG_COMMANDS & CFG_CMD_NET)  
  161. 391 #if defined(CONFIG_NET_MULTI)  
  162. 392   puts ("Net:   ");  
  163. 393 #endif  
  164. 394   eth_initialize(gd->bd);  
  165. 395 #endif  
  166. 396   /* main_loop() can return to retry autoboot, if so just run it again. */  
  167. 397   for (;;) {  
  168. 398     main_loop ();                               // 进入主循环  
  169. 399   }  
  170. 400   
  171. 401   /* NOTREACHED - no way out of command loop except booting */  
  172. 402 } 

    start_armboot()函数代码里有许多的宏开关,供用户根据自己开发板的情况进行配置。在start_armboot()函数第388行调用board_late_init()函数,该函数是开发板提供的,供不同的开发板做一些特有的初始化工作。

    在start_armboot()函数中,使用宏开关括起来的代码是在各种开发板上最常用的功能,如 CS8900网卡配置。整个函数配置完毕后,进 入一个for死循环,调用main_loop()函数。请读者注意,在main_loop()函数中也有一个for死循环。 start_armboot()函数使用死循环调用main_loop()函数,作用是防止main_loop()函数开始的初始化代码如果调用失败后重 新执行初始化操作,保证程序能进入到U-Boot的命令行。

    main_loop()函数做的都是与具体平台无关的工作,主要包括初始化启动次数限制机制、设置软件版本号、打印启动信息、解析命令等。

(1)设置启动次数有关参数。在进入main_loop()函数后,首先是根据配置加载已经保留的启动次数,并且根据配置判断是否超过启动次数。代码如下:

  1. 295 void main_loop (void)  
  2. 296 {  
  3. 297 #ifndef CFG_HUSH_PARSER  
  4. 298   static char lastcommand[CFG_CBSIZE] = { 0, };  
  5. 299   int len;  
  6. 300   int rc = 1;  
  7. 301   int flag;  
  8. 302 #endif  
  9. 303   
  10. 304 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)  
  11. 305   char *s;  
  12. 306   int bootdelay;  
  13. 307 #endif  
  14. 308 #ifdef CONFIG_PREBOOT  
  15. 309   char *p;  
  16. 310 #endif  
  17. 311 #ifdef CONFIG_BOOTCOUNT_LIMIT  
  18. 312   unsigned long bootcount = 0;  
  19. 313   unsigned long bootlimit = 0;  
  20. 314   char *bcs;  
  21. 315   char bcs_set[16];  
  22. 316 #endif /* CONFIG_BOOTCOUNT_LIMIT */  
  23. 317   
  24. 318 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)  
  25. 319   ulong bmp = 0;    /* default bitmap */  
  26. 320   extern int trab_vfd (ulong bitmap);  
  27. 321   
  28. 322 #ifdef CONFIG_MODEM_SUPPORT  
  29. 323   if (do_mdm_init)  
  30. 324     bmp = 1;  /* alternate bitmap */  
  31. 325 #endif  
  32. 326   trab_vfd (bmp);  
  33. 327 #endif  /* CONFIG_VFD && VFD_TEST_LOGO */  
  34. 328   
  35. 329 #ifdef CONFIG_BOOTCOUNT_LIMIT  
  36. 330   bootcount = bootcount_load();         // 加载保存的启动次数  
  37. 331   bootcount++;                          // 启动次数加1  
  38. 332   bootcount_store (bootcount);          // 更新启动次数  
  39. 333   sprintf (bcs_set, "%lu", bootcount);  // 打印启动次数  
  40. 334   setenv ("bootcount", bcs_set);  
  41. 335   bcs = getenv ("bootlimit");  
  42. 336   bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;  
  43.                                             // 转换启动次数字符串为UINT类型  
  44. 337 #endif /* CONFIG_BOOTCOUNT_LIMIT */ 

第329~337行是启动次数限制功能,启动次数限制可以被用户设置一个启动次数,然后保存在Flash存储器的特定位置,当到达启动次数后,U-Boot无法启动。该功能适合一些商业产品,通过配置不同的License限制用户重新启动系统。

(2)程序第339~348行是Modem功能。如果系统中有Modem,打开该功能可以接受其他用户通过电话网络的拨号请求。Modem功能通常供一些远程控制的系统使用,代码如下:

  1. 339 #ifdef CONFIG_MODEM_SUPPORT  
  2. 340   debug ("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);  
  3. 341   if (do_mdm_init) {                            // 判断是否需要初始化Modem  
  4. 342     char *str = strdup(getenv("mdm_cmd"));      // 获取Modem参数  
  5. 343     setenv ("preboot", str);  /* set or delete definition */  
  6. 344     if (str != NULL)  
  7. 345       free (str);  
  8. 346     mdm_init();   /* wait for modem connection */ // 初始化Modem  
  9. 347   }  
  10. 348 #endif   /* CONFIG_MODEM_SUPPORT */

(3)接下来设置U-Boot的版本号初始化命令自动完成功能等。代码如下:

  1. 350 #ifdef CONFIG_VERSION_VARIABLE  
  2. 351   {  
  3. 352     extern char version_string[];  
  4. 353   
  5. 354     setenv ("ver", version_string);   /* set version variable */   
  6.                                              // 设置版本号  
  7. 355   }  
  8. 356 #endif /* CONFIG_VERSION_VARIABLE */  
  9. 357   
  10. 358 #ifdef CFG_HUSH_PARSER  
  11. 359   u_boot_hush_start ();                 // 初始化Hash功能  
  12. 360 #endif  
  13. 361   
  14. 362 #ifdef CONFIG_AUTO_COMPLETE  
  15. 363   install_auto_complete();              // 初始化命令自动完成功能  
  16. 364 #endif  
  17. 365   
  18. 366 #ifdef CONFIG_PREBOOT  
  19. 367   if ((p = getenv ("preboot")) != NULL) {  
  20. 368 # ifdef CONFIG_AUTOBOOT_KEYED  
  21. 369     int prev = disable_ctrlc(1);   /* disable Control C checking */  
  22.                                            // 关闭Crtl+C组合键 
  23. 370 # endif  
  24. 371   
  25. 372 # ifndef CFG_HUSH_PARSER  
  26. 373     run_command (p, 0);  // 运行Boot参数  
  27. 374 # else  
  28. 375     parse_string_outer(p, FLAG_PARSE_SEMICOLON |  
  29. 376             FLAG_EXIT_FROM_LOOP);  
  30. 377 # endif  
  31. 378   
  32. 379 # ifdef CONFIG_AUTOBOOT_KEYED  
  33. 380     disable_ctrlc(prev);  /* restore Control C checking */  
  34.                                            // 恢复Ctrl+C组合键  
  35. 381 # endif  
  36. 382   }  
  37. 383 #endif /* CONFIG_PREBOOT */ 

程序第350~356行是动态版本号功能支持代码,version_string变量是在其他文件定义的一个字符串变量,当用户改变U-Boot版本的时候会更新该变量。打开动态版本支持功能后,U-Boot在启动的时候会显示最新的版本号。

程序第363行设置命令行自动完成功能,该功能与Linux的shell类似,当用户输入一部分命令后,可以通过按下键盘上的Tab键补全命令的剩 余部分

main_loop()函数不同的功能使用宏开关控制不仅能提高代码模块化,更主要的是针对嵌入式系统Flash存储器大小设计的。在嵌入式系统 上,不同的系统Flash存储空间不同。对于一些Flash空间比较紧张的设备来说,通过宏开关关闭一些不是特别必要的功能如命令行自动完成,可以减小 U-Boot编译后的文件大小。

 

(4)在进入主循环之前,如果配置了启动延迟功能,需要等待用户从串口或者网络接口输入。如果用户按下任意键打断,启动流程,会向终端打印出一个启动菜单。代码如下:

  1. 385 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)  
  2. 386   s = getenv ("bootdelay");  
  3. 387   bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;  
  4.                                                          // 启动延迟  
  5. 388   
  6. 389   debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);  
  7. 390   
  8. 391 # ifdef CONFIG_BOOT_RETRY_TIME  
  9. 392   init_cmd_timeout ();        // 初始化命令行超时机制  
  10. 393 # endif  /* CONFIG_BOOT_RETRY_TIME */  
  11. 394   
  12. 395 #ifdef CONFIG_BOOTCOUNT_LIMIT        //一般不会检查这破玩意。
  13. 396   if (bootlimit && (bootcount > bootlimit)) {  // 检查是否超出启动次数限制  
  14. 397     printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",  
  15. 398             (unsigned)bootlimit);  
  16. 399     s = getenv ("altbootcmd");  
  17. 400   }  
  18. 401   else  
  19. 402 #endif  /* CONFIG_BOOTCOUNT_LIMIT */  
  20. 403     s = getenv ("bootcmd");  // 获取启动命令参数  
  21. 404   
  22. 405   debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");  
  23. 406   
  24. 407   if (bootdelay >= 0 && s && !abortboot (bootdelay)) {    
  25.                                                      //检查是否支持启动延迟功能  
  26. 408 # ifdef CONFIG_AUTOBOOT_KEYED  
  27. 409     int prev = disable_ctrlc(1);  /* disable Control C checking */    
  28.                                                      // 关闭Ctrl+C组合键  
  29. 410 # endif  
  30. 411   
  31. 412 # ifndef CFG_HUSH_PARSER  
  32. 413     run_command (s, 0);      // 运行启动命令行  
  33. 414 # else  
  34. 415     parse_string_outer(s, FLAG_PARSE_SEMICOLON |  
  35. 416             FLAG_EXIT_FROM_LOOP);  
  36. 417 # endif  
  37. 418   
  38. 419 # ifdef CONFIG_AUTOBOOT_KEYED  
  39. 420     disable_ctrlc(prev);   /* restore Control C checking */  
  40.                                                      // 打开Ctrl+C组合键  
  41. 421 # endif  
  42. 422   }  
  43. 423   
  44. 424 # ifdef CONFIG_MENUKEY  
  45. 425   if (menukey == CONFIG_MENUKEY) {   // 检查是否支持菜单键  
  46. 426       s = getenv("menucmd");  
  47. 427       if (s) {  
  48. 428 # ifndef CFG_HUSH_PARSER  
  49. 429     run_command (s, 0);  
  50. 430 # else  
  51. 431     parse_string_outer(s, FLAG_PARSE_SEMICOLON |  
  52. 432             FLAG_EXIT_FROM_LOOP);  
  53. 433 # endif  
  54. 434       }  
  55. 435   }  
  56. 436 #endif /* CONFIG_MENUKEY */  
  57. 437 #endif  /* CONFIG_BOOTDELAY */  
  58. 438   
  59. 439 #ifdef CONFIG_AMIGAONEG3SE  
  60. 440   {  
  61. 441       extern void video_banner(void);  
  62. 442       video_banner();               // 打印启动图标  
  63. 443   }  
  64. 444 #endif 

(5)在各功能设置完毕后,程序第454行进入一个for死循环,该循环不断使用readline()函数(第463行)从控制台(一般是串口)读 取用户的输入,然后解析。有关如何解析命令请参考U-Boot代码中run_command()函数的定义,

  1. 446   /*  
  2. 447    * Main Loop for Monitor Command Processing  
  3. 448    */ 
  4. 449 #ifdef CFG_HUSH_PARSER  
  5. 450   parse_file_outer();  
  6. 451   /* This point is never reached */  
  7. 452   for (;;);  
  8. 453 #else  
  9. 454   for (;;) {                        // 进入命令行循环 
  10. 455 #ifdef CONFIG_BOOT_RETRY_TIME  
  11. 456     if (rc >= 0) {  
  12. 457       /* Saw enough of a valid command to  
  13. 458        * restart the timeout.  
  14. 459        */  
  15. 460       reset_cmd_timeout();          // 设置命令行超时  
  16. 461     }  
  17. 462 #endif  
  18. 463     len = readline (CFG_PROMPT);    // 读取命令  
  19. 464   
  20. 465     flag = 0; /* assume no special flags for now */  
  21. 466     if (len > 0)  
  22. 467       strcpy (lastcommand, console_buffer); 
  23. 468     else if (len == 0)  
  24. 469       flag |= CMD_FLAG_REPEAT;  
  25. 470 #ifdef CONFIG_BOOT_RETRY_TIME  
  26. 471     else if (len == -2) {  
  27. 472       /* -2 means timed out, retry autoboot  
  28. 473        */  
  29. 474       puts ("\nTimed out waiting for command\n");  
  30. 475 # ifdef CONFIG_RESET_TO_RETRY  
  31. 476       /* Reinit board to run initialization code again */  
  32. 477       do_reset (NULL, 0, 0, NULL);  
  33. 478 # else  
  34. 479       return;   /* retry autoboot */  
  35. 480 # endif 
  36. 481     }  
  37. 482 #endif  
  38. 483   
  39. 484     if (len == -1)  
  40. 485       puts ("<INTERRUPT>\n");  
  41. 486     else  
  42. 487       rc = run_command (lastcommand, flag);     // 运行命令 
  43. 488   
  44. 489     if (rc <= 0) {  
  45. 490       /* invalid command or not repeatable, forget it */  
  46. 491       lastcommand[0] = 0;  
  47. 492     }  
  48. 493   }   // dead loop
  49. 494 #endif /*CFG_HUSH_PARSER*/  
  50. 495 } 

U-BOOT的功能设计 基本就在这里。

转自:http://hi.baidu.com/kebey2004/blog/item/1267be8258b2aab26d8119d5.html



Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐