# STM32 & ESP32 HTTP Range 206 OTA Debugging & Dual-Bank Rollback Architecture
> Canonical: https://space-bit.com/guides/stm32-esp32-http-range-206-ota-dual-bank-rollback/ | Alternate: https://spacebit.cn/guides/stm32-esp32-http-range-206-ota-dual-bank-rollback/

Why do firmware downloads pass HTTP 200 on lab Wi-Fi yet silently fail or brick low-RAM MCUs over garage cellular links? A byte-level breakdown of truncated presigned URLs, 4KB TLS alignment, and watchdog rollback.

## TL;DR Constrained MCU OTA Laws (Citable Facts)
- Keep OTA URLs <= 48 Bytes: Standard S3/OSS presigned URLs run 450–600 bytes and silently overflow 128B/256B C `char url[]` buffers mid-signature; always terminate cloud tokens behind a short-signed Go gateway.
- Require True `HTTP/1.1 206 Partial Content`: Stream 4KB–16KB chunks matched to MCU Flash page sizes with per-chunk CRC32 so spotty 4G/Wi-Fi drops resume from the exact failed offset.
- Align TLS Records to 4KB (`ssl_buffer_size 4k`): Shrinks `MBEDTLS_SSL_IN_CONTENT_LEN` memory overhead without choking on 16KB upstream TLS frames during OTA handshakes.
- Gate A/B Boot Commit on Cloud Heartbeat: Verify full-slot SHA-256 before rebooting to Slot B, then require MQTTs/OCPP health check-in within 3 boot watchdog cycles or auto-revert cleanly to Slot A.

## Embedded OTA Transport & Flash Recovery Comparison
|Criteria|Short-Signed Gateway + HTTP 206 Range + A/B|Direct S3/OSS Presigned Full-Image GET|Raw MQTT QoS1 Binary Chunking|
|---|---|---|---|
|URL Buffer Footprint|Fixed `<= 48B` path (fits any static C buffer)|450–650 bytes (silently truncated into 403 failures)|No URL, but couples heavy blobs to telemetry link|
|Peak MCU RAM Usage|4KB–16KB chunk + 4KB TLS record (<24KB heap)|16KB+ TLS record buffers frequently trigger OOM resets|High broker memory fan-out during fleet waves|
|Garage / 4G Resume|Exact byte-offset `206 Partial Content` + CRC32|Resets to 0% on every TCP socket drop|Custom sequence window & ACK logic required|
|Power-Loss Safety|Inactive Slot B write + SHA-256 + 3-boot WDT revert|Single-bank overwrite leaves dead unit on brownout|Large binaries starve normal control messages|
|Canary Rollout Safety|1% → 10% → 50% → 100% auto-halt on error spike|Blind bulk triggers without error circuit breaker|Difficult per-stage bandwidth pacing|

## 4 Constrained OTA Production Fixes We Ship
### 01 · Stopping Silent `snprintf` Truncation Before It Ships
Cloud SDKs generate 500B+ query strings. Embedded C firmware capping URL buffers at 128B/256B chops off the signature tail and fails with cryptic HTTP 403/400. A compact HMAC gateway `/f/<id>?s=<token>` keeps URLs under 48 bytes.

### 02 · Enforcing Strict `206 Partial Content` & 4KB TLS Alignment
Simple HTTP stacks break if servers fall back to `200 OK` streaming megabytes at once. Pairing explicit Range headers with `ssl_buffer_size 4k` prevents mbedTLS read-buffer starvation.

### 03 · Post-Boot Connectivity Confirmation vs. Mere CRC Boot
Never commit Slot B purely because the MCU booted to `main()`. Wait until the new image completes a real TLS 8883 or OCPP handshake before calling `mark_app_valid`, eliminating 'orphan bricks'.

### 04 · Automated Canary Gates & Open-Source Reference Server
Our `aiot-production-kit` Go server (`cmd/constrained-ota-server`) provides a working reference for short-signed Range 206 manifests and automatic halt triggers.

## FAQ
### What if our MCU lacks enough internal Flash for full Dual-Bank A/B partitions?
We stage downloaded Range chunks onto your cellular/Wi-Fi co-processor or external SPI NOR Flash (4–8MB), verify the full SHA-256 digest before entering bootloader swap, drastically shrinking the power-loss window.

### Do you provide open-source code we can test immediately for short-signed Range OTA?
Yes—check `bobobo2026/aiot-production-kit` on GitHub (`cmd/constrained-ota-server`) for our Go reference server implementing short-signed paths, Range 206 streaming, and staged rollout limits.

### Can legacy devices already deployed in the field migrate cleanly without truck rolls?
Yes. We typically ship an interim compatibility bridge on the cloud gateway or a minimal staged bootloader update so existing units transition safely.

### What can a 2–3 week Lean POC verify for our stalled OTA pipeline?
We wire 1–2 of your actual EVT/DVT boards to our Go OTA gateway under simulated packet-loss and hard power-cuts, proving resumable 206 transfers and automatic A/B rollback end-to-end.
