Recompile Raspberry Pi Kernel

To speed up the process, we will use vmWare Player or Virtual Box to create an environment for the kernel compilation.

I used ubuntu-12.04.4-alternate-i386.iso as the kernel is functinally similar to the Raspberry Pi Kernel within Raspian.

create the image with 1 processor & 4 cores.

 

Prep Work

Install Dependencies

sudo apt-get install git-core gcc-4.6-arm-linux-gnueabi git libncurses5 libncurses5-dev qt4-dev-tools build-essential gcc-arm-linux-gnueabihf
sudo ln -s /usr/bin/arm-linux-gnueabi-gcc-4.6 /usr/bin/arm-linux-gnueabi-gcc

 

Create a root folder for this change which we will work in the rest of this tutorial

mkdir ~/raspberrypi
cd ~/raspberrypi
git clone https://github.com/raspberrypi/linux.git
git clone https://github.com/raspberrypi/tools.git
mkdir kernel
mkdir modules # Go into the linux folder which was created by git clone linux.git
cd linux/ # Check that the image is OK.
make mrproper

 

Configuration and Compiling

Within the linux folder type (this will make a default configuration for the raspberry pi)

make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- bcmrpi_defconfig

 

With tthe menuconfig command, we can modify that configuration and add custom features:

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

 

And finally we are able to compile it with the -k command, j5 is the reason you would want to create the virtual image with 4 cores as it will compile alot faster than doing this on the raspberry pi itself. General rule is that you take the amount of cores you have + 1. = j5 :)

make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k -j5

 

Create the modules

make modules_install ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- INSTALL_MOD_PATH=../modules/

 

Go into the tools/mkimage folder which was created by git clone tools.git

cd ../tools/mkimage/
./imagetool-uncompressed.py ../../linux/arch/arm/boot/Image

 

Download the latest firmware for the raspberry pi.

wget https://github.com/raspberrypi/firmware/archive/next.tar.gz
tar -zxvf next.tar.gz

 

Check that your sdcard is mounted with the mount command mount

 

mine was auto mounted as i had choosen the desktop image of ubuntu. and had mountet into the /media folder as two partitions, yours will be different:

  • /media/root/
  • /media/data/

 

create a backup location within your home folder, to ensure that you can go back if something goes wrong and backup all the files we will :

# Create Backup folders (for precaution, you could skip this part)
mkdir ~/backup_original 
mkdir ~/backup_original/boot_partition/
mkdir ~/backup_original/data_partition/
mkdir ~/backup_original/data_partition/lib

# Backup The Parts you will replace within the sdcard
sudo mv -f /media/boot/bootcode.bin ~/backup_original/boot_partition/
sudo mv -f /media/boot/start.elf ~/backup_original/boot_partition/
sudo mv -f /media/boot/kernel.img ~/backup_original/boot_partition/
sudo mv -f /media/data/opt/vc/ ~/backup_original/data_partition/opt/vc

# Backup modules and firmware folders
cd /media/data/lib/
sudo tar -cvf ~/backup_original/data_partition/lib/modules.tar modules
sudo tar -cvf ~/backup_original/data_partition/lib/firmware.tar firmware

# Remove the modules folder and firmware from /media/data/lib/
sudo rm -rf modules
sudo rm -rf firmware

 

# add the new kernel.img, bootcode.bin & start.elf to the boot partition of the sdcard.
sudo cp -f ~/raspberrypi/tools/mkimage/kernel.img /media/boot/kernel.img
sudo cp -f ~/firmware-next/boot/bootcode.bin /media/boot/bootcode.bin
sudo cp -f ~/firmware-next/boot/start.elf /media/boot/start.elf

# add the new modules, firmware & vc to the data partition of the sdcard.
sudo cp -af ~/raspberrypi/modules/lib/firmware /media/data/lib/
sudo cp -af ~/raspberrypi/modules/lib/modules /media/data/lib/
sudo cp -af ~/raspberrypi/firmware-next/hardfp/opt/vc/ /media/data/opt/vc

sync

 

Done!

 

Posted on: Tuesday 27 January 2015