blob: c1d2a1e57afee04ad1eb32e7dd5052f78ff5119b [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
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 Utzig1a2e41a2017-11-17 12:13:09 -020027#include "bootutil/bootutil_log.h"
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +010028#include "cbor_encode.h"
Fabio Utzig1a2e41a2017-11-17 12:13:09 -020029
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020030#ifdef __ZEPHYR__
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +020031#include <power/reboot.h>
Andrzej Puzdrowskif1d189c2019-12-12 09:34:11 +010032#include <sys/byteorder.h>
33#include <sys/__assert.h>
Peter Bigot54c1e3f2020-01-25 05:50:12 -060034#include <drivers/flash.h>
Andrzej Puzdrowskif1d189c2019-12-12 09:34:11 +010035#include <sys/crc.h>
36#include <sys/base64.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020037#else
Christopher Collins92ea77f2016-12-12 15:59:26 -080038#include <bsp/bsp.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include <hal/hal_system.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080040#include <os/endian.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080041#include <os/os_cputime.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020042#include <crc/crc16.h>
43#include <base64/base64.h>
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +020044#endif /* __ZEPHYR__ */
45
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020046#include <flash_map_backend/flash_map_backend.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020047#include <hal/hal_flash.h>
48#include <os/os.h>
49#include <os/os_malloc.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080050
51#include <bootutil/image.h>
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +020052#include <bootutil/bootutil.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080053
54#include "boot_serial/boot_serial.h"
55#include "boot_serial_priv.h"
56
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +020057#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
58#include "bootutil_priv.h"
59#endif
60
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +010061#include "serial_recovery_cbor.h"
62
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010063MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
64
Marko Kiiskila149b4572018-06-06 14:18:54 +030065#define BOOT_SERIAL_INPUT_MAX 512
Fabio Utzig6f49c272019-08-23 11:42:58 -030066#define BOOT_SERIAL_OUT_MAX 128
Christopher Collins92ea77f2016-12-12 15:59:26 -080067
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020068#ifdef __ZEPHYR__
Carles Cufi0165be82018-03-26 17:43:51 +020069/* base64 lib encodes data to null-terminated string */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020070#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 Puzdrowski8e96b832017-09-08 16:49:14 +020077#endif
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010078
Fabio Utzig6f49c272019-08-23 11:42:58 -030079#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 Kiiskila149b4572018-06-06 14:18:54 +030089static char in_buf[BOOT_SERIAL_INPUT_MAX + 1];
90static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1];
Marko Kiiskila8b1ce3a2018-06-14 13:20:46 -070091const struct boot_uart_funcs *boot_uf;
Christopher Collins92ea77f2016-12-12 15:59:26 -080092static uint32_t curr_off;
93static uint32_t img_size;
94static struct nmgr_hdr *bs_hdr;
95
96static char bs_obuf[BOOT_SERIAL_OUT_MAX];
97
Christopher Collins92ea77f2016-12-12 15:59:26 -080098static void boot_serial_output(void);
99
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100100static cbor_state_backups_t dummy_backups;
101static cbor_state_t cbor_state = {
102 .backups = &dummy_backups
Christopher Collins92ea77f2016-12-12 15:59:26 -0800103};
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105
106/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300107 * Convert version into string without use of snprintf().
Christopher Collins92ea77f2016-12-12 15:59:26 -0800108 */
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300109static int
110u32toa(char *tgt, uint32_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300112 char *dst;
113 uint32_t d = 1;
114 uint32_t dgt;
115 int n = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800116
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300117 dst = tgt;
118 while (val / d >= 10) {
119 d *= 10;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300121 while (d) {
122 dgt = val / d;
123 val %= d;
124 d /= 10;
125 if (n || dgt > 0 || d == 0) {
126 *dst++ = dgt + '0';
127 ++n;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800128 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800129 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300130 *dst = '\0';
131
132 return dst - tgt;
133}
134
135/*
136 * dst has to be able to fit "255.255.65535.4294967295" (25 characters).
137 */
138static void
139bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
140{
141 int off;
142
143 off = u32toa(dst, ver->iv_major);
144 dst[off++] = '.';
145 off += u32toa(dst + off, ver->iv_minor);
146 dst[off++] = '.';
147 off += u32toa(dst + off, ver->iv_revision);
148 dst[off++] = '.';
149 off += u32toa(dst + off, ver->iv_build_num);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800150}
151
152/*
153 * List images.
154 */
155static void
156bs_list(char *buf, int len)
157{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158 struct image_header hdr;
159 uint8_t tmpbuf[64];
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100160 uint32_t slot, area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800161 const struct flash_area *fap;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300162 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100164 map_start_encode(&cbor_state, 1);
165 tstrx_put(&cbor_state, "images");
166 list_start_encode(&cbor_state, 5);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300167 image_index = 0;
168 IMAGES_ITER(image_index) {
169 for (slot = 0; slot < 2; slot++) {
170 area_id = flash_area_id_from_multi_image_slot(image_index, slot);
171 if (flash_area_open(area_id, &fap)) {
172 continue;
173 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800174
Fabio Utzig6f49c272019-08-23 11:42:58 -0300175 flash_area_read(fap, 0, &hdr, sizeof(hdr));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176
Fabio Utzig6f49c272019-08-23 11:42:58 -0300177 if (hdr.ih_magic != IMAGE_MAGIC ||
178 bootutil_img_validate(NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
179 NULL, 0, NULL)) {
180 flash_area_close(fap);
181 continue;
182 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183 flash_area_close(fap);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300184
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100185 map_start_encode(&cbor_state, 20);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300186
187#if (BOOT_IMAGE_NUMBER > 1)
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100188 tstrx_put(&cbor_state, "image");
189 uintx32_put(&cbor_state, image_index);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300190#endif
191
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100192 tstrx_put(&cbor_state, "slot");
193 uintx32_put(&cbor_state, slot);
194 tstrx_put(&cbor_state, "version");
Fabio Utzig6f49c272019-08-23 11:42:58 -0300195
196 bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100197 tstrx_put_term(&cbor_state, (char *)tmpbuf);
198 map_end_encode(&cbor_state, 20);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800199 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800200 }
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100201 list_end_encode(&cbor_state, 5);
202 map_end_encode(&cbor_state, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800203 boot_serial_output();
204}
205
206/*
207 * Image upload request.
208 */
209static void
210bs_upload(char *buf, int len)
211{
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100212 const uint8_t *img_data = NULL;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100213 long long int off = UINT64_MAX;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800214 size_t img_blen = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300215 uint8_t rem_bytes;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100216 long long int data_len = UINT64_MAX;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300217 int img_num;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300218 size_t slen;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800219 const struct flash_area *fap = NULL;
220 int rc;
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200221#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
222 static off_t off_last = -1;
223 struct flash_sector sector;
224#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800225
Fabio Utzig6f49c272019-08-23 11:42:58 -0300226 img_num = 0;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300227
228 /*
229 * Expected data format.
230 * {
Fabio Utzig6f49c272019-08-23 11:42:58 -0300231 * "image":<image number in a multi-image set (OPTIONAL)>
232 * "data":<image data>
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300233 * "len":<image len>
234 * "off":<current offset of image data>
235 * }
236 */
237
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200238 struct Upload upload;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100239 uint32_t decoded_len;
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200240 bool result = cbor_decode_Upload((const uint8_t *)buf, len, &upload, &decoded_len);
241
242 if (!result || (len != decoded_len)) {
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100243 goto out_invalid_data;
244 }
Dominik Ermel470e2f32020-01-10 13:28:48 +0000245
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100246 for (int i = 0; i < upload._Upload_members_count; i++) {
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200247 struct Member_ *member = &upload._Upload_members[i];
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100248 switch(member->_Member_choice) {
249 case _Member_image:
250 img_num = member->_Member_image;
251 break;
252 case _Member_data:
253 img_data = member->_Member_data.value;
254 slen = member->_Member_data.len;
255 img_blen = slen;
256 break;
257 case _Member_len:
258 data_len = member->_Member_len;
259 break;
260 case _Member_off:
261 off = member->_Member_off;
262 break;
263 case _Member_sha:
264 default:
265 /* Nothing to do. */
266 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300267 }
268 }
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100269
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100270 if (off == UINT64_MAX || img_data == NULL) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300271 /*
272 * Offset must be set in every block.
273 */
274 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800275 }
276
Fabio Utzig6f49c272019-08-23 11:42:58 -0300277 rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800278 if (rc) {
279 rc = MGMT_ERR_EINVAL;
280 goto out;
281 }
282
283 if (off == 0) {
284 curr_off = 0;
285 if (data_len > fap->fa_size) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300286 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800287 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200288#ifndef CONFIG_BOOT_ERASE_PROGRESSIVELY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289 rc = flash_area_erase(fap, 0, fap->fa_size);
290 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300291 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200293#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294 img_size = data_len;
295 }
296 if (off != curr_off) {
297 rc = 0;
298 goto out;
299 }
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200300
301 if (curr_off + img_blen > img_size) {
302 rc = MGMT_ERR_EINVAL;
303 goto out;
304 }
305
306 rem_bytes = img_blen % flash_area_align(fap);
307
308 if ((curr_off + img_blen < img_size) && rem_bytes) {
309 img_blen -= rem_bytes;
310 rem_bytes = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300311 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200312
313#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
314 rc = flash_area_sector_from_off(curr_off + img_blen, &sector);
315 if (rc) {
316 BOOT_LOG_ERR("Unable to determine flash sector size");
317 goto out;
318 }
319 if (off_last != sector.fs_off) {
320 off_last = sector.fs_off;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200321 BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off);
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200322 rc = flash_area_erase(fap, sector.fs_off, sector.fs_size);
323 if (rc) {
324 BOOT_LOG_ERR("Error %d while erasing sector", rc);
325 goto out;
326 }
327 }
328#endif
329
330 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_blen);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200331 if (rem_bytes) {
332 /* the last chunk of the image might be unaligned */
333 uint8_t wbs_aligned[BOOT_MAX_ALIGN];
334 size_t w_size = img_blen - rem_bytes;
335
336 if (w_size) {
337 rc = flash_area_write(fap, curr_off, img_data, w_size);
338 if (rc) {
339 goto out_invalid_data;
340 }
341 curr_off += w_size;
342 img_blen -= w_size;
343 img_data += w_size;
344 }
345
346 if (img_blen) {
347 memcpy(wbs_aligned, img_data, rem_bytes);
348 memset(wbs_aligned + rem_bytes, flash_area_erased_val(fap),
349 sizeof(wbs_aligned) - rem_bytes);
350 rc = flash_area_write(fap, curr_off, wbs_aligned, flash_area_align(fap));
351 }
352
353 } else {
354 rc = flash_area_write(fap, curr_off, img_data, img_blen);
355 }
356
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300357 if (rc == 0) {
358 curr_off += img_blen;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200359#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
360 if (curr_off == img_size) {
361 /* get the last sector offset */
362 rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
363 if (rc) {
364 BOOT_LOG_ERR("Unable to determine flash sector of"
365 "the image trailer");
366 goto out;
367 }
368 /* Assure that sector for image trailer was erased. */
369 /* Check whether it was erased during previous upload. */
370 if (off_last < sector.fs_off) {
371 BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off);
372 rc = flash_area_erase(fap, sector.fs_off, sector.fs_size);
373 if (rc) {
374 BOOT_LOG_ERR("Error %d while erasing sector", rc);
375 goto out;
376 }
377 }
378 }
379#endif
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300380 } else {
381 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200384
Christopher Collins92ea77f2016-12-12 15:59:26 -0800385out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200386 BOOT_LOG_INF("RX: 0x%x", rc);
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100387 map_start_encode(&cbor_state, 10);
388 tstrx_put(&cbor_state, "rc");
389 uintx32_put(&cbor_state, rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800390 if (rc == 0) {
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100391 tstrx_put(&cbor_state, "off");
392 uintx32_put(&cbor_state, curr_off);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800393 }
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100394 map_end_encode(&cbor_state, 10);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800395
396 boot_serial_output();
397 flash_area_close(fap);
398}
399
400/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300401 * Console echo control/image erase. Send empty response, don't do anything.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800402 */
403static void
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300404bs_empty_rsp(char *buf, int len)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800405{
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100406 map_start_encode(&cbor_state, 10);
407 tstrx_put(&cbor_state, "rc");
408 uintx32_put(&cbor_state, 0);
409 map_end_encode(&cbor_state, 10);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410 boot_serial_output();
411}
412
413/*
414 * Reset, and (presumably) boot to newly uploaded image. Flush console
415 * before restarting.
416 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200417static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800418bs_reset(char *buf, int len)
419{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300420 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800421
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200422#ifdef __ZEPHYR__
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200423#ifdef CONFIG_MULTITHREADING
Carles Cufi7e7b4ad2020-03-30 19:12:02 +0200424 k_sleep(K_MSEC(250));
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200425#else
426 k_busy_wait(250000);
427#endif
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200428 sys_reboot(SYS_REBOOT_COLD);
429#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800430 os_cputime_delay_usecs(250000);
431 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200432#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800433}
434
435/*
436 * Parse incoming line of input from console.
437 * Expect newtmgr protocol with serial transport.
438 */
439void
440boot_serial_input(char *buf, int len)
441{
442 struct nmgr_hdr *hdr;
443
444 hdr = (struct nmgr_hdr *)buf;
445 if (len < sizeof(*hdr) ||
446 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
447 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
448 return;
449 }
450 bs_hdr = hdr;
451 hdr->nh_group = ntohs(hdr->nh_group);
452
453 buf += sizeof(*hdr);
454 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300455
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100456 cbor_state.payload_mut = (uint8_t *)bs_obuf;
457 cbor_state.payload_end = (const uint8_t *)bs_obuf
458 + sizeof(bs_obuf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800459
460 /*
461 * Limited support for commands.
462 */
463 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
464 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300465 case IMGMGR_NMGR_ID_STATE:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800466 bs_list(buf, len);
467 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300468 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800469 bs_upload(buf, len);
470 break;
471 default:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300472 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473 break;
474 }
475 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
476 switch (hdr->nh_id) {
477 case NMGR_ID_CONS_ECHO_CTRL:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300478 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800479 break;
480 case NMGR_ID_RESET:
481 bs_reset(buf, len);
482 break;
483 default:
484 break;
485 }
486 }
487}
488
489static void
490boot_serial_output(void)
491{
492 char *data;
493 int len;
494 uint16_t crc;
495 uint16_t totlen;
496 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
497 char buf[BOOT_SERIAL_OUT_MAX];
498 char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)];
499
500 data = bs_obuf;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100501 len = (uint32_t)cbor_state.payload_mut - (uint32_t)bs_obuf;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800502
503 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300504 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800505 bs_hdr->nh_len = htons(len);
506 bs_hdr->nh_group = htons(bs_hdr->nh_group);
507
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200508#ifdef __ZEPHYR__
Kumar Gala0813efe2020-05-27 12:25:41 -0500509 crc = crc16((uint8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL,
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300510 CRC16_INITIAL_CRC, false);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200511 crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true);
512#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800513 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
514 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200515#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800516 crc = htons(crc);
517
Marko Kiiskila149b4572018-06-06 14:18:54 +0300518 boot_uf->write(pkt_start, sizeof(pkt_start));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800519
520 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
521 totlen = htons(totlen);
522
523 memcpy(buf, &totlen, sizeof(totlen));
524 totlen = sizeof(totlen);
525 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
526 totlen += sizeof(*bs_hdr);
527 memcpy(&buf[totlen], data, len);
528 totlen += len;
529 memcpy(&buf[totlen], &crc, sizeof(crc));
530 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200531#ifdef __ZEPHYR__
532 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +0200533 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200534 totlen = enc_len;
535#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800536 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200537#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +0300538 boot_uf->write(encoded_buf, totlen);
539 boot_uf->write("\n\r", 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200540 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800541}
542
543/*
544 * Returns 1 if full packet has been received.
545 */
546static int
547boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
548{
549 int rc;
550 uint16_t crc;
551 uint16_t len;
Marko Kiiskilae5aeee42018-12-21 15:00:16 +0200552
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200553#ifdef __ZEPHYR__
554 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200555 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200556 if (err) {
557 return -1;
558 }
559#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560 if (*out_off + base64_decode_len(in) >= maxout) {
561 return -1;
562 }
563 rc = base64_decode(in, &out[*out_off]);
564 if (rc < 0) {
565 return -1;
566 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200567#endif
Fabio Utzig6f49c272019-08-23 11:42:58 -0300568
Christopher Collins92ea77f2016-12-12 15:59:26 -0800569 *out_off += rc;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300570 if (*out_off <= sizeof(uint16_t)) {
571 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800572 }
Fabio Utzig6f49c272019-08-23 11:42:58 -0300573
574 len = ntohs(*(uint16_t *)out);
575 if (len != *out_off - sizeof(uint16_t)) {
576 return 0;
577 }
578
579 if (len > *out_off - sizeof(uint16_t)) {
580 len = *out_off - sizeof(uint16_t);
581 }
582
583 out += sizeof(uint16_t);
584#ifdef __ZEPHYR__
585 crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true);
586#else
587 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
588#endif
589 if (crc || len <= sizeof(crc)) {
590 return 0;
591 }
592 *out_off -= sizeof(crc);
593 out[*out_off] = '\0';
594
595 return 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596}
597
598/*
599 * Task which waits reading console, expecting to get image over
600 * serial port.
601 */
602void
Marko Kiiskila149b4572018-06-06 14:18:54 +0300603boot_serial_start(const struct boot_uart_funcs *f)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800604{
605 int rc;
606 int off;
David Brown57f0df32020-05-12 08:39:21 -0600607 int dec_off = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800608 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300609 int max_input;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800610
Marko Kiiskila149b4572018-06-06 14:18:54 +0300611 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300612 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800613
614 off = 0;
615 while (1) {
Andrzej Puzdrowskiaea38eb2021-06-11 12:28:59 +0200616 MCUBOOT_CPU_IDLE();
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200617 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800618 if (rc <= 0 && !full_line) {
619 continue;
620 }
621 off += rc;
622 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300623 if (off == max_input) {
624 /*
625 * Full line, no newline yet. Reset the input buffer.
626 */
627 off = 0;
628 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800629 continue;
630 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200631 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
632 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800633 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200634 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
635 } else if (in_buf[0] == SHELL_NLIP_DATA_START1 &&
636 in_buf[1] == SHELL_NLIP_DATA_START2) {
637 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800638 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200639
640 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200642 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800643 }
644 off = 0;
645 }
646}