Skip to content

Commit 0657388

Browse files
sigvartmhbjarki-andreasen
authored andcommitted
[nrf noup] loader: Add firmware version check downgrade prevention
For nRF53, the only existing version number metadata is stored in the `firmware_info` structure in the network core. This utilizes PCD to read out the version number and compares it against the version number found in the secondary slot for the network core. Ref. NCSDK-21379 Signed-off-by: Sigvart Hovland <[email protected]> (cherry picked from commit 8e91ec1)
1 parent 6a5331b commit 0657388

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

boot/bootutil/src/loader.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151

5252
#if defined(CONFIG_SOC_NRF5340_CPUAPP) && defined(PM_CPUNET_B0N_ADDRESS)
5353
#include <dfu/pcd.h>
54+
#ifdef CONFIG_PCD_READ_NETCORE_APP_VERSION
55+
#include <fw_info_bare.h>
56+
int pcd_version_cmp_net(const struct flash_area *fap, struct image_header *hdr);
57+
#endif
5458
#endif
5559

5660
#ifdef MCUBOOT_ENC_IMAGES
@@ -1065,9 +1069,21 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
10651069
#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
10661070
if (slot != BOOT_PRIMARY_SLOT) {
10671071
/* Check if version of secondary slot is sufficient */
1068-
rc = boot_version_cmp(
1069-
&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1070-
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1072+
1073+
#if defined(CONFIG_SOC_NRF5340_CPUAPP) && defined(CONFIG_NRF53_MULTI_IMAGE_UPDATE) \
1074+
&& defined(CONFIG_PCD_APP) && defined(CONFIG_PCD_READ_NETCORE_APP_VERSION)
1075+
if (BOOT_CURR_IMG(state) == 1) {
1076+
rc = pcd_version_cmp_net(fap, boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1077+
} else {
1078+
rc = boot_version_cmp(
1079+
&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1080+
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1081+
}
1082+
#else
1083+
rc = boot_version_cmp(
1084+
&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1085+
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1086+
#endif
10711087
if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
10721088
BOOT_LOG_ERR("insufficient version in secondary slot");
10731089
flash_area_erase(fap, 0, flash_area_get_size(fap));

0 commit comments

Comments
 (0)