ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
safe_mode.cpp
Go to the documentation of this file.
1#include "safe_mode.h"
2
4#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6#include "esphome/core/util.h"
7
8#include <cerrno>
9#include <cinttypes>
10#include <cstdio>
11
12#ifdef USE_OTA_ROLLBACK
13#ifdef USE_ZEPHYR
14#include <zephyr/dfu/mcuboot.h>
15#elif defined(USE_ESP32)
16#include <esp_ota_ops.h>
17#include <esp_system.h>
18#include <esp_image_format.h>
19#endif
20#endif
21
22namespace esphome::safe_mode {
23
24static const char *const TAG = "safe_mode";
25
26#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK) && !defined(USE_OTA_PARTITIONS)
27// Find a non-running app partition. If verify is true, only returns a partition
28// whose image passes verification (expensive: reads flash). Returns nullptr if none found.
29static const esp_partition_t *find_alternate_app_partition(bool verify) {
30 const esp_partition_t *running = esp_ota_get_running_partition();
31 const esp_partition_t *result = nullptr;
32 esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, nullptr);
33 while (it != nullptr) {
34 const esp_partition_t *p = esp_partition_get(it);
35 if (p->address != running->address) {
36 if (!verify) {
37 result = p;
38 break;
39 }
40 esp_image_metadata_t data = {};
41 const esp_partition_pos_t part_pos = {
42 .offset = p->address,
43 .size = p->size,
44 };
45 if (esp_image_verify(ESP_IMAGE_VERIFY_SILENT, &part_pos, &data) == ESP_OK) {
46 result = p;
47 break;
48 }
49 }
50 it = esp_partition_next(it);
51 }
52 esp_partition_iterator_release(it);
53 return result;
54}
55#endif
56
58 ESP_LOGCONFIG(TAG,
59 "Safe Mode:\n"
60 " Successful after: %" PRIu32 "s\n"
61 " Invoke after: %u attempts\n"
62 " Duration: %" PRIu32 "s",
63 this->safe_mode_boot_is_good_after_ / 1000, // because milliseconds
65 this->safe_mode_enable_time_ / 1000); // because milliseconds
66#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK)
67 const char *state_str;
68 if (this->ota_state_ == ESP_OTA_IMG_NEW) {
69#ifdef USE_OTA_PARTITIONS
70 state_str = "support unknown";
71#else
72 state_str = "not supported";
73#endif
74 } else if (this->ota_state_ == ESP_OTA_IMG_PENDING_VERIFY) {
75 state_str = "supported";
76 } else {
77 state_str = "support unknown";
78 }
79 ESP_LOGCONFIG(TAG, " Bootloader rollback: %s", state_str);
80#endif
81
83 auto remaining_restarts = this->safe_mode_num_attempts_ - this->safe_mode_rtc_value_;
84 if (remaining_restarts) {
85 ESP_LOGW(TAG, "Last reset too quick; invoke in %" PRIu32 " restarts", remaining_restarts);
86 } else {
87 ESP_LOGW(TAG, "SAFE MODE IS ACTIVE");
88 }
89 }
90
91#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK)
92 const esp_partition_t *last_invalid = esp_ota_get_last_invalid_partition();
93 if (last_invalid != nullptr) {
94 ESP_LOGW(TAG,
95 "OTA rollback detected! Rolled back from partition '%s'\n"
96 " The device reset before the boot was marked successful",
97 last_invalid->label);
98 if (esp_reset_reason() == ESP_RST_BROWNOUT) {
99 ESP_LOGW(TAG, "Last reset was due to brownout - check your power supply!\n"
100 " See https://esphome.io/guides/faq.html#brownout-detector-was-triggered");
101 }
102 }
103 if (!this->app_ota_possible_) {
104 ESP_LOGW(TAG, "OTA updates are impossible.");
105#ifdef USE_OTA_PARTITIONS
106 ESP_LOGW(TAG, " OTA partition table update or serial flashing is required.");
107#else
108 if (find_alternate_app_partition(false) != nullptr) {
109 ESP_LOGW(TAG, " Activate safe mode to reboot to the recovery partition.");
110 } else {
111 ESP_LOGE(TAG, " No recovery partition available; serial flashing is required.");
112 }
113#endif
114 }
115#endif
116}
117
119
121 this->clean_rtc();
122 this->boot_successful_ = true;
123#if defined(USE_OTA_ROLLBACK)
124// Mark OTA partition as valid to prevent rollback
125#if defined(USE_ZEPHYR)
126 if (!boot_is_img_confirmed()) {
127 boot_write_img_confirmed();
128 }
129#elif defined(USE_ESP32)
130 // Mark OTA partition as valid to prevent rollback
131 esp_ota_mark_app_valid_cancel_rollback();
132#endif
133#endif
134 // Disable loop since we no longer need to check
135 this->disable_loop();
136}
137
139 if (!this->boot_successful_ &&
140 (App.get_loop_component_start_time() - this->safe_mode_start_time_) > this->safe_mode_boot_is_good_after_) {
141 // successful boot, reset counter
142 ESP_LOGI(TAG, "Boot seems successful; resetting boot loop counter");
143 this->mark_successful();
144 }
145}
146
148 uint32_t current_rtc = this->read_rtc_();
149
150 if (pending && current_rtc != SafeModeComponent::ENTER_SAFE_MODE_MAGIC) {
151 ESP_LOGI(TAG, "Device will enter on next boot");
153 }
154
155 if (!pending && current_rtc == SafeModeComponent::ENTER_SAFE_MODE_MAGIC) {
156 ESP_LOGI(TAG, "Safe mode pending has been cleared");
157 this->clean_rtc();
158 }
159}
160
164
165bool SafeModeComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time,
166 uint32_t boot_is_good_after) {
168 this->safe_mode_enable_time_ = enable_time;
169 this->safe_mode_boot_is_good_after_ = boot_is_good_after;
170 this->safe_mode_num_attempts_ = num_attempts;
172
173#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK)
174 // Check partition state to detect if bootloader supports rollback
175 const esp_partition_t *running_part = esp_ota_get_running_partition();
176 esp_ota_get_state_partition(running_part, &this->ota_state_);
177 const esp_partition_t *next_part = esp_ota_get_next_update_partition(nullptr);
178 this->app_ota_possible_ = (next_part != nullptr && next_part != running_part);
179#endif
180
181 uint32_t rtc_val = this->read_rtc_();
182 this->safe_mode_rtc_value_ = rtc_val;
183
184 bool is_manual = rtc_val == SafeModeComponent::ENTER_SAFE_MODE_MAGIC;
185
186 if (is_manual) {
187 ESP_LOGI(TAG, "Manual mode");
188 } else {
189 ESP_LOGCONFIG(TAG, "Unsuccessful boot attempts: %" PRIu32, rtc_val);
190 }
191
192 if (rtc_val < num_attempts && !is_manual) {
193 // increment counter
194 this->write_rtc_(rtc_val + 1);
195 return false;
196 }
197
198 this->clean_rtc();
199
200 if (!is_manual) {
201 ESP_LOGE(TAG, "Boot loop detected");
202 }
203
204#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK) && !defined(USE_OTA_PARTITIONS)
205 // Allow recovery of soft-bricked devices
206 // Instead of starting safe_mode, reboot to the other app partition if all conditions are met:
207 // - app OTA is impossible (for example because the other app partition has type 'factory')
208 // - the other app partition contains a valid app (for example Tasmota safeboot image or ESPHome)
209 // - allow_partition_access is not configured making recovery via partition table update impossible
210 // Image verification is deferred until here so the cost is only paid when entering safe mode,
211 // not on every boot.
212 if (!this->app_ota_possible_) {
213 const esp_partition_t *rollback_part = find_alternate_app_partition(true);
214 if (rollback_part != nullptr) {
215 esp_err_t err = esp_ota_set_boot_partition(rollback_part);
216 if (err == ESP_OK) {
217 ESP_LOGW(TAG, "OTA updates are impossible. Rebooting to recovery app.");
218 App.reboot();
219 } else {
220 ESP_LOGE(TAG, "Failed to set recovery boot partition: %s", esp_err_to_name(err));
221 }
222 }
223 }
224#endif
225
226 this->status_set_error();
227 this->set_timeout(enable_time, []() {
228 ESP_LOGW(TAG, "Timeout, restarting");
229 App.reboot();
230 });
231
232 // Delay here to allow power to stabilize before Wi-Fi/Ethernet is initialised
233 delay(300); // NOLINT
234 App.setup();
235
236 ESP_LOGW(TAG, "SAFE MODE IS ACTIVE");
237
238#ifdef USE_SAFE_MODE_CALLBACK
239 this->safe_mode_callback_.call();
240#endif
241
242 return true;
243}
244
249
252 if (!this->rtc_.load(&val))
253 return 0;
254 return val;
255}
256
258 // Save without sync - preferences will be written at shutdown or by IntervalSyncer.
259 // This avoids blocking the loop for 50+ ms on flash write. If the device crashes
260 // before sync, the boot wasn't really successful anyway and the counter should
261 // remain incremented.
262 uint32_t val = 0;
263 this->rtc_.save(&val);
264}
265
270
271} // namespace esphome::safe_mode
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.
Definition component.h:510
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.
Definition safe_mode.h:48
bool boot_successful_
set to true after boot is considered successful
Definition safe_mode.h:55
uint32_t safe_mode_start_time_
stores when safe mode was enabled
Definition safe_mode.h:50
uint32_t safe_mode_boot_is_good_after_
The amount of time after which the boot is considered successful.
Definition safe_mode.h:47
float get_setup_priority() const override
StaticCallbackManager< ESPHOME_SAFE_MODE_CALLBACK_COUNT, void()> safe_mode_callback_
Definition safe_mode.h:63
esp_ota_img_states_t ota_state_
Definition safe_mode.h:52
void set_safe_mode_pending(const bool &pending)
Set to true if the next startup will enter safe mode.
static const uint32_t ENTER_SAFE_MODE_MAGIC
a magic number to indicate that safe mode should be entered on next boot
Definition safe_mode.h:66
mopeka_std_values val[3]
const std::vector< uint8_t > & data
constexpr uint32_t RTC_KEY
RTC key for storing boot loop counter - used by safe_mode and preferences backends.
Definition safe_mode.h:15
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:53
ESPPreferences * global_preferences
void HOT delay(uint32_t ms)
Definition hal.cpp:82
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
Definition preferences.h:24
bool sync()
Commit pending writes to flash.
Definition preferences.h:32