@@ -593,12 +593,20 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs,
593593 const struct flash_area * fap_pri = NULL ;
594594 const struct flash_area * fap_sec = NULL ;
595595 int rc ;
596+ uint16_t unprotected_tlv_size_pri ;
597+ uint16_t unprotected_tlv_size_sec ;
596598
597599 BOOT_LOG_INF ("Starting swap using offset algorithm." );
598600
599601 last_idx = find_last_idx (state , copy_size );
600602 sector_sz = boot_img_sector_size (state , BOOT_SLOT_PRIMARY , 0 );
601603
604+ fap_pri = BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY );
605+ assert (fap_pri != NULL );
606+
607+ fap_sec = BOOT_IMG_AREA (state , BOOT_SLOT_SECONDARY );
608+ assert (fap_sec != NULL );
609+
602610 /* When starting a new swap upgrade, check that there is enough space */
603611 if (boot_status_is_reset (bs )) {
604612 sz = 0 ;
@@ -623,12 +631,6 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs,
623631 }
624632 }
625633
626- fap_pri = BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY );
627- assert (fap_pri != NULL );
628-
629- fap_sec = BOOT_IMG_AREA (state , BOOT_SLOT_SECONDARY );
630- assert (fap_sec != NULL );
631-
632634 fixup_revert (state , bs , fap_sec );
633635
634636 /* Init areas for storing swap status */
@@ -647,14 +649,24 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs,
647649 assert (rc == 0 );
648650 }
649651
652+ /* Read the unprotected TLV sizes from the boot swap status area, this information might get
653+ * jangled if rebooted during an update so it needs to be stored in this area for safe
654+ * retrieval
655+ */
656+ boot_read_unprotected_tlv_sizes (fap_pri , & unprotected_tlv_size_pri , & unprotected_tlv_size_sec );
657+ BOOT_LOG_DBG ("Unprotected TLV sizes image=%d: pri=%d, sec=%d" , BOOT_CURR_IMG (state ),
658+ unprotected_tlv_size_pri , unprotected_tlv_size_sec );
659+
650660 bs -> op = BOOT_STATUS_OP_SWAP ;
651661 idx = 0 ;
652662 used_sectors_pri = ((state -> imgs [BOOT_CURR_IMG (state )][BOOT_SLOT_PRIMARY ].hdr .ih_hdr_size +
663+ unprotected_tlv_size_pri +
653664 state -> imgs [BOOT_CURR_IMG (state )][BOOT_SLOT_PRIMARY ].hdr .ih_protect_tlv_size +
654665 state -> imgs [BOOT_CURR_IMG (state )][BOOT_SLOT_PRIMARY ].hdr .ih_img_size ) + sector_sz - 1 ) /
655666 sector_sz ;
656667 used_sectors_sec = ((state -> imgs [BOOT_CURR_IMG (state )][BOOT_SLOT_SECONDARY ].hdr .ih_hdr_size +
657668 state -> imgs [BOOT_CURR_IMG (state )][BOOT_SLOT_SECONDARY ].hdr .ih_protect_tlv_size +
669+ unprotected_tlv_size_sec +
658670 state -> imgs [BOOT_CURR_IMG (state )][BOOT_SLOT_SECONDARY ].hdr .ih_img_size ) + sector_sz - 1 ) /
659671 sector_sz ;
660672
@@ -770,6 +782,11 @@ int boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *si
770782 goto done ;
771783 }
772784
785+ /* This is needed as unprotected TLV size cannot be calculated once a swap has been started,
786+ * it is only able to be properly calculated when images are in pristine states
787+ */
788+ BOOT_IMG_UNPROTECTED_TLV_SIZE (state , slot ) = info .it_tlv_tot ;
789+
773790 * size = off + protect_tlv_size + info .it_tlv_tot ;
774791 rc = 0 ;
775792
0 commit comments