blob: b2be12b896f8173cb13765389a6fe6c3b0bb11fa [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>
34#include <crc16.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
Marko Kiiskila149b4572018-06-06 14:18:54 +030058#define BOOT_SERIAL_INPUT_MAX 512
Marko Kiiskilace50ab02018-06-06 11:33:33 +030059#define BOOT_SERIAL_OUT_MAX 80
Christopher Collins92ea77f2016-12-12 15:59:26 -080060
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020061#ifdef __ZEPHYR__
Carles Cufi0165be82018-03-26 17:43:51 +020062/* base64 lib encodes data to null-terminated string */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020063#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
64
65#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
66#define CRC_CITT_POLYMINAL 0x1021
67
68#define ntohs(x) sys_be16_to_cpu(x)
69#define htons(x) sys_cpu_to_be16(x)
70
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020071#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +030072static char in_buf[BOOT_SERIAL_INPUT_MAX + 1];
73static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1];
Marko Kiiskila8b1ce3a2018-06-14 13:20:46 -070074const struct boot_uart_funcs *boot_uf;
Christopher Collins92ea77f2016-12-12 15:59:26 -080075static uint32_t curr_off;
76static uint32_t img_size;
77static struct nmgr_hdr *bs_hdr;
78
79static char bs_obuf[BOOT_SERIAL_OUT_MAX];
80
81static int bs_cbor_writer(struct cbor_encoder_writer *, const char *data,
82 int len);
83static void boot_serial_output(void);
84
85static struct cbor_encoder_writer bs_writer = {
86 .write = bs_cbor_writer
87};
88static CborEncoder bs_root;
89static CborEncoder bs_rsp;
90
91int
92bs_cbor_writer(struct cbor_encoder_writer *cew, const char *data, int len)
93{
Marko Kiiskilace50ab02018-06-06 11:33:33 +030094 if (cew->bytes_written + len > sizeof(bs_obuf)) {
95 return CborErrorOutOfMemory;
96 }
97
Christopher Collins92ea77f2016-12-12 15:59:26 -080098 memcpy(&bs_obuf[cew->bytes_written], data, len);
99 cew->bytes_written += len;
100
101 return 0;
102}
103
104/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300105 * Convert version into string without use of snprintf().
Christopher Collins92ea77f2016-12-12 15:59:26 -0800106 */
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300107static int
108u32toa(char *tgt, uint32_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800109{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300110 char *dst;
111 uint32_t d = 1;
112 uint32_t dgt;
113 int n = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300115 dst = tgt;
116 while (val / d >= 10) {
117 d *= 10;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800118 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300119 while (d) {
120 dgt = val / d;
121 val %= d;
122 d /= 10;
123 if (n || dgt > 0 || d == 0) {
124 *dst++ = dgt + '0';
125 ++n;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800126 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800127 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300128 *dst = '\0';
129
130 return dst - tgt;
131}
132
133/*
134 * dst has to be able to fit "255.255.65535.4294967295" (25 characters).
135 */
136static void
137bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
138{
139 int off;
140
141 off = u32toa(dst, ver->iv_major);
142 dst[off++] = '.';
143 off += u32toa(dst + off, ver->iv_minor);
144 dst[off++] = '.';
145 off += u32toa(dst + off, ver->iv_revision);
146 dst[off++] = '.';
147 off += u32toa(dst + off, ver->iv_build_num);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800148}
149
150/*
151 * List images.
152 */
153static void
154bs_list(char *buf, int len)
155{
156 CborEncoder images;
157 CborEncoder image;
158 struct image_header hdr;
159 uint8_t tmpbuf[64];
160 int i, area_id;
161 const struct flash_area *fap;
162
163 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
164 cbor_encode_text_stringz(&bs_rsp, "images");
165 cbor_encoder_create_array(&bs_rsp, &images, CborIndefiniteLength);
166 for (i = 0; i < 2; i++) {
167 area_id = flash_area_id_from_image_slot(i);
168 if (flash_area_open(area_id, &fap)) {
169 continue;
170 }
171
172 flash_area_read(fap, 0, &hdr, sizeof(hdr));
173
174 if (hdr.ih_magic != IMAGE_MAGIC ||
175 bootutil_img_validate(&hdr, fap, tmpbuf, sizeof(tmpbuf),
176 NULL, 0, NULL)) {
177 flash_area_close(fap);
178 continue;
179 }
180 flash_area_close(fap);
181
182 cbor_encoder_create_map(&images, &image, CborIndefiniteLength);
183 cbor_encode_text_stringz(&image, "slot");
184 cbor_encode_int(&image, i);
185 cbor_encode_text_stringz(&image, "version");
186
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300187 bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800188 cbor_encode_text_stringz(&image, (char *)tmpbuf);
189 cbor_encoder_close_container(&images, &image);
190 }
191 cbor_encoder_close_container(&bs_rsp, &images);
192 cbor_encoder_close_container(&bs_root, &bs_rsp);
193 boot_serial_output();
194}
195
196/*
197 * Image upload request.
198 */
199static void
200bs_upload(char *buf, int len)
201{
202 CborParser parser;
203 struct cbor_buf_reader reader;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300204 struct CborValue root_value;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800205 struct CborValue value;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300206 uint8_t img_data[512];
207 long long int off = UINT_MAX;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800208 size_t img_blen = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300209 uint8_t rem_bytes;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300210 long long int data_len = UINT_MAX;
211 size_t slen;
212 char name_str[8];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800213 const struct flash_area *fap = NULL;
214 int rc;
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200215#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
216 static off_t off_last = -1;
217 struct flash_sector sector;
218#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800219
220 memset(img_data, 0, sizeof(img_data));
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300221
222 /*
223 * Expected data format.
224 * {
225 * "data":<img_data>
226 * "len":<image len>
227 * "off":<current offset of image data>
228 * }
229 */
230
231 /*
232 * Object comes within { ... }
233 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800234 cbor_buf_reader_init(&reader, (uint8_t *)buf, len);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200235#ifdef __ZEPHYR__
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300236 cbor_parser_cust_reader_init(&reader.r, 0, &parser, &root_value);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200237#else
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300238 cbor_parser_init(&reader.r, 0, &parser, &root_value);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200239#endif
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300240 if (!cbor_value_is_container(&root_value)) {
241 goto out_invalid_data;
242 }
243 if (cbor_value_enter_container(&root_value, &value)) {
244 goto out_invalid_data;
245 }
246 while (cbor_value_is_valid(&value)) {
247 /*
248 * Decode key.
249 */
250 if (cbor_value_calculate_string_length(&value, &slen)) {
251 goto out_invalid_data;
252 }
253 if (!cbor_value_is_text_string(&value) ||
254 slen >= sizeof(name_str) - 1) {
255 goto out_invalid_data;
256 }
257 if (cbor_value_copy_text_string(&value, name_str, &slen, &value)) {
258 goto out_invalid_data;
259 }
260 name_str[slen] = '\0';
261 if (!strcmp(name_str, "data")) {
262 /*
263 * Image data
264 */
265 if (value.type != CborByteStringType) {
266 goto out_invalid_data;
267 }
268 if (cbor_value_calculate_string_length(&value, &slen) ||
269 slen >= sizeof(img_data)) {
270 goto out_invalid_data;
271 }
272 if (cbor_value_copy_byte_string(&value, img_data, &slen, &value)) {
273 goto out_invalid_data;
274 }
275 img_blen = slen;
276 } else if (!strcmp(name_str, "off")) {
277 /*
278 * Offset of the data.
279 */
280 if (value.type != CborIntegerType) {
281 goto out_invalid_data;
282 }
283 if (cbor_value_get_int64(&value, &off)) {
284 goto out_invalid_data;
285 }
286 if (cbor_value_advance(&value)) {
287 goto out_invalid_data;
288 }
289 } else if (!strcmp(name_str, "len")) {
290 /*
291 * Length of the image. This should only be present in the first
292 * block of data; when offset is 0.
293 */
294 if (value.type != CborIntegerType) {
295 goto out_invalid_data;
296 }
297 if (cbor_value_get_int64(&value, &data_len)) {
298 goto out_invalid_data;
299 }
300 if (cbor_value_advance(&value)) {
301 goto out_invalid_data;
302 }
303 } else {
304 /*
305 * Unknown keys.
306 */
307 if (cbor_value_advance(&value)) {
308 goto out_invalid_data;
309 }
310 }
311 }
312 if (off == UINT_MAX) {
313 /*
314 * Offset must be set in every block.
315 */
316 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800317 }
318
Christopher Collins92ea77f2016-12-12 15:59:26 -0800319 rc = flash_area_open(flash_area_id_from_image_slot(0), &fap);
320 if (rc) {
321 rc = MGMT_ERR_EINVAL;
322 goto out;
323 }
324
325 if (off == 0) {
326 curr_off = 0;
327 if (data_len > fap->fa_size) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300328 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800329 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200330#ifndef CONFIG_BOOT_ERASE_PROGRESSIVELY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800331 rc = flash_area_erase(fap, 0, fap->fa_size);
332 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300333 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200335#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800336 img_size = data_len;
337 }
338 if (off != curr_off) {
339 rc = 0;
340 goto out;
341 }
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300342 if (curr_off + img_blen < img_size) {
343 rem_bytes = img_blen % flash_area_align(fap);
344 if (rem_bytes) {
345 img_blen -= rem_bytes;
346 }
347 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200348
349#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
350 rc = flash_area_sector_from_off(curr_off + img_blen, &sector);
351 if (rc) {
352 BOOT_LOG_ERR("Unable to determine flash sector size");
353 goto out;
354 }
355 if (off_last != sector.fs_off) {
356 off_last = sector.fs_off;
357 BOOT_LOG_INF("Moving to sector 0x%x", sector.fs_off);
358 rc = flash_area_erase(fap, sector.fs_off, sector.fs_size);
359 if (rc) {
360 BOOT_LOG_ERR("Error %d while erasing sector", rc);
361 goto out;
362 }
363 }
364#endif
365
366 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_blen);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800367 rc = flash_area_write(fap, curr_off, img_data, img_blen);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300368 if (rc == 0) {
369 curr_off += img_blen;
370 } else {
371 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800372 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200374
Christopher Collins92ea77f2016-12-12 15:59:26 -0800375out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200376 BOOT_LOG_INF("RX: 0x%x", rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800377 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
378 cbor_encode_text_stringz(&bs_rsp, "rc");
379 cbor_encode_int(&bs_rsp, rc);
380 if (rc == 0) {
381 cbor_encode_text_stringz(&bs_rsp, "off");
382 cbor_encode_uint(&bs_rsp, curr_off);
383 }
384 cbor_encoder_close_container(&bs_root, &bs_rsp);
385
386 boot_serial_output();
387 flash_area_close(fap);
388}
389
390/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300391 * Console echo control/image erase. Send empty response, don't do anything.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800392 */
393static void
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300394bs_empty_rsp(char *buf, int len)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800395{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300396 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
397 cbor_encode_text_stringz(&bs_rsp, "rc");
398 cbor_encode_int(&bs_rsp, 0);
399 cbor_encoder_close_container(&bs_root, &bs_rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800400 boot_serial_output();
401}
402
403/*
404 * Reset, and (presumably) boot to newly uploaded image. Flush console
405 * before restarting.
406 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200407static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800408bs_reset(char *buf, int len)
409{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300410 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800411
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200412#ifdef __ZEPHYR__
413 k_sleep(250);
414 sys_reboot(SYS_REBOOT_COLD);
415#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800416 os_cputime_delay_usecs(250000);
417 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200418#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800419}
420
421/*
422 * Parse incoming line of input from console.
423 * Expect newtmgr protocol with serial transport.
424 */
425void
426boot_serial_input(char *buf, int len)
427{
428 struct nmgr_hdr *hdr;
429
430 hdr = (struct nmgr_hdr *)buf;
431 if (len < sizeof(*hdr) ||
432 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
433 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
434 return;
435 }
436 bs_hdr = hdr;
437 hdr->nh_group = ntohs(hdr->nh_group);
438
439 buf += sizeof(*hdr);
440 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300441
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442 bs_writer.bytes_written = 0;
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200443#ifdef __ZEPHYR__
444 cbor_encoder_cust_writer_init(&bs_root, &bs_writer, 0);
445#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800446 cbor_encoder_init(&bs_root, &bs_writer, 0);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200447#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448
449 /*
450 * Limited support for commands.
451 */
452 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
453 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300454 case IMGMGR_NMGR_ID_STATE:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800455 bs_list(buf, len);
456 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300457 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458 bs_upload(buf, len);
459 break;
460 default:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300461 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800462 break;
463 }
464 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
465 switch (hdr->nh_id) {
466 case NMGR_ID_CONS_ECHO_CTRL:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300467 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 break;
469 case NMGR_ID_RESET:
470 bs_reset(buf, len);
471 break;
472 default:
473 break;
474 }
475 }
476}
477
478static void
479boot_serial_output(void)
480{
481 char *data;
482 int len;
483 uint16_t crc;
484 uint16_t totlen;
485 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
486 char buf[BOOT_SERIAL_OUT_MAX];
487 char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)];
488
489 data = bs_obuf;
490 len = bs_writer.bytes_written;
491
492 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300493 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800494 bs_hdr->nh_len = htons(len);
495 bs_hdr->nh_group = htons(bs_hdr->nh_group);
496
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200497#ifdef __ZEPHYR__
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300498 crc = crc16((u8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL,
499 CRC16_INITIAL_CRC, false);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200500 crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true);
501#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800502 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
503 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200504#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800505 crc = htons(crc);
506
Marko Kiiskila149b4572018-06-06 14:18:54 +0300507 boot_uf->write(pkt_start, sizeof(pkt_start));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800508
509 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
510 totlen = htons(totlen);
511
512 memcpy(buf, &totlen, sizeof(totlen));
513 totlen = sizeof(totlen);
514 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
515 totlen += sizeof(*bs_hdr);
516 memcpy(&buf[totlen], data, len);
517 totlen += len;
518 memcpy(&buf[totlen], &crc, sizeof(crc));
519 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200520#ifdef __ZEPHYR__
521 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +0200522 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200523 totlen = enc_len;
524#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800525 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200526#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +0300527 boot_uf->write(encoded_buf, totlen);
528 boot_uf->write("\n\r", 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200529 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530}
531
532/*
533 * Returns 1 if full packet has been received.
534 */
535static int
536boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
537{
538 int rc;
539 uint16_t crc;
540 uint16_t len;
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200541#ifdef __ZEPHYR__
542 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200543 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200544 if (err) {
545 return -1;
546 }
547#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548 if (*out_off + base64_decode_len(in) >= maxout) {
549 return -1;
550 }
551 rc = base64_decode(in, &out[*out_off]);
552 if (rc < 0) {
553 return -1;
554 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200555#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800556 *out_off += rc;
557
558 if (*out_off > sizeof(uint16_t)) {
559 len = ntohs(*(uint16_t *)out);
560
561 len = min(len, *out_off - sizeof(uint16_t));
562 out += sizeof(uint16_t);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200563#ifdef __ZEPHYR__
564 crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true);
565#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800566 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200567#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568 if (crc || len <= sizeof(crc)) {
569 return 0;
570 }
571 *out_off -= sizeof(crc);
572 out[*out_off] = '\0';
573
574 return 1;
575 }
576 return 0;
577}
578
579/*
580 * Task which waits reading console, expecting to get image over
581 * serial port.
582 */
583void
Marko Kiiskila149b4572018-06-06 14:18:54 +0300584boot_serial_start(const struct boot_uart_funcs *f)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800585{
586 int rc;
587 int off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800588 int dec_off;
589 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300590 int max_input;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800591
Marko Kiiskila149b4572018-06-06 14:18:54 +0300592 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300593 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800594
595 off = 0;
596 while (1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200597 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800598 if (rc <= 0 && !full_line) {
599 continue;
600 }
601 off += rc;
602 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300603 if (off == max_input) {
604 /*
605 * Full line, no newline yet. Reset the input buffer.
606 */
607 off = 0;
608 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800609 continue;
610 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200611 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
612 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800613 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200614 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
615 } else if (in_buf[0] == SHELL_NLIP_DATA_START1 &&
616 in_buf[1] == SHELL_NLIP_DATA_START2) {
617 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800618 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200619
620 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800621 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200622 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800623 }
624 off = 0;
625 }
626}