Skip to content

Commit ec97597

Browse files
tests: subsys: secure_storage: Erase flash before write once test
Where possible (i.e. not running in NS), erase the flash before testing the write once secure storage API. Signed-off-by: Pete Johanson <[email protected]>
1 parent f1c2ce4 commit ec97597

File tree

1 file changed

+46
-0
lines changed
  • tests/subsys/secure_storage/psa/its/src

1 file changed

+46
-0
lines changed

tests/subsys/secure_storage/psa/its/src/main.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,49 @@
44
#include <zephyr/ztest.h>
55
#include <psa/internal_trusted_storage.h>
66

7+
#include <zephyr/types.h>
8+
#include <zephyr/storage/flash_map.h>
9+
#include <zephyr/drivers/flash.h>
10+
711
/* The flash must be erased after this test suite is run for the write-once entry test to pass. */
12+
13+
#if !defined(CONFIG_BUILD_WITH_TFM) && defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
14+
15+
#define MAX_NUM_PAGES 2
16+
static const struct device *const fdev = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
17+
18+
#define FLASH_ERASE_END_ADDR \
19+
(FIXED_PARTITION_OFFSET(storage_partition) + FIXED_PARTITION_SIZE(storage_partition))
20+
21+
static void erase_flash(void)
22+
{
23+
int rc;
24+
off_t address = FIXED_PARTITION_OFFSET(storage_partition);
25+
struct flash_pages_info page_info;
26+
27+
#if defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
28+
const struct flash_parameters *fparam = flash_get_parameters(fdev);
29+
30+
if (!(flash_params_get_erase_cap(fparam) & FLASH_ERASE_C_EXPLICIT)) {
31+
return;
32+
}
33+
#endif
34+
35+
for (int i = 0; i < MAX_NUM_PAGES && address < FLASH_ERASE_END_ADDR; i++) {
36+
rc = flash_get_page_info_by_offs(fdev, address, &page_info);
37+
38+
zassert_equal(rc, 0, "should succeed");
39+
40+
TC_PRINT("Erasing %d at %ld\n", page_info.size, page_info.start_offset);
41+
rc = flash_erase(fdev, page_info.start_offset, page_info.size);
42+
zassert_equal(rc, 0, "should succeed");
43+
44+
address += page_info.size;
45+
}
46+
}
47+
48+
#endif
49+
850
ZTEST_SUITE(secure_storage_psa_its, NULL, NULL, NULL, NULL, NULL);
951

1052
#ifdef CONFIG_SECURE_STORAGE
@@ -118,6 +160,10 @@ ZTEST(secure_storage_psa_its, test_write_once_flag)
118160
const uint8_t data[MAX_DATA_SIZE] = {};
119161
struct psa_storage_info_t info;
120162

163+
#if !defined(CONFIG_BUILD_WITH_TFM) && defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
164+
erase_flash();
165+
#endif
166+
121167
ret = psa_its_set(uid, sizeof(data), data, PSA_STORAGE_FLAG_WRITE_ONCE);
122168
zassert_equal(ret, PSA_SUCCESS, "%s%d", (ret == PSA_ERROR_NOT_PERMITTED) ?
123169
"Has the flash been erased since this test ran? " : "", ret);

0 commit comments

Comments
 (0)