Bringing up BPI-F3 - Part 1
Tags: riscv, gentoo, bpif3
Published: 2024-05-19 15:05:09

> I like a lot non-x86 architectures, I still enjoy writing software for PowerPC even nowadays and I'm looking forward to see how SVE will be once there is hardware in the right price range. 
> Today it is the turn of RISC-V since there is eventually a, hopefully, nice board sporting a cpu with the RISC-V Vector Extension 1.0 that is now part of the [RISC-V](https://riscv.org/technical/specifications/).

## Where to start

The official documentation is available [here](https://docs.banana-pi.org/en/BPI-F3/BananaPi_BPI-F3).

The board comes with an empty eMMC, so either you start with a SD, or you need to upload a valid image using [dfu](https://docs.u-boot.org/en/latest/usage/dfu.html).

Sadly the usb-c does not multiplex also the serial, so you'll need to connect the usual 3 pins with a 3.3V usbserial.

```
ERROR:   CMD8
ERROR:   sd f! l:76
bm:0
ERROR:   emmc: invalid bootinfo magic code:0x0
ERROR:   invalid bootinfo image.
ERROR:   entering download mode.
ROM: usb download handler
usb2d_initialize : enter
Force usb FS mode!
Controller Run
usb rst int
SETUP: 0x0 0x5 0x1
SETUP: 0x80 0x6 0x100
SETUP: 0x80 0x6 0x100
SETUP: 0x80 0x6 0x302
SETUP: 0x80 0x6 0x302
SETUP: 0x80 0x6 0x301
SETUP: 0x80 0x6 0x301
SETUP: 0x80 0x6 0x30a
SETUP: 0x80 0x6 0x30a
SETUP: 0x80 0x6 0x200
SETUP: 0x80 0x6 0x200
SETUP: 0x80 0x6 0x300
```

Their images are Debian derivatives, I prefer [Gentoo](https://gentoo.org) and since we have [stage3](http://distfiles.gentoo.org/releases/riscv/autobuilds/) for `rv64_lp64d-openrc`, I guess I can give it a try.

Vendor-sources for kernel and bootloaders are available and I can piggy back over that image if I stumble over [opensbi](https://github.com/BPI-SINOVOIP/pi-opensbi/tree/v1.3-k1) or [u-boot](https://github.com/BPI-SINOVOIP/pi-u-boot/tree/v2022.10-k1) or I run out of time sorting out the [patches for linux-6.1.15](https://github.com/BPI-SINOVOIP/pi-linux/tree/linux-6.1.15-k1)

## Prerequisites

- [wezterm](https://github.com/wez/wezterm) or [screen](https://www.gnu.org/software/screen/) to see what's going on

- [u-boot-tools](https://packages.gentoo.org/packages/dev-embedded/u-boot-tools) are needed since the kernel image will  would come in a [FIT](https://docs.u-boot.org/en/latest/usage/fit/index.html) image.

- The [armbian](https://github.com/BPI-SINOVOIP/armbian-build/commits/v24.04.30/) tree or any of the pre-made builds provided.

- A recent [stage3](http://distfiles.gentoo.org/releases/riscv/autobuilds/)

## Lazy Setup

The base images share this layout:

```
Number  Start (sector)    End (sector)  Size       Code  Name
   1             256             767   256.0 KiB   8300  fsbl
   2             768             895   64.0 KiB    8300  env
   3            2048            4095   1024.0 KiB  8300  opensbi
   4            4096            8191   2.0 MiB     8300  uboot
   5            8192          532479   256.0 MiB   8300  bootfs
   6          532480              <>   <> GiB     8300  rootfs
``` 

With the `rootfs` taking the space it needs.

The easiest way is to copy the first 5 partitions to the eMMC 

```
# dd if=/dev/mmcblk0 of=/dev/mmcblk2 bs=1M count=259
```

And then recreate the `rootfs` partition.

> [!note] the initramfs does not support btrfs, nor the u-boot configuration. Until kernel and initramfs are replaced it is safer to stick to ext4 (I made this mistake).

Then the usual manual unpack of a `stage3` and chroot described in [Gentoo Handbook](https://wiki.gentoo.org/wiki/Handbook:Main_Page) works.

## Pitfalls known so far

- Make sure to enable the serial ports in `/etc/inittab`
```
s0:12345:respawn:/sbin/agetty -L 115200 ttyS0 vt100
s1:12345:respawn:/sbin/agetty -L 115200 ttyS1 vt100
```

- The default `u-boot` command script is incomplete, if you want to use the nvme, you have to enter the u-boot console (press `s`)
```
=> nvme scan
=> run nor_boot
```

## Coming next

I hadn't tried yet to:
- update u-boot and change a bit its configuration
- build a new kernel
- put everything together to have btrfs on nvme