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