mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
Remove toplevel trash, add debugfs and kernel config fragment
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
config BR2_PACKAGE_KERNEL_MODULE
|
||||
bool "kernel_module"
|
||||
help
|
||||
Kernel module hello world.
|
||||
|
||||
http://example.com/
|
||||
10
Makefile
10
Makefile
@@ -1,10 +0,0 @@
|
||||
obj-m += hello.o
|
||||
ccflags-y := -Wno-declaration-after-statement -std=gnu99
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
all:
|
||||
$(MAKE) -C '$(LINUX_DIR)' M='$(PWD)' modules
|
||||
|
||||
clean:
|
||||
$(MAKE) -C '$(LINUX_DIR)' M='$(PWD)' clean
|
||||
@@ -30,3 +30,4 @@ The Linux kernel version can be found with:
|
||||
1. Buildroot
|
||||
1. [hello](kernel_module/package/kernel_module/src/hello.c)
|
||||
1. [hello2](kernel_module/package/kernel_module/src/hello2.c)
|
||||
1. [debugfs](kernel_module/package/kernel_module/src/debugfs.c)
|
||||
|
||||
7
TODO.md
7
TODO.md
@@ -1,7 +0,0 @@
|
||||
# TODO
|
||||
|
||||
- build kernel module against a given kernel tree
|
||||
- build out of tree kernel module into the kernel image
|
||||
- http://stackoverflow.com/questions/7353851/insert-linux-kernel-module-statically
|
||||
- http://stackoverflow.com/questions/8421970/compiling-a-driver-as-a-part-of-a-kernel-not-as-a-module
|
||||
- .ko file format vs .o http://stackoverflow.com/questions/10476990/difference-between-o-and-ko-file
|
||||
@@ -1 +0,0 @@
|
||||
name: KERNEL_MODULE
|
||||
20
external.mk
20
external.mk
@@ -1,20 +0,0 @@
|
||||
################################################################################
|
||||
#
|
||||
# kernel_module
|
||||
#
|
||||
################################################################################
|
||||
|
||||
KERNEL_MODULE_VERSION = 1.0
|
||||
KERNEL_MODULE_SITE = ..
|
||||
KERNEL_MODULE_SITE_METHOD = local
|
||||
|
||||
define KERNEL_MODULE_BUILD_CMDS
|
||||
$(MAKE) -C '$(@D)' LINUX_DIR='$(LINUX_DIR)' PWD='$(@D)' CC='$(TARGET_CC)' LD='$(TARGET_LD)'
|
||||
endef
|
||||
|
||||
define KERNEL_MODULE_INSTALL_TARGET_CMDS
|
||||
$(INSTALL) -D -m 0755 '$(@D)/kernel_module.ko' '$(TARGET_DIR)/kernel_module.ko'
|
||||
endef
|
||||
|
||||
$(eval $(kernel-module))
|
||||
$(eval $(generic-package))
|
||||
13
hello.c
13
hello.c
@@ -1,13 +0,0 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/kernel.h>
|
||||
|
||||
int init_module(void)
|
||||
{
|
||||
printk(KERN_INFO "hello init\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void cleanup_module(void)
|
||||
{
|
||||
printk(KERN_INFO "hello cleanup\n");
|
||||
}
|
||||
4
kernel-config-frag
Normal file
4
kernel-config-frag
Normal file
@@ -0,0 +1,4 @@
|
||||
# If you change this file, you need to run:
|
||||
# rm -rf buildroot/output/build/linux-*.*.*/
|
||||
# before ./run
|
||||
CONFIG_DEBUG_FS=y
|
||||
@@ -1,4 +1,4 @@
|
||||
obj-m += hello.o hello2.o
|
||||
obj-m += debugfs.o hello.o hello2.o
|
||||
ccflags-y := -Wno-declaration-after-statement -std=gnu99
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
45
kernel_module/package/kernel_module/src/debugfs.c
Normal file
45
kernel_module/package/kernel_module/src/debugfs.c
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
Adapted from: https://github.com/chadversary/debugfs-tutorial/blob/47b3cf7ca47208c61ccb51b27aac6f9f932bfe0b/example1/debugfs_example1.c
|
||||
|
||||
Requires CONFIG_DEBUG_FS.
|
||||
|
||||
mkdir /debugfs
|
||||
mount -t debugfs none /sys/kernel/debug
|
||||
insmod /debugfs.ko
|
||||
cd /debugfs/kernel_module_cheat
|
||||
cat myfile
|
||||
|
||||
Output:
|
||||
|
||||
42
|
||||
*/
|
||||
|
||||
#include <linux/debugfs.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
static struct dentry *dir = 0;
|
||||
static u32 value = 42;
|
||||
|
||||
int init_module(void)
|
||||
{
|
||||
struct dentry *file;
|
||||
dir = debugfs_create_dir("kernel_module_cheat", 0);
|
||||
if (!dir) {
|
||||
printk(KERN_ALERT "debugfs_create_dir failed");
|
||||
return -1;
|
||||
}
|
||||
file = debugfs_create_u32("myfile", 0666, dir, &value);
|
||||
if (!file) {
|
||||
printk(KERN_ALERT "debugfs_create_u32 failed");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void cleanup_module(void)
|
||||
{
|
||||
debugfs_remove_recursive(dir);
|
||||
}
|
||||
@@ -1,5 +1,11 @@
|
||||
/*
|
||||
Hello world module.
|
||||
|
||||
dmesg -c
|
||||
insmod hello.ko
|
||||
dmesg -c | grep 'hello init'
|
||||
rmmod hello.ko
|
||||
dmesg -c | grep 'hello cleanup'
|
||||
*/
|
||||
|
||||
#include <linux/module.h>
|
||||
|
||||
8
run
8
run
@@ -2,7 +2,9 @@
|
||||
set -e
|
||||
cd buildroot
|
||||
make BR2_EXTERNAL="$(pwd)/../kernel_module" qemu_x86_64_defconfig
|
||||
echo 'BR2_PACKAGE_KERNEL_MODULE=y' >> .config
|
||||
# kernel_module-reconfigure rebuilds the modules if they were modified.
|
||||
env -u LD_LIBRARY_PATH make BR2_JLEVEL="$(($(nproc) - 2))" kernel_module-reconfigure all
|
||||
echo '
|
||||
BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="../kernel-config-frag"
|
||||
BR2_PACKAGE_KERNEL_MODULE=y
|
||||
' >> .config
|
||||
env -u LD_LIBRARY_PATH make BR2_JLEVEL="$(($(nproc) - 2))" kernel_module-rebuild all
|
||||
qemu-system-x86_64 -M pc -kernel output/images/bzImage -drive file=output/images/rootfs.ext2,if=virtio,format=raw -append root=/dev/vda -net nic,model=virtio -net user
|
||||
|
||||
Reference in New Issue
Block a user