blob: d0fc865ddef6dd865377f74448a97a83eeaeeaf1 [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ønningstada7d34ca2022-02-28 13:47:57 +010028#include "zcbor_encode.h"
Fabio Utzig1a2e41a2017-11-17 12:13:09 -020029
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020030#ifdef __ZEPHYR__
Dominik Ermel1eedec32021-12-16 12:55:02 +000031#include <sys/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
Wouter Cappelle953a7612021-05-03 16:53:05 +020061#ifdef MCUBOOT_ENC_IMAGES
62#include "single_loader.h"
63#endif
64
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +010065#include "serial_recovery_cbor.h"
Dominik Ermel88bd5672022-06-07 15:17:06 +000066#include "serial_recovery_echo.h"
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +020067#include "bootutil/boot_hooks.h"
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +010068
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020069BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010070
Marko Kiiskila149b4572018-06-06 14:18:54 +030071#define BOOT_SERIAL_INPUT_MAX 512
Andrzej Puzdrowskic9ac5cc2021-11-19 11:58:05 +010072#define BOOT_SERIAL_OUT_MAX (128 * BOOT_IMAGE_NUMBER)
Christopher Collins92ea77f2016-12-12 15:59:26 -080073
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020074#ifdef __ZEPHYR__
Carles Cufi0165be82018-03-26 17:43:51 +020075/* base64 lib encodes data to null-terminated string */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020076#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
77
78#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
79#define CRC_CITT_POLYMINAL 0x1021
80
81#define ntohs(x) sys_be16_to_cpu(x)
82#define htons(x) sys_cpu_to_be16(x)
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020083#endif
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010084
Fabio Utzig6f49c272019-08-23 11:42:58 -030085#if (BOOT_IMAGE_NUMBER > 1)
86#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
87#else
88#define IMAGES_ITER(x)
89#endif
90
Marko Kiiskila149b4572018-06-06 14:18:54 +030091static char in_buf[BOOT_SERIAL_INPUT_MAX + 1];
92static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1];
Marko Kiiskila8b1ce3a2018-06-14 13:20:46 -070093const struct boot_uart_funcs *boot_uf;
Christopher Collins92ea77f2016-12-12 15:59:26 -080094static struct nmgr_hdr *bs_hdr;
Wouter Cappellee3822f82022-01-19 15:39:43 +010095static bool bs_entry;
Christopher Collins92ea77f2016-12-12 15:59:26 -080096
97static char bs_obuf[BOOT_SERIAL_OUT_MAX];
98
Christopher Collins92ea77f2016-12-12 15:59:26 -080099static void boot_serial_output(void);
100
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100101static zcbor_state_t cbor_state[2];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800102
Dominik Ermel4c0f6c12022-03-04 15:47:37 +0000103void reset_cbor_state(void)
104{
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100105 zcbor_new_encode_state(cbor_state, 2, (uint8_t *)bs_obuf,
106 (size_t)bs_obuf + sizeof(bs_obuf), 0);
Dominik Ermel4c0f6c12022-03-04 15:47:37 +0000107}
108
Dominik Ermel3d51e432021-06-25 17:29:50 +0000109/**
Dominik Ermelbd69c3d2021-07-28 11:27:31 +0000110 * Function that processes MGMT_GROUP_ID_PERUSER mcumgr group and may be
111 * used to process any groups that have not been processed by generic boot
112 * serial implementation.
Dominik Ermel3d51e432021-06-25 17:29:50 +0000113 *
114 * @param[in] hdr -- the decoded header of mcumgr message;
115 * @param[in] buffer -- buffer with first mcumgr message;
116 * @param[in] len -- length of of data in buffer;
117 * @param[out] *cs -- object with encoded response.
118 *
119 * @return 0 on success; non-0 error code otherwise.
120 */
121extern int bs_peruser_system_specific(const struct nmgr_hdr *hdr,
122 const char *buffer,
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100123 int len, zcbor_state_t *cs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800124
Dominik Ermeld49cfc12022-06-09 08:24:48 +0000125#define zcbor_tstr_put_lit_cast(state, string) \
126 zcbor_tstr_encode_ptr(state, (uint8_t *)string, sizeof(string) - 1)
127
128#ifndef MCUBOOT_USE_SNPRINTF
Christopher Collins92ea77f2016-12-12 15:59:26 -0800129/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300130 * Convert version into string without use of snprintf().
Christopher Collins92ea77f2016-12-12 15:59:26 -0800131 */
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300132static int
133u32toa(char *tgt, uint32_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300135 char *dst;
136 uint32_t d = 1;
137 uint32_t dgt;
138 int n = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800139
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300140 dst = tgt;
141 while (val / d >= 10) {
142 d *= 10;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800143 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300144 while (d) {
145 dgt = val / d;
146 val %= d;
147 d /= 10;
148 if (n || dgt > 0 || d == 0) {
149 *dst++ = dgt + '0';
150 ++n;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800151 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300153 *dst = '\0';
154
155 return dst - tgt;
156}
157
158/*
159 * dst has to be able to fit "255.255.65535.4294967295" (25 characters).
160 */
161static void
162bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
163{
164 int off;
165
166 off = u32toa(dst, ver->iv_major);
167 dst[off++] = '.';
168 off += u32toa(dst + off, ver->iv_minor);
169 dst[off++] = '.';
170 off += u32toa(dst + off, ver->iv_revision);
171 dst[off++] = '.';
172 off += u32toa(dst + off, ver->iv_build_num);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173}
Dominik Ermeld49cfc12022-06-09 08:24:48 +0000174#else
175/*
176 * dst has to be able to fit "255.255.65535.4294967295" (25 characters).
177 */
178static void
179bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
180{
181 snprintf(dst, maxlen, "%hu.%hu.%hu.%u", (uint16_t)ver->iv_major,
182 (uint16_t)ver->iv_minor, ver->iv_revision, ver->iv_build_num);
183}
184#endif /* !MCUBOOT_USE_SNPRINTF */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800185
186/*
187 * List images.
188 */
189static void
190bs_list(char *buf, int len)
191{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800192 struct image_header hdr;
193 uint8_t tmpbuf[64];
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100194 uint32_t slot, area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800195 const struct flash_area *fap;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300196 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100198 zcbor_map_start_encode(cbor_state, 1);
199 zcbor_tstr_put_lit_cast(cbor_state, "images");
200 zcbor_list_start_encode(cbor_state, 5);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300201 image_index = 0;
202 IMAGES_ITER(image_index) {
203 for (slot = 0; slot < 2; slot++) {
204 area_id = flash_area_id_from_multi_image_slot(image_index, slot);
205 if (flash_area_open(area_id, &fap)) {
206 continue;
207 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800208
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200209 int rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
210 BOOT_HOOK_REGULAR, image_index, slot, &hdr);
211 if (rc == BOOT_HOOK_REGULAR)
212 {
213 flash_area_read(fap, 0, &hdr, sizeof(hdr));
214 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200216 fih_int fih_rc = FIH_FAILURE;
217
218 if (hdr.ih_magic == IMAGE_MAGIC)
219 {
220 BOOT_HOOK_CALL_FIH(boot_image_check_hook,
221 fih_int_encode(BOOT_HOOK_REGULAR),
222 fih_rc, image_index, slot);
223 if (fih_eq(fih_rc, BOOT_HOOK_REGULAR))
224 {
Wouter Cappelle953a7612021-05-03 16:53:05 +0200225#ifdef MCUBOOT_ENC_IMAGES
226 if (slot == 0 && IS_ENCRYPTED(&hdr)) {
227 /* Clear the encrypted flag we didn't supply a key
228 * This flag could be set if there was a decryption in place
229 * performed before. We will try to validate the image without
230 * decryption by clearing the flag in the heder. If
231 * still encrypted the validation will fail.
232 */
233 hdr.ih_flags &= ~(ENCRYPTIONFLAGS);
234 }
235#endif
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200236 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
237 NULL, 0, NULL);
238 }
239 }
240
241 flash_area_close(fap);
242
243 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Fabio Utzig6f49c272019-08-23 11:42:58 -0300244 continue;
245 }
Fabio Utzig6f49c272019-08-23 11:42:58 -0300246
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100247 zcbor_map_start_encode(cbor_state, 20);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300248
249#if (BOOT_IMAGE_NUMBER > 1)
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100250 zcbor_tstr_put_lit_cast(cbor_state, "image");
251 zcbor_uint32_put(cbor_state, image_index);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300252#endif
253
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100254 zcbor_tstr_put_lit_cast(cbor_state, "slot");
255 zcbor_uint32_put(cbor_state, slot);
256 zcbor_tstr_put_lit_cast(cbor_state, "version");
Fabio Utzig6f49c272019-08-23 11:42:58 -0300257
258 bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100259 zcbor_tstr_encode_ptr(cbor_state, tmpbuf, strlen((char *)tmpbuf));
260 zcbor_map_end_encode(cbor_state, 20);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800261 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800262 }
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100263 zcbor_list_end_encode(cbor_state, 5);
264 zcbor_map_end_encode(cbor_state, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800265 boot_serial_output();
266}
267
268/*
269 * Image upload request.
270 */
271static void
272bs_upload(char *buf, int len)
273{
Dominik Ermel5bd87442022-06-13 15:14:01 +0000274 static size_t img_size; /* Total image size, held for duration of upload */
275 static uint32_t curr_off; /* Expected current offset */
276 const uint8_t *img_chunk = NULL; /* Pointer to buffer with received image chunk */
277 size_t img_chunk_len = 0; /* Length of received image chunk */
278 size_t img_chunk_off = SIZE_MAX; /* Offset of image chunk within image */
279 uint8_t rem_bytes; /* Reminder bytes after aligning chunk write to
280 * to flash alignment */
Fabio Utzig6f49c272019-08-23 11:42:58 -0300281 int img_num;
Dominik Ermel5bd87442022-06-13 15:14:01 +0000282 size_t img_size_tmp = SIZE_MAX; /* Temp variable for image size */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800283 const struct flash_area *fap = NULL;
284 int rc;
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000285#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200286 struct flash_sector sector;
Dominik Ermel5bd87442022-06-13 15:14:01 +0000287 static off_t off_last = -1; /* Last erased offset */
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200288#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289
Fabio Utzig6f49c272019-08-23 11:42:58 -0300290 img_num = 0;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300291
292 /*
293 * Expected data format.
294 * {
Fabio Utzig6f49c272019-08-23 11:42:58 -0300295 * "image":<image number in a multi-image set (OPTIONAL)>
296 * "data":<image data>
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300297 * "len":<image len>
298 * "off":<current offset of image data>
299 * }
300 */
301
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200302 struct Upload upload;
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100303 size_t decoded_len;
304 uint_fast8_t result = cbor_decode_Upload((const uint8_t *)buf, len, &upload, &decoded_len);
Øyvind Rønningstad212a35b2021-05-07 21:06:48 +0200305
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100306 if ((result != ZCBOR_SUCCESS) || (len != decoded_len)) {
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100307 goto out_invalid_data;
308 }
Dominik Ermel470e2f32020-01-10 13:28:48 +0000309
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100310 for (int i = 0; i < upload._Upload_members_count; i++) {
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100311 struct Member_ *member = &upload._Upload_members[i]._Upload_members;
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100312 switch(member->_Member_choice) {
313 case _Member_image:
314 img_num = member->_Member_image;
315 break;
316 case _Member_data:
Dominik Ermel5bd87442022-06-13 15:14:01 +0000317 img_chunk = member->_Member_data.value;
318 img_chunk_len = member->_Member_data.len;
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100319 break;
320 case _Member_len:
Dominik Ermel5bd87442022-06-13 15:14:01 +0000321 img_size_tmp = member->_Member_len;
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100322 break;
323 case _Member_off:
Dominik Ermel5bd87442022-06-13 15:14:01 +0000324 img_chunk_off = member->_Member_off;
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100325 break;
326 case _Member_sha:
327 default:
328 /* Nothing to do. */
329 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300330 }
331 }
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +0100332
Dominik Ermel5bd87442022-06-13 15:14:01 +0000333 if (img_chunk_off == SIZE_MAX || img_chunk == NULL) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300334 /*
335 * Offset must be set in every block.
336 */
337 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800338 }
339
Dominik Ermel48decca2021-07-09 10:23:58 +0000340#if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
Fabio Utzig6f49c272019-08-23 11:42:58 -0300341 rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
Dominik Ermel48decca2021-07-09 10:23:58 +0000342#else
343 rc = flash_area_open(flash_area_id_from_direct_image(img_num), &fap);
344#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800345 if (rc) {
346 rc = MGMT_ERR_EINVAL;
347 goto out;
348 }
349
Dominik Ermel5bd87442022-06-13 15:14:01 +0000350 if (img_chunk_off == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800351 curr_off = 0;
Dominik Ermel5bd87442022-06-13 15:14:01 +0000352
353 if (img_size_tmp > flash_area_get_size(fap)) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300354 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800355 }
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200356#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
357 /* We are using swap state at end of flash area to store validation
358 * result. Make sure the user cannot write it from an image to skip validation.
359 */
Dominik Ermel5bd87442022-06-13 15:14:01 +0000360 if (img_size_tmp > (flash_area_get_size(fap) - BOOT_MAGIC_SZ)) {
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200361 goto out_invalid_data;
362 }
363#endif
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000364#ifndef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermel260ae092021-04-23 05:38:45 +0000365 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300367 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200369#endif
Dominik Ermel5bd87442022-06-13 15:14:01 +0000370 img_size = img_size_tmp;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371 }
Dominik Ermel5bd87442022-06-13 15:14:01 +0000372 if (img_chunk_off != curr_off) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373 rc = 0;
374 goto out;
375 }
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200376
Dominik Ermel5bd87442022-06-13 15:14:01 +0000377 if (curr_off + img_chunk_len > img_size) {
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200378 rc = MGMT_ERR_EINVAL;
379 goto out;
380 }
381
Dominik Ermel5bd87442022-06-13 15:14:01 +0000382 rem_bytes = img_chunk_len % flash_area_align(fap);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200383
Dominik Ermel5bd87442022-06-13 15:14:01 +0000384 if ((curr_off + img_chunk_len < img_size) && rem_bytes) {
385 img_chunk_len -= rem_bytes;
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200386 rem_bytes = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300387 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200388
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000389#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermel5bd87442022-06-13 15:14:01 +0000390 rc = flash_area_sector_from_off(curr_off + img_chunk_len, &sector);
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200391 if (rc) {
392 BOOT_LOG_ERR("Unable to determine flash sector size");
393 goto out;
394 }
Dominik Ermel260ae092021-04-23 05:38:45 +0000395 if (off_last != flash_sector_get_off(&sector)) {
396 off_last = flash_sector_get_off(&sector);
397 BOOT_LOG_INF("Erasing sector at offset 0x%x", flash_sector_get_off(&sector));
398 rc = flash_area_erase(fap, flash_sector_get_off(&sector),
399 flash_sector_get_size(&sector));
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200400 if (rc) {
401 BOOT_LOG_ERR("Error %d while erasing sector", rc);
402 goto out;
403 }
404 }
405#endif
406
Dominik Ermel5bd87442022-06-13 15:14:01 +0000407 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_chunk_len);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200408 if (rem_bytes) {
409 /* the last chunk of the image might be unaligned */
410 uint8_t wbs_aligned[BOOT_MAX_ALIGN];
Dominik Ermel5bd87442022-06-13 15:14:01 +0000411 size_t w_size = img_chunk_len - rem_bytes;
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200412
413 if (w_size) {
Dominik Ermel5bd87442022-06-13 15:14:01 +0000414 rc = flash_area_write(fap, curr_off, img_chunk, w_size);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200415 if (rc) {
416 goto out_invalid_data;
417 }
418 curr_off += w_size;
Dominik Ermel5bd87442022-06-13 15:14:01 +0000419 img_chunk_len -= w_size;
420 img_chunk += w_size;
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200421 }
422
Dominik Ermel5bd87442022-06-13 15:14:01 +0000423 if (img_chunk_len) {
424 memcpy(wbs_aligned, img_chunk, rem_bytes);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200425 memset(wbs_aligned + rem_bytes, flash_area_erased_val(fap),
426 sizeof(wbs_aligned) - rem_bytes);
427 rc = flash_area_write(fap, curr_off, wbs_aligned, flash_area_align(fap));
428 }
429
430 } else {
Dominik Ermel5bd87442022-06-13 15:14:01 +0000431 rc = flash_area_write(fap, curr_off, img_chunk, img_chunk_len);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200432 }
433
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300434 if (rc == 0) {
Dominik Ermel5bd87442022-06-13 15:14:01 +0000435 curr_off += img_chunk_len;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200436 if (curr_off == img_size) {
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200437#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200438 /* get the last sector offset */
439 rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
440 if (rc) {
441 BOOT_LOG_ERR("Unable to determine flash sector of"
442 "the image trailer");
443 goto out;
444 }
445 /* Assure that sector for image trailer was erased. */
446 /* Check whether it was erased during previous upload. */
Dominik Ermel260ae092021-04-23 05:38:45 +0000447 if (off_last < flash_sector_get_off(&sector)) {
448 BOOT_LOG_INF("Erasing sector at offset 0x%x",
449 flash_sector_get_off(&sector));
450 rc = flash_area_erase(fap, flash_sector_get_off(&sector),
451 flash_sector_get_size(&sector));
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200452 if (rc) {
453 BOOT_LOG_ERR("Error %d while erasing sector", rc);
454 goto out;
455 }
456 }
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200457#endif
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200458 rc = BOOT_HOOK_CALL(boot_serial_uploaded_hook, 0, img_num, fap,
459 img_size);
460 if (rc) {
461 BOOT_LOG_ERR("Error %d post upload hook", rc);
462 goto out;
463 }
464 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300465 } else {
466 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800467 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200469
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200471 BOOT_LOG_INF("RX: 0x%x", rc);
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100472 zcbor_map_start_encode(cbor_state, 10);
473 zcbor_tstr_put_lit_cast(cbor_state, "rc");
474 zcbor_uint32_put(cbor_state, rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800475 if (rc == 0) {
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100476 zcbor_tstr_put_lit_cast(cbor_state, "off");
477 zcbor_uint32_put(cbor_state, curr_off);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800478 }
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100479 zcbor_map_end_encode(cbor_state, 10);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480
481 boot_serial_output();
482 flash_area_close(fap);
Wouter Cappelle953a7612021-05-03 16:53:05 +0200483
484#ifdef MCUBOOT_ENC_IMAGES
485 if (curr_off == img_size) {
486 /* Last sector received, now start a decryption on the image if it is encrypted*/
487 rc = boot_handle_enc_fw();
488 }
489#endif //#ifdef MCUBOOT_ENC_IMAGES
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490}
491
Dominik Ermel4c0f6c12022-03-04 15:47:37 +0000492/*
493 * Send rc code only.
494 */
495static void
496bs_rc_rsp(int rc_code)
497{
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100498 zcbor_map_start_encode(cbor_state, 10);
499 zcbor_tstr_put_lit_cast(cbor_state, "rc");
500 zcbor_uint32_put(cbor_state, rc_code);
501 zcbor_map_end_encode(cbor_state, 10);
Dominik Ermel4c0f6c12022-03-04 15:47:37 +0000502 boot_serial_output();
503}
504
505
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200506#ifdef MCUBOOT_BOOT_MGMT_ECHO
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200507static void
508bs_echo(char *buf, int len)
509{
Dominik Ermel88bd5672022-06-07 15:17:06 +0000510 struct Echo echo = { 0 };
511 size_t decoded_len;
512 uint32_t rc = MGMT_ERR_EINVAL;
513 uint_fast8_t result = cbor_decode_Echo((const uint8_t *)buf, len, &echo, &decoded_len);
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200514
Dominik Ermel88bd5672022-06-07 15:17:06 +0000515 if ((result != ZCBOR_SUCCESS) || (len != decoded_len)) {
516 goto out;
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200517 }
Dominik Ermel88bd5672022-06-07 15:17:06 +0000518
519 if (echo._Echo_d.value == NULL) {
520 goto out;
521 }
522
523 zcbor_map_start_encode(cbor_state, 10);
524 zcbor_tstr_put_term(cbor_state, "r");
525 if (zcbor_tstr_encode(cbor_state, &echo._Echo_d) && zcbor_map_end_encode(cbor_state, 10)) {
526 boot_serial_output();
527 return;
528 } else {
529 rc = MGMT_ERR_ENOMEM;
530 }
531
532out:
533 reset_cbor_state();
534 bs_rc_rsp(rc);
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200535}
536#endif
537
Christopher Collins92ea77f2016-12-12 15:59:26 -0800538/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539 * Reset, and (presumably) boot to newly uploaded image. Flush console
540 * before restarting.
541 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200542static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800543bs_reset(char *buf, int len)
544{
Dominik Ermelc9dc2242021-07-28 17:08:23 +0000545 bs_rc_rsp(0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800546
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200547#ifdef __ZEPHYR__
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200548#ifdef CONFIG_MULTITHREADING
Carles Cufi7e7b4ad2020-03-30 19:12:02 +0200549 k_sleep(K_MSEC(250));
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200550#else
551 k_busy_wait(250000);
552#endif
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200553 sys_reboot(SYS_REBOOT_COLD);
554#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800555 os_cputime_delay_usecs(250000);
556 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200557#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800558}
559
560/*
561 * Parse incoming line of input from console.
562 * Expect newtmgr protocol with serial transport.
563 */
564void
565boot_serial_input(char *buf, int len)
566{
567 struct nmgr_hdr *hdr;
568
569 hdr = (struct nmgr_hdr *)buf;
570 if (len < sizeof(*hdr) ||
571 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
572 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
573 return;
574 }
575 bs_hdr = hdr;
576 hdr->nh_group = ntohs(hdr->nh_group);
577
578 buf += sizeof(*hdr);
579 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300580
Dominik Ermel4c0f6c12022-03-04 15:47:37 +0000581 reset_cbor_state();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800582
583 /*
584 * Limited support for commands.
585 */
586 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
587 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300588 case IMGMGR_NMGR_ID_STATE:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800589 bs_list(buf, len);
590 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300591 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800592 bs_upload(buf, len);
593 break;
594 default:
Dominik Ermelc9dc2242021-07-28 17:08:23 +0000595 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596 break;
597 }
598 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
599 switch (hdr->nh_id) {
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200600 case NMGR_ID_ECHO:
601#ifdef MCUBOOT_BOOT_MGMT_ECHO
602 bs_echo(buf, len);
603#endif
604 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800605 case NMGR_ID_CONS_ECHO_CTRL:
Dominik Ermelc9dc2242021-07-28 17:08:23 +0000606 bs_rc_rsp(0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800607 break;
608 case NMGR_ID_RESET:
609 bs_reset(buf, len);
610 break;
611 default:
Dominik Ermelc9dc2242021-07-28 17:08:23 +0000612 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800613 break;
614 }
Dominik Ermelbd69c3d2021-07-28 11:27:31 +0000615 } else if (MCUBOOT_PERUSER_MGMT_GROUP_ENABLED == 1) {
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100616 if (bs_peruser_system_specific(hdr, buf, len, cbor_state) == 0) {
Dominik Ermel3d51e432021-06-25 17:29:50 +0000617 boot_serial_output();
618 }
Dominik Ermelc9dc2242021-07-28 17:08:23 +0000619 } else {
620 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800621 }
Wouter Cappellee3822f82022-01-19 15:39:43 +0100622#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
623 bs_entry = true;
624#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800625}
626
627static void
628boot_serial_output(void)
629{
630 char *data;
631 int len;
632 uint16_t crc;
633 uint16_t totlen;
634 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
Dominik Ermel5ff89582022-03-03 17:09:07 +0000635 char buf[BOOT_SERIAL_OUT_MAX + sizeof(*bs_hdr) + sizeof(crc) + sizeof(totlen)];
636 char encoded_buf[BASE64_ENCODE_SIZE(sizeof(buf))];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800637
638 data = bs_obuf;
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100639 len = (uint32_t)cbor_state->payload_mut - (uint32_t)bs_obuf;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800640
641 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300642 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800643 bs_hdr->nh_len = htons(len);
644 bs_hdr->nh_group = htons(bs_hdr->nh_group);
645
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200646#ifdef __ZEPHYR__
Carles Cufib9192a42022-02-10 11:41:57 +0100647 crc = crc16_itu_t(CRC16_INITIAL_CRC, (uint8_t *)bs_hdr, sizeof(*bs_hdr));
648 crc = crc16_itu_t(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200649#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800650 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
651 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200652#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800653 crc = htons(crc);
654
Marko Kiiskila149b4572018-06-06 14:18:54 +0300655 boot_uf->write(pkt_start, sizeof(pkt_start));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800656
657 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
658 totlen = htons(totlen);
659
660 memcpy(buf, &totlen, sizeof(totlen));
661 totlen = sizeof(totlen);
662 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
663 totlen += sizeof(*bs_hdr);
664 memcpy(&buf[totlen], data, len);
665 totlen += len;
666 memcpy(&buf[totlen], &crc, sizeof(crc));
667 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200668#ifdef __ZEPHYR__
669 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +0200670 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200671 totlen = enc_len;
672#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800673 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200674#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +0300675 boot_uf->write(encoded_buf, totlen);
Dominik Ermel2f2b31c2022-03-03 12:39:27 +0000676 boot_uf->write("\n", 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200677 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800678}
679
680/*
681 * Returns 1 if full packet has been received.
682 */
683static int
684boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
685{
686 int rc;
687 uint16_t crc;
688 uint16_t len;
Marko Kiiskilae5aeee42018-12-21 15:00:16 +0200689
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200690#ifdef __ZEPHYR__
691 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200692 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200693 if (err) {
694 return -1;
695 }
696#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800697 if (*out_off + base64_decode_len(in) >= maxout) {
698 return -1;
699 }
700 rc = base64_decode(in, &out[*out_off]);
701 if (rc < 0) {
702 return -1;
703 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200704#endif
Fabio Utzig6f49c272019-08-23 11:42:58 -0300705
Christopher Collins92ea77f2016-12-12 15:59:26 -0800706 *out_off += rc;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300707 if (*out_off <= sizeof(uint16_t)) {
708 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800709 }
Fabio Utzig6f49c272019-08-23 11:42:58 -0300710
711 len = ntohs(*(uint16_t *)out);
712 if (len != *out_off - sizeof(uint16_t)) {
713 return 0;
714 }
715
716 if (len > *out_off - sizeof(uint16_t)) {
717 len = *out_off - sizeof(uint16_t);
718 }
719
720 out += sizeof(uint16_t);
721#ifdef __ZEPHYR__
Carles Cufib9192a42022-02-10 11:41:57 +0100722 crc = crc16_itu_t(CRC16_INITIAL_CRC, out, len);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300723#else
724 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
725#endif
726 if (crc || len <= sizeof(crc)) {
727 return 0;
728 }
729 *out_off -= sizeof(crc);
730 out[*out_off] = '\0';
731
732 return 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800733}
734
735/*
736 * Task which waits reading console, expecting to get image over
737 * serial port.
738 */
Wouter Cappellee3822f82022-01-19 15:39:43 +0100739static void
740boot_serial_read_console(const struct boot_uart_funcs *f,int timeout_in_ms)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800741{
742 int rc;
743 int off;
David Brown57f0df32020-05-12 08:39:21 -0600744 int dec_off = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800745 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300746 int max_input;
Wouter Cappellee3822f82022-01-19 15:39:43 +0100747 int elapsed_in_ms = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800748
Marko Kiiskila149b4572018-06-06 14:18:54 +0300749 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +0300750 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800751
752 off = 0;
Wouter Cappellee3822f82022-01-19 15:39:43 +0100753 while (timeout_in_ms > 0 || bs_entry) {
Andrzej Puzdrowskiaea38eb2021-06-11 12:28:59 +0200754 MCUBOOT_CPU_IDLE();
Hein Wessels56d28f02021-11-19 08:42:08 +0100755 MCUBOOT_WATCHDOG_FEED();
Wouter Cappellee3822f82022-01-19 15:39:43 +0100756#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
757 uint32_t start = k_uptime_get_32();
758#endif
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200759 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800760 if (rc <= 0 && !full_line) {
Wouter Cappellee3822f82022-01-19 15:39:43 +0100761 goto check_timeout;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800762 }
763 off += rc;
764 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300765 if (off == max_input) {
766 /*
767 * Full line, no newline yet. Reset the input buffer.
768 */
769 off = 0;
770 }
Wouter Cappellee3822f82022-01-19 15:39:43 +0100771 goto check_timeout;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800772 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200773 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
774 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800775 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200776 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
777 } else if (in_buf[0] == SHELL_NLIP_DATA_START1 &&
778 in_buf[1] == SHELL_NLIP_DATA_START2) {
779 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800780 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200781
782 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800783 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +0200784 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800785 }
786 off = 0;
Wouter Cappellee3822f82022-01-19 15:39:43 +0100787check_timeout:
788 /* Subtract elapsed time */
789#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
790 elapsed_in_ms = (k_uptime_get_32() - start);
791#endif
792 timeout_in_ms -= elapsed_in_ms;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800793 }
794}
Wouter Cappellee3822f82022-01-19 15:39:43 +0100795
796/*
797 * Task which waits reading console, expecting to get image over
798 * serial port.
799 */
800void
801boot_serial_start(const struct boot_uart_funcs *f)
802{
803 bs_entry = true;
804 boot_serial_read_console(f,0);
805}
806
807#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
808/*
809 * Task which waits reading console for a certain amount of timeout.
810 * If within this timeout no mcumgr command is received, the function is
811 * returning, else the serial boot is never exited
812 */
813void
814boot_serial_check_start(const struct boot_uart_funcs *f, int timeout_in_ms)
815{
816 bs_entry = false;
817 boot_serial_read_console(f,timeout_in_ms);
818}
819#endif