Compiling a kernel module for the raspberry pi 2 via Ubuntu host
Compiling a kernel module for the raspberry pi 2 via Ubuntu host
Normally compiling a kernel module for a linux distribution is rather straight forward, but on the raspberry pi however it's a little more involved. That's why in this article I am going to show how I build kernel modules for the raspbian wheezy distro.
There are a couple of infos on the web about how to do it, but piecing them together for a working solution took some effort. That's why I am writing them down for future use. Following these steps I successfully build kernel modules for raspberry pi 2 raspbian release 2015-02-16.
Background
Building your kernel module for the raspberry pi is a two step process. The first step is to build the complete kernel for the raspberry pi on the ubuntu machine. This will take a long time, but you'll need to do it only once. After the build is complete, you can build your kernel module fast and easy and just copy the resulting binary to the raspberry pi.
Requirements
- Raspberry Pi flashed with a raspbian wheezy distribution
Get the latest raspbian image from the raspberrypi foundations website.
- Ubuntu for cross-compiling
Both time and space constrains make building the raspberry pi kernel on the raspberry pi itself rather challenging. That's why I went to road of cross-compiling from ubuntu to raspberry pi.
If you don't have a ubuntu machine, grab a ubuntu image from their download page and spin it up in a virtual machine of your choice. Myself, I use virtualbox on a windows machine.
- Patience
- sudo apt-get install libc6-i386 lib32z1 lib32stdc++6
- sudo apt-get install git ncurses-dev make
Getting the correct source
The normal way
Getting the correct source to build the kernel is trickier than one would guess. As explained on kernelnewbies.org you need the exact version of the headers from the kernel that the modules are built for.
Normally you'd simply install the kernel header package for the specific kernel version that you get when you execute uname -r, but while raspbian provides header packages, the raspberry pi foundation, who modifies them does not, as you'll find out if you read through raspberry pi orgs forums.
The raspberry way
What they do however, is keep the complete source in their github linux repository. When they build a raspberry pi firmware, which is proprietery, closed source btw, those binaries are checked-in in the firmware repository including a file that contains the git hash that points to the commit in the linux repo that they used to build it.
So luckily for us there is a link that you can follow to get the exact source code and the steps are as follows:
- Get the Firmware Hash from the raspberry pi
- Get the Linux Kernel Hash from the 'Firmware' github repo
- Get the Linux Kernel Source from the 'Linux' github repo
Thanks to the raspberry pi forms user 'Zeta' who put a script together and posted it in this thread to perform those steps:
# Get the Firmware Hash from the Raspberry Pi
FIRMWARE_HASH=$(zgrep "* firmware as of" /usr/share/doc/raspberrypi-bootloader/changelog.Debian.gz | head -1 | awk '{ print $5 }')
# Get the git hash for this kernel
KERNEL_HASH=$(wget https://raw.github.com/raspberrypi/firmware/$FIRMWARE_HASH/extra/git_hash -O -)
# Checkout the files on ubuntu
git checkout $KERNEL_HASH
Building the kernel
Armed with this knowledge we can now finally execute a step by step plan:
- Start up ubuntu
- Get the tools from https://github.com/raspberrypi/tools e.g.:
git clone https://github.com/raspberrypi/tools - Set the environment variable CCPREFIX to point to the master tools like so:
export CCPREFIX=/home/INSERT YOUR USER NAME HERE/tools-master/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi- - Get the source from https://github.com/raspberrypi/linux e.g.:
git clone https://github.com/raspberrypi/tools - Set the environment variable KERNEL_SRC to point to the kernel source:
export KERNEL_SRC=/home/INSERT YOUR USER NAME HERE/linux - on the raspberry pi execute
FIRMWARE_HASH=$(zgrep "* firmware as of" /usr/share/doc/raspberrypi-bootloader/changelog.Debian.gz | head -1 | awk '{ print $5 }')to retrieve the firmware hash. - execute
KERNEL_HASH=$(wget https://raw.github.com/raspberrypi/firmware/$FIRMWARE_HASH/extra/git_hash -O -)to retrieve the kernel hash. - back on ubuntu checkout the correct version of the kernel source code inside the KERNEL_SRC directory using git:
git checkout INSERT_KERNEL_HASH_HERE - execute
make mrproperin the KERNEL_SRC directory. - copy /proc/config.gz from the raspberry pi to the KERNEL_SRC directory.
e.g.
scp pi@ipaddress:/proc/config.gz ./and unpack it:zcat config.gz > .config - //execute
make ARCH=arm CROSS_COMPILE=${CCPREFIX} oldconfig execute make ARCH=arm CROSS_COMPILE=${CCPREFIX} menuconfigto modify the config as your wish- execute
make ARCH=arm CROSS_COMPILE=${CCPREFIX} -j8 - ...and wait.

- ...keep waiting.
If everything worked out ok, the kernel should be successfully rebuilt. Now the source directory can be used to built kernel modules for your raspberry pi.
Building Kernel modules
Now that the kernel has been successfully built, a new MOdule.symvers has been created. You can now use a make file that references the kernel source directory to built the kernel module only. E.g. use a line like:
PREFIX = /home/[USER]/tools-master/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-
make ARCH=arm CROSS_COMPILE=$(PREFIX) modules -j8
make ARCH=arm CROSS_COMPILE=$(PREFIX) INSTALL_MOD_PATH=/*wherever you want*/ modules_install
As I need to continually build kernel modules for more than one kernel version, I copy the source directory and rename it to match the kernel version. If a new kernel version comes out, I just checkout the source in a new folder. In the makefile I reference the different source folders to build the kernel module for different kernel versions.For an example of a makefile you can take a look here.
References
- http://downloads.raspberrypi.org/raspbian_latest
- http://www.ubuntu.com/download/desktop
- http://elinux.org/Raspberry_Pi_Kernel_Compilation
- http://bchavez.bitarmory.com/archive/2013/01/16/compiling-kernel-modules-for-raspberry-pi.aspx
- http://www.raspberrypi.org/forums/viewtopic.php?f=66&t=57401
Last updated 2015/2/25 0:47:16
Compiling a kernel module for the raspberry pi 2 via Ubuntu host的更多相关文章
- Raspberry Pi Kernel Compilation 内核编译官方文档
elinux.org/Raspberry_Pi_Kernel_Compilation#Use_the_provided_compiler Software & Distributions: S ...
- 用树莓派Raspberry Pi和Micro:bit做一个自拍器
在这个项目中,我们将使用Python来构建一个由Micro:bit触发树莓派Raspberry Pi和相机模块的自拍器.这是开始使用硬件和简单文本编程的好方法. 我们将学习: 如何设置Raspberr ...
- 2019 年在 Raspberry Pi 「树莓派」上运行的 10 个操作系统推荐
原文:2019 年在 Raspberry Pi 「树莓派」上运行的 10 个操作系统推荐 image Raspberry Pi** 是一款基于 ARM 的单板计算机,默认运行一款称为 Raspbian ...
- Raspberry Pi 3 --- Kernel Building and Run in A New Version Kernal
ABSTRACT There are two main methods for building the kernel. You can build locally on a Raspberry Pi ...
- 在ubuntu上编译rasbian kernel(for raspberry pi 1)
raspberry pi官网的编译手册写的简洁有力,照着操作即可 https://www.raspberrypi.org/documentation/linux/kernel/building.md ...
- RASPBERRY PI 外设学习资源
参考: http://www.siongboon.com/projects/2013-07-08_raspberry_pi/index.html Raspberry Pi Get st ...
- Raspberry Pi I2C驱动 (Python)
本文参考 http://www.instructables.com/id/Raspberry-Pi-I2C-Python/all/?lang=zh 作者 AntMan232 In this instr ...
- Roomblock: a Platform for Learning ROS Navigation With Roomba, Raspberry Pi and RPLIDAR(转)
What is this? "Roomblock" is a robot platform consists of a Roomba, a Raspberry Pi 2, a ...
- Device trees, Overlays and Parameters of Raspberry Pi
Raspberry Pi's latest kernels and firmware, including Raspbian and NOOBS releases, now by default us ...
随机推荐
- Angular Material 按钮图标系列
工做中经常用到Angular Material 中的好多按钮素材,奈何官网经常上不去,所以只能自己把这些常用的按钮扒下来了,留给自己同时也留给大家方便查看. Angular material mat- ...
- Mac下在Shell终端下使用open快速打开窗口文件夹
Ubuntu下可以使用nautilus打开,但是在Mac替代的是open. 打开当前路径的窗口 oepn . 打开其他窗口 open /dirname 其实open不只可以打开窗口,应用同样支持. 关 ...
- WCF系列教程之WCF服务宿主与WCF服务部署
本文参考自http://www.cnblogs.com/wangweimutou/p/4377062.html,纯属读书笔记,加深记忆. 一.简介 任何一个程序的运行都需要依赖一个确定的进程中,WCF ...
- hibernate关联关系的crud之级联
cascade级联,只会影响CRUD的CUD,不会影响读取.不设置级联,从多的一方能读出一的一方,设了级联,从一的一方,默认也不能读出多的一方. 如果两个对象之间有关联,不管是一对多,多对一,单向还是 ...
- Android Studio: /dev/kvm device permission denied
https://stackoverflow.com/questions/37300811/android-studio-dev-kvm-device-permission-denied To chec ...
- WPF中使用相对资源来进行绑定,数据源是通过DataContext来指定的
1. 最外层是Window是对象,Window的ItemsControl使用了ItemsTemplate,然后在ItemsTemplate中要绑定Language属性, 而整个Window的数据源是通 ...
- [中英对照]User-Space Device Drivers in Linux: A First Look | 初识Linux用户态设备驱动程序
如对Linux用户态驱动程序开发有兴趣,请阅读本文,否则请飘过. User-Space Device Drivers in Linux: A First Look | 初识Linux用户态设备驱动程序 ...
- Leetcode 483. Smallest Good Base
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- 使用jquery获取url及url参数的方法(转)
转自:http://www.cnblogs.com/babycool/p/3169058.html 使用jquery获取url以及使用jquery获取url参数是我们经常要用到的操作 1.jquery ...
- JMM随笔
What? Java内存模型(Java Memory Model,JMM)主要是为了规定了线程和内存之间的一些关系. 根据JMM的设计: 系统存在一个主内存(Main Memory),Java中所有变 ...