独立编译Ko文件添加方式
1.创建文件夹test,其中包含test.c与编译规则Makefile文件:
2.在test.c中编写:
#include <linux/module.h> //包含模块加载卸载函数
static int __init test_init(void) //模块加载调用函数
{
printk(KERN_ERR "test init\n");
return 0;
}
static void __exit test_exit(void) //模块卸载调用函数
{
printk(KERN_ERR "test exit\n");
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL"); //模块包含协议
MODULE_AUTHOR("jz"); //作者
MODULE_DESCRIPTION("test"); //模块描述
3.在Makefile中填写,以x2000_darwin为例:
bhu@bhu-PC:~/work$ cat build/configs/x2000_darwin_factory_defconfig | grep kernel
APP_kernel_dir=../kernel/kernel-x2000 //获取kernel路径
KERN_DIR = /home/your_linux/kernel/kernel-x2000 #获取到的kernel绝对路径
obj-m += test.o #我们需要编译源文件,将其编译成模块
all:
make -C $(KERN_DIR) M=`pwd` modules #编译
.PHONY: clean #将clean声明为伪目标
clean:
make -C $(KERN_DIR) M=`pwd` modules clean #清除编译生成的文件
4.进入到test目录下,打开终端,并将编译器添加进环境变量,可以生成模块test.ko文件:
环境变量指定为对应的编译器,这里以x2000_darwin为例
bhu@bhu-PC:~/work$ cat build/configs/x2000_darwin_factory_defconfig | grep toolchain_dir
APP_toolchain_dir=../tools/toolchains/mips-gcc720-glibc229 //获取编译器路径
cd test
export PATH=/home/your_linux/tools/toolchains/mips-gcc720-glibc229/bin:$PATH #配置环境变量,注意这里使用的是获取到的编译器绝对路径
make
5.将生成的test.ko推入板子观察效果:
bhu@bhu-PC:~/Desktop/test$ adb push test.ko /usr/data
test.ko: 1 file pushed. 1.0 MB/s (2904 bytes in 0.003s)
bhu@bhu-PC:~/Desktop/test$ adb shell
# cd /usr/data/
# ls
evol test.ko
macaddr.txt wpa_supplicant.conf
6.将模块手动加载进开发板,并用dmesg查看加载模块时的打印:
# insmod test.ko
# lsmod
Module Size Used by Tainted: G
test 658 0 //test模块加载进去
# dmesg | tail
[ 7953.488834] <<-GTP-DEBUG->> [819]pre_touch:01, finger:81.
[ 7953.488841] <<-GTP-DEBUG->> [950] (0)(19, 372)[24]
[ 7953.488848] <<-GTP-DEBUG->> [472]ID:0, X:19, Y:372, W:24
[ 7953.499191] <<-GTP-DEBUG->> [819]pre_touch:01, finger:80.
[ 7953.499199] <<-GTP-DEBUG->> [961]@@@@ touch UP ,pre_pen is 0,pre_finger is 1!
[ 7953.499204] <<-GTP-DEBUG->> [970]Touch Release!
[ 7953.509936] <<-GTP-DEBUG->> [819]pre_touch:00, finger:80.
[ 7953.522234] <<-GTP-DEBUG->> [819]pre_touch:00, finger:80.
[91048.779043] adbd (1066): /proc/1066/oom_adj is deprecated, please use /proc/1066/oom_score_adj instead.
[91098.519476] test init //test模块加载打印
7.将模块卸载,并查看卸载打印:
# rmmod test.ko
# dmesg | tail
[ 7953.488841] <<-GTP-DEBUG->> [950] (0)(19, 372)[24]
[ 7953.488848] <<-GTP-DEBUG->> [472]ID:0, X:19, Y:372, W:24
[ 7953.499191] <<-GTP-DEBUG->> [819]pre_touch:01, finger:80.
[ 7953.499199] <<-GTP-DEBUG->> [961]@@@@ touch UP ,pre_pen is 0,pre_finger is 1!
[ 7953.499204] <<-GTP-DEBUG->> [970]Touch Release!
[ 7953.509936] <<-GTP-DEBUG->> [819]pre_touch:00, finger:80.
[ 7953.522234] <<-GTP-DEBUG->> [819]pre_touch:00, finger:80.
[91048.779043] adbd (1066): /proc/1066/oom_adj is deprecated, please use /proc/1066/oom_score_adj instead.
[91098.519476] test init
[91405.937885] test exit //test模块卸载打印