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 / pi5-run.sh

210 lines · 10800 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# 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).
38set -euo pipefail
39efi="${1:?EFI file}"; tag="${2:?tag}"
40hub=hub
41# ping the address ssh connects to for the alias (override with PI5_HUB_ADDR)
42hub_addr="${PI5_HUB_ADDR:-$(ssh -G "${hub}" 2>/dev/null | awk '/^hostname /{print $2; exit}')}"
43runs="${PI5_RUNS_DIR:-$HOME/workspace/pi5-runs}/${tag}"
44mkdir -p "${runs}"
45log() { echo "[$(date -u +%H:%M:%SZ)] $*" | tee -a "${runs}/run.log"; }
46hs() { ssh -o BatchMode=yes -o ConnectTimeout=15 "${hub}" "$@"; }
47mark_begin='# >>> AIENOS-PI5 one-shot tryboot block (pi5-run.sh adds it before a run, removes it after)'
48mark_end='# <<< AIENOS-PI5'
49
50[[ -r "${efi}" ]] || { echo "no EFI file ${efi}"; exit 2; }
51sha=$(sha256sum "${efi}" | cut -d' ' -f1)
52log "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.
57stamp_rc=0
58commit_stamp=$("$(dirname "$0")/efi-commit.sh" "${efi}" 2>&1) || stamp_rc=$?
59log "efi aienos_commit: ${commit_stamp}"
60if [[ ${stamp_rc} != 0 ]]; then
61 log "!!! NOT commit-bound (no single commit stamp found in the EFI)"
62elif [[ ${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"
64fi
65
66# ---- preflight on the hub ----
67hs 'set -e
68test "$(cat /proc/device-tree/chosen/bootloader/tryboot | od -An -tx1 | tr -d " ")" = 00000000
69test ! -e /boot/firmware/new || { echo "REFUSE: /boot/firmware/new exists (pending Ubuntu tryboot update)"; exit 1; }
70grep -qF "AIENOS-PI5 one-shot" /boot/firmware/config.txt && { echo "REFUSE: block already in config.txt"; exit 1; } || true
71lsblk -no LABEL /dev/mmcblk0p1 | grep -qx AIENOSPI5
72findmnt /dev/mmcblk0p1 >/dev/null && { echo "REFUSE: card mounted"; exit 1; } || true
73echo "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 ----
77scp -q -o BatchMode=yes "${efi}" "${hub}:/tmp/aienos-run.efi"
78hs "set -e
79sudo mkdir -p /mnt/aienos
80sudo mount /dev/mmcblk0p1 /mnt/aienos
81trap 'sudo umount /mnt/aienos' EXIT
82sudo cp /tmp/aienos-run.efi /mnt/aienos/EFI/BOOT/BOOTAA64.EFI
83sudo rm -rf /mnt/aienos/EFI/AIENOS
84test \"\$(sha256sum /mnt/aienos/EFI/BOOT/BOOTAA64.EFI | cut -d' ' -f1)\" = ${sha}
85sudo mkdir -p /boot/firmware/aienos/overlays
86for 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
88done
89test -e /boot/firmware/aienos/overlays/README || echo 'AIENOS-PI5 tryboot overlays (only bcm2712d0)' | sudo tee /boot/firmware/aienos/overlays/README >/dev/null
90sync
91echo 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.
98if [[ "${KLOG:-0}" == 1 ]]; then
99 . "$(dirname "$0")/klog-lib.sh"
100 slot_secs=4096
101 ext=$(hs "set -e
102sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT
103sudo mkdir -p /mnt/aienos/EFI/AIENOS
104sudo dd if=/dev/zero of=/mnt/aienos/EFI/AIENOS/KLOG.BIN bs=512 count=${slot_secs} conv=fsync status=none
105sync
106sudo 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
120sudo mount /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT
121sudo dd if=/tmp/klog-hdr.bin of=/mnt/aienos/EFI/AIENOS/KLOG.BIN bs=512 count=1 conv=notrunc,fsync status=none
122sync; echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null
123sudo dd if=/dev/mmcblk0 bs=512 skip=${klog_lba} count=1 iflag=direct status=none | cmp - /tmp/klog-hdr.bin
124sudo cp /tmp/KLOG.MAP /mnt/aienos/EFI/AIENOS/KLOG.MAP; sync
125echo 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)"
128fi
129if [[ "${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 ----
132hs "sudo cp -p /boot/firmware/config.txt /boot/firmware/config.txt.pre-aienos"
133hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.before"
134{
135cat <<BLOCK
136
137${mark_begin}
138[tryboot]
139armstub=aienos/RPI_EFI.fd
140device_tree=aienos/bcm2712-rpi-5-b.dtb
141overlay_prefix=aienos/overlays/
142device_tree_address=0x1f0000
143device_tree_end=0x210000
144framebuffer_depth=32
145disable_overscan=1
146usb_max_current_enable=1
147force_turbo=1
148initramfs aienos/RPI_NV0.bin,aienos/RPI_NV1.bin 0x04000000
149[all]
150${mark_end}
151BLOCK
152} > "${runs}/block.txt"
153scp -q -o BatchMode=yes "${runs}/block.txt" "${hub}:/tmp/aienos-block.txt"
154hs "set -e; sudo sh -c 'cat /tmp/aienos-block.txt >> /boot/firmware/config.txt'; sync
155diff /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt | grep -c '^>' "
156hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.during"
157if ! 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
161fi
162log "block appended; rebooting the hub with tryboot"
163t0=$(date +%s)
164hs "sudo reboot '0 tryboot'" >/dev/null 2>&1 || true
165
166# ---- wait: down, then back ----
167sleep 20
168down=0; back=0
169for 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
176done
177if [[ ${back} != 1 ]]; then
178 log "hub NOT back after ~15 min (down_seen=${down})"
179 exit 4
180fi
181log "hub answers ping again after $(( $(date +%s) - t0 )) s; waiting 60 s for ssh"
182sleep 60
183for i in 1 2 3 4 5; do hs true 2>/dev/null && break; sleep 60; done
184
185# ---- cleanup + evidence ----
186hs "set -e
187tb=\$(od -An -tx1 /proc/device-tree/chosen/bootloader/tryboot | tr -d ' ')
188echo tryboot_flag_now=\$tb
189sudo cp -p /boot/firmware/config.txt.pre-aienos /boot/firmware/config.txt; sync
190grep -qF 'AIENOS-PI5 one-shot' /boot/firmware/config.txt && echo BLOCK_STILL_THERE || echo block_removed
191uptime" 2>&1 | tee -a "${runs}/run.log"
192hs "cat /boot/firmware/config.txt" > "${runs}/config.txt.after"
193cmp -s "${runs}/config.txt.before" "${runs}/config.txt.after" && log "config.txt restored byte for byte" || log "WARNING config.txt differs from before"
194hs "set -e; sudo mount -o ro /dev/mmcblk0p1 /mnt/aienos; trap 'sudo umount /mnt/aienos' EXIT
195if [ -d /mnt/aienos/EFI/AIENOS ]; then sudo tar -C /mnt/aienos/EFI -cf - AIENOS; fi" > "${runs}/aienos.tar"
196if [[ -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
206else
207 log "NO report on the card: the AIENOS image did not run or did not write"
208 exit 5
209fi
210log "run ${tag} done"