User:Penguin/BeagleV-Fire-Gentoo
Preface
This guide is written in a way that forces the reader to manually perform every step, excluding environment variables via the setup script because I want to be able to change those. If you would like to follow the official guide provided by beagleboard, which is far more streamlined and automated, please see the references at the bottom of this document.
WARNING This guide is very much still a work in progress. Do not expect anything to work yet.
Prerequisites
emerge -a sys-block/bmap-tools sys-fs/genimage dev-libs/libyaml sys-fs/mtools mkdir beaglev-fire-gentoo && cd beaglev-fire-gentoo export PROJECT_ROOT="$PWD" # the target directory will be where we store relevant parts of our target image before we turn it into an image # the boot dir will contain the linux image, hss payloads, and anything else needed to boot mkdir -p target/boot target/rootfs # The tools directory will contain anything needed to generate pieces of the image. The HSS payload generator is one example of this mkdir tools # We will store any temporary downloads we need here, like the stage3 tarball mkdir downloads
Using the riscv toolchain provided via crossdev
Crossdev is required for this step. If you don’t have crossdev set up, use this guide to install it. At the time of writing this, crossdev installs riscv64-unknown-linux-gnu-gcc 13.2.1. This is note worthy because the official beagleboard guide uses 11.4. If there are issues with this toolchain, use the precompiled toolchain via these steps.
crossdev -t riscv64-unknown-linux-gnu export RISCV64_CC="riscv64-unknown-linux-gnu-"
Using a precompiled toolchain
mkdir mirror mkdir toolchain export GCC_VERSION=11.4.0 wget -c --directory-prefix=./mirror/ "https://mirrors.edge.kernel.org/pub/tools/crosstool/files/bin/x86_64/${GCC_VERSION}/x86_64-gcc-${GCC_VERSION}-nolibc-riscv64-linux.tar.xz" tar xvf ./mirror/x86_64-gcc-${GCC_VERSION}-nolibc-riscv64-linux.tar.xz --strip-components=2 -C ./toolchain/ export RISCV64_CC="$PWD/toolchain/bin/riscv64-linux-"
From this point forward, instructions will use the RISCV64_CC
variable as the toolchain.
WARNING: Using the precompiled toolchain instead of the toolchain provided by crossdev means the chroot and qemu steps likely won’t work. More information on this will be added later, but in the meantime just know that if you don’t use the crossdev toolchain, you’ll need to skip cross emerging and chrooting. This just means you’ll need to compile and configure on the device instead after the first boot. Using the toolchain provided by crossdev is highly recommended.
Export some other misc. environment variables:
export HSS_BRANCH="v2023.02" export HSS_REPO="https://github.com/polarfire-soc/hart-software-services.git" export UBOOT_BRANCH="linux4microchip+fpga-2023.02" export UBOOT_REPO="https://github.com/polarfire-soc/u-boot.git" export GIT_DEPTH=20 export CORES=$(getconf _NPROCESSORS_ONLN)
Configuring QEMU-user and binfmt
In a perfect world, producing a rootfs from scratch would simply mean downloading the tarball, unwrapping it into the destined rootfs folder, and running a few scripts to set it up. However, sometimes it can be useful to install a package or configure the system before deploying it to the target device. We can use qemu-user to chroot into our target system to do these things.
Prepare to install QEMU
echo 'QEMU_SOFTMMU_TARGETS="riscv64 x86_64"' >> /etc/portage/make.conf echo 'QEMU_USER_TARGETS="riscv64"' >> /etc/portage/make.conf echo app-emulation/qemu static-user >> /etc/portage/package.use/qemu
Install QEMU
emerge --ask --update --newuse --deep app-emulation/qemu
Now create a binary package for QEMU. This is done so that we can install it into our target system to run chroot properly.
quickpkg app-emulation/qemu
Non-root users will need access to use qemu.
usermod -aG kvm,qemu $USER
Specifying a riscv64 binfmt-misc handler for systemd-binfmt
Option A: Using the default binfmt configuration file shipped with qemu
“For the systemd-binfmt service, add files containing the desired handler registration strings under etc/binfmt.d. Modern versions of qemu ship a binfmt configuration file that supports all binary formats. Simply link it to /etc for binary format support, then skip the following manual file creation steps.” - Gentoo Embedded Handbook
ln -s /usr/share/qemu/binfmt.d/qemu.conf /etc/binfmt.d/qemu.conf
This supports all default architectures in the default qemu.conf
. If you only want to support riscv64, use the instructions below.
Option B: Adding support for just riscv64
echo -E ':riscv64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-riscv64:' > /etc/binfmt.d/qemu-riscv64-static.conf
Now restart the service to register the binary formats:
systemctl restart systemd-binfmt
See the chroot instructions below
Clone & Build Heart Software Services (HSS)
Clone HSS
if [ -d ./hart-software-services/ ] ; then rm -rf ./hart-software-services/ || true fi git clone -b ${HSS_BRANCH} ${HSS_REPO} ./hart-software-services/ --depth=${GIT_DEPTH}
Build HSS
make -C hart-software-services/tools/hss-payload-generator/ clean make -C hart-software-services/tools/hss-payload-generator/
Clone & Build u-boot
Clone u-boot
# remove old if it exists, optional if [ -d ./u-boot ] ; then rm -rf ./u-boot || true fi git clone -b ${UBOOT_BRANCH} ${UBOOT_REPO} ./u-boot/ --depth=${GIT_DEPTH}
Download the patches
The patches currently exist as part of BeagleV’s BeagleV-Fire-ubuntu
repo, so we’ll just clone the whole thing for now. Eventually, I’ll see if I can mirror just the patches somewhere.
git clone https://git.beagleboard.org/beaglev-fire/BeagleV-Fire-ubuntu.git ./beaglev --depth=${GIT_DEPTH}
WIP Build u-boot
# Apply patches to uboot cp -v ./beaglev/patches/u-boot/beaglev-fire/microchip_mpfs_icicle.h ./u-boot/include/configs/microchip_mpfs_icicle.h cp -v ./beaglev/patches/u-boot/beaglev-fire/microchip-mpfs-icicle-kit.dts ./u-boot/arch/riscv/dts/ cp -v ./beaglev/patches/u-boot/beaglev-fire/microchip_mpfs_icicle_defconfig ./u-boot/configs/microchip_mpfs_icicle_defconfig make -C u-boot ARCH=riscv CROSS_COMPILE=${RISCV64_CC} microchip_mpfs_icicle_defconfig # if you want to make configurations.... # make -C u-boot ARCH=riscv CROSS_COMPILE=${RISCV64_CC} menuconfig make -C u-boot ARCH=riscv CROSS_COMPILE=${RISCV64_CC} olddefconfig make -C u-boot ARCH=riscv CROSS_COMPILE=${RISCV64_CC} savedefconfig cp -v ./u-boot/defconfig ./u-boot/configs/microchip_mpfs_icicle_defconfig cp -v ./u-boot/defconfig ./beaglev/patches/u-boot/beaglev-fire/microchip_mpfs_icicle_defconfig make -C u-boot -j${CORES} ARCH=riscv CROSS_COMPILE=${RISCV_CC} all # copy the generated uboot into our boot folder cp -v ./u-boot/u-boot.bin ./target/boot/u-boot.bin # i have no idea why 2 copies are being made, one u-boot.bin and one src.bin cp -v ./u-boot/u-boot.bin ./target/boot/src.bin
TODO Build the Linux kernel
cd ./beaglev/linux/ if [ ! -f ./.patched ] ; then if [ -f arch/riscv/configs/mpfs_defconfig ] ; then git am ../patches/linux/0002-PCIe-Change-controller-and-bridge-base-address.patch git am ../patches/linux/0003-GPIO-Add-Microchip-CoreGPIO-driver.patch git am ../patches/linux/0004-ADC-Add-Microchip-MCP356X-driver.patch git am ../patches/linux/0005-Microchip-QSPI-Add-regular-transfers.patch git am ../patches/linux/0006-BeagleV-Fire-Add-printk-to-IM219-driver-for-board-te.patch git am ../patches/linux/0007-MMC-SPI-Hack-to-support-non-DMA-capable-SPI-ctrl.patch git am ../patches/linux/0008-Add-wireless-regdb-regulatory-database-file.patch git am ../patches/linux/0009-Makefile-build-mpfs-beaglev-fire.dtb.patch fi touch .patched fi if [ -f arch/riscv/configs/mpfs_defconfig ] ; then cp -v ../device-tree/src/microchip/mpfs-beaglev-fire.dts arch/riscv/boot/dts/microchip/ cp -v ../device-tree/src/microchip/mpfs-beaglev-fire-fabric.dtsi arch/riscv/boot/dts/microchip/ fi echo "make ARCH=riscv CROSS_COMPILE=${RISCV64_CC} clean" make ARCH=riscv CROSS_COMPILE=${RISCV64_CC} clean if [ -f arch/riscv/configs/mpfs_defconfig ] ; then cp -v ../patches/linux/mpfs_defconfig ./arch/riscv/configs/mpfs_defconfig echo "make ARCH=riscv CROSS_COMPILE=${RISCV64_CC} mpfs_defconfig" make ARCH=riscv CROSS_COMPILE=${RISCV64_CC} mpfs_defconfig ./scripts/config --set-str CONFIG_LOCALVERSION "-$(date +%Y%m%d)" ./scripts/config --enable CONFIG_CRYPTO_USER_API_HASH ./scripts/config --enable CONFIG_CRYPTO_USER_API_SKCIPHER ./scripts/config --enable CONFIG_KEY_DH_OPERATIONS ./scripts/config --enable CONFIG_CRYPTO_ECB ./scripts/config --enable CONFIG_CRYPTO_MD4 ./scripts/config --enable CONFIG_CRYPTO_MD5 ./scripts/config --enable CONFIG_CRYPTO_CBC ./scripts/config --enable CONFIG_CRYPTO_SHA256 ./scripts/config --enable CONFIG_CRYPTO_AES ./scripts/config --enable CONFIG_CRYPTO_DES ./scripts/config --enable CONFIG_CRYPTO_CMAC ./scripts/config --enable CONFIG_CRYPTO_HMAC ./scripts/config --enable CONFIG_CRYPTO_SHA512 ./scripts/config --enable CONFIG_CRYPTO_SHA1 echo "make -j${CORES} ARCH=riscv CROSS_COMPILE=${RISCV64_CC} olddefconfig" make -j${CORES} ARCH=riscv CROSS_COMPILE=${RISCV64_CC} olddefconfig else echo "make ARCH=riscv CROSS_COMPILE=${RISCV64_CC} defconfig" make ARCH=riscv CROSS_COMPILE=${RISCV64_CC} defconfig ./scripts/config --enable CONFIG_PCIE_MICROCHIP_HOST ./scripts/config --enable CONFIG_OF_OVERLAY ./scripts/config --enable CONFIG_I2C ./scripts/config --enable CONFIG_EEPROM_AT24 ./scripts/config --enable CONFIG_I2C_MICROCHIP_CORE ./scripts/config --enable CONFIG_SPI_MICROCHIP_CORE ./scripts/config --enable CONFIG_SPI_MICROCHIP_CORE_QSPI ./scripts/config --module CONFIG_SPI_SPIDEV ./scripts/config --enable CONFIG_GPIO_SYSFS ./scripts/config --enable CONFIG_HW_RANDOM_POLARFIRE_SOC ./scripts/config --enable CONFIG_USB_MUSB_HDRC ./scripts/config --enable CONFIG_NOP_USB_XCEIV ./scripts/config --enable CONFIG_USB_MUSB_POLARFIRE_SOC ./scripts/config --enable CONFIG_USB_MUSB_DUAL_ROLE ./scripts/config --enable CONFIG_MAILBOX ./scripts/config --enable CONFIG_POLARFIRE_SOC_MAILBOX ./scripts/config --disable CONFIG_SUN6I_MSGBOX ./scripts/config --enable CONFIG_REMOTEPROC ./scripts/config --enable CONFIG_REMOTEPROC_CDEV ./scripts/config --enable CONFIG_POLARFIRE_SOC_SYS_CTRL ./scripts/config --enable CONFIG_USB_GADGET ./scripts/config --enable CONFIG_USB_CONFIGFS ./scripts/config --enable CONFIG_CONFIGFS_FS ./scripts/config --enable CONFIG_USB_CONFIGFS_SERIAL ./scripts/config --enable CONFIG_USB_CONFIGFS_ACM ./scripts/config --enable CONFIG_USB_CONFIGFS_OBEX ./scripts/config --enable CONFIG_USB_CONFIGFS_NCM ./scripts/config --enable CONFIG_USB_CONFIGFS_ECM ./scripts/config --enable CONFIG_USB_CONFIGFS_ECM_SUBSET ./scripts/config --enable CONFIG_USB_CONFIGFS_RNDIS ./scripts/config --enable CONFIG_USB_CONFIGFS_EEM ./scripts/config --enable CONFIG_USB_CONFIGFS_PHONET ./scripts/config --enable CONFIG_USB_CONFIGFS_MASS_STORAGE ./scripts/config --enable CONFIG_USB_CONFIGFS_F_LB_SS ./scripts/config --enable CONFIG_USB_CONFIGFS_F_FS ./scripts/config --enable CONFIG_USB_CONFIGFS_F_UAC1 ./scripts/config --enable CONFIG_USB_CONFIGFS_F_UAC2 ./scripts/config --enable CONFIG_USB_CONFIGFS_F_MIDI ./scripts/config --enable CONFIG_USB_CONFIGFS_F_HID ./scripts/config --enable CONFIG_USB_CONFIGFS_F_UVC ./scripts/config --enable CONFIG_USB_CONFIGFS_F_PRINTER ./scripts/config --module CONFIG_MEDIA_SUPPORT ./scripts/config --enable CONFIG_MEDIA_SUPPORT_FILTER ./scripts/config --enable CONFIG_MEDIA_SUBDRV_AUTOSELECT ./scripts/config --enable CONFIG_MEDIA_CAMERA_SUPPORT ./scripts/config --module CONFIG_VIDEO_IMX219 ./scripts/config --module CONFIG_IIO #Cleanup large DRM... ./scripts/config --disable CONFIG_DRM ./scripts/config --disable CONFIG_DRM_RADEON ./scripts/config --disable CONFIG_DRM_NOUVEAU ./scripts/config --disable CONFIG_DRM_SUN4I #Optimize: ./scripts/config --enable CONFIG_IP_NF_IPTABLES ./scripts/config --enable CONFIG_NETFILTER_XTABLES ./scripts/config --enable CONFIG_NLS_ISO8859_1 ./scripts/config --enable CONFIG_BLK_DEV_DM ./scripts/config --set-str CONFIG_LOCALVERSION "-$(date +%Y%m%d)" echo "make -j${CORES} ARCH=riscv CROSS_COMPILE=${RISCV64_CC} olddefconfig" make -j${CORES} ARCH=riscv CROSS_COMPILE=${RISCV64_CC} olddefconfig fi echo "make -j${CORES} ARCH=riscv CROSS_COMPILE=${RISCV64_CC} Image modules dtbs" make -j${CORES} ARCH=riscv CROSS_COMPILE="ccache ${RISCV64_CC}" Image modules dtbs if [ ! -f ./arch/riscv/boot/Image ] ; then echo "Build Failed" exit 2 fi KERNEL_UTS=$(cat "${PROJECT_ROOT}/linux/include/generated/utsrelease.h" | awk '{print $3}' | sed 's/\"//g' ) if [ -d "${PROJECT_ROOT}/deploy/tmp/" ] ; then rm -rf "${PROJECT_ROOT}/deploy/tmp/" fi mkdir -p "${PROJECT_ROOT}/deploy/tmp/" make -s ARCH=riscv CROSS_COMPILE=${RISCV64_CC} modules_install INSTALL_MOD_PATH="${PROJECT_ROOT}/deploy/tmp" if [ -f "${PROJECT_ROOT}/deploy/${KERNEL_UTS}-modules.tar.gz" ] ; then rm -rf "${PROJECT_ROOT}/deploy/${KERNEL_UTS}-modules.tar.gz" || true fi echo "Compressing ${KERNEL_UTS}-modules.tar.gz..." echo "${KERNEL_UTS}" > "${PROJECT_ROOT}/deploy/.modules" cd "${PROJECT_ROOT}/deploy/tmp" || true tar --create --gzip --file "../${KERNEL_UTS}-modules.tar.gz" ./* cd "${PROJECT_ROOT}/linux/" || exit rm -rf "${PROJECT_ROOT}/deploy/tmp" || true if [ -f arch/riscv/configs/mpfs_defconfig ] ; then cp -v ./.config ../patches/linux/mpfs_defconfig cp -v ./arch/riscv/boot/dts/microchip/mpfs-beaglev-fire.dts ../patches/linux/dts/mpfs-beaglev-fire.dts cp -v ./arch/riscv/boot/dts/microchip/mpfs-beaglev-fire-fabric.dtsi ../patches/linux/dts/mpfs-beaglev-fire-fabric.dtsi else cp -v ./.config ../patches/linux/mainline/defconfig cp -v ./arch/riscv/boot/dts/microchip/mpfs-beaglev-fire.dts ../patches/linux/mainline/dts/mpfs-beaglev-fire.dts cp -v ./arch/riscv/boot/dts/microchip/mpfs-beaglev-fire-fabric.dtsi ../patches/linux/mainline/dts/mpfs-beaglev-fire-fabric.dtsi fi if [ ! -d ../deploy/input/ ] ; then mkdir -p ../deploy/input/ || true fi cp -v ./arch/riscv/boot/Image ../deploy/input/ cp -v ./arch/riscv/boot/dts/microchip/mpfs-beaglev-fire.dtb ../deploy/input/ cd ../ cp -v ./patches/linux/beaglev_fire.its ./deploy/input/ cd ./deploy/input/ gzip -9 Image -c > Image.gz if [ -f ../../u-boot/tools/mkimage ] ; then ../../u-boot/tools/mkimage -f beaglev_fire.its beaglev_fire.itb fi
WIP Generate the HSS payload
cd ./deploy/ if [ -f ./src.bin ] ; then if [ ! -d ./input/ ] ; then mkdir ./input/ fi if [ -f ./input/payload.bin ] ; then rm -rf ./input/payload.bin || true fi ./hss-payload-generator -vv -c config.yaml ./input/payload.bin date unset test_var test_var=$(strings ./u-boot.bin | grep 'U-Boot 20' | head -n1 || true) if [ ! "x${test_var}" = "x" ] ; then echo "[u-boot.bin: ${test_var}]" fi unset test_var test_var=$(strings ./src.bin | grep 'U-Boot 20' | head -n1 || true) if [ ! "x${test_var}" = "x" ] ; then echo "[src.bin: ${test_var}]" fi unset test_var test_var=$(strings ./input/payload.bin | grep 'U-Boot 20' | head -n1 || true) if [ ! "x${test_var}" = "x" ] ; then echo "[payload.bin:${test_var}]" fi fi #
WIP Installing the RootFS
Download and installing a stage tarball
This guide uses the systemd-mergedusr profile by default. Other profiles have not been tested.
wget https://distfiles.gentoo.org/releases/riscv/autobuilds/current-stage3-rv64_lp64d-systemd-mergedusr/stage3-rv64_lp64d-systemd-mergedusr-20231124T170207Z.tar.xz --directory-prefix=./download tar xpvf ./download/stage3-*.tar.xz --xattrs-include='*.*' --numeric-owner -C ./target/rootfs/
Emerge the base system
riscv64-unknown-linux-gnu-emerge -va1 @system --keep-going
Customize & configure the rootfs
Prepare to chroot
cd target mount --bind rootfs rootfs cd rootfs ROOT=$PWD emerge --usepkgonly --oneshot --nodeps qemu mount --bind /proc proc mount --bind /sys sys mount --bind /dev/pts dev/pts mkdir -p var/db/repos/gentoo mount --bind -o ro /var/db/repos/gentoo var/db/repos/gentoo mkdir -p usr/src/linux mount --bind -o ro linux usr/src/linux
Chroot into the target
chroot $PWD /bin/bash source /etc/profile export PS1="(chroot) ${PS1}"