Skip to content

Commit 1b6571d

Browse files
sigvartmhcvinayak
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]>
1 parent b80046d commit 1b6571d

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
@@ -50,6 +50,10 @@
5050

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

5559
#ifdef MCUBOOT_ENC_IMAGES
@@ -781,9 +785,21 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
781785
#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
782786
if (slot != BOOT_PRIMARY_SLOT) {
783787
/* Check if version of secondary slot is sufficient */
784-
rc = boot_version_cmp(
785-
&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
786-
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
788+
789+
#if defined(CONFIG_SOC_NRF5340_CPUAPP) && defined(CONFIG_NRF53_MULTI_IMAGE_UPDATE) \
790+
&& defined(CONFIG_PCD_APP) && defined(CONFIG_PCD_READ_NETCORE_APP_VERSION)
791+
if (BOOT_CURR_IMG(state) == 1) {
792+
rc = pcd_version_cmp_net(fap, boot_img_hdr(state, BOOT_SECONDARY_SLOT));
793+
} else {
794+
rc = boot_version_cmp(
795+
&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
796+
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
797+
}
798+
#else
799+
rc = boot_version_cmp(
800+
&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
801+
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
802+
#endif
787803
if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
788804
BOOT_LOG_ERR("insufficient version in secondary slot");
789805
flash_area_erase(fap, 0, flash_area_get_size(fap));

0 commit comments

Comments
 (0)