Skip to content

Commit f581923

Browse files
committed
[nrf noup] compression: Fix non-PM configurations
If the partition manager is not used, the area between the MCUboot header and the image is no longer filled with 0xFFs, but with 0x00s. Align the decompression logic, so the 0x00s are written into the NVM as part of the image copy operation, fixing the issue with incorrect digest value in compressed images. Ref: NCSDK-37697 Signed-off-by: Tomasz Chyrowicz <tomasz.chyrowicz@nordicsemi.no>
1 parent b66dad3 commit f581923

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

boot/zephyr/decompression.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,9 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
10891089
TARGET_STATIC uint8_t decomp_buf[DECOMP_BUF_ALLOC_SIZE] __attribute__((aligned(4)));
10901090
TARGET_STATIC struct image_header modified_hdr;
10911091
uint16_t decomp_buf_max_size;
1092+
#ifndef CONFIG_PARTITION_MANAGER_ENABLED
1093+
uint32_t hdr_write_pos = 0;
1094+
#endif
10921095

10931096
#if defined(CONFIG_NRF_COMPRESS_ARM_THUMB)
10941097
uint8_t unaligned_data_length = 0;
@@ -1206,6 +1209,40 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
12061209
goto finish;
12071210
}
12081211

1212+
#ifndef CONFIG_PARTITION_MANAGER_ENABLED
1213+
/* The image digest is calculated on a binary, that has the area between header and the image
1214+
* binary filled with zeros.
1215+
* The zeros are a result of building the image with the CONFIG_ROM_START_OFFSET option set to
1216+
* the ih_hdr_size.
1217+
* The configuration with partition manager leaves this area uninitialized and signs
1218+
* the image using the --pad-header option, which fills the gap with the erase value
1219+
* (usually 0xFF) instead of zeros.
1220+
*/
1221+
1222+
/* Reuse decompression buffer to write zeros. */
1223+
memset(decomp_buf, 0x00, sizeof(decomp_buf));
1224+
hdr_write_pos = sizeof(modified_hdr);
1225+
1226+
while (hdr_write_pos < hdr->ih_hdr_size) {
1227+
uint32_t set_size = hdr->ih_hdr_size - hdr_write_pos;
1228+
1229+
/* Assuming that sizeof(modified_hdr) is always aligned to write block size. */
1230+
if (set_size > sizeof(modified_hdr)) {
1231+
set_size = sizeof(modified_hdr);
1232+
}
1233+
1234+
rc = flash_area_write(fap_dst, off_dst + hdr_write_pos, decomp_buf, set_size);
1235+
if (rc != 0) {
1236+
BOOT_LOG_ERR("Flash write failed at offset: 0x%x, size: 0x%x, area: %d, rc: %d",
1237+
(off_dst + hdr_write_pos), set_size, fap_dst->fa_id, rc);
1238+
rc = BOOT_EFLASH;
1239+
goto finish;
1240+
}
1241+
1242+
hdr_write_pos += set_size;
1243+
}
1244+
#endif
1245+
12091246
/* Read in, decompress and write out data */
12101247
#ifdef MCUBOOT_ENC_IMAGES
12111248
while (pos < comp_size) {

0 commit comments

Comments
 (0)