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 | 1050 | if ((uint32_t)previous == UINT32_MAX) { | |
| 33 | ✗ | return -EOVERFLOW; | |
| 34 | } | ||
| 35 | 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 | 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 | 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 | 1312 | if (reply->direction != KFSW_FWU_LITE_REPLY || reply->opcode != request->opcode || | |
| 79 | 1300 | reply->request_id != request->request_id || kfsw_fwu_lite_receipt(reply, status) != 0) { | |
| 80 | 258 | return false; | |
| 81 | } | ||
| 82 | 1054 | if ((request->transfer_id != 0U && reply->transfer_id != request->transfer_id) || | |
| 83 | 1048 | (request->generation != 0U && reply->generation != request->generation)) { | |
| 84 | 24 | return false; | |
| 85 | } | ||
| 86 | 1030 | if (reply->status != KFSW_FWU_LITE_STATUS_OK) { | |
| 87 | 232 | return true; | |
| 88 | } | ||
| 89 | 798 | if (reply->transfer_id == 0U || reply->generation == 0U || !status->image.digest_present || | |
| 90 | 798 | status->image.block_size != KFSW_FWU_LITE_MAX_BLOCK_SIZE || status->image.size == 0U || | |
| 91 | 798 | status->aborted != (request->opcode == KFSW_FWU_LITE_OP_ABORT)) { | |
| 92 | ✗ | return false; | |
| 93 | } | ||
| 94 | 798 | if (((status->state == KFSW_FWU_VERIFIED || status->state == KFSW_FWU_READY) && | |
| 95 | 36 | (status->received != status->image.size || status->committed != status->image.size || | |
| 96 | 30 | status->actual_crc32 != status->image.crc32)) || | |
| 97 | 792 | (status->swap_scheduled && status->state != KFSW_FWU_READY) || | |
| 98 | 792 | (request->opcode == KFSW_FWU_LITE_OP_BLOCK && status->state != KFSW_FWU_RECEIVING) || | |
| 99 | 792 | (request->opcode == KFSW_FWU_LITE_OP_ABORT && | |
| 100 | 8 | (status->state != KFSW_FWU_IDLE || status->received != 0U || | |
| 101 | 8 | status->committed != 0U))) { | |
| 102 | 6 | return false; | |
| 103 | } | ||
| 104 | 792 | if (image != NULL && !kfsw_fwu_lite_same_image(image, &status->image)) { | |
| 105 | 6 | return false; | |
| 106 | } | ||
| 107 | 786 | if (accepted != UINT32_MAX && | |
| 108 | 510 | (status->received != accepted || status->actual_crc32 != crc)) { | |
| 109 | 12 | return false; | |
| 110 | } | ||
| 111 | 774 | if (request->opcode == KFSW_FWU_LITE_OP_VERIFY && status->state != KFSW_FWU_VERIFIED && | |
| 112 | ✗ | status->state != KFSW_FWU_READY) { | |
| 113 | ✗ | return false; | |
| 114 | } | ||
| 115 | 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 | 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 | 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 | 1112 | if (packet == NULL) { | |
| 151 | ✗ | return -ENOBUFS; | |
| 152 | } | ||
| 153 | 1112 | result = kfsw_fwu_lite_encode(request, packet->data, CSP_BUFFER_SIZE, &encoded); | |
| 154 | 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 | 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 | 1514 | if (packet == NULL) { | |
| 167 | 88 | break; | |
| 168 | } | ||
| 169 | 1450 | if (k_uptime_get() >= deadline) { | |
| 170 | 24 | csp_buffer_free(packet); | |
| 171 | 24 | break; | |
| 172 | } | ||
| 173 | 1426 | if (packet->length >= KFSW_FWU_LITE_HEADER_SIZE && | |
| 174 | 1426 | packet->length <= CSP_BUFFER_SIZE && packet->data[0] != KFSW_FWU_LITE_VERSION && | |
| 175 | 102 | packet->data[1] == KFSW_FWU_LITE_REPLY && packet->data[2] == request->opcode && | |
| 176 | 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 | 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 | 1426 | if (k_uptime_get() >= deadline) { | |
| 187 | ✗ | break; | |
| 188 | } | ||
| 189 | 1426 | if (match) { | |
| 190 | 1006 | *status = candidate; | |
| 191 | 1006 | return result; | |
| 192 | } | ||
| 193 | } | ||
| 194 | 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 | 1112 | if (result != -ETIMEDOUT || attempt >= kfsw_fwu_lite_get_retries()) { | |
| 204 | 1050 | return result; | |
| 205 | } | ||
| 206 | 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 | 102 | if (node == 0U || node >= KFSW_CSP_BROADCAST_ADDRESS || status == NULL || | |
| 226 | 46 | (handle == NULL && opcode != KFSW_FWU_LITE_OP_STATUS)) { | |
| 227 | ✗ | return -EINVAL; | |
| 228 | } | ||
| 229 | 102 | result = next_request(&request, opcode, handle); | |
| 230 | 102 | if (result != 0) { | |
| 231 | ✗ | return result; | |
| 232 | } | ||
| 233 | 102 | connection = connect_node(node); | |
| 234 | 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 | 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 | 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 | 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 | 1672 | if (source->host) { | |
| 323 | 1672 | long bytes = nsi_host_read(source->host_fd, buffer, size); | |
| 324 | |||
| 325 | 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 | 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 | 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 | 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 | 950 | if (bytes < 0) { | |
| 373 | ✗ | result = (int)bytes; | |
| 374 | ✗ | break; | |
| 375 | } | ||
| 376 | 950 | if (bytes == 0) { | |
| 377 | 236 | break; | |
| 378 | } | ||
| 379 | 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 | 708 | if (psa_hash_update(&hash, block, (size_t)bytes) != PSA_SUCCESS) { | |
| 386 | ✗ | result = -EIO; | |
| 387 | ✗ | break; | |
| 388 | } | ||
| 389 | } | ||
| 390 | 242 | if (result == 0 && psa_hash_finish(&hash, image->sha256, sizeof(image->sha256), | |
| 391 | &digest_size) != PSA_SUCCESS) { | ||
| 392 | ✗ | result = -EIO; | |
| 393 | } | ||
| 394 | 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 | 200 | if (result != 0) { | |
| 419 | ✗ | return result; | |
| 420 | } | ||
| 421 | 200 | if (resume > image->size || (resume != image->size && resume % image->block_size != 0U)) { | |
| 422 | ✗ | result = -EBADMSG; | |
| 423 | ✗ | goto out; | |
| 424 | } | ||
| 425 | 200 | if (psa_hash_setup(&hash, PSA_ALG_SHA_256) != PSA_SUCCESS) { | |
| 426 | ✗ | result = -EIO; | |
| 427 | ✗ | goto out; | |
| 428 | } | ||
| 429 | 200 | if (resume == 0U && status->actual_crc32 != 0U) { | |
| 430 | ✗ | result = -EILSEQ; | |
| 431 | ✗ | goto out; | |
| 432 | } | ||
| 433 | 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 | 548 | if (bytes != (ssize_t)count) { | |
| 437 | 6 | result = bytes < 0 ? (int)bytes : -ESTALE; | |
| 438 | 6 | goto out; | |
| 439 | } | ||
| 440 | 542 | crc = crc32_ieee_update(crc, block, count); | |
| 441 | 542 | if (psa_hash_update(&hash, block, count) != PSA_SUCCESS) { | |
| 442 | ✗ | result = -EIO; | |
| 443 | ✗ | goto out; | |
| 444 | } | ||
| 445 | 542 | if (offset < resume) { | |
| 446 | 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 | 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 | 512 | if (result != 0) { | |
| 462 | 14 | goto out; | |
| 463 | } | ||
| 464 | } | ||
| 465 | 522 | offset += (uint32_t)count; | |
| 466 | } | ||
| 467 | 336 | if (source_read(&source, block, 1U) != 0 || crc != image->crc32 || | |
| 468 | 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 | 254 | if (receipt != NULL) { | |
| 489 | 6 | *receipt = status; | |
| 490 | } | ||
| 491 | 254 | if (blocks_resent != NULL) { | |
| 492 | 6 | *blocks_resent = 0U; | |
| 493 | } | ||
| 494 | 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 | 242 | if (result != 0) { | |
| 499 | 6 | return result; | |
| 500 | } | ||
| 501 | 236 | connection = connect_node(node); | |
| 502 | 236 | if (connection == NULL) { | |
| 503 | ✗ | return -ENOTCONN; | |
| 504 | } | ||
| 505 | 236 | result = next_request(&request, KFSW_FWU_LITE_OP_STATUS, NULL); | |
| 506 | 236 | if (result == 0) { | |
| 507 | 236 | result = retry(connection, &request, &status, NULL, UINT32_MAX, 0U, NULL); | |
| 508 | } | ||
| 509 | 236 | if (result == -ENOENT) { | |
| 510 | 200 | result = next_request(&request, KFSW_FWU_LITE_OP_BEGIN, NULL); | |
| 511 | 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 | 236 | if (result == 0 && !kfsw_fwu_lite_same_image(&image, &status.image)) { | |
| 520 | 6 | result = -ESTALE; | |
| 521 | } | ||
| 522 | 236 | if (result == 0 && status.state != KFSW_FWU_RECEIVING && | |
| 523 | ✗ | status.state != KFSW_FWU_VERIFIED && status.state != KFSW_FWU_READY) { | |
| 524 | ✗ | result = -EINVAL; | |
| 525 | } | ||
| 526 | 236 | if (result == 0) { | |
| 527 | 200 | result = upload(connection, path, &image, &status, blocks_resent); | |
| 528 | } | ||
| 529 | 236 | csp_close(connection); | |
| 530 | 236 | if (receipt != NULL) { | |
| 531 | 6 | *receipt = status; | |
| 532 | } | ||
| 533 | 236 | return result; | |
| 534 | } | ||
| 535 |