Raspberry Pi Documentation - The Linux Kernel
Raspberry Pi Documentation - The Linux Kernel
html
Kernel
Edit this on GitHub
• You’ve written some Raspberry Pi-speci�c code that you want everyone to bene�t
from
• You’ve written a generic Linux kernel driver for a device and want everyone to use it
Initially, you should fork the Linux repository and clone that on your build system; this can
be either on the Raspberry Pi or on a Linux machine you’re using for cross-compiling. You
can then make your changes, test them, and commit them into your fork.
• For Raspberry Pi-speci�c changes or bug �xes, submit a pull request to the kernel.
• For general Linux kernel changes (i.e. a new driver), these need to be submitted
upstream �rst. Once they’ve been submitted upstream and accepted, submit the pull
request and we’ll receive it.
The default compilers and linkers that come with an OS are con�gured to build
executables to run on that OS - they are native tools - but that doesn’t have to be the case.
A cross-compiler is con�gured to build code for a target other than the one running the
build process, and using it is called cross-compilation.
• it allows a 64-bit kernel to be built using a 32-bit OS, and vice versa, and
The instructions below are divided into native builds and cross-compilation; choose the
section appropriate for your situation - although there are many common steps between
the two, there are also some important differences.
Choosing Sources
The git clone command above will download the current active branch (the one we are
building Raspberry Pi OS images from) without any history. Omitting the --depth=1 will
download the entire repository, including the full history of all branches, but this takes
much longer and occupies much more storage.
To download a different branch (again with no history), use the --branch option:
where <branch> is the name of the branch that you wish to download.
Refer to the original GitHub repository for information about the available branches.
Kernel Con�guration
Con�gure the kernel; as well as the default con�guration, you may wish to con�gure your
kernel in more detail or apply patches from another source, to add or remove required
functionality.
First, prepare the default con�guration by running the following commands, depending on
your Raspberry Pi model:
For Raspberry Pi 1, Zero and Zero W, and Raspberry Pi Compute Module 1 default (32-bit
only) build con�guration
cd linux
KERNEL=kernel
make bcmrpi_defconfig
cd linux
KERNEL=kernel7
make bcm2709_defconfig
For Raspberry Pi 4 and 400, and Raspberry Pi Compute Module 4 default 32-bit build
con�guration
cd linux
KERNEL=kernel7l
make bcm2711_defconfig
For Raspberry Pi 3, 3+, 4, 400 and Zero 2 W, and Raspberry Pi Compute Modules 3, 3+ and
4 default 64-bit build con�guration
cd linux
KERNEL=kernel8
make bcm2711_defconfig
In addition to your kernel con�guration changes, you may wish to adjust the LOCALVERSION
to ensure your new kernel does not receive the same version string as the upstream
kernel. This both clari�es you are running your own kernel in the output of uname and
ensures existing modules in /lib/modules are not overwritten.
CONFIG_LOCALVERSION="-v7l-MY_CUSTOM_KERNEL"
You can also change that setting graphically as shown in the kernel con�guration
instructions. It is located in "General setup" => "Local version - append to kernel release".
Build and install the kernel, modules, and Device Tree blobs; this step can take a long time
depending on the Raspberry Pi model in use. For the 32-bit kernel:
NOTE
On a Raspberry Pi 2/3/4, the -j4 �ag splits the work between all four cores, speeding
up compilation signi�cantly.
If you now reboot, your Raspberry Pi should be running your freshly-compiled kernel!
You can either do this using VirtualBox (or VMWare) on Windows, or install it directly onto
your computer. For reference, you can follow instructions online at Wikihow.
To build the sources for cross-compilation, make sure you have the dependencies needed
on your machine by executing:
sudo apt install git bc bison flex libssl-dev make libc6-dev libncurses5-dev
If you �nd you need other things, please submit a pull request to change the
documentation.
To download the minimal source tree for the current branch, run:
See Choosing sources above for instructions on how to choose a different branch.
Build sources
Enter the following commands to build the sources and Device Tree �les:
32-bit Con�gs
cd linux
KERNEL=kernel
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig
For Raspberry Pi 2, 3, 3+ and Zero 2 W, and Raspberry Pi Compute Modules 3 and 3+:
cd linux
KERNEL=kernel7
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
cd linux
KERNEL=kernel7l
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig
64-bit Con�gs
For Raspberry Pi 3, 3+, 4, 400 and Zero 2 W, and Raspberry Pi Compute Modules 3, 3+ and
4:
cd linux
KERNEL=kernel8
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig
NOTE
NOTE
Having built the kernel, you need to copy it onto your Raspberry Pi and install the modules;
this is best done directly using an SD card reader.
First, use lsblk before and after plugging in your SD card to identify it. You should end up
with something a lot like this:
sdb
sdb1
sdb2
with sdb1 being the FAT �lesystem (boot) partition, and sdb2 being the ext4 �lesystem
(root) partition.
mkdir mnt
mkdir mnt/fat32
mkdir mnt/ext4
NOTE
You should adjust the drive letter appropriately for your setup, e.g. if your SD card
appears as /dev/sdc instead of /dev/sdb.
For 32-bit
For 64-bit
Finally, copy the kernel and Device Tree blobs onto the SD card, making sure to back up
your old kernel:
For 32-bit
For 64-bit
Another option is to copy the kernel into the same place, but with a different �lename - for
instance, kernel-myconfig.img - rather than overwriting the kernel.img �le. You can then
edit the config.txt �le to select the kernel that the Raspberry Pi will boot:
kernel=kernel-myconfig.img
This has the advantage of keeping your custom kernel separate from the stock kernel
image managed by the system and any automatic update tools, and allowing you to easily
revert to a stock kernel in the event that your kernel cannot boot.
Finally, plug the card into the Raspberry Pi and boot it!
The Linux kernel is highly con�gurable; advanced users may wish to modify the default
con�guration to customise it to their needs, such as enabling a new or experimental
network protocol, or enabling support for new hardware.
Preparing to Con�gure
The menuconfig tool requires the ncurses development headers to compile properly. These
can be installed with the following command:
You’ll also need to download and prepare your kernel sources, as described in the build
guide. In particular, ensure you have installed the default con�guration.
Using menuconfig
Once you’ve got everything set up and ready to go, you can compile and run the
menuconfig utility as follows:
make menuconfig
The menuconfig utility has simple keyboard navigation. After a brief compilation, you’ll be
presented with a list of submenus containing all the options you can con�gure; there’s a
lot, so take your time to read through them and get acquainted.
Use the arrow keys to navigate, the Enter key to enter a submenu (indicated by --->),
Escape twice to go up a level or exit, and the space bar to cycle the state of an option.
Some options have multiple choices, in which case they’ll appear as a submenu and the
Enter key will select an option. You can press h on most entries to get help about that
speci�c option or menu.
Resist the temptation to enable or disable a lot of things on your �rst attempt; it’s relatively
easy to break your con�guration, so start small and get comfortable with the con�guration
and build process.
When building your custom kernel you may wish to apply patches, or collections of
patches ('patchsets'), to the Linux kernel.
Patchsets are often provided with newer hardware as a temporary measure, before the
patches are applied to the upstream Linux kernel ('mainline') and then propagated down to
the Raspberry Pi kernel sources. However, patchsets for other purposes exist, for instance
to enable a fully pre-emptible kernel for real-time usage.
Version Identi�cation
It’s important to check what version of the kernel you have when downloading and
applying patches. In a kernel source directory, running head Makefile -n 3 will show you
the version the sources relate to:
VERSION = 3
PATCHLEVEL = 10
SUBLEVEL = 25
In this instance, the sources are for a 3.10.25 kernel. You can see what version you’re
running on your system with the uname -r command.
Applying Patches
How you apply patches depends on the format in which the patches are made available.
Most patches are a single �le, and applied with the patch utility. For example, let’s
download and patch our example kernel version with the real-time kernel patches:
wget https://www.kernel.org/pub/linux/kernel/projects/rt/3.10/older/patch-3.1
0.25-rt23.patch.gz
gunzip patch-3.10.25-rt23.patch.gz
cat patch-3.10.25-rt23.patch | patch -p1
In our example we simply download the �le, uncompress it, and then pass it to the patch
utility using the cat tool and a Unix pipe.
git am -3 /path/to/patches/*
If in doubt, consult with the distributor of the patches, who should tell you how to apply
them. Some patchsets will require a speci�c commit to patch against; follow the details
provided by the patch distributor.
Kernel Headers
Edit this on GitHub
If you are compiling a kernel module or similar, you will need the Linux Kernel headers.
These provide the various function and structure de�nitions required when compiling code
that interfaces with the kernel.
If you have cloned the entire kernel from github, the headers are already included in the
source tree. If you don’t need all the extra �les, it is possible to install only the kernel
headers from the Raspberry Pi OS repo.
NOTE
It can take quite a while for this command to complete, as it installs a lot of small �les.
There is no progress indicator.
When a new kernel release is made, you will need the headers that match that kernel
version. It can take several weeks for the repo to be updated to re�ect the latest kernel
version. If this happens, the best approach is to clone the kernel as described in the Build
Section.
Raspberry Pi documentation is copyright © 2012-2023 Raspberry Pi Ltd and is licensed under a Creative Commons
Attribution-ShareAlike 4.0 International (CC BY-SA) licence.
Some content originates from the eLinux wiki, and is licensed under a Creative Commons Attribution-ShareAlike 3.0
Unported licence.