简体   繁体   中英

How to compile a kernel module for Raspberry pi?

I'm having trouble compiling a kernel module for a raspberry pi. I want to compile a "hello world" kernel module using the raspberry pi itself.

I am using raspbian wheezy 3.6.11+.

I tried following the directions at http://elinux.org/RPi_Kernel_Compilation .

Here is the Makefile I am using:

obj-m += hello-1.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

Here is the source code for hello-1.c:

/*  
 *  hello-1.c - The simplest kernel module.
 */
#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */

int init_module(void)
{
    printk(KERN_INFO "Hello world 1.\n");

    /* 
     * A non 0 return means init_module failed; module can't be loaded. 
     */
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye world 1.\n");
}

Here's what I get when I try to make the project:

root@raspberrypi:/home/pi/hello-module# make
make -C /lib/modules/3.6.11+/build M=/home/pi/hello-module modules
make: *** /lib/modules/3.6.11+/build: No such file or directory.  Stop.
make: *** [all] Error 2

I tried creating the build directory at /lib/modules/3.6.11+

make -C /lib/modules/3.6.11+/build M=/home/pi/hello-module modules
make[1]: Entering directory `/lib/modules/3.6.11+/build'
make[1]: *** No rule to make target `modules'.  Stop.
make[1]: Leaving directory `/lib/modules/3.6.11+/build'
make: *** [all] Error 2

I have GNU Make 3.81 and gcc (Debian 4.6.3-14+rpi1) 4.6.3 installed. I also installed the linux source using

sudo apt-get install linux-source

Any ideas on what I might do to get this to compile?

When compiling a module the -C parameter should point to the source tree where the kernel was built (don't clean it up!). If you built it on the pi its likely in a directory under your home directory.

The build directory under /lib/modules/<version> is a Debian-ism, where a cut-down version of the source tree is provided with just enough context to build modules against. The kernels from the Raspberry Pi Foundation kernels don't ship with a build directory.

They may be a bit out of date, but raspbian provides a kernel as a Debian-style package, which should include the build directory you could use to build kernel modules against.

sudo aptitude install linux-image-rpi-rpfv linux-headers-rpi-rpfv

Here are the steps I used to build the Hello World kernel module on Raspbian.

  1. Perform sudo rpi-update

    See https://github.com/Hexxeh/rpi-update for details on rpi-update . You have to be on the latest firmware and associated kernel to be able to perform the next step.

  2. Install and run rpi-source to install the source code that built the latest kernel that you are running. This will create the correct entry in /lib/modules for the kernel that you are running. Note: you don't need to be root to run this, however the script will perform certain tasks using sudo and the root password will be requested during the script execution.

    Instructions to install rpi-source can be found at https://github.com/notro/rpi-source/wiki

Once those steps are performed you should be able to make the Hello World kernel module.

johnma@raspberrypi ~/HelloWorld $ make
make -C /lib/modules/3.12.19+/build M=/home/johnma/HelloWorld modules
make[1]: Entering directory `/home/johnma/linux-c3db7205bcd8988cf7c185e50c8849542554b1f5'
  CC [M]  /home/johnma/HelloWorld/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/johnma/HelloWorld/hello.mod.o
  LD [M]  /home/johnma/HelloWorld/hello.ko
make[1]: Leaving directory `/home/johnma/linux-c3db7205bcd8988cf7c185e50c8849542554b1f5'

johnma@raspberrypi ~/HelloWorld $ sudo insmod hello.ko
johnma@raspberrypi ~/HelloWorld $ tail -1 /var/log/syslog
May 15 13:45:39 raspberrypi kernel: [59789.169461] Hello World :)

johnma@raspberrypi ~/HelloWorld $ sudo rmmod hello.ko
johnma@raspberrypi ~/HelloWorld $ tail -1 /var/log/syslog
May 15 13:46:10 raspberrypi kernel: [59819.503503] Goodbye World!

You first need kernel headers (and the corresponding kernel binary) to build your module.
Like Greg said, the raspbian distribution provides the packages :

sudo apt-get install linux-image-rpi-rpfv linux-headers-rpi-rpfv

Then, tell raspbian to boot your newly installed kernel (3.10-3-rpi for me).
Append this at end of /boot/config.txt and reboot your Pi :

# Parameters to boot on raspbian kernel (linux-image-rpi-rpfv package)
kernel=vmlinuz-3.10-3-rpi
initramfs initrd.img-3.10-3-rpi followkernel

Then, modify your Makefile to point the freshly installed kernel headers :

KERNEL_HEADERS=/lib/modules/$(shell uname -r)/build

obj-m := hello-1.o

all:
    @$(MAKE) -C $(KERNEL_HEADERS) M=$(PWD) modules

clean:      
    @$(MAKE) -C $(KERNEL_HEADERS) M=$(PWD) clean

This was a pain. I had to compile and install a kernel mode driver.After long search, i got the headers for pi 2 (3.18.7-v7+) from here .

sudo apt-get install dkms build-essential
wget http://www.niksula.hut.fi/~mhiienka/Rpi/linux-headers-rpi/linux-headers-3.18.7-v7%2b_3.18.7-v7%2b-2_armhf.deb
sudo dpkg -i linux-headers-3.18.7-v7+_3.18.7-v7+-2_armhf.deb

I was working on the exact same sample on my RPI with the exact same kernel. I managed to compile the module on my RPI but when I issued insmod I received an error. I followed the instructions here on an XUbuntu virtual machine (using my RPI's kernel version 3.6.y) and it worked perfectly. Not sure why compiling directly on the RPI did not work, that will be a problem for another day.

I had to change the Makefile to match the new environment.

obj-m += hello-1.o

all:
        make ARCH=arm CROSS_COMPILE=${CCPREFIX} -C /home/cstick/rpi/linux-rpi-3.6.y M=$(PWD) modules
clean:
        make -C /home/cstick/rpi/linux-rpi-3.6.y M=$(PWD) clean

我遇到了同样的问题,只需通过sudo apt-get install raspberrypi-kernel-headers修复它

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM