start_armboot()函數

來源:互聯網
上載者:User

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,UGG boots, 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 : "<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 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.