./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
190 / 201
94.5%
Branches:
136 / 187
72.7%
Functions:
10 / 11
90.9%

kfsw-services/src/fwu/fwu_lite_protocol.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <string.h>
3 #include <zephyr/sys/byteorder.h>
4 #include <zephyr/sys/crc.h>
5 #include <kfsw/services/fwu_lite.h>
6
7 7990 static int message_shape(const struct kfsw_fwu_lite_message *m)
8 {
9
3/4
✓ Branch 0 taken 7974 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7974 times.
7990 if ((m->data_size > KFSW_FWU_LITE_MAX_PAYLOAD_SIZE) || (m->data_size > UINT16_MAX)) {
10 16 return -EMSGSIZE;
11 }
12
3/4
✓ Branch 0 taken 7864 times.
✓ Branch 1 taken 110 times.
✓ Branch 2 taken 7864 times.
✗ Branch 3 not taken.
7974 if ((m->version != KFSW_FWU_LITE_VERSION) || (m->direction > KFSW_FWU_LITE_REPLY) ||
13
2/4
✓ Branch 0 taken 7864 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7864 times.
✗ Branch 3 not taken.
7864 (m->opcode < KFSW_FWU_LITE_OP_BEGIN) || (m->opcode > KFSW_FWU_LITE_OP_ABORT) ||
14
5/6
✓ Branch 0 taken 7856 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 6714 times.
✓ Branch 3 taken 1142 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6714 times.
7864 (m->request_id == 0U) || ((m->data_size != 0U) && (m->data == NULL))) {
15 118 return -EBADMSG;
16 }
17
2/2
✓ Branch 0 taken 4002 times.
✓ Branch 1 taken 3854 times.
7856 if (m->direction == KFSW_FWU_LITE_REPLY) {
18 7996 return (m->data_size == KFSW_FWU_LITE_RECEIPT_SIZE &&
19
1/2
✓ Branch 0 taken 3994 times.
✗ Branch 1 not taken.
3994 m->status <= KFSW_FWU_LITE_STATUS_NOT_FOUND)
20 ? 0
21
2/2
✓ Branch 0 taken 3994 times.
✓ Branch 1 taken 8 times.
7996 : -EBADMSG;
22 }
23
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3854 times.
3854 if (m->status != 0U) {
24 ✗ return -EBADMSG;
25 }
26
2/2
✓ Branch 0 taken 902 times.
✓ Branch 1 taken 2952 times.
3854 if (m->opcode == KFSW_FWU_LITE_OP_BEGIN) {
27
4/4
✓ Branch 0 taken 888 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 880 times.
✓ Branch 3 taken 8 times.
902 return (m->transfer_id != 0U && m->generation == 0U && m->offset == 0U &&
28
2/4
✓ Branch 0 taken 880 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 880 times.
✗ Branch 3 not taken.
880 m->crc32 == 0U && m->data_size == KFSW_FWU_LITE_DESCRIPTOR_SIZE)
29 ? 0
30
1/2
✓ Branch 0 taken 902 times.
✗ Branch 1 not taken.
1804 : -EBADMSG;
31 }
32
4/6
✓ Branch 0 taken 886 times.
✓ Branch 1 taken 2066 times.
✓ Branch 2 taken 886 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 886 times.
✗ Branch 5 not taken.
2952 if (m->opcode == KFSW_FWU_LITE_OP_STATUS && m->transfer_id == 0U && m->generation == 0U) {
33
3/6
✓ Branch 0 taken 886 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 886 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 886 times.
✗ Branch 5 not taken.
886 return (m->offset == 0U && m->crc32 == 0U && m->data_size == 0U) ? 0 : -EBADMSG;
34 }
35
2/4
✓ Branch 0 taken 2066 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2066 times.
2066 if (m->transfer_id == 0U || m->generation == 0U) {
36 ✗ return -EBADMSG;
37 }
38
2/2
✓ Branch 0 taken 1802 times.
✓ Branch 1 taken 264 times.
2066 if (m->opcode == KFSW_FWU_LITE_OP_BLOCK) {
39
1/2
✓ Branch 0 taken 1802 times.
✗ Branch 1 not taken.
1802 return m->data_size != 0U && m->data_size <= KFSW_FWU_LITE_MAX_BLOCK_SIZE
40 ? 0
41
1/2
✓ Branch 0 taken 1802 times.
✗ Branch 1 not taken.
3604 : -EBADMSG;
42 }
43
5/6
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 256 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 248 times.
✓ Branch 5 taken 8 times.
264 return (m->offset == 0U && m->crc32 == 0U && m->data_size == 0U) ? 0 : -EBADMSG;
44 }
45
46 2256 int kfsw_fwu_lite_encode(const struct kfsw_fwu_lite_message *m, uint8_t *buffer, size_t capacity,
47 size_t *encoded)
48 {
49 int result;
50
4/6
✓ Branch 0 taken 2248 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2248 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2248 times.
2256 if (m == NULL || buffer == NULL || encoded == NULL) {
51 8 return -EINVAL;
52 }
53 2248 result = message_shape(m);
54
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2240 times.
2248 if (result != 0) {
55 8 return result;
56 }
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2240 times.
2240 if (capacity < KFSW_FWU_LITE_HEADER_SIZE + m->data_size) {
58 ✗ return -EMSGSIZE;
59 }
60 2240 buffer[0] = m->version;
61 2240 buffer[1] = m->direction;
62 2240 buffer[2] = m->opcode;
63 2240 buffer[3] = m->status;
64 2240 sys_put_be32(m->request_id, buffer + 4);
65 2240 sys_put_be32(m->transfer_id, buffer + 8);
66 2240 sys_put_be32(m->generation, buffer + 12);
67 2240 sys_put_be32(m->offset, buffer + 16);
68 2240 sys_put_be32(m->crc32, buffer + 20);
69 2240 sys_put_be16((uint16_t)m->data_size, buffer + 24);
70 2240 sys_put_be16(0U, buffer + 26);
71
2/2
✓ Branch 0 taken 1894 times.
✓ Branch 1 taken 346 times.
2240 if (m->data_size != 0U) {
72 1894 memcpy(buffer + KFSW_FWU_LITE_HEADER_SIZE, m->data, m->data_size);
73 }
74 2240 *encoded = KFSW_FWU_LITE_HEADER_SIZE + m->data_size;
75 2240 return 0;
76 }
77
78 2588 int kfsw_fwu_lite_decode(const uint8_t *buffer, size_t size, struct kfsw_fwu_lite_message *m)
79 {
80
3/4
✓ Branch 0 taken 2580 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2580 times.
2588 if (buffer == NULL || m == NULL) {
81 8 return -EINVAL;
82 }
83
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2572 times.
2580 if (size < KFSW_FWU_LITE_HEADER_SIZE) {
84 8 return -EBADMSG;
85 }
86
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2564 times.
2572 if (size - KFSW_FWU_LITE_HEADER_SIZE > KFSW_FWU_LITE_MAX_PAYLOAD_SIZE) {
87 8 return -EMSGSIZE;
88 }
89
2/2
✓ Branch 1 taken 2556 times.
✓ Branch 2 taken 8 times.
2564 if (sys_get_be16(buffer + 26) != 0U ||
90
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 2540 times.
2556 sys_get_be16(buffer + 24) != size - KFSW_FWU_LITE_HEADER_SIZE) {
91 24 return -EBADMSG;
92 }
93 2540 *m = (struct kfsw_fwu_lite_message){
94 2540 .version = buffer[0],
95 2540 .direction = buffer[1],
96 2540 .opcode = buffer[2],
97 2540 .status = buffer[3],
98 2540 .request_id = sys_get_be32(buffer + 4),
99 2540 .transfer_id = sys_get_be32(buffer + 8),
100 2540 .generation = sys_get_be32(buffer + 12),
101 2540 .offset = sys_get_be32(buffer + 16),
102 2540 .crc32 = sys_get_be32(buffer + 20),
103 2540 .data_size = size - KFSW_FWU_LITE_HEADER_SIZE,
104 2540 .data = buffer + KFSW_FWU_LITE_HEADER_SIZE,
105 };
106 2540 return message_shape(m);
107 }
108
109 2048 void kfsw_fwu_lite_pack_image(const struct kfsw_fwu_image *image, uint8_t *data)
110 {
111 2048 sys_put_be32(image->size, data);
112 2048 sys_put_be32(image->crc32, data + 4);
113 2048 sys_put_be16(image->block_size, data + 8);
114 2048 memcpy(data + 10, image->sha256, KFSW_FWU_SHA256_SIZE);
115 2048 }
116
117 1978 int kfsw_fwu_lite_unpack_image(const uint8_t *data, size_t size, struct kfsw_fwu_image *image)
118 {
119
3/6
✓ Branch 0 taken 1978 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1978 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1978 times.
1978 if (data == NULL || image == NULL || size != KFSW_FWU_LITE_DESCRIPTOR_SIZE) {
120 ✗ return -EBADMSG;
121 }
122 1978 *image = (struct kfsw_fwu_image){.size = sys_get_be32(data),
123 1978 .crc32 = sys_get_be32(data + 4),
124 1978 .block_size = sys_get_be16(data + 8),
125 .digest_present = true};
126 1978 memcpy(image->sha256, data + 10, KFSW_FWU_SHA256_SIZE);
127
2/2
✓ Branch 0 taken 1642 times.
✓ Branch 1 taken 16 times.
1658 return image->size != 0U && image->block_size == KFSW_FWU_LITE_MAX_BLOCK_SIZE ? 0
128
2/2
✓ Branch 0 taken 1658 times.
✓ Branch 1 taken 320 times.
3636 : -EBADMSG;
129 }
130
131 898 bool kfsw_fwu_lite_same_image(const struct kfsw_fwu_image *a, const struct kfsw_fwu_image *b)
132 {
133
2/4
✓ Branch 0 taken 898 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 898 times.
✗ Branch 3 not taken.
898 return a->digest_present && b->digest_present && a->size == b->size &&
134
6/8
✓ Branch 0 taken 898 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 892 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 892 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 886 times.
✓ Branch 7 taken 6 times.
2688 a->crc32 == b->crc32 && a->block_size == b->block_size &&
135
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
892 memcmp(a->sha256, b->sha256, KFSW_FWU_SHA256_SIZE) == 0;
136 }
137
138 1632 static void pack_receipt(const struct kfsw_fwu_status *s, uint8_t *data)
139 {
140 1632 data[0] = s->state;
141
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1632 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 1592 times.
3264 data[1] = (s->swap_scheduled ? 1U : 0U) | (s->aborted ? 2U : 0U) |
142
2/2
✓ Branch 0 taken 1284 times.
✓ Branch 1 taken 348 times.
1632 (s->image.digest_present ? 4U : 0U);
143 1632 sys_put_be32(s->received, data + 2);
144 1632 sys_put_be32(s->committed, data + 6);
145 1632 sys_put_be32(s->actual_crc32, data + 10);
146 1632 kfsw_fwu_lite_pack_image(&s->image, data + 14);
147 1632 }
148
149 1570 int kfsw_fwu_lite_receipt(const struct kfsw_fwu_lite_message *reply, struct kfsw_fwu_status *s)
150 {
151
3/6
✓ Branch 0 taken 1570 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1570 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1570 times.
✗ Branch 6 not taken.
1570 if (reply == NULL || s == NULL || message_shape(reply) != 0 ||
152
2/4
✓ Branch 0 taken 1570 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1570 times.
✗ Branch 3 not taken.
1570 reply->direction != KFSW_FWU_LITE_REPLY || reply->data[0] > KFSW_FWU_VERIFIED ||
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1570 times.
1570 (reply->data[1] & ~7U) != 0U) {
154 ✗ return -EBADMSG;
155 }
156 1570 memset(s, 0, sizeof(*s));
157 1570 s->state = reply->data[0];
158 1570 s->swap_scheduled = (reply->data[1] & 1U) != 0U;
159 1570 s->aborted = (reply->data[1] & 2U) != 0U;
160 1570 s->received = sys_get_be32(reply->data + 2);
161 1570 s->committed = sys_get_be32(reply->data + 6);
162 1570 s->actual_crc32 = sys_get_be32(reply->data + 10);
163 /* An empty/error snapshot need not carry a valid lite descriptor. */
164 1570 (void)kfsw_fwu_lite_unpack_image(reply->data + 14, KFSW_FWU_LITE_DESCRIPTOR_SIZE,
165 &s->image);
166 1570 s->image.digest_present = (reply->data[1] & 4U) != 0U;
167 1570 s->total_size = s->image.size;
168 1570 s->expected_crc32 = s->image.crc32;
169 1570 s->handle.transfer_id = reply->transfer_id;
170 1570 s->handle.generation = reply->generation;
171
2/4
✓ Branch 0 taken 1570 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1570 times.
✗ Branch 3 not taken.
1570 if (s->committed > s->total_size || s->received > s->total_size ||
172
4/4
✓ Branch 0 taken 1558 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1552 times.
✓ Branch 3 taken 6 times.
1570 (s->state != KFSW_FWU_FAILED && s->committed > s->received) ||
173
3/4
✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1558 times.
1564 reply->offset != s->received || reply->crc32 != s->actual_crc32) {
174 12 return -EBADMSG;
175 }
176 1558 return 0;
177 }
178
179 1560 static uint8_t status_for_errno(int result)
180 {
181
9/10
✓ Branch 0 taken 1212 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 204 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 46 times.
✓ Branch 9 taken 10 times.
1560 switch (result) {
182 1212 case 0:
183 1212 return KFSW_FWU_LITE_STATUS_OK;
184 16 case -EBUSY:
185 16 return KFSW_FWU_LITE_STATUS_BUSY;
186 22 case -EACCES:
187 22 return KFSW_FWU_LITE_STATUS_OWNER;
188 34 case -ESTALE:
189 34 return KFSW_FWU_LITE_STATUS_STALE;
190 204 case -ENOENT:
191 204 return KFSW_FWU_LITE_STATUS_NOT_FOUND;
192 ✗ case -EFBIG:
193 ✗ return KFSW_FWU_LITE_STATUS_TOO_LARGE;
194 8 case -ESPIPE:
195 8 return KFSW_FWU_LITE_STATUS_OUT_OF_ORDER;
196 8 case -EILSEQ:
197 8 return KFSW_FWU_LITE_STATUS_BAD_IMAGE;
198 46 case -EBADMSG:
199 case -EINVAL:
200 case -EAGAIN:
201 46 return KFSW_FWU_LITE_STATUS_INVALID;
202 10 default:
203 10 return KFSW_FWU_LITE_STATUS_FAILED;
204 }
205 }
206
207 1640 int kfsw_fwu_lite_handle_peer(uint16_t peer, const struct kfsw_fwu_lite_message *request,
208 struct kfsw_fwu_lite_message *reply, uint8_t *receipt_data)
209 {
210 1640 struct kfsw_fwu_status snapshot = {0};
211 struct kfsw_fwu_handle handle;
212 struct kfsw_fwu_image image;
213 int result;
214
215
4/6
✓ Branch 0 taken 1632 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1632 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1632 times.
1640 if (request == NULL || reply == NULL || receipt_data == NULL) {
216 8 return -EINVAL;
217 }
218 1632 *reply = *request;
219 1632 reply->direction = KFSW_FWU_LITE_REPLY;
220 1632 reply->data = receipt_data;
221 1632 reply->data_size = KFSW_FWU_LITE_RECEIPT_SIZE;
222 1632 handle = (struct kfsw_fwu_handle){.owner = {.kind = KFSW_FWU_OWNER_LITE, .peer = peer},
223 1632 .transfer_id = request->transfer_id,
224 1632 .generation = request->generation};
225 1632 result = message_shape(request);
226
3/4
✓ Branch 0 taken 1568 times.
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1568 times.
1632 if (result != 0 || request->direction != KFSW_FWU_LITE_REQUEST) {
227 64 reply->status = KFSW_FWU_LITE_STATUS_INVALID;
228 64 goto done;
229 }
230
6/7
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 718 times.
✓ Branch 2 taken 306 times.
✓ Branch 3 taken 42 times.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 46 times.
✗ Branch 6 not taken.
1568 switch (request->opcode) {
231 408 case KFSW_FWU_LITE_OP_BEGIN:
232 408 result = kfsw_fwu_lite_unpack_image(request->data, request->data_size, &image);
233
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 8 times.
408 if (result == 0) {
234 400 result = kfsw_fwu_session_begin(&handle.owner, &image, request->transfer_id,
235 &handle, &snapshot);
236 }
237 408 break;
238 718 case KFSW_FWU_LITE_OP_BLOCK:
239
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 710 times.
718 if (crc32_ieee(request->data, request->data_size) != request->crc32) {
240 8 reply->status = KFSW_FWU_LITE_STATUS_BAD_BLOCK;
241 8 goto done;
242 }
243 710 result = kfsw_fwu_session_write(&handle, request->offset, request->data,
244 710 request->data_size, &snapshot);
245 710 break;
246 306 case KFSW_FWU_LITE_OP_STATUS:
247 612 result = request->generation == 0U
248 306 ? kfsw_fwu_session_lookup(&handle.owner, &snapshot)
249
1/2
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
306 : kfsw_fwu_session_status(&handle, &snapshot);
250 306 break;
251 42 case KFSW_FWU_LITE_OP_VERIFY:
252 42 result = kfsw_fwu_session_verify(&handle, &snapshot);
253 42 break;
254 48 case KFSW_FWU_LITE_OP_START_FLASHING:
255 48 result = kfsw_fwu_session_activate(&handle, &snapshot);
256 48 break;
257 46 case KFSW_FWU_LITE_OP_ABORT:
258 46 result = kfsw_fwu_session_abort(&handle, &snapshot);
259 46 break;
260 ✗ default:
261 ✗ result = -EINVAL;
262 }
263 1560 reply->status = status_for_errno(result);
264
2/2
✓ Branch 0 taken 1212 times.
✓ Branch 1 taken 348 times.
1560 if (result == 0) {
265 1212 reply->transfer_id = snapshot.handle.transfer_id;
266 1212 reply->generation = snapshot.handle.generation;
267
6/6
✓ Branch 0 taken 326 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 292 times.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 88 times.
✓ Branch 5 taken 204 times.
348 } else if (result == -EACCES || result == -ESTALE || result == -ENOENT) {
268 260 memset(&snapshot, 0, sizeof(snapshot));
269 }
270 88 done:
271 1632 reply->offset = snapshot.received;
272 1632 reply->crc32 = snapshot.actual_crc32;
273 1632 pack_receipt(&snapshot, receipt_data);
274 1632 return 0;
275 }
276
277 ✗ const char *kfsw_fwu_lite_status_name(uint8_t status)
278 {
279 static const char *const names[] = {"ok", "invalid", "out-of-order", "bad-block",
280 "bad-image", "too-large", "busy", "failed",
281 "owner", "stale", "not-found"};
282
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return status < sizeof(names) / sizeof(names[0]) ? names[status] : "unknown";
283 }
284