#!/usr/bin/env bash # Smoke-test the Pi 4 SD image in QEMU's raspi4b machine (no root needed). # # Boots the kernel, initrd and command line from the image's own # /boot/extlinux/extlinux.conf (first entry) with the SD image attached, and # gives you the serial console in this terminal. Login root / guix. # Quit with Ctrl-a x. # # QEMU's raspi4b is incomplete, so this differs from real hardware: # - U-Boot is skipped: QEMU can't run this kernel on the stock Pi 4 DTB, and # U-Boot would hand it exactly that. The DTB is patched instead: # * AON block (L2 irqchip, DVP clock gate, HDMI, its I2C) disabled: not # emulated, reading it aborts the kernel; # * mini-UART (ttyS1) disabled: QEMU's model storms IRQs; the PL011 # (ttyAMA0) is the console here instead; # * QEMU wires the SD card to the WiFi SDHCI (7e300000), so its WiFi # power sequence (firmware GPIOs, not emulated) is dropped. # - No GENET/PCIe/USB 3: no networking. # # Usage: ./run-qemu-rpi4.sh [guix-rpi4-claude.img] set -euo pipefail cd "$(dirname "$0")" IMG=${1:-guix-rpi4-claude.img} [ -e "$IMG" ] || { echo "no image $IMG; run ./build-rpi-image.sh first" >&2; exit 1; } WORK=$(mktemp -d) trap 'rm -rf "$WORK"' EXIT fdtput() { if type -P fdtput >/dev/null; then command fdtput "$@" else guix shell dtc -- fdtput "$@"; fi } # Root partition (2nd) offset, then read the boot entry with debugfs. start=$(/sbin/sfdisk -J "$IMG" | python3 -I -c \ 'import json,sys; print(json.load(sys.stdin)["partitiontable"]["partitions"][1]["start"])') ROOTFS="$IMG?offset=$((start * 512))" conf=$(/sbin/debugfs -R "cat /boot/extlinux/extlinux.conf" "$ROOTFS" 2>/dev/null) field() { awk -v k="$1" '$1 == k { $1 = ""; sub(/^ /, ""); print; exit }' <<<"$conf"; } kernel=$(field KERNEL); initrd=$(field INITRD); fdtdir=$(field FDTDIR); append=$(field APPEND) echo "kernel: $kernel"; echo "initrd: $initrd" /sbin/debugfs -R "dump $kernel $WORK/Image" "$ROOTFS" 2>/dev/null /sbin/debugfs -R "dump $initrd $WORK/initrd" "$ROOTFS" 2>/dev/null /sbin/debugfs -R "dump $fdtdir/broadcom/bcm2711-rpi-4-b.dtb $WORK/rpi4.dtb" "$ROOTFS" 2>/dev/null for node in clock@7ef00000 interrupt-controller@7ef00100 hdmi@7ef00700 \ i2c@7ef04500 hdmi@7ef05700 i2c@7ef09500 serial@7e215040; do fdtput -t s "$WORK/rpi4.dtb" "/soc/$node" status disabled done fdtput -d "$WORK/rpi4.dtb" /soc/mmc@7e300000 mmc-pwrseq fdtput -r "$WORK/rpi4.dtb" /soc/mmc@7e300000/wifi@1 # Writable copy, padded to a power of two (QEMU's SD card model insists). cp --sparse=always "$IMG" "$WORK/sd.img" chmod u+w "$WORK/sd.img" size=$(stat -c %s "$WORK/sd.img") pow=1; while [ $pow -lt "$size" ]; do pow=$((pow * 2)); done truncate -s $pow "$WORK/sd.img" append=$(sed -E 's/console=ttyS[0-9]+[^ ]*/console=ttyAMA0,115200/; s/console=tty1 ?//' <<<"$append") qemu-system-aarch64 -M raspi4b -m 2G \ -kernel "$WORK/Image" -initrd "$WORK/initrd" -dtb "$WORK/rpi4.dtb" \ -append "clk_ignore_unused $append" \ -drive if=sd,format=raw,file="$WORK/sd.img" \ -display none -monitor none -serial mon:stdio -serial null