@@ -454,7 +454,8 @@ int swap_status_source(struct boot_loader_state *state)
454
454
455
455
static void boot_swap_sectors (int idx , uint32_t sz , struct boot_loader_state * state ,
456
456
struct boot_status * bs , const struct flash_area * fap_pri ,
457
- const struct flash_area * fap_sec )
457
+ const struct flash_area * fap_sec , bool skip_primary ,
458
+ bool skip_secondary )
458
459
{
459
460
uint32_t pri_off ;
460
461
uint32_t sec_off ;
@@ -466,31 +467,41 @@ static void boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *st
466
467
sec_up_off = boot_img_sector_off (state , BOOT_PRIMARY_SLOT , (idx + 1 ));
467
468
468
469
if (bs -> state == BOOT_STATUS_STATE_0 ) {
469
- /* Copy from slot 0 X to slot 1 X */
470
- BOOT_LOG_DBG ("Erasing secondary 0x%x of 0x%x" , sec_off , sz );
471
- rc = boot_erase_region (fap_sec , sec_off , sz );
472
- assert (rc == 0 );
470
+ if (skip_primary == true) {
471
+ BOOT_LOG_DBG ("Skipping erase of secondary 0x%x and copy from primary 0x%x" , sec_off ,
472
+ pri_off );
473
+ } else {
474
+ /* Copy from slot 0 X to slot 1 X */
475
+ BOOT_LOG_DBG ("Erasing secondary 0x%x of 0x%x" , sec_off , sz );
476
+ rc = boot_erase_region (fap_sec , sec_off , sz );
477
+ assert (rc == 0 );
473
478
474
- BOOT_LOG_DBG ("Copying primary 0x%x -> secondary 0x%x of 0x%x" , pri_off , sec_off , sz );
475
- rc = BOOT_COPY_REGION (state , fap_pri , fap_sec , pri_off , sec_off , sz , 0 );
476
- assert (rc == 0 );
479
+ BOOT_LOG_DBG ("Copying primary 0x%x -> secondary 0x%x of 0x%x" , pri_off , sec_off , sz );
480
+ rc = BOOT_COPY_REGION (state , fap_pri , fap_sec , pri_off , sec_off , sz , 0 );
481
+ assert (rc == 0 );
482
+ }
477
483
478
484
rc = boot_write_status (state , bs );
479
485
bs -> state = BOOT_STATUS_STATE_1 ;
480
486
BOOT_STATUS_ASSERT (rc == 0 );
481
487
}
482
488
483
489
if (bs -> state == BOOT_STATUS_STATE_1 ) {
484
- /* Erase slot 0 X */
485
- BOOT_LOG_DBG ("Erasing primary 0x%x of 0x%x" , pri_off , sz );
486
- rc = boot_erase_region (fap_pri , pri_off , sz );
487
- assert (rc == 0 );
490
+ if (skip_secondary == true) {
491
+ BOOT_LOG_DBG ("Skipping erase of primary 0x%x and copy from secondary 0x%x" , pri_off ,
492
+ sec_up_off );
493
+ } else {
494
+ /* Erase slot 0 X */
495
+ BOOT_LOG_DBG ("Erasing primary 0x%x of 0x%x" , pri_off , sz );
496
+ rc = boot_erase_region (fap_pri , pri_off , sz );
497
+ assert (rc == 0 );
488
498
489
- /* Copy from slot 1 (X + 1) to slot 0 X */
490
- BOOT_LOG_DBG ("Copying secondary 0x%x -> primary 0x%x of 0x%x" , sec_up_off , pri_off ,
491
- sz );
492
- rc = BOOT_COPY_REGION (state , fap_sec , fap_pri , sec_up_off , pri_off , sz , 0 );
493
- assert (rc == 0 );
499
+ /* Copy from slot 1 (X + 1) to slot 0 X */
500
+ BOOT_LOG_DBG ("Copying secondary 0x%x -> primary 0x%x of 0x%x" , sec_up_off , pri_off ,
501
+ sz );
502
+ rc = BOOT_COPY_REGION (state , fap_sec , fap_pri , sec_up_off , pri_off , sz , 0 );
503
+ assert (rc == 0 );
504
+ }
494
505
495
506
rc = boot_write_status (state , bs );
496
507
bs -> idx ++ ;
@@ -501,7 +512,8 @@ static void boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *st
501
512
502
513
static void boot_swap_sectors_revert (int idx , uint32_t sz , struct boot_loader_state * state ,
503
514
struct boot_status * bs , const struct flash_area * fap_pri ,
504
- const struct flash_area * fap_sec , uint32_t sector_sz )
515
+ const struct flash_area * fap_sec , uint32_t sector_sz ,
516
+ bool skip_primary , bool skip_secondary )
505
517
{
506
518
uint32_t pri_off ;
507
519
uint32_t sec_off ;
@@ -516,31 +528,41 @@ static void boot_swap_sectors_revert(int idx, uint32_t sz, struct boot_loader_st
516
528
sec_up_off = boot_img_sector_off (state , BOOT_PRIMARY_SLOT , idx );
517
529
518
530
if (bs -> state == BOOT_STATUS_STATE_0 ) {
519
- /* Copy from slot 0 X to slot 1 X */
520
- BOOT_LOG_DBG ("Erasing secondary 0x%x of 0x%x" , sec_off , sz );
521
- rc = boot_erase_region (fap_sec , sec_off , sz );
522
- assert (rc == 0 );
531
+ if (skip_primary == true) {
532
+ BOOT_LOG_DBG ("Skipping erase of secondary 0x%x and copy from primary 0x%x" , sec_off ,
533
+ pri_off );
534
+ } else {
535
+ /* Copy from slot 0 X to slot 1 X */
536
+ BOOT_LOG_DBG ("Erasing secondary 0x%x of 0x%x" , sec_off , sz );
537
+ rc = boot_erase_region (fap_sec , sec_off , sz );
538
+ assert (rc == 0 );
523
539
524
- BOOT_LOG_DBG ("Copying primary 0x%x -> secondary 0x%x of 0x%x" , pri_off , sec_off , sz );
525
- rc = BOOT_COPY_REGION (state , fap_pri , fap_sec , pri_off , sec_off , sz , sector_sz );
526
- assert (rc == 0 );
540
+ BOOT_LOG_DBG ("Copying primary 0x%x -> secondary 0x%x of 0x%x" , pri_off , sec_off , sz );
541
+ rc = BOOT_COPY_REGION (state , fap_pri , fap_sec , pri_off , sec_off , sz , sector_sz );
542
+ assert (rc == 0 );
543
+ }
527
544
528
545
rc = boot_write_status (state , bs );
529
546
bs -> state = BOOT_STATUS_STATE_1 ;
530
547
BOOT_STATUS_ASSERT (rc == 0 );
531
548
}
532
549
533
550
if (bs -> state == BOOT_STATUS_STATE_1 ) {
534
- /* Erase slot 0 X */
535
- BOOT_LOG_DBG ("Erasing primary 0x%x of 0x%x" , pri_off , sz );
536
- rc = boot_erase_region (fap_pri , pri_off , sz );
537
- assert (rc == 0 );
551
+ if (skip_secondary == true) {
552
+ BOOT_LOG_DBG ("Skipping erase of primary 0x%x and copy from secondary 0x%x" , pri_off ,
553
+ sec_up_off );
554
+ } else {
555
+ /* Erase slot 0 X */
556
+ BOOT_LOG_DBG ("Erasing primary 0x%x of 0x%x" , pri_off , sz );
557
+ rc = boot_erase_region (fap_pri , pri_off , sz );
558
+ assert (rc == 0 );
538
559
539
- /* Copy from slot 1 (X + 1) to slot 0 X */
540
- BOOT_LOG_DBG ("Copying secondary 0x%x -> primary 0x%x of 0x%x" , sec_up_off , pri_off ,
541
- sz );
542
- rc = BOOT_COPY_REGION (state , fap_sec , fap_pri , sec_up_off , pri_off , sz , 0 );
543
- assert (rc == 0 );
560
+ /* Copy from slot 1 (X + 1) to slot 0 X */
561
+ BOOT_LOG_DBG ("Copying secondary 0x%x -> primary 0x%x of 0x%x" , sec_up_off , pri_off ,
562
+ sz );
563
+ rc = BOOT_COPY_REGION (state , fap_sec , fap_pri , sec_up_off , pri_off , sz , 0 );
564
+ assert (rc == 0 );
565
+ }
544
566
545
567
rc = boot_write_status (state , bs );
546
568
bs -> idx ++ ;
@@ -601,6 +623,8 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs,
601
623
uint32_t trailer_sz ;
602
624
uint32_t first_trailer_idx ;
603
625
uint32_t last_idx ;
626
+ uint32_t used_sectors_pri
627
+ uint32_t used_sectors_sec ;
604
628
uint8_t image_index ;
605
629
const struct flash_area * fap_pri ;
606
630
const struct flash_area * fap_sec ;
@@ -663,6 +687,14 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs,
663
687
664
688
bs -> op = BOOT_STATUS_OP_SWAP ;
665
689
idx = 0 ;
690
+ used_sectors_pri = ((state -> imgs [BOOT_CURR_IMG (state )][BOOT_PRIMARY_SLOT ].hdr .ih_hdr_size +
691
+ state -> imgs [BOOT_CURR_IMG (state )][BOOT_PRIMARY_SLOT ].hdr .ih_protect_tlv_size +
692
+ state -> imgs [BOOT_CURR_IMG (state )][BOOT_PRIMARY_SLOT ].hdr .ih_img_size ) + sector_sz - 1 ) /
693
+ sector_sz ;
694
+ used_sectors_sec = ((state -> imgs [BOOT_CURR_IMG (state )][BOOT_SECONDARY_SLOT ].hdr .ih_hdr_size +
695
+ state -> imgs [BOOT_CURR_IMG (state )][BOOT_SECONDARY_SLOT ].hdr .ih_protect_tlv_size +
696
+ state -> imgs [BOOT_CURR_IMG (state )][BOOT_SECONDARY_SLOT ].hdr .ih_img_size ) + sector_sz - 1 ) /
697
+ sector_sz ;
666
698
667
699
if (bs -> swap_type == BOOT_SWAP_TYPE_REVERT ||
668
700
boot_swap_type_multi (BOOT_CURR_IMG (state )) == BOOT_SWAP_TYPE_REVERT ) {
@@ -671,7 +703,9 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs,
671
703
uint32_t mirror_idx = last_idx - idx ;
672
704
673
705
boot_swap_sectors_revert (mirror_idx , sector_sz , state , bs , fap_pri , fap_sec ,
674
- sector_sz );
706
+ sector_sz ,
707
+ (mirror_idx > used_sectors_pri ? true : false),
708
+ (mirror_idx > used_sectors_sec ? true : false));
675
709
}
676
710
677
711
idx ++ ;
@@ -689,7 +723,9 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs,
689
723
} else {
690
724
while (idx <= last_idx ) {
691
725
if (idx >= (bs -> idx - BOOT_STATUS_IDX_0 )) {
692
- boot_swap_sectors (idx , sector_sz , state , bs , fap_pri , fap_sec );
726
+ boot_swap_sectors (idx , sector_sz , state , bs , fap_pri , fap_sec ,
727
+ (idx > used_sectors_pri ? true : false),
728
+ (idx > used_sectors_sec ? true : false));
693
729
}
694
730
695
731
idx ++ ;
0 commit comments