./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
39 / 41
95.1%
Branches:
24 / 34
70.6%
Functions:
3 / 3
100.0%

kfsw-platform/src/boot_store.c
Line Branch Exec Source
1 #include <errno.h>
2
3 #include <zephyr/drivers/flash.h>
4 #include <zephyr/kvss/nvs.h>
5 #include <zephyr/storage/flash_map.h>
6 #include <zephyr/sys/byteorder.h>
7 #include <zephyr/sys/crc.h>
8
9 #include <kfsw/platform/boot_store.h>
10
11 #define STORE_NODE DT_CHOSEN(kfsw_boot_budget_partition)
12 #define STORE_ID 1U
13 #define RECORD_SIZE 8U
14
15 static struct nvs_fs store;
16 static bool ready;
17
18 78 int kfsw_boot_store_open(void)
19 {
20 const struct flash_area *area;
21 struct flash_pages_info page;
22 int result;
23
24
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 16 times.
78 if (ready) {
25 62 return 0;
26 }
27 16 result = flash_area_open(DT_FIXED_PARTITION_ID(STORE_NODE), &area);
28
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (result != 0) {
29 ✗ return result;
30 }
31 16 store.flash_device = flash_area_get_device(area);
32 16 result = flash_get_page_info_by_offs(store.flash_device, area->fa_off, &page);
33
3/6
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 16 times.
16 if (result == 0 && (area->fa_size != 2U * page.size || page.size > UINT16_MAX)) {
34 ✗ result = -EINVAL;
35 }
36
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (result == 0) {
37 16 store.offset = area->fa_off;
38 16 store.sector_size = (uint16_t)page.size;
39 16 store.sector_count = 2U;
40 16 result = nvs_mount(&store);
41 }
42 /* NVS refuses both sectors closed and a damaged write area for good. An
43 empty store reads as a full budget, so erasing is the safe way out. */
44
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
16 if ((result == -EDEADLK) || (result == -ESPIPE)) {
45 2 result = flash_area_flatten(area, 0, area->fa_size);
46
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (result == 0) {
47 2 result = nvs_mount(&store);
48 }
49 }
50 16 flash_area_close(area);
51 16 ready = result == 0;
52 16 return result;
53 }
54
55 84 int kfsw_boot_store_read(uint8_t *left, uint8_t *maximum)
56 {
57 uint8_t record[RECORD_SIZE];
58 ssize_t result;
59
60
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 82 times.
84 if (!ready) {
61 2 return -ENODEV;
62 }
63 82 result = nvs_read(&store, STORE_ID, record, sizeof(record));
64
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 62 times.
82 if (result < 0) {
65 20 return (int)result;
66 }
67
5/8
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 62 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 60 times.
124 if (result != sizeof(record) || record[0] != 'B' || record[1] != 1U ||
68 62 sys_get_le32(&record[4]) != crc32_ieee(record, 4U)) {
69 2 return -EBADMSG;
70 }
71 60 *left = record[2];
72 60 *maximum = record[3];
73 60 return 0;
74 }
75
76 190 int kfsw_boot_store_write(uint8_t left, uint8_t maximum)
77 {
78 190 uint8_t record[RECORD_SIZE] = {'B', 1U, left, maximum};
79 ssize_t result;
80
81
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 188 times.
190 if (!ready) {
82 2 return -ENODEV;
83 }
84 188 sys_put_le32(crc32_ieee(record, 4U), &record[4]);
85 188 result = nvs_write(&store, STORE_ID, record, sizeof(record));
86
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 158 times.
188 return result < 0 ? (int)result : 0;
87 }
88