@@ -1213,23 +1213,50 @@ boot_copy_region(struct boot_loader_state *state,
12131213 int chunk_sz ;
12141214 int rc ;
12151215#ifdef MCUBOOT_ENC_IMAGES
1216- uint32_t off ;
1216+ uint32_t off = off_dst ;
12171217 uint32_t tlv_off ;
12181218 size_t blk_off ;
12191219 struct image_header * hdr ;
12201220 uint16_t idx ;
12211221 uint32_t blk_sz ;
1222- uint8_t image_index ;
1222+ uint8_t image_index = BOOT_CURR_IMG ( state ) ;
12231223 bool encrypted_src ;
12241224 bool encrypted_dst ;
1225- /* Assuming the secondary slot is source and needs decryption */
1225+ /* Assuming the secondary slot is source; note that 0 here not only
1226+ * means that primary slot is source, but also that there will be
1227+ * encryption happening, if it is 1 then there is decryption from
1228+ * secondary slot.
1229+ */
12261230 int source_slot = 1 ;
1231+ /* In case of encryption enabled, we may have to do more work than
1232+ * just copy bytes */
1233+ bool only_copy = false;
1234+ #else
1235+ (void )state ;
12271236#endif
12281237
12291238 TARGET_STATIC uint8_t buf [BUF_SZ ] __attribute__((aligned (4 )));
12301239
1231- #if !defined(MCUBOOT_ENC_IMAGES )
1232- (void )state ;
1240+ #ifdef MCUBOOT_ENC_IMAGES
1241+ encrypted_src = (flash_area_get_id (fap_src ) != FLASH_AREA_IMAGE_PRIMARY (image_index ));
1242+ encrypted_dst = (flash_area_get_id (fap_dst ) != FLASH_AREA_IMAGE_PRIMARY (image_index ));
1243+
1244+ if (encrypted_src != encrypted_dst ) {
1245+ if (encrypted_dst ) {
1246+ /* Need encryption, metadata from the primary slot */
1247+ hdr = boot_img_hdr (state , BOOT_PRIMARY_SLOT );
1248+ source_slot = 0 ;
1249+ } else {
1250+ /* Need decryption, metadata from the secondary slot */
1251+ hdr = boot_img_hdr (state , BOOT_SECONDARY_SLOT );
1252+ source_slot = 1 ;
1253+ }
1254+ } else {
1255+ /* In case when source and targe is the same area, this means that we
1256+ * only have to copy bytes, no encryption or decryption.
1257+ */
1258+ only_copy = true;
1259+ }
12331260#endif
12341261
12351262 bytes_copied = 0 ;
@@ -1246,56 +1273,41 @@ boot_copy_region(struct boot_loader_state *state,
12461273 }
12471274
12481275#ifdef MCUBOOT_ENC_IMAGES
1249- image_index = BOOT_CURR_IMG (state );
1250- encrypted_src = (flash_area_get_id (fap_src ) != FLASH_AREA_IMAGE_PRIMARY (image_index ));
1251- encrypted_dst = (flash_area_get_id (fap_dst ) != FLASH_AREA_IMAGE_PRIMARY (image_index ));
1252-
1253- if (encrypted_src != encrypted_dst ) {
1254- off = off_dst ;
1255-
1256- if (encrypted_dst ) {
1257- /* Need encryption, metadata from the primary slot */
1258- hdr = boot_img_hdr (state , BOOT_PRIMARY_SLOT );
1259- source_slot = 0 ;
1276+ /* If only copy, then does not matter if header indicates need for
1277+ * encryptio/decryptio, we just copy data. */
1278+ if (!only_copy && IS_ENCRYPTED (hdr )) {
1279+ uint32_t abs_off = off + bytes_copied ;
1280+ if (abs_off < hdr -> ih_hdr_size ) {
1281+ /* do not decrypt header */
1282+ if (abs_off + chunk_sz > hdr -> ih_hdr_size ) {
1283+ /* The lower part of the chunk contains header data */
1284+ blk_off = 0 ;
1285+ blk_sz = chunk_sz - (hdr -> ih_hdr_size - abs_off );
1286+ idx = hdr -> ih_hdr_size - abs_off ;
1287+ } else {
1288+ /* The chunk contains exclusively header data */
1289+ blk_sz = 0 ; /* nothing to decrypt */
1290+ }
12601291 } else {
1261- /* Need decryption, metadata from the secondary slot */
1262- hdr = boot_img_hdr ( state , BOOT_SECONDARY_SLOT ) ;
1263- source_slot = 1 ;
1292+ idx = 0 ;
1293+ blk_sz = chunk_sz ;
1294+ blk_off = ( abs_off - hdr -> ih_hdr_size ) & 0xf ;
12641295 }
12651296
1266- if (IS_ENCRYPTED (hdr )) {
1267- uint32_t abs_off = off + bytes_copied ;
1268- if (abs_off < hdr -> ih_hdr_size ) {
1269- /* do not decrypt header */
1270- if (abs_off + chunk_sz > hdr -> ih_hdr_size ) {
1271- /* The lower part of the chunk contains header data */
1272- blk_off = 0 ;
1273- blk_sz = chunk_sz - (hdr -> ih_hdr_size - abs_off );
1274- idx = hdr -> ih_hdr_size - abs_off ;
1297+ if (blk_sz > 0 )
1298+ {
1299+ tlv_off = BOOT_TLV_OFF (hdr );
1300+ if (abs_off + chunk_sz > tlv_off ) {
1301+ /* do not decrypt TLVs */
1302+ if (abs_off >= tlv_off ) {
1303+ blk_sz = 0 ;
12751304 } else {
1276- /* The chunk contains exclusively header data */
1277- blk_sz = 0 ; /* nothing to decrypt */
1278- }
1279- } else {
1280- idx = 0 ;
1281- blk_sz = chunk_sz ;
1282- blk_off = (abs_off - hdr -> ih_hdr_size ) & 0xf ;
1283- }
1284-
1285- if (blk_sz > 0 ) {
1286- tlv_off = BOOT_TLV_OFF (hdr );
1287- if (abs_off + chunk_sz > tlv_off ) {
1288- /* do not decrypt TLVs */
1289- if (abs_off >= tlv_off ) {
1290- blk_sz = 0 ;
1291- } else {
1292- blk_sz = tlv_off - abs_off ;
1293- }
1305+ blk_sz = tlv_off - abs_off ;
12941306 }
1295- boot_encrypt (BOOT_CURR_ENC (state ), source_slot ,
1296- (abs_off + idx ) - hdr -> ih_hdr_size , blk_sz ,
1297- blk_off , & buf [idx ]);
12981307 }
1308+ boot_encrypt (BOOT_CURR_ENC (state ), source_slot ,
1309+ (abs_off + idx ) - hdr -> ih_hdr_size , blk_sz ,
1310+ blk_off , & buf [idx ]);
12991311 }
13001312 }
13011313#endif
0 commit comments