Files
bfe-core1106-sdk/qemu/lib.sh
T
BFE EngineeringandClaude Opus 5.5 8fc32976a5
ci / mcdc (push) Successful in 34s
ci / kernel-build (push) Skipped
ci / quality (push) Successful in 46s
ci / bench (push) Successful in 12s
ci / test (push) Successful in 9s
ci / test (pull_request) Successful in 24s
ci / prune-artifacts (pull_request) Skipped
ci / kernel-build (pull_request) Skipped
ci / patches-apply (pull_request) Successful in 21s
ci / prune-artifacts (push) Skipped
ci / patches-apply (push) Successful in 41s
ci / bench (pull_request) Successful in 6s
ci / mcdc (pull_request) Successful in 29s
ci / badges (push) Skipped
ci / kernel-symbols (push) Successful in 1m48s
ci / qemu-tools (push) Successful in 5m39s
ci / quality (pull_request) Successful in 1m44s
ci / badges (pull_request) Skipped
ci / qemu-tools (pull_request) Successful in 3m42s
ci / kernel-symbols (pull_request) Successful in 6m55s
qemu: clear the two shellcheck notes in the prebuilt BusyBox check (flare-edge#2210)
CI's shellcheck 0.9.0 failed qemu-tools on #38: SC2015 on the
`[ -f ] && [ = ] || { ... }` refusal in qemu_get_busybox (now an if), and
SC2012 on the `ls | head` that picks a patch to edit in
busybox-panel-test.sh (now find | sort | head). No behaviour change.

Refs flare-edge#2210

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

183 lines
7.4 KiB
Bash

# shellcheck shell=bash
# Shared helpers for the qemu/ device-sim build scripts. Sourced, not executed.
# Callers must run under `set -euo pipefail` and define QEMU_DIR (the qemu/ dir).
BB_VER=1.31.0
BB_URL="https://busybox.net/downloads/binaries/${BB_VER}-defconfig-multiarch-musl/busybox-armv7l"
# The panel BusyBox source (RIG_BUSYBOX=panel, qemu/build-busybox.sh). From
# https://busybox.net/downloads/busybox-1.37.0.tar.bz2.sha256, the same pin as
# Buildroot 2025.02.8 package/busybox/busybox.hash.
BB_SRC_VER=1.37.0
# Read by build-busybox.sh, which sources this file.
# shellcheck disable=SC2034
BB_SRC_URL="https://busybox.net/downloads/busybox-${BB_SRC_VER}.tar.bz2"
BB_SRC_SHA256=3311dff32e746499f4df0d5df04d7eb396382d7e108bb9250e7b519b837043a4
qemu_log() { printf '\033[36m== %s\033[0m\n' "$*"; }
# Download URL to DEST and verify it against the sha256 WANT. A DEST that
# already matches is kept. FAILS CLOSED: three bounded attempts, then exit 1;
# a mismatching download never lands at DEST.
# Usage: qemu_fetch_verified URL WANT DEST LABEL
qemu_fetch_verified() {
local url="$1" want="$2" dest="$3" label="$4" got
if [ -f "$dest" ]; then
got="$(sha256sum "$dest" | awk '{print $1}')"
if [ "$want" = "$got" ]; then
qemu_log "$label sha256 verified"
return
fi
qemu_log "cached $label failed verification; fetching a clean copy"
fi
# Every attempt is bounded: a server that answers and then stalls would
# otherwise hold the job until its own timeout, with no retry (#1588). Below
# 1 KiB/s for the stall window, or past the total cap, curl gives up and the
# loop moves on. Defaults cap the worst case near 6 minutes; the tests
# shorten them through the environment.
local stall="${BB_FETCH_STALL_SECS:-20}" max="${BB_FETCH_MAX_SECS:-60}"
local delay="${BB_FETCH_RETRY_DELAY:-5}"
local attempt tmp="${dest}.download.$$"
rm -f "$tmp"
for attempt in 1 2 3; do
qemu_log "downloading $url (attempt $attempt/3)"
if curl --retry 1 --retry-delay "$delay" --retry-connrefused \
--connect-timeout "$stall" --speed-limit 1024 --speed-time "$stall" \
--max-time "$max" -fSL "$url" -o "$tmp"; then
got="$(sha256sum "$tmp" | awk '{print $1}')"
if [ "$want" = "$got" ]; then
mv -f "$tmp" "$dest"
qemu_log "$label sha256 verified"
return
fi
echo "$label sha256 mismatch on attempt $attempt: want $want got $got" >&2
fi
rm -f "$tmp"
done
echo "FATAL: could not fetch pinned $label after 3 verified attempts" >&2
exit 1
}
# The source inputs of the panel BusyBox, as one sha256: the pinned source
# tarball and every committed file under qemu/busybox/ the build reads. The
# compiler is not part of it (build-busybox.sh keys its rebuild on that too),
# so a job without the cross compiler can check a binary another job built.
# build-busybox.sh writes it next to the binary as <binary>.inputs.
qemu_busybox_panel_inputs() {
local d="$QEMU_DIR/busybox"
{
echo "busybox $BB_SRC_VER $BB_SRC_SHA256"
(cd "$d" && sha256sum panel.config rig.fragment applets.txt patches/*.patch)
} | sha256sum | awk '{print $1}'
}
# Provide the rig's static armv7 busybox and set $BB to its path.
#
# RIG_BUSYBOX picks which one (flare-edge#2210):
# multiarch (default) the busybox.net $BB_VER defconfig-multiarch-musl
# binary, fetched (or accepted via $BUSYBOX) and
# verified against qemu/busybox.sha256
# panel the image's BusyBox version and applet config, built
# from source by qemu/build-busybox.sh (needs a cross
# compiler; checked by its applet list, see there).
# With $BUSYBOX set, that prebuilt panel binary is used
# instead (a job without the cross compiler, given the
# binary an earlier build job made): it is accepted only
# when its $BUSYBOX.inputs, written by build-busybox.sh,
# equals qemu_busybox_panel_inputs of this tree, so a
# binary built from other config files is refused.
# FAILS CLOSED: a missing pin refuses to build, never silently skips
# verification, mirroring build/build-kernel.sh's tarball handling; an
# unknown RIG_BUSYBOX, or a prebuilt panel binary without matching inputs,
# is refused.
qemu_get_busybox() {
local out="${OUT:-$QEMU_DIR/out}"
mkdir -p "$out"
case "${RIG_BUSYBOX:-multiarch}" in
multiarch) ;;
panel)
if [ -n "${BUSYBOX:-}" ]; then
local want got
want="$(qemu_busybox_panel_inputs)"
got="$(cat "$BUSYBOX.inputs" 2>/dev/null || true)"
if [ ! -f "$BUSYBOX" ] || [ "$got" != "$want" ]; then
echo "FATAL: BUSYBOX=$BUSYBOX is not a panel BusyBox built from this tree's qemu/busybox/ ($BUSYBOX.inputs '${got:-missing}', want '$want'): run qemu/build-busybox.sh" >&2
exit 1
fi
BB="$BUSYBOX"
qemu_log "panel busybox: prebuilt $BB (inputs $want)"
return
fi
OUT="$out" bash "$QEMU_DIR/build-busybox.sh"
BB="$out/busybox-armv7l-panel"
return
;;
*)
echo "FATAL: RIG_BUSYBOX must be multiarch or panel, not '${RIG_BUSYBOX}'" >&2
exit 1
;;
esac
local sha_file="$QEMU_DIR/busybox.sha256"
BB="${BUSYBOX:-$out/busybox-armv7l}"
# Pin first: a missing pin refuses BEFORE downloading, same ordering as
# build/fetch-kernel-tarball.sh.
[ -f "$sha_file" ] || {
echo "FATAL: no pinned sha256 for busybox (expected $sha_file): refusing to build from an unverified binary" >&2
exit 1
}
qemu_fetch_verified "$BB_URL" "$(cat "$sha_file")" "$BB" busybox
}
# Stage the shared rootfs skeleton (qemu/rootfs/ + busybox) into $1.
# Requires qemu_get_busybox to have run (uses $BB).
qemu_stage_rootfs() {
local root="$1"
mkdir -p "$root/bin" "$root/sbin" "$root/dev" "$root/proc" "$root/sys" \
"$root/etc" "$root/tmp" "$root/mnt" "$root/userdata" "$root/oem" \
"$root/usr/bin" "$root/usr/share/udhcpc"
install -m 0755 "$BB" "$root/bin/busybox"
cp -a "$QEMU_DIR/rootfs/." "$root/"
chmod 0755 "$root/init" "$root/sbin/init" "$root/etc/rc" \
"$root/usr/share/udhcpc/default.script"
# git only tracks the executable bit, so a checkout lands etc/shadow at
# whatever the umask gives a non-executable file (644 under the common
# 022) -- root's crypt hash world-readable. Force the normal shadow mode
# here, once, for every caller (mkimage.sh and mkinitramfs.sh both stage
# through this function).
chmod 0600 "$root/etc/shadow"
}
# Parse a "SIZE[@OFFSET](NAME)" blkdevparts entry list (without the "vda:"
# prefix) and invoke a callback `$1 name offset_bytes size_bytes` per entry.
qemu_each_partition() {
local cb="$1" parts entry size_s off_s name size off
parts="${WARDEN_BLKDEVPARTS#*:}"
off=0
local IFS=','
for entry in $parts; do
name="${entry##*(}"; name="${name%)}"
size_s="${entry%%(*}"
if [ "${size_s#*@}" != "$size_s" ]; then
off_s="${size_s#*@}"; size_s="${size_s%@*}"
off="$(qemu_to_bytes "$off_s")"
fi
size="$(qemu_to_bytes "$size_s")"
"$cb" "$name" "$off" "$size"
off=$((off + size))
done
}
qemu_to_bytes() {
local v="$1" n mult=1
case "$v" in
*K) n="${v%K}"; mult=1024 ;;
*M) n="${v%M}"; mult=1048576 ;;
*G) n="${v%G}"; mult=1073741824 ;;
*) n="$v" ;;
esac
case "$n" in
''|*[!0-9]*) echo "FATAL: bad blkdevparts size/offset token: '$v'" >&2; exit 1 ;;
esac
echo $((n * mult))
}