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_bootdisk_test.sh
| 1 | #!/usr/bin/env bash |
| 2 | # qemu_ck_imac_bootdisk_test.sh -- QEMU test of the boot section: the network |
| 3 | # loader's verified kernel installs itself on the computer's own hard disk |
| 4 | # (native/kernel/x86/memory_x86.c boot_section, dev/fat_boot.c, dev/gpt_add.c, |
| 5 | # boot/net_loader.c struct nb_pass), and the firmware then starts it from that |
| 6 | # disk with no stick and no network. QEMU is an emulator: a PASS here |
| 7 | # qualifies nothing about any real machine or its firmware. |
| 8 | # |
| 9 | # One q35 + OVMF machine; a fresh firmware variable store for every start (no |
| 10 | # boot entries remembered). The hard disk is ck_gpt_image "one-old" (one |
| 11 | # partition full of a pattern, the rest unused) under the approved model |
| 12 | # "APPLE HDD ST1000DM003". |
| 13 | # start 1 stick (NETLDR.EFI) + network: the server offers the signed |
| 14 | # default kernel; it adds Allen's section and the boot section |
| 15 | # (EFI System Partition, 256 MiB) in unused space, installs itself |
| 16 | # as \EFI\BOOT\BOOTX64.EFI (slot A), reports boot_time; goodbye selects ACPI S5. |
| 17 | # Host: fsck.fat finds nothing to repair, mtools reads BOOTX64.EFI |
| 18 | # equal to the kernel image, the old partition and the stick are |
| 19 | # byte-identical. |
| 20 | # start 2 the hard disk alone, no network: the firmware starts the kernel |
| 21 | # from it; boot_time says no loader; Allen's memory loads (start 2). |
| 22 | # start 3 stick + network, a second kernel (another commit stamp): it goes |
| 23 | # to slot B. |
| 24 | # start 4 the hard disk alone: the second kernel is the one that runs. |
| 25 | # Final line: AIENOS_IMAC_BOOTDISK: PASS|FAIL|NOT_RUN exit 0 / 1 / 2 (tools) / 3 |
| 26 | set -euo pipefail |
| 27 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 28 | cd "${repo_root}" |
| 29 | tag="AIENOS_IMAC_BOOTDISK" |
| 30 | code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}" |
| 31 | vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}" |
| 32 | cross="" |
| 33 | if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi |
| 34 | for t in qemu-system-x86_64 mformat mmd mcopy mtype cmp sha256sum "${cross}gcc" "${cross}ld"; do |
| 35 | command -v "${t}" >/dev/null || { echo "${tag}: NOT_RUN (${t} missing)"; exit 2; } |
| 36 | done |
| 37 | fsck_fat="$(command -v fsck.fat || echo /usr/sbin/fsck.fat)" |
| 38 | [[ -x "${fsck_fat}" ]] || { echo "${tag}: NOT_RUN (fsck.fat missing)"; exit 2; } |
| 39 | [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; } |
| 40 | |
| 41 | . "${repo_root}/scripts/lib_gate_hold.sh" |
| 42 | if ! gh_quiet_check || ! gh_lock_take qemu_ck_imac_bootdisk_test "${AIENOS_GATE_MINUTES:-30}"; then |
| 43 | echo "${tag}: NOT_RUN (${gh_why})" |
| 44 | exit 3 |
| 45 | fi |
| 46 | work="$(mktemp -d)" |
| 47 | pids=() |
| 48 | cleanup() { |
| 49 | local p |
| 50 | for p in "${pids[@]}"; do kill "${p}" 2>/dev/null || true; done |
| 51 | rm -rf "${work}" |
| 52 | gh_lock_release |
| 53 | } |
| 54 | trap cleanup EXIT |
| 55 | |
| 56 | out="${work}/out" |
| 57 | out2="${work}/out2" |
| 58 | commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" |
| 59 | second="0000000000000000000000000000000000000b0b" |
| 60 | mk() { make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" "$@" >/dev/null; } |
| 61 | mk slot-tool imac-netboot-tool gpt-image |
| 62 | nb="${out}/host/ck_imac_netboot" |
| 63 | gpt="${out}/host/ck_gpt_image" |
| 64 | "${nb}" keygen "${work}/test.key" "${work}/test.pub" >/dev/null |
| 65 | mk CK_NETLDR_KEYS="${work}/test.pub" CK_NETLDR_FALLBACK_S=8 imac-netldr |
| 66 | mk CK_SLOT_KEYS="${work}/test.pub" imac-kernel |
| 67 | make -s -C native/kernel X86_CROSS="${cross}" OUT="${out2}" AIENOS_COMMIT="${second}" CK_SLOT_KEYS="${work}/test.pub" imac-kernel >/dev/null |
| 68 | push() { NETBOOT_TOOL_OUT="${out}" bash scripts/ck_imac_netboot_push.sh "$1" "${work}/test.key" "${work}/spool" >/dev/null; } |
| 69 | |
| 70 | stick="${work}/stick.img" |
| 71 | truncate -s 64M "${stick}" |
| 72 | mformat -i "${stick}" -F :: |
| 73 | mmd -i "${stick}" ::/EFI ::/EFI/BOOT |
| 74 | mcopy -i "${stick}" "${out}/NETLDR.EFI" ::/EFI/BOOT/BOOTX64.EFI |
| 75 | "${gpt}" create "${work}/hdd.img" 512 2048 one-old >/dev/null |
| 76 | cp --sparse=always "${work}/hdd.img" "${work}/hdd.before" |
| 77 | stick_sum="$(sha256sum <"${stick}")" |
| 78 | |
| 79 | fails=0 |
| 80 | pass() { echo "PASS $1"; } |
| 81 | fail() { echo "FAIL $1"; fails=$((fails + 1)); } |
| 82 | chk() { # description, file, pattern |
| 83 | if grep -aqE -- "$3" "$2"; then pass "$1"; else fail "$1 (no match for: $3)"; fi |
| 84 | } |
| 85 | nochk() { |
| 86 | if grep -aqE -- "$3" "$2"; then fail "$1 (unexpected: $3)"; else pass "$1"; fi |
| 87 | } |
| 88 | txt() { tr -d '\r' <"${work}/$1.log" 2>/dev/null || true; } |
| 89 | has() { txt "$1" | grep -aqE -- "$2"; } |
| 90 | wait_for() { # name seconds pattern |
| 91 | local i |
| 92 | for ((i = 0; i < $2 * 5; i++)); do |
| 93 | has "$1" "$3" && return 0 |
| 94 | sleep 0.2 |
| 95 | done |
| 96 | return 1 |
| 97 | } |
| 98 | lp=$((40000 + RANDOM % 10000)) |
| 99 | rp=$((lp + 1)) |
| 100 | start() { # name with-stick(0/1) |
| 101 | local name="$1" net=() |
| 102 | cp "${vars_fd}" "${work}/${name}.vars" |
| 103 | mkfifo "${work}/${name}.mon.in" "${work}/${name}.mon.out" |
| 104 | : >"${work}/${name}.log" |
| 105 | local disks=(-drive if=none,id=hdd,format=raw,file="${work}/hdd.img" |
| 106 | -device ide-hd,drive=hdd,bus=ide.1,model="APPLE HDD ST1000DM003",bootindex=2) |
| 107 | if [[ "$2" == 1 ]]; then |
| 108 | disks+=(-drive if=none,id=stick,format=raw,file="${stick}" -device ide-hd,drive=stick,bus=ide.0,bootindex=1) |
| 109 | net=(-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}" |
| 110 | -device virtio-net-pci,netdev=n0,mac=52:54:00:12:34:56) |
| 111 | else |
| 112 | net=(-nic none) |
| 113 | fi |
| 114 | qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 2 -m 1024 \ |
| 115 | -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ |
| 116 | -drive if=pflash,format=raw,file="${work}/${name}.vars" \ |
| 117 | "${disks[@]}" "${net[@]}" \ |
| 118 | -device qemu-xhci,id=xhci -device usb-kbd,bus=xhci.0,port=1 \ |
| 119 | -chardev pipe,id=mon,path="${work}/${name}.mon" -mon chardev=mon,mode=readline \ |
| 120 | -display none -serial file:"${work}/${name}.log" -no-reboot 2>"${work}/${name}.err" & |
| 121 | pids+=($!) |
| 122 | eval "pid_${name}=$!" |
| 123 | cat "${work}/${name}.mon.out" >/dev/null & |
| 124 | pids+=($!) |
| 125 | } |
| 126 | running() { local v="pid_$1"; kill -0 "${!v}" 2>/dev/null; } |
| 127 | stop() { local v="pid_$1"; kill "${!v}" 2>/dev/null || true; } |
| 128 | say() { # name "lower-case words": typed through the QEMU monitor, then Enter |
| 129 | local name="$1" text="$2" c k i |
| 130 | exec 3>"${work}/${name}.mon.in" |
| 131 | for ((i = 0; i < ${#text}; i++)); do |
| 132 | c="${text:i:1}" |
| 133 | case "${c}" in " ") k=spc ;; [a-z0-9]) k="${c}" ;; *) echo "say: no key for '${c}'" >&2; exit 2 ;; esac |
| 134 | echo "sendkey ${k}" >&3 |
| 135 | sleep 0.15 |
| 136 | done |
| 137 | echo "sendkey ret" >&3 |
| 138 | exec 3>&- |
| 139 | } |
| 140 | off() { wait_for "$1" 60 '^power: off via ACPI S5 '; } # QEMU has no supported panel: goodbye selects S5 |
| 141 | ready='^keyboard: type a line and press Enter' |
| 142 | run() { # name with-stick: start, wait for Allen, goodbye (S5), cleanup; result in name.txt |
| 143 | start "$1" "$2" |
| 144 | if wait_for "$1" 400 "${ready}"; then |
| 145 | if [[ "$1" == one ]]; then |
| 146 | # A fresh disk now opens setup before chat. Complete its real |
| 147 | # keyboard path without granting optional device/model consent. |
| 148 | # Sending goodbye here used to type it into setup instead. |
| 149 | local step |
| 150 | for step in intent scan consent llm ready; do |
| 151 | if wait_for "$1" 40 "^wizard: step ${step}$"; then |
| 152 | printf 'sendkey esc\n' >"${work}/$1.mon.in" |
| 153 | sleep 0.2 |
| 154 | else |
| 155 | fail "1: setup reached ${step} before chat" |
| 156 | break |
| 157 | fi |
| 158 | done |
| 159 | wait_for "$1" 40 '^wizard: finished' || fail "1: setup finished before goodbye" |
| 160 | else |
| 161 | chk "$1: completed setup stays closed" "${work}/$1.log" '^wizard: setup finished on an earlier start' |
| 162 | fi |
| 163 | say "$1" "goodbye" |
| 164 | off "$1" || true |
| 165 | fi |
| 166 | stop "$1" |
| 167 | txt "$1" >"${work}/$1.txt" |
| 168 | } |
| 169 | |
| 170 | mkdir -p "${work}/spool" |
| 171 | "${nb}" serve "${work}/spool" --qemu-dgram "${rp}:${lp}" >"${work}/serve.log" 2>&1 & |
| 172 | pids+=($!) |
| 173 | |
| 174 | echo "== start 1: stick + network, install ==" |
| 175 | push "${out}/IMACKERN.EFI" |
| 176 | run one 1 |
| 177 | chk "1: the network kernel started" "${work}/one.txt" '^netldr: starting the network kernel \([0-9]+ bytes\)$' |
| 178 | chk "1: Allen's section added" "${work}/one.txt" '^allen_memory: AIENOS partition added, entry [0-9]+, ' |
| 179 | chk "1: boot section added in unused space" "${work}/one.txt" '^boot_disk: boot section added, entry [0-9]+, first_lba=[0-9]+ last_lba=[0-9]+ \(256 MiB, EFI System Partition\); the existing partitions were not touched$' |
| 180 | chk "1: installed in slot A, read back" "${work}/one.txt" "^boot_disk: installed this kernel \\(commit ${commit:0:12}, generation [0-9]+, [0-9]+ bytes\\) as .EFI.BOOT.BOOTX64\\.EFI, slot A, new FAT32 volume, [0-9]+ blocks in [0-9]+ ms, read back exact; " |
| 181 | chk "1: boot times reported with the loader" "${work}/one.txt" '^boot_time: since power on: firmware until the loader [0-9]+ ms, loader [0-9]+ ms, kernel [0-9]+ ms; Allen.s window at [0-9]+ ms$' |
| 182 | chk "1: ACPI S5 after goodbye" "${work}/one.txt" '^power: off via ACPI S5 ' |
| 183 | nochk "1: no write refused by the fence" "${work}/one.txt" 'ahci_write: .*refused' |
| 184 | first="$(grep -aoE '^boot_disk: boot section added, entry [0-9]+, first_lba=[0-9]+' "${work}/one.txt" | grep -oE '[0-9]+$' || echo 0)" |
| 185 | if [[ "${first}" -gt 0 ]]; then |
| 186 | dd if="${work}/hdd.img" of="${work}/esp1.img" bs=512 skip="${first}" count=$((256 * 2048)) status=none |
| 187 | if "${fsck_fat}" -n "${work}/esp1.img" >"${work}/fsck1" 2>&1; then pass "1: fsck.fat finds nothing to repair" |
| 188 | else cat "${work}/fsck1"; fail "1: fsck.fat finds faults"; fi |
| 189 | if MTOOLS_SKIP_CHECK=1 mtype -i "${work}/esp1.img" ::/EFI/BOOT/BOOTX64.EFI >"${work}/got1" && |
| 190 | cmp -s "${work}/got1" "${out}/IMACKERN.EFI"; then pass "1: mtools reads BOOTX64.EFI equal to the kernel image" |
| 191 | else fail "1: BOOTX64.EFI differs from the kernel image"; fi |
| 192 | else |
| 193 | fail "1: no boot section first LBA" |
| 194 | fi |
| 195 | if cmp -s -i $((2048 * 512)) -n $((16384 * 512)) "${work}/hdd.before" "${work}/hdd.img"; then |
| 196 | pass "1: the old partition is byte-identical" |
| 197 | else fail "1: the old partition changed"; fi |
| 198 | [[ "$(sha256sum <"${stick}")" == "${stick_sum}" ]] && pass "1: the stick was not written" || fail "1: the stick changed" |
| 199 | |
| 200 | echo "== start 2: the hard disk alone ==" |
| 201 | run two 0 |
| 202 | nochk "2: no network loader" "${work}/two.txt" '^netldr: ' |
| 203 | chk "2: the kernel ran from the hard disk" "${work}/two.txt" "^aienos_commit: ${commit}\$" |
| 204 | chk "2: boot times without a loader" "${work}/two.txt" '^boot_time: since power on: firmware until this kernel [0-9]+ ms \(no loader\), kernel [0-9]+ ms; Allen.s window at [0-9]+ ms$' |
| 205 | chk "2: the boot section reported, slot A" "${work}/two.txt" '^boot_disk: .EFI.BOOT.BOOTX64\.EFI on disk [0-9]+: [0-9]+ bytes \(slot A\)$' |
| 206 | chk "2: Allen's memory loaded (second start)" "${work}/two.txt" '^allen_memory: loaded generation [0-9]+ \(2 of 2 copies valid\): .*1 earlier start$' |
| 207 | nochk "2: nothing added" "${work}/two.txt" 'partition added|boot section added' |
| 208 | |
| 209 | echo "== start 3: stick + network, a second kernel ==" |
| 210 | push "${out2}/IMACKERN.EFI" |
| 211 | run three 1 |
| 212 | chk "3: the second kernel ran" "${work}/three.txt" "^aienos_commit: ${second}\$" |
| 213 | chk "3: installed in slot B" "${work}/three.txt" '^boot_disk: installed this kernel \(commit [0-9a-f]{12}, generation [0-9]+, [0-9]+ bytes\) as .EFI.BOOT.BOOTX64\.EFI, slot B, [0-9]+ blocks in [0-9]+ ms, read back exact; ' |
| 214 | nochk "3: no new partition" "${work}/three.txt" 'partition added|boot section added' |
| 215 | |
| 216 | echo "== start 4: the hard disk alone ==" |
| 217 | run four 0 |
| 218 | chk "4: the second kernel runs from the hard disk" "${work}/four.txt" "^aienos_commit: ${second}\$" |
| 219 | chk "4: slot B reported" "${work}/four.txt" '^boot_disk: .EFI.BOOT.BOOTX64\.EFI on disk [0-9]+: [0-9]+ bytes \(slot B\)$' |
| 220 | dd if="${work}/hdd.img" of="${work}/esp4.img" bs=512 skip="${first:-0}" count=$((256 * 2048)) status=none |
| 221 | if "${fsck_fat}" -n "${work}/esp4.img" >"${work}/fsck4" 2>&1; then pass "4: fsck.fat finds nothing to repair" |
| 222 | else cat "${work}/fsck4"; fail "4: fsck.fat finds faults"; fi |
| 223 | |
| 224 | grep -ahE '^boot_time:|^boot_disk:' "${work}"/one.txt "${work}"/two.txt "${work}"/three.txt "${work}"/four.txt | sed 's/^/note: /' |
| 225 | if [[ "${fails}" -ne 0 ]]; then |
| 226 | for n in one two three four; do echo "---- ${n} (last 30 lines) ----"; tail -30 "${work}/${n}.txt"; done |
| 227 | echo "${tag}: FAIL (${fails} checks)" |
| 228 | exit 1 |
| 229 | fi |
| 230 | echo "${tag}: PASS" |