Skip to content

Commit 9efa2fd

Browse files
hakonfammbolivar-nordic
authored andcommitted
[nrf noup] zephyr: lock mcuboot using fprotect before jumping
This to enable the secure boot property of the system. Signed-off-by: Håkon Øye Amundsen <[email protected]> Signed-off-by: Emil Obalski <[email protected]> Signed-off-by: Andrzej Puzdrowski <[email protected]> Signed-off-by: Sigvart Hovland <[email protected]> Signed-off-by: Robert Lubos <[email protected]> Signed-off-by: Torsten Rasmussen <[email protected]> Signed-off-by: Ioannis Glaropoulos <[email protected]> Signed-off-by: Johann Fischer <[email protected]> (cherry picked from commit 0bb3e66) Signed-off-by: Trond Einar Snekvik <[email protected]> (cherry picked from commit 56da5da)
1 parent 48aee5f commit 9efa2fd

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

boot/zephyr/main.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ K_SEM_DEFINE(boot_log_sem, 1, 1);
8989
#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
9090
#endif /* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) */
9191

92+
#if USE_PARTITION_MANAGER && CONFIG_FPROTECT
93+
#include <fprotect.h>
94+
#include <pm_config.h>
95+
#endif
96+
9297
#ifdef CONFIG_SOC_FAMILY_NRF
9398
#include <hal/nrf_power.h>
9499

@@ -543,7 +548,30 @@ void main(void)
543548
#else
544549
BOOT_LOG_INF("Jumping to the first image slot");
545550
#endif
551+
552+
#if USE_PARTITION_MANAGER && CONFIG_FPROTECT
553+
554+
#ifdef PM_S1_ADDRESS
555+
/* MCUBoot is stored in either S0 or S1, protect both */
556+
#define PROTECT_SIZE (PM_MCUBOOT_PRIMARY_ADDRESS - PM_S0_ADDRESS)
557+
#define PROTECT_ADDR PM_S0_ADDRESS
558+
#else
559+
/* There is only one instance of MCUBoot */
560+
#define PROTECT_SIZE (PM_MCUBOOT_PRIMARY_ADDRESS - PM_MCUBOOT_ADDRESS)
561+
#define PROTECT_ADDR PM_MCUBOOT_ADDRESS
562+
#endif
563+
564+
rc = fprotect_area(PROTECT_ADDR, PROTECT_SIZE);
565+
566+
if (rc != 0) {
567+
BOOT_LOG_ERR("Protect mcuboot flash failed, cancel startup.");
568+
while (1)
569+
;
570+
}
571+
#endif /* USE_PARTITION_MANAGER && CONFIG_FPROTECT */
572+
546573
ZEPHYR_BOOT_LOG_STOP();
574+
547575
do_boot(&rsp);
548576

549577
BOOT_LOG_ERR("Never should get here");

boot/zephyr/pm.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ mcuboot_pad:
3939
size: CONFIG_PM_PARTITION_SIZE_MCUBOOT_PAD
4040
placement:
4141
before: [mcuboot_primary_app]
42-
align: {start: DT_FLASH_ERASE_BLOCK_SIZE}
42+
#ifdef CONFIG_FPROTECT
43+
align: {start: CONFIG_FPROTECT_BLOCK_SIZE}
44+
#endif

boot/zephyr/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ CONFIG_BOOT_BOOTSTRAP=n
2323
# CONFIG_TINYCRYPT_SHA256 is not set
2424

2525
CONFIG_FLASH=y
26+
CONFIG_FPROTECT=y
2627

2728
### Various Zephyr boards enable features that we don't want.
2829
# CONFIG_BT is not set

0 commit comments

Comments
 (0)