How to pass a value to a builtin Linux kernel module at boot time?

后端 未结 3 1329
轮回少年
轮回少年 2021-02-04 11:14

I want to pass a custom parameter to the kernel at boot time, which my new code will use. This parameter is a number.

I know how to pass value to kernel module using ker

相关标签:
3条回答
  • 2021-02-04 12:14

    If you know how to pass a value to a kernel module, you know enough :)

    insmod my_module param=value

    If your module is built-in in the kernel, you can add your parameter to the kernel parameters

    vmlinux ... my_module.param=value ...

    Here a reference: kernel-parameters.txt

    0 讨论(0)
  • 2021-02-04 12:19

    Modify your board file present in include/config/board_xxx.h of U-Boot, modify $bootargs similar to the last variable that is set in this example:

    setenv bootargs display=\${display} console=\${consoledev},\${baudrate} root=/dev/mmcblk0p1 rw rootdelay=1 control.cmd1={cmd1}
    

    control is the name of the built-in driver module that I cannot insmod because I need it for booting fully to the Linux prompt.

    cmd1 is the global variable I've defined in the module in which I've used:

    module_param(cmd1, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
    

    so, your $bootargs var simply needs to be appended with something like:

    <your_mod_name>.<your_mod_parameter_var_name>=<an_appropriate_value>

    0 讨论(0)
  • 2021-02-04 12:19

    Linux source documentation

    I prefer it from the hourse's mouth v4.12/Documentation/admin-guide/kernel-parameters.rst:

    Module parameters can be specified in two ways: via the kernel command
    line with a module name prefix, or via modprobe, e.g.:
    
        (kernel command line) usbcore.blinkenlights=1
        (modprobe command line) modprobe usbcore blinkenlights=1
    
    Parameters for modules which are built into the kernel need to be
    specified on the kernel command line.  modprobe looks through the
    kernel command line (/proc/cmdline) and collects module parameters
    when it loads a module, so the kernel command line can be used for
    loadable modules too.
    

    Easy way to try it out

    CONFIG_DUMMY_IRQ=y
    

    then on the command line:

    dummy-irq.irq=12
    

    and when the kernel boots you see:

    dummy-irq: registered for IRQ 12
    

    which is printed from the init of dummy-irq.c.

    Code path

    I didn't manage to follow the full code path yet, but I think the . is encoded at https://github.com/torvalds/linux/blob/v4.12/include/linux/moduleparam.h#L13:

    #define MODULE_PARAM_PREFIX KBUILD_MODNAME "."
    

    which gets expanded in the module_param macro waterfall, one step of which contains a comment by Linus that indicates how clear that code is:

    /* Lazy bastard, eh? */
    

    The QEMU GDB watch backtrace that ends up setting it for dummy-irq.c:irq is:

    #0  kstrtouint (s=<optimized out>, base=<optimized out>, res=0xffffffff81a8d820 <irq>) at lib/kstrtox.c:225
    #1  0xffffffff8106e124 in param_set_uint (val=<optimized out>, kp=<optimized out>) at kernel/params.c:295
    #2  0xffffffff8106ed98 in parse_one (handle_unknown=<optimized out>, arg=<optimized out>, max_level=<optimized out>, min_level=<optimized out>, num_params=<optimized out>, params=<optimized out>, doing=<optimized out>, val=<optimized out>, param=<optimized out>) at kernel/params.c:148
    #3  parse_args (doing=<optimized out>, args=0xffff880007fdb99f "", params=<optimized out>, num=<optimized out>, min_level=<optimized out>, max_level=<optimized out>, arg=0x0 <irq_stack_union>, unknown=0xffffffff81aeb8e5 <unknown_bootoption>) at kernel/params.c:243
    #4  0xffffffff81aebc6d in start_kernel () at init/main.c:518
    
    0 讨论(0)
提交回复
热议问题