Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | #include <assert.h> |
| 20 | #include <stddef.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <ctype.h> |
| 23 | #include <stdio.h> |
Almir Okato | 90be6e6 | 2022-09-23 14:52:25 -0300 | [diff] [blame] | 24 | #include <errno.h> |
Piotr Dymacz | 6a8746d | 2023-06-26 17:13:48 +0200 | [diff] [blame] | 25 | #include <limits.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 26 | |
| 27 | #include "sysflash/sysflash.h" |
| 28 | |
Fabio Utzig | 1a2e41a | 2017-11-17 12:13:09 -0200 | [diff] [blame] | 29 | #include "bootutil/bootutil_log.h" |
| 30 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 31 | #ifdef __ZEPHYR__ |
Fabio Baltieri | 888e261 | 2022-07-19 20:54:26 +0000 | [diff] [blame] | 32 | #include <zephyr/sys/reboot.h> |
| 33 | #include <zephyr/sys/byteorder.h> |
| 34 | #include <zephyr/sys/__assert.h> |
| 35 | #include <zephyr/drivers/flash.h> |
Gerard Marull-Paretas | 4eca54f | 2022-10-06 11:45:11 +0200 | [diff] [blame] | 36 | #include <zephyr/kernel.h> |
Fabio Baltieri | 888e261 | 2022-07-19 20:54:26 +0000 | [diff] [blame] | 37 | #include <zephyr/sys/crc.h> |
| 38 | #include <zephyr/sys/base64.h> |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 39 | #include <hal/hal_flash.h> |
| 40 | #elif __ESPRESSIF__ |
| 41 | #include <bootloader_utility.h> |
| 42 | #include <esp_rom_sys.h> |
Almir Okato | 7d3622f | 2022-10-20 12:44:58 -0300 | [diff] [blame] | 43 | #include <esp_crc.h> |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 44 | #include <endian.h> |
| 45 | #include <mbedtls/base64.h> |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 46 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 47 | #include <bsp/bsp.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 48 | #include <hal/hal_system.h> |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 49 | #include <hal/hal_flash.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 50 | #include <os/endian.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 51 | #include <os/os_cputime.h> |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 52 | #include <crc/crc16.h> |
| 53 | #include <base64/base64.h> |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 54 | #endif /* __ZEPHYR__ */ |
| 55 | |
Jamie McCrae | cb07e88 | 2023-04-14 09:28:24 +0100 | [diff] [blame] | 56 | #include <zcbor_decode.h> |
| 57 | #include <zcbor_encode.h> |
| 58 | #include "zcbor_bulk.h" |
| 59 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 60 | #include <flash_map_backend/flash_map_backend.h> |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 61 | #include <os/os.h> |
| 62 | #include <os/os_malloc.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 63 | |
| 64 | #include <bootutil/image.h> |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 65 | #include <bootutil/bootutil.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 66 | |
| 67 | #include "boot_serial/boot_serial.h" |
| 68 | #include "boot_serial_priv.h" |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 69 | #include "mcuboot_config/mcuboot_config.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 70 | |
Dominik Ermel | 3d4e55d | 2021-07-09 11:14:10 +0000 | [diff] [blame] | 71 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 72 | #include "bootutil_priv.h" |
| 73 | #endif |
| 74 | |
Wouter Cappelle | 953a761 | 2021-05-03 16:53:05 +0200 | [diff] [blame] | 75 | #ifdef MCUBOOT_ENC_IMAGES |
Jamie McCrae | c9fa608 | 2023-07-21 10:23:17 +0100 | [diff] [blame] | 76 | #include "boot_serial/boot_serial_encryption.h" |
Wouter Cappelle | 953a761 | 2021-05-03 16:53:05 +0200 | [diff] [blame] | 77 | #endif |
| 78 | |
Andrzej Puzdrowski | 4f9c730 | 2021-07-16 17:34:43 +0200 | [diff] [blame] | 79 | #include "bootutil/boot_hooks.h" |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 80 | |
Carlos Falgueras García | a4b4b0f | 2021-06-22 10:00:22 +0200 | [diff] [blame] | 81 | BOOT_LOG_MODULE_DECLARE(mcuboot); |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 82 | |
Jamie McCrae | cb07e88 | 2023-04-14 09:28:24 +0100 | [diff] [blame] | 83 | #ifndef ARRAY_SIZE |
| 84 | #define ARRAY_SIZE ZCBOR_ARRAY_SIZE |
| 85 | #endif |
| 86 | |
Jamie McCrae | ad1fb3d | 2022-12-01 14:24:37 +0000 | [diff] [blame] | 87 | #ifndef MCUBOOT_SERIAL_MAX_RECEIVE_SIZE |
| 88 | #define MCUBOOT_SERIAL_MAX_RECEIVE_SIZE 512 |
| 89 | #endif |
| 90 | |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 91 | #ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE |
| 92 | #define BOOT_SERIAL_IMAGE_STATE_SIZE_MAX 48 |
| 93 | #else |
| 94 | #define BOOT_SERIAL_IMAGE_STATE_SIZE_MAX 0 |
| 95 | #endif |
| 96 | #ifdef MCUBOOT_SERIAL_IMG_GRP_HASH |
| 97 | #define BOOT_SERIAL_HASH_SIZE_MAX 36 |
| 98 | #else |
| 99 | #define BOOT_SERIAL_HASH_SIZE_MAX 0 |
| 100 | #endif |
| 101 | |
| 102 | #define BOOT_SERIAL_OUT_MAX ((128 + BOOT_SERIAL_IMAGE_STATE_SIZE_MAX + \ |
| 103 | BOOT_SERIAL_HASH_SIZE_MAX) * BOOT_IMAGE_NUMBER) |
| 104 | |
Piotr Dymacz | f5e7753 | 2022-10-30 17:43:45 +0100 | [diff] [blame] | 105 | #define BOOT_SERIAL_FRAME_MTU 124 /* 127 - pkt start (2 bytes) and stop (1 byte) */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 106 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 107 | #ifdef __ZEPHYR__ |
Carles Cufi | 0165be8 | 2018-03-26 17:43:51 +0200 | [diff] [blame] | 108 | /* base64 lib encodes data to null-terminated string */ |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 109 | #define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1) |
| 110 | |
| 111 | #define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */ |
| 112 | #define CRC_CITT_POLYMINAL 0x1021 |
| 113 | |
| 114 | #define ntohs(x) sys_be16_to_cpu(x) |
| 115 | #define htons(x) sys_cpu_to_be16(x) |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 116 | #elif __ESPRESSIF__ |
| 117 | #define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1) |
| 118 | #define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */ |
| 119 | |
| 120 | #define ntohs(x) be16toh(x) |
| 121 | #define htons(x) htobe16(x) |
| 122 | |
| 123 | #define base64_decode mbedtls_base64_decode |
| 124 | #define base64_encode mbedtls_base64_encode |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 125 | #endif |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 126 | |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 127 | #if (BOOT_IMAGE_NUMBER > 1) |
| 128 | #define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x)) |
| 129 | #else |
| 130 | #define IMAGES_ITER(x) |
| 131 | #endif |
| 132 | |
Jamie McCrae | ad1fb3d | 2022-12-01 14:24:37 +0000 | [diff] [blame] | 133 | static char in_buf[MCUBOOT_SERIAL_MAX_RECEIVE_SIZE + 1]; |
| 134 | static char dec_buf[MCUBOOT_SERIAL_MAX_RECEIVE_SIZE + 1]; |
Marko Kiiskila | 8b1ce3a | 2018-06-14 13:20:46 -0700 | [diff] [blame] | 135 | const struct boot_uart_funcs *boot_uf; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 136 | static struct nmgr_hdr *bs_hdr; |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 137 | static bool bs_entry; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 138 | |
| 139 | static char bs_obuf[BOOT_SERIAL_OUT_MAX]; |
| 140 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 141 | static void boot_serial_output(void); |
| 142 | |
Jamie McCrae | 827118f | 2023-03-10 13:24:57 +0000 | [diff] [blame] | 143 | #ifdef MCUBOOT_SERIAL_IMG_GRP_HASH |
| 144 | static int boot_serial_get_hash(const struct image_header *hdr, |
| 145 | const struct flash_area *fap, uint8_t *hash); |
| 146 | #endif |
| 147 | |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 148 | static zcbor_state_t cbor_state[2]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 149 | |
Dominik Ermel | 4c0f6c1 | 2022-03-04 15:47:37 +0000 | [diff] [blame] | 150 | void reset_cbor_state(void) |
| 151 | { |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 152 | zcbor_new_encode_state(cbor_state, 2, (uint8_t *)bs_obuf, |
Daniel DeGrasse | c393b54 | 2023-05-23 21:52:09 +0000 | [diff] [blame] | 153 | sizeof(bs_obuf), 0); |
Dominik Ermel | 4c0f6c1 | 2022-03-04 15:47:37 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 156 | /** |
Dominik Ermel | bd69c3d | 2021-07-28 11:27:31 +0000 | [diff] [blame] | 157 | * Function that processes MGMT_GROUP_ID_PERUSER mcumgr group and may be |
| 158 | * used to process any groups that have not been processed by generic boot |
| 159 | * serial implementation. |
Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 160 | * |
| 161 | * @param[in] hdr -- the decoded header of mcumgr message; |
| 162 | * @param[in] buffer -- buffer with first mcumgr message; |
| 163 | * @param[in] len -- length of of data in buffer; |
| 164 | * @param[out] *cs -- object with encoded response. |
| 165 | * |
| 166 | * @return 0 on success; non-0 error code otherwise. |
| 167 | */ |
| 168 | extern int bs_peruser_system_specific(const struct nmgr_hdr *hdr, |
| 169 | const char *buffer, |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 170 | int len, zcbor_state_t *cs); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 171 | |
Dominik Ermel | d49cfc1 | 2022-06-09 08:24:48 +0000 | [diff] [blame] | 172 | #define zcbor_tstr_put_lit_cast(state, string) \ |
Jamie McCrae | 393af79 | 2023-04-14 11:31:16 +0100 | [diff] [blame] | 173 | zcbor_tstr_encode_ptr(state, (char *)string, sizeof(string) - 1) |
Dominik Ermel | d49cfc1 | 2022-06-09 08:24:48 +0000 | [diff] [blame] | 174 | |
| 175 | #ifndef MCUBOOT_USE_SNPRINTF |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 176 | /* |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 177 | * Convert version into string without use of snprintf(). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 178 | */ |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 179 | static int |
| 180 | u32toa(char *tgt, uint32_t val) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 181 | { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 182 | char *dst; |
| 183 | uint32_t d = 1; |
| 184 | uint32_t dgt; |
| 185 | int n = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 186 | |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 187 | dst = tgt; |
| 188 | while (val / d >= 10) { |
| 189 | d *= 10; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 190 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 191 | while (d) { |
| 192 | dgt = val / d; |
| 193 | val %= d; |
| 194 | d /= 10; |
| 195 | if (n || dgt > 0 || d == 0) { |
| 196 | *dst++ = dgt + '0'; |
| 197 | ++n; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 198 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 199 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 200 | *dst = '\0'; |
| 201 | |
| 202 | return dst - tgt; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * dst has to be able to fit "255.255.65535.4294967295" (25 characters). |
| 207 | */ |
| 208 | static void |
| 209 | bs_list_img_ver(char *dst, int maxlen, struct image_version *ver) |
| 210 | { |
| 211 | int off; |
| 212 | |
| 213 | off = u32toa(dst, ver->iv_major); |
| 214 | dst[off++] = '.'; |
| 215 | off += u32toa(dst + off, ver->iv_minor); |
| 216 | dst[off++] = '.'; |
| 217 | off += u32toa(dst + off, ver->iv_revision); |
Jamie McCrae | e5c57dd | 2023-03-21 14:45:21 +0000 | [diff] [blame] | 218 | |
| 219 | if (ver->iv_build_num != 0) { |
| 220 | dst[off++] = '.'; |
| 221 | off += u32toa(dst + off, ver->iv_build_num); |
| 222 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 223 | } |
Dominik Ermel | d49cfc1 | 2022-06-09 08:24:48 +0000 | [diff] [blame] | 224 | #else |
| 225 | /* |
| 226 | * dst has to be able to fit "255.255.65535.4294967295" (25 characters). |
| 227 | */ |
| 228 | static void |
| 229 | bs_list_img_ver(char *dst, int maxlen, struct image_version *ver) |
| 230 | { |
Jamie McCrae | e5c57dd | 2023-03-21 14:45:21 +0000 | [diff] [blame] | 231 | int len; |
| 232 | |
| 233 | len = snprintf(dst, maxlen, "%hu.%hu.%hu", (uint16_t)ver->iv_major, |
| 234 | (uint16_t)ver->iv_minor, ver->iv_revision); |
| 235 | |
| 236 | if (ver->iv_build_num != 0 && len > 0 && len < maxlen) { |
Benjamin Bigler | 480b97f | 2023-09-07 15:25:34 +0200 | [diff] [blame] | 237 | snprintf(&dst[len], (maxlen - len), ".%u", ver->iv_build_num); |
Jamie McCrae | e5c57dd | 2023-03-21 14:45:21 +0000 | [diff] [blame] | 238 | } |
Dominik Ermel | d49cfc1 | 2022-06-09 08:24:48 +0000 | [diff] [blame] | 239 | } |
| 240 | #endif /* !MCUBOOT_USE_SNPRINTF */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * List images. |
| 244 | */ |
| 245 | static void |
| 246 | bs_list(char *buf, int len) |
| 247 | { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 248 | struct image_header hdr; |
Øyvind Rønningstad | 9f4aefd | 2021-03-08 21:11:25 +0100 | [diff] [blame] | 249 | uint32_t slot, area_id; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 250 | const struct flash_area *fap; |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 251 | uint8_t image_index; |
Jamie McCrae | 827118f | 2023-03-10 13:24:57 +0000 | [diff] [blame] | 252 | #ifdef MCUBOOT_SERIAL_IMG_GRP_HASH |
| 253 | uint8_t hash[32]; |
| 254 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 255 | |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 256 | zcbor_map_start_encode(cbor_state, 1); |
| 257 | zcbor_tstr_put_lit_cast(cbor_state, "images"); |
| 258 | zcbor_list_start_encode(cbor_state, 5); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 259 | image_index = 0; |
| 260 | IMAGES_ITER(image_index) { |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 261 | #ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE |
| 262 | int swap_status = boot_swap_type_multi(image_index); |
| 263 | #endif |
| 264 | |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 265 | for (slot = 0; slot < 2; slot++) { |
Jamie McCrae | 82feb9a | 2023-06-26 09:35:05 +0100 | [diff] [blame] | 266 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 267 | uint8_t tmpbuf[64]; |
| 268 | |
| 269 | #ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE |
| 270 | bool active = false; |
| 271 | bool confirmed = false; |
| 272 | bool pending = false; |
| 273 | bool permanent = false; |
| 274 | #endif |
| 275 | |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 276 | area_id = flash_area_id_from_multi_image_slot(image_index, slot); |
| 277 | if (flash_area_open(area_id, &fap)) { |
| 278 | continue; |
| 279 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 280 | |
Andrzej Puzdrowski | 4f9c730 | 2021-07-16 17:34:43 +0200 | [diff] [blame] | 281 | int rc = BOOT_HOOK_CALL(boot_read_image_header_hook, |
| 282 | BOOT_HOOK_REGULAR, image_index, slot, &hdr); |
| 283 | if (rc == BOOT_HOOK_REGULAR) |
| 284 | { |
| 285 | flash_area_read(fap, 0, &hdr, sizeof(hdr)); |
| 286 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 287 | |
Andrzej Puzdrowski | 4f9c730 | 2021-07-16 17:34:43 +0200 | [diff] [blame] | 288 | if (hdr.ih_magic == IMAGE_MAGIC) |
| 289 | { |
| 290 | BOOT_HOOK_CALL_FIH(boot_image_check_hook, |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 291 | FIH_BOOT_HOOK_REGULAR, |
Andrzej Puzdrowski | 4f9c730 | 2021-07-16 17:34:43 +0200 | [diff] [blame] | 292 | fih_rc, image_index, slot); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 293 | if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR)) |
Andrzej Puzdrowski | 4f9c730 | 2021-07-16 17:34:43 +0200 | [diff] [blame] | 294 | { |
Wouter Cappelle | 953a761 | 2021-05-03 16:53:05 +0200 | [diff] [blame] | 295 | #ifdef MCUBOOT_ENC_IMAGES |
Jamie McCrae | c9fa608 | 2023-07-21 10:23:17 +0100 | [diff] [blame] | 296 | if (IS_ENCRYPTED(&hdr)) { |
| 297 | FIH_CALL(boot_image_validate_encrypted, fih_rc, fap, |
| 298 | &hdr, tmpbuf, sizeof(tmpbuf)); |
| 299 | } else { |
| 300 | #endif |
| 301 | FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr, |
| 302 | fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL); |
| 303 | #ifdef MCUBOOT_ENC_IMAGES |
Wouter Cappelle | 953a761 | 2021-05-03 16:53:05 +0200 | [diff] [blame] | 304 | } |
| 305 | #endif |
Andrzej Puzdrowski | 4f9c730 | 2021-07-16 17:34:43 +0200 | [diff] [blame] | 306 | } |
Jamie McCrae | 82feb9a | 2023-06-26 09:35:05 +0100 | [diff] [blame] | 307 | } |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 308 | |
Jamie McCrae | 82feb9a | 2023-06-26 09:35:05 +0100 | [diff] [blame] | 309 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
| 310 | flash_area_close(fap); |
| 311 | continue; |
Andrzej Puzdrowski | 4f9c730 | 2021-07-16 17:34:43 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Jamie McCrae | 827118f | 2023-03-10 13:24:57 +0000 | [diff] [blame] | 314 | #ifdef MCUBOOT_SERIAL_IMG_GRP_HASH |
| 315 | /* Retrieve SHA256 hash of image for identification */ |
| 316 | rc = boot_serial_get_hash(&hdr, fap, hash); |
| 317 | #endif |
| 318 | |
Andrzej Puzdrowski | 4f9c730 | 2021-07-16 17:34:43 +0200 | [diff] [blame] | 319 | flash_area_close(fap); |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 320 | zcbor_map_start_encode(cbor_state, 20); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 321 | |
| 322 | #if (BOOT_IMAGE_NUMBER > 1) |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 323 | zcbor_tstr_put_lit_cast(cbor_state, "image"); |
| 324 | zcbor_uint32_put(cbor_state, image_index); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 325 | #endif |
| 326 | |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 327 | #ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE |
| 328 | if (swap_status == BOOT_SWAP_TYPE_NONE) { |
| 329 | if (slot == BOOT_PRIMARY_SLOT) { |
| 330 | confirmed = true; |
| 331 | active = true; |
| 332 | } |
| 333 | } else if (swap_status == BOOT_SWAP_TYPE_TEST) { |
| 334 | if (slot == BOOT_PRIMARY_SLOT) { |
| 335 | confirmed = true; |
| 336 | } else { |
| 337 | pending = true; |
| 338 | } |
| 339 | } else if (swap_status == BOOT_SWAP_TYPE_PERM) { |
| 340 | if (slot == BOOT_PRIMARY_SLOT) { |
| 341 | confirmed = true; |
| 342 | } else { |
| 343 | pending = true; |
| 344 | permanent = true; |
| 345 | } |
| 346 | } else if (swap_status == BOOT_SWAP_TYPE_REVERT) { |
| 347 | if (slot == BOOT_PRIMARY_SLOT) { |
| 348 | active = true; |
| 349 | } else { |
| 350 | confirmed = true; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if (!(hdr.ih_flags & IMAGE_F_NON_BOOTABLE)) { |
| 355 | zcbor_tstr_put_lit_cast(cbor_state, "bootable"); |
Jamie McCrae | 82feb9a | 2023-06-26 09:35:05 +0100 | [diff] [blame] | 356 | zcbor_bool_put(cbor_state, true); |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | if (confirmed) { |
| 360 | zcbor_tstr_put_lit_cast(cbor_state, "confirmed"); |
| 361 | zcbor_bool_put(cbor_state, true); |
| 362 | } |
| 363 | |
| 364 | if (active) { |
| 365 | zcbor_tstr_put_lit_cast(cbor_state, "active"); |
| 366 | zcbor_bool_put(cbor_state, true); |
| 367 | } |
| 368 | |
| 369 | if (pending) { |
| 370 | zcbor_tstr_put_lit_cast(cbor_state, "pending"); |
| 371 | zcbor_bool_put(cbor_state, true); |
| 372 | } |
| 373 | |
| 374 | if (permanent) { |
| 375 | zcbor_tstr_put_lit_cast(cbor_state, "permanent"); |
| 376 | zcbor_bool_put(cbor_state, true); |
| 377 | } |
| 378 | #endif |
| 379 | |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 380 | zcbor_tstr_put_lit_cast(cbor_state, "slot"); |
| 381 | zcbor_uint32_put(cbor_state, slot); |
Jamie McCrae | 827118f | 2023-03-10 13:24:57 +0000 | [diff] [blame] | 382 | |
| 383 | #ifdef MCUBOOT_SERIAL_IMG_GRP_HASH |
| 384 | if (rc == 0) { |
| 385 | zcbor_tstr_put_lit_cast(cbor_state, "hash"); |
| 386 | zcbor_bstr_encode_ptr(cbor_state, hash, sizeof(hash)); |
| 387 | } |
| 388 | #endif |
| 389 | |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 390 | zcbor_tstr_put_lit_cast(cbor_state, "version"); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 391 | |
| 392 | bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver); |
Jamie McCrae | 827118f | 2023-03-10 13:24:57 +0000 | [diff] [blame] | 393 | |
Jamie McCrae | 393af79 | 2023-04-14 11:31:16 +0100 | [diff] [blame] | 394 | zcbor_tstr_encode_ptr(cbor_state, (char *)tmpbuf, strlen((char *)tmpbuf)); |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 395 | zcbor_map_end_encode(cbor_state, 20); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 396 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 397 | } |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 398 | zcbor_list_end_encode(cbor_state, 5); |
| 399 | zcbor_map_end_encode(cbor_state, 1); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 400 | boot_serial_output(); |
| 401 | } |
| 402 | |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 403 | #ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE |
| 404 | /* |
| 405 | * Set image state. |
| 406 | */ |
| 407 | static void |
| 408 | bs_set(char *buf, int len) |
| 409 | { |
| 410 | /* |
| 411 | * Expected data format. |
| 412 | * { |
| 413 | * "confirm":<true for confirm, false for test> |
| 414 | * "hash":<hash of image (OPTIONAL for single image only)> |
| 415 | * } |
| 416 | */ |
| 417 | uint8_t image_index = 0; |
| 418 | size_t decoded = 0; |
| 419 | uint8_t hash[32]; |
| 420 | bool confirm; |
| 421 | struct zcbor_string img_hash; |
| 422 | bool ok; |
| 423 | int rc; |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 424 | |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 425 | #ifdef MCUBOOT_SERIAL_IMG_GRP_HASH |
| 426 | bool found = false; |
| 427 | #endif |
| 428 | |
| 429 | zcbor_state_t zsd[4]; |
| 430 | zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1); |
| 431 | |
| 432 | struct zcbor_map_decode_key_val image_set_state_decode[] = { |
Jamie McCrae | 9fad4c1 | 2023-07-20 08:32:42 +0100 | [diff] [blame] | 433 | ZCBOR_MAP_DECODE_KEY_DECODER("confirm", zcbor_bool_decode, &confirm), |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 434 | #ifdef MCUBOOT_SERIAL_IMG_GRP_HASH |
| 435 | ZCBOR_MAP_DECODE_KEY_DECODER("hash", zcbor_bstr_decode, &img_hash), |
| 436 | #endif |
| 437 | }; |
| 438 | |
| 439 | ok = zcbor_map_decode_bulk(zsd, image_set_state_decode, ARRAY_SIZE(image_set_state_decode), |
| 440 | &decoded) == 0; |
| 441 | |
Piotr Dymacz | f2cb550 | 2023-06-28 12:09:19 +0200 | [diff] [blame] | 442 | if (!ok) { |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 443 | rc = MGMT_ERR_EINVAL; |
| 444 | goto out; |
| 445 | } |
| 446 | |
| 447 | #ifdef MCUBOOT_SERIAL_IMG_GRP_HASH |
| 448 | if ((img_hash.len != sizeof(hash) && img_hash.len != 0) || |
| 449 | (img_hash.len == 0 && BOOT_IMAGE_NUMBER > 1)) { |
| 450 | /* Hash is required and was not provided or is invalid size */ |
| 451 | rc = MGMT_ERR_EINVAL; |
| 452 | goto out; |
| 453 | } |
| 454 | |
| 455 | if (img_hash.len != 0) { |
| 456 | for (image_index = 0; image_index < BOOT_IMAGE_NUMBER; ++image_index) { |
| 457 | struct image_header hdr; |
| 458 | uint32_t area_id; |
| 459 | const struct flash_area *fap; |
| 460 | uint8_t tmpbuf[64]; |
| 461 | |
| 462 | area_id = flash_area_id_from_multi_image_slot(image_index, 1); |
| 463 | if (flash_area_open(area_id, &fap)) { |
| 464 | BOOT_LOG_ERR("Failed to open flash area ID %d", area_id); |
| 465 | continue; |
| 466 | } |
| 467 | |
| 468 | rc = BOOT_HOOK_CALL(boot_read_image_header_hook, |
| 469 | BOOT_HOOK_REGULAR, image_index, 1, &hdr); |
| 470 | if (rc == BOOT_HOOK_REGULAR) |
| 471 | { |
| 472 | flash_area_read(fap, 0, &hdr, sizeof(hdr)); |
| 473 | } |
| 474 | |
| 475 | if (hdr.ih_magic == IMAGE_MAGIC) |
| 476 | { |
| 477 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
| 478 | |
| 479 | BOOT_HOOK_CALL_FIH(boot_image_check_hook, |
| 480 | FIH_BOOT_HOOK_REGULAR, |
| 481 | fih_rc, image_index, 1); |
| 482 | if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR)) |
| 483 | { |
Jamie McCrae | c9fa608 | 2023-07-21 10:23:17 +0100 | [diff] [blame] | 484 | #ifdef MCUBOOT_ENC_IMAGES |
| 485 | if (IS_ENCRYPTED(&hdr)) { |
| 486 | FIH_CALL(boot_image_validate_encrypted, fih_rc, fap, |
| 487 | &hdr, tmpbuf, sizeof(tmpbuf)); |
| 488 | } else { |
| 489 | #endif |
| 490 | FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr, |
| 491 | fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL); |
| 492 | #ifdef MCUBOOT_ENC_IMAGES |
| 493 | } |
| 494 | #endif |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) { |
| 498 | continue; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /* Retrieve SHA256 hash of image for identification */ |
| 503 | rc = boot_serial_get_hash(&hdr, fap, hash); |
| 504 | flash_area_close(fap); |
| 505 | |
| 506 | if (rc == 0 && memcmp(hash, img_hash.value, sizeof(hash)) == 0) { |
| 507 | /* Hash matches, set this slot for test or confirmation */ |
| 508 | found = true; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (!found) { |
| 514 | /* Image was not found with specified hash */ |
| 515 | BOOT_LOG_ERR("Did not find image with specified hash"); |
| 516 | rc = MGMT_ERR_ENOENT; |
| 517 | goto out; |
| 518 | } |
| 519 | } |
| 520 | #endif |
| 521 | |
| 522 | rc = boot_set_pending_multi(image_index, confirm); |
| 523 | |
| 524 | out: |
| 525 | if (rc == 0) { |
| 526 | /* Success - return updated list of images */ |
| 527 | bs_list(buf, len); |
| 528 | } else { |
| 529 | /* Error code, only return the error */ |
| 530 | zcbor_map_start_encode(cbor_state, 10); |
| 531 | zcbor_tstr_put_lit_cast(cbor_state, "rc"); |
| 532 | zcbor_int32_put(cbor_state, rc); |
| 533 | zcbor_map_end_encode(cbor_state, 10); |
| 534 | |
| 535 | boot_serial_output(); |
| 536 | } |
| 537 | } |
| 538 | #endif |
| 539 | |
| 540 | /* |
| 541 | * Send rc code only. |
| 542 | */ |
| 543 | static void |
| 544 | bs_rc_rsp(int rc_code) |
| 545 | { |
| 546 | zcbor_map_start_encode(cbor_state, 10); |
| 547 | zcbor_tstr_put_lit_cast(cbor_state, "rc"); |
| 548 | zcbor_int32_put(cbor_state, rc_code); |
| 549 | zcbor_map_end_encode(cbor_state, 10); |
| 550 | boot_serial_output(); |
| 551 | } |
| 552 | |
| 553 | static void |
| 554 | bs_list_set(uint8_t op, char *buf, int len) |
| 555 | { |
| 556 | if (op == NMGR_OP_READ) { |
| 557 | bs_list(buf, len); |
| 558 | } else { |
| 559 | #ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE |
| 560 | bs_set(buf, len); |
| 561 | #else |
| 562 | bs_rc_rsp(MGMT_ERR_ENOTSUP); |
| 563 | #endif |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 568 | /** Erases range of flash, aligned to sector size |
| 569 | * |
| 570 | * Function will erase all sectors withing [start, end] range; it does not check |
| 571 | * the @p start for alignment, and it will use @p end to find boundaries of las |
| 572 | * sector to erase. Function returns offset of the first byte past the last |
| 573 | * erased sector, so basically offset of next sector to be erased if needed. |
| 574 | * The function is intended to be called iteratively with previously returned |
| 575 | * offset as @p start. |
| 576 | * |
| 577 | * @param start starting offset, aligned to sector offset; |
| 578 | * @param end ending offset, maybe anywhere within sector; |
| 579 | * |
| 580 | * @retval On success: offset of the first byte past last erased sector; |
| 581 | * On failure: -EINVAL. |
| 582 | */ |
| 583 | static off_t erase_range(const struct flash_area *fap, off_t start, off_t end) |
| 584 | { |
| 585 | struct flash_sector sect; |
| 586 | size_t size; |
| 587 | int rc; |
| 588 | |
| 589 | if (end >= flash_area_get_size(fap)) { |
| 590 | return -EINVAL; |
| 591 | } |
| 592 | |
| 593 | if (end < start) { |
| 594 | return start; |
| 595 | } |
| 596 | |
Dominik Ermel | 2476988 | 2023-01-05 13:36:35 +0000 | [diff] [blame] | 597 | if (flash_area_get_sector(fap, end, §)) { |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 598 | return -EINVAL; |
| 599 | } |
| 600 | |
| 601 | size = flash_sector_get_off(§) + flash_sector_get_size(§) - start; |
Stephanos Ioannidis | 09e2bd7 | 2022-07-11 22:01:49 +0900 | [diff] [blame] | 602 | BOOT_LOG_INF("Erasing range 0x%jx:0x%jx", (intmax_t)start, |
| 603 | (intmax_t)(start + size - 1)); |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 604 | |
| 605 | rc = flash_area_erase(fap, start, size); |
| 606 | if (rc != 0) { |
| 607 | BOOT_LOG_ERR("Error %d while erasing range", rc); |
| 608 | return -EINVAL; |
| 609 | } |
| 610 | |
| 611 | return start + size; |
| 612 | } |
| 613 | #endif |
| 614 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 615 | /* |
| 616 | * Image upload request. |
| 617 | */ |
| 618 | static void |
| 619 | bs_upload(char *buf, int len) |
| 620 | { |
Dominik Ermel | 5bd8744 | 2022-06-13 15:14:01 +0000 | [diff] [blame] | 621 | static size_t img_size; /* Total image size, held for duration of upload */ |
| 622 | static uint32_t curr_off; /* Expected current offset */ |
| 623 | const uint8_t *img_chunk = NULL; /* Pointer to buffer with received image chunk */ |
| 624 | size_t img_chunk_len = 0; /* Length of received image chunk */ |
| 625 | size_t img_chunk_off = SIZE_MAX; /* Offset of image chunk within image */ |
| 626 | uint8_t rem_bytes; /* Reminder bytes after aligning chunk write to |
| 627 | * to flash alignment */ |
Piotr Dymacz | 6a8746d | 2023-06-26 17:13:48 +0200 | [diff] [blame] | 628 | uint32_t img_num_tmp = UINT_MAX; /* Temp variable for image number */ |
| 629 | static uint32_t img_num = 0; |
Dominik Ermel | 5bd8744 | 2022-06-13 15:14:01 +0000 | [diff] [blame] | 630 | size_t img_size_tmp = SIZE_MAX; /* Temp variable for image size */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 631 | const struct flash_area *fap = NULL; |
| 632 | int rc; |
Jamie McCrae | cb07e88 | 2023-04-14 09:28:24 +0100 | [diff] [blame] | 633 | struct zcbor_string img_chunk_data; |
| 634 | size_t decoded = 0; |
| 635 | bool ok; |
Dominik Ermel | 3d4e55d | 2021-07-09 11:14:10 +0000 | [diff] [blame] | 636 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 637 | static off_t not_yet_erased = 0; /* Offset of next byte to erase; writes to flash |
| 638 | * are done in consecutive manner and erases are done |
| 639 | * to allow currently received chunk to be written; |
| 640 | * this state variable holds information where last |
| 641 | * erase has stopped to let us know whether erase |
| 642 | * is needed to be able to write current chunk. |
| 643 | */ |
| 644 | static struct flash_sector status_sector; |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 645 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 646 | |
Jamie McCrae | cb07e88 | 2023-04-14 09:28:24 +0100 | [diff] [blame] | 647 | zcbor_state_t zsd[4]; |
| 648 | zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1); |
| 649 | |
| 650 | struct zcbor_map_decode_key_val image_upload_decode[] = { |
Piotr Dymacz | 6a8746d | 2023-06-26 17:13:48 +0200 | [diff] [blame] | 651 | ZCBOR_MAP_DECODE_KEY_DECODER("image", zcbor_uint32_decode, &img_num_tmp), |
Jamie McCrae | cb07e88 | 2023-04-14 09:28:24 +0100 | [diff] [blame] | 652 | ZCBOR_MAP_DECODE_KEY_DECODER("data", zcbor_bstr_decode, &img_chunk_data), |
| 653 | ZCBOR_MAP_DECODE_KEY_DECODER("len", zcbor_size_decode, &img_size_tmp), |
| 654 | ZCBOR_MAP_DECODE_KEY_DECODER("off", zcbor_size_decode, &img_chunk_off), |
| 655 | }; |
| 656 | |
| 657 | ok = zcbor_map_decode_bulk(zsd, image_upload_decode, ARRAY_SIZE(image_upload_decode), |
| 658 | &decoded) == 0; |
| 659 | |
| 660 | if (!ok) { |
| 661 | goto out_invalid_data; |
| 662 | } |
| 663 | |
| 664 | img_chunk = img_chunk_data.value; |
| 665 | img_chunk_len = img_chunk_data.len; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 666 | |
| 667 | /* |
| 668 | * Expected data format. |
| 669 | * { |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 670 | * "image":<image number in a multi-image set (OPTIONAL)> |
| 671 | * "data":<image data> |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 672 | * "len":<image len> |
| 673 | * "off":<current offset of image data> |
| 674 | * } |
| 675 | */ |
| 676 | |
Dominik Ermel | 5bd8744 | 2022-06-13 15:14:01 +0000 | [diff] [blame] | 677 | if (img_chunk_off == SIZE_MAX || img_chunk == NULL) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 678 | /* |
| 679 | * Offset must be set in every block. |
| 680 | */ |
| 681 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Piotr Dymacz | 6a8746d | 2023-06-26 17:13:48 +0200 | [diff] [blame] | 684 | /* Use image number only from packet with offset == 0. */ |
| 685 | if (img_chunk_off == 0) { |
| 686 | if (img_num_tmp != UINT_MAX) { |
| 687 | img_num = img_num_tmp; |
| 688 | } else { |
| 689 | img_num = 0; |
| 690 | } |
| 691 | } |
| 692 | |
Dominik Ermel | 48decca | 2021-07-09 10:23:58 +0000 | [diff] [blame] | 693 | #if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD) |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 694 | rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap); |
Dominik Ermel | 48decca | 2021-07-09 10:23:58 +0000 | [diff] [blame] | 695 | #else |
| 696 | rc = flash_area_open(flash_area_id_from_direct_image(img_num), &fap); |
| 697 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 698 | if (rc) { |
| 699 | rc = MGMT_ERR_EINVAL; |
| 700 | goto out; |
| 701 | } |
| 702 | |
Dominik Ermel | 5bd8744 | 2022-06-13 15:14:01 +0000 | [diff] [blame] | 703 | if (img_chunk_off == 0) { |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 704 | /* Receiving chunk with 0 offset resets the upload state; this basically |
| 705 | * means that upload has started from beginning. |
| 706 | */ |
| 707 | const size_t area_size = flash_area_get_size(fap); |
Dominik Ermel | 5bd8744 | 2022-06-13 15:14:01 +0000 | [diff] [blame] | 708 | |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 709 | curr_off = 0; |
| 710 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
| 711 | /* Get trailer sector information; this is done early because inability to get |
| 712 | * that sector information means that upload will not work anyway. |
| 713 | * TODO: This is single occurrence issue, it should get detected during tests |
| 714 | * and fixed otherwise you are deploying broken mcuboot. |
| 715 | */ |
Dominik Ermel | 2476988 | 2023-01-05 13:36:35 +0000 | [diff] [blame] | 716 | if (flash_area_get_sector(fap, boot_status_off(fap), &status_sector)) { |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 717 | rc = MGMT_ERR_EUNKNOWN; |
| 718 | BOOT_LOG_ERR("Unable to determine flash sector of the image trailer"); |
| 719 | goto out; |
| 720 | } |
| 721 | #endif |
| 722 | |
Wouter Cappelle | bb7a39d | 2021-05-03 16:44:44 +0200 | [diff] [blame] | 723 | #if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE) |
| 724 | /* We are using swap state at end of flash area to store validation |
| 725 | * result. Make sure the user cannot write it from an image to skip validation. |
| 726 | */ |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 727 | if (img_size_tmp > (area_size - BOOT_MAGIC_SZ)) { |
Wouter Cappelle | bb7a39d | 2021-05-03 16:44:44 +0200 | [diff] [blame] | 728 | goto out_invalid_data; |
| 729 | } |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 730 | #else |
| 731 | if (img_size_tmp > area_size) { |
| 732 | goto out_invalid_data; |
| 733 | } |
| 734 | |
Wouter Cappelle | bb7a39d | 2021-05-03 16:44:44 +0200 | [diff] [blame] | 735 | #endif |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 736 | |
Dominik Ermel | 3d4e55d | 2021-07-09 11:14:10 +0000 | [diff] [blame] | 737 | #ifndef MCUBOOT_ERASE_PROGRESSIVELY |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 738 | /* Non-progressive erase erases entire image slot when first chunk of |
| 739 | * an image is received. |
| 740 | */ |
| 741 | rc = flash_area_erase(fap, 0, area_size); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 742 | if (rc) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 743 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 744 | } |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 745 | #else |
| 746 | not_yet_erased = 0; |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 747 | #endif |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 748 | |
Dominik Ermel | 5bd8744 | 2022-06-13 15:14:01 +0000 | [diff] [blame] | 749 | img_size = img_size_tmp; |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 750 | } else if (img_chunk_off != curr_off) { |
| 751 | /* If received chunk offset does not match expected one jump, pretend |
| 752 | * success and jump to out; out will respond to client with success |
| 753 | * and request the expected offset, held by curr_off. |
| 754 | */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 755 | rc = 0; |
| 756 | goto out; |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 757 | } else if (curr_off + img_chunk_len > img_size) { |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 758 | rc = MGMT_ERR_EINVAL; |
| 759 | goto out; |
| 760 | } |
| 761 | |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 762 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
| 763 | /* Progressive erase will erase enough flash, aligned to sector size, |
| 764 | * as needed for the current chunk to be written. |
| 765 | */ |
| 766 | not_yet_erased = erase_range(fap, not_yet_erased, |
| 767 | curr_off + img_chunk_len - 1); |
| 768 | |
| 769 | if (not_yet_erased < 0) { |
| 770 | rc = MGMT_ERR_EINVAL; |
| 771 | goto out; |
| 772 | } |
| 773 | #endif |
| 774 | |
| 775 | /* Writes are aligned to flash write alignment, so may drop a few bytes |
| 776 | * from the end of the buffer; we will request these bytes again with |
| 777 | * new buffer by responding with request for offset after the last aligned |
| 778 | * write. |
| 779 | */ |
Dominik Ermel | 5bd8744 | 2022-06-13 15:14:01 +0000 | [diff] [blame] | 780 | rem_bytes = img_chunk_len % flash_area_align(fap); |
Dominik Ermel | 7d2f0bf | 2022-06-21 16:15:34 +0000 | [diff] [blame] | 781 | img_chunk_len -= rem_bytes; |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 782 | |
Dominik Ermel | 7d2f0bf | 2022-06-21 16:15:34 +0000 | [diff] [blame] | 783 | if (curr_off + img_chunk_len + rem_bytes < img_size) { |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 784 | rem_bytes = 0; |
Fabio Utzig | 30f6b2a | 2018-03-29 16:18:53 -0300 | [diff] [blame] | 785 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 786 | |
Dominik Ermel | 5bd8744 | 2022-06-13 15:14:01 +0000 | [diff] [blame] | 787 | BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_chunk_len); |
Dominik Ermel | 7d2f0bf | 2022-06-21 16:15:34 +0000 | [diff] [blame] | 788 | /* Write flash aligned chunk, note that img_chunk_len now holds aligned length */ |
Jamie McCrae | 9d3fd7f | 2022-11-30 15:44:44 +0000 | [diff] [blame] | 789 | #if defined(MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE) && MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE > 0 |
| 790 | if (flash_area_align(fap) > 1 && |
| 791 | (((size_t)img_chunk) & (flash_area_align(fap) - 1)) != 0) { |
| 792 | /* Buffer address incompatible with write address, use buffer to write */ |
| 793 | uint8_t write_size = MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE; |
| 794 | uint8_t wbs_aligned[MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE]; |
| 795 | |
| 796 | while (img_chunk_len >= flash_area_align(fap)) { |
| 797 | if (write_size > img_chunk_len) { |
| 798 | write_size = img_chunk_len; |
| 799 | } |
| 800 | |
| 801 | memset(wbs_aligned, flash_area_erased_val(fap), sizeof(wbs_aligned)); |
| 802 | memcpy(wbs_aligned, img_chunk, write_size); |
| 803 | |
| 804 | rc = flash_area_write(fap, curr_off, wbs_aligned, write_size); |
| 805 | |
| 806 | if (rc != 0) { |
| 807 | goto out; |
| 808 | } |
| 809 | |
| 810 | curr_off += write_size; |
| 811 | img_chunk += write_size; |
| 812 | img_chunk_len -= write_size; |
| 813 | } |
| 814 | } else { |
| 815 | rc = flash_area_write(fap, curr_off, img_chunk, img_chunk_len); |
| 816 | } |
| 817 | #else |
Dominik Ermel | 7d2f0bf | 2022-06-21 16:15:34 +0000 | [diff] [blame] | 818 | rc = flash_area_write(fap, curr_off, img_chunk, img_chunk_len); |
Jamie McCrae | 9d3fd7f | 2022-11-30 15:44:44 +0000 | [diff] [blame] | 819 | #endif |
| 820 | |
Dominik Ermel | 7d2f0bf | 2022-06-21 16:15:34 +0000 | [diff] [blame] | 821 | if (rc == 0 && rem_bytes) { |
| 822 | /* Non-zero rem_bytes means that last chunk needs alignment; the aligned |
| 823 | * part, in the img_chunk_len - rem_bytes count bytes, has already been |
| 824 | * written by the above write, so we are left with the rem_bytes. |
| 825 | */ |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 826 | uint8_t wbs_aligned[BOOT_MAX_ALIGN]; |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 827 | |
Dominik Ermel | 7d2f0bf | 2022-06-21 16:15:34 +0000 | [diff] [blame] | 828 | memset(wbs_aligned, flash_area_erased_val(fap), sizeof(wbs_aligned)); |
| 829 | memcpy(wbs_aligned, img_chunk + img_chunk_len, rem_bytes); |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 830 | |
Dominik Ermel | 7d2f0bf | 2022-06-21 16:15:34 +0000 | [diff] [blame] | 831 | rc = flash_area_write(fap, curr_off + img_chunk_len, wbs_aligned, |
| 832 | flash_area_align(fap)); |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 835 | if (rc == 0) { |
Dominik Ermel | 7d2f0bf | 2022-06-21 16:15:34 +0000 | [diff] [blame] | 836 | curr_off += img_chunk_len + rem_bytes; |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 837 | if (curr_off == img_size) { |
Andrzej Puzdrowski | 4f9c730 | 2021-07-16 17:34:43 +0200 | [diff] [blame] | 838 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 839 | /* Assure that sector for image trailer was erased. */ |
| 840 | /* Check whether it was erased during previous upload. */ |
Dominik Ermel | bcc17b4 | 2022-06-15 15:33:04 +0000 | [diff] [blame] | 841 | off_t start = flash_sector_get_off(&status_sector); |
| 842 | |
| 843 | if (erase_range(fap, start, start) < 0) { |
| 844 | rc = MGMT_ERR_EUNKNOWN; |
| 845 | goto out; |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 846 | } |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 847 | #endif |
Andrzej Puzdrowski | 4f9c730 | 2021-07-16 17:34:43 +0200 | [diff] [blame] | 848 | rc = BOOT_HOOK_CALL(boot_serial_uploaded_hook, 0, img_num, fap, |
| 849 | img_size); |
| 850 | if (rc) { |
| 851 | BOOT_LOG_ERR("Error %d post upload hook", rc); |
| 852 | goto out; |
| 853 | } |
| 854 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 855 | } else { |
| 856 | out_invalid_data: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 857 | rc = MGMT_ERR_EINVAL; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 858 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 859 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 860 | out: |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 861 | BOOT_LOG_INF("RX: 0x%x", rc); |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 862 | zcbor_map_start_encode(cbor_state, 10); |
| 863 | zcbor_tstr_put_lit_cast(cbor_state, "rc"); |
Jamie McCrae | 0b6d343 | 2022-12-02 09:24:10 +0000 | [diff] [blame] | 864 | zcbor_int32_put(cbor_state, rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 865 | if (rc == 0) { |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 866 | zcbor_tstr_put_lit_cast(cbor_state, "off"); |
| 867 | zcbor_uint32_put(cbor_state, curr_off); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 868 | } |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 869 | zcbor_map_end_encode(cbor_state, 10); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 870 | |
| 871 | boot_serial_output(); |
Wouter Cappelle | 953a761 | 2021-05-03 16:53:05 +0200 | [diff] [blame] | 872 | |
| 873 | #ifdef MCUBOOT_ENC_IMAGES |
Jamie McCrae | c9fa608 | 2023-07-21 10:23:17 +0100 | [diff] [blame] | 874 | /* Check if this upload was for the primary slot */ |
| 875 | #if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD) |
| 876 | if (flash_area_id_from_multi_image_slot(img_num, 0) == FLASH_AREA_IMAGE_PRIMARY(0)) |
| 877 | #else |
| 878 | if (flash_area_id_from_direct_image(img_num) == FLASH_AREA_IMAGE_PRIMARY(0)) |
| 879 | #endif |
| 880 | { |
| 881 | if (curr_off == img_size) { |
| 882 | /* Last sector received, now start a decryption on the image if it is encrypted */ |
| 883 | rc = boot_handle_enc_fw(fap); |
| 884 | } |
Wouter Cappelle | 953a761 | 2021-05-03 16:53:05 +0200 | [diff] [blame] | 885 | } |
Jamie McCrae | c9fa608 | 2023-07-21 10:23:17 +0100 | [diff] [blame] | 886 | #endif |
| 887 | |
| 888 | flash_area_close(fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 889 | } |
| 890 | |
Wouter Cappelle | e3ff175 | 2021-05-03 16:36:22 +0200 | [diff] [blame] | 891 | #ifdef MCUBOOT_BOOT_MGMT_ECHO |
Wouter Cappelle | e3ff175 | 2021-05-03 16:36:22 +0200 | [diff] [blame] | 892 | static void |
| 893 | bs_echo(char *buf, int len) |
| 894 | { |
Jamie McCrae | cb07e88 | 2023-04-14 09:28:24 +0100 | [diff] [blame] | 895 | struct zcbor_string value = { 0 }; |
| 896 | struct zcbor_string key; |
| 897 | bool ok; |
Dominik Ermel | 88bd567 | 2022-06-07 15:17:06 +0000 | [diff] [blame] | 898 | uint32_t rc = MGMT_ERR_EINVAL; |
Wouter Cappelle | e3ff175 | 2021-05-03 16:36:22 +0200 | [diff] [blame] | 899 | |
Jamie McCrae | cb07e88 | 2023-04-14 09:28:24 +0100 | [diff] [blame] | 900 | zcbor_state_t zsd[4]; |
| 901 | zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1); |
| 902 | |
| 903 | if (!zcbor_map_start_decode(zsd)) { |
Dominik Ermel | 88bd567 | 2022-06-07 15:17:06 +0000 | [diff] [blame] | 904 | goto out; |
Wouter Cappelle | e3ff175 | 2021-05-03 16:36:22 +0200 | [diff] [blame] | 905 | } |
Dominik Ermel | 88bd567 | 2022-06-07 15:17:06 +0000 | [diff] [blame] | 906 | |
Jamie McCrae | cb07e88 | 2023-04-14 09:28:24 +0100 | [diff] [blame] | 907 | do { |
| 908 | ok = zcbor_tstr_decode(zsd, &key); |
| 909 | |
| 910 | if (ok) { |
| 911 | if (key.len == 1 && *key.value == 'd') { |
| 912 | ok = zcbor_tstr_decode(zsd, &value); |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | ok = zcbor_any_skip(zsd, NULL); |
| 917 | } |
| 918 | } while (ok); |
| 919 | |
| 920 | if (!ok || !zcbor_map_end_decode(zsd)) { |
Dominik Ermel | 88bd567 | 2022-06-07 15:17:06 +0000 | [diff] [blame] | 921 | goto out; |
| 922 | } |
| 923 | |
| 924 | zcbor_map_start_encode(cbor_state, 10); |
| 925 | zcbor_tstr_put_term(cbor_state, "r"); |
Jamie McCrae | cb07e88 | 2023-04-14 09:28:24 +0100 | [diff] [blame] | 926 | if (zcbor_tstr_encode(cbor_state, &value) && zcbor_map_end_encode(cbor_state, 10)) { |
Dominik Ermel | 88bd567 | 2022-06-07 15:17:06 +0000 | [diff] [blame] | 927 | boot_serial_output(); |
| 928 | return; |
| 929 | } else { |
| 930 | rc = MGMT_ERR_ENOMEM; |
| 931 | } |
| 932 | |
| 933 | out: |
| 934 | reset_cbor_state(); |
| 935 | bs_rc_rsp(rc); |
Wouter Cappelle | e3ff175 | 2021-05-03 16:36:22 +0200 | [diff] [blame] | 936 | } |
| 937 | #endif |
| 938 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 939 | /* |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 940 | * Reset, and (presumably) boot to newly uploaded image. Flush console |
| 941 | * before restarting. |
| 942 | */ |
Andrzej Puzdrowski | 268cdd0 | 2018-04-10 12:57:54 +0200 | [diff] [blame] | 943 | static void |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 944 | bs_reset(char *buf, int len) |
| 945 | { |
Dominik Ermel | b26fc48 | 2022-12-09 17:10:20 +0000 | [diff] [blame] | 946 | int rc = BOOT_HOOK_CALL(boot_reset_request_hook, 0, false); |
| 947 | if (rc == BOOT_RESET_REQUEST_HOOK_BUSY) { |
| 948 | rc = MGMT_ERR_EBUSY; |
| 949 | } else { |
| 950 | /* Currently whatever else is returned it is just converted |
| 951 | * to 0/no error. Boot serial starts accepting "force" parameter |
| 952 | * in command this needs to change. |
| 953 | */ |
| 954 | rc = 0; |
| 955 | } |
| 956 | bs_rc_rsp(rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 957 | |
Dominik Ermel | b26fc48 | 2022-12-09 17:10:20 +0000 | [diff] [blame] | 958 | if (rc == 0) { |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 959 | #ifdef __ZEPHYR__ |
Andrzej Puzdrowski | 0cf0dbd | 2021-05-14 11:55:57 +0200 | [diff] [blame] | 960 | #ifdef CONFIG_MULTITHREADING |
Dominik Ermel | b26fc48 | 2022-12-09 17:10:20 +0000 | [diff] [blame] | 961 | k_sleep(K_MSEC(250)); |
Andrzej Puzdrowski | 0cf0dbd | 2021-05-14 11:55:57 +0200 | [diff] [blame] | 962 | #else |
Dominik Ermel | b26fc48 | 2022-12-09 17:10:20 +0000 | [diff] [blame] | 963 | k_busy_wait(250000); |
Andrzej Puzdrowski | 0cf0dbd | 2021-05-14 11:55:57 +0200 | [diff] [blame] | 964 | #endif |
Dominik Ermel | b26fc48 | 2022-12-09 17:10:20 +0000 | [diff] [blame] | 965 | sys_reboot(SYS_REBOOT_COLD); |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 966 | #elif __ESPRESSIF__ |
Dominik Ermel | b26fc48 | 2022-12-09 17:10:20 +0000 | [diff] [blame] | 967 | esp_rom_delay_us(250000); |
| 968 | bootloader_reset(); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 969 | #else |
Dominik Ermel | b26fc48 | 2022-12-09 17:10:20 +0000 | [diff] [blame] | 970 | os_cputime_delay_usecs(250000); |
| 971 | hal_system_reset(); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 972 | #endif |
Dominik Ermel | b26fc48 | 2022-12-09 17:10:20 +0000 | [diff] [blame] | 973 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /* |
| 977 | * Parse incoming line of input from console. |
| 978 | * Expect newtmgr protocol with serial transport. |
| 979 | */ |
| 980 | void |
| 981 | boot_serial_input(char *buf, int len) |
| 982 | { |
| 983 | struct nmgr_hdr *hdr; |
| 984 | |
| 985 | hdr = (struct nmgr_hdr *)buf; |
| 986 | if (len < sizeof(*hdr) || |
| 987 | (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) || |
| 988 | (ntohs(hdr->nh_len) < len - sizeof(*hdr))) { |
| 989 | return; |
| 990 | } |
| 991 | bs_hdr = hdr; |
| 992 | hdr->nh_group = ntohs(hdr->nh_group); |
| 993 | |
| 994 | buf += sizeof(*hdr); |
| 995 | len -= sizeof(*hdr); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 996 | |
Dominik Ermel | 4c0f6c1 | 2022-03-04 15:47:37 +0000 | [diff] [blame] | 997 | reset_cbor_state(); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 998 | |
| 999 | /* |
| 1000 | * Limited support for commands. |
| 1001 | */ |
| 1002 | if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) { |
| 1003 | switch (hdr->nh_id) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 1004 | case IMGMGR_NMGR_ID_STATE: |
Jamie McCrae | fac2cab | 2023-03-30 10:07:36 +0100 | [diff] [blame] | 1005 | bs_list_set(hdr->nh_op, buf, len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1006 | break; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 1007 | case IMGMGR_NMGR_ID_UPLOAD: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1008 | bs_upload(buf, len); |
| 1009 | break; |
| 1010 | default: |
Dominik Ermel | c9dc224 | 2021-07-28 17:08:23 +0000 | [diff] [blame] | 1011 | bs_rc_rsp(MGMT_ERR_ENOTSUP); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1012 | break; |
| 1013 | } |
| 1014 | } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) { |
| 1015 | switch (hdr->nh_id) { |
Wouter Cappelle | e3ff175 | 2021-05-03 16:36:22 +0200 | [diff] [blame] | 1016 | case NMGR_ID_ECHO: |
| 1017 | #ifdef MCUBOOT_BOOT_MGMT_ECHO |
| 1018 | bs_echo(buf, len); |
| 1019 | #endif |
| 1020 | break; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1021 | case NMGR_ID_CONS_ECHO_CTRL: |
Dominik Ermel | c9dc224 | 2021-07-28 17:08:23 +0000 | [diff] [blame] | 1022 | bs_rc_rsp(0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1023 | break; |
| 1024 | case NMGR_ID_RESET: |
| 1025 | bs_reset(buf, len); |
| 1026 | break; |
| 1027 | default: |
Dominik Ermel | c9dc224 | 2021-07-28 17:08:23 +0000 | [diff] [blame] | 1028 | bs_rc_rsp(MGMT_ERR_ENOTSUP); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1029 | break; |
| 1030 | } |
Dominik Ermel | bd69c3d | 2021-07-28 11:27:31 +0000 | [diff] [blame] | 1031 | } else if (MCUBOOT_PERUSER_MGMT_GROUP_ENABLED == 1) { |
Øyvind Rønningstad | a7d34ca | 2022-02-28 13:47:57 +0100 | [diff] [blame] | 1032 | if (bs_peruser_system_specific(hdr, buf, len, cbor_state) == 0) { |
Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 1033 | boot_serial_output(); |
| 1034 | } |
Dominik Ermel | c9dc224 | 2021-07-28 17:08:23 +0000 | [diff] [blame] | 1035 | } else { |
| 1036 | bs_rc_rsp(MGMT_ERR_ENOTSUP); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1037 | } |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 1038 | #ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU |
| 1039 | bs_entry = true; |
| 1040 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | static void |
| 1044 | boot_serial_output(void) |
| 1045 | { |
| 1046 | char *data; |
Piotr Dymacz | f5e7753 | 2022-10-30 17:43:45 +0100 | [diff] [blame] | 1047 | int len, out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1048 | uint16_t crc; |
| 1049 | uint16_t totlen; |
Piotr Dymacz | f5e7753 | 2022-10-30 17:43:45 +0100 | [diff] [blame] | 1050 | char pkt_cont[2] = { SHELL_NLIP_DATA_START1, SHELL_NLIP_DATA_START2 }; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1051 | char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 }; |
Dominik Ermel | 5ff8958 | 2022-03-03 17:09:07 +0000 | [diff] [blame] | 1052 | char buf[BOOT_SERIAL_OUT_MAX + sizeof(*bs_hdr) + sizeof(crc) + sizeof(totlen)]; |
| 1053 | char encoded_buf[BASE64_ENCODE_SIZE(sizeof(buf))]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1054 | |
| 1055 | data = bs_obuf; |
Michal Gorecki | 5404130 | 2023-07-14 13:03:02 +0200 | [diff] [blame] | 1056 | len = (uintptr_t)cbor_state->payload_mut - (uintptr_t)bs_obuf; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1057 | |
| 1058 | bs_hdr->nh_op++; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 1059 | bs_hdr->nh_flags = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1060 | bs_hdr->nh_len = htons(len); |
| 1061 | bs_hdr->nh_group = htons(bs_hdr->nh_group); |
| 1062 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1063 | #ifdef __ZEPHYR__ |
Carles Cufi | b9192a4 | 2022-02-10 11:41:57 +0100 | [diff] [blame] | 1064 | crc = crc16_itu_t(CRC16_INITIAL_CRC, (uint8_t *)bs_hdr, sizeof(*bs_hdr)); |
| 1065 | crc = crc16_itu_t(crc, data, len); |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 1066 | #elif __ESPRESSIF__ |
| 1067 | /* For ESP32 it was used the CRC API in rom/crc.h */ |
Almir Okato | 7d3622f | 2022-10-20 12:44:58 -0300 | [diff] [blame] | 1068 | crc = ~esp_crc16_be(~CRC16_INITIAL_CRC, (uint8_t *)bs_hdr, sizeof(*bs_hdr)); |
| 1069 | crc = ~esp_crc16_be(~crc, (uint8_t *)data, len); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1070 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1071 | crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr)); |
| 1072 | crc = crc16_ccitt(crc, data, len); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1073 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1074 | crc = htons(crc); |
| 1075 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1076 | totlen = len + sizeof(*bs_hdr) + sizeof(crc); |
| 1077 | totlen = htons(totlen); |
| 1078 | |
| 1079 | memcpy(buf, &totlen, sizeof(totlen)); |
| 1080 | totlen = sizeof(totlen); |
| 1081 | memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr)); |
| 1082 | totlen += sizeof(*bs_hdr); |
| 1083 | memcpy(&buf[totlen], data, len); |
| 1084 | totlen += len; |
| 1085 | memcpy(&buf[totlen], &crc, sizeof(crc)); |
| 1086 | totlen += sizeof(crc); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1087 | #ifdef __ZEPHYR__ |
| 1088 | size_t enc_len; |
Carles Cufi | 0165be8 | 2018-03-26 17:43:51 +0200 | [diff] [blame] | 1089 | base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1090 | totlen = enc_len; |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 1091 | #elif __ESPRESSIF__ |
| 1092 | size_t enc_len; |
| 1093 | base64_encode((unsigned char *)encoded_buf, sizeof(encoded_buf), &enc_len, (unsigned char *)buf, totlen); |
| 1094 | totlen = enc_len; |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1095 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1096 | totlen = base64_encode(buf, totlen, encoded_buf, 1); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1097 | #endif |
Piotr Dymacz | f5e7753 | 2022-10-30 17:43:45 +0100 | [diff] [blame] | 1098 | |
| 1099 | out = 0; |
| 1100 | while (out < totlen) { |
| 1101 | if (out == 0) { |
| 1102 | boot_uf->write(pkt_start, sizeof(pkt_start)); |
| 1103 | } else { |
| 1104 | boot_uf->write(pkt_cont, sizeof(pkt_cont)); |
| 1105 | } |
| 1106 | |
| 1107 | len = MIN(BOOT_SERIAL_FRAME_MTU, totlen - out); |
| 1108 | boot_uf->write(&encoded_buf[out], len); |
| 1109 | |
| 1110 | out += len; |
| 1111 | |
| 1112 | boot_uf->write("\n", 1); |
| 1113 | } |
| 1114 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1115 | BOOT_LOG_INF("TX"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | /* |
| 1119 | * Returns 1 if full packet has been received. |
| 1120 | */ |
| 1121 | static int |
| 1122 | boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout) |
| 1123 | { |
| 1124 | int rc; |
| 1125 | uint16_t crc; |
| 1126 | uint16_t len; |
Marko Kiiskila | e5aeee4 | 2018-12-21 15:00:16 +0200 | [diff] [blame] | 1127 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1128 | #ifdef __ZEPHYR__ |
| 1129 | int err; |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 1130 | err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1131 | if (err) { |
| 1132 | return -1; |
| 1133 | } |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 1134 | #elif __ESPRESSIF__ |
| 1135 | int err; |
| 1136 | err = base64_decode((unsigned char *)&out[*out_off], maxout - *out_off, (size_t *)&rc, (unsigned char *)in, inlen); |
| 1137 | if (err) { |
| 1138 | return -1; |
| 1139 | } |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1140 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1141 | if (*out_off + base64_decode_len(in) >= maxout) { |
| 1142 | return -1; |
| 1143 | } |
| 1144 | rc = base64_decode(in, &out[*out_off]); |
| 1145 | if (rc < 0) { |
| 1146 | return -1; |
| 1147 | } |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 1148 | #endif |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 1149 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1150 | *out_off += rc; |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 1151 | if (*out_off <= sizeof(uint16_t)) { |
| 1152 | return 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1153 | } |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 1154 | |
| 1155 | len = ntohs(*(uint16_t *)out); |
| 1156 | if (len != *out_off - sizeof(uint16_t)) { |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | if (len > *out_off - sizeof(uint16_t)) { |
| 1161 | len = *out_off - sizeof(uint16_t); |
| 1162 | } |
| 1163 | |
| 1164 | out += sizeof(uint16_t); |
| 1165 | #ifdef __ZEPHYR__ |
Carles Cufi | b9192a4 | 2022-02-10 11:41:57 +0100 | [diff] [blame] | 1166 | crc = crc16_itu_t(CRC16_INITIAL_CRC, out, len); |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 1167 | #elif __ESPRESSIF__ |
Almir Okato | 7d3622f | 2022-10-20 12:44:58 -0300 | [diff] [blame] | 1168 | crc = ~esp_crc16_be(~CRC16_INITIAL_CRC, (uint8_t *)out, len); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 1169 | #else |
| 1170 | crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len); |
| 1171 | #endif |
| 1172 | if (crc || len <= sizeof(crc)) { |
| 1173 | return 0; |
| 1174 | } |
| 1175 | *out_off -= sizeof(crc); |
| 1176 | out[*out_off] = '\0'; |
| 1177 | |
| 1178 | return 1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | /* |
| 1182 | * Task which waits reading console, expecting to get image over |
| 1183 | * serial port. |
| 1184 | */ |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 1185 | static void |
| 1186 | boot_serial_read_console(const struct boot_uart_funcs *f,int timeout_in_ms) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1187 | { |
| 1188 | int rc; |
| 1189 | int off; |
David Brown | 57f0df3 | 2020-05-12 08:39:21 -0600 | [diff] [blame] | 1190 | int dec_off = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1191 | int full_line; |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 1192 | int max_input; |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 1193 | int elapsed_in_ms = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1194 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 1195 | boot_uf = f; |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 1196 | max_input = sizeof(in_buf); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1197 | |
| 1198 | off = 0; |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 1199 | while (timeout_in_ms > 0 || bs_entry) { |
Piotr Dymacz | 067f30a | 2022-08-12 18:25:34 +0200 | [diff] [blame] | 1200 | /* |
| 1201 | * Don't enter CPU idle state here if timeout based serial recovery is |
| 1202 | * used as otherwise the boot process hangs forever, waiting for input |
| 1203 | * from serial console (if single-thread mode is used). |
| 1204 | */ |
Piotr Dymacz | 3942e9b | 2022-07-18 10:19:25 +0200 | [diff] [blame] | 1205 | #ifndef MCUBOOT_SERIAL_WAIT_FOR_DFU |
Andrzej Puzdrowski | aea38eb | 2021-06-11 12:28:59 +0200 | [diff] [blame] | 1206 | MCUBOOT_CPU_IDLE(); |
Piotr Dymacz | 3942e9b | 2022-07-18 10:19:25 +0200 | [diff] [blame] | 1207 | #endif |
Hein Wessels | 56d28f0 | 2021-11-19 08:42:08 +0100 | [diff] [blame] | 1208 | MCUBOOT_WATCHDOG_FEED(); |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 1209 | #ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU |
| 1210 | uint32_t start = k_uptime_get_32(); |
| 1211 | #endif |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 1212 | rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1213 | if (rc <= 0 && !full_line) { |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 1214 | goto check_timeout; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1215 | } |
| 1216 | off += rc; |
| 1217 | if (!full_line) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 1218 | if (off == max_input) { |
| 1219 | /* |
| 1220 | * Full line, no newline yet. Reset the input buffer. |
| 1221 | */ |
| 1222 | off = 0; |
| 1223 | } |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 1224 | goto check_timeout; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1225 | } |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 1226 | if (in_buf[0] == SHELL_NLIP_PKT_START1 && |
| 1227 | in_buf[1] == SHELL_NLIP_PKT_START2) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1228 | dec_off = 0; |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 1229 | rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input); |
| 1230 | } else if (in_buf[0] == SHELL_NLIP_DATA_START1 && |
| 1231 | in_buf[1] == SHELL_NLIP_DATA_START2) { |
| 1232 | rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1233 | } |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 1234 | |
| 1235 | /* serve errors: out of decode memory, or bad encoding */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1236 | if (rc == 1) { |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 1237 | boot_serial_input(&dec_buf[2], dec_off - 2); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1238 | } |
| 1239 | off = 0; |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 1240 | check_timeout: |
| 1241 | /* Subtract elapsed time */ |
| 1242 | #ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU |
| 1243 | elapsed_in_ms = (k_uptime_get_32() - start); |
| 1244 | #endif |
| 1245 | timeout_in_ms -= elapsed_in_ms; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1246 | } |
| 1247 | } |
Wouter Cappelle | e3822f8 | 2022-01-19 15:39:43 +0100 | [diff] [blame] | 1248 | |
| 1249 | /* |
| 1250 | * Task which waits reading console, expecting to get image over |
| 1251 | * serial port. |
| 1252 | */ |
| 1253 | void |
| 1254 | boot_serial_start(const struct boot_uart_funcs *f) |
| 1255 | { |
| 1256 | bs_entry = true; |
| 1257 | boot_serial_read_console(f,0); |
| 1258 | } |
| 1259 | |
| 1260 | #ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU |
| 1261 | /* |
| 1262 | * Task which waits reading console for a certain amount of timeout. |
| 1263 | * If within this timeout no mcumgr command is received, the function is |
| 1264 | * returning, else the serial boot is never exited |
| 1265 | */ |
| 1266 | void |
| 1267 | boot_serial_check_start(const struct boot_uart_funcs *f, int timeout_in_ms) |
| 1268 | { |
| 1269 | bs_entry = false; |
| 1270 | boot_serial_read_console(f,timeout_in_ms); |
| 1271 | } |
| 1272 | #endif |
Jamie McCrae | 827118f | 2023-03-10 13:24:57 +0000 | [diff] [blame] | 1273 | |
| 1274 | #ifdef MCUBOOT_SERIAL_IMG_GRP_HASH |
| 1275 | /* Function to find the hash of an image, returns 0 on success. */ |
| 1276 | static int boot_serial_get_hash(const struct image_header *hdr, |
| 1277 | const struct flash_area *fap, uint8_t *hash) |
| 1278 | { |
| 1279 | struct image_tlv_iter it; |
| 1280 | uint32_t offset; |
| 1281 | uint16_t len; |
| 1282 | uint16_t type; |
| 1283 | int rc; |
| 1284 | |
| 1285 | /* Manifest data is concatenated to the end of the image. |
| 1286 | * It is encoded in TLV format. |
| 1287 | */ |
| 1288 | rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false); |
| 1289 | if (rc) { |
| 1290 | return -1; |
| 1291 | } |
| 1292 | |
| 1293 | /* Traverse through the TLV area to find the image hash TLV. */ |
| 1294 | while (true) { |
| 1295 | rc = bootutil_tlv_iter_next(&it, &offset, &len, &type); |
| 1296 | if (rc < 0) { |
| 1297 | return -1; |
| 1298 | } else if (rc > 0) { |
| 1299 | break; |
| 1300 | } |
| 1301 | |
| 1302 | if (type == IMAGE_TLV_SHA256) { |
| 1303 | /* Get the image's hash value from the manifest section. */ |
| 1304 | if (len != 32) { |
| 1305 | return -1; |
| 1306 | } |
| 1307 | |
| 1308 | rc = flash_area_read(fap, offset, hash, len); |
| 1309 | if (rc) { |
| 1310 | return -1; |
| 1311 | } |
| 1312 | |
| 1313 | return 0; |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | return -1; |
| 1318 | } |
| 1319 | #endif |