-
Notifications
You must be signed in to change notification settings - Fork 1.4k
TF-M PSA template: Lock Approtect in network core #17093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Copyright (c) 2024 Nordic Semiconductor ASA | ||
| * | ||
| * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
| */ | ||
|
|
||
| /** @file pcd_common.h | ||
| * | ||
| * @ingroup pcd | ||
| * @{ | ||
| * @brief Common definitions for the PCD API. | ||
| * | ||
| * Common definitions are split out from the main PCD API to allow usage | ||
| * from non-Zephyr code. | ||
| */ | ||
|
|
||
| #ifndef PCD_COMMON_H__ | ||
| #define PCD_COMMON_H__ | ||
|
|
||
| #ifndef CONFIG_SOC_SERIES_NRF53X | ||
| #error "PCD is only supported on nRF53 series" | ||
| #endif | ||
|
|
||
| #ifdef CONFIG_PCD_CMD_ADDRESS | ||
| /* PCD command block location is static. */ | ||
| #define PCD_CMD_ADDRESS CONFIG_PCD_CMD_ADDRESS | ||
|
|
||
| #else | ||
| /* PCD command block location is configured with Partition Manager. */ | ||
| #include <pm_config.h> | ||
|
|
||
| #ifdef PM_PCD_SRAM_ADDRESS | ||
| /* PCD command block is in this domain, we are compiling for application core. */ | ||
| #define PCD_CMD_ADDRESS PM_PCD_SRAM_ADDRESS | ||
| #else | ||
| /* PCD command block is in a different domain, we are compiling for network core. | ||
| * Extra '_' since its in a different domain. | ||
| */ | ||
| #define PCD_CMD_ADDRESS PM__PCD_SRAM_ADDRESS | ||
| #endif /* PM_PCD_SRAM_ADDRESS */ | ||
|
|
||
| #endif /* CONFIG_PCD_CMD_ADDRESS */ | ||
|
|
||
| /** Magic value written to indicate that a copy should take place. */ | ||
| #define PCD_CMD_MAGIC_COPY 0xb5b4b3b6 | ||
| /** Magic value written to indicate that debug should be locked. */ | ||
| #define PCD_CMD_MAGIC_LOCK_DEBUG 0xb6f249ec | ||
| /** Magic value written to indicate that a something failed. */ | ||
| #define PCD_CMD_MAGIC_FAIL 0x25bafc15 | ||
| /** Magic value written to indicate that a copy is done. */ | ||
| #define PCD_CMD_MAGIC_DONE 0xf103ce5d | ||
| /** Magic value written to indicate that a version number read should take place. */ | ||
| #define PCD_CMD_MAGIC_READ_VERSION 0xdca345ea | ||
|
|
||
| struct pcd_cmd { | ||
| uint32_t magic; /* Magic value to identify this structure in memory */ | ||
| const void *data; /* Data to copy*/ | ||
| size_t len; /* Number of bytes to copy */ | ||
| __INTPTR_TYPE__ offset; /* Offset to store the flash image in */ | ||
| } __aligned(4); | ||
|
|
||
| #define PCD_CMD ((volatile struct pcd_cmd * const)(PCD_CMD_ADDRESS)) | ||
|
|
||
| static inline void pcd_write_cmd_lock_debug(void) | ||
| { | ||
| *PCD_CMD = (struct pcd_cmd){ | ||
| .magic = PCD_CMD_MAGIC_LOCK_DEBUG, | ||
| }; | ||
| } | ||
|
|
||
| static inline bool pcd_read_cmd_done(void) | ||
| { | ||
| return PCD_CMD->magic == PCD_CMD_MAGIC_DONE; | ||
| } | ||
|
|
||
| static inline bool pcd_read_cmd_lock_debug(void) | ||
| { | ||
| return PCD_CMD->magic == PCD_CMD_MAGIC_LOCK_DEBUG; | ||
| } | ||
|
|
||
| #endif /* PCD_COMMON_H__ */ | ||
|
|
||
| /**@} */ |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,51 @@ | |||||||||||||||||||||||||||||||
| #include "nrf_provisioning.h" | ||||||||||||||||||||||||||||||||
| #include <identity_key.h> | ||||||||||||||||||||||||||||||||
| #include <tfm_spm_log.h> | ||||||||||||||||||||||||||||||||
| #include <pm_config.h> | ||||||||||||||||||||||||||||||||
| #if defined(NRF53_SERIES) && defined(PM_CPUNET_APP_ADDRESS) | ||||||||||||||||||||||||||||||||
| #include <dfu/pcd_common.h> | ||||||||||||||||||||||||||||||||
| #include <spu.h> | ||||||||||||||||||||||||||||||||
| #include <hal/nrf_reset.h> | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| #define DEBUG_LOCK_TIMEOUT_MS 3000 | ||||||||||||||||||||||||||||||||
| #define USEC_IN_MSEC 1000 | ||||||||||||||||||||||||||||||||
MarkusLassila marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
| #define USEC_IN_SEC 1000000 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| static enum tfm_plat_err_t disable_netcore_debug(void) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| /* NRF_RESET to secure. | ||||||||||||||||||||||||||||||||
| * It will be configured to the original value after the provisioning is done. | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| spu_peripheral_config_secure(NRF_RESET_S_BASE, SPU_LOCK_CONF_UNLOCKED); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /* Ensure that the network core is stopped. */ | ||||||||||||||||||||||||||||||||
| nrf_reset_network_force_off(NRF_RESET, true); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /* Debug lock command will be read in b0n startup. */ | ||||||||||||||||||||||||||||||||
| pcd_write_cmd_lock_debug(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /* Start the network core. */ | ||||||||||||||||||||||||||||||||
| nrf_reset_network_force_off(NRF_RESET, false); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /* Wait 1 second for the network core to start up. */ | ||||||||||||||||||||||||||||||||
| NRFX_DELAY_US(USEC_IN_SEC); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /* Wait for the debug lock to complete. */ | ||||||||||||||||||||||||||||||||
| for (int i = 0; i < DEBUG_LOCK_TIMEOUT_MS; i++) { | ||||||||||||||||||||||||||||||||
| if (!pcd_read_cmd_lock_debug()) { | ||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| NRFX_DELAY_US(USEC_IN_MSEC); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| /* Wait 1 second for the network core to start up. */ | |
| NRFX_DELAY_US(USEC_IN_SEC); | |
| /* Wait for the debug lock to complete. */ | |
| for (int i = 0; i < DEBUG_LOCK_TIMEOUT_MS; i++) { | |
| if (!pcd_read_cmd_lock_debug()) { | |
| break; | |
| } | |
| NRFX_DELAY_US(USEC_IN_MSEC); | |
| /* Wait for the core to boot and debug lock to complete. */ | |
| for (int i = 0; i < DEBUG_LOCK_TIMEOUT_MS; i++) { | |
| NRFX_DELAY_US(USEC_IN_MSEC); | |
| if (!pcd_read_cmd_lock_debug()) { | |
| break; | |
| } |
This is just an optimization... I don't really think there is anything wrong, other than it might be unnecessary slow to boot.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,9 @@ | |
| #include <dfu/pcd.h> | ||
| #include <zephyr/device.h> | ||
| #include <zephyr/devicetree.h> | ||
| #ifdef CONFIG_PCD_LOCK_NETCORE_APPROTECT | ||
| #include <nrfx_nvmc.h> | ||
| #endif | ||
|
|
||
| int main(void) | ||
| { | ||
|
|
@@ -39,10 +42,26 @@ int main(void) | |
|
|
||
| uint32_t s0_addr = s0_address_read(); | ||
| bool valid = false; | ||
| uint8_t status = pcd_fw_copy_status_get(); | ||
|
|
||
| switch (pcd_fw_copy_status_get()) { | ||
| #ifdef CONFIG_PCD_LOCK_NETCORE_DEBUG | ||
MarkusLassila marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case PCD_STATUS_LOCK_DEBUG: | ||
| nrfx_nvmc_word_write((uint32_t)&NRF_UICR_NS->APPROTECT, | ||
MarkusLassila marked this conversation as resolved.
Show resolved
Hide resolved
nordicjm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| UICR_APPROTECT_PALL_Protected); | ||
| while (!nrfx_nvmc_write_done_check()) | ||
| ; | ||
|
|
||
| pcd_done(); | ||
|
||
|
|
||
| /* Success, waiting to be rebooted */ | ||
| while (1) | ||
| ; | ||
| CODE_UNREACHABLE; | ||
| break; | ||
| #endif | ||
|
|
||
| #ifdef CONFIG_PCD_READ_NETCORE_APP_VERSION | ||
| if (status == PCD_STATUS_READ_VERSION) { | ||
| case PCD_STATUS_READ_VERSION: | ||
| err = pcd_find_fw_version(); | ||
| if (err < 0) { | ||
| printk("Unable to find valid firmware version %d\n\r", err); | ||
|
|
@@ -54,10 +73,10 @@ int main(void) | |
| while (1) | ||
| ; | ||
| CODE_UNREACHABLE; | ||
| } | ||
| break; | ||
| #endif | ||
|
|
||
| if (status == PCD_STATUS_COPY) { | ||
| case PCD_STATUS_COPY: | ||
| /* First we validate the data where the PCD CMD tells | ||
| * us that we can find it. | ||
| */ | ||
|
|
@@ -94,6 +113,10 @@ int main(void) | |
| while (1) | ||
| ; | ||
| CODE_UNREACHABLE; | ||
| break; | ||
|
|
||
| default: | ||
| break; | ||
| } | ||
|
|
||
| err = fprotect_area(PM_APP_ADDRESS, PM_APP_SIZE); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # | ||
| # Copyright (c) 2024 Nordic Semiconductor | ||
| # | ||
| # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
| # | ||
|
|
||
| source "${ZEPHYR_BASE}/share/sysbuild/Kconfig" | ||
|
|
||
| if BOARD_NRF5340DK_NRF5340_CPUAPP_NS | ||
|
|
||
| choice NETCORE | ||
| default NETCORE_EMPTY | ||
| endchoice | ||
|
|
||
| config SECURE_BOOT_NETCORE | ||
| default y | ||
|
|
||
| config NETCORE_APP_UPDATE | ||
| default y | ||
|
|
||
| config MCUBOOT_APP_SYNC_UPDATEABLE_IMAGES | ||
| default y | ||
|
|
||
| config PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY | ||
| default y | ||
|
|
||
| config MCUBOOT_USE_ALL_AVAILABLE_RAM | ||
|
||
| default y | ||
|
|
||
| endif | ||

Uh oh!
There was an error while loading. Please reload this page.