partition.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 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. /* interim to enable test_wl_host and test_fatfs_on_host compilation (both use IDF_TARGET_ESP32)
  12. * should go back to #include "sys/queue.h" once the tests are switched to CMake
  13. * see IDF-7000
  14. */
  15. #if __has_include(<bsd/string.h>)
  16. #include <bsd/sys/queue.h>
  17. #else
  18. #include "sys/queue.h"
  19. #endif
  20. #include "sdkconfig.h"
  21. #include "esp_flash_partitions.h"
  22. #include "esp_attr.h"
  23. #include "esp_partition.h"
  24. #if !CONFIG_IDF_TARGET_LINUX
  25. #include "esp_flash.h"
  26. #include "esp_flash_encrypt.h"
  27. #endif
  28. #include "esp_log.h"
  29. #include "esp_rom_md5.h"
  30. #include "bootloader_util.h"
  31. #if CONFIG_IDF_TARGET_LINUX
  32. #if __has_include(<bsd/string.h>)
  33. #include <bsd/string.h>
  34. #endif
  35. #include "esp_private/partition_linux.h"
  36. #endif
  37. #ifndef CONFIG_IDF_TARGET_LINUX
  38. #define MMU_PAGE_SIZE CONFIG_MMU_PAGE_SIZE
  39. #else
  40. // No relation to the page size on Linux; assume the same value as on ESP32
  41. #define MMU_PAGE_SIZE 65536
  42. #endif // CONFIG_MMU_PAGE_SIZE
  43. #ifndef NDEBUG
  44. // Enable built-in checks in queue.h in debug builds
  45. #define INVARIANTS
  46. #endif
  47. typedef struct partition_list_item_ {
  48. esp_partition_t info;
  49. bool user_registered;
  50. SLIST_ENTRY(partition_list_item_) next;
  51. } partition_list_item_t;
  52. typedef struct esp_partition_iterator_opaque_ {
  53. esp_partition_type_t type; // requested type
  54. esp_partition_subtype_t subtype; // requested subtype
  55. const char *label; // requested label (can be NULL)
  56. partition_list_item_t *next_item; // next item to iterate to
  57. esp_partition_t *info; // pointer to info (it is redundant, but makes code more readable)
  58. } esp_partition_iterator_opaque_t;
  59. static SLIST_HEAD(partition_list_head_, partition_list_item_) s_partition_list = SLIST_HEAD_INITIALIZER(s_partition_list);
  60. static _lock_t s_partition_list_lock;
  61. static const char *TAG = "partition";
  62. // Create linked list of partition_list_item_t structures.
  63. // This function is called only once, with s_partition_list_lock taken.
  64. static esp_err_t load_partitions(void)
  65. {
  66. const uint8_t *p_start;
  67. const uint8_t *p_end;
  68. #if !CONFIG_IDF_TARGET_LINUX
  69. spi_flash_mmap_handle_t handle;
  70. #endif
  71. // Temporary list of loaded partitions, if valid then we copy this to s_partition_list
  72. typeof(s_partition_list) new_partitions_list = SLIST_HEAD_INITIALIZER(s_partition_list);
  73. partition_list_item_t *last = NULL;
  74. #if CONFIG_PARTITION_TABLE_MD5
  75. const uint8_t *md5_part = NULL;
  76. const uint8_t *stored_md5;
  77. uint8_t calc_md5[ESP_ROM_MD5_DIGEST_LEN];
  78. md5_context_t context;
  79. esp_rom_md5_init(&context);
  80. #endif
  81. uint32_t partition_align_pg_size = (ESP_PARTITION_TABLE_OFFSET) & ~(MMU_PAGE_SIZE - 1);
  82. uint32_t partition_pad = ESP_PARTITION_TABLE_OFFSET - partition_align_pg_size;
  83. #if CONFIG_IDF_TARGET_LINUX
  84. esp_err_t err = esp_partition_file_mmap(&p_start);
  85. size_t mapped_size = ESP_PARTITION_EMULATED_SECTOR_SIZE;
  86. #else
  87. esp_err_t err = spi_flash_mmap(partition_align_pg_size,
  88. SPI_FLASH_SEC_SIZE, SPI_FLASH_MMAP_DATA, (const void **)&p_start, &handle);
  89. size_t mapped_size = SPI_FLASH_SEC_SIZE;
  90. #endif
  91. if (err != ESP_OK) {
  92. return err;
  93. }
  94. // calculate partition address within mmap-ed region
  95. p_start += partition_pad;
  96. p_end = p_start + mapped_size;
  97. for (const uint8_t *p_entry = p_start; p_entry < p_end; p_entry += sizeof(esp_partition_info_t)) {
  98. esp_partition_info_t entry;
  99. // copying to RAM instead of using pointer to flash to avoid any chance of TOCTOU due to cache miss
  100. // when flash encryption is used
  101. memcpy(&entry, p_entry, sizeof(entry));
  102. #if CONFIG_PARTITION_TABLE_MD5
  103. if (entry.magic == ESP_PARTITION_MAGIC_MD5) {
  104. md5_part = p_entry;
  105. break;
  106. }
  107. #endif
  108. if (entry.magic != ESP_PARTITION_MAGIC) {
  109. break;
  110. }
  111. #if CONFIG_PARTITION_TABLE_MD5
  112. esp_rom_md5_update(&context, &entry, sizeof(entry));
  113. #endif
  114. // allocate new linked list item and populate it with data from partition table
  115. partition_list_item_t *item = (partition_list_item_t *) calloc(sizeof(partition_list_item_t), 1);
  116. if (item == NULL) {
  117. err = ESP_ERR_NO_MEM;
  118. break;
  119. }
  120. #if CONFIG_IDF_TARGET_LINUX
  121. item->info.flash_chip = NULL;
  122. #else
  123. item->info.flash_chip = esp_flash_default_chip;
  124. #endif
  125. item->info.address = entry.pos.offset;
  126. item->info.size = entry.pos.size;
  127. #if CONFIG_IDF_TARGET_LINUX
  128. item->info.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE;
  129. #else
  130. item->info.erase_size = SPI_FLASH_SEC_SIZE;
  131. #endif
  132. item->info.type = entry.type;
  133. item->info.subtype = entry.subtype;
  134. item->info.encrypted = entry.flags & PART_FLAG_ENCRYPTED;
  135. item->info.readonly = entry.flags & PART_FLAG_READONLY;
  136. item->user_registered = false;
  137. #if CONFIG_IDF_TARGET_LINUX
  138. item->info.encrypted = false;
  139. #else
  140. if (!esp_flash_encryption_enabled()) {
  141. /* If flash encryption is not turned on, no partitions should be treated as encrypted */
  142. item->info.encrypted = false;
  143. } else if (entry.type == ESP_PARTITION_TYPE_APP
  144. || (entry.type == ESP_PARTITION_TYPE_DATA && entry.subtype == ESP_PARTITION_SUBTYPE_DATA_OTA)
  145. || (entry.type == ESP_PARTITION_TYPE_DATA && entry.subtype == ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS)) {
  146. /* If encryption is turned on, all app partitions and OTA data
  147. are always encrypted */
  148. item->info.encrypted = true;
  149. }
  150. #endif
  151. #if CONFIG_NVS_COMPATIBLE_PRE_V4_3_ENCRYPTION_FLAG
  152. if (entry.type == ESP_PARTITION_TYPE_DATA &&
  153. entry.subtype == ESP_PARTITION_SUBTYPE_DATA_NVS &&
  154. (entry.flags & PART_FLAG_ENCRYPTED)) {
  155. ESP_LOGI(TAG, "Ignoring encrypted flag for \"%s\" partition", entry.label);
  156. item->info.encrypted = false;
  157. }
  158. #endif
  159. // item->info.label is initialized by calloc, so resulting string will be null terminated
  160. strncpy(item->info.label, (const char *) entry.label, sizeof(item->info.label) - 1);
  161. // add it to the list
  162. if (last == NULL) {
  163. SLIST_INSERT_HEAD(&new_partitions_list, item, next);
  164. } else {
  165. SLIST_INSERT_AFTER(last, item, next);
  166. }
  167. last = item;
  168. }
  169. #if CONFIG_PARTITION_TABLE_MD5
  170. if (md5_part == NULL) {
  171. ESP_LOGE(TAG, "No MD5 found in partition table");
  172. err = ESP_ERR_NOT_FOUND;
  173. } else {
  174. stored_md5 = md5_part + ESP_PARTITION_MD5_OFFSET;
  175. esp_rom_md5_final(calc_md5, &context);
  176. #if !CONFIG_IDF_TARGET_LINUX
  177. ESP_LOG_BUFFER_HEXDUMP("calculated md5", calc_md5, ESP_ROM_MD5_DIGEST_LEN, ESP_LOG_VERBOSE);
  178. ESP_LOG_BUFFER_HEXDUMP("stored md5", stored_md5, ESP_ROM_MD5_DIGEST_LEN, ESP_LOG_VERBOSE);
  179. #endif
  180. if (memcmp(calc_md5, stored_md5, ESP_ROM_MD5_DIGEST_LEN) != 0) {
  181. ESP_LOGE(TAG, "Partition table MD5 mismatch");
  182. err = ESP_ERR_INVALID_STATE;
  183. } else {
  184. ESP_LOGV(TAG, "Partition table MD5 verified");
  185. }
  186. }
  187. #endif
  188. if (err == ESP_OK) {
  189. /* Don't copy the list to the static variable unless it's verified */
  190. s_partition_list = new_partitions_list;
  191. } else {
  192. /* Otherwise, free all the memory we just allocated */
  193. partition_list_item_t *it = new_partitions_list.slh_first;
  194. while (it) {
  195. partition_list_item_t *next = it->next.sle_next;
  196. free(it);
  197. it = next;
  198. }
  199. }
  200. #if !CONFIG_IDF_TARGET_LINUX
  201. spi_flash_munmap(handle);
  202. #endif
  203. return err;
  204. }
  205. void unload_partitions(void)
  206. {
  207. _lock_acquire(&s_partition_list_lock);
  208. partition_list_item_t *it;
  209. partition_list_item_t *tmp;
  210. SLIST_FOREACH_SAFE(it, &s_partition_list, next, tmp) {
  211. SLIST_REMOVE(&s_partition_list, it, partition_list_item_, next);
  212. free(it);
  213. }
  214. _lock_release(&s_partition_list_lock);
  215. assert(SLIST_EMPTY(&s_partition_list));
  216. }
  217. static esp_err_t ensure_partitions_loaded(void)
  218. {
  219. esp_err_t err = ESP_OK;
  220. if (SLIST_EMPTY(&s_partition_list)) {
  221. // only lock if list is empty (and check again after acquiring lock)
  222. _lock_acquire(&s_partition_list_lock);
  223. if (SLIST_EMPTY(&s_partition_list)) {
  224. ESP_LOGV(TAG, "Loading the partition table");
  225. err = load_partitions();
  226. if (err != ESP_OK) {
  227. ESP_LOGE(TAG, "load_partitions returned 0x%x", err);
  228. }
  229. }
  230. _lock_release(&s_partition_list_lock);
  231. }
  232. return err;
  233. }
  234. static esp_partition_iterator_opaque_t *iterator_create(esp_partition_type_t type,
  235. esp_partition_subtype_t subtype, const char *label)
  236. {
  237. esp_partition_iterator_opaque_t *it =
  238. (esp_partition_iterator_opaque_t *) malloc(sizeof(esp_partition_iterator_opaque_t));
  239. if (it == NULL) {
  240. return NULL;
  241. }
  242. it->type = type;
  243. it->subtype = subtype;
  244. it->label = label;
  245. it->next_item = SLIST_FIRST(&s_partition_list);
  246. it->info = NULL;
  247. return it;
  248. }
  249. esp_partition_iterator_t esp_partition_find(esp_partition_type_t type,
  250. esp_partition_subtype_t subtype, const char *label)
  251. {
  252. if (ensure_partitions_loaded() != ESP_OK) {
  253. return NULL;
  254. }
  255. // Searching for a specific subtype without specifying the type doesn't make
  256. // sense, and is likely a usage error.
  257. if (type == ESP_PARTITION_TYPE_ANY && subtype != ESP_PARTITION_SUBTYPE_ANY) {
  258. return NULL;
  259. }
  260. // create an iterator pointing to the start of the list
  261. // (next item will be the first one)
  262. esp_partition_iterator_t it = iterator_create(type, subtype, label);
  263. if (it == NULL) {
  264. return NULL;
  265. }
  266. // advance iterator to the next item which matches constraints
  267. it = esp_partition_next(it);
  268. // if nothing found, it == NULL and iterator has been released
  269. return it;
  270. }
  271. esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t it)
  272. {
  273. assert(it);
  274. // iterator reached the end of linked list?
  275. if (it->next_item == NULL) {
  276. esp_partition_iterator_release(it);
  277. return NULL;
  278. }
  279. _lock_acquire(&s_partition_list_lock);
  280. for (; it->next_item != NULL; it->next_item = SLIST_NEXT(it->next_item, next)) {
  281. esp_partition_t *p = &it->next_item->info;
  282. if (it->type != ESP_PARTITION_TYPE_ANY && it->type != p->type) {
  283. continue;
  284. }
  285. if (it->subtype != ESP_PARTITION_SUBTYPE_ANY && it->subtype != p->subtype) {
  286. continue;
  287. }
  288. if (it->label != NULL && strcmp(it->label, p->label) != 0) {
  289. continue;
  290. }
  291. // all constraints match, bail out
  292. break;
  293. }
  294. _lock_release(&s_partition_list_lock);
  295. if (it->next_item == NULL) {
  296. esp_partition_iterator_release(it);
  297. return NULL;
  298. }
  299. it->info = &it->next_item->info;
  300. it->next_item = SLIST_NEXT(it->next_item, next);
  301. return it;
  302. }
  303. const esp_partition_t *esp_partition_find_first(esp_partition_type_t type,
  304. esp_partition_subtype_t subtype, const char *label)
  305. {
  306. esp_partition_iterator_t it = esp_partition_find(type, subtype, label);
  307. if (it == NULL) {
  308. return NULL;
  309. }
  310. const esp_partition_t *res = esp_partition_get(it);
  311. esp_partition_iterator_release(it);
  312. return res;
  313. }
  314. void esp_partition_iterator_release(esp_partition_iterator_t iterator)
  315. {
  316. // iterator == NULL is okay
  317. free(iterator);
  318. }
  319. const esp_partition_t *esp_partition_get(esp_partition_iterator_t iterator)
  320. {
  321. assert(iterator != NULL);
  322. return iterator->info;
  323. }
  324. const esp_partition_t *esp_partition_verify(const esp_partition_t *partition)
  325. {
  326. assert(partition != NULL);
  327. const char *label = (strlen(partition->label) > 0) ? partition->label : NULL;
  328. esp_partition_iterator_t it = esp_partition_find(partition->type,
  329. partition->subtype,
  330. label);
  331. while (it != NULL) {
  332. const esp_partition_t *p = esp_partition_get(it);
  333. /* Can't memcmp() whole structure here as padding contents may be different */
  334. if (p->flash_chip == partition->flash_chip
  335. && p->address == partition->address
  336. && partition->size == p->size
  337. && partition->encrypted == p->encrypted) {
  338. esp_partition_iterator_release(it);
  339. return p;
  340. }
  341. it = esp_partition_next(it);
  342. }
  343. esp_partition_iterator_release(it);
  344. return NULL;
  345. }
  346. esp_err_t esp_partition_register_external(esp_flash_t *flash_chip, size_t offset, size_t size,
  347. const char *label, esp_partition_type_t type, esp_partition_subtype_t subtype,
  348. const esp_partition_t **out_partition)
  349. {
  350. if (out_partition != NULL) {
  351. *out_partition = NULL;
  352. }
  353. #if CONFIG_IDF_TARGET_LINUX
  354. return ESP_ERR_NOT_SUPPORTED;
  355. #else
  356. if (offset + size > flash_chip->size) {
  357. return ESP_ERR_INVALID_SIZE;
  358. }
  359. #endif // CONFIG_IDF_TARGET_LINUX
  360. esp_err_t err = ensure_partitions_loaded();
  361. if (err != ESP_OK) {
  362. return err;
  363. }
  364. partition_list_item_t *item = (partition_list_item_t *) calloc(sizeof(partition_list_item_t), 1);
  365. if (item == NULL) {
  366. return ESP_ERR_NO_MEM;
  367. }
  368. item->info.flash_chip = flash_chip;
  369. item->info.address = offset;
  370. item->info.size = size;
  371. item->info.type = type;
  372. item->info.subtype = subtype;
  373. item->info.encrypted = false;
  374. item->user_registered = true;
  375. strlcpy(item->info.label, label, sizeof(item->info.label));
  376. _lock_acquire(&s_partition_list_lock);
  377. partition_list_item_t *it = NULL;
  378. partition_list_item_t *last = NULL;
  379. SLIST_FOREACH(it, &s_partition_list, next) {
  380. /* Check if the new partition overlaps an existing one */
  381. if (it->info.flash_chip == flash_chip &&
  382. bootloader_util_regions_overlap(offset, offset + size,
  383. it->info.address, it->info.address + it->info.size)) {
  384. _lock_release(&s_partition_list_lock);
  385. free(item);
  386. return ESP_ERR_INVALID_ARG;
  387. }
  388. last = it;
  389. }
  390. if (last == NULL) {
  391. SLIST_INSERT_HEAD(&s_partition_list, item, next);
  392. } else {
  393. SLIST_INSERT_AFTER(last, item, next);
  394. }
  395. _lock_release(&s_partition_list_lock);
  396. if (out_partition != NULL) {
  397. *out_partition = &item->info;
  398. }
  399. return ESP_OK;
  400. }
  401. esp_err_t esp_partition_deregister_external(const esp_partition_t *partition)
  402. {
  403. esp_err_t result = ESP_ERR_NOT_FOUND;
  404. _lock_acquire(&s_partition_list_lock);
  405. partition_list_item_t *it;
  406. partition_list_item_t *tmp;
  407. SLIST_FOREACH_SAFE(it, &s_partition_list, next, tmp) {
  408. if (&it->info == partition) {
  409. if (!it->user_registered) {
  410. result = ESP_ERR_INVALID_ARG;
  411. break;
  412. }
  413. SLIST_REMOVE(&s_partition_list, it, partition_list_item_, next);
  414. free(it);
  415. result = ESP_OK;
  416. break;
  417. }
  418. }
  419. _lock_release(&s_partition_list_lock);
  420. return result;
  421. }