Skip to content

Commit 257265c

Browse files
tomchyde-nordic
authored andcommitted
loader: Optimize boot_check_header_erased(..)
The boot_check_header_erased(..) function may use a common function to check for a buffer value (bootutil_buffer_is_erased). Checking function should return bool intead of int. Signed-off-by: Tomasz Chyrowicz <tomasz.chyrowicz@nordicsemi.no>
1 parent 76e56e4 commit 257265c

File tree

1 file changed

+10
-32
lines changed

1 file changed

+10
-32
lines changed

boot/bootutil/src/loader.c

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -754,40 +754,21 @@ boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fa
754754
return true;
755755
}
756756

757-
/*
758-
* Check that a memory area consists of a given value.
759-
*/
760-
static inline bool
761-
boot_data_is_set_to(uint8_t val, void *data, size_t len)
762-
{
763-
uint8_t i;
764-
uint8_t *p = (uint8_t *)data;
765-
for (i = 0; i < len; i++) {
766-
if (val != p[i]) {
767-
return false;
768-
}
769-
}
770-
return true;
771-
}
772-
773-
static int
757+
static bool
774758
boot_check_header_erased(struct boot_loader_state *state, int slot)
775759
{
776760
const struct flash_area *fap = NULL;
777761
struct image_header *hdr;
778-
uint8_t erased_val;
779762

780763
fap = BOOT_IMG_AREA(state, slot);
781764
assert(fap != NULL);
782765

783-
erased_val = flash_area_erased_val(fap);
784-
785766
hdr = boot_img_hdr(state, slot);
786-
if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
787-
return -1;
767+
if (bootutil_buffer_is_erased(fap, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
768+
return true;
788769
}
789770

790-
return 0;
771+
return false;
791772
}
792773

793774
#if defined(MCUBOOT_DIRECT_XIP)
@@ -854,8 +835,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
854835
assert(fap != NULL);
855836

856837
hdr = boot_img_hdr(state, slot);
857-
if (boot_check_header_erased(state, slot) == 0 ||
858-
(hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
838+
if (boot_check_header_erased(state, slot) || (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
859839
#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_OFFSET)
860840
/*
861841
* This fixes an issue where an image might be erased, but a trailer
@@ -919,7 +899,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
919899
rc = boot_compare_version(
920900
&boot_img_hdr(state, BOOT_SLOT_SECONDARY)->ih_ver,
921901
&boot_img_hdr(state, BOOT_SLOT_PRIMARY)->ih_ver);
922-
if (rc < 0 && boot_check_header_erased(state, BOOT_SLOT_PRIMARY)) {
902+
if (rc < 0 && !boot_check_header_erased(state, BOOT_SLOT_PRIMARY)) {
923903
BOOT_LOG_ERR("insufficient version in secondary slot");
924904
boot_scramble_slot(fap, slot);
925905
/* Image in the secondary slot does not satisfy version requirement.
@@ -1731,9 +1711,8 @@ boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
17311711
* already been checked).
17321712
*/
17331713
FIH_DECLARE(fih_rc, FIH_FAILURE);
1734-
rc = boot_check_header_erased(state, BOOT_SLOT_PRIMARY);
17351714
FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SLOT_PRIMARY, bs, 0);
1736-
if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
1715+
if (boot_check_header_erased(state, BOOT_SLOT_PRIMARY) || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
17371716
rc = boot_copy_image(state, bs);
17381717
} else {
17391718
rc = boot_swap_image(state, bs);
@@ -1963,7 +1942,7 @@ boot_prepare_image_for_update(struct boot_loader_state *state,
19631942
rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
19641943
#ifdef MCUBOOT_BOOTSTRAP
19651944
/* When bootstrapping it's OK to not have image magic in the primary slot */
1966-
if (rc != 0 && boot_check_header_erased(state, BOOT_SLOT_PRIMARY) != 0) {
1945+
if (rc != 0 && !boot_check_header_erased(state, BOOT_SLOT_PRIMARY)) {
19671946
#else
19681947
if (rc != 0) {
19691948
#endif
@@ -2042,11 +2021,10 @@ boot_prepare_image_for_update(struct boot_loader_state *state,
20422021
* magic, so also run validation on the primary slot to be
20432022
* sure it's not OK.
20442023
*/
2045-
rc = boot_check_header_erased(state, BOOT_SLOT_PRIMARY);
20462024
FIH_CALL(boot_validate_slot, fih_rc,
20472025
state, BOOT_SLOT_PRIMARY, bs, 0);
2048-
2049-
if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
2026+
if (boot_check_header_erased(state, BOOT_SLOT_PRIMARY) ||
2027+
FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
20502028

20512029
rc = (boot_img_hdr(state, BOOT_SLOT_SECONDARY)->ih_magic == IMAGE_MAGIC) ? 1: 0;
20522030
FIH_CALL(boot_validate_slot, fih_rc,

0 commit comments

Comments
 (0)