1 /*
2 * Copyright (c) 2019 Peter Bigot Consulting, LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/fs/littlefs.h>
9 #include <zephyr/storage/flash_map.h>
10 #include "testfs_lfs.h"
11
12 #define SMALL_PARTITION small_partition
13 #define SMALL_PARTITION_ID FIXED_PARTITION_ID(SMALL_PARTITION)
14
15 #define MEDIUM_PARTITION medium_partition
16 #define MEDIUM_PARTITION_ID FIXED_PARTITION_ID(MEDIUM_PARTITION)
17
18 #define LARGE_PARTITION large_partition
19 #define LARGE_PARTITION_ID FIXED_PARTITION_ID(LARGE_PARTITION)
20
21 FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(small);
22 struct fs_mount_t testfs_small_mnt = {
23 .type = FS_LITTLEFS,
24 .fs_data = &small,
25 .storage_dev = (void *)SMALL_PARTITION_ID,
26 .mnt_point = TESTFS_MNT_POINT_SMALL,
27 };
28
29 #if CONFIG_APP_TEST_CUSTOM
30 FS_LITTLEFS_DECLARE_CUSTOM_CONFIG(medium, 4, MEDIUM_IO_SIZE, MEDIUM_IO_SIZE,
31 MEDIUM_CACHE_SIZE, MEDIUM_LOOKAHEAD_SIZE);
32 struct fs_mount_t testfs_medium_mnt = {
33 .type = FS_LITTLEFS,
34 .fs_data = &medium,
35 .storage_dev = (void *)MEDIUM_PARTITION_ID,
36 .mnt_point = TESTFS_MNT_POINT_MEDIUM,
37 };
38
39 static uint8_t large_read_buffer[LARGE_CACHE_SIZE];
40 static uint8_t large_prog_buffer[LARGE_CACHE_SIZE];
41 static uint32_t large_lookahead_buffer[LARGE_LOOKAHEAD_SIZE / 4U];
42 static struct fs_littlefs large = {
43 .cfg = {
44 .read_size = LARGE_IO_SIZE,
45 .prog_size = LARGE_IO_SIZE,
46 .cache_size = LARGE_CACHE_SIZE,
47 .lookahead_size = LARGE_LOOKAHEAD_SIZE,
48 .read_buffer = large_read_buffer,
49 .prog_buffer = large_prog_buffer,
50 .lookahead_buffer = large_lookahead_buffer,
51 },
52 };
53 struct fs_mount_t testfs_large_mnt = {
54 .type = FS_LITTLEFS,
55 .fs_data = &large,
56 .storage_dev = (void *)LARGE_PARTITION_ID,
57 .mnt_point = TESTFS_MNT_POINT_LARGE,
58 };
59
60 #endif /* CONFIG_APP_TEST_CUSTOM */
61
testfs_lfs_wipe_partition(const struct fs_mount_t * mp)62 int testfs_lfs_wipe_partition(const struct fs_mount_t *mp)
63 {
64 unsigned int id = (uintptr_t)mp->storage_dev;
65 const struct flash_area *pfa;
66 int rc = flash_area_open(id, &pfa);
67
68 if (rc < 0) {
69 TC_PRINT("Error accessing flash area %u [%d]\n",
70 id, rc);
71 return TC_FAIL;
72 }
73
74 TC_PRINT("Erasing %zu (0x%zx) bytes\n", pfa->fa_size, pfa->fa_size);
75 rc = flash_area_flatten(pfa, 0, pfa->fa_size);
76 (void)flash_area_close(pfa);
77
78 if (rc < 0) {
79 TC_PRINT("Error wiping flash area %u [%d]\n",
80 id, rc);
81 return TC_FAIL;
82 }
83
84 TC_PRINT("Wiped flash area %u for %s\n", id, mp->mnt_point);
85 return TC_PASS;
86 }
87