Passing an Array as Command Line Argument for Linux Kernel Module

Passing an array as command line argument for linux kernel module

You can pass arrays via

 insmod k1.ko a=10,20,30,40

see Linux Kernel Module Programming for more information and examples.

How to pass an entire list as command line argument in Python?

Command line arguments are always passed as strings. You will need to parse them into your required data type yourself.

>>> input = "[2,3,4,5]"
>>> map(float, input.strip('[]').split(','))
[2.0, 3.0, 4.0, 5.0]
>>> A = map(float, input.strip('[]').split(','))
>>> print(A, type(A))
([2.0, 3.0, 4.0, 5.0], <type 'list'>)

There are libraries like argparse and click that let you define your own argument type conversion but argparse treats "[2,3,4]" the same as [ 2 , 3 , 4 ] so I doubt it will be useful.

edit Jan 2019 This answer seems to get a bit of action still so I'll add another option taken directly from the argparse docs.

You can use action=append to allow repeated arguments to be collected into a single list.

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='append')
>>> parser.parse_args('--foo 1 --foo 2'.split())
Namespace(foo=['1', '2'])

In this case you would pass --foo ? once for each list item. Using OPs example: python filename.py --foo 2 --foo 3 --foo 4 --foo 5 would result in foo=[2,3,4,5]

How can I pass an array as argument to TCL script command line run?

To pass an array we should use array get <arrayname>. So, you should be calling the sub.tcl, as

set res [exec tclsh.exe sub.tcl [array get arr1] [array get arr2]]

In sub.tcl, directly print the args as

array set arr1 [lindex $argv 0]
array set arr2 [lindex $argv 1]
parray arr1
parray arr2

lsmod showing module is used by -2

@user3452214, instead of module_param_array_named(present, rollcalls, int, **5**, 0644); use module_param_array_named(present, rollcalls, int, **&count**, 0644); added one more variable i.e. static unsigned int count which keep count of the number written to the array. We need to pass the pointer as explained in the moduleparam.h, thus cannot pass numerical value for this parameter. It works fine!!!. Hope it solves your problem.

/**
* module_param_array_named - renamed parameter which is an array of some type
* @name: a valid C identifier which is the parameter name
* @array: the name of the array variable
* @type: the type, as per module_param()
* @nump: optional pointer filled in with the number written
* @perm: visibility in sysfs
*
* This exposes a different name than the actual variable name. See
* module_param_named() for why this might be necessary.
*/
#define module_param_array_named(name, array, type, nump, perm)

how does the bootloader pass the kernel command line to the kernel?

The same question sometimes popped up to me. So this is a moment to deep dive.

While x86 uses special protocol for booting Linux, in the case of arm64 a different scheme is used. For communication with kernel, boot loader puts only single address - loaded flatten device tree (FDT) into the x0 register.

Here is excerpt from Linux and the Device Tree, Runtime configuration:

In most cases, a DT will be the sole method of communicating data from
firmware to the kernel, so also gets used to pass in runtime and
configuration data like the kernel parameters string and the location
of an initrd image.

Most of this data is contained in the /chosen node, and when booting
Linux it will look something like this:

chosen {
bootargs = "console=ttyS0,115200 loglevel=8";
initrd-start = <0xc8000000>;
initrd-end = <0xc8200000>;
};

Here is another example of DT.

Next, during kernel early boot, the OF/FDT module parses the passed device tree and fill in boot_command_line variable.



Related Topics



Leave a reply



Submit