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 		.block_size = 32768, /* increase erase size */
49 		.read_buffer = large_read_buffer,
50 		.prog_buffer = large_prog_buffer,
51 		.lookahead_buffer = large_lookahead_buffer,
52 	},
53 };
54 struct fs_mount_t testfs_large_mnt = {
55 	.type = FS_LITTLEFS,
56 	.fs_data = &large,
57 	.storage_dev = (void *)LARGE_PARTITION_ID,
58 	.mnt_point = TESTFS_MNT_POINT_LARGE,
59 };
60 
61 #endif /* CONFIG_APP_TEST_CUSTOM */
62 
testfs_lfs_wipe_partition(const struct fs_mount_t * mp)63 int testfs_lfs_wipe_partition(const struct fs_mount_t *mp)
64 {
65 	unsigned int id = (uintptr_t)mp->storage_dev;
66 	const struct flash_area *pfa;
67 	int rc = flash_area_open(id, &pfa);
68 
69 	if (rc < 0) {
70 		TC_PRINT("Error accessing flash area %u [%d]\n",
71 			 id, rc);
72 		return TC_FAIL;
73 	}
74 
75 	TC_PRINT("Erasing %zu (0x%zx) bytes\n", pfa->fa_size, pfa->fa_size);
76 	rc = flash_area_flatten(pfa, 0, pfa->fa_size);
77 	(void)flash_area_close(pfa);
78 
79 	if (rc < 0) {
80 		TC_PRINT("Error wiping flash area %u [%d]\n",
81 			 id, rc);
82 		return TC_FAIL;
83 	}
84 
85 	TC_PRINT("Wiped flash area %u for %s\n", id, mp->mnt_point);
86 	return TC_PASS;
87 }
88