blob: 1d9778da1a45b074b641a2dac7bf6041b0390a6b [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__
Gerard Marull-Paretase20e0922021-04-29 10:12:15 +020030#include <sys/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ønningstadf42a8202019-12-13 03:27:54 +0100257 Upload_t upload;
258 if (!cbor_decode_Upload((const uint8_t *)buf, len, &upload)) {
259 goto out_invalid_data;
260 }
Dominik Ermel470e2f32020-01-10 13:28:48 +0000261
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100262 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 Kiiskilace50ab02018-06-06 11:33:33 +0300283 }
284 }
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100285
286 if (off == UINT_MAX || img_data == NULL) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300287 /*
288 * Offset must be set in every block.
289 */
290 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291 }
292
Fabio Utzig6f49c272019-08-23 11:42:58 -0300293 rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294 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 Kiiskilace50ab02018-06-06 11:33:33 +0300302 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800303 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200304#ifndef CONFIG_BOOT_ERASE_PROGRESSIVELY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305 rc = flash_area_erase(fap, 0, fap->fa_size);
306 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300307 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800308 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200309#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310 img_size = data_len;
311 }
312 if (off != curr_off) {
313 rc = 0;
314 goto out;
315 }
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200316
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 Utzig30f6b2a2018-03-29 16:18:53 -0300327 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200328
329#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
330 rc = flash_area_sector_from_off(curr_off + img_blen, &sector);
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 Puzdrowskic2e30cf2018-07-20 16:19:09 +0200337 BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off);
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200338 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 Puzdrowskif48de7a2020-10-19 09:42:02 +0200347 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 Kiiskilace50ab02018-06-06 11:33:33 +0300373 if (rc == 0) {
374 curr_off += img_blen;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200375#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), &sector);
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 Kiiskilace50ab02018-06-06 11:33:33 +0300396 } else {
397 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800398 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800399 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200400
Christopher Collins92ea77f2016-12-12 15:59:26 -0800401out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200402 BOOT_LOG_INF("RX: 0x%x", rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800403 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 Kiiskilace50ab02018-06-06 11:33:33 +0300417 * Console echo control/image erase. Send empty response, don't do anything.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800418 */
419static void
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300420bs_empty_rsp(char *buf, int len)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800421{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300422 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 Collins92ea77f2016-12-12 15:59:26 -0800426 boot_serial_output();
427}
428
429/*
430 * Reset, and (presumably) boot to newly uploaded image. Flush console
431 * before restarting.
432 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200433static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800434bs_reset(char *buf, int len)
435{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300436 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800437
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200438#ifdef __ZEPHYR__
Carles Cufi7e7b4ad2020-03-30 19:12:02 +0200439 k_sleep(K_MSEC(250));
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200440 sys_reboot(SYS_REBOOT_COLD);
441#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442 os_cputime_delay_usecs(250000);
443 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200444#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800445}
446
447/*
448 * Parse incoming line of input from console.
449 * Expect newtmgr protocol with serial transport.
450 */
451void
452boot_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 Kiiskilace50ab02018-06-06 11:33:33 +0300467
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 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 Kiiskilace50ab02018-06-06 11:33:33 +0300476 case IMGMGR_NMGR_ID_STATE:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800477 bs_list(buf, len);
478 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300479 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480 bs_upload(buf, len);
481 break;
482 default:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300483 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484 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 Kiiskilace50ab02018-06-06 11:33:33 +0300489 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490 break;
491 case NMGR_ID_RESET:
492 bs_reset(buf, len);
493 break;
494 default:
495 break;
496 }
497 }
498}
499
500static void
501boot_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 Kiiskilace50ab02018-06-06 11:33:33 +0300515 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800516 bs_hdr->nh_len = htons(len);
517 bs_hdr->nh_group = htons(bs_hdr->nh_group);
518
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200519#ifdef __ZEPHYR__
Kumar Gala0813efe2020-05-27 12:25:41 -0500520 crc = crc16((uint8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL,
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300521 CRC16_INITIAL_CRC, false);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200522 crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true);
523#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800524 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
525 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200526#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800527 crc = htons(crc);
528
Marko Kiiskila149b4572018-06-06 14:18:54 +0300529 boot_uf->write(pkt_start, sizeof(pkt_start));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530
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 Puzdrowski8e96b832017-09-08 16:49:14 +0200542#ifdef __ZEPHYR__
543 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +0200544 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200545 totlen = enc_len;
546#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800547 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200548#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +0300549 boot_uf->write(encoded_buf, totlen);
550 boot_uf->write("\n\r", 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200551 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800552}
553
554/*
555 * Returns 1 if full packet has been received.
556 */
557static int
558boot_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 Kiiskilae5aeee42018-12-21 15:00:16 +0200563
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200564#ifdef __ZEPHYR__
565 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200566 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200567 if (err) {
568 return -1;
569 }
570#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571 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 Puzdrowski8e96b832017-09-08 16:49:14 +0200578#endif
Fabio Utzig6f49c272019-08-23 11:42:58 -0300579
Christopher Collins92ea77f2016-12-12 15:59:26 -0800580 *out_off += rc;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300581 if (*out_off <= sizeof(uint16_t)) {
582 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800583 }
Fabio Utzig6f49c272019-08-23 11:42:58 -0300584
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 Collins92ea77f2016-12-12 15:59:26 -0800607}
608
609/*
610 * Task which waits reading console, expecting to get image over
611 * serial port.
612 */
613void
Marko Kiiskila149b4572018-06-06 14:18:54 +0300614boot_serial_start(const struct boot_uart_funcs *f)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800615{
616 int rc;
617 int off;
David Brown57f0df32020-05-12 08:39:21 -0600618 int dec_off = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800619 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300620 int max_input;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800621
Marko Kiiskila149b4572018-06-06 14:18:54 +0300622 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300623 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800624
625 off = 0;
626 while (1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200627 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 if (rc <= 0 && !full_line) {
629 continue;
630 }
631 off += rc;
632 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300633 if (off == max_input) {
634 /*
635 * Full line, no newline yet. Reset the input buffer.
636 */
637 off = 0;
638 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800639 continue;
640 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200641 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
642 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800643 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200644 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 Collins92ea77f2016-12-12 15:59:26 -0800648 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200649
650 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800651 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200652 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800653 }
654 off = 0;
655 }
656}