Difference between revisions of "RPi Upstream Kernel Compilation"

From eLinux.org
Jump to: navigation, search
m (Typo fixed)
(12 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
{{Template:RPi_Software}}
 +
 +
== 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 Checklist/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.
 
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.)
+
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), though a standard one will work nicely. Not all USB devices will work (it may depend on the type of transfer, kernel support, interrupt/control/bulk, the device uses, etc.), though the bulk of general use (Wifi, network, storage) will work nicely.
 +
 
 +
If something in this page doesn't make sense to you, pay a visit to [[RPi_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 successful 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 <tt>ARCH</tt> and <tt>CROSS_COMPILE</tt> 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 <tt>make clean</tt> 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 [http://lists.infradead.org/pipermail/linux-rpi-kernel/2014-September/000960.html 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 <tt>Device Drivers  --->  USB support  ---> DesignWare USB2 DRD Core Support</tt> and check both <tt>Host only mode</tt> '''and''' <tt>DWC2 Platform</tt>.
 +
 
 +
Leave the menuconfig, save the new <tt>.config</tt> and then build the kernel:
 +
 
 +
$ ARCH=arm CROSS_COMPILE=${CCPREFIX} chrt -i 0 make -j `grep -c ^processor /proc/cpuinfo`
 +
 
 +
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_config
 +
$ ARCH=arm CROSS_COMPILE=${CCPREFIX} chrt -i 0 make -j `grep -c ^processor /proc/cpuinfo`
 +
 
 +
Right, leave it alone. And now, let's setup the cmdline boot.
 +
 
 +
$ cd tools
 +
 
 +
And create the file <tt>boot.scr</tt> with the following content for Model B:
 +
 
 +
mmc dev 0
 +
setenv fdtfile bcm2835-rpi-b.dtb
 +
setenv bootargs earlyprintk console=tty0 console=ttyAMA0 root=/dev/mmcblk0p2 rootwait
 +
fatload mmc 0:1 ${kernel_addr_r} zImage
 +
fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}
 +
bootz ${kernel_addr_r} - ${fdt_addr_r}
 +
 
 +
Use the following for Model B+
 +
 
 +
mmc dev 0
 +
setenv fdtfile bcm2835-rpi-b-plus.dtb
 +
setenv bootargs earlyprintk console=tty0 console=ttyAMA0 root=/dev/mmcblk0p2 rootwait
 +
fatload mmc 0:1 ${kernel_addr_r} zImage
 +
fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}
 +
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 <tt>/media/boot</tt>.
 +
 
 +
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
  
This page is kept deliberately terse, so as not to duplicate [[RPi_Kernel_Compilation]], which describes how to compile the Raspberry Pi Foundation's downstream kernel. Read that page for some background if this page doesn't make sense to you.
+
That's right, keep it empty. And now, let's populate with the fresh stuff.
  
Get the mainline source:
+
Fetch start.elf and bootcode.bin from https://github.com/raspberrypi/firmware/tree/master/boot and save it to your <tt>$SD</tt>
  
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+
And finally - for Model B:
cd linux
 
  
Compile:
+
$ cd /usr/src
 +
$ cp u-boot/u-boot.bin $SD/kernel.img
 +
$ cp linux/arch/arm/boot/zImage linux/arch/arm/boot/dts/bcm2835-rpi-b.dtb u-boot/tools/boot.scr.uimg $SD
  
export CROSS_COMPILE=/path/to/cross_compiler-
+
for Model B+
make ARCH=arm bcm2835_defconfig
 
make -s ARCH=arm zImage -j8 -s && make -s ARCH=arm dtbs
 
  
Copy to SD card: Copy <tt>arch/arm/boot/zImage</tt> (renamed to <tt>kernel.img</tt>) and <tt>arch/arm/boot/dts/bcm2835-rpi-b.dtb</tt>.
+
$ cd /usr/src
 +
$ cp u-boot/u-boot.bin media/boot/kernel.img
 +
$ cp linux/arch/arm/boot/zImage $SD
 +
$ cp linux/arch/arm/boot/dts/bcm2835-rpi-b-plus.dtb $SD
 +
$ cp u-boot/tools/boot.scr.uimg $SD
  
The file <tt>config.txt</tt> is not needed when using U-Boot. I suggest deleting it, or rather, renaming it to disable it, but still keep it around if you need to go back.
 
  
You'll likely want to use upstream U-Boot to boot the upstream kernel. See [[RPi_U-Boot]]. U-Boot has known-working support for Device Tree. I'm not sure what state the binary firmware has for DT.
+
And you are done. Good luck!
  
 +
{{Template:Raspberry Pi}}
 
[[Category:RaspberryPi]]
 
[[Category:RaspberryPi]]
 
[[Category:Linux-kernel]]
 
[[Category:Linux-kernel]]

Revision as of 09:16, 30 November 2015

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 Checklist/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), though a standard one will work nicely. Not all USB devices will work (it may depend on the type of transfer, kernel support, interrupt/control/bulk, the device uses, etc.), though the bulk of general use (Wifi, network, storage) will work nicely.

If something in this page doesn't make sense to you, pay a visit to RPi_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 successful 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 `grep -c ^processor /proc/cpuinfo`

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_config
$ ARCH=arm CROSS_COMPILE=${CCPREFIX} chrt -i 0 make -j `grep -c ^processor /proc/cpuinfo`

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

$ cd tools

And create the file boot.scr with the following content for Model B:

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

Use the following for Model B+

mmc dev 0
setenv fdtfile bcm2835-rpi-b-plus.dtb
setenv bootargs earlyprintk console=tty0 console=ttyAMA0 root=/dev/mmcblk0p2 rootwait
fatload mmc 0:1 ${kernel_addr_r} zImage
fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}
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 start.elf and bootcode.bin from https://github.com/raspberrypi/firmware/tree/master/boot and save it to your $SD

And finally - for Model B:

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

for Model B+

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


And you are done. Good luck!