./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
240 / 284
84.5%
Branches:
187 / 260
71.9%
Functions:
20 / 20
100.0%

kfsw-services/src/fwu/fwu_lite_client.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <string.h>
3 #include <psa/crypto.h>
4 #include <csp/csp.h>
5 #include <kfsw/comms/csp.h>
6 #include <kfsw/platform/storage.h>
7 #include <kfsw/services/fwu_lite.h>
8 #include <zephyr/fs/fs.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/sys/atomic.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/sys/crc.h>
13 #include <zephyr/sys/util.h>
14 #if CONFIG_KFSW_FWU_LITE_HOST_FILES
15 #include <nsi_host_trampolines.h>
16 #endif
17
18 #define CONNECTION_OPTIONS \
19 ((CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION) | \
20 (IS_ENABLED(CONFIG_KFSW_FWU_LITE_RDP) ? CSP_O_RDP : 0))
21 #define STALE_LIMIT 16U
22 static atomic_t request_sequence;
23
24 1050 static int next_request(struct kfsw_fwu_lite_message *request, uint8_t opcode,
25 const struct kfsw_fwu_handle *handle)
26 {
27 atomic_val_t previous;
28
29 /* Saturate rather than reusing a request identity during this boot. */
30 do {
31 1050 previous = atomic_get(&request_sequence);
32
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1050 times.
1050 if ((uint32_t)previous == UINT32_MAX) {
33 ✗ return -EOVERFLOW;
34 }
35
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1050 times.
1050 } while (!atomic_cas(&request_sequence, previous, (atomic_val_t)((uint32_t)previous + 1U)));
36 1050 *request = (struct kfsw_fwu_lite_message){.version = KFSW_FWU_LITE_VERSION,
37 .direction = KFSW_FWU_LITE_REQUEST,
38 .opcode = opcode,
39 1050 .request_id = (uint32_t)previous + 1U};
40
2/2
✓ Branch 0 taken 568 times.
✓ Branch 1 taken 482 times.
1050 if (handle != NULL) {
41 568 request->transfer_id = handle->transfer_id;
42 568 request->generation = handle->generation;
43 }
44 1050 return 0;
45 }
46
47 1312 static int remote_error(uint8_t status)
48 {
49
6/10
✓ Branch 0 taken 1080 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 2 times.
1312 switch (status) {
50 1080 case KFSW_FWU_LITE_STATUS_OK:
51 1080 return 0;
52 200 case KFSW_FWU_LITE_STATUS_NOT_FOUND:
53 200 return -ENOENT;
54 6 case KFSW_FWU_LITE_STATUS_OWNER:
55 6 return -EACCES;
56 18 case KFSW_FWU_LITE_STATUS_STALE:
57 18 return -ESTALE;
58 ✗ case KFSW_FWU_LITE_STATUS_BUSY:
59 ✗ return -EBUSY;
60 ✗ case KFSW_FWU_LITE_STATUS_BAD_BLOCK:
61 case KFSW_FWU_LITE_STATUS_BAD_IMAGE:
62 ✗ return -EILSEQ;
63 ✗ case KFSW_FWU_LITE_STATUS_OUT_OF_ORDER:
64 ✗ return -ESPIPE;
65 ✗ case KFSW_FWU_LITE_STATUS_TOO_LARGE:
66 ✗ return -EFBIG;
67 6 case KFSW_FWU_LITE_STATUS_INVALID:
68 6 return -EINVAL;
69 2 default:
70 2 return -EIO;
71 }
72 }
73
74 1312 static bool correlated(const struct kfsw_fwu_lite_message *request,
75 const struct kfsw_fwu_lite_message *reply, struct kfsw_fwu_status *status,
76 const struct kfsw_fwu_image *image, uint32_t accepted, uint32_t crc)
77 {
78
3/4
✓ Branch 0 taken 1312 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1300 times.
✓ Branch 3 taken 12 times.
1312 if (reply->direction != KFSW_FWU_LITE_REPLY || reply->opcode != request->opcode ||
79
4/4
✓ Branch 0 taken 1066 times.
✓ Branch 1 taken 234 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 1054 times.
1300 reply->request_id != request->request_id || kfsw_fwu_lite_receipt(reply, status) != 0) {
80 258 return false;
81 }
82
4/4
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 274 times.
✓ Branch 2 taken 774 times.
✓ Branch 3 taken 6 times.
1054 if ((request->transfer_id != 0U && reply->transfer_id != request->transfer_id) ||
83
4/4
✓ Branch 0 taken 592 times.
✓ Branch 1 taken 456 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 574 times.
1048 (request->generation != 0U && reply->generation != request->generation)) {
84 24 return false;
85 }
86
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 798 times.
1030 if (reply->status != KFSW_FWU_LITE_STATUS_OK) {
87 232 return true;
88 }
89
3/6
✓ Branch 0 taken 798 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 798 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 798 times.
✗ Branch 5 not taken.
798 if (reply->transfer_id == 0U || reply->generation == 0U || !status->image.digest_present ||
90
2/4
✓ Branch 0 taken 798 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 798 times.
✗ Branch 3 not taken.
798 status->image.block_size != KFSW_FWU_LITE_MAX_BLOCK_SIZE || status->image.size == 0U ||
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 798 times.
798 status->aborted != (request->opcode == KFSW_FWU_LITE_OP_ABORT)) {
92 ✗ return false;
93 }
94
4/4
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 762 times.
798 if (((status->state == KFSW_FWU_VERIFIED || status->state == KFSW_FWU_READY) &&
95
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
36 (status->received != status->image.size || status->committed != status->image.size ||
96
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 status->actual_crc32 != status->image.crc32)) ||
97
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 792 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
792 (status->swap_scheduled && status->state != KFSW_FWU_READY) ||
98
3/4
✓ Branch 0 taken 510 times.
✓ Branch 1 taken 282 times.
✓ Branch 2 taken 510 times.
✗ Branch 3 not taken.
792 (request->opcode == KFSW_FWU_LITE_OP_BLOCK && status->state != KFSW_FWU_RECEIVING) ||
99
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 784 times.
792 (request->opcode == KFSW_FWU_LITE_OP_ABORT &&
100
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 (status->state != KFSW_FWU_IDLE || status->received != 0U ||
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 status->committed != 0U))) {
102 6 return false;
103 }
104
4/4
✓ Branch 0 taken 692 times.
✓ Branch 1 taken 100 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 686 times.
792 if (image != NULL && !kfsw_fwu_lite_same_image(image, &status->image)) {
105 6 return false;
106 }
107
2/2
✓ Branch 0 taken 510 times.
✓ Branch 1 taken 276 times.
786 if (accepted != UINT32_MAX &&
108
3/4
✓ Branch 0 taken 498 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 498 times.
510 (status->received != accepted || status->actual_crc32 != crc)) {
109 12 return false;
110 }
111
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 762 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
774 if (request->opcode == KFSW_FWU_LITE_OP_VERIFY && status->state != KFSW_FWU_VERIFIED &&
112
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ status->state != KFSW_FWU_READY) {
113 ✗ return false;
114 }
115
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 762 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
774 if (request->opcode == KFSW_FWU_LITE_OP_START_FLASHING && status->state != KFSW_FWU_READY) {
116 ✗ return false;
117 }
118 774 return true;
119 }
120
121 1514 static csp_packet_t *read_before_deadline(csp_conn_t *connection, int64_t deadline)
122 {
123 120 for (;;) {
124 1634 int64_t remaining = deadline - k_uptime_get();
125 csp_packet_t *packet;
126
127
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1610 times.
1634 if (remaining <= 0) {
128 1514 return NULL;
129 }
130 /* RDP clamps nonzero reads to its connection timeout. Poll instead. */
131 1610 packet = csp_read(connection,
132 IS_ENABLED(CONFIG_KFSW_FWU_LITE_RDP) ? 0U : (uint32_t)remaining);
133
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 120 times.
600 if (packet != NULL || !IS_ENABLED(CONFIG_KFSW_FWU_LITE_RDP)) {
134 1490 return packet;
135 }
136 120 k_sleep(K_MSEC(1));
137 }
138 }
139
140 1112 static int exchange(csp_conn_t *connection, const struct kfsw_fwu_lite_message *request,
141 struct kfsw_fwu_status *status, const struct kfsw_fwu_image *image,
142 uint32_t accepted, uint32_t crc)
143 {
144 1112 csp_packet_t *packet = csp_buffer_get(0);
145 1112 int64_t deadline = k_uptime_get() + kfsw_fwu_lite_get_timeout_ms();
146 size_t encoded;
147 1112 bool incompatible = false;
148 int result;
149
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1112 times.
1112 if (packet == NULL) {
151 ✗ return -ENOBUFS;
152 }
153 1112 result = kfsw_fwu_lite_encode(request, packet->data, CSP_BUFFER_SIZE, &encoded);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1112 times.
1112 if (result != 0) {
155 ✗ csp_buffer_free(packet);
156 ✗ return result;
157 }
158 1112 packet->length = (uint16_t)encoded;
159 1112 csp_send(connection, packet);
160
2/2
✓ Branch 0 taken 1514 times.
✓ Branch 1 taken 18 times.
1532 for (unsigned int dropped = 0U; dropped < STALE_LIMIT; dropped++) {
161 struct kfsw_fwu_lite_message reply;
162 1514 struct kfsw_fwu_status candidate = {0};
163 1514 bool match = false;
164
165 1514 packet = read_before_deadline(connection, deadline);
166
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 1450 times.
1514 if (packet == NULL) {
167 88 break;
168 }
169
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 1426 times.
1450 if (k_uptime_get() >= deadline) {
170 24 csp_buffer_free(packet);
171 24 break;
172 }
173
1/2
✓ Branch 0 taken 1426 times.
✗ Branch 1 not taken.
1426 if (packet->length >= KFSW_FWU_LITE_HEADER_SIZE &&
174
4/4
✓ Branch 0 taken 1420 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 102 times.
✓ Branch 3 taken 1318 times.
1426 packet->length <= CSP_BUFFER_SIZE && packet->data[0] != KFSW_FWU_LITE_VERSION &&
175
2/4
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
✗ Branch 3 not taken.
102 packet->data[1] == KFSW_FWU_LITE_REPLY && packet->data[2] == request->opcode &&
176
1/2
✓ Branch 1 taken 102 times.
✗ Branch 2 not taken.
102 sys_get_be32(packet->data + 4) == request->request_id) {
177 102 incompatible = true;
178 }
179 /* Decode in the owned packet after checking its physical capacity. */
180
4/4
✓ Branch 0 taken 1420 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1312 times.
✓ Branch 3 taken 108 times.
2846 if (packet->length <= CSP_BUFFER_SIZE &&
181 1420 kfsw_fwu_lite_decode(packet->data, packet->length, &reply) == 0) {
182 1312 match = correlated(request, &reply, &candidate, image, accepted, crc);
183 1312 result = remote_error(reply.status);
184 }
185 1426 csp_buffer_free(packet);
186
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1426 times.
1426 if (k_uptime_get() >= deadline) {
187 ✗ break;
188 }
189
2/2
✓ Branch 0 taken 1006 times.
✓ Branch 1 taken 420 times.
1426 if (match) {
190 1006 *status = candidate;
191 1006 return result;
192 }
193 }
194
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 100 times.
106 return incompatible ? -EPROTONOSUPPORT : -ETIMEDOUT;
195 }
196
197 1050 static int retry(csp_conn_t *connection, const struct kfsw_fwu_lite_message *request,
198 struct kfsw_fwu_status *status, const struct kfsw_fwu_image *image,
199 uint32_t accepted, uint32_t crc, uint32_t *resent)
200 {
201 1112 for (unsigned int attempt = 0U;; attempt++) {
202 1112 int result = exchange(connection, request, status, image, accepted, crc);
203
4/4
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 1012 times.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 62 times.
1112 if (result != -ETIMEDOUT || attempt >= kfsw_fwu_lite_get_retries()) {
204 1050 return result;
205 }
206
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 56 times.
62 if (resent != NULL) {
207 6 (*resent)++;
208 }
209 }
210 }
211
212 338 static csp_conn_t *connect_node(uint16_t node)
213 {
214 338 return csp_connect(CSP_PRIO_NORM, node, CONFIG_KFSW_FWU_LITE_CSP_PORT,
215 kfsw_fwu_lite_get_timeout_ms(), CONNECTION_OPTIONS);
216 }
217
218 102 static int control(uint16_t node, uint8_t opcode, const struct kfsw_fwu_handle *handle,
219 struct kfsw_fwu_status *status)
220 {
221 struct kfsw_fwu_lite_message request;
222 csp_conn_t *connection;
223 int result;
224
225
5/8
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 102 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 46 times.
✓ Branch 7 taken 56 times.
102 if (node == 0U || node >= KFSW_CSP_BROADCAST_ADDRESS || status == NULL ||
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 (handle == NULL && opcode != KFSW_FWU_LITE_OP_STATUS)) {
227 ✗ return -EINVAL;
228 }
229 102 result = next_request(&request, opcode, handle);
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (result != 0) {
231 ✗ return result;
232 }
233 102 connection = connect_node(node);
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (connection == NULL) {
235 ✗ return -ENOTCONN;
236 }
237 102 result = retry(connection, &request, status, NULL, UINT32_MAX, 0U, NULL);
238 102 csp_close(connection);
239 102 return result;
240 }
241
242 46 int kfsw_fwu_lite_remote_status(uint16_t node, const struct kfsw_fwu_handle *handle,
243 struct kfsw_fwu_status *status)
244 {
245 46 return control(node, KFSW_FWU_LITE_OP_STATUS, handle, status);
246 }
247
248 18 int kfsw_fwu_lite_remote_verify(uint16_t node, const struct kfsw_fwu_handle *handle,
249 struct kfsw_fwu_status *status)
250 {
251 18 return control(node, KFSW_FWU_LITE_OP_VERIFY, handle, status);
252 }
253
254 24 int kfsw_fwu_lite_remote_activate(uint16_t node, const struct kfsw_fwu_handle *handle,
255 struct kfsw_fwu_status *status)
256 {
257 24 return control(node, KFSW_FWU_LITE_OP_START_FLASHING, handle, status);
258 }
259
260 14 int kfsw_fwu_lite_remote_abort(uint16_t node, const struct kfsw_fwu_handle *handle,
261 struct kfsw_fwu_status *status)
262 {
263 14 return control(node, KFSW_FWU_LITE_OP_ABORT, handle, status);
264 }
265
266 18 int kfsw_fwu_lite_start_flashing(uint16_t node)
267 {
268 18 struct kfsw_fwu_status status = {0};
269 18 int result = kfsw_fwu_lite_remote_status(node, NULL, &status);
270 18 struct kfsw_fwu_handle handle = status.handle;
271
272
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 return result == 0 ? kfsw_fwu_lite_remote_activate(node, &handle, &status) : result;
273 }
274 /*
275 * The image can be a node file or, on a host process, a host file. Paths under
276 * the node's mount point are node files; boards don't build the host branch.
277 */
278 struct image_source {
279 bool host;
280 #if CONFIG_KFSW_FWU_LITE_HOST_FILES
281 int host_fd;
282 #endif
283
284 #if CONFIG_FILE_SYSTEM
285 struct fs_file_t file;
286 #endif
287 };
288
289 442 static bool path_is_host(const char *path)
290 {
291 #if CONFIG_KFSW_FWU_LITE_HOST_FILES
292 442 return strncmp(path, KFSW_STORAGE_MOUNT_POINT "/", sizeof(KFSW_STORAGE_MOUNT_POINT)) != 0;
293 #else
294 ARG_UNUSED(path);
295 return false;
296 #endif
297 }
298
299 442 static int source_open(struct image_source *source, const char *path)
300 {
301 442 source->host = path_is_host(path);
302
303 #if CONFIG_KFSW_FWU_LITE_HOST_FILES
304
1/2
✓ Branch 0 taken 442 times.
✗ Branch 1 not taken.
442 if (source->host) {
305 /* O_RDONLY is zero on every host this runs on. */
306 442 source->host_fd = nsi_host_open(path, 0);
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 442 times.
442 return (source->host_fd < 0) ? -ENOENT : 0;
308 }
309 #endif
310
311 #if CONFIG_FILE_SYSTEM
312 fs_file_t_init(&source->file);
313 return fs_open(&source->file, path, FS_O_READ);
314 #else
315 ✗ return -ENOTSUP;
316 #endif
317 }
318
319 1672 static ssize_t source_read(struct image_source *source, void *buffer, size_t size)
320 {
321 #if CONFIG_KFSW_FWU_LITE_HOST_FILES
322
1/2
✓ Branch 0 taken 1672 times.
✗ Branch 1 not taken.
1672 if (source->host) {
323 1672 long bytes = nsi_host_read(source->host_fd, buffer, size);
324
325
1/2
✓ Branch 0 taken 1672 times.
✗ Branch 1 not taken.
1672 return (bytes < 0) ? -EIO : (ssize_t)bytes;
326 }
327 #endif
328 #if CONFIG_FILE_SYSTEM
329 return fs_read(&source->file, buffer, size);
330 #else
331 ARG_UNUSED(source);
332 ARG_UNUSED(buffer);
333 ARG_UNUSED(size);
334 ✗ return -ENOTSUP;
335 #endif
336 }
337
338 442 static void source_close(struct image_source *source)
339 {
340 #if CONFIG_KFSW_FWU_LITE_HOST_FILES
341
1/2
✓ Branch 0 taken 442 times.
✗ Branch 1 not taken.
442 if (source->host) {
342 442 (void)nsi_host_close(source->host_fd);
343 442 return;
344 }
345 #endif
346 #if CONFIG_FILE_SYSTEM
347 (void)fs_close(&source->file);
348 #else
349 ARG_UNUSED(source);
350 #endif
351 }
352
353 242 static int describe_file(const char *path, struct kfsw_fwu_image *image)
354 {
355 struct image_source source;
356 uint8_t block[KFSW_FWU_LITE_MAX_BLOCK_SIZE];
357 242 psa_hash_operation_t hash = PSA_HASH_OPERATION_INIT;
358 size_t digest_size;
359 242 int result = source_open(&source, path);
360
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242 times.
242 if (result != 0) {
362 ✗ return result;
363 }
364 242 *image = (struct kfsw_fwu_image){.block_size = KFSW_FWU_LITE_MAX_BLOCK_SIZE,
365 .digest_present = true};
366
1/2
✓ Branch 1 taken 242 times.
✗ Branch 2 not taken.
242 if (psa_hash_setup(&hash, PSA_ALG_SHA_256) != PSA_SUCCESS) {
367 ✗ result = -EIO;
368 ✗ goto out;
369 }
370 708 while (true) {
371 950 ssize_t bytes = source_read(&source, block, sizeof(block));
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 950 times.
950 if (bytes < 0) {
373 ✗ result = (int)bytes;
374 ✗ break;
375 }
376
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 714 times.
950 if (bytes == 0) {
377 236 break;
378 }
379
3/4
✓ Branch 0 taken 708 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 708 times.
714 if ((size_t)bytes > sizeof(block) || (uint32_t)bytes > UINT32_MAX - image->size) {
380 6 result = -EFBIG;
381 6 break;
382 }
383 708 image->size += (uint32_t)bytes;
384 708 image->crc32 = crc32_ieee_update(image->crc32, block, (size_t)bytes);
385
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 708 times.
708 if (psa_hash_update(&hash, block, (size_t)bytes) != PSA_SUCCESS) {
386 ✗ result = -EIO;
387 ✗ break;
388 }
389 }
390
3/4
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 236 times.
242 if (result == 0 && psa_hash_finish(&hash, image->sha256, sizeof(image->sha256),
391 &digest_size) != PSA_SUCCESS) {
392 ✗ result = -EIO;
393 }
394
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 236 times.
✓ Branch 2 taken 236 times.
✗ Branch 3 not taken.
242 if (result == 0 && image->size == 0U) {
395 ✗ result = -EINVAL;
396 }
397 242 out:
398 242 (void)psa_hash_abort(&hash);
399 242 source_close(&source);
400 242 return result;
401 }
402
403 200 static int upload(csp_conn_t *connection, const char *path, const struct kfsw_fwu_image *image,
404 struct kfsw_fwu_status *status, uint32_t *resent)
405 {
406 200 struct kfsw_fwu_handle handle = status->handle;
407 struct kfsw_fwu_lite_message request;
408 struct image_source source;
409 200 psa_hash_operation_t hash = PSA_HASH_OPERATION_INIT;
410 uint8_t block[KFSW_FWU_LITE_MAX_BLOCK_SIZE];
411 uint8_t digest[KFSW_FWU_SHA256_SIZE];
412 200 uint32_t offset = 0U;
413 200 uint32_t crc = 0U;
414 200 uint32_t resume = status->received;
415 size_t digest_size;
416 200 int result = source_open(&source, path);
417
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (result != 0) {
419 ✗ return result;
420 }
421
4/6
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 194 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 194 times.
200 if (resume > image->size || (resume != image->size && resume % image->block_size != 0U)) {
422 ✗ result = -EBADMSG;
423 ✗ goto out;
424 }
425
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
200 if (psa_hash_setup(&hash, PSA_ALG_SHA_256) != PSA_SUCCESS) {
426 ✗ result = -EIO;
427 ✗ goto out;
428 }
429
3/4
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 182 times.
200 if (resume == 0U && status->actual_crc32 != 0U) {
430 ✗ result = -EILSEQ;
431 ✗ goto out;
432 }
433
2/2
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 174 times.
722 while (offset < image->size) {
434 548 size_t count = MIN(sizeof(block), image->size - offset);
435 548 ssize_t bytes = source_read(&source, block, count);
436
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 542 times.
548 if (bytes != (ssize_t)count) {
437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 result = bytes < 0 ? (int)bytes : -ESTALE;
438 6 goto out;
439 }
440 542 crc = crc32_ieee_update(crc, block, count);
441
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 542 times.
542 if (psa_hash_update(&hash, block, count) != PSA_SUCCESS) {
442 ✗ result = -EIO;
443 ✗ goto out;
444 }
445
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 512 times.
542 if (offset < resume) {
446
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 12 times.
30 if (offset + count == resume && crc != status->actual_crc32) {
447 6 result = -EILSEQ;
448 6 goto out;
449 }
450 } else {
451 512 result = next_request(&request, KFSW_FWU_LITE_OP_BLOCK, &handle);
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 512 times.
512 if (result != 0) {
453 ✗ goto out;
454 }
455 512 request.offset = offset;
456 512 request.crc32 = crc32_ieee(block, count);
457 512 request.data = block;
458 512 request.data_size = count;
459 512 result = retry(connection, &request, status, image,
460 512 offset + (uint32_t)count, crc, resent);
461
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 498 times.
512 if (result != 0) {
462 14 goto out;
463 }
464 }
465 522 offset += (uint32_t)count;
466 }
467
5/6
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 162 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 162 times.
✗ Branch 6 not taken.
336 if (source_read(&source, block, 1U) != 0 || crc != image->crc32 ||
468
1/2
✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
324 psa_hash_finish(&hash, digest, sizeof(digest), &digest_size) != PSA_SUCCESS ||
469 162 memcmp(digest, image->sha256, sizeof(digest)) != 0) {
470 12 result = -ESTALE;
471 }
472 162 out:
473 200 (void)psa_hash_abort(&hash);
474 200 source_close(&source);
475 200 return result;
476 }
477
478 254 int kfsw_fwu_lite_send_file(uint16_t node, const char *path, struct kfsw_fwu_status *receipt,
479 uint32_t *blocks_resent)
480 {
481 struct kfsw_fwu_image image;
482 254 struct kfsw_fwu_status status = {0};
483 struct kfsw_fwu_lite_message request;
484 uint8_t descriptor[KFSW_FWU_LITE_DESCRIPTOR_SIZE];
485 csp_conn_t *connection;
486 int result;
487
488
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 248 times.
254 if (receipt != NULL) {
489 6 *receipt = status;
490 }
491
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 248 times.
254 if (blocks_resent != NULL) {
492 6 *blocks_resent = 0U;
493 }
494
6/8
✓ Branch 0 taken 254 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 248 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 242 times.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 242 times.
254 if (path == NULL || path[0] != '/' || node == 0U || node >= KFSW_CSP_BROADCAST_ADDRESS) {
495 12 return -EINVAL;
496 }
497 242 result = describe_file(path, &image);
498
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 236 times.
242 if (result != 0) {
499 6 return result;
500 }
501 236 connection = connect_node(node);
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (connection == NULL) {
503 ✗ return -ENOTCONN;
504 }
505 236 result = next_request(&request, KFSW_FWU_LITE_OP_STATUS, NULL);
506
1/2
✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
236 if (result == 0) {
507 236 result = retry(connection, &request, &status, NULL, UINT32_MAX, 0U, NULL);
508 }
509
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 36 times.
236 if (result == -ENOENT) {
510 200 result = next_request(&request, KFSW_FWU_LITE_OP_BEGIN, NULL);
511
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (result == 0) {
512 200 request.transfer_id = request.request_id;
513 200 kfsw_fwu_lite_pack_image(&image, descriptor);
514 200 request.data = descriptor;
515 200 request.data_size = sizeof(descriptor);
516 200 result = retry(connection, &request, &status, &image, UINT32_MAX, 0U, NULL);
517 }
518 }
519
4/4
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 30 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 200 times.
236 if (result == 0 && !kfsw_fwu_lite_same_image(&image, &status.image)) {
520 6 result = -ESTALE;
521 }
522
3/4
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 200 times.
236 if (result == 0 && status.state != KFSW_FWU_RECEIVING &&
523
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ status.state != KFSW_FWU_VERIFIED && status.state != KFSW_FWU_READY) {
524 ✗ result = -EINVAL;
525 }
526
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 36 times.
236 if (result == 0) {
527 200 result = upload(connection, path, &image, &status, blocks_resent);
528 }
529 236 csp_close(connection);
530
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 230 times.
236 if (receipt != NULL) {
531 6 *receipt = status;
532 }
533 236 return result;
534 }
535