Flashing ArchLinux RISC-V to Spacemit K1

dark_mode format_list_bulleted
Flashing ArchLinux RISC-V to Spacemit K1
Packing the ArchLinux RISC-V porting image and flash it to the eMMC on board or NVMe disk.
history
History
archlinux
riscv
spacemit
visibility public group

NOTE for lazy person: I’ve packed the ROM package for eMMC with

  • Kernel 6.1.15 version: Build the images as following by yourself if you want newer.
  • /boot/bianbu.bmp is the boot splash image of ArchLinux
  • /etc/resolve.conf with nameserver 1.1.1.1
  • /etc/systemd/network/0-end.network with DHCP configuration set up

Build the images by yourself

Felix Yan’s wiki about the ArchLinux RISC-V porting.

postmarketOS Wiki about Banana Pi F3 with Spacemit K1 SoC.

Banana Pi forum question about booting with root filesystem on NVMe.

Make the ext4 rootfs image

Download the ArchLinux RISC-V porting rootfs archive here.

# Allocate image file. Count defined by your eMMC free size.
dd if=/dev/zero of=rootfs.ext4 bs=1G count=14

# Copy file system tree.
mkfs.ext4 rootfs.ext4
mount rootfs.ext4 /mnt
cp -rp .../archriscv/* /mnt/
umount /mnt

Modify the fstab

The rootfs may will be mounted as read-only due to filesystem check. If it happens, remount it with option -o rw,remount then modify the fstab.

/dev/mmcblk2p5 /boot ext4 defaults,nofail 0 0
/dev/mmcblk2p6 /     ext4 defaults,rw     0 0

Replace boot splash image (optional)

The splash image file is in the BMP bitmap format by Microsoft and location specified by the variable defined under U-Boot: splashfile which is usually bianbu.bmp on bootfs.ext4.

Fetch the U-Boot and kernel

Download the Bianbu distro image archive at archive.spacemit.com. I chose this.

[!NOTE]

Different release version has different kernel version.

It looks like:

.
├── bootfs.ext4
│     ^ /dev/mmcblk2p5
├── env.bin
├── factory
│        ├── bootinfo_emmc.bin
│        ├── bootinfo_sd.bin
│        ├── bootinfo_spinand.bin
│        ├── bootinfo_spinor.bin
│        └── FSBL.bin
├── fastboot.yaml
├── fw_dynamic.itb
├── genimage.cfg
├── partition_2M.json
├── partition_flash.json
├── partition_universal.json
├── rootfs.ext4
│     ^ /dev/mmcblk2p6: The image to be replaced.
└── u-boot.itb

Replace the rootfs.ext4 with the image you made.

Kernel modules

Copy all modules under /lib/modules from Bianbu’s rootfs.ext4 image to ArchLinux’s.

Some Bianbu releases does not contain kernel modules such as [ btrfs, xfs ] in the init ramdisk. Instead, they are stored on root filesystem. So you can only use ext4 for your root filesystem unless you build the kernel that with the modules static-linked in.

U-Boot environment variables

Force U-Boot falling back to prompt by removing SD Card from slot while U-Boot is booting.

env print -a

Option: Specify the root filesystem such as partition on NVMe

[!TIP]

Spacemit’s firmware does not support boot from NVMe. The options are 「eMMC」「SDcard」and「NAND/NOR SPI flash」only.

If you want to specify which partition or device should the kernel mounts as root filesystem, you have to append the lines below before the end of /boot/env_k1-x.txt.

lets_boot=setenv bootargs earlyprintk quiet clk_ignore_unused swiotlb=65536 workqueue.default_affinity_scope=system earlycon=sbi console=ttyS0,115200n8 loglevel=8 rdinit=/init rootwait rootfstype=ext4 root=${ROOT_FS}
autoboot=run lets_boot; run detect_dtb; run loadknl; run loaddtb; run loadramdisk; run start_kernel;

Substitute ${ROOT_FS} with the target partition or device.

  • /dev/mmcblk2p6
  • /dev/nvme0n1p1

Example: Specify the root as /dev/nvme0n1p1

The root partition filesystem has to be ext4 because the initramfs does not contain kernel module for btrfs.

The kernel on /dev/mmcblk2p5 (which is from bootfs.ext4) will mount /dev/nvme0n1p1 as the root filesystem.

Troubleshooting

Q: The U-Boot env var and boot args does not work on other Bianbu release.

A: For other Bianbu release version, try checking the U-Boot environment variables and the related booting commands and make effective one.

Help: How to get the boot args?

It’s /proc/cmdline.

cat /proc/cmdline

Help: How to inspect U-Boot environment variables so that I can set correct boot args and command sequence?

Make sure your board can NOT boot from any boot options so that it will fallback to U-Boot shell.

# This command will print all env variables.
==> env print

Choice 1: Flashing to SD Card

Use the genimage to generate sdcard image.

# Installation, for NixOS user.
nix-shell -p genimage

# Generate image.
cd .../archriscv
ln -s . input
genimage

# Write to the microSD.
dd if=./images/...-sdcard.img of=/dev/mmcblk2 bs=4096 status=progress

Choice 2: Flashing to eMMC

The SoC will load one program, for processing packets from the host about firmware flashing.

The tool is flashing the target device via executing fastboot commands. You may have heard of it on Android.

Requires fastboot CLI utility.

Flash tool

The board supports flashing firmware in fastboot mode.

The utilities below are required:

fastboot

Connect to the board

Connect USB port on the board to your computer. It will show as a DFU device, and turn to U-Boot USB download gadget while flashing.

lsusb
Bus 001 Device 009: ID 361c:1001 U-Boot USB download gadget

Help: Allow unprivileged users to access the board on DFU mode

Make the users in the group plugdev able to access it.

nano /etc/udev/rules.d/99-spacemit.rules
SUBSYSTEMS=="usb", ATTR{idVendor}=="361c", ATTR{idProduct}=="1001", GROUP="plugdev", MODE:="0660"

Flash images

[!TIP]

For BPi-F3 board: Press and hold the [FDL] and [RST] buttons in order to enter firmware update mode.

fastboot stage factory/FSBL.bin
fastboot continue
fastboot stage u-boot.itb
fastboot continue
fastboot flash gpt partition_universal.json
fastboot flash bootinfo factory/bootinfo_sd.bin
fastboot flash fsbl factory/FSBL.bin
fastboot flash env env.bin
fastboot flash opensbi fw_dynamic.itb
fastboot flash uboot u-boot.itb
fastboot flash bootfs bootfs.ext4
fastboot flash rootfs rootfs.ext4

It takes about 100 seconds when the rootfs.ext4 image is 2GiB.

Enjoy!

ArchLinux is now available on your board.

Reset your board. ArchLinux will boot!

The password of root is archriscv by Felix Yan.

Start services

Network

systemctl enable --now systemd-networkd systemd-resolved

Time Sync

systemctl enable --now systemd-timesyncd

[!NOTE]

SSL connections only work with correct time.

Install packages

pacman -Syu
pacman -S nano openssh

Choose the best mirror

nano /etc/pacman.d/mirrorlist
##
## Arch Linux RISC-V repository mirrorlist
##

## Worldwide
Server = https://riscv.mirror.pkgbuild.com/repo/$repo

## Canada
#Server = https://archriscv.felixc.at/repo/$repo

## China
#Server = https://mirror.iscas.ac.cn/archriscv/repo/$repo
#Server = https://mirrors.sustech.edu.cn/archriscv/repo/$repo
#Server = https://mirror.nju.edu.cn/archriscv/repo/$repo
#Server = https://mirrors.wsyu.edu.cn/archriscv/repo/$repo

## Finland
#Server = https://mirrors.felixc.at/archriscv/repo/$repo

Desktop Environment

The graphics performance is poor with llvmpipe.

But do not worry about it.

It’s not much better when using hardware acceleration.

KDE

pacman -S icu plasma-desktop plasma-nm plasma-pa plasma-workspace plasma-workspace-wallpapers xdg-desktop-portal-kde dolphin kate konsole okular spectacle
dbus-run-session startplasma-wayland

Xfce4

pacman -S icu xfce4 xorg
dbus-run-session startxfce4

GNOME

pacman -S gnome

FAQ

Missing drivers

Official documentation about building drivers and libraries on Spacemit K1.

About the graphics hardware acceleration

The SoC integrated Imagination BXE-2-32 without opensource driver yet.

I haven’t made them working in success after built on ArchLinux. I gave up discovering this valueless platform.

[!TIP] If somebody has the solution please publish it in the remark area. Thanks!

The Chinese capital Canyon Bridge Capital has been the current parent company of Imagination since 2017.

The alternative is the software implementation llvmpipe.

About the media hardware codec

Spacemit has designed their own media API for their codec. They called it Multi Processing Platform (MPP).

They supported this media API in FFmpeg and Gstreamer on their distro Bianbu. I really couldn’t understand: why didn’t they make it VAAPI-compatible?

What do I use it for?

It’s affording some functionalities of this site with services proxied via Cloudflare Daemon running on it. Such as statistics and web traffic analysis.

Remark