Linux内核启动:setup_arch

来源:互联网 发布:淘宝镜像安装失败 编辑:程序博客网 时间:2024/04/29 21:04
BootLoader可以向Linux传递参数,编译内核时也可以配置boot options

调试中使用的U-Boot bootargs如下:

noinitrd root=/dev/mtdblock3 rw console=ttySAC0,115200 init=/linuxrc mem=64M

内核版本:

2.6.35.7

内核的处理参数的整体过程如下:

u-boot将配置参数地址通过寄存器传递给内核

内核(arch/arm/kernel/head-common.S中的 __mmap_switched)将这个地址存入__atags_pointer(定义于arch/arm/kernel/setup.c)

setup_arch() 函数

[html] view plaincopyprint?
  1. <span style="font-size: 14px;">void __init setup_arch(char **cmdline_p)  
  2.   
  3. {  
  4.          struct tag *tags = (struct tag *)&init_tags;  
  5.          struct machine_desc *mdesc;  
  6.          char *from = default_command_line; 编译内核时配置的Boot Options  
  7.   
  8.   
  9.          unwind_init();  
  10.   
  11.   
  12.          setup_processor();  
  13.          mdesc = setup_machine(machine_arch_type);  
  14.          machine_name = mdesc->name;  
  15.   
  16.    
  17.          if (mdesc->soft_reboot)  
  18.                    reboot_setup("s");  
  19.   
  20.    
  21.          if (__atags_pointer)                                               检查BootLoader是否传入参数  
  22.                    tags = phys_to_virt(__atags_pointer);  
  23.          else if (mdesc->boot_params)  
  24.                    tags = phys_to_virt(mdesc->boot_params);  machine descriptor中传入的启动参数地址(arch/arm/mach-s3c2440/mach-mini2440.c)  
  25.   
  26.    
  27.          /*  
  28.           * If we have the old style parameters, convert them to  
  29.           * a tag list.  
  30.           */  
  31.          if (tags->hdr.tag != ATAG_CORE)  
  32.                    convert_to_tag_list(tags);  
  33.          if (tags->hdr.tag != ATAG_CORE)  
  34.                    tags = (struct tag *)&init_tags;       使用default init_tags,其中内存的定义是: 起始地址:0x30000000,大小是16M  
  35.   
  36.    
  37.          if (mdesc->fixup)  
  38.                    mdesc->fixup(mdesc, tags, &from, &meminfo);  
  39.   
  40.    
  41.          if (tags->hdr.tag == ATAG_CORE) {  
  42.                    if (meminfo.nr_banks != 0)                如果内存已经初始化,则忽略mem TAG  
  43.                             squash_mem_tags(tags);  
  44.                    save_atags(tags);  
  45.                    parse_tags(tags);             解析TAGS,其中如果U-boot传入ATAG_CMDLINE,则使用U-boot传入的bootargs覆盖default_command_line  
  46.          }  
  47.   
  48.    
  49.          init_mm.start_code = (unsigned long) _text;  
  50.          init_mm.end_code   = (unsigned long) _etext;  
  51.          init_mm.end_data   = (unsigned long) _edata;  
  52.          init_mm.brk        = (unsigned long) _end;  
  53.   
  54.    
  55.          /* parse_early_param needs a boot_command_line */  
  56.          strlcpy(boot_command_line, from, COMMAND_LINE_SIZE);         将defualt_command_line拷入boot_command_line  
  57.    
  58.   
  59.          /* populate cmd_line too for later use, preserving boot_command_line */  
  60.          strlcpy(cmd_line, boot_command_line, COMMAND_LINE_SIZE);  
  61.          *cmdline_p = cmd_line;  
  62.   
  63.    
  64.          parse_early_param();               大部分参数的early属性为0,即大部分参数在早期不处理,如noinitrd,console等  
  65.   
  66.    
  67.          paging_init(mdesc);  
  68.          request_standard_resources(&meminfo, mdesc);  
  69.   
  70.           
  71.          ***************  
  72. </span>  
  73. }  


一.u-boot传递TAG到kernel的解析

       在setup_arch函数的parse_tags中对传递过来的TAGLIST进行了解析

       对每一项的tag使用parse_tag分析,

       for (t = &__tagtable_begin; t < &__tagtable_end; t++)

              if (tag->hdr.tag == t->tag) {

                     t->parse(tag);

                     break;

              }

       其中__tagtable_begin,__tagtable_end在vmlinux.ld中也有定义,这里看tagtable的建立过程

       #define __tagtalbe(tag,fn)/

       Static struct tagtable __tagtable_##fn __tag={tag,fn}

       #define __tag __userd __attribute__((__section__(“.taglist.init”)))

对于上述宏中的fn,就是tagtable结构中的parse指针所指向的函数。

       而在setup.c中,已经通过__tagtalbe(ATAG_XXX,XXX)建立起所有可能的tagtable,所以可以通过遍历__tagtable_begin~__tagtable_end找到对应的tagtable,并调用对应的parse进行解析并配置