blob: 409ae9685db398aef6f29b858a98fb52c31cd7bb [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__
Andrzej Puzdrowskif1d189c2019-12-12 09:34:11 +010030#include <power/reboot.h>
31#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>
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +020036#include <cbor.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>
Christopher Collins92ea77f2016-12-12 15:59:26 -080044#include <tinycbor/cbor.h>
45#include <tinycbor/cbor_buf_reader.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>
54
55#include "boot_serial/boot_serial.h"
56#include "boot_serial_priv.h"
57
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +020058#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
59#include "bootutil_priv.h"
60#endif
61
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010062MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
63
Marko Kiiskila149b4572018-06-06 14:18:54 +030064#define BOOT_SERIAL_INPUT_MAX 512
Fabio Utzig6f49c272019-08-23 11:42:58 -030065#define BOOT_SERIAL_OUT_MAX 128
Christopher Collins92ea77f2016-12-12 15:59:26 -080066
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020067#ifdef __ZEPHYR__
Carles Cufi0165be82018-03-26 17:43:51 +020068/* base64 lib encodes data to null-terminated string */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020069#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
70
71#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
72#define CRC_CITT_POLYMINAL 0x1021
73
74#define ntohs(x) sys_be16_to_cpu(x)
75#define htons(x) sys_cpu_to_be16(x)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020076#endif
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010077
Fabio Utzig6f49c272019-08-23 11:42:58 -030078#ifndef BOOT_IMAGE_NUMBER
79#define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER
80#endif
81
82#if (BOOT_IMAGE_NUMBER > 1)
83#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
84#else
85#define IMAGES_ITER(x)
86#endif
87
Marko Kiiskila149b4572018-06-06 14:18:54 +030088static char in_buf[BOOT_SERIAL_INPUT_MAX + 1];
89static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1];
Marko Kiiskila8b1ce3a2018-06-14 13:20:46 -070090const struct boot_uart_funcs *boot_uf;
Christopher Collins92ea77f2016-12-12 15:59:26 -080091static uint32_t curr_off;
92static uint32_t img_size;
93static struct nmgr_hdr *bs_hdr;
94
95static char bs_obuf[BOOT_SERIAL_OUT_MAX];
96
97static int bs_cbor_writer(struct cbor_encoder_writer *, const char *data,
98 int len);
99static void boot_serial_output(void);
100
101static struct cbor_encoder_writer bs_writer = {
102 .write = bs_cbor_writer
103};
104static CborEncoder bs_root;
105static CborEncoder bs_rsp;
106
107int
108bs_cbor_writer(struct cbor_encoder_writer *cew, const char *data, int len)
109{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300110 if (cew->bytes_written + len > sizeof(bs_obuf)) {
111 return CborErrorOutOfMemory;
112 }
113
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114 memcpy(&bs_obuf[cew->bytes_written], data, len);
115 cew->bytes_written += len;
116
117 return 0;
118}
119
120/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300121 * Convert version into string without use of snprintf().
Christopher Collins92ea77f2016-12-12 15:59:26 -0800122 */
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300123static int
124u32toa(char *tgt, uint32_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300126 char *dst;
127 uint32_t d = 1;
128 uint32_t dgt;
129 int n = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800130
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300131 dst = tgt;
132 while (val / d >= 10) {
133 d *= 10;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300135 while (d) {
136 dgt = val / d;
137 val %= d;
138 d /= 10;
139 if (n || dgt > 0 || d == 0) {
140 *dst++ = dgt + '0';
141 ++n;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800142 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800143 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300144 *dst = '\0';
145
146 return dst - tgt;
147}
148
149/*
150 * dst has to be able to fit "255.255.65535.4294967295" (25 characters).
151 */
152static void
153bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
154{
155 int off;
156
157 off = u32toa(dst, ver->iv_major);
158 dst[off++] = '.';
159 off += u32toa(dst + off, ver->iv_minor);
160 dst[off++] = '.';
161 off += u32toa(dst + off, ver->iv_revision);
162 dst[off++] = '.';
163 off += u32toa(dst + off, ver->iv_build_num);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800164}
165
166/*
167 * List images.
168 */
169static void
170bs_list(char *buf, int len)
171{
172 CborEncoder images;
173 CborEncoder image;
174 struct image_header hdr;
175 uint8_t tmpbuf[64];
Fabio Utzig6f49c272019-08-23 11:42:58 -0300176 int slot, area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 const struct flash_area *fap;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300178 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800179
180 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
181 cbor_encode_text_stringz(&bs_rsp, "images");
182 cbor_encoder_create_array(&bs_rsp, &images, CborIndefiniteLength);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300183 image_index = 0;
184 IMAGES_ITER(image_index) {
185 for (slot = 0; slot < 2; slot++) {
186 area_id = flash_area_id_from_multi_image_slot(image_index, slot);
187 if (flash_area_open(area_id, &fap)) {
188 continue;
189 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800190
Fabio Utzig6f49c272019-08-23 11:42:58 -0300191 flash_area_read(fap, 0, &hdr, sizeof(hdr));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800192
Fabio Utzig6f49c272019-08-23 11:42:58 -0300193 if (hdr.ih_magic != IMAGE_MAGIC ||
194 bootutil_img_validate(NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
195 NULL, 0, NULL)) {
196 flash_area_close(fap);
197 continue;
198 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800199 flash_area_close(fap);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300200
201 cbor_encoder_create_map(&images, &image, CborIndefiniteLength);
202
203#if (BOOT_IMAGE_NUMBER > 1)
204 cbor_encode_text_stringz(&image, "image");
205 cbor_encode_int(&image, image_index);
206#endif
207
208 cbor_encode_text_stringz(&image, "slot");
209 cbor_encode_int(&image, slot);
210 cbor_encode_text_stringz(&image, "version");
211
212 bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
213 cbor_encode_text_stringz(&image, (char *)tmpbuf);
214 cbor_encoder_close_container(&images, &image);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800216 }
217 cbor_encoder_close_container(&bs_rsp, &images);
218 cbor_encoder_close_container(&bs_root, &bs_rsp);
219 boot_serial_output();
220}
221
222/*
223 * Image upload request.
224 */
225static void
226bs_upload(char *buf, int len)
227{
228 CborParser parser;
229 struct cbor_buf_reader reader;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300230 struct CborValue root_value;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800231 struct CborValue value;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300232 uint8_t img_data[512];
233 long long int off = UINT_MAX;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800234 size_t img_blen = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300235 uint8_t rem_bytes;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300236 long long int data_len = UINT_MAX;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300237 int img_num;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300238 size_t slen;
239 char name_str[8];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800240 const struct flash_area *fap = NULL;
241 int rc;
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200242#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
243 static off_t off_last = -1;
244 struct flash_sector sector;
245#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800246
247 memset(img_data, 0, sizeof(img_data));
Fabio Utzig6f49c272019-08-23 11:42:58 -0300248 img_num = 0;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300249
250 /*
251 * Expected data format.
252 * {
Fabio Utzig6f49c272019-08-23 11:42:58 -0300253 * "image":<image number in a multi-image set (OPTIONAL)>
254 * "data":<image data>
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300255 * "len":<image len>
256 * "off":<current offset of image data>
257 * }
258 */
259
260 /*
261 * Object comes within { ... }
262 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800263 cbor_buf_reader_init(&reader, (uint8_t *)buf, len);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200264#ifdef __ZEPHYR__
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300265 cbor_parser_cust_reader_init(&reader.r, 0, &parser, &root_value);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200266#else
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300267 cbor_parser_init(&reader.r, 0, &parser, &root_value);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200268#endif
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300269 if (!cbor_value_is_container(&root_value)) {
270 goto out_invalid_data;
271 }
272 if (cbor_value_enter_container(&root_value, &value)) {
273 goto out_invalid_data;
274 }
275 while (cbor_value_is_valid(&value)) {
276 /*
277 * Decode key.
278 */
279 if (cbor_value_calculate_string_length(&value, &slen)) {
280 goto out_invalid_data;
281 }
282 if (!cbor_value_is_text_string(&value) ||
283 slen >= sizeof(name_str) - 1) {
284 goto out_invalid_data;
285 }
286 if (cbor_value_copy_text_string(&value, name_str, &slen, &value)) {
287 goto out_invalid_data;
288 }
289 name_str[slen] = '\0';
290 if (!strcmp(name_str, "data")) {
291 /*
292 * Image data
293 */
294 if (value.type != CborByteStringType) {
295 goto out_invalid_data;
296 }
297 if (cbor_value_calculate_string_length(&value, &slen) ||
298 slen >= sizeof(img_data)) {
299 goto out_invalid_data;
300 }
301 if (cbor_value_copy_byte_string(&value, img_data, &slen, &value)) {
302 goto out_invalid_data;
303 }
304 img_blen = slen;
305 } else if (!strcmp(name_str, "off")) {
306 /*
307 * Offset of the data.
308 */
309 if (value.type != CborIntegerType) {
310 goto out_invalid_data;
311 }
312 if (cbor_value_get_int64(&value, &off)) {
313 goto out_invalid_data;
314 }
315 if (cbor_value_advance(&value)) {
316 goto out_invalid_data;
317 }
318 } else if (!strcmp(name_str, "len")) {
319 /*
320 * Length of the image. This should only be present in the first
321 * block of data; when offset is 0.
322 */
323 if (value.type != CborIntegerType) {
324 goto out_invalid_data;
325 }
326 if (cbor_value_get_int64(&value, &data_len)) {
327 goto out_invalid_data;
328 }
329 if (cbor_value_advance(&value)) {
330 goto out_invalid_data;
331 }
Fabio Utzig6f49c272019-08-23 11:42:58 -0300332 } else if (!strcmp(name_str, "image")) {
333 /*
334 * In a multi-image system, image number to upload to, if not
335 * present will upload to slot 0 of image set 0.
336 */
337 if (value.type != CborIntegerType) {
338 goto out_invalid_data;
339 }
340 if (cbor_value_get_int(&value, &img_num)) {
341 goto out_invalid_data;
342 }
343 if (cbor_value_advance(&value)) {
344 goto out_invalid_data;
345 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300346 } else {
347 /*
348 * Unknown keys.
349 */
350 if (cbor_value_advance(&value)) {
351 goto out_invalid_data;
352 }
353 }
354 }
355 if (off == UINT_MAX) {
356 /*
357 * Offset must be set in every block.
358 */
359 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800360 }
361
Fabio Utzig6f49c272019-08-23 11:42:58 -0300362 rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800363 if (rc) {
364 rc = MGMT_ERR_EINVAL;
365 goto out;
366 }
367
368 if (off == 0) {
369 curr_off = 0;
370 if (data_len > fap->fa_size) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300371 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800372 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200373#ifndef CONFIG_BOOT_ERASE_PROGRESSIVELY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800374 rc = flash_area_erase(fap, 0, fap->fa_size);
375 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300376 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800377 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200378#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800379 img_size = data_len;
380 }
381 if (off != curr_off) {
382 rc = 0;
383 goto out;
384 }
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300385 if (curr_off + img_blen < img_size) {
386 rem_bytes = img_blen % flash_area_align(fap);
387 if (rem_bytes) {
388 img_blen -= rem_bytes;
389 }
390 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200391
392#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
393 rc = flash_area_sector_from_off(curr_off + img_blen, &sector);
394 if (rc) {
395 BOOT_LOG_ERR("Unable to determine flash sector size");
396 goto out;
397 }
398 if (off_last != sector.fs_off) {
399 off_last = sector.fs_off;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200400 BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off);
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200401 rc = flash_area_erase(fap, sector.fs_off, sector.fs_size);
402 if (rc) {
403 BOOT_LOG_ERR("Error %d while erasing sector", rc);
404 goto out;
405 }
406 }
407#endif
408
409 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_blen);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410 rc = flash_area_write(fap, curr_off, img_data, img_blen);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300411 if (rc == 0) {
412 curr_off += img_blen;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200413#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
414 if (curr_off == img_size) {
415 /* get the last sector offset */
416 rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
417 if (rc) {
418 BOOT_LOG_ERR("Unable to determine flash sector of"
419 "the image trailer");
420 goto out;
421 }
422 /* Assure that sector for image trailer was erased. */
423 /* Check whether it was erased during previous upload. */
424 if (off_last < sector.fs_off) {
425 BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off);
426 rc = flash_area_erase(fap, sector.fs_off, sector.fs_size);
427 if (rc) {
428 BOOT_LOG_ERR("Error %d while erasing sector", rc);
429 goto out;
430 }
431 }
432 }
433#endif
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300434 } else {
435 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800437 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200438
Christopher Collins92ea77f2016-12-12 15:59:26 -0800439out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200440 BOOT_LOG_INF("RX: 0x%x", rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800441 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
442 cbor_encode_text_stringz(&bs_rsp, "rc");
443 cbor_encode_int(&bs_rsp, rc);
444 if (rc == 0) {
445 cbor_encode_text_stringz(&bs_rsp, "off");
446 cbor_encode_uint(&bs_rsp, curr_off);
447 }
448 cbor_encoder_close_container(&bs_root, &bs_rsp);
449
450 boot_serial_output();
451 flash_area_close(fap);
452}
453
454/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300455 * Console echo control/image erase. Send empty response, don't do anything.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800456 */
457static void
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300458bs_empty_rsp(char *buf, int len)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800459{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300460 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
461 cbor_encode_text_stringz(&bs_rsp, "rc");
462 cbor_encode_int(&bs_rsp, 0);
463 cbor_encoder_close_container(&bs_root, &bs_rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800464 boot_serial_output();
465}
466
467/*
468 * Reset, and (presumably) boot to newly uploaded image. Flush console
469 * before restarting.
470 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200471static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800472bs_reset(char *buf, int len)
473{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300474 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800475
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200476#ifdef __ZEPHYR__
477 k_sleep(250);
478 sys_reboot(SYS_REBOOT_COLD);
479#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480 os_cputime_delay_usecs(250000);
481 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200482#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483}
484
485/*
486 * Parse incoming line of input from console.
487 * Expect newtmgr protocol with serial transport.
488 */
489void
490boot_serial_input(char *buf, int len)
491{
492 struct nmgr_hdr *hdr;
493
494 hdr = (struct nmgr_hdr *)buf;
495 if (len < sizeof(*hdr) ||
496 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
497 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
498 return;
499 }
500 bs_hdr = hdr;
501 hdr->nh_group = ntohs(hdr->nh_group);
502
503 buf += sizeof(*hdr);
504 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300505
Christopher Collins92ea77f2016-12-12 15:59:26 -0800506 bs_writer.bytes_written = 0;
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200507#ifdef __ZEPHYR__
508 cbor_encoder_cust_writer_init(&bs_root, &bs_writer, 0);
509#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800510 cbor_encoder_init(&bs_root, &bs_writer, 0);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200511#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800512
513 /*
514 * Limited support for commands.
515 */
516 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
517 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300518 case IMGMGR_NMGR_ID_STATE:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800519 bs_list(buf, len);
520 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300521 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800522 bs_upload(buf, len);
523 break;
524 default:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300525 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526 break;
527 }
528 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
529 switch (hdr->nh_id) {
530 case NMGR_ID_CONS_ECHO_CTRL:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300531 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800532 break;
533 case NMGR_ID_RESET:
534 bs_reset(buf, len);
535 break;
536 default:
537 break;
538 }
539 }
540}
541
542static void
543boot_serial_output(void)
544{
545 char *data;
546 int len;
547 uint16_t crc;
548 uint16_t totlen;
549 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
550 char buf[BOOT_SERIAL_OUT_MAX];
551 char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)];
552
553 data = bs_obuf;
554 len = bs_writer.bytes_written;
555
556 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300557 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800558 bs_hdr->nh_len = htons(len);
559 bs_hdr->nh_group = htons(bs_hdr->nh_group);
560
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200561#ifdef __ZEPHYR__
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300562 crc = crc16((u8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL,
563 CRC16_INITIAL_CRC, false);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200564 crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true);
565#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800566 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
567 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200568#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800569 crc = htons(crc);
570
Marko Kiiskila149b4572018-06-06 14:18:54 +0300571 boot_uf->write(pkt_start, sizeof(pkt_start));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800572
573 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
574 totlen = htons(totlen);
575
576 memcpy(buf, &totlen, sizeof(totlen));
577 totlen = sizeof(totlen);
578 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
579 totlen += sizeof(*bs_hdr);
580 memcpy(&buf[totlen], data, len);
581 totlen += len;
582 memcpy(&buf[totlen], &crc, sizeof(crc));
583 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200584#ifdef __ZEPHYR__
585 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +0200586 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200587 totlen = enc_len;
588#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800589 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200590#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +0300591 boot_uf->write(encoded_buf, totlen);
592 boot_uf->write("\n\r", 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200593 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800594}
595
596/*
597 * Returns 1 if full packet has been received.
598 */
599static int
600boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
601{
602 int rc;
603 uint16_t crc;
604 uint16_t len;
Marko Kiiskilae5aeee42018-12-21 15:00:16 +0200605
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200606#ifdef __ZEPHYR__
607 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200608 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200609 if (err) {
610 return -1;
611 }
612#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800613 if (*out_off + base64_decode_len(in) >= maxout) {
614 return -1;
615 }
616 rc = base64_decode(in, &out[*out_off]);
617 if (rc < 0) {
618 return -1;
619 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200620#endif
Fabio Utzig6f49c272019-08-23 11:42:58 -0300621
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622 *out_off += rc;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300623 if (*out_off <= sizeof(uint16_t)) {
624 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800625 }
Fabio Utzig6f49c272019-08-23 11:42:58 -0300626
627 len = ntohs(*(uint16_t *)out);
628 if (len != *out_off - sizeof(uint16_t)) {
629 return 0;
630 }
631
632 if (len > *out_off - sizeof(uint16_t)) {
633 len = *out_off - sizeof(uint16_t);
634 }
635
636 out += sizeof(uint16_t);
637#ifdef __ZEPHYR__
638 crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true);
639#else
640 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
641#endif
642 if (crc || len <= sizeof(crc)) {
643 return 0;
644 }
645 *out_off -= sizeof(crc);
646 out[*out_off] = '\0';
647
648 return 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800649}
650
651/*
652 * Task which waits reading console, expecting to get image over
653 * serial port.
654 */
655void
Marko Kiiskila149b4572018-06-06 14:18:54 +0300656boot_serial_start(const struct boot_uart_funcs *f)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800657{
658 int rc;
659 int off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800660 int dec_off;
661 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300662 int max_input;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800663
Marko Kiiskila149b4572018-06-06 14:18:54 +0300664 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300665 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800666
667 off = 0;
668 while (1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200669 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800670 if (rc <= 0 && !full_line) {
671 continue;
672 }
673 off += rc;
674 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300675 if (off == max_input) {
676 /*
677 * Full line, no newline yet. Reset the input buffer.
678 */
679 off = 0;
680 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800681 continue;
682 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200683 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
684 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800685 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200686 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
687 } else if (in_buf[0] == SHELL_NLIP_DATA_START1 &&
688 in_buf[1] == SHELL_NLIP_DATA_START2) {
689 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800690 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200691
692 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800693 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200694 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800695 }
696 off = 0;
697 }
698}