Build an RPi3 64bit Kernel on your crossdev PC - sakaki-/gentoo-on-rpi-64bit GitHub Wiki
Build a 64-bit kernel for your Raspberry Pi 3 in minutes, using your crossdev
PC!
For instructions on building an RPi4 compatible kernel instead, please see my instructions here.
Prerequisites
This guide assumes that you have already set up crossdev
on your PC (and then used it to create a stable-branch aarch64-unknown-linux-gnu
toolchain), per these instructions. So, if you haven't yet, please do this first, before proceeding.
Building an RPi3-Compatible 64-bit Kernel
First, we need to download some kernel sources. The official Raspberry Pi Linux kernel tree (which may be found here) now contains support for arm64
kernels. Decide which kernel version branch you want to build by browsing on GitHub, and then download a shallow copy of this to your PC (users with sufficient bandwidth and disk space may of course clone the entire tree, and then choose the desired branch locally, but the following approach is much faster). I recommend using at least version rpi-4.19.y
to get the most up-to-date changes (and kernels that will boot on the RPi3 model B+, as well as the model B); in the below, I'll assume you want to use version rpi-4.19.y
(the same as in the current image; modify the instructions given for your desired branch, of course).
Working logged in as your regular user, not root (for security), issue:
user@gentoo_pc ~ $ mkdir -pv kbuild && cd kbuild
user@gentoo_pc kbuild $ rm -rf linux
user@gentoo_pc kbuild $ git clone --depth 1 https://github.com/raspberrypi/linux.git -b rpi-4.19.y
This may take some time to complete, depending on the speed of your network connection.
When it has completed, go into the newly created linux
directory, and set up the baseline bcmrpi3_defconfig
:
user@gentoo_pc kbuild $ cd linux
user@gentoo_pc linux $ make ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- distclean
user@gentoo_pc linux $ make ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- bcmrpi3_defconfig
Next, modify the configuration if you like to suit your needs (this step is optional, as a kernel built with the stock bcmrpi3_defconfig
will work perfectly well for most users):
user@gentoo_pc linux $ make ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- menuconfig
When ready, go ahead and build the kernel, modules, and dtbs. Issue:
user@gentoo_pc linux $ nice -n 19 make ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- -j$(nproc) Image modules dtbs
This does a parallel make
, so on a modern PC, it shoudn't take long (5-15 mins). The build is forced to run at the lowest system priority, to prevent your machine becoming too unresponsive during this time.
With the kernel built, we need to install it. Insert the target microSD card into a reader on your PC (this card should already have the appropriate baseline bootfiles and system in place, as it will do if it has been created using the image provided in this project, for example). Let's assume your target microSD-card's boot directory is on /dev/mmcblk0p1
, and its root is on /dev/mmcblk0p2
(adapt these paths to match your circumstances). Then, we begin by mounting these two partitions (I'm going to assume on /mnt/piboot
and /mnt/piroot
respectively; again, adapt as desired). Become root, and issue:
gentoo_pc ~ # mkdir -pv /mnt/pi{boot,root}
gentoo_pc ~ # mount -v /dev/mmcblk0p1 /mnt/piboot
gentoo_pc ~ # mount -v /dev/mmcblk0p2 /mnt/piroot
You can now can copy across the kernel (remember to back up the old version, if any, first, in case of problems). Substituting your regular user's account name (the one you logged into when building the kernel, above) for user
in the below, issue:
gentoo_pc ~ # cd /home/user/kbuild/linux
gentoo_pc linux # cp -v arch/arm64/boot/Image /mnt/piboot/kernel8.img
Note that by default, the kernel does not require a separate U-Boot loader.
Next, copy over the device tree blobs (at the time of writing arm64
was still using the 2710
dtb, but this may change to 2837
in future, so for safety, copy both, and also copy the new B+ and compute module 3 dtbs):
gentoo_pc linux # cp -v arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b-plus.dtb /mnt/piboot/
gentoo_pc linux # cp -v arch/arm64/boot/dts/broadcom/bcm2710-rpi-cm3.dtb /mnt/piboot/
gentoo_pc linux # cp -v arch/arm64/boot/dts/broadcom/bcm{2710,2837}-rpi-3-b.dtb /mnt/piboot/
Then, copy over the device tree overlay blobs (there are now the responsibility of the binary kernel package, not the boot firmware any longer):
gentoo_pc linux # cp -rv arch/arm64/boot/dts/overlays /mnt/piboot/
Lastly, copy over the modules:
gentoo_pc linux # make ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- modules_install INSTALL_MOD_PATH="/mnt/piroot"
gentoo_pc linux # cd
and build metadata:
gentoo_pc linux # cp -v Module.symvers System.map /mnt/piboot/
Note that we don't do a
make firmware_install
here, since that build target has been dropped for >= 4.14.
All done! You can now sync the filesystem, and unmount the microSD card partitions:
gentoo_pc ~ # sync
gentoo_pc ~ # umount -v /mnt/piboot
gentoo_pc ~ # umount -v /mnt/piroot
gentoo_pc ~ # rmdir /mnt/pi{boot,root}
Now you can remove the microSD card from your machine, insert it into your RPi3, and power on to try booting with your new 64-bit kernel!
Next Steps
Now that you have a functioning cross-compilation environment on your PC, why not hook it up to distcc
? This will allow your RPi3 to leverage the power of your PC when performing local builds (for C and C++ compilation and header pre-processing), and makes it possible to use a source-based distribution like Gentoo without huge update times. See these instructions for details on how to proceed.