Skip to content

Commit c5aadd7

Browse files
sigvartmhjfischer-no
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]> Signed-off-by: Dominik Ermel <[email protected]> (cherry picked from commit 4441695)
1 parent 4b36f9f commit c5aadd7

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

0 commit comments

Comments
 (0)