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