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) <noreply@anthropic.com>
This commit is contained in:
Mae PUGIN 2026-04-07 15:16:57 +02:00
parent 195fa6e7e8
commit 0448efebc9

View file

@ -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 //
// //