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