./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
117 / 131
89.3%
Branches:
40 / 52
76.9%
Functions:
10 / 12
83.3%

kfsw-platform/src/storage.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stddef.h>
3 #include <stdint.h>
4 #include <string.h>
5
6 #include <zephyr/device.h>
7 #include <zephyr/devicetree.h>
8 #include <zephyr/fs/fs.h>
9 #include <zephyr/fs/littlefs.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/storage/flash_map.h>
12 #include <zephyr/sys/util.h>
13
14 #include <kfsw/platform/storage.h>
15
16 #define KFSW_STORAGE_PARTITION_NODE DT_CHOSEN(kfsw_storage_partition)
17 #define KFSW_STORAGE_PARTITION_ID DT_FIXED_PARTITION_ID(KFSW_STORAGE_PARTITION_NODE)
18 #define KFSW_STORAGE_ERASE_SCAN_CHUNK_SIZE 64U
19
20 BUILD_ASSERT(DT_NODE_EXISTS(KFSW_STORAGE_PARTITION_NODE),
21 "kfsw,storage-partition must select a fixed partition");
22
23 #if CONFIG_KFSW_STORAGE_ZMS_DISK
24 #include <zephyr/storage/disk_access.h>
25
26 #include "storage_zms_disk.h"
27 FS_LITTLEFS_DECLARE_CUSTOM_CONFIG(kfsw_storage_littlefs, 4, KFSW_STORAGE_ZMS_SECTOR_SIZE,
28 KFSW_STORAGE_ZMS_SECTOR_SIZE, KFSW_STORAGE_ZMS_SECTOR_SIZE, 8);
29 #define STORAGE_DEVICE ((void *)KFSW_STORAGE_ZMS_DISK_NAME)
30 #define STORAGE_FLAGS (FS_MOUNT_FLAG_NO_FORMAT | FS_MOUNT_FLAG_USE_DISK_ACCESS)
31 #else
32 FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(kfsw_storage_littlefs);
33 #define STORAGE_DEVICE ((void *)KFSW_STORAGE_PARTITION_ID)
34 #define STORAGE_FLAGS FS_MOUNT_FLAG_NO_FORMAT
35 #endif
36
37 static struct fs_mount_t kfsw_storage_mount_point = {
38 .type = FS_LITTLEFS,
39 .fs_data = &kfsw_storage_littlefs,
40 .storage_dev = STORAGE_DEVICE,
41 .mnt_point = KFSW_STORAGE_MOUNT_POINT,
42 .flags = STORAGE_FLAGS,
43 };
44
45 K_MUTEX_DEFINE(kfsw_storage_lock);
46
47 static bool kfsw_storage_initialized;
48 static bool kfsw_storage_ready;
49 static const char *kfsw_storage_backend = "unavailable";
50
51 128 static int storage_init_locked(void)
52 {
53 const struct flash_area *area;
54 const struct device *device;
55 int result;
56
57
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 60 times.
128 if (kfsw_storage_initialized) {
58 68 return 0;
59 }
60
61 60 result = flash_area_open(KFSW_STORAGE_PARTITION_ID, &area);
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (result != 0) {
63 ✗ return result;
64 }
65
66 60 device = flash_area_get_device(area);
67
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 60 times.
60 if ((device == NULL) || !device_is_ready(device)) {
68 ✗ flash_area_close(area);
69 ✗ return -ENODEV;
70 }
71
72 60 kfsw_storage_backend = device->name;
73 60 kfsw_storage_initialized = true;
74 60 flash_area_close(area);
75 60 return 0;
76 }
77
78 68 static int storage_partition_is_erased(bool *is_erased)
79 {
80 #if CONFIG_KFSW_STORAGE_ZMS_DISK
81 6 return kfsw_storage_zms_is_erased(is_erased);
82 #else
83 const struct flash_area *area;
84 uint8_t data[KFSW_STORAGE_ERASE_SCAN_CHUNK_SIZE];
85 62 size_t offset = 0U;
86 int result;
87
88 62 *is_erased = false;
89 62 result = flash_area_open(KFSW_STORAGE_PARTITION_ID, &area);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (result != 0) {
91 ✗ return result;
92 }
93
94 62 const uint8_t erased_value = flash_area_erased_val(area);
95
96
2/2
✓ Branch 0 taken 67586 times.
✓ Branch 1 taken 60 times.
67646 while (offset < area->fa_size) {
97 67586 const size_t read_size = MIN(sizeof(data), area->fa_size - offset);
98
99 67586 result = flash_area_read(area, (off_t)offset, data, read_size);
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67586 times.
67586 if (result != 0) {
101 ✗ flash_area_close(area);
102 ✗ return result;
103 }
104
105
2/2
✓ Branch 0 taken 4325378 times.
✓ Branch 1 taken 67584 times.
4392962 for (size_t index = 0U; index < read_size; index++) {
106
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4325376 times.
4325378 if (data[index] != erased_value) {
107 2 flash_area_close(area);
108 2 return 0;
109 }
110 }
111 67584 offset += read_size;
112 }
113
114 60 *is_erased = true;
115 60 flash_area_close(area);
116 60 return 0;
117 #endif
118 }
119
120 52 int kfsw_storage_init(void)
121 {
122 int result;
123
124 52 k_mutex_lock(&kfsw_storage_lock, K_FOREVER);
125 52 result = storage_init_locked();
126 52 k_mutex_unlock(&kfsw_storage_lock);
127 52 return result;
128 }
129
130 78 int kfsw_storage_mount(void)
131 {
132 bool is_erased;
133 int result;
134
135 78 k_mutex_lock(&kfsw_storage_lock, K_FOREVER);
136
137
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 76 times.
78 if (kfsw_storage_ready) {
138 2 result = 0;
139 2 goto out;
140 }
141
142 76 result = storage_init_locked();
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (result != 0) {
144 ✗ goto out;
145 }
146
147 76 kfsw_storage_mount_point.flags = STORAGE_FLAGS;
148 76 result = fs_mount(&kfsw_storage_mount_point);
149
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 68 times.
76 if (result == 0) {
150 8 kfsw_storage_ready = true;
151 8 goto out;
152 }
153
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (result != -EFAULT) {
155 ✗ goto out;
156 }
157
158 68 result = storage_partition_is_erased(&is_erased);
159
4/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 62 times.
68 if ((result != 0) || !is_erased) {
160
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (result == 0) {
161 4 result = -EFAULT;
162 }
163 6 goto out;
164 }
165
166 62 kfsw_storage_mount_point.flags = STORAGE_FLAGS & ~FS_MOUNT_FLAG_NO_FORMAT;
167 62 result = fs_mount(&kfsw_storage_mount_point);
168 62 kfsw_storage_mount_point.flags = STORAGE_FLAGS;
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (result == 0) {
170 62 kfsw_storage_ready = true;
171 }
172
173 ✗ out:
174 #if CONFIG_KFSW_STORAGE_ZMS_DISK
175
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (result != 0) {
176 4 bool force = true;
177
178 4 disk_access_ioctl(KFSW_STORAGE_ZMS_DISK_NAME, DISK_IOCTL_CTRL_DEINIT, &force);
179 }
180 #endif
181 78 k_mutex_unlock(&kfsw_storage_lock);
182 78 return result;
183 }
184
185 50 int kfsw_storage_unmount(void)
186 {
187 50 int result = 0;
188
189 50 k_mutex_lock(&kfsw_storage_lock, K_FOREVER);
190
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 36 times.
50 if (kfsw_storage_ready) {
191 14 result = fs_unmount(&kfsw_storage_mount_point);
192
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (result == 0) {
193 14 kfsw_storage_ready = false;
194 #if CONFIG_KFSW_STORAGE_ZMS_DISK
195 4 bool force = true;
196
197 4 result = disk_access_ioctl(KFSW_STORAGE_ZMS_DISK_NAME,
198 DISK_IOCTL_CTRL_DEINIT, &force);
199 #endif
200 }
201 }
202 50 k_mutex_unlock(&kfsw_storage_lock);
203 50 return result;
204 }
205
206 5838 bool kfsw_storage_is_ready(void)
207 {
208 bool ready;
209
210 5838 k_mutex_lock(&kfsw_storage_lock, K_FOREVER);
211 5838 ready = kfsw_storage_ready;
212 5838 k_mutex_unlock(&kfsw_storage_lock);
213 5838 return ready;
214 }
215
216 156 static int fill_info(struct kfsw_storage_info *info, const char *backend, const char *mount_point,
217 bool ready)
218 {
219 struct fs_statvfs statistics;
220 156 int result = 0;
221
222 156 memset(info, 0, sizeof(*info));
223 156 info->filesystem = "LittleFS";
224 156 info->backend = backend;
225 156 info->mount_point = mount_point;
226 156 info->ready = ready;
227
228
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 2 times.
156 if (ready) {
229 154 result = fs_statvfs(mount_point, &statistics);
230
1/2
✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
154 if (result == 0) {
231 154 info->total_bytes =
232 154 (uint64_t)statistics.f_frsize * (uint64_t)statistics.f_blocks;
233 154 info->free_bytes =
234 154 (uint64_t)statistics.f_frsize * (uint64_t)statistics.f_bfree;
235 }
236 }
237 156 return result;
238 }
239
240 152 int kfsw_storage_get_info(struct kfsw_storage_info *info)
241 {
242 int result;
243
244
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 150 times.
152 if (info == NULL) {
245 2 return -EINVAL;
246 }
247
248 150 k_mutex_lock(&kfsw_storage_lock, K_FOREVER);
249 result =
250 150 fill_info(info, kfsw_storage_backend, KFSW_STORAGE_MOUNT_POINT, kfsw_storage_ready);
251 150 k_mutex_unlock(&kfsw_storage_lock);
252 150 return result;
253 }
254
255 #if CONFIG_KFSW_STORAGE_TMP
256 #define KFSW_STORAGE_TMP_DISK_NODE DT_CHOSEN(kfsw_tmp_disk)
257 #define KFSW_STORAGE_TMP_DISK_NAME DT_PROP(KFSW_STORAGE_TMP_DISK_NODE, disk_name)
258 #define KFSW_STORAGE_TMP_SECTOR_SIZE DT_PROP(KFSW_STORAGE_TMP_DISK_NODE, sector_size)
259
260 /* On a disk, littlefs reads, programs and caches whole sectors. */
261 FS_LITTLEFS_DECLARE_CUSTOM_CONFIG(kfsw_tmp_littlefs, 4, KFSW_STORAGE_TMP_SECTOR_SIZE,
262 KFSW_STORAGE_TMP_SECTOR_SIZE, KFSW_STORAGE_TMP_SECTOR_SIZE, 8);
263
264 static struct fs_mount_t kfsw_tmp_mount_point = {
265 .type = FS_LITTLEFS,
266 .fs_data = &kfsw_tmp_littlefs,
267 .storage_dev = (void *)KFSW_STORAGE_TMP_DISK_NAME,
268 .mnt_point = KFSW_STORAGE_TMP_MOUNT_POINT,
269 .flags = FS_MOUNT_FLAG_USE_DISK_ACCESS,
270 };
271
272 static bool kfsw_tmp_ready;
273
274 8 int kfsw_storage_tmp_mount(void)
275 {
276 8 int result = 0;
277
278 8 k_mutex_lock(&kfsw_storage_lock, K_FOREVER);
279
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 if (!kfsw_tmp_ready) {
280 /* RAM such as the STM32 SRAM2 keeps its contents across a reset. */
281 6 result = fs_mkfs(FS_LITTLEFS, (uintptr_t)KFSW_STORAGE_TMP_DISK_NAME,
282 &kfsw_tmp_littlefs, FS_MOUNT_FLAG_USE_DISK_ACCESS);
283
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (result == 0) {
284 6 result = fs_mount(&kfsw_tmp_mount_point);
285 }
286 6 kfsw_tmp_ready = result == 0;
287 }
288 8 k_mutex_unlock(&kfsw_storage_lock);
289 8 return result;
290 }
291
292 6 int kfsw_storage_get_tmp_info(struct kfsw_storage_info *info)
293 {
294 int result;
295
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (info == NULL) {
297 ✗ return -EINVAL;
298 }
299
300 6 k_mutex_lock(&kfsw_storage_lock, K_FOREVER);
301 6 result = fill_info(info, KFSW_STORAGE_TMP_DISK_NAME, KFSW_STORAGE_TMP_MOUNT_POINT,
302 kfsw_tmp_ready);
303 6 k_mutex_unlock(&kfsw_storage_lock);
304 6 return result;
305 }
306 #else
307 ✗ int kfsw_storage_tmp_mount(void)
308 {
309 ✗ return -ENOTSUP;
310 }
311
312 ✗ int kfsw_storage_get_tmp_info(struct kfsw_storage_info *info)
313 {
314 ARG_UNUSED(info);
315 ✗ return -ENOTSUP;
316 }
317 #endif
318