partition.c 21 KB

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