./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
224 / 259
86.5%
Branches:
119 / 172
69.2%
Functions:
18 / 18
100.0%

kfsw-services/src/parameter/parameter_persistence.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/fs/fs.h>
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/byteorder.h>
9 #include <zephyr/sys/crc.h>
10
11 #include <kfsw/platform/storage.h>
12 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_PARAM
13 #include <kfsw/services/log.h>
14 #include <kfsw/services/snapshot.h>
15 #include <kfsw/services/parameter.h>
16
17 #include "parameter_budget.h"
18 #include "param_wire.h"
19 #include "parameter_internal.h"
20
21 #define KFSW_PARAM_PERSIST_DIRECTORY KFSW_STORAGE_MOUNT_POINT "/params"
22 #define KFSW_PARAM_PERSIST_PATH KFSW_PARAM_PERSIST_DIRECTORY "/parameters.dat"
23 #define KFSW_PARAM_PERSIST_TEMP_PATH KFSW_PARAM_PERSIST_DIRECTORY "/parameters.tmp"
24 #define KFSW_PARAM_PERSIST_MAGIC "KPAR"
25 #define KFSW_PARAM_PERSIST_MAGIC_SIZE 4U
26 #define KFSW_PARAM_PERSIST_VERSION 1U
27 #define KFSW_PARAM_PERSIST_HEADER_SIZE 20U
28 #define KFSW_PARAM_PERSIST_CRC_OFFSET 16U
29 #define KFSW_PARAM_PERSIST_ENTRY_HEADER_SIZE 4U
30 #define KFSW_PARAM_PERSIST_MAX_NAME_SIZE KFSW_PARAM_NAME_MAX
31 #define KFSW_PARAM_PERSIST_MAX_VALUE_SIZE KFSW_PARAM_STRING_MAX
32
33 struct persist_entry {
34 const uint8_t *name;
35 const uint8_t *value;
36 uint16_t value_size;
37 uint8_t name_size;
38 uint8_t type;
39 };
40
41 K_MUTEX_DEFINE(kfsw_param_persist_lock);
42
43 BUILD_ASSERT(KFSW_PARAM_PERSIST_MAX_BYTES > KFSW_PARAM_PERSIST_HEADER_SIZE,
44 "the budget has to leave room for the header before any value fits");
45
46 static uint8_t snapshot[KFSW_PARAM_PERSIST_MAX_BYTES];
47
48 /* Size of the last snapshot built. */
49 static uint32_t snapshot_bytes;
50
51 458 static size_t bounded_string_length(const char *text, size_t maximum)
52 {
53 458 size_t length = 0U;
54
55
3/4
✓ Branch 0 taken 4862 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4404 times.
✓ Branch 3 taken 458 times.
4862 while ((length < maximum) && (text[length] != '\0')) {
56 4404 length++;
57 }
58 458 return length;
59 }
60
61 70 static int build_snapshot(size_t *snapshot_size)
62 {
63 70 size_t offset = KFSW_PARAM_PERSIST_HEADER_SIZE;
64 70 uint16_t entry_count = 0U;
65 70 int result = 0;
66
67 70 memset(snapshot, 0, sizeof(snapshot));
68 70 kfsw_param_table_lock();
69
2/2
✓ Branch 1 taken 1428 times.
✓ Branch 2 taken 70 times.
1498 for (size_t index = 0U; index < kfsw_param_entry_count(); index++) {
70 1428 const struct kfsw_param_entry *entry = kfsw_param_entry_at(index);
71 size_t name_size;
72 size_t header_offset;
73 uint16_t value_size;
74 uint8_t type;
75
76 /* Read-only values can still be saved, like the boot counter. */
77
1/2
✓ Branch 0 taken 1428 times.
✗ Branch 1 not taken.
1428 if ((entry->info.node != 0U) ||
78
2/2
✓ Branch 0 taken 462 times.
✓ Branch 1 taken 966 times.
1428 ((entry->info.flags & KFSW_PARAM_FLAG_PERSISTENT) == 0U) ||
79
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 458 times.
462 ((entry->info.flags & KFSW_PARAM_FLAG_OWNER_STORE) != 0U)) {
80 970 continue;
81 }
82
83 458 name_size = bounded_string_length(entry->info.name,
84 KFSW_PARAM_PERSIST_MAX_NAME_SIZE + 1U);
85
2/4
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 458 times.
458 if ((name_size == 0U) || (name_size > KFSW_PARAM_PERSIST_MAX_NAME_SIZE)) {
86 ✗ result = -ENAMETOOLONG;
87 ✗ break;
88 }
89 458 result = kfsw_param_wire_type_of(entry, &type, &value_size);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 458 times.
458 if (result != 0) {
91 ✗ kfsw_log_warning("PARAM: '%s' cannot be saved (%d); snapshot continues",
92 entry->info.name, result);
93 ✗ result = 0;
94 ✗ continue;
95 }
96 /* Check against the capacity, not the current length. */
97
2/2
✓ Branch 0 taken 454 times.
✓ Branch 1 taken 4 times.
458 if ((entry->info.type == KFSW_PARAM_STRING) ||
98
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 386 times.
454 (entry->info.type == KFSW_PARAM_DATA)) {
99 72 value_size = entry->info.array_size;
100 }
101
1/2
✓ Branch 0 taken 458 times.
✗ Branch 1 not taken.
458 if ((entry_count >= KFSW_PARAM_PERSIST_MAX_ENTRY_COUNT) ||
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 458 times.
458 (offset + KFSW_PARAM_PERSIST_ENTRY_HEADER_SIZE + name_size + value_size >
103 sizeof(snapshot))) {
104 ✗ result = -E2BIG;
105 ✗ break;
106 }
107
108 458 snapshot[offset] = (uint8_t)name_size;
109 458 snapshot[offset + 1U] = type;
110 458 header_offset = offset;
111 458 offset += KFSW_PARAM_PERSIST_ENTRY_HEADER_SIZE;
112 458 memcpy(&snapshot[offset], entry->info.name, name_size);
113 458 offset += name_size;
114 458 result = kfsw_param_wire_encode(entry, &snapshot[offset], sizeof(snapshot) - offset,
115 &value_size);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 458 times.
458 if (result != 0) {
117 ✗ break;
118 }
119 /* Written once the value is encoded: only then is a string's
120 * length known. */
121 458 sys_put_be16(value_size, &snapshot[header_offset + 2U]);
122 458 offset += value_size;
123 458 entry_count++;
124 }
125 70 kfsw_param_table_unlock();
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (result != 0) {
127 ✗ return result;
128 }
129
130 /* codechecker_intentional [bugprone-not-null-terminated-result] a binary magic */
131 70 memcpy(snapshot, KFSW_PARAM_PERSIST_MAGIC, KFSW_PARAM_PERSIST_MAGIC_SIZE);
132 70 sys_put_be16(KFSW_PARAM_PERSIST_VERSION, &snapshot[4]);
133 70 sys_put_be16(KFSW_PARAM_PERSIST_HEADER_SIZE, &snapshot[6]);
134 70 sys_put_be32((uint32_t)(offset - KFSW_PARAM_PERSIST_HEADER_SIZE), &snapshot[8]);
135 70 sys_put_be16(entry_count, &snapshot[12]);
136 70 sys_put_be16(0U, &snapshot[14]);
137 70 sys_put_be32(0U, &snapshot[KFSW_PARAM_PERSIST_CRC_OFFSET]);
138 70 sys_put_be32(crc32_ieee(snapshot, offset), &snapshot[KFSW_PARAM_PERSIST_CRC_OFFSET]);
139 70 *snapshot_size = offset;
140 70 snapshot_bytes = (uint32_t)offset;
141 70 return 0;
142 }
143
144 6 uint32_t kfsw_param_persist_bytes(void)
145 {
146 6 return snapshot_bytes;
147 }
148
149 6 uint32_t kfsw_param_persist_max_bytes(void)
150 {
151 6 return KFSW_PARAM_PERSIST_MAX_BYTES;
152 }
153
154 /*
155 * Refuse a snapshot larger than the budget or the free space.
156 */
157 70 static int check_budget(size_t snapshot_size)
158 {
159 struct kfsw_storage_info storage;
160 int result;
161
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (snapshot_size > KFSW_PARAM_PERSIST_MAX_BYTES) {
163 /* Unreachable while the snapshot fits its buffer; kept for a streaming writer. */
164 ✗ kfsw_log_error("PARAM: a snapshot of %u bytes is over the %u byte budget",
165 (unsigned int)snapshot_size,
166 (unsigned int)KFSW_PARAM_PERSIST_MAX_BYTES);
167 ✗ return -EFBIG;
168 }
169
170 70 result = kfsw_storage_get_info(&storage);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (result != 0) {
172 /* Not fatal: carry on if the partition can't report its free space. */
173 ✗ return 0;
174 }
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if ((uint64_t)snapshot_size > storage.free_bytes) {
176 ✗ kfsw_log_error("PARAM: a snapshot needs %u bytes and %u are free",
177 (unsigned int)snapshot_size, (unsigned int)storage.free_bytes);
178 ✗ return -ENOSPC;
179 }
180 70 return 0;
181 }
182
183 36 static int read_all(struct fs_file_t *file, uint8_t *data, size_t size)
184 {
185 36 size_t offset = 0U;
186
187
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
72 while (offset < size) {
188 36 ssize_t bytes_read = fs_read(file, &data[offset], size - offset);
189
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (bytes_read < 0) {
191 ✗ return (int)bytes_read;
192 }
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (bytes_read == 0) {
194 ✗ return -EMSGSIZE;
195 }
196 36 offset += (size_t)bytes_read;
197 }
198 36 return 0;
199 }
200
201 474 static int next_entry(const uint8_t *data, size_t size, size_t *offset, struct persist_entry *entry)
202 {
203 size_t entry_size;
204
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 474 times.
474 if (*offset + KFSW_PARAM_PERSIST_ENTRY_HEADER_SIZE > size) {
206 ✗ return -EBADMSG;
207 }
208
209 474 entry->name_size = data[*offset];
210 474 entry->type = data[*offset + 1U];
211 474 entry->value_size = sys_get_be16(&data[*offset + 2U]);
212 474 *offset += KFSW_PARAM_PERSIST_ENTRY_HEADER_SIZE;
213 474 entry_size = (size_t)entry->name_size + entry->value_size;
214
2/4
✓ Branch 0 taken 474 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 474 times.
✗ Branch 3 not taken.
474 if ((entry->name_size == 0U) || (entry->name_size > KFSW_PARAM_PERSIST_MAX_NAME_SIZE) ||
215
2/4
✓ Branch 0 taken 474 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 474 times.
✗ Branch 3 not taken.
474 (entry->value_size == 0U) || (entry->value_size > KFSW_PARAM_PERSIST_MAX_VALUE_SIZE) ||
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 474 times.
474 (*offset + entry_size > size)) {
217 ✗ return -EBADMSG;
218 }
219
220 474 entry->name = &data[*offset];
221 474 entry->value = &data[*offset + entry->name_size];
222 474 *offset += entry_size;
223 474 return 0;
224 }
225
226 36 static int validate_snapshot(size_t size, uint16_t *entry_count)
227 {
228 uint32_t expected_crc;
229 uint32_t actual_crc;
230 uint32_t payload_size;
231 36 size_t offset = KFSW_PARAM_PERSIST_HEADER_SIZE;
232
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (size < KFSW_PARAM_PERSIST_HEADER_SIZE) {
234 ✗ return -EMSGSIZE;
235 }
236
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 34 times.
36 if (memcmp(snapshot, KFSW_PARAM_PERSIST_MAGIC, KFSW_PARAM_PERSIST_MAGIC_SIZE) != 0) {
237 2 return -EBADMSG;
238 }
239
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 32 times.
34 if (sys_get_be16(&snapshot[4]) != KFSW_PARAM_PERSIST_VERSION) {
240 2 return -EPROTONOSUPPORT;
241 }
242
2/4
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
64 if ((sys_get_be16(&snapshot[6]) != KFSW_PARAM_PERSIST_HEADER_SIZE) ||
243 32 (sys_get_be16(&snapshot[14]) != 0U)) {
244 ✗ return -EBADMSG;
245 }
246
247 32 payload_size = sys_get_be32(&snapshot[8]);
248 32 *entry_count = sys_get_be16(&snapshot[12]);
249
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 2 times.
32 if ((payload_size != size - KFSW_PARAM_PERSIST_HEADER_SIZE) ||
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 (*entry_count > KFSW_PARAM_PERSIST_MAX_ENTRY_COUNT)) {
251 2 return -EMSGSIZE;
252 }
253
254 30 expected_crc = sys_get_be32(&snapshot[KFSW_PARAM_PERSIST_CRC_OFFSET]);
255 30 sys_put_be32(0U, &snapshot[KFSW_PARAM_PERSIST_CRC_OFFSET]);
256 30 actual_crc = crc32_ieee(snapshot, size);
257
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28 times.
30 if (actual_crc != expected_crc) {
258 2 return -EBADMSG;
259 }
260
261
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 28 times.
186 for (uint16_t index = 0U; index < *entry_count; index++) {
262 struct persist_entry entry;
263 158 int result = next_entry(snapshot, size, &offset, &entry);
264
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 if (result != 0) {
266 ✗ return result;
267 }
268 }
269
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 return (offset == size) ? 0 : -EBADMSG;
270 }
271
272 312 static bool entry_matches(const struct kfsw_param_entry *param_entry,
273 const struct persist_entry *persist_entry)
274 {
275 uint16_t value_size;
276 uint8_t type;
277
278
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 if ((kfsw_param_wire_type_of(param_entry, &type, &value_size) != 0) ||
279
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 308 times.
312 (type != persist_entry->type)) {
280 4 return false;
281 }
282
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 300 times.
308 if (type == KFSW_PARAM_WIRE_STRING) {
283 8 return persist_entry->value_size <= param_entry->info.array_size;
284 }
285 300 return value_size == persist_entry->value_size;
286 }
287
288 56 static int walk_snapshot(size_t size, uint16_t entry_count, bool apply)
289 {
290 56 size_t offset = KFSW_PARAM_PERSIST_HEADER_SIZE;
291
292
2/2
✓ Branch 0 taken 316 times.
✓ Branch 1 taken 56 times.
372 for (uint16_t index = 0U; index < entry_count; index++) {
293 struct persist_entry entry;
294 const struct kfsw_param_entry *param_entry;
295 char name[KFSW_PARAM_PERSIST_MAX_NAME_SIZE + 1U];
296 316 int result = next_entry(snapshot, size, &offset, &entry);
297
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (result != 0) {
299 ✗ return result;
300 }
301 316 memcpy(name, entry.name, entry.name_size);
302 316 name[entry.name_size] = '\0';
303 316 param_entry = kfsw_param_find_name(name);
304
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 4 times.
316 if ((param_entry == NULL) ||
305
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 ((param_entry->info.flags & KFSW_PARAM_FLAG_PERSISTENT) == 0U) ||
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 ((param_entry->info.flags & KFSW_PARAM_FLAG_OWNER_STORE) != 0U)) {
307 4 kfsw_log_warning("Ignoring '%s' from the snapshot", name);
308 8 continue;
309 }
310
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 308 times.
312 if (!entry_matches(param_entry, &entry)) {
311 4 kfsw_log_warning("Ignoring incompatible persistent parameter '%s'", name);
312 4 continue;
313 }
314 struct kfsw_param_value value;
315
316
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 308 times.
308 if (kfsw_param_wire_decode(param_entry, entry.type, entry.value, entry.value_size,
317 &value) != 0) {
318 ✗ kfsw_log_warning("Ignoring invalid persistent parameter '%s'", name);
319 ✗ continue;
320 }
321
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 154 times.
308 if (apply) {
322 154 result = kfsw_param_wire_apply(param_entry, &value);
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
154 if (result != 0) {
324 ✗ return result;
325 }
326 }
327 }
328 56 return 0;
329 }
330
331 28 static int apply_snapshot(size_t size, uint16_t entry_count)
332 {
333 int result;
334
335 28 kfsw_param_table_lock();
336 28 result = walk_snapshot(size, entry_count, false);
337
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (result == 0) {
338 28 result = walk_snapshot(size, entry_count, true);
339 }
340 28 kfsw_param_table_unlock();
341 28 return result;
342 }
343
344 72 static int persist_save(void)
345 {
346 size_t snapshot_size;
347 int result;
348
349
3/4
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 70 times.
72 if (!kfsw_param_is_initialized() || !kfsw_storage_is_ready()) {
350 2 return -EACCES;
351 }
352
353 70 k_mutex_lock(&kfsw_param_persist_lock, K_FOREVER);
354 70 result = build_snapshot(&snapshot_size);
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (result != 0) {
356 ✗ goto out;
357 }
358
359 70 result = fs_mkdir(KFSW_PARAM_PERSIST_DIRECTORY);
360
3/4
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
70 if ((result != 0) && (result != -EEXIST)) {
361 ✗ goto out;
362 }
363 70 result = check_budget(snapshot_size);
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (result != 0) {
365 ✗ goto out;
366 }
367 70 result = kfsw_snapshot_write(KFSW_PARAM_PERSIST_PATH, KFSW_PARAM_PERSIST_TEMP_PATH,
368 snapshot, snapshot_size);
369
370 70 out:
371 70 k_mutex_unlock(&kfsw_param_persist_lock);
372 70 return result;
373 }
374
375 42 static int persist_load(void)
376 {
377 struct fs_dirent entry;
378 struct fs_file_t file;
379 uint16_t entry_count;
380 int close_result;
381 int result;
382
383
3/4
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 40 times.
42 if (!kfsw_param_is_initialized() || !kfsw_storage_is_ready()) {
384 2 return -EACCES;
385 }
386
387 40 k_mutex_lock(&kfsw_param_persist_lock, K_FOREVER);
388 40 (void)fs_unlink(KFSW_PARAM_PERSIST_TEMP_PATH);
389 40 result = fs_stat(KFSW_PARAM_PERSIST_PATH, &entry);
390
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 36 times.
40 if (result != 0) {
391 4 goto out;
392 }
393
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 if ((entry.type != FS_DIR_ENTRY_FILE) || (entry.size < KFSW_PARAM_PERSIST_HEADER_SIZE) ||
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 (entry.size > sizeof(snapshot))) {
395 ✗ result = -EMSGSIZE;
396 ✗ goto out;
397 }
398
399 36 fs_file_t_init(&file);
400 36 result = fs_open(&file, KFSW_PARAM_PERSIST_PATH, FS_O_READ);
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (result != 0) {
402 ✗ goto out;
403 }
404 36 result = read_all(&file, snapshot, entry.size);
405 36 close_result = fs_close(&file);
406
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (result == 0) {
407 36 result = close_result;
408 }
409
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (result == 0) {
410 36 result = validate_snapshot(entry.size, &entry_count);
411 }
412
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 8 times.
36 if (result == 0) {
413 28 result = apply_snapshot(entry.size, entry_count);
414 }
415
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 28 times.
36 if (result == 0) {
416 /* Also recorded on load, so the size is known right after boot. */
417 28 snapshot_bytes = (uint32_t)entry.size;
418 }
419
420 8 out:
421 40 k_mutex_unlock(&kfsw_param_persist_lock);
422 40 return result;
423 }
424
425 44 int kfsw_param_persist_clear(void)
426 {
427 int active_result;
428 int temp_result;
429
430
2/4
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 44 times.
44 if (!kfsw_param_is_initialized() || !kfsw_storage_is_ready()) {
431 ✗ return -EACCES;
432 }
433
434 44 k_mutex_lock(&kfsw_param_persist_lock, K_FOREVER);
435 44 active_result = fs_unlink(KFSW_PARAM_PERSIST_PATH);
436 44 temp_result = fs_unlink(KFSW_PARAM_PERSIST_TEMP_PATH);
437 44 k_mutex_unlock(&kfsw_param_persist_lock);
438
439
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
44 if ((active_result != 0) && (active_result != -ENOENT)) {
440 ✗ return active_result;
441 }
442
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
44 return ((temp_result == 0) || (temp_result == -ENOENT)) ? 0 : temp_result;
443 }
444
445 /*
446 * Snapshot writes wear flash. Warn once when the save count crosses the limit.
447 */
448 66 static void warn_about_wear(void)
449 {
450 #if CONFIG_KFSW_PARAM_PERSIST_SAVE_WARN > 0
451 static bool warned;
452 struct kfsw_param_stats stats;
453
454
2/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 66 times.
66 if (warned || (kfsw_param_get_stats(&stats) != 0)) {
455 66 return;
456 }
457
1/2
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
66 if (stats.saves < CONFIG_KFSW_PARAM_PERSIST_SAVE_WARN) {
458 66 return;
459 }
460 ✗ warned = true;
461 ✗ kfsw_log_warning("PARAM: %u snapshots written since boot, each one an erase cycle; "
462 "param_autosave off and 'param persist' writes only when asked",
463 stats.saves);
464 #endif
465 }
466
467 /*
468 * Count saves and the failures at one exit each.
469 *
470 * The failure needs its own counter and its own log line because one caller,
471 * the autosave in kfsw_param_set, cannot pass the error on: the value is
472 * already applied and live, so failing the set would be wrong. Without this the
473 * node would report a persistent parameter as set and lose it at the next
474 * reset, with nothing said anywhere.
475 */
476 72 int kfsw_param_persist_save(void)
477 {
478 72 int result = persist_save();
479
480
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 6 times.
72 if (result == 0) {
481 66 kfsw_param_count_save();
482 66 warn_about_wear();
483 } else {
484 6 kfsw_param_count_save_failure();
485 6 kfsw_log_warning("PARAM: the snapshot was not written: %d; "
486 "persistent values will not survive a reset",
487 result);
488 }
489 72 return result;
490 }
491
492 8 int kfsw_param_persist_table_count(uint8_t table, uint16_t *count)
493 {
494 8 uint16_t found = 0U;
495
496 8 bool table_seen = false;
497
498
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (count == NULL) {
499 2 return -EINVAL;
500 }
501
502 /* Derive table existence from the entries, taking only one lock. */
503 6 kfsw_param_table_lock();
504
2/2
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 6 times.
132 for (size_t index = 0U; index < kfsw_param_entry_count(); index++) {
505 126 const struct kfsw_param_entry *entry = kfsw_param_entry_at(index);
506
507
3/4
✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 94 times.
✓ Branch 3 taken 32 times.
126 if ((entry == NULL) || ((uint8_t)(entry->info.id >> 8) != table)) {
508 94 continue;
509 }
510 32 table_seen = true;
511
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 26 times.
32 if ((entry->info.flags & KFSW_PARAM_FLAG_PERSISTENT) != 0U) {
512 6 found++;
513 }
514 }
515 6 kfsw_param_table_unlock();
516
517
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (!table_seen) {
518 2 return -ENOENT;
519 }
520 4 *count = found;
521 4 return 0;
522 }
523
524 42 int kfsw_param_persist_load(void)
525 {
526 42 int result = persist_load();
527
528 /* A missing snapshot is the ordinary first boot, not a failure. */
529
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 4 times.
42 if ((result != 0) && (result != -ENOENT)) {
530 10 kfsw_param_count_load_failure();
531 }
532 42 return result;
533 }
534