Skip to content

Commit 24323bc

Browse files
MarkusLassilarlubos
authored andcommitted
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. If TF-M is used, delay locking of the PCD commands memory area to be done in TF-M. NCSDK-17920 Signed-off-by: Markus Lassila <markus.lassila@nordicsemi.no>
1 parent 4d6b85d commit 24323bc

File tree

6 files changed

+125
-46
lines changed

6 files changed

+125
-46
lines changed

include/dfu/pcd.h

+5-22
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

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
#ifndef CONFIG_SOC_SERIES_NRF53X
21+
#error "PCD is only supported on nRF53 series"
22+
#endif
23+
24+
#ifdef CONFIG_PCD_CMD_ADDRESS
25+
/* PCD command block location is static. */
26+
#define PCD_CMD_ADDRESS CONFIG_PCD_CMD_ADDRESS
27+
28+
#else
29+
/* PCD command block location is configured with Partition Manager. */
30+
#include <pm_config.h>
31+
32+
#ifdef PM_PCD_SRAM_ADDRESS
33+
/* PCD command block is in this domain, we are compiling for application core. */
34+
#define PCD_CMD_ADDRESS PM_PCD_SRAM_ADDRESS
35+
#else
36+
/* PCD command block is in a different domain, we are compiling for network core.
37+
* Extra '_' since its in a different domain.
38+
*/
39+
#define PCD_CMD_ADDRESS PM__PCD_SRAM_ADDRESS
40+
#endif /* PM_PCD_SRAM_ADDRESS */
41+
42+
#endif /* CONFIG_PCD_CMD_ADDRESS */
43+
44+
/** Magic value written to indicate that a copy should take place. */
45+
#define PCD_CMD_MAGIC_COPY 0xb5b4b3b6
46+
/** Magic value written to indicate that debug should be locked. */
47+
#define PCD_CMD_MAGIC_LOCK_DEBUG 0xb6f249ec
48+
/** Magic value written to indicate that a something failed. */
49+
#define PCD_CMD_MAGIC_FAIL 0x25bafc15
50+
/** Magic value written to indicate that a copy is done. */
51+
#define PCD_CMD_MAGIC_DONE 0xf103ce5d
52+
/** Magic value written to indicate that a version number read should take place. */
53+
#define PCD_CMD_MAGIC_READ_VERSION 0xdca345ea
54+
55+
struct pcd_cmd {
56+
uint32_t magic; /* Magic value to identify this structure in memory */
57+
const void *data; /* Data to copy*/
58+
size_t len; /* Number of bytes to copy */
59+
__INTPTR_TYPE__ offset; /* Offset to store the flash image in */
60+
} __aligned(4);
61+
62+
#define PCD_CMD ((volatile struct pcd_cmd * const)(PCD_CMD_ADDRESS))
63+
64+
static inline void pcd_write_cmd_lock_debug(void)
65+
{
66+
*PCD_CMD = (struct pcd_cmd){
67+
.magic = PCD_CMD_MAGIC_LOCK_DEBUG,
68+
};
69+
}
70+
71+
static inline bool pcd_read_cmd_done(void)
72+
{
73+
return PCD_CMD->magic == PCD_CMD_MAGIC_DONE;
74+
}
75+
76+
static inline bool pcd_read_cmd_lock_debug(void)
77+
{
78+
return PCD_CMD->magic == PCD_CMD_MAGIC_LOCK_DEBUG;
79+
}
80+
81+
#endif /* PCD_COMMON_H__ */
82+
83+
/**@} */

samples/nrf5340/netboot/src/main.c

+27-4
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
{
@@ -39,10 +42,26 @@ int main(void)
3942

4043
uint32_t s0_addr = s0_address_read();
4144
bool valid = false;
42-
uint8_t status = pcd_fw_copy_status_get();
45+
46+
switch (pcd_fw_copy_status_get()) {
47+
#ifdef CONFIG_PCD_LOCK_NETCORE_DEBUG
48+
case PCD_STATUS_LOCK_DEBUG:
49+
nrfx_nvmc_word_write((uint32_t)&NRF_UICR_NS->APPROTECT,
50+
UICR_APPROTECT_PALL_Protected);
51+
while (!nrfx_nvmc_write_done_check())
52+
;
53+
54+
pcd_done();
55+
56+
/* Success, waiting to be rebooted */
57+
while (1)
58+
;
59+
CODE_UNREACHABLE;
60+
break;
61+
#endif
4362

4463
#ifdef CONFIG_PCD_READ_NETCORE_APP_VERSION
45-
if (status == PCD_STATUS_READ_VERSION) {
64+
case PCD_STATUS_READ_VERSION:
4665
err = pcd_find_fw_version();
4766
if (err < 0) {
4867
printk("Unable to find valid firmware version %d\n\r", err);
@@ -54,10 +73,10 @@ int main(void)
5473
while (1)
5574
;
5675
CODE_UNREACHABLE;
57-
}
76+
break;
5877
#endif
5978

60-
if (status == PCD_STATUS_COPY) {
79+
case PCD_STATUS_COPY:
6180
/* First we validate the data where the PCD CMD tells
6281
* us that we can find it.
6382
*/
@@ -94,6 +113,10 @@ int main(void)
94113
while (1)
95114
;
96115
CODE_UNREACHABLE;
116+
break;
117+
118+
default:
119+
break;
97120
}
98121

99122
err = fprotect_area(PM_APP_ADDRESS, PM_APP_SIZE);

subsys/pcd/Kconfig

+5
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

+4-19
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 */

west.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ manifest:
132132
compare-by-default: true
133133
- name: mcuboot
134134
repo-path: sdk-mcuboot
135-
revision: 720fa02787366f9f787b847194f6814921147770
135+
revision: 68b96b802cdeef77ce4200e776afa46f6d3cfb66
136136
path: bootloader/mcuboot
137137
- name: qcbor
138138
url: https://github.com/laurencelundblade/QCBOR

0 commit comments

Comments
 (0)