Skip to content

Instantly share code, notes, and snippets.

@chetbox
Last active March 5, 2025 16:04
Show Gist options
  • Save chetbox/3d2624b3727b35cf82d68f21f2449e2c to your computer and use it in GitHub Desktop.
Save chetbox/3d2624b3727b35cf82d68f21f2449e2c to your computer and use it in GitHub Desktop.
Run Raspberry Pi 64 system image with QEMU on macOS
#!/bin/bash
# Requirements:
# - macOS
# - qemu - `brew install qemu`
# - A raw 64-bit Raspberry Pi system image to boot (Usually a .img)
set -e
if [[ -z "$1" ]]; then
echo "Disk image expected as argument" 1>&2
exit 1
fi
# Mount disk and find where /boot is mounted
BOOT_MOUNT=$(
hdiutil attach -imagekey diskimage-class=CRawDiskImage "$1" | grep 'Windows_FAT_32' | sed 's/.*\t//'
)
# Copy required files to local folder
cp "$BOOT_MOUNT/bcm2710-rpi-3-b-plus.dtb" ./
cp "$BOOT_MOUNT/kernel8.img" ./
# Unmount disk
hdiutil detach "$BOOT_MOUNT"
# Make sure the disk is on a power of two boundary for QEMU
qemu-img resize -f raw "$1" 8G
qemu-system-aarch64 \
-M raspi3b \
-cpu cortex-a72 \
-append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1" \
-dtb bcm2710-rpi-3-b-plus.dtb \
-drive "if=sd,format=raw,file=$1" \
-kernel kernel8.img \
-m 1G -smp 4 \
-serial mon:stdio \
-device usb-net,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::5555-:22 \
-usb -device usb-mouse -device usb-kbd \
@jon-ESA
Copy link

jon-ESA commented Feb 20, 2025

Is it possible to run on HVF?

@chetbox
Copy link
Author

chetbox commented Feb 20, 2025

Is it possible to run on HVF?

It doesn't seem to work for me. I get this error:

qemu-system-aarch64: Cannot enable HVF when guest CPU has EL3 enabled

@jon-ESA
Copy link

jon-ESA commented Feb 21, 2025

Yes I also have the same error. The Pi4 onwards use aarch64 and likewise does the apple silicon. I therefore assumed some level of compatibility for virtualisation but HVF does not support EL3 and I have not been able to understand a method of disabling this requirement from within the pi image.
I also tried KVM with --enable-kvm and whilst QEMU did not present errors, the image did not boot.
I have checked also the img is 64 bit (tried desktop and lite).
I know the convention would be to post what code I tried, but I purged the project last night in favour of UTM.
Thanks

@chetbox
Copy link
Author

chetbox commented Feb 21, 2025

Have you managed to boot a Raspberry Pi OS image on UTM?

@jon-ESA
Copy link

jon-ESA commented Feb 21, 2025

Nope not virtualised at least. I'm experimenting with the aarch64 build on UTM for Arch linux, and also installing arch linux on the raspberry Pi. It seems Arch Linux has better support for electronics / robotics anyway over RaspbianOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment