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