From 5fd6d2da5bbe742b7e48075916c20f3a067c1cf2 Mon Sep 17 00:00:00 2001 From: Noah Date: Mon, 28 Sep 2026 00:32:14 -0600 Subject: [PATCH] qemu: provision a real U-Boot env and add expect_reboot The rig's env partition was left zeroed, so flared could never clear bootcount there. It treats that as "intentional reboot bookkeeping not durable", so every factory reset on the rig wiped the store, was then refused, and never rebooted (flare-edge#62). mkimage.sh now writes a valid 32 KiB Rockchip ENVF image (bootcount=0, LE CRC32 over the rest), the same format flared/src/envf.rs and flare-edge tools/envf.py read; tools/envf.py parses it with a matching CRC. An accepted reset then reboots the guest, which ends the VM under -no-reboot. qmp.py gains expect_reboot TIMEOUT_S for an action whose correct outcome is a reboot: it passes only when the console shows the kernel's orderly "reboot: Restarting system" (written after the step began) and QEMU is gone, and fails on a kernel panic, on an exit without that line, and on a VM still running at the deadline. The console is read after the liveness check so the guest's last lines are not missed. Six drive tests cover it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01441BzdGcvQmXkiN2qDjfZg --- qemu/mkimage.sh | 28 ++++++++++++++- qemu/tests/qmp.py | 63 ++++++++++++++++++++++++++++++++-- qemu/tests/test_qmp_drive.py | 66 ++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 3 deletions(-) diff --git a/qemu/mkimage.sh b/qemu/mkimage.sh index a3890e2..5252914 100755 --- a/qemu/mkimage.sh +++ b/qemu/mkimage.sh @@ -186,10 +186,16 @@ place_partition() { # scenarios. Bytes mirror flare-edge tools/mk-misc.py provisioning # defaults (A: prio 15 successful, B: prio 14 successful, CRC32-BE). misc) stage="__misc__" ;; + # env carries a REAL U-Boot environment: flared clears bootcount there + # before an intentional reboot, and treats a zeroed (bad-CRC) env as + # "bookkeeping not durable", so a factory reset on the rig was always + # refused (flare-edge#62). Format mirrors flare-edge flared/src/envf.rs + # and tools/envf.py: LE CRC32 of the rest, NUL-separated entries. + env) stage="__env__" ;; # Boot-chain partitions the VM never reads: present at the right offsets, # left zeroed. Enumerated (not a wildcard) so a typo'd name in # blkdevparts.conf fails HERE, not as a confusing mount error at boot. - env|idblock|uboot|boot_a|boot_b|recovery) stage="" ;; + idblock|uboot|boot_a|boot_b|recovery) stage="" ;; *) echo "FATAL: unknown partition name '$name' in blkdevparts.conf" >&2; exit 1 ;; esac DISK_END_TRACK "$off" "$size" @@ -220,6 +226,26 @@ PYMISC qemu_log " $name: AvbABData provisioned @ +2048" return 0 fi + if [ "$stage" = "__env__" ]; then + python3 - "$img" "$size" <<'PYENV' +import struct, sys, zlib +img, size = sys.argv[1], int(sys.argv[2]) +ENV_SIZE = 0x8000 # CONFIG_ENV_SIZE; flared refuses any other length +if size < ENV_SIZE: + sys.exit(f"FATAL: env partition is {size} bytes, need {ENV_SIZE}") +body = bytearray(ENV_SIZE - 4) +entries = b"bootcount=0\0\0" # one entry, then the empty terminator +body[:len(entries)] = entries +env = struct.pack("= deadline: + if restarted: + return "fail", f"the guest restarted but QEMU was still running after {timeout:g}s" + return "fail", f"no reboot within {timeout:g}s" + time.sleep(0.2) + + def verb_echo(ctx, lineno, cmd, args, line): print(f" [{lineno}] {' '.join(args)}", flush=True) return None @@ -1098,6 +1155,7 @@ VERBS = { "assert_region": verb_assert_region, "wait_region": verb_wait_region, "assert_ocr": verb_assert_ocr, + "expect_reboot": verb_expect_reboot, } @@ -1120,7 +1178,8 @@ def drive(s, f, script_path, outdir, size, ctl_path=None, console_path=None, ref print(f" [{lineno}] {tag}{cmd}{(' -- ' + detail) if detail else ''}", flush=True) refs_path = refs_path or os.path.join(outdir, "refs.json") - ctx = Ctx(s, f, size, ctl, load_refs(refs_path), refs_path, rs485_control, outdir, script_path) + ctx = Ctx(s, f, size, ctl, load_refs(refs_path), refs_path, rs485_control, outdir, script_path, + console_path) console = ConsoleWatch(console_path) for lineno, raw in enumerate(lines, 1): diff --git a/qemu/tests/test_qmp_drive.py b/qemu/tests/test_qmp_drive.py index fb3e003..c5a9c8f 100755 --- a/qemu/tests/test_qmp_drive.py +++ b/qemu/tests/test_qmp_drive.py @@ -858,6 +858,72 @@ class DriveConsoleCheck(unittest.TestCase): self.assertTrue(all(r["line"] == 1 for r in rows)) +class ExpectReboot(unittest.TestCase): + """expect_reboot (flare-edge#62): an action whose correct outcome is a + reboot ends the VM on this rig (-no-reboot), so the reboot itself is the + thing to assert. QEMU's behaviour is scripted through the fake rpc: each + query-status call can append console output and then report the VM gone.""" + + def run_reboot(self, steps, before=b"", timeout="2"): + d = tempfile.mkdtemp(prefix="console.") + console = os.path.join(d, "console.log") + with open(console, "wb") as fh: + fh.write(before) + calls = [] + + def rpc(sock, sock_file, obj): + if obj.get("execute") != "query-status": + return fake_rpc(sock, sock_file, obj) + step = steps[min(len(calls), len(steps) - 1)] + calls.append(step) + text, state = step + if text: + with open(console, "ab") as fh: + fh.write(text) + if state == "gone": + raise RuntimeError("QMP connection closed") + return {"status": state} + + return run_script(f"expect_reboot {timeout}\n", rpc_fn=rpc, console_path=console) + + def test_orderly_reboot_passes(self): + rc, by, rows = self.run_reboot([ + (b"", "running"), + (b"[ 12.3] reboot: Restarting system\n", "running"), + (b"", "gone"), + ]) + self.assertIsNone(rc) + self.assertEqual(rows[-1]["status"], "ok", rows) + + def test_qmp_reporting_shutdown_counts_as_gone(self): + rc, by, rows = self.run_reboot([(b"reboot: Restarting system\n", "shutdown")]) + self.assertEqual(rows[-1]["status"], "ok", rows) + + def test_no_reboot_before_the_deadline_fails(self): + rc, by, rows = self.run_reboot([(b"", "running")], timeout="0.5") + self.assertEqual(rc, 1) + self.assertIn("no reboot within", rows[-1]["detail"]) + + def test_a_kernel_panic_is_not_a_reboot(self): + rc, by, rows = self.run_reboot([ + (b"Kernel panic - not syncing: Attempted to kill init!\n" + b"reboot: Restarting system\n", "gone"), + ]) + self.assertEqual(rc, 1) + self.assertIn("kernel panic", rows[-1]["detail"]) + + def test_exiting_without_the_orderly_line_fails(self): + rc, by, rows = self.run_reboot([(b"", "gone")]) + self.assertEqual(rc, 1) + self.assertIn("without the kernel's orderly restart line", rows[-1]["detail"]) + + def test_a_restart_line_from_before_the_step_does_not_count(self): + rc, by, rows = self.run_reboot([(b"", "gone")], + before=b"reboot: Restarting system\n") + self.assertEqual(rc, 1) + self.assertIn("without the kernel's orderly restart line", rows[-1]["detail"]) + + class FullscreenToggleTracksRealState(unittest.TestCase): """qemu/tests/scripts/fullscreen-toggle-tracks-real-state.txt, offline: `home` clears the dashboard's real fullscreen state directly, and a