Skip to content

Commit 7d49f69

Browse files
committed
ci(esp_tee): Add tests for verifying behaviour for illegal flash accesses (SPI1)
1 parent c23714f commit 7d49f69

File tree

6 files changed

+346
-12
lines changed

6 files changed

+346
-12
lines changed

components/esp_tee/test_apps/tee_cli_app/sdkconfig.defaults

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
CONFIG_SECURE_ENABLE_TEE=y
33
CONFIG_SECURE_TEE_IRAM_SIZE=0x9000
44

5+
# Enabling flash protection over SPI1
6+
CONFIG_SECURE_TEE_EXT_FLASH_MEMPROT_SPI1=y
7+
58
# Custom partition table
69
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
710
CONFIG_PARTITION_TABLE_TWO_OTA_TEE=y

components/esp_tee/test_apps/tee_test_fw/main/test_esp_tee_flash_prot.c

Lines changed: 233 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -21,8 +21,11 @@
2121
#include "secure_service_num.h"
2222

2323
#include "unity.h"
24+
#include "ccomp_timer.h"
2425

2526
#define BOOT_COUNT_NAMESPACE "boot_count"
27+
#define TEST_PART_LABEL "custom"
28+
#define TEST_BUF_SZ 256
2629

2730
static const char *TAG = "test_esp_tee_flash_prot";
2831

@@ -106,6 +109,66 @@ TEST_CASE_MULTIPLE_STAGES("Test REE-TEE isolation: Flash - SPI0 (esp_partition_m
106109
test_initial_boot, test_esp_partition_mmap_api, test_esp_partition_mmap_api,
107110
test_esp_partition_mmap_api, test_esp_partition_mmap_api);
108111

112+
#if CONFIG_SECURE_TEE_EXT_FLASH_MEMPROT_SPI1
113+
static void test_esp_partition_api_r(const esp_partition_t *part)
114+
{
115+
TEST_ASSERT_NOT_NULL(part);
116+
uint8_t buf_r[128];
117+
memset(buf_r, 0x00, sizeof(buf_r));
118+
TEST_ESP_ERR(ESP_FAIL, esp_partition_read(part, 0x00, buf_r, sizeof(buf_r)));
119+
}
120+
121+
static void test_esp_partition_api_w(const esp_partition_t *part)
122+
{
123+
TEST_ASSERT_NOT_NULL(part);
124+
uint8_t buf_w[128];
125+
memset(buf_w, 0xA5, sizeof(buf_w));
126+
TEST_ESP_OK(esp_partition_write(part, 0x00, buf_w, sizeof(buf_w)));
127+
}
128+
129+
static void test_esp_partition_api_e(const esp_partition_t *part)
130+
{
131+
TEST_ASSERT_NOT_NULL(part);
132+
TEST_ESP_OK(esp_partition_erase_range(part, 0x00, SPI_FLASH_SEC_SIZE));
133+
}
134+
135+
static void test_esp_partition_api(void)
136+
{
137+
uint8_t boot_count = get_boot_count_from_nvs();
138+
boot_count++;
139+
set_boot_count_in_nvs(boot_count);
140+
141+
const esp_partition_t *part = NULL;
142+
switch (boot_count) {
143+
case 2:
144+
part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_TEE_0, NULL);
145+
test_esp_partition_api_r(part);
146+
break;
147+
case 3:
148+
part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_TEE_1, NULL);
149+
test_esp_partition_api_w(part);
150+
break;
151+
case 4:
152+
part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_TEE_SEC_STORAGE, NULL);
153+
test_esp_partition_api_w(part);
154+
break;
155+
case 5:
156+
part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_TEE_OTA, NULL);
157+
test_esp_partition_api_e(part);
158+
break;
159+
default:
160+
TEST_FAIL_MESSAGE("Unexpected stage");
161+
break;
162+
}
163+
164+
esp_restart();
165+
}
166+
167+
TEST_CASE_MULTIPLE_STAGES("Test REE-TEE isolation: Flash - SPI1 (esp_partition)", "[flash_prot][timeout=60]",
168+
test_initial_boot, test_esp_partition_api, test_esp_partition_api,
169+
test_esp_partition_api, test_esp_partition_api);
170+
#endif
171+
109172
/* ---------------------------------------------- API family 2: spi_flash ------------------------------------------------- */
110173

111174
static void test_spi_flash_mmap_api(void)
@@ -149,3 +212,172 @@ static void test_spi_flash_mmap_api(void)
149212
TEST_CASE_MULTIPLE_STAGES("Test REE-TEE isolation: Flash - SPI0 (spi_flash_mmap)", "[flash_prot][timeout=60]",
150213
test_initial_boot, test_spi_flash_mmap_api, test_spi_flash_mmap_api,
151214
test_spi_flash_mmap_api);
215+
216+
/* ---------------------------------------------- API family 3: esp_flash ------------------------------------------------- */
217+
218+
#if CONFIG_SECURE_TEE_EXT_FLASH_MEMPROT_SPI1
219+
static void test_esp_flash_api_r(uint32_t paddr)
220+
{
221+
uint8_t buf_r[128];
222+
memset(buf_r, 0x00, sizeof(buf_r));
223+
TEST_ESP_ERR(ESP_FAIL, esp_flash_read(NULL, buf_r, paddr, sizeof(buf_r)));
224+
}
225+
226+
static void test_esp_flash_api_w(uint32_t paddr)
227+
{
228+
uint8_t buf_w[128];
229+
memset(buf_w, 0xA5, sizeof(buf_w));
230+
TEST_ESP_OK(esp_flash_write(NULL, buf_w, paddr, sizeof(buf_w)));
231+
}
232+
233+
static void test_esp_flash_api_e(uint32_t paddr)
234+
{
235+
TEST_ESP_OK(esp_flash_erase_region(NULL, paddr, SPI_FLASH_SEC_SIZE));
236+
}
237+
238+
static void test_esp_flash_api(void)
239+
{
240+
uint8_t boot_count = get_boot_count_from_nvs();
241+
boot_count++;
242+
set_boot_count_in_nvs(boot_count);
243+
244+
const esp_partition_t *part = NULL;
245+
246+
switch (boot_count) {
247+
case 2:
248+
part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_TEE_0, NULL);
249+
TEST_ASSERT_NOT_NULL(part);
250+
test_esp_flash_api_w(part->address);
251+
break;
252+
case 3:
253+
part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_TEE_1, NULL);
254+
TEST_ASSERT_NOT_NULL(part);
255+
test_esp_flash_api_r(part->address);
256+
break;
257+
case 4:
258+
part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_TEE_SEC_STORAGE, NULL);
259+
TEST_ASSERT_NOT_NULL(part);
260+
test_esp_flash_api_e(part->address);
261+
break;
262+
case 5:
263+
part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_TEE_OTA, NULL);
264+
TEST_ASSERT_NOT_NULL(part);
265+
test_esp_flash_api_w(part->address);
266+
break;
267+
default:
268+
TEST_FAIL_MESSAGE("Unexpected stage");
269+
break;
270+
}
271+
272+
esp_restart();
273+
}
274+
275+
TEST_CASE_MULTIPLE_STAGES("Test REE-TEE isolation: Flash - SPI1 (esp_flash)", "[flash_prot][timeout=60]",
276+
test_initial_boot, test_esp_flash_api, test_esp_flash_api, test_esp_flash_api,
277+
test_esp_flash_api);
278+
279+
/* ---------------------------------------------- API family 4: esp_rom ------------------------------------------------- */
280+
281+
static IRAM_ATTR void test_esp_rom_spiflash_api_r(uint32_t paddr)
282+
{
283+
uint32_t buf_r[32];
284+
memset(buf_r, 0x00, sizeof(buf_r));
285+
esp_rom_spiflash_result_t rc = esp_rom_spiflash_read(paddr, buf_r, sizeof(buf_r));
286+
TEST_ASSERT_EQUAL_HEX(ESP_ROM_SPIFLASH_RESULT_OK, rc);
287+
ESP_LOG_BUFFER_HEXDUMP(TAG, buf_r, sizeof(buf_r), ESP_LOG_INFO);
288+
}
289+
290+
static IRAM_ATTR void test_esp_rom_spiflash_api_w(uint32_t paddr)
291+
{
292+
uint32_t buf_w[32];
293+
memset(buf_w, 0xA5, sizeof(buf_w));
294+
spi_flash_disable_interrupts_caches_and_other_cpu();
295+
esp_rom_spiflash_result_t rc = esp_rom_spiflash_write(paddr, buf_w, sizeof(buf_w));
296+
spi_flash_enable_interrupts_caches_and_other_cpu();
297+
TEST_ASSERT_EQUAL_HEX(ESP_ROM_SPIFLASH_RESULT_OK, rc);
298+
}
299+
300+
static IRAM_ATTR void test_esp_rom_spiflash_api_e(uint32_t paddr)
301+
{
302+
spi_flash_disable_interrupts_caches_and_other_cpu();
303+
esp_rom_spiflash_result_t rc = esp_rom_spiflash_erase_area(paddr, SPI_FLASH_SEC_SIZE);
304+
spi_flash_enable_interrupts_caches_and_other_cpu();
305+
TEST_ASSERT_EQUAL_HEX(ESP_ROM_SPIFLASH_RESULT_OK, rc);
306+
}
307+
308+
static void test_esp_rom_spiflash_api(void)
309+
{
310+
uint8_t boot_count = get_boot_count_from_nvs();
311+
boot_count++;
312+
set_boot_count_in_nvs(boot_count);
313+
314+
const esp_partition_t *part = NULL;
315+
316+
switch (boot_count) {
317+
case 2:
318+
part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_TEE_0, NULL);
319+
TEST_ASSERT_NOT_NULL(part);
320+
test_esp_rom_spiflash_api_r(part->address);
321+
TEST_FAIL_MESSAGE("System fault should have been generated");
322+
break;
323+
case 3:
324+
part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_TEE_1, NULL);
325+
TEST_ASSERT_NOT_NULL(part);
326+
test_esp_rom_spiflash_api_w(part->address);
327+
TEST_FAIL_MESSAGE("System fault should have been generated");
328+
break;
329+
case 4:
330+
part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_TEE_SEC_STORAGE, NULL);
331+
TEST_ASSERT_NOT_NULL(part);
332+
test_esp_rom_spiflash_api_e(part->address);
333+
TEST_FAIL_MESSAGE("System fault should have been generated");
334+
break;
335+
case 5:
336+
part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_TEE_OTA, NULL);
337+
TEST_ASSERT_NOT_NULL(part);
338+
test_esp_rom_spiflash_api_w(part->address);
339+
TEST_FAIL_MESSAGE("System fault should have been generated");
340+
break;
341+
default:
342+
TEST_FAIL_MESSAGE("Unexpected stage");
343+
break;
344+
}
345+
}
346+
347+
TEST_CASE_MULTIPLE_STAGES("Test REE-TEE isolation: Flash - SPI1 (esp_rom_spiflash)", "[flash_prot][timeout=60]",
348+
test_initial_boot, test_esp_rom_spiflash_api, test_esp_rom_spiflash_api,
349+
test_esp_rom_spiflash_api, test_esp_rom_spiflash_api);
350+
#endif
351+
352+
TEST_CASE("Test TEE flash read/write performance", "[flash_prot]")
353+
{
354+
const esp_partition_t *part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, TEST_PART_LABEL);
355+
TEST_ASSERT_NOT_NULL(part);
356+
357+
TEST_ESP_OK(esp_partition_erase_range(part, 0x00, part->size));
358+
TEST_ASSERT_TRUE((part->size % TEST_BUF_SZ) == 0);
359+
360+
ESP_LOGI(TAG, "R/W operations over a %luKB partition in %luB chunks...", part->size / 1024, TEST_BUF_SZ);
361+
362+
uint8_t buf_w[TEST_BUF_SZ];
363+
memset(buf_w, 0xA5, sizeof(buf_w));
364+
365+
float write_usec, read_usec;
366+
ccomp_timer_start();
367+
for (size_t offs = 0; offs < part->size; offs += TEST_BUF_SZ) {
368+
TEST_ESP_OK(esp_partition_write(part, offs, buf_w, TEST_BUF_SZ));
369+
}
370+
write_usec = ccomp_timer_stop();
371+
ESP_LOGI(TAG, "[Time taken] Write: %.2fus", write_usec);
372+
373+
uint8_t buf_r[TEST_BUF_SZ] = {};
374+
375+
ccomp_timer_start();
376+
for (size_t offs = 0; offs < part->size; offs += TEST_BUF_SZ) {
377+
TEST_ESP_OK(esp_partition_read(part, offs, buf_r, TEST_BUF_SZ));
378+
}
379+
read_usec = ccomp_timer_stop();
380+
381+
TEST_ASSERT_EQUAL_HEX8_ARRAY(buf_w, buf_r, TEST_BUF_SZ);
382+
ESP_LOGI(TAG, "[Time taken] Read: %.2fus", read_usec);
383+
}

components/esp_tee/test_apps/tee_test_fw/partitions.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ tee, app, tee_0, , 192K,
44
secure_storage, data, tee_sec_stg, , 64K,
55
factory, app, factory, , 512K,
66
nvs, data, nvs, , 24K,
7+
custom, data, , , 1M

components/esp_tee/test_apps/tee_test_fw/partitions_tee_ota.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ ota_0, app, ota_0, , 512K,
88
ota_1, app, ota_1, , 512K,
99
otadata, data, ota, , 8K,
1010
nvs, data, nvs, , 24K,
11+
custom, data, , , 1M

0 commit comments

Comments
 (0)