Skip to content

Commit 7565d53

Browse files
committed
fix(hw_support): Fix crash when reconfiguring flash from 40 to 80 MHz
Reading from the flash while it is being reconfigured leads to data corruption and a crash when the reconfiguration code is located in flash. This is only an issue if a device has a bootloader that runs with 40 MHz flash and an application flashed via OTA that runs with 80 MHz flash. If bootloader and application run with the same flash speed, the reconfiguration is basically a no-op and no data corruption occurs. Fix reconfiguration by placing the code back into IRAM.
1 parent d3ffbcc commit 7565d53

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

components/esp_system/port/cpu_start.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,22 @@ MSPI_INIT_ATTR void mspi_init(void)
634634
}
635635
#endif
636636
}
637+
638+
#if CONFIG_IDF_TARGET_ESP32
639+
#if !CONFIG_SPIRAM_BOOT_HW_INIT
640+
/*
641+
* Adjust flash configuration. This must be placed in IRAM because running from flash,
642+
* while it is being reconfigured, will result in corrupt data being read.
643+
*/
644+
NOINLINE_ATTR IRAM_ATTR static void configure_flash(esp_image_header_t *fhdr)
645+
{
646+
bootloader_flash_gpio_config(fhdr);
647+
bootloader_flash_dummy_config(fhdr);
648+
bootloader_flash_clock_config(fhdr);
649+
bootloader_flash_cs_timing_config();
650+
}
651+
#endif // !CONFIG_SPIRAM_BOOT_HW_INIT
652+
#endif // CONFIG_IDF_TARGET_ESP32
637653
#endif // !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
638654

639655
/*
@@ -874,10 +890,7 @@ NOINLINE_ATTR static void system_early_init(const soc_reset_reason_t *rst_reas)
874890
#if CONFIG_IDF_TARGET_ESP32
875891
#if !CONFIG_SPIRAM_BOOT_HW_INIT
876892
// If psram is uninitialized, we need to improve some flash configuration.
877-
bootloader_flash_clock_config(&fhdr);
878-
bootloader_flash_gpio_config(&fhdr);
879-
bootloader_flash_dummy_config(&fhdr);
880-
bootloader_flash_cs_timing_config();
893+
configure_flash(&fhdr);
881894
#endif //!CONFIG_SPIRAM_BOOT_HW_INIT
882895
#endif //CONFIG_IDF_TARGET_ESP32
883896

0 commit comments

Comments
 (0)