#!/bin/sh
# Puts the mask ROM ELF of every chip the runner emulates at
# .cache/esp-rom-elfs/$RELEASE/<rom> (plan M0). Release 20260528 is pinned by
# the checksum of each ELF itself: other releases ship the same files where
# they ship them at all (20241011 has all but the ESP32-P4 rev >= 3 and the
# ESP32-S31 ROMs, byte for byte), so a copy from any release under
# $IDF_TOOLS_PATH, or ~/.espressif when that is unset, counts, and one that
# differs is passed over. When one is still
# missing, the release tarball is downloaded from GitHub once, checked against
# its own pinned checksum, and every missing ELF is taken from it.
#
# Written for `make setup`, but runs from any directory: .cache/ is always the
# repository's. Prints nothing when every ELF is already in place.
set -eu
cd "$(dirname "$0")/.."

RELEASE=20260528
# One line per chip: the ELF, then its SHA-256. esp32tl/runtime.py's ROMS
# holds the same pins.
ROMS="esp32_rev300_rom.elf 920b70635440517866aab2230964a570d2cf2b676658d93c52fbac108c1cca31
esp32s3_rev0_rom.elf c0ce0f338d1de1bdc6efbef1591779a2a42c1ab7d759d3c6ae8ae63a7dd34cfd
esp32c3_rev3_rom.elf 19ac22e08707df926fb0cf4c54795d4067b983fae8635f396ded173a6d78fc3c
esp32c6_rev0_rom.elf 788e1d38724aeb8fd974fa10c4a7b089c02627d35342ce84b9e0b12b239f3551
esp32p4_rev300_rom.elf 90e723f71fe4701adee7e40bfab6971d8ecf5e356f4d5a81e77743248abc3a97
esp32p4_rev0_rom.elf 948f2c7d108d04a9c3a982f9c1c498fb17f2c4a68abc51f87782592034d7ce44
esp32s31_rev0_rom.elf a52ad7513deb656a910a5740125f1cce2c7941f11ce57213b7b43aea93d5ab87"
TARBALL=esp-rom-elfs-$RELEASE.tar.gz
TARBALL_SHA256=caa463d3cbef2430a5a35847c1d9f2f152403b17a802050927ff60c8da54fe46
URL=https://github.com/espressif/esp-rom-elfs/releases/download/$RELEASE/$TARBALL

dest_dir=.cache/esp-rom-elfs/$RELEASE

# Chosen before anything else: inside a test, a missing tool would only read
# as a checksum that differs, and the script would go on to download.
if command -v sha256sum >/dev/null 2>&1; then
	sha256() { sha256sum "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then
	sha256() { shasum -a 256 "$1" | cut -d' ' -f1; }
else
	echo "fetch-rom-elf: needs sha256sum or shasum" >&2
	exit 1
fi

# The ELFs not in place yet, as "<rom> <sha256>" lines.
missing=$(printf '%s\n' "$ROMS" | while read -r rom digest; do
	if [ ! -f "$dest_dir/$rom" ] || [ "$(sha256 "$dest_dir/$rom")" != "$digest" ]; then
		echo "$rom $digest"
	fi
done)
[ -n "$missing" ] || exit 0

mkdir -p "$dest_dir"
tmp=$(mktemp -d "$dest_dir/.fetch.XXXXXX")
trap 'rm -rf "$tmp"' EXIT
# dash, /bin/sh on Debian and Ubuntu, skips the EXIT trap when a signal ends
# the script; exiting from the signal's own trap runs it.
trap 'exit 130' INT
trap 'exit 143' TERM

# The pinned release first, so that the message names it when it is there.
# IDF_TOOLS_PATH is where ESP-IDF's installer puts its tools when set, in
# place of ~/.espressif, not beside it.
tools=${IDF_TOOLS_PATH:-${HOME:-}/.espressif}/tools/esp-rom-elfs
still=
while read -r rom digest; do
	found=
	pinned=$tools/$RELEASE/$rom
	seen_pinned=
	for f in "$pinned" "$tools"/*/"$rom"; do
		# The glob finds the pinned release again: it is checked once.
		if [ ! -f "$f" ] || { [ "$f" = "$pinned" ] && [ -n "$seen_pinned" ]; }; then
			continue
		fi
		[ "$f" != "$pinned" ] || seen_pinned=1
		if [ "$(sha256 "$f")" != "$digest" ]; then
			echo "fetch-rom-elf: $f differs from release $RELEASE's (SHA-256 $digest), skipped" >&2
			continue
		fi
		# A copy, not a link: an ESP-IDF upgrade may remove the release it came from.
		cp "$f" "$tmp/$rom"
		mv "$tmp/$rom" "$dest_dir/$rom"
		echo "fetch-rom-elf: $dest_dir/$rom copied from $f"
		found=1
		break
	done
	[ -n "$found" ] || still="$still$rom $digest
"
done <<EOF
$missing
EOF
[ -n "$still" ] || exit 0

echo "fetch-rom-elf: $(printf '%s' "$still" | cut -d' ' -f1 | tr '\n' ' ')not under $tools; downloading $URL"
# The tarball is 4.9 MB. A stalled download must not hold a CI job until its limit.
curl -fsSL --retry 3 --connect-timeout 20 --max-time 120 -o "$tmp/$TARBALL" "$URL"
got=$(sha256 "$tmp/$TARBALL")
if [ "$got" != "$TARBALL_SHA256" ]; then
	echo "fetch-rom-elf: $TARBALL has SHA-256 $got, expected $TARBALL_SHA256" >&2
	exit 1
fi
printf '%s' "$still" | while read -r rom digest; do
	tar -xzf "$tmp/$TARBALL" -C "$tmp" "$rom"
	got=$(sha256 "$tmp/$rom")
	if [ "$got" != "$digest" ]; then
		echo "fetch-rom-elf: $rom in $TARBALL has SHA-256 $got, expected $digest" >&2
		exit 1
	fi
	mv "$tmp/$rom" "$dest_dir/$rom"
	echo "fetch-rom-elf: $dest_dir/$rom downloaded"
done
