partition.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 == ESP_PARTITION_TYPE_APP
  206. || (entry.type == ESP_PARTITION_TYPE_DATA && entry.subtype == ESP_PARTITION_SUBTYPE_DATA_OTA)
  207. || (entry.type == ESP_PARTITION_TYPE_DATA && entry.subtype == ESP_PARTITION_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. #if CONFIG_NVS_COMPATIBLE_PRE_V4_3_ENCRYPTION_FLAG
  213. if (entry.type == ESP_PARTITION_TYPE_DATA &&
  214. entry.subtype == ESP_PARTITION_SUBTYPE_DATA_NVS &&
  215. (entry.flags & PART_FLAG_ENCRYPTED)) {
  216. ESP_LOGI(TAG, "Ignoring encrypted flag for \"%s\" partition", entry.label);
  217. item->info.encrypted = false;
  218. }
  219. #endif
  220. // item->info.label is initialized by calloc, so resulting string will be null terminated
  221. strncpy(item->info.label, (const char*) entry.label, sizeof(item->info.label) - 1);
  222. // add it to the list
  223. if (last == NULL) {
  224. SLIST_INSERT_HEAD(&new_partitions_list, item, next);
  225. } else {
  226. SLIST_INSERT_AFTER(last, item, next);
  227. }
  228. last = item;
  229. }
  230. #if CONFIG_PARTITION_TABLE_MD5
  231. if (md5_part == NULL) {
  232. ESP_LOGE(TAG, "No MD5 found in partition table");
  233. err = ESP_ERR_NOT_FOUND;
  234. } else {
  235. stored_md5 = md5_part + ESP_PARTITION_MD5_OFFSET;
  236. esp_rom_md5_final(calc_md5, &context);
  237. ESP_LOG_BUFFER_HEXDUMP("calculated md5", calc_md5, ESP_ROM_MD5_DIGEST_LEN, ESP_LOG_VERBOSE);
  238. ESP_LOG_BUFFER_HEXDUMP("stored md5", stored_md5, ESP_ROM_MD5_DIGEST_LEN, ESP_LOG_VERBOSE);
  239. if (memcmp(calc_md5, stored_md5, ESP_ROM_MD5_DIGEST_LEN) != 0) {
  240. ESP_LOGE(TAG, "Partition table MD5 mismatch");
  241. err = ESP_ERR_INVALID_STATE;
  242. } else {
  243. ESP_LOGD(TAG, "Partition table MD5 verified");
  244. }
  245. }
  246. #endif
  247. if (err == ESP_OK) {
  248. /* Don't copy the list to the static variable unless it's verified */
  249. s_partition_list = new_partitions_list;
  250. } else {
  251. /* Otherwise, free all the memory we just allocated */
  252. partition_list_item_t *it = new_partitions_list.slh_first;
  253. while (it) {
  254. partition_list_item_t *next = it->next.sle_next;
  255. free(it);
  256. it = next;
  257. }
  258. }
  259. spi_flash_munmap(handle);
  260. return err;
  261. }
  262. void esp_partition_iterator_release(esp_partition_iterator_t iterator)
  263. {
  264. // iterator == NULL is okay
  265. free(iterator);
  266. }
  267. const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator)
  268. {
  269. assert(iterator != NULL);
  270. return iterator->info;
  271. }
  272. esp_err_t esp_partition_register_external(esp_flash_t* flash_chip, size_t offset, size_t size,
  273. const char* label, esp_partition_type_t type, esp_partition_subtype_t subtype,
  274. const esp_partition_t** out_partition)
  275. {
  276. if (out_partition != NULL) {
  277. *out_partition = NULL;
  278. }
  279. #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  280. return ESP_ERR_NOT_SUPPORTED;
  281. #endif
  282. if (offset + size > flash_chip->size) {
  283. return ESP_ERR_INVALID_SIZE;
  284. }
  285. esp_err_t err = ensure_partitions_loaded();
  286. if (err != ESP_OK) {
  287. return err;
  288. }
  289. partition_list_item_t* item = (partition_list_item_t*) calloc(sizeof(partition_list_item_t), 1);
  290. if (item == NULL) {
  291. return ESP_ERR_NO_MEM;
  292. }
  293. item->info.flash_chip = flash_chip;
  294. item->info.address = offset;
  295. item->info.size = size;
  296. item->info.type = type;
  297. item->info.subtype = subtype;
  298. item->info.encrypted = false;
  299. item->user_registered = true;
  300. strlcpy(item->info.label, label, sizeof(item->info.label));
  301. _lock_acquire(&s_partition_list_lock);
  302. partition_list_item_t *it, *last = NULL;
  303. SLIST_FOREACH(it, &s_partition_list, next) {
  304. /* Check if the new partition overlaps an existing one */
  305. if (it->info.flash_chip == flash_chip &&
  306. bootloader_util_regions_overlap(offset, offset + size,
  307. it->info.address, it->info.address + it->info.size)) {
  308. _lock_release(&s_partition_list_lock);
  309. free(item);
  310. return ESP_ERR_INVALID_ARG;
  311. }
  312. last = it;
  313. }
  314. if (last == NULL) {
  315. SLIST_INSERT_HEAD(&s_partition_list, item, next);
  316. } else {
  317. SLIST_INSERT_AFTER(last, item, next);
  318. }
  319. _lock_release(&s_partition_list_lock);
  320. if (out_partition != NULL) {
  321. *out_partition = &item->info;
  322. }
  323. return ESP_OK;
  324. }
  325. esp_err_t esp_partition_deregister_external(const esp_partition_t* partition)
  326. {
  327. esp_err_t result = ESP_ERR_NOT_FOUND;
  328. _lock_acquire(&s_partition_list_lock);
  329. partition_list_item_t *it;
  330. SLIST_FOREACH(it, &s_partition_list, next) {
  331. if (&it->info == partition) {
  332. if (!it->user_registered) {
  333. result = ESP_ERR_INVALID_ARG;
  334. break;
  335. }
  336. SLIST_REMOVE(&s_partition_list, it, partition_list_item_, next);
  337. free(it);
  338. result = ESP_OK;
  339. break;
  340. }
  341. }
  342. _lock_release(&s_partition_list_lock);
  343. return result;
  344. }
  345. const esp_partition_t *esp_partition_verify(const esp_partition_t *partition)
  346. {
  347. assert(partition != NULL);
  348. const char *label = (strlen(partition->label) > 0) ? partition->label : NULL;
  349. esp_partition_iterator_t it = esp_partition_find(partition->type,
  350. partition->subtype,
  351. label);
  352. while (it != NULL) {
  353. const esp_partition_t *p = esp_partition_get(it);
  354. /* Can't memcmp() whole structure here as padding contents may be different */
  355. if (p->flash_chip == partition->flash_chip
  356. && p->address == partition->address
  357. && partition->size == p->size
  358. && partition->encrypted == p->encrypted) {
  359. esp_partition_iterator_release(it);
  360. return p;
  361. }
  362. it = esp_partition_next(it);
  363. }
  364. esp_partition_iterator_release(it);
  365. return NULL;
  366. }
  367. esp_err_t esp_partition_read(const esp_partition_t* partition,
  368. size_t src_offset, void* dst, size_t size)
  369. {
  370. assert(partition != NULL);
  371. if (src_offset > partition->size) {
  372. return ESP_ERR_INVALID_ARG;
  373. }
  374. if (src_offset + size > partition->size) {
  375. return ESP_ERR_INVALID_SIZE;
  376. }
  377. if (!partition->encrypted) {
  378. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  379. return esp_flash_read(partition->flash_chip, dst, partition->address + src_offset, size);
  380. #else
  381. return spi_flash_read(partition->address + src_offset, dst, size);
  382. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  383. } else {
  384. #if CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
  385. if (partition->flash_chip != esp_flash_default_chip) {
  386. return ESP_ERR_NOT_SUPPORTED;
  387. }
  388. /* Encrypted partitions need to be read via a cache mapping */
  389. const void *buf;
  390. spi_flash_mmap_handle_t handle;
  391. esp_err_t err;
  392. err = esp_partition_mmap(partition, src_offset, size,
  393. SPI_FLASH_MMAP_DATA, &buf, &handle);
  394. if (err != ESP_OK) {
  395. return err;
  396. }
  397. memcpy(dst, buf, size);
  398. spi_flash_munmap(handle);
  399. return ESP_OK;
  400. #else
  401. return ESP_ERR_NOT_SUPPORTED;
  402. #endif // CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
  403. }
  404. }
  405. esp_err_t esp_partition_write(const esp_partition_t* partition,
  406. size_t dst_offset, const void* src, size_t size)
  407. {
  408. assert(partition != NULL);
  409. if (dst_offset > partition->size) {
  410. return ESP_ERR_INVALID_ARG;
  411. }
  412. if (dst_offset + size > partition->size) {
  413. return ESP_ERR_INVALID_SIZE;
  414. }
  415. dst_offset = partition->address + dst_offset;
  416. if (!partition->encrypted) {
  417. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  418. return esp_flash_write(partition->flash_chip, src, dst_offset, size);
  419. #else
  420. return spi_flash_write(dst_offset, src, size);
  421. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  422. } else {
  423. #if CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
  424. if (partition->flash_chip != esp_flash_default_chip) {
  425. return ESP_ERR_NOT_SUPPORTED;
  426. }
  427. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  428. return esp_flash_write_encrypted(partition->flash_chip, dst_offset, src, size);
  429. #else
  430. return spi_flash_write_encrypted(dst_offset, src, size);
  431. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  432. #else
  433. return ESP_ERR_NOT_SUPPORTED;
  434. #endif // CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
  435. }
  436. }
  437. esp_err_t esp_partition_read_raw(const esp_partition_t* partition,
  438. size_t src_offset, void* dst, size_t size)
  439. {
  440. assert(partition != NULL);
  441. if (src_offset > partition->size) {
  442. return ESP_ERR_INVALID_ARG;
  443. }
  444. if (src_offset + size > partition->size) {
  445. return ESP_ERR_INVALID_SIZE;
  446. }
  447. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  448. return esp_flash_read(partition->flash_chip, dst, partition->address + src_offset, size);
  449. #else
  450. return spi_flash_read(partition->address + src_offset, dst, size);
  451. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  452. }
  453. esp_err_t esp_partition_write_raw(const esp_partition_t* partition,
  454. size_t dst_offset, const void* src, size_t size)
  455. {
  456. assert(partition != NULL);
  457. if (dst_offset > partition->size) {
  458. return ESP_ERR_INVALID_ARG;
  459. }
  460. if (dst_offset + size > partition->size) {
  461. return ESP_ERR_INVALID_SIZE;
  462. }
  463. dst_offset = partition->address + dst_offset;
  464. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  465. return esp_flash_write(partition->flash_chip, src, dst_offset, size);
  466. #else
  467. return spi_flash_write(dst_offset, src, size);
  468. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  469. }
  470. esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
  471. size_t offset, size_t size)
  472. {
  473. assert(partition != NULL);
  474. if (offset > partition->size) {
  475. return ESP_ERR_INVALID_ARG;
  476. }
  477. if (offset + size > partition->size) {
  478. return ESP_ERR_INVALID_SIZE;
  479. }
  480. if (size % SPI_FLASH_SEC_SIZE != 0) {
  481. return ESP_ERR_INVALID_SIZE;
  482. }
  483. if (offset % SPI_FLASH_SEC_SIZE != 0) {
  484. return ESP_ERR_INVALID_ARG;
  485. }
  486. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  487. return esp_flash_erase_region(partition->flash_chip, partition->address + offset, size);
  488. #else
  489. return spi_flash_erase_range(partition->address + offset, size);
  490. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  491. }
  492. /*
  493. * Note: current implementation ignores the possibility of multiple regions in the same partition being
  494. * mapped. Reference counting and address space re-use is delegated to spi_flash_mmap.
  495. *
  496. * If this becomes a performance issue (i.e. if we need to map multiple regions within the partition),
  497. * we can add esp_partition_mmapv which will accept an array of offsets and sizes, and return array of
  498. * mmaped pointers, and a single handle for all these regions.
  499. */
  500. esp_err_t esp_partition_mmap(const esp_partition_t* partition, size_t offset, size_t size,
  501. spi_flash_mmap_memory_t memory,
  502. const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
  503. {
  504. assert(partition != NULL);
  505. if (offset > partition->size) {
  506. return ESP_ERR_INVALID_ARG;
  507. }
  508. if (offset + size > partition->size) {
  509. return ESP_ERR_INVALID_SIZE;
  510. }
  511. if (partition->flash_chip != esp_flash_default_chip) {
  512. return ESP_ERR_NOT_SUPPORTED;
  513. }
  514. size_t phys_addr = partition->address + offset;
  515. // offset within 64kB block
  516. size_t region_offset = phys_addr & 0xffff;
  517. size_t mmap_addr = phys_addr & 0xffff0000;
  518. esp_err_t rc = spi_flash_mmap(mmap_addr, size+region_offset, memory, out_ptr, out_handle);
  519. // adjust returned pointer to point to the correct offset
  520. if (rc == ESP_OK) {
  521. *out_ptr = (void*) (((ptrdiff_t) *out_ptr) + region_offset);
  522. }
  523. return rc;
  524. }
  525. esp_err_t esp_partition_get_sha256(const esp_partition_t *partition, uint8_t *sha_256)
  526. {
  527. return bootloader_common_get_sha256_of_partition(partition->address, partition->size, partition->type, sha_256);
  528. }
  529. bool esp_partition_check_identity(const esp_partition_t *partition_1, const esp_partition_t *partition_2)
  530. {
  531. uint8_t sha_256[2][HASH_LEN] = { 0 };
  532. if (esp_partition_get_sha256(partition_1, sha_256[0]) == ESP_OK &&
  533. esp_partition_get_sha256(partition_2, sha_256[1]) == ESP_OK) {
  534. if (memcmp(sha_256[0], sha_256[1], HASH_LEN) == 0) {
  535. // The partitions are identity
  536. return true;
  537. }
  538. }
  539. return false;
  540. }
  541. bool esp_partition_main_flash_region_safe(size_t addr, size_t size)
  542. {
  543. bool result = true;
  544. if (addr <= ESP_PARTITION_TABLE_OFFSET + ESP_PARTITION_TABLE_MAX_LEN) {
  545. return false;
  546. }
  547. const esp_partition_t *p = esp_ota_get_running_partition();
  548. if (addr >= p->address && addr < p->address + p->size) {
  549. return false;
  550. }
  551. if (addr < p->address && addr + size > p->address) {
  552. return false;
  553. }
  554. return result;
  555. }