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 <daniel.degrasse@nxp.com>
This commit is contained in:
Daniel DeGrasse 2024-05-28 17:56:03 +00:00 committed by Henrik Brix Andersen
commit 179f254c7e
4 changed files with 85 additions and 76 deletions

View file

@ -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;
@ -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

View file

@ -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);

View file

@ -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 = {
@ -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;

View file

@ -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);