Skip to content

Commit c58b35c

Browse files
committed
bootutil: Add support for bootloader requests
Extend the common bootutil library to send bootloader requests instead of writing data directly into the active slot. Signed-off-by: Tomasz Chyrowicz <tomasz.chyrowicz@nordicsemi.no>
1 parent 54a176c commit c58b35c

File tree

1 file changed

+91
-5
lines changed

1 file changed

+91
-5
lines changed

boot/bootutil/src/bootutil_public.c

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
#include "bootutil_priv.h"
5252
#include "bootutil_misc.h"
5353

54+
#if defined(MCUBOOT_BOOT_REQUEST) && !defined(CONFIG_MCUBOOT)
55+
#include <bootutil/boot_request.h>
56+
#define SEND_BOOT_REQUEST
57+
#endif /* MCUBOOT_BOOT_REQUEST && !CONFIG_MCUBOOT */
58+
5459
#ifdef CONFIG_MCUBOOT
5560
BOOT_LOG_MODULE_DECLARE(mcuboot);
5661
#else
@@ -503,32 +508,93 @@ boot_write_copy_done(const struct flash_area *fap)
503508
return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
504509
}
505510

506-
#ifndef MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP
511+
#ifdef SEND_BOOT_REQUEST
512+
bool
513+
boot_send_requests(uint8_t magic, bool active, bool confirm, int image_id, uint32_t slot_id)
514+
{
515+
int rc;
516+
517+
/* Handle write-protected active image. */
518+
switch (magic) {
519+
case BOOT_MAGIC_GOOD:
520+
/* fall-through */
521+
522+
case BOOT_MAGIC_UNSET:
523+
if (confirm) {
524+
BOOT_LOG_DBG("Confirm image: %d, %d", image_id, slot_id);
525+
rc = boot_request_confirm_slot(image_id, slot_id);
526+
} else {
527+
BOOT_LOG_DBG("Set image preference: %d, %d", image_id, slot_id);
528+
rc = boot_request_set_preferred_slot(image_id, slot_id);
529+
}
530+
if (rc != 0) {
531+
rc = BOOT_EBADIMAGE;
532+
}
533+
break;
534+
535+
case BOOT_MAGIC_BAD:
536+
default:
537+
rc = BOOT_EBADIMAGE;
538+
break;
539+
}
540+
541+
if (active && (rc ==0)) {
542+
return true;
543+
}
544+
545+
return false;
546+
}
547+
#endif /* SEND_BOOT_REQUEST */
507548

508-
static int flash_area_to_image(const struct flash_area *fa)
549+
#if defined(SEND_BOOT_REQUEST) || (!defined(MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP))
550+
static int flash_area_to_image_slot(const struct flash_area *fa, uint32_t *slot)
509551
{
552+
int id = flash_area_get_id(fa);
510553
#if BOOT_IMAGE_NUMBER > 1
511554
uint8_t i = 0;
512-
int id = flash_area_get_id(fa);
513555

514556
while (i < BOOT_IMAGE_NUMBER) {
515-
if (FLASH_AREA_IMAGE_PRIMARY(i) == id || (FLASH_AREA_IMAGE_SECONDARY(i) == id)) {
557+
if (FLASH_AREA_IMAGE_PRIMARY(i) == id) {
558+
if (slot != NULL) {
559+
*slot = 0;
560+
}
561+
return i;
562+
} else if (FLASH_AREA_IMAGE_SECONDARY(i) == id) {
563+
if (slot != NULL) {
564+
*slot = 1;
565+
}
516566
return i;
517567
}
518568

519569
++i;
520570
}
521571
#else
522572
(void)fa;
573+
if (slot != NULL) {
574+
if (FLASH_AREA_IMAGE_PRIMARY(0) == id) {
575+
*slot = 0;
576+
} else if (FLASH_AREA_IMAGE_SECONDARY(0) == id) {
577+
*slot = 1;
578+
} else {
579+
*slot = UINT32_MAX;
580+
}
581+
}
523582
#endif
524583
return 0;
525584
}
585+
#endif /* defined(SEND_BOOT_REQUEST) || (!defined(MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP)) */
526586

587+
#ifndef MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP
527588
int
528589
boot_set_next(const struct flash_area *fa, bool active, bool confirm)
529590
{
530591
struct boot_swap_state slot_state;
531592
int rc;
593+
int image_id;
594+
uint32_t slot_id;
595+
596+
BOOT_LOG_DBG("boot_set_next: fa %p active == %d, confirm == %d",
597+
fa, (int)active, (int)confirm);
532598

533599
if (active) {
534600
confirm = true;
@@ -539,6 +605,14 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
539605
return rc;
540606
}
541607

608+
image_id = flash_area_to_image_slot(fa, &slot_id);
609+
610+
#ifdef SEND_BOOT_REQUEST
611+
if (boot_send_requests(slot_state.magic, active, confirm, image_id, slot_id)) {
612+
return rc;
613+
}
614+
#endif
615+
542616
switch (slot_state.magic) {
543617
case BOOT_MAGIC_GOOD:
544618
/* If non-active then swap already scheduled, else confirm needed.*/
@@ -569,7 +643,7 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
569643
} else {
570644
swap_type = BOOT_SWAP_TYPE_TEST;
571645
}
572-
rc = boot_write_swap_info(fa, swap_type, flash_area_to_image(fa));
646+
rc = boot_write_swap_info(fa, swap_type, image_id);
573647
}
574648
}
575649
break;
@@ -597,6 +671,10 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
597671
{
598672
struct boot_swap_state slot_state;
599673
int rc;
674+
#ifdef SEND_BOOT_REQUEST
675+
int image_id;
676+
uint32_t slot_id;
677+
#endif
600678

601679
BOOT_LOG_DBG("boot_set_next: fa %p active == %d, confirm == %d",
602680
fa, (int)active, (int)confirm);
@@ -615,6 +693,14 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
615693
return rc;
616694
}
617695

696+
#ifdef SEND_BOOT_REQUEST
697+
image_id = flash_area_to_image_slot(fa, &slot_id);
698+
699+
if (boot_send_requests(slot_state.magic, active, confirm, image_id, slot_id)) {
700+
return rc;
701+
}
702+
#endif
703+
618704
switch (slot_state.magic) {
619705
case BOOT_MAGIC_UNSET:
620706
/* Magic is needed for MCUboot to even consider booting an image */

0 commit comments

Comments
 (0)