Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4428371
  • 博文数量: 252
  • 博客积分: 5347
  • 博客等级: 大校
  • 技术积分: 13838
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-30 10:13
文章分类
文章存档

2022年(12)

2017年(11)

2016年(7)

2015年(14)

2014年(20)

2012年(9)

2011年(20)

2010年(153)

2009年(6)

分类: 嵌入式

2010-05-06 22:29:01

   在上一篇中分析到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 : "");
// 这里如果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;
}

下面我们看一下bootm_load_os()函数

static int bootm_load_os(image_info_t os, ulong *load_end, int boot_progress)
{
    uint8_t comp = os.comp;
    ulong load = os.load;
    ulong blob_start = os.start;
    ulong blob_end = os.end;
    ulong image_start = os.image_start;
    ulong image_len = os.image_len;
    uint unc_len = CONFIG_SYS_BOOTM_LEN;

    const char *type_name = genimg_get_type_name (os.type);

    switch (comp) { //判断image的压缩类型
    case IH_COMP_NONE:
        if (load == blob_start) {
            printf (" XIP %s ... ", type_name);
        } else {
            printf (" Loading %s ... ", type_name);
//如果在Image head中加载的地址和bootm命令参数2指定的地址相同,则不需要复制,直接执行
            if (load != image_start) {
                memmove_wd ((void *)load,
                        (void *)image_start, image_len, CHUNKSZ);
            }
        }
        *load_end = load + image_len;
        puts("OK\n");
        break;
    case IH_COMP_GZIP:
        printf (" Uncompressing %s ... ", type_name);
        if (gunzip ((void *)load, unc_len,
                    (uchar *)image_start, &image_len) != 0) {
            puts ("GUNZIP: uncompress, out-of-mem or overwrite error "
                "- must RESET board to recover\n");
            if (boot_progress)
                show_boot_progress (-6);
            return BOOTM_ERR_RESET;
        }
        *load_end = load + image_len;
        break;
#ifdef CONFIG_BZIP2
    case IH_COMP_BZIP2: //判断是什么类型的压缩类型
        printf (" Uncompressing %s ... ", type_name);
         int i = BZ2_bzBuffToBuffDecompress ((char*)load,
                    &unc_len, (char *)image_start, image_len,
                    CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
        if (i != BZ_OK) {
            printf ("BUNZIP2: uncompress or overwrite error %d "
                "- must RESET board to recover\n", i);
            if (boot_progress)
                show_boot_progress (-6);
            return BOOTM_ERR_RESET;
        }
        *load_end = load + unc_len;
        break;
#endif /* CONFIG_BZIP2 */
#ifdef CONFIG_LZMA
    case IH_COMP_LZMA:
        printf (" Uncompressing %s ... ", type_name);

....................
   return 0;
}

   如果image header中指示的加载地址和bootm命令中参数2指定的地址不相同,则表示要从image header中指示的加载地址处把image data copy到bootm命令中参数2指定的地址处,然后再执行。
   bootm命令是用来引导经过u-boot的工具mkimage打包后的kernel image的。
mkimage的用法
   uboot源代码的tools/目录下有mkimage工具,这个工具可以用来制作不压缩或者压缩的多种可启动映象文件。
   mkimage在制作映象文件的时候,是在原来的可执行映象文件的前面加上一个0x40字节的头,记录参数所指定的信息,这样uboot才能识别这个映象是针对哪个CPU体系结构的,哪个OS的,哪种类型,加载内存中的哪个位置, 入口点在内存的那个位置以及映象名是什么?到这里整个U-Boot是如何启动Linux内核的,基本上也就清楚了,特别是如何向Linux内核传送的参数。
PS:下面是“ARM Linux Kernel Boot Requirements”,这篇文章中介绍的,引导Linux内核启动的必须要满足的几个条件:

* CPU register settings //这里也就是我们的theKernel中的作用
          o r0 = 0.
          o r1 = machine type number.
          o r2 = physical address of tagged list in system RAM.
    * CPU mode
          o All forms of interrupts must be disabled (IRQs and FIQs.)
          o The CPU must be in SVC mode. (A special exception exists for Angel.)
    * Caches, MMUs
          o The MMU must be off.
          o Instruction cache may be on or off.
          o Data cache must be off and must not contain any stale data.
    * Devices
          o DMA to/from devices should be quiesced.
    * The boot loader is expected to call the kernel image by jumping directly to the first instruction of the kernel image.


阅读(7808) | 评论(0) | 转发(12) |
给主人留下些什么吧!~~