@@ -256,15 +256,6 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
256256 goto finish_without_clean ;
257257 }
258258
259- rc = compression_lzma -> init (NULL );
260- rc = compression_arm_thumb -> init (NULL );
261-
262- if (rc ) {
263- BOOT_LOG_ERR ("Decompression library fatal error" );
264- rc = BOOT_EBADSTATUS ;
265- goto finish_without_clean ;
266- }
267-
268259 /* We need a modified header which has the updated sizes, start with the original header */
269260 memcpy (& modified_hdr , hdr , sizeof (modified_hdr ));
270261
@@ -276,12 +267,28 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
276267 if (rc ) {
277268 BOOT_LOG_ERR ("Unable to determine decompressed size of compressed image" );
278269 rc = BOOT_EBADIMAGE ;
279- goto finish ;
270+ goto finish_without_clean ;
280271 }
281272
282273 modified_hdr .ih_flags &= ~COMPRESSIONFLAGS ;
283274 modified_hdr .ih_img_size = decompressed_image_size ;
284275
276+ rc = compression_lzma -> init (NULL , decompressed_image_size );
277+
278+ if (rc ) {
279+ BOOT_LOG_ERR ("Decompression library fatal error" );
280+ rc = BOOT_EBADSTATUS ;
281+ goto finish_without_clean ;
282+ }
283+
284+ rc = compression_arm_thumb -> init (NULL , decompressed_image_size );
285+
286+ if (rc ) {
287+ BOOT_LOG_ERR ("Decompression library fatal error" );
288+ rc = BOOT_EBADSTATUS ;
289+ goto finish ;
290+ }
291+
285292 /* Calculate the protected TLV size, these will not include the decompressed
286293 * sha/size/signature entries
287294 */
@@ -1101,7 +1108,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
11011108 if (rc ) {
11021109 BOOT_LOG_ERR ("Invalid/missing image decrypted compressed size value" );
11031110 rc = BOOT_EBADIMAGE ;
1104- goto finish ;
1111+ goto finish_without_clean ;
11051112 }
11061113
11071114 if (IS_ENCRYPTED (hdr )) {
@@ -1124,7 +1131,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
11241131 */
11251132 BOOT_LOG_ERR ("Invalid image compression flags: no supported compression found" );
11261133 rc = BOOT_EBADIMAGE ;
1127- goto finish ;
1134+ goto finish_without_clean ;
11281135 }
11291136
11301137 compression_lzma = nrf_compress_implementation_find (NRF_COMPRESS_TYPE_LZMA );
@@ -1135,16 +1142,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
11351142 /* Compression library missing or missing required function pointer */
11361143 BOOT_LOG_ERR ("Decompression library fatal error" );
11371144 rc = BOOT_EBADSTATUS ;
1138- goto finish ;
1139- }
1140-
1141- rc = compression_lzma -> init (NULL );
1142- rc = compression_arm_thumb -> init (NULL );
1143-
1144- if (rc ) {
1145- BOOT_LOG_ERR ("Decompression library fatal error" );
1146- rc = BOOT_EBADSTATUS ;
1147- goto finish ;
1145+ goto finish_without_clean ;
11481146 }
11491147
11501148 write_alignment = flash_area_align (fap_dst );
@@ -1158,12 +1156,28 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
11581156 if (rc ) {
11591157 BOOT_LOG_ERR ("Unable to determine decompressed size of compressed image" );
11601158 rc = BOOT_EBADIMAGE ;
1161- goto finish ;
1159+ goto finish_without_clean ;
11621160 }
11631161
11641162 modified_hdr .ih_flags &= ~COMPRESSIONFLAGS ;
11651163 modified_hdr .ih_img_size = decompressed_image_size ;
11661164
1165+ rc = compression_lzma -> init (NULL , decompressed_image_size );
1166+
1167+ if (rc ) {
1168+ BOOT_LOG_ERR ("Decompression library fatal error" );
1169+ rc = BOOT_EBADSTATUS ;
1170+ goto finish_without_clean ;
1171+ }
1172+
1173+ rc = compression_arm_thumb -> init (NULL , decompressed_image_size );
1174+
1175+ if (rc ) {
1176+ BOOT_LOG_ERR ("Decompression library fatal error" );
1177+ rc = BOOT_EBADSTATUS ;
1178+ goto finish ;
1179+ }
1180+
11671181 /* Calculate protected TLV size for target image once items are removed */
11681182 rc = boot_size_protected_tlvs (hdr , fap_src , & protected_tlv_size );
11691183
@@ -1457,6 +1471,11 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
14571471 }
14581472
14591473finish :
1474+ /* Clean up decompression system */
1475+ (void )compression_lzma -> deinit (NULL );
1476+ (void )compression_arm_thumb -> deinit (NULL );
1477+
1478+ finish_without_clean :
14601479 memset (decomp_buf , 0 , sizeof (decomp_buf ));
14611480
14621481 return rc ;
0 commit comments