Home » Linux Kernel » Kernel Booting and Porting » Cross Compilation of Linux kernel for Raspberry Pi 3 and Load It

Cross Compilation of Linux kernel for Raspberry Pi 3 and Load It

For this post, we assume that you already have a working root filesystem as we did in “Building kernel and filesystem using Yocto for Raspberry Pi / Creating Embedded Linux for RPi using Yocto” since we just want to showcase how to manually compile kernel for RPi.

Download Linux kernel source code from raspberry pi git

 $ mkdir -p /home/myuser/rpi 
 $ cd /home/myuser/rpi 
 $ git clone git://github.com/raspberrypi/linux.git --depth=1 

This will download the default HEAD branch from the remote repository.

$ cd linux 
$ git branch
      * rpi-4.9.y

downloading toolchain

$ cd /home/myuser/rpi 
 $ git clone https://github.com/raspberrypi/tools --depth 1 
 $ cd tools
$ git branch
* master

export toolchain path

 $ export PATH=$PATH:/home/myuser/rpi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin 

Configuring Linux Kernel

 $ cd linux 

default configuration file for RPi3 is arch/arm/configs/bcm2709_defconfig

 $ make ARCH=arm CROSS_COMPILE=arm-bcm2708-linux-gnueabi- bcm2709_defconfig 

Above command will do the default configuration of kernel for compilation for RPi

Now, we will compile the kernel to create kernel binary using below command,

 $ make ARCH=arm CROSS_COMPILE=arm-bcm2708-linux-gnueabi- zImage modules dtbs 

This command will create, kernel zImage at arch/arm/boot/zImage

$ file arch/arm/boot/zImage
arch/arm/boot/zImage: Linux kernel ARM boot executable zImage (little-endian)

Now, we have to copy this image into first partition of the SD card by using standard name as “kernel7.img” as,

 $ sudo cp -r arch/arm/boot/zImage /media/myuser/raspberrypi/kernel7.img 

Assuming that first partition of sdcard is mounted at /media/myuser/raspberrypi/ ( you can check same using df -h command )

Now, put unmount this SD card and put to RPi and power ON RPi,

You can check now the booted kernel is what we manually compiled as,

root@raspberrypi3:~# cat /proc/version 
Linux version 4.9.45-v7+ (myuser@myhost) (gcc version 4.7.1 20120402 (prerelease) (crosstool-NG 1.15.2) ) 
#1 SMP Mon Aug 28 19:52:58 IST 2017

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

1 thought on “Cross Compilation of Linux kernel for Raspberry Pi 3 and Load It”

Leave a Comment