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
55 changes: 55 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,62 @@
#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_PAGE_LAYOUT)

#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.

Suggested change
#define MAX_NUM_PAGES 2

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 int erase_flash(void)
{
int rc;
off_t address = FIXED_PARTITION_OFFSET(storage_partition);
struct flash_pages_info page_info;

while (address < FLASH_ERASE_END_ADDR) {
rc = flash_get_page_info_by_offs(fdev, address, &page_info);
if (rc < 0) {
return rc;
}

#if defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid using Flash API Kconfig in code that is not Flash API. Make test/subsystem specific Kconfig dependent on the Flash API kconfig.

/* Lacking an explicit erase, just write all ones to the pages */
uint8_t empty[page_info.size];
Copy link
Contributor

Choose a reason for hiding this comment

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

VLAs are not allowed


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

memset(empty, 0xff, page_info.size);

TC_PRINT("Setting %d at %ld to 0xff\n", page_info.size, page_info.start_offset);
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
TC_PRINT("Setting %d at %ld to 0xff\n", page_info.size, page_info.start_offset);
TC_PRINT("Setting %zu at %ld to 0xff\n", page_info.size, page_info.start_offset);

rc = flash_write(fdev, page_info.start_offset, empty, page_info.size);
if (rc < 0) {
return rc;
}
#else
TC_PRINT("Erasing %d at %ld\n", page_info.size, page_info.start_offset);
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
TC_PRINT("Erasing %d at %ld\n", page_info.size, page_info.start_offset);
TC_PRINT("Erasing %zu at %ld\n", page_info.size, page_info.start_offset);

rc = flash_erase(fdev, page_info.start_offset, page_info.size);
if (rc < 0) {
return rc;
}
#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 */


address += page_info.size;
}

return 0;
}

/* Low priority to ensure we run after any flash drivers are initialized */
SYS_INIT(erase_flash, POST_KERNEL, 100);

#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
Loading