Build Custom Loadable Kernel Module (LKM) for Raspberry Pi

From Embedded Workshop
Jump to navigation Jump to search

Begin by updating your package database

$ sudo apt-get update

Install the Raspbian header files for the kernel

$ sudo apt-get install raspberrypi-kernel-headers

It appears I may need to update my kernel to the version of the header files I just installed

$ sudo apt-get dist-upgrade

Example Linux Kernel Module source code:

/** 
* @file hello.c 
* @author Akshat Sinha 
* @date 10 Sept 2016 
* @version 0.1 
* @brief An introductory "Hello World!" loadable kernel 
* module (LKM) that can display a message in the /var/log/kern.log 
* file when the module is loaded and removed. The module can accept 
* an argument when it is loaded -- the name, which appears in the 
* kernel log files. 
*/
#include <linux/module.h>	 /* Needed by all modules */ 
#include <linux/kernel.h>	 /* Needed for KERN_INFO */ 
#include <linux/init.h>	 /* Needed for the macros */ 

///< The license type -- this affects runtime behavior 
MODULE_LICENSE("GPL"); 

///< The author -- visible when you use modinfo 
MODULE_AUTHOR("Akshat Sinha"); 

///< The description -- see modinfo 
MODULE_DESCRIPTION("A simple Hello world LKM!"); 

///< The version of the module 
MODULE_VERSION("0.1"); 

static int __init hello_start(void) 
{ 
	printk(KERN_INFO "Loading hello module...\n"); 
	printk(KERN_INFO "Hello world\n"); 
	return 0; 
} 

static void __exit hello_end(void) 
{ 
	printk(KERN_INFO "Goodbye Mr.\n"); 
} 

module_init(hello_start); 
module_exit(hello_end); 

Makefile for the above module:

#make sure to use a tab in front of the command vs spaces!
obj-m = hello.o
all:
       make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
       make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Build the module:

$ make

Load - Install the module:

$ insmod hello.ko

View log file showing the loading of the module:

$ tail /var/log/messags

Unload - remove the module:

$ rmmod hello.ko

View log file showing the loading of the module:

$ tail /var/log/messags


Helpful links:

https://www.geeksforgeeks.org/linux-kernel-module-programming-hello-world-program/
https://www.tldp.org/HOWTO/pdf/Module-HOWTO.pdf