Skip to content

Commit fcfe786

Browse files
committed
[nrf noup] boot: zephyr: Region protection adjustments for nRF54L
nrf-squash! [nrf noup] boot: zephyr: Disable self RWX This commit fixes a couple of issues regarding MCUBoot region protection for nRF54L. Also, support for region and protection is added for nRF54LM20 and nRF54LV10 platforms. Signed-off-by: Artur Hadasz <[email protected]>
1 parent 4b5901f commit fcfe786

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

boot/zephyr/Kconfig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,15 @@ config MCUBOOT_CLEANUP_RAM
496496
help
497497
Sets contents of memory to 0 before jumping to application.
498498

499+
config NCS_MCUBOOT_DISABLE_SELF_RWX_SUPPORTED
500+
bool
501+
default y if SOC_NRF54L15_CPUAPP || SOC_NRF54L10_CPUAPP || SOC_NRF54L05_CPUAPP
502+
default y if SOC_NRF54LV10A_ENGA_CPUAPP
503+
default y if SOC_NRF54LM20A_ENGA_CPUAPP
504+
499505
config NCS_MCUBOOT_DISABLE_SELF_RWX
500506
bool "Disable read and execution on self NVM"
501-
depends on (SOC_NRF54L15_CPUAPP || SOC_NRF54L10_CPUAPP || SOC_NRF54L05_CPUAPP) && !FPROTECT
507+
depends on NCS_MCUBOOT_DISABLE_SELF_RWX_SUPPORTED && !FPROTECT
502508
help
503509
Sets RRAMC's region no.4 protection before jumping to application.
504510
It disables reads writes and execution memory area which holds MCUBOOT.

boot/zephyr/main.c

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,12 @@ K_SEM_DEFINE(boot_log_sem, 1, 1);
150150
* !defined(CONFIG_LOG_MODE_MINIMAL)
151151
*/
152152

153+
#if USE_PARTITION_MANAGER
154+
#include <pm_config.h>
155+
#endif
156+
153157
#if USE_PARTITION_MANAGER && CONFIG_FPROTECT
154158
#include <fprotect.h>
155-
#include <pm_config.h>
156159
#endif
157160

158161
#if CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL
@@ -173,9 +176,48 @@ K_SEM_DEFINE(boot_log_sem, 1, 1);
173176

174177
#define RRAMC_REGION_RWX_LSB 0
175178
#define RRAMC_REGION_RWX_WIDTH 3
176-
#define RRAMC_REGION_TO_LOCK_ADDR NRF_RRAMC->REGION[4].CONFIG
177-
#define RRAMC_REGION_TO_LOCK_ADDR_H (((uint32_t)(&(RRAMC_REGION_TO_LOCK_ADDR))) >> 16)
178-
#define RRAMC_REGION_TO_LOCK_ADDR_L (((uint32_t)(&(RRAMC_REGION_TO_LOCK_ADDR))) & 0x0000fffful)
179+
180+
#define RRAMC_REGION_NUMBER 4
181+
#define NRF_RRAM_REGION_SIZE_UNIT 0x400
182+
#define NRF_RRAM_REGION_ADDRESS_RESOLUTION 0x400
183+
184+
#if defined(CONFIG_SOC_NRF54L15_CPUAPP) || defined(CONFIG_SOC_NRF54L05_CPUAPP) || \
185+
defined(CONFIG_SOC_NRF54L10_CPUAPP)
186+
#define MAX_PROTECTED_REGION_SIZE (31 * 1024)
187+
#elif defined(CONFIG_SOC_NRF54LV10A_ENGA_CPUAPP) || defined(CONFIG_SOC_NRF54LM20A_ENGA_CPUAPP)
188+
#define MAX_PROTECTED_REGION_SIZE (127 * 1024)
189+
#elif defined(CONFIG_SOC_NRF54LS05B_ENGA_CPUAPP)
190+
#define MAX_PROTECTED_REGION_SIZE (1023 * 1024)
191+
#endif
192+
193+
#define RRAMC_REGION_CONFIG NRF_RRAMC->REGION[RRAMC_REGION_NUMBER].CONFIG
194+
#define RRAMC_REGION_CONFIG_H (((uint32_t)(&(RRAMC_REGION_CONFIG))) >> 16)
195+
#define RRAMC_REGION_CONFIG_L (((uint32_t)(&(RRAMC_REGION_CONFIG))) & 0x0000fffful)
196+
197+
#define RRAMC_REGION_ADDRESS NRF_RRAMC->REGION[RRAMC_REGION_NUMBER].ADDRESS
198+
#define RRAMC_REGION_ADDRESS_H (((uint32_t)(&(RRAMC_REGION_ADDRESS))) >> 16)
199+
#define RRAMC_REGION_ADDRESS_L (((uint32_t)(&(RRAMC_REGION_ADDRESS))) & 0x0000fffful)
200+
201+
#if (CONFIG_NCS_IS_VARIANT_IMAGE)
202+
#define PROTECTED_REGION_START PM_S1_IMAGE_ADDRESS
203+
#define PROTECTED_REGION_SIZE PM_S1_IMAGE_SIZE
204+
#else
205+
#define PROTECTED_REGION_START PM_MCUBOOT_ADDRESS
206+
#define PROTECTED_REGION_SIZE PM_MCUBOOT_SIZE
207+
#endif
208+
209+
BUILD_ASSERT((PROTECTED_REGION_START % NRF_RRAM_REGION_ADDRESS_RESOLUTION) == 0,
210+
"Start of protected region is not aligned - not possible to protect");
211+
212+
BUILD_ASSERT((PROTECTED_REGION_SIZE % NRF_RRAM_REGION_SIZE_UNIT) == 0,
213+
"Size of protected region is not aligned - not possible to protect");
214+
215+
BUILD_ASSERT(PROTECTED_REGION_SIZE <= MAX_PROTECTED_REGION_SIZE,
216+
"Size of protected region is too big for protection");
217+
218+
#define PROTECTED_REGION_START_H ((PROTECTED_REGION_START) >> 16)
219+
#define PROTECTED_REGION_START_L ((PROTECTED_REGION_START) & 0x0000fffful)
220+
179221
#endif /* CONFIG_NCS_MCUBOOT_DISABLE_SELF_RWX */
180222

181223
BOOT_LOG_MODULE_REGISTER(mcuboot);
@@ -245,11 +287,20 @@ static void __ramfunc jump_in(uint32_t reset)
245287
" ldr r2, [r1]\n"
246288
/* Size of the region should be set at this point
247289
* by NSIB's DISABLE_NEXT_W.
248-
* If not, set it according partition size.
290+
* If not, the region has not been configured yet.
291+
* Set the size and address to the partition size and address.
249292
*/
250293
" ands r4, r2, %12\n"
251294
" cbnz r4, clear_rwx\n"
295+
/* Set the size of the protected region */
252296
" movt r2, %8\n"
297+
/* Set the address of the protected region */
298+
" movw r5, %13\n"
299+
" movt r5, %14\n"
300+
" movw r6, %15\n"
301+
" movt r6, %16\n"
302+
" str r6, [r5]\n"
303+
" dsb\n"
253304
"clear_rwx:\n"
254305
" bfc r2, %9, %10\n"
255306
/* Disallow further modifications */
@@ -270,13 +321,17 @@ static void __ramfunc jump_in(uint32_t reset)
270321
"r" (CLEANUP_RAM_GAP_SIZE),
271322
"i" (0)
272323
#ifdef CONFIG_NCS_MCUBOOT_DISABLE_SELF_RWX
273-
, "i" (RRAMC_REGION_TO_LOCK_ADDR_L),
274-
"i" (RRAMC_REGION_TO_LOCK_ADDR_H),
275-
"i" (CONFIG_PM_PARTITION_SIZE_B0_IMAGE / 1024),
324+
, "i" (RRAMC_REGION_CONFIG_L),
325+
"i" (RRAMC_REGION_CONFIG_H),
326+
"i" ((PROTECTED_REGION_SIZE) / (NRF_RRAM_REGION_SIZE_UNIT)),
276327
"i" (RRAMC_REGION_RWX_LSB),
277328
"i" (RRAMC_REGION_RWX_WIDTH),
278329
"i" (RRAMC_REGION_CONFIG_LOCK_Msk),
279-
"i" (RRAMC_REGION_CONFIG_SIZE_Msk)
330+
"i" (RRAMC_REGION_CONFIG_SIZE_Msk),
331+
"i" (RRAMC_REGION_ADDRESS_L),
332+
"i" (RRAMC_REGION_ADDRESS_H),
333+
"i" (PROTECTED_REGION_START_L),
334+
"i" (PROTECTED_REGION_START_H)
280335
#endif /* CONFIG_NCS_MCUBOOT_DISABLE_SELF_RWX */
281336
: "r0", "r1", "r2", "r3", "r4", "r5", "r6", "memory"
282337
);

0 commit comments

Comments
 (0)