1 .c源码
#include <linux/kernel.h>
#include <linux/init.h>
#include<linux/module.h>
static int hello_init(void)
{
printk(KERN_INFO "[init] Can you feelmei? I am ready to installarm_hello....\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_INFO "[exit] Yes. I can feel you.....I am read to leavearm_hello......\n");
return 0;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("Tbao -invt company-");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Asimple arm Hello World Module");
MODULE_ALIAS("A simple arm module");
2.Makefile
在内核源码中./drivers/test/中Makefile,在该目录下单独编译内核模块。
Ti335x Armv7开发板下:
obj-m += hello.o
CURRENT_PATH :=$(shell pwd)
LINUX_KERNEL :=$(shell uname -r)
LINUX_KERNEL_PATH:=/usr/local/ti-sdk-am335x-evm/board-support/linux-3.2.0-psp04.06.00.11
all:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH)ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules
clean:
make -C $(LINUX_KERNEL_PATH)M=$(CURRENT_PATH) clean
Ps:在单独编译内核的时候:出现以下报错
WARNING:Symboversion dump/usr/local/ti-sdk-am335x-evm/board-support/linux-3.2.0-psp04.06.00.11/
Module.symvers is missing; modules willhave no dependencies and modversions.
是需要先进行内核编译,在内核源码根目录下生成Module.symvers,在编辑内核模块就不错了。
在目标机上安装模块时:出错,是内核版本校验,内核版本不对。
root@am335x-evm:/mnt#insmod hello.ko
[9293.451572] hello: module_layout: kernel tainted.
[9293.456587] Disabling lock debugging due to kernel taint
[9293.462161] hello: version magic '3.2.0 mod_unload ARMv5 p2v8 'should be '3.2.0 mod_unload modversions ARMv7 p2v8 '
Error: couldnot insert module hello.ko: Invalid module formats
[ 107.792110] hello: version magic ' 3.2.0mod_unload modversions ARMv7 p2v8 ' should be '3.2.0 mod_unload modversionsARMv7 p2v8 '
Error: couldnot insert module hello.ko: Invalid module format
3.改内核:
Include/linux中的Vermagic.h: 控制版本的是宏
#if 0
//-------Tbao----à>20170828
#define VERMAGIC_STRING \
UTS_RELEASE" " \
MODULE_VERMAGIC_SMPMODULE_VERMAGIC_PREEMPT \
MODULE_VERMAGIC_MODULE_UNLOADMODULE_VERMAGIC_MODVERSIONS \
MODULE_ARCH_VERMAGIC
//<<-------Tbao----------
#endif
/************添加*************下面符合开发板的****/
#define VERMAGIC_STRING "3.2.0 mod_unload modversions ARMv7p2v8 "
注意不要有空格等。。。。。。。。
3.安装模块
dmesg | grep 可以查看加载模块中的信息。
insmod 加载模块
rmmod 卸载模块
lsmod 列出已经加载模块和信息
modinfo 查看模块信息
cat /proc/devices