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