blob: 5fa794a152d19e25b301777d025d0b1da4621928 [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"
28
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020029#ifdef __ZEPHYR__
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +020030#include <power/reboot.h>
Andrzej Puzdrowskif1d189c2019-12-12 09:34:11 +010031#include <sys/byteorder.h>
32#include <sys/__assert.h>
Peter Bigot54c1e3f2020-01-25 05:50:12 -060033#include <drivers/flash.h>
Andrzej Puzdrowskif1d189c2019-12-12 09:34:11 +010034#include <sys/crc.h>
35#include <sys/base64.h>
Dominik Ermel470e2f32020-01-10 13:28:48 +000036#include <tinycbor/cbor.h>
37#include <tinycbor/cbor_buf_reader.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020038#else
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include <bsp/bsp.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080040#include <hal/hal_system.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080041#include <os/endian.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080042#include <os/os_cputime.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020043#include <crc/crc16.h>
44#include <base64/base64.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080045#include <tinycbor/cbor.h>
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +020046#endif /* __ZEPHYR__ */
47
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020048#include <flash_map_backend/flash_map_backend.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020049#include <hal/hal_flash.h>
50#include <os/os.h>
51#include <os/os_malloc.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080052
53#include <bootutil/image.h>
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +020054#include <bootutil/bootutil.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080055
56#include "boot_serial/boot_serial.h"
57#include "boot_serial_priv.h"
58
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +020059#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
60#include "bootutil_priv.h"
61#endif
62
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +010063#include "serial_recovery_cbor.h"
64
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010065MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
66
Marko Kiiskila149b4572018-06-06 14:18:54 +030067#define BOOT_SERIAL_INPUT_MAX 512
Fabio Utzig6f49c272019-08-23 11:42:58 -030068#define BOOT_SERIAL_OUT_MAX 128
Christopher Collins92ea77f2016-12-12 15:59:26 -080069
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020070#ifdef __ZEPHYR__
Carles Cufi0165be82018-03-26 17:43:51 +020071/* base64 lib encodes data to null-terminated string */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020072#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 Puzdrowski8e96b832017-09-08 16:49:14 +020079#endif
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010080
Fabio Utzig6f49c272019-08-23 11:42:58 -030081#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 Kiiskila149b4572018-06-06 14:18:54 +030091static char in_buf[BOOT_SERIAL_INPUT_MAX + 1];
92static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1];
Marko Kiiskila8b1ce3a2018-06-14 13:20:46 -070093const struct boot_uart_funcs *boot_uf;
Christopher Collins92ea77f2016-12-12 15:59:26 -080094static uint32_t curr_off;
95static uint32_t img_size;
96static struct nmgr_hdr *bs_hdr;
97
98static char bs_obuf[BOOT_SERIAL_OUT_MAX];
99
100static int bs_cbor_writer(struct cbor_encoder_writer *, const char *data,
101 int len);
102static void boot_serial_output(void);
103
104static struct cbor_encoder_writer bs_writer = {
105 .write = bs_cbor_writer
106};
107static CborEncoder bs_root;
108static CborEncoder bs_rsp;
109
110int
111bs_cbor_writer(struct cbor_encoder_writer *cew, const char *data, int len)
112{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300113 if (cew->bytes_written + len > sizeof(bs_obuf)) {
114 return CborErrorOutOfMemory;
115 }
116
Christopher Collins92ea77f2016-12-12 15:59:26 -0800117 memcpy(&bs_obuf[cew->bytes_written], data, len);
118 cew->bytes_written += len;
119
120 return 0;
121}
122
123/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300124 * Convert version into string without use of snprintf().
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125 */
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300126static int
127u32toa(char *tgt, uint32_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800128{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300129 char *dst;
130 uint32_t d = 1;
131 uint32_t dgt;
132 int n = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800133
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300134 dst = tgt;
135 while (val / d >= 10) {
136 d *= 10;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800137 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300138 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 Collins92ea77f2016-12-12 15:59:26 -0800145 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800146 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300147 *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 */
155static void
156bs_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 Collins92ea77f2016-12-12 15:59:26 -0800167}
168
169/*
170 * List images.
171 */
172static void
173bs_list(char *buf, int len)
174{
175 CborEncoder images;
176 CborEncoder image;
177 struct image_header hdr;
178 uint8_t tmpbuf[64];
Fabio Utzig6f49c272019-08-23 11:42:58 -0300179 int slot, area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180 const struct flash_area *fap;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300181 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182
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 Utzig6f49c272019-08-23 11:42:58 -0300186 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 Collins92ea77f2016-12-12 15:59:26 -0800193
Fabio Utzig6f49c272019-08-23 11:42:58 -0300194 flash_area_read(fap, 0, &hdr, sizeof(hdr));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800195
Fabio Utzig6f49c272019-08-23 11:42:58 -0300196 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 Collins92ea77f2016-12-12 15:59:26 -0800202 flash_area_close(fap);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300203
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 Collins92ea77f2016-12-12 15:59:26 -0800218 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800219 }
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 */
228static void
229bs_upload(char *buf, int len)
230{
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100231 const uint8_t *img_data = NULL;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300232 long long int off = UINT_MAX;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800233 size_t img_blen = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300234 uint8_t rem_bytes;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300235 long long int data_len = UINT_MAX;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300236 int img_num;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300237 size_t slen;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238 const struct flash_area *fap = NULL;
239 int rc;
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200240#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
241 static off_t off_last = -1;
242 struct flash_sector sector;
243#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800244
Fabio Utzig6f49c272019-08-23 11:42:58 -0300245 img_num = 0;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300246
247 /*
248 * Expected data format.
249 * {
Fabio Utzig6f49c272019-08-23 11:42:58 -0300250 * "image":<image number in a multi-image set (OPTIONAL)>
251 * "data":<image data>
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300252 * "len":<image len>
253 * "off":<current offset of image data>
254 * }
255 */
256
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200257 struct Upload upload;
258 size_t decoded_len;
259 bool result = cbor_decode_Upload((const uint8_t *)buf, len, &upload, &decoded_len);
260
261 if (!result || (len != decoded_len)) {
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100262 goto out_invalid_data;
263 }
Dominik Ermel470e2f32020-01-10 13:28:48 +0000264
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100265 for (int i = 0; i < upload._Upload_members_count; i++) {
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200266 struct Member_ *member = &upload._Upload_members[i];
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100267 switch(member->_Member_choice) {
268 case _Member_image:
269 img_num = member->_Member_image;
270 break;
271 case _Member_data:
272 img_data = member->_Member_data.value;
273 slen = member->_Member_data.len;
274 img_blen = slen;
275 break;
276 case _Member_len:
277 data_len = member->_Member_len;
278 break;
279 case _Member_off:
280 off = member->_Member_off;
281 break;
282 case _Member_sha:
283 default:
284 /* Nothing to do. */
285 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300286 }
287 }
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100288
289 if (off == UINT_MAX || img_data == NULL) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300290 /*
291 * Offset must be set in every block.
292 */
293 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294 }
295
Fabio Utzig6f49c272019-08-23 11:42:58 -0300296 rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800297 if (rc) {
298 rc = MGMT_ERR_EINVAL;
299 goto out;
300 }
301
302 if (off == 0) {
303 curr_off = 0;
304 if (data_len > fap->fa_size) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300305 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800306 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200307#ifndef CONFIG_BOOT_ERASE_PROGRESSIVELY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800308 rc = flash_area_erase(fap, 0, fap->fa_size);
309 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300310 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200312#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313 img_size = data_len;
314 }
315 if (off != curr_off) {
316 rc = 0;
317 goto out;
318 }
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200319
320 if (curr_off + img_blen > img_size) {
321 rc = MGMT_ERR_EINVAL;
322 goto out;
323 }
324
325 rem_bytes = img_blen % flash_area_align(fap);
326
327 if ((curr_off + img_blen < img_size) && rem_bytes) {
328 img_blen -= rem_bytes;
329 rem_bytes = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300330 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200331
332#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
333 rc = flash_area_sector_from_off(curr_off + img_blen, &sector);
334 if (rc) {
335 BOOT_LOG_ERR("Unable to determine flash sector size");
336 goto out;
337 }
338 if (off_last != sector.fs_off) {
339 off_last = sector.fs_off;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200340 BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off);
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200341 rc = flash_area_erase(fap, sector.fs_off, sector.fs_size);
342 if (rc) {
343 BOOT_LOG_ERR("Error %d while erasing sector", rc);
344 goto out;
345 }
346 }
347#endif
348
349 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_blen);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200350 if (rem_bytes) {
351 /* the last chunk of the image might be unaligned */
352 uint8_t wbs_aligned[BOOT_MAX_ALIGN];
353 size_t w_size = img_blen - rem_bytes;
354
355 if (w_size) {
356 rc = flash_area_write(fap, curr_off, img_data, w_size);
357 if (rc) {
358 goto out_invalid_data;
359 }
360 curr_off += w_size;
361 img_blen -= w_size;
362 img_data += w_size;
363 }
364
365 if (img_blen) {
366 memcpy(wbs_aligned, img_data, rem_bytes);
367 memset(wbs_aligned + rem_bytes, flash_area_erased_val(fap),
368 sizeof(wbs_aligned) - rem_bytes);
369 rc = flash_area_write(fap, curr_off, wbs_aligned, flash_area_align(fap));
370 }
371
372 } else {
373 rc = flash_area_write(fap, curr_off, img_data, img_blen);
374 }
375
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300376 if (rc == 0) {
377 curr_off += img_blen;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200378#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
379 if (curr_off == img_size) {
380 /* get the last sector offset */
381 rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
382 if (rc) {
383 BOOT_LOG_ERR("Unable to determine flash sector of"
384 "the image trailer");
385 goto out;
386 }
387 /* Assure that sector for image trailer was erased. */
388 /* Check whether it was erased during previous upload. */
389 if (off_last < sector.fs_off) {
390 BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off);
391 rc = flash_area_erase(fap, sector.fs_off, sector.fs_size);
392 if (rc) {
393 BOOT_LOG_ERR("Error %d while erasing sector", rc);
394 goto out;
395 }
396 }
397 }
398#endif
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300399 } else {
400 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800401 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800402 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200403
Christopher Collins92ea77f2016-12-12 15:59:26 -0800404out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200405 BOOT_LOG_INF("RX: 0x%x", rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800406 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
407 cbor_encode_text_stringz(&bs_rsp, "rc");
408 cbor_encode_int(&bs_rsp, rc);
409 if (rc == 0) {
410 cbor_encode_text_stringz(&bs_rsp, "off");
411 cbor_encode_uint(&bs_rsp, curr_off);
412 }
413 cbor_encoder_close_container(&bs_root, &bs_rsp);
414
415 boot_serial_output();
416 flash_area_close(fap);
417}
418
419/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300420 * Console echo control/image erase. Send empty response, don't do anything.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800421 */
422static void
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300423bs_empty_rsp(char *buf, int len)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300425 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
426 cbor_encode_text_stringz(&bs_rsp, "rc");
427 cbor_encode_int(&bs_rsp, 0);
428 cbor_encoder_close_container(&bs_root, &bs_rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800429 boot_serial_output();
430}
431
432/*
433 * Reset, and (presumably) boot to newly uploaded image. Flush console
434 * before restarting.
435 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200436static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800437bs_reset(char *buf, int len)
438{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300439 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800440
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200441#ifdef __ZEPHYR__
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200442#ifdef CONFIG_MULTITHREADING
Carles Cufi7e7b4ad2020-03-30 19:12:02 +0200443 k_sleep(K_MSEC(250));
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200444#else
445 k_busy_wait(250000);
446#endif
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200447 sys_reboot(SYS_REBOOT_COLD);
448#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449 os_cputime_delay_usecs(250000);
450 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200451#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452}
453
454/*
455 * Parse incoming line of input from console.
456 * Expect newtmgr protocol with serial transport.
457 */
458void
459boot_serial_input(char *buf, int len)
460{
461 struct nmgr_hdr *hdr;
462
463 hdr = (struct nmgr_hdr *)buf;
464 if (len < sizeof(*hdr) ||
465 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
466 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
467 return;
468 }
469 bs_hdr = hdr;
470 hdr->nh_group = ntohs(hdr->nh_group);
471
472 buf += sizeof(*hdr);
473 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300474
Christopher Collins92ea77f2016-12-12 15:59:26 -0800475 bs_writer.bytes_written = 0;
476 cbor_encoder_init(&bs_root, &bs_writer, 0);
477
478 /*
479 * Limited support for commands.
480 */
481 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
482 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300483 case IMGMGR_NMGR_ID_STATE:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484 bs_list(buf, len);
485 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300486 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800487 bs_upload(buf, len);
488 break;
489 default:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300490 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800491 break;
492 }
493 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
494 switch (hdr->nh_id) {
495 case NMGR_ID_CONS_ECHO_CTRL:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300496 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800497 break;
498 case NMGR_ID_RESET:
499 bs_reset(buf, len);
500 break;
501 default:
502 break;
503 }
504 }
505}
506
507static void
508boot_serial_output(void)
509{
510 char *data;
511 int len;
512 uint16_t crc;
513 uint16_t totlen;
514 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
515 char buf[BOOT_SERIAL_OUT_MAX];
516 char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)];
517
518 data = bs_obuf;
519 len = bs_writer.bytes_written;
520
521 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300522 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800523 bs_hdr->nh_len = htons(len);
524 bs_hdr->nh_group = htons(bs_hdr->nh_group);
525
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200526#ifdef __ZEPHYR__
Kumar Gala0813efe2020-05-27 12:25:41 -0500527 crc = crc16((uint8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL,
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300528 CRC16_INITIAL_CRC, false);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200529 crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true);
530#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800531 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
532 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200533#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800534 crc = htons(crc);
535
Marko Kiiskila149b4572018-06-06 14:18:54 +0300536 boot_uf->write(pkt_start, sizeof(pkt_start));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537
538 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
539 totlen = htons(totlen);
540
541 memcpy(buf, &totlen, sizeof(totlen));
542 totlen = sizeof(totlen);
543 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
544 totlen += sizeof(*bs_hdr);
545 memcpy(&buf[totlen], data, len);
546 totlen += len;
547 memcpy(&buf[totlen], &crc, sizeof(crc));
548 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200549#ifdef __ZEPHYR__
550 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +0200551 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200552 totlen = enc_len;
553#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800554 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200555#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +0300556 boot_uf->write(encoded_buf, totlen);
557 boot_uf->write("\n\r", 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200558 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800559}
560
561/*
562 * Returns 1 if full packet has been received.
563 */
564static int
565boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
566{
567 int rc;
568 uint16_t crc;
569 uint16_t len;
Marko Kiiskilae5aeee42018-12-21 15:00:16 +0200570
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200571#ifdef __ZEPHYR__
572 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200573 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200574 if (err) {
575 return -1;
576 }
577#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800578 if (*out_off + base64_decode_len(in) >= maxout) {
579 return -1;
580 }
581 rc = base64_decode(in, &out[*out_off]);
582 if (rc < 0) {
583 return -1;
584 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200585#endif
Fabio Utzig6f49c272019-08-23 11:42:58 -0300586
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587 *out_off += rc;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300588 if (*out_off <= sizeof(uint16_t)) {
589 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800590 }
Fabio Utzig6f49c272019-08-23 11:42:58 -0300591
592 len = ntohs(*(uint16_t *)out);
593 if (len != *out_off - sizeof(uint16_t)) {
594 return 0;
595 }
596
597 if (len > *out_off - sizeof(uint16_t)) {
598 len = *out_off - sizeof(uint16_t);
599 }
600
601 out += sizeof(uint16_t);
602#ifdef __ZEPHYR__
603 crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true);
604#else
605 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
606#endif
607 if (crc || len <= sizeof(crc)) {
608 return 0;
609 }
610 *out_off -= sizeof(crc);
611 out[*out_off] = '\0';
612
613 return 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800614}
615
616/*
617 * Task which waits reading console, expecting to get image over
618 * serial port.
619 */
620void
Marko Kiiskila149b4572018-06-06 14:18:54 +0300621boot_serial_start(const struct boot_uart_funcs *f)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622{
623 int rc;
624 int off;
David Brown57f0df32020-05-12 08:39:21 -0600625 int dec_off = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800626 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300627 int max_input;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628
Marko Kiiskila149b4572018-06-06 14:18:54 +0300629 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300630 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800631
632 off = 0;
633 while (1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200634 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800635 if (rc <= 0 && !full_line) {
636 continue;
637 }
638 off += rc;
639 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300640 if (off == max_input) {
641 /*
642 * Full line, no newline yet. Reset the input buffer.
643 */
644 off = 0;
645 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800646 continue;
647 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200648 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
649 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800650 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200651 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
652 } else if (in_buf[0] == SHELL_NLIP_DATA_START1 &&
653 in_buf[1] == SHELL_NLIP_DATA_START2) {
654 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800655 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200656
657 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800658 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200659 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800660 }
661 off = 0;
662 }
663}