@@ -256,15 +256,6 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
256
256
goto finish_without_clean ;
257
257
}
258
258
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
-
268
259
/* We need a modified header which has the updated sizes, start with the original header */
269
260
memcpy (& modified_hdr , hdr , sizeof (modified_hdr ));
270
261
@@ -276,12 +267,28 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
276
267
if (rc ) {
277
268
BOOT_LOG_ERR ("Unable to determine decompressed size of compressed image" );
278
269
rc = BOOT_EBADIMAGE ;
279
- goto finish ;
270
+ goto finish_without_clean ;
280
271
}
281
272
282
273
modified_hdr .ih_flags &= ~COMPRESSIONFLAGS ;
283
274
modified_hdr .ih_img_size = decompressed_image_size ;
284
275
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
+
285
292
/* Calculate the protected TLV size, these will not include the decompressed
286
293
* sha/size/signature entries
287
294
*/
@@ -1101,7 +1108,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
1101
1108
if (rc ) {
1102
1109
BOOT_LOG_ERR ("Invalid/missing image decrypted compressed size value" );
1103
1110
rc = BOOT_EBADIMAGE ;
1104
- goto finish ;
1111
+ goto finish_without_clean ;
1105
1112
}
1106
1113
1107
1114
if (IS_ENCRYPTED (hdr )) {
@@ -1124,7 +1131,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
1124
1131
*/
1125
1132
BOOT_LOG_ERR ("Invalid image compression flags: no supported compression found" );
1126
1133
rc = BOOT_EBADIMAGE ;
1127
- goto finish ;
1134
+ goto finish_without_clean ;
1128
1135
}
1129
1136
1130
1137
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
1135
1142
/* Compression library missing or missing required function pointer */
1136
1143
BOOT_LOG_ERR ("Decompression library fatal error" );
1137
1144
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 ;
1148
1146
}
1149
1147
1150
1148
write_alignment = flash_area_align (fap_dst );
@@ -1158,12 +1156,28 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
1158
1156
if (rc ) {
1159
1157
BOOT_LOG_ERR ("Unable to determine decompressed size of compressed image" );
1160
1158
rc = BOOT_EBADIMAGE ;
1161
- goto finish ;
1159
+ goto finish_without_clean ;
1162
1160
}
1163
1161
1164
1162
modified_hdr .ih_flags &= ~COMPRESSIONFLAGS ;
1165
1163
modified_hdr .ih_img_size = decompressed_image_size ;
1166
1164
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
+
1167
1181
/* Calculate protected TLV size for target image once items are removed */
1168
1182
rc = boot_size_protected_tlvs (hdr , fap_src , & protected_tlv_size );
1169
1183
@@ -1457,6 +1471,11 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
1457
1471
}
1458
1472
1459
1473
finish :
1474
+ /* Clean up decompression system */
1475
+ (void )compression_lzma -> deinit (NULL );
1476
+ (void )compression_arm_thumb -> deinit (NULL );
1477
+
1478
+ finish_without_clean :
1460
1479
memset (decomp_buf , 0 , sizeof (decomp_buf ));
1461
1480
1462
1481
return rc ;
0 commit comments