linux 启动参数 实现

来源:互联网 发布:乾坤考研知乎 编辑:程序博客网 时间:2024/06/02 01:51
 

linux 在启动的过程中,可以采用系统默认的或uboot 传递来的参数进行启动配置 

要察看当前系统的启动参数可使用命令 cat /proc/cmdline

常用的配置参数包括:

mem=          ro        init=     loglevel   等等

 

这些特定的字符串和具体代码是通过__setup_param   和 __setup 关联起来。

这两个宏的定义如下:

#define __setup_param(str, unique_id, fn, early)            \
    static char __setup_str_##unique_id[] __initdata = str;    \
    static struct obs_kernel_param __setup_##unique_id    \
        __attribute_used__                \
        __attribute__((__section__(".init.setup")))    \
        __attribute__((aligned((sizeof(long)))))    \
        = { __setup_str_##unique_id, fn, early }
       
#define __setup(str, fn)                    \
    __setup_param(str, fn, fn, 0)
   
#define early_param(str, fn)                    \
    __setup_param(str, fn, fn, 1)

它们把函数名和函数调用的地址放到了一个结构体中,并借助连接器把这样的结构体 放在了一个叫做 .init.setup的段中。

 

例如:  
有一函数  :static int __init foo(char *str);

希望把它和字符串“foo=” 联系在一起,则可以在代码中添加宏:
__setup("foo=" , foo);


展开后就是如下的形式

static char __setup_str_foo[] __initdata = "foo=";   
static struct obs_kernel_param __setup_foo   
        __attribute_used__               
        __attribute__((__section__(".init.setup")))   
        __attribute__((aligned((sizeof(long)))))   
        = { __setup_str_foo, foo, 0 };//"foo=",foo,0
也就是说,启动参数(函数指针)被封装到obs_kernel_param结构中,
所有的内核启动参数形成内核映像.init.setup段中的一个
obs_kernel_param数组

 

另外也可以调用用early_param宏来申明需要'早期'处理的启动参数,例如在
arch\arm\kernel\setup.c就有如下的申明:
early_param("mem", parse_mem);
展开后和__setup是一样的只是early参数不一样,因此会在do_early_param
中被处理

 

内核在启动时会调用kernel\init\main.c 中的start_kernel 函数,在该函数中

parse_early_param();


 parse_args("Booting kernel", static_command_line, __start___param,
     __stop___param - __start___param,
     &unknown_bootoption);

 

分别负责解析__setup() 和 early_param()

parse_esrly_param()通过下面函数遍历obs_kernel_param数组,调用
支持函数
static int __init do_early_param(char *param, char *val)
{
    struct obs_kernel_param *p;

    for (p = __setup_start; p < __setup_end; p++) {
        if (p->early && strcmp(param, p->str) == 0) {
            if (p->setup_func(val) != 0)
                printk(KERN_WARNING
                       "Malformed early option '%s'\n", param);
        }
    }
    /* We accept everything at this stage. */
    return 0;
}

parse_args会调用下面函数:
static int __init obsolete_checksetup(char *line)
{
    struct obs_kernel_param *p;
    int had_early_param = 0;

    p = __setup_start;
    do {
        int n = strlen(p->str);
        if (!strncmp(line, p->str, n)) {
            if (p->early) {
                /* Already done in parse_early_param?
                 * (Needs exact match on param part).
                 * Keep iterating, as we can have early
                 * params and __setups of same names 8( */
                if (line[n] == '\0' || line[n] == '=')
                    had_early_param = 1;
            } else if (!p->setup_func) {
                printk(KERN_WARNING "Parameter %s is obsolete,"
                       " ignored\n", p->str);
                return 1;
            } else if (p->setup_func(line + n))//调用支持函数
                return 1;
        }
        p++;
    } while (p < __setup_end);

    return had_early_param;
}

原创粉丝点击