Skip to content

Commit

Permalink
Support for SPU to write protect bootloader flash region on applicati…
Browse files Browse the repository at this point in the history
…on startup.
  • Loading branch information
dgarske committed Oct 8, 2024
1 parent bd1215a commit 422f9e8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ BIG_ENDIAN?=0
USE_GCC?=1
USE_GCC_HEADLESS?=1
FLASH_OTP_KEYSTORE?=0
BOOTLOADER_PARTITION_SIZE?=$$(( $(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET)))

OBJS:= \
./src/string.o \
Expand Down Expand Up @@ -141,7 +142,6 @@ ifeq ($(FLASH_OTP_KEYSTORE),1)
endif

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

all: $(MAIN_TARGET)

Expand Down
35 changes: 31 additions & 4 deletions hal/nrf5340.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,13 +700,40 @@ void hal_init(void)
hal_net_check_version();
}

/* enable write protection for the region of flash specified */
int hal_flash_protect(uint32_t start, uint32_t len)
{
/* only application core supports SPU */
#ifdef TARGET_nrf5340_app
uint32_t region, n, i;

/* limit check */
if (start > FLASH_SIZE)
return -1;
/* truncate if exceeds flash size */
if (start + len > FLASH_SIZE)
len = FLASH_SIZE - start;

region = (start / SPU_BLOCK_SIZE);
n = (len / SPU_BLOCK_SIZE);

for (i = 0; i < n; i++) {
/* do not allow write to this region and lock till next reset */
SPU_FLASHREGION_PERM(region+i) = (
SPU_FLASHREGION_PERM_EXEC |
SPU_FLASHREGION_PERM_READ |
SPU_FLASHREGION_PERM_SECATTR |
SPU_FLASHREGION_PERM_LOCK
);
}
#endif
return 0;
}

void hal_prepare_boot(void)
{
/* TODO: Protect bootloader region of flash using SPU_FLASHREGION_PERM */
//WOLFBOOT_ORIGIN
//BOOTLOADER_PARTITION_SIZE
//FLASHREGION[n].PERM
/* Write protect bootloader region of flash */
hal_flash_protect(WOLFBOOT_ORIGIN, BOOTLOADER_PARTITION_SIZE);

if (enableShm) {
#ifdef TARGET_nrf5340_net
Expand Down
10 changes: 5 additions & 5 deletions hal/nrf5340.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ void sleep_us(uint32_t usec);

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

/* OTP */
Expand Down

0 comments on commit 422f9e8

Please sign in to comment.