Skip to content

Commit e261b28

Browse files
committed
boot_serial: Add optional img mgmt slot info feature
Adds a minimal version of the slot info feature to serial recovery, and enables it by default. Signed-off-by: Jamie McCrae <[email protected]>
1 parent 2db0654 commit e261b28

File tree

5 files changed

+317
-13
lines changed

5 files changed

+317
-13
lines changed

boot/boot_serial/src/boot_serial.c

Lines changed: 157 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,28 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
9595
#else
9696
#define BOOT_SERIAL_HASH_SIZE_MAX 0
9797
#endif
98+
#ifdef MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO
99+
#define BOOT_SERIAL_SLOT_INFO_SIZE_MAX 164
100+
#else
101+
#define BOOT_SERIAL_SLOT_INFO_SIZE_MAX 0
102+
#endif
103+
104+
#if (128 + BOOT_SERIAL_IMAGE_STATE_SIZE_MAX + BOOT_SERIAL_HASH_SIZE_MAX) > \
105+
BOOT_SERIAL_SLOT_INFO_SIZE_MAX
106+
#define BOOT_SERIAL_MAX_MESSAGE_SIZE (128 + BOOT_SERIAL_IMAGE_STATE_SIZE_MAX + \
107+
BOOT_SERIAL_HASH_SIZE_MAX)
108+
#else
109+
#define BOOT_SERIAL_MAX_MESSAGE_SIZE BOOT_SERIAL_SLOT_INFO_SIZE_MAX
110+
#endif
98111

99-
#define BOOT_SERIAL_OUT_MAX ((128 + BOOT_SERIAL_IMAGE_STATE_SIZE_MAX + \
100-
BOOT_SERIAL_HASH_SIZE_MAX) * BOOT_IMAGE_NUMBER)
112+
#define BOOT_SERIAL_OUT_MAX (BOOT_SERIAL_MAX_MESSAGE_SIZE * BOOT_IMAGE_NUMBER)
101113

102114
#define BOOT_SERIAL_FRAME_MTU 124 /* 127 - pkt start (2 bytes) and stop (1 byte) */
103115

116+
/* Number of estimated CBOR elements for responses */
117+
#define CBOR_ENTRIES_SLOT_INFO_IMAGE_MAP 4
118+
#define CBOR_ENTRIES_SLOT_INFO_SLOTS_MAP 3
119+
104120
#ifdef __ZEPHYR__
105121
/* base64 lib encodes data to null-terminated string */
106122
#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
@@ -259,11 +275,7 @@ bs_list(char *buf, int len)
259275
int swap_status = boot_swap_type_multi(image_index);
260276
#endif
261277

262-
#ifdef MCUBOOT_SINGLE_APPLICATION_SLOT
263-
for (slot = 0; slot < 1; slot++) {
264-
#else
265-
for (slot = 0; slot < 2; slot++) {
266-
#endif
278+
for (slot = 0; slot < MCUBOOT_IMAGE_NUMBER; slot++) {
267279
FIH_DECLARE(fih_rc, FIH_FAILURE);
268280
uint8_t tmpbuf[64];
269281

@@ -576,6 +588,139 @@ bs_list_set(uint8_t op, char *buf, int len)
576588
}
577589
}
578590

591+
#ifdef MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO
592+
static void
593+
bs_slot_info(uint8_t op, char *buf, int len)
594+
{
595+
uint32_t slot, area_id;
596+
const struct flash_area *fap;
597+
uint8_t image_index = 0;
598+
int rc;
599+
bool ok = true;
600+
const struct image_max_size *image_max_sizes;
601+
602+
if (op != NMGR_OP_READ) {
603+
bs_rc_rsp(MGMT_ERR_ENOTSUP);
604+
}
605+
606+
image_max_sizes = boot_get_max_app_size();
607+
608+
zcbor_map_start_encode(cbor_state, 1);
609+
zcbor_tstr_put_lit_cast(cbor_state, "images");
610+
zcbor_list_start_encode(cbor_state, MCUBOOT_IMAGE_NUMBER);
611+
612+
IMAGES_ITER(image_index) {
613+
for (slot = 0; slot < MCUBOOT_IMAGE_NUMBER; slot++) {
614+
if (slot == 0) {
615+
ok = zcbor_map_start_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_IMAGE_MAP) &&
616+
zcbor_tstr_put_lit(cbor_state, "image") &&
617+
zcbor_uint32_put(cbor_state, (uint32_t)image_index) &&
618+
zcbor_tstr_put_lit(cbor_state, "slots") &&
619+
zcbor_list_start_encode(cbor_state, MCUBOOT_IMAGE_NUMBER);
620+
621+
if (!ok) {
622+
goto finish;
623+
}
624+
}
625+
626+
ok = zcbor_map_start_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_SLOTS_MAP) &&
627+
zcbor_tstr_put_lit(cbor_state, "slot") &&
628+
zcbor_uint32_put(cbor_state, slot);
629+
630+
if (!ok) {
631+
goto finish;
632+
}
633+
634+
area_id = flash_area_id_from_multi_image_slot(image_index, slot);
635+
rc = flash_area_open(area_id, &fap);
636+
637+
if (rc) {
638+
ok = zcbor_tstr_put_lit(cbor_state, "rc") &&
639+
zcbor_int32_put(cbor_state, rc);
640+
} else {
641+
if (sizeof(fap->fa_size) == sizeof(uint64_t)) {
642+
ok = zcbor_tstr_put_lit(cbor_state, "size") &&
643+
zcbor_uint64_put(cbor_state, fap->fa_size);
644+
} else {
645+
ok = zcbor_tstr_put_lit(cbor_state, "size") &&
646+
zcbor_uint32_put(cbor_state, fap->fa_size);
647+
}
648+
649+
if (!ok) {
650+
flash_area_close(fap);
651+
goto finish;
652+
}
653+
654+
/*
655+
* Check if we support uploading to this slot and if so, return the
656+
* image ID
657+
*/
658+
#if defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
659+
ok = zcbor_tstr_put_lit(cbor_state, "upload_image_id") &&
660+
zcbor_uint32_put(cbor_state, (image_index + 1));
661+
#elif defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
662+
ok = zcbor_tstr_put_lit(cbor_state, "upload_image_id") &&
663+
zcbor_uint32_put(cbor_state, (image_index * 2 + slot + 1));
664+
#else
665+
if (slot == 1) {
666+
ok = zcbor_tstr_put_lit(cbor_state, "upload_image_id") &&
667+
zcbor_uint32_put(cbor_state, (image_index * 2 + 1));
668+
}
669+
#endif
670+
671+
flash_area_close(fap);
672+
673+
if (!ok) {
674+
goto finish;
675+
}
676+
677+
ok = zcbor_map_end_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_SLOTS_MAP);
678+
679+
if (!ok) {
680+
goto finish;
681+
}
682+
683+
if (slot == (MCUBOOT_IMAGE_NUMBER - 1)) {
684+
ok = zcbor_list_end_encode(cbor_state, MCUBOOT_IMAGE_NUMBER);
685+
686+
if (!ok) {
687+
goto finish;
688+
}
689+
690+
if (image_max_sizes[image_index].calculated == true) {
691+
ok = zcbor_tstr_put_lit(cbor_state, "max_image_size") &&
692+
zcbor_uint32_put(cbor_state,
693+
image_max_sizes[image_index].max_size);
694+
695+
if (!ok) {
696+
goto finish;
697+
}
698+
}
699+
700+
ok = zcbor_map_end_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_IMAGE_MAP);
701+
702+
}
703+
}
704+
705+
if (!ok) {
706+
goto finish;
707+
}
708+
}
709+
}
710+
711+
ok = zcbor_list_end_encode(cbor_state, MCUBOOT_IMAGE_NUMBER) &&
712+
zcbor_map_end_encode(cbor_state, 1);
713+
714+
finish:
715+
if (!ok) {
716+
reset_cbor_state();
717+
bs_rc_rsp(MGMT_ERR_ENOMEM);
718+
}
719+
720+
boot_serial_output();
721+
}
722+
#endif
723+
579724
#ifdef MCUBOOT_ERASE_PROGRESSIVELY
580725
/** Erases range of flash, aligned to sector size
581726
*
@@ -1019,6 +1164,11 @@ boot_serial_input(char *buf, int len)
10191164
case IMGMGR_NMGR_ID_UPLOAD:
10201165
bs_upload(buf, len);
10211166
break;
1167+
#ifdef MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO
1168+
case IMGMGR_NMGR_ID_SLOT_INFO:
1169+
bs_slot_info(hdr->nh_op, buf, len);
1170+
break;
1171+
#endif
10221172
default:
10231173
bs_rc_rsp(MGMT_ERR_ENOTSUP);
10241174
break;

boot/boot_serial/src/boot_serial_priv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ struct nmgr_hdr {
8181
*/
8282
#define IMGMGR_NMGR_ID_STATE 0
8383
#define IMGMGR_NMGR_ID_UPLOAD 1
84+
#define IMGMGR_NMGR_ID_SLOT_INFO 6
8485

8586
void boot_serial_input(char *buf, int len);
8687
extern const struct boot_uart_funcs *boot_uf;

0 commit comments

Comments
 (0)