Build the Linux Kernel for a Raspberry Pi 3 Model B+

Updated: 4 minute read

Introduction

This is the fourth post in a series of posts (in a tutorial) about building an embedded Linux system for a Raspberry Pi 3 Model B+. A summary post for the tutorial can be found here. The summary post also talks about …

  • what is required to follow along,
  • the <project/root/dir>,
  • some terms that will be used throughout the series,
  • a link to an article that explains how to prepare the SD card,
  • some hints on how to use the UART interface of the Raspberry Pi.

After Building a (Cross) Toolchain, Building U-Boot, and Running U-Boot on a Raspberry Pi 3 Model B+, it’s time to build a Linux kernel for the Raspberry Pi.

Anyways, let’s get to it.

Building the Kernel

First of all, we have to chose a kernel source. There are (at least) three options to chose from:

  • Use the latest and greatest version of the kernel, that is managed by Linus Torvalds himself. Even though, we are not going to use this one, the repo could be cloned by running:

    git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
    
  • Use the latest stable version that is maintained by Greg Kroah-Hartman. We are not going to use this version either, but in case you want to, you could get it by running:

    git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
    
  • Use a vendor specific kernel. There is one for the Raspberry Pi. The corresponding repo can be found here [1].

According to the book “Mastering Embedded Linux Programming” [2], the Raspberry Pi is very well supported in the mainline kernel, but the authors of the book still used the fork of the kernel that is maintained by the Raspberry Pi Foundation. The reasoning is, that this fork is more stable and more actively maintained, at least at the time when the book was written. I didn’t check if that is still the case, but still used the Raspberry Pi version of the kernel. As I had some problems with more recent versions of the kernel, I decided to use version 4.19 (at least for now):

cd <project/root/dir>
git clone --depth=1 -b rpi-4.19.y https://github.com/raspberrypi/linux.git
cd linux

The command does not clone the whole repo, but enough to build the kernel. And by not cloning everything, we save a lot of time :-). The commit that was checked out after cloning the repo was bfef951a.

To build the kernel, we need the toolchain that was built before. Therefore we have to prepend our PATH. In case you followed my description in Building a (Cross) Toolchain, you just have to adjust <project/root/dir> in the following command. Otherwise you might have to adjust more. To prepend the PATH, run:

PATH=<project/root/dir>/x-tools/aarch64-rpi3-linux-gnu/bin/:$PATH

Additionally we need to set two environment variable:

export CROSS_COMPILE=aarch64-rpi3-linux-gnu-
export ARCH=arm64

Now we can start building the kernel.

Note: The following command will delete a file called .config in your current working directory. In case you have been working on a configuration for a kernel before, you might want to save this .config file for later use. For good measure, you should run:

make distclean

before you start building the kernel.

The first thing you need to do to build kernel is to configure the kernel you want to build. This can be quite complex. It helps to use a sample configuration and, if needed, adjust it. You can find kernel configurations in the sub directories of the arch directory (e.g. arch/arm64/configs/bcmrpi3_defconfig, the one we will use). To use it, run:

make bcmrpi3_defconfig

It is possible to adjust the configuration. To do so you can run the following command to launch a terminal based configuration tool:

make menuconfig

For now, I didn’t change a lot. The only thing I changed to identify the kernel later on, I added my name to Local version - append to kernel release under General setup. When you are satisfied with the configuration, the final step is to actually build the bootloader. This is as simple as just running make:

make

My first attempt to build the kernel failed, due to a missing package on my host system. I had to install the package bc. After that everything build just fine. make does not only build the kernel itself, but also a device tree that is needed.

Once this command terminates and everything went to plan, you will have a bunch of new files in your current working directory linux and some of the sub directories. Which once are needed to actually run the kernel, will be the subject of the post Run the Linux Kernel on a Raspberry Pi 3 Model B+.

Change Log

2025-02-01:

  • Made the post fit into the series of posts about embedded Linus on a Raspberry Pi 3 Model B+.

2025-01-20:

  • Fixed some typos.

2023-11-13:

  • Correct path to the configuration file bcmrpi3_defconfig.
  • Add link to post about running the kernel.



Take care,
Andreas


References

  1. Raspberry Pi, “linux.” [Online]. Available at: https://github.com/raspberrypi/linux. [Accessed: 08-Nov-2023].
  2. F. Vasquez and C. Simmonds, Mastering Embedded Linux Programming, 3rd ed. Packt Publishing Ltd., 2021.

Updated:

Leave a comment