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> |
| 24 | |
| 25 | #include "sysflash/sysflash.h" |
| 26 | |
Fabio Utzig | 1a2e41a | 2017-11-17 12:13:09 -0200 | [diff] [blame] | 27 | #include "bootutil/bootutil_log.h" |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 28 | #include "cbor_encode.h" |
Fabio Utzig | 1a2e41a | 2017-11-17 12:13:09 -0200 | [diff] [blame] | 29 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 30 | #ifdef __ZEPHYR__ |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 31 | #include <sys/reboot.h> |
Andrzej Puzdrowski | f1d189c | 2019-12-12 09:34:11 +0100 | [diff] [blame] | 32 | #include <sys/byteorder.h> |
| 33 | #include <sys/__assert.h> |
Peter Bigot | 54c1e3f | 2020-01-25 05:50:12 -0600 | [diff] [blame] | 34 | #include <drivers/flash.h> |
Andrzej Puzdrowski | f1d189c | 2019-12-12 09:34:11 +0100 | [diff] [blame] | 35 | #include <sys/crc.h> |
| 36 | #include <sys/base64.h> |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 37 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 38 | #include <bsp/bsp.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 39 | #include <hal/hal_system.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 40 | #include <os/endian.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 41 | #include <os/os_cputime.h> |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 42 | #include <crc/crc16.h> |
| 43 | #include <base64/base64.h> |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 44 | #endif /* __ZEPHYR__ */ |
| 45 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 46 | #include <flash_map_backend/flash_map_backend.h> |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 47 | #include <hal/hal_flash.h> |
| 48 | #include <os/os.h> |
| 49 | #include <os/os_malloc.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 50 | |
| 51 | #include <bootutil/image.h> |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 52 | #include <bootutil/bootutil.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 53 | |
| 54 | #include "boot_serial/boot_serial.h" |
| 55 | #include "boot_serial_priv.h" |
| 56 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 57 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 58 | #include "bootutil_priv.h" |
| 59 | #endif |
| 60 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 61 | #ifdef MCUBOOT_ENC_IMAGES |
| 62 | #include "single_loader.h" |
| 63 | #endif |
| 64 | |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 65 | #include "serial_recovery_cbor.h" |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 66 | #include "bootutil/boot_hooks.h" |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 67 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 68 | BOOT_LOG_MODULE_DECLARE(mcuboot); |
| 69 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 70 | #define BOOT_SERIAL_INPUT_MAX 512 |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 71 | #define BOOT_SERIAL_OUT_MAX (128 * BOOT_IMAGE_NUMBER) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 72 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 73 | #ifdef __ZEPHYR__ |
Carles Cufi | 0165be8 | 2018-03-26 17:43:51 +0200 | [diff] [blame] | 74 | /* base64 lib encodes data to null-terminated string */ |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 75 | #define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1) |
| 76 | |
| 77 | #define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */ |
| 78 | #define CRC_CITT_POLYMINAL 0x1021 |
| 79 | |
| 80 | #define ntohs(x) sys_be16_to_cpu(x) |
| 81 | #define htons(x) sys_cpu_to_be16(x) |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 82 | #endif |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 83 | |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 84 | #if (BOOT_IMAGE_NUMBER > 1) |
| 85 | #define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x)) |
| 86 | #else |
| 87 | #define IMAGES_ITER(x) |
| 88 | #endif |
| 89 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 90 | static char in_buf[BOOT_SERIAL_INPUT_MAX + 1]; |
| 91 | static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1]; |
Marko Kiiskila | 8b1ce3a | 2018-06-14 13:20:46 -0700 | [diff] [blame] | 92 | const struct boot_uart_funcs *boot_uf; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 93 | static uint32_t curr_off; |
| 94 | static uint32_t img_size; |
| 95 | static struct nmgr_hdr *bs_hdr; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 96 | static bool bs_entry; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 97 | |
| 98 | static char bs_obuf[BOOT_SERIAL_OUT_MAX]; |
| 99 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 100 | static void boot_serial_output(void); |
| 101 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 102 | static cbor_state_backups_t dummy_backups; |
| 103 | static cbor_state_t cbor_state = { |
| 104 | .backups = &dummy_backups |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 105 | }; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 106 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 107 | /** |
| 108 | * Function that processes MGMT_GROUP_ID_PERUSER mcumgr group and may be |
| 109 | * used to process any groups that have not been processed by generic boot |
| 110 | * serial implementation. |
| 111 | * |
| 112 | * @param[in] hdr -- the decoded header of mcumgr message; |
| 113 | * @param[in] buffer -- buffer with first mcumgr message; |
| 114 | * @param[in] len -- length of of data in buffer; |
| 115 | * @param[out] *cs -- object with encoded response. |
| 116 | * |
| 117 | * @return 0 on success; non-0 error code otherwise. |
| 118 | */ |
| 119 | extern int bs_peruser_system_specific(const struct nmgr_hdr *hdr, |
| 120 | const char *buffer, |
| 121 | int len, cbor_state_t *cs); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 122 | |
| 123 | /* |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 124 | * Convert version into string without use of snprintf(). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 125 | */ |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 126 | static int |
| 127 | u32toa(char *tgt, uint32_t val) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 128 | { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 129 | char *dst; |
| 130 | uint32_t d = 1; |
| 131 | uint32_t dgt; |
| 132 | int n = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 133 | |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 134 | dst = tgt; |
| 135 | while (val / d >= 10) { |
| 136 | d *= 10; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 137 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 138 | while (d) { |
| 139 | dgt = val / d; |
| 140 | val %= d; |
| 141 | d /= 10; |
| 142 | if (n || dgt > 0 || d == 0) { |
| 143 | *dst++ = dgt + '0'; |
| 144 | ++n; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 145 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 146 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 147 | *dst = '\0'; |
| 148 | |
| 149 | return dst - tgt; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * dst has to be able to fit "255.255.65535.4294967295" (25 characters). |
| 154 | */ |
| 155 | static void |
| 156 | bs_list_img_ver(char *dst, int maxlen, struct image_version *ver) |
| 157 | { |
| 158 | int off; |
| 159 | |
| 160 | off = u32toa(dst, ver->iv_major); |
| 161 | dst[off++] = '.'; |
| 162 | off += u32toa(dst + off, ver->iv_minor); |
| 163 | dst[off++] = '.'; |
| 164 | off += u32toa(dst + off, ver->iv_revision); |
| 165 | dst[off++] = '.'; |
| 166 | off += u32toa(dst + off, ver->iv_build_num); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* |
| 170 | * List images. |
| 171 | */ |
| 172 | static void |
| 173 | bs_list(char *buf, int len) |
| 174 | { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 175 | struct image_header hdr; |
| 176 | uint8_t tmpbuf[64]; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 177 | uint32_t slot, area_id; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 178 | const struct flash_area *fap; |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 179 | uint8_t image_index; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 180 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 181 | map_start_encode(&cbor_state, 1); |
| 182 | tstrx_put(&cbor_state, "images"); |
| 183 | list_start_encode(&cbor_state, 5); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 184 | image_index = 0; |
| 185 | IMAGES_ITER(image_index) { |
| 186 | for (slot = 0; slot < 2; slot++) { |
| 187 | area_id = flash_area_id_from_multi_image_slot(image_index, slot); |
| 188 | if (flash_area_open(area_id, &fap)) { |
| 189 | continue; |
| 190 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 191 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 192 | int rc = BOOT_HOOK_CALL(boot_read_image_header_hook, |
| 193 | BOOT_HOOK_REGULAR, image_index, slot, &hdr); |
| 194 | if (rc == BOOT_HOOK_REGULAR) |
| 195 | { |
| 196 | flash_area_read(fap, 0, &hdr, sizeof(hdr)); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 197 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 198 | |
| 199 | fih_int fih_rc = FIH_FAILURE; |
| 200 | |
| 201 | if (hdr.ih_magic == IMAGE_MAGIC) |
| 202 | { |
| 203 | BOOT_HOOK_CALL_FIH(boot_image_check_hook, |
| 204 | fih_int_encode(BOOT_HOOK_REGULAR), |
| 205 | fih_rc, image_index, slot); |
| 206 | if (fih_eq(fih_rc, BOOT_HOOK_REGULAR)) |
| 207 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 208 | #ifdef MCUBOOT_ENC_IMAGES |
| 209 | if (slot == 0 && IS_ENCRYPTED(&hdr)) { |
| 210 | /* Clear the encrypted flag we didn't supply a key |
| 211 | * This flag could be set if there was a decryption in place |
| 212 | * performed before. We will try to validate the image without |
| 213 | * decryption by clearing the flag in the heder. If |
| 214 | * still encrypted the validation will fail. |
| 215 | */ |
| 216 | hdr.ih_flags &= ~(ENCRYPTIONFLAGS); |
| 217 | } |
| 218 | #endif |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 219 | FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf), |
| 220 | NULL, 0, NULL); |
| 221 | } |
| 222 | } |
| 223 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 224 | flash_area_close(fap); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 225 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 226 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
| 227 | continue; |
| 228 | } |
| 229 | |
| 230 | map_start_encode(&cbor_state, 20); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 231 | |
| 232 | #if (BOOT_IMAGE_NUMBER > 1) |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 233 | tstrx_put(&cbor_state, "image"); |
| 234 | uintx32_put(&cbor_state, image_index); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 235 | #endif |
| 236 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 237 | tstrx_put(&cbor_state, "slot"); |
| 238 | uintx32_put(&cbor_state, slot); |
| 239 | tstrx_put(&cbor_state, "version"); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 240 | |
| 241 | bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 242 | tstrx_put_term(&cbor_state, (char *)tmpbuf); |
| 243 | map_end_encode(&cbor_state, 20); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 244 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 245 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 246 | list_end_encode(&cbor_state, 5); |
| 247 | map_end_encode(&cbor_state, 1); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 248 | boot_serial_output(); |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Image upload request. |
| 253 | */ |
| 254 | static void |
| 255 | bs_upload(char *buf, int len) |
| 256 | { |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 257 | const uint8_t *img_data = NULL; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 258 | long long int off = UINT64_MAX; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 259 | size_t img_blen = 0; |
Fabio Utzig | 30f6b2a | 2018-03-29 16:18:53 -0300 | [diff] [blame] | 260 | uint8_t rem_bytes; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 261 | long long int data_len = UINT64_MAX; |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 262 | int img_num; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 263 | size_t slen; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 264 | const struct flash_area *fap = NULL; |
| 265 | int rc; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 266 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 267 | static off_t off_last = -1; |
| 268 | struct flash_sector sector; |
| 269 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 270 | |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 271 | img_num = 0; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 272 | |
| 273 | /* |
| 274 | * Expected data format. |
| 275 | * { |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 276 | * "image":<image number in a multi-image set (OPTIONAL)> |
| 277 | * "data":<image data> |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 278 | * "len":<image len> |
| 279 | * "off":<current offset of image data> |
| 280 | * } |
| 281 | */ |
| 282 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 283 | struct Upload upload; |
| 284 | uint32_t decoded_len; |
| 285 | bool result = cbor_decode_Upload((const uint8_t *)buf, len, &upload, &decoded_len); |
| 286 | |
| 287 | if (!result || (len != decoded_len)) { |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 288 | goto out_invalid_data; |
| 289 | } |
Dominik Ermel | 470e2f3 | 2020-01-10 13:28:48 +0000 | [diff] [blame] | 290 | |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 291 | for (int i = 0; i < upload._Upload_members_count; i++) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 292 | struct Member_ *member = &upload._Upload_members[i]; |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 293 | switch(member->_Member_choice) { |
| 294 | case _Member_image: |
| 295 | img_num = member->_Member_image; |
| 296 | break; |
| 297 | case _Member_data: |
| 298 | img_data = member->_Member_data.value; |
| 299 | slen = member->_Member_data.len; |
| 300 | img_blen = slen; |
| 301 | break; |
| 302 | case _Member_len: |
| 303 | data_len = member->_Member_len; |
| 304 | break; |
| 305 | case _Member_off: |
| 306 | off = member->_Member_off; |
| 307 | break; |
| 308 | case _Member_sha: |
| 309 | default: |
| 310 | /* Nothing to do. */ |
| 311 | break; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 312 | } |
| 313 | } |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 314 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 315 | if (off == UINT64_MAX || img_data == NULL) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 316 | /* |
| 317 | * Offset must be set in every block. |
| 318 | */ |
| 319 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 322 | #if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD) |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 323 | rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 324 | #else |
| 325 | rc = flash_area_open(flash_area_id_from_direct_image(img_num), &fap); |
| 326 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 327 | if (rc) { |
| 328 | rc = MGMT_ERR_EINVAL; |
| 329 | goto out; |
| 330 | } |
| 331 | |
| 332 | if (off == 0) { |
| 333 | curr_off = 0; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 334 | if (data_len > flash_area_get_size(fap)) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 335 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 336 | } |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 337 | #if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE) |
| 338 | /* We are using swap state at end of flash area to store validation |
| 339 | * result. Make sure the user cannot write it from an image to skip validation. |
| 340 | */ |
| 341 | if (data_len > (flash_area_get_size(fap) - BOOT_MAGIC_SZ)) { |
| 342 | goto out_invalid_data; |
| 343 | } |
| 344 | #endif |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 345 | #ifndef MCUBOOT_ERASE_PROGRESSIVELY |
| 346 | rc = flash_area_erase(fap, 0, flash_area_get_size(fap)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 347 | if (rc) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 348 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 349 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 350 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 351 | img_size = data_len; |
| 352 | } |
| 353 | if (off != curr_off) { |
| 354 | rc = 0; |
| 355 | goto out; |
| 356 | } |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 357 | |
| 358 | if (curr_off + img_blen > img_size) { |
| 359 | rc = MGMT_ERR_EINVAL; |
| 360 | goto out; |
| 361 | } |
| 362 | |
| 363 | rem_bytes = img_blen % flash_area_align(fap); |
| 364 | |
| 365 | if ((curr_off + img_blen < img_size) && rem_bytes) { |
| 366 | img_blen -= rem_bytes; |
| 367 | rem_bytes = 0; |
Fabio Utzig | 30f6b2a | 2018-03-29 16:18:53 -0300 | [diff] [blame] | 368 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 369 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 370 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 371 | rc = flash_area_sector_from_off(curr_off + img_blen, §or); |
| 372 | if (rc) { |
| 373 | BOOT_LOG_ERR("Unable to determine flash sector size"); |
| 374 | goto out; |
| 375 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 376 | if (off_last != flash_sector_get_off(§or)) { |
| 377 | off_last = flash_sector_get_off(§or); |
| 378 | BOOT_LOG_INF("Erasing sector at offset 0x%x", flash_sector_get_off(§or)); |
| 379 | rc = flash_area_erase(fap, flash_sector_get_off(§or), |
| 380 | flash_sector_get_size(§or)); |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 381 | if (rc) { |
| 382 | BOOT_LOG_ERR("Error %d while erasing sector", rc); |
| 383 | goto out; |
| 384 | } |
| 385 | } |
| 386 | #endif |
| 387 | |
| 388 | BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_blen); |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 389 | if (rem_bytes) { |
| 390 | /* the last chunk of the image might be unaligned */ |
| 391 | uint8_t wbs_aligned[BOOT_MAX_ALIGN]; |
| 392 | size_t w_size = img_blen - rem_bytes; |
| 393 | |
| 394 | if (w_size) { |
| 395 | rc = flash_area_write(fap, curr_off, img_data, w_size); |
| 396 | if (rc) { |
| 397 | goto out_invalid_data; |
| 398 | } |
| 399 | curr_off += w_size; |
| 400 | img_blen -= w_size; |
| 401 | img_data += w_size; |
| 402 | } |
| 403 | |
| 404 | if (img_blen) { |
| 405 | memcpy(wbs_aligned, img_data, rem_bytes); |
| 406 | memset(wbs_aligned + rem_bytes, flash_area_erased_val(fap), |
| 407 | sizeof(wbs_aligned) - rem_bytes); |
| 408 | rc = flash_area_write(fap, curr_off, wbs_aligned, flash_area_align(fap)); |
| 409 | } |
| 410 | |
| 411 | } else { |
| 412 | rc = flash_area_write(fap, curr_off, img_data, img_blen); |
| 413 | } |
| 414 | |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 415 | if (rc == 0) { |
| 416 | curr_off += img_blen; |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 417 | if (curr_off == img_size) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 418 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 419 | /* get the last sector offset */ |
| 420 | rc = flash_area_sector_from_off(boot_status_off(fap), §or); |
| 421 | if (rc) { |
| 422 | BOOT_LOG_ERR("Unable to determine flash sector of" |
| 423 | "the image trailer"); |
| 424 | goto out; |
| 425 | } |
| 426 | /* Assure that sector for image trailer was erased. */ |
| 427 | /* Check whether it was erased during previous upload. */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 428 | if (off_last < flash_sector_get_off(§or)) { |
| 429 | BOOT_LOG_INF("Erasing sector at offset 0x%x", |
| 430 | flash_sector_get_off(§or)); |
| 431 | rc = flash_area_erase(fap, flash_sector_get_off(§or), |
| 432 | flash_sector_get_size(§or)); |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 433 | if (rc) { |
| 434 | BOOT_LOG_ERR("Error %d while erasing sector", rc); |
| 435 | goto out; |
| 436 | } |
| 437 | } |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 438 | #endif |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 439 | rc = BOOT_HOOK_CALL(boot_serial_uploaded_hook, 0, img_num, fap, |
| 440 | img_size); |
| 441 | if (rc) { |
| 442 | BOOT_LOG_ERR("Error %d post upload hook", rc); |
| 443 | goto out; |
| 444 | } |
| 445 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 446 | } else { |
| 447 | out_invalid_data: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 448 | rc = MGMT_ERR_EINVAL; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 449 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 450 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 451 | out: |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 452 | BOOT_LOG_INF("RX: 0x%x", rc); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 453 | map_start_encode(&cbor_state, 10); |
| 454 | tstrx_put(&cbor_state, "rc"); |
| 455 | uintx32_put(&cbor_state, rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 456 | if (rc == 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 457 | tstrx_put(&cbor_state, "off"); |
| 458 | uintx32_put(&cbor_state, curr_off); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 459 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 460 | map_end_encode(&cbor_state, 10); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 461 | |
| 462 | boot_serial_output(); |
| 463 | flash_area_close(fap); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 464 | |
| 465 | #ifdef MCUBOOT_ENC_IMAGES |
| 466 | if (curr_off == img_size) { |
| 467 | /* Last sector received, now start a decryption on the image if it is encrypted*/ |
| 468 | rc = boot_handle_enc_fw(); |
| 469 | } |
| 470 | #endif //#ifdef MCUBOOT_ENC_IMAGES |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 473 | #ifdef MCUBOOT_BOOT_MGMT_ECHO |
| 474 | static bool |
| 475 | decode_echo(cbor_state_t *state, cbor_string_type_t *result) |
| 476 | { |
| 477 | size_t bsstrdecoded; |
| 478 | int ret; |
| 479 | |
| 480 | if (!map_start_decode(state)) { |
| 481 | return false; |
| 482 | } |
| 483 | ret = multi_decode(2, 2, &bsstrdecoded, (void *)tstrx_decode, state, result, sizeof(cbor_string_type_t)); |
| 484 | map_end_decode(state); |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | static void |
| 490 | bs_echo(char *buf, int len) |
| 491 | { |
| 492 | size_t bsstrdecoded; |
| 493 | cbor_string_type_t str[2]; |
| 494 | |
| 495 | if (entry_function((const uint8_t *)buf, len, str, &bsstrdecoded, (void *)decode_echo, 1, 2)) { |
| 496 | map_start_encode(&cbor_state, 10); |
| 497 | tstrx_put(&cbor_state, "r"); |
| 498 | tstrx_encode(&cbor_state, &str[1]); |
| 499 | map_end_encode(&cbor_state, 10); |
| 500 | boot_serial_output(); |
| 501 | } |
| 502 | } |
| 503 | #endif |
| 504 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 505 | /* |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 506 | * Send rc code only. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 507 | */ |
| 508 | static void |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 509 | bs_rc_rsp(int rc_code) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 510 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 511 | map_start_encode(&cbor_state, 10); |
| 512 | tstrx_put(&cbor_state, "rc"); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 513 | uintx32_put(&cbor_state, rc_code); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 514 | map_end_encode(&cbor_state, 10); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 515 | boot_serial_output(); |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Reset, and (presumably) boot to newly uploaded image. Flush console |
| 520 | * before restarting. |
| 521 | */ |
Andrzej Puzdrowski | 268cdd0 | 2018-04-10 12:57:54 +0200 | [diff] [blame] | 522 | static void |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 523 | bs_reset(char *buf, int len) |
| 524 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 525 | bs_rc_rsp(0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 526 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 527 | #ifdef __ZEPHYR__ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 528 | #ifdef CONFIG_MULTITHREADING |
Carles Cufi | 7e7b4ad | 2020-03-30 19:12:02 +0200 | [diff] [blame] | 529 | k_sleep(K_MSEC(250)); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 530 | #else |
| 531 | k_busy_wait(250000); |
| 532 | #endif |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 533 | sys_reboot(SYS_REBOOT_COLD); |
| 534 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 535 | os_cputime_delay_usecs(250000); |
| 536 | hal_system_reset(); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 537 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /* |
| 541 | * Parse incoming line of input from console. |
| 542 | * Expect newtmgr protocol with serial transport. |
| 543 | */ |
| 544 | void |
| 545 | boot_serial_input(char *buf, int len) |
| 546 | { |
| 547 | struct nmgr_hdr *hdr; |
| 548 | |
| 549 | hdr = (struct nmgr_hdr *)buf; |
| 550 | if (len < sizeof(*hdr) || |
| 551 | (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) || |
| 552 | (ntohs(hdr->nh_len) < len - sizeof(*hdr))) { |
| 553 | return; |
| 554 | } |
| 555 | bs_hdr = hdr; |
| 556 | hdr->nh_group = ntohs(hdr->nh_group); |
| 557 | |
| 558 | buf += sizeof(*hdr); |
| 559 | len -= sizeof(*hdr); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 560 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 561 | cbor_state.payload_mut = (uint8_t *)bs_obuf; |
| 562 | cbor_state.payload_end = (const uint8_t *)bs_obuf |
| 563 | + sizeof(bs_obuf); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * Limited support for commands. |
| 567 | */ |
| 568 | if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) { |
| 569 | switch (hdr->nh_id) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 570 | case IMGMGR_NMGR_ID_STATE: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 571 | bs_list(buf, len); |
| 572 | break; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 573 | case IMGMGR_NMGR_ID_UPLOAD: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 574 | bs_upload(buf, len); |
| 575 | break; |
| 576 | default: |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 577 | bs_rc_rsp(MGMT_ERR_ENOTSUP); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 578 | break; |
| 579 | } |
| 580 | } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) { |
| 581 | switch (hdr->nh_id) { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 582 | case NMGR_ID_ECHO: |
| 583 | #ifdef MCUBOOT_BOOT_MGMT_ECHO |
| 584 | bs_echo(buf, len); |
| 585 | #endif |
| 586 | break; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 587 | case NMGR_ID_CONS_ECHO_CTRL: |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 588 | bs_rc_rsp(0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 589 | break; |
| 590 | case NMGR_ID_RESET: |
| 591 | bs_reset(buf, len); |
| 592 | break; |
| 593 | default: |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 594 | bs_rc_rsp(MGMT_ERR_ENOTSUP); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 595 | break; |
| 596 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 597 | } else if (MCUBOOT_PERUSER_MGMT_GROUP_ENABLED == 1) { |
| 598 | if (bs_peruser_system_specific(hdr, buf, len, &cbor_state) == 0) { |
| 599 | boot_serial_output(); |
| 600 | } |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 601 | } else { |
| 602 | bs_rc_rsp(MGMT_ERR_ENOTSUP); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 603 | } |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 604 | #ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU |
| 605 | bs_entry = true; |
| 606 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | static void |
| 610 | boot_serial_output(void) |
| 611 | { |
| 612 | char *data; |
| 613 | int len; |
| 614 | uint16_t crc; |
| 615 | uint16_t totlen; |
| 616 | char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 }; |
| 617 | char buf[BOOT_SERIAL_OUT_MAX]; |
| 618 | char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)]; |
| 619 | |
| 620 | data = bs_obuf; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 621 | len = (uint32_t)cbor_state.payload_mut - (uint32_t)bs_obuf; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 622 | |
| 623 | bs_hdr->nh_op++; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 624 | bs_hdr->nh_flags = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 625 | bs_hdr->nh_len = htons(len); |
| 626 | bs_hdr->nh_group = htons(bs_hdr->nh_group); |
| 627 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 628 | #ifdef __ZEPHYR__ |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 629 | crc = crc16_itu_t(CRC16_INITIAL_CRC, (uint8_t *)bs_hdr, sizeof(*bs_hdr)); |
| 630 | crc = crc16_itu_t(crc, data, len); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 631 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 632 | crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr)); |
| 633 | crc = crc16_ccitt(crc, data, len); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 634 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 635 | crc = htons(crc); |
| 636 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 637 | boot_uf->write(pkt_start, sizeof(pkt_start)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 638 | |
| 639 | totlen = len + sizeof(*bs_hdr) + sizeof(crc); |
| 640 | totlen = htons(totlen); |
| 641 | |
| 642 | memcpy(buf, &totlen, sizeof(totlen)); |
| 643 | totlen = sizeof(totlen); |
| 644 | memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr)); |
| 645 | totlen += sizeof(*bs_hdr); |
| 646 | memcpy(&buf[totlen], data, len); |
| 647 | totlen += len; |
| 648 | memcpy(&buf[totlen], &crc, sizeof(crc)); |
| 649 | totlen += sizeof(crc); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 650 | #ifdef __ZEPHYR__ |
| 651 | size_t enc_len; |
Carles Cufi | 0165be8 | 2018-03-26 17:43:51 +0200 | [diff] [blame] | 652 | base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 653 | totlen = enc_len; |
| 654 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 655 | totlen = base64_encode(buf, totlen, encoded_buf, 1); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 656 | #endif |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 657 | boot_uf->write(encoded_buf, totlen); |
| 658 | boot_uf->write("\n\r", 2); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 659 | BOOT_LOG_INF("TX"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /* |
| 663 | * Returns 1 if full packet has been received. |
| 664 | */ |
| 665 | static int |
| 666 | boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout) |
| 667 | { |
| 668 | int rc; |
| 669 | uint16_t crc; |
| 670 | uint16_t len; |
Marko Kiiskila | e5aeee4 | 2018-12-21 15:00:16 +0200 | [diff] [blame] | 671 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 672 | #ifdef __ZEPHYR__ |
| 673 | int err; |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 674 | 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] | 675 | if (err) { |
| 676 | return -1; |
| 677 | } |
| 678 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 679 | if (*out_off + base64_decode_len(in) >= maxout) { |
| 680 | return -1; |
| 681 | } |
| 682 | rc = base64_decode(in, &out[*out_off]); |
| 683 | if (rc < 0) { |
| 684 | return -1; |
| 685 | } |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 686 | #endif |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 687 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 688 | *out_off += rc; |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 689 | if (*out_off <= sizeof(uint16_t)) { |
| 690 | return 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 691 | } |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 692 | |
| 693 | len = ntohs(*(uint16_t *)out); |
| 694 | if (len != *out_off - sizeof(uint16_t)) { |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | if (len > *out_off - sizeof(uint16_t)) { |
| 699 | len = *out_off - sizeof(uint16_t); |
| 700 | } |
| 701 | |
| 702 | out += sizeof(uint16_t); |
| 703 | #ifdef __ZEPHYR__ |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 704 | crc = crc16_itu_t(CRC16_INITIAL_CRC, out, len); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 705 | #else |
| 706 | crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len); |
| 707 | #endif |
| 708 | if (crc || len <= sizeof(crc)) { |
| 709 | return 0; |
| 710 | } |
| 711 | *out_off -= sizeof(crc); |
| 712 | out[*out_off] = '\0'; |
| 713 | |
| 714 | return 1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Task which waits reading console, expecting to get image over |
| 719 | * serial port. |
| 720 | */ |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 721 | static void |
| 722 | 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] | 723 | { |
| 724 | int rc; |
| 725 | int off; |
David Brown | 57f0df3 | 2020-05-12 08:39:21 -0600 | [diff] [blame] | 726 | int dec_off = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 727 | int full_line; |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 728 | int max_input; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 729 | int elapsed_in_ms = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 730 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 731 | boot_uf = f; |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 732 | max_input = sizeof(in_buf); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 733 | |
| 734 | off = 0; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 735 | while (timeout_in_ms > 0 || bs_entry) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 736 | MCUBOOT_CPU_IDLE(); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 737 | MCUBOOT_WATCHDOG_FEED(); |
| 738 | #ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU |
| 739 | uint32_t start = k_uptime_get_32(); |
| 740 | #endif |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 741 | rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 742 | if (rc <= 0 && !full_line) { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 743 | goto check_timeout; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 744 | } |
| 745 | off += rc; |
| 746 | if (!full_line) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 747 | if (off == max_input) { |
| 748 | /* |
| 749 | * Full line, no newline yet. Reset the input buffer. |
| 750 | */ |
| 751 | off = 0; |
| 752 | } |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 753 | goto check_timeout; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 754 | } |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 755 | if (in_buf[0] == SHELL_NLIP_PKT_START1 && |
| 756 | in_buf[1] == SHELL_NLIP_PKT_START2) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 757 | dec_off = 0; |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 758 | rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input); |
| 759 | } else if (in_buf[0] == SHELL_NLIP_DATA_START1 && |
| 760 | in_buf[1] == SHELL_NLIP_DATA_START2) { |
| 761 | 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] | 762 | } |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 763 | |
| 764 | /* serve errors: out of decode memory, or bad encoding */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 765 | if (rc == 1) { |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 766 | boot_serial_input(&dec_buf[2], dec_off - 2); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 767 | } |
| 768 | off = 0; |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 769 | check_timeout: |
| 770 | /* Subtract elapsed time */ |
| 771 | #ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU |
| 772 | elapsed_in_ms = (k_uptime_get_32() - start); |
| 773 | #endif |
| 774 | timeout_in_ms -= elapsed_in_ms; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 775 | } |
| 776 | } |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 777 | |
| 778 | /* |
| 779 | * Task which waits reading console, expecting to get image over |
| 780 | * serial port. |
| 781 | */ |
| 782 | void |
| 783 | boot_serial_start(const struct boot_uart_funcs *f) |
| 784 | { |
| 785 | bs_entry = true; |
| 786 | boot_serial_read_console(f,0); |
| 787 | } |
| 788 | |
| 789 | #ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU |
| 790 | /* |
| 791 | * Task which waits reading console for a certain amount of timeout. |
| 792 | * If within this timeout no mcumgr command is received, the function is |
| 793 | * returning, else the serial boot is never exited |
| 794 | */ |
| 795 | void |
| 796 | boot_serial_check_start(const struct boot_uart_funcs *f, int timeout_in_ms) |
| 797 | { |
| 798 | bs_entry = false; |
| 799 | boot_serial_read_console(f,timeout_in_ms); |
| 800 | } |
| 801 | #endif |