Linux内核启动:setup_arch
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() 函数
- <span style="font-size: 14px;">void __init setup_arch(char **cmdline_p)
- {
- struct tag *tags = (struct tag *)&init_tags;
- struct machine_desc *mdesc;
- char *from = default_command_line; 编译内核时配置的Boot Options
- unwind_init();
- setup_processor();
- mdesc = setup_machine(machine_arch_type);
- machine_name = mdesc->name;
- if (mdesc->soft_reboot)
- reboot_setup("s");
- if (__atags_pointer) 检查BootLoader是否传入参数
- tags = phys_to_virt(__atags_pointer);
- else if (mdesc->boot_params)
- tags = phys_to_virt(mdesc->boot_params); machine descriptor中传入的启动参数地址(arch/arm/mach-s3c2440/mach-mini2440.c)
- /*
- * If we have the old style parameters, convert them to
- * a tag list.
- */
- if (tags->hdr.tag != ATAG_CORE)
- convert_to_tag_list(tags);
- if (tags->hdr.tag != ATAG_CORE)
- tags = (struct tag *)&init_tags; 使用default init_tags,其中内存的定义是: 起始地址:0x30000000,大小是16M
- if (mdesc->fixup)
- mdesc->fixup(mdesc, tags, &from, &meminfo);
- if (tags->hdr.tag == ATAG_CORE) {
- if (meminfo.nr_banks != 0) 如果内存已经初始化,则忽略mem TAG
- squash_mem_tags(tags);
- save_atags(tags);
- parse_tags(tags); 解析TAGS,其中如果U-boot传入ATAG_CMDLINE,则使用U-boot传入的bootargs覆盖default_command_line
- }
- init_mm.start_code = (unsigned long) _text;
- init_mm.end_code = (unsigned long) _etext;
- init_mm.end_data = (unsigned long) _edata;
- init_mm.brk = (unsigned long) _end;
- /* parse_early_param needs a boot_command_line */
- strlcpy(boot_command_line, from, COMMAND_LINE_SIZE); 将defualt_command_line拷入boot_command_line
- /* populate cmd_line too for later use, preserving boot_command_line */
- strlcpy(cmd_line, boot_command_line, COMMAND_LINE_SIZE);
- *cmdline_p = cmd_line;
- parse_early_param(); 大部分参数的early属性为0,即大部分参数在早期不处理,如noinitrd,console等
- paging_init(mdesc);
- request_standard_resources(&meminfo, mdesc);
- ***************
- </span>
- }
void __init setup_arch(char **cmdline_p)
{
struct tag *tags = (struct tag *)&init_tags;
struct machine_desc *mdesc;
char *from = default_command_line; 编译内核时配置的Boot Options
unwind_init();
setup_processor();
mdesc = setup_machine(machine_arch_type);
machine_name = mdesc->name;
if (mdesc->soft_reboot)
reboot_setup("s");
if (__atags_pointer) 检查BootLoader是否传入参数
tags = phys_to_virt(__atags_pointer);
else if (mdesc->boot_params)
tags = phys_to_virt(mdesc->boot_params); machine descriptor中传入的启动参数地址(arch/arm/mach-s3c2440/mach-mini2440.c)
/*
* If we have the old style parameters, convert them to
* a tag list.
*/
if (tags->hdr.tag != ATAG_CORE)
convert_to_tag_list(tags);
if (tags->hdr.tag != ATAG_CORE)
tags = (struct tag *)&init_tags; 使用default init_tags,其中内存的定义是: 起始地址:0x30000000,大小是16M
if (mdesc->fixup)
mdesc->fixup(mdesc, tags, &from, &meminfo);
if (tags->hdr.tag == ATAG_CORE) {
if (meminfo.nr_banks != 0) 如果内存已经初始化,则忽略mem TAG
squash_mem_tags(tags);
save_atags(tags);
parse_tags(tags); 解析TAGS,其中如果U-boot传入ATAG_CMDLINE,则使用U-boot传入的bootargs覆盖default_command_line
}
init_mm.start_code = (unsigned long) _text;
init_mm.end_code = (unsigned long) _etext;
init_mm.end_data = (unsigned long) _edata;
init_mm.brk = (unsigned long) _end;
/* parse_early_param needs a boot_command_line */
strlcpy(boot_command_line, from, COMMAND_LINE_SIZE); 将defualt_command_line拷入boot_command_line
/* populate cmd_line too for later use, preserving boot_command_line */
strlcpy(cmd_line, boot_command_line, COMMAND_LINE_SIZE);
*cmdline_p = cmd_line;
parse_early_param(); 大部分参数的early属性为0,即大部分参数在早期不处理,如noinitrd,console等
paging_init(mdesc);
request_standard_resources(&meminfo, mdesc);
***************
}
一.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进行解析并配置