Run bare-metal program on VisionFive2

A little bit context about what I am going to do. Previously I bought a Nezha D1 SBC, and managed to port rv6 educational kernel on it.

Recently I got a VisionFive2 board and plan to do it the same but use Zig rather than C.

The first issue I encountered is how to quickly load my kernel to the board and up & running. Previously on Nezha, they have xfel tool to easily do that. After research, I found UART boot may be an option.

At the very beginning, I tried to replace VisionFive2 uboot SPL and write my spl implementing Xmodem. and it can help receive my kernel binary and move it to DRAM at 0x40000000. The simple idea took me a couple of days and I have done the xmodem part, but had no documentation to guide me how to initialise DRAM, and other peripherals, although I can find some clues from visionfive2 uboot repo. The documentation about system control registers is too short and not too much helpful. Here is my attempt: rvz/splz at master · chaoyangnz/rvz · GitHub

Then when I found uboot actually has a UART boot supporting YModem. Once I got this, things become simple.

I added CONFIG_SPL_YMODEM_SUPPORT=y in starfive_visionfive2_defconfig, and then re-build u-boot, it should be generating u-boot/spl/u-boot-spl.bin, which now supports YModem transmit.

Use spl_tool to add header: spl_tool -c -f spl/u-boot-spl.bin

Now the generated u-boot-spl.bin.normal.out can be loaded in UART via Xmodem when you see polling character CCCC.

Once the spl is loaded, it will be waiting you to load your kernel using Ymodem.

I just made a tool to automate the loading. You can download the CLI in rvz/tools/vf2-programmer/vf2-programmer at master · chaoyangnz/rvz · GitHub

Usage:
vf2-programmer -h

Example:
vf2-programmer -l 1 -t -f your_kernel.bin

Then your kernel is running at 0x40000000 now.

Hopefully it helps for whoever playing with bare metal and also hope StarFive to share more documentation and clearly reveal some internals.

8 Likes

I am interested in xv6/rv6 but am not familiar with bare-metal programming. I am trying to understand xv6. I don’t want to try an new programming language like Zig. could you provide a link to your kernel and more detailed instructions on how to modify u-boot-spl.bin. normal.out to load it.

I’d start by [reading the Massachusetts Institute of Technology Parallel and Distributed Operating Systems groups free ebook about xv6-riscv-book.

1 Like

I have read the MIT xv6 book, the commentary on the Unix V6 source code and watched the YouTube videos. I have xv6-public running on a small laptop and am working on the MIT lab exercises. I would like to put xv6 on my VisionFive2, but would like a copy of the xv6 kernel and more detailed instructions on how to load it onto the VF2

1 Like

@kengreen original xv6 riscv is running on QEMU. So if you need to run on a new board, you have to do some initialization and tweak something to adapt the board.

You just download my tool vs2-programmer (which included uboot-spl.normal.out) and upload your kernel to visionfive2 DRAM and test & run.

As I am still working in progress to port xv6, I am unable to provide you the ported source.

Is that clear?

3 Likes

Yes, I understand that I will need to include initialization and some other tweaks in the kernel to replace QEMU running under linux. I have downloaded your vf2-programmer and vf2-uboot-fitimager to my VF2. I also have xv6-riscv , riscv-gnu-toolchain and riscv-linux-gnu-gcc. Thanks for your efforts. I look forward to your success.