Bringing up BPI-F3 - Part 2
Tags: riscv, gentoo, bpif3
Published: 2024-05-20 19:38:42

This is part2, in [part1](https://dev.to/luzero/bringing-up-bpi-f3-part-1-3bm4) I shown how to get a Gentoo system on the nvme taking some shortcuts. Here are the notes from the second day with some improvements.
 
## Setting up the eMMC

[stintel](https://stijn.tintel.eu/blog/2024/05/19/compiling-uboot-bpi-f3/) already documented how to build uboot and install it, I'm summarizing it as well here.

### Dependencies
We need `dtc` to compile the device-tree and `mkimage` and git if we want to clone the trees.
``` sh
emerge dtc u-boot-tools dev-vcs/git
```

### Bootloader
This board boot process relies on [opensbi](https://github.com/riscv/opensbi) + [u-boot](https://u-boot.org)

- First build opensbi
``` sh
git clone https://github.com/BPI-SINOVOIP/pi-opensbi -b v1.3-k1
cd pi-opensbi
make PLATFORM=generic menuconfig
make -j 8
```
As [stintel](https://stijn.tintel.eu/blog/2024/05/19/compiling-uboot-bpi-f3/) already reported, the default configuration is wrong, so we have to make sure that the `build/platform/generic/kconfig/.config` has the support for K1x:
``` .config
# CONFIG_PLATFORM_SPACEMIT_K1PRO is not set
CONFIG_PLATFORM_SPACEMIT_K1X=y
```
If everything goes well `build/platform/generic/firmware/fw_dynamic.bin` will exist.

- Then build u-boot
``` sh
export OPENSBI=../pi-opensbi/build/platform/generic/firmware/fw_dynamic.bin
git clone https://github.com/BPI-SINOVOIP/pi-u-boot -b v2022.10-k1
cd pi-u-boot
make k1_defconfig
make -j8
```
If everything goes well it will produce the following files 
```
FSBL.bin  
bootinfo_emmc.bin  
bootinfo_sd.bin  
bootinfo_spinand.bin  
bootinfo_spinor.bin 
u-boot.bin
```

### Partitions
The [upstream instructions](https://docs.banana-pi.org/en/BPI-F3/GettingStarted_BPI-F3) suggest to copy over the layout, so the `uboot`, `bootfs`, `rootfs` partitions stay the same, but now the eMMC `fsbl` and `opensbi` are to live in their dedicated `/dev/mmcblk2boot0`. So it is up to you to do what you prefer with them.
Surely you have to setup `/dev/mmcblk2boot0`:
```
echo 0 > /sys/block/mmcblk2boot0/force_ro
dd if=bootinfo_emmc.bin of=/dev/mmcblk2boot0
dd if=FSBL.bin of=/dev/mmcblk2boot0 bs=512 seek=1
```

As **stintel** tested, all that's need is that a partition labeled `uboot` exists and contains `u-boot.bin` and a `bootfs` partition that contains `env_k1-x.txt`. It is a good idea to have `env` and `rootfs` as well.

## Kernel
Building the kernel is straightforward enough:
``` sh
git clone https://github.com/BPI-SINOVOIP/pi-linux -b linux-6.1.15-k1
ln -s pi-linux/ linux
cd linux
zcat /proc/config.gz > .config
make oldconfig # (or menuconfig)
make -j 10 && make modules_install
mount /boot # make sure it picks the right partition
make install
```

> Make sure to enable `HWMON` and `HWMON_NVME` so you'll have the thermal sensors available.

Edit `boot/env_k1-x.txt` accordingly and your new kernel is ready.

> **NOTE**: The initial kernel has at least a known annoying bug and it is missing `HWMON` support for its thermal sensors.
> If you use Rust, you'll notice immediately some strange issue with [futex()](https://bugzilla.kernel.org/show_bug.cgi?id=217923).
> - [here](https://github.com/BPI-SINOVOIP/pi-linux/pull/4) a patch to address the bug backported to the 6.1.15 vendored kernel.
> - [here](https://github.com/BPI-SINOVOIP/pi-linux/pull/3) my tiny patch to get something going regarding HWMON.

## Coming next
- Do not use at all an initrd (right now if you are following along you'd be using the initrd from the starting distro)
- Other u-boot tweaks