aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / scripts / qemu_ck_imac_netboot_test.sh
| 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) |
| 24 | set -euo pipefail |
| 25 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 26 | cd "${repo_root}" |
| 27 | tag="AIENOS_IMAC_NETBOOT" |
| 28 | code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}" |
| 29 | vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}" |
| 30 | cross="" |
| 31 | if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi |
| 32 | for 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; } |
| 34 | done |
| 35 | [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; } |
| 36 | |
| 37 | . "${repo_root}/scripts/lib_gate_hold.sh" |
| 38 | if ! 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 |
| 41 | fi |
| 42 | work="$(mktemp -d)" |
| 43 | qpid="" |
| 44 | spid="" |
| 45 | cleanup() { |
| 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 | } |
| 51 | trap cleanup EXIT |
| 52 | |
| 53 | out="${work}/out" |
| 54 | commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" |
| 55 | mk() { make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" "$@" >/dev/null; } |
| 56 | mk slot-tool imac-netboot-tool |
| 57 | nb="${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 |
| 60 | mk CK_NETLDR_KEYS="${work}/test.pub" CK_NETLDR_FALLBACK_S=8 imac-netldr |
| 61 | mk CK_X86_REPORT=1 imac-kernel |
| 62 | mk imac-kernel |
| 63 | push() { # image key -> next.job |
| 64 | NETBOOT_TOOL_OUT="${out}" bash scripts/ck_imac_netboot_push.sh "$1" "$2" "${work}/spool" >/dev/null |
| 65 | } |
| 66 | |
| 67 | img="${work}/esp.img" |
| 68 | truncate -s 64M "${img}" |
| 69 | mformat -i "${img}" -F :: |
| 70 | mmd -i "${img}" ::/EFI ::/EFI/BOOT ::/EFI/AIENOS |
| 71 | mcopy -i "${img}" "${out}/NETLDR.EFI" ::/EFI/BOOT/BOOTX64.EFI |
| 72 | mcopy -i "${img}" "${out}/IMACKERN.EFI" ::/EFI/AIENOS/IMACKERN.EFI |
| 73 | |
| 74 | fails=0 |
| 75 | pass() { echo "PASS $1"; } |
| 76 | fail() { echo "FAIL $1"; fails=$((fails + 1)); } |
| 77 | chk() { # description, file, pattern |
| 78 | if grep -aqE -- "$2" "$3"; then pass "$1"; else fail "$1 (no match for: $2)"; fi |
| 79 | } |
| 80 | count() { grep -acE -- "$1" "$2" || true; } |
| 81 | log="${work}/serial.log" |
| 82 | txt="${work}/serial.txt" |
| 83 | slog="${work}/serve.log" |
| 84 | snap() { tr -d '\r' <"${log}" >"${txt}" 2>/dev/null || : >"${txt}"; } |
| 85 | wait_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. |
| 97 | lp=$((40000 + RANDOM % 10000)) |
| 98 | rp=$((lp + 1)) |
| 99 | mkdir -p "${work}/spool" |
| 100 | push "${out}/IMACKERN-LAB.EFI" "${work}/test.key" |
| 101 | "${nb}" serve "${work}/spool" --qemu-dgram "${rp}:${lp}" >"${slog}" 2>&1 & |
| 102 | spid=$! |
| 103 | cp "${vars_fd}" "${work}/vars.fd" |
| 104 | qemu-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}" & |
| 111 | qpid=$! |
| 112 | |
| 113 | echo "== stage 1: good job ==" |
| 114 | wait_for '^netldr: AIENOS NETLDR v1 ' "${txt}" 240 || true |
| 115 | chk "loader started" '^netldr: AIENOS NETLDR v1 commit=[0-9a-f]+ keys=1 fallback=8s$' "${txt}" |
| 116 | wait_for '^power: restart via ' "${txt}" 400 || true |
| 117 | chk "NIC found" '^netldr: nic [0-9]+ of [0-9]+ mac=52:54:00:12:34:56 type=1 ' "${txt}" |
| 118 | chk "cable link seen" '^netldr: nic [0-9]+ link up$' "${txt}" |
| 119 | chk "offer received" '^netldr: the Spark offers a kernel: [0-9]+ bytes$' "${txt}" |
| 120 | chk "signature and digest verified" "^netldr: signature and SHA-512 verified: gen=[0-9]+ commit=${commit:0:12} len=[0-9]+\$" "${txt}" |
| 121 | chk "network kernel started" '^netldr: starting the network kernel \([0-9]+ bytes\)$' "${txt}" |
| 122 | chk "lab kernel ran to DONE" '^AIENOS-KERNEL-0 DONE$' "${txt}" |
| 123 | chk "lab kernel W^X audit" '^wx_audit: ok wx_pages=0 ' "${txt}" |
| 124 | chk "restart through the ACPI reset register" '^power: restart via the ACPI reset register port=0x[0-9a-f]+ value=0x[0-9a-f]+$' "${txt}" |
| 125 | chk "loader told the Spark VERIFIED" '^netldr: told the Spark: verified, starting \([1-3]/3 sent\)$' "${txt}" |
| 126 | chk "server saw VERIFIED" 'loader says: VERIFIED, starting' "${slog}" |
| 127 | ls "${work}/spool"/booted-*.job >/dev/null 2>&1 && pass "job retired as booted" || fail "job retired as booted" |
| 128 | wait_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" |
| 130 | chk "server answers WAIT with nothing queued" '^netldr: the Spark answered: nothing queued; asking again every 5 s$' "${txt}" |
| 131 | if 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 | |
| 133 | echo "== stage 2: tampered job ==" |
| 134 | push "${out}/IMACKERN-LAB.EFI" "${work}/test.key" |
| 135 | j="${work}/spool/next.job" |
| 136 | mv "${j}" "${work}/t.job" |
| 137 | off=$((4096 + 512)) |
| 138 | b="$(od -An -tu1 -j "${off}" -N 1 "${work}/t.job" | tr -d " ")" |
| 139 | printf "\\x$(printf %02x $((b ^ 0xff)))" | dd of="${work}/t.job" bs=1 seek="${off}" conv=notrunc status=none # invert one image byte |
| 140 | mv "${work}/t.job" "${j}" |
| 141 | wait_for '^netldr: REFUSED the network kernel' "${txt}" 120 || true |
| 142 | chk "tampered image refused" '^netldr: REFUSED the network kernel \(image SHA-512 mismatch\): nothing was started$' "${txt}" |
| 143 | chk "server saw REFUSED (digest)" 'loader says: REFUSED \(image SHA-512 mismatch\)' "${slog}" |
| 144 | |
| 145 | echo "== stage 3: wrong key ==" |
| 146 | push "${out}/IMACKERN-LAB.EFI" "${work}/other.key" |
| 147 | wait_for '^netldr: REFUSED the network kernel' "${txt}" 120 2 || true |
| 148 | chk "other key refused" '^netldr: REFUSED the network kernel \(signer_id names no trusted key\): nothing was started$' "${txt}" |
| 149 | chk "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 | |
| 152 | echo "== stage 4: server gone ==" |
| 153 | wait_for '^netldr: the Spark answered: nothing queued' "${txt}" 60 3 || true |
| 154 | kill "${spid}" 2>/dev/null || true |
| 155 | wait "${spid}" 2>/dev/null || true |
| 156 | spid="" |
| 157 | self_exit=0 |
| 158 | for _ in $(seq 1 300); do |
| 159 | if ! kill -0 "${qpid}" 2>/dev/null; then self_exit=1; break; fi |
| 160 | sleep 1 |
| 161 | done |
| 162 | snap |
| 163 | chk "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}" |
| 164 | chk "local kernel started" '^netldr: starting the local kernel \([0-9]+ bytes\)$' "${txt}" |
| 165 | chk "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" |
| 167 | if grep -aqE '^panic' "${txt}"; then fail "no panic"; else pass "no panic"; fi |
| 168 | echo "---- server log ----"; cat "${slog}" |
| 169 | echo "---- serial (netldr and power lines) ----"; grep -aE '^(netldr|power):' "${txt}" || true |
| 170 | echo "----" |
| 171 | if [[ ${fails} == 0 ]]; then echo "${tag}: PASS"; exit 0; fi |
| 172 | echo "${tag}: FAIL (${fails})" |
| 173 | exit 1 |