How to Emulate the Raspberry Pi 2 on Qemu

How to emulate the Raspberry Pi 2 on QEMU?

If you're comfortable building qemu, you can find support for pi2 system emulation here: https://github.com/0xabu/qemu. It's not particularly speedy, and the device emulations are incomplete, but you can resize the RAM and framebuffer.

There are brief instructions for booting Raspbian at the end of https://github.com/0xabu/qemu/wiki

How to emulate Raspberry Pi Raspbian with QEMU?

You should expand the Raspbian Image file before starting

Mount the Raspbian Image file with kpartx (might have to install kpartx)

$ sudo kpartx -av your-image.img
add map loop0p1 (252:5): 0 117187 linear /dev/loop0 1
add map loop0p2 (252:6): 0 3493888 linear /dev/loop0 118784

$ sudo mount /dev/mapper/loop0p2 /mnt/img1
$ cd /mnt/img1

Modify the /etc/fstab and comment out the MMCBLK mounts

$ sudo nano etc/fstab

proc /proc proc defaults 0 0
#/dev/mmcblk0p1 /boot vfat defaults 0 2
#/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
# a swapfile is not a swap partition, no line here
# use dphys-swapfile swap[on|off] for that

Modify /etc/ld.so.preload and comment out lines ...

$ sudo nano etc/ld.so.preload

#/usr/lib/arm-linux-gnueabihf/libarmmem.so

Unmount and destroy loops from kpartx

$ sudo umount /mnt/img1
$ sudo kpartx -d your-image.img

Get the Qemu kernel matching the Raspbian image here...

https://github.com/dhruvvyas90/qemu-rpi-kernel

I used this command to successfully emulate Raspbian Jessie

qemu-system-arm -kernel kernel-qemu-4.4.12-jessie -cpu arm1176 -m 256 -M versatilepb \
-no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
-redir tcp:5022::22 \
-hda 2016-05-27-raspbian-jessie-lite.img

qemu-system-arm hangs with Raspberry Pi 2 image

"Nothing happens" and "tried to execute from a bogus address" are often the result of either:

  • misconfigured kernel (is this kernel definitely intended to boot on the raspi2 board, and not on something else?)
  • something goes wrong in early boot before the kernel manages to produce output (though usually this causes a hang rather than a bad-address output)

For the latter, assuming this really is a raspi2 kernel, you might try using
earlycon=pl011,0x3f201000
in your kernel append arguments. (The Linux kernel can produce earlycon output for the PL011 UART, but not for the raspi-specific 'mini UART'.)

I would suggest also dropping "-nographic" and "-serial mon:stdio" for the moment. Then you can use the graphical UI to check both UART outputs. (You can do this without using the GUI by redirecting them both correctly using two lots of -serial command line options, but then you have to figure out sensible places to send them; the GUI's simpler.) The first serial port will be the PL011, and the second the mini-UART, so if you only tell QEMU where to send the first serial port output and the guest is writing to the second, you'll never see it.

Bare metal Raspberry Pi 2: Generating an SD card image for QEMU emulation

After doing a bit of reading and searching online, as well as a bit of help from other contributors such as Peter Maydell with his answer above, I think I've answered my own question. Unless I'm mistaken qemu-system-arm does not fully emulate the Raspberry Pi boot process, and instead just loads the kernel specified with the -kernel argument by loading the binary into the guest system's memory and jumping to the entry point. It doesn't look like any additional hardware bootloading is emualted for -M raspi2 unfortunately.

Can ARM qemu system emulator boot from card image without kernel param?
This question is similar and contains some more useful details on this issue, relating to qemu-system-arm as a whole..

Is there a Raspberry Pi hardware emulator that supports custom ISOs?

For most of the complex hardware (i.e. CPU, GPU, RAM, network cards, disks and so on), qemu has you covered, there also seem to be a --machine raspi3b flag that's supposed to be close to a Raspberry Pi 3B but I can't find any information about what is included exactly.

If you want to emulate some other raspi, you can use qemu-system-aarch64 --machine help to list all 64bits ARM devices that can be emulated by qemu (note that as of writing this, qemu has no support for the raspi4).

If the emulation offered by qemu doesn't include some hardware you want, a lot of things can be emulated using dummy kernel modules such as the GPIO mockup driver as explained here and the mac80211_hwsim module. It's probably not as close to the hardware as you'd like though, if you prefer a more hands on approach you could create qemu "hardware" to be a perfect replica of the pi.



Related Topics



Leave a reply



Submit