简体   繁体   English

如何为Raspberry pi编译内核模块?

[英]How to compile a kernel module for Raspberry pi?

I'm having trouble compiling a kernel module for a raspberry pi. 我在为raspberry pi编译内核模块时遇到了麻烦。 I want to compile a "hello world" kernel module using the raspberry pi itself. 我想使用raspberry pi本身编译一个“hello world”内核模块。

I am using raspbian wheezy 3.6.11+. 我正在使用raspbian wheezy 3.6.11+。

I tried following the directions at http://elinux.org/RPi_Kernel_Compilation . 我尝试按照http://elinux.org/RPi_Kernel_Compilation上的说明进行操作

Here is the Makefile I am using: 这是我正在使用的Makefile:

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的源代码:

/*  
 *  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+ 我尝试在/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. 我安装了GNU Make 3.81和gcc(Debian 4.6.3-14 + rpi1)4.6.3。 I also installed the linux source using 我也用linux安装了linux源代码

sudo apt-get install linux-source 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!). 编译模块时, -C参数应指向构建内核的源树(不要清理它!)。 If you built it on the pi its likely in a directory under your home directory. 如果你在pi上构建它可能在你的主目录下的目录中。

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. /lib/modules/<version>下的build目录是Debian-ism,其中源树的简化版本提供了足够的上下文来构建模块。 The kernels from the Raspberry Pi Foundation kernels don't ship with a build directory. Raspberry Pi Foundation内核的内核不附带build目录。

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. 它们可能有点过时,但raspbian提供了一个内核作为Debian风格的包,它应该包含你可以用来构建内核模块的build目录。

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. 以下是我在Raspbian上构建Hello World内核模块的步骤。

  1. Perform sudo rpi-update 执行sudo rpi-update

    See https://github.com/Hexxeh/rpi-update for details on rpi-update . 有关rpi-update详细信息,请参阅https://github.com/Hexxeh/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. 安装并运行rpi-source以安装构建您正在运行的最新内核的源代码。 This will create the correct entry in /lib/modules for the kernel that you are running. 这将在/lib/modules为正在运行的内核创建正确的条目。 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. 注意:您不需要root用户来运行它,但是脚本将使用sudo执行某些任务,并且在脚本执行期间将请求root密码。

    Instructions to install rpi-source can be found at https://github.com/notro/rpi-source/wiki 有关安装rpi-source说明,请访问https://github.com/notro/rpi-source/wiki

Once those steps are performed you should be able to make the Hello World kernel module. 执行这些步骤后,您应该能够make Hello World内核模块。

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 : 就像Greg说的那样,raspbian发行版提供了包:

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). 然后,告诉raspbian启动你新安装的内核(对我来说是3.10-3-rpi)。
Append this at end of /boot/config.txt and reboot your Pi : /boot/config.txt末尾添加它并重启你的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 : 然后,修改Makefile以指向新安装的内核头文件:

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 . 我不得不编译并安装内核模式驱动程序。经过长时间的搜索,我从这里获得了pi 2(3.18.7-v7 +)的头文件。

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. 我正在使用完全相同的内核在我的RPI上处理完全相同的样本。 I managed to compile the module on my RPI but when I issued insmod I received an error. 我设法在我的RPI上编译模块但是当我发出insmod时我收到了一个错误。 I followed the instructions here on an XUbuntu virtual machine (using my RPI's kernel version 3.6.y) and it worked perfectly. 我按照这里的说明 XUbuntu虚拟机上(使用我的RPI的内核版本3.6.y),它完美地工作。 Not sure why compiling directly on the RPI did not work, that will be a problem for another day. 不确定为什么直接在RPI上编译不起作用,这将是另一天的问题。

I had to change the Makefile to match the new environment. 我不得不更改Makefile以匹配新环境。

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修复它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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