./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
132 / 157
84.1%
Branches:
85 / 106
80.2%
Functions:
9 / 10
90.0%

kfsw-platform/src/storage_zms_disk.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <string.h>
3
4 #include <zephyr/drivers/disk.h>
5 #include <zephyr/drivers/flash.h>
6 #include <zephyr/init.h>
7 #include <zephyr/kernel.h>
8 #include <zephyr/kvss/zms.h>
9 #include <zephyr/storage/disk_access.h>
10 #include <zephyr/storage/flash_map.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/sys/crc.h>
13 #include <zephyr/sys/util.h>
14
15 #include "storage_zms_disk.h"
16
17 #define ZMS_PARTITION DT_CHOSEN(kfsw_storage_partition)
18 #define SECTOR_COUNT CONFIG_KFSW_STORAGE_ZMS_SECTOR_COUNT
19 #define PHYSICAL_SECTOR_SIZE (DT_REG_SIZE(ZMS_PARTITION) / 2U)
20 #define ZMS_HEADER_SIZE 16U
21 #define ZMS_HEADER_METADATA 0x4201U
22
23 BUILD_ASSERT(IS_ENABLED(CONFIG_LITTLE_ENDIAN), "Pinned ZMS format uses native byte order");
24 BUILD_ASSERT(!IS_ENABLED(CONFIG_ZMS_ID_64BIT), "Recognition supports 32-bit ZMS IDs only");
25 BUILD_ASSERT(DT_REG_SIZE(ZMS_PARTITION) % 2U == 0U);
26 BUILD_ASSERT(SECTOR_COUNT *(KFSW_STORAGE_ZMS_SECTOR_SIZE + 32U) + 160U < PHYSICAL_SECTOR_SIZE,
27 "One physical sector must hold the whole logical live set");
28
29 static struct zms_fs store;
30 K_MUTEX_DEFINE(disk_lock);
31 static uint8_t scan_buffer[KFSW_STORAGE_ZMS_SECTOR_SIZE];
32
33 /* Pinned Zephyr v4.4.0: empty ATE at the end of each physical sector.
34 * Recognize only its framing, CRC and version; ZMS owns recovery and GC.
35 */
36 34190 static int recognize(const struct flash_area *area)
37 {
38 34190 const size_t write_size = flash_get_write_block_size(area->fa_dev);
39 34190 const size_t ate_size = ROUND_UP(ZMS_HEADER_SIZE, write_size);
40 uint8_t header[ZMS_HEADER_SIZE];
41 34190 bool recognized = false;
42 34190 bool blank = true;
43 int result;
44
45
2/2
✓ Branch 0 taken 4376066 times.
✓ Branch 1 taken 34188 times.
4410254 for (size_t offset = 0U; offset < area->fa_size; offset += sizeof(scan_buffer)) {
46 4376066 const size_t size = MIN(sizeof(scan_buffer), area->fa_size - offset);
47
48 4376066 result = flash_area_read(area, offset, scan_buffer, size);
49
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4376064 times.
4376066 if (result != 0) {
50 2 return result;
51 }
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4376064 times.
✗ for (size_t index = 0U; index < size; index++) {
53 ✗ blank &= scan_buffer[index] == flash_area_erased_val(area);
54 }
55 }
56
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 34160 times.
34188 if (blank) {
57 28 return 0;
58 }
59
60
2/2
✓ Branch 0 taken 68318 times.
✓ Branch 1 taken 34158 times.
102476 for (size_t sector = 0U; sector < 2U; sector++) {
61 68318 result = flash_area_read(area, (sector + 1U) * PHYSICAL_SECTOR_SIZE - ate_size,
62 header, sizeof(header));
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68318 times.
68318 if (result != 0) {
64 ✗ return result;
65 }
66
3/4
✓ Branch 1 taken 36414 times.
✓ Branch 2 taken 31904 times.
✓ Branch 3 taken 36414 times.
✗ Branch 4 not taken.
104732 if (header[0] != crc8_ccitt(0xff, header + 1U, sizeof(header) - 1U) ||
67
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36414 times.
72828 sys_get_le16(header + 2U) != UINT16_MAX ||
68 36414 sys_get_le32(header + 4U) != UINT32_MAX) {
69 31904 continue;
70 }
71
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 36412 times.
36414 if (sys_get_le32(header + 12U) != ZMS_HEADER_METADATA) {
72 2 return -EPROTONOSUPPORT;
73 }
74 36412 recognized = true;
75 }
76
2/2
✓ Branch 0 taken 34152 times.
✓ Branch 1 taken 6 times.
34158 return recognized ? 0 : -EFAULT;
77 }
78
79 34190 static int initialize(struct disk_info *disk)
80 {
81 const struct flash_area *area;
82 struct flash_pages_info page;
83 int result;
84
85 ARG_UNUSED(disk);
86 34190 k_mutex_lock(&disk_lock, K_FOREVER);
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34190 times.
34190 if (store.ready) {
88 ✗ result = 0;
89 ✗ goto out;
90 }
91 34190 result = flash_area_open(DT_FIXED_PARTITION_ID(ZMS_PARTITION), &area);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34190 times.
34190 if (result != 0) {
93 ✗ goto out;
94 }
95
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34190 times.
34190 if (!device_is_ready(area->fa_dev)) {
96 ✗ result = -ENODEV;
97 ✗ goto close;
98 }
99 34190 const size_t write_size = flash_get_write_block_size(area->fa_dev);
100
101
3/6
✓ Branch 0 taken 34190 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34190 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 34190 times.
34190 if (write_size == 0U || write_size > 32U || flash_area_erased_val(area) != 0xff) {
102 ✗ result = -ENOTSUP;
103 ✗ goto close;
104 }
105 /* Check every erase page, including the partition end, before mounting. */
106
2/2
✓ Branch 0 taken 68380 times.
✓ Branch 1 taken 34190 times.
102570 for (size_t offset = 0U; offset < area->fa_size; offset += page.size) {
107 68380 result = flash_get_page_info_by_offs(area->fa_dev, area->fa_off + offset, &page);
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68380 times.
68380 if (result != 0) {
109 ✗ goto close;
110 }
111
1/2
✓ Branch 0 taken 68380 times.
✗ Branch 1 not taken.
68380 if (page.start_offset != area->fa_off + offset ||
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68380 times.
68380 PHYSICAL_SECTOR_SIZE % page.size != 0U) {
113 ✗ result = -EINVAL;
114 ✗ goto close;
115 }
116 }
117 34190 result = recognize(area);
118
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 34180 times.
34190 if (result == 0) {
119 34180 memset(&store, 0, sizeof(store));
120 34180 store.flash_device = area->fa_dev;
121 34180 store.offset = area->fa_off;
122 34180 store.sector_size = PHYSICAL_SECTOR_SIZE;
123 34180 store.sector_count = 2U;
124 34180 result = zms_mount(&store);
125 }
126 10 close:
127 34190 flash_area_close(area);
128 34190 out:
129 34190 k_mutex_unlock(&disk_lock);
130 34190 return result;
131 }
132
133 ✗ static int status(struct disk_info *disk)
134 {
135 int result;
136
137 ARG_UNUSED(disk);
138 ✗ k_mutex_lock(&disk_lock, K_FOREVER);
139 ✗ result = store.ready ? DISK_STATUS_OK : DISK_STATUS_UNINIT;
140 ✗ k_mutex_unlock(&disk_lock);
141 ✗ return result;
142 }
143
144 434724 static int read_sector(uint8_t *data, uint32_t sector)
145 {
146 434724 ssize_t result = zms_get_data_length(&store, sector + 1U);
147
148
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 434536 times.
434724 if (result == -ENOENT) {
149 188 memset(data, 0xff, KFSW_STORAGE_ZMS_SECTOR_SIZE);
150 188 return 0;
151 }
152
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 434534 times.
434536 if (result < 0) {
153 2 return result;
154 }
155
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 434530 times.
434534 if (result != KFSW_STORAGE_ZMS_SECTOR_SIZE) {
156 4 return -EIO;
157 }
158 434530 result = zms_read(&store, sector + 1U, data, KFSW_STORAGE_ZMS_SECTOR_SIZE);
159
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 434526 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
434530 return result == KFSW_STORAGE_ZMS_SECTOR_SIZE ? 0 : (result < 0 ? result : -EIO);
160 }
161
162 452908 static bool valid_request(const void *data, uint32_t start, uint32_t count)
163 {
164
8/8
✓ Branch 0 taken 452906 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 452904 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 452902 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 452900 times.
✓ Branch 7 taken 2 times.
452908 return data != NULL && count != 0U && start < SECTOR_COUNT && count <= SECTOR_COUNT - start;
165 }
166
167 434626 static int read_disk(struct disk_info *disk, uint8_t *data, uint32_t start, uint32_t count)
168 {
169 434626 int result = 0;
170
171 ARG_UNUSED(disk);
172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 434626 times.
434626 if (!valid_request(data, start, count)) {
173 ✗ return -EINVAL;
174 }
175 434626 k_mutex_lock(&disk_lock, K_FOREVER);
176
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 434624 times.
434626 if (!store.ready) {
177 2 result = -EACCES;
178 2 goto out;
179 }
180
2/2
✓ Branch 0 taken 434626 times.
✓ Branch 1 taken 434614 times.
869240 for (uint32_t index = 0U; index < count; index++) {
181 434626 result = read_sector(data + index * KFSW_STORAGE_ZMS_SECTOR_SIZE, start + index);
182
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 434616 times.
434626 if (result != 0) {
183 10 break;
184 }
185 }
186 434614 out:
187 434626 k_mutex_unlock(&disk_lock);
188 434626 return result;
189 }
190
191 18282 static int write_disk(struct disk_info *disk, const uint8_t *data, uint32_t start, uint32_t count)
192 {
193 18282 int result = 0;
194
195 ARG_UNUSED(disk);
196
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 18274 times.
18282 if (!valid_request(data, start, count)) {
197 8 return -EINVAL;
198 }
199 18274 k_mutex_lock(&disk_lock, K_FOREVER);
200
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18272 times.
18274 if (!store.ready) {
201 2 result = -EACCES;
202 2 goto out;
203 }
204
2/2
✓ Branch 0 taken 18278 times.
✓ Branch 1 taken 1208 times.
19486 for (uint32_t index = 0U; index < count; index++) {
205 18278 ssize_t written = zms_write(&store, start + index + 1U,
206 18278 data + index * KFSW_STORAGE_ZMS_SECTOR_SIZE,
207 KFSW_STORAGE_ZMS_SECTOR_SIZE);
208
209
4/4
✓ Branch 0 taken 18274 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 17064 times.
✓ Branch 3 taken 1210 times.
18278 if (written != 0 && written != KFSW_STORAGE_ZMS_SECTOR_SIZE) {
210
1/2
✓ Branch 0 taken 17064 times.
✗ Branch 1 not taken.
17064 result = written < 0 ? written : -EIO;
211 /* A failed append may have partially programmed flash. Recover
212 * through mount before accepting any further disk operations.
213 */
214 17064 store.ready = false;
215 17064 break;
216 }
217 }
218 1208 out:
219 18274 k_mutex_unlock(&disk_lock);
220 18274 return result;
221 }
222
223 34370 static int ioctl_disk(struct disk_info *disk, uint8_t command, void *buffer)
224 {
225 34370 int result = 0;
226
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34370 times.
34370 if (command == DISK_IOCTL_CTRL_INIT) {
228 ✗ return initialize(disk);
229 }
230 34370 k_mutex_lock(&disk_lock, K_FOREVER);
231
3/4
✓ Branch 0 taken 34192 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
34370 switch (command) {
232 34192 case DISK_IOCTL_CTRL_DEINIT:
233 34192 store.ready = false;
234 34192 break;
235 168 case DISK_IOCTL_CTRL_SYNC:
236
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 2 times.
168 result = store.ready ? 0 : -EACCES;
237 168 break;
238 10 case DISK_IOCTL_GET_SECTOR_COUNT:
239 case DISK_IOCTL_GET_SECTOR_SIZE:
240 case DISK_IOCTL_GET_ERASE_BLOCK_SZ:
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (buffer == NULL) {
242 ✗ result = -EINVAL;
243 ✗ break;
244 }
245 10 *(uint32_t *)buffer = command == DISK_IOCTL_GET_SECTOR_COUNT ? SECTOR_COUNT
246
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
12 : command == DISK_IOCTL_GET_SECTOR_SIZE
247 ? KFSW_STORAGE_ZMS_SECTOR_SIZE
248
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 : 1U;
249 10 break;
250 ✗ default:
251 ✗ result = -ENOTSUP;
252 }
253 34370 k_mutex_unlock(&disk_lock);
254 34370 return result;
255 }
256
257 6 int kfsw_storage_zms_is_erased(bool *is_erased)
258 {
259 6 int result = 0;
260
261 6 *is_erased = false;
262 6 k_mutex_lock(&disk_lock, K_FOREVER);
263
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (!store.ready) {
264 2 result = -EACCES;
265 2 goto out;
266 }
267
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 2 times.
100 for (uint32_t sector = 0U; sector < SECTOR_COUNT; sector++) {
268 98 result = read_sector(scan_buffer, sector);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
98 if (result != 0) {
270 ✗ goto out;
271 }
272
2/2
✓ Branch 0 taken 393218 times.
✓ Branch 1 taken 96 times.
393314 for (size_t index = 0U; index < sizeof(scan_buffer); index++) {
273
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 393216 times.
393218 if (scan_buffer[index] != 0xff) {
274 2 goto out;
275 }
276 }
277 }
278 2 *is_erased = true;
279 6 out:
280 6 k_mutex_unlock(&disk_lock);
281 6 return result;
282 }
283
284 static const struct disk_operations operations = {
285 .init = initialize,
286 .status = status,
287 .read = read_disk,
288 .write = write_disk,
289 .ioctl = ioctl_disk,
290 };
291
292 static struct disk_info disk = {
293 .name = KFSW_STORAGE_ZMS_DISK_NAME,
294 .ops = &operations,
295 };
296
297 2 static int register_disk(void)
298 {
299 2 return disk_access_register(&disk);
300 }
301
302 SYS_INIT(register_disk, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
303