main_loop()函数解析

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 */ 

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

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

45 339 #ifdef CONFIG_MODEM_SUPPORT  

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

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

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

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

50 344     if (str != NULL)  

51 345       free (str);  

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

53 347   }  

54 348 #endif  /* CONFIG_MODEM_SUPPORT */ 

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

55 350 #ifdef CONFIG_VERSION_VARIABLE  

56 351   {  

57 352     extern char version_string[];  

58 353   

59 354     setenv ("ver", version_string);  /* set version variable */   

60                                                 // 设置版本号  

61 355   }  

62 356 #endif /* CONFIG_VERSION_VARIABLE */  

63 357   

64 358 #ifdef CFG_HUSH_PARSER  

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

66 360 #endif  

67 361   

68 362 #ifdef CONFIG_AUTO_COMPLETE  

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

70 364 #endif  

71 365   

72 366 #ifdef CONFIG_PREBOOT  

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

74 368 # ifdef CONFIG_AUTOBOOT_KEYED  

75 369     int prev = disable_ctrlc(1);  /* disable Control C checking */  

76                                                 // 关闭Crtl+C组合键  

77 370 # endif  

78 371   

79 372 # ifndef CFG_HUSH_PARSER  

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

81 374 # else  

82 375     parse_string_outer(p, FLAG_PARSE_SEMICOLON |  

83 376             FLAG_EXIT_FROM_LOOP);  

84 377 # endif  

85 378   

86 379 # ifdef CONFIG_AUTOBOOT_KEYED  

87 380     disable_ctrlc(prev);  /* restore Control C checking */  

88                                                 // 恢复Ctrl+C组合键  

89 381 # endif  

90 382   }  

91 383 #endif /* CONFIG_PREBOOT */ 

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

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

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

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

93 386   s = getenv ("bootdelay");  

94 387   bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;  

95                                                         // 启动延迟  

96 388   

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

98 390   

99 391 # ifdef CONFIG_BOOT_RETRY_TIME  

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

101 393 # endif /* CONFIG_BOOT_RETRY_TIME */  

102 394   

103 395 #ifdef CONFIG_BOOTCOUNT_LIMIT  

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

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

106 398             (unsigned)bootlimit);  

107 399     s = getenv ("altbootcmd");  

108 400   }  

109 401   else  

110 402 #endif /* CONFIG_BOOTCOUNT_LIMIT */  

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

112 404   

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

114 406   

115 407   if (bootdelay >= 0 && s && !abortboot (bootdelay)) {    

116                                                     //检查是否支持启动延迟功能  

117 408 # ifdef CONFIG_AUTOBOOT_KEYED  

118 409     int prev = disable_ctrlc(1);  /* disable Control C checking */    

119                                                     // 关闭Ctrl+C组合键  

120 410 # endif  

121 411   

122 412 # ifndef CFG_HUSH_PARSER  

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

124 414 # else  

125 415     parse_string_outer(s, FLAG_PARSE_SEMICOLON |  

126 416             FLAG_EXIT_FROM_LOOP);  

127 417 # endif  

128 418   

129 419 # ifdef CONFIG_AUTOBOOT_KEYED  

130 420     disable_ctrlc(prev);  /* restore Control C checking */  

131                                                     // 打开Ctrl+C组合键  

132 421 # endif  

133 422   }  

134 423   

135 424 # ifdef CONFIG_MENUKEY  

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

137 426       s = getenv("menucmd");  

138 427       if (s) {  

139 428 # ifndef CFG_HUSH_PARSER  

140 429     run_command (s, 0);  

141 430 # else  

142 431     parse_string_outer(s, FLAG_PARSE_SEMICOLON |  

143 432             FLAG_EXIT_FROM_LOOP);  

144 433 # endif  

145 434       }  

146 435   }  

147 436 #endif /* CONFIG_MENUKEY */  

148 437 #endif  /* CONFIG_BOOTDELAY */  

149 438   

150 439 #ifdef CONFIG_AMIGAONEG3SE  

151 440   {  

152 441       extern void video_banner(void);  

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

154 443   }  

155 444 #endif 

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

156 446   /*  

157 447    * Main Loop for Monitor Command Processing  

158 448    */  

159 449 #ifdef CFG_HUSH_PARSER  

160 450   parse_file_outer();  

161 451   /* This point is never reached */  

162 452   for (;;);  

163 453 #else  

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

165 455 #ifdef CONFIG_BOOT_RETRY_TIME  

166 456     if (rc >= 0) {  

167 457       /* Saw enough of a valid command to  

168 458        * restart the timeout.  

169 459        */  

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

171 461     }  

172 462 #endif  

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

174 464   

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

176 466     if (len > 0)  

177 467       strcpy (lastcommand, console_buffer);  

178 468     else if (len == 0)  

179 469       flag |= CMD_FLAG_REPEAT;  

180 470 #ifdef CONFIG_BOOT_RETRY_TIME  

181 471     else if (len == -2) {  

182 472       /* -2 means timed out, retry autoboot  

183 473        */  

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

185 475 # ifdef CONFIG_RESET_TO_RETRY  

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

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

188 478 # else  

189 479       return;   /* retry autoboot */  

190 480 # endif  

191 481     }  

192 482 #endif  

193 483   

194 484     if (len == -1)  

195 485       puts ("<INTERRUPT>\n");  

196 486     else  

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

198 488   

199 489     if (rc <= 0) {  

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

201 491       lastcommand[0] = 0;  

202 492     }  

203 493   }  

204 494 #endif /*CFG_HUSH_PARSER*/  

205 495 } 

 

posted @ 2013-04-01 10:47  it_xls  阅读(1462)  评论(0编辑  收藏  举报