partition.c 19 KB

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