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_artifact.sh

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