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
5560BOOT_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
527588int
528589boot_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 ;
@@ -600,6 +674,10 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
600674{
601675 struct boot_swap_state slot_state ;
602676 int rc ;
677+ #ifdef SEND_BOOT_REQUEST
678+ int image_id ;
679+ uint32_t slot_id ;
680+ #endif
603681
604682 BOOT_LOG_DBG ("boot_set_next: fa %p active == %d, confirm == %d" ,
605683 fa , (int )active , (int )confirm );
@@ -618,6 +696,14 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
618696 return rc ;
619697 }
620698
699+ #ifdef SEND_BOOT_REQUEST
700+ image_id = flash_area_to_image_slot (fa , & slot_id );
701+
702+ if (boot_send_requests (slot_state .magic , active , confirm , image_id , slot_id )) {
703+ return rc ;
704+ }
705+ #endif
706+
621707 switch (slot_state .magic ) {
622708 case BOOT_MAGIC_UNSET :
623709 /* Magic is needed for MCUboot to even consider booting an image */
0 commit comments