User:RemDogKap/SandBox/Build U-Boot and OpenSBI From Source
Placeholder intro and explanation, I'm tired don't want to write now, contributions welcome
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
- Must use linux, can use wsl
Preparing the Build Environment
Fist, install the cross-compiler and if not already installed, git.
$ sudo apt update && sudo apt install gcc-riscv64-linux-gnu git
[Optional] Check the version of the riscv64-linux-gnu-gcc
compiler.
$ riscv64-linux-gnu-gcc -v
The output should looks something like:
Using built-in specs. COLLECT_GCC=riscv64-linux-gnu-gcc COLLECT_LTO_WRAPPER=usr/lib/gcc-cross/riscv64-linux-gnu/10/lto-wrapper Target: riscv64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 10.2.1-6' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-10 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libsanitizer --disable-libquadmath --disable-libquadmath-support --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --without-target-system-zlib --enable-multiarch --disable-werror --disable-multilib --with-arch=rv64imafdc --with-abi=lp64d --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=riscv64-linux-gnu --program-prefix=riscv64-linux-gnu- --includedir=/usr/riscv64-linux-gnu/include --with-build-config=bootstrap-lto-lean --enable-link-mutex Thread model: posix Supported LTO compression algorithms: zlib gcc version 10.2.1 20210110 (Debian 10.2.1-6)
Create or enter the directory where the files used in this guide will be stored. This tutorial will be using the Linux home directory.
Home Directory Example
$ cd ~
New Directory Example
$ mkdir ~/Star64-Firmware $ cd ~/Star64-Firmware/
Clone the StarFive Tools GitHub repository. This will be used to create the .bin.normal.out
SPL image and provide the .its
FIT image tree source file which provides the configuration for the .img
U-Boot image file.
$ git clone --depth 1 https://github.com/starfive-tech/Tools
Compile the SPL Tool used to create the SPL image in the <working_directory>/Tools/spl_tool/
directory using make
.
$ make -C Tools/spl_tool/
U-Boot
Clone the U-Boot GitHub repository. The entire history is not needed (--depth 1
) and we want the Star64 branch (-b Star64
).
$ git clone --depth 1 -b Star64 https://github.com/Fishwaldo/u-boot.git
Enter the U-Boot directory.
$ cd u-boot/
Select the U-Boot configuration for the Star64, marking the architecture as RISC-V (ARCH=riscv
) and using the cross-compiler installed earlier(CROSS_COMPILE=riscv64-linux-gnu-
).
$ make pine64_star64_defconfig ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu-
Finally, compile U-Boot.
$ make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu-
This should create three files.
- The U-Boot binary, later packaged as an OpenSBI payload, located at
<working_directory>/u-boot/u-boot.bin
. - The device tree blob (binary), which contains information on the characteristics of the device and is passed to the operating system instead of hard-coding device configurations into the operating system, and is located at
<working_directory>/u-boot/arch/riscv/dts/pine64_star64.dtb
. - The U-Boot secondary program loader which will later be packaged into the
u-boot-spl.bin.normal.out
file and is located at<working_directory>/u-boot/spl/u-boot-spl.bin
.
OpenSBI
Return to the directory where the files for this build are stored. In this case it is the Linux home directory. Unless you have since changed directories,
$ cd ..
should return to to the directory before the U-Boot section.
Clone the OpenSBI U-Boot repository and enter its directory. The entire history is not needed (--depth 1
).
$ git clone --depth 1 https://github.com/riscv/opensbi.git $ cd opensbi/
Compile OpenSBI using make
. Make sure to set
- the architecture as RISC-V (
ARCH=riscv
) - the compiler as the cross-compiler installed earlier (
CROSS_COMPILE=riscv64-linux-gnu-
) - the platform as generic (
PLATFORM=generic
) - the OpenSBI payload path as the path to the
u-boot.bin
file created in the U-Boot step (FW_PAYLOAD_PATH=<working_directory>/u-boot/u-boot.bin
) - the flattened device tree (device tree blob) as the
pine64_star64.dtb
file created in the U-Boot step (FW_FDT_PATH=<working_directory>/u-boot/arch/riscv/dts/pine64_star64.dtb
) FW_TEXT_START
as0x40000000
(FW_TEXT_START=0x40000000
).
$ make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- PLATFORM=generic FW_PAYLOAD_PATH=<working_directory>/u-boot/u-boot.bin FW_FDT_PATH=<working_directory>/u-boot/arch/riscv/dts/pine64_star64.dtb FW_TEXT_START=0x40000000
Make sure to replace <working_directory>
with the directory created or chosen in Preparing the Build Environment.
Example
$ make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- PLATFORM=generic FW_PAYLOAD_PATH=~/u-boot/u-boot.bin FW_FDT_PATH=~/u-boot/arch/riscv/dts/pine64_star64.dtb FW_TEXT_START=0x40000000
This should create one file, fw_payload.bin
, which contains OpenSBI with U-Boot as the payload. The file is located at opensbi/build/platform/generic/firmware/fw_payload.bin
.
Creating the SPL File
Go to the directory the SPL Tool compiled earlier is located in. If these instructions were followed exactly the following should work:
$ cd ../Tools/spl_tool/
Use the SPL Tool to create the u-boot-spl.bin.normal.out
SPL image.
$ ./spl_tool -c -f <working_directory>/u-boot/spl/u-boot-spl.bin
Make sure to replace <working_directory>
with the directory created or chosen in Preparing the Build Environment.
Example
$ ./spl_tool -c -f ~/u-boot/spl/u-boot-spl.bin
This should produce a u-boot-spl.bin.normal.out
file located at <working_directory>/u-boot/spl/u-boot-spl.bin.normal.out
Creating the fw_payload File
Return to the directory where the files for this build are stored. In this case it is the Linux home directory. Unless you have since changed directories,
$ cd ..
should work.
Enter the <working_directory>/Tools/uboot_its/
directory which holds the .its
FIT image tree source file which provides the configuration for the .img
U-Boot image file.
$ cd Tools/uboot_its/
The process of making the U-Boot image requires the fw_payload.bin
created earlier so copy that to the current directory.
$ cp <working_directory>/opensbi/build/platform/generic/firmware/fw_payload.bin ./
Make sure to replace <working_directory>
with the directory created or chosen in Preparing the Build Environment.
Example
$ cp ~/opensbi/build/platform/generic/firmware/fw_payload.bin ./
Make the star64_fw_payload.img
U-Boot image file using the U-Boot mkimage
tool. The mkimage
tool is located in the <working_directory>/u-boot/tools/
directory but requires the visionfive2-uboot-fit-image.its
file which is located in <working_directory>/Tools/uboot_its/
so should be executed in the uboot_its
directory.
$ <working_directory>/tools/mkimage -f visionfive2-uboot-fit-image.its -A riscv -O u-boot -T firmware star64_fw_payload.img
Make sure to replace <working_directory>
with the directory created or chosen in Preparing the Build Environment.
Example
$ ~/u-boot/tools/mkimage -f visionfive2-uboot-fit-image.its -A riscv -O u-boot -T firmware star64_fw_payload.img
There is currently no .its
file specifically for the Star64 so the VisionFive2's .its
file is used instead as they share a processor.
This should produce a file called star64_fw_payload.img
in the <working_directory>/Tools/uboot_its/
directory
Credits
https://doc-en.rvspace.org/VisionFive2/SWTRM/VisionFive2_SW_TRM/compiling_u-boot_and_kernel%20-%20vf2.html https://wiki.pine64.org/wiki/User:Vitali/Create_a_bootable_Debian/Linux_SDCard_for_the_Pine64/Star64_from_scratch