@@ -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
@@ -652,6 +760,29 @@ boot_read_sectors(struct boot_loader_state *state, struct boot_sector_buffer *se
652
760
653
761
BOOT_WRITE_SZ (state ) = boot_write_sz (state );
654
762
763
+ #if defined(MCUBOOT_LOGICAL_SECTOR_VALIDATION )
764
+ BOOT_LOG_INF ("boot_read_sectors: validate image %d slots" , image_index );
765
+ BOOT_LOG_INF ("boot_read_sectors: BOOT_PRIMARY_SLOT" );
766
+ if (boot_validate_logical_sectors (state , FLASH_AREA_IMAGE_PRIMARY (image_index ),
767
+ BOOT_IMG_AREA (state , BOOT_PRIMARY_SLOT )) != 0 ) {
768
+ rc = BOOT_EFLASH ;
769
+ }
770
+
771
+ BOOT_LOG_INF ("boot_read_sectors: BOOT_SECONDARY_SLOT" );
772
+ if (boot_validate_logical_sectors (state , FLASH_AREA_IMAGE_SECONDARY (image_index ),
773
+ BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT )) != 0 ) {
774
+ rc = BOOT_EFLASH_SEC ;
775
+ }
776
+
777
+ #if MCUBOOT_SWAP_USING_SCRATCH
778
+ BOOT_LOG_INF ("boot_read_sectors: SCRATCH" );
779
+ if (boot_validate_logical_sectors (state , FLASH_AREA_IMAGE_SCRATCH ,
780
+ state -> scratch .area ) != 0 ) {
781
+ rc = BOOT_EFLASH ;
782
+ }
783
+ #endif /* MCUBOOT_SWAP_USING_SCRATCH */
784
+ #endif /* defined(MCUBOOT_LOGICAL_SECTOR_VALIDATION) */
785
+
655
786
return 0 ;
656
787
}
657
788
@@ -789,6 +920,7 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
789
920
}
790
921
791
922
#if !defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )
923
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
792
924
static fih_ret
793
925
split_image_check (struct image_header * app_hdr ,
794
926
const struct flash_area * app_fap ,
@@ -819,6 +951,7 @@ split_image_check(struct image_header *app_hdr,
819
951
out :
820
952
FIH_RET (fih_rc );
821
953
}
954
+ #endif /* defined(MCUBOOT_LOGICAL_SECTOR_SIZE) && MCUBOOT_LOGICAL_SECTOR_SIZE != 0 */
822
955
#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
823
956
824
957
/*
@@ -2297,10 +2430,12 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2297
2430
2298
2431
BOOT_LOG_DBG ("context_boot_go" );
2299
2432
2433
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
2300
2434
#if defined(__BOOTSIM__ )
2301
2435
struct boot_sector_buffer sector_buf ;
2302
2436
sectors = & sector_buf ;
2303
2437
#endif
2438
+ #endif /* !defined(MCUBOOT_LOGICAL_SECTOR_SIZE) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0 */
2304
2439
2305
2440
has_upgrade = false;
2306
2441
@@ -2560,6 +2695,7 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2560
2695
FIH_RET (fih_rc );
2561
2696
}
2562
2697
2698
+ #if !defined(MCUBOOT_LOGICAL_SECTOR_SIZE ) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0
2563
2699
fih_ret
2564
2700
split_go (int loader_slot , int split_slot , void * * entry )
2565
2701
{
@@ -2625,6 +2761,7 @@ split_go(int loader_slot, int split_slot, void **entry)
2625
2761
2626
2762
FIH_RET (fih_rc );
2627
2763
}
2764
+ #endif /* !defined(MCUBOOT_LOGICAL_SECTOR_SIZE) || MCUBOOT_LOGICAL_SECTOR_SIZE == 0 */
2628
2765
2629
2766
#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
2630
2767
0 commit comments