Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 kernel command line i.e module_param(). Now i want to pass value from u-boot.

Is there a way to do this, either during or after boot?

like image 602
vishnumotghare Avatar asked Jul 15 '13 17:07

vishnumotghare


People also ask

How do you pass a parameter to a module?

To allow arguments to be passed to your module, declare the variables that will take the values of the command line arguments as global and then use the MODULE_PARM() macro, (defined in linux/module. h) to set the mechanism up.

How do you set a kernel argument at boot time?

To temporarily add a boot parameter to a kernel Now highlight the kernel you want to use, and press the e key. You should be able to see and edit the commands associated with the highlighted kernel. Go down to the line starting with linux and add your parameter foo=bar to its end. Now press Ctrl + x to boot.


1 Answers

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