This post show cross compile kernel module for Raspberry Pi on Ubuntu. It success to build kernel module, hello.ko, run on Raspberry Pi. It fail after the Pi update firmware with rpi-update!.
To make sure the source used to compile our code is same as that running on Raspberry Pi,
Cross compile Raspberry Pi kernel from source on Ubuntu and move the kernel.img and files to SD Card.
In Ubuntu, create hello.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
static int __init hello_init(void)
{
printk(KERN_INFO "Hello Raspberry Pi!\n");
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_INFO "Bye Raspberry Pi!\n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile
obj-m += hello.o
all:
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -C /<git clone linux directory> M=$(PWD) modules
clean:
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -C /<git clone linux directory>
M=$(PWD) clean
Where <git clone linux directory> is the git clone linux directory in
Cross compile Raspberry Pi kernel from source on Ubuntu and move the kernel.img and files to SD Card.
Then build the kernel module with the command:
$
make
Copy the generated hello.ko to Raspberry Pi.
Insert the module with the command:
$
sudo insmod hello.ko
or, remove it with the command:
$
sudo rmmod hello.ko
You can type
dmesg command to check that the word "
Hello Raspberry Pi!" and "
Bye Raspberry Pi!" logged. They are printed to kernel by hello.ko with the function printk().
The fail half is:
Once you update Raspberry Pi's firmmware with the command:
$
sudo rpi-update
Then reboot. hello.ko cannot be inserted again in new firmware, caused with "Invalid module format"!
Remark:
- It's 3.10.27+ #3 before rpi-update, with hello.ko insert successful.
- Updated to 3.10.27+ #630 PREEMPT after rpi-update, with insmod fail.