Raspberry Pi Kernel Compilation

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

See below for the various guides to get and compile a suitable kernel for your RPi, and then create a kernel.img according to the steps at the end.

= Raspberry PI kernel compilation =

You can compile the kernel on the board itself, but because of the limited resources it will take a lot of time. Alternatively you can crosscompile the kernel on another machine running Linux, Windows or OS X.

getting the compiler
You will need GIT to clone the kernel source tree from GitHub, compiler (gcc) and GNU Make: pacman -S git gcc make

(NOTE: git might be omitted if you decide to download sources in compressed format; this is far faster)

getting the sources
create a directory where you can work on the raspberry pi software. I called mine "raspberrypi". Then clone the git repository.

mkdir raspberrypi cd raspberrypi git clone https://github.com/raspberrypi/linux.git (NOTE: git might fail due to memory constraints; in this case creation of swap file might help. Be warned - this takes ages! To omit the revision history and reduce the download, you can add "--depth 1" to the end of the git clone command.)

Alternatively, download ZIP or TAR.GZ version of the sources from: https://github.com/raspberrypi/linux/downloads unpack and enter the extracted directory (this is your kernel directory - its sources to be precise)

configuring the kernel
Next, the kernel options are configured. Either copy the cut down Raspberry Pi .config file from the kernel source configs directory: cp arch/arm/configs/bcmrpi_cutdown_defconfig .config

Or alternatively, to use the configuration from a currently running Raspberry Pi image, connect to the target and extract the .config file. Then copy the resultant .config file into the Linux kernel source root directory: zcat /proc/config.gz > .config cp .config 

If needed - manual/additional configuration: make menuconfig

compile the kernel
make

(NOTE: this will take around 6h; You might find GNU Screen useful)

build kernel.img so your RPi can boot from it
Finally you need to build a kernel.img for your Pi to boot from. For this, you need the mkimage tool from the raspberrypi github repository:

git clone https://github.com/raspberrypi/tools

Alternatively, download 'imagetool-uncompressed.py' and its dependencies from (this takes far less time and resources): https://github.com/raspberrypi/tools/tree/master/mkimage

Before you can use this script you need Python v2 to be installed: pacman -S python2

Once all above is set up you should have the following files (checklist):
 * in the kernel folder you compiled a file: linux/arch/arm/boot/Image
 * python2 executable (it should be located by default in /usr/bin/python2)
 * imagetool-uncompressed.py script

If this is a case (you have all the above) convert your kernel image with the script:

python2 imagetool-uncompressed.py path/to/linux/arch/arm/boot/Image

This will create a file called kernel.img. Transfer this file into /boot directory (make sure the existing kernel.img in /boot directory gets replaced).

The last thing is to install kernel modules. To do this navigate to your kernel folder and execute: make modules_install

This will install all compiled modules into /lib/modules and possibly some additional files into /lib/firmware folders.

Reboot your RPi and pray :) reboot

TODO: verify & consolidate

getting the compiler
On Ubuntu Oneiric getting the arm cross compiler can be as easy as: sudo apt-get install gcc-arm-linux-gnueabi make sudo apt-get install git-core  #jhauser14905 -- might as well state the obvious, you need git installed! -- it's git-core on ubuntu. --REW sudo apt-get install ncurses-dev #MatthewEveritt -- Had to install this to use menuconfig.

(TODO: Is this the right one? More packages required? I did this a while ago! TODO: Other distributions?)

getting the sources
create a directory where you can work on the raspberry pi software. I called mine "raspberrypi". Then clone the git repository.

mkdir raspberrypi cd raspberrypi git clone https://github.com/raspberrypi/linux.git cd linux

compiling
Next, the kernel options are configured. Either copy the cut down Raspberry Pi .config file from the kernel source configs directory: cp arch/arm/configs/bcmrpi_cutdown_defconfig .config

Or alternatively, to use the configuration from a currently running Raspberry Pi image, connect to the target and extract the .config file. Then copy the resultant .config file into the Linux kernel source root directory: zcat /proc/config.gz > .config cp .config 

Configure the kernel with the copied .config file by running oldconfig: make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig

If manual/additional configuration of kernel options are needed run menuconfig: make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig

Then build the kernel: make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k

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.

If you get the error messages that arm-linux-gnueabi-gcc cannot be found when running make, run the following command:

sudo apt-get install gcc-arm-linux-gnueabi

getting the compiler
Build the cross toolchain: crossdev -S -v -t arm-unknown-linux-gnueabi

theBuell: on 2012-05-06, cross -S -v -A gnueabi arm works just fine

This command 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. For the exact usage refer to the crossdev manpage. A good starting point for figuring out the right versions are those which are stable for the arm architecture.

getting the sources
create a directory where you can work on the raspberry pi software. I called mine "raspberrypi". Then clone the git repository.

mkdir raspberrypi cd raspberrypi git clone https://github.com/raspberrypi/linux.git cd linux

compiling
Next you have to configure the kernel: cp arch/arm/configs/bcmrpi_cutdown_defconfig .config make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig

Then building the kernel: make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k

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.

getting the compiler
You will need GIT to clone the kernel source tree from GitHub: pacman -S git

Build the cross toolchain: arm-linux-gnueabi-gcc is on the AUR. If you use yaourt:

yaourt -S arm-linux-gnueabi-gcc

Yaourt is recommended as it will build all dependencies.

getting the sources
create a directory where you can work on the raspberry pi software. I called mine "raspberrypi". Then clone the git repository.

mkdir raspberrypi cd raspberrypi git clone https://github.com/raspberrypi/linux.git cd linux

compiling
Next you have to configure the kernel: cp arch/arm/configs/bcmrpi_cutdown_defconfig .config make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig

Then building the kernel: make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k

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.

Windows
TODO

getting the compiler
Ensure latest Xcode and command line tools are installed from Apple Developer Connection then Downoad and install an GNU ARM toolchain such as yagarto

Another option is the MacPorts arm-none-eabi-*:

ARM gcc:

port install arm-none-eabi-gcc

binutils:

port install arm-none-eabi-binutils

getting the sources
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 then clone the repository into the mounted image.

Otherwise, create a directory where you can work on the raspberry pi software. I called mine "raspberrypi". Then clone the git repository.

mkdir raspberrypi cd raspberrypi git clone https://github.com/raspberrypi/linux.git cd linux

compiling
Next you have to configure the kernel: (the running kernel config can be found in  on your RPi) cp arch/arm/configs/bcmrpi_cutdown_defconfig .config make ARCH=arm CROSS_COMPILE=/path/to/yagarto/bin/arm-none-eabi- oldconfig

or if you used the MacPorts make ARCH=arm CROSS_COMPILE=/opt/local/bin/arm-none-eabi- oldconfig

Then building the kernel: make ARCH=arm CROSS_COMPILE=/path/to/yagarto/bin/arm-none-eabi- -k

or if you used the MacPorts make ARCH=arm CROSS_COMPILE=/opt/local/bin/arm-none-eabi- -k

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. (Don't use these for the oldconfig option because it messes up the input and output).

If you get an error message that elf.h is missing

install macports install libelf and symlink to /usr/libelf: sudo port install libelf && sudo ln -s /opt/local/include/libelf /usr/include/libelf copy elf.h and elftypes.h to /usr/include

Edit elf.h and add
 * 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

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

= Final step: Making the 'kernel.img' for your Pi =

Finally you need to build a kernel.img for your Pi to boot from. The next two sections describe how to do this, depending on which firmware/bootloader version you're using.

Image Generation For Latest Firmware
With the latest firmware (available from https://github.com/raspberrypi/firmware), you no longer need to create an explicit kernel image; you can directly use Image or zImage from the kernel build process (under 'arch/arm/boot/' of the linux directory) as /boot/kernel.img.

cp linux/arch/arm/boot/Image /media//kernel.img

If you just copied the "Image" file to "kernel.img" in the boot partition of your SD card and the Raspberry Pi doesn't boot (black screen, only red powerled on), then this might help:
 * get the most recent firmware from github:

git clone https://github.com/raspberrypi/firmware/

cd firmware/boot cp arm128_start.elf arm192_start.elf arm224_start.elf bootcode.bin loader.bin start.elf /media//
 * copy the files from firmware/boot/ to the boot partition of your SD card

Image Generation For Older Firmware
For this, you need the mkimage tool from the raspberrypi github repository:

git clone https://github.com/raspberrypi/tools

In tools/mkimage, you'll find a python script called 'imagetool-uncompressed.py':

usage : imagetool-uncompressed.py

After building your linux kernel, you'll find the kernel image you require in 'arch/arm/boot/Image' of the linux directory. Convert your kernel image with the script:

python imagetool-uncompressed.py path/to/linux/arch/arm/boot/Image

= Transferring The Image To The Raspberry Pi =

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  directory over the network using sshfs: cd /mnt mkdir linux sshfs @ : linux cd linux make modules_install

If you got "Permission denied" when doing, try: sudo sh -c "cd linux ; make modules_install"

If that is not an option, you can also install the modules into a temporary folder: mkdir /tmp/modules make ARCH=arm modules_install INSTALL_MOD_PATH=/tmp/modules

Now you have to copy the contents of that directory to /lib/modules on the SD card.

Once you've done those two steps, you are ready to put the SD card in and try booting your new system!