Skip to content

Commit 911bb8c

Browse files
committed
[nrf noup] bootutil: Add support for bootloader requests
Extend the common bootutil library to send bootloader requests instead of writing data directly into the active slot. Ref: NCSDK-34439 Signed-off-by: Tomasz Chyrowicz <[email protected]>
1 parent f01a59f commit 911bb8c

File tree

1 file changed

+89
-5
lines changed

1 file changed

+89
-5
lines changed

boot/bootutil/src/bootutil_public.c

Lines changed: 89 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(CONFIG_MCUBOOT_BOOT_REQUEST) && !defined(CONFIG_MCUBOOT)
55+
#include <bootutil/boot_request.h>
56+
#define SEND_BOOT_REQUEST
57+
#endif /* CONFIG_MCUBOOT_BOOT_REQUEST && !CONFIG_MCUBOOT */
58+
5459
#ifdef CONFIG_MCUBOOT
5560
BOOT_LOG_MODULE_DECLARE(mcuboot);
5661
#else
@@ -503,32 +508,89 @@ 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+
static int
513+
send_boot_request(uint8_t magic, 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+
return rc;
542+
}
543+
#endif /* SEND_BOOT_REQUEST */
507544

508-
static int flash_area_to_image(const struct flash_area *fa)
545+
#if defined(SEND_BOOT_REQUEST) || (!defined(MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP))
546+
static int flash_area_to_image_slot(const struct flash_area *fa, uint32_t *slot)
509547
{
548+
int id = flash_area_get_id(fa);
510549
#if BOOT_IMAGE_NUMBER > 1
511550
uint8_t i = 0;
512-
int id = flash_area_get_id(fa);
513551

514552
while (i < BOOT_IMAGE_NUMBER) {
515-
if (FLASH_AREA_IMAGE_PRIMARY(i) == id || (FLASH_AREA_IMAGE_SECONDARY(i) == id)) {
553+
if (FLASH_AREA_IMAGE_PRIMARY(i) == id) {
554+
if (slot != NULL) {
555+
*slot = 0;
556+
}
557+
return i;
558+
} else if (FLASH_AREA_IMAGE_SECONDARY(i) == id) {
559+
if (slot != NULL) {
560+
*slot = 1;
561+
}
516562
return i;
517563
}
518564

519565
++i;
520566
}
521567
#else
522568
(void)fa;
569+
if (slot != NULL) {
570+
if (FLASH_AREA_IMAGE_PRIMARY(0) == id) {
571+
*slot = 0;
572+
} else if (FLASH_AREA_IMAGE_SECONDARY(0) == id) {
573+
*slot = 1;
574+
} else {
575+
*slot = UINT32_MAX;
576+
}
577+
}
523578
#endif
524579
return 0;
525580
}
581+
#endif /* defined(SEND_BOOT_REQUEST) || (!defined(MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP)) */
526582

583+
#ifndef MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP
527584
int
528585
boot_set_next(const struct flash_area *fa, bool active, bool confirm)
529586
{
530587
struct boot_swap_state slot_state;
531588
int rc;
589+
int image_id;
590+
uint32_t slot_id;
591+
592+
BOOT_LOG_DBG("boot_set_next: fa %p active == %d, confirm == %d",
593+
fa, (int)active, (int)confirm);
532594

533595
if (active) {
534596
confirm = true;
@@ -539,6 +601,15 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
539601
return rc;
540602
}
541603

604+
image_id = flash_area_to_image_slot(fa, &slot_id);
605+
606+
#ifdef SEND_BOOT_REQUEST
607+
rc = send_boot_request(slot_state.magic, confirm, image_id, slot_id);
608+
if ((rc != 0) || active) {
609+
return rc;
610+
}
611+
#endif
612+
542613
switch (slot_state.magic) {
543614
case BOOT_MAGIC_GOOD:
544615
/* If non-active then swap already scheduled, else confirm needed.*/
@@ -569,7 +640,7 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
569640
} else {
570641
swap_type = BOOT_SWAP_TYPE_TEST;
571642
}
572-
rc = boot_write_swap_info(fa, swap_type, flash_area_to_image(fa));
643+
rc = boot_write_swap_info(fa, swap_type, image_id);
573644
}
574645
}
575646
break;
@@ -600,6 +671,10 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
600671
{
601672
struct boot_swap_state slot_state;
602673
int rc;
674+
#ifdef SEND_BOOT_REQUEST
675+
int image_id;
676+
uint32_t slot_id;
677+
#endif
603678

604679
BOOT_LOG_DBG("boot_set_next: fa %p active == %d, confirm == %d",
605680
fa, (int)active, (int)confirm);
@@ -618,6 +693,15 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
618693
return rc;
619694
}
620695

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

0 commit comments

Comments
 (0)