Skip to content

Commit 6f86b85

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 7a65336 commit 6f86b85

File tree

1 file changed

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

1 file changed

+55
-0
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,62 @@
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_PAGE_LAYOUT)
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 int 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+
while (address < FLASH_ERASE_END_ADDR) {
28+
rc = flash_get_page_info_by_offs(fdev, address, &page_info);
29+
if (rc < 0) {
30+
return rc;
31+
}
32+
33+
#if defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
34+
/* Lacking an explicit erase, just write all ones to the pages */
35+
uint8_t empty[page_info.size];
36+
37+
memset(empty, 0xff, page_info.size);
38+
39+
TC_PRINT("Setting %d at %ld to 0xff\n", page_info.size, page_info.start_offset);
40+
rc = flash_write(fdev, page_info.start_offset, empty, page_info.size);
41+
if (rc < 0) {
42+
return rc;
43+
}
44+
#else
45+
TC_PRINT("Erasing %d at %ld\n", page_info.size, page_info.start_offset);
46+
rc = flash_erase(fdev, page_info.start_offset, page_info.size);
47+
if (rc < 0) {
48+
return rc;
49+
}
50+
#endif
51+
52+
address += page_info.size;
53+
}
54+
55+
return 0;
56+
}
57+
58+
/* Low priority to ensure we run after any flash drivers are initialized */
59+
SYS_INIT(erase_flash, POST_KERNEL, 100);
60+
61+
#endif
62+
863
ZTEST_SUITE(secure_storage_psa_its, NULL, NULL, NULL, NULL, NULL);
964

1065
#ifdef CONFIG_SECURE_STORAGE

0 commit comments

Comments
 (0)