12#ifdef USE_OTA_ROLLBACK
14#include <zephyr/dfu/mcuboot.h>
15#elif defined(USE_ESP32)
16#include <esp_ota_ops.h>
17#include <esp_system.h>
23static const char *
const TAG =
"safe_mode";
28 " Successful after: %" PRIu32
"s\n"
29 " Invoke after: %u attempts\n"
30 " Duration: %" PRIu32
"s",
34#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK)
35 const char *state_str;
37 state_str =
"not supported";
38 }
else if (this->
ota_state_ == ESP_OTA_IMG_PENDING_VERIFY) {
39 state_str =
"supported";
41 state_str =
"support unknown";
43 ESP_LOGCONFIG(TAG,
" Bootloader rollback: %s", state_str);
48 if (remaining_restarts) {
49 ESP_LOGW(TAG,
"Last reset too quick; invoke in %" PRIu32
" restarts", remaining_restarts);
51 ESP_LOGW(TAG,
"SAFE MODE IS ACTIVE");
55#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK)
56 const esp_partition_t *last_invalid = esp_ota_get_last_invalid_partition();
57 if (last_invalid !=
nullptr) {
59 "OTA rollback detected! Rolled back from partition '%s'\n"
60 " The device reset before the boot was marked successful",
62 if (esp_reset_reason() == ESP_RST_BROWNOUT) {
63 ESP_LOGW(TAG,
"Last reset was due to brownout - check your power supply!\n"
64 " See https://esphome.io/guides/faq.html#brownout-detector-was-triggered");
75#if defined(USE_OTA_ROLLBACK)
77#if defined(USE_ZEPHYR)
78 if (!boot_is_img_confirmed()) {
79 boot_write_img_confirmed();
81#elif defined(USE_ESP32)
83 esp_ota_mark_app_valid_cancel_rollback();
94 ESP_LOGI(TAG,
"Boot seems successful; resetting boot loop counter");
103 ESP_LOGI(TAG,
"Device will enter on next boot");
108 ESP_LOGI(TAG,
"Safe mode pending has been cleared");
125#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK)
127 const esp_partition_t *running = esp_ota_get_running_partition();
128 esp_ota_get_state_partition(running, &this->
ota_state_);
137 ESP_LOGI(TAG,
"Manual mode");
139 ESP_LOGCONFIG(TAG,
"Unsuccessful boot attempts: %" PRIu32, rtc_val);
142 if (rtc_val < num_attempts && !is_manual) {
151 ESP_LOGE(TAG,
"Boot loop detected");
156 ESP_LOGW(TAG,
"Timeout, restarting");
164 ESP_LOGW(TAG,
"SAFE MODE IS ACTIVE");
166#ifdef USE_SAFE_MODE_CALLBACK
void setup()
Reserve space for components to avoid memory fragmentation.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void disable_loop()
Disable this component's loop.
bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time, uint32_t boot_is_good_after)
uint32_t safe_mode_enable_time_
The time safe mode should remain active for.
void on_safe_shutdown() override
bool boot_successful_
set to true after boot is considered successful
uint32_t safe_mode_start_time_
stores when safe mode was enabled
uint32_t safe_mode_boot_is_good_after_
The amount of time after which the boot is considered successful.
float get_setup_priority() const override
esp_ota_img_states_t ota_state_
uint32_t safe_mode_rtc_value_
void dump_config() override
void set_safe_mode_pending(const bool &pending)
Set to true if the next startup will enter safe mode.
void write_rtc_(uint32_t val)
bool get_safe_mode_pending()
static const uint32_t ENTER_SAFE_MODE_MAGIC
a magic number to indicate that safe mode should be entered on next boot
CallbackManager< void()> safe_mode_callback_
uint8_t safe_mode_num_attempts_
constexpr uint32_t RTC_KEY
RTC key for storing boot loop counter - used by safe_mode and preferences backends.
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
ESPPreferences * global_preferences
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
bool sync()
Commit pending writes to flash.