A new post titled "[GSoC] Better RISC-V support, wrap-up" has been published on the coreboot blog. Find the full post at http://blogs.coreboot.org/blog/2016/08/23/gsoc-better-risc-v-support-wrap-up/

<p>In less than an hour, Google Summer of Code 2016 will be over (at least for us students). In this blog post, I will describe how you can use coreboot on RISC-V.</p>
<p>You can find the complete list of commits that I made during GSoC with <a href="https://review.coreboot.org/#/q/since:2016-04-22+before:2016-08-24+owner:neuschaefer+status:merged">this gerrit query.</a></p>
<h3>The details</h3>
<h4>Compiling spike, the RISC-V instruction-set-level simulator</h4>
<p>Spike, also known as riscv-isa-sim, is the reference implementation of RISC-V, and the only RISC-V platform that is currently known to work with coreboot (<a href="https://github.com/riscv/riscv-qemu">QEMU</a> is nominally also supported, but the corresponding coreboot code has not been updated in a while).</p>
<p>First, you need to build and install libfesvr:</p>
<ul>
<li>Clone the <a href="https://github.com/riscv/riscv-fesvr">libfesvr git repository</a></li>
<li>run <tt>./configure --prefix="$HOME" && make && make install</tt></li>
</ul>
<p>Then, you can compile and install spike:</p>
<ul>
<li>Clone the <a href="https://github.com/riscv/riscv-isa-sim">spike git repository</a>.</li>
<li>Apply the patch in <a href="https://github.com/riscv/riscv-isa-sim/pull/53">this pull request</a> to make console output possible.</li>
<li>Open <tt>riscv/processor.cc</tt> in a text editor and find <tt>processor_t::get_csr</tt>. Add a line that reads <tt>case CSR_MTIME: return 0;</tt>.</li>
<li>run <tt>./configure --prefix="$HOME" --with-fesvr="$HOME" && make && make install</tt></li>
</ul>
<h4>Compiling coreboot for RISC-V</h4>
<ul>
<li>Clone the <a href="https://review.coreboot.org/cgit/coreboot.git/">coreboot git repository</a>, if you haven’t already</li>
<li>Apply Iru Cai’s patch that <a href="https://review.coreboot.org/#/c/14604/11">updates the toolchain to GCC 6.1</a>. You will currently have to fix a merge conflict when you apply this patch, but it’s an easy one.</li>
<li>Run <tt>make crossgcc-riscv</tt></li>
<li>Run <tt>make menuconfig</tt> to configure coreboot. Select <tt>Emulation</tt> and <tt>SPIKE usb riscv</tt> in the Mainboard menu.</li>
<li>Run <tt>make</tt></li>
<li>Run <tt>util/riscvtools/make-spike-elf.sh build/coreboot.rom build/coreboot.elf</tt></li>
<li>Start coreboot by running <tt>spike build/coreboot.elf</tt>. You should see a few pages of output, ending in <tt>Payload not loaded</tt>.</li>
</ul>
<h4>Compiling and running Linux</h4>
<ul>
<li>Clone the <a href="https://github.com/riscv/riscv-linux">riscv-linux git repository</a> and check out the <tt>priv-1.9</tt> branch</li>
<li>Apply <a href="https://github.com/neuschaefer/riscv-linux/commit/84a4661e71cb7ca18d3c90a6559bbca8cb4927a9">this patch</a> that allows linux to be started in machine-mode.</li>
<li>Download a copy linux 4.6.x from <a href="https://kernel.org">kernel.org</a>, and unpack it. I’ll assume version 4.6.2 is used.</li>
<li><tt>cd</tt> into <tt>linux-4.6.2/arch</tt> and symlink the <tt>arch/riscv</tt> directory from riscv-linux</li>
<li>Back in <tt>linux-4.6.2</tt>, run <tt>make O=build ARCH=riscv defconfig</tt>; <tt>cd</tt> into the newly created <tt>build</tt> directory.</li>
<li>Run <tt>make ARCH=riscv menuconfig</tt>. In the “General Setup” menu of the linux menuconfig, enter <tt>path/to/coreboot/util/crossgcc/xgcc/bin/riscv64-elf-</tt> as the cross-compiler tool prefix.</li>
<li>Run <tt>make ARCH=riscv vmlinux</tt> to compile linux.</li>
<li>Open <tt>vmlinux</tt> in a hex editor, such as <tt>hexer</tt>. Change the 8-byte number at 0x18 to <tt>00 00 00 90 00 00 00 00</tt>; Add <tt>00 00 00 90 00 00 00 00</tt> to the numbers at 0x58 and 0x90, to load linux at physical addresses within RAM. It’s unfortunate that I have to recommend this step, but I did not come up with a better fix yet.</li>
</ul>
<p>Next, you need to add vmlinux to coreboot:</p>
<ul>
<li>Return to the coreboot directory.</li>
<li>Apply the remaining coreboot patches that are tagged <a href="https://review.coreboot.org/#/q/topic:riscv+status:open+owner:neuschaefer">riscv</a>.</li>
<li>In the <tt>Payload</tt> menu, select <tt>An ELF executable payload</tt>. Instead of <tt>payload.elf</tt>, select the <tt>vmlinux</tt> file.</li>
<li>Run <tt>make</tt> and <tt>util/riscvtools/make-spike-elf.sh build/coreboot.rom build/coreboot.elf</tt> again.</li>
<li>Run <tt>spike build/coreboot.elf</tt>. You should now see a Linux boot, at least partially.</li>
</ul>
<h3>Future work</h3>
<p>Even though my GSoC is over, coreboot’s support for RISC-V can still be improved, and I intend to fix at least some of the following things:</p>
<ul>
<li>As you can see above, running coreboot and linux on RISC-V currently isn’t straight-forward, but involves a few manual steps.</li>
<li>There are other RISC-V platforms that I’d like to see coreboot on, such as <a href="http://www.lowrisc.org/">lowRISC</a>.</li>
<li>Linux does not <i>completely</i> boot, i.e. into userspace. There are still some bugs to be ironed out.</li>
<li>Automatic testing could be used to detect regressions.</li>
<li>I only tested coreboot on RISC-V with Linux; support for other operating systems or payloads is welcome.</li>
</ul>
<h3>Acknowledgements</h3>
<p>I’d like to thank Ron Minnich and Furquan Shaikh for being good mentors, and everyone in the coreboot community for being helpful and friendly.</p>