diff --git a/.gitea/workflows/main.yml b/.gitea/workflows/main.yml index f5bc3cf..f4d23a5 100644 --- a/.gitea/workflows/main.yml +++ b/.gitea/workflows/main.yml @@ -26,6 +26,8 @@ jobs: apt-get install -y -qq --no-install-recommends openssh-client - name: Install Rust quality tools run: rustup component add rustfmt clippy + - name: Image safety gate + run: sh ci/check-image-safety.sh - name: Formatting run: cargo fmt --check - name: Last-attempt badge checks diff --git a/.gitea/workflows/next.yml b/.gitea/workflows/next.yml index 570af52..315b037 100644 --- a/.gitea/workflows/next.yml +++ b/.gitea/workflows/next.yml @@ -15,6 +15,8 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 + - name: Image safety gate + run: sh ci/check-image-safety.sh - name: Build release candidate run: cargo build --release --offline --locked - name: Select candidate version diff --git a/ci/check-image-safety.sh b/ci/check-image-safety.sh new file mode 100644 index 0000000..9dae4be --- /dev/null +++ b/ci/check-image-safety.sh @@ -0,0 +1,92 @@ +#!/bin/sh +# Refuse to publish an OS image whose payload would not fit, or would not boot, +# on the board it claims to target. +# +# The SDK's planner refuses a bad payload before it is ever written; this is the +# other half of the same rule, applied to the artefacts the OS repo produces. A +# wrongly-formatted image is not a firmware bug a board survives: the vendor SPL +# only parses a legacy uImage, stock U-Boot only parses a legacy uImage, and a +# raw zImage in the wrong slot boots nothing. +# +# Checks, per payload file found: +# * magic matches the format its name claims +# * size fits the target partition +# A file that is present but unreadable, or that claims a format it does not +# have, fails the gate. When no payload artefacts exist yet the gate says so and +# passes, so it can be wired into CI before the first image is built. +set -eu + +fail=0 +note() { printf ' %s\n' "$*"; } +bad() { printf ' FAIL %s\n' "$*"; fail=1; } + +# name-glob|format|max-bytes +RULES=' +kernel-mtd8.zImage|zimage|20971520 +zImage|zimage|20971520 +Image|zimage|16777216 +dtb-mtd6.dtb|fdt|131072 +am335x-alkane.dtb|fdt|131072 +meson-axg-alkane.dtb|fdt|131072 +ramdisk-mtd7.uimg|uimage|5242880 +uramdisk.image.gz|uimage|8388608 +uboot-mtd4.img|uimage|1835008 +' + +check_file() { + file=$1 + name=$(basename "$file") + size=$(wc -c < "$file") + rule=$(printf '%s\n' "$RULES" | awk -F'|' -v n="$name" '$1 == n { print; exit }') + if [ -z "$rule" ]; then + return 0 + fi + format=$(printf '%s' "$rule" | cut -d'|' -f2) + limit=$(printf '%s' "$rule" | cut -d'|' -f3) + if [ "$size" -gt "$limit" ]; then + bad "$name is $size bytes, larger than its $limit-byte partition" + return 0 + fi + magic=$(od -An -tx1 -N4 "$file" | tr -d ' \n') + case "$format" in + fdt) + [ "$magic" = "d00dfeed" ] || bad "$name does not start with the FDT magic (got $magic)" + ;; + uimage) + [ "$magic" = "27051956" ] || bad "$name is not a legacy uImage (got $magic)" + ;; + zimage) + # ARM zImage: 0x016f2818 at offset 0x24, stored little-endian on disk. + z=$(od -An -tx1 -j36 -N4 "$file" | tr -d ' \n') + [ "$z" = "18286f01" ] || bad "$name is not a raw ARM zImage (got $z at 0x24)" + ;; + esac + note "$name: $format, $size bytes" +} + +echo "image-safety: scanning for payload artefacts" + +found=0 +for dir in "${1:-.}" dist build/out target/payload; do + [ -d "$dir" ] || continue + for file in $(find "$dir" -maxdepth 3 -type f \ + \( -name 'kernel-mtd8.zImage' -o -name 'zImage' -o -name 'Image' \ + -o -name 'dtb-mtd6.dtb' -o -name 'am335x-alkane.dtb' \ + -o -name 'meson-axg-alkane.dtb' -o -name 'ramdisk-mtd7.uimg' \ + -o -name 'uramdisk.image.gz' -o -name 'uboot-mtd4.img' \) 2>/dev/null); do + found=$((found + 1)) + check_file "$file" + done +done + +if [ "$found" -eq 0 ]; then + note "no payload artefacts present in this checkout" +else + note "$found payload file(s) checked" +fi + +if [ "$fail" -ne 0 ]; then + echo "image-safety: FAILED - do not publish this image" >&2 + exit 1 +fi +echo "image-safety: OK"