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

268 lines · 15665 bytes Raw Blame History
1#!/usr/bin/env bash
2# pi5-run-allen.sh (pi5-run-artifact.sh with ALLEN_EXM=<exam card file> in place of ARTIFACT_BND, PI5-8 cut K) -- one remote, one-shot AIENOS test run on the hub Raspberry Pi 5.
3#
4# [KLOG=1] [UNLOCK_REQ=<512-byte request file>] [MODEL_MAP=<4096-byte MODEL.MAP>] pi5-run.sh <EFI file> <tag> [wait-file]
5#
6# 1. Puts <EFI file> on the AIENOS test microSD (mmcblk0p1, label AIENOSPI5) as
7# EFI/BOOT/BOOTAA64.EFI and removes the old report, so a report after the run
8# proves this run wrote it.
9# 2. Makes sure the Pi 5 UEFI firmware from the card is also on the USB stick's
10# boot partition under aienos/ (the Pi bootloader reads config from the stick).
11# 3. Appends a [tryboot]-only block to the stick's config.txt (backup kept on the
12# hub and here) and reboots with reboot '0 tryboot'. Raspberry Pi docs
13# (bootflow-eeprom.adoc): the flag "is cleared before starting the firmware, a
14# crash or reset will cause the original config.txt file to be loaded on the
15# next reboot". Normal boots never read the block.
16# 4. Waits for the hub to come back on normal Linux (ping every 30 s, ssh only
17# after ping answers; hub stick is fragile: one ssh at a time, no fast retries).
18# 5. Removes the block (restores the backup, checks it), copies the card's
19# EFI/AIENOS tree to ~/workspace/pi5-runs/<tag>/, unmounts.
20# If the hub is not back after 15 min: notify Drake (power-cycle the Pi; it comes
21# back on normal Linux because the flag is already cleared).
22set -euo pipefail
23efi="${1:?EFI file}"; tag="${2:?tag}"
24hub=hub
25runs="$HOME/workspace/pi5-runs/${tag}"
26mkdir -p "${runs}"
27log() { echo "[$(date -u +%H:%M:%SZ)] $*" | tee -a "${runs}/run.log"; }
28hs() { ssh -o BatchMode=yes -o ConnectTimeout=15 "${hub}" "$@"; }
29mark_begin='# >>> AIENOS-PI5 one-shot tryboot block (pi5-run.sh adds it before a run, removes it after)'
30mark_end='# <<< AIENOS-PI5'
31
32[[ -r "${efi}" ]] || { echo "no EFI file ${efi}"; exit 2; }
33sha=$(sha256sum "${efi}" | cut -d' ' -f1)
34log "run ${tag}: efi=${efi} sha256=${sha}"
35
36# ---- preflight on the hub ----
37hs 'set -e
38test "$(cat /proc/device-tree/chosen/bootloader/tryboot | od -An -tx1 | tr -d " ")" = 00000000
39test ! -e /boot/firmware/new || { echo "REFUSE: /boot/firmware/new exists (pending Ubuntu tryboot update)"; exit 1; }
40grep -qF "AIENOS-PI5 one-shot" /boot/firmware/config.txt && { echo "REFUSE: block already in config.txt"; exit 1; } || true
41lsblk -no LABEL /dev/mmcblk0p1 | grep -qx AIENOSPI5
42findmnt /dev/mmcblk0p1 >/dev/null && { echo "REFUSE: card mounted"; exit 1; } || true
43echo "users: $(who | wc -l)"' 2>&1 | tee -a "${runs}/run.log"
44[[ ${PIPESTATUS[0]} == 0 ]] || { log "preflight FAILED"; exit 1; }
45
46# ---- card: install EFI, clear old report; stick: firmware copy ----
47scp -q -o BatchMode=yes "${efi}" "${hub}:/tmp/aienos-run.efi"
48hs "set -e
49sudo mkdir -p /mnt/aienos
50sudo mount /dev/mmcblk0p1 /mnt/aienos
51trap 'sudo umount /mnt/aienos' EXIT
52sudo cp /tmp/aienos-run.efi /mnt/aienos/EFI/BOOT/BOOTAA64.EFI
53sudo rm -rf /mnt/aienos/EFI/AIENOS
54test \"\$(sha256sum /mnt/aienos/EFI/BOOT/BOOTAA64.EFI | cut -d' ' -f1)\" = ${sha}
55sudo mkdir -p /boot/firmware/aienos/overlays
56for f in RPI_EFI.fd RPI_NV0.bin RPI_NV1.bin bcm2712-rpi-5-b.dtb overlays/bcm2712d0.dtbo; do
57 if ! cmp -s /mnt/aienos/\$f /boot/firmware/aienos/\$f; then sudo cp /mnt/aienos/\$f /boot/firmware/aienos/\$f; fi
58done
59test -e /boot/firmware/aienos/overlays/README || echo 'AIENOS-PI5 tryboot overlays (only bcm2712d0)' | sudo tee /boot/firmware/aienos/overlays/README >/dev/null
60sync
61echo card+stick ready" 2>&1 | tee -a "${runs}/run.log"
62
63# ---- optional: KLOG slot for a kernel run (KLOG=1) ----
64# A 2 MiB contiguous file on the card plus \EFI\AIENOS\KLOG.MAP naming its
65# absolute first sector and a fresh nonce (aienos core/klog_slot.h). The raw
66# card sector is read back and compared before the map is written, so the
67# kernel is never told a position that is not the file.
68if [[ "${KLOG:-0}" == 1 ]]; then
69 kl="$(dirname "$0")/klog-lib.sh"; [[ -r "${kl}" ]] || kl="$(dirname "$0")/pi5/klog-lib.sh" # ~/workspace/pi5 copy, or the repo
70 . "${kl}"
71 slot_secs=4096
72 ext=$(hs "set -e
73sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT
74sudo mkdir -p /mnt/aienos/EFI/AIENOS
75sudo dd if=/dev/zero of=/mnt/aienos/EFI/AIENOS/KLOG.BIN bs=512 count=${slot_secs} conv=fsync status=none
76sync
77sudo hdparm --fibmap /mnt/aienos/EFI/AIENOS/KLOG.BIN")
78 echo "${ext}" >> "${runs}/run.log"
79 # hdparm --fibmap rows: byte_offset begin_LBA end_LBA sectors (absolute card LBAs)
80 rows=$(echo "${ext}" | awk '$1 ~ /^[0-9]+$/ && NF==4')
81 nexts=$(echo -n "${rows}" | grep -c . || true)
82 read -r poff klog_lba pend plen <<<"${rows}"
83 if [[ "${nexts}" != 1 || "${poff}" != 0 || "${plen}" != "${slot_secs}" || $((pend - klog_lba + 1)) != "${slot_secs}" ]]; then
84 log "KLOG.BIN is not one ${slot_secs}-sector extent (rows=${nexts} lba=${klog_lba} len=${plen}); stopping"; exit 1
85 fi
86 nonce=$(od -An -tx1 -N16 /dev/urandom | tr -d ' \n')
87 klog_hdr_empty "${klog_lba}" "${slot_secs}" "${nonce}" "${runs}/klog-hdr.bin"
88 klog_map "${klog_lba}" "${slot_secs}" "${nonce}" "${runs}/KLOG.MAP"
89 scp -q -o BatchMode=yes "${runs}/klog-hdr.bin" "${runs}/KLOG.MAP" "${hub}:/tmp/"
90 if ! hs "set -e
91sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT
92sudo dd if=/tmp/klog-hdr.bin of=/mnt/aienos/EFI/AIENOS/KLOG.BIN bs=512 count=1 conv=notrunc,fsync status=none
93sync; echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null
94sudo dd if=/dev/mmcblk0 bs=512 skip=${klog_lba} count=1 iflag=direct status=none | cmp - /tmp/klog-hdr.bin
95sudo cp /tmp/KLOG.MAP /mnt/aienos/EFI/AIENOS/KLOG.MAP; sync
96echo klog_slot_ready" 2>&1 | tee -a "${runs}/run.log"; then :; fi
97 if ! grep -q klog_slot_ready "${runs}/run.log"; then log "klog slot raw read-back FAILED; stopping before any reboot"; exit 1; fi
98 log "klog slot lba=${klog_lba} sectors=${slot_secs} nonce=${nonce} (raw card sector matches)"
99fi
100
101# ---- optional: unlock request for the PI5-4 qualification image (UNLOCK_REQ=<file>) ----
102# QUALIFICATION ONLY: the file holds a THROWAWAY recovery secret (aienos native/kernel/core/unlock_req.h:
103# 512 bytes, magic AIENUNQ1, tier tag QUALIF-THROWAWAY). It is copied to \EFI\AIENOS\UNLOCK.REQ on the
104# card before the run and zeroed + deleted from the card (and checked gone) after the run, before the
105# card's EFI/AIENOS tree is copied back. Never put a real operator secret in it.
106if [[ -n "${UNLOCK_REQ:-}" ]]; then
107 [[ -r "${UNLOCK_REQ}" && "$(stat -c %s "${UNLOCK_REQ}")" == 512 && "$(head -c 8 "${UNLOCK_REQ}")" == AIENUNQ1 && "$(dd if="${UNLOCK_REQ}" bs=1 skip=16 count=16 status=none)" == QUALIF-THROWAWAY ]] \
108 || { log "UNLOCK_REQ ${UNLOCK_REQ} is not a 512-byte AIENUNQ1 QUALIF-THROWAWAY request; stopping"; exit 1; }
109 req_sha=$(sha256sum "${UNLOCK_REQ}" | cut -d' ' -f1)
110 scp -q -o BatchMode=yes "${UNLOCK_REQ}" "${hub}:/tmp/aienos-unlock.req"
111 hs "set -e
112sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT
113sudo mkdir -p /mnt/aienos/EFI/AIENOS
114sudo cp /tmp/aienos-unlock.req /mnt/aienos/EFI/AIENOS/UNLOCK.REQ
115sudo rm -f /tmp/aienos-unlock.req
116sync
117test \"\$(sudo sha256sum /mnt/aienos/EFI/AIENOS/UNLOCK.REQ | cut -d' ' -f1)\" = ${req_sha}
118echo unlock_req_placed" 2>&1 | tee -a "${runs}/run.log"
119 grep -q unlock_req_placed "${runs}/run.log" || { log "UNLOCK.REQ placement FAILED; stopping before any reboot"; exit 1; }
120 log "UNLOCK.REQ placed on the card (QUALIFICATION ONLY, throwaway secret; sha256 of the request ${req_sha}); will be deleted after the run"
121fi
122# ---- optional: MODEL.MAP for the inference image (MODEL_MAP=<4096-byte map file>) ----
123# aienos native/boot/model_map.h: the UEFI stub reads \EFI\AIENOS\MODEL.MAP and then the model bytes by
124# absolute LBA from the disk (here the raw card partition AIENOS-MODEL, mmcblk0p3, written once by hand,
125# never touched by this script). Only the 4 KiB map goes on the ESP. The map is also left in the copied
126# EFI/AIENOS tree as evidence.
127if [[ -n "${MODEL_MAP:-}" ]]; then
128 [[ -r "${MODEL_MAP}" && "$(stat -c %s "${MODEL_MAP}")" == 4096 && "$(head -c 8 "${MODEL_MAP}")" == AIENMDL1 ]] \
129 || { log "MODEL_MAP ${MODEL_MAP} is not a 4096-byte AIENMDL1 map; stopping"; exit 1; }
130 map_sha=$(sha256sum "${MODEL_MAP}" | cut -d' ' -f1)
131 scp -q -o BatchMode=yes "${MODEL_MAP}" "${hub}:/tmp/aienos-model.map"
132 hs "set -e
133sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT
134sudo mkdir -p /mnt/aienos/EFI/AIENOS
135sudo cp /tmp/aienos-model.map /mnt/aienos/EFI/AIENOS/MODEL.MAP
136sudo rm -f /tmp/aienos-model.map
137sync
138test \"\$(sudo sha256sum /mnt/aienos/EFI/AIENOS/MODEL.MAP | cut -d' ' -f1)\" = ${map_sha}
139echo model_map_placed" 2>&1 | tee -a "${runs}/run.log"
140 grep -q model_map_placed "${runs}/run.log" || { log "MODEL.MAP placement FAILED; stopping before any reboot"; exit 1; }
141 log "MODEL.MAP placed on the card (sha256 ${map_sha})"
142fi
143# ---- optional: ALLEN card for the pi5-allen image (ALLEN_EXM=<file, "AIENALX1 QUALIF-ALLEN-EXAM" or "AIENALR1 QUALIF-ALLEN-ROUTE" header>) ----
144# The route card (aienos#399, made by scripts/allen_route_card.sh, which also refuses held-out and sealed files) holds
145# interactive requests without expected answers; the same path, size and held-out rules apply.
146# QUALIFICATION ONLY, train/dev rehearsal or bounds cases (aienos native/kernel/core/allen.h: at most 131072 bytes, made by
147# scripts/allen_exam_card.sh, which refuses held-out and sealed files). Copied to \EFI\AIENOS\ALLEN.EXM before the run; deleted
148# from the card (and checked gone) after the run; a copy stays in the run directory as evidence.
149if [[ -n "${ALLEN_EXM:-}" ]]; then
150 case "$(readlink -f "${ALLEN_EXM}")" in */sealed/*|*heldout*) log "ALLEN_EXM ${ALLEN_EXM} looks held-out or sealed; stopping"; exit 1 ;; esac
151 [[ -r "${ALLEN_EXM}" && "$(stat -c %s "${ALLEN_EXM}")" -le 131072 && ( "$(head -n1 "${ALLEN_EXM}")" == "AIENALX1 QUALIF-ALLEN-EXAM" || "$(head -n1 "${ALLEN_EXM}")" == "AIENALR1 QUALIF-ALLEN-ROUTE" ) ]] \
152 || { log "ALLEN_EXM ${ALLEN_EXM} is not an AIENALX1 exam or AIENALR1 route card of at most 131072 bytes; stopping"; exit 1; }
153 exm_sha=$(sha256sum "${ALLEN_EXM}" | cut -d" " -f1)
154 cp "${ALLEN_EXM}" "${runs}/ALLEN.EXM.sent"
155 scp -q -o BatchMode=yes "${ALLEN_EXM}" "${hub}:/tmp/aienos-allen.exm"
156 hs "set -e
157sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT
158sudo mkdir -p /mnt/aienos/EFI/AIENOS
159sudo cp /tmp/aienos-allen.exm /mnt/aienos/EFI/AIENOS/ALLEN.EXM
160sudo rm -f /tmp/aienos-allen.exm
161sync
162test \"\$(sudo sha256sum /mnt/aienos/EFI/AIENOS/ALLEN.EXM | cut -d' ' -f1)\" = ${exm_sha}
163echo allen_exm_placed" 2>&1 | tee -a "${runs}/run.log"
164 grep -q allen_exm_placed "${runs}/run.log" || { log "ALLEN.EXM placement FAILED; stopping before any reboot"; exit 1; }
165 log "ALLEN.EXM placed on the card (QUALIFICATION ONLY, sha256 ${exm_sha}); will be deleted after the run"
166fi
167if [[ "${DRY:-0}" == 1 ]]; then log "DRY=1: card prepared, stopping before any config change or reboot"; exit 0; fi
168
169# ---- config.txt: backup, append the tryboot-only block, check ----
170hs "sudo cp -p /boot/firmware/config.txt /boot/firmware/config.txt.pre-aienos"
171hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.before"
172{
173cat <<BLOCK
174
175${mark_begin}
176[tryboot]
177armstub=aienos/RPI_EFI.fd
178device_tree=aienos/bcm2712-rpi-5-b.dtb
179overlay_prefix=aienos/overlays/
180device_tree_address=0x1f0000
181device_tree_end=0x210000
182framebuffer_depth=32
183disable_overscan=1
184usb_max_current_enable=1
185force_turbo=1
186initramfs aienos/RPI_NV0.bin,aienos/RPI_NV1.bin 0x04000000
187[all]
188${mark_end}
189BLOCK
190} > "${runs}/block.txt"
191scp -q -o BatchMode=yes "${runs}/block.txt" "${hub}:/tmp/aienos-block.txt"
192hs "set -e; sudo sh -c 'cat /tmp/aienos-block.txt >> /boot/firmware/config.txt'; sync
193diff /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt | grep -c '^>' "
194hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.during"
195if ! diff <(cat "${runs}/config.txt.before" "${runs}/block.txt") "${runs}/config.txt.during" >/dev/null; then
196 log "config.txt is not exactly before+block: restoring and stopping"
197 hs "sudo cp -p /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt; sync"
198 exit 1
199fi
200log "block appended; rebooting the hub with tryboot"
201t0=$(date +%s)
202hs "sudo reboot '0 tryboot'" >/dev/null 2>&1 || true
203
204# ---- wait: down, then back ----
205sleep 20
206down=0; back=0
207for i in $(seq 1 30); do
208 if ping -c1 -W3 192.168.1.192 >/dev/null 2>&1; then
209 [[ ${down} == 1 ]] && { back=1; break; }
210 else
211 down=1
212 fi
213 sleep 30
214done
215if [[ ${back} != 1 ]]; then
216 log "hub NOT back after ~15 min (down_seen=${down})"
217 exit 4
218fi
219log "hub answers ping again after $(( $(date +%s) - t0 )) s; waiting 60 s for ssh"
220sleep 60
221for i in 1 2 3 4 5; do hs true 2>/dev/null && break; sleep 60; done
222
223# ---- cleanup + evidence ----
224hs "set -e
225tb=\$(od -An -tx1 /proc/device-tree/chosen/bootloader/tryboot | tr -d ' ')
226echo tryboot_flag_now=\$tb
227sudo cp -p /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt; sync
228grep -qF 'AIENOS-PI5 one-shot' /boot/firmware/config.txt && echo BLOCK_STILL_THERE || echo block_removed
229uptime" 2>&1 | tee -a "${runs}/run.log"
230hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.after"
231cmp -s "${runs}/config.txt.before" "${runs}/config.txt.after" && log "config.txt restored byte for byte" || log "WARNING config.txt differs from before"
232if [[ -n "${UNLOCK_REQ:-}" ]]; then
233 hs "set -e; sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT
234f=/mnt/aienos/EFI/AIENOS/UNLOCK.REQ
235if [ -e \$f ]; then sudo dd if=/dev/zero of=\$f bs=512 count=1 conv=notrunc,fsync status=none; sudo rm -f \$f; fi
236sync
237if [ -e \$f ]; then echo unlock_req_STILL_ON_CARD; else echo unlock_req_deleted_verified; fi
238sudo rm -f /tmp/aienos-unlock.req" 2>&1 | tee -a "${runs}/run.log"
239 if ! grep -q unlock_req_deleted_verified "${runs}/run.log"; then log "UNLOCK.REQ NOT confirmed deleted from the card: remove it by hand before any other use"; exit 6; fi
240 log "UNLOCK.REQ zeroed and deleted from the card (verified absent)"
241fi
242if [[ -n "${ALLEN_EXM:-}" ]]; then
243 hs "set -e; sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT
244f=/mnt/aienos/EFI/AIENOS/ALLEN.EXM
245sudo rm -f \$f
246sync
247if [ -e \$f ]; then echo allen_exm_STILL_ON_CARD; else echo allen_exm_deleted_verified; fi
248sudo rm -f /tmp/aienos-allen.exm" 2>&1 | tee -a "${runs}/run.log"
249 if ! grep -q allen_exm_deleted_verified "${runs}/run.log"; then log "ALLEN.EXM NOT confirmed deleted from the card: remove it by hand before any other use"; exit 6; fi
250 log "ALLEN.EXM deleted from the card (verified absent)"
251fi
252hs "set -e; sudo mount -o ro /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT
253if [ -d /mnt/aienos/EFI/AIENOS ]; then sudo tar -C /mnt/aienos/EFI -cf - AIENOS; fi" > "${runs}/aienos.tar"
254if [[ -s "${runs}/aienos.tar" ]]; then
255 tar -C "${runs}" -xf "${runs}/aienos.tar" && rm "${runs}/aienos.tar"
256 log "report copied: $(ls -R "${runs}/AIENOS" | tr '\n' ' ' | head -c 400)"
257 if [[ "${KLOG:-0}" == 1 ]]; then
258 if [[ -f "${runs}/AIENOS/KLOG.BIN" ]] && klog_extract "${runs}/AIENOS/KLOG.BIN" "${runs}/kernel.log" | tee -a "${runs}/run.log"; then
259 log "kernel log: $(wc -l < "${runs}/kernel.log") lines in ${runs}/kernel.log"
260 else
261 log "kernel log NOT written (slot still empty or missing)"
262 fi
263 fi
264else
265 log "NO report on the card: the AIENOS image did not run or did not write"
266 exit 5
267fi
268log "run ${tag} done"