Cross compiling Raspbian Kernel

I recently needed to build a number of additional kernel modules for the Rasbian distro to support the LTC4151 voltage and current monitor. The project in question required this to monitor a sealed lead acid battery power source and charging voltage.

I found a number of articles online with instruction on how to do this, however, many of them were out of date. The best link I found was on the elinux wiki: RPi Kernel Compilation, however, even this was a little hard to follow. What follows is the recipe that worked for me on an x86_64 funning Fedora 17, mostly based on the elinux page - with some of the rubbish removed.

So, first things first, we need to download the cross-compiler toolchain, the Linux kernel sources and the Raspberry Pi firmware as follows:

Get the toolchain:

One can clone a local git repo if so inclined, however, if you do not intend to start developing the toolchain, downloading a tar of the repo is much quicker and requires less space.

 $ wget 
 $ tar xvf master.tar.gz

Get kernel sources:

Again, with the kernel sources, unless you intend to start developing and submitting patches for the raspbian kernel, I would just download a tar as follows.

 $ wget
 $ tar xvf rpi-3.6.y.tar.gz

And finally, get the firmware

At the time of writing the master branch of the firmware contains the firmware for the 3.2.x kernels, so we need the 'next' branch for 3.6.x kernels.

 $ wget
 $ tar xvf next.tar.gz

Setup some helper variables

Rather than having to remember a number of different directories we will now define a number of helper variables that will be used during the cross-compilation process.

 $ export WORKDIR=${PWD}
 $ export CCPREFIX=${PWD}/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-
 $ export SRC=${PWD}/linux-rpi-3.6.y
 $ mkdir -p ./rootfs/{boot,lib}
 $ export ROOTFS=${PWD}/rootfs

Compile kernel

Before we can compile the kernel we need to get a working configuration file, the easiest way to get this is from a live Raspberry Pi with the following command.

 $ zcat /proc/config.gz > config

Transfer this 'config' file to the the head of the kernel sources tree downloaded earlier and rename it '.config'.

We are now ready to start the compilation, change to the kernel sources directory and run make as follows. (NOTE: if on a multicore system one can use the -j flag)

 $ cd $SRC

This can take a while depending on the system used to cross-compile. Once this stage is complete you will find the new kernel image files in the '${SRC}/arch/arm/boot' directory, we now need to install the modules built in the compilation to our '${ROOTFS} directory we created earlier, as follows.


Transfer the build

We now have everything we need to install the new kernel. there are many ways of doing this, basically, we just need to transfer the generated files to the raspbian's file system, either buy mounting this locally or transferring them to a live pi using scp. We can ease this process by populating the '$ROOTFS' we created earlier with the required files and then creating a tar archive:

 $ cp ${SRC}/arch/arm/boot/Image ${ROOTFS}/boot/kernel.img
 $ cp ${WORKDIR}/firmware-next/boot/{bootcode.bin,fixup.dat,start.elf} ${ROOTFS}/boot
 $ cp -rf ${WORKDIR}/firmware-next/hardfp/opt  ${ROOTFS}
 $ chown root:root -R ${ROOTFS}
 $ cd ${ROOTFS}
 $ tar cjvf ${WORKDIR}/rpi_rootfs.tar.bz2 .

Simply extract the archive into the Raspbian root file system and you should be ready to go.

Some notes:
  • You will need to be root to extract the tar;
  • You may need to remove old modules and kernel manually if you are compiling the same kernel with the same substring;
  • See the elinux page for the 'config.txt' for details of how to add multiple kernel versions to your pi.

And that is it. I found the hardest part of this was getting the right versions and firmware transferred over to the pi, that is, I was constantly finding there was a mismatch between the firmware and the kernel due to the instructions being unclear/outdated.

Following the above instructions will ensure that any additional modules you chose to compile will work with the installed kernel.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.
By submitting this form, you accept the Mollom privacy policy.