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" |
| 28 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 29 | #ifdef __ZEPHYR__ |
Andrzej Puzdrowski | f1d189c | 2019-12-12 09:34:11 +0100 | [diff] [blame] | 30 | #include <power/reboot.h> |
| 31 | #include <sys/byteorder.h> |
| 32 | #include <sys/__assert.h> |
Peter Bigot | 54c1e3f | 2020-01-25 05:50:12 -0600 | [diff] [blame^] | 33 | #include <drivers/flash.h> |
Andrzej Puzdrowski | f1d189c | 2019-12-12 09:34:11 +0100 | [diff] [blame] | 34 | #include <sys/crc.h> |
| 35 | #include <sys/base64.h> |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 36 | #include <cbor.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> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 44 | #include <tinycbor/cbor.h> |
| 45 | #include <tinycbor/cbor_buf_reader.h> |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 46 | #endif /* __ZEPHYR__ */ |
| 47 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 48 | #include <flash_map_backend/flash_map_backend.h> |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 49 | #include <hal/hal_flash.h> |
| 50 | #include <os/os.h> |
| 51 | #include <os/os_malloc.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 52 | |
| 53 | #include <bootutil/image.h> |
| 54 | |
| 55 | #include "boot_serial/boot_serial.h" |
| 56 | #include "boot_serial_priv.h" |
| 57 | |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 58 | #ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY |
| 59 | #include "bootutil_priv.h" |
| 60 | #endif |
| 61 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 62 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 63 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 64 | #define BOOT_SERIAL_INPUT_MAX 512 |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 65 | #define BOOT_SERIAL_OUT_MAX 128 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 66 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 67 | #ifdef __ZEPHYR__ |
Carles Cufi | 0165be8 | 2018-03-26 17:43:51 +0200 | [diff] [blame] | 68 | /* base64 lib encodes data to null-terminated string */ |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 69 | #define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1) |
| 70 | |
| 71 | #define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */ |
| 72 | #define CRC_CITT_POLYMINAL 0x1021 |
| 73 | |
| 74 | #define ntohs(x) sys_be16_to_cpu(x) |
| 75 | #define htons(x) sys_cpu_to_be16(x) |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 76 | #endif |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 77 | |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 78 | #ifndef BOOT_IMAGE_NUMBER |
| 79 | #define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER |
| 80 | #endif |
| 81 | |
| 82 | #if (BOOT_IMAGE_NUMBER > 1) |
| 83 | #define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x)) |
| 84 | #else |
| 85 | #define IMAGES_ITER(x) |
| 86 | #endif |
| 87 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 88 | static char in_buf[BOOT_SERIAL_INPUT_MAX + 1]; |
| 89 | static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1]; |
Marko Kiiskila | 8b1ce3a | 2018-06-14 13:20:46 -0700 | [diff] [blame] | 90 | const struct boot_uart_funcs *boot_uf; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 91 | static uint32_t curr_off; |
| 92 | static uint32_t img_size; |
| 93 | static struct nmgr_hdr *bs_hdr; |
| 94 | |
| 95 | static char bs_obuf[BOOT_SERIAL_OUT_MAX]; |
| 96 | |
| 97 | static int bs_cbor_writer(struct cbor_encoder_writer *, const char *data, |
| 98 | int len); |
| 99 | static void boot_serial_output(void); |
| 100 | |
| 101 | static struct cbor_encoder_writer bs_writer = { |
| 102 | .write = bs_cbor_writer |
| 103 | }; |
| 104 | static CborEncoder bs_root; |
| 105 | static CborEncoder bs_rsp; |
| 106 | |
| 107 | int |
| 108 | bs_cbor_writer(struct cbor_encoder_writer *cew, const char *data, int len) |
| 109 | { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 110 | if (cew->bytes_written + len > sizeof(bs_obuf)) { |
| 111 | return CborErrorOutOfMemory; |
| 112 | } |
| 113 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 114 | memcpy(&bs_obuf[cew->bytes_written], data, len); |
| 115 | cew->bytes_written += len; |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /* |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 121 | * Convert version into string without use of snprintf(). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 122 | */ |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 123 | static int |
| 124 | u32toa(char *tgt, uint32_t val) |
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 | char *dst; |
| 127 | uint32_t d = 1; |
| 128 | uint32_t dgt; |
| 129 | int n = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 130 | |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 131 | dst = tgt; |
| 132 | while (val / d >= 10) { |
| 133 | d *= 10; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 134 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 135 | while (d) { |
| 136 | dgt = val / d; |
| 137 | val %= d; |
| 138 | d /= 10; |
| 139 | if (n || dgt > 0 || d == 0) { |
| 140 | *dst++ = dgt + '0'; |
| 141 | ++n; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 142 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 143 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 144 | *dst = '\0'; |
| 145 | |
| 146 | return dst - tgt; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * dst has to be able to fit "255.255.65535.4294967295" (25 characters). |
| 151 | */ |
| 152 | static void |
| 153 | bs_list_img_ver(char *dst, int maxlen, struct image_version *ver) |
| 154 | { |
| 155 | int off; |
| 156 | |
| 157 | off = u32toa(dst, ver->iv_major); |
| 158 | dst[off++] = '.'; |
| 159 | off += u32toa(dst + off, ver->iv_minor); |
| 160 | dst[off++] = '.'; |
| 161 | off += u32toa(dst + off, ver->iv_revision); |
| 162 | dst[off++] = '.'; |
| 163 | off += u32toa(dst + off, ver->iv_build_num); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
| 167 | * List images. |
| 168 | */ |
| 169 | static void |
| 170 | bs_list(char *buf, int len) |
| 171 | { |
| 172 | CborEncoder images; |
| 173 | CborEncoder image; |
| 174 | struct image_header hdr; |
| 175 | uint8_t tmpbuf[64]; |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 176 | int 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 | |
| 180 | cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength); |
| 181 | cbor_encode_text_stringz(&bs_rsp, "images"); |
| 182 | cbor_encoder_create_array(&bs_rsp, &images, CborIndefiniteLength); |
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 | |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 191 | flash_area_read(fap, 0, &hdr, sizeof(hdr)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 192 | |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 193 | if (hdr.ih_magic != IMAGE_MAGIC || |
| 194 | bootutil_img_validate(NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf), |
| 195 | NULL, 0, NULL)) { |
| 196 | flash_area_close(fap); |
| 197 | continue; |
| 198 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 199 | flash_area_close(fap); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 200 | |
| 201 | cbor_encoder_create_map(&images, &image, CborIndefiniteLength); |
| 202 | |
| 203 | #if (BOOT_IMAGE_NUMBER > 1) |
| 204 | cbor_encode_text_stringz(&image, "image"); |
| 205 | cbor_encode_int(&image, image_index); |
| 206 | #endif |
| 207 | |
| 208 | cbor_encode_text_stringz(&image, "slot"); |
| 209 | cbor_encode_int(&image, slot); |
| 210 | cbor_encode_text_stringz(&image, "version"); |
| 211 | |
| 212 | bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver); |
| 213 | cbor_encode_text_stringz(&image, (char *)tmpbuf); |
| 214 | cbor_encoder_close_container(&images, &image); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 215 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 216 | } |
| 217 | cbor_encoder_close_container(&bs_rsp, &images); |
| 218 | cbor_encoder_close_container(&bs_root, &bs_rsp); |
| 219 | boot_serial_output(); |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Image upload request. |
| 224 | */ |
| 225 | static void |
| 226 | bs_upload(char *buf, int len) |
| 227 | { |
| 228 | CborParser parser; |
| 229 | struct cbor_buf_reader reader; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 230 | struct CborValue root_value; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 231 | struct CborValue value; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 232 | uint8_t img_data[512]; |
| 233 | long long int off = UINT_MAX; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 234 | size_t img_blen = 0; |
Fabio Utzig | 30f6b2a | 2018-03-29 16:18:53 -0300 | [diff] [blame] | 235 | uint8_t rem_bytes; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 236 | long long int data_len = UINT_MAX; |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 237 | int img_num; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 238 | size_t slen; |
| 239 | char name_str[8]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 240 | const struct flash_area *fap = NULL; |
| 241 | int rc; |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 242 | #ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY |
| 243 | static off_t off_last = -1; |
| 244 | struct flash_sector sector; |
| 245 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 246 | |
| 247 | memset(img_data, 0, sizeof(img_data)); |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 248 | img_num = 0; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 249 | |
| 250 | /* |
| 251 | * Expected data format. |
| 252 | * { |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 253 | * "image":<image number in a multi-image set (OPTIONAL)> |
| 254 | * "data":<image data> |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 255 | * "len":<image len> |
| 256 | * "off":<current offset of image data> |
| 257 | * } |
| 258 | */ |
| 259 | |
| 260 | /* |
| 261 | * Object comes within { ... } |
| 262 | */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 263 | cbor_buf_reader_init(&reader, (uint8_t *)buf, len); |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 264 | #ifdef __ZEPHYR__ |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 265 | cbor_parser_cust_reader_init(&reader.r, 0, &parser, &root_value); |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 266 | #else |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 267 | cbor_parser_init(&reader.r, 0, &parser, &root_value); |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 268 | #endif |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 269 | if (!cbor_value_is_container(&root_value)) { |
| 270 | goto out_invalid_data; |
| 271 | } |
| 272 | if (cbor_value_enter_container(&root_value, &value)) { |
| 273 | goto out_invalid_data; |
| 274 | } |
| 275 | while (cbor_value_is_valid(&value)) { |
| 276 | /* |
| 277 | * Decode key. |
| 278 | */ |
| 279 | if (cbor_value_calculate_string_length(&value, &slen)) { |
| 280 | goto out_invalid_data; |
| 281 | } |
| 282 | if (!cbor_value_is_text_string(&value) || |
| 283 | slen >= sizeof(name_str) - 1) { |
| 284 | goto out_invalid_data; |
| 285 | } |
| 286 | if (cbor_value_copy_text_string(&value, name_str, &slen, &value)) { |
| 287 | goto out_invalid_data; |
| 288 | } |
| 289 | name_str[slen] = '\0'; |
| 290 | if (!strcmp(name_str, "data")) { |
| 291 | /* |
| 292 | * Image data |
| 293 | */ |
| 294 | if (value.type != CborByteStringType) { |
| 295 | goto out_invalid_data; |
| 296 | } |
| 297 | if (cbor_value_calculate_string_length(&value, &slen) || |
| 298 | slen >= sizeof(img_data)) { |
| 299 | goto out_invalid_data; |
| 300 | } |
| 301 | if (cbor_value_copy_byte_string(&value, img_data, &slen, &value)) { |
| 302 | goto out_invalid_data; |
| 303 | } |
| 304 | img_blen = slen; |
| 305 | } else if (!strcmp(name_str, "off")) { |
| 306 | /* |
| 307 | * Offset of the data. |
| 308 | */ |
| 309 | if (value.type != CborIntegerType) { |
| 310 | goto out_invalid_data; |
| 311 | } |
| 312 | if (cbor_value_get_int64(&value, &off)) { |
| 313 | goto out_invalid_data; |
| 314 | } |
| 315 | if (cbor_value_advance(&value)) { |
| 316 | goto out_invalid_data; |
| 317 | } |
| 318 | } else if (!strcmp(name_str, "len")) { |
| 319 | /* |
| 320 | * Length of the image. This should only be present in the first |
| 321 | * block of data; when offset is 0. |
| 322 | */ |
| 323 | if (value.type != CborIntegerType) { |
| 324 | goto out_invalid_data; |
| 325 | } |
| 326 | if (cbor_value_get_int64(&value, &data_len)) { |
| 327 | goto out_invalid_data; |
| 328 | } |
| 329 | if (cbor_value_advance(&value)) { |
| 330 | goto out_invalid_data; |
| 331 | } |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 332 | } else if (!strcmp(name_str, "image")) { |
| 333 | /* |
| 334 | * In a multi-image system, image number to upload to, if not |
| 335 | * present will upload to slot 0 of image set 0. |
| 336 | */ |
| 337 | if (value.type != CborIntegerType) { |
| 338 | goto out_invalid_data; |
| 339 | } |
| 340 | if (cbor_value_get_int(&value, &img_num)) { |
| 341 | goto out_invalid_data; |
| 342 | } |
| 343 | if (cbor_value_advance(&value)) { |
| 344 | goto out_invalid_data; |
| 345 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 346 | } else { |
| 347 | /* |
| 348 | * Unknown keys. |
| 349 | */ |
| 350 | if (cbor_value_advance(&value)) { |
| 351 | goto out_invalid_data; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | if (off == UINT_MAX) { |
| 356 | /* |
| 357 | * Offset must be set in every block. |
| 358 | */ |
| 359 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 362 | rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 363 | if (rc) { |
| 364 | rc = MGMT_ERR_EINVAL; |
| 365 | goto out; |
| 366 | } |
| 367 | |
| 368 | if (off == 0) { |
| 369 | curr_off = 0; |
| 370 | if (data_len > fap->fa_size) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 371 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 372 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 373 | #ifndef CONFIG_BOOT_ERASE_PROGRESSIVELY |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 374 | rc = flash_area_erase(fap, 0, fap->fa_size); |
| 375 | if (rc) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 376 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 377 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 378 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 379 | img_size = data_len; |
| 380 | } |
| 381 | if (off != curr_off) { |
| 382 | rc = 0; |
| 383 | goto out; |
| 384 | } |
Fabio Utzig | 30f6b2a | 2018-03-29 16:18:53 -0300 | [diff] [blame] | 385 | if (curr_off + img_blen < img_size) { |
| 386 | rem_bytes = img_blen % flash_area_align(fap); |
| 387 | if (rem_bytes) { |
| 388 | img_blen -= rem_bytes; |
| 389 | } |
| 390 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 391 | |
| 392 | #ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY |
| 393 | rc = flash_area_sector_from_off(curr_off + img_blen, §or); |
| 394 | if (rc) { |
| 395 | BOOT_LOG_ERR("Unable to determine flash sector size"); |
| 396 | goto out; |
| 397 | } |
| 398 | if (off_last != sector.fs_off) { |
| 399 | off_last = sector.fs_off; |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 400 | BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off); |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 401 | rc = flash_area_erase(fap, sector.fs_off, sector.fs_size); |
| 402 | if (rc) { |
| 403 | BOOT_LOG_ERR("Error %d while erasing sector", rc); |
| 404 | goto out; |
| 405 | } |
| 406 | } |
| 407 | #endif |
| 408 | |
| 409 | BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_blen); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 410 | rc = flash_area_write(fap, curr_off, img_data, img_blen); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 411 | if (rc == 0) { |
| 412 | curr_off += img_blen; |
Andrzej Puzdrowski | c2e30cf | 2018-07-20 16:19:09 +0200 | [diff] [blame] | 413 | #ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY |
| 414 | if (curr_off == img_size) { |
| 415 | /* get the last sector offset */ |
| 416 | rc = flash_area_sector_from_off(boot_status_off(fap), §or); |
| 417 | if (rc) { |
| 418 | BOOT_LOG_ERR("Unable to determine flash sector of" |
| 419 | "the image trailer"); |
| 420 | goto out; |
| 421 | } |
| 422 | /* Assure that sector for image trailer was erased. */ |
| 423 | /* Check whether it was erased during previous upload. */ |
| 424 | if (off_last < sector.fs_off) { |
| 425 | BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off); |
| 426 | rc = flash_area_erase(fap, sector.fs_off, sector.fs_size); |
| 427 | if (rc) { |
| 428 | BOOT_LOG_ERR("Error %d while erasing sector", rc); |
| 429 | goto out; |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | #endif |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 434 | } else { |
| 435 | out_invalid_data: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 436 | rc = MGMT_ERR_EINVAL; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 437 | } |
Emanuele Di Santo | 205c8c6 | 2018-07-20 11:42:31 +0200 | [diff] [blame] | 438 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 439 | out: |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 440 | BOOT_LOG_INF("RX: 0x%x", rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 441 | cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength); |
| 442 | cbor_encode_text_stringz(&bs_rsp, "rc"); |
| 443 | cbor_encode_int(&bs_rsp, rc); |
| 444 | if (rc == 0) { |
| 445 | cbor_encode_text_stringz(&bs_rsp, "off"); |
| 446 | cbor_encode_uint(&bs_rsp, curr_off); |
| 447 | } |
| 448 | cbor_encoder_close_container(&bs_root, &bs_rsp); |
| 449 | |
| 450 | boot_serial_output(); |
| 451 | flash_area_close(fap); |
| 452 | } |
| 453 | |
| 454 | /* |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 455 | * Console echo control/image erase. Send empty response, don't do anything. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 456 | */ |
| 457 | static void |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 458 | bs_empty_rsp(char *buf, int len) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 459 | { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 460 | cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength); |
| 461 | cbor_encode_text_stringz(&bs_rsp, "rc"); |
| 462 | cbor_encode_int(&bs_rsp, 0); |
| 463 | cbor_encoder_close_container(&bs_root, &bs_rsp); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 464 | boot_serial_output(); |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Reset, and (presumably) boot to newly uploaded image. Flush console |
| 469 | * before restarting. |
| 470 | */ |
Andrzej Puzdrowski | 268cdd0 | 2018-04-10 12:57:54 +0200 | [diff] [blame] | 471 | static void |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 472 | bs_reset(char *buf, int len) |
| 473 | { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 474 | bs_empty_rsp(buf, len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 475 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 476 | #ifdef __ZEPHYR__ |
| 477 | k_sleep(250); |
| 478 | sys_reboot(SYS_REBOOT_COLD); |
| 479 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 480 | os_cputime_delay_usecs(250000); |
| 481 | hal_system_reset(); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 482 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Parse incoming line of input from console. |
| 487 | * Expect newtmgr protocol with serial transport. |
| 488 | */ |
| 489 | void |
| 490 | boot_serial_input(char *buf, int len) |
| 491 | { |
| 492 | struct nmgr_hdr *hdr; |
| 493 | |
| 494 | hdr = (struct nmgr_hdr *)buf; |
| 495 | if (len < sizeof(*hdr) || |
| 496 | (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) || |
| 497 | (ntohs(hdr->nh_len) < len - sizeof(*hdr))) { |
| 498 | return; |
| 499 | } |
| 500 | bs_hdr = hdr; |
| 501 | hdr->nh_group = ntohs(hdr->nh_group); |
| 502 | |
| 503 | buf += sizeof(*hdr); |
| 504 | len -= sizeof(*hdr); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 505 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 506 | bs_writer.bytes_written = 0; |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 507 | #ifdef __ZEPHYR__ |
| 508 | cbor_encoder_cust_writer_init(&bs_root, &bs_writer, 0); |
| 509 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 510 | cbor_encoder_init(&bs_root, &bs_writer, 0); |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 511 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 512 | |
| 513 | /* |
| 514 | * Limited support for commands. |
| 515 | */ |
| 516 | if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) { |
| 517 | switch (hdr->nh_id) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 518 | case IMGMGR_NMGR_ID_STATE: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 519 | bs_list(buf, len); |
| 520 | break; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 521 | case IMGMGR_NMGR_ID_UPLOAD: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 522 | bs_upload(buf, len); |
| 523 | break; |
| 524 | default: |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 525 | bs_empty_rsp(buf, len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 526 | break; |
| 527 | } |
| 528 | } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) { |
| 529 | switch (hdr->nh_id) { |
| 530 | case NMGR_ID_CONS_ECHO_CTRL: |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 531 | bs_empty_rsp(buf, len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 532 | break; |
| 533 | case NMGR_ID_RESET: |
| 534 | bs_reset(buf, len); |
| 535 | break; |
| 536 | default: |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | static void |
| 543 | boot_serial_output(void) |
| 544 | { |
| 545 | char *data; |
| 546 | int len; |
| 547 | uint16_t crc; |
| 548 | uint16_t totlen; |
| 549 | char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 }; |
| 550 | char buf[BOOT_SERIAL_OUT_MAX]; |
| 551 | char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)]; |
| 552 | |
| 553 | data = bs_obuf; |
| 554 | len = bs_writer.bytes_written; |
| 555 | |
| 556 | bs_hdr->nh_op++; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 557 | bs_hdr->nh_flags = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 558 | bs_hdr->nh_len = htons(len); |
| 559 | bs_hdr->nh_group = htons(bs_hdr->nh_group); |
| 560 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 561 | #ifdef __ZEPHYR__ |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 562 | crc = crc16((u8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL, |
| 563 | CRC16_INITIAL_CRC, false); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 564 | crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true); |
| 565 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 566 | crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr)); |
| 567 | crc = crc16_ccitt(crc, data, len); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 568 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 569 | crc = htons(crc); |
| 570 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 571 | boot_uf->write(pkt_start, sizeof(pkt_start)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 572 | |
| 573 | totlen = len + sizeof(*bs_hdr) + sizeof(crc); |
| 574 | totlen = htons(totlen); |
| 575 | |
| 576 | memcpy(buf, &totlen, sizeof(totlen)); |
| 577 | totlen = sizeof(totlen); |
| 578 | memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr)); |
| 579 | totlen += sizeof(*bs_hdr); |
| 580 | memcpy(&buf[totlen], data, len); |
| 581 | totlen += len; |
| 582 | memcpy(&buf[totlen], &crc, sizeof(crc)); |
| 583 | totlen += sizeof(crc); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 584 | #ifdef __ZEPHYR__ |
| 585 | size_t enc_len; |
Carles Cufi | 0165be8 | 2018-03-26 17:43:51 +0200 | [diff] [blame] | 586 | base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 587 | totlen = enc_len; |
| 588 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 589 | totlen = base64_encode(buf, totlen, encoded_buf, 1); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 590 | #endif |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 591 | boot_uf->write(encoded_buf, totlen); |
| 592 | boot_uf->write("\n\r", 2); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 593 | BOOT_LOG_INF("TX"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Returns 1 if full packet has been received. |
| 598 | */ |
| 599 | static int |
| 600 | boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout) |
| 601 | { |
| 602 | int rc; |
| 603 | uint16_t crc; |
| 604 | uint16_t len; |
Marko Kiiskila | e5aeee4 | 2018-12-21 15:00:16 +0200 | [diff] [blame] | 605 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 606 | #ifdef __ZEPHYR__ |
| 607 | int err; |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 608 | 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] | 609 | if (err) { |
| 610 | return -1; |
| 611 | } |
| 612 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 613 | if (*out_off + base64_decode_len(in) >= maxout) { |
| 614 | return -1; |
| 615 | } |
| 616 | rc = base64_decode(in, &out[*out_off]); |
| 617 | if (rc < 0) { |
| 618 | return -1; |
| 619 | } |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 620 | #endif |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 621 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 622 | *out_off += rc; |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 623 | if (*out_off <= sizeof(uint16_t)) { |
| 624 | return 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 625 | } |
Fabio Utzig | 6f49c27 | 2019-08-23 11:42:58 -0300 | [diff] [blame] | 626 | |
| 627 | len = ntohs(*(uint16_t *)out); |
| 628 | if (len != *out_off - sizeof(uint16_t)) { |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | if (len > *out_off - sizeof(uint16_t)) { |
| 633 | len = *out_off - sizeof(uint16_t); |
| 634 | } |
| 635 | |
| 636 | out += sizeof(uint16_t); |
| 637 | #ifdef __ZEPHYR__ |
| 638 | crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true); |
| 639 | #else |
| 640 | crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len); |
| 641 | #endif |
| 642 | if (crc || len <= sizeof(crc)) { |
| 643 | return 0; |
| 644 | } |
| 645 | *out_off -= sizeof(crc); |
| 646 | out[*out_off] = '\0'; |
| 647 | |
| 648 | return 1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Task which waits reading console, expecting to get image over |
| 653 | * serial port. |
| 654 | */ |
| 655 | void |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 656 | boot_serial_start(const struct boot_uart_funcs *f) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 657 | { |
| 658 | int rc; |
| 659 | int off; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 660 | int dec_off; |
| 661 | int full_line; |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 662 | int max_input; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 663 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 664 | boot_uf = f; |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 665 | max_input = sizeof(in_buf); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 666 | |
| 667 | off = 0; |
| 668 | while (1) { |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 669 | rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 670 | if (rc <= 0 && !full_line) { |
| 671 | continue; |
| 672 | } |
| 673 | off += rc; |
| 674 | if (!full_line) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 675 | if (off == max_input) { |
| 676 | /* |
| 677 | * Full line, no newline yet. Reset the input buffer. |
| 678 | */ |
| 679 | off = 0; |
| 680 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 681 | continue; |
| 682 | } |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 683 | if (in_buf[0] == SHELL_NLIP_PKT_START1 && |
| 684 | in_buf[1] == SHELL_NLIP_PKT_START2) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 685 | dec_off = 0; |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 686 | rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input); |
| 687 | } else if (in_buf[0] == SHELL_NLIP_DATA_START1 && |
| 688 | in_buf[1] == SHELL_NLIP_DATA_START2) { |
| 689 | 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] | 690 | } |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 691 | |
| 692 | /* serve errors: out of decode memory, or bad encoding */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 693 | if (rc == 1) { |
Andrzej Puzdrowski | ec1e4d1 | 2018-06-18 14:36:14 +0200 | [diff] [blame] | 694 | boot_serial_input(&dec_buf[2], dec_off - 2); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 695 | } |
| 696 | off = 0; |
| 697 | } |
| 698 | } |