./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
251 / 304
82.6%
Branches:
120 / 174
69.0%
Functions:
14 / 15
93.3%

kfsw-services/src/table/table.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #include <zephyr/fs/fs.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/sys/byteorder.h>
11 #include <zephyr/sys/crc.h>
12 #include <zephyr/sys/util.h>
13
14 #include <kfsw/platform/storage.h>
15 #if CONFIG_KFSW_EVENT
16 #include <kfsw/services/event.h>
17 #endif
18 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_TABLE
19 #include <kfsw/services/log.h>
20 #include <kfsw/services/snapshot.h>
21 #include <kfsw/services/table.h>
22
23 #include "../parameter/param_wire.h"
24 #include "../parameter/parameter_internal.h"
25
26 /*
27 * File layout, the same shape as the parameter snapshot so one decoder reads
28 * both. All fields are big-endian.
29 *
30 * 0 magic "KTBL"
31 * 4 format version
32 * 6 header size
33 * 8 payload bytes after the header
34 * 12 entry count
35 * 14 table identifier, then one reserved byte
36 * 16 CRC32 over the whole file with these four bytes zero
37 *
38 * Each entry is an offset within the table, a wire type code, the value size,
39 * and the value.
40 */
41 #define KFSW_TABLE_MAGIC "KTBL"
42 #define KFSW_TABLE_MAGIC_SIZE 4U
43 #define KFSW_TABLE_VERSION 1U
44 #define KFSW_TABLE_HEADER_SIZE 20U
45 #define KFSW_TABLE_CRC_OFFSET 16U
46 #define KFSW_TABLE_ENTRY_HEADER_SIZE 4U
47
48 #define KFSW_TABLE_MAX_BYTES \
49 (KFSW_TABLE_HEADER_SIZE + (KFSW_TABLE_MAX_ENTRIES * (KFSW_TABLE_ENTRY_HEADER_SIZE + \
50 KFSW_PARAM_WIRE_MAX_VALUE_SIZE)))
51
52 #define KFSW_TABLE_TEMPORARY_SUFFIX ".part"
53
54 /** One entry as it sits in the file. */
55 struct file_entry {
56 const uint8_t *value;
57 uint16_t value_size;
58 uint8_t offset;
59 uint8_t type;
60 };
61
62 /** One value a load replaced, kept so the load can be undone. */
63 struct saved_value {
64 uint16_t id;
65 struct kfsw_param_value value;
66 };
67
68 static K_MUTEX_DEFINE(table_lock);
69 static uint8_t image[KFSW_TABLE_MAX_BYTES];
70 static struct saved_value saved[KFSW_TABLE_MAX_ENTRIES];
71 static uint16_t saved_count;
72 static struct saved_value pending[KFSW_TABLE_MAX_ENTRIES];
73 /* Filled by a load's check pass and applied by the pass after it, so nothing is
74 * written until every entry has been accepted. */
75 static struct kfsw_param_value decoded[KFSW_TABLE_MAX_ENTRIES];
76 static struct kfsw_table_status status = {
77 .state = KFSW_TABLE_EMPTY,
78 };
79
80 112 static void count_up(uint32_t *counter)
81 {
82
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 if (*counter < UINT32_MAX) {
83 112 (*counter)++;
84 }
85 112 }
86
87 112 static void report_event(enum kfsw_table_event id, uint8_t table, uint16_t entries)
88 {
89 #if CONFIG_KFSW_EVENT
90 uint8_t payload[3];
91
92 payload[0] = table;
93 sys_put_be16(entries, &payload[1]);
94 kfsw_event_emit(KFSW_EVENT_SOURCE_TABLE, (uint16_t)id,
95 (id == KFSW_EVENT_TABLE_LOADED) ? KFSW_EVENT_INFO : KFSW_EVENT_WARNING,
96 payload, sizeof(payload));
97 #else
98 ARG_UNUSED(id);
99 ARG_UNUSED(table);
100 ARG_UNUSED(entries);
101 #endif
102 112 }
103
104 /* fs_read is allowed to return less than it was asked for. */
105 136 static int read_all(struct fs_file_t *file, uint8_t *data, size_t size)
106 {
107 136 size_t offset = 0U;
108
109
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 136 times.
272 while (offset < size) {
110 136 ssize_t read = fs_read(file, &data[offset], size - offset);
111
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (read < 0) {
113 ✗ return (int)read;
114 }
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (read == 0) {
116 ✗ return -EBADMSG;
117 }
118 136 offset += (size_t)read;
119 }
120 136 return 0;
121 }
122
123 144 static int read_image(const char *path, size_t *size)
124 {
125 struct fs_dirent entry;
126 struct fs_file_t file;
127 int result;
128 int close_result;
129
130 144 result = fs_stat(path, &entry);
131
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 136 times.
144 if (result != 0) {
132 8 return result;
133 }
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (entry.type != FS_DIR_ENTRY_FILE) {
135 ✗ return -EBADMSG;
136 }
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (entry.size < KFSW_TABLE_HEADER_SIZE) {
138 ✗ return -EBADMSG;
139 }
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (entry.size > (size_t)sizeof(image)) {
141 ✗ return -EFBIG;
142 }
143
144 136 fs_file_t_init(&file);
145 136 result = fs_open(&file, path, FS_O_READ);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (result != 0) {
147 ✗ return result;
148 }
149 136 result = read_all(&file, image, (size_t)entry.size);
150 136 close_result = fs_close(&file);
151
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 if (result == 0) {
152 136 result = close_result;
153 }
154
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 if (result == 0) {
155 136 *size = (size_t)entry.size;
156 }
157 136 return result;
158 }
159
160 136 static int validate_header(size_t size, uint16_t *entry_count, uint8_t *table)
161 {
162 uint32_t expected_crc;
163 uint32_t actual_crc;
164 uint32_t payload_size;
165
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (memcmp(image, KFSW_TABLE_MAGIC, KFSW_TABLE_MAGIC_SIZE) != 0) {
167 ✗ return -EBADMSG;
168 }
169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
136 if (sys_get_be16(&image[4]) != KFSW_TABLE_VERSION) {
170 ✗ return -ENOTSUP;
171 }
172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
136 if (sys_get_be16(&image[6]) != KFSW_TABLE_HEADER_SIZE) {
173 ✗ return -EBADMSG;
174 }
175 136 payload_size = sys_get_be32(&image[8]);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (payload_size != (uint32_t)(size - KFSW_TABLE_HEADER_SIZE)) {
177 ✗ return -EBADMSG;
178 }
179 136 *entry_count = sys_get_be16(&image[12]);
180
2/4
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 136 times.
136 if ((*entry_count == 0U) || (*entry_count > KFSW_TABLE_MAX_ENTRIES)) {
181 ✗ return -EBADMSG;
182 }
183 136 *table = image[14];
184
2/4
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 136 times.
136 if ((*table < KFSW_PARAM_TABLE_CORE_FIRST) || (*table > KFSW_PARAM_TABLE_MODULE_LAST)) {
185 ✗ return -EBADMSG;
186 }
187
188 136 expected_crc = sys_get_be32(&image[KFSW_TABLE_CRC_OFFSET]);
189 136 sys_put_be32(0U, &image[KFSW_TABLE_CRC_OFFSET]);
190 136 actual_crc = crc32_ieee(image, size);
191 136 sys_put_be32(expected_crc, &image[KFSW_TABLE_CRC_OFFSET]);
192
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 128 times.
136 if (actual_crc != expected_crc) {
193 8 return -EBADMSG;
194 }
195 128 return 0;
196 }
197
198 168 static int next_entry(size_t size, size_t *offset, struct file_entry *entry)
199 {
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if ((*offset + KFSW_TABLE_ENTRY_HEADER_SIZE) > size) {
201 ✗ return -EBADMSG;
202 }
203 168 entry->offset = image[*offset];
204 168 entry->type = image[*offset + 1U];
205 168 entry->value_size = sys_get_be16(&image[*offset + 2U]);
206 168 *offset += KFSW_TABLE_ENTRY_HEADER_SIZE;
207
208
2/4
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
168 if ((entry->value_size == 0U) || (entry->value_size > KFSW_PARAM_WIRE_MAX_VALUE_SIZE) ||
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 ((*offset + entry->value_size) > size)) {
210 ✗ return -EBADMSG;
211 }
212 168 entry->value = &image[*offset];
213 168 *offset += entry->value_size;
214 168 return 0;
215 }
216
217 /*
218 * Walks the whole file. A load keeps each accepted value for the pass that
219 * applies them; validating alone keeps nothing.
220 */
221 128 static int check_entries(size_t size, uint16_t entry_count, uint8_t table,
222 const struct kfsw_param_entry *entries[], bool keep_decoded,
223 struct kfsw_table_report *report)
224 {
225 128 size_t offset = KFSW_TABLE_HEADER_SIZE;
226 uint8_t seen[KFSW_TABLE_MAX_ENTRIES];
227 128 uint16_t seen_count = 0U;
228
229
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 64 times.
232 for (uint16_t index = 0U; index < entry_count; index++) {
230 const struct kfsw_param_entry *param_entry;
231 struct kfsw_param_value value;
232 struct file_entry entry;
233 168 int result = next_entry(size, &offset, &entry);
234
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (result != 0) {
236 ✗ report->failed_entry = index + 1U;
237 ✗ report->reason = result;
238 64 return -EBADMSG;
239 }
240
241 168 report->failed_entry = index + 1U;
242 168 report->failed_offset = entry.offset;
243
244 /* The same offset twice would make the file's effect depend on
245 * the order it is applied in. */
246
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 160 times.
200 for (uint16_t seen_index = 0U; seen_index < seen_count; seen_index++) {
247
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 32 times.
40 if (seen[seen_index] == entry.offset) {
248 8 report->reason = -EEXIST;
249 8 return -EBADMSG;
250 }
251 }
252 160 seen[seen_count] = entry.offset;
253 160 seen_count++;
254
255 160 param_entry = kfsw_param_find_id(KFSW_PARAM_ID(table, entry.offset));
256
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 144 times.
160 if (param_entry == NULL) {
257 16 report->reason = -ENXIO;
258 16 return -EBADMSG;
259 }
260
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 136 times.
144 if (param_entry->info.read_only) {
261 8 report->reason = -EROFS;
262 8 return -EBADMSG;
263 }
264
265
4/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 32 times.
136 if ((param_entry->definition->apply != NULL) && (entry_count != 1U)) {
266 8 report->reason = -ENOTSUP;
267 8 return -EBADMSG;
268 }
269
270 /* The parameter's own range check runs here, before anything is
271 * written, which is the whole point of validating a file. */
272 128 result = kfsw_param_wire_decode(param_entry, entry.type, entry.value,
273 128 entry.value_size, &value);
274
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 104 times.
128 if (result != 0) {
275 24 report->reason = result;
276 24 return -EBADMSG;
277 }
278
279
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 24 times.
104 if (entries != NULL) {
280 80 entries[index] = param_entry;
281 }
282
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 24 times.
104 if (keep_decoded) {
283 80 decoded[index] = value;
284 }
285 104 report->accepted++;
286 }
287
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (offset != size) {
289 ✗ report->reason = -EBADMSG;
290 ✗ return -EBADMSG;
291 }
292
293 64 report->failed_entry = 0U;
294 64 report->failed_offset = 0U;
295 64 report->reason = 0;
296 64 return 0;
297 }
298
299 152 static int prepare(const char *path, size_t *size, uint16_t *entry_count, uint8_t *table)
300 {
301 int result;
302
303
4/6
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 144 times.
152 if ((path == NULL) || (path[0] == '\0') || (strlen(path) >= (size_t)KFSW_TABLE_PATH_SIZE)) {
304 8 return -EINVAL;
305 }
306
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
144 if (!kfsw_param_is_initialized()) {
307 ✗ return -EACCES;
308 }
309
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
144 if (!kfsw_storage_is_ready()) {
310 ✗ return -ENODEV;
311 }
312
313 144 result = read_image(path, size);
314
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 136 times.
144 if (result != 0) {
315 8 return result;
316 }
317 136 return validate_header(*size, entry_count, table);
318 }
319
320 152 static int examine(const char *path, struct kfsw_table_report *report,
321 const struct kfsw_param_entry *entries[], bool keep_decoded)
322 {
323 152 uint16_t entry_count = 0U;
324 152 uint8_t table = 0U;
325 152 size_t size = 0U;
326 int result;
327
328 152 *report = (struct kfsw_table_report){0};
329
330 152 result = prepare(path, &size, &entry_count, &table);
331
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 128 times.
152 if (result != 0) {
332 24 report->reason = result;
333 24 return result;
334 }
335 128 report->table = table;
336 128 report->entries = entry_count;
337
338 128 return check_entries(size, entry_count, table, entries, keep_decoded, report);
339 }
340
341 80 int kfsw_table_validate(const char *path, struct kfsw_table_report *report)
342 {
343 struct kfsw_table_report local;
344 int result;
345
346 80 k_mutex_lock(&table_lock, K_FOREVER);
347 80 kfsw_param_table_lock();
348 80 result = examine(path, &local, NULL, false);
349 80 kfsw_param_table_unlock();
350 80 k_mutex_unlock(&table_lock);
351
352
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16 times.
80 if (report != NULL) {
353 64 *report = local;
354 }
355 80 return result;
356 }
357
358 72 int kfsw_table_load(const char *path, struct kfsw_table_report *report)
359 {
360 const struct kfsw_param_entry *entries[KFSW_TABLE_MAX_ENTRIES];
361 struct kfsw_table_report local;
362 int result;
363
364 72 k_mutex_lock(&table_lock, K_FOREVER);
365 72 kfsw_param_table_lock();
366 72 result = examine(path, &local, entries, true);
367
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 48 times.
72 if (result != 0) {
368 24 goto out;
369 }
370
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 48 times.
120 for (uint16_t index = 0U; index < local.entries; index++) {
371 72 pending[index].id = entries[index]->info.id;
372 72 result = kfsw_param_read_stored_entry(entries[index], &pending[index].value);
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (result != 0) {
374 ✗ local.failed_entry = index + 1U;
375 ✗ local.failed_offset = entries[index]->info.offset;
376 ✗ goto out;
377 }
378 }
379
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 40 times.
112 for (uint16_t index = 0U; index < local.entries; index++) {
380 72 result = kfsw_param_wire_apply(entries[index], &decoded[index]);
381
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 64 times.
72 if (result != 0) {
382 8 local.failed_entry = index + 1U;
383 8 local.failed_offset = entries[index]->info.offset;
384 8 goto out;
385 }
386 }
387 40 memcpy(saved, pending, local.entries * sizeof(saved[0]));
388 40 saved_count = local.entries;
389 40 status.state = KFSW_TABLE_LOADED;
390 40 status.table = local.table;
391 40 status.entries = local.entries;
392 40 count_up(&status.loads);
393 40 (void)strncpy(status.path, path, sizeof(status.path) - 1U);
394 40 status.path[sizeof(status.path) - 1U] = '\0';
395 72 out:
396 72 kfsw_param_table_unlock();
397
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 40 times.
72 if (result != 0) {
398
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 24 times.
32 if (local.reason == 0) {
399 8 local.reason = result;
400 }
401 32 count_up(&status.rejections);
402 }
403 72 k_mutex_unlock(&table_lock);
404
405
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 32 times.
72 if (result == 0) {
406 40 kfsw_log_info("Table %u loaded from '%s', %u entries", local.table, path,
407 local.entries);
408 40 report_event(KFSW_EVENT_TABLE_LOADED, local.table, local.entries);
409 } else {
410 32 kfsw_log_warning("Table file '%s' refused at entry %u (%d)", path,
411 local.failed_entry, local.reason);
412 32 report_event(KFSW_EVENT_TABLE_REJECTED, local.table, local.entries);
413 }
414
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 16 times.
72 if (report != NULL) {
415 56 *report = local;
416 }
417 72 return result;
418 }
419
420 64 int kfsw_table_revert(void)
421 {
422 64 uint16_t restored = 0U;
423 uint8_t table;
424 64 int result = 0;
425
426
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
64 if (!kfsw_param_is_initialized()) {
427 ✗ return -EACCES;
428 }
429
430 64 k_mutex_lock(&table_lock, K_FOREVER);
431
3/4
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
64 if ((status.state != KFSW_TABLE_LOADED) || (saved_count == 0U)) {
432 8 k_mutex_unlock(&table_lock);
433 8 return -ENOENT;
434 }
435 56 table = status.table;
436 56 kfsw_param_table_lock();
437
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 48 times.
128 for (uint16_t index = 0U; index < saved_count; index++) {
438 80 const struct kfsw_param_entry *entry = kfsw_param_find_id(saved[index].id);
439
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (entry == NULL) {
441 ✗ result = -ENOENT;
442 ✗ goto out;
443 }
444
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
80 if ((entry->definition->apply != NULL) && (saved_count != 1U)) {
445 ✗ result = -ENOTSUP;
446 ✗ goto out;
447 }
448 80 result = kfsw_param_validate_entry(entry, &saved[index].value);
449
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 72 times.
80 if (result != 0) {
450 8 goto out;
451 }
452 }
453
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 40 times.
112 for (uint16_t index = 0U; index < saved_count; index++) {
454 72 const struct kfsw_param_entry *entry = kfsw_param_find_id(saved[index].id);
455
456 72 result = kfsw_param_wire_apply(entry, &saved[index].value);
457
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 64 times.
72 if (result != 0) {
458 8 goto out;
459 }
460 64 restored++;
461 }
462 40 saved_count = 0U;
463 40 status.state = KFSW_TABLE_REVERTED;
464 40 count_up(&status.reverts);
465 56 out:
466 56 kfsw_param_table_unlock();
467 56 k_mutex_unlock(&table_lock);
468
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 40 times.
56 if (result != 0) {
469 16 kfsw_log_warning("Table %u undo refused: %d", table, result);
470 16 return result;
471 }
472 40 kfsw_log_info("Table %u reverted, %u values put back", table, restored);
473 40 report_event(KFSW_EVENT_TABLE_REVERTED, table, restored);
474 40 return 0;
475 }
476
477 8 int kfsw_table_dump(uint8_t table, const char *path, uint16_t *entries)
478 {
479 char temporary_path[KFSW_TABLE_PATH_SIZE + sizeof(KFSW_TABLE_TEMPORARY_SUFFIX)];
480 8 size_t offset = KFSW_TABLE_HEADER_SIZE;
481 8 uint16_t written_entries = 0U;
482 8 int result = 0;
483
484
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
8 if ((path == NULL) || (path[0] == '\0') || (strlen(path) >= (size_t)KFSW_TABLE_PATH_SIZE)) {
485 ✗ return -EINVAL;
486 }
487
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if ((table < KFSW_PARAM_TABLE_CORE_FIRST) || (table > KFSW_PARAM_TABLE_MODULE_LAST)) {
488 ✗ return -EINVAL;
489 }
490
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (!kfsw_param_is_initialized()) {
491 ✗ return -EACCES;
492 }
493
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (!kfsw_storage_is_ready()) {
494 ✗ return -ENODEV;
495 }
496
497 8 k_mutex_lock(&table_lock, K_FOREVER);
498 8 kfsw_param_table_lock();
499
2/2
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 8 times.
120 for (size_t index = 0U; index < kfsw_param_entry_count(); index++) {
500 112 const struct kfsw_param_entry *entry = kfsw_param_entry_at(index);
501 uint16_t value_size;
502 uint16_t value_bytes;
503 uint8_t type;
504
505
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 16 times.
112 if ((entry->info.table != table) || entry->info.read_only) {
506 96 continue;
507 }
508
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (kfsw_param_wire_type_of(entry, &type, &value_size) != 0) {
509 /* A type no file carries is left out rather than
510 * making the whole table undumpable. */
511 ✗ continue;
512 }
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (written_entries == KFSW_TABLE_MAX_ENTRIES) {
514 ✗ result = -E2BIG;
515 ✗ break;
516 }
517
518
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (kfsw_param_wire_encode(entry, &image[offset + KFSW_TABLE_ENTRY_HEADER_SIZE],
519 sizeof(image) - offset - KFSW_TABLE_ENTRY_HEADER_SIZE,
520 &value_bytes) != 0) {
521 ✗ result = -E2BIG;
522 ✗ break;
523 }
524 16 image[offset] = entry->info.offset;
525 16 image[offset + 1U] = type;
526 16 sys_put_be16(value_bytes, &image[offset + 2U]);
527 16 offset += KFSW_TABLE_ENTRY_HEADER_SIZE + value_bytes;
528 16 written_entries++;
529 }
530 8 kfsw_param_table_unlock();
531
532
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if ((result == 0) && (written_entries == 0U)) {
533 ✗ result = -ENOENT;
534 }
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (result != 0) {
536 ✗ k_mutex_unlock(&table_lock);
537 ✗ return result;
538 }
539
540 8 memcpy(image, KFSW_TABLE_MAGIC, KFSW_TABLE_MAGIC_SIZE);
541 8 sys_put_be16(KFSW_TABLE_VERSION, &image[4]);
542 8 sys_put_be16(KFSW_TABLE_HEADER_SIZE, &image[6]);
543 8 sys_put_be32((uint32_t)(offset - KFSW_TABLE_HEADER_SIZE), &image[8]);
544 8 sys_put_be16(written_entries, &image[12]);
545 8 image[14] = table;
546 8 image[15] = 0U;
547 8 sys_put_be32(0U, &image[KFSW_TABLE_CRC_OFFSET]);
548 8 sys_put_be32(crc32_ieee(image, offset), &image[KFSW_TABLE_CRC_OFFSET]);
549
550 8 result = fs_mkdir(KFSW_TABLE_DIRECTORY);
551
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if ((result != 0) && (result != -EEXIST)) {
552 ✗ k_mutex_unlock(&table_lock);
553 ✗ return result;
554 }
555
556 8 (void)snprintf(temporary_path, sizeof(temporary_path), "%s%s", path,
557 KFSW_TABLE_TEMPORARY_SUFFIX);
558 8 result = kfsw_snapshot_write(path, temporary_path, image, offset);
559 8 k_mutex_unlock(&table_lock);
560
561
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 if ((result == 0) && (entries != NULL)) {
562 8 *entries = written_entries;
563 }
564 8 return result;
565 }
566
567 196 int kfsw_table_get_status(struct kfsw_table_status *out)
568 {
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
196 if (out == NULL) {
570 ✗ return -EINVAL;
571 }
572 196 k_mutex_lock(&table_lock, K_FOREVER);
573 196 *out = status;
574 196 k_mutex_unlock(&table_lock);
575 196 return 0;
576 }
577
578 ✗ const char *kfsw_table_state_name(enum kfsw_table_state state)
579 {
580
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ switch (state) {
581 ✗ case KFSW_TABLE_EMPTY:
582 ✗ return "empty";
583 ✗ case KFSW_TABLE_LOADED:
584 ✗ return "loaded";
585 ✗ case KFSW_TABLE_REVERTED:
586 ✗ return "reverted";
587 ✗ default:
588 ✗ return "unknown";
589 }
590 }
591