interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / qemu_ck_imac_netboot_test.sh

173 lines · 8890 bytes Raw Blame History
1#!/usr/bin/env bash
2# qemu_ck_imac_netboot_test.sh -- QEMU test for the iMac lab network loader
3# (NETLDR.EFI, native/boot/net_loader.c) and its server (tools/ck_imac_netboot.c).
4# QEMU is an emulator: a PASS here qualifies nothing about a 2017 iMac, its
5# firmware's network driver or its Ethernet chip.
6#
7# One q35 + OVMF machine with a virtio NIC on the QEMU "-netdev dgram" backend
8# (raw Ethernet over a UDP pair on 127.0.0.1: no root, no tap) and NETLDR.EFI
9# as EFI/BOOT/BOOTX64.EFI, built with a fresh TEST key made here and an 8 s
10# fallback. The default IMACKERN.EFI sits beside it as \EFI\AIENOS\IMACKERN.EFI.
11# One boot, four stages, in order:
12# 1. good job: the lab kernel (IMACKERN-LAB.EFI) signed with the built-in key
13# is fetched, verified and started; it runs to DONE and restarts the
14# machine through the ACPI reset register; the loader comes back and the
15# server says WAIT.
16# 2. tampered job: the same signed job with one image byte flipped is refused
17# (image SHA-512 mismatch) and nothing starts.
18# 3. wrong key: a job signed with a second TEST key is refused (signer_id
19# names no trusted key) and nothing starts.
20# 4. server gone: after 8 s of silence the loader starts the kernel on its
21# own disk, which powers QEMU off through ACPI S5.
22# Needs qemu-system-x86_64 (>= 7.2 for dgram), OVMF, mtools and an x86_64 gcc.
23# Final line: AIENOS_IMAC_NETBOOT: PASS|FAIL|NOT_RUN exit 0 / 1 / 2 (tools) / 3 (NOT_RUN)
24set -euo pipefail
25repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
26cd "${repo_root}"
27tag="AIENOS_IMAC_NETBOOT"
28code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}"
29vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}"
30cross=""
31if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi
32for t in qemu-system-x86_64 mformat mmd mcopy "${cross}gcc" "${cross}ld"; do
33 command -v "${t}" >/dev/null || { echo "${tag}: NOT_RUN (${t} missing)"; exit 2; }
34done
35[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; }
36
37. "${repo_root}/scripts/lib_gate_hold.sh"
38if ! gh_quiet_check || ! gh_lock_take qemu_ck_imac_netboot_test "${AIENOS_GATE_MINUTES:-60}"; then
39 echo "${tag}: NOT_RUN (${gh_why})"
40 exit 3
41fi
42work="$(mktemp -d)"
43qpid=""
44spid=""
45cleanup() {
46 [[ -z "${qpid}" ]] || kill "${qpid}" 2>/dev/null || true
47 [[ -z "${spid}" ]] || kill "${spid}" 2>/dev/null || true
48 rm -rf "${work}"
49 gh_lock_release
50}
51trap cleanup EXIT
52
53out="${work}/out"
54commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
55mk() { make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" "$@" >/dev/null; }
56mk slot-tool imac-netboot-tool
57nb="${out}/host/ck_imac_netboot"
58"${nb}" keygen "${work}/test.key" "${work}/test.pub" >/dev/null
59"${nb}" keygen "${work}/other.key" "${work}/other.pub" >/dev/null
60mk CK_NETLDR_KEYS="${work}/test.pub" CK_NETLDR_FALLBACK_S=8 imac-netldr
61mk CK_X86_REPORT=1 imac-kernel
62mk imac-kernel
63push() { # image key -> next.job
64 NETBOOT_TOOL_OUT="${out}" bash scripts/ck_imac_netboot_push.sh "$1" "$2" "${work}/spool" >/dev/null
65}
66
67img="${work}/esp.img"
68truncate -s 64M "${img}"
69mformat -i "${img}" -F ::
70mmd -i "${img}" ::/EFI ::/EFI/BOOT ::/EFI/AIENOS
71mcopy -i "${img}" "${out}/NETLDR.EFI" ::/EFI/BOOT/BOOTX64.EFI
72mcopy -i "${img}" "${out}/IMACKERN.EFI" ::/EFI/AIENOS/IMACKERN.EFI
73
74fails=0
75pass() { echo "PASS $1"; }
76fail() { echo "FAIL $1"; fails=$((fails + 1)); }
77chk() { # description, file, pattern
78 if grep -aqE -- "$2" "$3"; then pass "$1"; else fail "$1 (no match for: $2)"; fi
79}
80count() { grep -acE -- "$1" "$2" || true; }
81log="${work}/serial.log"
82txt="${work}/serial.txt"
83slog="${work}/serve.log"
84snap() { tr -d '\r' <"${log}" >"${txt}" 2>/dev/null || : >"${txt}"; }
85wait_for() { # pattern file seconds [min count]
86 local n="${4:-1}"
87 for _ in $(seq 1 "$3"); do
88 snap
89 [[ "$(count "$1" "$2")" -ge "${n}" ]] && return 0
90 kill -0 "${qpid}" 2>/dev/null || return 1
91 sleep 1
92 done
93 return 1
94}
95
96# Free UDP ports for the dgram pair.
97lp=$((40000 + RANDOM % 10000))
98rp=$((lp + 1))
99mkdir -p "${work}/spool"
100push "${out}/IMACKERN-LAB.EFI" "${work}/test.key"
101"${nb}" serve "${work}/spool" --qemu-dgram "${rp}:${lp}" >"${slog}" 2>&1 &
102spid=$!
103cp "${vars_fd}" "${work}/vars.fd"
104qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 2 -m 1024 \
105 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
106 -drive if=pflash,format=raw,file="${work}/vars.fd" \
107 -drive if=none,id=esp,format=raw,file="${img}" -device ide-hd,drive=esp,bus=ide.0,bootindex=1 \
108 -netdev dgram,id=n0,local.type=inet,local.host=127.0.0.1,local.port="${lp}",remote.type=inet,remote.host=127.0.0.1,remote.port="${rp}" \
109 -device virtio-net-pci,netdev=n0,mac=52:54:00:12:34:56 \
110 -display none -serial file:"${log}" &
111qpid=$!
112
113echo "== stage 1: good job =="
114wait_for '^netldr: AIENOS NETLDR v1 ' "${txt}" 240 || true
115chk "loader started" '^netldr: AIENOS NETLDR v1 commit=[0-9a-f]+ keys=1 fallback=8s$' "${txt}"
116wait_for '^power: restart via ' "${txt}" 400 || true
117chk "NIC found" '^netldr: nic [0-9]+ of [0-9]+ mac=52:54:00:12:34:56 type=1 ' "${txt}"
118chk "cable link seen" '^netldr: nic [0-9]+ link up$' "${txt}"
119chk "offer received" '^netldr: the Spark offers a kernel: [0-9]+ bytes$' "${txt}"
120chk "signature and digest verified" "^netldr: signature and SHA-512 verified: gen=[0-9]+ commit=${commit:0:12} len=[0-9]+\$" "${txt}"
121chk "network kernel started" '^netldr: starting the network kernel \([0-9]+ bytes\)$' "${txt}"
122chk "lab kernel ran to DONE" '^AIENOS-KERNEL-0 DONE$' "${txt}"
123chk "lab kernel W^X audit" '^wx_audit: ok wx_pages=0 ' "${txt}"
124chk "restart through the ACPI reset register" '^power: restart via the ACPI reset register port=0x[0-9a-f]+ value=0x[0-9a-f]+$' "${txt}"
125chk "loader told the Spark VERIFIED" '^netldr: told the Spark: verified, starting \([1-3]/3 sent\)$' "${txt}"
126chk "server saw VERIFIED" 'loader says: VERIFIED, starting' "${slog}"
127ls "${work}/spool"/booted-*.job >/dev/null 2>&1 && pass "job retired as booted" || fail "job retired as booted"
128wait_for '^netldr: the Spark answered: nothing queued' "${txt}" 240 || true
129[[ "$(count '^netldr: AIENOS NETLDR v1 ' "${txt}")" -ge 2 ]] && pass "loader came back after the restart" || fail "loader came back after the restart"
130chk "server answers WAIT with nothing queued" '^netldr: the Spark answered: nothing queued; asking again every 5 s$' "${txt}"
131if grep -aqE '^(panic|power: off via ACPI S5)' "${txt}"; then fail "no panic, no power off in stage 1"; else pass "no panic, no power off in stage 1"; fi
132
133echo "== stage 2: tampered job =="
134push "${out}/IMACKERN-LAB.EFI" "${work}/test.key"
135j="${work}/spool/next.job"
136mv "${j}" "${work}/t.job"
137off=$((4096 + 512))
138b="$(od -An -tu1 -j "${off}" -N 1 "${work}/t.job" | tr -d " ")"
139printf "\\x$(printf %02x $((b ^ 0xff)))" | dd of="${work}/t.job" bs=1 seek="${off}" conv=notrunc status=none # invert one image byte
140mv "${work}/t.job" "${j}"
141wait_for '^netldr: REFUSED the network kernel' "${txt}" 120 || true
142chk "tampered image refused" '^netldr: REFUSED the network kernel \(image SHA-512 mismatch\): nothing was started$' "${txt}"
143chk "server saw REFUSED (digest)" 'loader says: REFUSED \(image SHA-512 mismatch\)' "${slog}"
144
145echo "== stage 3: wrong key =="
146push "${out}/IMACKERN-LAB.EFI" "${work}/other.key"
147wait_for '^netldr: REFUSED the network kernel' "${txt}" 120 2 || true
148chk "other key refused" '^netldr: REFUSED the network kernel \(signer_id names no trusted key\): nothing was started$' "${txt}"
149chk "server saw REFUSED (key)" 'loader says: REFUSED \(signer_id names no trusted key\)' "${slog}"
150[[ "$(count '^netldr: starting the network kernel' "${txt}")" == 1 ]] && pass "only the good job ever started" || fail "only the good job ever started"
151
152echo "== stage 4: server gone =="
153wait_for '^netldr: the Spark answered: nothing queued' "${txt}" 60 3 || true
154kill "${spid}" 2>/dev/null || true
155wait "${spid}" 2>/dev/null || true
156spid=""
157self_exit=0
158for _ in $(seq 1 300); do
159 if ! kill -0 "${qpid}" 2>/dev/null; then self_exit=1; break; fi
160 sleep 1
161done
162snap
163chk "fallback after 8 s of silence" '^netldr: no answer from the Spark \(frames sent, nothing came back\); starting the kernel on this stick in 10 s$' "${txt}"
164chk "local kernel started" '^netldr: starting the local kernel \([0-9]+ bytes\)$' "${txt}"
165chk "local (default) kernel powered off through ACPI S5" '^power: off via ACPI S5 ' "${txt}"
166[[ ${self_exit} == 1 ]] && pass "QEMU powered off by itself" || fail "QEMU powered off by itself"
167if grep -aqE '^panic' "${txt}"; then fail "no panic"; else pass "no panic"; fi
168echo "---- server log ----"; cat "${slog}"
169echo "---- serial (netldr and power lines) ----"; grep -aE '^(netldr|power):' "${txt}" || true
170echo "----"
171if [[ ${fails} == 0 ]]; then echo "${tag}: PASS"; exit 0; fi
172echo "${tag}: FAIL (${fails})"
173exit 1