Skip to content

Commit 9c78f7d

Browse files
committed
samples: nrf5340: netboot: Support Approtect lock
Add a PCD command for locking the Approtect in B0n. Approtect will be locked from TF-M, which is separate from Zephyr OS. For this purpose, the relevant parts of PCD library are moved to a separate header, which can be used in both TF-M and Zephyr. Signed-off-by: Markus Lassila <[email protected]>
1 parent 29d06ab commit 9c78f7d

File tree

5 files changed

+92
-41
lines changed

5 files changed

+92
-41
lines changed

include/dfu/pcd.h

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,18 @@
2525

2626
#include <zephyr/device.h>
2727
#include <sys/types.h>
28+
#include <dfu/pcd_common.h>
2829

2930
#ifdef __cplusplus
3031
extern "C" {
3132
#endif
3233

33-
#ifdef CONFIG_SOC_SERIES_NRF53X
34-
35-
#ifdef CONFIG_PCD_CMD_ADDRESS
36-
37-
#define PCD_CMD_ADDRESS CONFIG_PCD_CMD_ADDRESS
38-
39-
#else
40-
41-
#include <pm_config.h>
42-
43-
#ifdef PM_PCD_SRAM_ADDRESS
44-
#define PCD_CMD_ADDRESS PM_PCD_SRAM_ADDRESS
45-
#else
46-
/* extra '_' since its in a different domain */
47-
#define PCD_CMD_ADDRESS PM__PCD_SRAM_ADDRESS
48-
#endif /* PM_PCD_SRAM_ADDRESS */
49-
50-
#endif /* CONFIG_PCD_CMD_ADDRESS */
51-
52-
#endif /* CONFIG_SOC_SERIES_NRF53X */
53-
5434
enum pcd_status {
5535
PCD_STATUS_COPY = 0,
5636
PCD_STATUS_DONE = 1,
5737
PCD_STATUS_FAILED = 2,
5838
PCD_STATUS_READ_VERSION = 3,
39+
PCD_STATUS_LOCK_DEBUG = 4,
5940
};
6041

6142
/** @brief Sets up the PCD command structure with the location and size of the
@@ -87,8 +68,10 @@ int pcd_network_core_update(const void *src_addr, size_t len);
8768
int pcd_network_core_update_initiate(const void *src_addr, size_t len);
8869

8970
/** @brief Lock the RAM section used for IPC with the network core bootloader.
71+
*
72+
* @param lock_conf Lock configuration until next SoC reset.
9073
*/
91-
void pcd_lock_ram(void);
74+
void pcd_lock_ram(bool lock_conf);
9275

9376
/** @brief Update the PCD CMD to indicate that the operation has completed
9477
* successfully.

include/dfu/pcd_common.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
/** @file pcd_common.h
8+
*
9+
* @ingroup pcd
10+
* @{
11+
* @brief Common definitions for the PCD API.
12+
*
13+
* Common definitions are split out from the main PCD API to allow usage
14+
* from non-Zephyr code.
15+
*/
16+
17+
#ifndef PCD_COMMON_H__
18+
#define PCD_COMMON_H__
19+
20+
#ifdef CONFIG_SOC_SERIES_NRF53X
21+
22+
#ifdef CONFIG_PCD_CMD_ADDRESS
23+
24+
#define PCD_CMD_ADDRESS CONFIG_PCD_CMD_ADDRESS
25+
26+
#else
27+
28+
#include <pm_config.h>
29+
30+
#ifdef PM_PCD_SRAM_ADDRESS
31+
#define PCD_CMD_ADDRESS PM_PCD_SRAM_ADDRESS
32+
#else
33+
/* extra '_' since its in a different domain */
34+
#define PCD_CMD_ADDRESS PM__PCD_SRAM_ADDRESS
35+
#endif /* PM_PCD_SRAM_ADDRESS */
36+
37+
#endif /* CONFIG_PCD_CMD_ADDRESS */
38+
39+
#endif /* CONFIG_SOC_SERIES_NRF53X */
40+
41+
/** Magic value written to indicate that a copy should take place. */
42+
#define PCD_CMD_MAGIC_COPY 0xb5b4b3b6
43+
/** Magic value written to indicate that debug should be locked. */
44+
#define PCD_CMD_MAGIC_LOCK_DEBUG 0xb6f249ec
45+
/** Magic value written to indicate that a something failed. */
46+
#define PCD_CMD_MAGIC_FAIL 0x25bafc15
47+
/** Magic value written to indicate that a copy is done. */
48+
#define PCD_CMD_MAGIC_DONE 0xf103ce5d
49+
/** Magic value written to indicate that a version number read should take place. */
50+
#define PCD_CMD_MAGIC_READ_VERSION 0xdca345ea
51+
52+
struct pcd_cmd {
53+
uint32_t magic; /* Magic value to identify this structure in memory */
54+
const void *data; /* Data to copy*/
55+
size_t len; /* Number of bytes to copy */
56+
__INTPTR_TYPE__ offset; /* Offset to store the flash image in */
57+
} __aligned(4);
58+
59+
#endif /* PCD_COMMON_H__ */
60+
61+
/**@} */

samples/nrf5340/netboot/src/main.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include <dfu/pcd.h>
1616
#include <zephyr/device.h>
1717
#include <zephyr/devicetree.h>
18+
#ifdef CONFIG_PCD_LOCK_NETCORE_APPROTECT
19+
#include <nrfx_nvmc.h>
20+
#endif
1821

1922
int main(void)
2023
{
@@ -41,6 +44,20 @@ int main(void)
4144
bool valid = false;
4245
uint8_t status = pcd_fw_copy_status_get();
4346

47+
#ifdef CONFIG_PCD_LOCK_NETCORE_DEBUG
48+
if (status == PCD_STATUS_LOCK_DEBUG) {
49+
nrfx_nvmc_word_write((uint32_t)&NRF_UICR_NS->APPROTECT,
50+
UICR_APPROTECT_PALL_Protected);
51+
52+
pcd_done();
53+
54+
/* Success, waiting to be rebooted */
55+
while (1)
56+
;
57+
CODE_UNREACHABLE;
58+
}
59+
#endif
60+
4461
#ifdef CONFIG_PCD_READ_NETCORE_APP_VERSION
4562
if (status == PCD_STATUS_READ_VERSION) {
4663
err = pcd_find_fw_version();

subsys/pcd/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ config PCD_READ_NETCORE_APP_VERSION
3838

3939
config PCD_USE_CONSTANTS
4040
bool "Use KConfig constants rather than pm_config.h"
41+
depends on !PCD_LOCK_NETCORE_DEBUG
4142

4243
config PCD_CMD_ADDRESS
4344
hex "PCD Command Address in RAM"
@@ -62,6 +63,10 @@ config PCD_BUF_SIZE
6263
help
6364
Must be <= the page size of the flash device.
6465

66+
config PCD_LOCK_NETCORE_DEBUG
67+
bool "Include PCD command to lock network core debug"
68+
default n
69+
6570
endif # PCD_NET
6671

6772
endmenu

subsys/pcd/src/pcd.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818

1919
LOG_MODULE_REGISTER(pcd, CONFIG_PCD_LOG_LEVEL);
2020

21-
/** Magic value written to indicate that a copy should take place. */
22-
#define PCD_CMD_MAGIC_COPY 0xb5b4b3b6
23-
/** Magic value written to indicate that a something failed. */
24-
#define PCD_CMD_MAGIC_FAIL 0x25bafc15
25-
/** Magic value written to indicate that a copy is done. */
26-
#define PCD_CMD_MAGIC_DONE 0xf103ce5d
27-
/** Magic value written to indicate that a version number read should take place. */
28-
#define PCD_CMD_MAGIC_READ_VERSION 0xdca345ea
29-
3021
#ifdef CONFIG_PCD_APP
3122

3223
#include <hal/nrf_reset.h>
@@ -49,13 +40,6 @@ K_TIMER_DEFINE(network_core_finished_check_timer,
4940

5041
#endif /* CONFIG_PCD_APP */
5142

52-
struct pcd_cmd {
53-
uint32_t magic; /* Magic value to identify this structure in memory */
54-
const void *data; /* Data to copy*/
55-
size_t len; /* Number of bytes to copy */
56-
off_t offset; /* Offset to store the flash image in */
57-
} __aligned(4);
58-
5943
static struct pcd_cmd *cmd = (struct pcd_cmd *)PCD_CMD_ADDRESS;
6044

6145
void pcd_fw_copy_invalidate(void)
@@ -71,6 +55,8 @@ enum pcd_status pcd_fw_copy_status_get(void)
7155
return PCD_STATUS_READ_VERSION;
7256
} else if (cmd->magic == PCD_CMD_MAGIC_DONE) {
7357
return PCD_STATUS_DONE;
58+
} else if (cmd->magic == PCD_CMD_MAGIC_LOCK_DEBUG) {
59+
return PCD_STATUS_LOCK_DEBUG;
7460
}
7561

7662
return PCD_STATUS_FAILED;
@@ -278,12 +264,11 @@ int pcd_network_core_update(const void *src_addr, size_t len)
278264
return network_core_update(src_addr, len, true);
279265
}
280266

281-
void pcd_lock_ram(void)
267+
void pcd_lock_ram(bool lock_conf)
282268
{
283269
uint32_t region = PCD_CMD_ADDRESS/CONFIG_NRF_SPU_RAM_REGION_SIZE;
284270

285-
nrf_spu_ramregion_set(NRF_SPU, region, false, NRF_SPU_MEM_PERM_READ,
286-
true);
271+
nrf_spu_ramregion_set(NRF_SPU, region, false, NRF_SPU_MEM_PERM_READ, lock_conf);
287272
}
288273

289274
#endif /* CONFIG_PCD_APP */

0 commit comments

Comments
 (0)