How to update or customise the raspberry pi Linux kernel

This content has 6 years. Please, read this page keeping its age in your mind.

It is some times necessary to customise the Linux Kernel to add or remove specific support in terms of hardware compatibility. For instance, I wanted to experiment with the popular PEAK USB adapter. In that case, there is the SocketCAN Linux driver which is embedded in Kernel since version 3.6.  To enable this option in raspberry pi boards with need to customise the kernel.

Let’s start.  If we assume that we use a laptop or VM with Ubuntu amd64 architecture, we should first install the prerequisites for the cross-compilation and the custom kernel configuration tool.

sudo apt-get install libncurses5-dev

and in other tutorials they refer to:

If its 64-bit machine :

sudo apt-get install lib32z1 lib32ncurses5 lib32stdc++6 lib32z1 lib32z1-dev

Then we need to fetch the latest kernel and the tools from github:

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

It is convenient to have both Linux and tools in the same folder.

After that, we update the $PATH environment variable to make the system aware of file locations needed for cross-compilation.

echo PATH=\$PATH:~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin >> ~/.bashrc
source ~/.bashrc

Now let’s make the kernel (for raspberry pi 3) with the default config options:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig

After that we can use the menuconfig to further customise the kernel be executing:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig

So for example if we want to enable PEAK CAN USB adapter, we need to edit the following in the tree and then overwrite the .config we created in the previous step.

[*] Networking support  --->
   <*>   CAN bus subsystem support  --->
            CAN Device Drivers  --->
               CAN USB interfaces  --->
                   PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD

Now, its time for action, let’s compile:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs

depending on the cores that you have you can add -jn (n:processors number) to increase the speed.

After that it is wise to install the updated kernel directly to the SD card that contains your system. Use lsblk to identify your device:

mkdir mnt
mkdir mnt/fat32
mkdir mnt/ext4
sudo mount /dev/sdb1 mnt/fat32
sudo mount /dev/sdb2 mnt/ext4

It is convenient to do the mounting in the same terminal
After that we need to install the modules:

sudo make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=mnt/ext4 modules_install

and copy the new files required in the SD card

sudo cp arch/arm/boot/zImage mnt/fat32/$KERNEL.img
sudo cp arch/arm/boot/dts/*.dtb mnt/fat32/
sudo cp arch/arm/boot/dts/overlays/*.dtb* mnt/fat32/overlays/
sudo cp arch/arm/boot/dts/overlays/README mnt/fat32/overlays/
sudo umount mnt/fat32
sudo umount mnt/ext4

Now everything is ready for boot!

Useful references: