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__ |
| 30 | #include <misc/reboot.h> |
| 31 | #include <misc/byteorder.h> |
| 32 | #include <misc/__assert.h> |
| 33 | #include <flash.h> |
| 34 | #include <crc16.h> |
Carles Cufi | 0165be8 | 2018-03-26 17:43:51 +0200 | [diff] [blame] | 35 | #include <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 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 58 | #define BOOT_SERIAL_INPUT_MAX 512 |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 59 | #define BOOT_SERIAL_OUT_MAX 80 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 60 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 61 | #ifdef __ZEPHYR__ |
Carles Cufi | 0165be8 | 2018-03-26 17:43:51 +0200 | [diff] [blame] | 62 | /* base64 lib encodes data to null-terminated string */ |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 63 | #define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1) |
| 64 | |
| 65 | #define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */ |
| 66 | #define CRC_CITT_POLYMINAL 0x1021 |
| 67 | |
| 68 | #define ntohs(x) sys_be16_to_cpu(x) |
| 69 | #define htons(x) sys_cpu_to_be16(x) |
| 70 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 71 | #endif |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 72 | static char in_buf[BOOT_SERIAL_INPUT_MAX + 1]; |
| 73 | static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1]; |
| 74 | static const struct boot_uart_funcs *boot_uf; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 75 | static uint32_t curr_off; |
| 76 | static uint32_t img_size; |
| 77 | static struct nmgr_hdr *bs_hdr; |
| 78 | |
| 79 | static char bs_obuf[BOOT_SERIAL_OUT_MAX]; |
| 80 | |
| 81 | static int bs_cbor_writer(struct cbor_encoder_writer *, const char *data, |
| 82 | int len); |
| 83 | static void boot_serial_output(void); |
| 84 | |
| 85 | static struct cbor_encoder_writer bs_writer = { |
| 86 | .write = bs_cbor_writer |
| 87 | }; |
| 88 | static CborEncoder bs_root; |
| 89 | static CborEncoder bs_rsp; |
| 90 | |
| 91 | int |
| 92 | bs_cbor_writer(struct cbor_encoder_writer *cew, const char *data, int len) |
| 93 | { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 94 | if (cew->bytes_written + len > sizeof(bs_obuf)) { |
| 95 | return CborErrorOutOfMemory; |
| 96 | } |
| 97 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 98 | memcpy(&bs_obuf[cew->bytes_written], data, len); |
| 99 | cew->bytes_written += len; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | /* |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 105 | * Convert version into string without use of snprintf(). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 106 | */ |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 107 | static int |
| 108 | u32toa(char *tgt, uint32_t val) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 109 | { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 110 | char *dst; |
| 111 | uint32_t d = 1; |
| 112 | uint32_t dgt; |
| 113 | int n = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 114 | |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 115 | dst = tgt; |
| 116 | while (val / d >= 10) { |
| 117 | d *= 10; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 118 | } |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 119 | while (d) { |
| 120 | dgt = val / d; |
| 121 | val %= d; |
| 122 | d /= 10; |
| 123 | if (n || dgt > 0 || d == 0) { |
| 124 | *dst++ = dgt + '0'; |
| 125 | ++n; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 126 | } |
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 | *dst = '\0'; |
| 129 | |
| 130 | return dst - tgt; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * dst has to be able to fit "255.255.65535.4294967295" (25 characters). |
| 135 | */ |
| 136 | static void |
| 137 | bs_list_img_ver(char *dst, int maxlen, struct image_version *ver) |
| 138 | { |
| 139 | int off; |
| 140 | |
| 141 | off = u32toa(dst, ver->iv_major); |
| 142 | dst[off++] = '.'; |
| 143 | off += u32toa(dst + off, ver->iv_minor); |
| 144 | dst[off++] = '.'; |
| 145 | off += u32toa(dst + off, ver->iv_revision); |
| 146 | dst[off++] = '.'; |
| 147 | off += u32toa(dst + off, ver->iv_build_num); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* |
| 151 | * List images. |
| 152 | */ |
| 153 | static void |
| 154 | bs_list(char *buf, int len) |
| 155 | { |
| 156 | CborEncoder images; |
| 157 | CborEncoder image; |
| 158 | struct image_header hdr; |
| 159 | uint8_t tmpbuf[64]; |
| 160 | int i, area_id; |
| 161 | const struct flash_area *fap; |
| 162 | |
| 163 | cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength); |
| 164 | cbor_encode_text_stringz(&bs_rsp, "images"); |
| 165 | cbor_encoder_create_array(&bs_rsp, &images, CborIndefiniteLength); |
| 166 | for (i = 0; i < 2; i++) { |
| 167 | area_id = flash_area_id_from_image_slot(i); |
| 168 | if (flash_area_open(area_id, &fap)) { |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | flash_area_read(fap, 0, &hdr, sizeof(hdr)); |
| 173 | |
| 174 | if (hdr.ih_magic != IMAGE_MAGIC || |
| 175 | bootutil_img_validate(&hdr, fap, tmpbuf, sizeof(tmpbuf), |
| 176 | NULL, 0, NULL)) { |
| 177 | flash_area_close(fap); |
| 178 | continue; |
| 179 | } |
| 180 | flash_area_close(fap); |
| 181 | |
| 182 | cbor_encoder_create_map(&images, &image, CborIndefiniteLength); |
| 183 | cbor_encode_text_stringz(&image, "slot"); |
| 184 | cbor_encode_int(&image, i); |
| 185 | cbor_encode_text_stringz(&image, "version"); |
| 186 | |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 187 | bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 188 | cbor_encode_text_stringz(&image, (char *)tmpbuf); |
| 189 | cbor_encoder_close_container(&images, &image); |
| 190 | } |
| 191 | cbor_encoder_close_container(&bs_rsp, &images); |
| 192 | cbor_encoder_close_container(&bs_root, &bs_rsp); |
| 193 | boot_serial_output(); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Image upload request. |
| 198 | */ |
| 199 | static void |
| 200 | bs_upload(char *buf, int len) |
| 201 | { |
| 202 | CborParser parser; |
| 203 | struct cbor_buf_reader reader; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 204 | struct CborValue root_value; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 205 | struct CborValue value; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 206 | uint8_t img_data[512]; |
| 207 | long long int off = UINT_MAX; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 208 | size_t img_blen = 0; |
Fabio Utzig | 30f6b2a | 2018-03-29 16:18:53 -0300 | [diff] [blame] | 209 | uint8_t rem_bytes; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 210 | long long int data_len = UINT_MAX; |
| 211 | size_t slen; |
| 212 | char name_str[8]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 213 | const struct flash_area *fap = NULL; |
| 214 | int rc; |
| 215 | |
| 216 | memset(img_data, 0, sizeof(img_data)); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | * Expected data format. |
| 220 | * { |
| 221 | * "data":<img_data> |
| 222 | * "len":<image len> |
| 223 | * "off":<current offset of image data> |
| 224 | * } |
| 225 | */ |
| 226 | |
| 227 | /* |
| 228 | * Object comes within { ... } |
| 229 | */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 230 | cbor_buf_reader_init(&reader, (uint8_t *)buf, len); |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 231 | #ifdef __ZEPHYR__ |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 232 | cbor_parser_cust_reader_init(&reader.r, 0, &parser, &root_value); |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 233 | #else |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 234 | cbor_parser_init(&reader.r, 0, &parser, &root_value); |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 235 | #endif |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 236 | if (!cbor_value_is_container(&root_value)) { |
| 237 | goto out_invalid_data; |
| 238 | } |
| 239 | if (cbor_value_enter_container(&root_value, &value)) { |
| 240 | goto out_invalid_data; |
| 241 | } |
| 242 | while (cbor_value_is_valid(&value)) { |
| 243 | /* |
| 244 | * Decode key. |
| 245 | */ |
| 246 | if (cbor_value_calculate_string_length(&value, &slen)) { |
| 247 | goto out_invalid_data; |
| 248 | } |
| 249 | if (!cbor_value_is_text_string(&value) || |
| 250 | slen >= sizeof(name_str) - 1) { |
| 251 | goto out_invalid_data; |
| 252 | } |
| 253 | if (cbor_value_copy_text_string(&value, name_str, &slen, &value)) { |
| 254 | goto out_invalid_data; |
| 255 | } |
| 256 | name_str[slen] = '\0'; |
| 257 | if (!strcmp(name_str, "data")) { |
| 258 | /* |
| 259 | * Image data |
| 260 | */ |
| 261 | if (value.type != CborByteStringType) { |
| 262 | goto out_invalid_data; |
| 263 | } |
| 264 | if (cbor_value_calculate_string_length(&value, &slen) || |
| 265 | slen >= sizeof(img_data)) { |
| 266 | goto out_invalid_data; |
| 267 | } |
| 268 | if (cbor_value_copy_byte_string(&value, img_data, &slen, &value)) { |
| 269 | goto out_invalid_data; |
| 270 | } |
| 271 | img_blen = slen; |
| 272 | } else if (!strcmp(name_str, "off")) { |
| 273 | /* |
| 274 | * Offset of the data. |
| 275 | */ |
| 276 | if (value.type != CborIntegerType) { |
| 277 | goto out_invalid_data; |
| 278 | } |
| 279 | if (cbor_value_get_int64(&value, &off)) { |
| 280 | goto out_invalid_data; |
| 281 | } |
| 282 | if (cbor_value_advance(&value)) { |
| 283 | goto out_invalid_data; |
| 284 | } |
| 285 | } else if (!strcmp(name_str, "len")) { |
| 286 | /* |
| 287 | * Length of the image. This should only be present in the first |
| 288 | * block of data; when offset is 0. |
| 289 | */ |
| 290 | if (value.type != CborIntegerType) { |
| 291 | goto out_invalid_data; |
| 292 | } |
| 293 | if (cbor_value_get_int64(&value, &data_len)) { |
| 294 | goto out_invalid_data; |
| 295 | } |
| 296 | if (cbor_value_advance(&value)) { |
| 297 | goto out_invalid_data; |
| 298 | } |
| 299 | } else { |
| 300 | /* |
| 301 | * Unknown keys. |
| 302 | */ |
| 303 | if (cbor_value_advance(&value)) { |
| 304 | goto out_invalid_data; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | if (off == UINT_MAX) { |
| 309 | /* |
| 310 | * Offset must be set in every block. |
| 311 | */ |
| 312 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 315 | rc = flash_area_open(flash_area_id_from_image_slot(0), &fap); |
| 316 | if (rc) { |
| 317 | rc = MGMT_ERR_EINVAL; |
| 318 | goto out; |
| 319 | } |
| 320 | |
| 321 | if (off == 0) { |
| 322 | curr_off = 0; |
| 323 | if (data_len > fap->fa_size) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 324 | goto out_invalid_data; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 325 | } |
| 326 | rc = flash_area_erase(fap, 0, fap->fa_size); |
| 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 | } |
| 330 | img_size = data_len; |
| 331 | } |
| 332 | if (off != curr_off) { |
| 333 | rc = 0; |
| 334 | goto out; |
| 335 | } |
Fabio Utzig | 30f6b2a | 2018-03-29 16:18:53 -0300 | [diff] [blame] | 336 | if (curr_off + img_blen < img_size) { |
| 337 | rem_bytes = img_blen % flash_area_align(fap); |
| 338 | if (rem_bytes) { |
| 339 | img_blen -= rem_bytes; |
| 340 | } |
| 341 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 342 | rc = flash_area_write(fap, curr_off, img_data, img_blen); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 343 | if (rc == 0) { |
| 344 | curr_off += img_blen; |
| 345 | } else { |
| 346 | out_invalid_data: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 347 | rc = MGMT_ERR_EINVAL; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 348 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 349 | out: |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 350 | BOOT_LOG_INF("RX: 0x%x", rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 351 | cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength); |
| 352 | cbor_encode_text_stringz(&bs_rsp, "rc"); |
| 353 | cbor_encode_int(&bs_rsp, rc); |
| 354 | if (rc == 0) { |
| 355 | cbor_encode_text_stringz(&bs_rsp, "off"); |
| 356 | cbor_encode_uint(&bs_rsp, curr_off); |
| 357 | } |
| 358 | cbor_encoder_close_container(&bs_root, &bs_rsp); |
| 359 | |
| 360 | boot_serial_output(); |
| 361 | flash_area_close(fap); |
| 362 | } |
| 363 | |
| 364 | /* |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 365 | * Console echo control/image erase. Send empty response, don't do anything. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 366 | */ |
| 367 | static void |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 368 | bs_empty_rsp(char *buf, int len) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 369 | { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 370 | cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength); |
| 371 | cbor_encode_text_stringz(&bs_rsp, "rc"); |
| 372 | cbor_encode_int(&bs_rsp, 0); |
| 373 | cbor_encoder_close_container(&bs_root, &bs_rsp); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 374 | boot_serial_output(); |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Reset, and (presumably) boot to newly uploaded image. Flush console |
| 379 | * before restarting. |
| 380 | */ |
Andrzej Puzdrowski | 268cdd0 | 2018-04-10 12:57:54 +0200 | [diff] [blame] | 381 | static void |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 382 | bs_reset(char *buf, int len) |
| 383 | { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 384 | bs_empty_rsp(buf, len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 385 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 386 | #ifdef __ZEPHYR__ |
| 387 | k_sleep(250); |
| 388 | sys_reboot(SYS_REBOOT_COLD); |
| 389 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 390 | os_cputime_delay_usecs(250000); |
| 391 | hal_system_reset(); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 392 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Parse incoming line of input from console. |
| 397 | * Expect newtmgr protocol with serial transport. |
| 398 | */ |
| 399 | void |
| 400 | boot_serial_input(char *buf, int len) |
| 401 | { |
| 402 | struct nmgr_hdr *hdr; |
| 403 | |
| 404 | hdr = (struct nmgr_hdr *)buf; |
| 405 | if (len < sizeof(*hdr) || |
| 406 | (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) || |
| 407 | (ntohs(hdr->nh_len) < len - sizeof(*hdr))) { |
| 408 | return; |
| 409 | } |
| 410 | bs_hdr = hdr; |
| 411 | hdr->nh_group = ntohs(hdr->nh_group); |
| 412 | |
| 413 | buf += sizeof(*hdr); |
| 414 | len -= sizeof(*hdr); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 415 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 416 | bs_writer.bytes_written = 0; |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 417 | #ifdef __ZEPHYR__ |
| 418 | cbor_encoder_cust_writer_init(&bs_root, &bs_writer, 0); |
| 419 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 420 | cbor_encoder_init(&bs_root, &bs_writer, 0); |
Andrzej Puzdrowski | 386b592 | 2018-04-06 19:26:24 +0200 | [diff] [blame] | 421 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * Limited support for commands. |
| 425 | */ |
| 426 | if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) { |
| 427 | switch (hdr->nh_id) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 428 | case IMGMGR_NMGR_ID_STATE: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 429 | bs_list(buf, len); |
| 430 | break; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 431 | case IMGMGR_NMGR_ID_UPLOAD: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 432 | bs_upload(buf, len); |
| 433 | break; |
| 434 | default: |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 435 | bs_empty_rsp(buf, len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 436 | break; |
| 437 | } |
| 438 | } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) { |
| 439 | switch (hdr->nh_id) { |
| 440 | case NMGR_ID_CONS_ECHO_CTRL: |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 441 | bs_empty_rsp(buf, len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 442 | break; |
| 443 | case NMGR_ID_RESET: |
| 444 | bs_reset(buf, len); |
| 445 | break; |
| 446 | default: |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | static void |
| 453 | boot_serial_output(void) |
| 454 | { |
| 455 | char *data; |
| 456 | int len; |
| 457 | uint16_t crc; |
| 458 | uint16_t totlen; |
| 459 | char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 }; |
| 460 | char buf[BOOT_SERIAL_OUT_MAX]; |
| 461 | char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)]; |
| 462 | |
| 463 | data = bs_obuf; |
| 464 | len = bs_writer.bytes_written; |
| 465 | |
| 466 | bs_hdr->nh_op++; |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 467 | bs_hdr->nh_flags = 0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 468 | bs_hdr->nh_len = htons(len); |
| 469 | bs_hdr->nh_group = htons(bs_hdr->nh_group); |
| 470 | |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 471 | #ifdef __ZEPHYR__ |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 472 | crc = crc16((u8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL, |
| 473 | CRC16_INITIAL_CRC, false); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 474 | crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true); |
| 475 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 476 | crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr)); |
| 477 | crc = crc16_ccitt(crc, data, len); |
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 | crc = htons(crc); |
| 480 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 481 | boot_uf->write(pkt_start, sizeof(pkt_start)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 482 | |
| 483 | totlen = len + sizeof(*bs_hdr) + sizeof(crc); |
| 484 | totlen = htons(totlen); |
| 485 | |
| 486 | memcpy(buf, &totlen, sizeof(totlen)); |
| 487 | totlen = sizeof(totlen); |
| 488 | memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr)); |
| 489 | totlen += sizeof(*bs_hdr); |
| 490 | memcpy(&buf[totlen], data, len); |
| 491 | totlen += len; |
| 492 | memcpy(&buf[totlen], &crc, sizeof(crc)); |
| 493 | totlen += sizeof(crc); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 494 | #ifdef __ZEPHYR__ |
| 495 | size_t enc_len; |
Carles Cufi | 0165be8 | 2018-03-26 17:43:51 +0200 | [diff] [blame] | 496 | base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 497 | totlen = enc_len; |
| 498 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 499 | totlen = base64_encode(buf, totlen, encoded_buf, 1); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 500 | #endif |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 501 | boot_uf->write(encoded_buf, totlen); |
| 502 | boot_uf->write("\n\r", 2); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 503 | BOOT_LOG_INF("TX"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Returns 1 if full packet has been received. |
| 508 | */ |
| 509 | static int |
| 510 | boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout) |
| 511 | { |
| 512 | int rc; |
| 513 | uint16_t crc; |
| 514 | uint16_t len; |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 515 | #ifdef __ZEPHYR__ |
| 516 | int err; |
Carles Cufi | 0165be8 | 2018-03-26 17:43:51 +0200 | [diff] [blame] | 517 | err = base64_decode( &out[*out_off], maxout, &rc, in, inlen - 2); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 518 | if (err) { |
| 519 | return -1; |
| 520 | } |
| 521 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 522 | if (*out_off + base64_decode_len(in) >= maxout) { |
| 523 | return -1; |
| 524 | } |
| 525 | rc = base64_decode(in, &out[*out_off]); |
| 526 | if (rc < 0) { |
| 527 | return -1; |
| 528 | } |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 529 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 530 | *out_off += rc; |
| 531 | |
| 532 | if (*out_off > sizeof(uint16_t)) { |
| 533 | len = ntohs(*(uint16_t *)out); |
| 534 | |
| 535 | len = min(len, *out_off - sizeof(uint16_t)); |
| 536 | out += sizeof(uint16_t); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 537 | #ifdef __ZEPHYR__ |
| 538 | crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true); |
| 539 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 540 | crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len); |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 541 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 542 | if (crc || len <= sizeof(crc)) { |
| 543 | return 0; |
| 544 | } |
| 545 | *out_off -= sizeof(crc); |
| 546 | out[*out_off] = '\0'; |
| 547 | |
| 548 | return 1; |
| 549 | } |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Task which waits reading console, expecting to get image over |
| 555 | * serial port. |
| 556 | */ |
| 557 | void |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 558 | boot_serial_start(const struct boot_uart_funcs *f) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 559 | { |
| 560 | int rc; |
| 561 | int off; |
| 562 | char *buf; |
| 563 | char *dec; |
| 564 | int dec_off; |
| 565 | int full_line; |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 566 | int max_input; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 567 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 568 | boot_uf = f; |
Andrzej Puzdrowski | 8e96b83 | 2017-09-08 16:49:14 +0200 | [diff] [blame] | 569 | buf = in_buf; |
| 570 | dec = dec_buf; |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 571 | max_input = sizeof(in_buf); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 572 | |
| 573 | off = 0; |
| 574 | while (1) { |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 575 | rc = f->read(buf + off, sizeof(in_buf) - off, &full_line); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 576 | if (rc <= 0 && !full_line) { |
| 577 | continue; |
| 578 | } |
| 579 | off += rc; |
| 580 | if (!full_line) { |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 581 | if (off == max_input) { |
| 582 | /* |
| 583 | * Full line, no newline yet. Reset the input buffer. |
| 584 | */ |
| 585 | off = 0; |
| 586 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 587 | continue; |
| 588 | } |
| 589 | if (buf[0] == SHELL_NLIP_PKT_START1 && |
| 590 | buf[1] == SHELL_NLIP_PKT_START2) { |
| 591 | dec_off = 0; |
| 592 | rc = boot_serial_in_dec(&buf[2], off - 2, dec, &dec_off, max_input); |
| 593 | } else if (buf[0] == SHELL_NLIP_DATA_START1 && |
| 594 | buf[1] == SHELL_NLIP_DATA_START2) { |
| 595 | rc = boot_serial_in_dec(&buf[2], off - 2, dec, &dec_off, max_input); |
| 596 | } |
| 597 | if (rc == 1) { |
| 598 | boot_serial_input(&dec[2], dec_off - 2); |
| 599 | } |
| 600 | off = 0; |
| 601 | } |
| 602 | } |