RPi Upstream Kernel Compilation

From eLinux.org
Jump to: navigation, search

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.

Building an Upstream Kernel for you Raspberry Pi

First word of advice: Have you visited the RPi_Kernel_Compilation page? If you did not, read at least the Roadmap section.

This page describes how to compile an upstream or mainline kernel for the Raspberry Pi. It is mainly useful for upstream kernel maintainers/developers, or distribution kernel package maintainers.

As of 20140211, all of UART (serial), SD card, HDMI (via simple-framebuffer), and USB should work. Not all SD cards will work (it may depend on the transfer modes the card supports, and timing tolerances). Not all USB devices will work (it may depend on the type of transfer, interrupt/control/bulk, the device uses, etc.)

If something in this page doesn't make sense to you, pay a visit to RPi_Kernel_Compilation, RPi_Upstream_Kernel_Compilation and RPi_U-Boot articles.

As a quickstart, this procedure will build a monolithic kernel, without modules, and good for a start. Build a sucessful image first, and then start your adventures with your crafted kernel.

Preparing the environment

First of all, get the toolchain for cross compile your kernel:

$ cd /usr/src
$ git clone git://github.com/raspberrypi/tools.git --depth=1

Also, fetch Stephen Warren's u-boot branch (we will need it, because the default bootloader won't be of help):

$ git clone git://github.com/swarren/u-boot.git --depth=1

And now, get the mainline source:

$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git --depth=1

Define the following environment variable:

$ export KERNEL_SRC=/usr/src/linux
$ export CCPREFIX=/usr/src/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-
$ export MODULES_TEMP=/usr/src/modules

Building your kernel

Pro tip: Don't forget to prefix the make commands with the ARCH and CROSS_COMPILE prefixes. If you forget to use it and start a make command, it will probably result on a build failure. If you forgot and ran a make, do a make clean and start over. And now, let's prepare the Linux build recipe:

$ cd linux
$ ARCH=arm CROSS_COMPILE=${CCPREFIX} make bcm2835_defconfig

If you are building a Linux version which does not contains this patch (which is true at least up to 3.17), then you will have to manually select the USB controller, otherwise the USB ports (and networking!) will not work. Here is how to fix it. Enter the menuconfig

$ ARCH=arm CROSS_COMPILE=${CCPREFIX} make menuconfig

And then navigate to Device Drivers ---> USB support ---> DesignWare USB2 DRD Core Support and check both Host only mode and DWC2 Platform.

Leave the menuconfig, save the new .config and then build the kernel:

$ ARCH=arm CROSS_COMPILE=${CCPREFIX} chrt -i 0 make -j 8

Your build was sucessful? Great. Leve it alone for a while.

Building your bootloader

As of 20140918, a upstream kernel image can't be booted using the default bootloader. And that's why we need the Stephen Warren's das u-Boot. Let's build it:

$ cd ../u-boot
$ git checkout -b rpi_dev origin/rpi_dev
$ ARCH=arm CROSS_COMPILE=${CCPREFIX} chrt -i 0 make rpi_b_config
$ ARCH=arm CROSS_COMPILE=${CCPREFIX} chrt -i 0 make -j 8

Right, leave it alone. And now, let's setup the cmdline boot.

$ cd tools

And create the file boot.scr with the following content:

mmc dev 0
fatload mmc 0:1 ${kernel_addr_r} zImage
fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}
setenv fdtfile bcm2835-rpi-b.dtb
setenv bootargs earlyprintk console=tty0 console=ttyAMA0 root=/dev/mmcblk0p2 rootwait
bootz ${kernel_addr_r} - ${fdt_addr_r}

Pro tip: I've found that u-boot doesn't play nicely with long cmdlines. Avoid cluttering it. Otherwise it will print a quick setenv failure and your boot will fail.

And now, compile your cmdline:

$ ./mkimage -A arm -O linux -T script -C none -n boot.scr -d boot.scr boot.scr.uimg

Putting it all together

And now, it's time to set up your SD card. This procedure assumes that your SD card is at /media/boot.

First, let's save the current content to a backup directory (and empty the root directory):

$ export SD=/media/boot
$ mkdir $SD/backup
$ mv $SD/* $SD/backup

That's right, keep it empty. And now, let's populate with the fresh stuff.

Fetch a new firmware set from https://github.com/raspberrypi/firmware/tree/master/boot and save it to your $SD

And finally:

$ cd /usr/src
$ cp u-boot/u-boot.bin $SD/kernel.img
$ cp kernel/arch/arm/boot/zImage kernel/arch/arm/boot/dts/bcm2835-rpi-b.dtb u-boot/tools/boot.scr.uimg $SD

And you are done. Good luck!