partition.c 17 KB

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