Raspberry Pi Kernel Compilation

= Overview = This page explains how to rebuild the kernel image for the RPi. There are two possible routes available:
 * 1) Compile on the Raspberry Pi itself
 * 2) Cross compile on another Linux system

Both of these routes are covered below, however, you are strongly recommended to follow the cross compilation route. The low processing power of the RPi means that a local compile will take many hours.

= Get the kernel source = The kernel source should be downloaded from the RPI linux section on GitHub. Although you could just compile the vanilla kernel from Kernel.org, it will not have the necessary drivers and modules for the Broadcom SoC on the RPi. You can however apply patches from the vanilla kernel to the RPi one - be prepared for potential compiler grumbles though!

At the time of writing, two branches of interest are available:
 * rpi-3.2.27 - This is the version of the kernel currently used in Raspbian.
 * rpi-3.6-y - This is a development branch based on the current vanilla kernel. It will eventually replace the 3.2 branch.

You can download the source directly using git. For the 3.2 branch: git clone git://github.com/raspberrypi/linux.git and for the 3.6 branch: git fetch git://github.com/raspberrypi/linux.git rpi-3.6.y:refs/remotes/origin/rpi-3.6.y git checkout rpi-3.6.y Or you can download a tarball from the website using these links: rpi-3.2.27 rpi-3.6.y

= Get a compiler = Next, you will need to get a version of GCC in order to build the kernel.

Raspbian
apt-get update apt-get -y dist-upgrade apt-get -y install gcc make

Arch Linux
pacman -Syu pacman -S gcc make

Cross compiling from Linux
Please note that when cross-compiling, your compiler may not target the correct ARM processor by default. This will at best reduce performance, or worse, compile for a much newer processor resulting in illegal instructions in your code. The pre-built compiler or a custom-built compiler are recommended because of this. (For example, the latest GCC Linaro binary targets armv7-a by default, whereas the RPi requires armv6kz). It is possible to add extra compiler options to the HOSTCFLAGS line in Makefile. The correct flags are shown on the software page - note that you may also need to add -marm if your compiler produces Thumb code by default.

Use the provided compiler
Download the pre-built bmc2708 compiler from the RPI tools section on GitHub. git clone git://github.com/raspberrypi/tools.git or you can download a tarball from the website using this link.

Custom-built Linaro GCC
See Linaro GCC Compilation.

Ubuntu
apt-get install gcc-arm-linux-gnueabi make ncurses-dev

Gentoo Linux
crossdev -S -v -t arm-unknown-linux-gnueabi

Crossdev should create a cross-toolchain using the latest stable versions of the required packages. If it fails, you can specify exact versions by removing the "-S" flag and adding the "--b", "--g", "--k" and "--l" flags. On 2012-05-06, cross -S -v -A gnueabi arm works just fine.

Arch Linux
yaourt -S arm-linux-gnueabi-gcc

Macports
The Kernel source requires a case-sensitive filesystem. If you do not have a HFS+ Case-sensitive partition that can be used, create a disk image with the appropriate format. Ensure latest Xcode and command line tools are installed from Apple Developer Connection Install macports port install arm-none-eabi-gcc port install arm-none-eabi-binutils

If you get an error message that elf.h is missing sudo port install libelf && sudo ln -s /opt/local/include/libelf /usr/include/libelf From opensource.apple.com, download and copy elf.h and elftypes.h to /usr/include

Edit elf.h and add If you get a "SEGMENT_SIZE is undeclared" error open the Makefile and change the line: NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include) to NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include) -Dlinux
 * 1) define R_386_NONE       0
 * 2) define R_386_32         1
 * 3) define R_386_PC32       2
 * 4) define R_ARM_NONE       0
 * 5) define R_ARM_PC24       1
 * 6) define R_ARM_ABS32      2
 * 7) define R_MIPS_NONE      0
 * 8) define R_MIPS_16        1
 * 9) define R_MIPS_32        2
 * 10) define R_MIPS_REL32     3
 * 11) define R_MIPS_26        4
 * 12) define R_MIPS_HI16      5
 * 13) define R_MIPS_LO16      6

Yagarto
Download and install from here.

= Perform the compilation = Firstly, ensure your build directory is clean: make mrproper

Next, in all cases, you will want to get a working kernel configuration to start from. You can get the one running on the RPi by typing the following (on the RPi): zcat /proc/config.gz > .config then copy .config into your build directory.

Alternatively, the default configuration is available in the downloaded kernel source in arch/arm/configs/bcmrpi_defconfig. Just copy this to .config in the build directory.

From this point on, if you are cross-compiling, please substitute </tt> with your compiler binary prefix (e.g. arm-bcm2708hardfp-linux-gnueabi-</tt>) as each compiler will be named slightly differently. If you are building on the RPi, remove ARCH=arm CROSS_COMPILE=<your_compiler></tt> from each command.

Ensure that your configuration file is up-to-date: make ARCH=arm CROSS_COMPILE=<your_compiler> oldconfig If any configuration options have been added, you will be asked what set each option to. If you don't know the answer, just press enter to accept the default.

Optionally, if you want to make changes to the configuration, run this next: make ARCH=arm CROSS_COMPILE=<your_compiler> menuconfig

Now you are ready to build: make ARCH=arm CROSS_COMPILE=<your_compiler> If you are on a multi-core system, you can make the build faster by appending -j<N></tt> where N</tt> is the number of cores on your system plus one.

Find something else to get on with while the compilation takes place. On an average PC with the default configuration, this should take about 15 minutes.

= Prepare the image file = Because of the way the memory addresses are arranged in the Broadcom SoC, you will need to prepare the compiled image for use.

If you haven't got the tools directory from the Git repo, please do so now: git clone git://github.com/raspberrypi/tools.git or you can download a tarball from the website using this link.

In the tools set, there is a folder called mkimage</tt>. Enter this directory, then run the following: ./imagetool-uncompressed.py <your_kernel_build_directory>/arch/arm/boot/zImage

This will output a file called kernel.img</tt>.

= Transfer the build = Copy your new kernel.img</tt> file into the RPi boot partition, though preferably as a new file (such as kernel_new.img</tt>) just in case it doesn't work. If you're building on the RPi, just copy the file to /boot</tt>. If you use a different filename, edit config.txt</tt> change the kernel line: kernel=kernel_new.img
 * 1) kernel=kernel.img

Now you need to transfer the modules. In the build directory, run the following (substituting <modules_path></tt> for a folder somewhere (e.g. ~/modules</tt>): ARCH=arm CROSS_COMPILE=<your_compiler> make modules_install INSTALL_MOD_PATH=<modules_path>

The contents of this directory should then be copied into the RPi root directory. NOTE: If you have rebuilt the new kernel with exactly the same version as the one that's running, you'll need to remove the old modules first. Ideally this should be done offline by mounting the SD card on another system.

Your RPi should now be ready to boot the new kernel. However, at this point it's recommended that you update your GPU firmware and libraries. This is required if you've just moved from 3.2 to 3.6 as the firmware interface has changed.

= Get the firmware = The firmware and boot files should be updated at the same time to ensure that your new kernel works properly. Again, two branches are available:
 * master - This is the version of firmware currently used in Raspbian (i.e. it works with the 3.2 kernel).
 * next - This is a development branch which provides a newer GPU firmware to work with the updated drivers in the 3.6 kernel.

You can either download the source directly using git: You can download the firmware directly using git. For the master branch: git clone git://github.com/raspberrypi/firmware.git and for the next branch: git fetch git://github.com/raspberrypi/firmware.git next:refs/remotes/origin/next Or you can download a tarball from the website using these links: master next

= Transfer the firmware = Firstly, update the required boot files in the RPi boot</tt> directory with those you've downloaded. These are:
 * bootcode.bin
 * fixup.dat
 * start.elf

Next, you need to copy the VC libraries over. There are two copies of this: one for hard float and one for soft float. To find the correct one, run the following command (substituting the program name for your compiler binary as required): arm-none-linux-gnueabi-gcc -v 2>&1 | grep hard

If something prints out, and you can see --with-float=hard</tt>, you need the hard float ones. NOTE: The current version of Raspbian uses hard float.

Remove the /opt/vc</tt> directory from the RPi root, then:
 * For hard float, copy vc</tt> from the <tt>hardfp/opt</tt> directory into <tt>/opt</tt> in the RPi root directory
 * Otherwise copy <tt>vc</tt> from the top-level <tt>opt</tt> directory into <tt>/opt</tt> in the RPi root directory.

= Test your build = Power cycle your RPi and check the following:
 * If you have the serial port on the GPIO expander wired up, you should see the kernel booting.
 * The screen works - the kernel boots and you get a login prompt.
 * The VC interface is working - if the 'OK' LED flashes regularly eight or so times every few seconds once the OS has booted, it's not. You can also test this by running <tt>vcgencmd measure_temp</tt>. If it prints "VCHI initialization failed", you have the a mismatch between the firmware, the VC libraries, and the kernel driver.
 * Run <tt>uname -a</tt> and check that your new kernel is the one that's running.
 * Make sure you don't have any odd error messages during boot that may indicate a module isn't working properly. If you see <tt>missed completion of cmd 18</tt> regarding DMA transfers to the SD card, you can safely ignore it.