Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/scripts/pi5_run_allen.sh # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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= 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 [wait-file] 5 # 6 # 1. Puts 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//, 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). 22 set -euo pipefail 23 efi="${1:?EFI file}"; tag="${2:?tag}" 24 hub=hub 25 runs="$HOME/workspace/pi5-runs/${tag}" 26 mkdir -p "${runs}" 27 log() { echo "[$(date -u +%H:%M:%SZ)] $*" | tee -a "${runs}/run.log"; } 28 hs() { ssh -o BatchMode=yes -o ConnectTimeout=15 "${hub}" "$@"; } 29 mark_begin='# >>> AIENOS-PI5 one-shot tryboot block (pi5-run.sh adds it before a run, removes it after)' 30 mark_end='# <<< AIENOS-PI5' 31 32 [[ -r "${efi}" ]] || { echo "no EFI file ${efi}"; exit 2; } 33 sha=$(sha256sum "${efi}" | cut -d' ' -f1) 34 log "run ${tag}: efi=${efi} sha256=${sha}" 35 36 # ---- preflight on the hub ---- 37 hs 'set -e 38 test "$(cat /proc/device-tree/chosen/bootloader/tryboot | od -An -tx1 | tr -d " ")" = `00000000` 39 test ! -e /boot/firmware/new || { echo "REFUSE: /boot/firmware/new exists (pending Ubuntu tryboot update)"; exit 1; } 40 grep -qF "AIENOS-PI5 one-shot" /boot/firmware/config.txt && { echo "REFUSE: block already in config.txt"; exit 1; } || true 41 lsblk -no LABEL /dev/mmcblk0p1 | grep -qx AIENOSPI5 42 findmnt /dev/mmcblk0p1 >/dev/null && { echo "REFUSE: card mounted"; exit 1; } || true 43 echo "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 ---- 47 scp -q -o BatchMode=yes "${efi}" "${hub}:/tmp/aienos-run.efi" 48 hs "set -e 49 sudo mkdir -p /mnt/aienos 50 sudo mount /dev/mmcblk0p1 /mnt/aienos 51 trap 'sudo umount /mnt/aienos' EXIT 52 sudo cp /tmp/aienos-run.efi /mnt/aienos/EFI/BOOT/BOOTAA64.EFI 53 sudo rm -rf /mnt/aienos/EFI/AIENOS 54 test \"\$(sha256sum /mnt/aienos/EFI/BOOT/BOOTAA64.EFI | cut -d' ' -f1)\" = ${sha} 55 sudo mkdir -p /boot/firmware/aienos/overlays 56 for 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 58 done 59 test -e /boot/firmware/aienos/overlays/README || echo 'AIENOS-PI5 tryboot overlays (only bcm2712d0)' | sudo tee /boot/firmware/aienos/overlays/README >/dev/null 60 sync 61 echo 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. 68 if [[ "${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 73 sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 74 sudo mkdir -p /mnt/aienos/EFI/AIENOS 75 sudo dd if=/dev/zero of=/mnt/aienos/EFI/AIENOS/KLOG.BIN bs=512 count=${slot_secs} conv=fsync status=none 76 sync 77 sudo 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 91 sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 92 sudo dd if=/tmp/klog-hdr.bin of=/mnt/aienos/EFI/AIENOS/KLOG.BIN bs=512 count=1 conv=notrunc,fsync status=none 93 sync; echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null 94 sudo dd if=/dev/mmcblk0 bs=512 skip=${klog_lba} count=1 iflag=direct status=none | cmp - /tmp/klog-hdr.bin 95 sudo cp /tmp/KLOG.MAP /mnt/aienos/EFI/AIENOS/KLOG.MAP; sync 96 echo 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)" 99 fi 100 101 # ---- optional: unlock request for the PI5-4 qualification image (UNLOCK_REQ=) ---- 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. 106 if [[ -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 112 sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 113 sudo mkdir -p /mnt/aienos/EFI/AIENOS 114 sudo cp /tmp/aienos-unlock.req /mnt/aienos/EFI/AIENOS/UNLOCK.REQ 115 sudo rm -f /tmp/aienos-unlock.req 116 sync 117 test \"\$(sudo sha256sum /mnt/aienos/EFI/AIENOS/UNLOCK.REQ | cut -d' ' -f1)\" = ${req_sha} 118 echo 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" 121 fi 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. 127 if [[ -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 133 sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 134 sudo mkdir -p /mnt/aienos/EFI/AIENOS 135 sudo cp /tmp/aienos-model.map /mnt/aienos/EFI/AIENOS/MODEL.MAP 136 sudo rm -f /tmp/aienos-model.map 137 sync 138 test \"\$(sudo sha256sum /mnt/aienos/EFI/AIENOS/MODEL.MAP | cut -d' ' -f1)\" = ${map_sha} 139 echo 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})" 142 fi 143 # ---- optional: ALLEN card for the pi5-allen image (ALLEN_EXM=) ---- 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. 149 if [[ -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 157 sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 158 sudo mkdir -p /mnt/aienos/EFI/AIENOS 159 sudo cp /tmp/aienos-allen.exm /mnt/aienos/EFI/AIENOS/ALLEN.EXM 160 sudo rm -f /tmp/aienos-allen.exm 161 sync 162 test \"\$(sudo sha256sum /mnt/aienos/EFI/AIENOS/ALLEN.EXM | cut -d' ' -f1)\" = ${exm_sha} 163 echo 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" 166 fi 167 if [[ "${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 ---- 170 hs "sudo cp -p /boot/firmware/config.txt /boot/firmware/config.txt.pre-aienos" 171 hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.before" 172 { 173 cat < "${runs}/block.txt" 191 scp -q -o BatchMode=yes "${runs}/block.txt" "${hub}:/tmp/aienos-block.txt" 192 hs "set -e; sudo sh -c 'cat /tmp/aienos-block.txt >> /boot/firmware/config.txt'; sync 193 diff /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt | grep -c '^>' " 194 hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.during" 195 if ! 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 199 fi 200 log "block appended; rebooting the hub with tryboot" 201 t0=$(date +%s) 202 hs "sudo reboot '0 tryboot'" >/dev/null 2>&1 || true 203 204 # ---- wait: down, then back ---- 205 sleep 20 206 down=0; back=0 207 for 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 214 done 215 if [[ ${back} != 1 ]]; then 216 log "hub NOT back after ~15 min (down_seen=${down})" 217 exit 4 218 fi 219 log "hub answers ping again after $(( $(date +%s) - t0 )) s; waiting 60 s for ssh" 220 sleep 60 221 for i in 1 2 3 4 5; do hs true 2>/dev/null && break; sleep 60; done 222 223 # ---- cleanup + evidence ---- 224 hs "set -e 225 tb=\$(od -An -tx1 /proc/device-tree/chosen/bootloader/tryboot | tr -d ' ') 226 echo tryboot_flag_now=\$tb 227 sudo cp -p /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt; sync 228 grep -qF 'AIENOS-PI5 one-shot' /boot/firmware/config.txt && echo BLOCK_STILL_THERE || echo block_removed 229 uptime" 2>&1 | tee -a "${runs}/run.log" 230 hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.after" 231 cmp -s "${runs}/config.txt.before" "${runs}/config.txt.after" && log "config.txt restored byte for byte" || log "WARNING config.txt differs from before" 232 if [[ -n "${UNLOCK_REQ:-}" ]]; then 233 hs "set -e; sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 234 f=/mnt/aienos/EFI/AIENOS/UNLOCK.REQ 235 if [ -e \$f ]; then sudo dd if=/dev/zero of=\$f bs=512 count=1 conv=notrunc,fsync status=none; sudo rm -f \$f; fi 236 sync 237 if [ -e \$f ]; then echo unlock_req_STILL_ON_CARD; else echo unlock_req_deleted_verified; fi 238 sudo 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)" 241 fi 242 if [[ -n "${ALLEN_EXM:-}" ]]; then 243 hs "set -e; sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 244 f=/mnt/aienos/EFI/AIENOS/ALLEN.EXM 245 sudo rm -f \$f 246 sync 247 if [ -e \$f ]; then echo allen_exm_STILL_ON_CARD; else echo allen_exm_deleted_verified; fi 248 sudo 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)" 251 fi 252 hs "set -e; sudo mount -o ro /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 253 if [ -d /mnt/aienos/EFI/AIENOS ]; then sudo tar -C /mnt/aienos/EFI -cf - AIENOS; fi" > "${runs}/aienos.tar" 254 if [[ -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 264 else 265 log "NO report on the card: the AIENOS image did not run or did not write" 266 exit 5 267 fi 268 log "run ${tag} done"