From 179f254c7ee791ab8b83a04d22ff199a5fb6deba Mon Sep 17 00:00:00 2001 From: Daniel DeGrasse Date: Tue, 28 May 2024 17:56:03 +0000 Subject: [PATCH] tests: fs: fat_fs_api: wipe partition header before test Wipe partition header prior to fatfs testsuite starting. This insures that the disk will be in an unformatted state at the start of the test, so the first fs_mount() calls will fail as expected. Signed-off-by: Daniel DeGrasse --- tests/subsys/fs/fat_fs_api/src/common.c | 83 +++++++++++++++++++ tests/subsys/fs/fat_fs_api/src/test_fat.h | 1 + .../subsys/fs/fat_fs_api/src/test_fat_mkfs.c | 76 ----------------- .../subsys/fs/fat_fs_api/src/test_fat_mount.c | 1 + 4 files changed, 85 insertions(+), 76 deletions(-) diff --git a/tests/subsys/fs/fat_fs_api/src/common.c b/tests/subsys/fs/fat_fs_api/src/common.c index fd4aa46b308..348aa8442e8 100644 --- a/tests/subsys/fs/fat_fs_api/src/common.c +++ b/tests/subsys/fs/fat_fs_api/src/common.c @@ -6,12 +6,24 @@ */ #include "test_fat.h" +#ifdef CONFIG_DISK_DRIVER_FLASH +#include +#else +#include +#endif /* FatFs work area */ FATFS fat_fs; struct fs_file_t filep; const char test_str[] = "hello world!"; +/* For large disks, we only send 1024 erase requests + * This assumption relies on the fact that any filesystem headers will be + * stored within this range, and is made to improve execution time of this + * test + */ +#define MAX_ERASES 1024 + int check_file_dir_exists(const char *path) { int res; @@ -22,3 +34,74 @@ int check_file_dir_exists(const char *path) return !res; } + +#ifdef CONFIG_DISK_DRIVER_FLASH +int wipe_partition(void) +{ + /* In this test the first partition on flash device is used for FAT */ + unsigned int id = 0; + const struct flash_area *pfa; + int rc = flash_area_open(id, &pfa); + + if (rc < 0) { + TC_PRINT("Error accessing flash area %u [%d]\n", + id, rc); + return TC_FAIL; + } + + TC_PRINT("Erasing %zu (0x%zx) bytes\n", pfa->fa_size, pfa->fa_size); + rc = flash_area_flatten(pfa, 0, pfa->fa_size); + (void)flash_area_close(pfa); + + if (rc < 0) { + TC_PRINT("Error wiping flash area %u [%d]\n", + id, rc); + return TC_FAIL; + } + + return TC_PASS; +} +#else +static uint8_t erase_buffer[4096] = { 0 }; + +int wipe_partition(void) +{ + uint32_t sector_size; + uint32_t sector_count; + uint32_t sector_wr_jmp; + uint32_t sector_wr_size; + + if (disk_access_init(DISK_NAME)) { + TC_PRINT("Failed to init disk "DISK_NAME"\n"); + return TC_FAIL; + } + if (disk_access_ioctl(DISK_NAME, DISK_IOCTL_GET_SECTOR_COUNT, §or_count)) { + TC_PRINT("Failed to get disk "DISK_NAME" sector count\n"); + return TC_FAIL; + } + if (disk_access_ioctl(DISK_NAME, DISK_IOCTL_GET_SECTOR_SIZE, §or_size)) { + TC_PRINT("Failed to get disk "DISK_NAME" sector size\n"); + return TC_FAIL; + } + + if (sector_size > ARRAY_SIZE(erase_buffer)) { + TC_PRINT("Predefined \"erase_buffer\" to small to handle single sector\n"); + return TC_FAIL; + } + + sector_wr_size = MIN(sector_size, ARRAY_SIZE(erase_buffer)); + sector_wr_jmp = sector_wr_size / sector_wr_size; + TC_PRINT("For "DISK_NAME" using sector write size %"PRIu32" to write %"PRIu32" at once\n", + sector_wr_size, sector_wr_jmp); + + for (uint32_t sector_idx = 0; sector_idx < sector_count; sector_idx += sector_wr_jmp) { + if (disk_access_write(DISK_NAME, erase_buffer, sector_idx, 1)) { + TC_PRINT("Failed to \"erase\" sector %"PRIu32" to "DISK_NAME"\n", + sector_idx); + return TC_FAIL; + } + } + + return TC_PASS; +} +#endif diff --git a/tests/subsys/fs/fat_fs_api/src/test_fat.h b/tests/subsys/fs/fat_fs_api/src/test_fat.h index 8048ef084cf..c5ef9b12b26 100644 --- a/tests/subsys/fs/fat_fs_api/src/test_fat.h +++ b/tests/subsys/fs/fat_fs_api/src/test_fat.h @@ -39,6 +39,7 @@ extern const char test_str[]; extern FATFS fat_fs; int check_file_dir_exists(const char *path); +int wipe_partition(void); void test_fat_mount(void); void test_fat_unmount(void); diff --git a/tests/subsys/fs/fat_fs_api/src/test_fat_mkfs.c b/tests/subsys/fs/fat_fs_api/src/test_fat_mkfs.c index 5af5b53788c..ad8248f54dd 100644 --- a/tests/subsys/fs/fat_fs_api/src/test_fat_mkfs.c +++ b/tests/subsys/fs/fat_fs_api/src/test_fat_mkfs.c @@ -7,11 +7,6 @@ #include "test_fat.h" #include -#ifdef CONFIG_DISK_DRIVER_FLASH -#include -#else -#include -#endif /* mounting info */ static struct fs_mount_t fatfs_mnt = { @@ -30,77 +25,6 @@ int fs_mkfs_flags; const char *some_file_path = "/"DISK_NAME":/SOME"; const char *other_dir_path = "/"DISK_NAME":/OTHER"; -#ifdef CONFIG_DISK_DRIVER_FLASH -static int wipe_partition(void) -{ - /* In this test the first partition on flash device is used for FAT */ - unsigned int id = 0; - const struct flash_area *pfa; - int rc = flash_area_open(id, &pfa); - - if (rc < 0) { - TC_PRINT("Error accessing flash area %u [%d]\n", - id, rc); - return TC_FAIL; - } - - TC_PRINT("Erasing %zu (0x%zx) bytes\n", pfa->fa_size, pfa->fa_size); - rc = flash_area_flatten(pfa, 0, pfa->fa_size); - (void)flash_area_close(pfa); - - if (rc < 0) { - TC_PRINT("Error wiping flash area %u [%d]\n", - id, rc); - return TC_FAIL; - } - - return TC_PASS; -} -#else -static uint8_t erase_buffer[4096] = { 0 }; - -static int wipe_partition(void) -{ - uint32_t sector_size; - uint32_t sector_count; - uint32_t sector_wr_jmp; - uint32_t sector_wr_size; - - if (disk_access_init(DISK_NAME)) { - TC_PRINT("Failed to init disk "DISK_NAME"\n"); - return TC_FAIL; - } - if (disk_access_ioctl(DISK_NAME, DISK_IOCTL_GET_SECTOR_COUNT, §or_count)) { - TC_PRINT("Failed to get disk "DISK_NAME" sector count\n"); - return TC_FAIL; - } - if (disk_access_ioctl(DISK_NAME, DISK_IOCTL_GET_SECTOR_SIZE, §or_size)) { - TC_PRINT("Failed to get disk "DISK_NAME" sector size\n"); - return TC_FAIL; - } - - if (sector_size > ARRAY_SIZE(erase_buffer)) { - TC_PRINT("Predefined \"erase_buffer\" to small to handle single sector\n"); - return TC_FAIL; - } - - sector_wr_size = MIN(sector_size, ARRAY_SIZE(erase_buffer)); - sector_wr_jmp = sector_wr_size / sector_wr_size; - TC_PRINT("For "DISK_NAME" using sector write size "PRIu32" to write "PRIu32" at once\n", - sector_wr_size, sector_wr_jmp); - - for (uint32_t sector_idx = 0; sector_idx < sector_count; sector_idx += sector_wr_jmp) { - if (disk_access_write(DISK_NAME, erase_buffer, sector_idx, 1)) { - TC_PRINT("Faield to \"erase\" sector "PRIu32" to "DISK_NAME"\n", - sector_idx); - return TC_FAIL; - } - } - - return TC_PASS; -} -#endif - ZTEST(fat_fs_mkfs, test_mkfs_simple) { int ret; diff --git a/tests/subsys/fs/fat_fs_api/src/test_fat_mount.c b/tests/subsys/fs/fat_fs_api/src/test_fat_mount.c index d0c7de813c3..93748c852f7 100644 --- a/tests/subsys/fs/fat_fs_api/src/test_fat_mount.c +++ b/tests/subsys/fs/fat_fs_api/src/test_fat_mount.c @@ -85,6 +85,7 @@ void test_fat_unmount(void) void test_fat_mount(void) { + zassert_true(wipe_partition() == 0); zassert_false(test_unmount() == TC_PASS); zassert_true(test_mount_no_format() == TC_PASS); zassert_true(test_mount_rd_only_no_sys() == TC_PASS);