Cross compiling kernel for Raspberry Pi on Fedora 17 – Part 1

The Raspberry Pi is very limited in CPU and memory and doing computation heavy tasks like compiling the entire kernel on it would take a lot of time. In order to make this process faster a cross compiler is required.
There are multiple ways to install a cross compiler on your Fedora 17 powered development workstation, one of the simplest is just to install the gcc-arm-linux-gnu from the repository using the yum package manager.

yum -y install gcc-arm-linux-gnu
arm-linux-gnu-gcc --version
arm-linux-gnu-gcc (GCC) 4.7.1 20120606 (Red Hat 4.7.1-0.1.20120606)

Get kernel source code
Kernel sources can be obtained from Raspberry PI git repository. If you like experimenting then you can download (or clone with appropriate git commands) the master version of kernel which contains the latest codebase. If you are a bit less adventurous then you can download an older version of kernel from branch section that are considered more stable.

mkdir ~/raspberrypi   
cd ~/raspberrypi
wget -O raspberrypi-linux-3.2.27.tar.gz https://github.com/raspberrypi/linux/tarball/rpi-3.2.27
tar xzf raspberrypi-linux-3.2.27.tar.gz
mv raspberrypi-linux-965b922 kernel-3.2.27

Configure the kernel
Setting up a proper kernel configuration file from scratch is a very challenging task, therefore it is worth to download current config file (.config) from your current Raspberry Pi and us it as a base for kernel compilation. In order to do this execute the following command on the Raspberry Pi:

zcat /proc/config.gz  > /tmp/.config

Copy the .config file to your development workstation with scp.

scp pi@rpi:/tmp/.config ~/raspberrypi/kernel-3.2.27

If you prefer to use a default config instead of the above described one then you can pick one from the available configs in the kernel source tree. The Raspberry Pi configs are prefixed with “bcmrpi_”.

cd ~/raspberrypi/kernel-3.2.27
cp arch/arm/configs/bcmrpi_defconfig .config

It is usually a good idea to execute oldconfig before the menuconfig since the oldconfig will prompt only the newly introduced kernel configuration options and if you execute the menuconfig then maybe some important newly introduced kernel options will remain unnoticed.

make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnu- oldconfig
  HOSTCC  scripts/basic/fixdep
  #For the sake of clarity rest of the lines are removed...
*
* CPU Frequency scaling
*
CPU Frequency scaling (CPU_FREQ) [N/y/?] (NEW) 

To have the nice menu based kernel configuse the menuconfig:

make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnu- menuconfig

In the “General Setup” menu of the Kernel Configuration (menuconfig) you can set up a custom version string that will be appended to kernel version. Change the “Local version – append to kernel relase” to “-tutorial”. In older kernels where this menu item does not exist the following command will have the same effect

sed -i 's/EXTRAVERSION =.*/EXTRAVERSION = Tutorial/' Makefile

Compile kernel
To start compiling and create a the architecture specific kernel image:

make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnu-
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnu- modules

Transfer kernel to Raspberry Pi
The transfer is described on RPi_Kernel_Compilation site. Make sure that the sshfs is installed on the Raspberry (you can use also NFS if you wish) and the firewall on your workstation allows the connection to it. For the sake of clarity I copy the code here

sudo bash #Execute everything as root on Raspberry Pi
apt-get install sshfs
cd /mnt
mkdir raspberrypi
sshfs user@host:/home/user/raspberrypi raspberrypi
cd raspberrypi/kernel-3.2.27
make modules_install
cp arch/arm/boot/Image /boot/kernel.img

After reboot you will see that the newly complied (3.2.27-tutorial) kernel is running

pi@raspberrypi ~ $ uname -a
Linux raspberrypi 3.2.27-tutorial #1 PREEMPT Mon Sep 24 22:01:27 CEST 2012 armv6l GNU/Linux

There is a problem with gcc-arm-linux-gnu utilities. If you check the description of the package (sudo yum info gcc-arm-linux-gnu) you will see that it does not support cross-building of user space programs. If you would like to have a cross compiler that can be used for compiling user space code then build frameworks like OpenEmbedded or crosstool-ng might be the right tools for you. In the Part 2 I will show how to set up a full featured cross-compile environment on Fedora 17.

Advertisements