partition.c 20 KB

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