blob: 6e385b0a0376b7944fd2746db15cf14042eb54b1 [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/**
106 * Function that processes MGMT_GROUP_ID_PERUSER mcumgr commands.
107 * The function is system specific as the PERUSER commands are system specific.
108 *
109 * @param[in] hdr -- the decoded header of mcumgr message;
110 * @param[in] buffer -- buffer with first mcumgr message;
111 * @param[in] len -- length of of data in buffer;
112 * @param[out] *cs -- object with encoded response.
113 *
114 * @return 0 on success; non-0 error code otherwise.
115 */
116extern int bs_peruser_system_specific(const struct nmgr_hdr *hdr,
117 const char *buffer,
118 int len, cbor_state_t *cs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800119
120/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300121 * Convert version into string without use of snprintf().
Christopher Collins92ea77f2016-12-12 15:59:26 -0800122 */
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300123static int
124u32toa(char *tgt, uint32_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300126 char *dst;
127 uint32_t d = 1;
128 uint32_t dgt;
129 int n = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800130
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300131 dst = tgt;
132 while (val / d >= 10) {
133 d *= 10;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300135 while (d) {
136 dgt = val / d;
137 val %= d;
138 d /= 10;
139 if (n || dgt > 0 || d == 0) {
140 *dst++ = dgt + '0';
141 ++n;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800142 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800143 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300144 *dst = '\0';
145
146 return dst - tgt;
147}
148
149/*
150 * dst has to be able to fit "255.255.65535.4294967295" (25 characters).
151 */
152static void
153bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
154{
155 int off;
156
157 off = u32toa(dst, ver->iv_major);
158 dst[off++] = '.';
159 off += u32toa(dst + off, ver->iv_minor);
160 dst[off++] = '.';
161 off += u32toa(dst + off, ver->iv_revision);
162 dst[off++] = '.';
163 off += u32toa(dst + off, ver->iv_build_num);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800164}
165
166/*
167 * List images.
168 */
169static void
170bs_list(char *buf, int len)
171{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800172 struct image_header hdr;
173 uint8_t tmpbuf[64];
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100174 uint32_t slot, area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175 const struct flash_area *fap;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300176 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100178 map_start_encode(&cbor_state, 1);
179 tstrx_put(&cbor_state, "images");
180 list_start_encode(&cbor_state, 5);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300181 image_index = 0;
182 IMAGES_ITER(image_index) {
183 for (slot = 0; slot < 2; slot++) {
184 area_id = flash_area_id_from_multi_image_slot(image_index, slot);
185 if (flash_area_open(area_id, &fap)) {
186 continue;
187 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800188
Fabio Utzig6f49c272019-08-23 11:42:58 -0300189 flash_area_read(fap, 0, &hdr, sizeof(hdr));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800190
Fabio Utzig6f49c272019-08-23 11:42:58 -0300191 if (hdr.ih_magic != IMAGE_MAGIC ||
192 bootutil_img_validate(NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
193 NULL, 0, NULL)) {
194 flash_area_close(fap);
195 continue;
196 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197 flash_area_close(fap);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300198
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100199 map_start_encode(&cbor_state, 20);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300200
201#if (BOOT_IMAGE_NUMBER > 1)
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100202 tstrx_put(&cbor_state, "image");
203 uintx32_put(&cbor_state, image_index);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300204#endif
205
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100206 tstrx_put(&cbor_state, "slot");
207 uintx32_put(&cbor_state, slot);
208 tstrx_put(&cbor_state, "version");
Fabio Utzig6f49c272019-08-23 11:42:58 -0300209
210 bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100211 tstrx_put_term(&cbor_state, (char *)tmpbuf);
212 map_end_encode(&cbor_state, 20);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800213 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800214 }
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100215 list_end_encode(&cbor_state, 5);
216 map_end_encode(&cbor_state, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800217 boot_serial_output();
218}
219
220/*
221 * Image upload request.
222 */
223static void
224bs_upload(char *buf, int len)
225{
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100226 const uint8_t *img_data = NULL;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100227 long long int off = UINT64_MAX;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800228 size_t img_blen = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300229 uint8_t rem_bytes;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100230 long long int data_len = UINT64_MAX;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300231 int img_num;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300232 size_t slen;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800233 const struct flash_area *fap = NULL;
234 int rc;
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000235#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200236 static off_t off_last = -1;
237 struct flash_sector sector;
238#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800239
Fabio Utzig6f49c272019-08-23 11:42:58 -0300240 img_num = 0;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300241
242 /*
243 * Expected data format.
244 * {
Fabio Utzig6f49c272019-08-23 11:42:58 -0300245 * "image":<image number in a multi-image set (OPTIONAL)>
246 * "data":<image data>
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300247 * "len":<image len>
248 * "off":<current offset of image data>
249 * }
250 */
251
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200252 struct Upload upload;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100253 uint32_t decoded_len;
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200254 bool result = cbor_decode_Upload((const uint8_t *)buf, len, &upload, &decoded_len);
255
256 if (!result || (len != decoded_len)) {
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100257 goto out_invalid_data;
258 }
Dominik Ermel470e2f32020-01-10 13:28:48 +0000259
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100260 for (int i = 0; i < upload._Upload_members_count; i++) {
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200261 struct Member_ *member = &upload._Upload_members[i];
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100262 switch(member->_Member_choice) {
263 case _Member_image:
264 img_num = member->_Member_image;
265 break;
266 case _Member_data:
267 img_data = member->_Member_data.value;
268 slen = member->_Member_data.len;
269 img_blen = slen;
270 break;
271 case _Member_len:
272 data_len = member->_Member_len;
273 break;
274 case _Member_off:
275 off = member->_Member_off;
276 break;
277 case _Member_sha:
278 default:
279 /* Nothing to do. */
280 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300281 }
282 }
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100283
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100284 if (off == UINT64_MAX || img_data == NULL) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300285 /*
286 * Offset must be set in every block.
287 */
288 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289 }
290
Dominik Ermel48decca2021-07-09 10:23:58 +0000291#if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
Fabio Utzig6f49c272019-08-23 11:42:58 -0300292 rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
Dominik Ermel48decca2021-07-09 10:23:58 +0000293#else
294 rc = flash_area_open(flash_area_id_from_direct_image(img_num), &fap);
295#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800296 if (rc) {
297 rc = MGMT_ERR_EINVAL;
298 goto out;
299 }
300
301 if (off == 0) {
302 curr_off = 0;
Dominik Ermel260ae092021-04-23 05:38:45 +0000303 if (data_len > flash_area_get_size(fap)) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300304 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305 }
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000306#ifndef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermel260ae092021-04-23 05:38:45 +0000307 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800308 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300309 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200311#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800312 img_size = data_len;
313 }
314 if (off != curr_off) {
315 rc = 0;
316 goto out;
317 }
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200318
319 if (curr_off + img_blen > img_size) {
320 rc = MGMT_ERR_EINVAL;
321 goto out;
322 }
323
324 rem_bytes = img_blen % flash_area_align(fap);
325
326 if ((curr_off + img_blen < img_size) && rem_bytes) {
327 img_blen -= rem_bytes;
328 rem_bytes = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300329 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200330
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000331#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200332 rc = flash_area_sector_from_off(curr_off + img_blen, &sector);
333 if (rc) {
334 BOOT_LOG_ERR("Unable to determine flash sector size");
335 goto out;
336 }
Dominik Ermel260ae092021-04-23 05:38:45 +0000337 if (off_last != flash_sector_get_off(&sector)) {
338 off_last = flash_sector_get_off(&sector);
339 BOOT_LOG_INF("Erasing sector at offset 0x%x", flash_sector_get_off(&sector));
340 rc = flash_area_erase(fap, flash_sector_get_off(&sector),
341 flash_sector_get_size(&sector));
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200342 if (rc) {
343 BOOT_LOG_ERR("Error %d while erasing sector", rc);
344 goto out;
345 }
346 }
347#endif
348
349 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_blen);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200350 if (rem_bytes) {
351 /* the last chunk of the image might be unaligned */
352 uint8_t wbs_aligned[BOOT_MAX_ALIGN];
353 size_t w_size = img_blen - rem_bytes;
354
355 if (w_size) {
356 rc = flash_area_write(fap, curr_off, img_data, w_size);
357 if (rc) {
358 goto out_invalid_data;
359 }
360 curr_off += w_size;
361 img_blen -= w_size;
362 img_data += w_size;
363 }
364
365 if (img_blen) {
366 memcpy(wbs_aligned, img_data, rem_bytes);
367 memset(wbs_aligned + rem_bytes, flash_area_erased_val(fap),
368 sizeof(wbs_aligned) - rem_bytes);
369 rc = flash_area_write(fap, curr_off, wbs_aligned, flash_area_align(fap));
370 }
371
372 } else {
373 rc = flash_area_write(fap, curr_off, img_data, img_blen);
374 }
375
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300376 if (rc == 0) {
377 curr_off += img_blen;
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000378#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200379 if (curr_off == img_size) {
380 /* get the last sector offset */
381 rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
382 if (rc) {
383 BOOT_LOG_ERR("Unable to determine flash sector of"
384 "the image trailer");
385 goto out;
386 }
387 /* Assure that sector for image trailer was erased. */
388 /* Check whether it was erased during previous upload. */
Dominik Ermel260ae092021-04-23 05:38:45 +0000389 if (off_last < flash_sector_get_off(&sector)) {
390 BOOT_LOG_INF("Erasing sector at offset 0x%x",
391 flash_sector_get_off(&sector));
392 rc = flash_area_erase(fap, flash_sector_get_off(&sector),
393 flash_sector_get_size(&sector));
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200394 if (rc) {
395 BOOT_LOG_ERR("Error %d while erasing sector", rc);
396 goto out;
397 }
398 }
399 }
400#endif
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300401 } else {
402 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800403 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800404 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200405
Christopher Collins92ea77f2016-12-12 15:59:26 -0800406out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200407 BOOT_LOG_INF("RX: 0x%x", rc);
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100408 map_start_encode(&cbor_state, 10);
409 tstrx_put(&cbor_state, "rc");
410 uintx32_put(&cbor_state, rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800411 if (rc == 0) {
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100412 tstrx_put(&cbor_state, "off");
413 uintx32_put(&cbor_state, curr_off);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800414 }
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100415 map_end_encode(&cbor_state, 10);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800416
417 boot_serial_output();
418 flash_area_close(fap);
419}
420
421/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300422 * Console echo control/image erase. Send empty response, don't do anything.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800423 */
424static void
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300425bs_empty_rsp(char *buf, int len)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800426{
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100427 map_start_encode(&cbor_state, 10);
428 tstrx_put(&cbor_state, "rc");
429 uintx32_put(&cbor_state, 0);
430 map_end_encode(&cbor_state, 10);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800431 boot_serial_output();
432}
433
434/*
435 * Reset, and (presumably) boot to newly uploaded image. Flush console
436 * before restarting.
437 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200438static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800439bs_reset(char *buf, int len)
440{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300441 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200443#ifdef __ZEPHYR__
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200444#ifdef CONFIG_MULTITHREADING
Carles Cufi7e7b4ad2020-03-30 19:12:02 +0200445 k_sleep(K_MSEC(250));
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200446#else
447 k_busy_wait(250000);
448#endif
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200449 sys_reboot(SYS_REBOOT_COLD);
450#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800451 os_cputime_delay_usecs(250000);
452 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200453#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454}
455
456/*
457 * Parse incoming line of input from console.
458 * Expect newtmgr protocol with serial transport.
459 */
460void
461boot_serial_input(char *buf, int len)
462{
463 struct nmgr_hdr *hdr;
464
465 hdr = (struct nmgr_hdr *)buf;
466 if (len < sizeof(*hdr) ||
467 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
468 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
469 return;
470 }
471 bs_hdr = hdr;
472 hdr->nh_group = ntohs(hdr->nh_group);
473
474 buf += sizeof(*hdr);
475 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300476
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100477 cbor_state.payload_mut = (uint8_t *)bs_obuf;
478 cbor_state.payload_end = (const uint8_t *)bs_obuf
479 + sizeof(bs_obuf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480
481 /*
482 * Limited support for commands.
483 */
484 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
485 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300486 case IMGMGR_NMGR_ID_STATE:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800487 bs_list(buf, len);
488 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300489 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490 bs_upload(buf, len);
491 break;
492 default:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300493 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800494 break;
495 }
496 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
497 switch (hdr->nh_id) {
498 case NMGR_ID_CONS_ECHO_CTRL:
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300499 bs_empty_rsp(buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800500 break;
501 case NMGR_ID_RESET:
502 bs_reset(buf, len);
503 break;
504 default:
505 break;
506 }
Dominik Ermel3d51e432021-06-25 17:29:50 +0000507 } else if (MCUBOOT_PERUSER_MGMT_GROUP_ENABLED == 1 &&
508 hdr->nh_group >= MGMT_GROUP_ID_PERUSER) {
509 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}