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