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 / pi5 / pi5-run.sh
| 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 | # Runs from the Spark against the hub Pi 5 over ssh (host alias "hub"); never run it on the hub itself. Run folders go under $PI5_RUNS_DIR (default ~/workspace/pi5-runs). |
| 18 | # pi5-run.sh -- one remote, one-shot AIENOS test run on the hub Raspberry Pi 5. |
| 19 | # |
| 20 | # pi5-run.sh <EFI file> <tag> [wait-file] |
| 21 | # |
| 22 | # 1. Puts <EFI file> on the AIENOS test microSD (mmcblk0p1, label AIENOSPI5) as |
| 23 | # EFI/BOOT/BOOTAA64.EFI and removes the old report, so a report after the run |
| 24 | # proves this run wrote it. |
| 25 | # 2. Makes sure the Pi 5 UEFI firmware from the card is also on the USB stick's |
| 26 | # boot partition under aienos/ (the Pi bootloader reads config from the stick). |
| 27 | # 3. Appends a [tryboot]-only block to the stick's config.txt (backup kept on the |
| 28 | # hub and here) and reboots with reboot '0 tryboot'. Raspberry Pi docs |
| 29 | # (bootflow-eeprom.adoc): the flag "is cleared before starting the firmware, a |
| 30 | # crash or reset will cause the original config.txt file to be loaded on the |
| 31 | # next reboot". Normal boots never read the block. |
| 32 | # 4. Waits for the hub to come back on normal Linux (ping every 30 s, ssh only |
| 33 | # after ping answers; hub stick is fragile: one ssh at a time, no fast retries). |
| 34 | # 5. Removes the block (restores the backup, checks it), copies the card's |
| 35 | # EFI/AIENOS tree to ~/workspace/pi5-runs/<tag>/, unmounts. |
| 36 | # If the hub is not back after 15 min: notify Drake (power-cycle the Pi; it comes |
| 37 | # back on normal Linux because the flag is already cleared). |
| 38 | set -euo pipefail |
| 39 | efi="${1:?EFI file}"; tag="${2:?tag}" |
| 40 | hub=hub |
| 41 | # ping the address ssh connects to for the alias (override with PI5_HUB_ADDR) |
| 42 | hub_addr="${PI5_HUB_ADDR:-$(ssh -G "${hub}" 2>/dev/null | awk '/^hostname /{print $2; exit}')}" |
| 43 | runs="${PI5_RUNS_DIR:-$HOME/workspace/pi5-runs}/${tag}" |
| 44 | mkdir -p "${runs}" |
| 45 | log() { echo "[$(date -u +%H:%M:%SZ)] $*" | tee -a "${runs}/run.log"; } |
| 46 | hs() { ssh -o BatchMode=yes -o ConnectTimeout=15 "${hub}" "$@"; } |
| 47 | mark_begin='# >>> AIENOS-PI5 one-shot tryboot block (pi5-run.sh adds it before a run, removes it after)' |
| 48 | mark_end='# <<< AIENOS-PI5' |
| 49 | |
| 50 | [[ -r "${efi}" ]] || { echo "no EFI file ${efi}"; exit 2; } |
| 51 | sha=$(sha256sum "${efi}" | cut -d' ' -f1) |
| 52 | log "run ${tag}: efi=${efi} sha256=${sha}" |
| 53 | |
| 54 | # ---- which source commit does this EFI claim? (the kernel prints the same string as aienos_commit) ---- |
| 55 | # A Pi build made from a tree with uncommitted changes to tracked files ends in |
| 56 | # -dirty (native/kernel/Makefile); such a run is not tied to any GitHub commit. |
| 57 | stamp_rc=0 |
| 58 | commit_stamp=$("$(dirname "$0")/efi-commit.sh" "${efi}" 2>&1) || stamp_rc=$? |
| 59 | log "efi aienos_commit: ${commit_stamp}" |
| 60 | if [[ ${stamp_rc} != 0 ]]; then |
| 61 | log "!!! NOT commit-bound (no single commit stamp found in the EFI)" |
| 62 | elif [[ ${commit_stamp} == *-dirty ]]; then |
| 63 | log "!!! NOT commit-bound (dirty build): ${commit_stamp%-dirty} is only the commit the build started from; this EFI also holds uncommitted changes" |
| 64 | fi |
| 65 | |
| 66 | # ---- preflight on the hub ---- |
| 67 | hs 'set -e |
| 68 | test "$(cat /proc/device-tree/chosen/bootloader/tryboot | od -An -tx1 | tr -d " ")" = 00000000 |
| 69 | test ! -e /boot/firmware/new || { echo "REFUSE: /boot/firmware/new exists (pending Ubuntu tryboot update)"; exit 1; } |
| 70 | grep -qF "AIENOS-PI5 one-shot" /boot/firmware/config.txt && { echo "REFUSE: block already in config.txt"; exit 1; } || true |
| 71 | lsblk -no LABEL /dev/mmcblk0p1 | grep -qx AIENOSPI5 |
| 72 | findmnt /dev/mmcblk0p1 >/dev/null && { echo "REFUSE: card mounted"; exit 1; } || true |
| 73 | echo "users: $(who | wc -l)"' 2>&1 | tee -a "${runs}/run.log" |
| 74 | [[ ${PIPESTATUS[0]} == 0 ]] || { log "preflight FAILED"; exit 1; } |
| 75 | |
| 76 | # ---- card: install EFI, clear old report; stick: firmware copy ---- |
| 77 | scp -q -o BatchMode=yes "${efi}" "${hub}:/tmp/aienos-run.efi" |
| 78 | hs "set -e |
| 79 | sudo mkdir -p /mnt/aienos |
| 80 | sudo mount /dev/mmcblk0p1 /mnt/aienos |
| 81 | trap 'sudo umount /mnt/aienos' EXIT |
| 82 | sudo cp /tmp/aienos-run.efi /mnt/aienos/EFI/BOOT/BOOTAA64.EFI |
| 83 | sudo rm -rf /mnt/aienos/EFI/AIENOS |
| 84 | test \"\$(sha256sum /mnt/aienos/EFI/BOOT/BOOTAA64.EFI | cut -d' ' -f1)\" = ${sha} |
| 85 | sudo mkdir -p /boot/firmware/aienos/overlays |
| 86 | for f in RPI_EFI.fd RPI_NV0.bin RPI_NV1.bin bcm2712-rpi-5-b.dtb overlays/bcm2712d0.dtbo; do |
| 87 | if ! cmp -s /mnt/aienos/\$f /boot/firmware/aienos/\$f; then sudo cp /mnt/aienos/\$f /boot/firmware/aienos/\$f; fi |
| 88 | done |
| 89 | test -e /boot/firmware/aienos/overlays/README || echo 'AIENOS-PI5 tryboot overlays (only bcm2712d0)' | sudo tee /boot/firmware/aienos/overlays/README >/dev/null |
| 90 | sync |
| 91 | echo card+stick ready" 2>&1 | tee -a "${runs}/run.log" |
| 92 | |
| 93 | # ---- optional: KLOG slot for a kernel run (KLOG=1) ---- |
| 94 | # A 2 MiB contiguous file on the card plus \EFI\AIENOS\KLOG.MAP naming its |
| 95 | # absolute first sector and a fresh nonce (aienos core/klog_slot.h). The raw |
| 96 | # card sector is read back and compared before the map is written, so the |
| 97 | # kernel is never told a position that is not the file. |
| 98 | if [[ "${KLOG:-0}" == 1 ]]; then |
| 99 | . "$(dirname "$0")/klog-lib.sh" |
| 100 | slot_secs=4096 |
| 101 | ext=$(hs "set -e |
| 102 | sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT |
| 103 | sudo mkdir -p /mnt/aienos/EFI/AIENOS |
| 104 | sudo dd if=/dev/zero of=/mnt/aienos/EFI/AIENOS/KLOG.BIN bs=512 count=${slot_secs} conv=fsync status=none |
| 105 | sync |
| 106 | sudo hdparm --fibmap /mnt/aienos/EFI/AIENOS/KLOG.BIN") |
| 107 | echo "${ext}" >> "${runs}/run.log" |
| 108 | # hdparm --fibmap rows: byte_offset begin_LBA end_LBA sectors (absolute card LBAs) |
| 109 | rows=$(echo "${ext}" | awk '$1 ~ /^[0-9]+$/ && NF==4') |
| 110 | nexts=$(echo -n "${rows}" | grep -c . || true) |
| 111 | read -r poff klog_lba pend plen <<<"${rows}" |
| 112 | if [[ "${nexts}" != 1 || "${poff}" != 0 || "${plen}" != "${slot_secs}" || $((pend - klog_lba + 1)) != "${slot_secs}" ]]; then |
| 113 | log "KLOG.BIN is not one ${slot_secs}-sector extent (rows=${nexts} lba=${klog_lba} len=${plen}); stopping"; exit 1 |
| 114 | fi |
| 115 | nonce=$(od -An -tx1 -N16 /dev/urandom | tr -d ' \n') |
| 116 | klog_hdr_empty "${klog_lba}" "${slot_secs}" "${nonce}" "${runs}/klog-hdr.bin" |
| 117 | klog_map "${klog_lba}" "${slot_secs}" "${nonce}" "${runs}/KLOG.MAP" |
| 118 | scp -q -o BatchMode=yes "${runs}/klog-hdr.bin" "${runs}/KLOG.MAP" "${hub}:/tmp/" |
| 119 | if ! hs "set -e |
| 120 | sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT |
| 121 | sudo dd if=/tmp/klog-hdr.bin of=/mnt/aienos/EFI/AIENOS/KLOG.BIN bs=512 count=1 conv=notrunc,fsync status=none |
| 122 | sync; echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null |
| 123 | sudo dd if=/dev/mmcblk0 bs=512 skip=${klog_lba} count=1 iflag=direct status=none | cmp - /tmp/klog-hdr.bin |
| 124 | sudo cp /tmp/KLOG.MAP /mnt/aienos/EFI/AIENOS/KLOG.MAP; sync |
| 125 | echo klog_slot_ready" 2>&1 | tee -a "${runs}/run.log"; then :; fi |
| 126 | if ! grep -q klog_slot_ready "${runs}/run.log"; then log "klog slot raw read-back FAILED; stopping before any reboot"; exit 1; fi |
| 127 | log "klog slot lba=${klog_lba} sectors=${slot_secs} nonce=${nonce} (raw card sector matches)" |
| 128 | fi |
| 129 | if [[ "${DRY:-0}" == 1 ]]; then log "DRY=1: card prepared, stopping before any config change or reboot"; exit 0; fi |
| 130 | |
| 131 | # ---- config.txt: backup, append the tryboot-only block, check ---- |
| 132 | hs "sudo cp -p /boot/firmware/config.txt /boot/firmware/config.txt.pre-aienos" |
| 133 | hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.before" |
| 134 | { |
| 135 | cat <<BLOCK |
| 136 | |
| 137 | ${mark_begin} |
| 138 | [tryboot] |
| 139 | armstub=aienos/RPI_EFI.fd |
| 140 | device_tree=aienos/bcm2712-rpi-5-b.dtb |
| 141 | overlay_prefix=aienos/overlays/ |
| 142 | device_tree_address=0x1f0000 |
| 143 | device_tree_end=0x210000 |
| 144 | framebuffer_depth=32 |
| 145 | disable_overscan=1 |
| 146 | usb_max_current_enable=1 |
| 147 | force_turbo=1 |
| 148 | initramfs aienos/RPI_NV0.bin,aienos/RPI_NV1.bin 0x04000000 |
| 149 | [all] |
| 150 | ${mark_end} |
| 151 | BLOCK |
| 152 | } > "${runs}/block.txt" |
| 153 | scp -q -o BatchMode=yes "${runs}/block.txt" "${hub}:/tmp/aienos-block.txt" |
| 154 | hs "set -e; sudo sh -c 'cat /tmp/aienos-block.txt >> /boot/firmware/config.txt'; sync |
| 155 | diff /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt | grep -c '^>' " |
| 156 | hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.during" |
| 157 | if ! diff <(cat "${runs}/config.txt.before" "${runs}/block.txt") "${runs}/config.txt.during" >/dev/null; then |
| 158 | log "config.txt is not exactly before+block: restoring and stopping" |
| 159 | hs "sudo cp -p /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt; sync" |
| 160 | exit 1 |
| 161 | fi |
| 162 | log "block appended; rebooting the hub with tryboot" |
| 163 | t0=$(date +%s) |
| 164 | hs "sudo reboot '0 tryboot'" >/dev/null 2>&1 || true |
| 165 | |
| 166 | # ---- wait: down, then back ---- |
| 167 | sleep 20 |
| 168 | down=0; back=0 |
| 169 | for i in $(seq 1 30); do |
| 170 | if ping -c1 -W3 "${hub_addr}" >/dev/null 2>&1; then |
| 171 | [[ ${down} == 1 ]] && { back=1; break; } |
| 172 | else |
| 173 | down=1 |
| 174 | fi |
| 175 | sleep 30 |
| 176 | done |
| 177 | if [[ ${back} != 1 ]]; then |
| 178 | log "hub NOT back after ~15 min (down_seen=${down})" |
| 179 | exit 4 |
| 180 | fi |
| 181 | log "hub answers ping again after $(( $(date +%s) - t0 )) s; waiting 60 s for ssh" |
| 182 | sleep 60 |
| 183 | for i in 1 2 3 4 5; do hs true 2>/dev/null && break; sleep 60; done |
| 184 | |
| 185 | # ---- cleanup + evidence ---- |
| 186 | hs "set -e |
| 187 | tb=\$(od -An -tx1 /proc/device-tree/chosen/bootloader/tryboot | tr -d ' ') |
| 188 | echo tryboot_flag_now=\$tb |
| 189 | sudo cp -p /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt; sync |
| 190 | grep -qF 'AIENOS-PI5 one-shot' /boot/firmware/config.txt && echo BLOCK_STILL_THERE || echo block_removed |
| 191 | uptime" 2>&1 | tee -a "${runs}/run.log" |
| 192 | hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.after" |
| 193 | cmp -s "${runs}/config.txt.before" "${runs}/config.txt.after" && log "config.txt restored byte for byte" || log "WARNING config.txt differs from before" |
| 194 | hs "set -e; sudo mount -o ro /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT |
| 195 | if [ -d /mnt/aienos/EFI/AIENOS ]; then sudo tar -C /mnt/aienos/EFI -cf - AIENOS; fi" > "${runs}/aienos.tar" |
| 196 | if [[ -s "${runs}/aienos.tar" ]]; then |
| 197 | tar -C "${runs}" -xf "${runs}/aienos.tar" && rm "${runs}/aienos.tar" |
| 198 | log "report copied: $(ls -R "${runs}/AIENOS" | tr '\n' ' ' | head -c 400)" |
| 199 | if [[ "${KLOG:-0}" == 1 ]]; then |
| 200 | if [[ -f "${runs}/AIENOS/KLOG.BIN" ]] && klog_extract "${runs}/AIENOS/KLOG.BIN" "${runs}/kernel.log" | tee -a "${runs}/run.log"; then |
| 201 | log "kernel log: $(wc -l < "${runs}/kernel.log") lines in ${runs}/kernel.log" |
| 202 | else |
| 203 | log "kernel log NOT written (slot still empty or missing)" |
| 204 | fi |
| 205 | fi |
| 206 | else |
| 207 | log "NO report on the card: the AIENOS image did not run or did not write" |
| 208 | exit 5 |
| 209 | fi |
| 210 | log "run ${tag} done" |