#!/usr/bin/env bash # Builds, picks a flash offset that avoids the module's defective byte, flashes, # and prints the serial log. # # The module this was developed against has one byte in its SPI flash at # 0xFADE7 whose bits 5..7 cannot be programmed (see README). Whatever value the # app image places there gets corrupted, the image checksum fails, and the # bootloader refuses to boot it. The image itself is unaffected by where it is # placed in flash, so moving the app partition is enough: this picks the lowest # 64 KB-aligned offset whose byte at that address the flash can actually store. # # Usage: scripts/flash.sh [--monitor SECONDS] set -euo pipefail cd "$(dirname "$0")/.." readonly BAD_FLASH_ADDR="${BAD_FLASH_ADDR:-0xFADE7}" readonly ALIGN="${ALIGN:-0x10000}" readonly MAX_APP_SIZE="${MAX_APP_SIZE:-0x120000}" MONITOR_SECS=25 if [ "${1:-}" = "--monitor" ]; then MONITOR_SECS="${2:-25}" fi port="${ESP_PORT:-}" if [ -z "$port" ]; then port=$(ls /dev/cu.usbmodem* /dev/cu.wchusbserial* /dev/cu.SLAB* /dev/cu.usbserial* 2>/dev/null | head -1 || true) fi if [ -z "$port" ]; then echo "no serial port found; set ESP_PORT=/dev/cu.xxx" >&2 exit 1 fi out=$(ls -d target/xtensa-esp32s3-espidf/release/build/esp-idf-sys-*/out/build 2>/dev/null | head -1 || true) echo "==> build" ./scripts/build.sh echo "==> pick app offset" espflash save-image --chip esp32s3 --format esp-idf \ target/xtensa-esp32s3-espidf/release/esp32 /tmp/esp32-cam-app.bin >/dev/null read -r offset < <(python3 - <= len(data): continue if data[at] & 0xE0 == 0: # top three bits must be storable print(off) break else: sys.exit("no usable app offset found; this module's flash is too broken") PY ) printf '0x%x\n' "$offset" # Write the chosen offset into partitions.csv. python3 - "$offset" <<'PY' import re, sys offset = int(sys.argv[1], 0) p = "partitions.csv" s = open(p).read() s2 = re.sub(r"^(factory,\s+app,\s+factory,\s+)0x[0-9a-fA-F]+", lambda m: m.group(1) + "0x%06x" % offset, s, flags=re.M) if s2 != s: open(p, "w").write(s2) print("partitions.csv updated") else: print("partitions.csv already at 0x%06x" % offset) PY echo "==> reconfigure + rebuild with the new table" rm -rf target/xtensa-esp32s3-espidf/release/build/esp-idf-sys-* ./scripts/build.sh espflash save-image --chip esp32s3 --format esp-idf \ target/xtensa-esp32s3-espidf/release/esp32 /tmp/esp32-cam-app.bin >/dev/null out=$(ls -d target/xtensa-esp32s3-espidf/release/build/esp-idf-sys-*/out/build | head -1) # The rebuild can shift the image, so confirm before committing to the flash. python3 - <&2; exit 1; } import sys data = open("/tmp/esp32-cam-app.bin", "rb").read() at = $BAD_FLASH_ADDR - $offset if not (0 <= at < len(data)) or data[at] & 0xE0: print("byte at 0x%X is 0x%02X, not storable" % ($BAD_FLASH_ADDR, data[at] if 0 <= at < len(data) else -1)) sys.exit(1) print("byte at 0x%X is 0x%02X, storable" % ($BAD_FLASH_ADDR, data[at])) PY echo "==> flash $port" esptool_py=$(ls "$HOME"/.espressif/tools/python/*/venv/bin/python 2>/dev/null | head -1) "$esptool_py" -m esptool --chip esp32s3 --port "$port" --baud 460800 write-flash \ 0x0 "$out/bootloader/bootloader.bin" \ 0x8000 "$out/partition_table/partition-table.bin" \ "$offset" /tmp/esp32-cam-app.bin 2>&1 | grep -viE 'writing at|^\[1A' | tail -4 if [ "$MONITOR_SECS" -gt 0 ]; then echo "==> serial log (${MONITOR_SECS}s)" python3 - "$port" "$MONITOR_SECS" <<'PY' import os, sys, termios, time fd = os.open(sys.argv[1], os.O_RDONLY | os.O_NOCTTY) a = termios.tcgetattr(fd) a[0] = a[1] = a[3] = 0 a[2] = termios.CS8 | termios.CREAD | termios.CLOCAL a[4] = a[5] = termios.B115200 a[6][termios.VMIN] = 0 a[6][termios.VTIME] = 1 termios.tcsetattr(fd, termios.TCSANOW, a) end = time.time() + float(sys.argv[2]) while time.time() < end: try: d = os.read(fd, 4096) except OSError: break if d: sys.stdout.write(d.decode("utf-8", "replace")) sys.stdout.flush() PY fi