|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "esp_partition.h" |
| 8 | +#include "spi_flash_mmap.h" |
| 9 | +#include "esp_tee_flash.h" |
| 10 | +#include "esp_log.h" |
| 11 | + |
| 12 | +static __attribute__((unused)) const char *TAG = "partition_tee"; |
| 13 | + |
| 14 | +const esp_partition_t *esp_partition_find_first(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label) |
| 15 | +{ |
| 16 | + static esp_partition_t internal_partition = {0}; |
| 17 | + esp_partition_info_t partition_info = {0}; |
| 18 | + |
| 19 | + esp_err_t err = esp_tee_flash_find_partition(type, subtype, label, &partition_info); |
| 20 | + if (err != ESP_OK) { |
| 21 | + return NULL; |
| 22 | + } |
| 23 | + |
| 24 | + // Populate the internal partition structure |
| 25 | + internal_partition.flash_chip = NULL; |
| 26 | + internal_partition.type = partition_info.type; |
| 27 | + internal_partition.subtype = partition_info.subtype; |
| 28 | + internal_partition.address = partition_info.pos.offset; |
| 29 | + internal_partition.size = partition_info.pos.size; |
| 30 | + internal_partition.erase_size = SPI_FLASH_SEC_SIZE; |
| 31 | + strncpy(internal_partition.label, (char *)partition_info.label, sizeof(internal_partition.label) - 1); |
| 32 | + internal_partition.encrypted = partition_info.flags & PART_FLAG_ENCRYPTED; |
| 33 | + internal_partition.readonly = partition_info.flags & PART_FLAG_READONLY; |
| 34 | + |
| 35 | + return &internal_partition; |
| 36 | +} |
| 37 | + |
| 38 | +esp_err_t esp_partition_read(const esp_partition_t *partition, |
| 39 | + size_t src_offset, void *dst, size_t size) |
| 40 | +{ |
| 41 | + assert(partition != NULL); |
| 42 | + if (src_offset > partition->size) { |
| 43 | + return ESP_ERR_INVALID_ARG; |
| 44 | + } |
| 45 | + if (size > partition->size - src_offset) { |
| 46 | + return ESP_ERR_INVALID_SIZE; |
| 47 | + } |
| 48 | + |
| 49 | + if (!partition->encrypted) { |
| 50 | + return esp_tee_flash_read(partition->address + src_offset, dst, size, false); |
| 51 | + } |
| 52 | + |
| 53 | + const void *buf = esp_tee_flash_mmap(partition->address + src_offset, size); |
| 54 | + if (buf == NULL) { |
| 55 | + return ESP_ERR_NO_MEM; |
| 56 | + } |
| 57 | + |
| 58 | + memcpy(dst, buf, size); |
| 59 | + esp_tee_flash_munmap(buf); |
| 60 | + return ESP_OK; |
| 61 | +} |
| 62 | + |
| 63 | +esp_err_t esp_partition_write(const esp_partition_t *partition, |
| 64 | + size_t dst_offset, const void *src, size_t size) |
| 65 | +{ |
| 66 | + assert(partition != NULL); |
| 67 | + if (partition->readonly) { |
| 68 | + return ESP_ERR_NOT_ALLOWED; |
| 69 | + } |
| 70 | + if (dst_offset > partition->size) { |
| 71 | + return ESP_ERR_INVALID_ARG; |
| 72 | + } |
| 73 | + if (size > partition->size - dst_offset) { |
| 74 | + return ESP_ERR_INVALID_SIZE; |
| 75 | + } |
| 76 | + dst_offset = partition->address + dst_offset; |
| 77 | + if (!partition->encrypted) { |
| 78 | + return esp_tee_flash_write(dst_offset, (void *)src, size, false); |
| 79 | + } |
| 80 | + |
| 81 | + return esp_tee_flash_write(dst_offset, (void *)src, size, true); |
| 82 | +} |
| 83 | + |
| 84 | +esp_err_t esp_partition_read_raw(const esp_partition_t *partition, |
| 85 | + size_t src_offset, void *dst, size_t size) |
| 86 | +{ |
| 87 | + assert(partition != NULL); |
| 88 | + if (src_offset > partition->size) { |
| 89 | + return ESP_ERR_INVALID_ARG; |
| 90 | + } |
| 91 | + if (size > partition->size - src_offset) { |
| 92 | + return ESP_ERR_INVALID_SIZE; |
| 93 | + } |
| 94 | + |
| 95 | + return esp_tee_flash_read(partition->address + src_offset, dst, size, false); |
| 96 | +} |
| 97 | + |
| 98 | +esp_err_t esp_partition_write_raw(const esp_partition_t *partition, |
| 99 | + size_t dst_offset, const void *src, size_t size) |
| 100 | +{ |
| 101 | + assert(partition != NULL); |
| 102 | + if (partition->readonly) { |
| 103 | + return ESP_ERR_NOT_ALLOWED; |
| 104 | + } |
| 105 | + if (dst_offset > partition->size) { |
| 106 | + return ESP_ERR_INVALID_ARG; |
| 107 | + } |
| 108 | + if (size > partition->size - dst_offset) { |
| 109 | + return ESP_ERR_INVALID_SIZE; |
| 110 | + } |
| 111 | + dst_offset = partition->address + dst_offset; |
| 112 | + |
| 113 | + return esp_tee_flash_write(dst_offset, (void *)src, size, false); |
| 114 | +} |
| 115 | + |
| 116 | +esp_err_t esp_partition_erase_range(const esp_partition_t *partition, |
| 117 | + size_t offset, size_t size) |
| 118 | +{ |
| 119 | + assert(partition != NULL); |
| 120 | + if (partition->readonly) { |
| 121 | + return ESP_ERR_NOT_ALLOWED; |
| 122 | + } |
| 123 | + if (offset > partition->size) { |
| 124 | + return ESP_ERR_INVALID_ARG; |
| 125 | + } |
| 126 | + if (size > partition->size - offset) { |
| 127 | + return ESP_ERR_INVALID_SIZE; |
| 128 | + } |
| 129 | + |
| 130 | + return esp_tee_flash_erase_range(partition->address + offset, size); |
| 131 | +} |
| 132 | + |
| 133 | +uint32_t esp_partition_get_main_flash_sector_size(void) |
| 134 | +{ |
| 135 | + return SPI_FLASH_SEC_SIZE; |
| 136 | +} |
0 commit comments