public inbox for linux-riscv@lists.infradead.org
 help / color / mirror / Atom feed
From: Michael Opdenacker <michael.opdenacker@bootlin.com>
To: linux-riscv@lists.infradead.org
Subject: No device tree source for the QEMU Risc-V virt board?
Date: Wed, 23 Dec 2020 21:22:15 +0100	[thread overview]
Message-ID: <787d35c6-8933-ae5e-2e3a-a3de8c6f27ee@bootlin.com> (raw)

Greetings,

I struggled to get Linux to boot from U-Boot on the QEMU Risc-V virt
board. I know that I can boot Linux directly, and this is well
documented, but booting U-Boot first and then Linux was much harder and
not really documented (at least according to what I found).

I believe that booting Linux from U-Boot is better in terms of
demonstrating a more realistic Risc-V system, that's why I wanted to do
this.

One of the issues that I got was that U-Boot didn't want to boot the
kernel binary without a DTB. However, I didn't find any corresponding DT
sources in the Linux sources. To get a working DTB, I had to boot Linux
directly, and then get the "/sys/firmware/fdt" file contents! Would you
have any other way to suggest to make me prouder?

For people interested in doing the same thing, here are my raw notes...

Thanks in advance for your insights about the QEMU virt board DTB. I'm
probably missing something.

Cheers,

Michael.

---

QEMU Risc-V instructions
For booting Linux from U-Boot in QEMU's virt machine


Linux (5.10.0)
export ARCH=riscv
make defconfig
make -j8 Image

Create a disk image with 2 partitions, to mimic a real-life case:
- A fat32 one (for the kernel and DTB)
- An ext2 or ext4 one (for the root fs)

To boot this kernel from QEMU, I need to compile opensbi for Linux
cd opensbi
make PLATFORM=generic FW_PAYLOAD_PATH=../linux/arch/arm/boot/Image

Boot ing Linux directly:
qemu-system-riscv64 -M virt -m 2G -nographic -kernel
opensbi/build/platform/generic/firmware/fw_payload.elf -device
virtio-blk-device,drive=hd0 -drive file=disk.img,format=raw,id=hd0
-append "console=ttyS0 rw root=/dev/vda2 earlycon=sbi"

From the booted system, copy /sys/firmware/fdt to the first partition of
the virtio drive, naming the file "dtb". Why don't we get one from the
Linux kernel sources?

Now, we can prepare U-Boot for booting Linux. Without a dtb, U-Boot
refused to start the Linux kernel.

U-Boot (c15f44acf9d473f4682bfdc63b8aebd313492b15, 2021.04-rc4)

cd u-boot
make qemu-riscv64_smode_defconfig

Modify the configuration to support loading an environment from a FAT
partition on a virtio disk. That's closer to real life:
CONFIG_ENV_IS_IN_FAT=y
CONFIG_ENV_FAT_INTERFACE="virtio"
CONFIG_ENV_FAT_DEVICE_AND_PART="0:1"
make -j 8

Now, let's compile opensbi again:
cd opensbi
make clean
make PLATFORM=generic FW_PAYLOAD_PATH=../u-boot/u-boot.bin

We can now boot U-Boot:
qemu-system-riscv64 -M virt -m 2G -nographic -bios
opensbi/build/platform/generic/firmware/fw_payload.elf -device
virtio-blk-device,drive=hd0 -drive file=disk.img,format=raw,id=hd0

The first time, interrupt the U-Boot countdown and set environment
variables:
setenv bootcmd 'fatload virtio 0:1 80200000 Image; fatload virtio 0:1
0x82000000 dtb; booti 0x80200000 - 0x82000000'
setenv bootargs 'root=/dev/vda2 rootwait console=ttyS0
earlycon=sbiroot=/dev/vda2 rootwait console=ttyS0 earlycon=sbi'
saveenv

You can now restart QEMU and Linux boots!

-- 
Michael Opdenacker, CEO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

             reply	other threads:[~2020-12-23 20:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-23 20:22 Michael Opdenacker [this message]
2020-12-23 22:59 ` No device tree source for the QEMU Risc-V virt board? Atish Patra
2021-01-08 15:02   ` Michael Opdenacker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=787d35c6-8933-ae5e-2e3a-a3de8c6f27ee@bootlin.com \
    --to=michael.opdenacker@bootlin.com \
    --cc=linux-riscv@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox