撰写并运行内核模块

mkdir -p src/modules/hello_imx6ull
cd src/modules/hello_imx6ull
vi hello_imx6ull.c

hello_imx6ull.c

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>

static int __init hello_imx6ull_init(void)
{
	pr_info("hello imx6ull module init\n");

	return 0;
}

static void __exit hello_imx6ull_exit(void)
{
	pr_info("hello imx6ull module exit\n");
}

module_init(hello_imx6ull_init);
module_exit(hello_imx6ull_exit);

MODULE_DESCRIPTION("hello imx6ull - Linux Lab real board module example");
MODULE_AUTHOR("iOSDevLog <iosdevlog@iosdevlog.com>");
MODULE_LICENSE("GPL");

Makefile

# ARCH = arm
# CROSS_COMPILE = arm-linux-gnueabi-

KERNEL_SRC ?= /lib/modules/`uname -r`/build

obj-m	+=   hello_imx6ull.o


modules:
	$(MAKE) -C $(KERNEL_SRC) M=$$PWD modules;

modules-install:
	$(MAKE) -C $(KERNEL_SRC) M=$$PWD modules-install;

clean:
	$(MAKE) -C $(KERNEL_SRC) M=$$PWD clean;
	$(RM) *.ko;

编译该内核模块

make modules m=hello_imx6ull
make modules-install m=hello_imx6ull
make modules-upload

在真板上启动内核模块

make login

登录 IMX6ULL。

sudo modprobe hello_imx6ull
modprobe: ERROR: could not insert 'hello_imx6ull': Exec format error

原因未知。

2021/01/04 重头执行

预览版 从 TF卡 fire-config刷机 到 emmc。

重新执行了一遍 next 分支的操作。

已经有了 hello_imx6ull 模块,这次新建一个 hello_linux_lab_hello

cp -r src/modules/hello src/modules/hello_linux_lab_hello
cd src/modules/hello_linux_lab_hello
mv hello.c hello_linux_lab_hello.c

hello_linux_lab_hello.c

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>

static int __init hello_linux_lab_imx6ull_init(void)
{
	pr_info("hello linux-lab imx6ull module init\n");

	return 0;
}

static void __exit hello_linux_lab_imx6ull_exit(void)
{
	pr_info("hello linux-lab imx6ull module exit\n");
}

module_init(hello_linux_lab_imx6ull_init);
module_exit(hello_linux_lab_imx6ull_exit);

MODULE_DESCRIPTION("hello linux-lab imx6ull - Linux Lab real board module example");
MODULE_AUTHOR("iOSDevLog <iosdevlog@iosdevlog.com>");
MODULE_LICENSE("GPL");

Makeifle

KERNEL_SRC ?= /lib/modules/`uname -r`/build

obj-m	+=   hello_linux_lab_imx6ull.o


modules:
	$(MAKE) -C $(KERNEL_SRC) M=$$PWD modules;

modules-install:
	$(MAKE) -C $(KERNEL_SRC) M=$$PWD modules-install;

clean:
	$(MAKE) -C $(KERNEL_SRC) M=$$PWD clean;
	rm -f *.ko;

Compile a kernel module and upload it

make modules m=hello_linux_lab_imx6ull
make modules-install m=hello_linux_lab_imx6ull
make modules-upload

验证

make login

查看 hello_linux_lab_imx6ull.ko 类型。

debian@npi:~$ ls /lib/modules/4.19.35+/extra/
hello.ko  hello_imx6ull.ko  hello_linux_lab_imx6ull.ko
sudo apt install file
file /lib/modules/4.19.35+/extra/*

hello.ko 一样。

/lib/modules/4.19.35+/extra/hello.ko:                   ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=c669968dec7cfd12ff354756b21f4cb6653fc74b, with debug_info, not stripped
/lib/modules/4.19.35+/extra/hello_imx6ull.ko:           ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=70f2771a81593d78ef1aea082b7bbf1ab54d6e11, with debug_info, not stripped
/lib/modules/4.19.35+/extra/hello_linux_lab_imx6ull.ko: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=a72a2e20aa70392320fdcef88bb7844ae38a7c2c, with debug_info, not stripped

查看 hello_linux_lab_imx6ull 信息。

debian@npi:~$ sudo modprobe hello_linux_lab_imx6ull                            
[ 1177.900660] hello linux-lab imx6ull module init                             
debian@npi:~$ lsmod | grep hello_linux_lab_imx6ull                             
hello_linux_lab_imx6ull    16384  0                                            
debian@npi:~$ dmesg | grep "hello linux"                                       
[ 1177.900660] hello linux-lab imx6ull module init                             
debian@npi:~$ modinfo hello_linux_lab_imx6ull                                  
filename:       /lib/modules/4.19.35+/extra/hello_linux_lab_imx6ull.ko         
license:        GPL                                                            
author:         iOSDevLog <iosdevlog@iosdevlog.com>                            
description:    hello linux-lab imx6ull - Linux Lab real board module example  
srcversion:     23F6F2563D3621635B131C2                                        
depends:                                                                       
name:           hello_linux_lab_imx6ull                                        
vermagic:       4.19.35+ SMP preempt mod_unload modversions ARMv7 p2v8

hello_linux_lab_imx6ull

总结

年前不知什么原因,导致 Exec format error 错误,今天用干净的环境重新执行一下又可以了。

确实要使用相同的环境(抽象出虚拟层)。