blob: 11ba341ad153f7636533b10ca0f329aa1896b8d0 [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>
Almir Okato90be6e62022-09-23 14:52:25 -030024#include <errno.h>
Piotr Dymacz6a8746d2023-06-26 17:13:48 +020025#include <limits.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080026
27#include "sysflash/sysflash.h"
28
Fabio Utzig1a2e41a2017-11-17 12:13:09 -020029#include "bootutil/bootutil_log.h"
30
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020031#ifdef __ZEPHYR__
Fabio Baltieri888e2612022-07-19 20:54:26 +000032#include <zephyr/sys/reboot.h>
33#include <zephyr/sys/byteorder.h>
34#include <zephyr/sys/__assert.h>
35#include <zephyr/drivers/flash.h>
Gerard Marull-Paretas4eca54f2022-10-06 11:45:11 +020036#include <zephyr/kernel.h>
Fabio Baltieri888e2612022-07-19 20:54:26 +000037#include <zephyr/sys/crc.h>
38#include <zephyr/sys/base64.h>
Almir Okatoe8cbc0d2022-06-13 10:45:39 -030039#include <hal/hal_flash.h>
40#elif __ESPRESSIF__
41#include <bootloader_utility.h>
42#include <esp_rom_sys.h>
Almir Okato7d3622f2022-10-20 12:44:58 -030043#include <esp_crc.h>
Almir Okatoe8cbc0d2022-06-13 10:45:39 -030044#include <endian.h>
45#include <mbedtls/base64.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020046#else
Christopher Collins92ea77f2016-12-12 15:59:26 -080047#include <bsp/bsp.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080048#include <hal/hal_system.h>
Almir Okatoe8cbc0d2022-06-13 10:45:39 -030049#include <hal/hal_flash.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080050#include <os/endian.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080051#include <os/os_cputime.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020052#include <crc/crc16.h>
53#include <base64/base64.h>
Andrzej Puzdrowski386b5922018-04-06 19:26:24 +020054#endif /* __ZEPHYR__ */
55
Jamie McCraecb07e882023-04-14 09:28:24 +010056#include <zcbor_decode.h>
57#include <zcbor_encode.h>
58#include "zcbor_bulk.h"
59
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020060#include <flash_map_backend/flash_map_backend.h>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020061#include <os/os.h>
62#include <os/os_malloc.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080063
64#include <bootutil/image.h>
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +020065#include <bootutil/bootutil.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080066
67#include "boot_serial/boot_serial.h"
68#include "boot_serial_priv.h"
Almir Okatoe8cbc0d2022-06-13 10:45:39 -030069#include "mcuboot_config/mcuboot_config.h"
Jamie McCrae5c5222f2023-09-12 09:43:49 +010070#include "../src/bootutil_priv.h"
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +020071
Wouter Cappelle953a7612021-05-03 16:53:05 +020072#ifdef MCUBOOT_ENC_IMAGES
Jamie McCraec9fa6082023-07-21 10:23:17 +010073#include "boot_serial/boot_serial_encryption.h"
Wouter Cappelle953a7612021-05-03 16:53:05 +020074#endif
75
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +020076#include "bootutil/boot_hooks.h"
Øyvind Rønningstadf42a8202019-12-13 03:27:54 +010077
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020078BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010079
Jamie McCraecb07e882023-04-14 09:28:24 +010080#ifndef ARRAY_SIZE
81#define ARRAY_SIZE ZCBOR_ARRAY_SIZE
82#endif
83
Jamie McCraead1fb3d2022-12-01 14:24:37 +000084#ifndef MCUBOOT_SERIAL_MAX_RECEIVE_SIZE
85#define MCUBOOT_SERIAL_MAX_RECEIVE_SIZE 512
86#endif
87
Jamie McCraefac2cab2023-03-30 10:07:36 +010088#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
89#define BOOT_SERIAL_IMAGE_STATE_SIZE_MAX 48
90#else
91#define BOOT_SERIAL_IMAGE_STATE_SIZE_MAX 0
92#endif
93#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
94#define BOOT_SERIAL_HASH_SIZE_MAX 36
95#else
96#define BOOT_SERIAL_HASH_SIZE_MAX 0
97#endif
98
99#define BOOT_SERIAL_OUT_MAX ((128 + BOOT_SERIAL_IMAGE_STATE_SIZE_MAX + \
100 BOOT_SERIAL_HASH_SIZE_MAX) * BOOT_IMAGE_NUMBER)
101
Piotr Dymaczf5e77532022-10-30 17:43:45 +0100102#define BOOT_SERIAL_FRAME_MTU 124 /* 127 - pkt start (2 bytes) and stop (1 byte) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800103
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200104#ifdef __ZEPHYR__
Carles Cufi0165be82018-03-26 17:43:51 +0200105/* base64 lib encodes data to null-terminated string */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200106#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
107
108#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
109#define CRC_CITT_POLYMINAL 0x1021
110
111#define ntohs(x) sys_be16_to_cpu(x)
112#define htons(x) sys_cpu_to_be16(x)
Almir Okatoe8cbc0d2022-06-13 10:45:39 -0300113#elif __ESPRESSIF__
114#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
115#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
116
117#define ntohs(x) be16toh(x)
118#define htons(x) htobe16(x)
119
120#define base64_decode mbedtls_base64_decode
121#define base64_encode mbedtls_base64_encode
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200122#endif
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +0100123
Fabio Utzig6f49c272019-08-23 11:42:58 -0300124#if (BOOT_IMAGE_NUMBER > 1)
125#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
126#else
127#define IMAGES_ITER(x)
128#endif
129
Jamie McCraead1fb3d2022-12-01 14:24:37 +0000130static char in_buf[MCUBOOT_SERIAL_MAX_RECEIVE_SIZE + 1];
131static char dec_buf[MCUBOOT_SERIAL_MAX_RECEIVE_SIZE + 1];
Marko Kiiskila8b1ce3a2018-06-14 13:20:46 -0700132const struct boot_uart_funcs *boot_uf;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800133static struct nmgr_hdr *bs_hdr;
Wouter Cappellee3822f82022-01-19 15:39:43 +0100134static bool bs_entry;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135
136static char bs_obuf[BOOT_SERIAL_OUT_MAX];
137
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138static void boot_serial_output(void);
139
Jamie McCrae827118f2023-03-10 13:24:57 +0000140#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
141static int boot_serial_get_hash(const struct image_header *hdr,
142 const struct flash_area *fap, uint8_t *hash);
143#endif
144
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100145static zcbor_state_t cbor_state[2];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800146
Dominik Ermel4c0f6c12022-03-04 15:47:37 +0000147void reset_cbor_state(void)
148{
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100149 zcbor_new_encode_state(cbor_state, 2, (uint8_t *)bs_obuf,
Daniel DeGrassec393b542023-05-23 21:52:09 +0000150 sizeof(bs_obuf), 0);
Dominik Ermel4c0f6c12022-03-04 15:47:37 +0000151}
152
Dominik Ermel3d51e432021-06-25 17:29:50 +0000153/**
Dominik Ermelbd69c3d2021-07-28 11:27:31 +0000154 * Function that processes MGMT_GROUP_ID_PERUSER mcumgr group and may be
155 * used to process any groups that have not been processed by generic boot
156 * serial implementation.
Dominik Ermel3d51e432021-06-25 17:29:50 +0000157 *
158 * @param[in] hdr -- the decoded header of mcumgr message;
159 * @param[in] buffer -- buffer with first mcumgr message;
160 * @param[in] len -- length of of data in buffer;
161 * @param[out] *cs -- object with encoded response.
162 *
163 * @return 0 on success; non-0 error code otherwise.
164 */
165extern int bs_peruser_system_specific(const struct nmgr_hdr *hdr,
166 const char *buffer,
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100167 int len, zcbor_state_t *cs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168
Dominik Ermeld49cfc12022-06-09 08:24:48 +0000169#define zcbor_tstr_put_lit_cast(state, string) \
Jamie McCrae393af792023-04-14 11:31:16 +0100170 zcbor_tstr_encode_ptr(state, (char *)string, sizeof(string) - 1)
Dominik Ermeld49cfc12022-06-09 08:24:48 +0000171
172#ifndef MCUBOOT_USE_SNPRINTF
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173/*
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300174 * Convert version into string without use of snprintf().
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175 */
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300176static int
177u32toa(char *tgt, uint32_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800178{
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300179 char *dst;
180 uint32_t d = 1;
181 uint32_t dgt;
182 int n = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300184 dst = tgt;
185 while (val / d >= 10) {
186 d *= 10;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800187 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300188 while (d) {
189 dgt = val / d;
190 val %= d;
191 d /= 10;
192 if (n || dgt > 0 || d == 0) {
193 *dst++ = dgt + '0';
194 ++n;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800195 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800196 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300197 *dst = '\0';
198
199 return dst - tgt;
200}
201
202/*
203 * dst has to be able to fit "255.255.65535.4294967295" (25 characters).
204 */
205static void
206bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
207{
208 int off;
209
210 off = u32toa(dst, ver->iv_major);
211 dst[off++] = '.';
212 off += u32toa(dst + off, ver->iv_minor);
213 dst[off++] = '.';
214 off += u32toa(dst + off, ver->iv_revision);
Jamie McCraee5c57dd2023-03-21 14:45:21 +0000215
216 if (ver->iv_build_num != 0) {
217 dst[off++] = '.';
218 off += u32toa(dst + off, ver->iv_build_num);
219 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800220}
Dominik Ermeld49cfc12022-06-09 08:24:48 +0000221#else
222/*
223 * dst has to be able to fit "255.255.65535.4294967295" (25 characters).
224 */
225static void
226bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
227{
Jamie McCraee5c57dd2023-03-21 14:45:21 +0000228 int len;
229
230 len = snprintf(dst, maxlen, "%hu.%hu.%hu", (uint16_t)ver->iv_major,
231 (uint16_t)ver->iv_minor, ver->iv_revision);
232
233 if (ver->iv_build_num != 0 && len > 0 && len < maxlen) {
Benjamin Bigler480b97f2023-09-07 15:25:34 +0200234 snprintf(&dst[len], (maxlen - len), ".%u", ver->iv_build_num);
Jamie McCraee5c57dd2023-03-21 14:45:21 +0000235 }
Dominik Ermeld49cfc12022-06-09 08:24:48 +0000236}
237#endif /* !MCUBOOT_USE_SNPRINTF */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238
239/*
240 * List images.
241 */
242static void
243bs_list(char *buf, int len)
244{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800245 struct image_header hdr;
Øyvind Rønningstad9f4aefd2021-03-08 21:11:25 +0100246 uint32_t slot, area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800247 const struct flash_area *fap;
Fabio Utzig6f49c272019-08-23 11:42:58 -0300248 uint8_t image_index;
Jamie McCrae827118f2023-03-10 13:24:57 +0000249#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
250 uint8_t hash[32];
251#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800252
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100253 zcbor_map_start_encode(cbor_state, 1);
254 zcbor_tstr_put_lit_cast(cbor_state, "images");
255 zcbor_list_start_encode(cbor_state, 5);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300256 image_index = 0;
257 IMAGES_ITER(image_index) {
Jamie McCraefac2cab2023-03-30 10:07:36 +0100258#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
259 int swap_status = boot_swap_type_multi(image_index);
260#endif
261
Jamie McCraec7aa2c02023-09-13 12:43:30 +0100262#ifdef MCUBOOT_SINGLE_APPLICATION_SLOT
263 for (slot = 0; slot < 1; slot++) {
264#else
Fabio Utzig6f49c272019-08-23 11:42:58 -0300265 for (slot = 0; slot < 2; slot++) {
Jamie McCraec7aa2c02023-09-13 12:43:30 +0100266#endif
Jamie McCrae82feb9a2023-06-26 09:35:05 +0100267 FIH_DECLARE(fih_rc, FIH_FAILURE);
Jamie McCraefac2cab2023-03-30 10:07:36 +0100268 uint8_t tmpbuf[64];
269
270#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
271 bool active = false;
272 bool confirmed = false;
273 bool pending = false;
274 bool permanent = false;
275#endif
276
Fabio Utzig6f49c272019-08-23 11:42:58 -0300277 area_id = flash_area_id_from_multi_image_slot(image_index, slot);
278 if (flash_area_open(area_id, &fap)) {
279 continue;
280 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800281
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200282 int rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
283 BOOT_HOOK_REGULAR, image_index, slot, &hdr);
284 if (rc == BOOT_HOOK_REGULAR)
285 {
286 flash_area_read(fap, 0, &hdr, sizeof(hdr));
287 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800288
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200289 if (hdr.ih_magic == IMAGE_MAGIC)
290 {
291 BOOT_HOOK_CALL_FIH(boot_image_check_hook,
Michael Grand5047f032022-11-24 16:49:56 +0100292 FIH_BOOT_HOOK_REGULAR,
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200293 fih_rc, image_index, slot);
Michael Grand5047f032022-11-24 16:49:56 +0100294 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200295 {
Jamie McCraec7aa2c02023-09-13 12:43:30 +0100296#if defined(MCUBOOT_ENC_IMAGES)
Jamie McCrae5e6cffb2023-11-24 08:27:23 +0000297#if !defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
Jamie McCraec7aa2c02023-09-13 12:43:30 +0100298 if (IS_ENCRYPTED(&hdr) && MUST_DECRYPT(fap, image_index, &hdr)) {
Jamie McCraec9fa6082023-07-21 10:23:17 +0100299 FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
300 &hdr, tmpbuf, sizeof(tmpbuf));
301 } else {
Jamie McCrae5e6cffb2023-11-24 08:27:23 +0000302#endif
Jamie McCraec7aa2c02023-09-13 12:43:30 +0100303 if (IS_ENCRYPTED(&hdr)) {
304 /*
305 * There is an image present which has an encrypted flag set but is
306 * not encrypted, therefore remove the flag from the header and run a
307 * normal image validation on it.
308 */
309 hdr.ih_flags &= ~ENCRYPTIONFLAGS;
310 }
Jamie McCraec9fa6082023-07-21 10:23:17 +0100311#endif
Jamie McCraec7aa2c02023-09-13 12:43:30 +0100312
Jamie McCraec9fa6082023-07-21 10:23:17 +0100313 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr,
314 fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
Jamie McCrae5e6cffb2023-11-24 08:27:23 +0000315#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
Wouter Cappelle953a7612021-05-03 16:53:05 +0200316 }
317#endif
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200318 }
Jamie McCrae82feb9a2023-06-26 09:35:05 +0100319 }
Jamie McCraefac2cab2023-03-30 10:07:36 +0100320
Jamie McCrae82feb9a2023-06-26 09:35:05 +0100321 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
322 flash_area_close(fap);
323 continue;
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200324 }
325
Jamie McCrae827118f2023-03-10 13:24:57 +0000326#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
327 /* Retrieve SHA256 hash of image for identification */
328 rc = boot_serial_get_hash(&hdr, fap, hash);
329#endif
330
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200331 flash_area_close(fap);
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100332 zcbor_map_start_encode(cbor_state, 20);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300333
334#if (BOOT_IMAGE_NUMBER > 1)
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100335 zcbor_tstr_put_lit_cast(cbor_state, "image");
336 zcbor_uint32_put(cbor_state, image_index);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300337#endif
338
Jamie McCraefac2cab2023-03-30 10:07:36 +0100339#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
340 if (swap_status == BOOT_SWAP_TYPE_NONE) {
341 if (slot == BOOT_PRIMARY_SLOT) {
342 confirmed = true;
343 active = true;
344 }
345 } else if (swap_status == BOOT_SWAP_TYPE_TEST) {
346 if (slot == BOOT_PRIMARY_SLOT) {
347 confirmed = true;
348 } else {
349 pending = true;
350 }
351 } else if (swap_status == BOOT_SWAP_TYPE_PERM) {
352 if (slot == BOOT_PRIMARY_SLOT) {
353 confirmed = true;
354 } else {
355 pending = true;
356 permanent = true;
357 }
358 } else if (swap_status == BOOT_SWAP_TYPE_REVERT) {
359 if (slot == BOOT_PRIMARY_SLOT) {
360 active = true;
361 } else {
362 confirmed = true;
363 }
364 }
365
366 if (!(hdr.ih_flags & IMAGE_F_NON_BOOTABLE)) {
367 zcbor_tstr_put_lit_cast(cbor_state, "bootable");
Jamie McCrae82feb9a2023-06-26 09:35:05 +0100368 zcbor_bool_put(cbor_state, true);
Jamie McCraefac2cab2023-03-30 10:07:36 +0100369 }
370
371 if (confirmed) {
372 zcbor_tstr_put_lit_cast(cbor_state, "confirmed");
373 zcbor_bool_put(cbor_state, true);
374 }
375
376 if (active) {
377 zcbor_tstr_put_lit_cast(cbor_state, "active");
378 zcbor_bool_put(cbor_state, true);
379 }
380
381 if (pending) {
382 zcbor_tstr_put_lit_cast(cbor_state, "pending");
383 zcbor_bool_put(cbor_state, true);
384 }
385
386 if (permanent) {
387 zcbor_tstr_put_lit_cast(cbor_state, "permanent");
388 zcbor_bool_put(cbor_state, true);
389 }
390#endif
391
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100392 zcbor_tstr_put_lit_cast(cbor_state, "slot");
393 zcbor_uint32_put(cbor_state, slot);
Jamie McCrae827118f2023-03-10 13:24:57 +0000394
395#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
396 if (rc == 0) {
397 zcbor_tstr_put_lit_cast(cbor_state, "hash");
398 zcbor_bstr_encode_ptr(cbor_state, hash, sizeof(hash));
399 }
400#endif
401
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100402 zcbor_tstr_put_lit_cast(cbor_state, "version");
Fabio Utzig6f49c272019-08-23 11:42:58 -0300403
404 bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
Jamie McCrae827118f2023-03-10 13:24:57 +0000405
Jamie McCrae393af792023-04-14 11:31:16 +0100406 zcbor_tstr_encode_ptr(cbor_state, (char *)tmpbuf, strlen((char *)tmpbuf));
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100407 zcbor_map_end_encode(cbor_state, 20);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800408 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409 }
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100410 zcbor_list_end_encode(cbor_state, 5);
411 zcbor_map_end_encode(cbor_state, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800412 boot_serial_output();
413}
414
Jamie McCraefac2cab2023-03-30 10:07:36 +0100415#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
416/*
417 * Set image state.
418 */
419static void
420bs_set(char *buf, int len)
421{
422 /*
423 * Expected data format.
424 * {
425 * "confirm":<true for confirm, false for test>
426 * "hash":<hash of image (OPTIONAL for single image only)>
427 * }
428 */
429 uint8_t image_index = 0;
430 size_t decoded = 0;
431 uint8_t hash[32];
432 bool confirm;
433 struct zcbor_string img_hash;
434 bool ok;
435 int rc;
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000436
Jamie McCraefac2cab2023-03-30 10:07:36 +0100437#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
438 bool found = false;
439#endif
440
441 zcbor_state_t zsd[4];
Øyvind Rønningstad63ddb712024-01-10 10:43:18 +0100442 zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1, NULL, 0);
Jamie McCraefac2cab2023-03-30 10:07:36 +0100443
444 struct zcbor_map_decode_key_val image_set_state_decode[] = {
Jamie McCrae9fad4c12023-07-20 08:32:42 +0100445 ZCBOR_MAP_DECODE_KEY_DECODER("confirm", zcbor_bool_decode, &confirm),
Jamie McCraefac2cab2023-03-30 10:07:36 +0100446#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
447 ZCBOR_MAP_DECODE_KEY_DECODER("hash", zcbor_bstr_decode, &img_hash),
448#endif
449 };
450
451 ok = zcbor_map_decode_bulk(zsd, image_set_state_decode, ARRAY_SIZE(image_set_state_decode),
452 &decoded) == 0;
453
Piotr Dymaczf2cb5502023-06-28 12:09:19 +0200454 if (!ok) {
Jamie McCraefac2cab2023-03-30 10:07:36 +0100455 rc = MGMT_ERR_EINVAL;
456 goto out;
457 }
458
459#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
460 if ((img_hash.len != sizeof(hash) && img_hash.len != 0) ||
461 (img_hash.len == 0 && BOOT_IMAGE_NUMBER > 1)) {
462 /* Hash is required and was not provided or is invalid size */
463 rc = MGMT_ERR_EINVAL;
464 goto out;
465 }
466
467 if (img_hash.len != 0) {
468 for (image_index = 0; image_index < BOOT_IMAGE_NUMBER; ++image_index) {
469 struct image_header hdr;
470 uint32_t area_id;
471 const struct flash_area *fap;
472 uint8_t tmpbuf[64];
473
474 area_id = flash_area_id_from_multi_image_slot(image_index, 1);
475 if (flash_area_open(area_id, &fap)) {
476 BOOT_LOG_ERR("Failed to open flash area ID %d", area_id);
477 continue;
478 }
479
480 rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
481 BOOT_HOOK_REGULAR, image_index, 1, &hdr);
482 if (rc == BOOT_HOOK_REGULAR)
483 {
484 flash_area_read(fap, 0, &hdr, sizeof(hdr));
485 }
486
487 if (hdr.ih_magic == IMAGE_MAGIC)
488 {
489 FIH_DECLARE(fih_rc, FIH_FAILURE);
490
491 BOOT_HOOK_CALL_FIH(boot_image_check_hook,
492 FIH_BOOT_HOOK_REGULAR,
493 fih_rc, image_index, 1);
494 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
495 {
Jamie McCraec9fa6082023-07-21 10:23:17 +0100496#ifdef MCUBOOT_ENC_IMAGES
497 if (IS_ENCRYPTED(&hdr)) {
498 FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
499 &hdr, tmpbuf, sizeof(tmpbuf));
500 } else {
501#endif
502 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr,
503 fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
504#ifdef MCUBOOT_ENC_IMAGES
505 }
506#endif
Jamie McCraefac2cab2023-03-30 10:07:36 +0100507 }
508
509 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
510 continue;
511 }
512 }
513
514 /* Retrieve SHA256 hash of image for identification */
515 rc = boot_serial_get_hash(&hdr, fap, hash);
516 flash_area_close(fap);
517
518 if (rc == 0 && memcmp(hash, img_hash.value, sizeof(hash)) == 0) {
519 /* Hash matches, set this slot for test or confirmation */
520 found = true;
521 break;
522 }
523 }
524
525 if (!found) {
526 /* Image was not found with specified hash */
527 BOOT_LOG_ERR("Did not find image with specified hash");
528 rc = MGMT_ERR_ENOENT;
529 goto out;
530 }
531 }
532#endif
533
534 rc = boot_set_pending_multi(image_index, confirm);
535
536out:
537 if (rc == 0) {
538 /* Success - return updated list of images */
539 bs_list(buf, len);
540 } else {
541 /* Error code, only return the error */
542 zcbor_map_start_encode(cbor_state, 10);
543 zcbor_tstr_put_lit_cast(cbor_state, "rc");
544 zcbor_int32_put(cbor_state, rc);
545 zcbor_map_end_encode(cbor_state, 10);
546
547 boot_serial_output();
548 }
549}
550#endif
551
552/*
553 * Send rc code only.
554 */
555static void
556bs_rc_rsp(int rc_code)
557{
558 zcbor_map_start_encode(cbor_state, 10);
559 zcbor_tstr_put_lit_cast(cbor_state, "rc");
560 zcbor_int32_put(cbor_state, rc_code);
561 zcbor_map_end_encode(cbor_state, 10);
562 boot_serial_output();
563}
564
565static void
566bs_list_set(uint8_t op, char *buf, int len)
567{
568 if (op == NMGR_OP_READ) {
569 bs_list(buf, len);
570 } else {
571#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
572 bs_set(buf, len);
573#else
574 bs_rc_rsp(MGMT_ERR_ENOTSUP);
575#endif
576 }
577}
578
579#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000580/** Erases range of flash, aligned to sector size
581 *
582 * Function will erase all sectors withing [start, end] range; it does not check
583 * the @p start for alignment, and it will use @p end to find boundaries of las
584 * sector to erase. Function returns offset of the first byte past the last
585 * erased sector, so basically offset of next sector to be erased if needed.
586 * The function is intended to be called iteratively with previously returned
587 * offset as @p start.
588 *
589 * @param start starting offset, aligned to sector offset;
590 * @param end ending offset, maybe anywhere within sector;
591 *
592 * @retval On success: offset of the first byte past last erased sector;
593 * On failure: -EINVAL.
594 */
595static off_t erase_range(const struct flash_area *fap, off_t start, off_t end)
596{
597 struct flash_sector sect;
598 size_t size;
599 int rc;
600
601 if (end >= flash_area_get_size(fap)) {
602 return -EINVAL;
603 }
604
605 if (end < start) {
606 return start;
607 }
608
Dominik Ermel24769882023-01-05 13:36:35 +0000609 if (flash_area_get_sector(fap, end, &sect)) {
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000610 return -EINVAL;
611 }
612
613 size = flash_sector_get_off(&sect) + flash_sector_get_size(&sect) - start;
Stephanos Ioannidis09e2bd72022-07-11 22:01:49 +0900614 BOOT_LOG_INF("Erasing range 0x%jx:0x%jx", (intmax_t)start,
615 (intmax_t)(start + size - 1));
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000616
617 rc = flash_area_erase(fap, start, size);
618 if (rc != 0) {
619 BOOT_LOG_ERR("Error %d while erasing range", rc);
620 return -EINVAL;
621 }
622
623 return start + size;
624}
625#endif
626
Christopher Collins92ea77f2016-12-12 15:59:26 -0800627/*
628 * Image upload request.
629 */
630static void
631bs_upload(char *buf, int len)
632{
Dominik Ermel5bd87442022-06-13 15:14:01 +0000633 static size_t img_size; /* Total image size, held for duration of upload */
634 static uint32_t curr_off; /* Expected current offset */
635 const uint8_t *img_chunk = NULL; /* Pointer to buffer with received image chunk */
636 size_t img_chunk_len = 0; /* Length of received image chunk */
637 size_t img_chunk_off = SIZE_MAX; /* Offset of image chunk within image */
638 uint8_t rem_bytes; /* Reminder bytes after aligning chunk write to
639 * to flash alignment */
Piotr Dymacz6a8746d2023-06-26 17:13:48 +0200640 uint32_t img_num_tmp = UINT_MAX; /* Temp variable for image number */
641 static uint32_t img_num = 0;
Dominik Ermel5bd87442022-06-13 15:14:01 +0000642 size_t img_size_tmp = SIZE_MAX; /* Temp variable for image size */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800643 const struct flash_area *fap = NULL;
644 int rc;
Jamie McCraecb07e882023-04-14 09:28:24 +0100645 struct zcbor_string img_chunk_data;
646 size_t decoded = 0;
647 bool ok;
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000648#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000649 static off_t not_yet_erased = 0; /* Offset of next byte to erase; writes to flash
650 * are done in consecutive manner and erases are done
651 * to allow currently received chunk to be written;
652 * this state variable holds information where last
653 * erase has stopped to let us know whether erase
654 * is needed to be able to write current chunk.
655 */
656 static struct flash_sector status_sector;
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200657#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800658
Jamie McCraecb07e882023-04-14 09:28:24 +0100659 zcbor_state_t zsd[4];
Øyvind Rønningstad63ddb712024-01-10 10:43:18 +0100660 zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1, NULL, 0);
Jamie McCraecb07e882023-04-14 09:28:24 +0100661
662 struct zcbor_map_decode_key_val image_upload_decode[] = {
Piotr Dymacz6a8746d2023-06-26 17:13:48 +0200663 ZCBOR_MAP_DECODE_KEY_DECODER("image", zcbor_uint32_decode, &img_num_tmp),
Jamie McCraecb07e882023-04-14 09:28:24 +0100664 ZCBOR_MAP_DECODE_KEY_DECODER("data", zcbor_bstr_decode, &img_chunk_data),
665 ZCBOR_MAP_DECODE_KEY_DECODER("len", zcbor_size_decode, &img_size_tmp),
666 ZCBOR_MAP_DECODE_KEY_DECODER("off", zcbor_size_decode, &img_chunk_off),
667 };
668
669 ok = zcbor_map_decode_bulk(zsd, image_upload_decode, ARRAY_SIZE(image_upload_decode),
670 &decoded) == 0;
671
672 if (!ok) {
673 goto out_invalid_data;
674 }
675
676 img_chunk = img_chunk_data.value;
677 img_chunk_len = img_chunk_data.len;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300678
679 /*
680 * Expected data format.
681 * {
Fabio Utzig6f49c272019-08-23 11:42:58 -0300682 * "image":<image number in a multi-image set (OPTIONAL)>
683 * "data":<image data>
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300684 * "len":<image len>
685 * "off":<current offset of image data>
686 * }
687 */
688
Dominik Ermel5bd87442022-06-13 15:14:01 +0000689 if (img_chunk_off == SIZE_MAX || img_chunk == NULL) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300690 /*
691 * Offset must be set in every block.
692 */
693 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800694 }
695
Piotr Dymacz6a8746d2023-06-26 17:13:48 +0200696 /* Use image number only from packet with offset == 0. */
697 if (img_chunk_off == 0) {
698 if (img_num_tmp != UINT_MAX) {
699 img_num = img_num_tmp;
700 } else {
701 img_num = 0;
702 }
703 }
704
Dominik Ermel48decca2021-07-09 10:23:58 +0000705#if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
Fabio Utzig6f49c272019-08-23 11:42:58 -0300706 rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
Dominik Ermel48decca2021-07-09 10:23:58 +0000707#else
708 rc = flash_area_open(flash_area_id_from_direct_image(img_num), &fap);
709#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800710 if (rc) {
711 rc = MGMT_ERR_EINVAL;
712 goto out;
713 }
714
Dominik Ermel5bd87442022-06-13 15:14:01 +0000715 if (img_chunk_off == 0) {
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000716 /* Receiving chunk with 0 offset resets the upload state; this basically
717 * means that upload has started from beginning.
718 */
719 const size_t area_size = flash_area_get_size(fap);
Dominik Ermel5bd87442022-06-13 15:14:01 +0000720
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000721 curr_off = 0;
722#ifdef MCUBOOT_ERASE_PROGRESSIVELY
723 /* Get trailer sector information; this is done early because inability to get
724 * that sector information means that upload will not work anyway.
725 * TODO: This is single occurrence issue, it should get detected during tests
726 * and fixed otherwise you are deploying broken mcuboot.
727 */
Dominik Ermel24769882023-01-05 13:36:35 +0000728 if (flash_area_get_sector(fap, boot_status_off(fap), &status_sector)) {
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000729 rc = MGMT_ERR_EUNKNOWN;
730 BOOT_LOG_ERR("Unable to determine flash sector of the image trailer");
731 goto out;
732 }
733#endif
734
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200735#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
736 /* We are using swap state at end of flash area to store validation
737 * result. Make sure the user cannot write it from an image to skip validation.
738 */
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000739 if (img_size_tmp > (area_size - BOOT_MAGIC_SZ)) {
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200740 goto out_invalid_data;
741 }
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000742#else
743 if (img_size_tmp > area_size) {
744 goto out_invalid_data;
745 }
746
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200747#endif
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000748
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000749#ifndef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000750 /* Non-progressive erase erases entire image slot when first chunk of
751 * an image is received.
752 */
753 rc = flash_area_erase(fap, 0, area_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800754 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300755 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800756 }
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000757#else
758 not_yet_erased = 0;
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200759#endif
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000760
Dominik Ermel5bd87442022-06-13 15:14:01 +0000761 img_size = img_size_tmp;
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000762 } else if (img_chunk_off != curr_off) {
763 /* If received chunk offset does not match expected one jump, pretend
764 * success and jump to out; out will respond to client with success
765 * and request the expected offset, held by curr_off.
766 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800767 rc = 0;
768 goto out;
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000769 } else if (curr_off + img_chunk_len > img_size) {
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200770 rc = MGMT_ERR_EINVAL;
771 goto out;
772 }
773
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000774#ifdef MCUBOOT_ERASE_PROGRESSIVELY
775 /* Progressive erase will erase enough flash, aligned to sector size,
776 * as needed for the current chunk to be written.
777 */
778 not_yet_erased = erase_range(fap, not_yet_erased,
779 curr_off + img_chunk_len - 1);
780
781 if (not_yet_erased < 0) {
782 rc = MGMT_ERR_EINVAL;
783 goto out;
784 }
785#endif
786
787 /* Writes are aligned to flash write alignment, so may drop a few bytes
788 * from the end of the buffer; we will request these bytes again with
789 * new buffer by responding with request for offset after the last aligned
790 * write.
791 */
Dominik Ermel5bd87442022-06-13 15:14:01 +0000792 rem_bytes = img_chunk_len % flash_area_align(fap);
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000793 img_chunk_len -= rem_bytes;
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200794
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000795 if (curr_off + img_chunk_len + rem_bytes < img_size) {
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200796 rem_bytes = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300797 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200798
Dominik Ermel5bd87442022-06-13 15:14:01 +0000799 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_chunk_len);
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000800 /* Write flash aligned chunk, note that img_chunk_len now holds aligned length */
Jamie McCrae9d3fd7f2022-11-30 15:44:44 +0000801#if defined(MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE) && MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE > 0
802 if (flash_area_align(fap) > 1 &&
803 (((size_t)img_chunk) & (flash_area_align(fap) - 1)) != 0) {
804 /* Buffer address incompatible with write address, use buffer to write */
805 uint8_t write_size = MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE;
806 uint8_t wbs_aligned[MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE];
807
808 while (img_chunk_len >= flash_area_align(fap)) {
809 if (write_size > img_chunk_len) {
810 write_size = img_chunk_len;
811 }
812
813 memset(wbs_aligned, flash_area_erased_val(fap), sizeof(wbs_aligned));
814 memcpy(wbs_aligned, img_chunk, write_size);
815
816 rc = flash_area_write(fap, curr_off, wbs_aligned, write_size);
817
818 if (rc != 0) {
819 goto out;
820 }
821
822 curr_off += write_size;
823 img_chunk += write_size;
824 img_chunk_len -= write_size;
825 }
826 } else {
827 rc = flash_area_write(fap, curr_off, img_chunk, img_chunk_len);
828 }
829#else
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000830 rc = flash_area_write(fap, curr_off, img_chunk, img_chunk_len);
Jamie McCrae9d3fd7f2022-11-30 15:44:44 +0000831#endif
832
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000833 if (rc == 0 && rem_bytes) {
834 /* Non-zero rem_bytes means that last chunk needs alignment; the aligned
835 * part, in the img_chunk_len - rem_bytes count bytes, has already been
836 * written by the above write, so we are left with the rem_bytes.
837 */
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200838 uint8_t wbs_aligned[BOOT_MAX_ALIGN];
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200839
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000840 memset(wbs_aligned, flash_area_erased_val(fap), sizeof(wbs_aligned));
841 memcpy(wbs_aligned, img_chunk + img_chunk_len, rem_bytes);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200842
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000843 rc = flash_area_write(fap, curr_off + img_chunk_len, wbs_aligned,
844 flash_area_align(fap));
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200845 }
846
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300847 if (rc == 0) {
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000848 curr_off += img_chunk_len + rem_bytes;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200849 if (curr_off == img_size) {
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200850#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200851 /* Assure that sector for image trailer was erased. */
852 /* Check whether it was erased during previous upload. */
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000853 off_t start = flash_sector_get_off(&status_sector);
854
855 if (erase_range(fap, start, start) < 0) {
856 rc = MGMT_ERR_EUNKNOWN;
857 goto out;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200858 }
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200859#endif
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200860 rc = BOOT_HOOK_CALL(boot_serial_uploaded_hook, 0, img_num, fap,
861 img_size);
862 if (rc) {
863 BOOT_LOG_ERR("Error %d post upload hook", rc);
864 goto out;
865 }
866 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300867 } else {
868 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800869 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800870 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200871
Christopher Collins92ea77f2016-12-12 15:59:26 -0800872out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200873 BOOT_LOG_INF("RX: 0x%x", rc);
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100874 zcbor_map_start_encode(cbor_state, 10);
875 zcbor_tstr_put_lit_cast(cbor_state, "rc");
Jamie McCrae0b6d3432022-12-02 09:24:10 +0000876 zcbor_int32_put(cbor_state, rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800877 if (rc == 0) {
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100878 zcbor_tstr_put_lit_cast(cbor_state, "off");
879 zcbor_uint32_put(cbor_state, curr_off);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800880 }
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100881 zcbor_map_end_encode(cbor_state, 10);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800882
883 boot_serial_output();
Wouter Cappelle953a7612021-05-03 16:53:05 +0200884
885#ifdef MCUBOOT_ENC_IMAGES
Jamie McCraec9fa6082023-07-21 10:23:17 +0100886 /* Check if this upload was for the primary slot */
887#if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
888 if (flash_area_id_from_multi_image_slot(img_num, 0) == FLASH_AREA_IMAGE_PRIMARY(0))
889#else
890 if (flash_area_id_from_direct_image(img_num) == FLASH_AREA_IMAGE_PRIMARY(0))
891#endif
892 {
893 if (curr_off == img_size) {
894 /* Last sector received, now start a decryption on the image if it is encrypted */
895 rc = boot_handle_enc_fw(fap);
896 }
Wouter Cappelle953a7612021-05-03 16:53:05 +0200897 }
Jamie McCraec9fa6082023-07-21 10:23:17 +0100898#endif
899
900 flash_area_close(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800901}
902
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200903#ifdef MCUBOOT_BOOT_MGMT_ECHO
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200904static void
905bs_echo(char *buf, int len)
906{
Jamie McCraecb07e882023-04-14 09:28:24 +0100907 struct zcbor_string value = { 0 };
908 struct zcbor_string key;
909 bool ok;
Dominik Ermel88bd5672022-06-07 15:17:06 +0000910 uint32_t rc = MGMT_ERR_EINVAL;
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200911
Jamie McCraecb07e882023-04-14 09:28:24 +0100912 zcbor_state_t zsd[4];
Øyvind Rønningstad63ddb712024-01-10 10:43:18 +0100913 zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1, NULL, 0);
Jamie McCraecb07e882023-04-14 09:28:24 +0100914
915 if (!zcbor_map_start_decode(zsd)) {
Dominik Ermel88bd5672022-06-07 15:17:06 +0000916 goto out;
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200917 }
Dominik Ermel88bd5672022-06-07 15:17:06 +0000918
Jamie McCraecb07e882023-04-14 09:28:24 +0100919 do {
920 ok = zcbor_tstr_decode(zsd, &key);
921
922 if (ok) {
923 if (key.len == 1 && *key.value == 'd') {
924 ok = zcbor_tstr_decode(zsd, &value);
925 break;
926 }
927
928 ok = zcbor_any_skip(zsd, NULL);
929 }
930 } while (ok);
931
932 if (!ok || !zcbor_map_end_decode(zsd)) {
Dominik Ermel88bd5672022-06-07 15:17:06 +0000933 goto out;
934 }
935
936 zcbor_map_start_encode(cbor_state, 10);
Øyvind Rønningstad205d7e52024-01-29 11:37:45 +0100937 zcbor_tstr_put_lit(cbor_state, "r");
Jamie McCraecb07e882023-04-14 09:28:24 +0100938 if (zcbor_tstr_encode(cbor_state, &value) && zcbor_map_end_encode(cbor_state, 10)) {
Dominik Ermel88bd5672022-06-07 15:17:06 +0000939 boot_serial_output();
940 return;
941 } else {
942 rc = MGMT_ERR_ENOMEM;
943 }
944
945out:
946 reset_cbor_state();
947 bs_rc_rsp(rc);
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200948}
949#endif
950
Christopher Collins92ea77f2016-12-12 15:59:26 -0800951/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800952 * Reset, and (presumably) boot to newly uploaded image. Flush console
953 * before restarting.
954 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200955static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800956bs_reset(char *buf, int len)
957{
Dominik Ermelb26fc482022-12-09 17:10:20 +0000958 int rc = BOOT_HOOK_CALL(boot_reset_request_hook, 0, false);
959 if (rc == BOOT_RESET_REQUEST_HOOK_BUSY) {
960 rc = MGMT_ERR_EBUSY;
961 } else {
962 /* Currently whatever else is returned it is just converted
963 * to 0/no error. Boot serial starts accepting "force" parameter
964 * in command this needs to change.
965 */
966 rc = 0;
967 }
968 bs_rc_rsp(rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800969
Dominik Ermelb26fc482022-12-09 17:10:20 +0000970 if (rc == 0) {
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200971#ifdef __ZEPHYR__
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200972#ifdef CONFIG_MULTITHREADING
Dominik Ermelb26fc482022-12-09 17:10:20 +0000973 k_sleep(K_MSEC(250));
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200974#else
Dominik Ermelb26fc482022-12-09 17:10:20 +0000975 k_busy_wait(250000);
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200976#endif
Dominik Ermelb26fc482022-12-09 17:10:20 +0000977 sys_reboot(SYS_REBOOT_COLD);
Almir Okatoe8cbc0d2022-06-13 10:45:39 -0300978#elif __ESPRESSIF__
Dominik Ermelb26fc482022-12-09 17:10:20 +0000979 esp_rom_delay_us(250000);
980 bootloader_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200981#else
Dominik Ermelb26fc482022-12-09 17:10:20 +0000982 os_cputime_delay_usecs(250000);
983 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200984#endif
Dominik Ermelb26fc482022-12-09 17:10:20 +0000985 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800986}
987
988/*
989 * Parse incoming line of input from console.
990 * Expect newtmgr protocol with serial transport.
991 */
992void
993boot_serial_input(char *buf, int len)
994{
995 struct nmgr_hdr *hdr;
996
997 hdr = (struct nmgr_hdr *)buf;
998 if (len < sizeof(*hdr) ||
999 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
1000 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
1001 return;
1002 }
1003 bs_hdr = hdr;
1004 hdr->nh_group = ntohs(hdr->nh_group);
1005
1006 buf += sizeof(*hdr);
1007 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001008
Dominik Ermel4c0f6c12022-03-04 15:47:37 +00001009 reset_cbor_state();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001010
1011 /*
1012 * Limited support for commands.
1013 */
1014 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
1015 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001016 case IMGMGR_NMGR_ID_STATE:
Jamie McCraefac2cab2023-03-30 10:07:36 +01001017 bs_list_set(hdr->nh_op, buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001018 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001019 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001020 bs_upload(buf, len);
1021 break;
1022 default:
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001023 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001024 break;
1025 }
1026 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
1027 switch (hdr->nh_id) {
Wouter Cappellee3ff1752021-05-03 16:36:22 +02001028#ifdef MCUBOOT_BOOT_MGMT_ECHO
Jamie McCraee9fccef2023-11-06 14:25:57 +00001029 case NMGR_ID_ECHO:
Wouter Cappellee3ff1752021-05-03 16:36:22 +02001030 bs_echo(buf, len);
Wouter Cappellee3ff1752021-05-03 16:36:22 +02001031 break;
Jamie McCraee9fccef2023-11-06 14:25:57 +00001032#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001033 case NMGR_ID_CONS_ECHO_CTRL:
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001034 bs_rc_rsp(0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001035 break;
1036 case NMGR_ID_RESET:
1037 bs_reset(buf, len);
1038 break;
1039 default:
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001040 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001041 break;
1042 }
Dominik Ermelbd69c3d2021-07-28 11:27:31 +00001043 } else if (MCUBOOT_PERUSER_MGMT_GROUP_ENABLED == 1) {
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +01001044 if (bs_peruser_system_specific(hdr, buf, len, cbor_state) == 0) {
Dominik Ermel3d51e432021-06-25 17:29:50 +00001045 boot_serial_output();
1046 }
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001047 } else {
1048 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001049 }
Wouter Cappellee3822f82022-01-19 15:39:43 +01001050#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1051 bs_entry = true;
1052#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001053}
1054
1055static void
1056boot_serial_output(void)
1057{
1058 char *data;
Piotr Dymaczf5e77532022-10-30 17:43:45 +01001059 int len, out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001060 uint16_t crc;
1061 uint16_t totlen;
Piotr Dymaczf5e77532022-10-30 17:43:45 +01001062 char pkt_cont[2] = { SHELL_NLIP_DATA_START1, SHELL_NLIP_DATA_START2 };
Christopher Collins92ea77f2016-12-12 15:59:26 -08001063 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
Dominik Ermel5ff89582022-03-03 17:09:07 +00001064 char buf[BOOT_SERIAL_OUT_MAX + sizeof(*bs_hdr) + sizeof(crc) + sizeof(totlen)];
1065 char encoded_buf[BASE64_ENCODE_SIZE(sizeof(buf))];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001066
1067 data = bs_obuf;
Michal Gorecki54041302023-07-14 13:03:02 +02001068 len = (uintptr_t)cbor_state->payload_mut - (uintptr_t)bs_obuf;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001069
1070 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001071 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001072 bs_hdr->nh_len = htons(len);
1073 bs_hdr->nh_group = htons(bs_hdr->nh_group);
1074
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001075#ifdef __ZEPHYR__
Carles Cufib9192a42022-02-10 11:41:57 +01001076 crc = crc16_itu_t(CRC16_INITIAL_CRC, (uint8_t *)bs_hdr, sizeof(*bs_hdr));
1077 crc = crc16_itu_t(crc, data, len);
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001078#elif __ESPRESSIF__
1079 /* For ESP32 it was used the CRC API in rom/crc.h */
Almir Okato7d3622f2022-10-20 12:44:58 -03001080 crc = ~esp_crc16_be(~CRC16_INITIAL_CRC, (uint8_t *)bs_hdr, sizeof(*bs_hdr));
1081 crc = ~esp_crc16_be(~crc, (uint8_t *)data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001082#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001083 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
1084 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001085#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001086 crc = htons(crc);
1087
Christopher Collins92ea77f2016-12-12 15:59:26 -08001088 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
1089 totlen = htons(totlen);
1090
1091 memcpy(buf, &totlen, sizeof(totlen));
1092 totlen = sizeof(totlen);
1093 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
1094 totlen += sizeof(*bs_hdr);
1095 memcpy(&buf[totlen], data, len);
1096 totlen += len;
1097 memcpy(&buf[totlen], &crc, sizeof(crc));
1098 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001099#ifdef __ZEPHYR__
1100 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +02001101 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001102 totlen = enc_len;
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001103#elif __ESPRESSIF__
1104 size_t enc_len;
1105 base64_encode((unsigned char *)encoded_buf, sizeof(encoded_buf), &enc_len, (unsigned char *)buf, totlen);
1106 totlen = enc_len;
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001107#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001108 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001109#endif
Piotr Dymaczf5e77532022-10-30 17:43:45 +01001110
1111 out = 0;
1112 while (out < totlen) {
1113 if (out == 0) {
1114 boot_uf->write(pkt_start, sizeof(pkt_start));
1115 } else {
1116 boot_uf->write(pkt_cont, sizeof(pkt_cont));
1117 }
1118
1119 len = MIN(BOOT_SERIAL_FRAME_MTU, totlen - out);
1120 boot_uf->write(&encoded_buf[out], len);
1121
1122 out += len;
1123
1124 boot_uf->write("\n", 1);
1125 }
1126
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001127 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -08001128}
1129
1130/*
1131 * Returns 1 if full packet has been received.
1132 */
1133static int
1134boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
1135{
1136 int rc;
1137 uint16_t crc;
1138 uint16_t len;
Marko Kiiskilae5aeee42018-12-21 15:00:16 +02001139
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001140#ifdef __ZEPHYR__
1141 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001142 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001143 if (err) {
1144 return -1;
1145 }
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001146#elif __ESPRESSIF__
1147 int err;
1148 err = base64_decode((unsigned char *)&out[*out_off], maxout - *out_off, (size_t *)&rc, (unsigned char *)in, inlen);
1149 if (err) {
1150 return -1;
1151 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001152#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001153 if (*out_off + base64_decode_len(in) >= maxout) {
1154 return -1;
1155 }
1156 rc = base64_decode(in, &out[*out_off]);
1157 if (rc < 0) {
1158 return -1;
1159 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001160#endif
Fabio Utzig6f49c272019-08-23 11:42:58 -03001161
Christopher Collins92ea77f2016-12-12 15:59:26 -08001162 *out_off += rc;
Fabio Utzig6f49c272019-08-23 11:42:58 -03001163 if (*out_off <= sizeof(uint16_t)) {
1164 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001165 }
Fabio Utzig6f49c272019-08-23 11:42:58 -03001166
1167 len = ntohs(*(uint16_t *)out);
1168 if (len != *out_off - sizeof(uint16_t)) {
1169 return 0;
1170 }
1171
Fabio Utzig6f49c272019-08-23 11:42:58 -03001172 out += sizeof(uint16_t);
1173#ifdef __ZEPHYR__
Carles Cufib9192a42022-02-10 11:41:57 +01001174 crc = crc16_itu_t(CRC16_INITIAL_CRC, out, len);
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001175#elif __ESPRESSIF__
Almir Okato7d3622f2022-10-20 12:44:58 -03001176 crc = ~esp_crc16_be(~CRC16_INITIAL_CRC, (uint8_t *)out, len);
Fabio Utzig6f49c272019-08-23 11:42:58 -03001177#else
1178 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
1179#endif
1180 if (crc || len <= sizeof(crc)) {
1181 return 0;
1182 }
1183 *out_off -= sizeof(crc);
1184 out[*out_off] = '\0';
1185
1186 return 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001187}
1188
1189/*
1190 * Task which waits reading console, expecting to get image over
1191 * serial port.
1192 */
Wouter Cappellee3822f82022-01-19 15:39:43 +01001193static void
1194boot_serial_read_console(const struct boot_uart_funcs *f,int timeout_in_ms)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001195{
1196 int rc;
1197 int off;
David Brown57f0df32020-05-12 08:39:21 -06001198 int dec_off = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001199 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +03001200 int max_input;
Wouter Cappellee3822f82022-01-19 15:39:43 +01001201 int elapsed_in_ms = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001202
Jamie McCrae6ba46c02023-09-15 10:33:44 +01001203#ifndef MCUBOOT_SERIAL_WAIT_FOR_DFU
1204 bool allow_idle = true;
1205#endif
1206
Marko Kiiskila149b4572018-06-06 14:18:54 +03001207 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +03001208 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001209
1210 off = 0;
Wouter Cappellee3822f82022-01-19 15:39:43 +01001211 while (timeout_in_ms > 0 || bs_entry) {
Piotr Dymacz067f30a2022-08-12 18:25:34 +02001212 /*
1213 * Don't enter CPU idle state here if timeout based serial recovery is
1214 * used as otherwise the boot process hangs forever, waiting for input
1215 * from serial console (if single-thread mode is used).
1216 */
Piotr Dymacz3942e9b2022-07-18 10:19:25 +02001217#ifndef MCUBOOT_SERIAL_WAIT_FOR_DFU
Jamie McCrae6ba46c02023-09-15 10:33:44 +01001218 if (allow_idle == true) {
1219 MCUBOOT_CPU_IDLE();
1220 allow_idle = false;
1221 }
Piotr Dymacz3942e9b2022-07-18 10:19:25 +02001222#endif
Hein Wessels56d28f02021-11-19 08:42:08 +01001223 MCUBOOT_WATCHDOG_FEED();
Wouter Cappellee3822f82022-01-19 15:39:43 +01001224#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1225 uint32_t start = k_uptime_get_32();
1226#endif
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001227 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001228 if (rc <= 0 && !full_line) {
Jamie McCrae6ba46c02023-09-15 10:33:44 +01001229#ifndef MCUBOOT_SERIAL_WAIT_FOR_DFU
1230 allow_idle = true;
1231#endif
Wouter Cappellee3822f82022-01-19 15:39:43 +01001232 goto check_timeout;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001233 }
1234 off += rc;
1235 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001236 if (off == max_input) {
1237 /*
1238 * Full line, no newline yet. Reset the input buffer.
1239 */
1240 off = 0;
1241 }
Wouter Cappellee3822f82022-01-19 15:39:43 +01001242 goto check_timeout;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001243 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001244 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
1245 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001246 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001247 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
1248 } else if (in_buf[0] == SHELL_NLIP_DATA_START1 &&
1249 in_buf[1] == SHELL_NLIP_DATA_START2) {
1250 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001251 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001252
1253 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001254 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001255 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001256 }
1257 off = 0;
Wouter Cappellee3822f82022-01-19 15:39:43 +01001258check_timeout:
1259 /* Subtract elapsed time */
1260#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1261 elapsed_in_ms = (k_uptime_get_32() - start);
1262#endif
1263 timeout_in_ms -= elapsed_in_ms;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001264 }
1265}
Wouter Cappellee3822f82022-01-19 15:39:43 +01001266
1267/*
1268 * Task which waits reading console, expecting to get image over
1269 * serial port.
1270 */
1271void
1272boot_serial_start(const struct boot_uart_funcs *f)
1273{
1274 bs_entry = true;
1275 boot_serial_read_console(f,0);
1276}
1277
1278#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1279/*
1280 * Task which waits reading console for a certain amount of timeout.
1281 * If within this timeout no mcumgr command is received, the function is
1282 * returning, else the serial boot is never exited
1283 */
1284void
1285boot_serial_check_start(const struct boot_uart_funcs *f, int timeout_in_ms)
1286{
1287 bs_entry = false;
1288 boot_serial_read_console(f,timeout_in_ms);
1289}
1290#endif
Jamie McCrae827118f2023-03-10 13:24:57 +00001291
1292#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
1293/* Function to find the hash of an image, returns 0 on success. */
1294static int boot_serial_get_hash(const struct image_header *hdr,
1295 const struct flash_area *fap, uint8_t *hash)
1296{
1297 struct image_tlv_iter it;
1298 uint32_t offset;
1299 uint16_t len;
1300 uint16_t type;
1301 int rc;
1302
1303 /* Manifest data is concatenated to the end of the image.
1304 * It is encoded in TLV format.
1305 */
1306 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
1307 if (rc) {
1308 return -1;
1309 }
1310
1311 /* Traverse through the TLV area to find the image hash TLV. */
1312 while (true) {
1313 rc = bootutil_tlv_iter_next(&it, &offset, &len, &type);
1314 if (rc < 0) {
1315 return -1;
1316 } else if (rc > 0) {
1317 break;
1318 }
1319
1320 if (type == IMAGE_TLV_SHA256) {
1321 /* Get the image's hash value from the manifest section. */
1322 if (len != 32) {
1323 return -1;
1324 }
1325
1326 rc = flash_area_read(fap, offset, hash, len);
1327 if (rc) {
1328 return -1;
1329 }
1330
1331 return 0;
1332 }
1333 }
1334
1335 return -1;
1336}
1337#endif