Files
bfe-core1106-sdk/qemu/mkimage.sh
T
BFE EngineeringandClaude Opus 5.5 a4bbfbd83b
ci / prune-artifacts (push) Skipped
ci / patches-apply (push) Successful in 24s
ci / bench (pull_request) Successful in 16s
ci / test (push) Successful in 24s
ci / mcdc (push) Successful in 29s
ci / kernel-build (push) Skipped
ci / test (pull_request) Successful in 10s
ci / quality (push) Successful in 39s
ci / badges (push) Skipped
ci / mcdc (pull_request) Successful in 30s
ci / bench (push) Successful in 10s
ci / qemu-tools (push) Successful in 3m16s
ci / qemu-tools (pull_request) Successful in 2m34s
ci / kernel-symbols (pull_request) Successful in 1m54s
ci / prune-artifacts (pull_request) Skipped
ci / kernel-build (pull_request) Skipped
ci / patches-apply (pull_request) Successful in 55s
ci / quality (pull_request) Successful in 1m36s
ci / badges (pull_request) Skipped
ci / kernel-symbols (push) Successful in 4m24s
qemu: boot the UI through the panel's shipped init chain (flare-edge#2210)
The rig's stage-2 init starts /usr/bin/warden-ui directly as root, so it
never ran S03warden-accounts, S99lvgl or warden-ui-run: no privilege drop,
respawn loop or safe mode was exercised, and the BusyBox setpriv bug of
flare-edge #1903 left every rebuild image without a UI while CI was green.

- mkimage.sh: ROOTFS_OVERLAY lays a directory of panel files over the
  rootfs of both slots, modes kept. Only etc/ and usr/ at its top, no
  symlinks; a refused overlay writes no image.
- sbin/init: with warden.initchain on the command line it runs
  /etc/init.d/S00-S09 before the bus and S10-S99 after the payload daemons,
  in place of the direct UI start (warden_init_chain in warden-lib.sh). The
  splash handover is marked done and warden-ui-run gets "-b fbdev". A chain
  boot without scripts prints WARDEN-QEMU-INITCHAIN-EMPTY and starts no UI.
- warden_rig_bus_owners: when the accounts exist, cred/ui belongs to
  warden-ui and the cred directory to group warden-bus, as S10bus does.
- Tests: init-chain-test.sh (19 checks, also under busybox sh) and
  rootfs-overlay.sh (17 scenarios, images read back with debugfs), both in CI.

Refs flare-edge#2210

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KerYPVh72hhNgKi3wS5ijG
2026-10-28 10:35:29 -06:00

319 lines
14 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build the VM's virtio disk image carrying the device's canonical 12-partition
# A/B layout (qemu/blkdevparts.conf: the same string U-Boot and Linux parse on
# hardware; there is no MBR/GPT). Every partition is placed at the exact offset
# the cmdline string declares; rootfs_a/rootfs_b/oem_a/oem_b/userdata get ext4,
# the boot-chain partitions (env/idblock/uboot/misc/boot_a/boot_b/recovery)
# stay zeroed: the VM enters at -kernel and never reads them.
#
# Built entirely UNPRIVILEGED: per-partition mkfs.ext4 -d (no loop mounts, no
# sudo), then dd'd into a sparse raw image.
#
# Usage: mkimage.sh [--portal-url URL] [--state KEY=VALUE]... [--fw-version V]
# [--rootfs-image PATH] [--oem-image PATH]
# --rootfs-image/--oem-image place REAL device images (raw ext4, e.g. a
# flare-edge build's rootfs.img/oem.img matched pair) into slot A instead of
# the busybox skeleton; slot B keeps the skeleton as a known-good fallback.
# Env:
# BUSYBOX path to a local busybox binary (skips the download; still verified)
# OUT output dir (default: qemu/out); image at $OUT/disk.img
# PAYLOAD_DIR the directory whose binaries land in /usr/bin of both slots
# (default qemu/payload). Lets one run boot its own payload set
# (flare-edge's hook set for a flow that pins documents: the
# production payload plus the test-flavor UI and wthctl) while
# other runs read qemu/payload at the same time. Not a
# directory: fails closed.
# ROOTFS_OVERLAY a directory laid over the staged rootfs of both slots,
# after the payload, with its modes kept (cp -a). Only etc/ and
# usr/ may appear at its top, and no entry may be a symlink, so
# it can neither replace the rig's own /init and /sbin/init nor
# point outside the image. flare-edge #2210 stages the panel's
# S03warden-accounts, S99lvgl and /usr/lib/warden/warden-ui-run
# this way for a boot with warden.initchain (rootfs/sbin/init).
# Not a directory, or a disallowed entry: fails closed.
# SEED_DIR a directory of pre-built userdata/warden files (one file per
# settings key, e.g. flare-edge tools/seed-fixtures.py's output)
# copied in VERBATIM, after --portal-url/--state. Lets a caller
# seed an arbitrary key set -- a flow spec's setup.seed can name
# anything settings.c reads -- without growing --state into a
# multi-value flag; last one written wins, same as --state.
set -euo pipefail
QEMU_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=qemu/lib.sh disable=SC1091
. "$QEMU_DIR/lib.sh"
# shellcheck source=qemu/blkdevparts.conf disable=SC1091
. "$QEMU_DIR/blkdevparts.conf"
OUT="${OUT:-$QEMU_DIR/out}"
# mkfs.ext4 lives in sbin, which user shells on Debian don't have on PATH.
PATH="$PATH:/usr/sbin:/sbin"
PORTAL_URL=""
STATE_KV=()
FW_VERSION="0.0.1"
ROOTFS_IMAGE=""
OEM_IMAGE=""
while [ $# -gt 0 ]; do
case "$1" in
--portal-url) PORTAL_URL="${2:?--portal-url needs a value}"; shift 2 ;;
--state)
case "${2:?--state needs KEY=VALUE}" in
*=*) ;;
*) echo "FATAL: --state needs KEY=VALUE, got '$2'" >&2; exit 1 ;;
esac
case "${2%%=*}" in
.|..)
echo "FATAL: --state key cannot be '.' or '..'" >&2
exit 1 ;;
*[!A-Za-z0-9_.]*|'')
echo "FATAL: --state key '${2%%=*}' must match [A-Za-z0-9_.]+ (it becomes a filename)" >&2
exit 1 ;;
esac
STATE_KV+=("$2"); shift 2 ;;
--fw-version) FW_VERSION="${2:?--fw-version needs a value}"; shift 2 ;;
--rootfs-image) ROOTFS_IMAGE="${2:?--rootfs-image needs a path}"; shift 2 ;;
--oem-image) OEM_IMAGE="${2:?--oem-image needs a path}"; shift 2 ;;
*) echo "FATAL: unknown argument '$1' (usage: mkimage.sh [--portal-url URL] [--state KEY=VALUE]... [--fw-version V])" >&2; exit 1 ;;
esac
done
qemu_get_busybox
SCRATCH="$(mktemp -d "${TMPDIR:-/tmp}/warden-qemu-image.XXXXXX")"
trap 'rm -rf "$SCRATCH"' EXIT
# Stage the rootfs tree once (skeleton + payload into /usr/bin), reused for
# both slots so A and B start byte-identical, like a factory flash.
ROOT="$SCRATCH/root"
qemu_stage_rootfs "$ROOT"
PAYLOAD_DIR="${PAYLOAD_DIR:-$QEMU_DIR/payload}"
[ -d "$PAYLOAD_DIR" ] || { echo "FATAL: PAYLOAD_DIR '$PAYLOAD_DIR' is not a directory" >&2; exit 1; }
for p in "$PAYLOAD_DIR"/*; do
[ -f "$p" ] || continue
case "$(basename "$p")" in README.md) continue ;; esac
install -m 0755 "$p" "$ROOT/usr/bin/$(basename "$p")"
done
# ROOTFS_OVERLAY (see the Env note above): checked whole before anything of
# it is copied, so a refused overlay leaves no partial tree and no image.
if [ -n "${ROOTFS_OVERLAY:-}" ]; then
[ -d "$ROOTFS_OVERLAY" ] || { echo "FATAL: ROOTFS_OVERLAY '$ROOTFS_OVERLAY' is not a directory" >&2; exit 1; }
for entry in "$ROOTFS_OVERLAY"/* "$ROOTFS_OVERLAY"/.[!.]*; do
[ -e "$entry" ] || [ -L "$entry" ] || continue
case "$(basename "$entry")" in
etc|usr) [ -d "$entry" ] && [ ! -L "$entry" ] && continue ;;
esac
echo "FATAL: ROOTFS_OVERLAY may hold only the directories etc/ and usr/ at its top, not '$(basename "$entry")'" >&2
exit 1
done
links="$(find "$ROOTFS_OVERLAY" -type l)"
[ -z "$links" ] || { echo "FATAL: ROOTFS_OVERLAY holds symlinks: $links" >&2; exit 1; }
cp -a "$ROOTFS_OVERLAY"/. "$ROOT/"
fi
# Firmware version stamp: same path the device build writes; flared reads its
# running version here (downgrade rules key off it).
printf '%s\n' "$FW_VERSION" > "$ROOT/etc/warden-firmware-version"
# Seed persistent state (flared: one file per key under /userdata/warden).
# Newline-terminated, matching how flare-edge's fw-e2e-test.sh seeds the store.
UDATA="$SCRATCH/userdata"
mkdir -p "$UDATA/warden"
[ -n "$PORTAL_URL" ] && printf '%s\n' "$PORTAL_URL" > "$UDATA/warden/flare.url"
for kv in ${STATE_KV[@]+"${STATE_KV[@]}"}; do
printf '%s\n' "${kv#*=}" > "$UDATA/warden/${kv%%=*}"
done
# SEED_DIR: a pre-built set of settings files (see the Env note above), copied
# in whole rather than re-parsed here -- seed-fixtures.py already wrote them
# in the exact format settings.c reads (filename = key, mode 0600 for a
# secret), so re-deriving that here would be a second place to keep in sync
# with settings.c. Applied after --state so a seeded file can override a
# same-named --state value; `cp -a` preserves the 0600 on a secret entry.
if [ -n "${SEED_DIR:-}" ]; then
[ -d "$SEED_DIR" ] || { echo "FATAL: SEED_DIR '$SEED_DIR' is not a directory" >&2; exit 1; }
# Validate every entry before cp -a touches any of them: `cp -a` preserves
# a symlink rather than following it, so one placed in SEED_DIR would land
# as a live symlink under /userdata/warden that a later read (flared's
# settings loader, running inside the guest) resolves through. Fail closed
# on the first bad entry the same way --state fails closed above, rather
# than copy it into the image and let it surface as a confusing read
# later. Only a top-level plain file matches "one file per settings key"
# (the Env note above), with one exception, profiles/rs485/<slug>.w485p
# (installed device profiles, checked below); seed-fixtures.py nests
# nothing else, and neither should anything else pointed at this hook.
#
# Charset is seed-fixtures.py's KEY_RE, not --state's stricter
# [A-Za-z0-9_.]+ above: seed-fixtures.py deliberately also allows '-' for
# keys like "gas-plant.devices" (flare-edge#151), and committed flow
# specs (gas_compression, gas_plant, liquid_pumping, power_generation)
# already seed hyphenated keys through this exact path -- --state's
# charset would reject every one of them.
while IFS= read -r -d '' entry; do
base="$(basename "$entry")"
case "$base" in
*[!A-Za-z0-9_.-]*)
echo "FATAL: SEED_DIR entry '$base' must match [A-Za-z0-9_.-]+ (it becomes a filename)" >&2
exit 1 ;;
esac
if [ -L "$entry" ]; then
echo "FATAL: SEED_DIR entry '$base' is a symlink (refusing to copy it into userdata verbatim)" >&2
exit 1
fi
# The one directory allowed: installed RS485 device profiles, as
# warden-modbus keeps them (/userdata/warden/profiles/rs485/<slug>.w485p),
# so a flow can boot with a profile already installed (flare-edge#387).
# Nothing in it but plain <slug>.w485p files directly under rs485/.
if [ "$base" = profiles ] && [ -d "$entry" ]; then
while IFS= read -r -d '' p; do
rel="${p#"$entry"/}"
if [ "$rel" = rs485 ] && [ -d "$p" ] && [ ! -L "$p" ]; then continue; fi
case "$rel" in
rs485/*/*) ok=0 ;;
rs485/*.w485p) ok=1 ;;
*) ok=0 ;;
esac
name="${rel#rs485/}"
case "${name%.w485p}" in ''|*[!a-z0-9-]*) ok=0 ;; esac
if [ "$ok" -ne 1 ] || [ -L "$p" ] || [ ! -f "$p" ]; then
echo "FATAL: SEED_DIR entry 'profiles/$rel' is not a plain profiles/rs485/<slug>.w485p file" >&2
exit 1
fi
done < <(find "$entry" -mindepth 1 -print0)
continue
fi
[ -f "$entry" ] || {
echo "FATAL: SEED_DIR entry '$base' is not a plain file (refusing to copy it into userdata verbatim)" >&2
exit 1
}
done < <(find "$SEED_DIR" -mindepth 1 -maxdepth 1 -print0)
cp -a "$SEED_DIR"/. "$UDATA/warden/"
fi
mkdir -p "$SCRATCH/empty"
# mkfs an ext4 partition image of exactly $2 bytes from staged dir $1.
mkfs_part() {
local stage="$1" bytes="$2" img="$3"
rm -f "$img"
truncate -s "$bytes" "$img"
mkfs.ext4 -F -q -d "$stage" "$img"
}
DISK="$OUT/disk.img"
rm -f "$DISK"
# dd a REAL raw image into a partition window, fail-closed on overflow.
place_real_image() { # $1 src image, $2 offset, $3 partition size, $4 name
local srcsz
srcsz=$(stat -c %s "$1")
[ "$srcsz" -le "$3" ] || {
echo "FATAL: $4 image $1 ($srcsz bytes) exceeds the $3-byte partition" >&2
exit 1
}
dd if="$1" of="$DISK" bs=4096 seek=$(($2 / 4096)) conv=notrunc,sparse status=none
qemu_log " $4: REAL image $(basename "$1") ($((srcsz / 1048576))M) @ $2"
}
place_partition() {
local name="$1" off="$2" size="$3" stage=""
case "$name" in
rootfs_a)
if [ -n "$ROOTFS_IMAGE" ]; then
[ -f "$ROOTFS_IMAGE" ] || { echo "FATAL: --rootfs-image $ROOTFS_IMAGE not found" >&2; exit 1; }
DISK_END_TRACK "$off" "$size"
place_real_image "$ROOTFS_IMAGE" "$off" "$size" "$name"
return 0
fi
stage="$ROOT" ;;
oem_a)
if [ -n "$OEM_IMAGE" ]; then
[ -f "$OEM_IMAGE" ] || { echo "FATAL: --oem-image $OEM_IMAGE not found" >&2; exit 1; }
DISK_END_TRACK "$off" "$size"
place_real_image "$OEM_IMAGE" "$off" "$size" "$name"
return 0
fi
stage="$SCRATCH/empty" ;;
rootfs_b) stage="$ROOT" ;;
userdata) stage="$UDATA" ;;
oem_b) stage="$SCRATCH/empty" ;;
# misc carries REAL AvbABData (byte 2048): flared's slotctl fail-closes
# on a bad magic before any OTA write, so a zeroed misc blocks apply
# scenarios. Bytes mirror flare-edge tools/mk-misc.py provisioning
# defaults (A: prio 15 successful, B: prio 14 successful, CRC32-BE).
misc) stage="__misc__" ;;
# env carries a REAL U-Boot environment: flared clears bootcount there
# before an intentional reboot, and treats a zeroed (bad-CRC) env as
# "bookkeeping not durable", so a factory reset on the rig was always
# refused (flare-edge#62). Format mirrors flare-edge flared/src/envf.rs
# and tools/envf.py: LE CRC32 of the rest, NUL-separated entries.
env) stage="__env__" ;;
# Boot-chain partitions the VM never reads: present at the right offsets,
# left zeroed. Enumerated (not a wildcard) so a typo'd name in
# blkdevparts.conf fails HERE, not as a confusing mount error at boot.
idblock|uboot|boot_a|boot_b|recovery) stage="" ;;
*) echo "FATAL: unknown partition name '$name' in blkdevparts.conf" >&2; exit 1 ;;
esac
DISK_END_TRACK "$off" "$size"
# dd in 4K blocks: every offset in the canonical layout is 4K-aligned;
# assert rather than assume, a misaligned write would corrupt a neighbor.
if [ $((off % 4096)) -ne 0 ] || [ $((size % 4096)) -ne 0 ]; then
echo "FATAL: partition $name not 4K-aligned (off=$off size=$size)" >&2
exit 1
fi
[ -z "$stage" ] && return 0
local img="$SCRATCH/$name.img"
if [ "$stage" = "__misc__" ]; then
python3 - "$img" "$size" <<'PYMISC'
import struct, sys, zlib
img, size = sys.argv[1], int(sys.argv[2])
s = bytearray(28)
s[0:4] = b"\0AB0" # AB_MAGIC
s[4] = 1 # major
s[8:12] = bytes([15, 0, 1, 0]) # slot A: priority, tries, successful
s[12:16] = bytes([14, 0, 1, 0]) # slot B
meta = bytes(s) + struct.pack(">I", zlib.crc32(bytes(s)) & 0xFFFFFFFF)
buf = bytearray(size)
buf[2048:2048 + len(meta)] = meta
open(img, "wb").write(buf)
PYMISC
dd if="$img" of="$DISK" bs=4096 seek=$((off / 4096)) \
conv=notrunc,sparse status=none
qemu_log " $name: AvbABData provisioned @ +2048"
return 0
fi
if [ "$stage" = "__env__" ]; then
python3 - "$img" "$size" <<'PYENV'
import struct, sys, zlib
img, size = sys.argv[1], int(sys.argv[2])
ENV_SIZE = 0x8000 # CONFIG_ENV_SIZE; flared refuses any other length
if size < ENV_SIZE:
sys.exit(f"FATAL: env partition is {size} bytes, need {ENV_SIZE}")
body = bytearray(ENV_SIZE - 4)
entries = b"bootcount=0\0\0" # one entry, then the empty terminator
body[:len(entries)] = entries
env = struct.pack("<I", zlib.crc32(bytes(body)) & 0xFFFFFFFF) + bytes(body)
buf = bytearray(size)
buf[:ENV_SIZE] = env
open(img, "wb").write(buf)
PYENV
dd if="$img" of="$DISK" bs=4096 seek=$((off / 4096)) \
conv=notrunc,sparse status=none
qemu_log " $name: U-Boot env provisioned (bootcount=0)"
return 0
fi
mkfs_part "$stage" "$size" "$img"
dd if="$img" of="$DISK" bs=4096 seek=$((off / 4096)) \
conv=notrunc,sparse status=none
qemu_log " $name: ext4, $((size / 1048576))M @ $off"
}
DISK_END=0
# Max, not last: blkdevparts grammar permits explicit @offsets out of order.
DISK_END_TRACK() { [ $(($1 + $2)) -gt "$DISK_END" ] && DISK_END=$(($1 + $2)); return 0; }
qemu_log "building $DISK ($WARDEN_BLKDEVPARTS)"
truncate -s 0 "$DISK"
qemu_each_partition place_partition
truncate -s "$DISK_END" "$DISK"
qemu_log "disk image: $DISK ($(du -h "$DISK" | cut -f1) used, $((DISK_END / 1048576))M apparent)"