partition.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 "rom/md5_hash.h"
  20. #include "esp_flash_partitions.h"
  21. #include "esp_attr.h"
  22. #include "esp_flash_data_types.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. #define HASH_LEN 32 /* SHA-256 digest length */
  29. #define MD5_DIGEST_LEN 16
  30. #ifndef NDEBUG
  31. // Enable built-in checks in queue.h in debug builds
  32. #define INVARIANTS
  33. #endif
  34. #include "rom/queue.h"
  35. static const char *TAG = "partition";
  36. typedef struct partition_list_item_ {
  37. esp_partition_t info;
  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();
  49. static SLIST_HEAD(partition_list_head_, partition_list_item_) s_partition_list =
  50. SLIST_HEAD_INITIALIZER(s_partition_list);
  51. static _lock_t s_partition_list_lock;
  52. esp_partition_iterator_t esp_partition_find(esp_partition_type_t type,
  53. esp_partition_subtype_t subtype, const char* label)
  54. {
  55. if (SLIST_EMPTY(&s_partition_list)) {
  56. // only lock if list is empty (and check again after acquiring lock)
  57. _lock_acquire(&s_partition_list_lock);
  58. esp_err_t err = ESP_OK;
  59. if (SLIST_EMPTY(&s_partition_list)) {
  60. err = load_partitions();
  61. }
  62. _lock_release(&s_partition_list_lock);
  63. if (err != ESP_OK) {
  64. return NULL;
  65. }
  66. }
  67. // create an iterator pointing to the start of the list
  68. // (next item will be the first one)
  69. esp_partition_iterator_t it = iterator_create(type, subtype, label);
  70. // advance iterator to the next item which matches constraints
  71. it = esp_partition_next(it);
  72. // if nothing found, it == NULL and iterator has been released
  73. return it;
  74. }
  75. esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t it)
  76. {
  77. assert(it);
  78. // iterator reached the end of linked list?
  79. if (it->next_item == NULL) {
  80. esp_partition_iterator_release(it);
  81. return NULL;
  82. }
  83. _lock_acquire(&s_partition_list_lock);
  84. for (; it->next_item != NULL; it->next_item = SLIST_NEXT(it->next_item, next)) {
  85. esp_partition_t* p = &it->next_item->info;
  86. if (it->type != p->type) {
  87. continue;
  88. }
  89. if (it->subtype != 0xff && it->subtype != p->subtype) {
  90. continue;
  91. }
  92. if (it->label != NULL && strcmp(it->label, p->label) != 0) {
  93. continue;
  94. }
  95. // all constraints match, bail out
  96. break;
  97. }
  98. _lock_release(&s_partition_list_lock);
  99. if (it->next_item == NULL) {
  100. esp_partition_iterator_release(it);
  101. return NULL;
  102. }
  103. it->info = &it->next_item->info;
  104. it->next_item = SLIST_NEXT(it->next_item, next);
  105. return it;
  106. }
  107. const esp_partition_t* esp_partition_find_first(esp_partition_type_t type,
  108. esp_partition_subtype_t subtype, const char* label)
  109. {
  110. esp_partition_iterator_t it = esp_partition_find(type, subtype, label);
  111. if (it == NULL) {
  112. return NULL;
  113. }
  114. const esp_partition_t* res = esp_partition_get(it);
  115. esp_partition_iterator_release(it);
  116. return res;
  117. }
  118. static esp_partition_iterator_opaque_t* iterator_create(esp_partition_type_t type,
  119. esp_partition_subtype_t subtype, const char* label)
  120. {
  121. esp_partition_iterator_opaque_t* it =
  122. (esp_partition_iterator_opaque_t*) malloc(sizeof(esp_partition_iterator_opaque_t));
  123. it->type = type;
  124. it->subtype = subtype;
  125. it->label = label;
  126. it->next_item = SLIST_FIRST(&s_partition_list);
  127. it->info = NULL;
  128. return it;
  129. }
  130. // Create linked list of partition_list_item_t structures.
  131. // This function is called only once, with s_partition_list_lock taken.
  132. static esp_err_t load_partitions()
  133. {
  134. const uint8_t *p_start;
  135. const uint8_t *p_end;
  136. spi_flash_mmap_handle_t handle;
  137. // Temporary list of loaded partitions, if valid then we copy this to s_partition_list
  138. typeof(s_partition_list) new_partitions_list = SLIST_HEAD_INITIALIZER(s_partition_list);
  139. partition_list_item_t* last = NULL;
  140. #if CONFIG_PARTITION_TABLE_MD5
  141. const uint8_t *md5_part = NULL;
  142. const uint8_t *stored_md5;
  143. uint8_t calc_md5[MD5_DIGEST_LEN];
  144. struct MD5Context context;
  145. MD5Init(&context);
  146. #endif
  147. // map 64kB block where partition table is located
  148. esp_err_t err = spi_flash_mmap(ESP_PARTITION_TABLE_OFFSET & 0xffff0000,
  149. SPI_FLASH_SEC_SIZE, SPI_FLASH_MMAP_DATA, (const void **)&p_start, &handle);
  150. if (err != ESP_OK) {
  151. return err;
  152. }
  153. // calculate partition address within mmap-ed region
  154. p_start += (ESP_PARTITION_TABLE_OFFSET & 0xffff);
  155. p_end = p_start + SPI_FLASH_SEC_SIZE;
  156. for(const uint8_t *p_entry = p_start; p_entry < p_end; p_entry += sizeof(esp_partition_info_t)) {
  157. esp_partition_info_t entry;
  158. // copying to RAM instead of using pointer to flash to avoid any chance of TOCTOU due to cache miss
  159. // when flash encryption is used
  160. memcpy(&entry, p_entry, sizeof(entry));
  161. #if CONFIG_PARTITION_TABLE_MD5
  162. if (entry.magic == ESP_PARTITION_MAGIC_MD5) {
  163. md5_part = p_entry;
  164. break;
  165. }
  166. #endif
  167. if (entry.magic != ESP_PARTITION_MAGIC) {
  168. break;
  169. }
  170. #if CONFIG_PARTITION_TABLE_MD5
  171. MD5Update(&context, (void *)&entry, sizeof(entry));
  172. #endif
  173. // allocate new linked list item and populate it with data from partition table
  174. partition_list_item_t* item = (partition_list_item_t*) malloc(sizeof(partition_list_item_t));
  175. item->info.address = entry.pos.offset;
  176. item->info.size = entry.pos.size;
  177. item->info.type = entry.type;
  178. item->info.subtype = entry.subtype;
  179. item->info.encrypted = entry.flags & PART_FLAG_ENCRYPTED;
  180. if (!esp_flash_encryption_enabled()) {
  181. /* If flash encryption is not turned on, no partitions should be treated as encrypted */
  182. item->info.encrypted = false;
  183. } else if (entry.type == PART_TYPE_APP
  184. || (entry.type == PART_TYPE_DATA && entry.subtype == PART_SUBTYPE_DATA_OTA)
  185. || (entry.type == PART_TYPE_DATA && entry.subtype == PART_SUBTYPE_DATA_NVS_KEYS)) {
  186. /* If encryption is turned on, all app partitions and OTA data
  187. are always encrypted */
  188. item->info.encrypted = true;
  189. }
  190. // item->info.label is initialized by calloc, so resulting string will be null terminated
  191. strncpy(item->info.label, (const char*) entry.label, sizeof(item->info.label) - 1);
  192. // add it to the list
  193. if (last == NULL) {
  194. SLIST_INSERT_HEAD(&new_partitions_list, item, next);
  195. } else {
  196. SLIST_INSERT_AFTER(last, item, next);
  197. }
  198. last = item;
  199. }
  200. #if CONFIG_PARTITION_TABLE_MD5
  201. if (md5_part == NULL) {
  202. ESP_LOGE(TAG, "No MD5 found in partition table");
  203. err = ESP_ERR_NOT_FOUND;
  204. } else {
  205. stored_md5 = md5_part + 16;
  206. MD5Final(calc_md5, &context);
  207. ESP_LOG_BUFFER_HEXDUMP("calculated md5", calc_md5, MD5_DIGEST_LEN, ESP_LOG_VERBOSE);
  208. ESP_LOG_BUFFER_HEXDUMP("stored md5", stored_md5, MD5_DIGEST_LEN, ESP_LOG_VERBOSE);
  209. if (memcmp(calc_md5, stored_md5, MD5_DIGEST_LEN) != 0) {
  210. ESP_LOGE(TAG, "Partition table MD5 mismatch");
  211. err = ESP_ERR_INVALID_STATE;
  212. } else {
  213. ESP_LOGD(TAG, "Partition table MD5 verified");
  214. }
  215. }
  216. #endif
  217. if (err == ESP_OK) {
  218. /* Don't copy the list to the static variable unless it's verified */
  219. s_partition_list = new_partitions_list;
  220. } else {
  221. /* Otherwise, free all the memory we just allocated */
  222. partition_list_item_t *it = new_partitions_list.slh_first;
  223. while (it) {
  224. partition_list_item_t *next = it->next.sle_next;
  225. free(it);
  226. it = next;
  227. }
  228. }
  229. spi_flash_munmap(handle);
  230. return ESP_OK;
  231. }
  232. void esp_partition_iterator_release(esp_partition_iterator_t iterator)
  233. {
  234. // iterator == NULL is okay
  235. free(iterator);
  236. }
  237. const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator)
  238. {
  239. assert(iterator != NULL);
  240. return iterator->info;
  241. }
  242. const esp_partition_t *esp_partition_verify(const esp_partition_t *partition)
  243. {
  244. assert(partition != NULL);
  245. const char *label = (strlen(partition->label) > 0) ? partition->label : NULL;
  246. esp_partition_iterator_t it = esp_partition_find(partition->type,
  247. partition->subtype,
  248. label);
  249. while (it != NULL) {
  250. const esp_partition_t *p = esp_partition_get(it);
  251. /* Can't memcmp() whole structure here as padding contents may be different */
  252. if (p->address == partition->address
  253. && partition->size == p->size
  254. && partition->encrypted == p->encrypted) {
  255. esp_partition_iterator_release(it);
  256. return p;
  257. }
  258. it = esp_partition_next(it);
  259. }
  260. esp_partition_iterator_release(it);
  261. return NULL;
  262. }
  263. esp_err_t esp_partition_read(const esp_partition_t* partition,
  264. size_t src_offset, void* dst, size_t size)
  265. {
  266. assert(partition != NULL);
  267. if (src_offset > partition->size) {
  268. return ESP_ERR_INVALID_ARG;
  269. }
  270. if (src_offset + size > partition->size) {
  271. return ESP_ERR_INVALID_SIZE;
  272. }
  273. if (!partition->encrypted) {
  274. return spi_flash_read(partition->address + src_offset, dst, size);
  275. } else {
  276. /* Encrypted partitions need to be read via a cache mapping */
  277. const void *buf;
  278. spi_flash_mmap_handle_t handle;
  279. esp_err_t err;
  280. err = esp_partition_mmap(partition, src_offset, size,
  281. SPI_FLASH_MMAP_DATA, &buf, &handle);
  282. if (err != ESP_OK) {
  283. return err;
  284. }
  285. memcpy(dst, buf, size);
  286. spi_flash_munmap(handle);
  287. return ESP_OK;
  288. }
  289. }
  290. esp_err_t esp_partition_write(const esp_partition_t* partition,
  291. size_t dst_offset, const void* src, size_t size)
  292. {
  293. assert(partition != NULL);
  294. if (dst_offset > partition->size) {
  295. return ESP_ERR_INVALID_ARG;
  296. }
  297. if (dst_offset + size > partition->size) {
  298. return ESP_ERR_INVALID_SIZE;
  299. }
  300. dst_offset = partition->address + dst_offset;
  301. if (partition->encrypted) {
  302. return spi_flash_write_encrypted(dst_offset, src, size);
  303. } else {
  304. return spi_flash_write(dst_offset, src, size);
  305. }
  306. }
  307. esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
  308. size_t start_addr, size_t size)
  309. {
  310. assert(partition != NULL);
  311. if (start_addr > partition->size) {
  312. return ESP_ERR_INVALID_ARG;
  313. }
  314. if (start_addr + size > partition->size) {
  315. return ESP_ERR_INVALID_SIZE;
  316. }
  317. if (size % SPI_FLASH_SEC_SIZE != 0) {
  318. return ESP_ERR_INVALID_SIZE;
  319. }
  320. if (start_addr % SPI_FLASH_SEC_SIZE != 0) {
  321. return ESP_ERR_INVALID_ARG;
  322. }
  323. return spi_flash_erase_range(partition->address + start_addr, size);
  324. }
  325. /*
  326. * Note: current implementation ignores the possibility of multiple regions in the same partition being
  327. * mapped. Reference counting and address space re-use is delegated to spi_flash_mmap.
  328. *
  329. * If this becomes a performance issue (i.e. if we need to map multiple regions within the partition),
  330. * we can add esp_partition_mmapv which will accept an array of offsets and sizes, and return array of
  331. * mmaped pointers, and a single handle for all these regions.
  332. */
  333. esp_err_t esp_partition_mmap(const esp_partition_t* partition, uint32_t offset, uint32_t size,
  334. spi_flash_mmap_memory_t memory,
  335. const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
  336. {
  337. assert(partition != NULL);
  338. if (offset > partition->size) {
  339. return ESP_ERR_INVALID_ARG;
  340. }
  341. if (offset + size > partition->size) {
  342. return ESP_ERR_INVALID_SIZE;
  343. }
  344. size_t phys_addr = partition->address + offset;
  345. // offset within 64kB block
  346. size_t region_offset = phys_addr & 0xffff;
  347. size_t mmap_addr = phys_addr & 0xffff0000;
  348. esp_err_t rc = spi_flash_mmap(mmap_addr, size+region_offset, memory, out_ptr, out_handle);
  349. // adjust returned pointer to point to the correct offset
  350. if (rc == ESP_OK) {
  351. *out_ptr = (void*) (((ptrdiff_t) *out_ptr) + region_offset);
  352. }
  353. return rc;
  354. }
  355. esp_err_t esp_partition_get_sha256(const esp_partition_t *partition, uint8_t *sha_256)
  356. {
  357. return bootloader_common_get_sha256_of_partition(partition->address, partition->size, partition->type, sha_256);
  358. }
  359. bool esp_partition_check_identity(const esp_partition_t *partition_1, const esp_partition_t *partition_2)
  360. {
  361. uint8_t sha_256[2][HASH_LEN] = { 0 };
  362. if (esp_partition_get_sha256(partition_1, sha_256[0]) == ESP_OK &&
  363. esp_partition_get_sha256(partition_2, sha_256[1]) == ESP_OK) {
  364. if (memcmp(sha_256[0], sha_256[1], HASH_LEN) == 0) {
  365. // The partitions are identity
  366. return true;
  367. }
  368. }
  369. return false;
  370. }