Itron Riva Kernel Module Building Table of Contents Introduction... 2 Creating the Project Directory... 2 Creating the Makefile... 3 Creating main.c... 5 Building The Kernel Module... 6 1
Introduction What follows is a step-by-step introduction to building a Linux kernel module for the Itron Riva platform. Using these steps, you can build Linux kernel modules for any of the following Itron Riva devices: Itron Riva Dev Board Itron Riva Dev Mini Board Itron Riva SmartNIC Itron Riva Electric Meter Itron Riva CAM (CGR ACT Module) The following assumptions are being made: You have at least one Itron Riva Dev boards mentioned above You have installed the Itron Riva SDK (Software Developers Kit) You are using Ubuntu 16.04. Other Linux versions should work, but the steps here were performed on an Ubuntu 16.04 system. You are familiar with using the Linux command-line prompt By following these steps, you will create a Linux kernel module named hello.ko. You will load and unload the kernel module, observing messages from the module by using the dmesg command. Creating the Project Directory The first step is to open a command-line prompt: $ The next step is to change to a directory where you want to create this project. In this case we will put the project in the Documents/Projects directory, so we change to that: $ cd Documents/Projects $ Now create a directory named hello-km. This is the directory where you will create the kernel module. $ mkdir hello-km 2
Finally change into the hello-km directory: $ cd hello-km Creating the Makefile Next you will create a Makefile with the contents shown below. Using the text editor of your choice (e.g. vim or nano), create a file named Makefile that has the following contents: # # Makefile for building Itron Riva Kernel Modules # # For questions/support: norm.mcentire@itron.com # # 2017-09-17 # obj-m := km-module.o #km-module-objs := main.o load.o unload.o km-module-objs := main.o KERNELDIR := /home/nmcentire/documents/p4client/openwayinternational/riva_openway_a RCHIMEDES_SR_4-1/Workspace/ItronRiva-Dev/build_armv7l-timesys-linuxuclibcgnueabi/linux-4.4 PWD := $(shell pwd) all: 3
$(MAKE) -C $(KERNELDIR) ARCH=arm CROSS_COMPILE=armv7l-timesyslinux-uclibcgnueabi- M=$(PWD) modules clean: rm -rf *.ko *.o *.mod.c core Module.symvers Module.markers modules.order.*.cmd.tmp_versions You can test your Makefile out from the command-line by typing make: $ make make -C /home/nmcentire/documents/p4client/openwayinternational/riva_openway_a RCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linuxuclibcgnueabi/linux-4.4 ARCH=arm CROSS_COMPILE=armv7l-timesys-linuxuclibcgnueabi- M=/home/nmcentire/Documents/Projects/hello-km modules make[1]: Entering directory '/home/nmcentire/documents/p4client/openwayinternational/riva_openway_a RCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linuxuclibcgnueabi/linux-4.4' Makefile:669: Cannot use CONFIG_CC_STACKPROTECTOR_REGULAR: -fstackprotector not supported by compiler make[1]: armv7l-timesys-linux-uclibcgnueabi-gcc: Command not found make[2]: *** No rule to make target '/home/nmcentire/documents/projects/hellokm/main.o', needed by '/home/nmcentire/documents/projects/hello-km/kmmodule.o'. Stop. Makefile:1404: recipe for target '_module_/home/nmcentire/documents/projects/hello-km' failed 4
make[1]: *** [_module_/home/nmcentire/documents/projects/hello-km] Error 2 make[1]: Leaving directory '/home/nmcentire/documents/p4client/openwayinternational/riva_openway_a RCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linuxuclibcgnueabi/linux-4.4' Makefile:10: recipe for target 'all' failed make: *** [all] Error 2 From the about above, notice that you will receive an error. This is normal, because you have not yet created the main.c. Creating main.c Next you will create a main.c source file with the contents shown below. Using the text editor of your choice (e.g. vim or nano), create a file named main.c that has the following contents: /* * main.c * * Main file for hello-km kernel module * * For questions/support: norm.mcentire@itron.com * * 2017-09-17 * */ #include <linux/module.h> //#include "load.h" 5
//#include "unload.h" static int init km_hello_module_init(void) { printk(kern_info "km_hello_module_init\n"); return 0; } module_init(km_hello_module_init); static void exit km_hello_module_exit(void) { printk(kern_info "km_hello_module_exit\n"); } module_exit(km_hello_module_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Norm McEntire <norm.mcentire@itron.com>"); MODULE_DESCRIPTION("Itron Riva Dev Hello Kerenel Module"); You are now ready to build the kernel module. Building The Kernel Module You should now have two files in your project: Makefile and main.c: $ ls Makefile main.c To build the project, make sure the cross-compile toolchain is in your path: $ echo $PATH 6
80/toolchain/bin:/home/nmcentire/bin:/home/nmcentire/.local/bin:/usr/local/sbin:/usr /local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin If your toolchain is not in your PATH, you can set it like this: $ export PATH=/home/nmcentire/itron-isom-4-1-80/toolchain/bin:$PATH After making sure your PATH is setup correctly, you can now make your project by typing make: $ make make -C /home/nmcentire/documents/p4client/openwayinternational/riva_openway_a /home/nmcentire/itron-isom-4-1- RCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linuxuclibcgnueabi/linux-4.4 ARCH=arm CROSS_COMPILE=armv7l-timesys-linuxuclibcgnueabi- M=/home/nmcentire/Documents/Projects/hello-km modules make[1]: Entering directory '/home/nmcentire/documents/p4client/openwayinternational/riva_openway_a RCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linuxuclibcgnueabi/linux-4.4' CC [M] /home/nmcentire/documents/projects/hello-km/main.o LD [M] /home/nmcentire/documents/projects/hello-km/km-module.o Building modules, stage 2. MODPOST 1 modules CC /home/nmcentire/documents/projects/hello-km/km-module.mod.o LD [M] /home/nmcentire/documents/projects/hello-km/km-module.ko make[1]: Leaving directory '/home/nmcentire/documents/p4client/openwayinternational/riva_openway_a 7
RCHIMEDES_SR_4-1/Workspace/SmartNic-Dev/build_armv7l-timesys-linuxuclibcgnueabi/linux-4.4' 8