The instructions provided below appears to be outdated, are inconsistent and fail for the current version of the reffered repositories.

+

Consider http://www.raspberrypi.org/documentation/linux/kernel/building.md, for which the cross-compilation instructions is confirmed to work.

+

(19 March 2015, tools: 783eb21c, linux: c4ba28133).

+

+

The "[https://github.com/Re4son/re4son-kernel-builder Re4son kernel-builder]" provides a working environment to build the latest kernel packages and can be used as a reference for studying how I cross compile the Re4son-Kernel.

= Overview =

= Overview =

−

This page explains how to rebuild the kernel image for the RPi. There are two possible routes available:

+

This page explains how to rebuild the kernel image for the Raspberry Pi. There are two possible routes available:

# Compile on the Raspberry Pi itself

# Compile on the Raspberry Pi itself

# Cross compile on another Linux system

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

+

Both of these routes are covered below, however, you are strongly recommended to follow the cross-compilation route. The low processing power of the Raspberry Pi means that a local compile will take many hours. A compilation of the latest kernel and modules took about 752 minutes (12h30m)!

+

+

If you want to compile an upstream kernel, rather than the Raspberry Pi Foundation's downstream kernel, please see [[RPi_Upstream_Kernel_Compilation]] for a few tips.

== Example Checklist/Roadmap ==

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

+

This series of steps yielded a successful custom/updated hardfp kernel to a stock Raspbian installation, cross compiled from an AMD 64-bit 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:

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

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

= Get the kernel source =

= Get the kernel source =

−

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!

+

The kernel source should be downloaded from the [https://github.com/raspberrypi/linux Raspberry Pi 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 Raspberry Pi. You can however apply patches from the vanilla kernel to the Raspberry Pi one - be prepared for potential compiler grumbles though!

−

At the time of writing, two branches of interest are available:

+

On Jan 2014, the current is rpi-3.10.y. You can check this and other available versions by browsing [https://github.com/raspberrypi/linux Raspberry Pi Linux section on GitHub]

−

* '''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.

+

−

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

+

You can download the source directly using Git. For the 3.10 branch:

<pre>

<pre>

−

git clone git://github.com/raspberrypi/linux.git

+

git init

+

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

</pre>

</pre>

−

and for the 3.6 branch:

+

And for the other stable code branch, change the numbers in the following to suit:

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]

+

+

Or you can download a tarball from the same website:

= Get a compiler =

= Get a compiler =

Next, you will need to get a version of GCC in order to build the kernel.

Next, you will need to get a version of GCC in order to build the kernel.

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.

+

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 Raspberry Pi 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 ===

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

or you can download a tarball from the website using [https://github.com/raspberrypi/tools/archive/master.tar.gz this link].

+

+

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

=== Custom-built Linaro GCC ===

Line 101:

Line 168:

</pre>

</pre>

−

=== Gentoo Linux ===

+

=== Funtoo or Gentoo Linux ===

<pre>

<pre>

crossdev -S -v -t arm-unknown-linux-gnueabi

crossdev -S -v -t arm-unknown-linux-gnueabi

</pre>

</pre>

−

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.

+

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

Line 113:

Line 180:

</pre>

</pre>

−

== Cross compiling from OSX ==

+

== Cross compiling from OS X ==

=== Macports ===

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

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

+

From opensource.apple.com, download and copy [https://opensource.apple.com/source/dtrace/dtrace-48/sys/elf.h?txt elf.h] and [https://opensource.apple.com/source/dtrace/dtrace-48/sys/elftypes.h?txt elftypes.h] to /usr/include

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

+

Next, in all cases, you will want to get a working kernel configuration to start from. You can get the one running on the Raspberry Pi by typing the following (on the Raspberry Pi):

<pre>

<pre>

zcat /proc/config.gz > .config

zcat /proc/config.gz > .config

</pre>

</pre>

−

then copy <tt>.config</tt> into your build directory.

+

Then copy <tt>.config</tt> into your build directory.

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.

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.

−

From this point on, if you are cross-compiling, please substitute <tt><your_compiler></tt> with your compiler binary prefix (e.g. <tt>arm-bcm2708hardfp-linux-gnueabi-</tt>) as each compiler will be named slightly differently. If you are building on the RPi, remove <tt>ARCH=arm CROSS_COMPILE=<your_compiler></tt> from each command.

+

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 Raspberry Pi, remove <tt>ARCH=arm CROSS_COMPILE=${CCPREFIX}</tt> from each command.

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

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

<pre>

<pre>

−

make ARCH=arm CROSS_COMPILE=<your_compiler> oldconfig

+

make ARCH=arm CROSS_COMPILE=${CCPREFIX} oldconfig

</pre>

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

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.

Line 174:

Line 318:

Optionally, if you want to make changes to the configuration, run this next:

Optionally, if you want to make changes to the configuration, run this next:

<pre>

<pre>

−

make ARCH=arm CROSS_COMPILE=<your_compiler> menuconfig

+

make ARCH=arm CROSS_COMPILE=${CCPREFIX} menuconfig

</pre>

</pre>

−

Now you are ready to build:

+

Now you are ready to build:<br>

+

(On the Raspberry Pi, type 'screen' to open a virtual screen. If you use it you can disconnect from the Raspberry Pi and compile overnight...)

<pre>

<pre>

−

make ARCH=arm CROSS_COMPILE=<your_compiler>

+

make ARCH=arm CROSS_COMPILE=${CCPREFIX}

</pre>

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

+

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 (that is, -j3 for two cores).

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.

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.

−

= Prepare the image file =

+

The modules will be build with the following command.

−

Because of the way the memory addresses are arranged in the Broadcom SoC, you will need to prepare the compiled image for use.

+

−

+

−

If you haven't got the tools directory from the Git repo, please do so now:

+

<pre>

<pre>

−

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

+

make ARCH=arm CROSS_COMPILE=${CCPREFIX} modules

</pre>

</pre>

−

or you can download a tarball from the website using [https://github.com/raspberrypi/tools/archive/master.tar.gz this link].

−

−

In the tools set, there is a folder called <tt>mkimage</tt>. Enter this directory, then run the following:

Copy your new <tt>kernel.img</tt> 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:

+

The fully built kernel will be <tt>arch/arm/boot/Image</tt>. Copy your new kernel file into the Raspberry Pi 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 Raspberry Pi, just copy the file to <tt>/boot</tt>. If you use a different filename, edit <tt>config.txt</tt> change the kernel line:

<pre>

<pre>

kernel=kernel_new.img

kernel=kernel_new.img

Line 208:

Line 342:

</pre>

</pre>

−

Now you need to transfer the modules. In the build directory, run the following (substituting <tt><modules_path></tt> for a folder somewhere (e.g. <tt>~/modules</tt>):

+

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

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.

The contents of this directory, a single <pre>lib</pre> directory, should then be copied into the Raspberry Pi root directory, merging or overwriting <pre>/lib</pre>

+

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.

+

+

NOTE: The lib directory will have symlinks back to the kernel sources (lib/modules/<kernel-version>/source and lib/modules/<kernel-version>/build). If you have limited space on the SD card and don't intend to compile modules on the Raspberry Pi itself, you will probably want to remove those links before you transfer the lib directory. The size difference can be many hundreds of MB.

−

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

+

Your Raspberry Pi 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 =

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

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

+

* '''master''' - This is the version of firmware currently used in Raspbian (that is, 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.

* '''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 either download the source directly using Git:

−

You can download the firmware directly using git. For the master branch:

+

You can download the firmware directly using Git. For the master branch:

Firstly, update the required boot files in the RPi <tt>boot</tt> directory with those you've downloaded. These are:

+

Firstly, update the required boot files in the Raspberry Pi <tt>boot</tt> directory with those you've downloaded. These are:

* bootcode.bin

* bootcode.bin

* fixup.dat

* fixup.dat

* start.elf

* 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 (substituting the program name for your compiler binary as required):

+

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>

<pre>

−

arm-none-linux-gnueabi-gcc -v 2>&1 | grep hard

+

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

</pre>

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

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:

+

Remove the <tt>/opt/vc</tt> directory from the Raspberry Pi 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

+

* For hard float, copy <tt>vc</tt> from the <tt>hardfp/opt</tt> directory into <tt>/opt</tt> in the Raspberry Pi root directory

−

* Otherwise copy <tt>vc</tt> from the top-level <tt>opt</tt> directory into <tt>/opt</tt> in the RPi root directory.

Note: The hard float vs soft float here refers only to the kernel itself, not the functionality it provides. Your applications will still be able to use hard floats. The kernel doesn't use floats anyway, so it is not something to worry about as long as you select the correct <tt>vc</tt> directory to copy.

= Test your build =

= Test your build =

−

Power cycle your RPi and check the following:

+

Power cycle your Raspberry Pi and check the following:

* If you have the serial port on the GPIO expander wired up, you should see the kernel booting.

* 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 screen works - the kernel boots and you get a login prompt.

Line 257:

Line 401:

* Run <tt>uname -a</tt> and check that your new kernel is the one that's running.

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

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

+

+

= Build modules for the running kernel =

+

You need the kernel sources for the currently running kernel to successfully build kernel modules for the Raspberry Pi. More specifically, only parts of the complete source, the so called kernel headers are needed.

+

There are two ways to arrive at a state from which you can build kernel modules on the Raspberry Pi. <br />

+

[[Raspberry_Pi_Kernel_Compilation#1._Using_rpi-source|1.]] is most suitable if you have been using Kernel sources from raspberrypi.org <br />

+

[[Raspberry_Pi_Kernel_Compilation#2._Manually|2.]] is using only mainline kernel stuff

+

+

== 1. Using rpi-source ==

+

[https://github.com/notro/rpi-source/wiki rpi-source] downloads a matching source for the running kernel.

The second line will create the .ko files and it will create the necessary folder structure (/usr/src/linux/lib/modules/`uname -r`/[build|kernel|source]. build and source are symlinks to the kernel sources. kernel contains the module files (under some subdirectories).

+

+

Now you can continue with [[RPi_Upstream_Kernel_Compilation]] compiling the bootloader and so on. Finally also copy the used kernel sources onto the SD card, e.g. under /home/pi/linux

+

$ cp -av /usr/src/linux/ <sdcard-ext4-root>/home/pi/

+

+

Boot the Raspberry Pi, then execute

+

$ cd /home/pi

+

$ sudo chown -R pi:pi linux/

+

to change back ownership to you (this is necessary unless your username on your build machine is also "pi"...). Now copy /home/pi/linux/lib/modules/* to the correct

+

$ sudo cp -R /home/pi/linux/lib/modules/`uname -r`/ /lib/modules/

+

Correct the symlinks:

+

$ sudo cd /lib/modules/`uname -r`/

+

$ sudo rm build source

+

$ ln -s /home/pi/linux build

+

$ ln -s /home/pi/linux source

+

Now, we need to fix one more problem: During the cross-compilation build, a couple scripts were compiled for the host. We also need them for the Raspberry Pi. Still on the Pi, this can be checked with e.g.

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

Important note

The instructions provided below appears to be outdated, are inconsistent and fail for the current version of the reffered repositories.
Consider http://www.raspberrypi.org/documentation/linux/kernel/building.md, for which the cross-compilation instructions is confirmed to work.
(19 March 2015, tools: 783eb21c, linux: c4ba28133).

The "Re4son kernel-builder" provides a working environment to build the latest kernel packages and can be used as a reference for studying how I cross compile the Re4son-Kernel.

Overview

This page explains how to rebuild the kernel image for the Raspberry Pi. There are two possible routes available:

Compile on the Raspberry Pi itself

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 Raspberry Pi means that a local compile will take many hours. A compilation of the latest kernel and modules took about 752 minutes (12h30m)!

If you want to compile an upstream kernel, rather than the Raspberry Pi Foundation's downstream kernel, please see RPi_Upstream_Kernel_Compilation for a few tips.

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 AMD 64-bit 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:

Transfer the following files from the firmware/boot directory to the Raspberry Pi /boot directory:

bootcode.bin

fixup.dat

start.elf

Transfer the firmware/hardfp/opt directory to the Raspberry Pi /opt directory

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 Raspberry Pi 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 Raspberry Pi. You can however apply patches from the vanilla kernel to the Raspberry Pi one - be prepared for potential compiler grumbles though!

2. 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 Raspberry Pi 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.

Custom-built Linaro GCC

Ubuntu

apt-get install gcc-arm-linux-gnueabi make ncurses-dev

Funtoo or 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 OS X

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 the latest versin of Xcode and command line tools are installed from Apple Developer Connection
Install macports

Yagarto

Perform the compilation

Next, in all cases, you will want to get a working kernel configuration to start from. You can get the one running on the Raspberry Pi by typing the following (on the Raspberry Pi):

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 Raspberry Pi, 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:
(On the Raspberry Pi, type 'screen' to open a virtual screen. If you use it you can disconnect from the Raspberry Pi and compile overnight...)

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 (that is, -j3 for two cores).

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.

The modules will be build with the following command.

make ARCH=arm CROSS_COMPILE=${CCPREFIX} modules

Transfer the build

The fully built kernel will be arch/arm/boot/Image. Copy your new kernel file into the Raspberry Pi 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 Raspberry Pi, 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.

directory, should then be copied into the Raspberry Pi root directory, merging or overwriting

/lib

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.

NOTE: The lib directory will have symlinks back to the kernel sources (lib/modules/<kernel-version>/source and lib/modules/<kernel-version>/build). If you have limited space on the SD card and don't intend to compile modules on the Raspberry Pi itself, you will probably want to remove those links before you transfer the lib directory. The size difference can be many hundreds of MB.

Your Raspberry Pi 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 (that is, 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:

Or you can download a tarball from the website using these links: masternext

Transfer the firmware

Firstly, update the required boot files in the Raspberry Pi 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 Raspberry Pi root, then:

For hard float, copy vc from the hardfp/opt directory into /opt in the Raspberry Pi root directory

Otherwise copy vc from the top-level opt directory into /opt in the Raspberry Pi root directory.

Note: The hard float vs soft float here refers only to the kernel itself, not the functionality it provides. Your applications will still be able to use hard floats. The kernel doesn't use floats anyway, so it is not something to worry about as long as you select the correct vc directory to copy.

Test your build

Power cycle your Raspberry Pi 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.

Build modules for the running kernel

You need the kernel sources for the currently running kernel to successfully build kernel modules for the Raspberry Pi. More specifically, only parts of the complete source, the so called kernel headers are needed.
There are two ways to arrive at a state from which you can build kernel modules on the Raspberry Pi. 1. is most suitable if you have been using Kernel sources from raspberrypi.org 2. is using only mainline kernel stuff

1. Using rpi-source

rpi-source downloads a matching source for the running kernel.
It supports rpi-update kernels and Raspian kernels.

The second line will create the .ko files and it will create the necessary folder structure (/usr/src/linux/lib/modules/`uname -r`/[build|kernel|source]. build and source are symlinks to the kernel sources. kernel contains the module files (under some subdirectories).

Now you can continue with RPi_Upstream_Kernel_Compilation compiling the bootloader and so on. Finally also copy the used kernel sources onto the SD card, e.g. under /home/pi/linux

$ cp -av /usr/src/linux/ <sdcard-ext4-root>/home/pi/

Boot the Raspberry Pi, then execute

$ cd /home/pi
$ sudo chown -R pi:pi linux/

to change back ownership to you (this is necessary unless your username on your build machine is also "pi"...). Now copy /home/pi/linux/lib/modules/* to the correct

Now, we need to fix one more problem: During the cross-compilation build, a couple scripts were compiled for the host. We also need them for the Raspberry Pi. Still on the Pi, this can be checked with e.g.