./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
376 / 416
90.4%
Branches:
245 / 297, 2 excl
82.5%
Functions:
34 / 35
97.1%

kfsw-services/src/fwu/fwu.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <psa/crypto.h>
3 #include <stdbool.h>
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <string.h>
7
8 #include <zephyr/devicetree.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/storage/flash_map.h>
11 #include <zephyr/storage/stream_flash.h>
12 #include <zephyr/sys/crc.h>
13 #include <zephyr/sys/util.h>
14
15 #if CONFIG_KFSW_FWU_MCUBOOT
16 #include <zephyr/dfu/mcuboot.h>
17 #endif
18
19 #include <kfsw/services/fwu.h>
20 #if CONFIG_KFSW_BOOT_STORE
21 #include <kfsw/services/boot_budget.h>
22 #endif
23 #include "fwu_internal.h"
24 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_FWU
25 #include <kfsw/services/log.h>
26
27 #define KFSW_FWU_PARTITION_NODE DT_CHOSEN(kfsw_fwu_partition)
28 #define KFSW_FWU_PARTITION_PRESENT DT_NODE_EXISTS(KFSW_FWU_PARTITION_NODE)
29
30 #if KFSW_FWU_PARTITION_PRESENT
31 #define KFSW_FWU_PARTITION_ID DT_FIXED_PARTITION_ID(KFSW_FWU_PARTITION_NODE)
32 #define KFSW_FWU_PARTITION_SIZE DT_REG_SIZE(KFSW_FWU_PARTITION_NODE)
33 #endif
34
35 #if CONFIG_KFSW_FWU_RECOVERY
36 BUILD_ASSERT(KFSW_FWU_PARTITION_PRESENT, "Recovery requires a target partition");
37 BUILD_ASSERT(DT_SAME_NODE(KFSW_FWU_PARTITION_NODE, DT_NODELABEL(slot0_partition)),
38 "Recovery must target primary A");
39 BUILD_ASSERT(DT_SAME_NODE(DT_CHOSEN(zephyr_code_partition), DT_NODELABEL(slot1_partition)),
40 "Recovery must execute from permanent Golden");
41 BUILD_ASSERT(DT_SAME_NODE(DT_MTD_FROM_FIXED_PARTITION(KFSW_FWU_PARTITION_NODE),
42 DT_MTD_FROM_FIXED_PARTITION(DT_CHOSEN(zephyr_code_partition))),
43 "Recovery partitions must share flash");
44 BUILD_ASSERT(DT_REG_ADDR(KFSW_FWU_PARTITION_NODE) + DT_REG_SIZE(KFSW_FWU_PARTITION_NODE) <=
45 DT_REG_ADDR(DT_CHOSEN(zephyr_code_partition)) ||
46 DT_REG_ADDR(DT_CHOSEN(zephyr_code_partition)) +
47 DT_REG_SIZE(DT_CHOSEN(zephyr_code_partition)) <=
48 DT_REG_ADDR(KFSW_FWU_PARTITION_NODE),
49 "Recovery code and target must not overlap");
50 BUILD_ASSERT(CONFIG_KFSW_FWU_SLOT_OFFSET_SECTORS == 0, "Recovery must not reserve a swap offset");
51 BUILD_ASSERT(!IS_ENABLED(CONFIG_KFSW_FWU_MCUBOOT) && !IS_ENABLED(CONFIG_KFSW_GOLDEN) &&
52 !IS_ENABLED(CONFIG_KFSW_SCRUB) && !IS_ENABLED(CONFIG_KFSW_FWU_FILES),
53 "Recovery cannot use secondary-slot services");
54 BUILD_ASSERT(IS_ENABLED(CONFIG_BOOTLOADER_MCUBOOT) ||
55 (IS_ENABLED(CONFIG_ZTEST) && IS_ENABLED(CONFIG_ARCH_POSIX)),
56 "Recovery requires MCUboot outside native tests");
57 #if CONFIG_BOOTLOADER_MCUBOOT
58 BUILD_ASSERT(IS_ENABLED(CONFIG_MCUBOOT_BOOTLOADER_MODE_FIRMWARE_UPDATER) &&
59 IS_ENABLED(CONFIG_MCUBOOT_APPLICATION_FIRMWARE_UPDATER),
60 "Recovery requires MCUboot firmware updater mode");
61 #endif
62 #endif
63
64 #if CONFIG_BOOTLOADER_MCUBOOT && CONFIG_KFSW_FWU_MCUBOOT && \
65 (CONFIG_KFSW_FWU_SLOT_OFFSET_SECTORS > 0)
66 BUILD_ASSERT(IS_ENABLED(CONFIG_MCUBOOT_BOOTLOADER_MODE_SWAP_USING_OFFSET),
67 "MCUboot swaps an image written past the slot start only in swap-using-offset mode");
68 #endif
69
70 /* Swap mode reserves the last sector for the bootloader trailer. */
71 #define KFSW_FWU_TRAILER_SECTORS (IS_ENABLED(CONFIG_KFSW_FWU_RECOVERY) ? 0U : 1U)
72
73 /* Serializes session lifetime with flash I/O so no owner can replace an active writer. */
74 K_MUTEX_DEFINE(fwu_lock);
75 static struct kfsw_fwu_status fwu_state = {
76 .state = KFSW_FWU_IDLE,
77 .target_bound = KFSW_FWU_PARTITION_PRESENT,
78 };
79 #if CONFIG_KFSW_FWU_LITE
80 /* Protected by fwu_lock with the canonical writer and generation. */
81 static uint8_t last_block[CONFIG_KFSW_FWU_LITE_BLOCK_SIZE];
82 static uint32_t last_offset;
83 static size_t last_size;
84 #endif
85 static unsigned int secondary_readers;
86 static uint32_t generation;
87 static struct kfsw_fwu_handle local_handle;
88 static const struct kfsw_fwu_owner local_owner = {.kind = KFSW_FWU_OWNER_LOCAL};
89
90 1508 static bool secondary_needed_for_revert(void)
91 {
92 #if CONFIG_KFSW_FWU_MCUBOOT
93 14 return mcuboot_swap_type() == BOOT_SWAP_TYPE_REVERT;
94 #else
95 1494 return false;
96 #endif
97 }
98
99 22 int kfsw_fwu_secondary_acquire(void)
100 {
101 22 k_mutex_lock(&fwu_lock, K_FOREVER);
102
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
22 if ((fwu_state.state == KFSW_FWU_RECEIVING) || (fwu_state.state == KFSW_FWU_FAILED)) {
103 4 k_mutex_unlock(&fwu_lock);
104 4 return -EBUSY;
105 }
106 18 secondary_readers++;
107 18 k_mutex_unlock(&fwu_lock);
108 18 return 0;
109 }
110
111 18 void kfsw_fwu_secondary_release(void)
112 {
113 18 k_mutex_lock(&fwu_lock, K_FOREVER);
114 18 __ASSERT_NO_MSG(secondary_readers > 0U);
115 18 secondary_readers--;
116 18 k_mutex_unlock(&fwu_lock);
117 18 }
118
119 #if KFSW_FWU_PARTITION_PRESENT
120 static uint8_t fwu_stream_buffer[CONFIG_KFSW_FWU_STREAM_BUFFER_SIZE];
121 static struct stream_flash_ctx fwu_stream;
122 static const struct flash_area *fwu_area;
123
124 /* Uses its own flash area handle instead of the shared one. */
125 3634 static int fwu_sector_size(void)
126 {
127 const struct flash_area *area;
128 const struct device *device;
129 struct flash_pages_info page;
130 int result;
131
132 3634 result = flash_area_open(KFSW_FWU_PARTITION_ID, &area);
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3634 times.
3634 if (result != 0) {
134 ✗ return result;
135 }
136
137 3634 device = flash_area_get_device(area);
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3634 times.
3634 if (device == NULL) {
139 ✗ flash_area_close(area);
140 ✗ return -ENODEV;
141 }
142
143 3634 result = flash_get_page_info_by_offs(device, area->fa_off, &page);
144 3634 flash_area_close(area);
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3634 times.
3634 if (result != 0) {
146 ✗ return result;
147 }
148
149 3634 return (int)page.size;
150 }
151 #endif /* KFSW_FWU_PARTITION_PRESENT */
152
153 3094 uint32_t kfsw_fwu_slot_write_offset(void)
154 {
155 #if KFSW_FWU_PARTITION_PRESENT && CONFIG_KFSW_FWU_SLOT_OFFSET_SECTORS > 0
156 2286 int sector = fwu_sector_size();
157
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2286 times.
2286 if (sector <= 0) {
159 ✗ return 0U;
160 }
161
162 2286 return (uint32_t)sector * CONFIG_KFSW_FWU_SLOT_OFFSET_SECTORS;
163 #else
164 808 return 0U;
165 #endif
166 }
167
168 1348 uint32_t kfsw_fwu_max_image_size(void)
169 {
170 #if KFSW_FWU_PARTITION_PRESENT
171 1348 uint32_t offset = kfsw_fwu_slot_write_offset();
172 uint32_t trailer;
173 1348 int sector = fwu_sector_size();
174
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1348 times.
1348 if (sector <= 0) {
176 ✗ return 0U;
177 }
178
179 1348 trailer = (uint32_t)sector * KFSW_FWU_TRAILER_SECTORS;
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1348 times.
1348 if ((offset + trailer) >= KFSW_FWU_PARTITION_SIZE) {
181 ✗ return 0U;
182 }
183
184 1348 return KFSW_FWU_PARTITION_SIZE - offset - trailer;
185 #else
186 ✗ return 0U;
187 #endif
188 }
189
190 #if KFSW_FWU_PARTITION_PRESENT
191 /* Caller holds fwu_lock. */
192 874 static void fwu_close_stream(void)
193 {
194
2/2
✓ Branch 0 taken 628 times.
✓ Branch 1 taken 246 times.
874 if (fwu_area != NULL) {
195 628 flash_area_close(fwu_area);
196 628 fwu_area = NULL;
197 }
198 874 }
199
200 /* Caller holds fwu_lock. Leaves the slot erased. */
201 1362 static int fwu_erase_slot(void)
202 {
203 int result;
204
205 1362 result = flash_area_open(KFSW_FWU_PARTITION_ID, &fwu_area);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1362 times.
1362 if (result != 0) {
207 ✗ return result;
208 }
209
210 1362 result = flash_area_flatten(fwu_area, 0, KFSW_FWU_PARTITION_SIZE);
211
2/2
✓ Branch 0 taken 1292 times.
✓ Branch 1 taken 70 times.
1362 if (result == 0) {
212 1292 fwu_state.committed = 0U;
213 }
214 1362 flash_area_close(fwu_area);
215 1362 fwu_area = NULL;
216
217 1362 return result;
218 }
219 #endif /* KFSW_FWU_PARTITION_PRESENT */
220
221 658 static int fwu_begin_locked(uint32_t total_size, uint32_t expected_crc32)
222 {
223 #if KFSW_FWU_PARTITION_PRESENT
224 uint32_t maximum;
225 uint32_t write_offset;
226 int result;
227
228 658 maximum = kfsw_fwu_max_image_size();
229
230 658 result = fwu_erase_slot();
231
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 634 times.
658 if (result != 0) {
232 /* Log why the image was refused. */
233 24 kfsw_log_error("Firmware update: could not erase the slot (%d)", result);
234 24 fwu_state.state = KFSW_FWU_FAILED;
235 24 fwu_state.failed++;
236 24 return result;
237 }
238
239 634 write_offset = kfsw_fwu_slot_write_offset();
240
241 634 result = flash_area_open(KFSW_FWU_PARTITION_ID, &fwu_area);
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 if (result != 0) {
243 ✗ kfsw_log_error("Firmware update: could not open the slot (%d)", result);
244 ✗ fwu_state.state = KFSW_FWU_FAILED;
245 ✗ fwu_state.failed++;
246 ✗ return result;
247 }
248
249 /* The stream writer gets the whole region, which is a multiple of the write
250 * block size; the image size limit is checked here instead.
251 */
252 634 result = stream_flash_init(&fwu_stream, flash_area_get_device(fwu_area), fwu_stream_buffer,
253 634 sizeof(fwu_stream_buffer), fwu_area->fa_off + write_offset,
254 maximum, NULL);
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 if (result != 0) {
256 ✗ kfsw_log_error("Firmware update: could not prepare the slot for writing (%d)",
257 result);
258 ✗ fwu_close_stream();
259 ✗ fwu_state.state = KFSW_FWU_FAILED;
260 ✗ fwu_state.failed++;
261 ✗ return result;
262 }
263
264 kfsw_log_debug("Firmware update slot ready: offset %u, %u bytes usable", write_offset,
265 maximum);
266 634 fwu_state.state = KFSW_FWU_RECEIVING;
267 634 fwu_state.started++;
268
269 634 kfsw_log_info("Firmware update started: %u bytes, crc32=%08x", total_size, expected_crc32);
270 634 return 0;
271 #else
272 ARG_UNUSED(total_size);
273 ARG_UNUSED(expected_crc32);
274 ✗ return -ENODEV;
275 #endif
276 }
277
278 11530 static int fwu_write_locked(uint32_t offset, const void *data, size_t size)
279 {
280 #if KFSW_FWU_PARTITION_PRESENT
281 int result;
282
283
4/4
✓ Branch 0 taken 11526 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 11522 times.
11530 if ((data == NULL) || (size == 0U)) {
284 8 return -EINVAL;
285 }
286
287
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 11506 times.
11522 if (fwu_state.state != KFSW_FWU_RECEIVING) {
288 16 return -EINVAL;
289 }
290
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 11490 times.
11506 if (offset != fwu_state.received) {
291 16 return -ESPIPE;
292 }
293
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 11478 times.
11490 if (size > fwu_state.total_size - offset) {
294 12 return -EFBIG;
295 }
296
297 11478 result = stream_flash_buffered_write(&fwu_stream, data, size, false);
298 11478 fwu_state.committed = MIN(fwu_stream.bytes_written, fwu_state.total_size);
299
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 11470 times.
11478 if (result != 0) {
300 8 fwu_state.state = KFSW_FWU_FAILED;
301 8 fwu_state.failed++;
302 8 return result;
303 }
304
305 11470 fwu_state.actual_crc32 = crc32_ieee_update(fwu_state.actual_crc32, data, size);
306 11470 fwu_state.received += size;
307
308 11470 return 0;
309 #else
310 ARG_UNUSED(offset);
311 ARG_UNUSED(data);
312 ARG_UNUSED(size);
313 ✗ return -ENODEV;
314 #endif
315 }
316
317 #if KFSW_FWU_PARTITION_PRESENT
318 /* Caller holds fwu_lock. Reads only image bytes, excluding stream padding. */
319 170 static int fwu_check_stored_image(void)
320 {
321 uint8_t chunk[128];
322 uint8_t digest[KFSW_FWU_SHA256_SIZE];
323 size_t digest_size;
324 170 psa_hash_operation_t hash = PSA_HASH_OPERATION_INIT;
325 170 uint32_t write_offset = kfsw_fwu_slot_write_offset();
326 170 uint32_t crc = 0U;
327 170 uint32_t offset = 0U;
328 int result;
329
330
3/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
208 if (fwu_state.image.digest_present &&
331 38 (psa_hash_setup(&hash, PSA_ALG_SHA_256) != PSA_SUCCESS)) {
332 ✗ return -EIO;
333 }
334
2/2
✓ Branch 0 taken 16542 times.
✓ Branch 1 taken 162 times.
16704 while (offset < fwu_state.total_size) {
335 16542 size_t size = MIN(sizeof(chunk), fwu_state.total_size - offset);
336
337 16542 result = flash_area_read(fwu_area, write_offset + offset, chunk, size);
338
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 16534 times.
16542 if (result != 0) {
339 8 (void)psa_hash_abort(&hash);
340 8 return result;
341 }
342
3/4
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 16408 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 126 times.
16660 if (fwu_state.image.digest_present &&
343 126 (psa_hash_update(&hash, chunk, size) != PSA_SUCCESS)) {
344 ✗ (void)psa_hash_abort(&hash);
345 ✗ return -EIO;
346 }
347 16534 crc = crc32_ieee_update(crc, chunk, size);
348 16534 offset += size;
349 }
350
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 128 times.
162 if (fwu_state.image.digest_present) {
351 34 psa_status_t result = psa_hash_finish(&hash, digest, sizeof(digest), &digest_size);
352
353 34 (void)psa_hash_abort(&hash);
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (result != PSA_SUCCESS) {
355 ✗ return -EIO;
356 }
357
2/4
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
52 if ((digest_size != sizeof(digest)) ||
358
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
34 (memcmp(digest, fwu_state.image.sha256, sizeof(digest)) != 0)) {
359 4 return -EILSEQ;
360 }
361 }
362
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 4 times.
158 return crc == fwu_state.expected_crc32 ? 0 : -EILSEQ;
363 }
364
365 /* Caller holds fwu_lock. Verification makes buffered bytes readable without
366 * scheduling an upgrade; both upload protocols use this boundary.
367 */
368 254 static int fwu_verify_locked(void)
369 {
370 int result;
371
372
3/4
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 210 times.
254 if ((fwu_state.state == KFSW_FWU_VERIFIED) || (fwu_state.state == KFSW_FWU_READY)) {
373 44 return 0;
374 }
375
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 206 times.
210 if (fwu_state.state != KFSW_FWU_RECEIVING) {
376 4 return -EINVAL;
377 }
378
379
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 186 times.
206 if (fwu_state.received != fwu_state.total_size) {
380 20 return -EAGAIN;
381 }
382
383
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 178 times.
186 if (fwu_state.actual_crc32 != fwu_state.expected_crc32) {
384 8 kfsw_log_error("Firmware update rejected: crc32 %08x, expected %08x",
385 fwu_state.actual_crc32, fwu_state.expected_crc32);
386 8 fwu_close_stream();
387 8 result = fwu_erase_slot();
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (result != 0) {
389 ✗ kfsw_log_error("Firmware update cleanup failed (%d)", result);
390 }
391 8 fwu_state.state = KFSW_FWU_FAILED;
392 8 fwu_state.failed++;
393 8 return -EILSEQ;
394 }
395
396 /* Flush whatever is still buffered before anything reads the slot. */
397 178 result = stream_flash_buffered_write(&fwu_stream, NULL, 0, true);
398 178 fwu_state.committed = MIN(fwu_stream.bytes_written, fwu_state.total_size);
399
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 8 times.
178 if (result == 0) {
400 170 result = fwu_check_stored_image();
401 }
402
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 154 times.
178 if (result != 0) {
403 24 fwu_close_stream();
404
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 16 times.
24 if (result == -EILSEQ) {
405 8 int cleanup = fwu_erase_slot();
406
407
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (cleanup != 0) {
408 4 kfsw_log_error("Firmware update cleanup failed (%d)", cleanup);
409 }
410 }
411 24 fwu_state.state = KFSW_FWU_FAILED;
412 24 fwu_state.failed++;
413 24 return result;
414 }
415 154 fwu_close_stream();
416 154 fwu_state.committed = fwu_state.total_size;
417 154 fwu_state.state = KFSW_FWU_VERIFIED;
418 154 return 0;
419 }
420 #else
421 ✗ static int fwu_verify_locked(void)
422 {
423 ✗ return -ENODEV;
424 }
425 #endif
426
427 186 static int fwu_finish_locked(void)
428 {
429 #if KFSW_FWU_PARTITION_PRESENT
430 int result;
431
432
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 164 times.
186 if (fwu_state.state == KFSW_FWU_READY) {
433 22 return 0;
434 }
435
3/4
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 160 times.
164 if ((secondary_readers != 0U) || secondary_needed_for_revert()) {
436 4 return -EBUSY;
437 }
438 160 result = fwu_verify_locked();
439
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 136 times.
160 if (result != 0) {
440 24 return result;
441 }
442
443 #if CONFIG_KFSW_FWU_MCUBOOT
444 4 result = boot_request_upgrade(BOOT_UPGRADE_TEST);
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (result != 0) {
446 ✗ fwu_state.state = KFSW_FWU_FAILED;
447 ✗ fwu_state.failed++;
448 ✗ return result;
449 }
450
451 /* Check that the bootloader scheduled the swap; otherwise it boots the old image. */
452
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (mcuboot_swap_type() != BOOT_SWAP_TYPE_TEST) {
453 ✗ kfsw_log_error("Firmware update: no swap was scheduled");
454 ✗ fwu_state.state = KFSW_FWU_FAILED;
455 ✗ fwu_state.failed++;
456 ✗ return -EIO;
457 }
458 4 fwu_state.swap_scheduled = true;
459 #endif
460
461 136 fwu_state.state = KFSW_FWU_READY;
462 136 fwu_state.completed++;
463
464 #if CONFIG_KFSW_BOOT_STORE
465 4 result = kfsw_boot_budget_refill();
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (result != 0) {
467 /* The installed image remains usable even if the budget store fails. */
468 ✗ kfsw_log_error("Firmware installed but boot budget refill failed (%d)", result);
469 }
470 #endif
471
472 136 kfsw_log_info("Firmware update ready: %u bytes accepted", fwu_state.total_size);
473 136 return 0;
474 #else
475 ✗ return -ENODEV;
476 #endif
477 }
478
479 694 static int fwu_abort_locked(void)
480 {
481
3/4
✓ Branch 0 taken 688 times.
✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 688 times.
694 if ((secondary_readers != 0U) || secondary_needed_for_revert()) {
482 6 return -EBUSY;
483 }
484
485 #if KFSW_FWU_PARTITION_PRESENT
486 int result;
487
488
2/2
✓ Branch 0 taken 434 times.
✓ Branch 1 taken 254 times.
688 if (fwu_state.state == KFSW_FWU_RECEIVING) {
489 434 fwu_state.failed++;
490 }
491 688 fwu_close_stream();
492 688 result = fwu_erase_slot();
493
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 646 times.
688 if (result != 0) {
494 42 fwu_state.state = KFSW_FWU_FAILED;
495 42 return result;
496 }
497 #endif
498
499 646 fwu_state.aborted = true;
500 646 fwu_state.committed = 0U;
501 646 fwu_state.total_size = 0U;
502 646 fwu_state.received = 0U;
503 646 fwu_state.expected_crc32 = 0U;
504 646 fwu_state.actual_crc32 = 0U;
505 646 fwu_state.swap_scheduled = false;
506 646 fwu_state.state = KFSW_FWU_IDLE;
507
508 646 return 0;
509 }
510
511 24756 static bool owner_equal(const struct kfsw_fwu_owner *a, const struct kfsw_fwu_owner *b)
512 {
513
4/4
✓ Branch 0 taken 24648 times.
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 24598 times.
✓ Branch 3 taken 50 times.
24756 return (a->kind == b->kind) && (a->peer == b->peer);
514 }
515
516 68 static bool image_equal(const struct kfsw_fwu_image *a, const struct kfsw_fwu_image *b)
517 {
518
3/4
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 4 times.
68 return a->digest_present && b->digest_present && (a->size == b->size) &&
519
6/8
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 56 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 24 times.
✗ Branch 7 not taken.
192 (a->crc32 == b->crc32) && (a->block_size == b->block_size) &&
520
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 12 times.
56 (memcmp(a->sha256, b->sha256, sizeof(a->sha256)) == 0);
521 }
522
523 /* Identity checks and the operation share fwu_lock, including local adapters. */
524 13006 static int fwu_check_handle(const struct kfsw_fwu_handle *handle)
525 {
526
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 12974 times.
13006 if (!owner_equal(&handle->owner, &fwu_state.handle.owner)) {
527 32 return -EACCES;
528 }
529
3/4
✓ Branch 0 taken 12974 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12898 times.
✓ Branch 3 taken 76 times.
12974 if ((handle->generation == 0U) || (handle->generation != fwu_state.handle.generation) ||
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12898 times.
12898 (handle->transfer_id != fwu_state.handle.transfer_id)) {
531 76 return -ESTALE;
532 }
533 12898 return 0;
534 }
535
536 /* Snapshot is copied before dropping the lock, including failed operations. */
537 2892 static int fwu_snapshot_unlock(int result, struct kfsw_fwu_status *status)
538 {
539 2892 *status = fwu_state;
540 2892 k_mutex_unlock(&fwu_lock);
541 2892 return result;
542 }
543
544 840 static int fwu_session_begin_locked(const struct kfsw_fwu_owner *owner,
545 const struct kfsw_fwu_image *image, uint32_t transfer_id,
546 struct kfsw_fwu_handle *handle)
547 {
548
2/2
✓ Branch 0 taken 836 times.
✓ Branch 1 taken 4 times.
840 if ((owner->kind < KFSW_FWU_OWNER_LOCAL) || (owner->kind > KFSW_FWU_OWNER_LITE) ||
549
2/2
✓ Branch 0 taken 828 times.
✓ Branch 1 taken 8 times.
836 (image->size == 0U) ||
550
6/6
✓ Branch 0 taken 502 times.
✓ Branch 1 taken 326 times.
✓ Branch 2 taken 498 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 494 times.
✓ Branch 5 taken 4 times.
828 (image->digest_present && ((owner->kind != KFSW_FWU_OWNER_LITE) ||
551
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 490 times.
494 (transfer_id == 0U) || (image->block_size == 0U)))) {
552 24 return -EINVAL;
553 }
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 816 times.
816 if (!fwu_state.target_bound) {
555 ✗ return -ENODEV;
556 }
557
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 670 times.
816 if ((fwu_state.state != KFSW_FWU_IDLE) &&
558
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
146 !((fwu_state.state == KFSW_FWU_READY) && !fwu_state.image.digest_present &&
559
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
4 !image->digest_present && owner_equal(owner, &fwu_state.handle.owner))) {
560
4/4
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 38 times.
280 if ((fwu_state.state != KFSW_FWU_FAILED) &&
561 138 owner_equal(owner, &fwu_state.handle.owner) &&
562
4/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 24 times.
168 (transfer_id == fwu_state.handle.transfer_id) &&
563 68 image_equal(image, &fwu_state.image)) {
564 44 *handle = fwu_state.handle;
565 44 return 0;
566 }
567 98 return -EBUSY;
568 }
569
6/6
✓ Branch 0 taken 644 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 396 times.
✓ Branch 3 taken 248 times.
✓ Branch 4 taken 372 times.
✓ Branch 5 taken 24 times.
1070 if (fwu_state.aborted && (transfer_id != 0U) &&
570 396 owner_equal(owner, &fwu_state.handle.owner) &&
571
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 364 times.
372 (transfer_id == fwu_state.handle.transfer_id)) {
572 8 return -ESTALE;
573 }
574
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 660 times.
666 if (image->size > kfsw_fwu_max_image_size()) {
575 6 return -EFBIG;
576 }
577
3/4
✓ Branch 0 taken 660 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 658 times.
660 if ((secondary_readers != 0U) || secondary_needed_for_revert()) {
578 2 return -EBUSY;
579 }
580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 658 times.
658 if (generation == UINT32_MAX) {
581 ✗ return -EOVERFLOW;
582 }
583 658 generation++;
584 #if CONFIG_KFSW_FWU_LITE
585 366 last_size = 0U;
586 #endif
587 658 fwu_state.handle.owner = *owner;
588
2/2
✓ Branch 0 taken 402 times.
✓ Branch 1 taken 256 times.
658 fwu_state.handle.transfer_id = transfer_id != 0U ? transfer_id : generation;
589 658 fwu_state.handle.generation = generation;
590 658 fwu_state.image = *image;
591 658 fwu_state.aborted = false;
592 658 fwu_state.committed = 0U;
593 658 fwu_state.total_size = image->size;
594 658 fwu_state.expected_crc32 = image->crc32;
595 658 fwu_state.received = 0U;
596 658 fwu_state.actual_crc32 = 0U;
597 658 fwu_state.swap_scheduled = false;
598 658 *handle = fwu_state.handle;
599 658 return fwu_begin_locked(image->size, image->crc32);
600 }
601
602 624 int kfsw_fwu_session_begin(const struct kfsw_fwu_owner *owner, const struct kfsw_fwu_image *image,
603 uint32_t transfer_id, struct kfsw_fwu_handle *handle,
604 struct kfsw_fwu_status *status)
605 {
606 int result;
607
608
6/8
✓ Branch 0 taken 620 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 616 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 616 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 616 times.
624 if ((owner == NULL) || (image == NULL) || (handle == NULL) || (status == NULL)) {
609 8 return -EINVAL;
610 }
611 616 k_mutex_lock(&fwu_lock, K_FOREVER);
612 616 result = fwu_session_begin_locked(owner, image, transfer_id, handle);
613 616 return fwu_snapshot_unlock(result, status);
614 }
615
616 870 int kfsw_fwu_session_write(const struct kfsw_fwu_handle *handle, uint32_t offset, const void *data,
617 size_t size, struct kfsw_fwu_status *status)
618 {
619 int result;
620
621
3/4
✓ Branch 0 taken 866 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 866 times.
870 if ((handle == NULL) || (status == NULL)) {
622 4 return -EINVAL;
623 }
624 866 k_mutex_lock(&fwu_lock, K_FOREVER);
625 866 result = fwu_check_handle(handle);
626 #if CONFIG_KFSW_FWU_LITE
627
3/4
✓ Branch 0 taken 712 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 712 times.
✗ Branch 3 not taken.
728 if ((result == 0) && fwu_state.image.digest_present) {
628
3/4
✓ Branch 0 taken 704 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 704 times.
712 if (fwu_state.state != KFSW_FWU_RECEIVING || fwu_state.aborted) {
629 8 return fwu_snapshot_unlock(-EINVAL, status);
630 }
631
3/6
✓ Branch 0 taken 704 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 704 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 704 times.
✗ Branch 5 not taken.
704 if ((data == NULL) || (size == 0U) || (size > sizeof(last_block)) ||
632
2/4
✓ Branch 0 taken 704 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 704 times.
✗ Branch 3 not taken.
704 (size > fwu_state.image.size) || (offset > fwu_state.image.size - size) ||
633
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 492 times.
704 ((size != fwu_state.image.block_size) &&
634
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 204 times.
212 (offset + size != fwu_state.image.size))) {
635 8 return fwu_snapshot_unlock(-EINVAL, status);
636 }
637
4/4
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 246 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 396 times.
696 if ((last_size != 0U) && (offset == last_offset)) {
638
2/3
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
54 result = ((size == last_size) && (memcmp(data, last_block, size) == 0))
639 ? 0
640
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
108 : -EILSEQ;
641 54 return fwu_snapshot_unlock(result, status);
642 }
643 }
644 #endif
645
2/2
✓ Branch 0 taken 772 times.
✓ Branch 1 taken 24 times.
796 if (result == 0) {
646 772 result = fwu_write_locked(offset, data, size);
647 #if CONFIG_KFSW_FWU_LITE
648
3/4
✓ Branch 0 taken 632 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 632 times.
✗ Branch 3 not taken.
642 if ((result == 0) && fwu_state.image.digest_present) {
649 632 memcpy(last_block, data, size);
650 632 last_size = size;
651 632 last_offset = offset;
652 }
653 #endif
654 }
655 796 return fwu_snapshot_unlock(result, status);
656 }
657
658 96 int kfsw_fwu_session_verify(const struct kfsw_fwu_handle *handle, struct kfsw_fwu_status *status)
659 {
660 int result;
661
662
3/4
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 92 times.
96 if ((handle == NULL) || (status == NULL)) {
663 4 return -EINVAL;
664 }
665 92 k_mutex_lock(&fwu_lock, K_FOREVER);
666 92 result = fwu_check_handle(handle);
667
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 14 times.
92 if (result == 0) {
668 78 result = fwu_verify_locked();
669 }
670 92 return fwu_snapshot_unlock(result, status);
671 }
672
673 98 int kfsw_fwu_session_activate(const struct kfsw_fwu_handle *handle, struct kfsw_fwu_status *status)
674 {
675 int result;
676
677
3/4
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 94 times.
98 if ((handle == NULL) || (status == NULL)) {
678 4 return -EINVAL;
679 }
680 94 k_mutex_lock(&fwu_lock, K_FOREVER);
681 94 result = fwu_check_handle(handle);
682
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 14 times.
94 if (result == 0) {
683
4/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 18 times.
80 if ((fwu_state.state != KFSW_FWU_VERIFIED) && (fwu_state.state != KFSW_FWU_READY)) {
684 30 result = -EINVAL;
685 } else {
686 50 result = fwu_finish_locked();
687 }
688 }
689 94 return fwu_snapshot_unlock(result, status);
690 }
691
692 902 int kfsw_fwu_session_abort(const struct kfsw_fwu_handle *handle, struct kfsw_fwu_status *status)
693 {
694 int result;
695
696
3/4
✓ Branch 0 taken 898 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 898 times.
902 if ((handle == NULL) || (status == NULL)) {
697 4 return -EINVAL;
698 }
699 898 k_mutex_lock(&fwu_lock, K_FOREVER);
700 898 result = fwu_check_handle(handle);
701
4/4
✓ Branch 0 taken 866 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 584 times.
✓ Branch 3 taken 282 times.
898 if ((result == 0) && !fwu_state.aborted) {
702 584 result = fwu_abort_locked();
703 }
704 898 return fwu_snapshot_unlock(result, status);
705 }
706
707 314 int kfsw_fwu_session_lookup(const struct kfsw_fwu_owner *owner, struct kfsw_fwu_status *status)
708 {
709 int result;
710
711
3/4
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 306 times.
314 if ((owner == NULL) || (status == NULL)) {
712 8 return -EINVAL;
713 }
714 306 k_mutex_lock(&fwu_lock, K_FOREVER);
715
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 198 times.
300 result = fwu_state.handle.generation == 0U || fwu_state.aborted ? -ENOENT
716
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 6 times.
708 : owner_equal(owner, &fwu_state.handle.owner) ? 0
717
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 14 times.
102 : -EACCES;
718 306 return fwu_snapshot_unlock(result, status);
719 }
720
721 24 int kfsw_fwu_session_status(const struct kfsw_fwu_handle *handle, struct kfsw_fwu_status *status)
722 {
723 int result;
724
725
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
24 if ((handle == NULL) || (status == NULL)) {
726 4 return -EINVAL;
727 }
728 20 k_mutex_lock(&fwu_lock, K_FOREVER);
729 20 result = fwu_check_handle(handle);
730 20 return fwu_snapshot_unlock(result, status);
731 }
732
733 224 int kfsw_fwu_begin(uint32_t total_size, uint32_t expected_crc32)
734 {
735 224 const struct kfsw_fwu_image image = {.size = total_size, .crc32 = expected_crc32};
736 224 struct kfsw_fwu_handle handle = {0};
737 int result;
738
739 /* The local handle must be installed before a concurrent local operation. */
740 224 k_mutex_lock(&fwu_lock, K_FOREVER);
741 224 result = fwu_session_begin_locked(&local_owner, &image, 0U, &handle);
742
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 36 times.
224 if (handle.generation != 0U) {
743 188 local_handle = handle;
744 }
745 224 k_mutex_unlock(&fwu_lock);
746 224 return result;
747 }
748
749 10942 static int fwu_check_local(void)
750 {
751
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 10930 times.
10942 if (!owner_equal(&local_owner, &fwu_state.handle.owner)) {
752 12 return -EACCES;
753 }
754
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10922 times.
10930 if (fwu_state.state == KFSW_FWU_IDLE) {
755 8 return -EINVAL;
756 }
757 10922 return fwu_check_handle(&local_handle);
758 }
759
760 10770 int kfsw_fwu_write(uint32_t offset, const void *data, size_t size)
761 {
762 int result;
763
764 10770 k_mutex_lock(&fwu_lock, K_FOREVER);
765 10770 result = fwu_check_local();
766
2/2
✓ Branch 0 taken 10758 times.
✓ Branch 1 taken 12 times.
10770 if (result == 0) {
767 10758 result = fwu_write_locked(offset, data, size);
768 }
769 10770 k_mutex_unlock(&fwu_lock);
770 10770 return result;
771 }
772
773 24 int kfsw_fwu_verify(void)
774 {
775 int result;
776
777 24 k_mutex_lock(&fwu_lock, K_FOREVER);
778 24 result = fwu_check_local();
779
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 if (result == 0) {
780 16 result = fwu_verify_locked();
781 }
782 24 k_mutex_unlock(&fwu_lock);
783 24 return result;
784 }
785
786 148 int kfsw_fwu_finish(void)
787 {
788 int result;
789
790 148 k_mutex_lock(&fwu_lock, K_FOREVER);
791 148 result = fwu_check_local();
792
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 12 times.
148 if (result == 0) {
793 136 result = fwu_finish_locked();
794 }
795 148 k_mutex_unlock(&fwu_lock);
796 148 return result;
797 }
798
799 168 int kfsw_fwu_abort(void)
800 {
801 int result;
802
803 168 k_mutex_lock(&fwu_lock, K_FOREVER);
804
2/2
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 130 times.
168 if (!owner_equal(&local_owner, &fwu_state.handle.owner)) {
805 38 result = -EACCES;
806
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 114 times.
130 } else if (fwu_state.aborted) {
807 16 result = 0;
808 } else {
809
1/2
✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
114 result = fwu_state.handle.generation == 0U ? 0 : fwu_check_handle(&local_handle);
810
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 4 times.
114 if (result == 0) {
811 110 result = fwu_abort_locked();
812 }
813 }
814 168 k_mutex_unlock(&fwu_lock);
815 168 return result;
816 }
817
818 1106 int kfsw_fwu_get_status(struct kfsw_fwu_status *status)
819 {
820
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1102 times.
1106 if (status == NULL) {
821 4 return -EINVAL;
822 }
823
824 1102 k_mutex_lock(&fwu_lock, K_FOREVER);
825 1102 *status = fwu_state;
826 1102 k_mutex_unlock(&fwu_lock);
827
828 1102 return 0;
829 }
830
831 20 const char *kfsw_fwu_state_name(enum kfsw_fwu_state state)
832 {
833
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
20 switch (state) {
834 4 case KFSW_FWU_IDLE:
835 4 return "idle";
836 4 case KFSW_FWU_RECEIVING:
837 4 return "receiving";
838 4 case KFSW_FWU_READY:
839 4 return "ready";
840 4 case KFSW_FWU_FAILED:
841 4 return "failed";
842 ✗ case KFSW_FWU_VERIFIED:
843 ✗ return "verified";
844 4 default:
845 4 return "unknown";
846 }
847 }
848