Skip to content

Commit f1aa499

Browse files
taltenbachnordicjm
authored andcommitted
boot_serial: Initialize a bootloader state for bs_list and bs_set
Until d00b11d, it was possible to call bootutil_img_validate with a NULL bootloader state, provided the image is not encrypted. This was used in boot_serial for bs_list and bs_set. However, for swap strategies, a valid bootloader state is now needed by bootutil_max_image_size, invoked from bootutil_img_validate. Therefore, that change caused a NULL pointer access when calling bs_list or bs_set. To fix that issue, a valid bootloader state is now initialized and given to bs_list and bs_set each time bs_list_set is called. This state needs to be initialized with flash areas and sectors, which are used in bootutil_max_image_size. To avoid superfluous memory allocations, the global bootloader state defined in loader.c is used. This is assuming boot_serial_start and context_boot_go cannot run concurrently. Signed-off-by: Thomas Altenbach <[email protected]>
1 parent f1efd48 commit f1aa499

File tree

1 file changed

+55
-15
lines changed

1 file changed

+55
-15
lines changed

boot/boot_serial/src/boot_serial.c

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
282282
* List images.
283283
*/
284284
static void
285-
bs_list(char *buf, int len)
285+
bs_list(struct boot_loader_state *state, char *buf, int len)
286286
{
287287
struct image_header hdr;
288288
uint32_t slot, area_id;
@@ -295,11 +295,13 @@ bs_list(char *buf, int len)
295295
zcbor_map_start_encode(cbor_state, 1);
296296
zcbor_tstr_put_lit_cast(cbor_state, "images");
297297
zcbor_list_start_encode(cbor_state, 5);
298-
image_index = 0;
299-
IMAGES_ITER(image_index) {
298+
299+
IMAGES_ITER(BOOT_CURR_IMG(state)) {
300300
#if defined(MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE) || defined(MCUBOOT_SWAP_USING_OFFSET)
301-
int swap_status = boot_swap_type_multi(image_index);
301+
int swap_status = boot_swap_type_multi(BOOT_CURR_IMG(state));
302302
#endif
303+
image_index = BOOT_CURR_IMG(state);
304+
(void) image_index; /* Might be unused depending on the configuration */
303305

304306
for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
305307
FIH_DECLARE(fih_rc, FIH_FAILURE);
@@ -380,10 +382,10 @@ bs_list(char *buf, int len)
380382
#endif
381383

382384
#ifdef MCUBOOT_SWAP_USING_OFFSET
383-
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
385+
FIH_CALL(bootutil_img_validate, fih_rc, state, &hdr,
384386
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL, start_off);
385387
#else
386-
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
388+
FIH_CALL(bootutil_img_validate, fih_rc, state, &hdr,
387389
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
388390
#endif
389391
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
@@ -495,7 +497,7 @@ bs_list(char *buf, int len)
495497
* Set image state.
496498
*/
497499
static void
498-
bs_set(char *buf, int len)
500+
bs_set(struct boot_loader_state *state, char *buf, int len)
499501
{
500502
/*
501503
* Expected data format.
@@ -544,10 +546,12 @@ bs_set(char *buf, int len)
544546
}
545547

546548
if (img_hash.len != 0) {
547-
IMAGES_ITER(image_index) {
549+
IMAGES_ITER(BOOT_CURR_IMG(state)) {
548550
#ifdef MCUBOOT_SWAP_USING_OFFSET
549-
int swap_status = boot_swap_type_multi(image_index);
551+
int swap_status = boot_swap_type_multi(BOOT_CURR_IMG(state));
550552
#endif
553+
image_index = BOOT_CURR_IMG(state);
554+
(void) image_index; /* Might be unused depending on the configuration */
551555

552556
for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
553557
struct image_header hdr;
@@ -613,10 +617,10 @@ bs_set(char *buf, int len)
613617
} else {
614618
#endif
615619
#ifdef MCUBOOT_SWAP_USING_OFFSET
616-
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
620+
FIH_CALL(bootutil_img_validate, fih_rc, state, &hdr,
617621
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL, start_off);
618622
#else
619-
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
623+
FIH_CALL(bootutil_img_validate, fih_rc, state, &hdr,
620624
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
621625
#endif
622626
#ifdef MCUBOOT_ENC_IMAGES
@@ -662,7 +666,7 @@ bs_set(char *buf, int len)
662666
out:
663667
if (rc == 0) {
664668
/* Success - return updated list of images */
665-
bs_list(buf, len);
669+
bs_list(state, buf, len);
666670
} else {
667671
/* Error code, only return the error */
668672
zcbor_map_start_encode(cbor_state, 10);
@@ -691,16 +695,52 @@ bs_rc_rsp(int rc_code)
691695
static void
692696
bs_list_set(uint8_t op, char *buf, int len)
693697
{
698+
int rc;
699+
struct boot_loader_state *state;
700+
bool area_opened = false;
701+
702+
state = boot_get_loader_state();
703+
boot_state_clear(state);
704+
705+
rc = boot_open_all_flash_areas(state);
706+
if (rc != 0) {
707+
BOOT_LOG_ERR("Failed to open flash areas: %d", rc);
708+
rc = MGMT_ERR_EUNKNOWN;
709+
goto out;
710+
}
711+
712+
area_opened = true;
713+
714+
#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
715+
IMAGES_ITER(BOOT_CURR_IMG(state)) {
716+
rc = boot_read_sectors(state, NULL);
717+
if (rc != 0) {
718+
BOOT_LOG_ERR("Failed to read sectors: %d", rc);
719+
rc = MGMT_ERR_EUNKNOWN;
720+
goto out;
721+
}
722+
}
723+
#endif
724+
694725
if (op == NMGR_OP_READ) {
695-
bs_list(buf, len);
726+
bs_list(state, buf, len);
696727
} else {
697728
#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
698-
bs_set(buf, len);
729+
bs_set(state, buf, len);
699730
#else
700-
bs_rc_rsp(MGMT_ERR_ENOTSUP);
731+
rc = MGMT_ERR_ENOTSUP;
701732
#endif
702733
}
703734

735+
out:
736+
if (area_opened) {
737+
boot_close_all_flash_areas(state);
738+
}
739+
740+
if (rc != 0) {
741+
bs_rc_rsp(rc);
742+
}
743+
704744
reset_cbor_state();
705745
}
706746

0 commit comments

Comments
 (0)