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.

Compiling on the Raspberry pi itself
TODO: write the rest of this section.

getting the compiler
On Ubuntu Oneiric getting the arm cross compiler can be as easy as:

sudo apt-get install gcc-4.6-arm-linux-gnueabi sudo apt-get install git  #jhauser14905 -- might as well state the obvious, you need git installed!

(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

jhauser14905: on 2012-01-28, with all package updates applied, i had to add the following symlink in order to get the make commands to work. otherwise they would error out

sudo ln -s /usr/bin/arm-linux-gnueabi-gcc-4.6 /usr/bin/arm-linux-gnueabi-gcc

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.

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

sudo ln -s /usr/bin/arm-linux-gnueabi-gcc-4.6 /usr/bin/arm-linux-gnueabi-gcc

this creates a symbolic link to the 4.6 gcc binary

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

port install arm-none-eabi-binutils

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

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

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

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

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 mount @ : 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!