blob: c32a24fb390ef653996e303a52e571f4e5c4f404 [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
Fabio Utzig6f49c272019-08-23 11:42:58 -0300262 for (slot = 0; slot < 2; slot++) {
Jamie McCrae82feb9a2023-06-26 09:35:05 +0100263 FIH_DECLARE(fih_rc, FIH_FAILURE);
Jamie McCraefac2cab2023-03-30 10:07:36 +0100264 uint8_t tmpbuf[64];
265
266#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
267 bool active = false;
268 bool confirmed = false;
269 bool pending = false;
270 bool permanent = false;
271#endif
272
Fabio Utzig6f49c272019-08-23 11:42:58 -0300273 area_id = flash_area_id_from_multi_image_slot(image_index, slot);
274 if (flash_area_open(area_id, &fap)) {
275 continue;
276 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800277
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200278 int rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
279 BOOT_HOOK_REGULAR, image_index, slot, &hdr);
280 if (rc == BOOT_HOOK_REGULAR)
281 {
282 flash_area_read(fap, 0, &hdr, sizeof(hdr));
283 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800284
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200285 if (hdr.ih_magic == IMAGE_MAGIC)
286 {
287 BOOT_HOOK_CALL_FIH(boot_image_check_hook,
Michael Grand5047f032022-11-24 16:49:56 +0100288 FIH_BOOT_HOOK_REGULAR,
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200289 fih_rc, image_index, slot);
Michael Grand5047f032022-11-24 16:49:56 +0100290 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200291 {
Wouter Cappelle953a7612021-05-03 16:53:05 +0200292#ifdef MCUBOOT_ENC_IMAGES
Jamie McCraec9fa6082023-07-21 10:23:17 +0100293 if (IS_ENCRYPTED(&hdr)) {
294 FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
295 &hdr, tmpbuf, sizeof(tmpbuf));
296 } else {
297#endif
298 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr,
299 fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
300#ifdef MCUBOOT_ENC_IMAGES
Wouter Cappelle953a7612021-05-03 16:53:05 +0200301 }
302#endif
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200303 }
Jamie McCrae82feb9a2023-06-26 09:35:05 +0100304 }
Jamie McCraefac2cab2023-03-30 10:07:36 +0100305
Jamie McCrae82feb9a2023-06-26 09:35:05 +0100306 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
307 flash_area_close(fap);
308 continue;
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200309 }
310
Jamie McCrae827118f2023-03-10 13:24:57 +0000311#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
312 /* Retrieve SHA256 hash of image for identification */
313 rc = boot_serial_get_hash(&hdr, fap, hash);
314#endif
315
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200316 flash_area_close(fap);
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100317 zcbor_map_start_encode(cbor_state, 20);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300318
319#if (BOOT_IMAGE_NUMBER > 1)
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100320 zcbor_tstr_put_lit_cast(cbor_state, "image");
321 zcbor_uint32_put(cbor_state, image_index);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300322#endif
323
Jamie McCraefac2cab2023-03-30 10:07:36 +0100324#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
325 if (swap_status == BOOT_SWAP_TYPE_NONE) {
326 if (slot == BOOT_PRIMARY_SLOT) {
327 confirmed = true;
328 active = true;
329 }
330 } else if (swap_status == BOOT_SWAP_TYPE_TEST) {
331 if (slot == BOOT_PRIMARY_SLOT) {
332 confirmed = true;
333 } else {
334 pending = true;
335 }
336 } else if (swap_status == BOOT_SWAP_TYPE_PERM) {
337 if (slot == BOOT_PRIMARY_SLOT) {
338 confirmed = true;
339 } else {
340 pending = true;
341 permanent = true;
342 }
343 } else if (swap_status == BOOT_SWAP_TYPE_REVERT) {
344 if (slot == BOOT_PRIMARY_SLOT) {
345 active = true;
346 } else {
347 confirmed = true;
348 }
349 }
350
351 if (!(hdr.ih_flags & IMAGE_F_NON_BOOTABLE)) {
352 zcbor_tstr_put_lit_cast(cbor_state, "bootable");
Jamie McCrae82feb9a2023-06-26 09:35:05 +0100353 zcbor_bool_put(cbor_state, true);
Jamie McCraefac2cab2023-03-30 10:07:36 +0100354 }
355
356 if (confirmed) {
357 zcbor_tstr_put_lit_cast(cbor_state, "confirmed");
358 zcbor_bool_put(cbor_state, true);
359 }
360
361 if (active) {
362 zcbor_tstr_put_lit_cast(cbor_state, "active");
363 zcbor_bool_put(cbor_state, true);
364 }
365
366 if (pending) {
367 zcbor_tstr_put_lit_cast(cbor_state, "pending");
368 zcbor_bool_put(cbor_state, true);
369 }
370
371 if (permanent) {
372 zcbor_tstr_put_lit_cast(cbor_state, "permanent");
373 zcbor_bool_put(cbor_state, true);
374 }
375#endif
376
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100377 zcbor_tstr_put_lit_cast(cbor_state, "slot");
378 zcbor_uint32_put(cbor_state, slot);
Jamie McCrae827118f2023-03-10 13:24:57 +0000379
380#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
381 if (rc == 0) {
382 zcbor_tstr_put_lit_cast(cbor_state, "hash");
383 zcbor_bstr_encode_ptr(cbor_state, hash, sizeof(hash));
384 }
385#endif
386
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100387 zcbor_tstr_put_lit_cast(cbor_state, "version");
Fabio Utzig6f49c272019-08-23 11:42:58 -0300388
389 bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
Jamie McCrae827118f2023-03-10 13:24:57 +0000390
Jamie McCrae393af792023-04-14 11:31:16 +0100391 zcbor_tstr_encode_ptr(cbor_state, (char *)tmpbuf, strlen((char *)tmpbuf));
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100392 zcbor_map_end_encode(cbor_state, 20);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800393 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800394 }
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100395 zcbor_list_end_encode(cbor_state, 5);
396 zcbor_map_end_encode(cbor_state, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800397 boot_serial_output();
398}
399
Jamie McCraefac2cab2023-03-30 10:07:36 +0100400#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
401/*
402 * Set image state.
403 */
404static void
405bs_set(char *buf, int len)
406{
407 /*
408 * Expected data format.
409 * {
410 * "confirm":<true for confirm, false for test>
411 * "hash":<hash of image (OPTIONAL for single image only)>
412 * }
413 */
414 uint8_t image_index = 0;
415 size_t decoded = 0;
416 uint8_t hash[32];
417 bool confirm;
418 struct zcbor_string img_hash;
419 bool ok;
420 int rc;
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000421
Jamie McCraefac2cab2023-03-30 10:07:36 +0100422#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
423 bool found = false;
424#endif
425
426 zcbor_state_t zsd[4];
427 zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1);
428
429 struct zcbor_map_decode_key_val image_set_state_decode[] = {
Jamie McCrae9fad4c12023-07-20 08:32:42 +0100430 ZCBOR_MAP_DECODE_KEY_DECODER("confirm", zcbor_bool_decode, &confirm),
Jamie McCraefac2cab2023-03-30 10:07:36 +0100431#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
432 ZCBOR_MAP_DECODE_KEY_DECODER("hash", zcbor_bstr_decode, &img_hash),
433#endif
434 };
435
436 ok = zcbor_map_decode_bulk(zsd, image_set_state_decode, ARRAY_SIZE(image_set_state_decode),
437 &decoded) == 0;
438
Piotr Dymaczf2cb5502023-06-28 12:09:19 +0200439 if (!ok) {
Jamie McCraefac2cab2023-03-30 10:07:36 +0100440 rc = MGMT_ERR_EINVAL;
441 goto out;
442 }
443
444#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
445 if ((img_hash.len != sizeof(hash) && img_hash.len != 0) ||
446 (img_hash.len == 0 && BOOT_IMAGE_NUMBER > 1)) {
447 /* Hash is required and was not provided or is invalid size */
448 rc = MGMT_ERR_EINVAL;
449 goto out;
450 }
451
452 if (img_hash.len != 0) {
453 for (image_index = 0; image_index < BOOT_IMAGE_NUMBER; ++image_index) {
454 struct image_header hdr;
455 uint32_t area_id;
456 const struct flash_area *fap;
457 uint8_t tmpbuf[64];
458
459 area_id = flash_area_id_from_multi_image_slot(image_index, 1);
460 if (flash_area_open(area_id, &fap)) {
461 BOOT_LOG_ERR("Failed to open flash area ID %d", area_id);
462 continue;
463 }
464
465 rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
466 BOOT_HOOK_REGULAR, image_index, 1, &hdr);
467 if (rc == BOOT_HOOK_REGULAR)
468 {
469 flash_area_read(fap, 0, &hdr, sizeof(hdr));
470 }
471
472 if (hdr.ih_magic == IMAGE_MAGIC)
473 {
474 FIH_DECLARE(fih_rc, FIH_FAILURE);
475
476 BOOT_HOOK_CALL_FIH(boot_image_check_hook,
477 FIH_BOOT_HOOK_REGULAR,
478 fih_rc, image_index, 1);
479 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
480 {
Jamie McCraec9fa6082023-07-21 10:23:17 +0100481#ifdef MCUBOOT_ENC_IMAGES
482 if (IS_ENCRYPTED(&hdr)) {
483 FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
484 &hdr, tmpbuf, sizeof(tmpbuf));
485 } else {
486#endif
487 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr,
488 fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
489#ifdef MCUBOOT_ENC_IMAGES
490 }
491#endif
Jamie McCraefac2cab2023-03-30 10:07:36 +0100492 }
493
494 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
495 continue;
496 }
497 }
498
499 /* Retrieve SHA256 hash of image for identification */
500 rc = boot_serial_get_hash(&hdr, fap, hash);
501 flash_area_close(fap);
502
503 if (rc == 0 && memcmp(hash, img_hash.value, sizeof(hash)) == 0) {
504 /* Hash matches, set this slot for test or confirmation */
505 found = true;
506 break;
507 }
508 }
509
510 if (!found) {
511 /* Image was not found with specified hash */
512 BOOT_LOG_ERR("Did not find image with specified hash");
513 rc = MGMT_ERR_ENOENT;
514 goto out;
515 }
516 }
517#endif
518
519 rc = boot_set_pending_multi(image_index, confirm);
520
521out:
522 if (rc == 0) {
523 /* Success - return updated list of images */
524 bs_list(buf, len);
525 } else {
526 /* Error code, only return the error */
527 zcbor_map_start_encode(cbor_state, 10);
528 zcbor_tstr_put_lit_cast(cbor_state, "rc");
529 zcbor_int32_put(cbor_state, rc);
530 zcbor_map_end_encode(cbor_state, 10);
531
532 boot_serial_output();
533 }
534}
535#endif
536
537/*
538 * Send rc code only.
539 */
540static void
541bs_rc_rsp(int rc_code)
542{
543 zcbor_map_start_encode(cbor_state, 10);
544 zcbor_tstr_put_lit_cast(cbor_state, "rc");
545 zcbor_int32_put(cbor_state, rc_code);
546 zcbor_map_end_encode(cbor_state, 10);
547 boot_serial_output();
548}
549
550static void
551bs_list_set(uint8_t op, char *buf, int len)
552{
553 if (op == NMGR_OP_READ) {
554 bs_list(buf, len);
555 } else {
556#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
557 bs_set(buf, len);
558#else
559 bs_rc_rsp(MGMT_ERR_ENOTSUP);
560#endif
561 }
562}
563
564#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000565/** Erases range of flash, aligned to sector size
566 *
567 * Function will erase all sectors withing [start, end] range; it does not check
568 * the @p start for alignment, and it will use @p end to find boundaries of las
569 * sector to erase. Function returns offset of the first byte past the last
570 * erased sector, so basically offset of next sector to be erased if needed.
571 * The function is intended to be called iteratively with previously returned
572 * offset as @p start.
573 *
574 * @param start starting offset, aligned to sector offset;
575 * @param end ending offset, maybe anywhere within sector;
576 *
577 * @retval On success: offset of the first byte past last erased sector;
578 * On failure: -EINVAL.
579 */
580static off_t erase_range(const struct flash_area *fap, off_t start, off_t end)
581{
582 struct flash_sector sect;
583 size_t size;
584 int rc;
585
586 if (end >= flash_area_get_size(fap)) {
587 return -EINVAL;
588 }
589
590 if (end < start) {
591 return start;
592 }
593
Dominik Ermel24769882023-01-05 13:36:35 +0000594 if (flash_area_get_sector(fap, end, &sect)) {
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000595 return -EINVAL;
596 }
597
598 size = flash_sector_get_off(&sect) + flash_sector_get_size(&sect) - start;
Stephanos Ioannidis09e2bd72022-07-11 22:01:49 +0900599 BOOT_LOG_INF("Erasing range 0x%jx:0x%jx", (intmax_t)start,
600 (intmax_t)(start + size - 1));
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000601
602 rc = flash_area_erase(fap, start, size);
603 if (rc != 0) {
604 BOOT_LOG_ERR("Error %d while erasing range", rc);
605 return -EINVAL;
606 }
607
608 return start + size;
609}
610#endif
611
Christopher Collins92ea77f2016-12-12 15:59:26 -0800612/*
613 * Image upload request.
614 */
615static void
616bs_upload(char *buf, int len)
617{
Dominik Ermel5bd87442022-06-13 15:14:01 +0000618 static size_t img_size; /* Total image size, held for duration of upload */
619 static uint32_t curr_off; /* Expected current offset */
620 const uint8_t *img_chunk = NULL; /* Pointer to buffer with received image chunk */
621 size_t img_chunk_len = 0; /* Length of received image chunk */
622 size_t img_chunk_off = SIZE_MAX; /* Offset of image chunk within image */
623 uint8_t rem_bytes; /* Reminder bytes after aligning chunk write to
624 * to flash alignment */
Piotr Dymacz6a8746d2023-06-26 17:13:48 +0200625 uint32_t img_num_tmp = UINT_MAX; /* Temp variable for image number */
626 static uint32_t img_num = 0;
Dominik Ermel5bd87442022-06-13 15:14:01 +0000627 size_t img_size_tmp = SIZE_MAX; /* Temp variable for image size */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 const struct flash_area *fap = NULL;
629 int rc;
Jamie McCraecb07e882023-04-14 09:28:24 +0100630 struct zcbor_string img_chunk_data;
631 size_t decoded = 0;
632 bool ok;
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000633#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000634 static off_t not_yet_erased = 0; /* Offset of next byte to erase; writes to flash
635 * are done in consecutive manner and erases are done
636 * to allow currently received chunk to be written;
637 * this state variable holds information where last
638 * erase has stopped to let us know whether erase
639 * is needed to be able to write current chunk.
640 */
641 static struct flash_sector status_sector;
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200642#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800643
Jamie McCraecb07e882023-04-14 09:28:24 +0100644 zcbor_state_t zsd[4];
645 zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1);
646
647 struct zcbor_map_decode_key_val image_upload_decode[] = {
Piotr Dymacz6a8746d2023-06-26 17:13:48 +0200648 ZCBOR_MAP_DECODE_KEY_DECODER("image", zcbor_uint32_decode, &img_num_tmp),
Jamie McCraecb07e882023-04-14 09:28:24 +0100649 ZCBOR_MAP_DECODE_KEY_DECODER("data", zcbor_bstr_decode, &img_chunk_data),
650 ZCBOR_MAP_DECODE_KEY_DECODER("len", zcbor_size_decode, &img_size_tmp),
651 ZCBOR_MAP_DECODE_KEY_DECODER("off", zcbor_size_decode, &img_chunk_off),
652 };
653
654 ok = zcbor_map_decode_bulk(zsd, image_upload_decode, ARRAY_SIZE(image_upload_decode),
655 &decoded) == 0;
656
657 if (!ok) {
658 goto out_invalid_data;
659 }
660
661 img_chunk = img_chunk_data.value;
662 img_chunk_len = img_chunk_data.len;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300663
664 /*
665 * Expected data format.
666 * {
Fabio Utzig6f49c272019-08-23 11:42:58 -0300667 * "image":<image number in a multi-image set (OPTIONAL)>
668 * "data":<image data>
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300669 * "len":<image len>
670 * "off":<current offset of image data>
671 * }
672 */
673
Dominik Ermel5bd87442022-06-13 15:14:01 +0000674 if (img_chunk_off == SIZE_MAX || img_chunk == NULL) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300675 /*
676 * Offset must be set in every block.
677 */
678 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800679 }
680
Piotr Dymacz6a8746d2023-06-26 17:13:48 +0200681 /* Use image number only from packet with offset == 0. */
682 if (img_chunk_off == 0) {
683 if (img_num_tmp != UINT_MAX) {
684 img_num = img_num_tmp;
685 } else {
686 img_num = 0;
687 }
688 }
689
Dominik Ermel48decca2021-07-09 10:23:58 +0000690#if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
Fabio Utzig6f49c272019-08-23 11:42:58 -0300691 rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
Dominik Ermel48decca2021-07-09 10:23:58 +0000692#else
693 rc = flash_area_open(flash_area_id_from_direct_image(img_num), &fap);
694#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800695 if (rc) {
696 rc = MGMT_ERR_EINVAL;
697 goto out;
698 }
699
Dominik Ermel5bd87442022-06-13 15:14:01 +0000700 if (img_chunk_off == 0) {
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000701 /* Receiving chunk with 0 offset resets the upload state; this basically
702 * means that upload has started from beginning.
703 */
704 const size_t area_size = flash_area_get_size(fap);
Dominik Ermel5bd87442022-06-13 15:14:01 +0000705
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000706 curr_off = 0;
707#ifdef MCUBOOT_ERASE_PROGRESSIVELY
708 /* Get trailer sector information; this is done early because inability to get
709 * that sector information means that upload will not work anyway.
710 * TODO: This is single occurrence issue, it should get detected during tests
711 * and fixed otherwise you are deploying broken mcuboot.
712 */
Dominik Ermel24769882023-01-05 13:36:35 +0000713 if (flash_area_get_sector(fap, boot_status_off(fap), &status_sector)) {
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000714 rc = MGMT_ERR_EUNKNOWN;
715 BOOT_LOG_ERR("Unable to determine flash sector of the image trailer");
716 goto out;
717 }
718#endif
719
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200720#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
721 /* We are using swap state at end of flash area to store validation
722 * result. Make sure the user cannot write it from an image to skip validation.
723 */
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000724 if (img_size_tmp > (area_size - BOOT_MAGIC_SZ)) {
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200725 goto out_invalid_data;
726 }
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000727#else
728 if (img_size_tmp > area_size) {
729 goto out_invalid_data;
730 }
731
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200732#endif
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000733
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000734#ifndef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000735 /* Non-progressive erase erases entire image slot when first chunk of
736 * an image is received.
737 */
738 rc = flash_area_erase(fap, 0, area_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800739 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300740 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800741 }
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000742#else
743 not_yet_erased = 0;
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200744#endif
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000745
Dominik Ermel5bd87442022-06-13 15:14:01 +0000746 img_size = img_size_tmp;
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000747 } else if (img_chunk_off != curr_off) {
748 /* If received chunk offset does not match expected one jump, pretend
749 * success and jump to out; out will respond to client with success
750 * and request the expected offset, held by curr_off.
751 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800752 rc = 0;
753 goto out;
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000754 } else if (curr_off + img_chunk_len > img_size) {
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200755 rc = MGMT_ERR_EINVAL;
756 goto out;
757 }
758
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000759#ifdef MCUBOOT_ERASE_PROGRESSIVELY
760 /* Progressive erase will erase enough flash, aligned to sector size,
761 * as needed for the current chunk to be written.
762 */
763 not_yet_erased = erase_range(fap, not_yet_erased,
764 curr_off + img_chunk_len - 1);
765
766 if (not_yet_erased < 0) {
767 rc = MGMT_ERR_EINVAL;
768 goto out;
769 }
770#endif
771
772 /* Writes are aligned to flash write alignment, so may drop a few bytes
773 * from the end of the buffer; we will request these bytes again with
774 * new buffer by responding with request for offset after the last aligned
775 * write.
776 */
Dominik Ermel5bd87442022-06-13 15:14:01 +0000777 rem_bytes = img_chunk_len % flash_area_align(fap);
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000778 img_chunk_len -= rem_bytes;
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200779
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000780 if (curr_off + img_chunk_len + rem_bytes < img_size) {
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200781 rem_bytes = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300782 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200783
Dominik Ermel5bd87442022-06-13 15:14:01 +0000784 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_chunk_len);
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000785 /* Write flash aligned chunk, note that img_chunk_len now holds aligned length */
Jamie McCrae9d3fd7f2022-11-30 15:44:44 +0000786#if defined(MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE) && MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE > 0
787 if (flash_area_align(fap) > 1 &&
788 (((size_t)img_chunk) & (flash_area_align(fap) - 1)) != 0) {
789 /* Buffer address incompatible with write address, use buffer to write */
790 uint8_t write_size = MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE;
791 uint8_t wbs_aligned[MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE];
792
793 while (img_chunk_len >= flash_area_align(fap)) {
794 if (write_size > img_chunk_len) {
795 write_size = img_chunk_len;
796 }
797
798 memset(wbs_aligned, flash_area_erased_val(fap), sizeof(wbs_aligned));
799 memcpy(wbs_aligned, img_chunk, write_size);
800
801 rc = flash_area_write(fap, curr_off, wbs_aligned, write_size);
802
803 if (rc != 0) {
804 goto out;
805 }
806
807 curr_off += write_size;
808 img_chunk += write_size;
809 img_chunk_len -= write_size;
810 }
811 } else {
812 rc = flash_area_write(fap, curr_off, img_chunk, img_chunk_len);
813 }
814#else
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000815 rc = flash_area_write(fap, curr_off, img_chunk, img_chunk_len);
Jamie McCrae9d3fd7f2022-11-30 15:44:44 +0000816#endif
817
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000818 if (rc == 0 && rem_bytes) {
819 /* Non-zero rem_bytes means that last chunk needs alignment; the aligned
820 * part, in the img_chunk_len - rem_bytes count bytes, has already been
821 * written by the above write, so we are left with the rem_bytes.
822 */
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200823 uint8_t wbs_aligned[BOOT_MAX_ALIGN];
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200824
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000825 memset(wbs_aligned, flash_area_erased_val(fap), sizeof(wbs_aligned));
826 memcpy(wbs_aligned, img_chunk + img_chunk_len, rem_bytes);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200827
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000828 rc = flash_area_write(fap, curr_off + img_chunk_len, wbs_aligned,
829 flash_area_align(fap));
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200830 }
831
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300832 if (rc == 0) {
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000833 curr_off += img_chunk_len + rem_bytes;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200834 if (curr_off == img_size) {
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200835#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200836 /* Assure that sector for image trailer was erased. */
837 /* Check whether it was erased during previous upload. */
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000838 off_t start = flash_sector_get_off(&status_sector);
839
840 if (erase_range(fap, start, start) < 0) {
841 rc = MGMT_ERR_EUNKNOWN;
842 goto out;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200843 }
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200844#endif
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200845 rc = BOOT_HOOK_CALL(boot_serial_uploaded_hook, 0, img_num, fap,
846 img_size);
847 if (rc) {
848 BOOT_LOG_ERR("Error %d post upload hook", rc);
849 goto out;
850 }
851 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300852 } else {
853 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800854 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800855 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200856
Christopher Collins92ea77f2016-12-12 15:59:26 -0800857out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200858 BOOT_LOG_INF("RX: 0x%x", rc);
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100859 zcbor_map_start_encode(cbor_state, 10);
860 zcbor_tstr_put_lit_cast(cbor_state, "rc");
Jamie McCrae0b6d3432022-12-02 09:24:10 +0000861 zcbor_int32_put(cbor_state, rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800862 if (rc == 0) {
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100863 zcbor_tstr_put_lit_cast(cbor_state, "off");
864 zcbor_uint32_put(cbor_state, curr_off);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800865 }
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100866 zcbor_map_end_encode(cbor_state, 10);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800867
868 boot_serial_output();
Wouter Cappelle953a7612021-05-03 16:53:05 +0200869
870#ifdef MCUBOOT_ENC_IMAGES
Jamie McCraec9fa6082023-07-21 10:23:17 +0100871 /* Check if this upload was for the primary slot */
872#if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
873 if (flash_area_id_from_multi_image_slot(img_num, 0) == FLASH_AREA_IMAGE_PRIMARY(0))
874#else
875 if (flash_area_id_from_direct_image(img_num) == FLASH_AREA_IMAGE_PRIMARY(0))
876#endif
877 {
878 if (curr_off == img_size) {
879 /* Last sector received, now start a decryption on the image if it is encrypted */
880 rc = boot_handle_enc_fw(fap);
881 }
Wouter Cappelle953a7612021-05-03 16:53:05 +0200882 }
Jamie McCraec9fa6082023-07-21 10:23:17 +0100883#endif
884
885 flash_area_close(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800886}
887
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200888#ifdef MCUBOOT_BOOT_MGMT_ECHO
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200889static void
890bs_echo(char *buf, int len)
891{
Jamie McCraecb07e882023-04-14 09:28:24 +0100892 struct zcbor_string value = { 0 };
893 struct zcbor_string key;
894 bool ok;
Dominik Ermel88bd5672022-06-07 15:17:06 +0000895 uint32_t rc = MGMT_ERR_EINVAL;
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200896
Jamie McCraecb07e882023-04-14 09:28:24 +0100897 zcbor_state_t zsd[4];
898 zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1);
899
900 if (!zcbor_map_start_decode(zsd)) {
Dominik Ermel88bd5672022-06-07 15:17:06 +0000901 goto out;
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200902 }
Dominik Ermel88bd5672022-06-07 15:17:06 +0000903
Jamie McCraecb07e882023-04-14 09:28:24 +0100904 do {
905 ok = zcbor_tstr_decode(zsd, &key);
906
907 if (ok) {
908 if (key.len == 1 && *key.value == 'd') {
909 ok = zcbor_tstr_decode(zsd, &value);
910 break;
911 }
912
913 ok = zcbor_any_skip(zsd, NULL);
914 }
915 } while (ok);
916
917 if (!ok || !zcbor_map_end_decode(zsd)) {
Dominik Ermel88bd5672022-06-07 15:17:06 +0000918 goto out;
919 }
920
921 zcbor_map_start_encode(cbor_state, 10);
922 zcbor_tstr_put_term(cbor_state, "r");
Jamie McCraecb07e882023-04-14 09:28:24 +0100923 if (zcbor_tstr_encode(cbor_state, &value) && zcbor_map_end_encode(cbor_state, 10)) {
Dominik Ermel88bd5672022-06-07 15:17:06 +0000924 boot_serial_output();
925 return;
926 } else {
927 rc = MGMT_ERR_ENOMEM;
928 }
929
930out:
931 reset_cbor_state();
932 bs_rc_rsp(rc);
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200933}
934#endif
935
Christopher Collins92ea77f2016-12-12 15:59:26 -0800936/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800937 * Reset, and (presumably) boot to newly uploaded image. Flush console
938 * before restarting.
939 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200940static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800941bs_reset(char *buf, int len)
942{
Dominik Ermelb26fc482022-12-09 17:10:20 +0000943 int rc = BOOT_HOOK_CALL(boot_reset_request_hook, 0, false);
944 if (rc == BOOT_RESET_REQUEST_HOOK_BUSY) {
945 rc = MGMT_ERR_EBUSY;
946 } else {
947 /* Currently whatever else is returned it is just converted
948 * to 0/no error. Boot serial starts accepting "force" parameter
949 * in command this needs to change.
950 */
951 rc = 0;
952 }
953 bs_rc_rsp(rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800954
Dominik Ermelb26fc482022-12-09 17:10:20 +0000955 if (rc == 0) {
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200956#ifdef __ZEPHYR__
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200957#ifdef CONFIG_MULTITHREADING
Dominik Ermelb26fc482022-12-09 17:10:20 +0000958 k_sleep(K_MSEC(250));
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200959#else
Dominik Ermelb26fc482022-12-09 17:10:20 +0000960 k_busy_wait(250000);
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200961#endif
Dominik Ermelb26fc482022-12-09 17:10:20 +0000962 sys_reboot(SYS_REBOOT_COLD);
Almir Okatoe8cbc0d2022-06-13 10:45:39 -0300963#elif __ESPRESSIF__
Dominik Ermelb26fc482022-12-09 17:10:20 +0000964 esp_rom_delay_us(250000);
965 bootloader_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200966#else
Dominik Ermelb26fc482022-12-09 17:10:20 +0000967 os_cputime_delay_usecs(250000);
968 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200969#endif
Dominik Ermelb26fc482022-12-09 17:10:20 +0000970 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800971}
972
973/*
974 * Parse incoming line of input from console.
975 * Expect newtmgr protocol with serial transport.
976 */
977void
978boot_serial_input(char *buf, int len)
979{
980 struct nmgr_hdr *hdr;
981
982 hdr = (struct nmgr_hdr *)buf;
983 if (len < sizeof(*hdr) ||
984 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
985 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
986 return;
987 }
988 bs_hdr = hdr;
989 hdr->nh_group = ntohs(hdr->nh_group);
990
991 buf += sizeof(*hdr);
992 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300993
Dominik Ermel4c0f6c12022-03-04 15:47:37 +0000994 reset_cbor_state();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800995
996 /*
997 * Limited support for commands.
998 */
999 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
1000 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001001 case IMGMGR_NMGR_ID_STATE:
Jamie McCraefac2cab2023-03-30 10:07:36 +01001002 bs_list_set(hdr->nh_op, buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001003 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001004 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001005 bs_upload(buf, len);
1006 break;
1007 default:
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001008 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001009 break;
1010 }
1011 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
1012 switch (hdr->nh_id) {
Wouter Cappellee3ff1752021-05-03 16:36:22 +02001013 case NMGR_ID_ECHO:
1014#ifdef MCUBOOT_BOOT_MGMT_ECHO
1015 bs_echo(buf, len);
1016#endif
1017 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001018 case NMGR_ID_CONS_ECHO_CTRL:
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001019 bs_rc_rsp(0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001020 break;
1021 case NMGR_ID_RESET:
1022 bs_reset(buf, len);
1023 break;
1024 default:
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001025 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001026 break;
1027 }
Dominik Ermelbd69c3d2021-07-28 11:27:31 +00001028 } else if (MCUBOOT_PERUSER_MGMT_GROUP_ENABLED == 1) {
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +01001029 if (bs_peruser_system_specific(hdr, buf, len, cbor_state) == 0) {
Dominik Ermel3d51e432021-06-25 17:29:50 +00001030 boot_serial_output();
1031 }
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001032 } else {
1033 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001034 }
Wouter Cappellee3822f82022-01-19 15:39:43 +01001035#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1036 bs_entry = true;
1037#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001038}
1039
1040static void
1041boot_serial_output(void)
1042{
1043 char *data;
Piotr Dymaczf5e77532022-10-30 17:43:45 +01001044 int len, out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001045 uint16_t crc;
1046 uint16_t totlen;
Piotr Dymaczf5e77532022-10-30 17:43:45 +01001047 char pkt_cont[2] = { SHELL_NLIP_DATA_START1, SHELL_NLIP_DATA_START2 };
Christopher Collins92ea77f2016-12-12 15:59:26 -08001048 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
Dominik Ermel5ff89582022-03-03 17:09:07 +00001049 char buf[BOOT_SERIAL_OUT_MAX + sizeof(*bs_hdr) + sizeof(crc) + sizeof(totlen)];
1050 char encoded_buf[BASE64_ENCODE_SIZE(sizeof(buf))];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001051
1052 data = bs_obuf;
Michal Gorecki54041302023-07-14 13:03:02 +02001053 len = (uintptr_t)cbor_state->payload_mut - (uintptr_t)bs_obuf;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001054
1055 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001056 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001057 bs_hdr->nh_len = htons(len);
1058 bs_hdr->nh_group = htons(bs_hdr->nh_group);
1059
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001060#ifdef __ZEPHYR__
Carles Cufib9192a42022-02-10 11:41:57 +01001061 crc = crc16_itu_t(CRC16_INITIAL_CRC, (uint8_t *)bs_hdr, sizeof(*bs_hdr));
1062 crc = crc16_itu_t(crc, data, len);
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001063#elif __ESPRESSIF__
1064 /* For ESP32 it was used the CRC API in rom/crc.h */
Almir Okato7d3622f2022-10-20 12:44:58 -03001065 crc = ~esp_crc16_be(~CRC16_INITIAL_CRC, (uint8_t *)bs_hdr, sizeof(*bs_hdr));
1066 crc = ~esp_crc16_be(~crc, (uint8_t *)data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001067#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001068 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
1069 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001070#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001071 crc = htons(crc);
1072
Christopher Collins92ea77f2016-12-12 15:59:26 -08001073 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
1074 totlen = htons(totlen);
1075
1076 memcpy(buf, &totlen, sizeof(totlen));
1077 totlen = sizeof(totlen);
1078 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
1079 totlen += sizeof(*bs_hdr);
1080 memcpy(&buf[totlen], data, len);
1081 totlen += len;
1082 memcpy(&buf[totlen], &crc, sizeof(crc));
1083 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001084#ifdef __ZEPHYR__
1085 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +02001086 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001087 totlen = enc_len;
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001088#elif __ESPRESSIF__
1089 size_t enc_len;
1090 base64_encode((unsigned char *)encoded_buf, sizeof(encoded_buf), &enc_len, (unsigned char *)buf, totlen);
1091 totlen = enc_len;
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001092#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001093 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001094#endif
Piotr Dymaczf5e77532022-10-30 17:43:45 +01001095
1096 out = 0;
1097 while (out < totlen) {
1098 if (out == 0) {
1099 boot_uf->write(pkt_start, sizeof(pkt_start));
1100 } else {
1101 boot_uf->write(pkt_cont, sizeof(pkt_cont));
1102 }
1103
1104 len = MIN(BOOT_SERIAL_FRAME_MTU, totlen - out);
1105 boot_uf->write(&encoded_buf[out], len);
1106
1107 out += len;
1108
1109 boot_uf->write("\n", 1);
1110 }
1111
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001112 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -08001113}
1114
1115/*
1116 * Returns 1 if full packet has been received.
1117 */
1118static int
1119boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
1120{
1121 int rc;
1122 uint16_t crc;
1123 uint16_t len;
Marko Kiiskilae5aeee42018-12-21 15:00:16 +02001124
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001125#ifdef __ZEPHYR__
1126 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001127 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001128 if (err) {
1129 return -1;
1130 }
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001131#elif __ESPRESSIF__
1132 int err;
1133 err = base64_decode((unsigned char *)&out[*out_off], maxout - *out_off, (size_t *)&rc, (unsigned char *)in, inlen);
1134 if (err) {
1135 return -1;
1136 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001137#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001138 if (*out_off + base64_decode_len(in) >= maxout) {
1139 return -1;
1140 }
1141 rc = base64_decode(in, &out[*out_off]);
1142 if (rc < 0) {
1143 return -1;
1144 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001145#endif
Fabio Utzig6f49c272019-08-23 11:42:58 -03001146
Christopher Collins92ea77f2016-12-12 15:59:26 -08001147 *out_off += rc;
Fabio Utzig6f49c272019-08-23 11:42:58 -03001148 if (*out_off <= sizeof(uint16_t)) {
1149 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001150 }
Fabio Utzig6f49c272019-08-23 11:42:58 -03001151
1152 len = ntohs(*(uint16_t *)out);
1153 if (len != *out_off - sizeof(uint16_t)) {
1154 return 0;
1155 }
1156
1157 if (len > *out_off - sizeof(uint16_t)) {
1158 len = *out_off - sizeof(uint16_t);
1159 }
1160
1161 out += sizeof(uint16_t);
1162#ifdef __ZEPHYR__
Carles Cufib9192a42022-02-10 11:41:57 +01001163 crc = crc16_itu_t(CRC16_INITIAL_CRC, out, len);
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001164#elif __ESPRESSIF__
Almir Okato7d3622f2022-10-20 12:44:58 -03001165 crc = ~esp_crc16_be(~CRC16_INITIAL_CRC, (uint8_t *)out, len);
Fabio Utzig6f49c272019-08-23 11:42:58 -03001166#else
1167 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
1168#endif
1169 if (crc || len <= sizeof(crc)) {
1170 return 0;
1171 }
1172 *out_off -= sizeof(crc);
1173 out[*out_off] = '\0';
1174
1175 return 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001176}
1177
1178/*
1179 * Task which waits reading console, expecting to get image over
1180 * serial port.
1181 */
Wouter Cappellee3822f82022-01-19 15:39:43 +01001182static void
1183boot_serial_read_console(const struct boot_uart_funcs *f,int timeout_in_ms)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001184{
1185 int rc;
1186 int off;
David Brown57f0df32020-05-12 08:39:21 -06001187 int dec_off = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001188 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +03001189 int max_input;
Wouter Cappellee3822f82022-01-19 15:39:43 +01001190 int elapsed_in_ms = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001191
Jamie McCrae6ba46c02023-09-15 10:33:44 +01001192#ifndef MCUBOOT_SERIAL_WAIT_FOR_DFU
1193 bool allow_idle = true;
1194#endif
1195
Marko Kiiskila149b4572018-06-06 14:18:54 +03001196 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +03001197 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001198
1199 off = 0;
Wouter Cappellee3822f82022-01-19 15:39:43 +01001200 while (timeout_in_ms > 0 || bs_entry) {
Piotr Dymacz067f30a2022-08-12 18:25:34 +02001201 /*
1202 * Don't enter CPU idle state here if timeout based serial recovery is
1203 * used as otherwise the boot process hangs forever, waiting for input
1204 * from serial console (if single-thread mode is used).
1205 */
Piotr Dymacz3942e9b2022-07-18 10:19:25 +02001206#ifndef MCUBOOT_SERIAL_WAIT_FOR_DFU
Jamie McCrae6ba46c02023-09-15 10:33:44 +01001207 if (allow_idle == true) {
1208 MCUBOOT_CPU_IDLE();
1209 allow_idle = false;
1210 }
Piotr Dymacz3942e9b2022-07-18 10:19:25 +02001211#endif
Hein Wessels56d28f02021-11-19 08:42:08 +01001212 MCUBOOT_WATCHDOG_FEED();
Wouter Cappellee3822f82022-01-19 15:39:43 +01001213#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1214 uint32_t start = k_uptime_get_32();
1215#endif
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001216 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001217 if (rc <= 0 && !full_line) {
Jamie McCrae6ba46c02023-09-15 10:33:44 +01001218#ifndef MCUBOOT_SERIAL_WAIT_FOR_DFU
1219 allow_idle = true;
1220#endif
Wouter Cappellee3822f82022-01-19 15:39:43 +01001221 goto check_timeout;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001222 }
1223 off += rc;
1224 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001225 if (off == max_input) {
1226 /*
1227 * Full line, no newline yet. Reset the input buffer.
1228 */
1229 off = 0;
1230 }
Wouter Cappellee3822f82022-01-19 15:39:43 +01001231 goto check_timeout;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001232 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001233 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
1234 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001235 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001236 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
1237 } else if (in_buf[0] == SHELL_NLIP_DATA_START1 &&
1238 in_buf[1] == SHELL_NLIP_DATA_START2) {
1239 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001240 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001241
1242 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001243 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001244 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001245 }
1246 off = 0;
Wouter Cappellee3822f82022-01-19 15:39:43 +01001247check_timeout:
1248 /* Subtract elapsed time */
1249#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1250 elapsed_in_ms = (k_uptime_get_32() - start);
1251#endif
1252 timeout_in_ms -= elapsed_in_ms;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001253 }
1254}
Wouter Cappellee3822f82022-01-19 15:39:43 +01001255
1256/*
1257 * Task which waits reading console, expecting to get image over
1258 * serial port.
1259 */
1260void
1261boot_serial_start(const struct boot_uart_funcs *f)
1262{
1263 bs_entry = true;
1264 boot_serial_read_console(f,0);
1265}
1266
1267#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1268/*
1269 * Task which waits reading console for a certain amount of timeout.
1270 * If within this timeout no mcumgr command is received, the function is
1271 * returning, else the serial boot is never exited
1272 */
1273void
1274boot_serial_check_start(const struct boot_uart_funcs *f, int timeout_in_ms)
1275{
1276 bs_entry = false;
1277 boot_serial_read_console(f,timeout_in_ms);
1278}
1279#endif
Jamie McCrae827118f2023-03-10 13:24:57 +00001280
1281#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
1282/* Function to find the hash of an image, returns 0 on success. */
1283static int boot_serial_get_hash(const struct image_header *hdr,
1284 const struct flash_area *fap, uint8_t *hash)
1285{
1286 struct image_tlv_iter it;
1287 uint32_t offset;
1288 uint16_t len;
1289 uint16_t type;
1290 int rc;
1291
1292 /* Manifest data is concatenated to the end of the image.
1293 * It is encoded in TLV format.
1294 */
1295 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
1296 if (rc) {
1297 return -1;
1298 }
1299
1300 /* Traverse through the TLV area to find the image hash TLV. */
1301 while (true) {
1302 rc = bootutil_tlv_iter_next(&it, &offset, &len, &type);
1303 if (rc < 0) {
1304 return -1;
1305 } else if (rc > 0) {
1306 break;
1307 }
1308
1309 if (type == IMAGE_TLV_SHA256) {
1310 /* Get the image's hash value from the manifest section. */
1311 if (len != 32) {
1312 return -1;
1313 }
1314
1315 rc = flash_area_read(fap, offset, hash, len);
1316 if (rc) {
1317 return -1;
1318 }
1319
1320 return 0;
1321 }
1322 }
1323
1324 return -1;
1325}
1326#endif