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 | 7990 | if ((m->data_size > KFSW_FWU_LITE_MAX_PAYLOAD_SIZE) || (m->data_size > UINT16_MAX)) { | |
| 10 | 16 | return -EMSGSIZE; | |
| 11 | } | ||
| 12 | 7974 | if ((m->version != KFSW_FWU_LITE_VERSION) || (m->direction > KFSW_FWU_LITE_REPLY) || | |
| 13 | 7864 | (m->opcode < KFSW_FWU_LITE_OP_BEGIN) || (m->opcode > KFSW_FWU_LITE_OP_ABORT) || | |
| 14 | 7864 | (m->request_id == 0U) || ((m->data_size != 0U) && (m->data == NULL))) { | |
| 15 | 118 | return -EBADMSG; | |
| 16 | } | ||
| 17 | 7856 | if (m->direction == KFSW_FWU_LITE_REPLY) { | |
| 18 | 7996 | return (m->data_size == KFSW_FWU_LITE_RECEIPT_SIZE && | |
| 19 | 3994 | m->status <= KFSW_FWU_LITE_STATUS_NOT_FOUND) | |
| 20 | ? 0 | ||
| 21 | 7996 | : -EBADMSG; | |
| 22 | } | ||
| 23 | 3854 | if (m->status != 0U) { | |
| 24 | ✗ | return -EBADMSG; | |
| 25 | } | ||
| 26 | 3854 | if (m->opcode == KFSW_FWU_LITE_OP_BEGIN) { | |
| 27 | 902 | return (m->transfer_id != 0U && m->generation == 0U && m->offset == 0U && | |
| 28 | 880 | m->crc32 == 0U && m->data_size == KFSW_FWU_LITE_DESCRIPTOR_SIZE) | |
| 29 | ? 0 | ||
| 30 | 1804 | : -EBADMSG; | |
| 31 | } | ||
| 32 | 2952 | if (m->opcode == KFSW_FWU_LITE_OP_STATUS && m->transfer_id == 0U && m->generation == 0U) { | |
| 33 | 886 | return (m->offset == 0U && m->crc32 == 0U && m->data_size == 0U) ? 0 : -EBADMSG; | |
| 34 | } | ||
| 35 | 2066 | if (m->transfer_id == 0U || m->generation == 0U) { | |
| 36 | ✗ | return -EBADMSG; | |
| 37 | } | ||
| 38 | 2066 | if (m->opcode == KFSW_FWU_LITE_OP_BLOCK) { | |
| 39 | 1802 | return m->data_size != 0U && m->data_size <= KFSW_FWU_LITE_MAX_BLOCK_SIZE | |
| 40 | ? 0 | ||
| 41 | 3604 | : -EBADMSG; | |
| 42 | } | ||
| 43 | 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 | 2256 | if (m == NULL || buffer == NULL || encoded == NULL) { | |
| 51 | 8 | return -EINVAL; | |
| 52 | } | ||
| 53 | 2248 | result = message_shape(m); | |
| 54 | 2248 | if (result != 0) { | |
| 55 | 8 | return result; | |
| 56 | } | ||
| 57 | 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 | 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 | 2588 | if (buffer == NULL || m == NULL) { | |
| 81 | 8 | return -EINVAL; | |
| 82 | } | ||
| 83 | 2580 | if (size < KFSW_FWU_LITE_HEADER_SIZE) { | |
| 84 | 8 | return -EBADMSG; | |
| 85 | } | ||
| 86 | 2572 | if (size - KFSW_FWU_LITE_HEADER_SIZE > KFSW_FWU_LITE_MAX_PAYLOAD_SIZE) { | |
| 87 | 8 | return -EMSGSIZE; | |
| 88 | } | ||
| 89 | 2564 | if (sys_get_be16(buffer + 26) != 0U || | |
| 90 | 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 | 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 | 1658 | return image->size != 0U && image->block_size == KFSW_FWU_LITE_MAX_BLOCK_SIZE ? 0 | |
| 128 | 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 | 898 | return a->digest_present && b->digest_present && a->size == b->size && | |
| 134 | 2688 | a->crc32 == b->crc32 && a->block_size == b->block_size && | |
| 135 | 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 | 3264 | data[1] = (s->swap_scheduled ? 1U : 0U) | (s->aborted ? 2U : 0U) | | |
| 142 | 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 | 1570 | if (reply == NULL || s == NULL || message_shape(reply) != 0 || | |
| 152 | 1570 | reply->direction != KFSW_FWU_LITE_REPLY || reply->data[0] > KFSW_FWU_VERIFIED || | |
| 153 | 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 | 1570 | if (s->committed > s->total_size || s->received > s->total_size || | |
| 172 | 1570 | (s->state != KFSW_FWU_FAILED && s->committed > s->received) || | |
| 173 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 1560 | if (result == 0) { | |
| 265 | 1212 | reply->transfer_id = snapshot.handle.transfer_id; | |
| 266 | 1212 | reply->generation = snapshot.handle.generation; | |
| 267 | 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 | ✗ | return status < sizeof(names) / sizeof(names[0]) ? names[status] : "unknown"; | |
| 283 | } | ||
| 284 |