Skip to content

Commit 69ffb5d

Browse files
committed
[nrf noup] boot: zephyr: Trailing write protected region
The write permissions granted above the highest addressed slot started at the end of the partition, leaving partitions placed above the slot writable by the booted image. On the nRF9251 DK, the PERIPHCONF and MPCCONF partitions sit above slot0, as the bootloader and slot0 partitions are kept adjacent. Move the start of that range up by the configured trailing region size which needs no additional MPC override. Only the entries applied when booting an image from its first slot are covered, so a BUILD_ASSERT rejects the option together with direct XIP. The size defaults to zero, which leaves the generated entries unchanged. Signed-off-by: Jan Tore Guggedal <jantore.guggedal@nordicsemi.no>
1 parent 2d9dab4 commit 69ffb5d

1 file changed

Lines changed: 40 additions & 3 deletions

File tree

boot/zephyr/load_ironside_se_conf_mpc.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,37 @@ CHECK_MPC_ADDRESS_ALIGNMENT("At least one secondary image partition", SECONDARY_
273273
CHECK_MPC_ADDRESS_ALIGNMENT("At least one secondary image partition", SECONDARY_ACTIVE_3_START);
274274
CHECK_MPC_ADDRESS_ALIGNMENT("At least one secondary image partition", SECONDARY_ACTIVE_3_END);
275275

276+
/* Define a region to write protect above the highest addressed image. The trailing region
277+
* covers partitions placed above the slots that must not be writable by the booted image,
278+
* such as a PERIPHCONF or MPCCONF partition.
279+
*
280+
* It only moves the start of the RWX range that already begins at the last slot end, so no
281+
* additional MPC override is needed. The size defaults to zero, leaving only the slot
282+
* partitions write protected.
283+
*/
284+
#define TRAILING_WP_SIZE CONFIG_NCS_MCUBOOT_MPCCONF_STATIC_WRITE_PROTECTION_TRAILING_REGION_SIZE
285+
286+
#define PRIMARY_WP_END (PRIMARY_ACTIVE_3_END + TRAILING_WP_SIZE)
287+
288+
CHECK_MPC_ADDRESS_ALIGNMENT("Trailing write protected region", TRAILING_WP_SIZE);
289+
290+
/* Write permissions above the highest addressed slot are granted from the end of its write
291+
* protected region. A trailing region reaching past the end of the accessible MRAM would leave
292+
* that range without the entry that grants them, silently write protecting partitions that are
293+
* meant to stay writable, such as a settings partition.
294+
*/
295+
BUILD_ASSERT(PRIMARY_WP_END <= ACCESSIBLE_MRAM_END,
296+
"The trailing write protected region extends past the end of the accessible MRAM");
297+
298+
/* Only primary_entries grants write permissions from the write protected end. The entries
299+
* used when direct XIP boots an image from its upper slot (slot1, slot3, ...) are not
300+
* covered, so the protection would depend on which slot was booted.
301+
*/
302+
#if defined(MCUBOOT_DIRECT_XIP)
303+
BUILD_ASSERT(TRAILING_WP_SIZE == 0,
304+
"The trailing write protected region is not supported together with direct XIP.");
305+
#endif
306+
276307
/* MPC overrides used to implement image write protection.
277308
*
278309
* See the explanations on the various tables below to understand how they are used.
@@ -432,6 +463,12 @@ static const struct mpcconf_entry uicr_entries[] __used Z_GENERIC_DOT_SECTION(mp
432463
* Override 10: RWX | [ACTIVE_2 end - ACTIVE_3 start] (third inter-image gap when N > 3)
433464
* Override 11: RWX | [last active partition end - end of accessible MRAM]
434465
*
466+
* The override numbers above are the ones used on the nRF54H20. Other SoCs assign the same
467+
* roles to different overrides, see the MPC110_OVERRIDE_* definitions above.
468+
*
469+
* The last of the ranges above starts at the write protected end of the last active partition,
470+
* so a configured trailing region shrinks it, see TRAILING_WP_SIZE above.
471+
*
435472
* Note that the effect of overlapping the R_X with RWX is RWX (perms are OR-ed).
436473
*/
437474
static const struct mpcconf_entry primary_entries[] = {
@@ -504,15 +541,15 @@ static const struct mpcconf_entry primary_entries[] = {
504541
#endif
505542
#endif /* MCUBOOT_IMAGE_NUMBER > 3 */
506543

507-
#if ACCESSIBLE_MRAM_END > PRIMARY_ACTIVE_3_END
508-
/* RWX | [ACTIVE_3 end - user end] */
544+
#if ACCESSIBLE_MRAM_END > PRIMARY_WP_END
545+
/* RWX | [ACTIVE_3 write protected end - user end] */
509546
{
510547
MPCCONF_ENTRY_CONFIG0_VALUE(
511548
/* LOCK */ true, /* ENABLE */ true,
512549
MPC110_OVERRIDE_LAST_ACTIVE_END_TO_ACCESSIBLE_MRAM_END_RWX),
513550
MPCCONF_ENTRY_CONFIG1_VALUE(/* R */ true, /* W */ true,
514551
/* X */ true,
515-
/* S */ false, PRIMARY_ACTIVE_3_END),
552+
/* S */ false, PRIMARY_WP_END),
516553
MPCCONF_ENTRY_CONFIG2_VALUE(MPCCONF_OWNER, ACCESSIBLE_MRAM_END),
517554
MPCCONF_ENTRY_CONFIG3_VALUE(MASTERPORT_DEFAULT),
518555
},

0 commit comments

Comments
 (0)