Difference between revisions of "Raspberry Pi Kernel Compilation"

From eLinux.org
Jump to: navigation, search
(Arch Linux)
(Transfer the build: Added in where the kernel actually is. This was taken out after a step that explicitly created kernel.img was removed.)
(39 intermediate revisions by 16 users not shown)
Line 1: Line 1:
 
{{Template:RPi_Software}}
 
{{Template:RPi_Software}}
 +
 
= Overview =
 
= Overview =
First, you are going to get and build the linux kernel and its modules using a suitable compiler (a "cross-compiler" if you aren't building it on the same hardware you will be running it on) and then you are going to create a kernel image from the uncompressed kernel (Image) to place on the sd, along with the modules you build alongside it.
+
This page explains how to rebuild the kernel image for the RPi. There are two possible routes available:
= From the Raspberry pi =
+
# Compile on the Raspberry Pi itself
== Firmware ==
+
# Cross compile on another Linux system
cd /opt
+
 
git clone --depth 1 git://github.com/raspberrypi/firmware.git
+
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.
cd firmware/boot
+
 
cp arm128_start.elf arm192_start.elf arm224_start.elf bootcode.bin loader.bin start.elf /boot/
+
== Example Checklist/Roadmap ==
cd /opt
+
This section serves to hold a new user's hand just a bit more than some of the other more generic information below in the document. To get more information on the steps in the roadmap, search this page for additional details. It assumes you can navigate filesystems, move files across systems, and have a general understanding of compiling linux kernels, filesystems, partitions, and block devices.
rm -r firmware
+
 
== Kernel compilation ==
+
This series of steps yielded a successful custom/updated hardfp kernel to a stock Raspbian installation, cross compiled from an amd64 Debian system without regression on any kernel configuration options or requiring modified boot parameters. Be aware that in the worst case, you may need to overlay a stock set of kernel/modules/firmware on the Raspberry Pi if something fails. If you do not know how to do this, then a reimage of the SD card may be necessary. Assuming this is not an issue for your configuration, continue onward:
=== Debian pre-build===
+
# Get the latest raspberrypi kernel source (git://github.com/raspberrypi/linux.git)
apt-get update
+
# Set an environment variable KERNEL_SRC to point to the location of the source (e.g. KERNEL_SRC=/home/me/linux/)
apt-get -y dist-upgrade
+
# Get the latest raspberrypi compiler (git clone git://github.com/raspberrypi/tools.git)
apt-get -y install git gcc make tmux
+
# Set an environment variable CCPREFIX to point to the location of tools (e.g. CCPREFIX=/home/me/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-)
=== Arch Linux pre-build===
+
# From the kernel clone location, clean the kernel source with "make mrproper"
pacman -Syu
+
# Pull the /proc/config.gz from the running Raspbian installation
pacman -S git gcc make tmux
+
# Prime kernel with the old config by running "ARCH=arm CROSS_COMPILE=${CCPREFIX} make oldconfig"
=== Common ===
+
# Modify the kernel config by either modifying the .config file or using "ARCH=arm CROSS_COMPILE=${CCPREFIX} make menuconfig"
cd /opt
+
# Build the new kernel by using "ARCH=arm CROSS_COMPILE=${CCPREFIX} make"
mkdir raspberrypi
+
# Set an environment variable MODULES_TEMP to point to the location of the source (e.g. MODULES_TEMP=/home/me/modules/)
cd raspberrypi
+
# Set aside the new kernel modules by using "ARCH=arm CROSS_COMPILE=${CCPREFIX} modules_install INSTALL_MOD_PATH=${MODULES_TEMP} make"
git clone --depth 1 git://github.com/raspberrypi/linux.git
+
# From the tools clone location, in the mkimage directory, run "./imagetool-uncompressed.py ${KERNEL_SRC}/arch/arm/boot/zImage"
cd linux
+
# Move the resulting kernel.img to the Raspberry Pi's /boot/ directory
zcat /proc/config.gz > .config
+
# Package up the modules into an archive such that at the top level, the structure looks like this:
#optional#make menuconfig
+
#* ./firmware
tmux new -s make
+
#* ./firmware/brcm
nice make; nice make modules
+
#* ./firmware/edgeport
[Ctrl]+[B],[D]
+
#* ./firmware/emi26
############## … 5 hours later ...
+
#* ...
tmux a -t m
+
#* ./modules
[Ctrl]+[D]
+
#* ./modules/3.6.11+
cp arch/arm/boot/Image /boot/kernel.img
+
#* ./modules/3.6.11+/kernel
make ARCH=arm modules_install INSTALL_MOD_PATH=/
+
#* ./modules/3.6.11+/kernel/lib
cd /opt
+
#* ./modules/3.6.11+/kernel/fs
rm -r raspberrypi
+
#* ...
shutdown -r now;
+
# Move the modules archive to the Raspberry Pi and extract them such that the aforementioned firmware and modules directories overwrite /lib/firmware and /lib/modules
= From a foreign machine =
+
# Get the latest raspberrypi firmware (git://github.com/raspberrypi/firmware.git)
== Firmware ==
+
# Transfer the following files from the firmware/boot directory to the Raspberry pi /boot directory:
cd /opt
+
#* bootcode.bin
git clone git://github.com/raspberrypi/firmware.git
+
#* fixup.dat
cd firmware/boot
+
#* start.elf
scp arm128_start.elf arm192_start.elf arm224_start.elf bootcode.bin loader.bin start.elf <user>@<host>:/boot/
+
# Transfer the firmware/hardfp/opt directory to the Raspberry pi /opt directory
After the first time:
+
# Reboot the Raspberry Pi
cd /opt/firmware
+
The Raspberry Pi should now boot with the newly configured/recompiled kernel.
git pull
+
 
cd boot
+
= Get the kernel source =
scp arm128_start.elf arm192_start.elf arm224_start.elf bootcode.bin loader.bin start.elf <user>@<host>:/boot/
+
The kernel source should be downloaded from the [https://github.com/raspberrypi/linux RPI linux section on GitHub]. Although you could just compile the vanilla kernel from [http://www.kernel.org/ 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!
== Kernel compilation ==
+
 
You can use the "-j" flag to improve compilation time. If you have a dual core machine you can use "-j 3", for a quad core machine you can use "-j 6", and so on.
+
At the time of writing, two branches of interest are available:
=== Ubunt ===
+
* '''rpi-3.2.27''' - This is the version of the kernel currently used in Raspbian, but not exactly the same - Raspbian stock kernel image (the one available from the foundation's website) has a 3.2.27+ version marking. Please see [http://www.raspberrypi.org/phpBB3/viewtopic.php?f=66&t=26616 this post] for more details.
apt-get install git gcc-arm-linux-gnueabi make ncurses-dev
+
* '''rpi-3.6-y''' - This is a development branch based on the current vanilla kernel. It will eventually replace the 3.2 branch. At the time of writing, the exact version is 3.6.11.
cd /opt
+
 
mkdir raspberrypi
+
You can download the source directly using git. For the 3.2 branch:
cd raspberrypi
+
<pre>
git clone git://github.com/raspberrypi/linux.git
+
git clone --depth 1 git://github.com/raspberrypi/linux.git
cd linux
+
git checkout rpi-3.2.27
cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
+
</pre>
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
+
and for the 3.6 branch:
#optional#make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig
+
<pre>
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k
+
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
 +
</pre>
 +
Or you can download a tarball from the website using these links: [https://github.com/raspberrypi/linux/archive/rpi-3.2.27.tar.gz rpi-3.2.27] [https://github.com/raspberrypi/linux/archive/rpi-3.6.y.tar.gz rpi-3.6.y]
 +
 
 +
= Get a compiler =
 +
Next, you will need to get a version of GCC in order to build the kernel.
 +
 
 +
== On the RPi ==
 +
 
 +
=== Raspbian ===
 +
<pre>
 +
apt-get update
 +
apt-get -y dist-upgrade
 +
apt-get -y install gcc make
 +
</pre>
 +
 
 +
=== Arch Linux ===
 +
<pre>
 +
pacman -Syu
 +
pacman -S gcc make
 +
</pre>
 +
 
 +
== 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 <tt>HOSTCFLAGS</tt> line in <tt>Makefile</tt>. The correct flags are shown on the [[RPi_Software#Compiler|software page]] - note that you may also need to add <tt>-marm</tt> if your compiler produces Thumb code by default.
 +
 
 +
=== Use the provided compiler ===
 +
Download the pre-built bmc2708 compiler from the [https://github.com/raspberrypi/tools/tree/master/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi RPI tools section on GitHub].
 +
<pre>
 +
git clone git://github.com/raspberrypi/tools.git
 +
</pre>
 +
or you can download a tarball from the website using [https://github.com/raspberrypi/tools/archive/master.tar.gz this link].
 +
 
 +
=== Custom-built Linaro GCC ===
 +
See [[RPi_Linaro_GCC_Compilation|Linaro GCC Compilation]].
 +
 
 +
=== Ubuntu ===
 +
<pre>
 +
apt-get install gcc-arm-linux-gnueabi make ncurses-dev
 +
</pre>
 +
 
 
=== Gentoo Linux ===
 
=== Gentoo Linux ===
crossdev -S -v -t arm-unknown-linux-gnueabi
+
<pre>
mkdir raspberrypi
+
crossdev -S -v -t arm-unknown-linux-gnueabi
cd raspberrypi
+
</pre>
git clone https://github.com/raspberrypi/linux.git
 
cd linux
 
cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
 
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
 
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k
 
theBuell: on 2012-05-06, cross -S -v -A gnueabi arm works just fine
 
  
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.
+
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, <tt>cross -S -v -A gnueabi arm</tt> works just fine.
  
 
=== Arch Linux ===
 
=== Arch Linux ===
pacman -S git
+
<pre>
yaourt -S arm-linux-gnueabi-gcc
+
yaourt -S arm-linux-gnueabi-gcc
cd /opt
+
</pre>
mkdir raspberrypi
 
cd raspberrypi
 
git clone https://github.com/raspberrypi/linux.git
 
cd linux
 
cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
 
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
 
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k
 
  
=== OS X ===
+
== Cross compiling from OSX ==
 +
=== 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.
 
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 [http://developer.apple.com/downloads Apple Developer Connection]  
+
Ensure latest Xcode and command line tools are installed from [http://developer.apple.com/downloads Apple Developer Connection]
==== Yagarto ====
+
Install [http://guide.macports.org/#installing macports]
Downoad and install an GNU ARM toolchain such as [http://www.yagarto.de/#downloadmac yagarto].
+
<pre>
==== Macports ====
+
port install arm-none-eabi-gcc
[http://guide.macports.org/#installing macports]
+
port install arm-none-eabi-binutils
port install arm-none-eabi-gcc
+
</pre>
port install arm-none-eabi-binutils
+
 
==== Common ====
 
cd /opt
 
mkdir raspberrypi
 
cd raspberrypi
 
git clone git://github.com/raspberrypi/linux.git
 
cd linux
 
cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
 
==== Yagarto ====
 
make ARCH=arm CROSS_COMPILE=/path/to/yagarto/bin/arm-none-eabi- oldconfig
 
make ARCH=arm CROSS_COMPILE=/path/to/yagarto/bin/arm-none-eabi- -k
 
==== Macports ====
 
make ARCH=arm CROSS_COMPILE=/opt/local/bin/arm-none-eabi- oldconfig
 
make ARCH=arm CROSS_COMPILE=/opt/local/bin/arm-none-eabi- -k
 
 
'''If you get an error message that elf.h is missing'''
 
'''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
 
  sudo port install libelf && sudo ln -s /opt/local/include/libelf /usr/include/libelf
copy [http://opensource.apple.com/source/dtrace/dtrace-48/sys/elf.h?txt elf.h] and [http://opensource.apple.com/source/dtrace/dtrace-48/sys/elftypes.h?txt elftypes.h] to /usr/include
+
From opensource.apple.com, download and copy [http://opensource.apple.com/source/dtrace/dtrace-48/sys/elf.h?txt elf.h] and [http://opensource.apple.com/source/dtrace/dtrace-48/sys/elftypes.h?txt elftypes.h] to /usr/include
 +
 
 
Edit elf.h and add
 
Edit elf.h and add
 
  #define R_386_NONE        0
 
  #define R_386_NONE        0
Line 130: Line 147:
 
to
 
to
 
  NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include) -Dlinux
 
  NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include) -Dlinux
== Transferring The Build ==
 
Then you have to transfer this img file to the /boot directory and install the compiled modules. Unfortunately the compiled modules are not in a single place, there are two options of installing them.
 
  
Boot your RaspberryPi and mount the <code>linux</code> directory over the network using sshfs:
+
=== Yagarto ===
cd /mnt
+
Download and install from [http://www.yagarto.de/#downloadmac here].
mkdir linux
+
 
sshfs <user>@<host>:<path/to/linux> linux
+
= Perform the compilation =
cd linux
+
Firstly, ensure your build directory is clean:
make install
+
<pre>
make modules_install
+
make mrproper
If you got "Permission denied" when doing <code>cd linux</code>, try:
+
</pre>
sudo sh -c "cd linux ; make modules_install"
+
 
If that is not an option, you can also install the modules into a temporary folder:
+
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):
mkdir /tmp/modules
+
<pre>
make ARCH=arm modules_install INSTALL_MOD_PATH=/tmp/modules
+
zcat /proc/config.gz > .config
Now you have to copy the contents of that directory to /lib/modules on the SD card.
+
</pre>
scp linux/arch/arm/boot/Image <user>@<host>:/boot/kernel.img
+
then copy <tt>.config</tt> into your build directory.
scp -r /tmp/modules/* <user>@<host>://lib/modules/
+
 
Once you've done those two steps, you are ready to put the SD card in and try booting your new system!
+
Alternatively, the default configuration is available in the downloaded kernel source in <tt>arch/arm/configs/bcmrpi_defconfig</tt>. Just copy this to <tt>.config</tt> in the build directory.
=References=
+
 
<references/>
+
From this point on, if you are cross-compiling, set an environment variable <tt>CCPREFIX</tt> that points to the prefix of your compiler binary as each compiler will be named slightly differently.
 +
<pre>
 +
export CCPREFIX=/path/to/your/compiler/binary/prefix-of-binary-
 +
</pre>
 +
 
 +
If you are building on the RPi, remove <tt>ARCH=arm CROSS_COMPILE=${CCPREFIX}</tt> from each command.
 +
 
 +
Ensure that your configuration file is up-to-date:
 +
<pre>
 +
make ARCH=arm CROSS_COMPILE=${CCPREFIX} oldconfig
 +
</pre>
 +
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:
 +
<pre>
 +
make ARCH=arm CROSS_COMPILE=${CCPREFIX} menuconfig
 +
</pre>
 +
 
 +
Now you are ready to build:
 +
<pre>
 +
make ARCH=arm CROSS_COMPILE=${CCPREFIX}
 +
</pre>
 +
If you are on a multi-core system, you can make the build faster by appending <tt>-j<N></tt> where <tt>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.
 +
 
 +
= Transfer the build =
 +
The fully built kernel will be <tt>arch/arm/boot/Image</tt>. Copy your new kernel file into the RPi boot partition, though preferably as a new file (such as <tt>kernel_new.img</tt>) just in case it doesn't work. If you're building on the RPi, just copy the file to <tt>/boot</tt>. If you use a different filename, edit <tt>config.txt</tt> change the kernel line:
 +
<pre>
 +
kernel=kernel_new.img
 +
#kernel=kernel.img
 +
</pre>
 +
 
 +
Now you need to transfer the modules. Set an environment variable that points to a temporary module path.
 +
<pre>
 +
export MODULES_TEMP=~/modules
 +
</pre>
 +
 
 +
In the build directory, run the following command:
 +
<pre>
 +
make ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=${MODULES_TEMP} modules_install
 +
</pre>
 +
 
 +
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:
 +
<pre>
 +
git clone git://github.com/raspberrypi/firmware.git
 +
</pre>
 +
and for the next branch:
 +
<pre>
 +
git fetch git://github.com/raspberrypi/firmware.git next:refs/remotes/origin/next
 +
</pre>
 +
Or you can download a tarball from the website using these links: [https://github.com/raspberrypi/firmware/archive/master.tar.gz master] [https://github.com/raspberrypi/firmware/archive/next.tar.gz next]
 +
 
 +
= Transfer the firmware =
 +
Firstly, update the required boot files in the RPi <tt>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:
 +
<pre>
 +
${CCPREFIX}gcc -v 2>&1 | grep hard
 +
</pre>
 +
 
 +
If something prints out, and you can see <tt>--with-float=hard</tt>, you need the hard float ones. NOTE: The current version of Raspbian uses hard float.
 +
 
 +
Remove the <tt>/opt/vc</tt> directory from the RPi root, then:
 +
* For hard float, copy <tt>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.
 +
 
 
{{Template:Raspberry Pi}}
 
{{Template:Raspberry Pi}}
 
[[Category:RaspberryPi]]
 
[[Category:RaspberryPi]]

Revision as of 18:18, 6 February 2013

Back to the Hub.


Software & Distributions:

Software - an overview.

Distributions - operating systems and development environments for the Raspberry Pi.

Kernel Compilation - advice on compiling a kernel.

Performance - measures of the Raspberry Pi's performance.

Programming - programming languages that might be used on the Raspberry Pi.

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.

Example Checklist/Roadmap

This section serves to hold a new user's hand just a bit more than some of the other more generic information below in the document. To get more information on the steps in the roadmap, search this page for additional details. It assumes you can navigate filesystems, move files across systems, and have a general understanding of compiling linux kernels, filesystems, partitions, and block devices.

This series of steps yielded a successful custom/updated hardfp kernel to a stock Raspbian installation, cross compiled from an amd64 Debian system without regression on any kernel configuration options or requiring modified boot parameters. Be aware that in the worst case, you may need to overlay a stock set of kernel/modules/firmware on the Raspberry Pi if something fails. If you do not know how to do this, then a reimage of the SD card may be necessary. Assuming this is not an issue for your configuration, continue onward:

  1. Get the latest raspberrypi kernel source (git://github.com/raspberrypi/linux.git)
  2. Set an environment variable KERNEL_SRC to point to the location of the source (e.g. KERNEL_SRC=/home/me/linux/)
  3. Get the latest raspberrypi compiler (git clone git://github.com/raspberrypi/tools.git)
  4. Set an environment variable CCPREFIX to point to the location of tools (e.g. CCPREFIX=/home/me/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-)
  5. From the kernel clone location, clean the kernel source with "make mrproper"
  6. Pull the /proc/config.gz from the running Raspbian installation
  7. Prime kernel with the old config by running "ARCH=arm CROSS_COMPILE=${CCPREFIX} make oldconfig"
  8. Modify the kernel config by either modifying the .config file or using "ARCH=arm CROSS_COMPILE=${CCPREFIX} make menuconfig"
  9. Build the new kernel by using "ARCH=arm CROSS_COMPILE=${CCPREFIX} make"
  10. Set an environment variable MODULES_TEMP to point to the location of the source (e.g. MODULES_TEMP=/home/me/modules/)
  11. Set aside the new kernel modules by using "ARCH=arm CROSS_COMPILE=${CCPREFIX} modules_install INSTALL_MOD_PATH=${MODULES_TEMP} make"
  12. From the tools clone location, in the mkimage directory, run "./imagetool-uncompressed.py ${KERNEL_SRC}/arch/arm/boot/zImage"
  13. Move the resulting kernel.img to the Raspberry Pi's /boot/ directory
  14. Package up the modules into an archive such that at the top level, the structure looks like this:
    • ./firmware
    • ./firmware/brcm
    • ./firmware/edgeport
    • ./firmware/emi26
    • ...
    • ./modules
    • ./modules/3.6.11+
    • ./modules/3.6.11+/kernel
    • ./modules/3.6.11+/kernel/lib
    • ./modules/3.6.11+/kernel/fs
    • ...
  15. Move the modules archive to the Raspberry Pi and extract them such that the aforementioned firmware and modules directories overwrite /lib/firmware and /lib/modules
  16. Get the latest raspberrypi firmware (git://github.com/raspberrypi/firmware.git)
  17. Transfer the following files from the firmware/boot directory to the Raspberry pi /boot directory:
    • bootcode.bin
    • fixup.dat
    • start.elf
  18. Transfer the firmware/hardfp/opt directory to the Raspberry pi /opt directory
  19. Reboot the Raspberry Pi

The Raspberry Pi should now boot with the newly configured/recompiled kernel.

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, but not exactly the same - Raspbian stock kernel image (the one available from the foundation's website) has a 3.2.27+ version marking. Please see this post for more details.
  • rpi-3.6-y - This is a development branch based on the current vanilla kernel. It will eventually replace the 3.2 branch. At the time of writing, the exact version is 3.6.11.

You can download the source directly using git. For the 3.2 branch:

git clone --depth 1 git://github.com/raspberrypi/linux.git
git checkout rpi-3.2.27

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.

On the RPi

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

Cross compiling from OSX

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

#define R_386_NONE        0
#define R_386_32          1
#define R_386_PC32        2
#define R_ARM_NONE        0
#define R_ARM_PC24        1
#define R_ARM_ABS32       2
#define R_MIPS_NONE       0
#define R_MIPS_16         1
#define R_MIPS_32         2
#define R_MIPS_REL32      3
#define R_MIPS_26         4
#define R_MIPS_HI16       5
#define R_MIPS_LO16       6

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

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, set an environment variable CCPREFIX that points to the prefix of your compiler binary as each compiler will be named slightly differently.

export CCPREFIX=/path/to/your/compiler/binary/prefix-of-binary-

If you are building on the RPi, remove ARCH=arm CROSS_COMPILE=${CCPREFIX} from each command.

Ensure that your configuration file is up-to-date:

make ARCH=arm CROSS_COMPILE=${CCPREFIX} 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=${CCPREFIX} menuconfig

Now you are ready to build:

make ARCH=arm CROSS_COMPILE=${CCPREFIX}

If you are on a multi-core system, you can make the build faster by appending -j<N> where N 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.

Transfer the build

The fully built kernel will be arch/arm/boot/Image. Copy your new kernel file into the RPi boot partition, though preferably as a new file (such as kernel_new.img) just in case it doesn't work. If you're building on the RPi, just copy the file to /boot. If you use a different filename, edit config.txt change the kernel line:

kernel=kernel_new.img
#kernel=kernel.img

Now you need to transfer the modules. Set an environment variable that points to a temporary module path.

export MODULES_TEMP=~/modules

In the build directory, run the following command:

make ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=${MODULES_TEMP} modules_install

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 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:

${CCPREFIX}gcc -v 2>&1 | grep hard

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

Remove the /opt/vc directory from the RPi root, then:

  • For hard float, copy vc from the hardfp/opt directory into /opt in the RPi root directory
  • Otherwise copy vc from the top-level opt directory into /opt 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 vcgencmd measure_temp. If it prints "VCHI initialization failed", you have the a mismatch between the firmware, the VC libraries, and the kernel driver.
  • Run uname -a 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 missed completion of cmd 18 regarding DMA transfers to the SD card, you can safely ignore it.