驅動程式 - Linux Device Driver(LDD) - Kernel command line(CONFIG_CMDLINE)加上"--"



參考資訊:
https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html

在Kernel command line加上"--",代表"--"後面的參數是要傳遞給init用的,如下範例:

CONFIG_CMDLINE="console=ttyS1,115200 rootwait root=/dev/mmcblk0p1 ro fstype=vfat init=/mininit -- test1=0 test2=1"

init/main.c

/* Anything after -- gets handed straight to init. */
static int __init set_init_arg(char *param, char *val, const char *unused, void *arg)
{
    unsigned int i;

    if (panic_later)
        return 0;

    repair_env_string(param, val, unused, NULL);

    for (i = 0; argv_init[i]; i++) {
        if (i == MAX_INIT_ARGS) {
            panic_later = "init";
            panic_param = param;
            return 0;
        }
    }
    argv_init[i] = param;
    printk("%s, param %d: %s\n", __func__, i, param);
    return 0;
}

set_init_arg()會解析"--"後面的參數

把傳遞給init的參數列印出來

static int run_init_process(const char *init_filename)
{
    argv_init[0] = init_filename;

    printk("argv[1]: %s\n", argv_init[1]);
    printk("argv[2]: %s\n", argv_init[2]);
    return do_execve(getname_kernel(init_filename), (const char __user *const __user *)argv_init, (const char __user *const __user *)envp_init);
}

結果

[    0.000000] Kernel command line: console=ttyS1,115200 rootwait root=/dev/mmcblk0p1 ro fstype=vfat init=/mininit -- test1=0 test2=1
[    0.000000] set_init_arg, param 1: test1=0
[    0.000000] set_init_arg, param 2: test2=1
...
[    1.320000] argv[1]: test1=0
[    1.320000] argv[2]: test2=1