Commit 6fe259b1 authored by Dominik Ermel's avatar Dominik Ermel Committed by Andrzej Puzdrowski
Browse files

boot: Simplify copy loop in boot_copy_region

Move checking of conditions, which remain the same for the whole
loop run, outside of the loop.

Signed-off-by: default avatarDominik Ermel <dominik.ermel@nordicsemi.no>
parent 7e69047b
Loading
Loading
Loading
Loading
+37 −25
Changes for boot/bootutil/src/loader.c: 37 added lines, 25 removed lines.
Original line number Diff line number Diff line
@@ -1213,23 +1213,50 @@ boot_copy_region(struct boot_loader_state *state,
    int chunk_sz;
    int rc;
#ifdef MCUBOOT_ENC_IMAGES
    uint32_t off;
    uint32_t off = off_dst;
    uint32_t tlv_off;
    size_t blk_off;
    struct image_header *hdr;
    uint16_t idx;
    uint32_t blk_sz;
    uint8_t image_index;
    uint8_t image_index = BOOT_CURR_IMG(state);
    bool encrypted_src;
    bool encrypted_dst;
    /* Assuming the secondary slot is source and needs decryption */
    /* Assuming the secondary slot is source; note that 0 here not only
     * means that primary slot is source, but also that there will be
     * encryption happening, if it is 1 then there is decryption from
     * secondary slot.
     */
    int source_slot = 1;
    /* In case of encryption enabled, we may have to do more work than
     * just copy bytes */
    bool only_copy = false;
#else
    (void)state;
#endif

    TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));

#if !defined(MCUBOOT_ENC_IMAGES)
    (void)state;
#ifdef MCUBOOT_ENC_IMAGES
    encrypted_src = (flash_area_get_id(fap_src) != FLASH_AREA_IMAGE_PRIMARY(image_index));
    encrypted_dst = (flash_area_get_id(fap_dst) != FLASH_AREA_IMAGE_PRIMARY(image_index));

    if (encrypted_src != encrypted_dst) {
        if (encrypted_dst) {
            /* Need encryption, metadata from the primary slot */
            hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
            source_slot = 0;
        } else {
            /* Need decryption, metadata from the secondary slot */
            hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
            source_slot = 1;
        }
    } else {
        /* In case when source and targe is the same area, this means that we
         * only have to copy bytes, no encryption or decryption.
         */
        only_copy = true;
    }
#endif

    bytes_copied = 0;
@@ -1246,24 +1273,9 @@ boot_copy_region(struct boot_loader_state *state,
        }

#ifdef MCUBOOT_ENC_IMAGES
        image_index = BOOT_CURR_IMG(state);
        encrypted_src = (flash_area_get_id(fap_src) != FLASH_AREA_IMAGE_PRIMARY(image_index));
        encrypted_dst = (flash_area_get_id(fap_dst) != FLASH_AREA_IMAGE_PRIMARY(image_index));

        if (encrypted_src != encrypted_dst) {
            off = off_dst;

            if (encrypted_dst) {
                /* Need encryption, metadata from the primary slot */
                hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
                source_slot = 0;
            } else {
                /* Need decryption, metadata from the secondary slot */
                hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
                source_slot = 1;
            }

            if (IS_ENCRYPTED(hdr)) {
        /* If only copy, then does not matter if header indicates need for
         * encryptio/decryptio, we just copy data. */
        if (!only_copy && IS_ENCRYPTED(hdr)) {
            uint32_t abs_off = off + bytes_copied;
            if (abs_off < hdr->ih_hdr_size) {
                /* do not decrypt header */
@@ -1282,7 +1294,8 @@ boot_copy_region(struct boot_loader_state *state,
                blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
            }

                if (blk_sz > 0) {
            if (blk_sz > 0)
            {
                tlv_off = BOOT_TLV_OFF(hdr);
                if (abs_off + chunk_sz > tlv_off) {
                    /* do not decrypt TLVs */
@@ -1297,7 +1310,6 @@ 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);