@@ -254,15 +254,6 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
254254 goto finish_without_clean ;
255255 }
256256
257- rc = compression_lzma -> init (NULL );
258- rc = compression_arm_thumb -> init (NULL );
259-
260- if (rc ) {
261- BOOT_LOG_ERR ("Decompression library fatal error" );
262- rc = BOOT_EBADSTATUS ;
263- goto finish_without_clean ;
264- }
265-
266257 /* We need a modified header which has the updated sizes, start with the original header */
267258 memcpy (& modified_hdr , hdr , sizeof (modified_hdr ));
268259
@@ -274,12 +265,28 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
274265 if (rc ) {
275266 BOOT_LOG_ERR ("Unable to determine decompressed size of compressed image" );
276267 rc = BOOT_EBADIMAGE ;
277- goto finish ;
268+ goto finish_without_clean ;
278269 }
279270
280271 modified_hdr .ih_flags &= ~COMPRESSIONFLAGS ;
281272 modified_hdr .ih_img_size = decompressed_image_size ;
282273
274+ rc = compression_lzma -> init (NULL , decompressed_image_size );
275+
276+ if (rc ) {
277+ BOOT_LOG_ERR ("Decompression library fatal error" );
278+ rc = BOOT_EBADSTATUS ;
279+ goto finish_without_clean ;
280+ }
281+
282+ rc = compression_arm_thumb -> init (NULL , decompressed_image_size );
283+
284+ if (rc ) {
285+ BOOT_LOG_ERR ("Decompression library fatal error" );
286+ rc = BOOT_EBADSTATUS ;
287+ goto finish ;
288+ }
289+
283290 /* Calculate the protected TLV size, these will not include the decompressed
284291 * sha/size/signature entries
285292 */
@@ -1023,7 +1030,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
10231030 if (rc ) {
10241031 BOOT_LOG_ERR ("Invalid/missing image decrypted compressed size value" );
10251032 rc = BOOT_EBADIMAGE ;
1026- goto finish ;
1033+ goto finish_without_clean ;
10271034 }
10281035
10291036 if (IS_ENCRYPTED (hdr )) {
@@ -1046,7 +1053,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
10461053 */
10471054 BOOT_LOG_ERR ("Invalid image compression flags: no supported compression found" );
10481055 rc = BOOT_EBADIMAGE ;
1049- goto finish ;
1056+ goto finish_without_clean ;
10501057 }
10511058
10521059 compression_lzma = nrf_compress_implementation_find (NRF_COMPRESS_TYPE_LZMA );
@@ -1057,16 +1064,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
10571064 /* Compression library missing or missing required function pointer */
10581065 BOOT_LOG_ERR ("Decompression library fatal error" );
10591066 rc = BOOT_EBADSTATUS ;
1060- goto finish ;
1061- }
1062-
1063- rc = compression_lzma -> init (NULL );
1064- rc = compression_arm_thumb -> init (NULL );
1065-
1066- if (rc ) {
1067- BOOT_LOG_ERR ("Decompression library fatal error" );
1068- rc = BOOT_EBADSTATUS ;
1069- goto finish ;
1067+ goto finish_without_clean ;
10701068 }
10711069
10721070 write_alignment = flash_area_align (fap_dst );
@@ -1078,12 +1076,28 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
10781076 if (rc ) {
10791077 BOOT_LOG_ERR ("Unable to determine decompressed size of compressed image" );
10801078 rc = BOOT_EBADIMAGE ;
1081- goto finish ;
1079+ goto finish_without_clean ;
10821080 }
10831081
10841082 modified_hdr .ih_flags &= ~COMPRESSIONFLAGS ;
10851083 modified_hdr .ih_img_size = decompressed_image_size ;
10861084
1085+ rc = compression_lzma -> init (NULL , decompressed_image_size );
1086+
1087+ if (rc ) {
1088+ BOOT_LOG_ERR ("Decompression library fatal error" );
1089+ rc = BOOT_EBADSTATUS ;
1090+ goto finish_without_clean ;
1091+ }
1092+
1093+ rc = compression_arm_thumb -> init (NULL , decompressed_image_size );
1094+
1095+ if (rc ) {
1096+ BOOT_LOG_ERR ("Decompression library fatal error" );
1097+ rc = BOOT_EBADSTATUS ;
1098+ goto finish ;
1099+ }
1100+
10871101 /* Calculate protected TLV size for target image once items are removed */
10881102 rc = boot_size_protected_tlvs (hdr , fap_src , & protected_tlv_size );
10891103
@@ -1400,6 +1414,11 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
14001414 }
14011415
14021416finish :
1417+ /* Clean up decompression system */
1418+ (void )compression_lzma -> deinit (NULL );
1419+ (void )compression_arm_thumb -> deinit (NULL );
1420+
1421+ finish_without_clean :
14031422 memset (decomp_buf , 0 , sizeof (decomp_buf ));
14041423
14051424 return rc ;
0 commit comments