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:

  1. Get the Firmware Hash from the raspberry pi
  2. Get the Linux Kernel Hash from the 'Firmware' github repo
  3. 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:

  1. Start up ubuntu
  2. Get the tools from https://github.com/raspberrypi/tools e.g.: git clone https://github.com/raspberrypi/tools
  3. 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-
  4. Get the source from https://github.com/raspberrypi/linux e.g.: git clone https://github.com/raspberrypi/tools
  5. Set the environment variable KERNEL_SRC to point to the kernel source: export KERNEL_SRC=/home/INSERT YOUR USER NAME HERE/linux
  6. 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.
  7. execute KERNEL_HASH=$(wget https://raw.github.com/raspberrypi/firmware/$FIRMWARE_HASH/extra/git_hash -O -) to retrieve the kernel hash.
  8. 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
  9. execute make mrproper in the KERNEL_SRC directory.
  10. 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

  11. //execute make ARCH=arm CROSS_COMPILE=${CCPREFIX} oldconfig
  12. execute make ARCH=arm CROSS_COMPILE=${CCPREFIX} menuconfig to modify the config as your wish
  13. execute make ARCH=arm CROSS_COMPILE=${CCPREFIX} -j8
  14. ...and wait.

  15. ...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

Last updated 2015/2/25 0:47:16



Compiling a kernel module for the raspberry pi 2 via Ubuntu host的更多相关文章

  1. Raspberry Pi Kernel Compilation 内核编译官方文档

    elinux.org/Raspberry_Pi_Kernel_Compilation#Use_the_provided_compiler Software & Distributions: S ...

  2. 用树莓派Raspberry Pi和Micro:bit做一个自拍器

    在这个项目中,我们将使用Python来构建一个由Micro:bit触发树莓派Raspberry Pi和相机模块的自拍器.这是开始使用硬件和简单文本编程的好方法. 我们将学习: 如何设置Raspberr ...

  3. 2019 年在 Raspberry Pi 「树莓派」上运行的 10 个操作系统推荐

    原文:2019 年在 Raspberry Pi 「树莓派」上运行的 10 个操作系统推荐 image Raspberry Pi** 是一款基于 ARM 的单板计算机,默认运行一款称为 Raspbian ...

  4. 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 ...

  5. 在ubuntu上编译rasbian kernel(for raspberry pi 1)

    raspberry pi官网的编译手册写的简洁有力,照着操作即可 https://www.raspberrypi.org/documentation/linux/kernel/building.md ...

  6. RASPBERRY PI 外设学习资源

    参考: http://www.siongboon.com/projects/2013-07-08_raspberry_pi/index.html Raspberry Pi         Get st ...

  7. Raspberry Pi I2C驱动 (Python)

    本文参考 http://www.instructables.com/id/Raspberry-Pi-I2C-Python/all/?lang=zh 作者 AntMan232 In this instr ...

  8. 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 ...

  9. Device trees, Overlays and Parameters of Raspberry Pi

    Raspberry Pi's latest kernels and firmware, including Raspbian and NOOBS releases, now by default us ...

随机推荐

  1. 网络基础 04_IP编址

    1 IP地址简介 什么是IP地址 在IP网络中,任何一个节点都需要一个唯一的IP IPV4 :32位 点分十进制 2 IP编址分类 有类编址 IP地址的类别 IP地址类型 网络地址:指代网络的地址.在 ...

  2. java中复制bean

    BeanUtils.copyProperties(p,d); p是等待被赋值的对象,d是源对象,将d中属性值赋值的p中对应的字段,d中有的属性p中必须有,p可以有更多属性

  3. hiho#1449 重复旋律6 求长度为k的串最大次数 后缀自动机

    题目传送门 题目大意:求长度为k的串的最大次数,把k从1到length的所有答案全部输出. 思路: 这道题放在$SAM$里就是求长度$k$对应的所有$right$集中最大的大小. 我们以$aabab$ ...

  4. python 进程和线程(代码知识部分)

    二.代码知识部分 一 multiprocessing模块介绍: python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情 ...

  5. Cassandra概念学习系列之Windows里下载且安装配置Cassandra(最新的3.11.1版本)(图文详解)

    不多说,直接上干货!  最近我开始在windows环境中使用Cassandra,虽然在Cassandra站点的安装命令非常清楚和精简,我仍然在环境配置上遇到一些问题.所以我想为后来者分享下我的经验. ...

  6. Android Studio: /dev/kvm device permission denied

    https://stackoverflow.com/questions/37300811/android-studio-dev-kvm-device-permission-denied To chec ...

  7. Java Struts(文件下载)

    1.从注册成功页面跳转至用户详情页面(跳转至UserListAction) 2.UserListAction调用service获得用户列表,并将这些数据传送到UserList.jsp中,UserLis ...

  8. 图说使用socket建立TCP连接

    在网络应用如火如荼的今天,熟悉TCP/IP网络编程,那是最好不过.如果你并不非常熟悉,不妨花几分钟读一读. 为了帮助快速理解,先上个图,典型的使用socket建立和使用TCP/UDP连接过程为(截图来 ...

  9. [中英对照]INTEL与AT&T汇编语法对比

    本文首先对文章Intel and AT&T Syntax做一个中英文对照翻译,然后给出一个简单的例子,再用gdb反汇编后,对INTEL与AT&T的汇编语法进行对照从而加深理解. Int ...

  10. 草稿-把vim变成IDE

    从昨天下午到现在一直在研究vim,初学者,从vim最基本的命令开始看起的.是通过vimtutor学习的. 看到最后一章的时候,发现原来vimtutor中的知识知识vim中的冰山一角,vim真正的强大之 ...