blob: 9b63043af44e7759a8e4fe1fd1cead3a4cc480a5 [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];
74static const 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;
215
216 memset(img_data, 0, sizeof(img_data));
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300217
218 /*
219 * Expected data format.
220 * {
221 * "data":<img_data>
222 * "len":<image len>
223 * "off":<current offset of image data>
224 * }
225 */
226
227 /*
228 * Object comes within { ... }
229 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800230 cbor_buf_reader_init(&reader, (uint8_t *)buf, len);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200231#ifdef __ZEPHYR__
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300232 cbor_parser_cust_reader_init(&reader.r, 0, &parser, &root_value);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200233#else
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300234 cbor_parser_init(&reader.r, 0, &parser, &root_value);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200235#endif
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300236 if (!cbor_value_is_container(&root_value)) {
237 goto out_invalid_data;
238 }
239 if (cbor_value_enter_container(&root_value, &value)) {
240 goto out_invalid_data;
241 }
242 while (cbor_value_is_valid(&value)) {
243 /*
244 * Decode key.
245 */
246 if (cbor_value_calculate_string_length(&value, &slen)) {
247 goto out_invalid_data;
248 }
249 if (!cbor_value_is_text_string(&value) ||
250 slen >= sizeof(name_str) - 1) {
251 goto out_invalid_data;
252 }
253 if (cbor_value_copy_text_string(&value, name_str, &slen, &value)) {
254 goto out_invalid_data;
255 }
256 name_str[slen] = '\0';
257 if (!strcmp(name_str, "data")) {
258 /*
259 * Image data
260 */
261 if (value.type != CborByteStringType) {
262 goto out_invalid_data;
263 }
264 if (cbor_value_calculate_string_length(&value, &slen) ||
265 slen >= sizeof(img_data)) {
266 goto out_invalid_data;
267 }
268 if (cbor_value_copy_byte_string(&value, img_data, &slen, &value)) {
269 goto out_invalid_data;
270 }
271 img_blen = slen;
272 } else if (!strcmp(name_str, "off")) {
273 /*
274 * Offset of the data.
275 */
276 if (value.type != CborIntegerType) {
277 goto out_invalid_data;
278 }
279 if (cbor_value_get_int64(&value, &off)) {
280 goto out_invalid_data;
281 }
282 if (cbor_value_advance(&value)) {
283 goto out_invalid_data;
284 }
285 } else if (!strcmp(name_str, "len")) {
286 /*
287 * Length of the image. This should only be present in the first
288 * block of data; when offset is 0.
289 */
290 if (value.type != CborIntegerType) {
291 goto out_invalid_data;
292 }
293 if (cbor_value_get_int64(&value, &data_len)) {
294 goto out_invalid_data;
295 }
296 if (cbor_value_advance(&value)) {
297 goto out_invalid_data;
298 }
299 } else {
300 /*
301 * Unknown keys.
302 */
303 if (cbor_value_advance(&value)) {
304 goto out_invalid_data;
305 }
306 }
307 }
308 if (off == UINT_MAX) {
309 /*
310 * Offset must be set in every block.
311 */
312 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313 }
314
Christopher Collins92ea77f2016-12-12 15:59:26 -0800315 rc = flash_area_open(flash_area_id_from_image_slot(0), &fap);
316 if (rc) {
317 rc = MGMT_ERR_EINVAL;
318 goto out;
319 }
320
321 if (off == 0) {
322 curr_off = 0;
323 if (data_len > fap->fa_size) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300324 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800325 }
326 rc = flash_area_erase(fap, 0, fap->fa_size);
327 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300328 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800329 }
330 img_size = data_len;
331 }
332 if (off != curr_off) {
333 rc = 0;
334 goto out;
335 }
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300336 if (curr_off + img_blen < img_size) {
337 rem_bytes = img_blen % flash_area_align(fap);
338 if (rem_bytes) {
339 img_blen -= rem_bytes;
340 }
341 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800342 rc = flash_area_write(fap, curr_off, img_data, img_blen);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300343 if (rc == 0) {
344 curr_off += img_blen;
345 } else {
346 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200350 BOOT_LOG_INF("RX: 0x%x", rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800351 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
352 cbor_encode_text_stringz(&bs_rsp, "rc");
353 cbor_encode_int(&bs_rsp, rc);
354 if (rc == 0) {
355 cbor_encode_text_stringz(&bs_rsp, "off");
356 cbor_encode_uint(&bs_rsp, curr_off);
357 }
358 cbor_encoder_close_container(&bs_root, &bs_rsp);
359
360 boot_serial_output();
361 flash_area_close(fap);
362}
363
364/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300365 * Console echo control/image erase. Send empty response, don't do anything.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366 */
367static void
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300368bs_empty_rsp(char *buf, int len)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800369{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300370 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
371 cbor_encode_text_stringz(&bs_rsp, "rc");
372 cbor_encode_int(&bs_rsp, 0);
373 cbor_encoder_close_container(&bs_root, &bs_rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800374 boot_serial_output();
375}
376
377/*
378 * Reset, and (presumably) boot to newly uploaded image. Flush console
379 * before restarting.
380 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200381static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382bs_reset(char *buf, int len)
383{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300384 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800385
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200386#ifdef __ZEPHYR__
387 k_sleep(250);
388 sys_reboot(SYS_REBOOT_COLD);
389#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800390 os_cputime_delay_usecs(250000);
391 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200392#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800393}
394
395/*
396 * Parse incoming line of input from console.
397 * Expect newtmgr protocol with serial transport.
398 */
399void
400boot_serial_input(char *buf, int len)
401{
402 struct nmgr_hdr *hdr;
403
404 hdr = (struct nmgr_hdr *)buf;
405 if (len < sizeof(*hdr) ||
406 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
407 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
408 return;
409 }
410 bs_hdr = hdr;
411 hdr->nh_group = ntohs(hdr->nh_group);
412
413 buf += sizeof(*hdr);
414 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300415
Christopher Collins92ea77f2016-12-12 15:59:26 -0800416 bs_writer.bytes_written = 0;
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200417#ifdef __ZEPHYR__
418 cbor_encoder_cust_writer_init(&bs_root, &bs_writer, 0);
419#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800420 cbor_encoder_init(&bs_root, &bs_writer, 0);
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +0200421#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422
423 /*
424 * Limited support for commands.
425 */
426 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
427 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300428 case IMGMGR_NMGR_ID_STATE:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800429 bs_list(buf, len);
430 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300431 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800432 bs_upload(buf, len);
433 break;
434 default:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300435 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436 break;
437 }
438 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
439 switch (hdr->nh_id) {
440 case NMGR_ID_CONS_ECHO_CTRL:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300441 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442 break;
443 case NMGR_ID_RESET:
444 bs_reset(buf, len);
445 break;
446 default:
447 break;
448 }
449 }
450}
451
452static void
453boot_serial_output(void)
454{
455 char *data;
456 int len;
457 uint16_t crc;
458 uint16_t totlen;
459 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
460 char buf[BOOT_SERIAL_OUT_MAX];
461 char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)];
462
463 data = bs_obuf;
464 len = bs_writer.bytes_written;
465
466 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300467 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 bs_hdr->nh_len = htons(len);
469 bs_hdr->nh_group = htons(bs_hdr->nh_group);
470
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200471#ifdef __ZEPHYR__
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300472 crc = crc16((u8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL,
473 CRC16_INITIAL_CRC, false);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200474 crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true);
475#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800476 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
477 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200478#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800479 crc = htons(crc);
480
Marko Kiiskila149b4572018-06-06 14:18:54 +0300481 boot_uf->write(pkt_start, sizeof(pkt_start));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800482
483 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
484 totlen = htons(totlen);
485
486 memcpy(buf, &totlen, sizeof(totlen));
487 totlen = sizeof(totlen);
488 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
489 totlen += sizeof(*bs_hdr);
490 memcpy(&buf[totlen], data, len);
491 totlen += len;
492 memcpy(&buf[totlen], &crc, sizeof(crc));
493 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200494#ifdef __ZEPHYR__
495 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +0200496 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200497 totlen = enc_len;
498#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800499 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200500#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +0300501 boot_uf->write(encoded_buf, totlen);
502 boot_uf->write("\n\r", 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200503 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800504}
505
506/*
507 * Returns 1 if full packet has been received.
508 */
509static int
510boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
511{
512 int rc;
513 uint16_t crc;
514 uint16_t len;
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200515#ifdef __ZEPHYR__
516 int err;
Carles Cufi0165be82018-03-26 17:43:51 +0200517 err = base64_decode( &out[*out_off], maxout, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200518 if (err) {
519 return -1;
520 }
521#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800522 if (*out_off + base64_decode_len(in) >= maxout) {
523 return -1;
524 }
525 rc = base64_decode(in, &out[*out_off]);
526 if (rc < 0) {
527 return -1;
528 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200529#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530 *out_off += rc;
531
532 if (*out_off > sizeof(uint16_t)) {
533 len = ntohs(*(uint16_t *)out);
534
535 len = min(len, *out_off - sizeof(uint16_t));
536 out += sizeof(uint16_t);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200537#ifdef __ZEPHYR__
538 crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true);
539#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800540 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200541#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800542 if (crc || len <= sizeof(crc)) {
543 return 0;
544 }
545 *out_off -= sizeof(crc);
546 out[*out_off] = '\0';
547
548 return 1;
549 }
550 return 0;
551}
552
553/*
554 * Task which waits reading console, expecting to get image over
555 * serial port.
556 */
557void
Marko Kiiskila149b4572018-06-06 14:18:54 +0300558boot_serial_start(const struct boot_uart_funcs *f)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800559{
560 int rc;
561 int off;
562 char *buf;
563 char *dec;
564 int dec_off;
565 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300566 int max_input;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800567
Marko Kiiskila149b4572018-06-06 14:18:54 +0300568 boot_uf = f;
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200569 buf = in_buf;
570 dec = dec_buf;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300571 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800572
573 off = 0;
574 while (1) {
Marko Kiiskila149b4572018-06-06 14:18:54 +0300575 rc = f->read(buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576 if (rc <= 0 && !full_line) {
577 continue;
578 }
579 off += rc;
580 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300581 if (off == max_input) {
582 /*
583 * Full line, no newline yet. Reset the input buffer.
584 */
585 off = 0;
586 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587 continue;
588 }
589 if (buf[0] == SHELL_NLIP_PKT_START1 &&
590 buf[1] == SHELL_NLIP_PKT_START2) {
591 dec_off = 0;
592 rc = boot_serial_in_dec(&buf[2], off - 2, dec, &dec_off, max_input);
593 } else if (buf[0] == SHELL_NLIP_DATA_START1 &&
594 buf[1] == SHELL_NLIP_DATA_START2) {
595 rc = boot_serial_in_dec(&buf[2], off - 2, dec, &dec_off, max_input);
596 }
597 if (rc == 1) {
598 boot_serial_input(&dec[2], dec_off - 2);
599 }
600 off = 0;
601 }
602}