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 : "<UNDEFINED>");
- 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 ("<INTERRUPT>\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 }