Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions tests/subsys/fs/fat_fs_api/prj_sdmmc.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CONFIG_FILE_SYSTEM=y
CONFIG_LOG=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_ZTEST=y
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_ZTEST_STACK_SIZE=2048
83 changes: 83 additions & 0 deletions tests/subsys/fs/fat_fs_api/src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@
*/

#include "test_fat.h"
#ifdef CONFIG_DISK_DRIVER_FLASH
#include <zephyr/storage/flash_map.h>
#else
#include <zephyr/storage/disk_access.h>
#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;
Expand All @@ -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, &sector_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, &sector_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
1 change: 1 addition & 0 deletions tests/subsys/fs/fat_fs_api/src/test_fat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
76 changes: 0 additions & 76 deletions tests/subsys/fs/fat_fs_api/src/test_fat_mkfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

#include "test_fat.h"
#include <ff.h>
#ifdef CONFIG_DISK_DRIVER_FLASH
#include <zephyr/storage/flash_map.h>
#else
#include <zephyr/storage/disk_access.h>
#endif

/* mounting info */
static struct fs_mount_t fatfs_mnt = {
Expand All @@ -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, &sector_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, &sector_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;
Expand Down
1 change: 1 addition & 0 deletions tests/subsys/fs/fat_fs_api/src/test_fat_mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions tests/subsys/fs/fat_fs_api/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ tests:
filesystem.fat.api.mmc:
extra_args: CONF_FILE="prj_mmc.conf"
filter: dt_compat_enabled("zephyr,mmc-disk")
filesystem.fat.api.sdmmc:
extra_args: CONF_FILE="prj_sdmmc.conf"
filter: dt_compat_enabled("zephyr,sdmmc-disk")
filesystem.fat.ram.api:
platform_allow:
- native_sim
Expand Down