yocto编译内核驱动

0. 内核模块编译方法

(1) 基于内核树在源码外编译模块

(2) 内核源码内编译模块

1. 基于内核树在源码外编译模块

(0) 使能构建环境

1
source oe-init-build-env

使能之后,会进入build目录下

(1) 创建自定义meta层

在yocto的构建目录下,创建一个新的meta层

1
bitbake-layers create-layer ../../meta-hello

(2) 添加层到构建环境

1
bitbake-layers add-layer ../../meta-hello 
1
2
3
4
5
6
7
8
yocto/poky/build$ bitbake-layers show-layers
NOTE: Starting bitbake server...
layer path priority
========================================================================================================
core /home/ll/Free/yocto/poky/meta 5
yocto /home/ll/Free/yocto/poky/meta-poky 5
yoctobsp /home/ll/Free/yocto/poky/meta-yocto-bsp 5
meta-hello /home/ll/Free/yocto/meta-hello 6

(3) 添加驱动代码模块

1
2
yocto$ ls
meta-arm meta-hello meta-openembedded meta-renesas meta-ros meta-ros-publisher poky
1
$ mkdir -p meta-hello/recipes-kernel/hello/files

hello_drv.cMakefile添加到该目录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
hello_drv.c

1 #include <linux/kernel.h>
2 #include <linux/module.h>
3
4 static int __init hello_drv_init(void)
5 {
6 pr_info("hello world drv!\r\n");
7 return 0;
8 }
9
10 static void __exit hello_drv_exit(void)
11 {
12 pr_info("good bye drv!\r\n");
13 }
14
15 module_init(hello_drv_init);
16 module_exit(hello_drv_exit);
17 MODULE_LICENSE("GPL");hello_drv.c
1
2
3
4
5
6
7
8
9
10
11
12
Makefile

1 obj-m := hello_drv.o
2
3 all:
4 $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules
5
6 modules_install:
7 $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules_install
8
9 clean:
10 $(MAKE) -C $(KERNEL_SRC) M=$(PWD) clean

meta-hello/recipes-kernel/hello目录下创建 hello.bb文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
hello.bb

1 SUMMARY = "Example of how to build a Linux Kernel Module"
2 DESCRIPTION = "${SUMMARY}"
3
4 LICENSE = "GPL-2.0-only"
5 LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/GPL-2.0-only;md5=801f80980d171dd6425610833a22dbe6"
6
7 inherit module
8
9 SRC_URI = "file://hello_drv.c \
10 file://Makefile"
11
12 S = "${WORKDIR}"
13
14 UNPACKDIR = "${S}"
15
16 RPROVIDES:${PN} += "kernel-module-hello"

meta-hello目录架构

(4) 在目标镜像中包含驱动模块

meta/conf/machine/qemux86-64.conf文件下添加驱动模块

1
MACHINE_EXTRA_RRECOMMENDS = "kernel-module-snd-ens1370 kernel-module-snd-rawmidi kernel-module-hello"

(5) 构建镜像

(0)确保 meta-hello的内容被包含

通过 local.conf 向 image 安装列表添加你的 package

build/conf/local.conf 中加入:

1
IMAGE_INSTALL_append = " hello"
(1)编译镜像
1
yocto/poky/build$ bitbake core-image-minimal
(2)编译完成

2. 内核源码内编译

(0) 使能构建环境

1
source oe-init-build-env

使能之后,会进入build目录下

(1) 查找内核源码

1
bitbake -s | grep linux

(2) 提取内核源码

1
devtool modify linux-yocto-dev

(3) 添加hello_drv模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
hello_drv.c

1 #include <linux/kernel.h>
2 #include <linux/module.h>
3
4 static int __init hello_drv_init(void)
5 {
6 pr_info("hello world drv!\r\n");
7 return 0;
8 }
9
10 static void __exit hello_drv_exit(void)
11 {
12 pr_info("good bye drv!\r\n");
13 }
14
15 module_init(hello_drv_init);
16 module_exit(hello_drv_exit);
17 MODULE_LICENSE("GPL");
1
2
Makefile
1 obj-$(CONFIG_HELLO_DRV) += hello_drv.o
1
2
3
4
5
6
Kconfig
1 config HELLO_DRV
2 tristate "Create a hello world drv module"
3 default n
4 help
5 This is a module to print hello world drv

上层目录

1
Makefile
1
Kconfig

(4) 执行menuconfig,将新添加的驱动模块勾选上

1
$ bitbake virtual/kernel -c menuconfig
1
Device Drivers --> Character devices --> <*> Create a hello world drv module

(5) 单独编译linux内核,确保添加的驱动模块没有问题

1
$ devtool build linux-yocto-dev

(6) 编译整个image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
$ devtool build-image core-image-sato
NOTE: Starting bitbake server...
Loading cache: 100% |#############################################################################################################################################################################| Time: 0:00:04
Loaded 7290 entries from dependency cache.
Parsing recipes: 100% |###########################################################################################################################################################################| Time: 0:00:10
Parsing of 4778 .bb files complete (4777 cached, 1 parsed). 7291 targets, 321 skipped, 1 masked, 0 errors.
WARNING: Skipping recipe linux-yocto-dev as it doesn't produce a package with the same name
WARNING: No recipes in workspace, building image core-image-sato unmodified
NOTE: Reconnecting to bitbake server...
Loading cache: 100% |#############################################################################################################################################################################| Time: 0:00:05
Loaded 7290 entries from dependency cache.
Parsing recipes: 100% |###########################################################################################################################################################################| Time: 0:00:08
Parsing of 4778 .bb files complete (4777 cached, 1 parsed). 7291 targets, 321 skipped, 1 masked, 0 errors.
NOTE: Resolving any missing task queue dependencies

Build Configuration:
BB_VERSION = "2.12.0"
BUILD_SYS = "x86_64-linux"
NATIVELSBSTRING = "universal"
TARGET_SYS = "aarch64-poky-linux"
MACHINE = "radxa-zero-3e"
DISTRO = "poky"
DISTRO_VERSION = "5.2.1"
TUNE_FEATURES = "aarch64 crypto cortexa55"
TARGET_FPU = ""
DISTRO_NAME = "Poky (Yocto Project Reference Distro)"
ROS_DISTRO = "humble"
ROS_VERSION = "2"
ROS_PYTHON_VERSION = "3"
meta
meta-poky
meta-yocto-bsp
meta-testlayer
meta-hellmodlayer = "my-walnascar:ee0d8d8a61d8e22a3dd00c32cde58ee6e8ec458f"
meta-arm-toolchain
meta-arm = "walnascar:c63ce2117ed9ad720c61a1e2df5d381ca1ac54a2"
meta-rockchip = "walnascar:d5b97e3d82a866272f7235b7c3904bc1b85eb4c0"
meta-oe
meta-python = "walnascar:2169c9afcc0945045bea49f58011080942d4ddb4"
meta-ros-common
meta-ros2
meta-ros2-humble = "master:5fefe7f3c9e733321cb1786539a8c9c7571294cf"
workspace = "my-walnascar:ee0d8d8a61d8e22a3dd00c32cde58ee6e8ec458f"

WARNING: /home/root1/Work/yocto/poky/meta/recipes-kernel/linux/linux-yocto-dev.bb:do_compile is tainted from a forced run | ETA: 0:00:01
Sstate summary: Wanted 26 Local 13 Mirrors 0 Missed 13 Current 6897 (50% match, 99% complete)################################################################################### | ETA: 0:00:01
Removing 13 stale sstate objects for arch radxa_zero_3e: 100% |###################################################################################################################################| Time: 0:00:00
NOTE: Executing Tasks
NOTE: Tasks Summary: Attempted 13952 tasks of which 13931 didn't need to be rerun and all succeeded.

Summary: There was 1 WARNING message.
INFO: Successfully built core-image-sato. You can find output files in /home/root1/Work/yocto/poky/build/tmp/deploy/images/radxa-zero-3e

(7) 制作补丁

(0)查看代码差异
1
$ git status
1
$ git add
1
$ git commit -m "xxx xxx xxx"

提交修改的代码

(1)创建layer目录
1
$ bitbake-layers create-layer ../meta-hellodrvlayer
(2)添加layer目录
1
$ bitbake-layers add-layer ../meta-hellodrvlayer

制作补丁,并指定到layer文件夹

1
$ devtool finish linux-yocto-dev ../meta-hellodrvlayer

(8) 构建镜像

1
bitbake core-image-sato