Files
makronix/tools/write-stick.sh
T
lotherk 1dbc4b8734 uefi stick: write-stick.sh (guarded) moves the backup GPT to the device end; STUPID.md goes (t0086)
The stick boots under OVMF (measured 2026-11-17); the old finding was a serial-line artefact.
The lesson is in tools/AGENTS.md; the status file is deleted as docs/AGENTS.md wants.
2026-11-17 08:52:46 +00:00

147 lines
5.5 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# write-stick.sh -- write a Makronix image to a USB stick, safely
#
# usage: tools/write-stick.sh [-y] IMAGE DEVICE [DEVICE]
#
# refuses everything that is not a removable USB disk, larger-than-image, not
# on the root/boot/swap list, and asks (or, with -y and the device given again,
# expects) the device name before writing.
set -u
YES=0
if [ "${1:-}" = "-y" ]; then
YES=1
shift
fi
[ $# -ge 2 ] || { echo "usage: ${0##*/} [-y] IMAGE DEVICE [DEVICE]" >&2; exit 2; }
IMAGE=$1
DEV=$2
CONFIRM=${3:-}
[ -f "$IMAGE" ] || { echo "write-stick: image not found: $IMAGE" >&2; exit 1; }
[ -b "$DEV" ] || { echo "write-stick: not a block device: $DEV" >&2; exit 1; }
NAME=$(basename "$DEV")
BASE=${NAME%%[0-9]*}
# whole disk only: /sys/block/<name> must exist
[ -d "/sys/block/$NAME" ] || { echo "write-stick: '$DEV' looks like a partition, use the whole disk (e.g. /dev/sdb)" >&2; exit 1; }
# removable / USB transport
REMOVABLE=$(cat "/sys/block/$NAME/removable" 2>/dev/null || echo 0)
TRAN=""
if command -v lsblk >/dev/null 2>&1; then
TRAN=$(lsblk -d -n -o TRAN "$DEV" 2>/dev/null | tr -d ' ')
fi
if [ "$REMOVABLE" != "1" ] && [ "$TRAN" != "usb" ]; then
echo "write-stick: $DEV is not a removable USB device (removable=$REMOVABLE, tran=${TRAN:-unknown})" >&2
exit 1
fi
# root/boot/swap protection
for src in $(findmnt -n -o SOURCE 2>/dev/null); do
case $src in
"$DEV"*) echo "write-stick: $DEV is in use ($src); refusing" >&2; exit 1 ;;
esac
done
if grep -q "^$DEV" /proc/swaps 2>/dev/null; then
echo "write-stick: $DEV is swap; refusing" >&2; exit 1
fi
# size check
IMGSZ=$(stat -c %s "$IMAGE" 2>/dev/null || stat -f %z "$IMAGE")
DEVSZ=$(($(cat "/sys/block/$NAME/size" 2>/dev/null || echo 0) * 512))
if [ "$DEVSZ" -lt "$IMGSZ" ]; then
echo "write-stick: device ($DEVSZ bytes) smaller than image ($IMGSZ bytes)" >&2
exit 1
fi
# sha512 sidecar
DIR=$(dirname "$IMAGE")
BASEN=$(basename "$IMAGE")
FOUND=""
for c in "$DIR/SHA512" "$DIR/$BASEN.sha512" "$DIR/$BASEN.SHA512"; do
if [ -f "$c" ]; then FOUND=$c; break; fi
done
if [ -n "$FOUND" ]; then
EXPECTED=$(grep -i "$BASEN" "$FOUND" 2>/dev/null | head -1 | awk '{print $NF}')
# SHA512 lines are "SHA512 (file) = hash"; pick the hash
EXPECTED=$(grep -i "$BASEN" "$FOUND" 2>/dev/null | sed -n 's/.*= *//p' | head -1 | tr -d ' ')
if [ -n "$EXPECTED" ]; then
GOT=$(sha256sum 2>/dev/null >/dev/null; sha512sum "$IMAGE" 2>/dev/null | awk '{print $1}')
[ -n "$GOT" ] || GOT=$(shasum -a 512 "$IMAGE" 2>/dev/null | awk '{print $1}')
if [ "$GOT" != "$EXPECTED" ]; then
echo "write-stick: sha512 mismatch for $IMAGE" >&2
echo " expected: $EXPECTED" >&2
echo " got : $GOT" >&2
exit 1
fi
fi
fi
echo "Image : $IMAGE ($IMGSZ bytes)"
echo "Device: $DEV ($DEVSZ bytes) removable=$REMOVABLE tran=${TRAN:-unknown}"
if command -v lsblk >/dev/null 2>&1; then
echo "Partitions:"; lsblk "$DEV" 2>/dev/null || true
fi
echo "SHA512: ${GOT:-unknown}"
if [ "$YES" = "1" ]; then
if [ "$CONFIRM" != "$DEV" ]; then
echo "write-stick: with -y you must repeat the device name exactly as the third argument" >&2
exit 1
fi
else
printf 'Type the device name to confirm (%s): ' "$DEV"
read -r ANSWER
[ "$ANSWER" = "$DEV" ] || { echo "write-stick: confirmation mismatch, aborting" >&2; exit 1; }
fi
# unmount partitions
for p in $(lsblk -ln -o NAME "$DEV" 2>/dev/null | tail -n +2); do
umount "/dev/$p" 2>/dev/null || true
done
echo "write-stick: writing..."
dd if="$IMAGE" of="$DEV" bs=4M conv=fsync status=progress
sync
echo "write-stick: verifying first/last 16 MiB..."
BS=16777216
cmp -n "$BS" "$IMAGE" "$DEV" || { echo "verify head failed" >&2; exit 1; }
# tail verify via dd skip (best effort)
if [ "$IMGSZ" -gt "$BS" ]; then
dd if="$IMAGE" of=/tmp/opencode/wstick-tail.img bs=1 skip=$((IMGSZ-BS)) count=$BS 2>/dev/null
dd if="$DEV" of=/tmp/opencode/wstick-tail.dev bs=1 skip=$((IMGSZ-BS)) count=$BS 2>/dev/null
cmp /tmp/opencode/wstick-tail.img /tmp/opencode/wstick-tail.dev || { echo "verify tail failed" >&2; exit 1; }
rm -f /tmp/opencode/wstick-tail.img /tmp/opencode/wstick-tail.dev
fi
# The image is smaller than the stick: its backup GPT (header and entries) is in
# the last 33 sectors of the IMAGE, not at the end of the DEVICE, and the
# protective MBR names the image's size. The UEFI spec wants the backup header in
# the device's last block and some firmware refuses a disk without it (fdisk -l:
# "The backup GPT table is not on the end of the device"). fdisk(8) writes both
# right; the boot code in the first 440 bytes and the partitions stay as they are
# (measured 2026-11-17 on a copy: same checksum of the boot code, same two
# partitions). The stale backup inside the image area is then zeroed.
if [ "$DEVSZ" -gt "$IMGSZ" ]; then
FDISK=$(command -v fdisk 2>/dev/null || true)
[ -n "$FDISK" ] || [ ! -x /usr/sbin/fdisk ] || FDISK=/usr/sbin/fdisk
if [ -n "$FDISK" ]; then
echo "write-stick: moving the backup GPT to the end of the device..."
printf 'w\n' | "$FDISK" "$DEV" > /dev/null 2>&1 ||
echo "write-stick: warning: fdisk could not rewrite the GPT of $DEV" >&2
dd if=/dev/zero of="$DEV" bs=512 seek=$((IMGSZ/512-33)) count=33 conv=fsync status=none ||
echo "write-stick: warning: could not clear the old backup GPT" >&2
"$FDISK" -l "$DEV" 2>&1 | grep -i -E 'backup|mismatch|corrupt' ||
echo "write-stick: the GPT of $DEV is consistent"
else
echo "write-stick: fdisk not found: the backup GPT stays inside the image area" >&2
fi
fi
echo "write-stick: done"