blob: 5213866ad6ebee2b1d4db02da7427082fc5f1cb8 [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)
297 if (IS_ENCRYPTED(&hdr) && MUST_DECRYPT(fap, image_index, &hdr)) {
Jamie McCraec9fa6082023-07-21 10:23:17 +0100298 FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
299 &hdr, tmpbuf, sizeof(tmpbuf));
300 } else {
Jamie McCraec7aa2c02023-09-13 12:43:30 +0100301 if (IS_ENCRYPTED(&hdr)) {
302 /*
303 * There is an image present which has an encrypted flag set but is
304 * not encrypted, therefore remove the flag from the header and run a
305 * normal image validation on it.
306 */
307 hdr.ih_flags &= ~ENCRYPTIONFLAGS;
308 }
Jamie McCraec9fa6082023-07-21 10:23:17 +0100309#endif
Jamie McCraec7aa2c02023-09-13 12:43:30 +0100310
Jamie McCraec9fa6082023-07-21 10:23:17 +0100311 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr,
312 fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
Jamie McCraec7aa2c02023-09-13 12:43:30 +0100313#if defined(MCUBOOT_ENC_IMAGES)
Wouter Cappelle953a7612021-05-03 16:53:05 +0200314 }
315#endif
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200316 }
Jamie McCrae82feb9a2023-06-26 09:35:05 +0100317 }
Jamie McCraefac2cab2023-03-30 10:07:36 +0100318
Jamie McCrae82feb9a2023-06-26 09:35:05 +0100319 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
320 flash_area_close(fap);
321 continue;
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200322 }
323
Jamie McCrae827118f2023-03-10 13:24:57 +0000324#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
325 /* Retrieve SHA256 hash of image for identification */
326 rc = boot_serial_get_hash(&hdr, fap, hash);
327#endif
328
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200329 flash_area_close(fap);
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100330 zcbor_map_start_encode(cbor_state, 20);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300331
332#if (BOOT_IMAGE_NUMBER > 1)
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100333 zcbor_tstr_put_lit_cast(cbor_state, "image");
334 zcbor_uint32_put(cbor_state, image_index);
Fabio Utzig6f49c272019-08-23 11:42:58 -0300335#endif
336
Jamie McCraefac2cab2023-03-30 10:07:36 +0100337#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
338 if (swap_status == BOOT_SWAP_TYPE_NONE) {
339 if (slot == BOOT_PRIMARY_SLOT) {
340 confirmed = true;
341 active = true;
342 }
343 } else if (swap_status == BOOT_SWAP_TYPE_TEST) {
344 if (slot == BOOT_PRIMARY_SLOT) {
345 confirmed = true;
346 } else {
347 pending = true;
348 }
349 } else if (swap_status == BOOT_SWAP_TYPE_PERM) {
350 if (slot == BOOT_PRIMARY_SLOT) {
351 confirmed = true;
352 } else {
353 pending = true;
354 permanent = true;
355 }
356 } else if (swap_status == BOOT_SWAP_TYPE_REVERT) {
357 if (slot == BOOT_PRIMARY_SLOT) {
358 active = true;
359 } else {
360 confirmed = true;
361 }
362 }
363
364 if (!(hdr.ih_flags & IMAGE_F_NON_BOOTABLE)) {
365 zcbor_tstr_put_lit_cast(cbor_state, "bootable");
Jamie McCrae82feb9a2023-06-26 09:35:05 +0100366 zcbor_bool_put(cbor_state, true);
Jamie McCraefac2cab2023-03-30 10:07:36 +0100367 }
368
369 if (confirmed) {
370 zcbor_tstr_put_lit_cast(cbor_state, "confirmed");
371 zcbor_bool_put(cbor_state, true);
372 }
373
374 if (active) {
375 zcbor_tstr_put_lit_cast(cbor_state, "active");
376 zcbor_bool_put(cbor_state, true);
377 }
378
379 if (pending) {
380 zcbor_tstr_put_lit_cast(cbor_state, "pending");
381 zcbor_bool_put(cbor_state, true);
382 }
383
384 if (permanent) {
385 zcbor_tstr_put_lit_cast(cbor_state, "permanent");
386 zcbor_bool_put(cbor_state, true);
387 }
388#endif
389
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100390 zcbor_tstr_put_lit_cast(cbor_state, "slot");
391 zcbor_uint32_put(cbor_state, slot);
Jamie McCrae827118f2023-03-10 13:24:57 +0000392
393#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
394 if (rc == 0) {
395 zcbor_tstr_put_lit_cast(cbor_state, "hash");
396 zcbor_bstr_encode_ptr(cbor_state, hash, sizeof(hash));
397 }
398#endif
399
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100400 zcbor_tstr_put_lit_cast(cbor_state, "version");
Fabio Utzig6f49c272019-08-23 11:42:58 -0300401
402 bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
Jamie McCrae827118f2023-03-10 13:24:57 +0000403
Jamie McCrae393af792023-04-14 11:31:16 +0100404 zcbor_tstr_encode_ptr(cbor_state, (char *)tmpbuf, strlen((char *)tmpbuf));
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100405 zcbor_map_end_encode(cbor_state, 20);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800406 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800407 }
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100408 zcbor_list_end_encode(cbor_state, 5);
409 zcbor_map_end_encode(cbor_state, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410 boot_serial_output();
411}
412
Jamie McCraefac2cab2023-03-30 10:07:36 +0100413#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
414/*
415 * Set image state.
416 */
417static void
418bs_set(char *buf, int len)
419{
420 /*
421 * Expected data format.
422 * {
423 * "confirm":<true for confirm, false for test>
424 * "hash":<hash of image (OPTIONAL for single image only)>
425 * }
426 */
427 uint8_t image_index = 0;
428 size_t decoded = 0;
429 uint8_t hash[32];
430 bool confirm;
431 struct zcbor_string img_hash;
432 bool ok;
433 int rc;
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000434
Jamie McCraefac2cab2023-03-30 10:07:36 +0100435#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
436 bool found = false;
437#endif
438
439 zcbor_state_t zsd[4];
440 zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1);
441
442 struct zcbor_map_decode_key_val image_set_state_decode[] = {
Jamie McCrae9fad4c12023-07-20 08:32:42 +0100443 ZCBOR_MAP_DECODE_KEY_DECODER("confirm", zcbor_bool_decode, &confirm),
Jamie McCraefac2cab2023-03-30 10:07:36 +0100444#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
445 ZCBOR_MAP_DECODE_KEY_DECODER("hash", zcbor_bstr_decode, &img_hash),
446#endif
447 };
448
449 ok = zcbor_map_decode_bulk(zsd, image_set_state_decode, ARRAY_SIZE(image_set_state_decode),
450 &decoded) == 0;
451
Piotr Dymaczf2cb5502023-06-28 12:09:19 +0200452 if (!ok) {
Jamie McCraefac2cab2023-03-30 10:07:36 +0100453 rc = MGMT_ERR_EINVAL;
454 goto out;
455 }
456
457#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
458 if ((img_hash.len != sizeof(hash) && img_hash.len != 0) ||
459 (img_hash.len == 0 && BOOT_IMAGE_NUMBER > 1)) {
460 /* Hash is required and was not provided or is invalid size */
461 rc = MGMT_ERR_EINVAL;
462 goto out;
463 }
464
465 if (img_hash.len != 0) {
466 for (image_index = 0; image_index < BOOT_IMAGE_NUMBER; ++image_index) {
467 struct image_header hdr;
468 uint32_t area_id;
469 const struct flash_area *fap;
470 uint8_t tmpbuf[64];
471
472 area_id = flash_area_id_from_multi_image_slot(image_index, 1);
473 if (flash_area_open(area_id, &fap)) {
474 BOOT_LOG_ERR("Failed to open flash area ID %d", area_id);
475 continue;
476 }
477
478 rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
479 BOOT_HOOK_REGULAR, image_index, 1, &hdr);
480 if (rc == BOOT_HOOK_REGULAR)
481 {
482 flash_area_read(fap, 0, &hdr, sizeof(hdr));
483 }
484
485 if (hdr.ih_magic == IMAGE_MAGIC)
486 {
487 FIH_DECLARE(fih_rc, FIH_FAILURE);
488
489 BOOT_HOOK_CALL_FIH(boot_image_check_hook,
490 FIH_BOOT_HOOK_REGULAR,
491 fih_rc, image_index, 1);
492 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
493 {
Jamie McCraec9fa6082023-07-21 10:23:17 +0100494#ifdef MCUBOOT_ENC_IMAGES
495 if (IS_ENCRYPTED(&hdr)) {
496 FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
497 &hdr, tmpbuf, sizeof(tmpbuf));
498 } else {
499#endif
500 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr,
501 fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
502#ifdef MCUBOOT_ENC_IMAGES
503 }
504#endif
Jamie McCraefac2cab2023-03-30 10:07:36 +0100505 }
506
507 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
508 continue;
509 }
510 }
511
512 /* Retrieve SHA256 hash of image for identification */
513 rc = boot_serial_get_hash(&hdr, fap, hash);
514 flash_area_close(fap);
515
516 if (rc == 0 && memcmp(hash, img_hash.value, sizeof(hash)) == 0) {
517 /* Hash matches, set this slot for test or confirmation */
518 found = true;
519 break;
520 }
521 }
522
523 if (!found) {
524 /* Image was not found with specified hash */
525 BOOT_LOG_ERR("Did not find image with specified hash");
526 rc = MGMT_ERR_ENOENT;
527 goto out;
528 }
529 }
530#endif
531
532 rc = boot_set_pending_multi(image_index, confirm);
533
534out:
535 if (rc == 0) {
536 /* Success - return updated list of images */
537 bs_list(buf, len);
538 } else {
539 /* Error code, only return the error */
540 zcbor_map_start_encode(cbor_state, 10);
541 zcbor_tstr_put_lit_cast(cbor_state, "rc");
542 zcbor_int32_put(cbor_state, rc);
543 zcbor_map_end_encode(cbor_state, 10);
544
545 boot_serial_output();
546 }
547}
548#endif
549
550/*
551 * Send rc code only.
552 */
553static void
554bs_rc_rsp(int rc_code)
555{
556 zcbor_map_start_encode(cbor_state, 10);
557 zcbor_tstr_put_lit_cast(cbor_state, "rc");
558 zcbor_int32_put(cbor_state, rc_code);
559 zcbor_map_end_encode(cbor_state, 10);
560 boot_serial_output();
561}
562
563static void
564bs_list_set(uint8_t op, char *buf, int len)
565{
566 if (op == NMGR_OP_READ) {
567 bs_list(buf, len);
568 } else {
569#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
570 bs_set(buf, len);
571#else
572 bs_rc_rsp(MGMT_ERR_ENOTSUP);
573#endif
574 }
575}
576
577#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000578/** Erases range of flash, aligned to sector size
579 *
580 * Function will erase all sectors withing [start, end] range; it does not check
581 * the @p start for alignment, and it will use @p end to find boundaries of las
582 * sector to erase. Function returns offset of the first byte past the last
583 * erased sector, so basically offset of next sector to be erased if needed.
584 * The function is intended to be called iteratively with previously returned
585 * offset as @p start.
586 *
587 * @param start starting offset, aligned to sector offset;
588 * @param end ending offset, maybe anywhere within sector;
589 *
590 * @retval On success: offset of the first byte past last erased sector;
591 * On failure: -EINVAL.
592 */
593static off_t erase_range(const struct flash_area *fap, off_t start, off_t end)
594{
595 struct flash_sector sect;
596 size_t size;
597 int rc;
598
599 if (end >= flash_area_get_size(fap)) {
600 return -EINVAL;
601 }
602
603 if (end < start) {
604 return start;
605 }
606
Dominik Ermel24769882023-01-05 13:36:35 +0000607 if (flash_area_get_sector(fap, end, &sect)) {
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000608 return -EINVAL;
609 }
610
611 size = flash_sector_get_off(&sect) + flash_sector_get_size(&sect) - start;
Stephanos Ioannidis09e2bd72022-07-11 22:01:49 +0900612 BOOT_LOG_INF("Erasing range 0x%jx:0x%jx", (intmax_t)start,
613 (intmax_t)(start + size - 1));
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000614
615 rc = flash_area_erase(fap, start, size);
616 if (rc != 0) {
617 BOOT_LOG_ERR("Error %d while erasing range", rc);
618 return -EINVAL;
619 }
620
621 return start + size;
622}
623#endif
624
Christopher Collins92ea77f2016-12-12 15:59:26 -0800625/*
626 * Image upload request.
627 */
628static void
629bs_upload(char *buf, int len)
630{
Dominik Ermel5bd87442022-06-13 15:14:01 +0000631 static size_t img_size; /* Total image size, held for duration of upload */
632 static uint32_t curr_off; /* Expected current offset */
633 const uint8_t *img_chunk = NULL; /* Pointer to buffer with received image chunk */
634 size_t img_chunk_len = 0; /* Length of received image chunk */
635 size_t img_chunk_off = SIZE_MAX; /* Offset of image chunk within image */
636 uint8_t rem_bytes; /* Reminder bytes after aligning chunk write to
637 * to flash alignment */
Piotr Dymacz6a8746d2023-06-26 17:13:48 +0200638 uint32_t img_num_tmp = UINT_MAX; /* Temp variable for image number */
639 static uint32_t img_num = 0;
Dominik Ermel5bd87442022-06-13 15:14:01 +0000640 size_t img_size_tmp = SIZE_MAX; /* Temp variable for image size */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641 const struct flash_area *fap = NULL;
642 int rc;
Jamie McCraecb07e882023-04-14 09:28:24 +0100643 struct zcbor_string img_chunk_data;
644 size_t decoded = 0;
645 bool ok;
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000646#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000647 static off_t not_yet_erased = 0; /* Offset of next byte to erase; writes to flash
648 * are done in consecutive manner and erases are done
649 * to allow currently received chunk to be written;
650 * this state variable holds information where last
651 * erase has stopped to let us know whether erase
652 * is needed to be able to write current chunk.
653 */
654 static struct flash_sector status_sector;
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200655#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800656
Jamie McCraecb07e882023-04-14 09:28:24 +0100657 zcbor_state_t zsd[4];
658 zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1);
659
660 struct zcbor_map_decode_key_val image_upload_decode[] = {
Piotr Dymacz6a8746d2023-06-26 17:13:48 +0200661 ZCBOR_MAP_DECODE_KEY_DECODER("image", zcbor_uint32_decode, &img_num_tmp),
Jamie McCraecb07e882023-04-14 09:28:24 +0100662 ZCBOR_MAP_DECODE_KEY_DECODER("data", zcbor_bstr_decode, &img_chunk_data),
663 ZCBOR_MAP_DECODE_KEY_DECODER("len", zcbor_size_decode, &img_size_tmp),
664 ZCBOR_MAP_DECODE_KEY_DECODER("off", zcbor_size_decode, &img_chunk_off),
665 };
666
667 ok = zcbor_map_decode_bulk(zsd, image_upload_decode, ARRAY_SIZE(image_upload_decode),
668 &decoded) == 0;
669
670 if (!ok) {
671 goto out_invalid_data;
672 }
673
674 img_chunk = img_chunk_data.value;
675 img_chunk_len = img_chunk_data.len;
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300676
677 /*
678 * Expected data format.
679 * {
Fabio Utzig6f49c272019-08-23 11:42:58 -0300680 * "image":<image number in a multi-image set (OPTIONAL)>
681 * "data":<image data>
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300682 * "len":<image len>
683 * "off":<current offset of image data>
684 * }
685 */
686
Dominik Ermel5bd87442022-06-13 15:14:01 +0000687 if (img_chunk_off == SIZE_MAX || img_chunk == NULL) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300688 /*
689 * Offset must be set in every block.
690 */
691 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800692 }
693
Piotr Dymacz6a8746d2023-06-26 17:13:48 +0200694 /* Use image number only from packet with offset == 0. */
695 if (img_chunk_off == 0) {
696 if (img_num_tmp != UINT_MAX) {
697 img_num = img_num_tmp;
698 } else {
699 img_num = 0;
700 }
701 }
702
Dominik Ermel48decca2021-07-09 10:23:58 +0000703#if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
Fabio Utzig6f49c272019-08-23 11:42:58 -0300704 rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
Dominik Ermel48decca2021-07-09 10:23:58 +0000705#else
706 rc = flash_area_open(flash_area_id_from_direct_image(img_num), &fap);
707#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800708 if (rc) {
709 rc = MGMT_ERR_EINVAL;
710 goto out;
711 }
712
Dominik Ermel5bd87442022-06-13 15:14:01 +0000713 if (img_chunk_off == 0) {
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000714 /* Receiving chunk with 0 offset resets the upload state; this basically
715 * means that upload has started from beginning.
716 */
717 const size_t area_size = flash_area_get_size(fap);
Dominik Ermel5bd87442022-06-13 15:14:01 +0000718
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000719 curr_off = 0;
720#ifdef MCUBOOT_ERASE_PROGRESSIVELY
721 /* Get trailer sector information; this is done early because inability to get
722 * that sector information means that upload will not work anyway.
723 * TODO: This is single occurrence issue, it should get detected during tests
724 * and fixed otherwise you are deploying broken mcuboot.
725 */
Dominik Ermel24769882023-01-05 13:36:35 +0000726 if (flash_area_get_sector(fap, boot_status_off(fap), &status_sector)) {
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000727 rc = MGMT_ERR_EUNKNOWN;
728 BOOT_LOG_ERR("Unable to determine flash sector of the image trailer");
729 goto out;
730 }
731#endif
732
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200733#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
734 /* We are using swap state at end of flash area to store validation
735 * result. Make sure the user cannot write it from an image to skip validation.
736 */
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000737 if (img_size_tmp > (area_size - BOOT_MAGIC_SZ)) {
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200738 goto out_invalid_data;
739 }
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000740#else
741 if (img_size_tmp > area_size) {
742 goto out_invalid_data;
743 }
744
Wouter Cappellebb7a39d2021-05-03 16:44:44 +0200745#endif
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000746
Dominik Ermel3d4e55d2021-07-09 11:14:10 +0000747#ifndef MCUBOOT_ERASE_PROGRESSIVELY
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000748 /* Non-progressive erase erases entire image slot when first chunk of
749 * an image is received.
750 */
751 rc = flash_area_erase(fap, 0, area_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800752 if (rc) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300753 goto out_invalid_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800754 }
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000755#else
756 not_yet_erased = 0;
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200757#endif
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000758
Dominik Ermel5bd87442022-06-13 15:14:01 +0000759 img_size = img_size_tmp;
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000760 } else if (img_chunk_off != curr_off) {
761 /* If received chunk offset does not match expected one jump, pretend
762 * success and jump to out; out will respond to client with success
763 * and request the expected offset, held by curr_off.
764 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800765 rc = 0;
766 goto out;
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000767 } else if (curr_off + img_chunk_len > img_size) {
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200768 rc = MGMT_ERR_EINVAL;
769 goto out;
770 }
771
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000772#ifdef MCUBOOT_ERASE_PROGRESSIVELY
773 /* Progressive erase will erase enough flash, aligned to sector size,
774 * as needed for the current chunk to be written.
775 */
776 not_yet_erased = erase_range(fap, not_yet_erased,
777 curr_off + img_chunk_len - 1);
778
779 if (not_yet_erased < 0) {
780 rc = MGMT_ERR_EINVAL;
781 goto out;
782 }
783#endif
784
785 /* Writes are aligned to flash write alignment, so may drop a few bytes
786 * from the end of the buffer; we will request these bytes again with
787 * new buffer by responding with request for offset after the last aligned
788 * write.
789 */
Dominik Ermel5bd87442022-06-13 15:14:01 +0000790 rem_bytes = img_chunk_len % flash_area_align(fap);
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000791 img_chunk_len -= rem_bytes;
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200792
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000793 if (curr_off + img_chunk_len + rem_bytes < img_size) {
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200794 rem_bytes = 0;
Fabio Utzig30f6b2a2018-03-29 16:18:53 -0300795 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200796
Dominik Ermel5bd87442022-06-13 15:14:01 +0000797 BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_chunk_len);
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000798 /* Write flash aligned chunk, note that img_chunk_len now holds aligned length */
Jamie McCrae9d3fd7f2022-11-30 15:44:44 +0000799#if defined(MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE) && MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE > 0
800 if (flash_area_align(fap) > 1 &&
801 (((size_t)img_chunk) & (flash_area_align(fap) - 1)) != 0) {
802 /* Buffer address incompatible with write address, use buffer to write */
803 uint8_t write_size = MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE;
804 uint8_t wbs_aligned[MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE];
805
806 while (img_chunk_len >= flash_area_align(fap)) {
807 if (write_size > img_chunk_len) {
808 write_size = img_chunk_len;
809 }
810
811 memset(wbs_aligned, flash_area_erased_val(fap), sizeof(wbs_aligned));
812 memcpy(wbs_aligned, img_chunk, write_size);
813
814 rc = flash_area_write(fap, curr_off, wbs_aligned, write_size);
815
816 if (rc != 0) {
817 goto out;
818 }
819
820 curr_off += write_size;
821 img_chunk += write_size;
822 img_chunk_len -= write_size;
823 }
824 } else {
825 rc = flash_area_write(fap, curr_off, img_chunk, img_chunk_len);
826 }
827#else
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000828 rc = flash_area_write(fap, curr_off, img_chunk, img_chunk_len);
Jamie McCrae9d3fd7f2022-11-30 15:44:44 +0000829#endif
830
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000831 if (rc == 0 && rem_bytes) {
832 /* Non-zero rem_bytes means that last chunk needs alignment; the aligned
833 * part, in the img_chunk_len - rem_bytes count bytes, has already been
834 * written by the above write, so we are left with the rem_bytes.
835 */
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200836 uint8_t wbs_aligned[BOOT_MAX_ALIGN];
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200837
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000838 memset(wbs_aligned, flash_area_erased_val(fap), sizeof(wbs_aligned));
839 memcpy(wbs_aligned, img_chunk + img_chunk_len, rem_bytes);
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200840
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000841 rc = flash_area_write(fap, curr_off + img_chunk_len, wbs_aligned,
842 flash_area_align(fap));
Andrzej Puzdrowskif48de7a2020-10-19 09:42:02 +0200843 }
844
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300845 if (rc == 0) {
Dominik Ermel7d2f0bf2022-06-21 16:15:34 +0000846 curr_off += img_chunk_len + rem_bytes;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200847 if (curr_off == img_size) {
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200848#ifdef MCUBOOT_ERASE_PROGRESSIVELY
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200849 /* Assure that sector for image trailer was erased. */
850 /* Check whether it was erased during previous upload. */
Dominik Ermelbcc17b42022-06-15 15:33:04 +0000851 off_t start = flash_sector_get_off(&status_sector);
852
853 if (erase_range(fap, start, start) < 0) {
854 rc = MGMT_ERR_EUNKNOWN;
855 goto out;
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200856 }
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200857#endif
Andrzej Puzdrowski4f9c7302021-07-16 17:34:43 +0200858 rc = BOOT_HOOK_CALL(boot_serial_uploaded_hook, 0, img_num, fap,
859 img_size);
860 if (rc) {
861 BOOT_LOG_ERR("Error %d post upload hook", rc);
862 goto out;
863 }
864 }
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300865 } else {
866 out_invalid_data:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800867 rc = MGMT_ERR_EINVAL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800868 }
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200869
Christopher Collins92ea77f2016-12-12 15:59:26 -0800870out:
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200871 BOOT_LOG_INF("RX: 0x%x", rc);
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100872 zcbor_map_start_encode(cbor_state, 10);
873 zcbor_tstr_put_lit_cast(cbor_state, "rc");
Jamie McCrae0b6d3432022-12-02 09:24:10 +0000874 zcbor_int32_put(cbor_state, rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800875 if (rc == 0) {
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100876 zcbor_tstr_put_lit_cast(cbor_state, "off");
877 zcbor_uint32_put(cbor_state, curr_off);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800878 }
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +0100879 zcbor_map_end_encode(cbor_state, 10);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800880
881 boot_serial_output();
Wouter Cappelle953a7612021-05-03 16:53:05 +0200882
883#ifdef MCUBOOT_ENC_IMAGES
Jamie McCraec9fa6082023-07-21 10:23:17 +0100884 /* Check if this upload was for the primary slot */
885#if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
886 if (flash_area_id_from_multi_image_slot(img_num, 0) == FLASH_AREA_IMAGE_PRIMARY(0))
887#else
888 if (flash_area_id_from_direct_image(img_num) == FLASH_AREA_IMAGE_PRIMARY(0))
889#endif
890 {
891 if (curr_off == img_size) {
892 /* Last sector received, now start a decryption on the image if it is encrypted */
893 rc = boot_handle_enc_fw(fap);
894 }
Wouter Cappelle953a7612021-05-03 16:53:05 +0200895 }
Jamie McCraec9fa6082023-07-21 10:23:17 +0100896#endif
897
898 flash_area_close(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800899}
900
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200901#ifdef MCUBOOT_BOOT_MGMT_ECHO
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200902static void
903bs_echo(char *buf, int len)
904{
Jamie McCraecb07e882023-04-14 09:28:24 +0100905 struct zcbor_string value = { 0 };
906 struct zcbor_string key;
907 bool ok;
Dominik Ermel88bd5672022-06-07 15:17:06 +0000908 uint32_t rc = MGMT_ERR_EINVAL;
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200909
Jamie McCraecb07e882023-04-14 09:28:24 +0100910 zcbor_state_t zsd[4];
911 zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1);
912
913 if (!zcbor_map_start_decode(zsd)) {
Dominik Ermel88bd5672022-06-07 15:17:06 +0000914 goto out;
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200915 }
Dominik Ermel88bd5672022-06-07 15:17:06 +0000916
Jamie McCraecb07e882023-04-14 09:28:24 +0100917 do {
918 ok = zcbor_tstr_decode(zsd, &key);
919
920 if (ok) {
921 if (key.len == 1 && *key.value == 'd') {
922 ok = zcbor_tstr_decode(zsd, &value);
923 break;
924 }
925
926 ok = zcbor_any_skip(zsd, NULL);
927 }
928 } while (ok);
929
930 if (!ok || !zcbor_map_end_decode(zsd)) {
Dominik Ermel88bd5672022-06-07 15:17:06 +0000931 goto out;
932 }
933
934 zcbor_map_start_encode(cbor_state, 10);
935 zcbor_tstr_put_term(cbor_state, "r");
Jamie McCraecb07e882023-04-14 09:28:24 +0100936 if (zcbor_tstr_encode(cbor_state, &value) && zcbor_map_end_encode(cbor_state, 10)) {
Dominik Ermel88bd5672022-06-07 15:17:06 +0000937 boot_serial_output();
938 return;
939 } else {
940 rc = MGMT_ERR_ENOMEM;
941 }
942
943out:
944 reset_cbor_state();
945 bs_rc_rsp(rc);
Wouter Cappellee3ff1752021-05-03 16:36:22 +0200946}
947#endif
948
Christopher Collins92ea77f2016-12-12 15:59:26 -0800949/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800950 * Reset, and (presumably) boot to newly uploaded image. Flush console
951 * before restarting.
952 */
Andrzej Puzdrowski268cdd02018-04-10 12:57:54 +0200953static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800954bs_reset(char *buf, int len)
955{
Dominik Ermelb26fc482022-12-09 17:10:20 +0000956 int rc = BOOT_HOOK_CALL(boot_reset_request_hook, 0, false);
957 if (rc == BOOT_RESET_REQUEST_HOOK_BUSY) {
958 rc = MGMT_ERR_EBUSY;
959 } else {
960 /* Currently whatever else is returned it is just converted
961 * to 0/no error. Boot serial starts accepting "force" parameter
962 * in command this needs to change.
963 */
964 rc = 0;
965 }
966 bs_rc_rsp(rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800967
Dominik Ermelb26fc482022-12-09 17:10:20 +0000968 if (rc == 0) {
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200969#ifdef __ZEPHYR__
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200970#ifdef CONFIG_MULTITHREADING
Dominik Ermelb26fc482022-12-09 17:10:20 +0000971 k_sleep(K_MSEC(250));
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200972#else
Dominik Ermelb26fc482022-12-09 17:10:20 +0000973 k_busy_wait(250000);
Andrzej Puzdrowski0cf0dbd2021-05-14 11:55:57 +0200974#endif
Dominik Ermelb26fc482022-12-09 17:10:20 +0000975 sys_reboot(SYS_REBOOT_COLD);
Almir Okatoe8cbc0d2022-06-13 10:45:39 -0300976#elif __ESPRESSIF__
Dominik Ermelb26fc482022-12-09 17:10:20 +0000977 esp_rom_delay_us(250000);
978 bootloader_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200979#else
Dominik Ermelb26fc482022-12-09 17:10:20 +0000980 os_cputime_delay_usecs(250000);
981 hal_system_reset();
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200982#endif
Dominik Ermelb26fc482022-12-09 17:10:20 +0000983 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800984}
985
986/*
987 * Parse incoming line of input from console.
988 * Expect newtmgr protocol with serial transport.
989 */
990void
991boot_serial_input(char *buf, int len)
992{
993 struct nmgr_hdr *hdr;
994
995 hdr = (struct nmgr_hdr *)buf;
996 if (len < sizeof(*hdr) ||
997 (hdr->nh_op != NMGR_OP_READ && hdr->nh_op != NMGR_OP_WRITE) ||
998 (ntohs(hdr->nh_len) < len - sizeof(*hdr))) {
999 return;
1000 }
1001 bs_hdr = hdr;
1002 hdr->nh_group = ntohs(hdr->nh_group);
1003
1004 buf += sizeof(*hdr);
1005 len -= sizeof(*hdr);
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001006
Dominik Ermel4c0f6c12022-03-04 15:47:37 +00001007 reset_cbor_state();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001008
1009 /*
1010 * Limited support for commands.
1011 */
1012 if (hdr->nh_group == MGMT_GROUP_ID_IMAGE) {
1013 switch (hdr->nh_id) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001014 case IMGMGR_NMGR_ID_STATE:
Jamie McCraefac2cab2023-03-30 10:07:36 +01001015 bs_list_set(hdr->nh_op, buf, len);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001016 break;
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001017 case IMGMGR_NMGR_ID_UPLOAD:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001018 bs_upload(buf, len);
1019 break;
1020 default:
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001021 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001022 break;
1023 }
1024 } else if (hdr->nh_group == MGMT_GROUP_ID_DEFAULT) {
1025 switch (hdr->nh_id) {
Wouter Cappellee3ff1752021-05-03 16:36:22 +02001026#ifdef MCUBOOT_BOOT_MGMT_ECHO
Jamie McCraee9fccef2023-11-06 14:25:57 +00001027 case NMGR_ID_ECHO:
Wouter Cappellee3ff1752021-05-03 16:36:22 +02001028 bs_echo(buf, len);
Wouter Cappellee3ff1752021-05-03 16:36:22 +02001029 break;
Jamie McCraee9fccef2023-11-06 14:25:57 +00001030#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001031 case NMGR_ID_CONS_ECHO_CTRL:
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001032 bs_rc_rsp(0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001033 break;
1034 case NMGR_ID_RESET:
1035 bs_reset(buf, len);
1036 break;
1037 default:
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001038 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001039 break;
1040 }
Dominik Ermelbd69c3d2021-07-28 11:27:31 +00001041 } else if (MCUBOOT_PERUSER_MGMT_GROUP_ENABLED == 1) {
Øyvind Rønningstada7d34ca2022-02-28 13:47:57 +01001042 if (bs_peruser_system_specific(hdr, buf, len, cbor_state) == 0) {
Dominik Ermel3d51e432021-06-25 17:29:50 +00001043 boot_serial_output();
1044 }
Dominik Ermelc9dc2242021-07-28 17:08:23 +00001045 } else {
1046 bs_rc_rsp(MGMT_ERR_ENOTSUP);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001047 }
Wouter Cappellee3822f82022-01-19 15:39:43 +01001048#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1049 bs_entry = true;
1050#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001051}
1052
1053static void
1054boot_serial_output(void)
1055{
1056 char *data;
Piotr Dymaczf5e77532022-10-30 17:43:45 +01001057 int len, out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001058 uint16_t crc;
1059 uint16_t totlen;
Piotr Dymaczf5e77532022-10-30 17:43:45 +01001060 char pkt_cont[2] = { SHELL_NLIP_DATA_START1, SHELL_NLIP_DATA_START2 };
Christopher Collins92ea77f2016-12-12 15:59:26 -08001061 char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
Dominik Ermel5ff89582022-03-03 17:09:07 +00001062 char buf[BOOT_SERIAL_OUT_MAX + sizeof(*bs_hdr) + sizeof(crc) + sizeof(totlen)];
1063 char encoded_buf[BASE64_ENCODE_SIZE(sizeof(buf))];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001064
1065 data = bs_obuf;
Michal Gorecki54041302023-07-14 13:03:02 +02001066 len = (uintptr_t)cbor_state->payload_mut - (uintptr_t)bs_obuf;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001067
1068 bs_hdr->nh_op++;
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001069 bs_hdr->nh_flags = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001070 bs_hdr->nh_len = htons(len);
1071 bs_hdr->nh_group = htons(bs_hdr->nh_group);
1072
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001073#ifdef __ZEPHYR__
Carles Cufib9192a42022-02-10 11:41:57 +01001074 crc = crc16_itu_t(CRC16_INITIAL_CRC, (uint8_t *)bs_hdr, sizeof(*bs_hdr));
1075 crc = crc16_itu_t(crc, data, len);
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001076#elif __ESPRESSIF__
1077 /* For ESP32 it was used the CRC API in rom/crc.h */
Almir Okato7d3622f2022-10-20 12:44:58 -03001078 crc = ~esp_crc16_be(~CRC16_INITIAL_CRC, (uint8_t *)bs_hdr, sizeof(*bs_hdr));
1079 crc = ~esp_crc16_be(~crc, (uint8_t *)data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001080#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001081 crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
1082 crc = crc16_ccitt(crc, data, len);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001083#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001084 crc = htons(crc);
1085
Christopher Collins92ea77f2016-12-12 15:59:26 -08001086 totlen = len + sizeof(*bs_hdr) + sizeof(crc);
1087 totlen = htons(totlen);
1088
1089 memcpy(buf, &totlen, sizeof(totlen));
1090 totlen = sizeof(totlen);
1091 memcpy(&buf[totlen], bs_hdr, sizeof(*bs_hdr));
1092 totlen += sizeof(*bs_hdr);
1093 memcpy(&buf[totlen], data, len);
1094 totlen += len;
1095 memcpy(&buf[totlen], &crc, sizeof(crc));
1096 totlen += sizeof(crc);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001097#ifdef __ZEPHYR__
1098 size_t enc_len;
Carles Cufi0165be82018-03-26 17:43:51 +02001099 base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf, totlen);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001100 totlen = enc_len;
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001101#elif __ESPRESSIF__
1102 size_t enc_len;
1103 base64_encode((unsigned char *)encoded_buf, sizeof(encoded_buf), &enc_len, (unsigned char *)buf, totlen);
1104 totlen = enc_len;
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001105#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001106 totlen = base64_encode(buf, totlen, encoded_buf, 1);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001107#endif
Piotr Dymaczf5e77532022-10-30 17:43:45 +01001108
1109 out = 0;
1110 while (out < totlen) {
1111 if (out == 0) {
1112 boot_uf->write(pkt_start, sizeof(pkt_start));
1113 } else {
1114 boot_uf->write(pkt_cont, sizeof(pkt_cont));
1115 }
1116
1117 len = MIN(BOOT_SERIAL_FRAME_MTU, totlen - out);
1118 boot_uf->write(&encoded_buf[out], len);
1119
1120 out += len;
1121
1122 boot_uf->write("\n", 1);
1123 }
1124
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001125 BOOT_LOG_INF("TX");
Christopher Collins92ea77f2016-12-12 15:59:26 -08001126}
1127
1128/*
1129 * Returns 1 if full packet has been received.
1130 */
1131static int
1132boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
1133{
1134 int rc;
1135 uint16_t crc;
1136 uint16_t len;
Marko Kiiskilae5aeee42018-12-21 15:00:16 +02001137
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001138#ifdef __ZEPHYR__
1139 int err;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001140 err = base64_decode( &out[*out_off], maxout - *out_off, &rc, in, inlen - 2);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001141 if (err) {
1142 return -1;
1143 }
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001144#elif __ESPRESSIF__
1145 int err;
1146 err = base64_decode((unsigned char *)&out[*out_off], maxout - *out_off, (size_t *)&rc, (unsigned char *)in, inlen);
1147 if (err) {
1148 return -1;
1149 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001150#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001151 if (*out_off + base64_decode_len(in) >= maxout) {
1152 return -1;
1153 }
1154 rc = base64_decode(in, &out[*out_off]);
1155 if (rc < 0) {
1156 return -1;
1157 }
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +02001158#endif
Fabio Utzig6f49c272019-08-23 11:42:58 -03001159
Christopher Collins92ea77f2016-12-12 15:59:26 -08001160 *out_off += rc;
Fabio Utzig6f49c272019-08-23 11:42:58 -03001161 if (*out_off <= sizeof(uint16_t)) {
1162 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001163 }
Fabio Utzig6f49c272019-08-23 11:42:58 -03001164
1165 len = ntohs(*(uint16_t *)out);
1166 if (len != *out_off - sizeof(uint16_t)) {
1167 return 0;
1168 }
1169
1170 if (len > *out_off - sizeof(uint16_t)) {
1171 len = *out_off - sizeof(uint16_t);
1172 }
1173
1174 out += sizeof(uint16_t);
1175#ifdef __ZEPHYR__
Carles Cufib9192a42022-02-10 11:41:57 +01001176 crc = crc16_itu_t(CRC16_INITIAL_CRC, out, len);
Almir Okatoe8cbc0d2022-06-13 10:45:39 -03001177#elif __ESPRESSIF__
Almir Okato7d3622f2022-10-20 12:44:58 -03001178 crc = ~esp_crc16_be(~CRC16_INITIAL_CRC, (uint8_t *)out, len);
Fabio Utzig6f49c272019-08-23 11:42:58 -03001179#else
1180 crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
1181#endif
1182 if (crc || len <= sizeof(crc)) {
1183 return 0;
1184 }
1185 *out_off -= sizeof(crc);
1186 out[*out_off] = '\0';
1187
1188 return 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001189}
1190
1191/*
1192 * Task which waits reading console, expecting to get image over
1193 * serial port.
1194 */
Wouter Cappellee3822f82022-01-19 15:39:43 +01001195static void
1196boot_serial_read_console(const struct boot_uart_funcs *f,int timeout_in_ms)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001197{
1198 int rc;
1199 int off;
David Brown57f0df32020-05-12 08:39:21 -06001200 int dec_off = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001201 int full_line;
Marko Kiiskila149b4572018-06-06 14:18:54 +03001202 int max_input;
Wouter Cappellee3822f82022-01-19 15:39:43 +01001203 int elapsed_in_ms = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001204
Jamie McCrae6ba46c02023-09-15 10:33:44 +01001205#ifndef MCUBOOT_SERIAL_WAIT_FOR_DFU
1206 bool allow_idle = true;
1207#endif
1208
Marko Kiiskila149b4572018-06-06 14:18:54 +03001209 boot_uf = f;
Marko Kiiskila149b4572018-06-06 14:18:54 +03001210 max_input = sizeof(in_buf);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001211
1212 off = 0;
Wouter Cappellee3822f82022-01-19 15:39:43 +01001213 while (timeout_in_ms > 0 || bs_entry) {
Piotr Dymacz067f30a2022-08-12 18:25:34 +02001214 /*
1215 * Don't enter CPU idle state here if timeout based serial recovery is
1216 * used as otherwise the boot process hangs forever, waiting for input
1217 * from serial console (if single-thread mode is used).
1218 */
Piotr Dymacz3942e9b2022-07-18 10:19:25 +02001219#ifndef MCUBOOT_SERIAL_WAIT_FOR_DFU
Jamie McCrae6ba46c02023-09-15 10:33:44 +01001220 if (allow_idle == true) {
1221 MCUBOOT_CPU_IDLE();
1222 allow_idle = false;
1223 }
Piotr Dymacz3942e9b2022-07-18 10:19:25 +02001224#endif
Hein Wessels56d28f02021-11-19 08:42:08 +01001225 MCUBOOT_WATCHDOG_FEED();
Wouter Cappellee3822f82022-01-19 15:39:43 +01001226#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1227 uint32_t start = k_uptime_get_32();
1228#endif
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001229 rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001230 if (rc <= 0 && !full_line) {
Jamie McCrae6ba46c02023-09-15 10:33:44 +01001231#ifndef MCUBOOT_SERIAL_WAIT_FOR_DFU
1232 allow_idle = true;
1233#endif
Wouter Cappellee3822f82022-01-19 15:39:43 +01001234 goto check_timeout;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001235 }
1236 off += rc;
1237 if (!full_line) {
Marko Kiiskilace50ab02018-06-06 11:33:33 +03001238 if (off == max_input) {
1239 /*
1240 * Full line, no newline yet. Reset the input buffer.
1241 */
1242 off = 0;
1243 }
Wouter Cappellee3822f82022-01-19 15:39:43 +01001244 goto check_timeout;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001245 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001246 if (in_buf[0] == SHELL_NLIP_PKT_START1 &&
1247 in_buf[1] == SHELL_NLIP_PKT_START2) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001248 dec_off = 0;
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001249 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
1250 } else if (in_buf[0] == SHELL_NLIP_DATA_START1 &&
1251 in_buf[1] == SHELL_NLIP_DATA_START2) {
1252 rc = boot_serial_in_dec(&in_buf[2], off - 2, dec_buf, &dec_off, max_input);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001253 }
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001254
1255 /* serve errors: out of decode memory, or bad encoding */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001256 if (rc == 1) {
Andrzej Puzdrowskiec1e4d12018-06-18 14:36:14 +02001257 boot_serial_input(&dec_buf[2], dec_off - 2);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001258 }
1259 off = 0;
Wouter Cappellee3822f82022-01-19 15:39:43 +01001260check_timeout:
1261 /* Subtract elapsed time */
1262#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1263 elapsed_in_ms = (k_uptime_get_32() - start);
1264#endif
1265 timeout_in_ms -= elapsed_in_ms;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001266 }
1267}
Wouter Cappellee3822f82022-01-19 15:39:43 +01001268
1269/*
1270 * Task which waits reading console, expecting to get image over
1271 * serial port.
1272 */
1273void
1274boot_serial_start(const struct boot_uart_funcs *f)
1275{
1276 bs_entry = true;
1277 boot_serial_read_console(f,0);
1278}
1279
1280#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
1281/*
1282 * Task which waits reading console for a certain amount of timeout.
1283 * If within this timeout no mcumgr command is received, the function is
1284 * returning, else the serial boot is never exited
1285 */
1286void
1287boot_serial_check_start(const struct boot_uart_funcs *f, int timeout_in_ms)
1288{
1289 bs_entry = false;
1290 boot_serial_read_console(f,timeout_in_ms);
1291}
1292#endif
Jamie McCrae827118f2023-03-10 13:24:57 +00001293
1294#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
1295/* Function to find the hash of an image, returns 0 on success. */
1296static int boot_serial_get_hash(const struct image_header *hdr,
1297 const struct flash_area *fap, uint8_t *hash)
1298{
1299 struct image_tlv_iter it;
1300 uint32_t offset;
1301 uint16_t len;
1302 uint16_t type;
1303 int rc;
1304
1305 /* Manifest data is concatenated to the end of the image.
1306 * It is encoded in TLV format.
1307 */
1308 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
1309 if (rc) {
1310 return -1;
1311 }
1312
1313 /* Traverse through the TLV area to find the image hash TLV. */
1314 while (true) {
1315 rc = bootutil_tlv_iter_next(&it, &offset, &len, &type);
1316 if (rc < 0) {
1317 return -1;
1318 } else if (rc > 0) {
1319 break;
1320 }
1321
1322 if (type == IMAGE_TLV_SHA256) {
1323 /* Get the image's hash value from the manifest section. */
1324 if (len != 32) {
1325 return -1;
1326 }
1327
1328 rc = flash_area_read(fap, offset, hash, len);
1329 if (rc) {
1330 return -1;
1331 }
1332
1333 return 0;
1334 }
1335 }
1336
1337 return -1;
1338}
1339#endif