如何在启动时将值传递给内置Linux内核模块?

时间:2013-07-15 17:06:21

标签: linux linux-kernel linux-device-driver

我想在启动时将自定义参数传递给内核, 我的新代码将使用。此参数是一个数字。

我知道如何使用内核命令行将值传递给内核模块,即module_param()。现在我想从u-boot传递价值。

有没有办法在启动期间或启动后执行此操作?

3 个答案:

答案 0 :(得分:9)

如果您知道如何将值传递给内核模块,那么您就足够了解了。)

insmod my_module param=value

如果您的模块内置于内核中,则可以将参数添加到内核参数

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

这里有一个参考:kernel-parameters.txt

答案 1 :(得分:8)

修改U-Boot的 include / config / board_xxx.h 中的电路板文件,修改 $ bootargs ,类似于此示例中设置的最后一个变量:

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

control 是我无法修改的内置驱动程序模块的名称,因为我需要它来完全启动到Linux提示符。

cmd1 是我在我使用过的模块中定义的全局变量:

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

所以,你的 $ bootargs var只需附加如下内容:

<your_mod_name>.<your_mod_parameter_var_name>=<an_appropriate_value>

答案 2 :(得分:2)

Linux源文档

我更喜欢从小时的嘴里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.

轻松尝试

CONFIG_DUMMY_IRQ=y

然后在命令行上:

dummy-irq.irq=12

当内核启动时,您会看到:

dummy-irq: registered for IRQ 12

init的{​​{1}}打印出来。

代码路径

我还没有设法遵循完整的代码路径,但我认为dummy-irq.c编码为https://github.com/torvalds/linux/blob/v4.12/include/linux/moduleparam.h#L13

.

#define MODULE_PARAM_PREFIX KBUILD_MODNAME "." 宏瀑布中得到扩展,其中一步包含Linus的注释,表明该代码有多清晰:

module_param

最终为/* Lazy bastard, eh? */ 设置的QEMU GDB watch回溯是:

dummy-irq.c:irq