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_rollback_test.sh
| 1 | #!/usr/bin/env bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # qemu_ck_rollback_test.sh -- CK gate M0_ROLLBACK: one-time BootNext rollback |
| 18 | # with the AIENOS C kernel image as the candidate, in QEMU AArch64 with AAVMF. |
| 19 | # |
| 20 | # Design (ACCEPTED, no A/B slots, ADR 0024 Q3): docs/BOOT_HANDOFF_CONTRACT.md |
| 21 | # section 7 and 7.1, docs/NATIVE_BOOT_ROLLBACK_CONTRACT.md. The Rust |
| 22 | # aienos-rollback-mock stays the stager (\EFI\BOOT\BOOTAA64.EFI: writes |
| 23 | # Boot0001 "Default Linux OS", Boot0000 "AIENOS Candidate", BootOrder=0001, |
| 24 | # BootNext=0000, resets) and the Default entry (\EFI\DEFAULT\default.efi). |
| 25 | # Only the candidate changes: \EFI\AIENOS\candidate.efi is a C kernel image |
| 26 | # built here with make. The Rust-mock run (scripts/qemu_native_rollback_test.sh) |
| 27 | # is untouched and keeps its own labels; this script prints CK_ROLLBACK_* and |
| 28 | # AIENOS_CK_M0_ROLLBACK lines only. |
| 29 | # |
| 30 | # Each case is a lane with its own fresh copy of AAVMF_VARS.fd (the emulated |
| 31 | # UEFI variable store) and its own ESP folder, four boots: |
| 32 | # boot 1 stage the mock stager sets BootNext=0000 and resets |
| 33 | # boot 2 attempt firmware consumes BootNext and starts (or fails to load) |
| 34 | # Boot0000 |
| 35 | # boot 3 default the next boot (for rejected/absent the Default already ran |
| 36 | # in boot 2, so boot 3 is its first repeat) |
| 37 | # boot 4 default one more boot: still Default, no second candidate attempt |
| 38 | # After every boot the host reads the variable store file itself |
| 39 | # (native/kernel/tools/ck_uefi_vars.c, read-only), so the BootNext and |
| 40 | # BootOrder verdicts come from the firmware's own store, not from a guest print. |
| 41 | # |
| 42 | # Cases (lane -> candidate.efi): |
| 43 | # normal the default C core image: runs to "report_kind: final", PSCI reset |
| 44 | # panic TEST-only CK_TEST_ROLLBACK=bad-magic: the kernel refuses its |
| 45 | # handoff ("panic: handoff: bad magic") and resets |
| 46 | # cpu-fault TEST-only CK_TEST_ROLLBACK=cpu-fault: BRK at EL1 after |
| 47 | # "kernel: alive"; fault report ("report_kind: fault", EC 0x3c), reset |
| 48 | # hang TEST-only CK_TEST_ROLLBACK=hang: marker, then spins with |
| 49 | # interrupts masked; the host checks it is still running |
| 50 | # AIENOS_HANG_HOLD seconds later with no reset, then kills QEMU |
| 51 | # (stands in for the operator power-cycle or an outside watchdog; |
| 52 | # the stub disables the UEFI watchdog, there is none in the guest) |
| 53 | # rejected the default C image cut to its first 4096 bytes (PE headers |
| 54 | # intact, sections missing): the firmware must refuse to load it |
| 55 | # absent Boot0000 names a file that does not exist (stager mode absent) |
| 56 | # |
| 57 | # Checks per lane (each its own PASS/FAIL line): |
| 58 | # staged after boot 1 the store holds BootNext=0000, BootOrder=0001, |
| 59 | # Boot0000="AIENOS Candidate" |
| 60 | # attempt boot 2 shows the case's own evidence (above) and the firmware |
| 61 | # line for Boot0000; QEMU ended by reset (exit 0), except hang |
| 62 | # returned the first boot after the attempt runs Boot0001 "Default Linux |
| 63 | # OS" (BdsDxe line and the mock's BOOT_CURRENT=0001) |
| 64 | # once BootNext is absent in the store after boots 2, 3 and 4, and |
| 65 | # the firmware dispatched Boot0000 in boot 2 only |
| 66 | # order BootOrder: 0001 first and Boot0000 never in it after any boot; |
| 67 | # identical after boots 2, 3 and 4 (no later change); and equal |
| 68 | # after boot 2 to the absent lane, where no candidate code ran |
| 69 | # at all (the control). AAVMF itself appends its own |
| 70 | # auto-created options (UiApp, UEFI Misc Device, EFI Internal |
| 71 | # Shell) to BootOrder on the boot after the stager wrote |
| 72 | # BootOrder=0001; the control lane shows that this is the |
| 73 | # firmware, not the candidate. That firmware append is printed |
| 74 | # as an observation (same class as the Machine 1 note in |
| 75 | # docs/M0_NATIVE_BOOT_ROLLBACK_AUDIT.md row 7). |
| 76 | # |
| 77 | # Rows: native/kernel/GATES.md 42-46. QEMU only: a PASS here qualifies |
| 78 | # nothing physical. Never touches a real disk, NVRAM, firmware or key: every |
| 79 | # variable store is a temp copy of AAVMF_VARS.fd, every ESP a temp folder. |
| 80 | # |
| 81 | # Usage: bash scripts/qemu_ck_rollback_test.sh (full run) |
| 82 | # bash scripts/qemu_ck_rollback_test.sh --self-test (judges on canned |
| 83 | # logs and store dumps, no QEMU) |
| 84 | # Environment: AAVMF_CODE, AAVMF_VARS, AIENOS_QEMU_TIMEOUT (per boot, 120 s), |
| 85 | # AIENOS_HANG_HOLD (10 s), AIENOS_QUIET_FLAG (read only), AIENOS_GATE_LOCK / |
| 86 | # AIENOS_GATE_MINUTES (QEMU gate lock, scripts/lib_gate_hold.sh), AIENOS_ROLLBACK_KEEP=DIR (copy |
| 87 | # the lane logs and store dumps there). |
| 88 | # Exit: 0 PASS, 1 FAIL, 2 missing tool, 3 NOT_RUN (quiet flag or lock held). |
| 89 | set -euo pipefail |
| 90 | |
| 91 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 92 | cd "${repo_root}" |
| 93 | verdict_name="AIENOS_CK_M0_ROLLBACK" |
| 94 | |
| 95 | failed=0 |
| 96 | pass() { echo "PASS $1"; } |
| 97 | fail() { echo "FAIL $1"; failed=1; } |
| 98 | |
| 99 | # --------------------------------------------------------------------------- |
| 100 | # Judges. Arguments are file paths; each returns 0 only on its own evidence. |
| 101 | # --------------------------------------------------------------------------- |
| 102 | # vget DUMP KEY -> value of "KEY=value" in a ck_uefi_vars dump, or "missing" |
| 103 | vget() { local v; v="$(sed -n "s/^$2=//p" "$1" 2>/dev/null | head -n1)"; echo "${v:-missing}"; } |
| 104 | # order_ok DUMP: BootOrder present, 0001 first, 0000 nowhere in it |
| 105 | order_ok() { |
| 106 | local o; o="$(vget "$1" BootOrder)" |
| 107 | [[ "${o}" =~ ^0001(,[0-9A-F]{4})*$ ]] && [[ ",${o}," != *",0000,"* ]] |
| 108 | } |
| 109 | staged_ok() { # DUMP after boot 1 |
| 110 | [[ "$(vget "$1" BootNext)" == 0000 && "$(vget "$1" BootOrder)" == 0001 && \ |
| 111 | "$(vget "$1" Boot0000)" == "AIENOS Candidate" && "$(vget "$1" Boot0001)" == "Default Linux OS" ]] |
| 112 | } |
| 113 | starts0() { grep -ac 'BdsDxe: starting Boot0000 "AIENOS Candidate"' "$1" 2>/dev/null || true; } |
| 114 | has() { grep -aqF -- "$2" "$1" 2>/dev/null; } |
| 115 | # no_c_report LOG: no kernel end report of any kind |
| 116 | no_c_end() { ! grep -aqE '^report_kind: (final|panic|fault)' "$1" 2>/dev/null; } |
| 117 | |
| 118 | # attempt_ok CASE LOG RC [HELD] |
| 119 | attempt_ok() { |
| 120 | local case="$1" log="$2" rc="$3" held="${4:-0}" |
| 121 | case "${case}" in |
| 122 | normal) |
| 123 | [[ "${rc}" == 0 && "$(starts0 "${log}")" == 1 ]] && has "${log}" "kernel_impl: c (native/kernel, Lane 18)" && \ |
| 124 | has "${log}" "kernel: alive" && has "${log}" "report_kind: final" && \ |
| 125 | has "${log}" "note: QEMU qualifies nothing physical" && \ |
| 126 | ! grep -aqE '^report_kind: (panic|fault)' "${log}" && ! has "${log}" "TEST-ONLY rollback candidate" ;; |
| 127 | panic) |
| 128 | [[ "${rc}" == 0 && "$(starts0 "${log}")" == 1 ]] && \ |
| 129 | has "${log}" "rollback_test: TEST-ONLY rollback candidate (bad-magic)" && \ |
| 130 | has "${log}" "report_kind: panic" && grep -aqx 'panic: handoff: bad magic' <(tr -d '\r' <"${log}") && \ |
| 131 | ! has "${log}" "kernel: alive" && ! has "${log}" "report_kind: final" ;; |
| 132 | cpu-fault) |
| 133 | [[ "${rc}" == 0 && "$(starts0 "${log}")" == 1 ]] && has "${log}" "kernel: alive" && \ |
| 134 | has "${log}" "rollback_test: TEST-ONLY rollback candidate (cpu-fault)" && \ |
| 135 | has "${log}" "report_kind: fault" && grep -aqE '^fault: vector=sync_el_spx el=1 .* ec=0x3c ' "${log}" && \ |
| 136 | ! has "${log}" "report_kind: final" ;; |
| 137 | hang) |
| 138 | [[ "${held}" == 1 && "$(starts0 "${log}")" == 1 ]] && has "${log}" "kernel: alive" && \ |
| 139 | has "${log}" "rollback_test: TEST-ONLY rollback candidate (hang)" && no_c_end "${log}" ;; |
| 140 | rejected|absent) |
| 141 | [[ "${rc}" == 0 && "$(starts0 "${log}")" == 0 ]] && has "${log}" "failed to load Boot0000" && \ |
| 142 | ! has "${log}" "kernel_impl: c" && has "${log}" 'BdsDxe: starting Boot0001 "Default Linux OS"' ;; |
| 143 | *) return 1 ;; |
| 144 | esac |
| 145 | } |
| 146 | returned_ok() { # LOG of the first boot that must be the Default |
| 147 | has "$1" 'BdsDxe: starting Boot0001 "Default Linux OS"' && has "$1" "DEFAULT_OS: BOOT_CURRENT=0001" |
| 148 | } |
| 149 | # once_ok CASE V2 V3 V4 LOG2 LOG3 LOG4 |
| 150 | once_ok() { |
| 151 | local case="$1" want=1 |
| 152 | [[ "${case}" == rejected || "${case}" == absent ]] && want=0 |
| 153 | [[ "$(vget "$2" BootNext)" == absent && "$(vget "$3" BootNext)" == absent && "$(vget "$4" BootNext)" == absent ]] && \ |
| 154 | [[ "$(starts0 "$5")" == "${want}" && "$(starts0 "$6")" == 0 && "$(starts0 "$7")" == 0 ]] && \ |
| 155 | ! has "$6" "loading Boot0000" && ! has "$7" "loading Boot0000" |
| 156 | } |
| 157 | # order_lane_ok V1 V2 V3 V4 CONTROL_V2 |
| 158 | order_lane_ok() { |
| 159 | local o2; o2="$(vget "$2" BootOrder)" |
| 160 | order_ok "$1" && order_ok "$2" && order_ok "$3" && order_ok "$4" && \ |
| 161 | [[ "$(vget "$3" BootOrder)" == "${o2}" && "$(vget "$4" BootOrder)" == "${o2}" ]] && \ |
| 162 | [[ -f "$5" && "$(vget "$5" BootOrder)" == "${o2}" ]] |
| 163 | } |
| 164 | |
| 165 | if [[ "${1:-}" == --self-test ]]; then |
| 166 | st=0; t="$(mktemp -d)"; trap 'rm -rf "${t}"' EXIT |
| 167 | expect() { # label want(0|1) cmd... |
| 168 | local label="$1" want="$2" rc=0; shift 2 |
| 169 | "$@" || rc=$?; [[ "${rc}" != 0 ]] && rc=1 |
| 170 | if [[ "${rc}" == "${want}" ]]; then echo "PASS ${label}"; else echo "FAIL ${label} (wanted ${want}, got ${rc})"; st=1; fi |
| 171 | } |
| 172 | printf 'uefi_vars: store=authenticated live=17 obsolete=6\nBootOrder=0001\nBootNext=0000\nBoot0000=AIENOS Candidate\nBoot0001=Default Linux OS\n' >"${t}/v1" |
| 173 | printf 'uefi_vars: store=authenticated live=18 obsolete=14\nBootOrder=0001,0002,0003,0004\nBootNext=absent\nBoot0000=AIENOS Candidate\nBoot0001=Default Linux OS\n' >"${t}/v2" |
| 174 | cp "${t}/v2" "${t}/ctl" |
| 175 | expect "staged store accepted" 0 staged_ok "${t}/v1" |
| 176 | expect "unstaged store (BootNext absent) refused as staged" 1 staged_ok "${t}/v2" |
| 177 | sed 's/^BootOrder=0001$/BootOrder=0000,0001/' "${t}/v1" >"${t}/v1p" |
| 178 | expect "stager that promoted Boot0000 refused as staged" 1 staged_ok "${t}/v1p" |
| 179 | expect "order: settled lane equal to control accepted" 0 order_lane_ok "${t}/v1" "${t}/v2" "${t}/v2" "${t}/v2" "${t}/ctl" |
| 180 | sed 's/^BootOrder=.*/BootOrder=0000,0001,0002,0003,0004/' "${t}/v2" >"${t}/vprom" |
| 181 | expect "order: Boot0000 promoted into BootOrder refused" 1 order_lane_ok "${t}/v1" "${t}/vprom" "${t}/vprom" "${t}/vprom" "${t}/vprom" |
| 182 | sed 's/^BootOrder=.*/BootOrder=0002,0001,0003,0004/' "${t}/v2" >"${t}/vfirst" |
| 183 | expect "order: Default no longer first refused" 1 order_lane_ok "${t}/v1" "${t}/v2" "${t}/vfirst" "${t}/v2" "${t}/ctl" |
| 184 | sed 's/^BootOrder=.*/BootOrder=0001,0003/' "${t}/v2" >"${t}/vdiff" |
| 185 | expect "order: lane differs from the control refused" 1 order_lane_ok "${t}/v1" "${t}/vdiff" "${t}/vdiff" "${t}/vdiff" "${t}/ctl" |
| 186 | expect "order: later boot changed BootOrder refused" 1 order_lane_ok "${t}/v1" "${t}/v2" "${t}/v2" "${t}/vdiff" "${t}/ctl" |
| 187 | expect "order: missing control refused" 1 order_lane_ok "${t}/v1" "${t}/v2" "${t}/v2" "${t}/v2" "${t}/nocontrol" |
| 188 | sed 's/^BootNext=absent$/BootNext=0000/' "${t}/v2" >"${t}/vnext" |
| 189 | good2='BdsDxe: starting Boot0000 "AIENOS Candidate" from x' |
| 190 | printf '%s\n' "${good2}" >"${t}/l2"; printf 'BdsDxe: starting Boot0001 "Default Linux OS" from x\n' >"${t}/l3" |
| 191 | expect "once: BootNext gone after boots 2-4, one dispatch accepted" 0 once_ok normal "${t}/v2" "${t}/v2" "${t}/v2" "${t}/l2" "${t}/l3" "${t}/l3" |
| 192 | expect "once: BootNext still set after boot 2 refused" 1 once_ok normal "${t}/vnext" "${t}/v2" "${t}/v2" "${t}/l2" "${t}/l3" "${t}/l3" |
| 193 | expect "once: BootNext back after boot 4 refused" 1 once_ok normal "${t}/v2" "${t}/v2" "${t}/vnext" "${t}/l2" "${t}/l3" "${t}/l3" |
| 194 | expect "once: second dispatch in boot 3 refused" 1 once_ok normal "${t}/v2" "${t}/v2" "${t}/v2" "${t}/l2" "${t}/l2" "${t}/l3" |
| 195 | expect "once: rejected lane must show no dispatch" 1 once_ok rejected "${t}/v2" "${t}/v2" "${t}/v2" "${t}/l2" "${t}/l3" "${t}/l3" |
| 196 | # attempt judges on canned serial logs |
| 197 | { echo "${good2}"; echo "kernel_impl: c (native/kernel, Lane 18)"; echo "kernel: alive"; echo "report_kind: final"; echo "note: QEMU qualifies nothing physical"; } >"${t}/normal" |
| 198 | expect "attempt normal: final report accepted" 0 attempt_ok normal "${t}/normal" 0 |
| 199 | expect "attempt normal: QEMU timeout (exit 124) refused" 1 attempt_ok normal "${t}/normal" 124 |
| 200 | { cat "${t}/normal"; echo "report_kind: panic"; } >"${t}/normalp" |
| 201 | expect "attempt normal: a panic in the log refused" 1 attempt_ok normal "${t}/normalp" 0 |
| 202 | grep -v 'report_kind: final' "${t}/normal" >"${t}/nofinal" |
| 203 | expect "attempt normal: no final report refused" 1 attempt_ok normal "${t}/nofinal" 0 |
| 204 | { echo "${good2}"; echo "rollback_test: TEST-ONLY rollback candidate (bad-magic): x"; echo "report_kind: panic"; printf 'panic: handoff: bad magic\r\n'; } >"${t}/panic" |
| 205 | expect "attempt panic: bad magic refusal accepted" 0 attempt_ok panic "${t}/panic" 0 |
| 206 | sed 's/bad magic/unsupported version 2 (kernel reads CHANDOF3 only)/' "${t}/panic" >"${t}/panicv" |
| 207 | expect "attempt panic: a different refusal reason refused" 1 attempt_ok panic "${t}/panicv" 0 |
| 208 | { echo "${good2}"; echo "kernel: alive"; echo "rollback_test: TEST-ONLY rollback candidate (cpu-fault): x"; echo "report_kind: fault"; echo "fault: vector=sync_el_spx el=1 esr=0xf200007b ec=0x3c far=0x0 elr=0x1"; } >"${t}/fault" |
| 209 | expect "attempt cpu-fault: BRK fault report accepted" 0 attempt_ok cpu-fault "${t}/fault" 0 |
| 210 | sed 's/ec=0x3c/ec=0x25/' "${t}/fault" >"${t}/faultdab" |
| 211 | expect "attempt cpu-fault: another exception class refused" 1 attempt_ok cpu-fault "${t}/faultdab" 0 |
| 212 | { echo "${good2}"; echo "kernel: alive"; echo "rollback_test: TEST-ONLY rollback candidate (hang): x"; } >"${t}/hang" |
| 213 | expect "attempt hang: held and silent accepted" 0 attempt_ok hang "${t}/hang" 137 1 |
| 214 | expect "attempt hang: QEMU ended by itself refused" 1 attempt_ok hang "${t}/hang" 0 0 |
| 215 | { cat "${t}/hang"; echo "report_kind: final"; } >"${t}/hangend" |
| 216 | expect "attempt hang: an end report after the marker refused" 1 attempt_ok hang "${t}/hangend" 137 1 |
| 217 | { echo 'BdsDxe: failed to load Boot0000 "AIENOS Candidate" from x: Not Found'; echo 'BdsDxe: starting Boot0001 "Default Linux OS" from x'; } >"${t}/absent" |
| 218 | expect "attempt absent: load failure then Default accepted" 0 attempt_ok absent "${t}/absent" 0 |
| 219 | { cat "${t}/absent"; echo "${good2}"; } >"${t}/absentran" |
| 220 | expect "attempt absent: a dispatched Boot0000 refused" 1 attempt_ok absent "${t}/absentran" 0 |
| 221 | printf 'BdsDxe: starting Boot0001 "Default Linux OS" from x\nDEFAULT_OS: BOOT_CURRENT=0001\n' >"${t}/l3d" |
| 222 | expect "returned: Default boot accepted" 0 returned_ok "${t}/l3d" |
| 223 | expect "returned: candidate boot again refused" 1 returned_ok "${t}/l2" |
| 224 | if [[ ${st} == 0 ]]; then echo "CK_ROLLBACK_SELF_TEST: PASS"; exit 0; fi |
| 225 | echo "CK_ROLLBACK_SELF_TEST: FAIL"; exit 1 |
| 226 | fi |
| 227 | |
| 228 | # --------------------------------------------------------------------------- |
| 229 | # Full run |
| 230 | # --------------------------------------------------------------------------- |
| 231 | code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" |
| 232 | vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" |
| 233 | qemu_timeout="${AIENOS_QEMU_TIMEOUT:-120}" |
| 234 | hang_hold="${AIENOS_HANG_HOLD:-10}" |
| 235 | command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${verdict_name}: NOT_RUN"; exit 2; } |
| 236 | [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict_name}: NOT_RUN"; exit 2; } |
| 237 | command -v cargo >/dev/null || { echo "cargo not installed (the stager/Default mock is Rust)"; echo "${verdict_name}: NOT_RUN"; exit 2; } |
| 238 | |
| 239 | # Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): |
| 240 | # scripts/lib_gate_hold.sh (aienos#278). |
| 241 | . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" |
| 242 | if ! gh_quiet_check; then |
| 243 | echo "NOT_RUN ${gh_why}" |
| 244 | echo "${verdict_name}: NOT_RUN" |
| 245 | exit 3 |
| 246 | fi |
| 247 | if ! gh_lock_take qemu_ck_rollback_test "${AIENOS_GATE_MINUTES:-60}"; then |
| 248 | echo "NOT_RUN ${gh_why}" |
| 249 | echo "${verdict_name}: NOT_RUN" |
| 250 | exit 3 |
| 251 | fi |
| 252 | # Release only this run's own gate lock record, at most once. |
| 253 | release_lock() { gh_lock_release; } |
| 254 | work="$(mktemp -d)" |
| 255 | qemu_pid="" |
| 256 | cleanup() { |
| 257 | if [[ -n "${qemu_pid}" ]]; then kill -9 "${qemu_pid}" 2>/dev/null || true; fi |
| 258 | if [[ -n "${AIENOS_ROLLBACK_KEEP:-}" ]]; then mkdir -p "${AIENOS_ROLLBACK_KEEP}" && cp -r "${work}/lanes" "${AIENOS_ROLLBACK_KEEP}/" 2>/dev/null || true; fi |
| 259 | rm -rf "${work}"; release_lock |
| 260 | } |
| 261 | trap cleanup EXIT |
| 262 | |
| 263 | cross="" |
| 264 | if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi |
| 265 | commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" |
| 266 | echo "============================================================" |
| 267 | echo "AIENOS CK M0_ROLLBACK: C kernel image as the one-time BootNext candidate (QEMU)" |
| 268 | echo "commit: ${commit}$( [[ -z "$(git status --porcelain 2>/dev/null)" ]] || echo ' (dirty tree)')" |
| 269 | echo "firmware: $(basename "${code_fd}") vars template sha256 $(sha256sum "${vars_fd}" | cut -c1-64)" |
| 270 | echo "============================================================" |
| 271 | |
| 272 | echo "building aienos-rollback-mock (stager and Default, unchanged Rust mock) ..." |
| 273 | cargo build --quiet --release -p aienos-boot --target aarch64-unknown-uefi --features handoff --bin aienos-rollback-mock |
| 274 | # cargo writes under CARGO_TARGET_DIR when the caller sets one (the local sandbox does); the |
| 275 | # hosted job leaves it unset and gets the repository's own target/. |
| 276 | mock="${CARGO_TARGET_DIR:-target}/aarch64-unknown-uefi/release/aienos-rollback-mock.efi" |
| 277 | [[ -f "${mock}" ]] || { echo "mock build failed"; echo "${verdict_name}: FAIL"; exit 1; } |
| 278 | |
| 279 | echo "building the C kernel images ..." |
| 280 | out_def="${repo_root}/target/native-kernel" |
| 281 | make -s -C native/kernel CROSS="${cross}" OUT="${out_def}" AIENOS_COMMIT="${commit}" >/dev/null |
| 282 | make -s -C native/kernel OUT="${out_def}" uefi-vars >/dev/null |
| 283 | vars_tool="${out_def}/host/ck_uefi_vars" |
| 284 | declare -A img=([normal]="${out_def}/BOOTAA64.EFI") |
| 285 | for m in bad-magic cpu-fault hang; do |
| 286 | o="${repo_root}/target/native-kernel-test-rollback-${m}" |
| 287 | make -s -C native/kernel CROSS="${cross}" OUT="${o}" AIENOS_COMMIT="${commit}" CK_TEST_ROLLBACK="${m}" >/dev/null |
| 288 | img[${m}]="${o}/BOOTAA64.EFI" |
| 289 | done |
| 290 | img[panic]="${img[bad-magic]}" |
| 291 | head -c 4096 "${img[normal]}" >"${work}/truncated.efi" |
| 292 | img[rejected]="${work}/truncated.efi" |
| 293 | for k in normal panic cpu-fault hang rejected; do |
| 294 | echo "candidate ${k}: sha256 $(sha256sum "${img[${k}]}" | cut -c1-64) bytes $(stat -c %s "${img[${k}]}")" |
| 295 | done |
| 296 | if grep -aqF "TEST-ONLY rollback candidate" "${img[normal]}"; then |
| 297 | fail "the default C image carries a TEST-only rollback candidate" |
| 298 | fi |
| 299 | if "${vars_tool}" --self-test >"${work}/vars_selftest.log" 2>&1; then |
| 300 | pass "ck_uefi_vars self-test ($(grep -c '^PASS' "${work}/vars_selftest.log") cases)" |
| 301 | else |
| 302 | cat "${work}/vars_selftest.log"; fail "ck_uefi_vars self-test" |
| 303 | fi |
| 304 | |
| 305 | qemu_args() { # VARS ESP LOG |
| 306 | echo -M virt,virtualization=on,gic-version=3 -accel tcg,thread=single -cpu max -smp 2 -m 512 \ |
| 307 | -drive "if=pflash,format=raw,readonly=on,file=${code_fd}" \ |
| 308 | -drive "if=pflash,format=raw,file=$1" \ |
| 309 | -drive "if=none,id=esp,format=raw,file=fat:rw:$2" \ |
| 310 | -device virtio-blk-pci,drive=esp -display none -nic none -serial "file:$3" -no-reboot |
| 311 | } |
| 312 | boot() { # LANE N -> sets boot_rc; dumps the store to LANE/vN |
| 313 | local d="$1" n="$2" |
| 314 | set +e |
| 315 | # shellcheck disable=SC2046 |
| 316 | timeout "${qemu_timeout}" qemu-system-aarch64 $(qemu_args "${d}/vars.fd" "${d}/esp" "${d}/boot${n}.log") </dev/null >"${d}/qemu${n}.out" 2>&1 |
| 317 | boot_rc=$? |
| 318 | set -e |
| 319 | "${vars_tool}" "${d}/vars.fd" >"${d}/v${n}" 2>&1 || true |
| 320 | } |
| 321 | hang_held=0 |
| 322 | boot_hang() { # LANE N: start, wait for the marker, hold, then force the reset |
| 323 | local d="$1" n="$2" log="$1/boot$2.log" waited=0 i |
| 324 | hang_held=0 |
| 325 | # shellcheck disable=SC2046 |
| 326 | qemu-system-aarch64 $(qemu_args "${d}/vars.fd" "${d}/esp" "${log}") </dev/null >"${d}/qemu${n}.out" 2>&1 & |
| 327 | qemu_pid=$! |
| 328 | for ((i = 0; i < qemu_timeout * 2; i++)); do |
| 329 | sleep 0.5 |
| 330 | if grep -aqF "rollback_test: TEST-ONLY rollback candidate (hang)" "${log}" 2>/dev/null; then waited=1; break; fi |
| 331 | kill -0 "${qemu_pid}" 2>/dev/null || break |
| 332 | done |
| 333 | if [[ "${waited}" == 1 ]]; then |
| 334 | sleep "${hang_hold}" |
| 335 | if kill -0 "${qemu_pid}" 2>/dev/null && no_c_end "${log}"; then |
| 336 | hang_held=1 |
| 337 | echo "hang: QEMU still running ${hang_hold} s after the hang marker, no end report; host forces the reset" |
| 338 | fi |
| 339 | fi |
| 340 | kill -9 "${qemu_pid}" 2>/dev/null || true |
| 341 | wait "${qemu_pid}" 2>/dev/null || true |
| 342 | qemu_pid="" |
| 343 | boot_rc=137 |
| 344 | "${vars_tool}" "${d}/vars.fd" >"${d}/v${n}" 2>&1 || true |
| 345 | } |
| 346 | |
| 347 | lanes=(absent normal panic cpu-fault hang rejected) # absent first: it is the control |
| 348 | declare -A verdict=() |
| 349 | all_once=PASS; all_order=PASS |
| 350 | for lane in "${lanes[@]}"; do |
| 351 | d="${work}/lanes/${lane}" |
| 352 | mkdir -p "${d}/esp/EFI/BOOT" "${d}/esp/EFI/AIENOS" "${d}/esp/EFI/DEFAULT" |
| 353 | cp "${vars_fd}" "${d}/vars.fd" |
| 354 | cp "${mock}" "${d}/esp/EFI/BOOT/BOOTAA64.EFI" |
| 355 | cp "${mock}" "${d}/esp/EFI/DEFAULT/default.efi" |
| 356 | if [[ "${lane}" == absent ]]; then |
| 357 | echo absent >"${d}/esp/EFI/AIENOS/ROLLBACK_MODE.TXT" |
| 358 | else |
| 359 | echo normal >"${d}/esp/EFI/AIENOS/ROLLBACK_MODE.TXT" |
| 360 | cp "${img[${lane}]}" "${d}/esp/EFI/AIENOS/candidate.efi" |
| 361 | fi |
| 362 | echo "" |
| 363 | echo "--- [lane ${lane}] ---" |
| 364 | "${vars_tool}" "${d}/vars.fd" >"${d}/v0" 2>&1 || true |
| 365 | boot "${d}" 1; rc1=${boot_rc} |
| 366 | if [[ "${lane}" == hang ]]; then boot_hang "${d}" 2; else boot "${d}" 2; fi |
| 367 | rc2=${boot_rc} |
| 368 | boot "${d}" 3; rc3=${boot_rc} |
| 369 | boot "${d}" 4; rc4=${boot_rc} |
| 370 | echo "qemu exits: stage=${rc1} attempt=${rc2} next=${rc3} repeat=${rc4}" |
| 371 | echo "store after boot 1: $(grep -E '^Boot(Order|Next)=' "${d}/v1" | paste -sd' ')" |
| 372 | echo "store after boot 2: $(grep -E '^Boot(Order|Next)=' "${d}/v2" | paste -sd' ')" |
| 373 | echo "store after boot 4: $(grep -E '^Boot(Order|Next)=' "${d}/v4" | paste -sd' ')" |
| 374 | first_default="${d}/boot3.log" |
| 375 | [[ "${lane}" == rejected || "${lane}" == absent ]] && first_default="${d}/boot2.log" |
| 376 | ok=1 |
| 377 | staged_ok "${d}/v1" && pass "${lane}: staged (BootNext=0000, BootOrder=0001, Boot0000 \"AIENOS Candidate\", read from the store)" \ |
| 378 | || { fail "${lane}: staged"; ok=0; } |
| 379 | attempt_ok "${lane}" "${d}/boot2.log" "${rc2}" "${hang_held}" && pass "${lane}: attempt evidence in boot 2" \ |
| 380 | || { fail "${lane}: attempt evidence in boot 2 (exit ${rc2})"; ok=0; } |
| 381 | returned_ok "${first_default}" && pass "${lane}: next boot is Boot0001 Default (BOOT_CURRENT=0001)" \ |
| 382 | || { fail "${lane}: return to Default"; ok=0; } |
| 383 | returned_ok "${d}/boot4.log" && pass "${lane}: repeat boot is still Default" || { fail "${lane}: repeat boot"; ok=0; } |
| 384 | once_ok "${lane}" "${d}/v2" "${d}/v3" "${d}/v4" "${d}/boot2.log" "${d}/boot3.log" "${d}/boot4.log" && \ |
| 385 | pass "${lane}: BootNext consumed once (absent in the store after boots 2-4; Boot0000 dispatched $( [[ ${lane} == rejected || ${lane} == absent ]] && echo 'never, load refused once' || echo 'in boot 2 only'))" \ |
| 386 | || { fail "${lane}: BootNext consumed exactly once"; ok=0; all_once=FAIL; } |
| 387 | order_lane_ok "${d}/v1" "${d}/v2" "${d}/v3" "${d}/v4" "${work}/lanes/absent/v2" && \ |
| 388 | pass "${lane}: BootOrder 0001 first, no 0000, same after boots 2-4 and equal to the no-candidate control" \ |
| 389 | || { fail "${lane}: BootOrder"; ok=0; all_order=FAIL; } |
| 390 | [[ "$(vget "${d}/v1" BootOrder)" != "$(vget "${d}/v2" BootOrder)" ]] && \ |
| 391 | echo "observation: AAVMF rewrote BootOrder $(vget "${d}/v1" BootOrder) -> $(vget "${d}/v2" BootOrder) in boot 2 (firmware auto-created options; the control lane shows the same)" |
| 392 | verdict[${lane}]=$([[ ${ok} == 1 ]] && echo PASS || echo FAIL) |
| 393 | done |
| 394 | |
| 395 | echo "" |
| 396 | echo "============================================================" |
| 397 | echo "Evidence kind: C-image, QEMU (AAVMF). Physical: NOT_RUN." |
| 398 | echo "CK_ROLLBACK_NORMAL: ${verdict[normal]}" |
| 399 | echo "CK_ROLLBACK_FAULT_PANIC: ${verdict[panic]}" |
| 400 | echo "CK_ROLLBACK_FAULT_CPU: ${verdict[cpu-fault]}" |
| 401 | echo "CK_ROLLBACK_TIMEOUT: ${verdict[hang]}" |
| 402 | echo "CK_ROLLBACK_REJECTED: ${verdict[rejected]}" |
| 403 | echo "CK_ROLLBACK_ABSENT: ${verdict[absent]}" |
| 404 | echo "CK_ROLLBACK_BOOTNEXT_CONSUMED: ${all_once}" |
| 405 | echo "CK_ROLLBACK_DEFAULT_UNCHANGED: ${all_order}" |
| 406 | if [[ "${failed}" == 0 ]]; then |
| 407 | echo "${verdict_name}: PASS (QEMU only, C kernel image as candidate; physical NOT_RUN)" |
| 408 | exit 0 |
| 409 | fi |
| 410 | echo "${verdict_name}: FAIL" |
| 411 | exit 1 |