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