Skip to content

Commit a18f635

Browse files
taltenbachnordicjm
authored andcommitted
boot: Remove now superfluous start_off parameters
When swap-offset is used, multiple routines called directly or indirectly by bs_list or bs_set needed to know the start offset of the image in the secondary slot. When a valid bootloader state is available, this value is simply retrieved from the state. Since bs_list and bs_set had no valid state, a start_off parameter was added to all those routines to obtain the start offset without using the bootloader state. This is no longer needed, and since the start_off parameters were used only by bs_list and bs_set, they can be removed, which make the code simpler and easier to read in numerous places. Signed-off-by: Thomas Altenbach <[email protected]>
1 parent c62c19d commit a18f635

File tree

8 files changed

+6
-111
lines changed

8 files changed

+6
-111
lines changed

boot/boot_serial/include/boot_serial/boot_serial_encryption.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ fih_ret
2323
boot_image_validate_encrypted(const struct flash_area *fa_p,
2424
struct image_header *hdr, uint8_t *buf,
2525
uint16_t buf_size
26-
#ifdef MCUBOOT_SWAP_USING_OFFSET
27-
, uint32_t start_off
28-
#endif
2926
);
3027

3128
/**

boot/boot_serial/src/boot_serial.c

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ bs_list(struct boot_loader_state *state, char *buf, int len)
327327
#ifdef MCUBOOT_SWAP_USING_OFFSET
328328
if (slot == BOOT_SECONDARY_SLOT && swap_status != BOOT_SWAP_TYPE_REVERT) {
329329
start_off = boot_img_sector_size(state, slot, 0);
330+
state->secondary_offset[image_index] = start_off;
330331
}
331332
#endif
332333

@@ -351,13 +352,8 @@ bs_list(struct boot_loader_state *state, char *buf, int len)
351352
#if defined(MCUBOOT_ENC_IMAGES)
352353
#if !defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
353354
if (IS_ENCRYPTED(&hdr) && MUST_DECRYPT(fap, image_index, &hdr)) {
354-
#ifdef MCUBOOT_SWAP_USING_OFFSET
355-
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
356-
&hdr, tmpbuf, sizeof(tmpbuf), start_off);
357-
#else
358355
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
359356
&hdr, tmpbuf, sizeof(tmpbuf));
360-
#endif
361357
} else {
362358
#endif
363359
if (IS_ENCRYPTED(&hdr)) {
@@ -369,14 +365,8 @@ bs_list(struct boot_loader_state *state, char *buf, int len)
369365
hdr.ih_flags &= ~ENCRYPTIONFLAGS;
370366
}
371367
#endif
372-
373-
#ifdef MCUBOOT_SWAP_USING_OFFSET
374-
FIH_CALL(bootutil_img_validate, fih_rc, state, &hdr,
375-
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL, start_off);
376-
#else
377368
FIH_CALL(bootutil_img_validate, fih_rc, state, &hdr,
378369
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
379-
#endif
380370
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
381371
}
382372
#endif
@@ -557,6 +547,7 @@ bs_set(struct boot_loader_state *state, char *buf, int len)
557547
#ifdef MCUBOOT_SWAP_USING_OFFSET
558548
if (slot == BOOT_SECONDARY_SLOT && swap_status != BOOT_SWAP_TYPE_REVERT) {
559549
start_off = boot_img_sector_size(state, slot, 0);
550+
state->secondary_offset[image_index] = start_off;
560551
}
561552
#endif
562553

@@ -582,22 +573,12 @@ bs_set(struct boot_loader_state *state, char *buf, int len)
582573
{
583574
#ifdef MCUBOOT_ENC_IMAGES
584575
if (IS_ENCRYPTED(&hdr)) {
585-
#ifdef MCUBOOT_SWAP_USING_OFFSET
586-
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
587-
&hdr, tmpbuf, sizeof(tmpbuf), start_off);
588-
#else
589576
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
590577
&hdr, tmpbuf, sizeof(tmpbuf));
591-
#endif
592578
} else {
593579
#endif
594-
#ifdef MCUBOOT_SWAP_USING_OFFSET
595-
FIH_CALL(bootutil_img_validate, fih_rc, state, &hdr,
596-
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL, start_off);
597-
#else
598580
FIH_CALL(bootutil_img_validate, fih_rc, state, &hdr,
599581
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
600-
#endif
601582
#ifdef MCUBOOT_ENC_IMAGES
602583
}
603584
#endif

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ BOOT_LOG_MODULE_DECLARE(serial_encryption);
2121
fih_ret
2222
boot_image_validate_encrypted(const struct flash_area *fa_p,
2323
struct image_header *hdr, uint8_t *buf,
24-
uint16_t buf_size
25-
#ifdef MCUBOOT_SWAP_USING_OFFSET
26-
, uint32_t start_off
27-
#endif
28-
)
24+
uint16_t buf_size)
2925
{
3026
FIH_DECLARE(fih_rc, FIH_FAILURE);
3127

@@ -37,11 +33,7 @@ boot_image_validate_encrypted(const struct flash_area *fa_p,
3733

3834
memset(&boot_data, 0, sizeof(struct boot_loader_state));
3935
if(IS_ENCRYPTED(hdr)) {
40-
#ifdef MCUBOOT_SWAP_USING_OFFSET
41-
rc = boot_enc_load(state, 1, hdr, fa_p, bs, start_off);
42-
#else
4336
rc = boot_enc_load(state, 1, hdr, fa_p, bs);
44-
#endif
4537
if (rc < 0) {
4638
FIH_RET(fih_rc);
4739
}
@@ -51,13 +43,8 @@ boot_image_validate_encrypted(const struct flash_area *fa_p,
5143
}
5244
}
5345

54-
#ifdef MCUBOOT_SWAP_USING_OFFSET
55-
FIH_CALL(bootutil_img_validate, fih_rc, state,
56-
hdr, fa_p, buf, buf_size, NULL, 0, NULL, start_off);
57-
#else
5846
FIH_CALL(bootutil_img_validate, fih_rc, state,
5947
hdr, fa_p, buf, buf_size, NULL, 0, NULL);
60-
#endif
6148

6249
FIH_RET(fih_rc);
6350
}
@@ -248,11 +235,7 @@ decrypt_image_inplace(const struct flash_area *fa_p,
248235
#endif
249236
memset(&boot_data, 0, sizeof(struct boot_loader_state));
250237
/* Load the encryption keys into cache */
251-
#ifdef MCUBOOT_SWAP_USING_OFFSET
252-
rc = boot_enc_load(state, 0, hdr, fa_p, bs, 0);
253-
#else
254238
rc = boot_enc_load(state, 0, hdr, fa_p, bs);
255-
#endif
256239
if (rc < 0) {
257240
FIH_RET(fih_rc);
258241
}

boot/bootutil/include/bootutil/enc_key.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ int boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot,
6767
const struct boot_status *bs);
6868
int boot_enc_load(struct boot_loader_state *state, int slot,
6969
const struct image_header *hdr, const struct flash_area *fap,
70-
struct boot_status *bs
71-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
72-
, uint32_t start_off
73-
#endif
74-
);
70+
struct boot_status *bs);
7571
bool boot_enc_valid(struct enc_key_data *enc_state, int slot);
7672
void boot_enc_encrypt(struct enc_key_data *enc_state, int slot,
7773
uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);

boot/bootutil/include/bootutil/image.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,6 @@ fih_ret bootutil_img_validate(struct boot_loader_state *state,
208208
const struct flash_area *fap,
209209
uint8_t *tmp_buf, uint32_t tmp_buf_sz,
210210
uint8_t *seed, int seed_len, uint8_t *out_hash
211-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
212-
, uint32_t start_off
213-
#endif
214211
);
215212

216213
struct image_tlv_iter {

boot/bootutil/src/encrypted.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -585,11 +585,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
585585
int
586586
boot_enc_load(struct boot_loader_state *state, int slot,
587587
const struct image_header *hdr, const struct flash_area *fap,
588-
struct boot_status *bs
589-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
590-
, uint32_t start_off
591-
#endif
592-
)
588+
struct boot_status *bs)
593589
{
594590
struct enc_key_data *enc_state = BOOT_CURR_ENC(state);
595591
uint32_t off;
@@ -614,11 +610,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
614610
boot_enc_init(enc_state, slot);
615611

616612
#if defined(MCUBOOT_SWAP_USING_OFFSET)
617-
#if defined(MCUBOOT_SERIAL_RECOVERY)
618-
it.start_off = boot_get_state_secondary_offset(state, fap) + start_off;
619-
#else
620613
it.start_off = boot_get_state_secondary_offset(state, fap);
621-
#endif
622614
#endif
623615

624616
rc = bootutil_tlv_iter_begin(&it, hdr, fap, BOOT_ENC_TLV, false);

boot/bootutil/src/image_validate.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ bootutil_img_hash(struct boot_loader_state *state,
7272
struct image_header *hdr, const struct flash_area *fap,
7373
uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
7474
uint8_t *seed, int seed_len
75-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
76-
, uint32_t start_offset
77-
#endif
7875
)
7976
{
8077
bootutil_sha_context sha_ctx;
@@ -133,11 +130,7 @@ bootutil_img_hash(struct boot_loader_state *state,
133130
/* For swap using offset mode, the image starts in the second sector of the upgrade slot, so
134131
* apply the offset when this is needed
135132
*/
136-
#if defined(MCUBOOT_SERIAL_RECOVERY)
137-
sector_off = boot_get_state_secondary_offset(state, fap) + start_offset;
138-
#else
139133
sector_off = boot_get_state_secondary_offset(state, fap);
140-
#endif
141134
#endif
142135

143136
bootutil_sha_init(&sha_ctx);
@@ -493,9 +486,6 @@ bootutil_img_validate(struct boot_loader_state *state,
493486
struct image_header *hdr, const struct flash_area *fap,
494487
uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
495488
int seed_len, uint8_t *out_hash
496-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
497-
, uint32_t start_offset
498-
#endif
499489
)
500490
{
501491
#if (defined(EXPECTED_KEY_TLV) && defined(MCUBOOT_HW_KEY)) || defined(MCUBOOT_HW_ROLLBACK_PROT)
@@ -536,12 +526,7 @@ bootutil_img_validate(struct boot_loader_state *state,
536526
BOOT_LOG_DBG("bootutil_img_validate: flash area %p", fap);
537527

538528
#if defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE)
539-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
540-
rc = bootutil_img_hash(state, hdr, fap, tmp_buf, tmp_buf_sz, hash, seed, seed_len,
541-
start_offset);
542-
#else
543529
rc = bootutil_img_hash(state, hdr, fap, tmp_buf, tmp_buf_sz, hash, seed, seed_len);
544-
#endif
545530
if (rc) {
546531
goto out;
547532
}
@@ -561,11 +546,7 @@ bootutil_img_validate(struct boot_loader_state *state,
561546
#endif
562547

563548
#if defined(MCUBOOT_SWAP_USING_OFFSET)
564-
#if defined(MCUBOOT_SERIAL_RECOVERY)
565-
it.start_off = boot_get_state_secondary_offset(state, fap) + start_offset;
566-
#else
567549
it.start_off = boot_get_state_secondary_offset(state, fap);
568-
#endif
569550
#endif
570551

571552
rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);

boot/bootutil/src/loader.c

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -772,11 +772,7 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
772772
*/
773773
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
774774
if (MUST_DECRYPT(fap, BOOT_CURR_IMG(state), hdr)) {
775-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
776-
rc = boot_enc_load(state, 1, hdr, fap, bs, 0);
777-
#else
778775
rc = boot_enc_load(state, 1, hdr, fap, bs);
779-
#endif
780776
if (rc < 0) {
781777
FIH_RET(fih_rc);
782778
}
@@ -786,13 +782,8 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
786782
}
787783
#endif
788784

789-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
790-
FIH_CALL(bootutil_img_validate, fih_rc, state, hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
791-
NULL, 0, NULL, 0);
792-
#else
793785
FIH_CALL(bootutil_img_validate, fih_rc, state, hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
794786
NULL, 0, NULL);
795-
#endif
796787

797788
FIH_RET(fih_rc);
798789
}
@@ -815,24 +806,15 @@ split_image_check(struct image_header *app_hdr,
815806
}
816807
}
817808

818-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
819-
FIH_CALL(bootutil_img_validate, fih_rc, NULL, loader_hdr, loader_fap,
820-
tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash, 0);
821-
#else
822809
FIH_CALL(bootutil_img_validate, fih_rc, NULL, loader_hdr, loader_fap,
823810
tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
824-
#endif
811+
825812
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
826813
FIH_RET(fih_rc);
827814
}
828815

829-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
830-
FIH_CALL(bootutil_img_validate, fih_rc, NULL, app_hdr, app_fap,
831-
tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL, 0);
832-
#else
833816
FIH_CALL(bootutil_img_validate, fih_rc, NULL, app_hdr, app_fap,
834817
tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
835-
#endif
836818

837819
out:
838820
FIH_RET(fih_rc);
@@ -1620,15 +1602,9 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
16201602

16211603
#ifdef MCUBOOT_ENC_IMAGES
16221604
if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
1623-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
1624-
rc = boot_enc_load(state, BOOT_SECONDARY_SLOT,
1625-
boot_img_hdr(state, BOOT_SECONDARY_SLOT),
1626-
fap_secondary_slot, bs, 0);
1627-
#else
16281605
rc = boot_enc_load(state, BOOT_SECONDARY_SLOT,
16291606
boot_img_hdr(state, BOOT_SECONDARY_SLOT),
16301607
fap_secondary_slot, bs);
1631-
#endif
16321608

16331609
if (rc < 0) {
16341610
return BOOT_EBADIMAGE;
@@ -1751,11 +1727,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
17511727
#ifdef MCUBOOT_ENC_IMAGES
17521728
if (IS_ENCRYPTED(hdr)) {
17531729
fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
1754-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
1755-
rc = boot_enc_load(state, 0, hdr, fap, bs, 0);
1756-
#else
17571730
rc = boot_enc_load(state, 0, hdr, fap, bs);
1758-
#endif
17591731
assert(rc >= 0);
17601732

17611733
if (rc == 0) {
@@ -1779,11 +1751,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
17791751
hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
17801752
if (IS_ENCRYPTED(hdr)) {
17811753
fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
1782-
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
1783-
rc = boot_enc_load(state, 1, hdr, fap, bs, 0);
1784-
#else
17851754
rc = boot_enc_load(state, 1, hdr, fap, bs);
1786-
#endif
17871755
assert(rc >= 0);
17881756

17891757
if (rc == 0) {

0 commit comments

Comments
 (0)