main_loop中執行的最重要的操作便是引導內核。引導分兩步。第一步先讀取內核鏡像uImage到內存。然後再使用bootm引導內核。
讀取內核只是進行內存復制,因此僅僅需要分析bootm。
- /*******************************************************************/
- /* bootm - boot application image from image in memory */
- /*******************************************************************/
- 引導應用程序在內存中的鏡像。
- common/cmd_bootm.c
- int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
- {
- ulong iflag;
- ulong load_end = 0;
- int ret;
- boot_os_fn *boot_fn;
- /* determine if we have a sub command */檢查是否有子命令
- if (argc > 1) {
- char *endp;
- simple_strtoul(argv[1], &endp, 16); //調用字符串轉無符號整形的函數,endp指向尾字符。
- /* endp pointing to NULL means that argv[1] was just a
- * valid number, pass it along to the normal bootm processing 以空字符結尾則當作是合法的數,作為正常引導的參數
- *
- * If endp is ':' or '#' assume a FIT identifier so pass
- * along for normal processing.
- *
- * Right now we assume the first arg should never be '-'
- */
- if ((*endp != 0) && (*endp != ':') && (*endp != '#')) //是否有子命令。一般不會有,所以這個路徑不會執行。
- return do_bootm_subcommand(cmdtp, flag, argc, argv);
- }
- if (bootm_start(cmdtp, flag, argc, argv)) //bootm_start的分析見下一篇文章。這個函數執行成功返回0,失敗返回1。這個函數主要用於獲取鏡像的信息並存入images。
- return 1;
- /*
- * We have reached the point of no return: we are going to
- * overwrite all exception vector code, so we cannot easily
- * recover from any failures any more...
- */
- iflag = disable_interrupts();//禁用中斷。
- ret = bootm_load_os(images.os, &load_end, 1);//將鏡像的數據從images.os.image_start復制到images.os.load 打印:Loading Kernel Image ... OK
- if (ret < 0) {//上個函數調用失敗才會進入這個路徑
- if (ret == BOOTM_ERR_RESET)
- do_reset (cmdtp, flag, argc, argv);
- if (ret == BOOTM_ERR_OVERLAP) {
- if (images.legacy_hdr_valid) {
- if (image_get_type (&images.legacy_hdr_os_copy) == IH_TYPE_MULTI)
- puts ("WARNING: legacy format multi component "
- "image overwritten/n");
- } else {
- puts ("ERROR: new format image overwritten - "
- "must RESET the board to recover/n");
- show_boot_progress (-113);
- do_reset (cmdtp, flag, argc, argv);
- }
- }
- if (ret == BOOTM_ERR_UNIMPLEMENTED) {
- if (iflag)
- enable_interrupts();
- show_boot_progress (-7);
- return 1;
- }
- }
- lmb_reserve(&images.lmb, images.os.load, (load_end - images.os.load));//由於沒有定義CONFIG_LMB,這個函數調用被定義為一個空的宏。
- if (images.os.type == IH_TYPE_STANDALONE) {//處理單獨的程序鏡像。顯然引導內核時不會進入這個路徑。
- if (iflag)
- enable_interrupts();
- /* This may return when 'autostart' is 'no' */
- bootm_start_standalone(iflag, argc, argv);
- return 0;
- }
- show_boot_progress (8);
- boot_fn = boot_os[images.os.os];//根據操作系統的類型獲取引導操作系統的函數, boot_os代碼如下。
- if (boot_fn == NULL) {
- if (iflag)
- enable_interrupts();
- printf ("ERROR: booting os '%s' (%d) is not supported/n",
- genimg_get_os_name(images.os.os), images.os.os);
- show_boot_progress (-8);
- return 1;
- }
- arch_preboot_os();//禁用中斷,重設中斷向量
- boot_fn(0, argc, argv, &images);//調用do_bootm_linux,分析在下面。
- show_boot_progress (-9);
- #ifdef DEBUG
- puts ("/n## Control returned to monitor - resetting.../n");
- #endif
- do_reset (cmdtp, flag, argc, argv);
- return 1;
- }
2。boot_os
- static boot_os_fn *boot_os[] = {
- #ifdef CONFIG_BOOTM_LINUX
- [IH_OS_LINUX] = do_bootm_linux,
- #endif
- #ifdef CONFIG_BOOTM_NETBSD
- [IH_OS_NETBSD] = do_bootm_netbsd,
- #endif
- #ifdef CONFIG_LYNXKDI
- [IH_OS_LYNXOS] = do_bootm_lynxkdi,
- #endif
- #ifdef CONFIG_BOOTM_RTEMS
- [IH_OS_RTEMS] = do_bootm_rtems,
- #endif
- #if defined(CONFIG_CMD_ELF)
- [IH_OS_VXWORKS] = do_bootm_vxworks,
- [IH_OS_QNX] = do_bootm_qnxelf,
- #endif
- #ifdef CONFIG_INTEGRITY
- [IH_OS_INTEGRITY] = do_bootm_integrity,
- #endif
- };
3。do_bootm_linux
之前所做的引導工作都是與操作系統無關的,接下來都是針對linux 的。
可以看到,參數flag傳過來的是0。
- arch/arm/lib/bootm.c
- int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
- {
- bd_t *bd = gd->bd;
- char *s;
- int machid = bd->bi_arch_number;
- void (*theKernel)(int zero, int arch, uint params);
- #ifdef CONFIG_CMDLINE_TAG//如果定義了這個宏,將會把bootargs傳給內核。
- char *commandline = getenv ("bootargs");//獲取bootargs環境變量。
- #endif
- if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
- return 1;
- theKernel = (void (*)(int, int, uint))images->ep;//鏡像的入口地址處也就是需要執行的函數入口
- s = getenv ("machid");
- if (s) {
- machid = simple_strtoul (s, NULL, 16);
- printf ("Using machid 0x%x from environment/n", machid);
- }
- show_boot_progress (15);
- debug ("## Transferring control to Linux (at address %08lx) .../n",
- (ulong) theKernel);
- //下面將會初始化傳給內核的標簽(tag),這些標簽裡包含了必要的環境變量和參數。
- #if defined (CONFIG_SETUP_MEMORY_TAGS) || /
- defined (CONFIG_CMDLINE_TAG) || /
- defined (CONFIG_INITRD_TAG) || /
- defined (CONFIG_SERIAL_TAG) || /
- defined (CONFIG_REVISION_TAG)
- setup_start_tag (bd);
- #ifdef CONFIG_SETUP_MEMORY_TAGS
- setup_memory_tags (bd);//內存有關的定義
- #endif
- #ifdef CONFIG_CMDLINE_TAG
- setup_commandline_tag (bd, commandline);//將bootargs傳給tag
- #endif
- #ifdef CONFIG_INITRD_TAG
- if (images->rd_start && images->rd_end)//沒有ramdisk所以不會執行這個路徑
- setup_initrd_tag (bd, images->rd_start, images->rd_end);
- #endif
- setup_end_tag (bd);
- #endif
- /* we assume that the kernel is in place */
- printf ("/nStarting kernel .../n/n");//引導內核之前最後一次打印信息。
- cleanup_before_linux ();//禁用中斷和cache.
- theKernel (0, machid, bd->bi_boot_params);//跳到內核中執行。<<<<全劇終>>>>
- /* does not return */
- return 1;
- }
4.
- arch/arm/include/asm
- /* The list must start with an ATAG_CORE node */
- #define ATAG_CORE 0x54410001
- struct tag_core {
- u32 flags; /* bit 0 = read-only */
- u32 pagesize;
- u32 rootdev;
- };
- /* command line: /0 terminated string */
- #define ATAG_CMDLINE 0x54410009
- struct tag_cmdline {//bootargs保存在它的後面
- char cmdline[1]; /* this is the minimum size */
- };
- /* describes where the compressed ramdisk image lives (virtual address) */
- /*
- * this one accidentally used virtual addresses - as such,
- * its depreciated.
- */
- #define ATAG_INITRD 0x54410005
- /* describes where the compressed ramdisk image lives (physical address) */
- #define ATAG_INITRD2 0x54420005
- struct tag_initrd {//描述ramdisk的分布
- u32 start; /* physical start address */
- u32 size; /* size of compressed ramdisk image in bytes */
- };
- /* it is allowed to have multiple ATAG_MEM nodes */
- #define ATAG_MEM 0x54410002
- struct tag_mem32 {
- u32 size;
- u32 start; /* physical start address */
- };
- struct tag_header {
- u32 size;
- u32 tag;
- };
- struct tag {
- struct tag_header hdr;
- union {
- struct tag_core core;
- struct tag_mem32 mem;
- struct tag_videotext videotext;
- struct tag_ramdisk ramdisk;
- struct tag_initrd initrd;
- struct tag_serialnr serialnr;
- struct tag_revision revision;
- struct tag_videolfb videolfb;
- struct tag_cmdline cmdline;
- /*
- * Acorn specific
- */
- struct tag_acorn acorn;
- /*
- * DC21285 specific
- */
- struct tag_memclk memclk;
- } u;
- };
5
- arch/arm/include/asm/setup.h
- #define tag_next(t) ((struct tag *)((u32 *)(t) + (t)->hdr.size))//跳到下一個tag地址處
- arch/arm/lib/bootm.c
- static void setup_start_tag (bd_t *bd)
- {
- params = (struct tag *) bd->bi_boot_params; //CFG_ENV_ADDR(0x80000100)設置環境變量的頭地址。
- params->hdr.tag = ATAG_CORE; //第一個tag默認為 tag_core。ATAG_CORE 是默認的開始類型。
- params->hdr.size = tag_size (tag_core);
- params->u.core.flags = 0;
- params->u.core.pagesize = 0;
- params->u.core.rootdev = 0;
- params = tag_next (params); //跳到下一個tag地址處
- }
- #ifdef CONFIG_SETUP_MEMORY_TAGS
- static void setup_memory_tags (bd_t *bd)//設置內存分布的tag
- {
- int i;
- for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {//如果有多個內存芯片,則會循環多次。對3250來說,只有一片。
- params->hdr.tag = ATAG_MEM;
- params->hdr.size = tag_size (tag_mem32);
- params->u.mem.start = bd->bi_dram[i].start;//內存塊的開始地址。PHYS_SDRAM_1;在smartarm3250.h中,起始地址(0x80000000)
- params->u.mem.size = bd->bi_dram[i].size;//內存塊的大小
- params = tag_next (params);//跳到下一個tag地址處
- }
- }
- #endif /* CONFIG_SETUP_MEMORY_TAGS */
- static void setup_commandline_tag (bd_t *bd, char *commandline)
- {
- char *p;
- if (!commandline)
- return;
- /* eat leading white space */
- for (p = commandline; *p == ' '; p++);//跳過開頭的空格
- /* skip non-existent command lines so the kernel will still
- * use its default command line.
- */
- if (*p == '/0')
- return;
- params->hdr.tag = ATAG_CMDLINE;
- params->hdr.size =
- (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
- strcpy (params->u.cmdline.cmdline, p);//將bootargs復制到cmdline後面。
- params = tag_next (params);//跳到下一個tag地址處
- }
- #ifdef CONFIG_INITRD_TAG
- static void setup_initrd_tag (bd_t *bd, ulong initrd_start, ulong initrd_end)
- {
- /* an ATAG_INITRD node tells the kernel where the compressed
- * ramdisk can be found. ATAG_RDIMG is a better name, actually.
- */
- params->hdr.tag = ATAG_INITRD2;
- params->hdr.size = tag_size (tag_initrd);
- params->u.initrd.start = initrd_start;
- params->u.initrd.size = initrd_end - initrd_start;
- params = tag_next (params);
- }
- #endif /* CONFIG_INITRD_TAG */
- static void setup_end_tag (bd_t *bd)
- {
- params->hdr.tag = ATAG_NONE;//最後一個tag是一個空類型,標志這tag的結束。
- params->hdr.size = 0;
- }