51
51
#include "bootutil_priv.h"
52
52
#include "bootutil_misc.h"
53
53
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
+
54
59
#ifdef CONFIG_MCUBOOT
55
60
BOOT_LOG_MODULE_DECLARE (mcuboot );
56
61
#else
@@ -503,32 +508,93 @@ 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
+ 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 */
507
548
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 )
509
551
{
552
+ int id = flash_area_get_id (fa );
510
553
#if BOOT_IMAGE_NUMBER > 1
511
554
uint8_t i = 0 ;
512
- int id = flash_area_get_id (fa );
513
555
514
556
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
+ }
516
566
return i ;
517
567
}
518
568
519
569
++ i ;
520
570
}
521
571
#else
522
572
(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
+ }
523
582
#endif
524
583
return 0 ;
525
584
}
585
+ #endif /* defined(SEND_BOOT_REQUEST) || (!defined(MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP)) */
526
586
587
+ #ifndef MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP
527
588
int
528
589
boot_set_next (const struct flash_area * fa , bool active , bool confirm )
529
590
{
530
591
struct boot_swap_state slot_state ;
531
592
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 );
532
598
533
599
if (active ) {
534
600
confirm = true;
@@ -539,6 +605,14 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
539
605
return rc ;
540
606
}
541
607
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
+
542
616
switch (slot_state .magic ) {
543
617
case BOOT_MAGIC_GOOD :
544
618
/* 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)
569
643
} else {
570
644
swap_type = BOOT_SWAP_TYPE_TEST ;
571
645
}
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 );
573
647
}
574
648
}
575
649
break ;
@@ -597,6 +671,10 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
597
671
{
598
672
struct boot_swap_state slot_state ;
599
673
int rc ;
674
+ #ifdef SEND_BOOT_REQUEST
675
+ int image_id ;
676
+ uint32_t slot_id ;
677
+ #endif
600
678
601
679
BOOT_LOG_DBG ("boot_set_next: fa %p active == %d, confirm == %d" ,
602
680
fa , (int )active , (int )confirm );
@@ -615,6 +693,14 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
615
693
return rc ;
616
694
}
617
695
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
+
618
704
switch (slot_state .magic ) {
619
705
case BOOT_MAGIC_UNSET :
620
706
/* Magic is needed for MCUboot to even consider booting an image */
0 commit comments