Anwar Ghammam
Anwar Ghammam

Reputation: 191

Emulating Raspberry Pi 4 with QEMU?

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.

Upvotes: 19

Views: 25971

Answers (6)

Hubert Gruniaux
Hubert Gruniaux

Reputation: 194

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.

Upvotes: 5

U007D
U007D

Reputation: 6318

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!

Upvotes: 0

Thibault Poncetta
Thibault Poncetta

Reputation: 59

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

Upvotes: 4

Paul Wratt
Paul Wratt

Reputation: 161

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

Upvotes: 1

Jiang
Jiang

Reputation: 81

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

Upvotes: 7

Peter Maydell
Peter Maydell

Reputation: 11403

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.

Upvotes: 6

Related Questions