diff --git a/README.adoc b/README.adoc index 66e76da..0ae5622 100644 --- a/README.adoc +++ b/README.adoc @@ -1239,6 +1239,8 @@ although `-E` is smarter: so you should almost always use it, unless you are really counting each cycle ;-) +This method prevents the BusyBox' init from launching a shell, so you cannot interact with the system afterwards. If you also want that, use: <>. + If the script is large, you can add it to a gitignored file and pass that to `-E` as in: .... @@ -1286,6 +1288,7 @@ But this fails when we are `init` itself! but why not just use your super simple and effective `/poweroff.out` and be done with it? +[[init-busybox]] === Run command at the end of BusyBox init If you rely on something that BusyBox' init set up for you like networking, you could do: diff --git a/kernel_module/README.adoc b/kernel_module/README.adoc index 6e9650c..57636f4 100644 --- a/kernel_module/README.adoc +++ b/kernel_module/README.adoc @@ -9,7 +9,7 @@ .. link:params.c[] .. link:vermagic.c[] .. link:vermagic_fail.c[] -.. link:module_version.c[] +.. link:module_info.c[] . Pseudo filesystems .. link:debugfs.c[] .. link:procfs.c[] @@ -36,6 +36,8 @@ .. link:character_device.c[] .. link:character_device_create.c[] .. link:virt_to_phys.c[] +. Utilities +.. kstrto . Hardware device drivers .. link:pci_min.c[] .. link:pci.c[] diff --git a/kernel_module/debugfs.c b/kernel_module/debugfs.c index 9345d23..5e8f405 100644 --- a/kernel_module/debugfs.c +++ b/kernel_module/debugfs.c @@ -18,20 +18,51 @@ Only the more basic fops can be implemented in debugfs, e.g. mmap is never calle #include #include /* S_IRUSR */ -static struct dentry *dir; +static struct dentry *dir, *toplevel_file; static u32 value = 42; +/* This basically re-implents the write operation of debugfs_create_u32, + * it is just an excuse to illustrate a fop. */ +static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off) +{ + int ret; + unsigned long long res; + /* https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel */ + ret = kstrtoull_from_user(buf, len, 10, &res); + if (ret) { + /* Negative error code. */ + return ret; + } else { + value = res; + *off= len; + return len; + } +} + +static const struct file_operations fops = { + .owner = THIS_MODULE, + .write = write, +}; + static int myinit(void) { struct dentry *file; dir = debugfs_create_dir("lkmc_debugfs", 0); if (!dir) { - printk(KERN_ALERT "debugfs_create_dir failed"); + pr_alert("debugfs_create_dir failed"); return -1; } - file = debugfs_create_u32("myfile", S_IRUSR, dir, &value); + file = debugfs_create_u32("myfile", S_IRUSR | S_IWUSR, dir, &value); if (!file) { - printk(KERN_ALERT "debugfs_create_u32 failed"); + pr_alert("debugfs_create_u32 failed"); + return -1; + } + + /* Created on the toplevel of the debugfs mount, + * and with explicit fops instead of a fixed integer value. */ + toplevel_file = debugfs_create_file( + "lkmc_debugfs_file", S_IWUSR, NULL, NULL, &fops); + if (!toplevel_file) { return -1; } return 0; @@ -40,6 +71,7 @@ static int myinit(void) static void myexit(void) { debugfs_remove_recursive(dir); + debugfs_remove(toplevel_file); } module_init(myinit) diff --git a/kernel_module/hello.c b/kernel_module/hello.c index a7f64a8..1271bd2 100644 --- a/kernel_module/hello.c +++ b/kernel_module/hello.c @@ -14,6 +14,9 @@ Hello world module. static int myinit(void) { pr_info("hello init\n"); + /* 0 for success, any negative value means failure, + * E* consts if you want to specify failure cause. + * https://www.linux.com/learn/kernel-newbie-corner-loadable-kernel-modules-coming-and-going */ return 0; } diff --git a/kernel_module/kstrto.c b/kernel_module/kstrto.c new file mode 100644 index 0000000..5a5703f --- /dev/null +++ b/kernel_module/kstrto.c @@ -0,0 +1,53 @@ +/* +https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658 + +Usage: + + /kstrto.sh +*/ + +#include +#include +#include +#include /* S_IRUSR */ + +static struct dentry *toplevel_file; + +static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off) +{ + int ret; + unsigned long long res; + ret = kstrtoull_from_user(buf, len, 10, &res); + if (ret) { + /* Negative error code. */ + pr_info("ko = %d\n", ret); + return ret; + } else { + pr_info("ok = %llu\n", res); + *off= len; + return len; + } +} + +static const struct file_operations fops = { + .owner = THIS_MODULE, + .write = write, +}; + +static int myinit(void) +{ + toplevel_file = debugfs_create_file("lkmc_kstrto", S_IWUSR, NULL, NULL, &fops); + if (!toplevel_file) { + return -1; + } + return 0; +} + +static void myexit(void) +{ + debugfs_remove(toplevel_file); +} + +module_init(myinit) +module_exit(myexit) +MODULE_LICENSE("GPL"); diff --git a/kernel_module/module_info.c b/kernel_module/module_info.c new file mode 100644 index 0000000..937d1c1 --- /dev/null +++ b/kernel_module/module_info.c @@ -0,0 +1,32 @@ +/* +- https://stackoverflow.com/questions/4839024/how-to-find-the-version-of-a-compiled-kernel-module/42556565#42556565 +- https://stackoverflow.com/questions/19467150/significance-of-this-module-in-linux-driver/49812248#49812248 + +Usage: + + insmod /module_info.ko + # dmesg => name = module_info + # dmesg => version = 1.0 + cat /sys/module/module_info/version + # => module_info + modinfo /module_info.ko | grep -E '^version:' + # => version: 1.0 +*/ + +#include +#include + +static int myinit(void) +{ + /* Set by default based on the module file name. */ + pr_info("name = %s\n", THIS_MODULE->name); + pr_info("version = %s\n", THIS_MODULE->version); + return 0; +} + +static void myexit(void) {} + +module_init(myinit) +module_exit(myexit) +MODULE_VERSION("1.0"); +MODULE_LICENSE("GPL"); diff --git a/kernel_module/module_version.c b/kernel_module/module_version.c deleted file mode 100644 index aac8189..0000000 --- a/kernel_module/module_version.c +++ /dev/null @@ -1,28 +0,0 @@ -/* -https://stackoverflow.com/questions/4839024/how-to-find-the-version-of-a-compiled-kernel-module/42556565#42556565 - - insmod /module_version.ko - cat /sys/modules/module_version/version - # => 1.0 - cat /sys/module/module_version/srcversion - # => AB0F06618BC3A36B687CDC5 - modinfo /module_version.ko | grep -E '^(src|)version' - # => version: 1.0 - # => srcversion: AB0F06618BC3A36B687CDC5 -*/ - -#include -#include - -static int myinit(void) -{ - pr_info(__FILE__ "\n"); - return 0; -} - -static void myexit(void) {} - -module_init(myinit) -module_exit(myexit) -MODULE_VERSION("1.0"); -MODULE_LICENSE("GPL"); diff --git a/rootfs_overlay/debugfs.sh b/rootfs_overlay/debugfs.sh index ecde8f6..08a4135 100755 --- a/rootfs_overlay/debugfs.sh +++ b/rootfs_overlay/debugfs.sh @@ -1,12 +1,22 @@ #!/bin/sh set -ex -mkdir -p /debugfs +d=/debugfs +mkdir -p "$d" # We also added a fstab entry that mounts this under /sys/kernel/debug autmoatically. # That is the most common place to mount it. # The /sys/kernel/debug directory gets created automatically when debugfs is # compiled into the kernel, but it does not get mounted automatically. mount -t debugfs none /debugfs insmod /debugfs.ko -cd /debugfs/lkmc_debugfs +cd "${d}/lkmc_debugfs" + cat myfile # => 42 + +echo 13 > myfile +cat myfile +# => 13 + +echo 666 > "${d}/lkmc_debugfs_file" +cat myfile +# => 666 diff --git a/rootfs_overlay/kstrto.sh b/rootfs_overlay/kstrto.sh new file mode 100755 index 0000000..05628f5 --- /dev/null +++ b/rootfs_overlay/kstrto.sh @@ -0,0 +1,7 @@ +#!/bin/sh +insmod /kstrto.ko +cd /sys/kernel/debug +echo 1234 > lkmc_kstrto +# 1234 +echo foobar > lkmc_kstrto +# -22