Skip to content

Commit a1839d7

Browse files
authored
WIP: Add Build instructions for compiling a 64-Bit kernel (raspberrypi#1653)
* Declare build instructions as 32-bit * Early 64-Bit guide. Needs Testing. * Added some formatting for the config and build * Move some instructions for CI that apply to both arches * Added some better seperators * Looks weird to read when generated into a web format so it has been clarified * Clarify headings and add seperators * Undo "Add Seperators" as it did not format well as I wished * Use correct pi 3 defconfig * Remove bad comment from code * Fix instructions after following guide to compile for Pi 3 * Use distro cross-compilers in kernel build * Fixed change after testing installation. Compiling is still the same * Remove binutils requirment already as it is a dep for gcc-arm * Remove zlib1g from instructions as mentioned in comments. * Modifed configuring the kernel build to add instructions for the 64bit build * Remove required packages as it is already in meta-package as rec * Revert "Remove required packages as it is already in meta-package as rec" This reverts commit d721932. * remove ccache instructions for when we were using the tools repo * combine dependencies and toolchain install instructions * Remove addition to title * Remove extra colon * Lowercase *-Bit to *-bit * Add README from build * Fixed Capitialization for reviewed change * Partially Undo Capitialization Fix * Updated header to help user choose which compiling guide to use * Revert "Updated header to help user choose which compiling guide to use" This reverts commit 12fb61d. * Lowercase "Bit" identifier * New Header provided from comments
1 parent 69d084e commit a1839d7

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

linux/kernel/building.md

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Kernel building
22

3-
There are two main methods for building the kernel. You can build locally on a Raspberry Pi, which will take a long time; or you can cross-compile, which is much quicker, but requires more setup.
3+
The default compilers and linkers that come with an OS are configured 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 configured to build code for a target other than the one running the build process, and using it is called cross-compilation.
4+
5+
Cross-compilation of the Raspberry Pi kernel is useful for two reasons:
6+
7+
* it allows a 64-bit kernel to be built using a 32-bit OS, and vice versa, and
8+
* even a modest laptop can cross-compile a Pi kernel significantly faster than the Pi itself.
9+
10+
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.
411

512
## Local building
613

@@ -92,7 +99,7 @@ sudo cp arch/arm/boot/zImage /boot/$KERNEL.img
9299

93100
**Note**: On a Raspberry Pi 2/3/4, the `-j4` flag splits the work between all four cores, speeding up compilation significantly.
94101

95-
## Cross-compiling
102+
## Cross-compiling
96103

97104
First, you will need a suitable Linux cross-compilation host. We tend to use Ubuntu; since Raspberry Pi OS is
98105
also a Debian distribution, it means many aspects are similar, such as the command lines.
@@ -103,10 +110,21 @@ You can either do this using VirtualBox (or VMWare) on Windows, or install it di
103110

104111
To build the sources for cross-compilation, make sure you have the dependencies needed on your machine by executing:
105112
```bash
106-
sudo apt install git bc bison flex libssl-dev make libc6-dev libncurses5-dev crossbuild-essential-armhf
113+
sudo apt install git bc bison flex libssl-dev make libc6-dev libncurses5-dev
107114
```
115+
108116
If you find you need other things, please submit a pull request to change the documentation.
109117

118+
#### Install the 32-bit toolchain for a 32-bit kernel
119+
```bash
120+
sudo apt install crossbuild-essential-armhf
121+
```
122+
123+
#### Or, install the 64-bit toolchain for a 64-bit kernel
124+
```bash
125+
sudo apt install crossbuild-essential-arm64
126+
```
127+
110128
### Get sources
111129

112130
To download the minimal source tree for the current branch, run:
@@ -121,6 +139,7 @@ See [**Choosing sources**](#choosing_sources) above for instructions on how to c
121139

122140
Enter the following commands to build the sources and Device Tree files:
123141

142+
#### 32-bit configs
124143
For Pi 1, Pi Zero, Pi Zero W, or Compute Module:
125144

126145
```bash
@@ -145,14 +164,36 @@ KERNEL=kernel7l
145164
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig
146165
```
147166

148-
Then, for all:
167+
#### 64-bit configs
168+
For Pi 3, Pi 3+ or Compute Module 3:
169+
```bash
170+
cd linux
171+
KERNEL=kernel8
172+
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcmrpi3_defconfig
173+
```
149174

175+
For Raspberry Pi 4:
150176
```bash
151-
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs
177+
cd linux
178+
KERNEL=kernel8
179+
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig
152180
```
153181

182+
#### Build with configs
183+
154184
**Note**: To speed up compilation on multiprocessor systems, and get some improvement on single processor ones, use `-j n`, where n is the number of processors * 1.5. Alternatively, feel free to experiment and see what works!
155185

186+
##### For all 32-bit builds
187+
```bash
188+
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs
189+
```
190+
191+
##### For all 64-bit builds
192+
**Note**: Note the difference between Image target between 32 and 64-bit.
193+
```bash
194+
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image modules dtbs
195+
```
196+
156197
### Install directly onto the SD card
157198

158199
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.
@@ -190,14 +231,23 @@ sudo mount /dev/sdb6 mnt/fat32
190231
sudo mount /dev/sdb7 mnt/ext4
191232
```
192233

193-
Next, install the modules:
194234

235+
Next, install the kernel modules onto the SD card:
236+
237+
#### For 32-bit
195238
```bash
196239
sudo env PATH=$PATH make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=mnt/ext4 modules_install
197240
```
198241

242+
#### For 64-bit
243+
```bash
244+
sudo env PATH=$PATH make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=mnt/ext4 modules_install
245+
```
246+
199247
Finally, copy the kernel and Device Tree blobs onto the SD card, making sure to back up your old kernel:
200248

249+
#### For 32-bit
250+
201251
```bash
202252
sudo cp mnt/fat32/$KERNEL.img mnt/fat32/$KERNEL-backup.img
203253
sudo cp arch/arm/boot/zImage mnt/fat32/$KERNEL.img
@@ -208,6 +258,18 @@ sudo umount mnt/fat32
208258
sudo umount mnt/ext4
209259
```
210260

261+
#### For 64-bit
262+
263+
```bash
264+
sudo cp mnt/fat32/$KERNEL.img mnt/fat32/$KERNEL-backup.img
265+
sudo cp arch/arm64/boot/Image mnt/fat32/$KERNEL.img
266+
sudo cp arch/arm64/boot/dts/broadcom/*.dtb mnt/fat32/
267+
sudo cp arch/arm64/boot/dts/overlays/*.dtb* mnt/fat32/overlays/
268+
sudo cp arch/arm64/boot/dts/overlays/README mnt/fat32/overlays/
269+
sudo umount mnt/fat32
270+
sudo umount mnt/ext4
271+
```
272+
211273
Another option is to copy the kernel into the same place, but with a different filename - for instance, kernel-myconfig.img - rather than overwriting the kernel.img file. You can then edit the config.txt file to select the kernel that the Pi will boot into:
212274

213275
```

linux/kernel/configuring.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ Once you've got everything set up and ready to go, you can compile and run the `
2222
$ make menuconfig
2323
```
2424

25-
If you're cross-compiling,:
25+
If you're cross-compiling a 32-bit kernel:
2626

2727
```
2828
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
2929
```
3030

31+
Or, if you are cross-compiling a 64-bit kernel:
32+
33+
```
34+
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
35+
```
36+
3137
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 configure; there's a lot, so take your time to read through them and get acquainted.
3238

3339
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 specific option or menu.

0 commit comments

Comments
 (0)