Skip to content

Commit c9f7bcd

Browse files
committed
feat(esp_tee): Support the nvs_flash for the ESP-TEE build
1 parent aaebc37 commit c9f7bcd

File tree

10 files changed

+197
-6
lines changed

10 files changed

+197
-6
lines changed

components/esp_partition/CMakeLists.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
idf_build_get_property(esp_tee_build ESP_TEE_BUILD)
2+
13
# bootloader build simplified version
24
if(BOOTLOADER_BUILD)
35
set(srcs "partition_bootloader.c")
@@ -10,7 +12,19 @@ idf_component_register(SRCS "${srcs}"
1012
REQUIRES ${reqs}
1113
PRIV_REQUIRES ${priv_reqs})
1214

13-
# regular, non bootloader build
15+
# esp-tee build simplified version
16+
elseif(esp_tee_build)
17+
set(srcs "partition_tee.c")
18+
set(reqs "spi_flash")
19+
set(priv_reqs "tee_flash_mgr")
20+
21+
idf_component_register(SRCS "${srcs}"
22+
INCLUDE_DIRS "include"
23+
PRIV_INCLUDE_DIRS ${private_include_dirs}
24+
REQUIRES ${reqs}
25+
PRIV_REQUIRES ${priv_reqs})
26+
27+
# regular, OS build
1428
else()
1529
set(srcs "partition.c")
1630
set(priv_reqs esp_system spi_flash partition_table)
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}

components/esp_tee/subproject/main/ld/esp32c6/esp_tee.ld.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ SECTIONS
148148
*(.rodata_desc .rodata_desc.*) /* Should be the first. TEE App version info. DO NOT PUT ANYTHING BEFORE IT! */
149149
*(.rodata .rodata.*)
150150
*(.srodata .srodata.*)
151+
*(.gcc_except_table .gcc_except_table.*)
151152
_tee_xip_data_end = ABSOLUTE(.);
152153
} > flash_data_seg
153154

@@ -213,6 +214,12 @@ SECTIONS
213214
*libmbedcrypto.a:*(.literal .text .literal.* .text.*)
214215
/* HMAC-DS layer */
215216
*libesp_security.a:*(.literal .text .literal.* .text.*)
217+
/* NVS flash and related modules */
218+
*libnvs_flash.a:*(.literal .text .literal.* .text.*)
219+
*libstdc++.a:*(.literal .text .literal.* .text.*)
220+
*libgcc.a:*(.literal .text .literal.* .text.*)
221+
/* esp_partition API */
222+
*libesp_partition.a:*(.literal .text .literal.* .text.*)
216223
/* TEE attestation module */
217224
*libattestation.a:*(.literal .text .literal.* .text.*)
218225
*json_generator.a:*(.literal .text .literal.* .text.*)

components/mbedtls/esp_tee/esp_tee_mbedtls.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/aes/dma/esp_aes.c"
5252
"${COMPONENT_DIR}/port/aes/dma/esp_aes_dma_core.c")
5353

5454
target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/aes/esp_aes_common.c"
55+
"${COMPONENT_DIR}/port/aes/esp_aes_xts.c"
5556
"${COMPONENT_DIR}/port/aes/esp_aes_gcm.c")
5657

5758
# SHA implementation

components/mbedtls/esp_tee/esp_tee_mbedtls_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
#define MBEDTLS_CIPHER_C
3737
#define MBEDTLS_AES_C
3838
#define MBEDTLS_GCM_C
39+
#define MBEDTLS_AES_ALT
3940
#define MBEDTLS_GCM_ALT
41+
#define MBEDTLS_CIPHER_MODE_XTS
4042

4143
#define MBEDTLS_ASN1_WRITE_C
4244
#define MBEDTLS_ASN1_PARSE_C

components/nvs_flash/CMakeLists.txt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
idf_build_get_property(esp_tee_build ESP_TEE_BUILD)
2+
13
if(BOOTLOADER_BUILD)
24
# bootloader build simplified version
35
set(srcs "src/nvs_bootloader.c"
@@ -13,8 +15,33 @@ if(BOOTLOADER_BUILD)
1315
PRIV_INCLUDE_DIRS "private_include"
1416
)
1517

18+
elseif(esp_tee_build)
19+
# esp-tee build simplified version
20+
set(srcs "src/nvs_api.cpp"
21+
"src/nvs_item_hash_list.cpp"
22+
"src/nvs_page.cpp"
23+
"src/nvs_pagemanager.cpp"
24+
"src/nvs_storage.cpp"
25+
"src/nvs_handle_simple.cpp"
26+
"src/nvs_handle_locked.cpp"
27+
"src/nvs_partition.cpp"
28+
"src/nvs_encrypted_partition.cpp"
29+
"src/nvs_partition_lookup.cpp"
30+
"src/nvs_partition_manager.cpp"
31+
"src/nvs_types.cpp"
32+
"src/nvs_platform.cpp")
33+
34+
set(requires esp_partition mbedtls)
35+
set(priv_requires spi_flash newlib)
36+
37+
idf_component_register(SRCS "${srcs}"
38+
REQUIRES "${requires}"
39+
PRIV_REQUIRES "${priv_requires}"
40+
INCLUDE_DIRS "include"
41+
PRIV_INCLUDE_DIRS "private_include")
42+
1643
else()
17-
# regular, non bootloader build
44+
# regular, OS build
1845
idf_build_get_property(target IDF_TARGET)
1946

2047
set(srcs "src/nvs_api.cpp"
@@ -66,4 +93,4 @@ else()
6693
target_compile_options(${COMPONENT_LIB} PUBLIC "-fno-analyzer")
6794
endif()
6895

69-
endif() #bootloader build
96+
endif() #non-OS build

components/nvs_flash/src/nvs_api.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ extern "C" esp_err_t nvs_flash_init_partition(const char *part_name)
129129
}
130130
Lock lock;
131131

132+
assert(nvs::Page::SEC_SIZE == esp_partition_get_main_flash_sector_size());
132133
return NVSPartitionManager::get_instance()->init_partition(part_name);
133134
}
134135

@@ -169,6 +170,7 @@ extern "C" esp_err_t nvs_flash_secure_init_partition(const char *part_name, nvs_
169170
}
170171
Lock lock;
171172

173+
assert(nvs::Page::SEC_SIZE == esp_partition_get_main_flash_sector_size());
172174
return NVSPartitionManager::get_instance()->secure_init_partition(part_name, cfg);
173175
}
174176

components/nvs_flash/src/nvs_memory_management.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
*/
66

77
#include <cstdlib>
8+
#if !ESP_TEE_BUILD
89
#include "esp_heap_caps.h"
10+
#endif
911

1012
#pragma once
1113

components/nvs_flash/src/nvs_page.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -15,7 +15,7 @@ namespace nvs {
1515

1616
Page::Page() : mPartition(nullptr) { }
1717

18-
const uint32_t nvs::Page::SEC_SIZE = esp_partition_get_main_flash_sector_size();
18+
const uint32_t nvs::Page::SEC_SIZE = 4096;
1919

2020
uint32_t Page::Header::calculateCrc32()
2121
{

components/nvs_flash/src/nvs_platform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
using namespace nvs;
99

10-
#ifdef LINUX_TARGET
10+
#if LINUX_TARGET || ESP_TEE_BUILD
1111
Lock::Lock() {}
1212
Lock::~Lock() {}
1313
esp_err_t nvs::Lock::init() {return ESP_OK;}

0 commit comments

Comments
 (0)