@@ -56,13 +56,15 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
5656/* Currently only used by imgmgr */
5757int boot_current_slot ;
5858
59+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
5960#if (!defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )) || \
6061defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
6162/* Used for holding static buffers in multiple functions to work around issues
6263 * in older versions of gcc (e.g. 4.8.4)
6364 */
6465static struct boot_sector_buffer sector_buffers ;
6566#endif
67+ #endif /* !defined(MCUBOOT_LOGICAL_SECTOR_SIZE) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0 */
6668
6769/**
6870 * @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
625627
626628#if (!defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )) || \
627629defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
630+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
628631int
629632boot_initialize_area (struct boot_loader_state * state , int flash_area )
630633{
@@ -665,6 +668,112 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area)
665668 return 0 ;
666669}
667670
671+ #else /* defined(MCUBOOT_LOGICAL_SECTOR_SIZE) && MCUBOOT_LOGICAL_SECTOR_SIZE != 0 */
672+ #if defined(MCUBOOT_LOGICAL_SECTOR_VALIDATION )
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_validate_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+
686+ assert (fa != NULL );
687+ assert (faid != 0 );
688+
689+ slot_off = flash_area_get_off (fa );
690+ slot_size = flash_area_get_size (fa );
691+
692+
693+ /* Go till all validations are complete or we face issue that does not allow
694+ * to proceede with further tests.
695+ */
696+ BOOT_LOG_INF ("boot_validate_logical_sectors: validating flash area %p" , fa );
697+ BOOT_LOG_INF ("boot_validate_logical_sectors: MCUBOOT_LOGICAL_SECTOR_SIZE == 0x%x" ,
698+ MCUBOOT_LOGICAL_SECTOR_SIZE );
699+ BOOT_LOG_INF ("boot_validate_logical_sectors: slot offset == 0x%x" , slot_off );
700+ if (slot_size != 0 ) {
701+ BOOT_LOG_INF ("boot_validate_logical_sectors: slot size == 0x%x" , slot_size );
702+ } else {
703+ BOOT_LOG_ERR ("boot_validate_logical_sectors: 0 size slot" );
704+ return BOOT_EFLASH ;
705+ }
706+
707+ BOOT_LOG_INF ("boot_validate_logical_sectors: max %d logical sectors" ,
708+ slot_size / MCUBOOT_LOGICAL_SECTOR_SIZE );
709+
710+ if (slot_off % MCUBOOT_LOGICAL_SECTOR_SIZE ) {
711+ BOOT_LOG_ERR ("boot_validate_logical_sectors: area offset not aligned" );
712+ final_rc = BOOT_EFLASH ;
713+ }
714+
715+ if (slot_size % MCUBOOT_LOGICAL_SECTOR_SIZE ) {
716+ BOOT_LOG_ERR ("boot_validate_logical_sectors: area size not aligned" );
717+ final_rc = BOOT_EFLASH ;
718+ }
719+
720+ /* Check all hardware specific pages against erase pages of a device */
721+ for (size_t i = 0 ; i < num_sectors ; i ++ ) {
722+ struct flash_sector fas ;
723+
724+ MCUBOOT_WATCHDOG_FEED ();
725+
726+ BOOT_LOG_INF ("boot_validate_logical_sectors: page 0x%x:0x%x " , slot_off , sect_off );
727+ rc = flash_area_get_sector (fa , sect_off , & fas );
728+ if (rc < 0 ) {
729+ BOOT_LOG_ERR ("boot_validate_logical_sectors: query err %d" , rc );
730+ final_rc = BOOT_EFLASH ;
731+ continue ;
732+ }
733+
734+
735+ if (flash_sector_get_off (& fas ) % MCUBOOT_LOGICAL_SECTOR_SIZE ) {
736+ BOOT_LOG_ERR ("boot_validate_logical_sectors: misaligned offset" );
737+ final_rc = BOOT_EFLASH ;
738+ }
739+
740+ sect_off += flash_sector_get_size (& fas );
741+ }
742+
743+ BOOT_LOG_INF ("boot_validate_logical_sectors: done %d" , final_rc );
744+
745+ return final_rc ;
746+ }
747+ #endif /* MCUBOOT_LOGICAL_SECTOR_VALIDATION */
748+
749+ static int
750+ boot_initialize_area (struct boot_loader_state * state , int flash_area )
751+ {
752+ size_t area_size ;
753+ uint32_t * out_num_sectors ;
754+
755+ if (flash_area == FLASH_AREA_IMAGE_PRIMARY (BOOT_CURR_IMG (state ))) {
756+ area_size = flash_area_get_size (BOOT_IMG_AREA (state , BOOT_PRIMARY_SLOT ));
757+ out_num_sectors = & BOOT_IMG (state , BOOT_PRIMARY_SLOT ).num_sectors ;
758+ } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY (BOOT_CURR_IMG (state ))) {
759+ area_size = flash_area_get_size (BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT ));
760+ out_num_sectors = & BOOT_IMG (state , BOOT_SECONDARY_SLOT ).num_sectors ;
761+ #if MCUBOOT_SWAP_USING_SCRATCH
762+ } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH ) {
763+ area_size = flash_area_get_size (state -> scratch .area );
764+ out_num_sectors = & state -> scratch .num_sectors ;
765+ #endif
766+ } else {
767+ return BOOT_EFLASH ;
768+ }
769+
770+ * out_num_sectors = area_size / MCUBOOT_LOGICAL_SECTOR_SIZE ;
771+
772+ return 0 ;
773+ }
774+
775+ #endif /* defined(MCUBOOT_LOGICAL_SECTOR_SIZE) && MCUBOOT_LOGICAL_SECTOR_SIZE != 0 */
776+
668777static uint32_t
669778boot_write_sz (struct boot_loader_state * state )
670779{
@@ -694,12 +803,13 @@ boot_read_sectors(struct boot_loader_state *state, struct boot_sector_buffer *se
694803 uint8_t image_index ;
695804 int rc ;
696805
806+ image_index = BOOT_CURR_IMG (state );
807+
808+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
697809 if (sectors == NULL ) {
698810 sectors = & sector_buffers ;
699811 }
700812
701- image_index = BOOT_CURR_IMG (state );
702-
703813 BOOT_IMG (state , BOOT_PRIMARY_SLOT ).sectors =
704814 sectors -> primary [image_index ];
705815#if BOOT_NUM_SLOTS > 1
@@ -709,6 +819,9 @@ boot_read_sectors(struct boot_loader_state *state, struct boot_sector_buffer *se
709819 state -> scratch .sectors = sectors -> scratch ;
710820#endif
711821#endif
822+ #else
823+ (void )sectors ;
824+ #endif /* !defined(MCUBOOT_LOGICAL_SECTOR_SIZE) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0 */
712825
713826 rc = boot_initialize_area (state , FLASH_AREA_IMAGE_PRIMARY (image_index ));
714827 if (rc != 0 ) {
@@ -732,6 +845,29 @@ boot_read_sectors(struct boot_loader_state *state, struct boot_sector_buffer *se
732845
733846 BOOT_WRITE_SZ (state ) = boot_write_sz (state );
734847
848+ #if defined(MCUBOOT_LOGICAL_SECTOR_VALIDATION )
849+ BOOT_LOG_INF ("boot_read_sectors: validate image %d slots" , image_index );
850+ BOOT_LOG_INF ("boot_read_sectors: BOOT_PRIMARY_SLOT" );
851+ if (boot_validate_logical_sectors (state , FLASH_AREA_IMAGE_PRIMARY (image_index ),
852+ BOOT_IMG_AREA (state , BOOT_PRIMARY_SLOT )) != 0 ) {
853+ rc = BOOT_EFLASH ;
854+ }
855+
856+ BOOT_LOG_INF ("boot_read_sectors: BOOT_SECONDARY_SLOT" );
857+ if (boot_validate_logical_sectors (state , FLASH_AREA_IMAGE_SECONDARY (image_index ),
858+ BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT )) != 0 ) {
859+ rc = BOOT_EFLASH_SEC ;
860+ }
861+
862+ #if MCUBOOT_SWAP_USING_SCRATCH
863+ BOOT_LOG_INF ("boot_read_sectors: SCRATCH" );
864+ if (boot_validate_logical_sectors (state , FLASH_AREA_IMAGE_SCRATCH ,
865+ state -> scratch .area ) != 0 ) {
866+ rc = BOOT_EFLASH ;
867+ }
868+ #endif /* MCUBOOT_SWAP_USING_SCRATCH */
869+ #endif /* defined(MCUBOOT_LOGICAL_SECTOR_VALIDATION) */
870+
735871 return 0 ;
736872}
737873#endif
0 commit comments