[esp32] Add OTA rollback support (#12460)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Jonathan Swoboda
2025-12-16 20:07:57 -05:00
committed by GitHub
co-authored by Claude
parent 431183eebc
commit 1122ec354f
5 changed files with 39 additions and 9 deletions
@@ -9,6 +9,10 @@
#include <cinttypes>
#include <cstdio>
#ifdef USE_OTA_ROLLBACK
#include <esp_ota_ops.h>
#endif
namespace esphome {
namespace safe_mode {
@@ -32,6 +36,14 @@ void SafeModeComponent::dump_config() {
ESP_LOGW(TAG, "SAFE MODE IS ACTIVE");
}
}
#ifdef USE_OTA_ROLLBACK
const esp_partition_t *last_invalid = esp_ota_get_last_invalid_partition();
if (last_invalid != nullptr) {
ESP_LOGW(TAG, "OTA rollback detected! Rolled back from partition '%s'", last_invalid->label);
ESP_LOGW(TAG, "The device reset before the boot was marked successful");
}
#endif
}
float SafeModeComponent::get_setup_priority() const { return setup_priority::AFTER_WIFI; }
@@ -42,6 +54,10 @@ void SafeModeComponent::loop() {
ESP_LOGI(TAG, "Boot seems successful; resetting boot loop counter");
this->clean_rtc();
this->boot_successful_ = true;
#ifdef USE_OTA_ROLLBACK
// Mark OTA partition as valid to prevent rollback
esp_ota_mark_app_valid_cancel_rollback();
#endif
// Disable loop since we no longer need to check
this->disable_loop();
}