51
51
#include "bootutil_priv.h"
52
52
#include "bootutil_misc.h"
53
53
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
+
54
59
#ifdef CONFIG_MCUBOOT
55
60
BOOT_LOG_MODULE_DECLARE (mcuboot );
56
61
#else
@@ -503,32 +508,79 @@ boot_write_copy_done(const struct flash_area *fap)
503
508
return boot_write_trailer_flag (fap , off , BOOT_FLAG_SET );
504
509
}
505
510
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 = BOOT_EBADIMAGE ;
507
516
508
- static int flash_area_to_image (const struct flash_area * fa )
517
+ /* Handle write-protected active image. */
518
+ if ((magic == BOOT_MAGIC_GOOD ) || (magic == BOOT_MAGIC_UNSET )) {
519
+ if (confirm ) {
520
+ BOOT_LOG_DBG ("Confirm image: %d, %d" , image_id , slot_id );
521
+ rc = boot_request_confirm_slot (image_id , slot_id );
522
+ } else {
523
+ BOOT_LOG_DBG ("Set image preference: %d, %d" , image_id , slot_id );
524
+ rc = boot_request_set_preferred_slot (image_id , slot_id );
525
+ }
526
+ if (rc != 0 ) {
527
+ rc = BOOT_EBADIMAGE ;
528
+ }
529
+ }
530
+
531
+ return rc ;
532
+ }
533
+ #endif /* SEND_BOOT_REQUEST */
534
+
535
+ #if defined(SEND_BOOT_REQUEST ) || (!defined(MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP ))
536
+ static int flash_area_to_image_slot (const struct flash_area * fa , uint32_t * slot )
509
537
{
538
+ int id = flash_area_get_id (fa );
510
539
#if BOOT_IMAGE_NUMBER > 1
511
540
uint8_t i = 0 ;
512
- int id = flash_area_get_id (fa );
513
541
514
542
while (i < BOOT_IMAGE_NUMBER ) {
515
- if (FLASH_AREA_IMAGE_PRIMARY (i ) == id || (FLASH_AREA_IMAGE_SECONDARY (i ) == id )) {
543
+ if (FLASH_AREA_IMAGE_PRIMARY (i ) == id ) {
544
+ if (slot != NULL ) {
545
+ * slot = 0 ;
546
+ }
547
+ return i ;
548
+ } else if (FLASH_AREA_IMAGE_SECONDARY (i ) == id ) {
549
+ if (slot != NULL ) {
550
+ * slot = 1 ;
551
+ }
516
552
return i ;
517
553
}
518
554
519
555
++ i ;
520
556
}
521
557
#else
522
558
(void )fa ;
559
+ if (slot != NULL ) {
560
+ if (FLASH_AREA_IMAGE_PRIMARY (0 ) == id ) {
561
+ * slot = 0 ;
562
+ } else if (FLASH_AREA_IMAGE_SECONDARY (0 ) == id ) {
563
+ * slot = 1 ;
564
+ } else {
565
+ * slot = UINT32_MAX ;
566
+ }
567
+ }
523
568
#endif
524
569
return 0 ;
525
570
}
571
+ #endif /* defined(SEND_BOOT_REQUEST) || (!defined(MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP)) */
526
572
573
+ #ifndef MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP
527
574
int
528
575
boot_set_next (const struct flash_area * fa , bool active , bool confirm )
529
576
{
530
577
struct boot_swap_state slot_state ;
531
578
int rc ;
579
+ int image_id ;
580
+ uint32_t slot_id ;
581
+
582
+ BOOT_LOG_DBG ("boot_set_next: fa %p active == %d, confirm == %d" ,
583
+ fa , (int )active , (int )confirm );
532
584
533
585
if (active ) {
534
586
confirm = true;
@@ -539,6 +591,15 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
539
591
return rc ;
540
592
}
541
593
594
+ image_id = flash_area_to_image_slot (fa , & slot_id );
595
+
596
+ #ifdef SEND_BOOT_REQUEST
597
+ rc = send_boot_request (slot_state .magic , confirm , image_id , slot_id );
598
+ if ((rc != 0 ) || active ) {
599
+ return rc ;
600
+ }
601
+ #endif
602
+
542
603
switch (slot_state .magic ) {
543
604
case BOOT_MAGIC_GOOD :
544
605
/* If non-active then swap already scheduled, else confirm needed.*/
@@ -569,7 +630,7 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
569
630
} else {
570
631
swap_type = BOOT_SWAP_TYPE_TEST ;
571
632
}
572
- rc = boot_write_swap_info (fa , swap_type , flash_area_to_image ( fa ) );
633
+ rc = boot_write_swap_info (fa , swap_type , image_id );
573
634
}
574
635
}
575
636
break ;
@@ -600,6 +661,10 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
600
661
{
601
662
struct boot_swap_state slot_state ;
602
663
int rc ;
664
+ #ifdef SEND_BOOT_REQUEST
665
+ int image_id ;
666
+ uint32_t slot_id ;
667
+ #endif
603
668
604
669
BOOT_LOG_DBG ("boot_set_next: fa %p active == %d, confirm == %d" ,
605
670
fa , (int )active , (int )confirm );
@@ -618,6 +683,15 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
618
683
return rc ;
619
684
}
620
685
686
+ #ifdef SEND_BOOT_REQUEST
687
+ image_id = flash_area_to_image_slot (fa , & slot_id );
688
+
689
+ rc = send_boot_request (slot_state .magic , confirm , image_id , slot_id );
690
+ if ((rc != 0 ) || active ) {
691
+ return rc ;
692
+ }
693
+ #endif
694
+
621
695
switch (slot_state .magic ) {
622
696
case BOOT_MAGIC_UNSET :
623
697
/* Magic is needed for MCUboot to even consider booting an image */
0 commit comments