Emulating Raspberry Pi 4 with QEMU?
Asked Answered
L

6

18

I want to emulate Raspberry Pi 4 using QEMU, but I am not able to find any image for RPi4. I need a kernel with which QEMU can emulate a Cortex-A72.

Limicolous answered 11/4, 2021 at 13:18 Comment(0)
R
7

I just boot raspios bullseye on a x86 ubuntu laptop, it can show the desktop, can be login in. But it's very slow.

qemu-system-aarch64 -M virt,highmem=off -smp 8 -m 2G -cpu cortex-a72 -kernel linux-stable/arch/arm64/boot/Image -append root=PARTUUID=d97f5830-02 rw console=ttyAMA0 -serial telnet:localhost:4321,server,nowait -monitor telnet:localhost:4322,server,nowait -device VGA,id=vga1 -device secondary-vga,id=vga2 -object iothread,id=io1 -device virtio-blk-pci,drive=disk0,iothread=io1 -drive data/images/2022-01-28-raspios-bullseye-arm64.img

I build the kernel image follow this guide.

https://github.com/anholt/linux/wiki/Raspberry-Pi-development-environment#building-the-Kernel

Of course, as the raspios is emulated on the x86 laptop, it's definitely slow. So, if you can virtualize it on an arm64 host, you can use the accelerator like kvm, hvf etc.

    qemu-system-aarch64 \
      -M virt,highmem=off,accel=hvf \
      -cpu host \
      -m 1G \
      -smp 4 \
      -kernel $KERNEL_IMAGE_PATH -append "root=/dev/vda2 rw console=ttyAMA0" \
      -netdev user,id=n1,ipv6=off,hostfwd=tcp::5555-:22 -device e1000,netdev=n1 \
      -hda data/images/2022-01-28-raspios-bullseye-arm64.img \
      -serial telnet:localhost:4321,server,nowait \
      -monitor telnet:localhost:4322,server,nowait \
      -device VGA,id=vga2 \
      -drive file=data/images/2021-10-30-raspios-bullseye-armhf.img,if=virtio
Response answered 19/2, 2022 at 13:17 Comment(0)
R
6

QEMU does not have a model of the raspberry pi 4 at this time (only the 0, 1ap, 2b, 3ap and 3b). If some other machine type that QEMU does support would be good enough for you you could build a kernel for that machine type and use that. (For instance, for a lot of people all they really want is "boots a 64-bit Linux userspace" and they don't need it to really exactly match the Raspberry Pi board hardware; for those people the 'virt' board is usually a good choice.)

Whatever you do, you need to make sure that the machine type you ask QEMU to emulate matches the machine type you've built the kernel for -- if you try to boot a kernel on a board that it does not support it will not work.

Raggedy answered 12/4, 2021 at 9:54 Comment(0)
A
4

there is a patch here if you want. You can compile qemu with raspi4b2g (Patch not from me)

https://github.com/0xMirasio/qemu-patch-raspberry4.git

Alta answered 16/2, 2022 at 9:1 Comment(0)
E
4

The official support for the Raspberry Pi 4 has landed in the main branch of QEMU. The next version of QEMU (9.0) should have this support included.

Once installed, you could simply use:

qemu-system-aarch64 -machine raspi4b -kernel ...

Note however that, for now (february 2024), the following periphericals are not yet supported:

  • PCIE Root Port
  • GENET Ethernet Controller

This answer is intended more for future readers and less for the original questioner.

Experimentation answered 28/2 at 15:12 Comment(1)
Thanks. I was able to get this working. I personally wished I'd found some complete "known good" command examples, so here is mine. After installing 9.0.0-rc4 via brew install qemu --HEAD I can launch a RPi4 VM using an unmodified kernel and disk image as follows: qemu-system-aarch64 -machine raspi4b -serial stdio -dtb ../boot/bcm2711-rpi-4-b.dtb -kernel kernel8.img -drive file=./sdcard.img,id=mysdcard,if=sd -append 'root=PARTUUID=738a4d67-02 rootfstype=ext4 rootwait console=ttyAMA0,115200 earlyprintk loglevel=8' Keyboard and network don't seem to work, yet.Eschew
V
1

or try the following branch (2019), which may or maynot be patch equivalent to that already mentioned: https://gitlab.com/philmd/qemu/-/tree/raspi4_wip

Vistula answered 25/3, 2022 at 0:38 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewHydrolyze
C
0

I have posted a fork of the qemu repo with -machine raspi4b smoke-tested at https://github.com/U007D/qemu. Credit to Sergey Kambalin and his patch set for the work.

tl;dr build instructions:

  • Install qemu prereqs and clone above qemu repo
  • cd qemu && mkdir build && cd build
  • ../configure --disable-werror (--disable-werror is required (at least on macOS) because as of XCode 15, linker options changed which give warnings against previously valid arguments. Likely not needed on other platforms. Build is otherwise clean.)
  • make (This build will still fail (after compilation) at the testing stage--test failure is documented in the patches thread linked above)
  • ./qemu-system-<your_host_architecture> -machine help will show raspi4b as one of the supported machines.
  • Note: This is the latest (as of this writing), v3 patch set from the above-linked patches thread.
  • Enjoy!
Camise answered 31/12, 2023 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.