Skip to content

Commit 422f9e8

Browse files
committed
Support for SPU to write protect bootloader flash region on application startup.
1 parent bd1215a commit 422f9e8

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ BIG_ENDIAN?=0
2828
USE_GCC?=1
2929
USE_GCC_HEADLESS?=1
3030
FLASH_OTP_KEYSTORE?=0
31+
BOOTLOADER_PARTITION_SIZE?=$$(( $(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET)))
3132

3233
OBJS:= \
3334
./src/string.o \
@@ -141,7 +142,6 @@ ifeq ($(FLASH_OTP_KEYSTORE),1)
141142
endif
142143

143144
ASFLAGS:=$(CFLAGS)
144-
BOOTLOADER_PARTITION_SIZE?=$$(( $(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET)))
145145

146146
all: $(MAIN_TARGET)
147147

hal/nrf5340.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,13 +700,40 @@ void hal_init(void)
700700
hal_net_check_version();
701701
}
702702

703+
/* enable write protection for the region of flash specified */
704+
int hal_flash_protect(uint32_t start, uint32_t len)
705+
{
706+
/* only application core supports SPU */
707+
#ifdef TARGET_nrf5340_app
708+
uint32_t region, n, i;
709+
710+
/* limit check */
711+
if (start > FLASH_SIZE)
712+
return -1;
713+
/* truncate if exceeds flash size */
714+
if (start + len > FLASH_SIZE)
715+
len = FLASH_SIZE - start;
716+
717+
region = (start / SPU_BLOCK_SIZE);
718+
n = (len / SPU_BLOCK_SIZE);
719+
720+
for (i = 0; i < n; i++) {
721+
/* do not allow write to this region and lock till next reset */
722+
SPU_FLASHREGION_PERM(region+i) = (
723+
SPU_FLASHREGION_PERM_EXEC |
724+
SPU_FLASHREGION_PERM_READ |
725+
SPU_FLASHREGION_PERM_SECATTR |
726+
SPU_FLASHREGION_PERM_LOCK
727+
);
728+
}
729+
#endif
730+
return 0;
731+
}
703732

704733
void hal_prepare_boot(void)
705734
{
706-
/* TODO: Protect bootloader region of flash using SPU_FLASHREGION_PERM */
707-
//WOLFBOOT_ORIGIN
708-
//BOOTLOADER_PARTITION_SIZE
709-
//FLASHREGION[n].PERM
735+
/* Write protect bootloader region of flash */
736+
hal_flash_protect(WOLFBOOT_ORIGIN, BOOTLOADER_PARTITION_SIZE);
710737

711738
if (enableShm) {
712739
#ifdef TARGET_nrf5340_net

hal/nrf5340.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ void sleep_us(uint32_t usec);
106106

107107
#define SPU_BLOCK_SIZE (16 * 1024)
108108
#define SPU_FLASHREGION_PERM(n) *((volatile uint32_t *)(SPU_BASE + 0x600 + (((n) & 0x3F) * 0x4)))
109-
#define SPU_FLASHREGION_PERM_EXEC (1 << 0)
110-
#define SPU_FLASHREGION_PERM_WRITE (1 << 1)
111-
#define SPU_FLASHREGION_PERM_READ (1 << 2)
112-
#define SPU_FLASHREGION_PERM_SECATTR (1 << 4)
113-
#define SPU_FLASHREGION_PERM_LOCK (1 << 8)
109+
#define SPU_FLASHREGION_PERM_EXEC (1 << 0) /* Allow instruction fetches from flash region */
110+
#define SPU_FLASHREGION_PERM_WRITE (1 << 1) /* Allow write operation to region */
111+
#define SPU_FLASHREGION_PERM_READ (1 << 2) /* Allow read operation from flash region */
112+
#define SPU_FLASHREGION_PERM_SECATTR (1 << 4) /* Flash region n security attribute is secure */
113+
#define SPU_FLASHREGION_PERM_LOCK (1 << 8) /* The content of this register can't be changed until the next reset */
114114
#endif
115115

116116
/* OTP */

0 commit comments

Comments
 (0)