From 0448efebc99d9cba488ff908c8a5eb373b61e8be Mon Sep 17 00:00:00 2001 From: Mae PUGIN <48982737+mornepousse@users.noreply.github.com> Date: Tue, 7 Apr 2026 15:16:57 +0200 Subject: [PATCH] fix: Auto-erase otadata when flashing factory partition When writing to factory (0x20000), automatically erase the otadata partition (0xF000, 8KB) by writing 0xFF blocks. This forces the bootloader to fall back to factory on next boot, instead of continuing to boot from ota_0. Without this, flashing factory "succeeds" but the ESP keeps booting the old firmware from ota_0. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/logic/flasher.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/logic/flasher.rs b/src/logic/flasher.rs index 7a197ea..606902c 100644 --- a/src/logic/flasher.rs +++ b/src/logic/flasher.rs @@ -648,9 +648,29 @@ pub fn flash_firmware( // We pass reboot=false here so the chip stays in bootloader mode for // // the MD5 verification step that follows. A hard reset is done after. // // ------------------------------------------------------------------ // - send_progress(0.93, "Finalizing write...".into()); + send_progress(0.92, "Finalizing write...".into()); flash_end(&mut port, false)?; + // ------------------------------------------------------------------ // + // Step 8b: Erase otadata partition (0xF000, 8KB) when flashing factory // + // // + // If we just wrote to the factory partition, the bootloader might // + // still have otadata pointing to ota_0. We erase otadata to force // + // the bootloader to fall back to factory on next boot. // + // ------------------------------------------------------------------ // + if offset == 0x20000 { + send_progress(0.93, "Erasing otadata (force factory boot)...".into()); + const OTADATA_OFFSET: u32 = 0xF000; + const OTADATA_SIZE: u32 = 0x2000; // 8 KB + flash_begin(&mut port, OTADATA_OFFSET, OTADATA_SIZE, FLASH_BLOCK_SIZE)?; + let empty_block = vec![0xFFu8; FLASH_BLOCK_SIZE as usize]; + let otadata_blocks = (OTADATA_SIZE + FLASH_BLOCK_SIZE - 1) / FLASH_BLOCK_SIZE; + for i in 0..otadata_blocks { + flash_data(&mut port, i, &empty_block)?; + } + flash_end(&mut port, false)?; + } + // ------------------------------------------------------------------ // // Step 9: MD5 post-write verification // // //