Chinaunix首页 | 论坛 | 博客
  • 博客访问: 852722
  • 博文数量: 286
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1980
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-04 16:41
文章分类

全部博文(286)

文章存档

2020年(2)

2018年(5)

2017年(95)

2016年(69)

2015年(15)

2014年(100)

我的朋友

分类: LINUX

2014-08-15 13:07:44

原文地址:u-boot分析(2) 作者:charming2440

14.3  U-Boot启动流程分析

U-Boot支持许多的处理器和开发板,主要是该软件有良好的架构,本节以使用ARM处理器的smdk2410开发板为例分析U-Boot的启动流程,在其他的处理器架构上,U-Boot也执行类似的启动流程。图14-3是U-Boot在ARM处理器的启动步骤。

图14-3  U-Boot在ARM处理器的启动步骤

图14-3列出了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应用程序的入口在此文件中。

14.3.1  _start标号

在U-Boot工程中,每种处理器目录下都有一个start.S文件,该文件中有一个_start标号,是整个U-Boot代码的入口点。以ARM 9处理器为例,代码如下:

32 /*

33  ********************************************************************

34  *

35  * Jump vector table as in table 3.1 in [1]

36  *

37  *******************************************************************

38  */

39

40

41 .globl _start

42 _start: b       reset                 // 复位向量:无条件跳转到reset标号

43   ldr pc, _undefined_instruction     // 未定义指令向量

44   ldr pc, _software_interrupt     // 软件中断向量

45   ldr pc, _prefetch_abort         // 预取指令异常向量

46   ldr pc, _data_abort             // 数据操作异常向量

47   ldr pc, _not_used                  // 未使用

48   ldr pc, _irq                       // 慢速中断向量

49   ldr pc, _fiq                       // 快速中断向量

50

51 _undefined_instruction: .word undefined_instruction  
                                        // 定义中断向量表入口地址

52 _software_interrupt:  .word software_interrupt

53 _prefetch_abort:  .word prefetch_abort

54 _data_abort:    .word data_abort

55 _not_used:    .word not_used

56 _irq:     .word irq

57 _fiq:     .word fiq

58

59   .balignl 16,0xdeadbeef

60

61

62 /*

63  *******************************************************************

64  *

65  * Startup Code (reset vector)

66  *

67  * do important init only if we don't start from memory!

68  * relocate armboot to ram

69  * setup stack

70  * jump to second stage

71  *

72  *******************************************************************

73  */

74

75 _TEXT_BASE:

76   .word TEXT_BASE             // 定义整个U-Boot镜像文件在内存加载的地址

77

78 .globl _armboot_start

79 _armboot_start:

80   .word _start

81

82 /*

83  * These are defined in the board-specific linker script.

84  */

85 .globl _bss_start            // 定义代码段起始

86 _bss_start:

87   .word __bss_start

88

89 .globl _bss_end          // 定义代码段结束地址

90 _bss_end:

91   .word _end

92

93 #ifdef CONFIG_USE_IRQ

94 /* IRQ stack memory (calculated at run-time) */

95 .globl IRQ_STACK_START   // 定义IRQ的堆栈地址

96 IRQ_STACK_START:

97   .word 0x0badc0de

98

99 /* IRQ stack memory (calculated at run-time) */

100 .globl FIQ_STACK_START  // 定义FIQ的堆栈地址

101 FIQ_STACK_START:

102   .word 0x0badc0de

103 #endif

_start标号下面的代码主要是一些伪指令,设置全局变量,供启动程序把U-Boot映像从Flash存储器复制到内存中。其中比较重要的变量是TEXT_BASE,该变量是通过连接脚本得到的,在本例中,TEXT_BASE全局变量定义在board/smdk2410/config.mk文件中,默认值是0x33F80000。TEXT_BASE变量需要根据开发板的情况自己修改,具体地址需要根据硬件设计确定。

其他还有一些全局变量例如__bss_start、_end等定义在board/smdk2410/u-boot.lds文件中。u-boot.lds文件保存了U-Boot数据段代码段等在内存中的存放情况,具体的值由编译器计算。

_start标号一开始定义了ARM处理器7个中断向量的向量表,对应ARM处理器的7种模式。由于上电一开始处理器会从地址0执行指令,因此第一个指令直接跳转到reset标号。reset执行机器初始化的一些操作,此处的跳转指令,无论是冷启动还是热启动开发板都会执行reset标号的代码。

%提示:reset也属于一种异常模式,并且该模式的代码不需要返回。

14.3.2  reset标号

reset标号的代码在处理器启动的时候最先被执行。

106 /*

107 * the actual reset code

108 */

109

110 reset:

111   /*

112    * set the cpu to SVC32 mode

113    */

114   mrs r0,cpsr                   // 保存CPSR寄存器的值到r0寄存器

115   bic r0,r0,#0x1f               // 清除中断

116   orr r0,r0,#0xd3

117   msr cpsr,r0                   // 设置CPSR为超级保护模式

118

119 /* turn off the watchdog */      // 关闭看门狗

120 #if defined(CONFIG_S3C2400)

121 # define pWTCON   0x15300000 // 看门狗寄存器地址

122 # define INTMSK   0x14400008  /* Interupt-Controller base addresses */
                                    // 中断控制器基址

123 # define CLKDIVN  0x14800014  /* clock divisor register */

124 #elif defined(CONFIG_S3C2410)

125 # define pWTCON   0x53000000

126 # define INTMSK   0x4A000008  /* Interupt-Controller base addresses */

127 # define INTSUBMSK  0x4A00001C

128 # define CLKDIVN  0x4C000014  /* clock divisor register */

129 #endif

130

131 #if defined(CONFIG_S3C2400) || defined(CONFIG_S3C2410)

132   ldr     r0, =pWTCON            // 取出当前看门狗控制寄存器的地址到r0

133   mov     r1, #0x0               // 设置r1寄存器值为0

134   str     r1, [r0]               // 写入看门狗控制寄存器

135

136   /*

137    * mask all IRQs by setting all bits in the INTMR - default

138    */

139   mov r1, #0xffffffff           // 设置r1

140   ldr r0, =INTMSK               // 取出中断屏蔽寄存器地址到r0

141   str r1, [r0]                  // r1的值写入中断屏蔽寄存器

142 # if defined(CONFIG_S3C2410)

143   ldr r1, =0x3ff

144   ldr r0, =INTSUBMSK

145   str r1, [r0]

146 # endif

147

148   /* FCLK:HCLK:PCLK = 1:2:4 */

149   /* default FCLK is 120 MHz ! */

150   ldr r0, =CLKDIVN              // 取出时钟寄存器地址到r0

151   mov r1, #3                 // 设置r1的值

152   str r1, [r0]                  // 写入时钟配置

153 #endif  /* CONFIG_S3C2400 || CONFIG_S3C2410 */

154

155   /*

156    * we do sys-critical inits only at reboot,

157    * not when booting from ram!

158    */

159 #ifndef CONFIG_SKIP_LOWLEVEL_INIT

160   bl  cpu_init_crit              // 跳转到开发板相关初始化代码

161 #endif

程序第114行取出CPSR寄存器的值,CPSR寄存器保存当前系统状态,第115行使用比特清除命令清空了CPSR寄存器的中断控制位,表示清除中断。程序116行设置了CPSR寄存器的处理器模式位为超级保护模式,然后在第117行写入 CPSR的值强制切换处理器为超级保护模式。

程序第120~129行定义看门狗控制器有关的变量,第131~153行根据平台设置看门狗定时器。在程序第150行设置时钟分频寄存器的值。

程序第160行需要根据CONFIG_SKIP_LOWLEVEL_INIT宏的值是否跳转到cpu_init_crit标号执行。请注意这里使用bl指令,在执行完cpu_init_crit标号的代码后会      返回。

14.3.3  cpu_init_crit标号

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

228 /*

229  *****************************************************************

230  *

231  * CPU_init_critical registers

232  *

233  * setup important registers

234  * setup memory timing

235  *

236  ******************************************************************

237  */

238

239

240 #ifndef CONFIG_SKIP_LOWLEVEL_INIT

241 cpu_init_crit:

242   /*

243    * flush v4 I/D caches

244    */

245   mov r0, #0

246   mcr p15, 0, r0, c7, c7, 0 /* flush v3/v4 cache */      // 刷新cache

247   mcr p15, 0, r0, c8, c7, 0 /* flush v4 TLB */           // 刷新TLB

248

249   /*

250    * disable MMU stuff and caches                        // 关闭MMU

251    */

252   mrc p15, 0, r0, c1, c0, 0

253   bic r0, r0, #0x00002300 @ clear bits 13, 9:8 (--V- --RS)

254   bic r0, r0, #0x00000087 @ clear bits 7, 2:0 (B--- -CAM)

255   orr r0, r0, #0x00000002 @ set bit 2 (A) Align

256   orr r0, r0, #0x00001000 @ set bit 12 (I) I-Cache

257   mcr p15, 0, r0, c1, c0, 0

258

259   /*

260    * before relocating, we have to setup RAM timing

261    * because memory timing is board-dependend, you will

262    * find a lowlevel_init.S in your board directory.

263    */

264   mov ip, lr

265   bl  lowlevel_init                              // 跳转到lowlevel_init

266   mov lr, ip

267   mov pc, lr

268 #endif /* CONFIG_SKIP_LOWLEVEL_INIT */

程序第245~247行刷新cache和TLB。cache是一种高速缓存存储器,用于保存CPU频繁使用的数据。在使用Cache技术的处理器上,当一条指令要访问内存的数据时,首先查询cache缓存中是否有数据以及数据是否过期,如果数据未过期则从cache读出数据。处理器会定期回写cache中的数据到内存。根据程序的局部性原理,使用cache后可以大大加快处理器访问内存数据的速度。

TLB的作用是在处理器访问内存数据的时候做地址转换。TLB的全称是Translation Lookaside Buffer,可以翻译做旁路缓冲。TLB中存放了一些页表文件,文件中记录了虚拟地址和物理地址的映射关系。当应用程序访问一个虚拟地址的时候,会从TLB中查询出对应的物理地址,然后访问物理地址。TLB通常是一个分层结构,使用与Cache类似的原理。处理器使用一定的算法把最常用的页表放在最先访问的层次。

%提示:ARM处理器Cache和TLB的配置寄存器可以参考ARM体系结构手册。

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

程序265行跳转到lowlevel_init标号,执行与开发板相关的初始化配置。

14.3.4  lowlevel_init标号

lowlevel_init标号位于board/smdk2410/lowlevel_init.S文件,代码如下:

132 .globl lowlevel_init

133 lowlevel_init:

134   /* memory control configuration */

135   /* make r0 relative the current location so that it */

136   /* reads SMRDATA out of FLASH rather than memory ! */

137   ldr     r0, =SMRDATA                       // 读取SMRDATA变量地址

138   ldr r1, _TEXT_BASE                     // 读取_TEXT_BASE变量地址

139   sub r0, r0, r1

140   ldr r1, =BWSCON /* Bus Width Status Controller*/ // 读取总线宽度寄存器

141   add     r2, r0, #13*4                   // 得到SMRDATA占用的大小

142 0:

143   ldr     r3, [r0], #4                   // 加载SMRDATA到内存

144   str     r3, [r1], #4

145   cmp     r2, r0

146   bne     0b

147

148   /* everything is fine now */

149   mov pc, lr

152 /* the literal pools origin */

153

154 SMRDATA:            // 定义SMRDATA值

155     .word (0+(B1_BWSCON<<4)+(B2_BWSCON<<8)+(B3_BWSCON<<12)+(B4_BWSCON 
        <<16)+(B5_BWSCON<<20)+(B6_BWSCON<<24)+(B7_BWSCON<<28))

156     .word ((B0_Tacs<<13)+(B0_Tcos<<11)+(B0_Tacc<<8)+(B0_ Tcoh<<6)+
        (B0_Tah<<4)+(B0_Tacp<<2)+(B0_PMC))

157     .word ((B1_Tacs<<13)+(B1_Tcos<<11)+(B1_Tacc<<8)+(B1_Tcoh<<6)+ 
        (B1_Tah<<4)+(B1_Tacp<<2)+(B1_PMC))

158     .word ((B2_Tacs<<13)+(B2_Tcos<<11)+(B2_Tacc<<8)+(B2_Tcoh<<6)+ 
        (B2_Tah<<4)+(B2_Tacp<<2)+(B2_PMC))

159     .word ((B3_Tacs<<13)+(B3_Tcos<<11)+(B3_Tacc<<8)+(B3_Tcoh<<6)+ 
        (B3_Tah<<4)+(B3_Tacp<<2)+(B3_PMC))

160     .word ((B4_Tacs<<13)+(B4_Tcos<<11)+(B4_Tacc<<8)+(B4_Tcoh<<6)+ 
        (B4_Tah<<4)+(B4_Tacp<<2)+(B4_PMC))

161     .word ((B5_Tacs<<13)+(B5_Tcos<<11)+(B5_Tacc<<8)+(B5_Tcoh<<6)+ 
        (B5_Tah<<4)+(B5_Tacp<<2)+(B5_PMC))

162     .word ((B6_MT<<15)+(B6_Trcd<<2)+(B6_SCAN))

163     .word ((B7_MT<<15)+(B7_Trcd<<2)+(B7_SCAN))

164     .word ((REFEN<<23)+(TREFMD<<22)+(Trp<<20)+(Trc<<18)+ 
        (Tchr<<16)+REFCNT)

165     .word 0x32

166     .word 0x30

167     .word 0x30

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

程序第142~146行复制SMRDATA到内存。SMRDATA是开发板上内存映射的配置,有关内存映射关系请参考S3C2440A芯片手册。

14.3.5  relocate标号

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

163 #ifndef CONFIG_SKIP_RELOCATE_UBOOT

164 relocate:       /* relocate U-Boot to RAM     */

165   adr r0, _start    /* r0 <- current position of code   *
                                                // 获取当前代码存放地址

166   ldr r1, _TEXT_BASE    /* test if we run from flash or RAM */
                                                // 获取内存存放代码地址

167   cmp     r0, r1                  /* don't reloc during debug         */
                                                // 检查是否需要加载

168   beq     stack_setup

169

170   ldr r2, _armboot_start                 // 获取stage2代码存放地址

171   ldr r3, _bss_start                     // 获取内存代码段起始地址

172   sub r2, r3, r2    /* r2 <- size of armboot            */  
                                                // 计算stage2代码长度

173   add r2, r0, r2    /* r2 <- source end address         *
                                                // 计算stage2代码结束地址

174

175 copy_loop:

176   ldmia r0!, {r3-r10}   /* copy from source address [r0]    */
                                                // 从Flash复制代码到内存

177   stmia r1!, {r3-r10}   /* copy to   target address [r1]    */

178   cmp r0, r2      /* until source end addreee [r2]    */

179   ble copy_loop

180 #endif  /* CONFIG_SKIP_RELOCATE_UBOOT */

181

182   /* Set up the stack               *
                                                // 在内存中建立堆栈

183 stack_setup:

184   ldr r0, _TEXT_BASE    /* upper 128 KiB: relocated uboot   */

185   sub r0, r0, #CFG_MALLOC_LEN /* malloc area     */  // 分配内存区域

186   sub r0, r0, #CFG_GBL_DATA_SIZE /* bdinfo   */

187 #ifdef CONFIG_USE_IRQ

188   sub r0, r0, #(CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ)

189 #endif

190   sub sp, r0, #12   /* leave 3 words for abort-stack    */

191

192 clear_bss:                                  // 初始化内存bss段内容为0

193   ldr r0, _bss_start    /* find start of bss segment        */
                                                // 查找bss段起始地址

194   ldr r1, _bss_end    /* stop here    */  // 查找bss段结束地址

195   mov   r2, #0x00000000   /* clear   */    // 清空bss段内容

196

197 clbss_l:str r2, [r0]    /* clear loop...                    */

198   add r0, r0, #4

199   cmp r0, r1

200   ble clbss_l

223   ldr pc, _start_armboot // 设置程序指针为start_armboot()函数地址

224

225 _start_armboot: .word start_armboot

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

%提示:第24章介绍Flash存储器的时候会介绍一种NOR类型Flash存储器,可以像使用内存一样直接执行程序。从S3C2440A手册的内存映射图中可以看出,NOR Flash被映射到地址0开始的内存空间。

程序第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语言编写的函数。

14.3.6  start_armboot()函数

start_armboot()函数主要初始化ARM系统的硬件和环境变量,包括Flash存储器、FrameBuffer、网卡等,最后进入U-Boot应用程序主循环。start_armboot()函数代码如下:

236 void start_armboot (void)

237 {

238   init_fnc_t **init_fnc_ptr;

239   char *s;

240 #ifndef CFG_NO_FLASH

241   ulong size;

242 #endif

243 #if defined(CONFIG_VFD) || defined(CONFIG_LCD)

244   unsigned long addr;

245 #endif

246

247   /* Pointer is writable since we allocated a register for it */

248   gd = (gd_t*)(_armboot_start - CFG_MALLOC_LEN - sizeof(gd_t));

249   /* compiler optimization barrier needed for GCC >= 3.4 */

250   __asm__ __volatile__("": : :"memory");

251

252   memset ((void*)gd, 0, sizeof (gd_t));

253   gd->bd = (bd_t*)((char*)gd - sizeof(bd_t));

254   memset (gd->bd, 0, sizeof (bd_t));

255

256   monitor_flash_len = _bss_start - _armboot_start;

257

258   for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {

259     if ((*init_fnc_ptr)() != 0) {

260       hang ();

261     }

262   }

263

264 #ifndef CFG_NO_FLASH

265   /* configure available FLASH banks */

266   size = flash_init ();         // 初始化Flash存储器配置

267   display_flash_config (size);  // 显示Flash存储器配置

268 #endif /* CFG_NO_FLASH */

269

270 #ifdef CONFIG_VFD

271 # ifndef PAGE_SIZE

272 #   define PAGE_SIZE 4096

273 # endif

274   /*

275    * reserve memory for VFD display (always full pages)

276    */

277   /* bss_end is defined in the board-specific linker script */

278   addr = (_bss_end + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
                                            // 计算FrameBuffer内存地址

279   size = vfd_setmem (addr);             // 设置FrameBuffer占用内存大小

280   gd->fb_base = addr;                   // 设置FrameBuffer内存起始地址

281 #endif /* CONFIG_VFD */

282

283 #ifdef CONFIG_LCD

284 # ifndef PAGE_SIZE

285 #   define PAGE_SIZE 4096

286 # endif

287   /*

288    * reserve memory for LCD display (always full pages)

289    */

290   /* bss_end is defined in the board-specific linker script */

291   addr = (_bss_end + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
                                            // 计算FrameBuffer内存地址

292   size = lcd_setmem (addr);             // 设置FrameBuffer大小

293   gd->fb_base = addr;                   // 设置FrameBuffer内存起始地址

294 #endif /* CONFIG_LCD */

295

296   /* armboot_start is defined in the board-specific linker script */

297   mem_malloc_init (_armboot_start - CFG_MALLOC_LEN);

298

299 #if (CONFIG_COMMANDS & CFG_CMD_NAND)

300   puts ("NAND:  ");

301   nand_init();    /* go init the NAND */  // 初始化NAND Flash存储器

302 #endif

303

304 #ifdef CONFIG_HAS_DATAFLASH

305   AT91F_DataflashInit();             // 初始化Hash表

306   dataflash_print_info();

307 #endif

308

309   /* initialize environment */

310   env_relocate ();                                  // 重新设置环境变量

311    

312 #ifdef CONFIG_VFD

313   /* must do this after the framebuffer is allocated */

314   drv_vfd_init();                                   // 初始化虚拟显示设备

315 #endif /* CONFIG_VFD */

316

317   /* IP Address */

318   gd->bd->bi_ip_addr = getenv_IPaddr ("ipaddr"); // 设置网卡的IP地址

319

320   /* MAC Address */

321   {

322     int i;

323     ulong reg;

324     char *s, *e;

325     char tmp[64];

326

327     i = getenv_r ("ethaddr", tmp, sizeof (tmp)); // 从网卡寄存器读取
                                                           MAC地址

328     s = (i > 0) ? tmp : NULL;

329

330     for (reg = 0; reg < 6; ++reg) {

331       gd->bd->bi_enetaddr[reg] = s ? simple_strtoul (s, &e, 16) : 0;

332       if (s)

333         s = (*e) ? e + 1 : e;

334     }

335

336 #ifdef CONFIG_HAS_ETH1

337     i = getenv_r ("eth1addr", tmp, sizeof (tmp));    // 读取Hash值

338     s = (i > 0) ? tmp : NULL;

339

340     for (reg = 0; reg < 6; ++reg) {

341       gd->bd->bi_enet1addr[reg] = s ? simple_strtoul (s, &e, 16) : 0;

342       if (s)

343         s = (*e) ? e + 1 : e;

344     }

345 #endif

346   }

347

348   devices_init ();  /* get the devices list going. */
                                                    // 初始化开发板上的设备

349

350 #ifdef CONFIG_CMC_PU2

351   load_sernum_ethaddr ();

352 #endif /* CONFIG_CMC_PU2 */

353

354   jumptable_init ();                         // 初始化跳转表

355

356   console_init_r ();  /* fully init console as a device */
                                                    // 初始化控制台

357

358 #if defined(CONFIG_MISC_INIT_R)

359   /* miscellaneous platform dependent initialisations */

360   misc_init_r ();                               // 初始化其他设备

361 #endif

362

363   /* enable exceptions */

364   enable_interrupts ();                         // 打开中断

365

366   /* Perform network card initialisation if necessary */

367 #ifdef CONFIG_DRIVER_CS8900

368   cs8900_get_enetaddr (gd->bd->bi_enetaddr); // 获取CS8900网卡MAC地址

369 #endif

370

371 #if defined(CONFIG_DRIVER_SMC91111) || defined (CONFIG_DRIVER_ 
LAN91C96)

372   if (getenv ("ethaddr")) {

373     smc_set_mac_addr(gd->bd->bi_enetaddr);       // 设置SMC网卡MAC地址

374   }

375 #endif /* CONFIG_DRIVER_SMC91111 || CONFIG_DRIVER_LAN91C96 */

376

377   /* Initialize from environment */

378   if ((s = getenv ("loadaddr")) != NULL) {

379     load_addr = simple_strtoul (s, NULL, 16);

380   }

381 #if (CONFIG_COMMANDS & CFG_CMD_NET)

382   if ((s = getenv ("bootfile")) != NULL) {

383     copy_filename (BootFile, s, sizeof (BootFile));  // 保存FrameBuffer

384   }

385 #endif  /* CFG_CMD_NET */

386

387 #ifdef BOARD_LATE_INIT

388   board_late_init ();                           // 开发板相关设备初始化

389 #endif

390 #if (CONFIG_COMMANDS & CFG_CMD_NET)

391 #if defined(CONFIG_NET_MULTI)

392   puts ("Net:   ");

393 #endif

394   eth_initialize(gd->bd);

395 #endif

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

397   for (;;) {

398     main_loop ();                                // 进入主循环

399   }

400

401   /* NOTREACHED - no way out of command loop except booting */

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的命令行。

14.3.7  main_loop()函数

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

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

295 void main_loop (void)

296 {

297 #ifndef CFG_HUSH_PARSER

298   static char lastcommand[CFG_CBSIZE] = { 0, };

299   int len;

300   int rc = 1;

301   int flag;

302 #endif

303

304 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)

305   char *s;

306   int bootdelay;

307 #endif

308 #ifdef CONFIG_PREBOOT

309   char *p;

310 #endif

311 #ifdef CONFIG_BOOTCOUNT_LIMIT

312   unsigned long bootcount = 0;

313   unsigned long bootlimit = 0;

314   char *bcs;

315   char bcs_set[16];

316 #endif /* CONFIG_BOOTCOUNT_LIMIT */

317

318 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)

319   ulong bmp = 0;    /* default bitmap */

320   extern int trab_vfd (ulong bitmap);

321

322 #ifdef CONFIG_MODEM_SUPPORT

323   if (do_mdm_init)

324     bmp = 1;  /* alternate bitmap */

325 #endif

326   trab_vfd (bmp);

327 #endif  /* CONFIG_VFD && VFD_TEST_LOGO */

328

329 #ifdef CONFIG_BOOTCOUNT_LIMIT

330   bootcount = bootcount_load();         // 加载保存的启动次数

331   bootcount++;                          // 启动次数加1

332   bootcount_store (bootcount);          // 更新启动次数

333   sprintf (bcs_set, "%lu", bootcount);  // 打印启动次数

334   setenv ("bootcount", bcs_set);

335   bcs = getenv ("bootlimit");

336   bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
                                            // 转换启动次数字符串为UINT类型

337 #endif /* CONFIG_BOOTCOUNT_LIMIT */

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

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

339 #ifdef CONFIG_MODEM_SUPPORT

340   debug ("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);

341   if (do_mdm_init) {                        // 判断是否需要初始化Modem

342     char *str = strdup(getenv("mdm_cmd"));      // 获取Modem参数

343     setenv ("preboot", str);  /* set or delete definition */

344     if (str != NULL)

345       free (str);

346     mdm_init(); /* wait for modem connection */   // 初始化Modem

347   }

348 #endif  /* CONFIG_MODEM_SUPPORT */

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

350 #ifdef CONFIG_VERSION_VARIABLE

351   {

352     extern char version_string[];

353

354     setenv ("ver", version_string);  /* set version variable */   
                                                // 设置版本号

355   }

356 #endif /* CONFIG_VERSION_VARIABLE */

357

358 #ifdef CFG_HUSH_PARSER

359   u_boot_hush_start ();                     // 初始化Hash功能

360 #endif

361

362 #ifdef CONFIG_AUTO_COMPLETE

363   install_auto_complete();                  // 初始化命令自动完成功能

364 #endif

365

366 #ifdef CONFIG_PREBOOT

367   if ((p = getenv ("preboot")) != NULL) {

368 # ifdef CONFIG_AUTOBOOT_KEYED

369     int prev = disable_ctrlc(1);  /* disable Control C checking */
                                                // 关闭Crtl+C组合键

370 # endif

371

372 # ifndef CFG_HUSH_PARSER

373     run_command (p, 0);  // 运行Boot参数

374 # else

375     parse_string_outer(p, FLAG_PARSE_SEMICOLON |

376             FLAG_EXIT_FROM_LOOP);

377 # endif

378

379 # ifdef CONFIG_AUTOBOOT_KEYED

380     disable_ctrlc(prev);  /* restore Control C checking */
                                                // 恢复Ctrl+C组合键

381 # endif

382   }

383 #endif /* CONFIG_PREBOOT */

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

程序第363行设置命令行自动完成功能,该功能与Linux的shell类似,当用户输入一部分命令后,可以通过按下键盘上的Tab键补全命令的剩余部分。main_loop()函数不同的功能使用宏开关控制不仅能提高代码模块化,更主要的是针对嵌入式系统Flash存储器大小设计的。在嵌入式系统上,不同的系统Flash存储空间不同。对于一些Flash空间比较紧张的设备来说,通过宏开关关闭一些不是特别必要的功能如命令行自动完成,可以减小U-Boot编译后的文件大小。

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

385 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)

386   s = getenv ("bootdelay");

387   bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
                                                        // 启动延迟

388

389   debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);

390

391 # ifdef CONFIG_BOOT_RETRY_TIME

392   init_cmd_timeout ();      // 初始化命令行超时机制

393 # endif /* CONFIG_BOOT_RETRY_TIME */

394

395 #ifdef CONFIG_BOOTCOUNT_LIMIT

396   if (bootlimit && (bootcount > bootlimit)) { // 检查是否超出启动次数限制

397     printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",

398             (unsigned)bootlimit);

399     s = getenv ("altbootcmd");

400   }

401   else

402 #endif /* CONFIG_BOOTCOUNT_LIMIT */

403     s = getenv ("bootcmd");  // 获取启动命令参数

404

405   debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "");

406

407   if (bootdelay >= 0 && s && !abortboot (bootdelay)) {  
                                                    //检查是否支持启动延迟功能

408 # ifdef CONFIG_AUTOBOOT_KEYED

409     int prev = disable_ctrlc(1);  /* disable Control C checking */    
                                                    // 关闭Ctrl+C组合键

410 # endif

411

412 # ifndef CFG_HUSH_PARSER

413     run_command (s, 0);      // 运行启动命令行

414 # else

415     parse_string_outer(s, FLAG_PARSE_SEMICOLON |

416             FLAG_EXIT_FROM_LOOP);

417 # endif

418

419 # ifdef CONFIG_AUTOBOOT_KEYED

420     disable_ctrlc(prev);  /* restore Control C checking */
                                                    // 打开Ctrl+C组合键

421 # endif

422   }

423

424 # ifdef CONFIG_MENUKEY

425   if (menukey == CONFIG_MENUKEY) {  // 检查是否支持菜单键

426       s = getenv("menucmd");

427       if (s) {

428 # ifndef CFG_HUSH_PARSER

429     run_command (s, 0);

430 # else

431     parse_string_outer(s, FLAG_PARSE_SEMICOLON |

432             FLAG_EXIT_FROM_LOOP);

433 # endif

434       }

435   }

436 #endif /* CONFIG_MENUKEY */

437 #endif  /* CONFIG_BOOTDELAY */

438

439 #ifdef CONFIG_AMIGAONEG3SE

440   {

441       extern void video_banner(void);

442       video_banner();                // 打印启动图标

443   }

444 #endif

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

446   /*

447    * Main Loop for Monitor Command Processing

448    */

449 #ifdef CFG_HUSH_PARSER

450   parse_file_outer();

451   /* This point is never reached */

452   for (;;);

453 #else

454   for (;;) {                     // 进入命令行循环

455 #ifdef CONFIG_BOOT_RETRY_TIME

456     if (rc >= 0) {

457       /* Saw enough of a valid command to

458        * restart the timeout.

459        */

460       reset_cmd_timeout();           // 设置命令行超时

461     }

462 #endif

463     len = readline (CFG_PROMPT); // 读取命令

464

465     flag = 0; /* assume no special flags for now */

466     if (len > 0)

467       strcpy (lastcommand, console_buffer);

468     else if (len == 0)

469       flag |= CMD_FLAG_REPEAT;

470 #ifdef CONFIG_BOOT_RETRY_TIME

471     else if (len == -2) {

472       /* -2 means timed out, retry autoboot

473        */

474       puts ("\nTimed out waiting for command\n");

475 # ifdef CONFIG_RESET_TO_RETRY

476       /* Reinit board to run initialization code again */

477       do_reset (NULL, 0, 0, NULL);

478 # else

479       return;   /* retry autoboot */

480 # endif

481     }

482 #endif

483

484     if (len == -1)

485       puts ("\n");

486     else

487       rc = run_command (lastcommand, flag);  // 运行命令

488

489     if (rc <= 0) {

490       /* invalid command or not repeatable, forget it */

491       lastcommand[0] = 0;

492     }

493   }

494 #endif /*CFG_HUSH_PARSER*/

495 }

阅读(832) | 评论(0) | 转发(0) |
0

上一篇:u-boot分析(1)

下一篇:u-boot分析(3)

给主人留下些什么吧!~~