ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ota_partitions_esp_idf.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
3
5
6#ifdef USE_OTA_PARTITIONS
8#include "esphome/core/log.h"
9
10#include <esp_image_format.h>
11#include <esp_ota_ops.h>
12#include <nvs_flash.h>
13
14#include <cinttypes>
15#include <cstring>
16
17namespace esphome::ota {
18
19static const char *const TAG = "ota.idf";
20
21static inline bool check_overlap(uint32_t a_offset, size_t a_size, uint32_t b_offset, size_t b_size) {
22 return (a_offset + a_size > b_offset && b_offset + b_size > a_offset);
23}
24
25// Wraps esp_partition_find/_get/_next/_release. Returns nullptr if no APP partition at `address`
26// is at least `min_size` bytes.
27static const esp_partition_t *find_app_partition_at(uint32_t address, size_t min_size) {
28 const esp_partition_t *found = nullptr;
29 esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, nullptr);
30 while (it != nullptr) {
31 const esp_partition_t *p = esp_partition_get(it);
32 if (p->address == address && p->size >= min_size) {
33 found = p;
34 break;
35 }
36 it = esp_partition_next(it);
37 }
38 esp_partition_iterator_release(it);
39 return found;
40}
41
42// Validates the staged partition table and picks the post-update boot slot. All non-destructive
43// checks live here; the destructive write is in update_partition_table().
44// Side effect: registers the live partition-table region as partition_table_part_ so the caller
45// can write to it; abort() releases it on error.
47 PartitionTablePlan &plan) {
49 if (validate_result != OTA_RESPONSE_OK) {
50 return validate_result;
51 }
52
53 int num_partitions = 0;
54 const esp_partition_info_t *new_partition_table = reinterpret_cast<const esp_partition_info_t *>(this->buf_);
55 esp_err_t err = esp_partition_table_verify(new_partition_table, true, &num_partitions);
56 if (err != ESP_OK) {
57 ESP_LOGE(TAG, "esp_partition_table_verify failed (new partition table) (err=0x%X)", err);
59 }
60
61 // esp_partition_table_verify does not catch a missing MD5 entry, but the bootloader refuses
62 // to boot from a table without one.
63 bool checksum_found = false;
64 for (size_t i = 0; i < ESP_PARTITION_TABLE_MAX_ENTRIES; i++) {
65 if (new_partition_table[i].magic == ESP_PARTITION_MAGIC_MD5) {
66 checksum_found = true;
67 break;
68 }
69 }
70 if (!checksum_found) {
71 ESP_LOGE(TAG, "New partition table has no checksum");
73 }
74
75 // Slot-selection policy when multiple slots can host the running app: pick the FIRST eligible
76 // slot in table order, preferring the no-copy path (matching offset) over the copy path.
77 // Deterministic and table-ordering-stable.
78 int app_partitions_found = 0;
79 int new_app_part_index = -1;
80 int new_app_part_index_with_copy = -1;
81 const esp_partition_t *app_copy_dest_part = nullptr;
82 bool otadata_partition_found = false;
83 bool otadata_overlap = false;
84 bool nvs_partition_found = false;
85 for (int i = 0; i < num_partitions; i++) {
86 const esp_partition_info_t *new_part = &new_partition_table[i];
87 if (new_part->type == ESP_PARTITION_TYPE_APP) {
88 app_partitions_found++;
89 if (new_part->pos.size >= running_app_size) {
90 if (new_part->pos.offset == running_app_offset) {
91 if (new_app_part_index == -1) {
92 new_app_part_index = i;
93 }
94 } else if (new_app_part_index_with_copy == -1 &&
95 !check_overlap(running_app_offset, running_app_size, new_part->pos.offset, running_app_size)) {
96 // esp_partition_copy writes into a registered partition; need one at this offset in the
97 // current table.
98 const esp_partition_t *p = find_app_partition_at(new_part->pos.offset, running_app_size);
99 if (p != nullptr) {
100 new_app_part_index_with_copy = i;
101 app_copy_dest_part = p;
102 }
103 }
104 }
105 } else if (new_part->type == ESP_PARTITION_TYPE_DATA) {
106 if (new_part->subtype == ESP_PARTITION_SUBTYPE_DATA_OTA) {
107 otadata_partition_found = true;
108 otadata_overlap = check_overlap(running_app_offset, running_app_size, new_part->pos.offset, new_part->pos.size);
109 } else if (new_part->subtype == ESP_PARTITION_SUBTYPE_DATA_NVS &&
110 strncmp(reinterpret_cast<const char *>(new_part->label), "nvs", sizeof(new_part->label)) == 0) {
111 nvs_partition_found = true;
112 }
113 }
114 }
115
116 if (new_app_part_index == -1 && new_app_part_index_with_copy == -1) {
117 // Most likely cause: the user picked the wrong migration .bin for their running app's size.
118 // Rejecting here is non-destructive (no flash op has run yet); the user can safely retry with
119 // a different .bin. Log enough info that they can pick the right method without guessing.
120 ESP_LOGE(TAG,
121 "The new partition table must contain a compatible app partition with:\n"
122 " size: at least %" PRIu32 " bytes (0x%" PRIX32 ")\n"
123 " address: one of",
124 (uint32_t) running_app_size, (uint32_t) running_app_size);
125 esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, nullptr);
126 while (it != nullptr) {
127 const esp_partition_t *partition = esp_partition_get(it);
128 if (partition->size >= running_app_size) {
129 ESP_LOGE(TAG, " 0x%" PRIX32, partition->address);
130 }
131 it = esp_partition_next(it);
132 }
133 esp_partition_iterator_release(it);
134 ESP_LOGE(TAG, "Upload a different partition table. No flash content was modified.");
136 }
137 if (app_partitions_found < 2) {
138 ESP_LOGE(TAG, "New partition table needs at least 2 app partitions, found %d", app_partitions_found);
140 }
141 if (!otadata_partition_found) {
142 ESP_LOGE(TAG, "New partition table is missing the required otadata partition");
144 }
145 if (!nvs_partition_found) {
146 ESP_LOGE(TAG, "New partition table is missing the required nvs partition");
148 }
149 if (otadata_overlap) {
150 // Unlikely, the otadata partition is before the start of the first app partition in most cases
151 ESP_LOGE(TAG,
152 "New otadata partition overlaps with the running app at address: 0x%" PRIX32 ", running app size: %" PRIu32
153 " bytes",
154 running_app_offset, (uint32_t) running_app_size);
156 }
157
158 if (new_app_part_index != -1) {
159 plan.target_app_index = new_app_part_index;
160 plan.copy_dest_part = nullptr;
161 } else {
162 plan.target_app_index = new_app_part_index_with_copy;
163 plan.copy_dest_part = app_copy_dest_part;
164 }
165 return OTA_RESPONSE_OK;
166}
167
169 if (this->buf_written_ == 0 || this->image_size_ != this->buf_written_) {
170 ESP_LOGE(TAG, "Not enough data received");
172 }
173
174 // Without a valid running-app size we cannot compute overlap or copy bounds. zero indicates
175 // esp_ota_get_running_partition() failed (e.g. cache unloaded by a previous aborted OTA).
176 uint32_t running_app_offset;
177 size_t running_app_size;
178 get_running_app_position(running_app_offset, running_app_size);
179 if (running_app_size == 0) {
180 ESP_LOGE(TAG, "Failed to determine running app position");
182 }
183
185 OTAResponseTypes validate_result = this->validate_new_partition_table_(running_app_offset, running_app_size, plan);
186 if (validate_result != OTA_RESPONSE_OK) {
187 return validate_result;
188 }
189
190 // ERROR severity so the warning shows up in default log filters; any failure past this point
191 // can leave the device unbootable until it is recovered with a serial flash.
192 ESP_LOGE(TAG, "Starting partition table update.\n"
193 " DO NOT REMOVE POWER until the device reboots successfully.\n"
194 " Loss of power during this operation may render the device\n"
195 " unable to boot until it is recovered via a serial flash.");
196
197 // One guard over the whole critical section in case an IDF call takes longer than expected on
198 // some chip variant.
199 watchdog::WatchdogManager watchdog(15000);
200
201 esp_err_t err;
202 const esp_partition_info_t *new_partition_table = reinterpret_cast<const esp_partition_info_t *>(this->buf_);
203
204 if (plan.copy_dest_part != nullptr) {
205 // Resolve the source via running_app_offset rather than esp_ota_get_running_partition() in
206 // case a prior aborted partition-table OTA called esp_partition_unload_all() in this boot,
207 // which leaves esp_ota_get_running_partition() returning nullptr.
208 const esp_partition_t *running_app_part = find_app_partition_at(running_app_offset, running_app_size);
209 if (running_app_part == nullptr) {
210 ESP_LOGE(TAG, "Cannot resolve running app partition at address 0x%" PRIX32, running_app_offset);
212 }
213 ESP_LOGD(TAG, "Copying running app from 0x%X to 0x%X (size: 0x%X)", running_app_part->address,
214 plan.copy_dest_part->address, running_app_size);
215 err = esp_partition_copy(plan.copy_dest_part, 0, running_app_part, 0, running_app_size);
216 if (err != ESP_OK) {
217 ESP_LOGE(TAG, "esp_partition_copy failed (err=0x%X)", err);
219 }
220 }
221
222 // Deinit NVS only just before the first destructive write so verify/copy failure paths return
223 // with NVS still functional. From this point on, components that hold open NVS handles
224 // (e.g. preferences) will fail with ESP_ERR_NVS_INVALID_HANDLE on success or failure;
225 // nvs_flash_init() can re-init the subsystem but cannot revive existing handles. On the
226 // success path the device reboots immediately afterwards so this doesn't matter; on the
227 // failure path the user must reboot the device before retrying.
228 nvs_flash_deinit();
229
230 // Update the partition table
231 err = esp_ota_begin(this->partition_table_part_, ESP_PARTITION_TABLE_MAX_LEN, &this->update_handle_);
232 if (err != ESP_OK) {
233 esp_ota_abort(this->update_handle_);
234 this->update_handle_ = 0;
235 ESP_LOGE(TAG, "esp_ota_begin failed (err=0x%X)", err);
237 }
238 err = esp_ota_write(this->update_handle_, this->buf_, ESP_PARTITION_TABLE_MAX_LEN);
239 if (err != ESP_OK) {
240 esp_ota_abort(this->update_handle_);
241 this->update_handle_ = 0;
242 ESP_LOGE(TAG, "esp_ota_write failed (err=0x%X)", err);
244 }
245 err = esp_ota_end(this->update_handle_);
246 this->update_handle_ = 0; // esp_ota_end releases the handle internally
247 if (err != ESP_OK) {
248 ESP_LOGE(TAG, "esp_ota_end failed (err=0x%X)", err);
250 }
251 // unload first, then null the member pointer; if abort() ran between the two steps it would
252 // see a freed pointer. esp_partition_unload_all() invalidates partition_table_part_ too, so
253 // an explicit deregister would be redundant.
254 esp_partition_unload_all();
255 this->partition_table_part_ = nullptr;
256
257 // Write otadata to set the new boot partition
258 const esp_partition_info_t *new_part = &new_partition_table[plan.target_app_index];
259 const esp_partition_t *new_boot_partition = find_app_partition_at(new_part->pos.offset, 0);
260 if (new_boot_partition == nullptr) {
261 ESP_LOGE(TAG, "Selected app partition not found after partition table update");
263 }
264 ESP_LOGD(TAG, "Setting next boot partition to 0x%X", new_boot_partition->address);
265 err = esp_ota_set_boot_partition(new_boot_partition);
266 if (err != ESP_OK) {
267 ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (err=0x%X)", err);
269 }
270 return OTA_RESPONSE_OK;
271}
272
274 esp_err_t err = esp_partition_register_external(
275 nullptr, ESP_PRIMARY_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_SIZE, "PrimaryPrtTable",
276 ESP_PARTITION_TYPE_PARTITION_TABLE, ESP_PARTITION_SUBTYPE_PARTITION_TABLE_PRIMARY, &this->partition_table_part_);
277 if (err != ESP_OK) {
278 ESP_LOGE(TAG, "esp_partition_register_external failed (partition table) (err=0x%X)", err);
280 }
281
282 int num_partitions = 0;
283 const esp_partition_info_t *existing_partition_table = nullptr;
284 esp_partition_mmap_handle_t partition_table_map;
285 err = esp_partition_mmap(this->partition_table_part_, 0, ESP_PARTITION_TABLE_MAX_LEN, ESP_PARTITION_MMAP_DATA,
286 reinterpret_cast<const void **>(&existing_partition_table), &partition_table_map);
287 if (err != ESP_OK) {
288 ESP_LOGE(TAG, "esp_partition_mmap failed (partition table) (err=0x%X)", err);
290 }
291 err = esp_partition_table_verify(existing_partition_table, true, &num_partitions);
292 esp_partition_munmap(partition_table_map);
293 if (err != ESP_OK) {
294 ESP_LOGE(TAG, "esp_partition_table_verify failed (existing partition table) (err=0x%X)", err);
296 }
297 return OTA_RESPONSE_OK;
298}
299
300// Process-scoped cache. Cannot be a backend member: backends are per-connection but the cache
301// must outlive a connection that called esp_partition_unload_all(), after which
302// esp_ota_get_running_partition() no longer returns valid data.
303static bool s_running_app_initialized = false;
304static uint32_t s_running_app_cached_offset = 0;
305static size_t s_running_app_cached_size = 0;
306
307// Flag-gated rather than size==0 so a failed first call doesn't poison the cache.
308void get_running_app_position(uint32_t &offset, size_t &size) {
309 if (!s_running_app_initialized) {
310 const esp_partition_t *running_app_part = esp_ota_get_running_partition();
311 if (running_app_part == nullptr || running_app_part->erase_size == 0) {
312 // Surface zeros without committing to the cache so a later call has a chance to succeed.
313 offset = 0;
314 size = 0;
315 return;
316 }
317
318 uint32_t pending_offset = running_app_part->address;
319 size_t pending_size = running_app_part->size;
320
321 const esp_partition_pos_t running_app_pos = {
322 .offset = running_app_part->address,
323 .size = running_app_part->size,
324 };
325 esp_image_metadata_t image_metadata = {};
326 image_metadata.start_addr = running_app_part->address;
327 if (esp_image_verify(ESP_IMAGE_VERIFY_SILENT, &running_app_pos, &image_metadata) == ESP_OK &&
328 image_metadata.image_len < running_app_part->size) {
329 pending_size = image_metadata.image_len;
330 }
331 // Round up to a full flash sector so the copy spans complete erase blocks.
332 pending_size = ((pending_size + running_app_part->erase_size - 1) / running_app_part->erase_size) *
333 running_app_part->erase_size;
334
335 s_running_app_cached_offset = pending_offset;
336 s_running_app_cached_size = pending_size;
337 s_running_app_initialized = true;
338 }
339
340 offset = s_running_app_cached_offset;
341 size = s_running_app_cached_size;
342}
343
344} // namespace esphome::ota
345
346#endif // USE_OTA_PARTITIONS
347#endif // USE_ESP32
uint8_t address
Definition bl0906.h:4
OTAResponseTypes validate_new_partition_table_(uint32_t running_app_offset, size_t running_app_size, PartitionTablePlan &plan)
OTAResponseTypes register_and_validate_partition_table_part_()
void get_running_app_position(uint32_t &offset, size_t &size)
@ OTA_RESPONSE_ERROR_PARTITION_TABLE_UPDATE
Definition ota_backend.h:46
@ OTA_RESPONSE_ERROR_PARTITION_TABLE_VERIFY
Definition ota_backend.h:45
uint16_t size
Definition helpers.cpp:25
static void uint32_t