• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = 0;
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     // create an iterator pointing to the start of the list
91     // (next item will be the first one)
92     esp_partition_iterator_t it = iterator_create(type, subtype, label);
93     // advance iterator to the next item which matches constraints
94     it = esp_partition_next(it);
95     // if nothing found, it == NULL and iterator has been released
96     return it;
97 }
98 
esp_partition_next(esp_partition_iterator_t it)99 esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t it)
100 {
101     assert(it);
102     // iterator reached the end of linked list?
103     if (it->next_item == NULL) {
104         esp_partition_iterator_release(it);
105         return NULL;
106     }
107     _lock_acquire(&s_partition_list_lock);
108     for (; it->next_item != NULL; it->next_item = SLIST_NEXT(it->next_item, next)) {
109         esp_partition_t* p = &it->next_item->info;
110         if (it->type != p->type) {
111             continue;
112         }
113         if (it->subtype != 0xff && it->subtype != p->subtype) {
114             continue;
115         }
116         if (it->label != NULL && strcmp(it->label, p->label) != 0) {
117             continue;
118         }
119         // all constraints match, bail out
120         break;
121     }
122     _lock_release(&s_partition_list_lock);
123     if (it->next_item == NULL) {
124         esp_partition_iterator_release(it);
125         return NULL;
126     }
127     it->info = &it->next_item->info;
128     it->next_item = SLIST_NEXT(it->next_item, next);
129     return it;
130 }
131 
esp_partition_find_first(esp_partition_type_t type,esp_partition_subtype_t subtype,const char * label)132 const esp_partition_t* esp_partition_find_first(esp_partition_type_t type,
133         esp_partition_subtype_t subtype, const char* label)
134 {
135     esp_partition_iterator_t it = esp_partition_find(type, subtype, label);
136     if (it == NULL) {
137         return NULL;
138     }
139     const esp_partition_t* res = esp_partition_get(it);
140     esp_partition_iterator_release(it);
141     return res;
142 }
143 
iterator_create(esp_partition_type_t type,esp_partition_subtype_t subtype,const char * label)144 static esp_partition_iterator_opaque_t* iterator_create(esp_partition_type_t type,
145         esp_partition_subtype_t subtype, const char* label)
146 {
147     esp_partition_iterator_opaque_t* it =
148             (esp_partition_iterator_opaque_t*) malloc(sizeof(esp_partition_iterator_opaque_t));
149     it->type = type;
150     it->subtype = subtype;
151     it->label = label;
152     it->next_item = SLIST_FIRST(&s_partition_list);
153     it->info = NULL;
154     return it;
155 }
156 
157 // Create linked list of partition_list_item_t structures.
158 // This function is called only once, with s_partition_list_lock taken.
load_partitions(void)159 static esp_err_t load_partitions(void)
160 {
161     const uint8_t *p_start;
162     const uint8_t *p_end;
163     spi_flash_mmap_handle_t handle;
164 
165     // Temporary list of loaded partitions, if valid then we copy this to s_partition_list
166     typeof(s_partition_list) new_partitions_list = SLIST_HEAD_INITIALIZER(s_partition_list);
167     partition_list_item_t* last = NULL;
168 
169 #if CONFIG_PARTITION_TABLE_MD5
170     const uint8_t *md5_part = NULL;
171     const uint8_t *stored_md5;
172     uint8_t calc_md5[ESP_ROM_MD5_DIGEST_LEN];
173     md5_context_t context;
174 
175     esp_rom_md5_init(&context);
176 #endif
177 
178     // map 64kB block where partition table is located
179     esp_err_t err = spi_flash_mmap(ESP_PARTITION_TABLE_OFFSET & 0xffff0000,
180                                    SPI_FLASH_SEC_SIZE, SPI_FLASH_MMAP_DATA, (const void **)&p_start, &handle);
181     if (err != ESP_OK) {
182         return err;
183     }
184     // calculate partition address within mmap-ed region
185     p_start += (ESP_PARTITION_TABLE_OFFSET & 0xffff);
186     p_end = p_start + SPI_FLASH_SEC_SIZE;
187 
188     for(const uint8_t *p_entry = p_start; p_entry < p_end; p_entry += sizeof(esp_partition_info_t)) {
189         esp_partition_info_t entry;
190         // copying to RAM instead of using pointer to flash to avoid any chance of TOCTOU due to cache miss
191         // when flash encryption is used
192         memcpy(&entry, p_entry, sizeof(entry));
193 
194 #if CONFIG_PARTITION_TABLE_MD5
195         if (entry.magic == ESP_PARTITION_MAGIC_MD5) {
196             md5_part = p_entry;
197             break;
198         }
199 #endif
200         if (entry.magic != ESP_PARTITION_MAGIC) {
201             break;
202         }
203 
204 #if CONFIG_PARTITION_TABLE_MD5
205         esp_rom_md5_update(&context, &entry, sizeof(entry));
206 #endif
207 
208         // allocate new linked list item and populate it with data from partition table
209         partition_list_item_t* item = (partition_list_item_t*) calloc(sizeof(partition_list_item_t), 1);
210         if (item == NULL) {
211             err = ESP_ERR_NO_MEM;
212             break;
213         }
214         item->info.flash_chip = esp_flash_default_chip;
215         item->info.address = entry.pos.offset;
216         item->info.size = entry.pos.size;
217         item->info.type = entry.type;
218         item->info.subtype = entry.subtype;
219         item->info.encrypted = entry.flags & PART_FLAG_ENCRYPTED;
220         item->user_registered = false;
221 
222         if (!esp_flash_encryption_enabled()) {
223             /* If flash encryption is not turned on, no partitions should be treated as encrypted */
224             item->info.encrypted = false;
225         } else if (entry.type == PART_TYPE_APP
226                 || (entry.type == PART_TYPE_DATA && entry.subtype == PART_SUBTYPE_DATA_OTA)
227                 || (entry.type == PART_TYPE_DATA && entry.subtype == PART_SUBTYPE_DATA_NVS_KEYS)) {
228             /* If encryption is turned on, all app partitions and OTA data
229                are always encrypted */
230             item->info.encrypted = true;
231         }
232 
233         // item->info.label is initialized by calloc, so resulting string will be null terminated
234         strncpy(item->info.label, (const char*) entry.label, sizeof(item->info.label) - 1);
235 
236         // add it to the list
237         if (last == NULL) {
238             SLIST_INSERT_HEAD(&new_partitions_list, item, next);
239         } else {
240             SLIST_INSERT_AFTER(last, item, next);
241         }
242         last = item;
243     }
244 
245 #if CONFIG_PARTITION_TABLE_MD5
246     if (md5_part == NULL) {
247         ESP_LOGE(TAG, "No MD5 found in partition table");
248         err = ESP_ERR_NOT_FOUND;
249     } else {
250         stored_md5 = md5_part + ESP_PARTITION_MD5_OFFSET;
251 
252         esp_rom_md5_final(calc_md5, &context);
253 
254         ESP_LOG_BUFFER_HEXDUMP("calculated md5", calc_md5, ESP_ROM_MD5_DIGEST_LEN, ESP_LOG_VERBOSE);
255         ESP_LOG_BUFFER_HEXDUMP("stored md5", stored_md5, ESP_ROM_MD5_DIGEST_LEN, ESP_LOG_VERBOSE);
256 
257         if (memcmp(calc_md5, stored_md5, ESP_ROM_MD5_DIGEST_LEN) != 0) {
258             ESP_LOGE(TAG, "Partition table MD5 mismatch");
259             err = ESP_ERR_INVALID_STATE;
260         } else {
261             ESP_LOGD(TAG, "Partition table MD5 verified");
262         }
263     }
264 #endif
265 
266     if (err == ESP_OK) {
267         /* Don't copy the list to the static variable unless it's verified */
268         s_partition_list = new_partitions_list;
269     } else {
270         /* Otherwise, free all the memory we just allocated */
271         partition_list_item_t *it = new_partitions_list.slh_first;
272         while (it) {
273             partition_list_item_t *next = it->next.sle_next;
274             free(it);
275             it = next;
276         }
277     }
278 
279     spi_flash_munmap(handle);
280     return err;
281 }
282 
esp_partition_iterator_release(esp_partition_iterator_t iterator)283 void esp_partition_iterator_release(esp_partition_iterator_t iterator)
284 {
285     // iterator == NULL is okay
286     free(iterator);
287 }
288 
esp_partition_get(esp_partition_iterator_t iterator)289 const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator)
290 {
291     assert(iterator != NULL);
292     return iterator->info;
293 }
294 
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)295 esp_err_t esp_partition_register_external(esp_flash_t* flash_chip, size_t offset, size_t size,
296         const char* label, esp_partition_type_t type, esp_partition_subtype_t subtype,
297         const esp_partition_t** out_partition)
298 {
299     if (out_partition != NULL) {
300         *out_partition = NULL;
301     }
302 #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
303     return ESP_ERR_NOT_SUPPORTED;
304 #endif
305 
306     if (offset + size > flash_chip->size) {
307         return ESP_ERR_INVALID_SIZE;
308     }
309 
310     esp_err_t err = ensure_partitions_loaded();
311     if (err != ESP_OK) {
312         return err;
313     }
314 
315     partition_list_item_t* item = (partition_list_item_t*) calloc(sizeof(partition_list_item_t), 1);
316     if (item == NULL) {
317         return ESP_ERR_NO_MEM;
318     }
319     item->info.flash_chip = flash_chip;
320     item->info.address = offset;
321     item->info.size = size;
322     item->info.type = type;
323     item->info.subtype = subtype;
324     item->info.encrypted = false;
325     item->user_registered = true;
326     strlcpy(item->info.label, label, sizeof(item->info.label));
327 
328     _lock_acquire(&s_partition_list_lock);
329     partition_list_item_t *it, *last = NULL;
330     SLIST_FOREACH(it, &s_partition_list, next) {
331         /* Check if the new partition overlaps an existing one */
332         if (it->info.flash_chip == flash_chip &&
333                 bootloader_util_regions_overlap(offset, offset + size,
334                                                 it->info.address, it->info.address + it->info.size)) {
335             _lock_release(&s_partition_list_lock);
336             free(item);
337             return ESP_ERR_INVALID_ARG;
338         }
339         last = it;
340     }
341     if (last == NULL) {
342         SLIST_INSERT_HEAD(&s_partition_list, item, next);
343     } else {
344         SLIST_INSERT_AFTER(last, item, next);
345     }
346     _lock_release(&s_partition_list_lock);
347     if (out_partition != NULL) {
348         *out_partition = &item->info;
349     }
350     return ESP_OK;
351 }
352 
esp_partition_deregister_external(const esp_partition_t * partition)353 esp_err_t esp_partition_deregister_external(const esp_partition_t* partition)
354 {
355     esp_err_t result = ESP_ERR_NOT_FOUND;
356     _lock_acquire(&s_partition_list_lock);
357     partition_list_item_t *it;
358     SLIST_FOREACH(it, &s_partition_list, next) {
359         if (&it->info == partition) {
360             if (!it->user_registered) {
361                 result = ESP_ERR_INVALID_ARG;
362                 break;
363             }
364             SLIST_REMOVE(&s_partition_list, it, partition_list_item_, next);
365             free(it);
366             result = ESP_OK;
367             break;
368         }
369     }
370     _lock_release(&s_partition_list_lock);
371     return result;
372 }
373 
esp_partition_verify(const esp_partition_t * partition)374 const esp_partition_t *esp_partition_verify(const esp_partition_t *partition)
375 {
376     assert(partition != NULL);
377     const char *label = (strlen(partition->label) > 0) ? partition->label : NULL;
378     esp_partition_iterator_t it = esp_partition_find(partition->type,
379                                                      partition->subtype,
380                                                      label);
381     while (it != NULL) {
382         const esp_partition_t *p = esp_partition_get(it);
383         /* Can't memcmp() whole structure here as padding contents may be different */
384         if (p->flash_chip == partition->flash_chip
385             && p->address == partition->address
386             && partition->size == p->size
387             && partition->encrypted == p->encrypted) {
388             esp_partition_iterator_release(it);
389             return p;
390         }
391         it = esp_partition_next(it);
392     }
393     esp_partition_iterator_release(it);
394     return NULL;
395 }
396 
esp_partition_read(const esp_partition_t * partition,size_t src_offset,void * dst,size_t size)397 esp_err_t esp_partition_read(const esp_partition_t* partition,
398         size_t src_offset, void* dst, size_t size)
399 {
400     assert(partition != NULL);
401     if (src_offset > partition->size) {
402         return ESP_ERR_INVALID_ARG;
403     }
404     if (src_offset + size > partition->size) {
405         return ESP_ERR_INVALID_SIZE;
406     }
407 
408     if (!partition->encrypted) {
409 #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
410         return esp_flash_read(partition->flash_chip, dst, partition->address + src_offset, size);
411 #else
412         return spi_flash_read(partition->address + src_offset, dst, size);
413 #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
414     } else {
415 #if CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
416         if (partition->flash_chip != esp_flash_default_chip) {
417             return ESP_ERR_NOT_SUPPORTED;
418         }
419 
420         /* Encrypted partitions need to be read via a cache mapping */
421         const void *buf;
422         spi_flash_mmap_handle_t handle;
423         esp_err_t err;
424 
425         err = esp_partition_mmap(partition, src_offset, size,
426                                  SPI_FLASH_MMAP_DATA, &buf, &handle);
427         if (err != ESP_OK) {
428             return err;
429         }
430         memcpy(dst, buf, size);
431         spi_flash_munmap(handle);
432         return ESP_OK;
433 #else
434         return ESP_ERR_NOT_SUPPORTED;
435 #endif // CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
436     }
437 }
438 
esp_partition_write(const esp_partition_t * partition,size_t dst_offset,const void * src,size_t size)439 esp_err_t esp_partition_write(const esp_partition_t* partition,
440                              size_t dst_offset, const void* src, size_t size)
441 {
442     assert(partition != NULL);
443     if (dst_offset > partition->size) {
444         return ESP_ERR_INVALID_ARG;
445     }
446     if (dst_offset + size > partition->size) {
447         return ESP_ERR_INVALID_SIZE;
448     }
449     dst_offset = partition->address + dst_offset;
450     if (!partition->encrypted) {
451 #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
452         return esp_flash_write(partition->flash_chip, src, dst_offset, size);
453 #else
454         return spi_flash_write(dst_offset, src, size);
455 #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
456     } else {
457 #if CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
458         if (partition->flash_chip != esp_flash_default_chip) {
459             return ESP_ERR_NOT_SUPPORTED;
460         }
461         return spi_flash_write_encrypted(dst_offset, src, size);
462 #else
463         return ESP_ERR_NOT_SUPPORTED;
464 #endif // CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
465     }
466 }
467 
esp_partition_read_raw(const esp_partition_t * partition,size_t src_offset,void * dst,size_t size)468 esp_err_t esp_partition_read_raw(const esp_partition_t* partition,
469         size_t src_offset, void* dst, size_t size)
470 {
471     assert(partition != NULL);
472     if (src_offset > partition->size) {
473         return ESP_ERR_INVALID_ARG;
474     }
475     if (src_offset + size > partition->size) {
476         return ESP_ERR_INVALID_SIZE;
477     }
478 
479 #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
480     return esp_flash_read(partition->flash_chip, dst, partition->address + src_offset, size);
481 #else
482     return spi_flash_read(partition->address + src_offset, dst, size);
483 #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
484 }
485 
esp_partition_write_raw(const esp_partition_t * partition,size_t dst_offset,const void * src,size_t size)486 esp_err_t esp_partition_write_raw(const esp_partition_t* partition,
487                              size_t dst_offset, const void* src, size_t size)
488 {
489     assert(partition != NULL);
490     if (dst_offset > partition->size) {
491         return ESP_ERR_INVALID_ARG;
492     }
493     if (dst_offset + size > partition->size) {
494         return ESP_ERR_INVALID_SIZE;
495     }
496     dst_offset = partition->address + dst_offset;
497 
498 #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
499     return esp_flash_write(partition->flash_chip, src, dst_offset, size);
500 #else
501     return spi_flash_write(dst_offset, src, size);
502 #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
503 }
504 
esp_partition_erase_range(const esp_partition_t * partition,size_t offset,size_t size)505 esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
506                                     size_t offset, size_t size)
507 {
508     assert(partition != NULL);
509     if (offset > partition->size) {
510         return ESP_ERR_INVALID_ARG;
511     }
512     if (offset + size > partition->size) {
513         return ESP_ERR_INVALID_SIZE;
514     }
515     if (size % SPI_FLASH_SEC_SIZE != 0) {
516         return ESP_ERR_INVALID_SIZE;
517     }
518     if (offset % SPI_FLASH_SEC_SIZE != 0) {
519         return ESP_ERR_INVALID_ARG;
520     }
521 #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
522     return esp_flash_erase_region(partition->flash_chip, partition->address + offset, size);
523 #else
524     return spi_flash_erase_range(partition->address + offset, size);
525 #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
526 }
527 
528 /*
529  * Note: current implementation ignores the possibility of multiple regions in the same partition being
530  * mapped. Reference counting and address space re-use is delegated to spi_flash_mmap.
531  *
532  * If this becomes a performance issue (i.e. if we need to map multiple regions within the partition),
533  * we can add esp_partition_mmapv which will accept an array of offsets and sizes, and return array of
534  * mmaped pointers, and a single handle for all these regions.
535  */
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)536 esp_err_t esp_partition_mmap(const esp_partition_t* partition, size_t offset, size_t size,
537                              spi_flash_mmap_memory_t memory,
538                              const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
539 {
540     assert(partition != NULL);
541     if (offset > partition->size) {
542         return ESP_ERR_INVALID_ARG;
543     }
544     if (offset + size > partition->size) {
545         return ESP_ERR_INVALID_SIZE;
546     }
547     if (partition->flash_chip != esp_flash_default_chip) {
548         return ESP_ERR_NOT_SUPPORTED;
549     }
550     size_t phys_addr = partition->address + offset;
551     // offset within 64kB block
552     size_t region_offset = phys_addr & 0xffff;
553     size_t mmap_addr = phys_addr & 0xffff0000;
554     esp_err_t rc = spi_flash_mmap(mmap_addr, size+region_offset, memory, out_ptr, out_handle);
555     // adjust returned pointer to point to the correct offset
556     if (rc == ESP_OK) {
557         *out_ptr = (void*) (((ptrdiff_t) *out_ptr) + region_offset);
558     }
559     return rc;
560 }
561 
esp_partition_get_sha256(const esp_partition_t * partition,uint8_t * sha_256)562 esp_err_t esp_partition_get_sha256(const esp_partition_t *partition, uint8_t *sha_256)
563 {
564     return bootloader_common_get_sha256_of_partition(partition->address, partition->size, partition->type, sha_256);
565 }
566 
esp_partition_check_identity(const esp_partition_t * partition_1,const esp_partition_t * partition_2)567 bool esp_partition_check_identity(const esp_partition_t *partition_1, const esp_partition_t *partition_2)
568 {
569     uint8_t sha_256[2][HASH_LEN] = { 0 };
570 
571     if (esp_partition_get_sha256(partition_1, sha_256[0]) == ESP_OK &&
572         esp_partition_get_sha256(partition_2, sha_256[1]) == ESP_OK) {
573 
574         if (memcmp(sha_256[0], sha_256[1], HASH_LEN) == 0) {
575             // The partitions are identity
576             return true;
577         }
578     }
579     return false;
580 }
581 
esp_partition_main_flash_region_safe(size_t addr,size_t size)582 bool esp_partition_main_flash_region_safe(size_t addr, size_t size)
583 {
584     bool result = true;
585     if (addr <= ESP_PARTITION_TABLE_OFFSET + ESP_PARTITION_TABLE_MAX_LEN) {
586         return false;
587     }
588     const esp_partition_t *p = esp_ota_get_running_partition();
589     if (addr >= p->address && addr < p->address + p->size) {
590         return false;
591     }
592     if (addr < p->address && addr + size > p->address) {
593         return false;
594     }
595     return result;
596 }
597