Sunday, October 28, 2012

How to Compile RaspberryPI Kernel

The actual process of compiling Linux Kernel is not really difficult, and it has been documented in hundreds of websites and blogs. However since the raspberrypi cpu's is not really too fast it can take many hours to compile the kernel native in the raspberrypi. Then is essential to setup another computer to perform cross compilation and be able to copy the Image and modules files.

I found two great tutorials here and here, which guide me in the right direction. Since there are many different ways to achieve the same goal. I will documents the steps that worked for me.

In your Linux Desktop

1) First install Git if you don't have it already.
$ sudo apt-get install git

2) Install packages required to cross compilation
$ sudo apt-get install git gcc-arm-linux-gnueabi make ncurses-dev

3) Create a directory to download raspbian kernel in your home directory.

$ cd ~/
$ mkdir raspberrypi
$ cd raspberrypi$ git clone https://github.com/raspberrypi/firmware
$ git clone git://github.com/raspberrypi/linux.git

 -- or to download just the latest --

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

4) Configure kernel
Option 1: Obtain the .config from your existing Raspberrypi Kernel
$ sudo zcat /proc/config.gz > .config

Then, copy this file to your desktop computer raspberrypi/linux directory with scp or by any other way.

Option 2: Use the standard broadcom raspberrypi cutdown .config file.
$ cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
Once you have .config file perform a "oldconfig" this will create a new .config based in the old .config file but with options and selections according to the new kernel file.

$ make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
This will ask you a thousand questions, just press Enter to accept the default configuration. You would be able to make changes in the next step.

Optional - But probably the only good reason to recompile the kernel. Add or modify kernel options and settings.
$ make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig

5) Compile kernel
$ make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k
In you have a multicore processor use the "-j" argument with a number between 1.5 or twice the number of cpu's in your computer.
ej. in my AMD 4 cores I use the following. It just takes around 5 minutes to compile.
 $ make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k -j 6

In your Raspberry PI.

Copy your files to a RaspberryPI. We have many ways to do this, I will describe my favorite.

6) Boot your raspberry - easy !

7) Install sshfs in your RPI, also you must have ssh or openssh-server for debian distros installed and running in your desktop.
 $ sudo apt-get install sshfs

8) Install Modules in your RaspberryPI

$ cd /mnt
$ mkdir raspbian
$ sshfs user@host:raspberrypi raspbian
$ cd raspbian/linux
$ sudo make modules_install

9) Copy new firmware
$ cd /mnt/raspbian/firmware/boot
$  cp * /boot

10) Copy yout custom kernel from Desktop to PI
$ sudo cp arch/arm/boot/Image /boot/kernel.img
11) Edit /boot/config.txt and add your memory split settings
gpu_mem_256=64
gpu_mem_512=128

12) Reboot