分享

关于U-boot-main.c文件如何启动内核

 向往~ 2011-08-26

原文地址:http://blogold./u3/104447/showart_2228889.html

很精彩! 非常感谢原创作者!

 可惜百度博文有40000字数限制!++

在上一篇中分析到u-Boot启动Linux内核的函数do_bootm_linux,这一篇则着重分析,U-boot是如果一步一步启动内核的。

    我们可以看到在,start_armboot()函数的最后,在一个无限循环中调用了函数main_loop(),该函数在common/main.c文件中被定义,我们可以看到下面的一段代码:

#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
    s = getenv ("bootdelay");   //得到环境变量中bootdelay
    bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;

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

 如果定义了CONFIG_BOOTDELAY,则在没有CONFIG_BOOTDELAY秒中,串口上没有输入,则会进行自动的引导Linux内核。也就是执行bootcmd命令。

#ifdef CONFIG_BOOTCOUNT_LIMIT //启动次数的限制功能,如果到达一定次数,将不能启动u-boot.
    if (bootlimit && (bootcount > bootlimit)) {//检查是否超出启动次数限制
        printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
         (unsigned)bootlimit);
        s = getenv ("altbootcmd");//启动延时
    }
    else
#endif /* CONFIG_BOOTCOUNT_LIMIT */
        s = getenv ("bootcmd");// 获得启动参数

    debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
// 这里如果bootdelay大于0,并且中间没有被中断的话,执行命令行参数
    if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
# ifdef CONFIG_AUTOBOOT_KEYED
  int prev = disable_ctrlc(1); /* disable Control C checking */
# endif
# ifndef CONFIG_SYS_HUSH_PARSER
  run_command (s, 0); //运行启动的命令行,例如 可以使用tftp命令
# else
  parse_string_outer(s, FLAG_PARSE_SEMICOLON |
        FLAG_EXIT_FROM_LOOP);
# endif

   到这里我们就可以看到是怎么调用设置的命令行参数的,在这里还要使用到bootm命令,先来看看bootm命令的实现,在common/cmd_bootm.c

#define CONFIG_BOOTM_LINUX 1
#define CONFIG_BOOTM_NETBSD 1
#define CONFIG_BOOTM_RTEMS 1

#ifdef CONFIG_BOOTM_LINUX
extern boot_os_fn do_bootm_linux;
#endif
#ifdef CONFIG_BOOTM_NETBSD
static boot_os_fn do_bootm_netbsd;
#endif

   可以看出如果定义了CONFIG_BOOTM_LINUX这个宏的话,就会使用外部文件定义的do_bootm_linux函数,在arm体系结构中,就是在lib_arm/bootm.c文件中,可以从lib_arm/bootm.c文件中的59行看到do_bootm_linux()的定义。其中第64行声明了这样一个函数指针theKernel

void (*theKernel)(int zero, int arch, uint params);

   看看它的名字和参数的命名我们也可以猜到这个其实就是内核的入口函数的指针了。几个参数的命名也说明了下文提到的ARM Linux内核启动要求的第一条,因为根据ACPS(ARM/Thumb Procedure Call Standard)的规定,这三个参数就是依次使用r0,r1和r2来传递的。接下来第73行就是给这个函数指针赋值: 

theKernel = (void (*)(int, int, uint))images->ep;

    可以看到theKernel被赋值为images->ep,这个image指使用tools/mkimage工具程序制作uImage时加在linux.bin.gz前面的一个头部,而ep结构体成员保存的就是使用mkimage时指定的-e参数的值,即内核的入口点(Entry Point)。知道了images->ep的意义之后,给theKernel赋这个值也就是理所当然的了。
     image是bootm_headers结构体的指针,可以在inlcude/image.h文件中看到这个结构体的定义如下:

typedef struct bootm_headers {
     ............................

    int        fit_noffset_fdt;/* FDT blob subimage node offset */
#endif

#ifndef USE_HOSTCC
    image_info_t    os;        /* os image info */
    ulong        ep;        /* entry point of OS */

    ulong        rd_start, rd_end;/* ramdisk start/end */
...............

}

最后是对内核入口函数的调用,发生在第128行:

theKernel (0, machid, bd->bi_boot_params);

   调用的时候对参数进行赋值,r0=0,r1=bd->bi_arch_number,r2=bd-> bi_boot_params,一个都不少。至此U-Boot的使命完成,开始进入ARM Linux的世界。
   要知道哪个地址是启动内核,哪个地址启动文件系统,要分析common/cmd_bootm.c中的函数 do_bootm,因为引导kernel就是bootm这条命令的工作,do_bootm是命令bootm的执行函数现在我们来分析一下common/cmd_bootm.c中的函数do_bootm,这是bootm命令的处理函数.do_bootm()函数中的很多功能都是分成了函数的形式,而在以前的版本中没有这么有结构层次,这里我们也只是分析对引导Linux内核有作用的部分,因为这是一个在common文件夹下的文件,也就意味着,在引导别的操作系统时也会用到这个函数,而不单单是Linux操作系统.

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;
#ifndef CONFIG_RELOC_FIXUP_WORKS
    static int relocated = 0;
    /* relocate boot function table */
    if (!relocated) {
        int i;
        for (i = 0; i < ARRAY_SIZE(boot_os); i++)
            if (boot_os[i] != NULL)
                boot_os[i] += gd->reloc_off;
        relocated = 1;
    }
#endif
    /* determine if we have a sub command */
    if (argc > 1) {
        char *endp;

        simple_strtoul(argv[1], &endp, 16);
    
        if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
            return do_bootm_subcommand(cmdtp, flag, argc, argv);
    }

    if (bootm_start(cmdtp, flag, argc, argv)) //提取mkimage生成的文件头部,放到bootm_headers_t结构体中
        return 1;

    iflag = disable_interrupts();

#if defined(CONFIG_CMD_USB)
       usb_stop();
#endif

#ifdef CONFIG_AMIGAONEG3SE
    /*
     * We've possible left the caches enabled during
     * bios emulation, so turn them off again
     */

    icache_disable();
    dcache_disable();
#endif

    ret = bootm_load_os(images.os, &load_end, 1); //加载操作系统的关键部分 确定使用的地址
    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));

    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);

#ifdef CONFIG_SILENT_CONSOLE //这里处理Linux操作系统
    if (images.os.os == IH_OS_LINUX)
        fixup_silent_linux();//该函数中处理bootarg参数
#endif
    boot_fn = boot_os[images.os.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();
/*下面的函数,继续引导内核的镜像,复制image header 到全局变量header;
检查header的魔数,检查数,header和image中的这两个。确定image的体系结构和类型(KERNEL  or MULTI),关闭中断,加载image到header中的加载地址*/

    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;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多