Raspberry Pi 3 64 bit Install

History
The Raspberry Pi 3 with a 64-bit capable CPU became available on Feb. 2016. At the outset, it was difficult to install Gentoo on the Pi 3 in 64-bit mode. A lot of work by a lot of people has almost brought a 64-bit Gentoo install on the Pi 3 down to almost a standard handbook install.

Currently the raspberrypi-userland is not fully supported in 64-bit userland, therefore e.g. the raspberry pi camera will not work natively here.

What works
All the Pi 3 hardware is supported in 64-bit mode.


 * Wi-Fi
 * USB
 * Ethernet (needs USB)
 * Bluetooth
 * Hardware Video Acceleration
 * Sound over HDMI

Untested

 * PAL/NTSC video output
 * Analogue sound output

Whats required

 * Gentoo install (or prefix install) on a PC
 * microSD card reader for the PC
 * Raspberry Pi 3
 * microSD card > 8G
 * USB keyboard
 * USB mouse
 * HDMI display

The content of the microSD card will be wiped during the install.

Installation overview

 * 1) Install crossdev on the PC.
 * 2) Fetch the Raspberry Pi firmware.
 * 3) Fetch the Raspberry Pi kernel.
 * 4) Partition the microSD card.
 * 5) Fetch the Gentoo bits of the install.
 * 6) Cross compile and install the kernel.
 * 7) Setup.
 * 8) Boot the Pi to test.

Install crossdev on the PC
There are no prebuilt kernel images for the Pi 3 in its arm64 mode. Until an kernel is running, the system cannot boot in 64-bit mode. It's a chicken and egg problem. Once the Pi is bootstrapped with a kernel, it can build its own kernels.

crossdev is Gentoos' tool for building cross compiler tool chains. Once its installed, we will use it to build the kernel on the Gentoo PC. Install the crossdev tool:

Using crossdev to build a cross compiler
An overlay may need nominated to store the cross ebuilds, crossdev will provide an alert if this is the case.

Convert files as crossdev asks e.g.

error: please convert /etc/portage/package.env to a directory

by appending _file to the existing filename

making the directory

then moving into the directory.

Rinse and repeat until crossdev is happy.

crossdev will take a while. It's building the following packages:


 * binutils: binutils-[latest]
 * gcc: gcc-[latest]
 * Linux headers: linux-headers-[latest]
 * libc:glibc-[latest]

When crossdev completes a cross toolchain will be available:

It will also create an target root in  This is used by cross emerge.

Pure cross compiling packages, other than the kernel, is out of scope of this guide.

Fetch the Raspberry Pi firmware
The Raspberry Pi firmware is maintained in a git repository. Git will be needed to obtain a copy of the firmeware files.

As a normal user, create a directory somewhere for raspberry pi firmware and kernel files:

Clone the firmware sources:

The directory will be needed out of the stable branch. The  option is used so that not all the history will be fetched (it is unnecessary). This will save a lot of space, bandwidth, and time.

There is nothing to build. is used as is.

Fetch the Raspberry Pi kernel
Stay in directory and run:

With absolutely no fanfare at all, 64-bit support was added to this kernel tree late in 2016. No more searching for odd patches.

Not everything has been accepted by the mainline kernel yet but it's getting closer. Feel free to test mainline kernel builds every once in a while. If it builds successfully and the Rpi can be used to test successful boot, then this section of the guide can be updated with instructions for mainline Linux.

The master/main Raspberry Pi Foundation branch may be broken. Test if needed. It is better to use a tagged release branch. At the time of writing that is rpi-4.14.y. The 4.14 mainline kernel is still at -rc status. Checkout should be optional and will work only if --depth option is omitted (beware, large repo).

With the kernel source tree in place, ready for configuring and cross compiling.

Configure the kernel
As user:

The config file is almost right as it stands. It defaults to the powersave CPU governor, which runs the Pi at 600MHz.

All the governors are there. The ondemand governor is recommended.

Either use menuconfig to change the default CPU governor, add something to the kernel command line, or change it after booting.

Search for CPU_FREQ_DEFAULT_GOV go to that location and set the default to ondemand. Exit menuconfig, saving the change.

.config - Linux/arm64 4.14.72-raspberrypi Kernel Configuration > CPU Power Management > CPU Frequency scaling ────────────────────────────────── ┌────────────────────────── CPU Frequency scaling ───────────────────────────┐ │ Arrow keys navigate the menu.  selects submenus ---> (or empty   │    │  submenus ). Highlighted letters are hotkeys. Pressing  includes, │ │  excludes,  modularizes features. Press  to exit, <?>   │ │ for Help,  for Search. Legend: [*] built-in [ ] excluded   module │ │ ┌────────────────────────────────────────────────────────────────────────┐ │   │ │    [*] CPU Frequency scaling                                           │ │ │ │   [*]   CPU frequency transition statistics                           │ │ │ │   [ ]     CPU frequency transition statistics details                 │ │ │ │         Default CPUFreq governor (powersave)  --->                    │ │ │ │   <*>   'performance' governor                                        │ │ │ │   -*-   'powersave' governor                                          │ │ │ │   <*>   'userspace' governor for userspace frequency scaling          │ │ │ │   <*>   'ondemand' cpufreq policy governor                            │ │

The kernel contains lots of support for hardware you don't have and possibly have never heard of. More confident readers may be tempted to trim things out now. A word of advice - don't, at least, not until the system boots.

Cross compiling the kernel
The kernel will not link with the gold linker. It ends with

aarch64-unknown-linux-gnu-ld: fatal error: -shared and -pie are incompatible

If you don't know what the gold linker is, you are not using it.

The build is conventional, other than telling the build system to build for arm64 and use the cross compiler.

Change  to the number of parallel make jobs you want to run. Convention is N number of CPU cores + 1.

Background
The Raspberry Pi does not need a boot loader. There is no firmware for the ARM CPU, like there is in a PC. Instead, the GPU manages loading software for the CPU to execute, while the CPU is held reset. Its the GPU that has the 'BIOS' that gets everything started.

This makes it impossible to brick the Pi, since at worst, the microSD card needs to be reloaded.

This arrangement does impose some constraints on the microSD card.

The partition table must be MSDOS The first partition (/boot) must be vfat.

The file has some useful defaults that make setup easier, which we will take advantage of later.

Partitioning
Depending on how the microSD card is connected to the PC, it my be or

In the example below, it's. Using the partitioning tool of your choice, make three partitions on your microSD card.

boot 128Mb swap 2G root (/) the rest.

Using, and your microSD card block device, not my

Make the partition table and add partitions
Make a new MS-DOS disklable

Command (m for help): o

Add a new partition - this will be 128Mb for

Command (m for help): n Partition type p  primary (0 primary, 0 extended, 4 free) e  extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-15523839, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-15523839, default 15523839): +128M

Has created a new partition 1 of type 'Linux' and of size 128 MiB.

Add a 2G partition 2 for swap.

Put the remaining space in partition 3 for root. Check with

Command (m for help): p

Using an 8G microSD card it should show

Device    Boot   Start      End  Sectors  Size Id Type /dev/sdk1         2048   264191   262144  128M 83 Linux /dev/sdk2       264192  4458495  4194304    2G 83 Linux /dev/sdk3      4458496 15523839 11065344  5.3G 83 Linux

Set flags and partition types
Toggle the bootable flag on partition 1

Command (m for help): a Partition number (1-3, default 3): 1

The bootable flag on partition 1 is enabled now.

Mark partition 1 as FAT

Command (m for help): t Partition number (1-3, default 3): 1 Partition type (type L to list all types): c

Changed type of partition 'Linux' to 'W95 FAT32 (LBA)'.

Mark partition 2 as swap

Command (m for help): t Partition number (1-3, default 3): 2 Partition type (type L to list all types): 82

Changed type of partition 'Linux swap / Solaris' to 'Linux swap / Solaris'.

Check again.

Device    Boot   Start      End  Sectors  Size Id Type /dev/sdk1 *       2048   264191   262144  128M  c W95 FAT32 (LBA) /dev/sdk2       264192  4458495  4194304    2G 82 Linux swap / Solaris /dev/sdk3      4458496 15523839 11065344  5.3G 83 Linux

Save the new partition table
Exit with either  or

w  write table to disk and exit q  quit without saving changes

Make filesystems
You know the block devices from the partitioning step above. Fill in the ... to match your system.

vfat for

for swap

ext4 for root

The inode count cannot be changed after filesystem creation and can limit the number of files on a files system. The Gentoo repository alone needs over 17,000 inodes.

Fetch the Gentoo bits of the install
To make it easy to cross refer to the Gentoo_Handbook

Mount the microSD card root filesystem at

Install the arm64 stage 3
Fetch the arm64 stage3 and untar it with

should be empty.

Install a Gentoo repository snapshot
''This step is not actually needed to boot the Pi but won't work without it. No software can be installed without the Gentoo repository containing the ebuilds to emerge.''

Following the Handbook fetch and unpack a Gentoo repository shapshot in the normal way.

Careful readers can copy their host as long as  and  are omitted.

If is not able to be executed, unpack the Gentoo repository snapshot manually like the following.

The Raspberry Pi Foundation provided files
Mount the microSD card boot at. (The microSD card root should still be mounted at )

As root, copy the content of your normal users to.

You should end up with files in, not a directory called.

Not all the files there are required. It's a one size fits all for all models of Raspberry Pi operating in 32-bit mode.

Install the kernel to the microSD card
The kernel was built above, now to install it. The kernel is in three parts:


 * 1) Kernel binary.
 * 2) Kernel modules.
 * 3) The device tree.

Install the kernel binary
As root, copy the kernel binary from the build location

It is possible to use other kernel file names by adding entries to

Install the device tree
The device tree binary describes the hardware to the kernel. This avoids having all the existing hardware configurations hard coded into the kernel.

Due to the way the Raspberry Pi 64-bit kernel support has been added, there are going to be two different device trees with the same file name. A 32-bit version and a 64-bit version. They are not interchangeable. Move the 32-bit version out of the way.

For Raspberry Pi 3B:

For Raspberry Pi 3B Plus:

That's renamed the 32-bit version.

Copy the dtb from the build location

For Raspberry Pi 3B:

For Raspberry Pi 3B Plus:

It is possible to use other device tree file names by adding entries to

Install the kernel modules
From the top of the kernel tree, install the kernel modules.

The INSTALL_MOD_PATH is the root of the filesystem the modules are to be installed into. Due to kernel naming

it's unlikely you will have a 4.10.0-rc6-v8+ so omitting INSTALL_MOD_PATH is probably harmless to your build host install.

Checking the kernel install
Notice the two device tree binaries and the.

Shows that the kernel modules were installed to the correct location.

Raspberry Pi 3 peripherals
Now that we have the base operating system in, we will need to do some file configuration by hand to get the peripherals working.

Serial port configuration
The Gentoo stage 3 comes with the default Gentoo serial port configuration. In Raspbian Jessie, udev rules exist that provide aliases for GPIO14:15 and the Bluetooth serial port. If you're not using the pi3-miniuart-bt or the pi3-disable-bt device tree overlays, (mini UART) points to  (GPIO14:15) while  (Bluetooth) points to  (Bluetooth).

If, however, the pi3-miniuart-bt overlay is in use, udev rules automagically make point to  (GPIO14:15) while  points to  (Bluetooth). If the pi3-disable-bt overlay is in use, still points to  (GPIO14:15) while Bluetooth/ are disabled.

This allows the use of in any Bluetooth configuration files and command line arguments regardless of which serial port it is assigned to. It also allows the use of / in any apps and/or configuration files/command line arguments which reference the GPIO14:15 pins regardless of which overlay is/is not in use.

In order to make things easier when working with the device tree overlays for the Bluetooth module as well as any applications which were written in a Raspbian environment that uses the and  aliases, we'll need to first create a udev rule to mimic this behavior.

Open up -

Find this line and comment it out by appending a # at the beginning of the line -

Save and exit the file. This prevents Gentoo from assigning the serial console to on  mount, which will conflict with Bluetooth operation if left uncommented.

Next, create the empty file in  -

Copy and paste this into the empty file, then save and exit the file -

This will assign and  to the  group just as they are in Raspbian Jessie. It will also provide the (GPIO14:15) and  (Bluetooth) aliases, which eases the task of switching the serial ports around between the Bluetooth and GPIO14:15.

Install Wi-Fi firmware
The Raspberry Pi 3 WiFi requires firmware to operate. The files and  are both required to be present in  for Raspberry Pi 3B, while the files  and  are required for Raspberry Pi 3B Plus.

They can be downloaded via git.kernel.org or github.com/armbian.

Fetch the Raspberry Pi 3B Wireless Firmware:

Fetch the Raspberry PI 3B+ Wireless Firmware:

Kernel
You'll also want to ensure that Broadcom IEEE802.11n embedded FullMAC WLAN driver with SDIO bus interface support is enabled in the kernel. In the menu shown below, this is all that needs to be enabled. You can set the FullMAC WLAN driver as a module or compile it into the kernel. If you plan to use only the onboard WiFi, everything else under the Wireless LAN menu can be disabled.

Once the firmware files are copied over to, the appropriate kernel settings are built, and a wireless network manager (such as wpa_supplicant) is installed, the WiFi should work.

Install Bluetooth firmware
The Raspberry Pi 3B needs the firmware file, and the Raspberry Pi 3B+ needs the  firmware file. The firmware files can be found in the Raspbian bluez-firmware GitHub repository. The firmware files need to be placed in the directory.

Create the firmware directory:

Fetch the Raspberry Pi 3B Bluetooth firmware:

Fetch the Raspberry Pi 3B+ Bluetooth firmware:

After boot:

Attach the serial device to the Bluetooth stack using, which is provided by the  package:

Alternatively, can be used if  is built with the   USE flag enabled:

Both commands will create a HCI device (e.g. ) in and load the required firmware. To have the HCI device created at boot using, the following OpenRC init script can be used:

Make the init script executable:

After booting Pi

Start :

Start at boot:

Setup
The chroot environment is very handy as it allows you to configure your startup run levels as well as update your Portage snapshot. Additionally, you can set the root password as well as create users in the chroot environment.

If you plan to only access your Pi via SSH, you'll need at least networking on startup, which requires either net.eth0 or dhcpcd to be added to the default run level, as well as OpenSSH itself (sshd).

Much of the setup prior to booting a new Gentoo install is done in a chroot. Chrooting into an arm64 install from some other arch is beyond the scope of this guide. Instead, only the bare minimum setup from outside the chroot is covered. Consult the Embedded Handbook/General/Compiling with qemu user chroot page to learn how to use qemu to establish a cross-chroot.

Root password
There are several ways to generate a password hash for. I usually copy the hash from another system. My Pi uses an root entry:

root:$6$xxPVR/Td5iP$/7Asdgq0ux2sgNkklnndcG4g3493kUYfrrdenBXjxBxEsoLneJpDAwOyX/kkpFB4pU5dlhHEyN0SK4eh/WpmO0::0:99999:7:::

The matching password is raspberry.

Feel free to use that line to replace the root entry in

You can change the password once you are logged in.

/etc/fstab
On the Pi, the microSD card will be with partitions, , and

Edit to match.

/boot/config.txt
This file will not exist until its created. Create it with the following content.

Alternative: Use the /boot/config.txt file from a working Raspbian install
Here is a file from a working Raspbian install. If the above does not work, this one should.

/boot/cmdline.txt
This file will not exist until its created. Create it with the following content.

This boots the Pi with a kernel command line of

8250.nr_uarts=0 cma=256M@256M dma.dmachans=0x7f35 bcm2708_fb.fbwidth=1920 bcm2708_fb.fbheight=1080 bcm2709.boardrev=0xa02082 bcm2709.serial=0x8e2830fe smsc95xx.macaddr=B8:27:EB:28:30:FE bcm2708_fb.fbswap=1 bcm2709.uart_clock=48000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000 root=/dev/mmcblk0p3 rootfstype=ext4 rootwait

It really is one long line.

Alternative: Use the /boot/cmdline.txt file from a working Raspbian install
Here is the file from a working Raspbian install. If the above does not work, this one should. Be sure to set root to your root partition, and rootfstype to your partition type. and  are shown below as examples.

Setting the console keymap
This step is optional if you can log in using the default

keymap="us"

Set the keymap to something you use, e.g.

Boot the Pi to test
Unmount the microSD card.

When the prompt returns, move the microSD card to the Raspberry Pi and power on.

For 10 seconds (it seems much longer) you should see the GPU 'Rainbow' test pattern, then the familiar boot messages.

Log in at the Pi console. Nothing was added to any runlevels during the install, so networking was not started, nor anything that depends on networking, like ntpd and sshd.

The Pi does not have a hardware real time clock. Its time will be Jan 1, 1970.

What next
As always with Gentoo, if it booted, that's the hard bit done.

All The setup steps in the Gentoo Handbook Fix the MAC address or use a static IP Allow root logins via ssh Add a crond, a logger and other things the handbook does before the reboot. Add Kernel Sources (or at least the .config)

WiFi and Bluetooth
It's unlikely that WiFi or Bluetooth will work at first boot. Expect to add some control tools for both.

CFLAGS
CFLAGS="-march=armv8-a+crc -mtune=cortex-a53 -O2 -pipe -fomit-frame-pointer"

gcc-6.x allows the use of  but that will prevent the use of distcc. The above is the same as gcc-6.3 would set for  anyway.

MAKEOPTS
With only 1G RAM, and four cores, the conventional  is a bit aggressive for building larger things. It will force swapping or even appear to lock up the Pi completely, to the point where it won't even respond to the console.

Use files in and entries in  to set MAKEOPTS on a per package basis.

Networking
Note that is not in the stage 3, nor is   in the default runlevel.

Install netifrc, dhcpcd, and add to openrc.

Depending on user requirements set the eth0 configuration.

or

sshd
The default configuration for will not allow password based root logins. A few possible solutions exist to this issue:


 * 1) Add a normal ssh public key for the root user. This involves logging in as the root user, then generating the keys.
 * 2) Add a normal user so the wheel group. Connect via SSH as this user, then use  or  to gain root privileges.
 * 3) Edit the  in order to allow password based root logins. This method of resolution is insecure and not recommended.

Updating the tool chain
Once you boot, you may have the desire to update  first thing. However, as of the time of this writing, the latest stage3 for was built in December 2016. A lot of things in the tool chain will be out of date with what is on the current Portage tree. Once you've booted the Raspberry Pi and confirmed that you have an internet connection, you'll want to first run to get the absolute latest tree, then run  to get all of your Perl packages up to date.

Network time sync
The Raspberry Pi 3 does not have a hardware real time clock on board. There are vendors online where you can order RTC modules made for the Pi, but if you don't plan to run one, I highly recommend installing a NTP client.

First, we'll set the initial time using the command. Date and time will be entered in  format and the time is in 24-hr format -

As an example, if the time is 10:05PM on 7/31/2017 -

As with most things Gentoo, the NTP daemon is just an emerge away -

Or you can also use busybox variant:

Don't forget to edit the config file:

And start it:

Remove the hardware clock service hwclock from the boot runlevel and replace it with the software clock service swclock -

Make sure you have the correct time zone set to the area which most closely matches your locale in -

As an example, if you live in California, you would do -

Install your timezone libraries -

Start the NTP client and add it to the default runlevel -

No video output
It may be an issue with the boot config. Useful if using an DVI to HDMI cable.

Not booting
USB Booting is not enabled by default, it will need to be enabled.

Where to get help
On Internet Relay Chat

On the Gentoo Forums, start a new topic in the Gentoo on ARM forum.

I don't mind a PM on the forums with a link to your post. I don't do one to one help via email or the forums PM system. You will either get no response at all or a request to make a public post. That way others may learn from your misfortune.

Acknowledgements
Everyone contributing to the arm64 software base.

Especially Sakaki, who showed the way on the final steps.