1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdlib.h>
16 #include <assert.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include <sys/lock.h>
20 #include "esp_flash_partitions.h"
21 #include "esp_attr.h"
22 #include "esp_flash.h"
23 #include "esp_spi_flash.h"
24 #include "esp_partition.h"
25 #include "esp_flash_encrypt.h"
26 #include "esp_log.h"
27 #include "esp_rom_md5.h"
28 #include "bootloader_common.h"
29 #include "bootloader_util.h"
30 #include "esp_ota_ops.h"
31
32 #define HASH_LEN 32 /* SHA-256 digest length */
33
34 #ifndef NDEBUG
35 // Enable built-in checks in queue.h in debug builds
36 #define INVARIANTS
37 #endif
38 #include "sys/queue.h"
39
40 typedef struct partition_list_item_ {
41 esp_partition_t info;
42 bool user_registered;
43 SLIST_ENTRY(partition_list_item_) next;
44 } partition_list_item_t;
45
46 typedef struct esp_partition_iterator_opaque_ {
47 esp_partition_type_t type; // requested type
48 esp_partition_subtype_t subtype; // requested subtype
49 const char* label; // requested label (can be NULL)
50 partition_list_item_t* next_item; // next item to iterate to
51 esp_partition_t* info; // pointer to info (it is redundant, but makes code more readable)
52 } esp_partition_iterator_opaque_t;
53
54
55 static esp_partition_iterator_opaque_t* iterator_create(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label);
56 static esp_err_t load_partitions(void);
57 static esp_err_t ensure_partitions_loaded(void);
58
59
60 static const char* TAG = "partition";
61 static SLIST_HEAD(partition_list_head_, partition_list_item_) s_partition_list =
62 SLIST_HEAD_INITIALIZER(s_partition_list);
63 static _lock_t s_partition_list_lock;
64
65
ensure_partitions_loaded(void)66 static esp_err_t ensure_partitions_loaded(void)
67 {
68 esp_err_t err = ESP_OK;
69 if (SLIST_EMPTY(&s_partition_list)) {
70 // only lock if list is empty (and check again after acquiring lock)
71 _lock_acquire(&s_partition_list_lock);
72 if (SLIST_EMPTY(&s_partition_list)) {
73 ESP_LOGD(TAG, "Loading the partition table");
74 err = load_partitions();
75 if (err != ESP_OK) {
76 ESP_LOGE(TAG, "load_partitions returned 0x%x", err);
77 }
78 }
79 _lock_release(&s_partition_list_lock);
80 }
81 return err;
82 }
83
esp_partition_find(esp_partition_type_t type,esp_partition_subtype_t subtype,const char * label)84 esp_partition_iterator_t esp_partition_find(esp_partition_type_t type,
85 esp_partition_subtype_t subtype, const char* label)
86 {
87 if (ensure_partitions_loaded() != ESP_OK) {
88 return NULL;
89 }
90 // Searching for a specific subtype without specifying the type doesn't make
91 // sense, and is likely a usage error.
92 if (type == ESP_PARTITION_TYPE_ANY && subtype != ESP_PARTITION_SUBTYPE_ANY) {
93 return NULL;
94 }
95 // create an iterator pointing to the start of the list
96 // (next item will be the first one)
97 esp_partition_iterator_t it = iterator_create(type, subtype, label);
98 // advance iterator to the next item which matches constraints
99 it = esp_partition_next(it);
100 // if nothing found, it == NULL and iterator has been released
101 return it;
102 }
103
esp_partition_next(esp_partition_iterator_t it)104 esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t it)
105 {
106 assert(it);
107 // iterator reached the end of linked list?
108 if (it->next_item == NULL) {
109 esp_partition_iterator_release(it);
110 return NULL;
111 }
112 _lock_acquire(&s_partition_list_lock);
113 for (; it->next_item != NULL; it->next_item = SLIST_NEXT(it->next_item, next)) {
114 esp_partition_t* p = &it->next_item->info;
115 if (it->type != ESP_PARTITION_TYPE_ANY && it->type != p->type) {
116 continue;
117 }
118 if (it->subtype != ESP_PARTITION_SUBTYPE_ANY && it->subtype != p->subtype) {
119 continue;
120 }
121 if (it->label != NULL && strcmp(it->label, p->label) != 0) {
122 continue;
123 }
124 // all constraints match, bail out
125 break;
126 }
127 _lock_release(&s_partition_list_lock);
128 if (it->next_item == NULL) {
129 esp_partition_iterator_release(it);
130 return NULL;
131 }
132 it->info = &it->next_item->info;
133 it->next_item = SLIST_NEXT(it->next_item, next);
134 return it;
135 }
136
esp_partition_find_first(esp_partition_type_t type,esp_partition_subtype_t subtype,const char * label)137 const esp_partition_t* esp_partition_find_first(esp_partition_type_t type,
138 esp_partition_subtype_t subtype, const char* label)
139 {
140 esp_partition_iterator_t it = esp_partition_find(type, subtype, label);
141 if (it == NULL) {
142 return NULL;
143 }
144 const esp_partition_t* res = esp_partition_get(it);
145 esp_partition_iterator_release(it);
146 return res;
147 }
148
iterator_create(esp_partition_type_t type,esp_partition_subtype_t subtype,const char * label)149 static esp_partition_iterator_opaque_t* iterator_create(esp_partition_type_t type,
150 esp_partition_subtype_t subtype, const char* label)
151 {
152 esp_partition_iterator_opaque_t* it =
153 (esp_partition_iterator_opaque_t*) malloc(sizeof(esp_partition_iterator_opaque_t));
154 it->type = type;
155 it->subtype = subtype;
156 it->label = label;
157 it->next_item = SLIST_FIRST(&s_partition_list);
158 it->info = NULL;
159 return it;
160 }
161
162 // Create linked list of partition_list_item_t structures.
163 // This function is called only once, with s_partition_list_lock taken.
load_partitions(void)164 static esp_err_t load_partitions(void)
165 {
166 const uint8_t *p_start;
167 const uint8_t *p_end;
168 spi_flash_mmap_handle_t handle;
169
170 // Temporary list of loaded partitions, if valid then we copy this to s_partition_list
171 typeof(s_partition_list) new_partitions_list = SLIST_HEAD_INITIALIZER(s_partition_list);
172 partition_list_item_t* last = NULL;
173
174 #if CONFIG_PARTITION_TABLE_MD5
175 const uint8_t *md5_part = NULL;
176 const uint8_t *stored_md5;
177 uint8_t calc_md5[ESP_ROM_MD5_DIGEST_LEN];
178 md5_context_t context;
179
180 esp_rom_md5_init(&context);
181 #endif
182
183 // map 64kB block where partition table is located
184 esp_err_t err = spi_flash_mmap(ESP_PARTITION_TABLE_OFFSET & 0xffff0000,
185 SPI_FLASH_SEC_SIZE, SPI_FLASH_MMAP_DATA, (const void **)&p_start, &handle);
186 if (err != ESP_OK) {
187 return err;
188 }
189 // calculate partition address within mmap-ed region
190 p_start += (ESP_PARTITION_TABLE_OFFSET & 0xffff);
191 p_end = p_start + SPI_FLASH_SEC_SIZE;
192
193 for(const uint8_t *p_entry = p_start; p_entry < p_end; p_entry += sizeof(esp_partition_info_t)) {
194 esp_partition_info_t entry;
195 // copying to RAM instead of using pointer to flash to avoid any chance of TOCTOU due to cache miss
196 // when flash encryption is used
197 memcpy(&entry, p_entry, sizeof(entry));
198
199 #if CONFIG_PARTITION_TABLE_MD5
200 if (entry.magic == ESP_PARTITION_MAGIC_MD5) {
201 md5_part = p_entry;
202 break;
203 }
204 #endif
205 if (entry.magic != ESP_PARTITION_MAGIC) {
206 break;
207 }
208
209 #if CONFIG_PARTITION_TABLE_MD5
210 esp_rom_md5_update(&context, &entry, sizeof(entry));
211 #endif
212
213 // allocate new linked list item and populate it with data from partition table
214 partition_list_item_t* item = (partition_list_item_t*) calloc(sizeof(partition_list_item_t), 1);
215 if (item == NULL) {
216 err = ESP_ERR_NO_MEM;
217 break;
218 }
219 item->info.flash_chip = esp_flash_default_chip;
220 item->info.address = entry.pos.offset;
221 item->info.size = entry.pos.size;
222 item->info.type = entry.type;
223 item->info.subtype = entry.subtype;
224 item->info.encrypted = entry.flags & PART_FLAG_ENCRYPTED;
225 item->user_registered = false;
226
227 if (!esp_flash_encryption_enabled()) {
228 /* If flash encryption is not turned on, no partitions should be treated as encrypted */
229 item->info.encrypted = false;
230 } else if (entry.type == ESP_PARTITION_TYPE_APP
231 || (entry.type == ESP_PARTITION_TYPE_DATA && entry.subtype == ESP_PARTITION_SUBTYPE_DATA_OTA)
232 || (entry.type == ESP_PARTITION_TYPE_DATA && entry.subtype == ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS)) {
233 /* If encryption is turned on, all app partitions and OTA data
234 are always encrypted */
235 item->info.encrypted = true;
236 }
237
238 #if CONFIG_NVS_COMPATIBLE_PRE_V4_3_ENCRYPTION_FLAG
239 if (entry.type == ESP_PARTITION_TYPE_DATA &&
240 entry.subtype == ESP_PARTITION_SUBTYPE_DATA_NVS &&
241 (entry.flags & PART_FLAG_ENCRYPTED)) {
242 ESP_LOGI(TAG, "Ignoring encrypted flag for \"%s\" partition", entry.label);
243 item->info.encrypted = false;
244 }
245 #endif
246
247 // item->info.label is initialized by calloc, so resulting string will be null terminated
248 strncpy(item->info.label, (const char*) entry.label, sizeof(item->info.label) - 1);
249
250 // add it to the list
251 if (last == NULL) {
252 SLIST_INSERT_HEAD(&new_partitions_list, item, next);
253 } else {
254 SLIST_INSERT_AFTER(last, item, next);
255 }
256 last = item;
257 }
258
259 #if CONFIG_PARTITION_TABLE_MD5
260 if (md5_part == NULL) {
261 ESP_LOGE(TAG, "No MD5 found in partition table");
262 err = ESP_ERR_NOT_FOUND;
263 } else {
264 stored_md5 = md5_part + ESP_PARTITION_MD5_OFFSET;
265
266 esp_rom_md5_final(calc_md5, &context);
267
268 ESP_LOG_BUFFER_HEXDUMP("calculated md5", calc_md5, ESP_ROM_MD5_DIGEST_LEN, ESP_LOG_VERBOSE);
269 ESP_LOG_BUFFER_HEXDUMP("stored md5", stored_md5, ESP_ROM_MD5_DIGEST_LEN, ESP_LOG_VERBOSE);
270
271 if (memcmp(calc_md5, stored_md5, ESP_ROM_MD5_DIGEST_LEN) != 0) {
272 ESP_LOGE(TAG, "Partition table MD5 mismatch");
273 err = ESP_ERR_INVALID_STATE;
274 } else {
275 ESP_LOGD(TAG, "Partition table MD5 verified");
276 }
277 }
278 #endif
279
280 if (err == ESP_OK) {
281 /* Don't copy the list to the static variable unless it's verified */
282 s_partition_list = new_partitions_list;
283 } else {
284 /* Otherwise, free all the memory we just allocated */
285 partition_list_item_t *it = new_partitions_list.slh_first;
286 while (it) {
287 partition_list_item_t *next = it->next.sle_next;
288 free(it);
289 it = next;
290 }
291 }
292
293 spi_flash_munmap(handle);
294 return err;
295 }
296
esp_partition_iterator_release(esp_partition_iterator_t iterator)297 void esp_partition_iterator_release(esp_partition_iterator_t iterator)
298 {
299 // iterator == NULL is okay
300 free(iterator);
301 }
302
esp_partition_get(esp_partition_iterator_t iterator)303 const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator)
304 {
305 assert(iterator != NULL);
306 return iterator->info;
307 }
308
esp_partition_register_external(esp_flash_t * flash_chip,size_t offset,size_t size,const char * label,esp_partition_type_t type,esp_partition_subtype_t subtype,const esp_partition_t ** out_partition)309 esp_err_t esp_partition_register_external(esp_flash_t* flash_chip, size_t offset, size_t size,
310 const char* label, esp_partition_type_t type, esp_partition_subtype_t subtype,
311 const esp_partition_t** out_partition)
312 {
313 if (out_partition != NULL) {
314 *out_partition = NULL;
315 }
316 #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
317 return ESP_ERR_NOT_SUPPORTED;
318 #endif
319
320 if (offset + size > flash_chip->size) {
321 return ESP_ERR_INVALID_SIZE;
322 }
323
324 esp_err_t err = ensure_partitions_loaded();
325 if (err != ESP_OK) {
326 return err;
327 }
328
329 partition_list_item_t* item = (partition_list_item_t*) calloc(sizeof(partition_list_item_t), 1);
330 if (item == NULL) {
331 return ESP_ERR_NO_MEM;
332 }
333 item->info.flash_chip = flash_chip;
334 item->info.address = offset;
335 item->info.size = size;
336 item->info.type = type;
337 item->info.subtype = subtype;
338 item->info.encrypted = false;
339 item->user_registered = true;
340 strlcpy(item->info.label, label, sizeof(item->info.label));
341
342 _lock_acquire(&s_partition_list_lock);
343 partition_list_item_t *it, *last = NULL;
344 SLIST_FOREACH(it, &s_partition_list, next) {
345 /* Check if the new partition overlaps an existing one */
346 if (it->info.flash_chip == flash_chip &&
347 bootloader_util_regions_overlap(offset, offset + size,
348 it->info.address, it->info.address + it->info.size)) {
349 _lock_release(&s_partition_list_lock);
350 free(item);
351 return ESP_ERR_INVALID_ARG;
352 }
353 last = it;
354 }
355 if (last == NULL) {
356 SLIST_INSERT_HEAD(&s_partition_list, item, next);
357 } else {
358 SLIST_INSERT_AFTER(last, item, next);
359 }
360 _lock_release(&s_partition_list_lock);
361 if (out_partition != NULL) {
362 *out_partition = &item->info;
363 }
364 return ESP_OK;
365 }
366
esp_partition_deregister_external(const esp_partition_t * partition)367 esp_err_t esp_partition_deregister_external(const esp_partition_t* partition)
368 {
369 esp_err_t result = ESP_ERR_NOT_FOUND;
370 _lock_acquire(&s_partition_list_lock);
371 partition_list_item_t *it;
372 SLIST_FOREACH(it, &s_partition_list, next) {
373 if (&it->info == partition) {
374 if (!it->user_registered) {
375 result = ESP_ERR_INVALID_ARG;
376 break;
377 }
378 SLIST_REMOVE(&s_partition_list, it, partition_list_item_, next);
379 free(it);
380 result = ESP_OK;
381 break;
382 }
383 }
384 _lock_release(&s_partition_list_lock);
385 return result;
386 }
387
esp_partition_verify(const esp_partition_t * partition)388 const esp_partition_t *esp_partition_verify(const esp_partition_t *partition)
389 {
390 assert(partition != NULL);
391 const char *label = (strlen(partition->label) > 0) ? partition->label : NULL;
392 esp_partition_iterator_t it = esp_partition_find(partition->type,
393 partition->subtype,
394 label);
395 while (it != NULL) {
396 const esp_partition_t *p = esp_partition_get(it);
397 /* Can't memcmp() whole structure here as padding contents may be different */
398 if (p->flash_chip == partition->flash_chip
399 && p->address == partition->address
400 && partition->size == p->size
401 && partition->encrypted == p->encrypted) {
402 esp_partition_iterator_release(it);
403 return p;
404 }
405 it = esp_partition_next(it);
406 }
407 esp_partition_iterator_release(it);
408 return NULL;
409 }
410
esp_partition_read(const esp_partition_t * partition,size_t src_offset,void * dst,size_t size)411 esp_err_t esp_partition_read(const esp_partition_t* partition,
412 size_t src_offset, void* dst, size_t size)
413 {
414 assert(partition != NULL);
415 if (src_offset > partition->size) {
416 return ESP_ERR_INVALID_ARG;
417 }
418 if (src_offset + size > partition->size) {
419 return ESP_ERR_INVALID_SIZE;
420 }
421
422 if (!partition->encrypted) {
423 #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
424 return esp_flash_read(partition->flash_chip, dst, partition->address + src_offset, size);
425 #else
426 return spi_flash_read(partition->address + src_offset, dst, size);
427 #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
428 } else {
429 #if CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
430 if (partition->flash_chip != esp_flash_default_chip) {
431 return ESP_ERR_NOT_SUPPORTED;
432 }
433
434 /* Encrypted partitions need to be read via a cache mapping */
435 const void *buf;
436 spi_flash_mmap_handle_t handle;
437 esp_err_t err;
438
439 err = esp_partition_mmap(partition, src_offset, size,
440 SPI_FLASH_MMAP_DATA, &buf, &handle);
441 if (err != ESP_OK) {
442 return err;
443 }
444 memcpy(dst, buf, size);
445 spi_flash_munmap(handle);
446 return ESP_OK;
447 #else
448 return ESP_ERR_NOT_SUPPORTED;
449 #endif // CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
450 }
451 }
452
esp_partition_write(const esp_partition_t * partition,size_t dst_offset,const void * src,size_t size)453 esp_err_t esp_partition_write(const esp_partition_t* partition,
454 size_t dst_offset, const void* src, size_t size)
455 {
456 assert(partition != NULL);
457 if (dst_offset > partition->size) {
458 return ESP_ERR_INVALID_ARG;
459 }
460 if (dst_offset + size > partition->size) {
461 return ESP_ERR_INVALID_SIZE;
462 }
463 dst_offset = partition->address + dst_offset;
464 if (!partition->encrypted) {
465 #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
466 return esp_flash_write(partition->flash_chip, src, dst_offset, size);
467 #else
468 return spi_flash_write(dst_offset, src, size);
469 #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
470 } else {
471 #if CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
472 if (partition->flash_chip != esp_flash_default_chip) {
473 return ESP_ERR_NOT_SUPPORTED;
474 }
475 #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
476 return esp_flash_write_encrypted(partition->flash_chip, dst_offset, src, size);
477 #else
478 return spi_flash_write_encrypted(dst_offset, src, size);
479 #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
480 #else
481 return ESP_ERR_NOT_SUPPORTED;
482 #endif // CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
483 }
484 }
485
esp_partition_read_raw(const esp_partition_t * partition,size_t src_offset,void * dst,size_t size)486 esp_err_t esp_partition_read_raw(const esp_partition_t* partition,
487 size_t src_offset, void* dst, size_t size)
488 {
489 assert(partition != NULL);
490 if (src_offset > partition->size) {
491 return ESP_ERR_INVALID_ARG;
492 }
493 if (src_offset + size > partition->size) {
494 return ESP_ERR_INVALID_SIZE;
495 }
496
497 #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
498 return esp_flash_read(partition->flash_chip, dst, partition->address + src_offset, size);
499 #else
500 return spi_flash_read(partition->address + src_offset, dst, size);
501 #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
502 }
503
esp_partition_write_raw(const esp_partition_t * partition,size_t dst_offset,const void * src,size_t size)504 esp_err_t esp_partition_write_raw(const esp_partition_t* partition,
505 size_t dst_offset, const void* src, size_t size)
506 {
507 assert(partition != NULL);
508 if (dst_offset > partition->size) {
509 return ESP_ERR_INVALID_ARG;
510 }
511 if (dst_offset + size > partition->size) {
512 return ESP_ERR_INVALID_SIZE;
513 }
514 dst_offset = partition->address + dst_offset;
515
516 #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
517 return esp_flash_write(partition->flash_chip, src, dst_offset, size);
518 #else
519 return spi_flash_write(dst_offset, src, size);
520 #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
521 }
522
esp_partition_erase_range(const esp_partition_t * partition,size_t offset,size_t size)523 esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
524 size_t offset, size_t size)
525 {
526 assert(partition != NULL);
527 if (offset > partition->size) {
528 return ESP_ERR_INVALID_ARG;
529 }
530 if (offset + size > partition->size) {
531 return ESP_ERR_INVALID_SIZE;
532 }
533 if (size % SPI_FLASH_SEC_SIZE != 0) {
534 return ESP_ERR_INVALID_SIZE;
535 }
536 if (offset % SPI_FLASH_SEC_SIZE != 0) {
537 return ESP_ERR_INVALID_ARG;
538 }
539 #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
540 return esp_flash_erase_region(partition->flash_chip, partition->address + offset, size);
541 #else
542 return spi_flash_erase_range(partition->address + offset, size);
543 #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
544 }
545
546 /*
547 * Note: current implementation ignores the possibility of multiple regions in the same partition being
548 * mapped. Reference counting and address space re-use is delegated to spi_flash_mmap.
549 *
550 * If this becomes a performance issue (i.e. if we need to map multiple regions within the partition),
551 * we can add esp_partition_mmapv which will accept an array of offsets and sizes, and return array of
552 * mmaped pointers, and a single handle for all these regions.
553 */
esp_partition_mmap(const esp_partition_t * partition,size_t offset,size_t size,spi_flash_mmap_memory_t memory,const void ** out_ptr,spi_flash_mmap_handle_t * out_handle)554 esp_err_t esp_partition_mmap(const esp_partition_t* partition, size_t offset, size_t size,
555 spi_flash_mmap_memory_t memory,
556 const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
557 {
558 assert(partition != NULL);
559 if (offset > partition->size) {
560 return ESP_ERR_INVALID_ARG;
561 }
562 if (offset + size > partition->size) {
563 return ESP_ERR_INVALID_SIZE;
564 }
565 if (partition->flash_chip != esp_flash_default_chip) {
566 return ESP_ERR_NOT_SUPPORTED;
567 }
568 size_t phys_addr = partition->address + offset;
569 // offset within 64kB block
570 size_t region_offset = phys_addr & 0xffff;
571 size_t mmap_addr = phys_addr & 0xffff0000;
572 esp_err_t rc = spi_flash_mmap(mmap_addr, size+region_offset, memory, out_ptr, out_handle);
573 // adjust returned pointer to point to the correct offset
574 if (rc == ESP_OK) {
575 *out_ptr = (void*) (((ptrdiff_t) *out_ptr) + region_offset);
576 }
577 return rc;
578 }
579
esp_partition_get_sha256(const esp_partition_t * partition,uint8_t * sha_256)580 esp_err_t esp_partition_get_sha256(const esp_partition_t *partition, uint8_t *sha_256)
581 {
582 return bootloader_common_get_sha256_of_partition(partition->address, partition->size, partition->type, sha_256);
583 }
584
esp_partition_check_identity(const esp_partition_t * partition_1,const esp_partition_t * partition_2)585 bool esp_partition_check_identity(const esp_partition_t *partition_1, const esp_partition_t *partition_2)
586 {
587 uint8_t sha_256[2][HASH_LEN] = { 0 };
588
589 if (esp_partition_get_sha256(partition_1, sha_256[0]) == ESP_OK &&
590 esp_partition_get_sha256(partition_2, sha_256[1]) == ESP_OK) {
591
592 if (memcmp(sha_256[0], sha_256[1], HASH_LEN) == 0) {
593 // The partitions are identity
594 return true;
595 }
596 }
597 return false;
598 }
599
esp_partition_main_flash_region_safe(size_t addr,size_t size)600 bool esp_partition_main_flash_region_safe(size_t addr, size_t size)
601 {
602 bool result = true;
603 if (addr <= ESP_PARTITION_TABLE_OFFSET + ESP_PARTITION_TABLE_MAX_LEN) {
604 return false;
605 }
606 const esp_partition_t *p = esp_ota_get_running_partition();
607 if (addr >= p->address && addr < p->address + p->size) {
608 return false;
609 }
610 if (addr < p->address && addr + size > p->address) {
611 return false;
612 }
613 return result;
614 }
615