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