blob: 979e98ddcdd9300affab0b68999cda5b9b9f1cdd [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"
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +010028#include "cbor_encode.h"
Fabio Utzig1a2e41a2017-11-17 12:13:09 -020029
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020030#ifdef __ZEPHYR__
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +020031#include <power/reboot.h>
Andrzej Puzdrowskif1d189c2019-12-12 09:34:11 +010032#include <sys/byteorder.h>
33#include <sys/__assert.h>
Peter Bigot54c1e3f2020-01-25 05:50:12 -060034#include <drivers/flash.h>
Andrzej Puzdrowskif1d189c2019-12-12 09:34:11 +010035#include <sys/crc.h>
36#include <sys/base64.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>
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +020044#endif /* __ZEPHYR__ */
45
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020046#include <flash_map_backend/flash_map_backend.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020047#include <hal/hal_flash.h>
48#include <os/os.h>
49#include <os/os_malloc.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080050
51#include <bootutil/image.h>
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +020052#include <bootutil/bootutil.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080053
54#include "boot_serial/boot_serial.h"
55#include "boot_serial_priv.h"
56
Dominik Ermel3d4e55d2021-07-09 11:14:10 +000057#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +020058#include "bootutil_priv.h"
59#endif
60
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +010061#include "serial_recovery_cbor.h"
62
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020063BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010064
Marko Kiiskila149b4572018-06-06 14:18:54 +030065#define BOOT_SERIAL_INPUT_MAX 512
Fabio Utzig6f49c272019-08-23 11:42:58 -030066#define BOOT_SERIAL_OUT_MAX 128
Christopher Collins92ea77f2016-12-12 15:59:26 -080067
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020068#ifdef __ZEPHYR__
Carles Cufi0165be82018-03-26 17:43:51 +020069/* base64 lib encodes data to null-terminated string */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020070#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
71
72#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
73#define CRC_CITT_POLYMINAL 0x1021
74
75#define ntohs(x) sys_be16_to_cpu(x)
76#define htons(x) sys_cpu_to_be16(x)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020077#endif
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010078
Fabio Utzig6f49c272019-08-23 11:42:58 -030079#ifndef BOOT_IMAGE_NUMBER
80#define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER
81#endif
82
83#if (BOOT_IMAGE_NUMBER > 1)
84#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
85#else
86#define IMAGES_ITER(x)
87#endif
88
Marko Kiiskila149b4572018-06-06 14:18:54 +030089static char in_buf[BOOT_SERIAL_INPUT_MAX + 1];
90static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1];
Marko Kiiskila8b1ce3a2018-06-14 13:20:46 -070091const struct boot_uart_funcs *boot_uf;
Christopher Collins92ea77f2016-12-12 15:59:26 -080092static uint32_t curr_off;
93static uint32_t img_size;
94static struct nmgr_hdr *bs_hdr;
95
96static char bs_obuf[BOOT_SERIAL_OUT_MAX];
97
Christopher Collins92ea77f2016-12-12 15:59:26 -080098static void boot_serial_output(void);
99
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100100static cbor_state_backups_t dummy_backups;
101static cbor_state_t cbor_state = {
102 .backups = &dummy_backups
Christopher Collins92ea77f2016-12-12 15:59:26 -0800103};
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104
Dominik Ermel3d51e432021-06-25 17:29:50 +0000105/**
Dominik Ermelbd69c3d2021-07-28 11:27:31 +0000106 * Function that processes MGMT_GROUP_ID_PERUSER mcumgr group and may be
107 * used to process any groups that have not been processed by generic boot
108 * serial implementation.
Dominik Ermel3d51e432021-06-25 17:29:50 +0000109 *
110 * @param[in] hdr -- the decoded header of mcumgr message;
111 * @param[in] buffer -- buffer with first mcumgr message;
112 * @param[in] len -- length of of data in buffer;
113 * @param[out] *cs -- object with encoded response.
114 *
115 * @return 0 on success; non-0 error code otherwise.
116 */
117extern int bs_peruser_system_specific(const struct nmgr_hdr *hdr,
118 const char *buffer,
119 int len, cbor_state_t *cs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120
121/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300122 * Convert version into string without use of snprintf().
Christopher Collins92ea77f2016-12-12 15:59:26 -0800123 */
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300124static int
125u32toa(char *tgt, uint32_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800126{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300127 char *dst;
128 uint32_t d = 1;
129 uint32_t dgt;
130 int n = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800131
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300132 dst = tgt;
133 while (val / d >= 10) {
134 d *= 10;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300136 while (d) {
137 dgt = val / d;
138 val %= d;
139 d /= 10;
140 if (n || dgt > 0 || d == 0) {
141 *dst++ = dgt + '0';
142 ++n;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800143 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800144 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300145 *dst = '\0';
146
147 return dst - tgt;
148}
149
150/*
151 * dst has to be able to fit "255.255.65535.4294967295" (25 characters).
152 */
153static void
154bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
155{
156 int off;
157
158 off = u32toa(dst, ver->iv_major);
159 dst[off++] = '.';
160 off += u32toa(dst + off, ver->iv_minor);
161 dst[off++] = '.';
162 off += u32toa(dst + off, ver->iv_revision);
163 dst[off++] = '.';
164 off += u32toa(dst + off, ver->iv_build_num);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800165}
166
167/*
168 * List images.
169 */
170static void
171bs_list(char *buf, int len)
172{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173 struct image_header hdr;
174 uint8_t tmpbuf[64];
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100175 uint32_t slot, area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176 const struct flash_area *fap;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300177 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800178
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100179 map_start_encode(&cbor_state, 1);
180 tstrx_put(&cbor_state, "images");
181 list_start_encode(&cbor_state, 5);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300182 image_index = 0;
183 IMAGES_ITER(image_index) {
184 for (slot = 0; slot < 2; slot++) {
185 area_id = flash_area_id_from_multi_image_slot(image_index, slot);
186 if (flash_area_open(area_id, &fap)) {
187 continue;
188 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189
Fabio Utzig6f49c272019-08-23 11:42:58 -0300190 flash_area_read(fap, 0, &hdr, sizeof(hdr));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800191
Fabio Utzig6f49c272019-08-23 11:42:58 -0300192 if (hdr.ih_magic != IMAGE_MAGIC ||
193 bootutil_img_validate(NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
194 NULL, 0, NULL)) {
195 flash_area_close(fap);
196 continue;
197 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800198 flash_area_close(fap);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300199
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100200 map_start_encode(&cbor_state, 20);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300201
202#if (BOOT_IMAGE_NUMBER > 1)
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100203 tstrx_put(&cbor_state, "image");
204 uintx32_put(&cbor_state, image_index);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300205#endif
206
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100207 tstrx_put(&cbor_state, "slot");
208 uintx32_put(&cbor_state, slot);
209 tstrx_put(&cbor_state, "version");
Fabio Utzig6f49c272019-08-23 11:42:58 -0300210
211 bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100212 tstrx_put_term(&cbor_state, (char *)tmpbuf);
213 map_end_encode(&cbor_state, 20);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800214 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215 }
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100216 list_end_encode(&cbor_state, 5);
217 map_end_encode(&cbor_state, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800218 boot_serial_output();
219}
220
221/*
222 * Image upload request.
223 */
224static void
225bs_upload(char *buf, int len)
226{
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100227 const uint8_t *img_data = NULL;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100228 long long int off = UINT64_MAX;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800229 size_t img_blen = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300230 uint8_t rem_bytes;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100231 long long int data_len = UINT64_MAX;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300232 int img_num;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300233 size_t slen;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800234 const struct flash_area *fap = NULL;
235 int rc;
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000236#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200237 static off_t off_last = -1;
238 struct flash_sector sector;
239#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800240
Fabio Utzig6f49c272019-08-23 11:42:58 -0300241 img_num = 0;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300242
243 /*
244 * Expected data format.
245 * {
Fabio Utzig6f49c272019-08-23 11:42:58 -0300246 * "image":<image number in a multi-image set (OPTIONAL)>
247 * "data":<image data>
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300248 * "len":<image len>
249 * "off":<current offset of image data>
250 * }
251 */
252
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200253 struct Upload upload;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100254 uint32_t decoded_len;
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200255 bool result = cbor_decode_Upload((const uint8_t *)buf, len, &upload, &decoded_len);
256
257 if (!result || (len != decoded_len)) {
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100258 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++) {
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200262 struct Member_ *member = &upload._Upload_members[i];
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100263 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
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100285 if (off == UINT64_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
Dominik Ermel48decca2021-07-09 10:23:58 +0000292#if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
Fabio Utzig6f49c272019-08-23 11:42:58 -0300293 rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
Dominik Ermel48decca2021-07-09 10:23:58 +0000294#else
295 rc = flash_area_open(flash_area_id_from_direct_image(img_num), &fap);
296#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800297 if (rc) {
298 rc = MGMT_ERR_EINVAL;
299 goto out;
300 }
301
302 if (off == 0) {
303 curr_off = 0;
Dominik Ermel260ae092021-04-23 05:38:45 +0000304 if (data_len > flash_area_get_size(fap)) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300305 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800306 }
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000307#ifndef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermel260ae092021-04-23 05:38:45 +0000308 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800309 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300310 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200312#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313 img_size = data_len;
314 }
315 if (off != curr_off) {
316 rc = 0;
317 goto out;
318 }
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200319
320 if (curr_off + img_blen > img_size) {
321 rc = MGMT_ERR_EINVAL;
322 goto out;
323 }
324
325 rem_bytes = img_blen % flash_area_align(fap);
326
327 if ((curr_off + img_blen < img_size) && rem_bytes) {
328 img_blen -= rem_bytes;
329 rem_bytes = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300330 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200331
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000332#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200333 rc = flash_area_sector_from_off(curr_off + img_blen, &sector);
334 if (rc) {
335 BOOT_LOG_ERR("Unable to determine flash sector size");
336 goto out;
337 }
Dominik Ermel260ae092021-04-23 05:38:45 +0000338 if (off_last != flash_sector_get_off(&sector)) {
339 off_last = flash_sector_get_off(&sector);
340 BOOT_LOG_INF("Erasing sector at offset 0x%x", flash_sector_get_off(&sector));
341 rc = flash_area_erase(fap, flash_sector_get_off(&sector),
342 flash_sector_get_size(&sector));
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200343 if (rc) {
344 BOOT_LOG_ERR("Error %d while erasing sector", rc);
345 goto out;
346 }
347 }
348#endif
349
350 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_blen);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200351 if (rem_bytes) {
352 /* the last chunk of the image might be unaligned */
353 uint8_t wbs_aligned[BOOT_MAX_ALIGN];
354 size_t w_size = img_blen - rem_bytes;
355
356 if (w_size) {
357 rc = flash_area_write(fap, curr_off, img_data, w_size);
358 if (rc) {
359 goto out_invalid_data;
360 }
361 curr_off += w_size;
362 img_blen -= w_size;
363 img_data += w_size;
364 }
365
366 if (img_blen) {
367 memcpy(wbs_aligned, img_data, rem_bytes);
368 memset(wbs_aligned + rem_bytes, flash_area_erased_val(fap),
369 sizeof(wbs_aligned) - rem_bytes);
370 rc = flash_area_write(fap, curr_off, wbs_aligned, flash_area_align(fap));
371 }
372
373 } else {
374 rc = flash_area_write(fap, curr_off, img_data, img_blen);
375 }
376
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300377 if (rc == 0) {
378 curr_off += img_blen;
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000379#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200380 if (curr_off == img_size) {
381 /* get the last sector offset */
382 rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
383 if (rc) {
384 BOOT_LOG_ERR("Unable to determine flash sector of"
385 "the image trailer");
386 goto out;
387 }
388 /* Assure that sector for image trailer was erased. */
389 /* Check whether it was erased during previous upload. */
Dominik Ermel260ae092021-04-23 05:38:45 +0000390 if (off_last < flash_sector_get_off(&sector)) {
391 BOOT_LOG_INF("Erasing sector at offset 0x%x",
392 flash_sector_get_off(&sector));
393 rc = flash_area_erase(fap, flash_sector_get_off(&sector),
394 flash_sector_get_size(&sector));
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200395 if (rc) {
396 BOOT_LOG_ERR("Error %d while erasing sector", rc);
397 goto out;
398 }
399 }
400 }
401#endif
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300402 } else {
403 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800404 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800405 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200406
Christopher Collins92ea77f2016-12-12 15:59:26 -0800407out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200408 BOOT_LOG_INF("RX: 0x%x", rc);
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100409 map_start_encode(&cbor_state, 10);
410 tstrx_put(&cbor_state, "rc");
411 uintx32_put(&cbor_state, rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800412 if (rc == 0) {
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100413 tstrx_put(&cbor_state, "off");
414 uintx32_put(&cbor_state, curr_off);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800415 }
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100416 map_end_encode(&cbor_state, 10);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800417
418 boot_serial_output();
419 flash_area_close(fap);
420}
421
422/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300423 * Console echo control/image erase. Send empty response, don't do anything.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424 */
425static void
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300426bs_empty_rsp(char *buf, int len)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800427{
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100428 map_start_encode(&cbor_state, 10);
429 tstrx_put(&cbor_state, "rc");
430 uintx32_put(&cbor_state, 0);
431 map_end_encode(&cbor_state, 10);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800432 boot_serial_output();
433}
434
435/*
436 * Reset, and (presumably) boot to newly uploaded image. Flush console
437 * before restarting.
438 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200439static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800440bs_reset(char *buf, int len)
441{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300442 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800443
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200444#ifdef __ZEPHYR__
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200445#ifdef CONFIG_MULTITHREADING
Carles Cufi7e7b4ad2020-03-30 19:12:02 +0200446 k_sleep(K_MSEC(250));
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200447#else
448 k_busy_wait(250000);
449#endif
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200450 sys_reboot(SYS_REBOOT_COLD);
451#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452 os_cputime_delay_usecs(250000);
453 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200454#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800455}
456
457/*
458 * Parse incoming line of input from console.
459 * Expect newtmgr protocol with serial transport.
460 */
461void
462boot_serial_input(char *buf, int len)
463{
464 struct nmgr_hdr *hdr;
465
466 hdr = (struct nmgr_hdr *)buf;
467 if (len < sizeof(*hdr) ||
468 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
469 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
470 return;
471 }
472 bs_hdr = hdr;
473 hdr->nh_group = ntohs(hdr->nh_group);
474
475 buf += sizeof(*hdr);
476 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300477
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100478 cbor_state.payload_mut = (uint8_t *)bs_obuf;
479 cbor_state.payload_end = (const uint8_t *)bs_obuf
480 + sizeof(bs_obuf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481
482 /*
483 * Limited support for commands.
484 */
485 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
486 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300487 case IMGMGR_NMGR_ID_STATE:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800488 bs_list(buf, len);
489 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300490 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800491 bs_upload(buf, len);
492 break;
493 default:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300494 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800495 break;
496 }
497 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
498 switch (hdr->nh_id) {
499 case NMGR_ID_CONS_ECHO_CTRL:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300500 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800501 break;
502 case NMGR_ID_RESET:
503 bs_reset(buf, len);
504 break;
505 default:
506 break;
507 }
Dominik Ermelbd69c3d2021-07-28 11:27:31 +0000508 } else if (MCUBOOT_PERUSER_MGMT_GROUP_ENABLED == 1) {
Dominik Ermel3d51e432021-06-25 17:29:50 +0000509 if (bs_peruser_system_specific(hdr, buf, len, &cbor_state) == 0) {
510 boot_serial_output();
511 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800512 }
513}
514
515static void
516boot_serial_output(void)
517{
518 char *data;
519 int len;
520 uint16_t crc;
521 uint16_t totlen;
522 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
523 char buf[BOOT_SERIAL_OUT_MAX];
524 char encoded_buf[BASE64_ENCODE_SIZE(BOOT_SERIAL_OUT_MAX)];
525
526 data = bs_obuf;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100527 len = (uint32_t)cbor_state.payload_mut - (uint32_t)bs_obuf;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800528
529 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300530 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800531 bs_hdr->nh_len = htons(len);
532 bs_hdr->nh_group = htons(bs_hdr->nh_group);
533
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200534#ifdef __ZEPHYR__
Kumar Gala0813efe2020-05-27 12:25:41 -0500535 crc = crc16((uint8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL,
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300536 CRC16_INITIAL_CRC, false);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200537 crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true);
538#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
540 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200541#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800542 crc = htons(crc);
543
Marko Kiiskila149b4572018-06-06 14:18:54 +0300544 boot_uf->write(pkt_start, sizeof(pkt_start));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545
546 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
547 totlen = htons(totlen);
548
549 memcpy(buf, &totlen, sizeof(totlen));
550 totlen = sizeof(totlen);
551 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
552 totlen += sizeof(*bs_hdr);
553 memcpy(&buf[totlen], data, len);
554 totlen += len;
555 memcpy(&buf[totlen], &crc, sizeof(crc));
556 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200557#ifdef __ZEPHYR__
558 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +0200559 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200560 totlen = enc_len;
561#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800562 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200563#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +0300564 boot_uf->write(encoded_buf, totlen);
565 boot_uf->write("\n\r", 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200566 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800567}
568
569/*
570 * Returns 1 if full packet has been received.
571 */
572static int
573boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
574{
575 int rc;
576 uint16_t crc;
577 uint16_t len;
Marko Kiiskilae5aeee42018-12-21 15:00:16 +0200578
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200579#ifdef __ZEPHYR__
580 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200581 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200582 if (err) {
583 return -1;
584 }
585#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800586 if (*out_off + base64_decode_len(in) >= maxout) {
587 return -1;
588 }
589 rc = base64_decode(in, &out[*out_off]);
590 if (rc < 0) {
591 return -1;
592 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200593#endif
Fabio Utzig6f49c272019-08-23 11:42:58 -0300594
Christopher Collins92ea77f2016-12-12 15:59:26 -0800595 *out_off += rc;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300596 if (*out_off <= sizeof(uint16_t)) {
597 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800598 }
Fabio Utzig6f49c272019-08-23 11:42:58 -0300599
600 len = ntohs(*(uint16_t *)out);
601 if (len != *out_off - sizeof(uint16_t)) {
602 return 0;
603 }
604
605 if (len > *out_off - sizeof(uint16_t)) {
606 len = *out_off - sizeof(uint16_t);
607 }
608
609 out += sizeof(uint16_t);
610#ifdef __ZEPHYR__
611 crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true);
612#else
613 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
614#endif
615 if (crc || len <= sizeof(crc)) {
616 return 0;
617 }
618 *out_off -= sizeof(crc);
619 out[*out_off] = '\0';
620
621 return 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622}
623
624/*
625 * Task which waits reading console, expecting to get image over
626 * serial port.
627 */
628void
Marko Kiiskila149b4572018-06-06 14:18:54 +0300629boot_serial_start(const struct boot_uart_funcs *f)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800630{
631 int rc;
632 int off;
David Brown57f0df32020-05-12 08:39:21 -0600633 int dec_off = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300635 int max_input;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800636
Marko Kiiskila149b4572018-06-06 14:18:54 +0300637 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300638 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800639
640 off = 0;
641 while (1) {
Andrzej Puzdrowskiaea38eb2021-06-11 12:28:59 +0200642 MCUBOOT_CPU_IDLE();
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200643 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800644 if (rc <= 0 && !full_line) {
645 continue;
646 }
647 off += rc;
648 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300649 if (off == max_input) {
650 /*
651 * Full line, no newline yet. Reset the input buffer.
652 */
653 off = 0;
654 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800655 continue;
656 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200657 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
658 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800659 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200660 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
661 } else if (in_buf[0] == SHELL_NLIP_DATA_START1 &&
662 in_buf[1] == SHELL_NLIP_DATA_START2) {
663 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800664 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200665
666 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800667 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200668 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800669 }
670 off = 0;
671 }
672}