Getting Started with Embedded Linux: Part Ten - Building a Raspberry Pi kernel - Embedded.com

Getting Started with Embedded Linux: Part Ten — Building a Raspberry Pi kernel

In the last installment, we set up a Raspberry Pi SBC (Single Board Computer) and used it to build a simple driver. This is an example of self-hosting, the development model in which we use an embedded target as our development platform.

We are going to take this one step further in this installment. First we will build the kernel on the Raspberry Pi, and then build it on a faster host system. Using a separate host for development is an example of the cross-development model.

Building the Kernel on the RPi
Last time, we used the rpi-source script to download and install the headers required to build a kernel module on the RPi. The same developer, notro , who created that script, has also created a rpi-build script which makes it easy to build the kernel. Instructions can be found on the GitHub page:

$ wget https://raw.githubusercontent.com/notro/rpi-build/master/rpi-build
$ sudo mv rpi-build /usr/bin/rpi-build
$ sudo chmod +x /usr/bin/rpi-build

While the rpi-source script was written in Python, the rpi-build script is written in Ruby which needs to be installed before we can run it:

$ sudo apt-get update
$ sudo apt-get install ruby

The first time you run rpi-build , it will check for missing dependencies. Reply Y when asked whether you want to install these dependencies. After this, we can build the kernel using this script:

$ rpi-build use[stdlib] linux install

This is going to take a while. While the script downloads files and starts compiling the kernel files, let's bring up our VM running Fedora and look at building the Raspberry Pi kernel on a faster system.

We are going to use the git source control system to make a copy of the cross tool chain and kernel from the Rasbberry Pi repository on github.com . First we need to install git as root:

$ su
Password:
# yum install git

And then we can download the tool chain and kernel source:

$ git clone –depth 1 git://github.com/raspberrypi/tools.git
$ git clone –depth 1 git://github.com/raspberrypi/linux.git

Let's get the Linux kernel ready to build:

$ cd ~/linux
$ make mrproper
$ export CCPREFIX=~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-

This selects the Linaro version of GCC-4.8.3 as our cross compiler. A cross compiler runs on one system, in this case our VM running on an X86-64 host system, and generates object code which will execute on a different processor, in this case, ARM, and specifically for the Broadcom BCM2708 processor used by the Raspberry Pi.

We are going to use the default Linux configuration for the Raspberry Pi:

$ cp arch/arm/configs/bcmrpi_defconfig .config
$ make ARCH=arm CROSS_COMPILE=${CCPREFIX} oldconfig

Alternately, we could have copied the configuration from the Raspberry Pi, which can be found in /proc/config.gz . Use gunzip to unpack this file and rename it from config to .config before running make oldconfig as shown above. When you do this, you will be asked about new options which have been added to the kernel but which are not mentioned in the .config file you are using. I simply hit return for all of these (many) options, selecting the default.

We are now ready to build on our host X86 system for the ARM target:

$ make ARCH=arm CROSS_COMPILE=${CCPREFIX}
$ make ARCH=arm CROSS_COMPILE=${CCPREFIX} modules

Building on an X86 host, even a VM, takes under an hour. On the Raspberry Pi, the kernel build takes about 6.5 hours.

Installing the kernel
On the Raspberry Pi, we can copy the newly built kernel to the boot directory renaming it:

# cp /linux/arch/arm/boot/Image /boot/kernel_rpi.img

Onthe host system, things are a bit more complicated. Power off theRaspberry Pi and remove the SDcard. Insert the SDcard into a card readeron the host system. Mount the boot partition from the SDcard on thehost. Now we can copy the kernel built on the host to correct locationon the SDcard. We can also install the modules into a temporarydirectory, then merge these into the /lib directory on the SDcard.

$ cp ~/linux/arch/arm/boot/Image /run/media/eager/boot/kernel_host.img
$ make ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=~/modules modules_install
$ sudo cp -r ~/modules/lib /run/media/eager/*/lib

Onelast thing before we take the SDcard from the host system and reinsertit into the Raspberry Pi. Edit the kernel command line to specify thatwe want to boot the kernel built on the host. Open /boot/config.txt in an editor and make the following change:

#kernel=kernel.img
kernel=kernel_host.img

If the default kernel=kernel.img line is missing, just add the second line at the bottom of the file.

Unmountthe file systems from the SDcard and remove it from the host system.Reinsert it into the Raspberry Pi and power it on. If everything hasbeen done correctly, our new Linux kernel will boot on the Rpi. We canconfirm this by looking at the build date displayed when we log in.

Summary
TheRaspberry Pi is a moderately powerful single board computer, so it ispossible to build the kernel on the target, even if it takes severalhours. Building the entire file system, with all of the user librariesand applications might take days.

We've begun looking atcross-platform development which is frequently used with embeddedsystems. In this model, we build on a powerful host system using across-development tool chain, generating object code for the target. Inthis example, we physically moved the SDcard which contained the rootfile system from the target system and mounted it on the host. This is aversion of what is sometimes called sneakernet . There areother ways to copy a kernel or program which is built on a host systemusing a cross-compiler to a target system, which we will discuss in thenext installment.


Join over 2,000 technical professionals and embedded systems hardware, software, and firmware developers at ESC BostonMay 6-7, 2015, and learn about the latest techniques and tips forreducing time, cost, and complexity in the development process.

Passes for the ESC Boston 2015 Technical Conferenceare available at the conference's official site, with discountedadvance pricing until May 1, 2015. Make sure to follow updates about ESCBoston's other talks, programs, and announcements via the Destination ESC blog on Embedded.com and social media accounts Twitter, Facebook, LinkedIn, and Google+.

The Embedded Systems Conference, EE Times, and Embedded.com are owned by UBM Canon.

1 thought on “Getting Started with Embedded Linux: Part Ten — Building a Raspberry Pi kernel

  1. “”The Raspberry Pi is a moderately powerful single board computer, so it is possible to build the kernel on the target, even if it takes several hours. Building the entire file system, with all of the user libraries and applications might take days.”nn

    Log in to Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.