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