Booting Linux from U-Boot in qemu-system-riscv64?

1,713 views
Skip to first unread message

Michael Opdenacker

unread,
Dec 2, 2020, 2:21:24 AM12/2/20
to sw-...@groups.riscv.org, u-b...@lists.denx.de
Greetings,

To prepare an embedded Linux demo (on RiscV), I'm trying to boot Linux
from U-Boot in qemu-system-riscv64, using the "virt" emulated machine...
Note that I'm using QEMU 5.0.

The well documented way to boot Linux works:

In Linux master:
CROSS_COMPILE=riscv64-linux-gnu- (toolchain built by Buildroot)
ARCH=riscv
make defconfig
make

In opensbi:
make PLATFORM=generic FW_PAYLOAD_PATH=../linux/arch/riscv/boot/Image

Running QEMU:
qemu-system-riscv64 -nographic  -machine virt -kernel
opensbi/build/platform/generic/firmware/fw_payload.elf -append
"console=ttyS0"

I can also boot U-Boot, but cannot manage to boot the Linux kernel with it:

In U-Boot mainline:

CROSS_COMPILE=riscv64-linux-gnu-
make qemu-riscv64_smode_defconfig
Modify the environment:
- Unset CONFIG_ENV_IS_NOWHERE
- CONFIG_ENV_IS_IN_FAT=y
- CONFIG_ENV_FAT_INTERFACE="virtio"
- CONFIG_ENV_FAT_DEVICE_AND_PART="0:1"
- CONFIG_ENV_FAT_FILE="uboot.env"
make

Note that having U-Boot in "S" mode seems to be required to boot Linux.
If U-Boot is in "M" mode, it works, but there are faults (as expected)
when trying to execute Linux.

In opensbi:
make PLATFORM=generic FW_PAYLOAD_PATH=../u-boot/u-boot.bin

I then prepared a disk.img image with a first FAT partition containing:
- A "dtb" file captured from the first Linux boot (from /sys/firmware/fdt)
- A "uImage" file made from the command:
mkimage -A riscv -O linux -C none  -T kernel -a 80200000 -e 80200000 -n
"Linux" -d arch/riscv/boot/Image uImage

Then I'm starting QEMU as follows:

qemu-system-riscv64 -m 1G -nographic  -machine virt -bios
opensbi/build/platform/generic/firmware/fw_payload.bin -device
virtio-blk-device,drive=hd0 -drive file=disk.img,format=raw,id=hd0

OpenSBI v0.8-58-g781cafd
   ____                    _____ ____ _____
  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|____/_____|
        | |
        |_|

Platform Name             : riscv-virtio,qemu
Platform Features         : timer,mfdeleg
Platform HART Count       : 1
Firmware Base             : 0x80000000
Firmware Size             : 104 KB
Runtime SBI Version       : 0.2

Domain0 Name              : root
Domain0 Boot HART         : 0
Domain0 HARTs             : 0*
Domain0 Region00          : 0x0000000080000000-0x000000008001ffff ()
Domain0 Region01          : 0x0000000000000000-0xffffffffffffffff (R,W,X)
Domain0 Next Address      : 0x0000000080200000
Domain0 Next Arg1         : 0x0000000082200000
Domain0 Next Mode         : S-mode
Domain0 SysReset          : yes

Boot HART ID              : 0
Boot HART Domain          : root
Boot HART ISA             : rv64imafdcsu
Boot HART Features        : scounteren,mcounteren,time
Boot HART PMP Count       : 16
Boot HART PMP Granularity : 4
Boot HART PMP Address Bits: 54
Boot HART MHPM Count      : 0
Boot HART MHPM Count      : 0
Boot HART MIDELEG         : 0x0000000000000222
Boot HART MEDELEG         : 0x000000000000b109


U-Boot 2021.01-rc2-00161-gf6cf78dc6a (Dec 02 2020 - 07:05:39 +0100)

CPU:   rv64imafdcsu
Model: riscv-virtio,qemu
DRAM:  1 GiB
Loading Environment from FAT... OK
In:    uart@10000000
Out:   uart@10000000
Err:   uart@10000000
Net:   No ethernet found.
Hit any key to stop autoboot:  0
bootcmd=printenv bootcmd; fatload virtio 0:1 0x84000000 uImage; fatload
virtio 0:1 0x88000000 dtb; bootm 0x84000000 - 0x88000000
17704512 bytes read in 39 ms (432.9 MiB/s)
3618 bytes read in 1 ms (3.5 MiB/s)
## Booting kernel from Legacy Image at 84000000 ...
   Image Name:   Linux
   Image Type:   RISC-V Linux Kernel Image (uncompressed)
   Data Size:    17704448 Bytes = 16.9 MiB
   Load Address: 80200000
   Entry Point:  80200000
   Verifying Checksum ... OK
## Flattened Device Tree blob at 88000000
   Booting using the fdt blob at 0x88000000
   Loading Kernel Image
   Using Device Tree in place at 0000000088000000, end 0000000088003e21

Starting kernel ...


... and that's all I get, no further output. I'm not sure my dtb file is
suitable though. I'm surprised there is no DTS file for the riscv64
"virt" board emulated by QEMU in the Linux kernel sources, as U-Boot
will refuse to boot Linux without one.

Note that in U-Boot, the bootargs environment is set to:
bootargs=console=ttyS0

I'll be interested in your advise, if you already managed to do
something like this.

Cheers,

Michael.

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

Anup Patel

unread,
Dec 2, 2020, 9:28:56 AM12/2/20
to Michael Opdenacker, RISC-V SW Dev, U-Boot Mailing List
Hi Michael,

Set bootargs to "root=/dev/vda2 rootwait console=ttyS0 earlycon=sbi" in U-Boot before doing bootm

Regards,
Anup

--
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+un...@groups.riscv.org.
To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/6071db20-58af-55d3-5bec-8561d18a1707%40bootlin.com.

Michael Opdenacker

unread,
Dec 2, 2020, 9:54:03 AM12/2/20
to Anup Patel, RISC-V SW Dev, U-Boot Mailing List
Hi Anup,

On 12/2/20 3:28 PM, Anup Patel wrote:
> root=/dev/vda2 rootwait console=ttyS0 earlycon=sbi


Many thanks for the "earlycon=sbi" tip. It doesn't solve the problem yet
but definitely helps to get further details:

[    0.000000] Linux version 5.10.0-rc5-00357-gaae5ab854e38
(mike@mike-laptop) (riscv64-linux-gnu-gcc (Ubuntu 9.3.0-17ubuntu1~20.04)
9.3.0, GNU ld (GNU Binutils for Ubuntu) 2.34) #11 SMP Tue Dec 1 07:13:44
CET 2020
[    0.000000] OF: fdt: Ignoring memory range 0x80000000 - 0x80200000
[    0.000000] earlycon: sbi0 at I/O port 0x0 (options '')
[    0.000000] printk: bootconsole [sbi0] enabled
[    0.000000] efi: UEFI not found.
[    0.000000] Zone ranges:
[    0.000000]   DMA32    [mem 0x0000000080200000-0x0000000081ffffff]
[    0.000000]   Normal   empty
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0: [mem 0x0000000080200000-0x0000000081ffffff]
[    0.000000] Initmem setup node 0 [mem
0x0000000080200000-0x0000000081ffffff]
[    0.000000] Unable to handle kernel paging request at virtual address
ffffffe007e00001
[    0.000000] Oops [#1]
[    0.000000] Modules linked in:
[    0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted
5.10.0-rc5-00357-gaae5ab854e38 #11
[    0.000000] epc: ffffffe0004a8b38 ra : ffffffe000021cb8 sp :
ffffffe001003f60
[    0.000000]  gp : ffffffe0010e1fb0 tp : ffffffe00100bd40 t0 :
ffffffe001d8efc0
[    0.000000]  t1 : 0000000081f8efc0 t2 : 2d656b696d40656b s0 :
ffffffe001003f80
[    0.000000]  s1 : ffffffe007e00000 a0 : ffffffe007e00000 a1 :
ffffffe001d8ef80
[    0.000000]  a2 : 0000000000000000 a3 : 0000000000000000 a4 :
0000000000000000
[    0.000000]  a5 : ffffffdf7fe00000 a6 : ffffffe0010104b8 a7 :
0000000000000030
[    0.000000]  s2 : ffffffe0010e30a8 s3 : 0000000000000003 s4 :
fffffffffffffff3
[    0.000000]  s5 : 00000000bffe1c70 s6 : 0000000000000000 s7 :
0000000000000000
[    0.000000]  s8 : 00000000bff6a042 s9 : 0000000080200000 s10:
0000000000000000
[    0.000000]  s11: 0000000000000000 t3 : 0000000000000018 t4 :
0000000000000001
[    0.000000]  t5 : 0000000000000018 t6 : 0000000001e00000
[    0.000000] status: 0000000000000100 badaddr: ffffffe007e00001 cause:
000000000000000d
[    0.000000] random: get_random_bytes called from
print_oops_end_marker+0x22/0x44 with crng_init=0
[    0.000000] ---[ end trace 0000000000000000 ]---
[    0.000000] Kernel panic - not syncing: Attempted to kill the idle task!
[    0.000000] ---[ end Kernel panic - not syncing: Attempted to kill
the idle task! ]---


Now I guess I can compare with the run in which the kernel is directly
started without U-Boot...

Suggestions welcome of course...

Thanks again,

Venkata Rao Gaddam

unread,
Jan 6, 2022, 2:52:38 AM1/6/22
to RISC-V SW Dev, Michael Opdenacker, u-b...@lists.denx.de
Hello Michael,

I followed the same approach as you mentioned in booting Linux from u-boot, but facing an issue @saveenv after u-boot.
And it is displaying, Loading Environment from FAT... but not showing OK. 
I just tried to save bootargs by setenv and saveenv commands. When I tried to save, it displays... Saving Environment to FAT... Failed (1).
Tried with different u-boot versions but no luck and disk.img is properly done, created 2 partitions, 1 for boot and 2 for rootfs.
No issues in disk.img creation but somehow it is not detecting FAT partition.

Can you please help on this?

Best Regards,
VenkataRao 

Michael Opdenacker

unread,
Jan 6, 2022, 3:38:39 AM1/6/22
to Venkata Rao Gaddam, RISC-V SW Dev
Hi Venkata,

On 1/6/22 8:52 AM, Venkata Rao Gaddam wrote:
> Hello Michael,
>
> I followed the same approach as you mentioned in booting Linux from
> u-boot, but facing an issue @saveenv after u-boot.
> And it is displaying, Loading Environment from FAT... but not showing OK. 
> I just tried to save bootargs by setenv and saveenv commands. When I
> tried to save, it displays... Saving Environment to FAT... Failed (1).
> Tried with different u-boot versions but no luck and disk.img is
> properly done, created 2 partitions, 1 for boot and 2 for rootfs.
> No issues in disk.img creation but somehow it is not detecting FAT
> partition.
>
> Can you please help on this?

Unfortunately, no time at the moment to support you because of other
projects...
Can you at least mount the FAT partition from your Linux host?

Cheers
Michael

--
Michael Opdenacker, Bootlin

Venkata Rao Gaddam

unread,
Jan 6, 2022, 6:38:59 AM1/6/22
to RISC-V SW Dev, Michael Opdenacker, Venkata Rao Gaddam
Hi Michael,

Thanks for quick reply.
I have already checked 2 partitions after mounting, showing Linux kernel in 1st Partition and RFS in 2nd partition.

Formatted 2 partitions properly by below commands then copied Linux kernel into boot and RFS into rootfs partitions.
sudo mkfs.vfat -F 32 -n boot /dev/loop26p1
sudo mkfs.ext4 -L rootfs /dev/loop26p2

1st Partition 
---------------------
fpga@fpga-riscv:~/Venkat/Linux_Tests/riscv$ ls -al /mnt/boot/
total 19362
drwxr-xr-x 2 root root      512 Jan  1  1970 .
drwxr-xr-x 4 root root     4096 Jan  5 16:22 ..
-rwxr-xr-x 1 root root 19822080 Jan  5 16:23 Image

2nd Partition
----------------------
fpga@fpga-riscv:~/Venkat/Linux_Tests/riscv$ sudo mount /dev/loop26p2 /mnt/rootfs/
fpga@fpga-riscv:~/Venkat/Linux_Tests/riscv$ ls -al /mnt/rootfs/
total 48
drwxrwxr-x 9 fpga fpga  4096 Jan  6 13:38 .
drwxr-xr-x 4 root root  4096 Jan  5 16:22 ..
drwxrwxr-x 2 fpga fpga  4096 Jan  5 22:21 bin
drwxr-xr-x 2 root root  4096 Jan  6 13:38 dev
drwx------ 2 root root 16384 Jan  5 16:16 lost+found
drwxr-xr-x 2 root root  4096 Jan  6 13:38 proc
drwxrwxr-x 2 fpga fpga  4096 Jan  5 22:21 sbin
drwxr-xr-x 2 root root  4096 Jan  6 13:38 sys
drwxrwxr-x 4 fpga fpga  4096 Jan  5 22:21 usr


Best Regards,
VenkataRao

Venkata Rao Gaddam

unread,
Jan 6, 2022, 7:03:07 AM1/6/22
to RISC-V SW Dev, Venkata Rao Gaddam, Michael Opdenacker
Hi Michael,


Please find boot log below.

fpga@fpga-riscv:~/Venkat/Linux_Tests/riscv$ ./run-qemu

OpenSBI v1.0

   ____                    _____ ____ _____
  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|____/_____|
        | |
        |_|

Platform Name             : riscv-virtio,qemu
Platform Features         : medeleg
Platform HART Count       : 8
Platform IPI Device       : aclint-mswi
Platform Timer Device     : aclint-mtimer @ 10000000Hz
Platform Console Device   : uart8250
Platform HSM Device       : ---
Platform Reboot Device    : sifive_test
Platform Shutdown Device  : sifive_test
Firmware Base             : 0x80000000
Firmware Size             : 308 KB
Runtime SBI Version       : 0.3

Domain0 Name              : root
Domain0 Boot HART         : 7
Domain0 HARTs             : 0*,1*,2*,3*,4*,5*,6*,7*
Domain0 Region00          : 0x0000000002000000-0x000000000200ffff (I)
Domain0 Region01          : 0x0000000080000000-0x000000008007ffff ()
Domain0 Region02          : 0x0000000000000000-0xffffffffffffffff (R,W,X)

Domain0 Next Address      : 0x0000000080200000
Domain0 Next Arg1         : 0x0000000082200000
Domain0 Next Mode         : S-mode
Domain0 SysReset          : yes

Boot HART ID              : 7

Boot HART Domain          : root
Boot HART ISA             : rv64imafdcsu
Boot HART Features        : scounteren,mcounteren,time
Boot HART PMP Count       : 16
Boot HART PMP Granularity : 4
Boot HART PMP Address Bits: 54
Boot HART MHPM Count      : 0
Boot HART MIDELEG         : 0x0000000000000222
Boot HART MEDELEG         : 0x000000000000b109


U-Boot 2021.10 (Jan 06 2022 - 12:51:24 +0530)

CPU:   rv64imafdcsu
Model: riscv-virtio,qemu
DRAM:  2 GiB
Flash: 32 MiB
Loading Environment from FAT... In:    uart@10000000

Out:   uart@10000000
Err:   uart@10000000
Net:   No ethernet found.
Hit any key to stop autoboot:  0
=> setenv bootargs 'root=/dev/vda2 rootwait console=ttyS0 earlycon=sbi'
=> setenv bootcmd 'fatload virtio 0:1 84000000 Image; booti 0x84000000 - ${fdtcontroladdr}'
=> saveenv

Saving Environment to FAT... Failed (1)
=> ls virtio 0:1
Couldn't find partition virtio 0:1
=> 


Best Regards,
VenkataRao

Michael Opdenacker

unread,
Jan 6, 2022, 8:54:02 AM1/6/22
to Venkata Rao Gaddam, RISC-V SW Dev, michael.o...@bootlin.com
Hi Venkata Rao

On 1/6/22 1:03 PM, Venkata Rao Gaddam wrote:
> Hi Michael,
>
>
> Please find boot log below.
>
> fpga@fpga-riscv:~/Venkat/Linux_Tests/riscv$ ./run-qemu


What are the contents of your run-qemu script?
Cheers
Michael.

Venkata Rao Gaddam

unread,
Jan 6, 2022, 9:02:40 AM1/6/22
to RISC-V SW Dev, Michael Opdenacker, Venkata Rao Gaddam
Hi Michael,

Please find that script below.

#!/bin/sh
qemu-system-riscv64 -m 2G \
-nographic \
-machine virt \
-smp 8 \
-bios opensbi/build/platform/generic/firmware/fw_payload.elf \
-drive file=disk.img,format=raw,id=hd0 \
-device virtio-blk-device,drive=hd0 \

Best Regards,
VenkataRao

Venkata Rao Gaddam

unread,
Jan 7, 2022, 4:26:28 AM1/7/22
to RISC-V SW Dev, Venkata Rao Gaddam, Michael Opdenacker
Hi Michael,

Is there any issue in my procedure?


Best Regards,
VenkataRao

Venkata Rao Gaddam

unread,
Jan 7, 2022, 6:14:34 AM1/7/22
to RISC-V SW Dev, Venkata Rao Gaddam, Michael Opdenacker
Hi Michael,

I did a small mistake, now it's working.
Thanks for your support.

Best Regards,
VenkataRao

Reply all
Reply to author
Forward
0 new messages