partition.c 11 KB

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