Skip to content

Commit 7f9ac97

Browse files
de-nordicnvlsianpu
authored andcommitted
boot: Change boot_enc_load to take slot number instead of image
In all cases where boot_enc_load is called it is known what slot is addressed, so it is better to just pass the slot number instead of making the boot_enc_load figure out slot number from image index and provided flash area object. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 71120e4 commit 7f9ac97

File tree

4 files changed

+13
-25
lines changed

4 files changed

+13
-25
lines changed

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ boot_image_validate_encrypted(const struct flash_area *fa_p,
3636
memset(&boot_data, 0, sizeof(struct boot_loader_state));
3737
image_index = BOOT_CURR_IMG(state);
3838
if(IS_ENCRYPTED(hdr)) {
39-
rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fa_p, bs);
39+
rc = boot_enc_load(BOOT_CURR_ENC(state), 1, hdr, fa_p, bs);
4040
if (rc < 0) {
4141
FIH_RET(fih_rc);
4242
}
@@ -218,7 +218,6 @@ decrypt_image_inplace(const struct flash_area *fa_p,
218218
size_t sect_size;
219219
size_t sect_count;
220220
size_t sect;
221-
uint8_t image_index;
222221
struct flash_sector sector;
223222

224223
memset(&boot_data, 0, sizeof(struct boot_loader_state));
@@ -228,8 +227,6 @@ decrypt_image_inplace(const struct flash_area *fa_p,
228227
rc = flash_area_get_sector(fa_p, boot_status_off(fa_p), &sector);
229228

230229

231-
image_index = BOOT_CURR_IMG(state);
232-
233230
if(IS_ENCRYPTED(hdr)) {
234231
#if 0 //Skip this step?, the image will just not boot if it's not decrypted properly
235232
static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
@@ -241,7 +238,7 @@ decrypt_image_inplace(const struct flash_area *fa_p,
241238
#endif
242239
memset(&boot_data, 0, sizeof(struct boot_loader_state));
243240
/* Load the encryption keys into cache */
244-
rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fa_p, bs);
241+
rc = boot_enc_load(BOOT_CURR_ENC(state), 0, hdr, fa_p, bs);
245242
if (rc < 0) {
246243
FIH_RET(fih_rc);
247244
}

boot/bootutil/include/bootutil/enc_key.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int boot_enc_init(struct enc_key_data *enc_state, uint8_t slot);
5555
int boot_enc_drop(struct enc_key_data *enc_state, uint8_t slot);
5656
int boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot,
5757
const struct boot_status *bs);
58-
int boot_enc_load(struct enc_key_data *enc_state, int image_index,
58+
int boot_enc_load(struct enc_key_data *enc_state, int slot,
5959
const struct image_header *hdr, const struct flash_area *fap,
6060
struct boot_status *bs);
6161
bool boot_enc_valid(struct enc_key_data *enc_state, int image_index,

boot/bootutil/src/encrypted.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
607607
* Load encryption key.
608608
*/
609609
int
610-
boot_enc_load(struct enc_key_data *enc_state, int image_index,
610+
boot_enc_load(struct enc_key_data *enc_state, int slot,
611611
const struct image_header *hdr, const struct flash_area *fap,
612612
struct boot_status *bs)
613613
{
@@ -619,15 +619,8 @@ boot_enc_load(struct enc_key_data *enc_state, int image_index,
619619
#else
620620
uint8_t buf[EXPECTED_ENC_LEN];
621621
#endif
622-
uint8_t slot;
623622
int rc;
624623

625-
rc = flash_area_id_to_multi_image_slot(image_index, flash_area_get_id(fap));
626-
if (rc < 0) {
627-
return rc;
628-
}
629-
slot = rc;
630-
631624
/* Already loaded... */
632625
if (enc_state[slot].valid) {
633626
return 1;

boot/bootutil/src/loader.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,6 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
781781
const struct flash_area *fap, struct boot_status *bs)
782782
{
783783
TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
784-
uint8_t image_index;
785784
int rc;
786785
FIH_DECLARE(fih_rc, FIH_FAILURE);
787786

@@ -792,13 +791,11 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
792791
(void)bs;
793792
(void)rc;
794793

795-
image_index = BOOT_CURR_IMG(state);
796-
797794
/* In the case of ram loading the image has already been decrypted as it is
798795
* decrypted when copied in ram */
799796
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
800-
if (MUST_DECRYPT(fap, image_index, hdr)) {
801-
rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
797+
if (MUST_DECRYPT(fap, BOOT_CURR_IMG(state), hdr)) {
798+
rc = boot_enc_load(BOOT_CURR_ENC(state), 1, hdr, fap, bs);
802799
if (rc < 0) {
803800
FIH_RET(fih_rc);
804801
}
@@ -808,8 +805,9 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
808805
}
809806
#endif
810807

811-
FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
812-
hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
808+
FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state),
809+
BOOT_CURR_IMG(state), hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
810+
NULL, 0, NULL);
813811

814812
FIH_RET(fih_rc);
815813
}
@@ -1403,7 +1401,7 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
14031401

14041402
#ifdef MCUBOOT_ENC_IMAGES
14051403
if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
1406-
rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
1404+
rc = boot_enc_load(BOOT_CURR_ENC(state), BOOT_SECONDARY_SLOT,
14071405
boot_img_hdr(state, BOOT_SECONDARY_SLOT),
14081406
fap_secondary_slot, bs);
14091407

@@ -1527,7 +1525,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
15271525
#ifdef MCUBOOT_ENC_IMAGES
15281526
if (IS_ENCRYPTED(hdr)) {
15291527
fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
1530-
rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
1528+
rc = boot_enc_load(BOOT_CURR_ENC(state), 0, hdr, fap, bs);
15311529
assert(rc >= 0);
15321530

15331531
if (rc == 0) {
@@ -1551,7 +1549,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
15511549
hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
15521550
if (IS_ENCRYPTED(hdr)) {
15531551
fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
1554-
rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
1552+
rc = boot_enc_load(BOOT_CURR_ENC(state), 1, hdr, fap, bs);
15551553
assert(rc >= 0);
15561554

15571555
if (rc == 0) {
@@ -2750,7 +2748,7 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
27502748
goto done;
27512749
}
27522750

2753-
rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2751+
rc = boot_enc_load(BOOT_CURR_ENC(state), slot, hdr, fap_src, &bs);
27542752
if (rc < 0) {
27552753
goto done;
27562754
}

0 commit comments

Comments
 (0)