blob: 13af2b3fd5a05586dd267bebe5290968f398b49b [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>
Dominik Ermel470e2f32020-01-10 13:28:48 +000036#include <tinycbor/cbor.h>
37#include <tinycbor/cbor_buf_reader.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020038#else
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include <bsp/bsp.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080040#include <hal/hal_system.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080041#include <os/endian.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080042#include <os/os_cputime.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020043#include <crc/crc16.h>
44#include <base64/base64.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080045#include <tinycbor/cbor.h>
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +020046#endif /* __ZEPHYR__ */
47
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020048#include <flash_map_backend/flash_map_backend.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020049#include <hal/hal_flash.h>
50#include <os/os.h>
51#include <os/os_malloc.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080052
53#include <bootutil/image.h>
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
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +010062#include "serial_recovery_cbor.h"
63
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010064MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
65
Marko Kiiskila149b4572018-06-06 14:18:54 +030066#define BOOT_SERIAL_INPUT_MAX 512
Fabio Utzig6f49c272019-08-23 11:42:58 -030067#define BOOT_SERIAL_OUT_MAX 128
Christopher Collins92ea77f2016-12-12 15:59:26 -080068
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020069#ifdef __ZEPHYR__
Carles Cufi0165be82018-03-26 17:43:51 +020070/* base64 lib encodes data to null-terminated string */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020071#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
72
73#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
74#define CRC_CITT_POLYMINAL 0x1021
75
76#define ntohs(x) sys_be16_to_cpu(x)
77#define htons(x) sys_cpu_to_be16(x)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020078#endif
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010079
Fabio Utzig6f49c272019-08-23 11:42:58 -030080#ifndef BOOT_IMAGE_NUMBER
81#define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER
82#endif
83
84#if (BOOT_IMAGE_NUMBER > 1)
85#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
86#else
87#define IMAGES_ITER(x)
88#endif
89
Marko Kiiskila149b4572018-06-06 14:18:54 +030090static char in_buf[BOOT_SERIAL_INPUT_MAX + 1];
91static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1];
Marko Kiiskila8b1ce3a2018-06-14 13:20:46 -070092const struct boot_uart_funcs *boot_uf;
Christopher Collins92ea77f2016-12-12 15:59:26 -080093static uint32_t curr_off;
94static uint32_t img_size;
95static struct nmgr_hdr *bs_hdr;
96
97static char bs_obuf[BOOT_SERIAL_OUT_MAX];
98
99static int bs_cbor_writer(struct cbor_encoder_writer *, const char *data,
100 int len);
101static void boot_serial_output(void);
102
103static struct cbor_encoder_writer bs_writer = {
104 .write = bs_cbor_writer
105};
106static CborEncoder bs_root;
107static CborEncoder bs_rsp;
108
109int
110bs_cbor_writer(struct cbor_encoder_writer *cew, const char *data, int len)
111{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300112 if (cew->bytes_written + len > sizeof(bs_obuf)) {
113 return CborErrorOutOfMemory;
114 }
115
Christopher Collins92ea77f2016-12-12 15:59:26 -0800116 memcpy(&bs_obuf[cew->bytes_written], data, len);
117 cew->bytes_written += len;
118
119 return 0;
120}
121
122/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300123 * Convert version into string without use of snprintf().
Christopher Collins92ea77f2016-12-12 15:59:26 -0800124 */
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300125static int
126u32toa(char *tgt, uint32_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800127{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300128 char *dst;
129 uint32_t d = 1;
130 uint32_t dgt;
131 int n = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800132
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300133 dst = tgt;
134 while (val / d >= 10) {
135 d *= 10;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800136 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300137 while (d) {
138 dgt = val / d;
139 val %= d;
140 d /= 10;
141 if (n || dgt > 0 || d == 0) {
142 *dst++ = dgt + '0';
143 ++n;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800144 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800145 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300146 *dst = '\0';
147
148 return dst - tgt;
149}
150
151/*
152 * dst has to be able to fit "255.255.65535.4294967295" (25 characters).
153 */
154static void
155bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
156{
157 int off;
158
159 off = u32toa(dst, ver->iv_major);
160 dst[off++] = '.';
161 off += u32toa(dst + off, ver->iv_minor);
162 dst[off++] = '.';
163 off += u32toa(dst + off, ver->iv_revision);
164 dst[off++] = '.';
165 off += u32toa(dst + off, ver->iv_build_num);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166}
167
168/*
169 * List images.
170 */
171static void
172bs_list(char *buf, int len)
173{
174 CborEncoder images;
175 CborEncoder image;
176 struct image_header hdr;
177 uint8_t tmpbuf[64];
Fabio Utzig6f49c272019-08-23 11:42:58 -0300178 int slot, area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800179 const struct flash_area *fap;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300180 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800181
182 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
183 cbor_encode_text_stringz(&bs_rsp, "images");
184 cbor_encoder_create_array(&bs_rsp, &images, CborIndefiniteLength);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300185 image_index = 0;
186 IMAGES_ITER(image_index) {
187 for (slot = 0; slot < 2; slot++) {
188 area_id = flash_area_id_from_multi_image_slot(image_index, slot);
189 if (flash_area_open(area_id, &fap)) {
190 continue;
191 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800192
Fabio Utzig6f49c272019-08-23 11:42:58 -0300193 flash_area_read(fap, 0, &hdr, sizeof(hdr));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194
Fabio Utzig6f49c272019-08-23 11:42:58 -0300195 if (hdr.ih_magic != IMAGE_MAGIC ||
196 bootutil_img_validate(NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
197 NULL, 0, NULL)) {
198 flash_area_close(fap);
199 continue;
200 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201 flash_area_close(fap);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300202
203 cbor_encoder_create_map(&images, &image, CborIndefiniteLength);
204
205#if (BOOT_IMAGE_NUMBER > 1)
206 cbor_encode_text_stringz(&image, "image");
207 cbor_encode_int(&image, image_index);
208#endif
209
210 cbor_encode_text_stringz(&image, "slot");
211 cbor_encode_int(&image, slot);
212 cbor_encode_text_stringz(&image, "version");
213
214 bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
215 cbor_encode_text_stringz(&image, (char *)tmpbuf);
216 cbor_encoder_close_container(&images, &image);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800217 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800218 }
219 cbor_encoder_close_container(&bs_rsp, &images);
220 cbor_encoder_close_container(&bs_root, &bs_rsp);
221 boot_serial_output();
222}
223
224/*
225 * Image upload request.
226 */
227static void
228bs_upload(char *buf, int len)
229{
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100230 const uint8_t *img_data = NULL;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300231 long long int off = UINT_MAX;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800232 size_t img_blen = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300233 uint8_t rem_bytes;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300234 long long int data_len = UINT_MAX;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300235 int img_num;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300236 size_t slen;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800237 const struct flash_area *fap = NULL;
238 int rc;
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200239#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
240 static off_t off_last = -1;
241 struct flash_sector sector;
242#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800243
Fabio Utzig6f49c272019-08-23 11:42:58 -0300244 img_num = 0;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300245
246 /*
247 * Expected data format.
248 * {
Fabio Utzig6f49c272019-08-23 11:42:58 -0300249 * "image":<image number in a multi-image set (OPTIONAL)>
250 * "data":<image data>
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300251 * "len":<image len>
252 * "off":<current offset of image data>
253 * }
254 */
255
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100256 Upload_t upload;
257 if (!cbor_decode_Upload((const uint8_t *)buf, len, &upload)) {
258 goto out_invalid_data;
259 }
Dominik Ermel470e2f32020-01-10 13:28:48 +0000260
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100261 for (int i = 0; i < upload._Upload_members_count; i++) {
262 _Member_t *member = &upload._Upload_members[i];
263 switch(member->_Member_choice) {
264 case _Member_image:
265 img_num = member->_Member_image;
266 break;
267 case _Member_data:
268 img_data = member->_Member_data.value;
269 slen = member->_Member_data.len;
270 img_blen = slen;
271 break;
272 case _Member_len:
273 data_len = member->_Member_len;
274 break;
275 case _Member_off:
276 off = member->_Member_off;
277 break;
278 case _Member_sha:
279 default:
280 /* Nothing to do. */
281 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300282 }
283 }
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100284
285 if (off == UINT_MAX || img_data == NULL) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300286 /*
287 * Offset must be set in every block.
288 */
289 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800290 }
291
Fabio Utzig6f49c272019-08-23 11:42:58 -0300292 rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800293 if (rc) {
294 rc = MGMT_ERR_EINVAL;
295 goto out;
296 }
297
298 if (off == 0) {
299 curr_off = 0;
300 if (data_len > fap->fa_size) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300301 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200303#ifndef CONFIG_BOOT_ERASE_PROGRESSIVELY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800304 rc = flash_area_erase(fap, 0, fap->fa_size);
305 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300306 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800307 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200308#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800309 img_size = data_len;
310 }
311 if (off != curr_off) {
312 rc = 0;
313 goto out;
314 }
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300315 if (curr_off + img_blen < img_size) {
316 rem_bytes = img_blen % flash_area_align(fap);
317 if (rem_bytes) {
318 img_blen -= rem_bytes;
319 }
320 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200321
322#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
323 rc = flash_area_sector_from_off(curr_off + img_blen, &sector);
324 if (rc) {
325 BOOT_LOG_ERR("Unable to determine flash sector size");
326 goto out;
327 }
328 if (off_last != sector.fs_off) {
329 off_last = sector.fs_off;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200330 BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off);
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200331 rc = flash_area_erase(fap, sector.fs_off, sector.fs_size);
332 if (rc) {
333 BOOT_LOG_ERR("Error %d while erasing sector", rc);
334 goto out;
335 }
336 }
337#endif
338
339 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_blen);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800340 rc = flash_area_write(fap, curr_off, img_data, img_blen);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300341 if (rc == 0) {
342 curr_off += img_blen;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200343#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
344 if (curr_off == img_size) {
345 /* get the last sector offset */
346 rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
347 if (rc) {
348 BOOT_LOG_ERR("Unable to determine flash sector of"
349 "the image trailer");
350 goto out;
351 }
352 /* Assure that sector for image trailer was erased. */
353 /* Check whether it was erased during previous upload. */
354 if (off_last < sector.fs_off) {
355 BOOT_LOG_INF("Erasing sector at offset 0x%x", sector.fs_off);
356 rc = flash_area_erase(fap, sector.fs_off, sector.fs_size);
357 if (rc) {
358 BOOT_LOG_ERR("Error %d while erasing sector", rc);
359 goto out;
360 }
361 }
362 }
363#endif
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300364 } else {
365 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800367 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200368
Christopher Collins92ea77f2016-12-12 15:59:26 -0800369out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200370 BOOT_LOG_INF("RX: 0x%x", rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
372 cbor_encode_text_stringz(&bs_rsp, "rc");
373 cbor_encode_int(&bs_rsp, rc);
374 if (rc == 0) {
375 cbor_encode_text_stringz(&bs_rsp, "off");
376 cbor_encode_uint(&bs_rsp, curr_off);
377 }
378 cbor_encoder_close_container(&bs_root, &bs_rsp);
379
380 boot_serial_output();
381 flash_area_close(fap);
382}
383
384/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300385 * Console echo control/image erase. Send empty response, don't do anything.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800386 */
387static void
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300388bs_empty_rsp(char *buf, int len)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800389{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300390 cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
391 cbor_encode_text_stringz(&bs_rsp, "rc");
392 cbor_encode_int(&bs_rsp, 0);
393 cbor_encoder_close_container(&bs_root, &bs_rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800394 boot_serial_output();
395}
396
397/*
398 * Reset, and (presumably) boot to newly uploaded image. Flush console
399 * before restarting.
400 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200401static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800402bs_reset(char *buf, int len)
403{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300404 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800405
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200406#ifdef __ZEPHYR__
407 k_sleep(250);
408 sys_reboot(SYS_REBOOT_COLD);
409#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410 os_cputime_delay_usecs(250000);
411 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200412#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800413}
414
415/*
416 * Parse incoming line of input from console.
417 * Expect newtmgr protocol with serial transport.
418 */
419void
420boot_serial_input(char *buf, int len)
421{
422 struct nmgr_hdr *hdr;
423
424 hdr = (struct nmgr_hdr *)buf;
425 if (len < sizeof(*hdr) ||
426 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
427 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
428 return;
429 }
430 bs_hdr = hdr;
431 hdr->nh_group = ntohs(hdr->nh_group);
432
433 buf += sizeof(*hdr);
434 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300435
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436 bs_writer.bytes_written = 0;
437 cbor_encoder_init(&bs_root, &bs_writer, 0);
438
439 /*
440 * Limited support for commands.
441 */
442 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
443 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300444 case IMGMGR_NMGR_ID_STATE:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800445 bs_list(buf, len);
446 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300447 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448 bs_upload(buf, len);
449 break;
450 default:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300451 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452 break;
453 }
454 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
455 switch (hdr->nh_id) {
456 case NMGR_ID_CONS_ECHO_CTRL:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300457 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458 break;
459 case NMGR_ID_RESET:
460 bs_reset(buf, len);
461 break;
462 default:
463 break;
464 }
465 }
466}
467
468static void
469boot_serial_output(void)
470{
471 char *data;
472 int len;
473 uint16_t crc;
474 uint16_t totlen;
475 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
476 char buf[BOOT_SERIAL_OUT_MAX];
477 char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)];
478
479 data = bs_obuf;
480 len = bs_writer.bytes_written;
481
482 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300483 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484 bs_hdr->nh_len = htons(len);
485 bs_hdr->nh_group = htons(bs_hdr->nh_group);
486
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200487#ifdef __ZEPHYR__
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300488 crc = crc16((u8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL,
489 CRC16_INITIAL_CRC, false);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200490 crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true);
491#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800492 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
493 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200494#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800495 crc = htons(crc);
496
Marko Kiiskila149b4572018-06-06 14:18:54 +0300497 boot_uf->write(pkt_start, sizeof(pkt_start));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800498
499 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
500 totlen = htons(totlen);
501
502 memcpy(buf, &totlen, sizeof(totlen));
503 totlen = sizeof(totlen);
504 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
505 totlen += sizeof(*bs_hdr);
506 memcpy(&buf[totlen], data, len);
507 totlen += len;
508 memcpy(&buf[totlen], &crc, sizeof(crc));
509 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200510#ifdef __ZEPHYR__
511 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +0200512 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200513 totlen = enc_len;
514#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800515 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200516#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +0300517 boot_uf->write(encoded_buf, totlen);
518 boot_uf->write("\n\r", 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200519 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800520}
521
522/*
523 * Returns 1 if full packet has been received.
524 */
525static int
526boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
527{
528 int rc;
529 uint16_t crc;
530 uint16_t len;
Marko Kiiskilae5aeee42018-12-21 15:00:16 +0200531
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200532#ifdef __ZEPHYR__
533 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200534 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200535 if (err) {
536 return -1;
537 }
538#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539 if (*out_off + base64_decode_len(in) >= maxout) {
540 return -1;
541 }
542 rc = base64_decode(in, &out[*out_off]);
543 if (rc < 0) {
544 return -1;
545 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200546#endif
Fabio Utzig6f49c272019-08-23 11:42:58 -0300547
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548 *out_off += rc;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300549 if (*out_off <= sizeof(uint16_t)) {
550 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551 }
Fabio Utzig6f49c272019-08-23 11:42:58 -0300552
553 len = ntohs(*(uint16_t *)out);
554 if (len != *out_off - sizeof(uint16_t)) {
555 return 0;
556 }
557
558 if (len > *out_off - sizeof(uint16_t)) {
559 len = *out_off - sizeof(uint16_t);
560 }
561
562 out += sizeof(uint16_t);
563#ifdef __ZEPHYR__
564 crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true);
565#else
566 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
567#endif
568 if (crc || len <= sizeof(crc)) {
569 return 0;
570 }
571 *out_off -= sizeof(crc);
572 out[*out_off] = '\0';
573
574 return 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800575}
576
577/*
578 * Task which waits reading console, expecting to get image over
579 * serial port.
580 */
581void
Marko Kiiskila149b4572018-06-06 14:18:54 +0300582boot_serial_start(const struct boot_uart_funcs *f)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800583{
584 int rc;
585 int off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800586 int dec_off;
587 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300588 int max_input;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800589
Marko Kiiskila149b4572018-06-06 14:18:54 +0300590 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300591 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800592
593 off = 0;
594 while (1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200595 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596 if (rc <= 0 && !full_line) {
597 continue;
598 }
599 off += rc;
600 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300601 if (off == max_input) {
602 /*
603 * Full line, no newline yet. Reset the input buffer.
604 */
605 off = 0;
606 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800607 continue;
608 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200609 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
610 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800611 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200612 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
613 } else if (in_buf[0] == SHELL_NLIP_DATA_START1 &&
614 in_buf[1] == SHELL_NLIP_DATA_START2) {
615 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800616 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200617
618 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800619 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200620 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800621 }
622 off = 0;
623 }
624}