官术网_书友最值得收藏!

Getting ready

To turn our dummy-code.c file into a kernel module, we only have to change our kernel settings, allowing the compilation of our example module (by replacing the * character with M in the kernel configuration menu). However, under some circumstances, it could be more useful having our driver released into a dedicated archive completely separated from kernel sources. Even in this case, no changes are to be done to the existing code, and we will be able to compile dummy-code.c inside the kernel source tree, or even outside it!

To build up our first kernel module as external code, we can safely take the preceding dummy-code.c file and then put it into a dedicated directory with the following Makefile:

ifndef KERNEL_DIR
$(error KERNEL_DIR must be set in the command line)
endif
PWD := $(shell pwd)
ARCH ?= arm64
CROSS_COMPILE ?= aarch64-linux-gnu-

# This specifies the kernel module to be compiled
obj-m += dummy-code.o

# The default action
all: modules

# The main tasks
modules clean:
make -C $(KERNEL_DIR) \
ARCH=$(ARCH) \
CROSS_COMPILE=$(CROSS_COMPILE) \
SUBDIRS=$(PWD) $@

Looking at the preceding code, we see that the KERNEL_DIR variable must be supplied on the command line pointing to the path to ESPRESSObin's previously compiled kernel sources, while the ARCH and CROSS_COMPILE variables are not mandatory since Makefile specifies them (however, supplying them on the command line will take precedence).

Also, we should verify that the insmod and rmmod commands are available in our ESPRESSObin, as follows:

# insmod -h
Usage:
insmod [options] filename [args]
Options:
-V, --version show version
-h, --help show this help

If they are not present, then they can be installed by adding the kmod package with the usual apt install kmod command.

主站蜘蛛池模板: 永川市| 伊川县| 航空| 龙山县| 兰考县| 淮滨县| 安庆市| 汤原县| 南和县| 云林县| 资兴市| 肥东县| 镇原县| 广丰县| 德钦县| 凯里市| 嵊州市| 米泉市| 泰安市| 河东区| 勐海县| 北票市| 广昌县| 巴南区| 湟中县| 长岭县| 枞阳县| 黎川县| 同江市| 阿尔山市| 登封市| 乌拉特中旗| 宁安市| 昌江| 岢岚县| 柳河县| 兴城市| 吉木乃县| 西青区| 类乌齐县| 望都县|