@@ -68,13 +68,15 @@ static struct boot_loader_state boot_data;
68
68
static struct image_max_size image_max_sizes [BOOT_IMAGE_NUMBER ] = {0 };
69
69
#endif
70
70
71
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
71
72
#if (!defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )) || \
72
73
defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
73
74
/* Used for holding static buffers in multiple functions to work around issues
74
75
* in older versions of gcc (e.g. 4.8.4)
75
76
*/
76
77
static struct boot_sector_buffer sector_buffers ;
77
78
#endif
79
+ #endif /* !defined(MCUBOOT_LOGICAL_SECTOR_SIZE) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0 */
78
80
79
81
#if (BOOT_IMAGE_NUMBER > 1 )
80
82
#define IMAGES_ITER (x ) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
@@ -296,6 +298,7 @@ boot_version_cmp(const struct image_version *ver1,
296
298
}
297
299
#endif
298
300
301
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
299
302
#if (!defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )) || \
300
303
defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
301
304
static int
@@ -336,6 +339,111 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area)
336
339
return 0 ;
337
340
}
338
341
#endif
342
+ #else /* defined(MCUBOOT_LOGICAL_SECTOR_SIZE) && MCUBOOT_LOGICAL_SECTOR_SIZE != 0 */
343
+ #if defined(MCUBOOT_LOGICAL_SECTOR_VALIDATION )
344
+ /* Validation can only run once all flash areaa area open and pointers to
345
+ * flash area objects area stored in state.
346
+ */
347
+ static int
348
+ boot_validate_logical_sectors (const struct boot_loader_state * state , int faid , const struct flash_area * fa )
349
+ {
350
+ uint32_t num_sectors = BOOT_MAX_IMG_SECTORS ;
351
+ size_t slot_size ;
352
+ size_t slot_off ;
353
+ size_t sect_off = 0 ;
354
+ int rc ;
355
+ int final_rc = 0 ;
356
+
357
+ assert (fa != NULL );
358
+ assert (faid != 0 );
359
+
360
+ slot_off = flash_area_get_off (fa );
361
+ slot_size = flash_area_get_size (fa );
362
+
363
+
364
+ /* Go till all validations are complete or we face issue that does not allow
365
+ * to proceede with further tests.
366
+ */
367
+ BOOT_LOG_INF ("boot_validate_logical_sectors: validating flash area %p" , fa );
368
+ BOOT_LOG_INF ("boot_validate_logical_sectors: MCUBOOT_LOGICAL_SECTOR_SIZE == 0x%x" ,
369
+ MCUBOOT_LOGICAL_SECTOR_SIZE );
370
+ BOOT_LOG_INF ("boot_validate_logical_sectors: slot offset == 0x%x" , slot_off );
371
+ if (slot_size != 0 ) {
372
+ BOOT_LOG_INF ("boot_validate_logical_sectors: slot size == 0x%x" , slot_size );
373
+ } else {
374
+ BOOT_LOG_ERR ("boot_validate_logical_sectors: 0 size slot" );
375
+ return BOOT_EFLASH ;
376
+ }
377
+
378
+ BOOT_LOG_INF ("boot_validate_logical_sectors: max %d logical sectors" ,
379
+ slot_size / MCUBOOT_LOGICAL_SECTOR_SIZE );
380
+
381
+ if (slot_off % MCUBOOT_LOGICAL_SECTOR_SIZE ) {
382
+ BOOT_LOG_ERR ("boot_validate_logical_sectors: area offset not aligned" );
383
+ final_rc = BOOT_EFLASH ;
384
+ }
385
+
386
+ if (slot_size % MCUBOOT_LOGICAL_SECTOR_SIZE ) {
387
+ BOOT_LOG_ERR ("boot_validate_logical_sectors: area size not aligned" );
388
+ final_rc = BOOT_EFLASH ;
389
+ }
390
+
391
+ /* Check all hardware specific pages against erase pages of a device */
392
+ for (size_t i = 0 ; i < num_sectors ; i ++ ) {
393
+ struct flash_sector fas ;
394
+
395
+ MCUBOOT_WATCHDOG_FEED ();
396
+
397
+ BOOT_LOG_INF ("boot_validate_logical_sectors: page 0x%x:0x%x " , slot_off , sect_off );
398
+ rc = flash_area_get_sector (fa , sect_off , & fas );
399
+ if (rc < 0 ) {
400
+ BOOT_LOG_ERR ("boot_validate_logical_sectors: query err %d" , rc );
401
+ final_rc = BOOT_EFLASH ;
402
+ continue ;
403
+ }
404
+
405
+
406
+ if (flash_sector_get_off (& fas ) % MCUBOOT_LOGICAL_SECTOR_SIZE ) {
407
+ BOOT_LOG_ERR ("boot_validate_logical_sectors: misaligned offset" );
408
+ final_rc = BOOT_EFLASH ;
409
+ }
410
+
411
+ sect_off += flash_sector_get_size (& fas );
412
+ }
413
+
414
+ BOOT_LOG_INF ("boot_validate_logical_sectors: done %d" , final_rc );
415
+
416
+ return final_rc ;
417
+ }
418
+ #endif /* MCUBOOT_LOGICAL_SECTOR_VALIDATION */
419
+
420
+ static int
421
+ boot_initialize_area (struct boot_loader_state * state , int flash_area )
422
+ {
423
+ size_t area_size ;
424
+ uint32_t * out_num_sectors ;
425
+
426
+ if (flash_area == FLASH_AREA_IMAGE_PRIMARY (BOOT_CURR_IMG (state ))) {
427
+ area_size = flash_area_get_size (BOOT_IMG_AREA (state , BOOT_PRIMARY_SLOT ));
428
+ out_num_sectors = & BOOT_IMG (state , BOOT_PRIMARY_SLOT ).num_sectors ;
429
+ } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY (BOOT_CURR_IMG (state ))) {
430
+ area_size = flash_area_get_size (BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT ));
431
+ out_num_sectors = & BOOT_IMG (state , BOOT_SECONDARY_SLOT ).num_sectors ;
432
+ #if MCUBOOT_SWAP_USING_SCRATCH
433
+ } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH ) {
434
+ area_size = flash_area_get_size (state -> scratch .area );
435
+ out_num_sectors = & state -> scratch .num_sectors ;
436
+ #endif
437
+ } else {
438
+ return BOOT_EFLASH ;
439
+ }
440
+
441
+ * out_num_sectors = area_size / MCUBOOT_LOGICAL_SECTOR_SIZE ;
442
+
443
+ return 0 ;
444
+ }
445
+
446
+ #endif /* defined(MCUBOOT_LOGICAL_SECTOR_SIZE) && MCUBOOT_LOGICAL_SECTOR_SIZE != 0 */
339
447
340
448
#if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
341
449
static int
@@ -618,19 +726,23 @@ boot_read_sectors(struct boot_loader_state *state, struct boot_sector_buffer *se
618
726
uint8_t image_index ;
619
727
int rc ;
620
728
729
+ image_index = BOOT_CURR_IMG (state );
730
+
731
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
621
732
if (sectors == NULL ) {
622
733
sectors = & sector_buffers ;
623
734
}
624
735
625
- image_index = BOOT_CURR_IMG (state );
626
-
627
736
BOOT_IMG (state , BOOT_PRIMARY_SLOT ).sectors =
628
737
sectors -> primary [image_index ];
629
738
BOOT_IMG (state , BOOT_SECONDARY_SLOT ).sectors =
630
739
sectors -> secondary [image_index ];
631
740
#if MCUBOOT_SWAP_USING_SCRATCH
632
741
state -> scratch .sectors = sectors -> scratch ;
633
742
#endif
743
+ #else
744
+ (void )sectors ;
745
+ #endif /* !defined(MCUBOOT_LOGICAL_SECTOR_SIZE) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0 */
634
746
635
747
rc = boot_initialize_area (state , FLASH_AREA_IMAGE_PRIMARY (image_index ));
636
748
if (rc != 0 ) {
@@ -652,6 +764,29 @@ boot_read_sectors(struct boot_loader_state *state, struct boot_sector_buffer *se
652
764
653
765
BOOT_WRITE_SZ (state ) = boot_write_sz (state );
654
766
767
+ #if defined(MCUBOOT_LOGICAL_SECTOR_VALIDATION )
768
+ BOOT_LOG_INF ("boot_read_sectors: validate image %d slots" , image_index );
769
+ BOOT_LOG_INF ("boot_read_sectors: BOOT_PRIMARY_SLOT" );
770
+ if (boot_validate_logical_sectors (state , FLASH_AREA_IMAGE_PRIMARY (image_index ),
771
+ BOOT_IMG_AREA (state , BOOT_PRIMARY_SLOT )) != 0 ) {
772
+ rc = BOOT_EFLASH ;
773
+ }
774
+
775
+ BOOT_LOG_INF ("boot_read_sectors: BOOT_SECONDARY_SLOT" );
776
+ if (boot_validate_logical_sectors (state , FLASH_AREA_IMAGE_SECONDARY (image_index ),
777
+ BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT )) != 0 ) {
778
+ rc = BOOT_EFLASH_SEC ;
779
+ }
780
+
781
+ #if MCUBOOT_SWAP_USING_SCRATCH
782
+ BOOT_LOG_INF ("boot_read_sectors: SCRATCH" );
783
+ if (boot_validate_logical_sectors (state , FLASH_AREA_IMAGE_SCRATCH ,
784
+ state -> scratch .area ) != 0 ) {
785
+ rc = BOOT_EFLASH ;
786
+ }
787
+ #endif /* MCUBOOT_SWAP_USING_SCRATCH */
788
+ #endif /* defined(MCUBOOT_LOGICAL_SECTOR_VALIDATION) */
789
+
655
790
return 0 ;
656
791
}
657
792
@@ -789,6 +924,7 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
789
924
}
790
925
791
926
#if !defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )
927
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
792
928
static fih_ret
793
929
split_image_check (struct image_header * app_hdr ,
794
930
const struct flash_area * app_fap ,
@@ -819,6 +955,7 @@ split_image_check(struct image_header *app_hdr,
819
955
out :
820
956
FIH_RET (fih_rc );
821
957
}
958
+ #endif /* defined(MCUBOOT_LOGICAL_SECTOR_SIZE) && MCUBOOT_LOGICAL_SECTOR_SIZE != 0 */
822
959
#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
823
960
824
961
/*
@@ -2297,10 +2434,12 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2297
2434
2298
2435
BOOT_LOG_DBG ("context_boot_go" );
2299
2436
2437
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
2300
2438
#if defined(__BOOTSIM__ )
2301
2439
struct boot_sector_buffer sector_buf ;
2302
2440
sectors = & sector_buf ;
2303
2441
#endif
2442
+ #endif /* !defined(MCUBOOT_LOGICAL_SECTOR_SIZE) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0 */
2304
2443
2305
2444
has_upgrade = false;
2306
2445
@@ -2560,6 +2699,7 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2560
2699
FIH_RET (fih_rc );
2561
2700
}
2562
2701
2702
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
2563
2703
fih_ret
2564
2704
split_go (int loader_slot , int split_slot , void * * entry )
2565
2705
{
@@ -2625,6 +2765,7 @@ split_go(int loader_slot, int split_slot, void **entry)
2625
2765
2626
2766
FIH_RET (fih_rc );
2627
2767
}
2768
+ #endif /* !defined(MCUBOOT_LOGICAL_SECTOR_SIZE) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0 */
2628
2769
2629
2770
#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
2630
2771
0 commit comments