uboot那边可以设置bootargs
linux kernel那边有Default command string,对应CONFIG_CMDLINE,这个选项有时候出现在General Setup里面,有时候出现在boot option里面。
启动参数相关的函数
early_param,这个函数在kernel启动之后通过parse_early_param来解析
__setup,这个函数在kernel启动之后通过parse_args来解析
常见的启动参数
earlyprintk
在arch/arm/kernel/early_printk.c文件中
static void early_write(const char *s, unsigned n)
{
while (n-- > 0) {
if (*s == '\n')
printch('\r');
printch(*s);
s++;
}
}
static void early_console_write(struct console *con, const char *s, unsigned n)
{
early_write(s, n);
}
static struct console early_console_dev = {
.name = "earlycon",
.write = early_console_write,
.flags = CON_PRINTBUFFER | CON_BOOT,
.index = -1,
};
static int __init setup_early_printk(char *buf)
{
early_console = &early_console_dev;
register_console(&early_console_dev);
return 0;
}
early_param("earlyprintk", setup_early_printk);
上面的early_param会被编译到.init.setup段中,如下,.init.setup段中保存了struct obs_kernel_param结构体,其中early_param的”earlyprintk”会被赋值到struct obs_kernel_param中的str,setup_early_printk会被赋值到struct obs_kernel_param中的setup_func
struct obs_kernel_param {
const char *str;
int (*setup_func)(char *);
int early;
};
#define __setup_param(str, unique_id, fn, early) \
static const char __setup_str_##unique_id[] __initconst \
__aligned(1) = str; \
static struct obs_kernel_param __setup_##unique_id \
__used __section(.init.setup) \
__attribute__((aligned((sizeof(long))))) \
= { __setup_str_##unique_id, fn, early }
#define __setup(str, fn) \
__setup_param(str, fn, fn, 0)
当系统起来的时候parse_early_param,最终会调用到do_early_param,他就去解析之前编译到.init.setup段中的struct obs_kernel_param结构体,如果发现cmdline中传来的param和struct obs_kernel_param结构体中的str是匹配的,那么会调用struct obs_kernel_param中的setup_func函数,也就是上面注册的setup_early_printk函数。
static int __init do_early_param(char *param, char *val, const char *unused)
{
const struct obs_kernel_param *p;
for (p = __setup_start; p < __setup_end; p++) {
if ((p->early && parameq(param, p->str)) ||
(strcmp(param, "console") == 0 &&
strcmp(p->str, "earlycon") == 0)
) {
if (p->setup_func(val) != 0)
pr_warn("Malformed early option '%s'\n", param);
}
}
/* We accept everything at this stage. */
return 0;
}
所以为了打印内核引导时候的消息,就需要在uboot的启动参数中添加earlyprink,并且在内核的配置中添加CONFIG_DEBUG_LL