00. 目录
文章目录
01. 概述
大部分有修改内核需求的用户基本上都是对于驱动的修改或者是对于设备树的修改, 其中设备树目录在 arch/arm/boot/dts 目录下,设备树插件目录在 arch/arm/boot/dts/overlays 目录下, 驱动目录在kernel/drivers下, 每个不同的驱动占用一个子目录,如char、block、 net、 mtd、 i2c等。
下面介绍如何往内核中添加一个简单的驱动模块,在drivers中我们将在这个目录下添加新的驱动模块, 在char目录下新建hello目录,并在hello目录下新建 hello.c 以及Makefile文件,目录结构如下所示
deng@local:~/A72/x3399_linux_new/kernel/drivers/char$ tree hello/
hello/
├── hello.c
└── Makefile
0 directories, 2 files
deng@local:~/A72/x3399_linux_new/kernel/drivers/char$
02. 编写hello.c文件
hello.c文件为模块的主要内容
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
static int __init hello_init(void)
{
printk(KERN_EMERG "[ KERN_EMERG ] Hello Module Init\n");
printk( "[ default ] Hello Module Init\n");
return 0;
}
static void __exit hello_exit(void)
{
printk("[ default ] Hello Module Exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL2");
MODULE_AUTHOR("uplooking ");
MODULE_DESCRIPTION("hello world module");
MODULE_ALIAS("test_module");
03. 编写Makefile文件
deng@local:~/A72/x3399_linux_new/kernel/drivers/char/hello$ pwd
/home/deng/A72/x3399_linux_new/kernel/drivers/char/hello
deng@local:~/A72/x3399_linux_new/kernel/drivers/char/hello$
deng@local:~/A72/x3399_linux_new/kernel/drivers/char/hello$ cat Makefile
obj-$(CONFIG_HELLO_MODULE) := hello.o
makefile文件内容如下
obj-$(CONFIG_HELLO_MODULE) := hello.o
03. 修改上层Makefile
除了hello/Makefile文件之外还需要修改上一层Makefile的文件的内容, 添加内容如下
04. 修改上层Kconfig文件
添加kconfig文件内容如下
05. 重新配置内核
执行以下命令
deng@local:~/A72/x3399_linux_new/kernel$ make menuconfig
06. 重新编译内核
deng@local:~/A72/x3399_linux_new$ pwd
/home/deng/A72/x3399_linux_new
deng@local:~/A72/x3399_linux_new$ ./mk.sh -k
查看是否编译
# LD drivers/char/hello/built-in.o
deng@local:~/A72/x3399_linux_new$ ./mk.sh -k
#
# configuration written to .config
#
scripts/kconfig/conf --silentoldconfig Kconfig
CHK include/config/kernel.release
CHK include/generated/uapi/linux/version.h
CHK scripts/mod/devicetable-offsets.h
CHK include/generated/utsrelease.h
make[1]: 'arch/arm64/boot/dts/rockchip/x3399-linux.dtb' is up to date.
CHK include/generated/timeconst.h
CHK include/generated/bounds.h
CHK include/generated/asm-offsets.h
CALL scripts/checksyscalls.sh
make[1]: 'include/generated/vdso-offsets.h' is up to date.
CHK include/generated/compile.h
LD drivers/char/hello/built-in.o
LD drivers/char/built-in.o
LD drivers/built-in.o
LINK vmlinux
LD vmlinux.o
MODPOST vmlinux.o
GEN .version
CHK include/generated/compile.h
UPD include/generated/compile.h
CC init/version.o
LD init/built-in.o
KSYM .tmp_kallsyms1.o
KSYM .tmp_kallsyms2.o
LD vmlinux
SORTEX vmlinux
SYSMAP System.map
OBJCOPY arch/arm64/boot/Image
LZ4C arch/arm64/boot/Image.lz4
Image: kernel.img is ready
CHK include/config/kernel.release
CHK include/generated/uapi/linux/version.h
CHK include/generated/utsrelease.h
CHK scripts/mod/devicetable-offsets.h
CHK include/generated/timeconst.h
CHK include/generated/bounds.h
CHK include/generated/asm-offsets.h
CALL scripts/checksyscalls.sh
make[2]: 'include/generated/vdso-offsets.h' is up to date.
Building modules, stage 2.
MODPOST 3 modules
Pack to resource.img successed!
Image: resource.img (with x3399-linux.dtb logo.bmp logo_kernel.bmp) is ready
Image: boot.img (with Image resource.img) is ready
Image: zboot.img (with Image.lz4 resource.img) is ready
'/home/deng/A72/x3399_linux_new/kernel/boot.img' -> '/home/deng/A72/x3399_linux_new/output/boot.img'
deng@local:~/A72/x3399_linux_new$