partition.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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_data_types.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. #define HASH_LEN 32 /* SHA-256 digest length */
  28. #ifndef NDEBUG
  29. // Enable built-in checks in queue.h in debug builds
  30. #define INVARIANTS
  31. #endif
  32. #include "rom/queue.h"
  33. typedef struct partition_list_item_ {
  34. esp_partition_t info;
  35. SLIST_ENTRY(partition_list_item_) next;
  36. } partition_list_item_t;
  37. typedef struct esp_partition_iterator_opaque_ {
  38. esp_partition_type_t type; // requested type
  39. esp_partition_subtype_t subtype; // requested subtype
  40. const char* label; // requested label (can be NULL)
  41. partition_list_item_t* next_item; // next item to iterate to
  42. esp_partition_t* info; // pointer to info (it is redundant, but makes code more readable)
  43. } esp_partition_iterator_opaque_t;
  44. static esp_partition_iterator_opaque_t* iterator_create(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label);
  45. static esp_err_t load_partitions();
  46. static SLIST_HEAD(partition_list_head_, partition_list_item_) s_partition_list =
  47. SLIST_HEAD_INITIALIZER(s_partition_list);
  48. static _lock_t s_partition_list_lock;
  49. esp_partition_iterator_t esp_partition_find(esp_partition_type_t type,
  50. esp_partition_subtype_t subtype, const char* label)
  51. {
  52. if (SLIST_EMPTY(&s_partition_list)) {
  53. // only lock if list is empty (and check again after acquiring lock)
  54. _lock_acquire(&s_partition_list_lock);
  55. esp_err_t err = ESP_OK;
  56. if (SLIST_EMPTY(&s_partition_list)) {
  57. err = load_partitions();
  58. }
  59. _lock_release(&s_partition_list_lock);
  60. if (err != ESP_OK) {
  61. return NULL;
  62. }
  63. }
  64. // create an iterator pointing to the start of the list
  65. // (next item will be the first one)
  66. esp_partition_iterator_t it = iterator_create(type, subtype, label);
  67. // advance iterator to the next item which matches constraints
  68. it = esp_partition_next(it);
  69. // if nothing found, it == NULL and iterator has been released
  70. return it;
  71. }
  72. esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t it)
  73. {
  74. assert(it);
  75. // iterator reached the end of linked list?
  76. if (it->next_item == NULL) {
  77. esp_partition_iterator_release(it);
  78. return NULL;
  79. }
  80. _lock_acquire(&s_partition_list_lock);
  81. for (; it->next_item != NULL; it->next_item = SLIST_NEXT(it->next_item, next)) {
  82. esp_partition_t* p = &it->next_item->info;
  83. if (it->type != p->type) {
  84. continue;
  85. }
  86. if (it->subtype != 0xff && it->subtype != p->subtype) {
  87. continue;
  88. }
  89. if (it->label != NULL && strcmp(it->label, p->label) != 0) {
  90. continue;
  91. }
  92. // all constraints match, bail out
  93. break;
  94. }
  95. _lock_release(&s_partition_list_lock);
  96. if (it->next_item == NULL) {
  97. esp_partition_iterator_release(it);
  98. return NULL;
  99. }
  100. it->info = &it->next_item->info;
  101. it->next_item = SLIST_NEXT(it->next_item, next);
  102. return it;
  103. }
  104. const esp_partition_t* esp_partition_find_first(esp_partition_type_t type,
  105. esp_partition_subtype_t subtype, const char* label)
  106. {
  107. esp_partition_iterator_t it = esp_partition_find(type, subtype, label);
  108. if (it == NULL) {
  109. return NULL;
  110. }
  111. const esp_partition_t* res = esp_partition_get(it);
  112. esp_partition_iterator_release(it);
  113. return res;
  114. }
  115. static esp_partition_iterator_opaque_t* iterator_create(esp_partition_type_t type,
  116. esp_partition_subtype_t subtype, const char* label)
  117. {
  118. esp_partition_iterator_opaque_t* it =
  119. (esp_partition_iterator_opaque_t*) malloc(sizeof(esp_partition_iterator_opaque_t));
  120. it->type = type;
  121. it->subtype = subtype;
  122. it->label = label;
  123. it->next_item = SLIST_FIRST(&s_partition_list);
  124. it->info = NULL;
  125. return it;
  126. }
  127. // Create linked list of partition_list_item_t structures.
  128. // This function is called only once, with s_partition_list_lock taken.
  129. static esp_err_t load_partitions()
  130. {
  131. const uint32_t* ptr;
  132. spi_flash_mmap_handle_t handle;
  133. // map 64kB block where partition table is located
  134. esp_err_t err = spi_flash_mmap(ESP_PARTITION_TABLE_OFFSET & 0xffff0000,
  135. SPI_FLASH_SEC_SIZE, SPI_FLASH_MMAP_DATA, (const void**) &ptr, &handle);
  136. if (err != ESP_OK) {
  137. return err;
  138. }
  139. // calculate partition address within mmap-ed region
  140. const esp_partition_info_t* it = (const esp_partition_info_t*)
  141. (ptr + (ESP_PARTITION_TABLE_OFFSET & 0xffff) / sizeof(*ptr));
  142. const esp_partition_info_t* end = it + SPI_FLASH_SEC_SIZE / sizeof(*it);
  143. // tail of the linked list of partitions
  144. partition_list_item_t* last = NULL;
  145. for (; it != end; ++it) {
  146. if (it->magic != ESP_PARTITION_MAGIC) {
  147. break;
  148. }
  149. // allocate new linked list item and populate it with data from partition table
  150. partition_list_item_t* item = (partition_list_item_t*) malloc(sizeof(partition_list_item_t));
  151. item->info.address = it->pos.offset;
  152. item->info.size = it->pos.size;
  153. item->info.type = it->type;
  154. item->info.subtype = it->subtype;
  155. item->info.encrypted = it->flags & PART_FLAG_ENCRYPTED;
  156. if (esp_flash_encryption_enabled() && (
  157. it->type == PART_TYPE_APP
  158. || (it->type == PART_TYPE_DATA && it->subtype == PART_SUBTYPE_DATA_OTA)
  159. || (it->type == PART_TYPE_DATA && it->subtype == PART_SUBTYPE_DATA_NVS_KEYS))) {
  160. /* If encryption is turned on, all app partitions and OTA data
  161. are always encrypted */
  162. item->info.encrypted = true;
  163. }
  164. // it->label may not be zero-terminated
  165. strncpy(item->info.label, (const char*) it->label, sizeof(item->info.label) - 1);
  166. item->info.label[sizeof(it->label)] = 0;
  167. // add it to the list
  168. if (last == NULL) {
  169. SLIST_INSERT_HEAD(&s_partition_list, item, next);
  170. } else {
  171. SLIST_INSERT_AFTER(last, item, next);
  172. }
  173. last = item;
  174. }
  175. spi_flash_munmap(handle);
  176. return ESP_OK;
  177. }
  178. void esp_partition_iterator_release(esp_partition_iterator_t iterator)
  179. {
  180. // iterator == NULL is okay
  181. free(iterator);
  182. }
  183. const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator)
  184. {
  185. assert(iterator != NULL);
  186. return iterator->info;
  187. }
  188. const esp_partition_t *esp_partition_verify(const esp_partition_t *partition)
  189. {
  190. assert(partition != NULL);
  191. const char *label = (strlen(partition->label) > 0) ? partition->label : NULL;
  192. esp_partition_iterator_t it = esp_partition_find(partition->type,
  193. partition->subtype,
  194. label);
  195. while (it != NULL) {
  196. const esp_partition_t *p = esp_partition_get(it);
  197. /* Can't memcmp() whole structure here as padding contents may be different */
  198. if (p->address == partition->address
  199. && partition->size == p->size
  200. && partition->encrypted == p->encrypted) {
  201. esp_partition_iterator_release(it);
  202. return p;
  203. }
  204. it = esp_partition_next(it);
  205. }
  206. esp_partition_iterator_release(it);
  207. return NULL;
  208. }
  209. esp_err_t esp_partition_read(const esp_partition_t* partition,
  210. size_t src_offset, void* dst, size_t size)
  211. {
  212. assert(partition != NULL);
  213. if (src_offset > partition->size) {
  214. return ESP_ERR_INVALID_ARG;
  215. }
  216. if (src_offset + size > partition->size) {
  217. return ESP_ERR_INVALID_SIZE;
  218. }
  219. if (!partition->encrypted) {
  220. return spi_flash_read(partition->address + src_offset, dst, size);
  221. } else {
  222. /* Encrypted partitions need to be read via a cache mapping */
  223. const void *buf;
  224. spi_flash_mmap_handle_t handle;
  225. esp_err_t err;
  226. err = esp_partition_mmap(partition, src_offset, size,
  227. SPI_FLASH_MMAP_DATA, &buf, &handle);
  228. if (err != ESP_OK) {
  229. return err;
  230. }
  231. memcpy(dst, buf, size);
  232. spi_flash_munmap(handle);
  233. return ESP_OK;
  234. }
  235. }
  236. esp_err_t esp_partition_write(const esp_partition_t* partition,
  237. size_t dst_offset, const void* src, size_t size)
  238. {
  239. assert(partition != NULL);
  240. if (dst_offset > partition->size) {
  241. return ESP_ERR_INVALID_ARG;
  242. }
  243. if (dst_offset + size > partition->size) {
  244. return ESP_ERR_INVALID_SIZE;
  245. }
  246. dst_offset = partition->address + dst_offset;
  247. if (partition->encrypted) {
  248. return spi_flash_write_encrypted(dst_offset, src, size);
  249. } else {
  250. return spi_flash_write(dst_offset, src, size);
  251. }
  252. }
  253. esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
  254. size_t start_addr, size_t size)
  255. {
  256. assert(partition != NULL);
  257. if (start_addr > partition->size) {
  258. return ESP_ERR_INVALID_ARG;
  259. }
  260. if (start_addr + size > partition->size) {
  261. return ESP_ERR_INVALID_SIZE;
  262. }
  263. if (size % SPI_FLASH_SEC_SIZE != 0) {
  264. return ESP_ERR_INVALID_SIZE;
  265. }
  266. if (start_addr % SPI_FLASH_SEC_SIZE != 0) {
  267. return ESP_ERR_INVALID_ARG;
  268. }
  269. return spi_flash_erase_range(partition->address + start_addr, size);
  270. }
  271. /*
  272. * Note: current implementation ignores the possibility of multiple regions in the same partition being
  273. * mapped. Reference counting and address space re-use is delegated to spi_flash_mmap.
  274. *
  275. * If this becomes a performance issue (i.e. if we need to map multiple regions within the partition),
  276. * we can add esp_partition_mmapv which will accept an array of offsets and sizes, and return array of
  277. * mmaped pointers, and a single handle for all these regions.
  278. */
  279. esp_err_t esp_partition_mmap(const esp_partition_t* partition, uint32_t offset, uint32_t size,
  280. spi_flash_mmap_memory_t memory,
  281. const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
  282. {
  283. assert(partition != NULL);
  284. if (offset > partition->size) {
  285. return ESP_ERR_INVALID_ARG;
  286. }
  287. if (offset + size > partition->size) {
  288. return ESP_ERR_INVALID_SIZE;
  289. }
  290. size_t phys_addr = partition->address + offset;
  291. // offset within 64kB block
  292. size_t region_offset = phys_addr & 0xffff;
  293. size_t mmap_addr = phys_addr & 0xffff0000;
  294. esp_err_t rc = spi_flash_mmap(mmap_addr, size+region_offset, memory, out_ptr, out_handle);
  295. // adjust returned pointer to point to the correct offset
  296. if (rc == ESP_OK) {
  297. *out_ptr = (void*) (((ptrdiff_t) *out_ptr) + region_offset);
  298. }
  299. return rc;
  300. }
  301. esp_err_t esp_partition_get_sha256(const esp_partition_t *partition, uint8_t *sha_256)
  302. {
  303. return bootloader_common_get_sha256_of_partition(partition->address, partition->size, partition->type, sha_256);
  304. }
  305. bool esp_partition_check_identity(const esp_partition_t *partition_1, const esp_partition_t *partition_2)
  306. {
  307. uint8_t sha_256[2][HASH_LEN] = { 0 };
  308. if (esp_partition_get_sha256(partition_1, sha_256[0]) == ESP_OK &&
  309. esp_partition_get_sha256(partition_2, sha_256[1]) == ESP_OK) {
  310. if (memcmp(sha_256[0], sha_256[1], HASH_LEN) == 0) {
  311. // The partitions are identity
  312. return true;
  313. }
  314. }
  315. return false;
  316. }