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_artifact.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_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=, 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 [wait-file] 20 # 21 # 1. Puts 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//, 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). 37 set -euo pipefail 38 efi="${1:?EFI file}"; tag="${2:?tag}" 39 hub=hub 40 # ping the address ssh connects to for the alias (override with PI5_HUB_ADDR) 41 hub_addr="${PI5_HUB_ADDR:-$(ssh -G "${hub}" 2>/dev/null | awk '/^hostname /{print $2; exit}')}" 42 runs="$HOME/workspace/pi5-runs/${tag}" 43 mkdir -p "${runs}" 44 log() { echo "[$(date -u +%H:%M:%SZ)] $*" | tee -a "${runs}/run.log"; } 45 hs() { ssh -o BatchMode=yes -o ConnectTimeout=15 "${hub}" "$@"; } 46 mark_begin='# >>> AIENOS-PI5 one-shot tryboot block (pi5-run.sh adds it before a run, removes it after)' 47 mark_end='# <<< AIENOS-PI5' 48 49 [[ -r "${efi}" ]] || { echo "no EFI file ${efi}"; exit 2; } 50 sha=$(sha256sum "${efi}" | cut -d' ' -f1) 51 log "run ${tag}: efi=${efi} sha256=${sha}" 52 53 # ---- preflight on the hub ---- 54 hs 'set -e 55 test "$(cat /proc/device-tree/chosen/bootloader/tryboot | od -An -tx1 | tr -d " ")" = `00000000` 56 test ! -e /boot/firmware/new || { echo "REFUSE: /boot/firmware/new exists (pending Ubuntu tryboot update)"; exit 1; } 57 grep -qF "AIENOS-PI5 one-shot" /boot/firmware/config.txt && { echo "REFUSE: block already in config.txt"; exit 1; } || true 58 lsblk -no LABEL /dev/mmcblk0p1 | grep -qx AIENOSPI5 59 findmnt /dev/mmcblk0p1 >/dev/null && { echo "REFUSE: card mounted"; exit 1; } || true 60 echo "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 ---- 64 scp -q -o BatchMode=yes "${efi}" "${hub}:/tmp/aienos-run.efi" 65 hs "set -e 66 sudo mkdir -p /mnt/aienos 67 sudo mount /dev/mmcblk0p1 /mnt/aienos 68 trap 'sudo umount /mnt/aienos' EXIT 69 sudo cp /tmp/aienos-run.efi /mnt/aienos/EFI/BOOT/BOOTAA64.EFI 70 sudo rm -rf /mnt/aienos/EFI/AIENOS 71 test \"\$(sha256sum /mnt/aienos/EFI/BOOT/BOOTAA64.EFI | cut -d' ' -f1)\" = ${sha} 72 sudo mkdir -p /boot/firmware/aienos/overlays 73 for 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 75 done 76 test -e /boot/firmware/aienos/overlays/README || echo 'AIENOS-PI5 tryboot overlays (only bcm2712d0)' | sudo tee /boot/firmware/aienos/overlays/README >/dev/null 77 sync 78 echo 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. 85 if [[ "${KLOG:-0}" == 1 ]]; then 86 . "$(dirname "$0")/klog-lib.sh" 87 slot_secs=4096 88 ext=$(hs "set -e 89 sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 90 sudo mkdir -p /mnt/aienos/EFI/AIENOS 91 sudo dd if=/dev/zero of=/mnt/aienos/EFI/AIENOS/KLOG.BIN bs=512 count=${slot_secs} conv=fsync status=none 92 sync 93 sudo 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 107 sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 108 sudo dd if=/tmp/klog-hdr.bin of=/mnt/aienos/EFI/AIENOS/KLOG.BIN bs=512 count=1 conv=notrunc,fsync status=none 109 sync; echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null 110 sudo dd if=/dev/mmcblk0 bs=512 skip=${klog_lba} count=1 iflag=direct status=none | cmp - /tmp/klog-hdr.bin 111 sudo cp /tmp/KLOG.MAP /mnt/aienos/EFI/AIENOS/KLOG.MAP; sync 112 echo 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)" 115 fi 116 117 # ---- optional: unlock request for the PI5-4 qualification image (UNLOCK_REQ=) ---- 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. 122 if [[ -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 128 sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 129 sudo mkdir -p /mnt/aienos/EFI/AIENOS 130 sudo cp /tmp/aienos-unlock.req /mnt/aienos/EFI/AIENOS/UNLOCK.REQ 131 sudo rm -f /tmp/aienos-unlock.req 132 sync 133 test \"\$(sudo sha256sum /mnt/aienos/EFI/AIENOS/UNLOCK.REQ | cut -d' ' -f1)\" = ${req_sha} 134 echo 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" 137 fi 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. 143 if [[ -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 149 sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 150 sudo mkdir -p /mnt/aienos/EFI/AIENOS 151 sudo cp /tmp/aienos-model.map /mnt/aienos/EFI/AIENOS/MODEL.MAP 152 sudo rm -f /tmp/aienos-model.map 153 sync 154 test \"\$(sudo sha256sum /mnt/aienos/EFI/AIENOS/MODEL.MAP | cut -d' ' -f1)\" = ${map_sha} 155 echo 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})" 158 fi 159 # ---- optional: signed artifact bundle for the PI5-7 cut 3 EL0 artifact image (ARTIFACT_BND=) ---- 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. 163 if [[ -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 170 sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 171 sudo mkdir -p /mnt/aienos/EFI/AIENOS 172 sudo cp /tmp/aienos-artifact.bnd /mnt/aienos/EFI/AIENOS/ARTIFACT.BND 173 sudo rm -f /tmp/aienos-artifact.bnd 174 sync 175 test \"\$(sudo sha256sum /mnt/aienos/EFI/AIENOS/ARTIFACT.BND | cut -d' ' -f1)\" = ${bnd_sha} 176 echo 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" 179 fi 180 if [[ "${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 ---- 183 hs "sudo cp -p /boot/firmware/config.txt /boot/firmware/config.txt.pre-aienos" 184 hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.before" 185 { 186 cat < "${runs}/block.txt" 204 scp -q -o BatchMode=yes "${runs}/block.txt" "${hub}:/tmp/aienos-block.txt" 205 hs "set -e; sudo sh -c 'cat /tmp/aienos-block.txt >> /boot/firmware/config.txt'; sync 206 diff /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt | grep -c '^>' " 207 hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.during" 208 if ! 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 212 fi 213 log "block appended; rebooting the hub with tryboot" 214 t0=$(date +%s) 215 hs "sudo reboot '0 tryboot'" >/dev/null 2>&1 || true 216 217 # ---- wait: down, then back ---- 218 sleep 20 219 down=0; back=0 220 for 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 227 done 228 if [[ ${back} != 1 ]]; then 229 log "hub NOT back after ~15 min (down_seen=${down})" 230 exit 4 231 fi 232 log "hub answers ping again after $(( $(date +%s) - t0 )) s; waiting 60 s for ssh" 233 sleep 60 234 for i in 1 2 3 4 5; do hs true 2>/dev/null && break; sleep 60; done 235 236 # ---- cleanup + evidence ---- 237 hs "set -e 238 tb=\$(od -An -tx1 /proc/device-tree/chosen/bootloader/tryboot | tr -d ' ') 239 echo tryboot_flag_now=\$tb 240 sudo cp -p /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt; sync 241 grep -qF 'AIENOS-PI5 one-shot' /boot/firmware/config.txt && echo BLOCK_STILL_THERE || echo block_removed 242 uptime" 2>&1 | tee -a "${runs}/run.log" 243 hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.after" 244 cmp -s "${runs}/config.txt.before" "${runs}/config.txt.after" && log "config.txt restored byte for byte" || log "WARNING config.txt differs from before" 245 if [[ -n "${UNLOCK_REQ:-}" ]]; then 246 hs "set -e; sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 247 f=/mnt/aienos/EFI/AIENOS/UNLOCK.REQ 248 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 249 sync 250 if [ -e \$f ]; then echo unlock_req_STILL_ON_CARD; else echo unlock_req_deleted_verified; fi 251 sudo 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)" 254 fi 255 if [[ -n "${ARTIFACT_BND:-}" ]]; then 256 hs "set -e; sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 257 f=/mnt/aienos/EFI/AIENOS/ARTIFACT.BND 258 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 259 sync 260 if [ -e \$f ]; then echo artifact_bnd_STILL_ON_CARD; else echo artifact_bnd_deleted_verified; fi 261 sudo 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)" 264 fi 265 hs "set -e; sudo mount -o ro /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT 266 if [ -d /mnt/aienos/EFI/AIENOS ]; then sudo tar -C /mnt/aienos/EFI -cf - AIENOS; fi" > "${runs}/aienos.tar" 267 if [[ -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 277 else 278 log "NO report on the card: the AIENOS image did not run or did not write" 279 exit 5 280 fi 281 log "run ${tag} done"