blob: 6f964d47d2d3ab2ed011830fc428e65351702b82 [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__
30#include <misc/reboot.h>
31#include <misc/byteorder.h>
32#include <misc/__assert.h>
33#include <flash.h>
Emanuele Di Santo401d7b32019-01-11 12:59:00 +010034#include <crc.h>
Carles Cufi0165be82018-03-26 17:43:51 +020035#include <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
Marko Kiiskilace50ab02018-06-06 11:33:33 +030065#define BOOT_SERIAL_OUT_MAX 80
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
Marko Kiiskila149b4572018-06-06 14:18:54 +030078static char in_buf[BOOT_SERIAL_INPUT_MAX + 1];
79static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1];
Marko Kiiskila8b1ce3a2018-06-14 13:20:46 -070080const struct boot_uart_funcs *boot_uf;
Christopher Collins92ea77f2016-12-12 15:59:26 -080081static uint32_t curr_off;
82static uint32_t img_size;
83static struct nmgr_hdr *bs_hdr;
84
85static char bs_obuf[BOOT_SERIAL_OUT_MAX];
86
87static int bs_cbor_writer(struct cbor_encoder_writer *, const char *data,
88 int len);
89static void boot_serial_output(void);
90
91static struct cbor_encoder_writer bs_writer = {
92 .write = bs_cbor_writer
93};
94static CborEncoder bs_root;
95static CborEncoder bs_rsp;
96
97int
98bs_cbor_writer(struct cbor_encoder_writer *cew, const char *data, int len)
99{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300100 if (cew->bytes_written + len > sizeof(bs_obuf)) {
101 return CborErrorOutOfMemory;
102 }
103
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104 memcpy(&bs_obuf[cew->bytes_written], data, len);
105 cew->bytes_written += len;
106
107 return 0;
108}
109
110/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300111 * Convert version into string without use of snprintf().
Christopher Collins92ea77f2016-12-12 15:59:26 -0800112 */
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300113static int
114u32toa(char *tgt, uint32_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800115{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300116 char *dst;
117 uint32_t d = 1;
118 uint32_t dgt;
119 int n = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300121 dst = tgt;
122 while (val / d >= 10) {
123 d *= 10;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800124 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300125 while (d) {
126 dgt = val / d;
127 val %= d;
128 d /= 10;
129 if (n || dgt > 0 || d == 0) {
130 *dst++ = dgt + '0';
131 ++n;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800132 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800133 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300134 *dst = '\0';
135
136 return dst - tgt;
137}
138
139/*
140 * dst has to be able to fit "255.255.65535.4294967295" (25 characters).
141 */
142static void
143bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
144{
145 int off;
146
147 off = u32toa(dst, ver->iv_major);
148 dst[off++] = '.';
149 off += u32toa(dst + off, ver->iv_minor);
150 dst[off++] = '.';
151 off += u32toa(dst + off, ver->iv_revision);
152 dst[off++] = '.';
153 off += u32toa(dst + off, ver->iv_build_num);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800154}
155
156/*
157 * List images.
158 */
159static void
160bs_list(char *buf, int len)
161{
162 CborEncoder images;
163 CborEncoder image;
164 struct image_header hdr;
165 uint8_t tmpbuf[64];
166 int i, area_id;
167 const struct flash_area *fap;
168
169 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
170 cbor_encode_text_stringz(&bs_rsp, "images");
171 cbor_encoder_create_array(&bs_rsp, &images, CborIndefiniteLength);
172 for (i = 0; i < 2; i++) {
173 area_id = flash_area_id_from_image_slot(i);
174 if (flash_area_open(area_id, &fap)) {
175 continue;
176 }
177
178 flash_area_read(fap, 0, &hdr, sizeof(hdr));
179
180 if (hdr.ih_magic != IMAGE_MAGIC ||
Fabio Utzig10ee6482019-08-01 12:04:52 -0300181 bootutil_img_validate(NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182 NULL, 0, NULL)) {
183 flash_area_close(fap);
184 continue;
185 }
186 flash_area_close(fap);
187
188 cbor_encoder_create_map(&images, &image, CborIndefiniteLength);
189 cbor_encode_text_stringz(&image, "slot");
190 cbor_encode_int(&image, i);
191 cbor_encode_text_stringz(&image, "version");
192
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300193 bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194 cbor_encode_text_stringz(&image, (char *)tmpbuf);
195 cbor_encoder_close_container(&images, &image);
196 }
197 cbor_encoder_close_container(&bs_rsp, &images);
198 cbor_encoder_close_container(&bs_root, &bs_rsp);
199 boot_serial_output();
200}
201
202/*
203 * Image upload request.
204 */
205static void
206bs_upload(char *buf, int len)
207{
208 CborParser parser;
209 struct cbor_buf_reader reader;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300210 struct CborValue root_value;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211 struct CborValue value;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300212 uint8_t img_data[512];
213 long long int off = UINT_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;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300216 long long int data_len = UINT_MAX;
217 size_t slen;
218 char name_str[8];
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
226 memset(img_data, 0, sizeof(img_data));
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300227
228 /*
229 * Expected data format.
230 * {
231 * "data":<img_data>
232 * "len":<image len>
233 * "off":<current offset of image data>
234 * }
235 */
236
237 /*
238 * Object comes within { ... }
239 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800240 cbor_buf_reader_init(&reader, (uint8_t *)buf, len);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200241#ifdef __ZEPHYR__
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300242 cbor_parser_cust_reader_init(&reader.r, 0, &parser, &root_value);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200243#else
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300244 cbor_parser_init(&reader.r, 0, &parser, &root_value);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200245#endif
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300246 if (!cbor_value_is_container(&root_value)) {
247 goto out_invalid_data;
248 }
249 if (cbor_value_enter_container(&root_value, &value)) {
250 goto out_invalid_data;
251 }
252 while (cbor_value_is_valid(&value)) {
253 /*
254 * Decode key.
255 */
256 if (cbor_value_calculate_string_length(&value, &slen)) {
257 goto out_invalid_data;
258 }
259 if (!cbor_value_is_text_string(&value) ||
260 slen >= sizeof(name_str) - 1) {
261 goto out_invalid_data;
262 }
263 if (cbor_value_copy_text_string(&value, name_str, &slen, &value)) {
264 goto out_invalid_data;
265 }
266 name_str[slen] = '\0';
267 if (!strcmp(name_str, "data")) {
268 /*
269 * Image data
270 */
271 if (value.type != CborByteStringType) {
272 goto out_invalid_data;
273 }
274 if (cbor_value_calculate_string_length(&value, &slen) ||
275 slen >= sizeof(img_data)) {
276 goto out_invalid_data;
277 }
278 if (cbor_value_copy_byte_string(&value, img_data, &slen, &value)) {
279 goto out_invalid_data;
280 }
281 img_blen = slen;
282 } else if (!strcmp(name_str, "off")) {
283 /*
284 * Offset of the data.
285 */
286 if (value.type != CborIntegerType) {
287 goto out_invalid_data;
288 }
289 if (cbor_value_get_int64(&value, &off)) {
290 goto out_invalid_data;
291 }
292 if (cbor_value_advance(&value)) {
293 goto out_invalid_data;
294 }
295 } else if (!strcmp(name_str, "len")) {
296 /*
297 * Length of the image. This should only be present in the first
298 * block of data; when offset is 0.
299 */
300 if (value.type != CborIntegerType) {
301 goto out_invalid_data;
302 }
303 if (cbor_value_get_int64(&value, &data_len)) {
304 goto out_invalid_data;
305 }
306 if (cbor_value_advance(&value)) {
307 goto out_invalid_data;
308 }
309 } else {
310 /*
311 * Unknown keys.
312 */
313 if (cbor_value_advance(&value)) {
314 goto out_invalid_data;
315 }
316 }
317 }
318 if (off == UINT_MAX) {
319 /*
320 * Offset must be set in every block.
321 */
322 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800323 }
324
Christopher Collins92ea77f2016-12-12 15:59:26 -0800325 rc = flash_area_open(flash_area_id_from_image_slot(0), &fap);
326 if (rc) {
327 rc = MGMT_ERR_EINVAL;
328 goto out;
329 }
330
331 if (off == 0) {
332 curr_off = 0;
333 if (data_len > fap->fa_size) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300334 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800335 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200336#ifndef CONFIG_BOOT_ERASE_PROGRESSIVELY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800337 rc = flash_area_erase(fap, 0, fap->fa_size);
338 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300339 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800340 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200341#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800342 img_size = data_len;
343 }
344 if (off != curr_off) {
345 rc = 0;
346 goto out;
347 }
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300348 if (curr_off + img_blen < img_size) {
349 rem_bytes = img_blen % flash_area_align(fap);
350 if (rem_bytes) {
351 img_blen -= rem_bytes;
352 }
353 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200354
355#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
356 rc = flash_area_sector_from_off(curr_off + img_blen, &sector);
357 if (rc) {
358 BOOT_LOG_ERR("Unable to determine flash sector size");
359 goto out;
360 }
361 if (off_last != sector.fs_off) {
362 off_last = sector.fs_off;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200363 BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off);
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200364 rc = flash_area_erase(fap, sector.fs_off, sector.fs_size);
365 if (rc) {
366 BOOT_LOG_ERR("Error %d while erasing sector", rc);
367 goto out;
368 }
369 }
370#endif
371
372 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_blen);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373 rc = flash_area_write(fap, curr_off, img_data, img_blen);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300374 if (rc == 0) {
375 curr_off += img_blen;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200376#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
377 if (curr_off == img_size) {
378 /* get the last sector offset */
379 rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
380 if (rc) {
381 BOOT_LOG_ERR("Unable to determine flash sector of"
382 "the image trailer");
383 goto out;
384 }
385 /* Assure that sector for image trailer was erased. */
386 /* Check whether it was erased during previous upload. */
387 if (off_last < sector.fs_off) {
388 BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off);
389 rc = flash_area_erase(fap, sector.fs_off, sector.fs_size);
390 if (rc) {
391 BOOT_LOG_ERR("Error %d while erasing sector", rc);
392 goto out;
393 }
394 }
395 }
396#endif
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300397 } else {
398 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800399 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800400 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200401
Christopher Collins92ea77f2016-12-12 15:59:26 -0800402out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200403 BOOT_LOG_INF("RX: 0x%x", rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800404 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
405 cbor_encode_text_stringz(&bs_rsp, "rc");
406 cbor_encode_int(&bs_rsp, rc);
407 if (rc == 0) {
408 cbor_encode_text_stringz(&bs_rsp, "off");
409 cbor_encode_uint(&bs_rsp, curr_off);
410 }
411 cbor_encoder_close_container(&bs_root, &bs_rsp);
412
413 boot_serial_output();
414 flash_area_close(fap);
415}
416
417/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300418 * Console echo control/image erase. Send empty response, don't do anything.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800419 */
420static void
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300421bs_empty_rsp(char *buf, int len)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300423 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
424 cbor_encode_text_stringz(&bs_rsp, "rc");
425 cbor_encode_int(&bs_rsp, 0);
426 cbor_encoder_close_container(&bs_root, &bs_rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800427 boot_serial_output();
428}
429
430/*
431 * Reset, and (presumably) boot to newly uploaded image. Flush console
432 * before restarting.
433 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200434static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800435bs_reset(char *buf, int len)
436{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300437 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800438
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200439#ifdef __ZEPHYR__
440 k_sleep(250);
441 sys_reboot(SYS_REBOOT_COLD);
442#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800443 os_cputime_delay_usecs(250000);
444 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200445#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800446}
447
448/*
449 * Parse incoming line of input from console.
450 * Expect newtmgr protocol with serial transport.
451 */
452void
453boot_serial_input(char *buf, int len)
454{
455 struct nmgr_hdr *hdr;
456
457 hdr = (struct nmgr_hdr *)buf;
458 if (len < sizeof(*hdr) ||
459 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
460 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
461 return;
462 }
463 bs_hdr = hdr;
464 hdr->nh_group = ntohs(hdr->nh_group);
465
466 buf += sizeof(*hdr);
467 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300468
Christopher Collins92ea77f2016-12-12 15:59:26 -0800469 bs_writer.bytes_written = 0;
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200470#ifdef __ZEPHYR__
471 cbor_encoder_cust_writer_init(&bs_root, &bs_writer, 0);
472#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473 cbor_encoder_init(&bs_root, &bs_writer, 0);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200474#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800475
476 /*
477 * Limited support for commands.
478 */
479 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
480 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300481 case IMGMGR_NMGR_ID_STATE:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800482 bs_list(buf, len);
483 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300484 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800485 bs_upload(buf, len);
486 break;
487 default:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300488 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800489 break;
490 }
491 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
492 switch (hdr->nh_id) {
493 case NMGR_ID_CONS_ECHO_CTRL:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300494 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800495 break;
496 case NMGR_ID_RESET:
497 bs_reset(buf, len);
498 break;
499 default:
500 break;
501 }
502 }
503}
504
505static void
506boot_serial_output(void)
507{
508 char *data;
509 int len;
510 uint16_t crc;
511 uint16_t totlen;
512 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
513 char buf[BOOT_SERIAL_OUT_MAX];
514 char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)];
515
516 data = bs_obuf;
517 len = bs_writer.bytes_written;
518
519 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300520 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800521 bs_hdr->nh_len = htons(len);
522 bs_hdr->nh_group = htons(bs_hdr->nh_group);
523
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200524#ifdef __ZEPHYR__
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300525 crc = crc16((u8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL,
526 CRC16_INITIAL_CRC, false);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200527 crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true);
528#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800529 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
530 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200531#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800532 crc = htons(crc);
533
Marko Kiiskila149b4572018-06-06 14:18:54 +0300534 boot_uf->write(pkt_start, sizeof(pkt_start));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800535
536 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
537 totlen = htons(totlen);
538
539 memcpy(buf, &totlen, sizeof(totlen));
540 totlen = sizeof(totlen);
541 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
542 totlen += sizeof(*bs_hdr);
543 memcpy(&buf[totlen], data, len);
544 totlen += len;
545 memcpy(&buf[totlen], &crc, sizeof(crc));
546 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200547#ifdef __ZEPHYR__
548 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +0200549 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200550 totlen = enc_len;
551#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800552 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200553#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +0300554 boot_uf->write(encoded_buf, totlen);
555 boot_uf->write("\n\r", 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200556 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800557}
558
559/*
560 * Returns 1 if full packet has been received.
561 */
562static int
563boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
564{
565 int rc;
566 uint16_t crc;
567 uint16_t len;
Marko Kiiskilae5aeee42018-12-21 15:00:16 +0200568
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200569#ifdef __ZEPHYR__
570 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200571 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200572 if (err) {
573 return -1;
574 }
575#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576 if (*out_off + base64_decode_len(in) >= maxout) {
577 return -1;
578 }
579 rc = base64_decode(in, &out[*out_off]);
580 if (rc < 0) {
581 return -1;
582 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200583#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800584 *out_off += rc;
585
586 if (*out_off > sizeof(uint16_t)) {
587 len = ntohs(*(uint16_t *)out);
Marko Kiiskilae5aeee42018-12-21 15:00:16 +0200588 if (len != *out_off - sizeof(uint16_t)) {
589 return 0;
590 }
Andrzej Puzdrowski0cd178d2019-02-19 14:17:22 +0100591
592 if (len > *out_off - sizeof(uint16_t)) {
593 len = *out_off - sizeof(uint16_t);
594 }
595
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596 out += sizeof(uint16_t);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200597#ifdef __ZEPHYR__
598 crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true);
599#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800600 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200601#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800602 if (crc || len <= sizeof(crc)) {
603 return 0;
604 }
605 *out_off -= sizeof(crc);
606 out[*out_off] = '\0';
607
608 return 1;
609 }
610 return 0;
611}
612
613/*
614 * Task which waits reading console, expecting to get image over
615 * serial port.
616 */
617void
Marko Kiiskila149b4572018-06-06 14:18:54 +0300618boot_serial_start(const struct boot_uart_funcs *f)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800619{
620 int rc;
621 int off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622 int dec_off;
623 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300624 int max_input;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800625
Marko Kiiskila149b4572018-06-06 14:18:54 +0300626 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300627 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628
629 off = 0;
630 while (1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200631 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800632 if (rc <= 0 && !full_line) {
633 continue;
634 }
635 off += rc;
636 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300637 if (off == max_input) {
638 /*
639 * Full line, no newline yet. Reset the input buffer.
640 */
641 off = 0;
642 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800643 continue;
644 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200645 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
646 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800647 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200648 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
649 } else if (in_buf[0] == SHELL_NLIP_DATA_START1 &&
650 in_buf[1] == SHELL_NLIP_DATA_START2) {
651 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800652 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200653
654 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800655 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200656 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800657 }
658 off = 0;
659 }
660}