main_loop()函數

來源:互聯網
上載者:User

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   }  
  49. 494 #endif /*CFG_HUSH_PARSER*/  
  50. 495 } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.