Skip to content

Commit

Permalink
boot: Simplify copy loop in boot_copy_region
Browse files Browse the repository at this point in the history
Move checking of conditions, which remain the same for the whole
loop run, outside of the loop.

Signed-off-by: Dominik Ermel <[email protected]>
  • Loading branch information
de-nordic authored and nvlsianpu committed Aug 6, 2024
1 parent 7e69047 commit 6fe259b
Showing 1 changed file with 61 additions and 49 deletions.
110 changes: 61 additions & 49 deletions boot/bootutil/src/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -1246,56 +1273,41 @@ 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;
/* 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 */
if (abs_off + 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 - abs_off);
idx = hdr->ih_hdr_size - abs_off;
} else {
/* The chunk contains exclusively header data */
blk_sz = 0; /* nothing to decrypt */
}
} else {
/* Need decryption, metadata from the secondary slot */
hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
source_slot = 1;
idx = 0;
blk_sz = chunk_sz;
blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
}

if (IS_ENCRYPTED(hdr)) {
uint32_t abs_off = off + bytes_copied;
if (abs_off < hdr->ih_hdr_size) {
/* do not decrypt header */
if (abs_off + 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 - abs_off);
idx = hdr->ih_hdr_size - abs_off;
if (blk_sz > 0)
{
tlv_off = BOOT_TLV_OFF(hdr);
if (abs_off + chunk_sz > tlv_off) {
/* do not decrypt TLVs */
if (abs_off >= tlv_off) {
blk_sz = 0;
} else {
/* The chunk contains exclusively header data */
blk_sz = 0; /* nothing to decrypt */
}
} else {
idx = 0;
blk_sz = chunk_sz;
blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
}

if (blk_sz > 0) {
tlv_off = BOOT_TLV_OFF(hdr);
if (abs_off + chunk_sz > tlv_off) {
/* do not decrypt TLVs */
if (abs_off >= tlv_off) {
blk_sz = 0;
} else {
blk_sz = tlv_off - abs_off;
}
blk_sz = tlv_off - abs_off;
}
boot_encrypt(BOOT_CURR_ENC(state), source_slot,
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
blk_off, &buf[idx]);
}
boot_encrypt(BOOT_CURR_ENC(state), source_slot,
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
blk_off, &buf[idx]);
}
}
#endif
Expand Down

0 comments on commit 6fe259b

Please sign in to comment.