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 / native_boot_preflight.sh

157 lines · 10224 bytes Raw Blame History
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# native_boot_preflight.sh -- READ-ONLY preflight before an attended one-time
18# native AIENOS boot on the Spark (docs/native/PREFLIGHT-AND-RECOVERY.md
19# section 2). It only reads: `efibootmgr` with no arguments, the SecureBoot
20# EFI variable file, findmnt, git status, the image file and the quiet flag.
21# It never writes a variable, never stages, never reboots, needs no sudo.
22#
23# Usage: bash scripts/native_boot_preflight.sh [--image EFI [--expect-sha256 HEX]]
24# bash scripts/native_boot_preflight.sh --self-test (canned inputs, no machine state)
25#
26# Checks, one line each (PASS / STOP / INFO); the last line is
27# NATIVE_PREFLIGHT: READY every STOP-class check passed
28# NATIVE_PREFLIGHT: NOT_READY at least one STOP line (exit 1)
29# Items Drake does in person (at the machine, display and keyboard attached,
30# fresh go-ahead for this boot) cannot be read by a script: they are printed
31# as ASK lines and never counted as passed.
32#
33# Environment (for the self-test and for reading a saved capture):
34# AIENOS_PREFLIGHT_EFIBOOTMGR file holding saved `efibootmgr` output
35# AIENOS_PREFLIGHT_SECUREBOOT path of the SecureBoot variable file
36# AIENOS_PREFLIGHT_ESP ESP mount point (default /boot/efi)
37# AIENOS_QUIET_FLAG quiet flag path (default ~/workspace/.spark-quiet)
38set -uo pipefail
39
40repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
41default_entry="0001"
42# The recovery entry is identified by its partition GUID, never by its number: the same stick
43# was Boot0004 on 2026-09-25 and is Boot0002 on 2026-10-28 (docs/campaigns/B7B-PHYSICAL-BOOT-PREP.md).
44recov_partuuid="${AIENOS_RECOVERY_PARTUUID:-f227357c-1e80-4fd0-a55e-2efbef1dd25c}"
45recov_label="AIENOSRECOV"
46sb_default="/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c"
47
48# preflight EFIBOOTMGR_TEXT SB_FILE ESP QUIET_FLAG TREE_STATUS IMAGE EXPECT
49# Prints the check lines; returns 0 READY, 1 NOT_READY.
50preflight() {
51 local boot="$1" sb="$2" esp="$3" quiet="$4" tree="$5" image="$6" expect="$7" stops=0
52 stop() { echo "STOP $*"; stops=$((stops + 1)); }
53 ok() { echo "PASS $*"; }
54 echo "ASK Drake is at the machine with a display and keyboard attached, and gave a fresh go-ahead for this boot"
55 if [[ -z "${boot}" ]]; then
56 stop "efibootmgr gave no output (not a UEFI Linux host, or efibootmgr missing)"
57 else
58 local order next current recov
59 order="$(sed -n 's/^BootOrder: //p' <<<"${boot}")"
60 next="$(sed -n 's/^BootNext: //p' <<<"${boot}")"
61 current="$(sed -n 's/^BootCurrent: //p' <<<"${boot}")"
62 recov="$(grep -iE "^Boot[0-9A-F]{4}\*? .*GPT,${recov_partuuid}," <<<"${boot}" || true)"
63 echo "INFO BootCurrent=${current:-?} BootOrder=${order:-?}"
64 [[ "${recov}" == *"${recov_label}"* && $(grep -c . <<<"${recov}") == 1 ]] && ok "recovery stick: ${recov%%\**} ${recov_label} PARTUUID ${recov_partuuid} listed" \
65 || stop "recovery stick: exactly one ${recov_label} entry with PARTUUID ${recov_partuuid} not listed (got '${recov:-none}'); see docs/RECOVERY_MEDIA_MACHINE1.md"
66 [[ "${order%%,*}" == "${default_entry}" ]] && ok "BootOrder starts with ${default_entry} (Linux, the permanent default)" \
67 || stop "BootOrder does not start with ${default_entry} (got '${order:-none}')"
68 [[ -z "${next}" ]] && ok "BootNext is not set (no other one-time boot pending)" \
69 || stop "BootNext is already set (${next}); another staging is pending"
70 [[ "${current}" == "${default_entry}" ]] && ok "running from Boot${default_entry} (Linux)" \
71 || stop "BootCurrent is '${current:-?}', not ${default_entry}: not booted from the default entry"
72 fi
73 if [[ -r "${sb}" ]]; then
74 local b; b="$(od -An -t u1 -j 4 -N 1 "${sb}" | tr -d ' ')"
75 case "${b}" in
76 0) echo "INFO Secure Boot: off (recorded only; development decision 2026-10-01; a release needs it on; never flip it here)" ;;
77 1) stop "Secure Boot: on; the unsigned C image would be refused (Secure Boot Violation); do not turn it off as part of this procedure" ;;
78 *) stop "Secure Boot variable unreadable (byte 4 = '${b}')" ;;
79 esac
80 else
81 stop "Secure Boot variable ${sb} not readable"
82 fi
83 if [[ -n "$(findmnt -no SOURCE "${esp}" 2>/dev/null)" ]]; then ok "ESP mounted at ${esp}"; else stop "ESP not mounted at ${esp}"; fi
84 if [[ -e "${quiet}" ]]; then stop "quiet flag ${quiet} is held: $(head -c 120 "${quiet}" 2>/dev/null)"; else ok "no quiet flag held (no other hardware run)"; fi
85 if [[ -z "${tree}" ]]; then ok "working tree clean"; else stop "working tree not clean ($(wc -l <<<"${tree}") entries): the image must map to one commit"; fi
86 if [[ -n "${image}" ]]; then
87 if [[ -r "${image}" ]]; then
88 local d; d="$(sha256sum "${image}" | cut -c1-64)"
89 echo "INFO image ${image} sha256=${d}"
90 if [[ -n "${expect}" ]]; then
91 [[ "${d}" == "${expect,,}" ]] && ok "image digest equals the expected digest (from scripts/ck_repro_build.sh)" \
92 || stop "image digest ${d} differs from the expected ${expect}"
93 else
94 echo "ASK compare this digest with the CK_REPRO_BUILD line for this commit (--expect-sha256 checks it)"
95 fi
96 else
97 stop "image ${image} not readable"
98 fi
99 fi
100 echo "ASK the candidate is the one-time BootNext entry; Linux (Boot${default_entry}) stays first in BootOrder; the stage script makes the pre-boot capture itself"
101 if [[ ${stops} == 0 ]]; then echo "NATIVE_PREFLIGHT: READY (read-only checks only; the ASK lines are Drake's)"; return 0; fi
102 echo "NATIVE_PREFLIGHT: NOT_READY (${stops} STOP)"
103 return 1
104}
105
106if [[ "${1:-}" == --self-test ]]; then
107 st=0; t="$(mktemp -d)"; trap 'rm -rf "${t}"' EXIT
108 good=$'BootCurrent: 0001\nTimeout: 5 seconds\nBootOrder: 0001,0003\nBoot0001* ubuntu\nBoot0003* UEFI PXE\nBoot0002* AIENOSRECOV\tHD(1,GPT,f227357c-1e80-4fd0-a55e-2efbef1dd25c,0x800,0x1df000)/File(\\EFI\\BOOT\\BOOTAA64.EFI)'
109 printf '\0\0\0\0\0' >"${t}/sb_off"; printf '\0\0\0\0\1' >"${t}/sb_on"
110 esp="/" # any mounted path stands in for the ESP in the self-test
111 printf 'img' >"${t}/img"; dig="$(sha256sum "${t}/img" | cut -c1-64)"
112 case_() { # label want(0|1) boot sb quiet tree image expect
113 local rc=0; preflight "$3" "$4" "${esp}" "$5" "$6" "$7" "$8" >"${t}/out" 2>&1 || rc=1
114 if [[ "${rc}" == "$2" ]]; then echo "PASS $1"; else echo "FAIL $1 (wanted $2, got ${rc})"; cat "${t}/out"; st=1; fi
115 }
116 case_ "good state is READY" 0 "${good}" "${t}/sb_off" "${t}/noflag" "" "${t}/img" "${dig}"
117 case_ "BootNext pending -> NOT_READY" 1 "${good}"$'\nBootNext: 0000' "${t}/sb_off" "${t}/noflag" "" "" ""
118 case_ "recovery stick absent -> NOT_READY" 1 "${good/Boot0002\* AIENOSRECOV/}" "${t}/sb_off" "${t}/noflag" "" "" ""
119 case_ "AIENOSRECOV label on another PARTUUID -> NOT_READY" 1 "${good/f227357c/0badc0de}" "${t}/sb_off" "${t}/noflag" "" "" ""
120 case_ "recovery entry renumbered (Boot0004) is still READY" 0 "${good/Boot0002/Boot0004}" "${t}/sb_off" "${t}/noflag" "" "${t}/img" "${dig}"
121 case_ "BootOrder not Linux first -> NOT_READY" 1 "${good/BootOrder: 0001,0003/BootOrder: 0000,0001}" "${t}/sb_off" "${t}/noflag" "" "" ""
122 case_ "booted from another entry -> NOT_READY" 1 "${good/BootCurrent: 0001/BootCurrent: 0004}" "${t}/sb_off" "${t}/noflag" "" "" ""
123 case_ "Secure Boot on -> NOT_READY" 1 "${good}" "${t}/sb_on" "${t}/noflag" "" "" ""
124 case_ "Secure Boot variable missing -> NOT_READY" 1 "${good}" "${t}/nosb" "${t}/noflag" "" "" ""
125 touch "${t}/flag"
126 case_ "quiet flag held -> NOT_READY" 1 "${good}" "${t}/sb_off" "${t}/flag" "" "" ""
127 case_ "dirty tree -> NOT_READY" 1 "${good}" "${t}/sb_off" "${t}/noflag" " M x" "" ""
128 case_ "image digest mismatch -> NOT_READY" 1 "${good}" "${t}/sb_off" "${t}/noflag" "" "${t}/img" "00${dig:2}"
129 case_ "no efibootmgr output -> NOT_READY" 1 "" "${t}/sb_off" "${t}/noflag" "" "" ""
130 # The script must stay read-only: no variable-writing efibootmgr flag, no sudo, no reboot.
131 if grep -nE '(^|[^#]*)(efibootmgr +-[a-zA-Z]|--bootnext|--bootorder|--create|sudo |reboot|systemctl|chattr|dd |mkfs)' "${BASH_SOURCE[0]}" \
132 | grep -vE "^[0-9]+: *#|grep -nE|^[0-9]+: *echo " >/dev/null; then
133 echo "FAIL script contains a writing command"; st=1
134 else
135 echo "PASS script contains no writing command (efibootmgr flags, sudo, reboot, chattr, dd, mkfs)"
136 fi
137 if [[ ${st} == 0 ]]; then echo "NATIVE_PREFLIGHT_SELF_TEST: PASS"; exit 0; fi
138 echo "NATIVE_PREFLIGHT_SELF_TEST: FAIL"; exit 1
139fi
140
141image="" expect=""
142while [[ $# -gt 0 ]]; do
143 case "$1" in
144 --image) image="${2:-}"; shift 2 ;;
145 --expect-sha256) expect="${2:-}"; shift 2 ;;
146 *) echo "usage: $0 [--image EFI [--expect-sha256 HEX]] | --self-test"; exit 2 ;;
147 esac
148done
149if [[ -n "${AIENOS_PREFLIGHT_EFIBOOTMGR:-}" ]]; then
150 boot="$(cat "${AIENOS_PREFLIGHT_EFIBOOTMGR}" 2>/dev/null || true)"
151else
152 boot="$(efibootmgr 2>/dev/null || true)"
153fi
154tree="$(git -C "${repo_root}" status --porcelain --untracked-files=normal 2>/dev/null || echo '?? not a git checkout')"
155echo "commit: $(git -C "${repo_root}" rev-parse HEAD 2>/dev/null || echo unknown)"
156preflight "${boot}" "${AIENOS_PREFLIGHT_SECUREBOOT:-${sb_default}}" "${AIENOS_PREFLIGHT_ESP:-/boot/efi}" \
157 "${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" "${tree}" "${image}" "${expect}"