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__ |
Andrzej Puzdrowski | f1d189c | 2019-12-12 09:34:11 +0100 | [diff] [blame] | 31 | #include <power/reboot.h> |
| 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 | |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 61 | #include "serial_recovery_cbor.h" |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 62 | #include "bootutil/boot_hooks.h" |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 63 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 64 | BOOT_LOG_MODULE_DECLARE(mcuboot); |
| 65 | |
| 66 | #ifndef BOOT_IMAGE_NUMBER |
| 67 | #define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER |
| 68 | #endif |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 69 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 70 | #define BOOT_SERIAL_INPUT_MAX 512 |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 71 | #define BOOT_SERIAL_OUT_MAX (128 * MCUBOOT_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; |
| 96 | |
| 97 | static char bs_obuf[BOOT_SERIAL_OUT_MAX]; |
| 98 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 99 | static void boot_serial_output(void); |
| 100 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 101 | static cbor_state_backups_t dummy_backups; |
| 102 | static cbor_state_t cbor_state = { |
| 103 | .backups = &dummy_backups |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 104 | }; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 105 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 106 | /** |
| 107 | * Function that processes MGMT_GROUP_ID_PERUSER mcumgr group and may be |
| 108 | * used to process any groups that have not been processed by generic boot |
| 109 | * serial implementation. |
| 110 | * |
| 111 | * @param[in] hdr -- the decoded header of mcumgr message; |
| 112 | * @param[in] buffer -- buffer with first mcumgr message; |
| 113 | * @param[in] len -- length of of data in buffer; |
| 114 | * @param[out] *cs -- object with encoded response. |
| 115 | * |
| 116 | * @return 0 on success; non-0 error code otherwise. |
| 117 | */ |
| 118 | extern int bs_peruser_system_specific(const struct nmgr_hdr *hdr, |
| 119 | const char *buffer, |
| 120 | int len, cbor_state_t *cs); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 121 | |
| 122 | /* |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 123 | * Convert version into string without use of snprintf(). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 124 | */ |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 125 | static int |
| 126 | u32toa(char *tgt, uint32_t val) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 127 | { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 128 | char *dst; |
| 129 | uint32_t d = 1; |
| 130 | uint32_t dgt; |
| 131 | int n = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 132 | |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 133 | dst = tgt; |
| 134 | while (val / d >= 10) { |
| 135 | d *= 10; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 136 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 137 | while (d) { |
| 138 | dgt = val / d; |
| 139 | val %= d; |
| 140 | d /= 10; |
| 141 | if (n || dgt > 0 || d == 0) { |
| 142 | *dst++ = dgt + '0'; |
| 143 | ++n; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 144 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 145 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 146 | *dst = '\0'; |
| 147 | |
| 148 | return dst - tgt; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * dst has to be able to fit "255.255.65535.4294967295" (25 characters). |
| 153 | */ |
| 154 | static void |
| 155 | bs_list_img_ver(char *dst, int maxlen, struct image_version *ver) |
| 156 | { |
| 157 | int off; |
| 158 | |
| 159 | off = u32toa(dst, ver->iv_major); |
| 160 | dst[off++] = '.'; |
| 161 | off += u32toa(dst + off, ver->iv_minor); |
| 162 | dst[off++] = '.'; |
| 163 | off += u32toa(dst + off, ver->iv_revision); |
| 164 | dst[off++] = '.'; |
| 165 | off += u32toa(dst + off, ver->iv_build_num); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
| 169 | * List images. |
| 170 | */ |
| 171 | static void |
| 172 | bs_list(char *buf, int len) |
| 173 | { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 174 | struct image_header hdr; |
| 175 | uint8_t tmpbuf[64]; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 176 | uint32_t slot, area_id; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 177 | const struct flash_area *fap; |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 178 | uint8_t image_index; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 179 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 180 | map_start_encode(&cbor_state, 1); |
| 181 | tstrx_put(&cbor_state, "images"); |
| 182 | list_start_encode(&cbor_state, 5); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 183 | image_index = 0; |
| 184 | IMAGES_ITER(image_index) { |
| 185 | for (slot = 0; slot < 2; slot++) { |
| 186 | area_id = flash_area_id_from_multi_image_slot(image_index, slot); |
| 187 | if (flash_area_open(area_id, &fap)) { |
| 188 | continue; |
| 189 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 190 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 191 | int rc = BOOT_HOOK_CALL(boot_read_image_header_hook, |
| 192 | BOOT_HOOK_REGULAR, image_index, slot, &hdr); |
| 193 | if (rc == BOOT_HOOK_REGULAR) |
| 194 | { |
| 195 | flash_area_read(fap, 0, &hdr, sizeof(hdr)); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 196 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 197 | |
| 198 | fih_int fih_rc = FIH_FAILURE; |
| 199 | |
| 200 | if (hdr.ih_magic == IMAGE_MAGIC) |
| 201 | { |
| 202 | BOOT_HOOK_CALL_FIH(boot_image_check_hook, |
| 203 | fih_int_encode(BOOT_HOOK_REGULAR), |
| 204 | fih_rc, image_index, slot); |
| 205 | if (fih_eq(fih_rc, BOOT_HOOK_REGULAR)) |
| 206 | { |
| 207 | FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf), |
| 208 | NULL, 0, NULL); |
| 209 | } |
| 210 | } |
| 211 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 212 | flash_area_close(fap); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 213 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 214 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | map_start_encode(&cbor_state, 20); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 219 | |
| 220 | #if (BOOT_IMAGE_NUMBER > 1) |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 221 | tstrx_put(&cbor_state, "image"); |
| 222 | uintx32_put(&cbor_state, image_index); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 223 | #endif |
| 224 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 225 | tstrx_put(&cbor_state, "slot"); |
| 226 | uintx32_put(&cbor_state, slot); |
| 227 | tstrx_put(&cbor_state, "version"); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 228 | |
| 229 | bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 230 | tstrx_put_term(&cbor_state, (char *)tmpbuf); |
| 231 | map_end_encode(&cbor_state, 20); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 232 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 233 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 234 | list_end_encode(&cbor_state, 5); |
| 235 | map_end_encode(&cbor_state, 1); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 236 | boot_serial_output(); |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Image upload request. |
| 241 | */ |
| 242 | static void |
| 243 | bs_upload(char *buf, int len) |
| 244 | { |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 245 | const uint8_t *img_data = NULL; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 246 | long long int off = UINT64_MAX; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 247 | size_t img_blen = 0; |
Fabio Utzig | 30f6b2a | 2018-03-29 16:18:53 -0300 | [diff] [blame] | 248 | uint8_t rem_bytes; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 249 | long long int data_len = UINT64_MAX; |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 250 | int img_num; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 251 | size_t slen; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 252 | const struct flash_area *fap = NULL; |
| 253 | int rc; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 254 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 255 | static off_t off_last = -1; |
| 256 | struct flash_sector sector; |
| 257 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 258 | |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 259 | img_num = 0; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 260 | |
| 261 | /* |
| 262 | * Expected data format. |
| 263 | * { |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 264 | * "image":<image number in a multi-image set (OPTIONAL)> |
| 265 | * "data":<image data> |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 266 | * "len":<image len> |
| 267 | * "off":<current offset of image data> |
| 268 | * } |
| 269 | */ |
| 270 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 271 | struct Upload upload; |
| 272 | uint32_t decoded_len; |
| 273 | bool result = cbor_decode_Upload((const uint8_t *)buf, len, &upload, &decoded_len); |
| 274 | |
| 275 | if (!result || (len != decoded_len)) { |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 276 | goto out_invalid_data; |
| 277 | } |
Dominik Ermel | 470e2f3 | 2020-01-10 13:28:48 +0000 | [diff] [blame] | 278 | |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 279 | for (int i = 0; i < upload._Upload_members_count; i++) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 280 | struct Member_ *member = &upload._Upload_members[i]; |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 281 | switch(member->_Member_choice) { |
| 282 | case _Member_image: |
| 283 | img_num = member->_Member_image; |
| 284 | break; |
| 285 | case _Member_data: |
| 286 | img_data = member->_Member_data.value; |
| 287 | slen = member->_Member_data.len; |
| 288 | img_blen = slen; |
| 289 | break; |
| 290 | case _Member_len: |
| 291 | data_len = member->_Member_len; |
| 292 | break; |
| 293 | case _Member_off: |
| 294 | off = member->_Member_off; |
| 295 | break; |
| 296 | case _Member_sha: |
| 297 | default: |
| 298 | /* Nothing to do. */ |
| 299 | break; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 300 | } |
| 301 | } |
Øyvind Rønningstad | f42a820 | 2019-12-13 03:27:54 +0100 | [diff] [blame] | 302 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 303 | if (off == UINT64_MAX || img_data == NULL) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 304 | /* |
| 305 | * Offset must be set in every block. |
| 306 | */ |
| 307 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 310 | #if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD) |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 311 | 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^] | 312 | #else |
| 313 | rc = flash_area_open(flash_area_id_from_direct_image(img_num), &fap); |
| 314 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 315 | if (rc) { |
| 316 | rc = MGMT_ERR_EINVAL; |
| 317 | goto out; |
| 318 | } |
| 319 | |
| 320 | if (off == 0) { |
| 321 | curr_off = 0; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 322 | if (data_len > flash_area_get_size(fap)) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 323 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 324 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 325 | #ifndef MCUBOOT_ERASE_PROGRESSIVELY |
| 326 | rc = flash_area_erase(fap, 0, flash_area_get_size(fap)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 327 | if (rc) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 328 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 329 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 330 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 331 | img_size = data_len; |
| 332 | } |
| 333 | if (off != curr_off) { |
| 334 | rc = 0; |
| 335 | goto out; |
| 336 | } |
Andrzej Puzdrowski | f48de7a | 2020-10-19 09:42:02 +0200 | [diff] [blame] | 337 | |
| 338 | if (curr_off + img_blen > img_size) { |
| 339 | rc = MGMT_ERR_EINVAL; |
| 340 | goto out; |
| 341 | } |
| 342 | |
| 343 | rem_bytes = img_blen % flash_area_align(fap); |
| 344 | |
| 345 | if ((curr_off + img_blen < img_size) && rem_bytes) { |
| 346 | img_blen -= rem_bytes; |
| 347 | rem_bytes = 0; |
Fabio Utzig | 30f6b2a | 2018-03-29 16:18:53 -0300 | [diff] [blame] | 348 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 349 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 350 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 351 | rc = flash_area_sector_from_off(curr_off + img_blen, §or); |
| 352 | if (rc) { |
| 353 | BOOT_LOG_ERR("Unable to determine flash sector size"); |
| 354 | goto out; |
| 355 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 356 | if (off_last != flash_sector_get_off(§or)) { |
| 357 | off_last = flash_sector_get_off(§or); |
| 358 | BOOT_LOG_INF("Erasing sector at offset 0x%x", flash_sector_get_off(§or)); |
| 359 | rc = flash_area_erase(fap, flash_sector_get_off(§or), |
| 360 | flash_sector_get_size(§or)); |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 361 | if (rc) { |
| 362 | BOOT_LOG_ERR("Error %d while erasing sector", rc); |
| 363 | goto out; |
| 364 | } |
| 365 | } |
| 366 | #endif |
| 367 | |
| 368 | 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] | 369 | if (rem_bytes) { |
| 370 | /* the last chunk of the image might be unaligned */ |
| 371 | uint8_t wbs_aligned[BOOT_MAX_ALIGN]; |
| 372 | size_t w_size = img_blen - rem_bytes; |
| 373 | |
| 374 | if (w_size) { |
| 375 | rc = flash_area_write(fap, curr_off, img_data, w_size); |
| 376 | if (rc) { |
| 377 | goto out_invalid_data; |
| 378 | } |
| 379 | curr_off += w_size; |
| 380 | img_blen -= w_size; |
| 381 | img_data += w_size; |
| 382 | } |
| 383 | |
| 384 | if (img_blen) { |
| 385 | memcpy(wbs_aligned, img_data, rem_bytes); |
| 386 | memset(wbs_aligned + rem_bytes, flash_area_erased_val(fap), |
| 387 | sizeof(wbs_aligned) - rem_bytes); |
| 388 | rc = flash_area_write(fap, curr_off, wbs_aligned, flash_area_align(fap)); |
| 389 | } |
| 390 | |
| 391 | } else { |
| 392 | rc = flash_area_write(fap, curr_off, img_data, img_blen); |
| 393 | } |
| 394 | |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 395 | if (rc == 0) { |
| 396 | curr_off += img_blen; |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 397 | if (curr_off == img_size) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 398 | #ifdef MCUBOOT_ERASE_PROGRESSIVELY |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 399 | /* get the last sector offset */ |
| 400 | rc = flash_area_sector_from_off(boot_status_off(fap), §or); |
| 401 | if (rc) { |
| 402 | BOOT_LOG_ERR("Unable to determine flash sector of" |
| 403 | "the image trailer"); |
| 404 | goto out; |
| 405 | } |
| 406 | /* Assure that sector for image trailer was erased. */ |
| 407 | /* Check whether it was erased during previous upload. */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 408 | if (off_last < flash_sector_get_off(§or)) { |
| 409 | BOOT_LOG_INF("Erasing sector at offset 0x%x", |
| 410 | flash_sector_get_off(§or)); |
| 411 | rc = flash_area_erase(fap, flash_sector_get_off(§or), |
| 412 | flash_sector_get_size(§or)); |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 413 | if (rc) { |
| 414 | BOOT_LOG_ERR("Error %d while erasing sector", rc); |
| 415 | goto out; |
| 416 | } |
| 417 | } |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 418 | #endif |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 419 | rc = BOOT_HOOK_CALL(boot_serial_uploaded_hook, 0, img_num, fap, |
| 420 | img_size); |
| 421 | if (rc) { |
| 422 | BOOT_LOG_ERR("Error %d post upload hook", rc); |
| 423 | goto out; |
| 424 | } |
| 425 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 426 | } else { |
| 427 | out_invalid_data: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 428 | rc = MGMT_ERR_EINVAL; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 429 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 430 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 431 | out: |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 432 | BOOT_LOG_INF("RX: 0x%x", rc); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 433 | map_start_encode(&cbor_state, 10); |
| 434 | tstrx_put(&cbor_state, "rc"); |
| 435 | uintx32_put(&cbor_state, rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 436 | if (rc == 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 437 | tstrx_put(&cbor_state, "off"); |
| 438 | uintx32_put(&cbor_state, curr_off); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 439 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 440 | map_end_encode(&cbor_state, 10); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 441 | |
| 442 | boot_serial_output(); |
| 443 | flash_area_close(fap); |
| 444 | } |
| 445 | |
| 446 | /* |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 447 | * Console echo control/image erase. Send empty response, don't do anything. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 448 | */ |
| 449 | static void |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 450 | bs_empty_rsp(char *buf, int len) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 451 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 452 | map_start_encode(&cbor_state, 10); |
| 453 | tstrx_put(&cbor_state, "rc"); |
| 454 | uintx32_put(&cbor_state, 0); |
| 455 | map_end_encode(&cbor_state, 10); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 456 | boot_serial_output(); |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Reset, and (presumably) boot to newly uploaded image. Flush console |
| 461 | * before restarting. |
| 462 | */ |
Andrzej Puzdrowski | 268cdd0 | 2018-04-10 12:57:54 +0200 | [diff] [blame] | 463 | static void |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 464 | bs_reset(char *buf, int len) |
| 465 | { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 466 | bs_empty_rsp(buf, len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 467 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 468 | #ifdef __ZEPHYR__ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 469 | #ifdef CONFIG_MULTITHREADING |
Carles Cufi | 7e7b4ad | 2020-03-30 19:12:02 +0200 | [diff] [blame] | 470 | k_sleep(K_MSEC(250)); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 471 | #else |
| 472 | k_busy_wait(250000); |
| 473 | #endif |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 474 | sys_reboot(SYS_REBOOT_COLD); |
| 475 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 476 | os_cputime_delay_usecs(250000); |
| 477 | hal_system_reset(); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 478 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Parse incoming line of input from console. |
| 483 | * Expect newtmgr protocol with serial transport. |
| 484 | */ |
| 485 | void |
| 486 | boot_serial_input(char *buf, int len) |
| 487 | { |
| 488 | struct nmgr_hdr *hdr; |
| 489 | |
| 490 | hdr = (struct nmgr_hdr *)buf; |
| 491 | if (len < sizeof(*hdr) || |
| 492 | (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) || |
| 493 | (ntohs(hdr->nh_len) < len - sizeof(*hdr))) { |
| 494 | return; |
| 495 | } |
| 496 | bs_hdr = hdr; |
| 497 | hdr->nh_group = ntohs(hdr->nh_group); |
| 498 | |
| 499 | buf += sizeof(*hdr); |
| 500 | len -= sizeof(*hdr); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 501 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 502 | cbor_state.payload_mut = (uint8_t *)bs_obuf; |
| 503 | cbor_state.payload_end = (const uint8_t *)bs_obuf |
| 504 | + sizeof(bs_obuf); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 505 | |
| 506 | /* |
| 507 | * Limited support for commands. |
| 508 | */ |
| 509 | if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) { |
| 510 | switch (hdr->nh_id) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 511 | case IMGMGR_NMGR_ID_STATE: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 512 | bs_list(buf, len); |
| 513 | break; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 514 | case IMGMGR_NMGR_ID_UPLOAD: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 515 | bs_upload(buf, len); |
| 516 | break; |
| 517 | default: |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 518 | bs_empty_rsp(buf, len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 519 | break; |
| 520 | } |
| 521 | } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) { |
| 522 | switch (hdr->nh_id) { |
| 523 | case NMGR_ID_CONS_ECHO_CTRL: |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 524 | bs_empty_rsp(buf, len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 525 | break; |
| 526 | case NMGR_ID_RESET: |
| 527 | bs_reset(buf, len); |
| 528 | break; |
| 529 | default: |
| 530 | break; |
| 531 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 532 | } else if (MCUBOOT_PERUSER_MGMT_GROUP_ENABLED == 1) { |
| 533 | if (bs_peruser_system_specific(hdr, buf, len, &cbor_state) == 0) { |
| 534 | boot_serial_output(); |
| 535 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | static void |
| 540 | boot_serial_output(void) |
| 541 | { |
| 542 | char *data; |
| 543 | int len; |
| 544 | uint16_t crc; |
| 545 | uint16_t totlen; |
| 546 | char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 }; |
| 547 | char buf[BOOT_SERIAL_OUT_MAX]; |
| 548 | char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)]; |
| 549 | |
| 550 | data = bs_obuf; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 551 | len = (uint32_t)cbor_state.payload_mut - (uint32_t)bs_obuf; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 552 | |
| 553 | bs_hdr->nh_op++; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 554 | bs_hdr->nh_flags = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 555 | bs_hdr->nh_len = htons(len); |
| 556 | bs_hdr->nh_group = htons(bs_hdr->nh_group); |
| 557 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 558 | #ifdef __ZEPHYR__ |
Kumar Gala | 0813efe | 2020-05-27 12:25:41 -0500 | [diff] [blame] | 559 | crc = crc16((uint8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL, |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 560 | CRC16_INITIAL_CRC, false); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 561 | crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true); |
| 562 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 563 | crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr)); |
| 564 | crc = crc16_ccitt(crc, data, len); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 565 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 566 | crc = htons(crc); |
| 567 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 568 | boot_uf->write(pkt_start, sizeof(pkt_start)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 569 | |
| 570 | totlen = len + sizeof(*bs_hdr) + sizeof(crc); |
| 571 | totlen = htons(totlen); |
| 572 | |
| 573 | memcpy(buf, &totlen, sizeof(totlen)); |
| 574 | totlen = sizeof(totlen); |
| 575 | memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr)); |
| 576 | totlen += sizeof(*bs_hdr); |
| 577 | memcpy(&buf[totlen], data, len); |
| 578 | totlen += len; |
| 579 | memcpy(&buf[totlen], &crc, sizeof(crc)); |
| 580 | totlen += sizeof(crc); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 581 | #ifdef __ZEPHYR__ |
| 582 | size_t enc_len; |
Carles Cufi | 0165be8 | 2018-03-26 17:43:51 +0200 | [diff] [blame] | 583 | base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 584 | totlen = enc_len; |
| 585 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 586 | totlen = base64_encode(buf, totlen, encoded_buf, 1); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 587 | #endif |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 588 | boot_uf->write(encoded_buf, totlen); |
| 589 | boot_uf->write("\n\r", 2); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 590 | BOOT_LOG_INF("TX"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Returns 1 if full packet has been received. |
| 595 | */ |
| 596 | static int |
| 597 | boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout) |
| 598 | { |
| 599 | int rc; |
| 600 | uint16_t crc; |
| 601 | uint16_t len; |
Marko Kiiskila | e5aeee4 | 2018-12-21 15:00:16 +0200 | [diff] [blame] | 602 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 603 | #ifdef __ZEPHYR__ |
| 604 | int err; |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 605 | 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] | 606 | if (err) { |
| 607 | return -1; |
| 608 | } |
| 609 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 610 | if (*out_off + base64_decode_len(in) >= maxout) { |
| 611 | return -1; |
| 612 | } |
| 613 | rc = base64_decode(in, &out[*out_off]); |
| 614 | if (rc < 0) { |
| 615 | return -1; |
| 616 | } |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 617 | #endif |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 618 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 619 | *out_off += rc; |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 620 | if (*out_off <= sizeof(uint16_t)) { |
| 621 | return 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 622 | } |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 623 | |
| 624 | len = ntohs(*(uint16_t *)out); |
| 625 | if (len != *out_off - sizeof(uint16_t)) { |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | if (len > *out_off - sizeof(uint16_t)) { |
| 630 | len = *out_off - sizeof(uint16_t); |
| 631 | } |
| 632 | |
| 633 | out += sizeof(uint16_t); |
| 634 | #ifdef __ZEPHYR__ |
| 635 | crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true); |
| 636 | #else |
| 637 | crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len); |
| 638 | #endif |
| 639 | if (crc || len <= sizeof(crc)) { |
| 640 | return 0; |
| 641 | } |
| 642 | *out_off -= sizeof(crc); |
| 643 | out[*out_off] = '\0'; |
| 644 | |
| 645 | return 1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* |
| 649 | * Task which waits reading console, expecting to get image over |
| 650 | * serial port. |
| 651 | */ |
| 652 | void |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 653 | boot_serial_start(const struct boot_uart_funcs *f) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 654 | { |
| 655 | int rc; |
| 656 | int off; |
David Brown | 57f0df3 | 2020-05-12 08:39:21 -0600 | [diff] [blame] | 657 | int dec_off = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 658 | int full_line; |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 659 | int max_input; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 660 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 661 | boot_uf = f; |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 662 | max_input = sizeof(in_buf); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 663 | |
| 664 | off = 0; |
| 665 | while (1) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 666 | MCUBOOT_CPU_IDLE(); |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 667 | rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 668 | if (rc <= 0 && !full_line) { |
| 669 | continue; |
| 670 | } |
| 671 | off += rc; |
| 672 | if (!full_line) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 673 | if (off == max_input) { |
| 674 | /* |
| 675 | * Full line, no newline yet. Reset the input buffer. |
| 676 | */ |
| 677 | off = 0; |
| 678 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 679 | continue; |
| 680 | } |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 681 | if (in_buf[0] == SHELL_NLIP_PKT_START1 && |
| 682 | in_buf[1] == SHELL_NLIP_PKT_START2) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 683 | dec_off = 0; |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 684 | rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input); |
| 685 | } else if (in_buf[0] == SHELL_NLIP_DATA_START1 && |
| 686 | in_buf[1] == SHELL_NLIP_DATA_START2) { |
| 687 | 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] | 688 | } |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 689 | |
| 690 | /* serve errors: out of decode memory, or bad encoding */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 691 | if (rc == 1) { |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 692 | boot_serial_input(&dec_buf[2], dec_off - 2); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 693 | } |
| 694 | off = 0; |
| 695 | } |
| 696 | } |