Commit e38b0afa authored by Andrzej Puzdrowski's avatar Andrzej Puzdrowski
Browse files

loader: Allow image header bigger than 1 KB for encrypted images

boot_copy_region() was written so it assumes that the image header
must fit int the intermediary buffer of 1 KB size. A bigger header
will cause a overflow in calculation of size of data chunk to be
decrypted.

This patch allow to use header bigger than that buffer size and
mitigate the limitation described above.

Signed-off-by: default avatarAndrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
parent 21f0376f
Loading
Loading
Loading
Loading
+15 −4
Changes for boot/bootutil/src/loader.c: 15 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -950,16 +950,26 @@ boot_copy_region(struct boot_loader_state *state,
            }
#endif
            if (IS_ENCRYPTED(hdr)) {
                blk_sz = chunk_sz;
                idx = 0;
                if (off + bytes_copied < hdr->ih_hdr_size) {
                    /* do not decrypt header */
                    if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size) {
                        /* The lower part of the chunk contains header data */
                        blk_off = 0;
                    blk_sz = chunk_sz - hdr->ih_hdr_size;
                    idx = hdr->ih_hdr_size;
                        blk_sz = chunk_sz - (hdr->ih_hdr_size - off -
                                 bytes_copied);
                        idx = hdr->ih_hdr_size  - off - bytes_copied;
                    } else {
                        /* The chunk contains exclusively header data */
                        blk_sz = 0; /* nothing to decrypt */
                    }
                } else {
                    idx = 0;
                    blk_sz = chunk_sz;
                    blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
                }

                if (blk_sz > 0)
                {
                    tlv_off = BOOT_TLV_OFF(hdr);
                    if (off + bytes_copied + chunk_sz > tlv_off) {
                        /* do not decrypt TLVs */
@@ -974,6 +984,7 @@ boot_copy_region(struct boot_loader_state *state,
                            blk_off, &buf[idx]);
                }
            }
        }
#endif

        rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);