./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
15 / 23
65.2%
Branches:
3 / 6
50.0%
Functions:
4 / 11
36.4%

kfsw-services/src/fwu-parameters/fwu_table.c
Line Branch Exec Source
1 #include <stdint.h>
2
3 #include <zephyr/sys/util.h>
4
5 #include <kfsw/services/fwu.h>
6 #include <kfsw/services/parameter.h>
7 #if CONFIG_KFSW_FWU_LITE_CSP
8 #include <errno.h>
9
10 #include <kfsw/services/fwu_lite.h>
11 #endif
12
13 /* Read-only, so an unverified image can't be marked ready from the ground. */
14 static uint32_t fwu_total_size;
15 static uint32_t fwu_received;
16 static uint32_t fwu_expected_crc32;
17 static uint32_t fwu_actual_crc32;
18 static uint32_t fwu_started;
19 static uint32_t fwu_completed;
20 static uint32_t fwu_failed;
21 static uint8_t fwu_state;
22 static uint8_t fwu_swap_scheduled;
23 static uint8_t fwu_target_bound;
24
25 6 static void sample_status(void)
26 {
27 struct kfsw_fwu_status status;
28
29
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (kfsw_fwu_get_status(&status) != 0) {
30 ✗ return;
31 }
32 6 fwu_total_size = status.total_size;
33 6 fwu_received = status.received;
34 6 fwu_expected_crc32 = status.expected_crc32;
35 6 fwu_actual_crc32 = status.actual_crc32;
36 6 fwu_started = status.started;
37 6 fwu_completed = status.completed;
38 6 fwu_failed = status.failed;
39 6 fwu_state = status.state;
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 fwu_swap_scheduled = status.swap_scheduled ? 1U : 0U;
41
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 fwu_target_bound = status.target_bound ? 1U : 0U;
42 }
43
44 #define FWU_SAMPLE(field, type) \
45 static void sample_##field(void *value) \
46 { \
47 sample_status(); \
48 *(type *)value = fwu_##field; \
49 }
50
51 ✗ FWU_SAMPLE(total_size, uint32_t)
52 ✗ FWU_SAMPLE(received, uint32_t)
53 2 FWU_SAMPLE(expected_crc32, uint32_t)
54 ✗ FWU_SAMPLE(actual_crc32, uint32_t)
55 ✗ FWU_SAMPLE(started, uint32_t)
56 ✗ FWU_SAMPLE(completed, uint32_t)
57 ✗ FWU_SAMPLE(failed, uint32_t)
58 2 FWU_SAMPLE(state, uint8_t)
59 2 FWU_SAMPLE(swap_scheduled, uint8_t)
60 ✗ FWU_SAMPLE(target_bound, uint8_t)
61
62 #if CONFIG_KFSW_FWU_LITE_CSP
63 static uint32_t fwu_lite_timeout_ms = CONFIG_KFSW_FWU_LITE_TIMEOUT_MS;
64 static uint8_t fwu_lite_retries = CONFIG_KFSW_FWU_LITE_BLOCK_RETRIES;
65 static uint16_t fwu_lite_block_size = CONFIG_KFSW_FWU_LITE_BLOCK_SIZE;
66
67 static void sample_lite_timeout(void *value)
68 {
69 *(uint32_t *)value = kfsw_fwu_lite_get_timeout_ms();
70 }
71
72 static void sample_lite_retries(void *value)
73 {
74 *(uint8_t *)value = kfsw_fwu_lite_get_retries();
75 }
76
77 static int validate_lite_timeout(const union kfsw_param_scalar *value)
78 {
79 return kfsw_fwu_lite_check_timeout_ms(value->u32);
80 }
81
82 static void apply_lite_timeout(const union kfsw_param_scalar *value)
83 {
84 (void)kfsw_fwu_lite_set_timeout_ms(value->u32);
85 }
86
87 static int validate_lite_retries(const union kfsw_param_scalar *value)
88 {
89 return (value->u8 == 0U) ? -EINVAL : 0;
90 }
91
92 static void apply_lite_retries(const union kfsw_param_scalar *value)
93 {
94 (void)kfsw_fwu_lite_set_retries(value->u8);
95 }
96 #endif
97
98 static const struct kfsw_param_definition fwu_param_definitions[] = {
99 {
100 .offset = 0x00U,
101 .type = KFSW_PARAM_U32,
102 .flags = KFSW_PARAM_FLAG_READ_ONLY,
103 .name = "fwu_total_size",
104 .unit = "B",
105 .description = "Image size the sender declared",
106 .value = &fwu_total_size,
107 .sample = sample_total_size,
108 },
109 {
110 .offset = 0x04U,
111 .type = KFSW_PARAM_U32,
112 .flags = KFSW_PARAM_FLAG_READ_ONLY,
113 .name = "fwu_received",
114 .unit = "B",
115 .description = "Bytes accepted so far",
116 .value = &fwu_received,
117 .sample = sample_received,
118 },
119 {
120 .offset = 0x08U,
121 .type = KFSW_PARAM_X32,
122 .flags = KFSW_PARAM_FLAG_READ_ONLY,
123 .name = "fwu_expected_crc",
124 .description = "CRC32 the sender declared; IEEE, not the CSP variant",
125 .value = &fwu_expected_crc32,
126 .sample = sample_expected_crc32,
127 },
128 {
129 .offset = 0x0cU,
130 .type = KFSW_PARAM_X32,
131 .flags = KFSW_PARAM_FLAG_READ_ONLY,
132 .name = "fwu_actual_crc",
133 .description = "CRC32 accumulated over what was accepted",
134 .value = &fwu_actual_crc32,
135 .sample = sample_actual_crc32,
136 },
137 {
138 .offset = 0x10U,
139 .type = KFSW_PARAM_U32,
140 .flags = KFSW_PARAM_FLAG_READ_ONLY,
141 .name = "fwu_started",
142 .description = "Transfers begun since boot",
143 .value = &fwu_started,
144 .sample = sample_started,
145 },
146 {
147 .offset = 0x14U,
148 .type = KFSW_PARAM_U32,
149 .flags = KFSW_PARAM_FLAG_READ_ONLY,
150 .name = "fwu_completed",
151 .description = "Transfers that reached ready since boot",
152 .value = &fwu_completed,
153 .sample = sample_completed,
154 },
155 {
156 .offset = 0x18U,
157 .type = KFSW_PARAM_U32,
158 .flags = KFSW_PARAM_FLAG_READ_ONLY,
159 .name = "fwu_failed",
160 .description = "Transfers that failed or were aborted since boot",
161 .value = &fwu_failed,
162 .sample = sample_failed,
163 },
164 {
165 .offset = 0x1cU,
166 .type = KFSW_PARAM_U8,
167 .flags = KFSW_PARAM_FLAG_READ_ONLY,
168 .name = "fwu_state",
169 .description = "idle, receiving, ready or failed",
170 .value = &fwu_state,
171 .sample = sample_state,
172 },
173 {
174 .offset = 0x1dU,
175 .type = KFSW_PARAM_U8,
176 .flags = KFSW_PARAM_FLAG_READ_ONLY,
177 .name = "fwu_swap_scheduled",
178 .description = "Whether the bootloader accepted the image",
179 .value = &fwu_swap_scheduled,
180 .sample = sample_swap_scheduled,
181 },
182 {
183 .offset = 0x1eU,
184 .type = KFSW_PARAM_U8,
185 .flags = KFSW_PARAM_FLAG_READ_ONLY,
186 .name = "fwu_target_bound",
187 .description = "Whether a target partition was bound at build time",
188 .value = &fwu_target_bound,
189 .sample = sample_target_bound,
190 },
191 #if CONFIG_KFSW_FWU_LITE_CSP
192 {
193 .offset = 0x20U,
194 .type = KFSW_PARAM_U32,
195 .flags = KFSW_PARAM_FLAG_CONFIGURATION,
196 .name = "fwu_lite_timeout_ms",
197 .unit = "ms",
198 .description = "Reply timeout for the next block",
199 .value = &fwu_lite_timeout_ms,
200 .default_value = {.u32 = CONFIG_KFSW_FWU_LITE_TIMEOUT_MS},
201 .validate = validate_lite_timeout,
202 .changed = apply_lite_timeout,
203 .sample = sample_lite_timeout,
204 },
205 {
206 .offset = 0x24U,
207 .type = KFSW_PARAM_U8,
208 .flags = KFSW_PARAM_FLAG_CONFIGURATION,
209 .name = "fwu_lite_retries",
210 .description = "Times a block is repeated before the transfer is abandoned",
211 .value = &fwu_lite_retries,
212 .default_value = {.u8 = CONFIG_KFSW_FWU_LITE_BLOCK_RETRIES},
213 .validate = validate_lite_retries,
214 .changed = apply_lite_retries,
215 .sample = sample_lite_retries,
216 },
217 {
218 .offset = 0x26U,
219 .type = KFSW_PARAM_U16,
220 /* Compile-time: it sizes the message buffers. */
221 .flags = KFSW_PARAM_FLAG_READ_ONLY,
222 .name = "fwu_lite_block_size",
223 .unit = "B",
224 .description = "Image bytes per block; fixed at build time",
225 .value = &fwu_lite_block_size,
226 .default_value = {.u16 = CONFIG_KFSW_FWU_LITE_BLOCK_SIZE},
227 },
228 #endif
229 };
230
231 KFSW_PARAM_TABLE_FITS_THE_LINK(fwu_param_definitions);
232
233 const struct kfsw_param_definition_set kfsw_fwu_param_definitions = {
234 .table = KFSW_FWU_PARAM_TABLE_ID,
235 .name = KFSW_FWU_PARAM_TABLE_NAME,
236 .description = "Firmware update state and checks",
237 .definitions = fwu_param_definitions,
238 .count = ARRAY_SIZE(fwu_param_definitions),
239 };
240