Files
noahandClaude Opus 4.8 d612bb6521 make-afw: emit SHA256SUMS for the built bundle so it cannot drift
The producer verified the bundle but never wrote the SHA256SUMS release
artifact, so a stale file from a previous build could describe a different
bundle. Write it (overwrite) from the finished file.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B1W6qHxYpv7yf7Ah57g4rQ
2026-11-04 15:25:38 -06:00

147 lines
5.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build a full-OS .afw install bundle for one board family.
#
# A .afw is a strict POSIX ustar with members in this exact order:
# manifest.json
# manifest.json.sig (only for an Ed25519-signed bundle)
# payload/<image>... (in manifest.images order)
#
# The manifest carries the board, arch, SDK/OS revisions, every image's size
# and SHA-256, the partition plan, bootloader_policy, the network recovery path
# and a liveness marker. ci/check-afw.py verifies the finished bundle before
# this script returns success. There is no default signing key: unsigned is
# allowed only with an explicit --allow-unsigned, and that is bench-only.
#
# usage:
# ci/make-afw.sh --board cv1835 --version 0.1.0 \
# --input build/out/cv1835 --sdk-dir ../alkane-sdk \
# --sdk-revision "$(git -C ../alkane-sdk rev-parse HEAD)" \
# --os-revision "$(git rev-parse HEAD)" \
# --output dist [--plan plan.json] [--signing-key FILE] [--allow-unsigned]
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BOARD=
VERSION=
INPUT=
OUTPUT=dist
PLAN=
SIGNING_KEY=
SDK_DIR=
SDK_REVISION=
OS_REVISION=
KERNEL_VERSION=
ALLOW_UNSIGNED=0
usage() {
sed -n '2,30p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'
exit "${1:-1}"
}
while [ "$#" -gt 0 ]; do
case "$1" in
--board) BOARD=${2:?--board needs a value}; shift 2 ;;
--version) VERSION=${2:?--version needs a value}; shift 2 ;;
--input) INPUT=${2:?--input needs a value}; shift 2 ;;
--output) OUTPUT=${2:?--output needs a value}; shift 2 ;;
--plan) PLAN=${2:?--plan needs a value}; shift 2 ;;
--signing-key) SIGNING_KEY=${2:?--signing-key needs a value}; shift 2 ;;
--sdk-dir) SDK_DIR=${2:?--sdk-dir needs a value}; shift 2 ;;
--sdk-revision) SDK_REVISION=${2:?--sdk-revision needs a value}; shift 2 ;;
--os-revision) OS_REVISION=${2:?--os-revision needs a value}; shift 2 ;;
--kernel-version) KERNEL_VERSION=${2:?--kernel-version needs a value}; shift 2 ;;
--allow-unsigned) ALLOW_UNSIGNED=1; shift ;;
-h|--help) usage 0 ;;
*) echo "make-afw: unknown argument: $1" >&2; usage 2 ;;
esac
done
[ -n "$BOARD" ] || { echo "make-afw: --board is required" >&2; usage 2; }
[ -n "$VERSION" ] || { echo "make-afw: --version is required" >&2; usage 2; }
[ -n "$INPUT" ] || { echo "make-afw: --input is required" >&2; usage 2; }
[ -d "$INPUT" ] || { echo "make-afw: input directory not found: $INPUT" >&2; exit 1; }
if [ -z "$SDK_REVISION" ] && [ -n "$SDK_DIR" ]; then
SDK_REVISION=$(git -C "$SDK_DIR" rev-parse HEAD)
fi
if [ -z "$OS_REVISION" ]; then
OS_REVISION=$(git -C "$ROOT" rev-parse HEAD)
fi
[ -n "$SDK_REVISION" ] || { echo "make-afw: --sdk-revision or --sdk-dir is required" >&2; exit 1; }
mkdir -p "$OUTPUT"
OUTPUT="$(cd "$OUTPUT" && pwd)"
WORK=$(mktemp -d "${TMPDIR:-/tmp}/make-afw.XXXXXX")
trap 'rm -rf "$WORK"' EXIT
MANIFEST_ARGS=(
--board "$BOARD"
--version "$VERSION"
--input "$INPUT"
--staging "$WORK"
--sdk-revision "$SDK_REVISION"
--os-revision "$OS_REVISION"
--os-dir "$ROOT"
)
[ -n "$PLAN" ] && MANIFEST_ARGS+=(--plan "$PLAN")
[ -n "$SIGNING_KEY" ] && MANIFEST_ARGS+=(--signing-key "$SIGNING_KEY")
[ -n "$KERNEL_VERSION" ] && MANIFEST_ARGS+=(--kernel-version "$KERNEL_VERSION")
python3 "$ROOT/ci/afw_manifest.py" "${MANIFEST_ARGS[@]}" >/dev/null
ARCH=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["arch"])' "$WORK/manifest.json")
NAME="alkane-os-$VERSION-$BOARD-$ARCH.afw"
MEMBERS="$WORK/members.txt"
python3 - "$WORK" >"$MEMBERS" <<'PY'
import json
import sys
from pathlib import Path
stage = Path(sys.argv[1])
manifest = json.loads((stage / "manifest.json").read_text())
members = ["manifest.json"]
if manifest["signature"]["member"]:
members.append(manifest["signature"]["member"])
members.extend(image["file"] for image in manifest["images"])
for member in members:
if not (stage / member).is_file():
raise SystemExit(f"staged member is missing: {member}")
print("\n".join(members))
PY
MTIME=()
if [ -n "${SOURCE_DATE_EPOCH:-}" ]; then
MTIME=(--mtime="@${SOURCE_DATE_EPOCH}")
fi
tar --format=ustar --owner=0 --group=0 --numeric-owner "${MTIME[@]}" \
--create --file "$OUTPUT/$NAME" -C "$WORK" -T "$MEMBERS"
GOT=$(tar -tf "$OUTPUT/$NAME")
WANT=$(cat "$MEMBERS")
if [ "$GOT" != "$WANT" ]; then
echo "make-afw: FATAL: member order is wrong" >&2
printf 'expected:\n%s\nactual:\n%s\n' "$WANT" "$GOT" >&2
rm -f "$OUTPUT/$NAME"
exit 1
fi
CHECK=(--expect-board "$BOARD")
if [ -f "$WORK/manifest.pub" ]; then
CHECK+=(--public-key "$(cat "$WORK/manifest.pub")")
elif [ "$ALLOW_UNSIGNED" -eq 1 ]; then
CHECK+=(--allow-unsigned)
else
echo "make-afw: unsigned bundle needs --allow-unsigned (bench-only)" >&2
rm -f "$OUTPUT/$NAME"
exit 1
fi
python3 "$ROOT/ci/check-afw.py" "$OUTPUT/$NAME" "${CHECK[@]}"
# Emit a SHA256SUMS covering exactly this bundle, so the release artifact can
# never drift from the file it describes. Overwrite, do not append.
( cd "$OUTPUT" && sha256sum "$NAME" > SHA256SUMS )
printf '%s\n' "$OUTPUT/$NAME"