Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tests/subsys/secure_storage/psa/its/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,49 @@
#include <zephyr/ztest.h>
#include <psa/internal_trusted_storage.h>

#include <zephyr/types.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/drivers/flash.h>

/* The flash must be erased after this test suite is run for the write-once entry test to pass. */

#if !defined(CONFIG_BUILD_WITH_TFM) && defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)

#define MAX_NUM_PAGES 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you come up with this value? Is it even needed?

static const struct device *const fdev = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));

#define FLASH_ERASE_END_ADDR \
(FIXED_PARTITION_OFFSET(storage_partition) + FIXED_PARTITION_SIZE(storage_partition))

static void erase_flash(void)
{
int rc;
off_t address = FIXED_PARTITION_OFFSET(storage_partition);
struct flash_pages_info page_info;

#if defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
const struct flash_parameters *fparam = flash_get_parameters(fdev);

if (!(flash_params_get_erase_cap(fparam) & FLASH_ERASE_C_EXPLICIT)) {
Copy link
Member

@dsseng dsseng Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe in case we have no erase operation (e.g. nRF54 RRAM and MRAM) we should fully overwrite the region with zeroes or ones? (edit: or here means I did not verify which of this does Secure Storage consider to be empty, there's obviously a single variant it does) That would also erase the data to make sure we start from a known-empty state.

native_sim can also simulate such NVM model IIRC, so it should be testable even without such HW. Please take a look at native simulator flash Kconfig file for details, or if it's missing such a variant you might need to open an issue

return;
}
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif
#endif /* CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE */


for (int i = 0; i < MAX_NUM_PAGES && address < FLASH_ERASE_END_ADDR; i++) {
rc = flash_get_page_info_by_offs(fdev, address, &page_info);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking nit to unify the code style here

Suggested change

zassert_equal(rc, 0, "should succeed");

TC_PRINT("Erasing %d at %ld\n", page_info.size, page_info.start_offset);
rc = flash_erase(fdev, page_info.start_offset, page_info.size);
zassert_equal(rc, 0, "should succeed");

address += page_info.size;
}
}

#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif
#endif /* !CONFIG_BUILD_WITH_TFM && CONFIG_FLASH_HAS_EXPLICIT_ERASE */


ZTEST_SUITE(secure_storage_psa_its, NULL, NULL, NULL, NULL, NULL);

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

#if !defined(CONFIG_BUILD_WITH_TFM) && defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
erase_flash();
#endif

ret = psa_its_set(uid, sizeof(data), data, PSA_STORAGE_FLAG_WRITE_ONCE);
zassert_equal(ret, PSA_SUCCESS, "%s%d", (ret == PSA_ERROR_NOT_PERMITTED) ?
"Has the flash been erased since this test ran? " : "", ret);
Expand Down
Loading