@@ -454,7 +454,8 @@ int swap_status_source(struct boot_loader_state *state)
454454
455455static void boot_swap_sectors (int idx , uint32_t sz , struct boot_loader_state * state ,
456456 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 )
458459{
459460 uint32_t pri_off ;
460461 uint32_t sec_off ;
@@ -466,31 +467,41 @@ static void boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *st
466467 sec_up_off = boot_img_sector_off (state , BOOT_PRIMARY_SLOT , (idx + 1 ));
467468
468469 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 );
473478
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+ }
477483
478484 rc = boot_write_status (state , bs );
479485 bs -> state = BOOT_STATUS_STATE_1 ;
480486 BOOT_STATUS_ASSERT (rc == 0 );
481487 }
482488
483489 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 );
488498
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+ }
494505
495506 rc = boot_write_status (state , bs );
496507 bs -> idx ++ ;
@@ -501,7 +512,8 @@ static void boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *st
501512
502513static void boot_swap_sectors_revert (int idx , uint32_t sz , struct boot_loader_state * state ,
503514 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 )
505517{
506518 uint32_t pri_off ;
507519 uint32_t sec_off ;
@@ -516,31 +528,41 @@ static void boot_swap_sectors_revert(int idx, uint32_t sz, struct boot_loader_st
516528 sec_up_off = boot_img_sector_off (state , BOOT_PRIMARY_SLOT , idx );
517529
518530 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 );
523539
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+ }
527544
528545 rc = boot_write_status (state , bs );
529546 bs -> state = BOOT_STATUS_STATE_1 ;
530547 BOOT_STATUS_ASSERT (rc == 0 );
531548 }
532549
533550 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 );
538559
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+ }
544566
545567 rc = boot_write_status (state , bs );
546568 bs -> idx ++ ;
@@ -601,6 +623,8 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs,
601623 uint32_t trailer_sz ;
602624 uint32_t first_trailer_idx ;
603625 uint32_t last_idx ;
626+ uint32_t used_sectors_pri ;
627+ uint32_t used_sectors_sec ;
604628 uint8_t image_index ;
605629 const struct flash_area * fap_pri ;
606630 const struct flash_area * fap_sec ;
@@ -663,6 +687,14 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs,
663687
664688 bs -> op = BOOT_STATUS_OP_SWAP ;
665689 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 ;
666698
667699 if (bs -> swap_type == BOOT_SWAP_TYPE_REVERT ||
668700 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,
671703 uint32_t mirror_idx = last_idx - idx ;
672704
673705 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));
675709 }
676710
677711 idx ++ ;
@@ -689,7 +723,9 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs,
689723 } else {
690724 while (idx <= last_idx ) {
691725 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));
693729 }
694730
695731 idx ++ ;
0 commit comments