@@ -254,15 +254,6 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
254
254
goto finish_without_clean ;
255
255
}
256
256
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
-
266
257
/* We need a modified header which has the updated sizes, start with the original header */
267
258
memcpy (& modified_hdr , hdr , sizeof (modified_hdr ));
268
259
@@ -274,12 +265,28 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
274
265
if (rc ) {
275
266
BOOT_LOG_ERR ("Unable to determine decompressed size of compressed image" );
276
267
rc = BOOT_EBADIMAGE ;
277
- goto finish ;
268
+ goto finish_without_clean ;
278
269
}
279
270
280
271
modified_hdr .ih_flags &= ~COMPRESSIONFLAGS ;
281
272
modified_hdr .ih_img_size = decompressed_image_size ;
282
273
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
+
283
290
/* Calculate the protected TLV size, these will not include the decompressed
284
291
* sha/size/signature entries
285
292
*/
@@ -1023,7 +1030,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
1023
1030
if (rc ) {
1024
1031
BOOT_LOG_ERR ("Invalid/missing image decrypted compressed size value" );
1025
1032
rc = BOOT_EBADIMAGE ;
1026
- goto finish ;
1033
+ goto finish_without_clean ;
1027
1034
}
1028
1035
1029
1036
if (IS_ENCRYPTED (hdr )) {
@@ -1046,7 +1053,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
1046
1053
*/
1047
1054
BOOT_LOG_ERR ("Invalid image compression flags: no supported compression found" );
1048
1055
rc = BOOT_EBADIMAGE ;
1049
- goto finish ;
1056
+ goto finish_without_clean ;
1050
1057
}
1051
1058
1052
1059
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
1057
1064
/* Compression library missing or missing required function pointer */
1058
1065
BOOT_LOG_ERR ("Decompression library fatal error" );
1059
1066
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 ;
1070
1068
}
1071
1069
1072
1070
write_alignment = flash_area_align (fap_dst );
@@ -1078,12 +1076,28 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
1078
1076
if (rc ) {
1079
1077
BOOT_LOG_ERR ("Unable to determine decompressed size of compressed image" );
1080
1078
rc = BOOT_EBADIMAGE ;
1081
- goto finish ;
1079
+ goto finish_without_clean ;
1082
1080
}
1083
1081
1084
1082
modified_hdr .ih_flags &= ~COMPRESSIONFLAGS ;
1085
1083
modified_hdr .ih_img_size = decompressed_image_size ;
1086
1084
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
+
1087
1101
/* Calculate protected TLV size for target image once items are removed */
1088
1102
rc = boot_size_protected_tlvs (hdr , fap_src , & protected_tlv_size );
1089
1103
@@ -1400,6 +1414,11 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
1400
1414
}
1401
1415
1402
1416
finish :
1417
+ /* Clean up decompression system */
1418
+ (void )compression_lzma -> deinit (NULL );
1419
+ (void )compression_arm_thumb -> deinit (NULL );
1420
+
1421
+ finish_without_clean :
1403
1422
memset (decomp_buf , 0 , sizeof (decomp_buf ));
1404
1423
1405
1424
return rc ;
0 commit comments