Linux Device Driver Development Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

Let's see how to do it by following these steps:

  1. After placing the dummy-code.c and Makefile files in our current working directory on the host PC, it should look like the following when using the ls command, as follows:
$ ls
dummy-code.c Makefile
  1. Then, we can compile our module by using the following command:
$ make KERNEL_DIR=../../../linux/
make -C ../../../linux/ \
ARCH=arm64 \
CROSS_COMPILE=aarch64-linux-gnu- \
SUBDIRS=/home/giometti/Projects/ldddc/github/chapter_2/module modules
make[1]: Entering directory '/home/giometti/Projects/ldddc/linux'
CC [M] /home/giometti/Projects/ldddc/github/chapter_2/module/dummy-code.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/giometti/Projects/ldddc/github/chapter_2/module/dummy-code.mod.o
LD [M] /home/giometti/Projects/ldddc/github/chapter_2/module/dummy-code.ko
make[1]: Leaving directory '/home/giometti/Projects/ldddc/linux'

As we can see, now we have several files in the current working directory, and one of them is named dummy-code.ko; this is our kernel module ready to be transferred to the ESPRESSObin!

  1. Once the module has been moved into the target system (for example, by using the scp command), we can load it by using the insmod utility, as follows:
# insmod dummy-code.ko
  1. Now, by using the lsmod command, we can ask the system to display all loaded modules. On my ESPRESSObin, I only have the dummy-code.ko module, so my output is as shown:
# lsmod 
Module Size Used by
dummy_code 16384 0
Note that the .ko postfix has been removed by the kernel module name, as the - character is replaced by _.
  1. Then, we can remove our module from the kernel by using the rmmod command, as follows:
# rmmod dummy_code
In case you get the following error, please verify you're running the correct Image file we got in Chapter 1 Installing the Development System
rmmod: ERROR: ../libkmod/libkmod.c:514 lookup_builtin_file() could not open builtin file '/lib/modules/4.18.0-dirty/modules.builtin.bin'