Skip to content

Commit 4b8038f

Browse files
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 <[email protected]>
1 parent b1f2eb9 commit 4b8038f

File tree

4 files changed

+85
-76
lines changed

4 files changed

+85
-76
lines changed

tests/subsys/fs/fat_fs_api/src/common.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@
66
*/
77

88
#include "test_fat.h"
9+
#ifdef CONFIG_DISK_DRIVER_FLASH
10+
#include <zephyr/storage/flash_map.h>
11+
#else
12+
#include <zephyr/storage/disk_access.h>
13+
#endif
914

1015
/* FatFs work area */
1116
FATFS fat_fs;
1217
struct fs_file_t filep;
1318
const char test_str[] = "hello world!";
1419

20+
/* For large disks, we only send 1024 erase requests
21+
* This assumption relies on the fact that any filesystem headers will be
22+
* stored within this range, and is made to improve execution time of this
23+
* test
24+
*/
25+
#define MAX_ERASES 1024
26+
1527
int check_file_dir_exists(const char *path)
1628
{
1729
int res;
@@ -22,3 +34,74 @@ int check_file_dir_exists(const char *path)
2234

2335
return !res;
2436
}
37+
38+
#ifdef CONFIG_DISK_DRIVER_FLASH
39+
int wipe_partition(void)
40+
{
41+
/* In this test the first partition on flash device is used for FAT */
42+
unsigned int id = 0;
43+
const struct flash_area *pfa;
44+
int rc = flash_area_open(id, &pfa);
45+
46+
if (rc < 0) {
47+
TC_PRINT("Error accessing flash area %u [%d]\n",
48+
id, rc);
49+
return TC_FAIL;
50+
}
51+
52+
TC_PRINT("Erasing %zu (0x%zx) bytes\n", pfa->fa_size, pfa->fa_size);
53+
rc = flash_area_flatten(pfa, 0, pfa->fa_size);
54+
(void)flash_area_close(pfa);
55+
56+
if (rc < 0) {
57+
TC_PRINT("Error wiping flash area %u [%d]\n",
58+
id, rc);
59+
return TC_FAIL;
60+
}
61+
62+
return TC_PASS;
63+
}
64+
#else
65+
static uint8_t erase_buffer[4096] = { 0 };
66+
67+
int wipe_partition(void)
68+
{
69+
uint32_t sector_size;
70+
uint32_t sector_count;
71+
uint32_t sector_wr_jmp;
72+
uint32_t sector_wr_size;
73+
74+
if (disk_access_init(DISK_NAME)) {
75+
TC_PRINT("Failed to init disk "DISK_NAME"\n");
76+
return TC_FAIL;
77+
}
78+
if (disk_access_ioctl(DISK_NAME, DISK_IOCTL_GET_SECTOR_COUNT, &sector_count)) {
79+
TC_PRINT("Failed to get disk "DISK_NAME" sector count\n");
80+
return TC_FAIL;
81+
}
82+
if (disk_access_ioctl(DISK_NAME, DISK_IOCTL_GET_SECTOR_SIZE, &sector_size)) {
83+
TC_PRINT("Failed to get disk "DISK_NAME" sector size\n");
84+
return TC_FAIL;
85+
}
86+
87+
if (sector_size > ARRAY_SIZE(erase_buffer)) {
88+
TC_PRINT("Predefined \"erase_buffer\" to small to handle single sector\n");
89+
return TC_FAIL;
90+
}
91+
92+
sector_wr_size = MIN(sector_size, ARRAY_SIZE(erase_buffer));
93+
sector_wr_jmp = sector_wr_size / sector_wr_size;
94+
TC_PRINT("For "DISK_NAME" using sector write size %"PRIu32" to write %"PRIu32" at once\n",
95+
sector_wr_size, sector_wr_jmp);
96+
97+
for (uint32_t sector_idx = 0; sector_idx < sector_count; sector_idx += sector_wr_jmp) {
98+
if (disk_access_write(DISK_NAME, erase_buffer, sector_idx, 1)) {
99+
TC_PRINT("Failed to \"erase\" sector %"PRIu32" to "DISK_NAME"\n",
100+
sector_idx);
101+
return TC_FAIL;
102+
}
103+
}
104+
105+
return TC_PASS;
106+
}
107+
#endif

tests/subsys/fs/fat_fs_api/src/test_fat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern const char test_str[];
3939
extern FATFS fat_fs;
4040

4141
int check_file_dir_exists(const char *path);
42+
int wipe_partition(void);
4243

4344
void test_fat_mount(void);
4445
void test_fat_unmount(void);

tests/subsys/fs/fat_fs_api/src/test_fat_mkfs.c

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77

88
#include "test_fat.h"
99
#include <ff.h>
10-
#ifdef CONFIG_DISK_DRIVER_FLASH
11-
#include <zephyr/storage/flash_map.h>
12-
#else
13-
#include <zephyr/storage/disk_access.h>
14-
#endif
1510

1611
/* mounting info */
1712
static struct fs_mount_t fatfs_mnt = {
@@ -30,77 +25,6 @@ int fs_mkfs_flags;
3025
const char *some_file_path = "/"DISK_NAME":/SOME";
3126
const char *other_dir_path = "/"DISK_NAME":/OTHER";
3227

33-
#ifdef CONFIG_DISK_DRIVER_FLASH
34-
static int wipe_partition(void)
35-
{
36-
/* In this test the first partition on flash device is used for FAT */
37-
unsigned int id = 0;
38-
const struct flash_area *pfa;
39-
int rc = flash_area_open(id, &pfa);
40-
41-
if (rc < 0) {
42-
TC_PRINT("Error accessing flash area %u [%d]\n",
43-
id, rc);
44-
return TC_FAIL;
45-
}
46-
47-
TC_PRINT("Erasing %zu (0x%zx) bytes\n", pfa->fa_size, pfa->fa_size);
48-
rc = flash_area_flatten(pfa, 0, pfa->fa_size);
49-
(void)flash_area_close(pfa);
50-
51-
if (rc < 0) {
52-
TC_PRINT("Error wiping flash area %u [%d]\n",
53-
id, rc);
54-
return TC_FAIL;
55-
}
56-
57-
return TC_PASS;
58-
}
59-
#else
60-
static uint8_t erase_buffer[4096] = { 0 };
61-
62-
static int wipe_partition(void)
63-
{
64-
uint32_t sector_size;
65-
uint32_t sector_count;
66-
uint32_t sector_wr_jmp;
67-
uint32_t sector_wr_size;
68-
69-
if (disk_access_init(DISK_NAME)) {
70-
TC_PRINT("Failed to init disk "DISK_NAME"\n");
71-
return TC_FAIL;
72-
}
73-
if (disk_access_ioctl(DISK_NAME, DISK_IOCTL_GET_SECTOR_COUNT, &sector_count)) {
74-
TC_PRINT("Failed to get disk "DISK_NAME" sector count\n");
75-
return TC_FAIL;
76-
}
77-
if (disk_access_ioctl(DISK_NAME, DISK_IOCTL_GET_SECTOR_SIZE, &sector_size)) {
78-
TC_PRINT("Failed to get disk "DISK_NAME" sector size\n");
79-
return TC_FAIL;
80-
}
81-
82-
if (sector_size > ARRAY_SIZE(erase_buffer)) {
83-
TC_PRINT("Predefined \"erase_buffer\" to small to handle single sector\n");
84-
return TC_FAIL;
85-
}
86-
87-
sector_wr_size = MIN(sector_size, ARRAY_SIZE(erase_buffer));
88-
sector_wr_jmp = sector_wr_size / sector_wr_size;
89-
TC_PRINT("For "DISK_NAME" using sector write size "PRIu32" to write "PRIu32" at once\n",
90-
sector_wr_size, sector_wr_jmp);
91-
92-
for (uint32_t sector_idx = 0; sector_idx < sector_count; sector_idx += sector_wr_jmp) {
93-
if (disk_access_write(DISK_NAME, erase_buffer, sector_idx, 1)) {
94-
TC_PRINT("Faield to \"erase\" sector "PRIu32" to "DISK_NAME"\n",
95-
sector_idx);
96-
return TC_FAIL;
97-
}
98-
}
99-
100-
return TC_PASS;
101-
}
102-
#endif
103-
10428
ZTEST(fat_fs_mkfs, test_mkfs_simple)
10529
{
10630
int ret;

tests/subsys/fs/fat_fs_api/src/test_fat_mount.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void test_fat_unmount(void)
8585

8686
void test_fat_mount(void)
8787
{
88+
zassert_true(wipe_partition() == 0);
8889
zassert_false(test_unmount() == TC_PASS);
8990
zassert_true(test_mount_no_format() == TC_PASS);
9091
zassert_true(test_mount_rd_only_no_sys() == TC_PASS);

0 commit comments

Comments
 (0)