@@ -56,13 +56,15 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
56
56
/* Currently only used by imgmgr */
57
57
int boot_current_slot ;
58
58
59
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
59
60
#if (!defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )) || \
60
61
defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
61
62
/* Used for holding static buffers in multiple functions to work around issues
62
63
* in older versions of gcc (e.g. 4.8.4)
63
64
*/
64
65
static struct boot_sector_buffer sector_buffers ;
65
66
#endif
67
+ #endif /* !defined(MCUBOOT_LOGICAL_SECTOR_SIZE) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0 */
66
68
67
69
/**
68
70
* @brief Determine if the data at two memory addresses is equal
@@ -625,6 +627,7 @@ boot_erase_region(const struct flash_area *fa, uint32_t off, uint32_t size, bool
625
627
626
628
#if (!defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )) || \
627
629
defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
630
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
628
631
int
629
632
boot_initialize_area (struct boot_loader_state * state , int flash_area )
630
633
{
@@ -665,6 +668,137 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area)
665
668
return 0 ;
666
669
}
667
670
671
+ #else /* defined(MCUBOOT_LOGICAL_SECTOR_SIZE) && MCUBOOT_LOGICAL_SECTOR_SIZE != 0 */
672
+ #if defined(MCUBOOT_VERIFY_LOGICAL_SECTORS )
673
+ /* Validation can only run once all flash areas are open and pointers to
674
+ * flash area objects are stored in state.
675
+ */
676
+ static int
677
+ boot_verify_logical_sectors (const struct boot_loader_state * state , int faid , const struct flash_area * fa )
678
+ {
679
+ uint32_t num_sectors = BOOT_MAX_IMG_SECTORS ;
680
+ size_t slot_size ;
681
+ size_t slot_off ;
682
+ size_t sect_off = 0 ;
683
+ int rc ;
684
+ int final_rc = 0 ;
685
+ const struct flash_parameters * fparams ;
686
+ bool device_with_erase ;
687
+ uint32_t wbs ;
688
+
689
+ assert (fa != NULL );
690
+ assert (faid != 0 );
691
+
692
+ slot_off = flash_area_get_off (fa );
693
+ slot_size = flash_area_get_size (fa );
694
+
695
+ fparams = flash_get_parameters (flash_area_get_device (fa ));
696
+ wbs = fparams -> write_block_size ;
697
+
698
+ device_with_erase =
699
+ (flash_params_get_erase_cap (fparams ) & FLASH_ERASE_C_EXPLICIT ) & FLASH_ERASE_C_EXPLICIT ;
700
+ /* Go till all verifications are complete or we face issue that does not allow
701
+ * to proceede with further tests.
702
+ */
703
+ BOOT_LOG_INF ("boot_verify_logical_sectors: verify flash area %p" , fa );
704
+ BOOT_LOG_INF ("boot_verify_logical_sectors: MCUBOOT_LOGICAL_SECTOR_SIZE == 0x%x" ,
705
+ MCUBOOT_LOGICAL_SECTOR_SIZE );
706
+ BOOT_LOG_INF ("boot_verify_logical_sectors: slot offset == 0x%x" , slot_off );
707
+ if (slot_size != 0 ) {
708
+ BOOT_LOG_INF ("boot_verify_logical_sectors: slot size == 0x%x" , slot_size );
709
+ } else {
710
+ BOOT_LOG_ERR ("boot_verify_logical_sectors: 0 size slot" );
711
+ return BOOT_EFLASH ;
712
+ }
713
+ BOOT_LOG_INF ("boot_verify_logical_sectors: write block size %u" , wbs );
714
+ BOOT_LOG_INF ("boot_verify_logical_sectors: device with%s erase" ,
715
+ device_with_erase ? "" : "out" );
716
+
717
+ if (slot_size % MCUBOOT_LOGICAL_SECTOR_SIZE ) {
718
+ BOOT_LOG_ERR ("boot_verify_logical_sectors: area size not aligned" );
719
+ final_rc = BOOT_EFLASH ;
720
+ }
721
+
722
+ BOOT_LOG_INF ("boot_verify_logical_sectors: max %d logical sectors" ,
723
+ slot_size / MCUBOOT_LOGICAL_SECTOR_SIZE );
724
+
725
+ if (device_with_erase ) {
726
+ /* Devices with explicit erase require alignment to page */
727
+ if (slot_off % MCUBOOT_LOGICAL_SECTOR_SIZE ) {
728
+ BOOT_LOG_ERR ("boot_verify_logical_sectors: area offset not aligned" );
729
+ final_rc = BOOT_EFLASH ;
730
+ }
731
+
732
+ /* Check all hardware specific pages against erase pages of a device */
733
+ for (size_t i = 0 ; i < num_sectors ; i ++ ) {
734
+ struct flash_sector fas ;
735
+
736
+ MCUBOOT_WATCHDOG_FEED ();
737
+
738
+ BOOT_LOG_INF ("boot_verify_logical_sectors: page 0x%x:0x%x " , slot_off , sect_off );
739
+ rc = flash_area_get_sector (fa , sect_off , & fas );
740
+ if (rc < 0 ) {
741
+ BOOT_LOG_ERR ("boot_verify_logical_sectors: query err %d" , rc );
742
+ final_rc = BOOT_EFLASH ;
743
+ continue ;
744
+ }
745
+
746
+
747
+ if (flash_sector_get_off (& fas ) % MCUBOOT_LOGICAL_SECTOR_SIZE ) {
748
+ BOOT_LOG_ERR ("boot_verify_logical_sectors: misaligned offset" );
749
+ final_rc = BOOT_EFLASH ;
750
+ }
751
+
752
+ sect_off += flash_sector_get_size (& fas );
753
+ }
754
+ } else {
755
+ /* Devices with no-explicit erase require alignment to write block size */
756
+
757
+ if (MCUBOOT_LOGICAL_SECTOR_SIZE % wbs ) {
758
+ BOOT_LOG_ERR ("boot_verify_logical_sectors: sector size not aligned to write block" );
759
+ final_rc = BOOT_EFLASH ;
760
+ }
761
+
762
+ if (slot_off % wbs ) {
763
+ BOOT_LOG_ERR ("boot_verify_logical_sectors: slot not aligned to write block" );
764
+ final_rc = BOOT_EFLASH ;
765
+ }
766
+ }
767
+
768
+ BOOT_LOG_INF ("boot_verify_logical_sectors: completed (%d)" , final_rc );
769
+
770
+ return final_rc ;
771
+ }
772
+ #endif /* MCUBOOT_LOGICAL_SECTOR_VALIDATION */
773
+
774
+ static int
775
+ boot_initialize_area (struct boot_loader_state * state , int flash_area )
776
+ {
777
+ size_t area_size ;
778
+ uint32_t * out_num_sectors ;
779
+
780
+ if (flash_area == FLASH_AREA_IMAGE_PRIMARY (BOOT_CURR_IMG (state ))) {
781
+ area_size = flash_area_get_size (BOOT_IMG_AREA (state , BOOT_PRIMARY_SLOT ));
782
+ out_num_sectors = & BOOT_IMG (state , BOOT_PRIMARY_SLOT ).num_sectors ;
783
+ } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY (BOOT_CURR_IMG (state ))) {
784
+ area_size = flash_area_get_size (BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT ));
785
+ out_num_sectors = & BOOT_IMG (state , BOOT_SECONDARY_SLOT ).num_sectors ;
786
+ #if MCUBOOT_SWAP_USING_SCRATCH
787
+ } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH ) {
788
+ area_size = flash_area_get_size (state -> scratch .area );
789
+ out_num_sectors = & state -> scratch .num_sectors ;
790
+ #endif
791
+ } else {
792
+ return BOOT_EFLASH ;
793
+ }
794
+
795
+ * out_num_sectors = area_size / MCUBOOT_LOGICAL_SECTOR_SIZE ;
796
+
797
+ return 0 ;
798
+ }
799
+
800
+ #endif /* defined(MCUBOOT_LOGICAL_SECTOR_SIZE) && MCUBOOT_LOGICAL_SECTOR_SIZE != 0 */
801
+
668
802
static uint32_t
669
803
boot_write_sz (struct boot_loader_state * state )
670
804
{
@@ -694,12 +828,13 @@ boot_read_sectors(struct boot_loader_state *state, struct boot_sector_buffer *se
694
828
uint8_t image_index ;
695
829
int rc ;
696
830
831
+ image_index = BOOT_CURR_IMG (state );
832
+
833
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
697
834
if (sectors == NULL ) {
698
835
sectors = & sector_buffers ;
699
836
}
700
837
701
- image_index = BOOT_CURR_IMG (state );
702
-
703
838
BOOT_IMG (state , BOOT_PRIMARY_SLOT ).sectors =
704
839
sectors -> primary [image_index ];
705
840
#if BOOT_NUM_SLOTS > 1
@@ -709,6 +844,9 @@ boot_read_sectors(struct boot_loader_state *state, struct boot_sector_buffer *se
709
844
state -> scratch .sectors = sectors -> scratch ;
710
845
#endif
711
846
#endif
847
+ #else
848
+ (void )sectors ;
849
+ #endif /* !defined(MCUBOOT_LOGICAL_SECTOR_SIZE) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0 */
712
850
713
851
rc = boot_initialize_area (state , FLASH_AREA_IMAGE_PRIMARY (image_index ));
714
852
if (rc != 0 ) {
@@ -732,6 +870,29 @@ boot_read_sectors(struct boot_loader_state *state, struct boot_sector_buffer *se
732
870
733
871
BOOT_WRITE_SZ (state ) = boot_write_sz (state );
734
872
873
+ #if defined(MCUBOOT_VERIFY_LOGICAL_SECTORS )
874
+ BOOT_LOG_INF ("boot_read_sectors: verify image %d slots" , image_index );
875
+ BOOT_LOG_INF ("boot_read_sectors: BOOT_PRIMARY_SLOT" );
876
+ if (boot_verify_logical_sectors (state , FLASH_AREA_IMAGE_PRIMARY (image_index ),
877
+ BOOT_IMG_AREA (state , BOOT_PRIMARY_SLOT )) != 0 ) {
878
+ rc = BOOT_EFLASH ;
879
+ }
880
+
881
+ BOOT_LOG_INF ("boot_read_sectors: BOOT_SECONDARY_SLOT" );
882
+ if (boot_verify_logical_sectors (state , FLASH_AREA_IMAGE_SECONDARY (image_index ),
883
+ BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT )) != 0 ) {
884
+ rc = BOOT_EFLASH_SEC ;
885
+ }
886
+
887
+ #if MCUBOOT_SWAP_USING_SCRATCH
888
+ BOOT_LOG_INF ("boot_read_sectors: SCRATCH" );
889
+ if (boot_verify_logical_sectors (state , FLASH_AREA_IMAGE_SCRATCH ,
890
+ state -> scratch .area ) != 0 ) {
891
+ rc = BOOT_EFLASH ;
892
+ }
893
+ #endif /* MCUBOOT_SWAP_USING_SCRATCH */
894
+ #endif /* defined(MCUBOOT_LOGICAL_SECTOR_VALIDATION) */
895
+
735
896
return 0 ;
736
897
}
737
898
#endif
0 commit comments