Skip to content

Commit b6ed896

Browse files
committed
Cleanup initialization code for rp2350.
+ TZEN=1 in config + Changed SRAM settings (allocated 256KB to the secure supervisor) + Updated documentation + Added FLASH_NSC area for sg stubs
1 parent 6681e54 commit b6ed896

File tree

6 files changed

+77
-54
lines changed

6 files changed

+77
-54
lines changed

IDE/pico-sdk/rp2350/README.md

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
## wolfBoot port for rp2350 (Raspberry pi pico 2)
22

3+
### Support for TrustZone
4+
5+
By default, TZEN=1 is enabled in the provided configuration. wolfBoot will run
6+
from the Secure domain, and will stage the application in the Non-Secure domain.
7+
8+
The flash memory is divided as follows:
9+
10+
- wolfBoot partition (0x10000000 - 0x1003FFFF), 224 KB
11+
- Non-secure callable partition (for secure gateway) (0x10038000 - 0x1003FFFF), 32 KB
12+
- Boot partition (0x10040000 - 0x1007FFFF), 768 KB
13+
- Update partition (0x10100000 - 0x1013FFFF), 768 KB
14+
- Unused flash space (0x101C1000 - 0x101FFFFF), 252 KB
15+
- Swap space (0x101C0000 - 0x101C0FFF), 4 KB
16+
17+
The SRAM bank0 is assigned to the Secure domain, and enforced using both SAU and `ACCESS_CONTROL` registers.
18+
19+
- Secure SRAM0-3: 0x20000000 - 0x2003FFFF, 256 KB
20+
- Non-secure SRAM4-7: 0x20040000 - 0x2007FFFF, 256 KB
21+
- Non-secure stack for application SRAM8-9: 0x20080000 - 0x20081FFF, 8 KB
22+
23+
```
24+
325
### Requirements
426
527
#### External debugger
@@ -31,18 +53,10 @@ From wolfBoot root directory, copy the example configuration:
3153
cp config/examples/rp2350.config .config
3254
```
3355
34-
By default, the config file indicates the following partition layout:
35-
36-
```
37-
wolfBoot partition: 256 KB, at address 0x10000000 to 0x1003FFFF
38-
Boot partition: 768 KB, at address 0x10040000 to 0x1007FFFF
39-
Update partition: 768 KB, at address 0x10100000 to 0x1013FFFF
40-
Swap space: 4 KB, at address 0x101C0000 to 0x101C0FFF
41-
Unused flash space: 252 KB, at address 0x101C1000 to 0x101FFFFF
42-
```
43-
4456
You can now edit the .config file to change partition sizes/offsets, algorithms,
45-
add/remove features, etc.
57+
disable trustzone, add/remove features, etc.
58+
59+
When TZEN=0, the application will run in the Secure domain.
4660
4761
When the configuration is complete, run `make`. This will:
4862
@@ -61,6 +75,7 @@ return to this directory and run:
6175
6276
```
6377
cd wolfboot
78+
export PICO_SDK_PATH=...
6479
./build-wolfboot.sh
6580
```
6681
@@ -69,7 +84,8 @@ This version of wolfboot incorporates the `.boot2` sequence needed to enable
6984
the QSPI device, provided by the pico-sdk and always embedded in all
7085
applications.
7186
72-
wolfboot.bin contains the bootloader, configured as follows:
87+
wolfboot.bin contains the bootloader, and can be loaded into the RP2350, starting at address 0x10000000.
88+
The script will automatically upload the binary if a JLink debugger is connected.
7389
7490
### Building and uploading the application
7591
@@ -85,6 +101,13 @@ The linker script included is modified to change the application entry point
85101
from 0x10000000 to 0x10040400, which is the start of the application code,
86102
taking into account the wolfBoot header size.
87103
104+
The application is signed with the wolfBoot private key, and the signature is
105+
stored in the manifest header of the application binary.
106+
107+
The output file `build/blink_v1_signed.bin` is automatically uploaded to the RP2350 if a JLink debugger is connected.
108+
The application image is stored in the boot partition, starting at address 0x10040000.
109+
The entry point of the application (0x10040400), set in the linker script `hal/rp2350-app.ld`, is the start of the application code, taking into account the wolfBoot header size.
110+
88111
89112
### Testing the application
90113
@@ -93,3 +116,5 @@ every 500ms.
93116
94117
If the above steps are successful, the LED on the board should start blinking.
95118
119+
The code has been tested on a Seeed studio XIAO RP2350 board.
120+

IDE/pico-sdk/rp2350/test-app/runtime.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
#include <stdint.h>
2-
void runtime_init_bootrom_reset(void)
3-
{
4-
}
5-
6-
void runtime_init_clocks(void)
7-
{
8-
}
9-
10-
112
typedef void (*preinit_fn_t)(void);
123

134
void runtime_init_cpasr(void)

config/examples/rp2350.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ARCH?=ARM
2-
TZEN?=0
2+
TZEN?=1
33
TARGET?=rp2350
44
SIGN?=ECC256
55
HASH?=SHA256

hal/rp2350-app.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ MEMORY
2525
{
2626
BOOT(rx) : ORIGIN = 0x10000000, LENGTH = 0x40400
2727
FLASH(rx) : ORIGIN = 0x10040400, LENGTH = 0x1D0000
28-
RAM(rwx) : ORIGIN = 0x20010000, LENGTH = 0x6E000
29-
SCRATCH_X(rwx) : ORIGIN = 0x2007E000, LENGTH = 4k
30-
SCRATCH_Y(rwx) : ORIGIN = 0x2007F000, LENGTH = 4k
28+
RAM(rwx) : ORIGIN = 0x20040000, LENGTH = 0x40000
29+
SCRATCH_X(rwx) : ORIGIN = 0x20080000, LENGTH = 4k
30+
SCRATCH_Y(rwx) : ORIGIN = 0x20081000, LENGTH = 4k
3131
}
3232

3333
ENTRY(_entry_point)

hal/rp2350.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* rp2350.c
22
*
3-
* Stubs for custom HAL implementation. Defines the
4-
* functions used by wolfboot for a specific target.
3+
* Custom HAL implementation. Defines the
4+
* functions used by wolfboot for raspberry-pi pico2 (rp2350)
55
*
6-
* Copyright (C) 2021 wolfSSL Inc.
6+
* Copyright (C) 2025 wolfSSL Inc.
77
*
88
* This file is part of wolfBoot.
99
*
@@ -110,9 +110,6 @@
110110

111111
#endif
112112

113-
114-
115-
116113
#ifdef __WOLFBOOT
117114
void hal_init(void)
118115
{
@@ -121,7 +118,6 @@ void hal_init(void)
121118
#endif
122119
}
123120

124-
125121
#ifdef TZEN
126122
static void rp2350_configure_sau(void)
127123
{
@@ -130,8 +126,8 @@ static void rp2350_configure_sau(void)
130126
sau_init_region(0, 0x10000000, 0x1002FFFF, 1); /* Secure flash */
131127
sau_init_region(1, 0x10030000, 0x1003FFFF, 1); /* Non-secure-callable flash */
132128
sau_init_region(2, 0x10040000, 0x101FFFFF, 0); /* Non-secure flash */
133-
sau_init_region(3, 0x20000000, 0x20007FFF, 1); /* Secure RAM */
134-
sau_init_region(4, 0x20008000, 0x20081FFF, 0); /* Non-secure RAM */
129+
sau_init_region(3, 0x20000000, 0x2003FFFF, 1); /* Secure RAM (Low 256K) */
130+
sau_init_region(4, 0x20040000, 0x20081FFF, 0); /* Non-secure RAM (High 256 + 8K) */
135131
sau_init_region(6, 0x40000000, 0x5FFFFFFF, 0); /* Non-secure peripherals */
136132
sau_init_region(7, 0xD0000000, 0xDFFFFFFF, 0); /* Non-secure SIO region */
137133

@@ -159,37 +155,32 @@ static void rp2350_configure_nvic(void)
159155
static void rp2350_configure_access_control(void)
160156
{
161157
int i;
162-
/* Reset ACCESSCTRL */
163-
const uint32_t secure_fl = (ACCESS_BITS_SU | ACCESS_BITS_SP | ACCESS_BITS_DMA | ACCESS_BITS_DBG | ACCESS_BITS_CORE0 | ACCESS_BITS_CORE1) | ACCESS_MAGIC;
158+
const uint32_t secure_fl = (ACCESS_BITS_SU | ACCESS_BITS_SP | ACCESS_BITS_DMA | ACCESS_BITS_DBG | ACCESS_BITS_CORE0) | ACCESS_MAGIC;
164159
const uint32_t non_secure_fl = (ACCESS_BITS_NSU | ACCESS_BITS_NSP | ACCESS_BITS_DMA | ACCESS_BITS_DBG | ACCESS_BITS_CORE0 | ACCESS_BITS_CORE1) | ACCESS_MAGIC;
165160

166-
//ACCESS_CONTROL_CFGRESET = 1;
167-
/* Corresponding regions for the secure flash and RAM */
168-
/*
169-
for(i = 0; i < 2; i++) {
161+
/* Set access control to Secure for lower RAM (0x20000000 - 0x2003FFFF) */
162+
for (i = 0; i < 4; i ++)
170163
ACCESS_CONTROL_SRAM(i) = secure_fl;
171-
}
172-
*/
173-
for (i = 0; i < 10; i++) {
164+
165+
/* Set access control to Non-secure for upper RAM (0x20040000 - 0x20081FFF) */
166+
for (i = 4; i < 10; i++)
174167
ACCESS_CONTROL_SRAM(i) = non_secure_fl | secure_fl;
175-
}
168+
169+
/* Set access control for peripherals */
176170
ACCESS_CONTROL_ROM = secure_fl | non_secure_fl;
177171
ACCESS_CONTROL_XIP_MAIN = non_secure_fl | secure_fl;
178172
ACCESS_CONTROL_DMA = non_secure_fl;
179173
ACCESS_CONTROL_TRNG = secure_fl;
180-
ACCESS_CONTROL_SYSCFG = secure_fl | non_secure_fl;
174+
ACCESS_CONTROL_SYSCFG = secure_fl;
181175
ACCESS_CONTROL_SHA256 = secure_fl;
182-
ACCESS_CONTROL_GPIOMASK0 = 0xFFFFFFFF;
183-
ACCESS_CONTROL_GPIOMASK1 = 0xFFFFFFFF;
184176
ACCESS_CONTROL_IO_BANK0 = non_secure_fl | secure_fl;
185177
ACCESS_CONTROL_IO_BANK1 = non_secure_fl | secure_fl;
186178
ACCESS_CONTROL_PADS_BANK0 = non_secure_fl | secure_fl;
187-
// ACCESS_CONTROL_FORCE_CORE_NS = (1 << 1); /* Force core 1 to non-secure */
188179
ACCESS_CONTROL_PIO0 = non_secure_fl | secure_fl;
189180
ACCESS_CONTROL_PIO1 = non_secure_fl | secure_fl;
190181
ACCESS_CONTROL_PIO2 = non_secure_fl | secure_fl;
191182

192-
ACCESS_CONTROL_I2C0 = non_secure_fl|secure_fl;
183+
ACCESS_CONTROL_I2C0 = non_secure_fl |secure_fl;
193184
ACCESS_CONTROL_I2C1 = non_secure_fl | secure_fl;
194185
ACCESS_CONTROL_PWM = non_secure_fl | secure_fl;
195186
ACCESS_CONTROL_SPI0 = non_secure_fl | secure_fl;
@@ -201,10 +192,18 @@ static void rp2350_configure_access_control(void)
201192
ACCESS_CONTROL_ADC = non_secure_fl | secure_fl;
202193
ACCESS_CONTROL_RESETS = non_secure_fl | secure_fl;
203194

195+
/* Force core 1 to non-secure */
196+
ACCESS_CONTROL_FORCE_CORE_NS = (1 << 1) | ACCESS_MAGIC;
197+
198+
/* GPIO masks: Each bit represents "NS allowed" for a GPIO pin */
199+
ACCESS_CONTROL_GPIOMASK0 = 0xFFFFFFFF;
200+
ACCESS_CONTROL_GPIOMASK1 = 0xFFFFFFFF;
201+
204202
CPACR |= 0x000000FF; /* Enable access to coprocessors CP0-CP7 */
205203
NSACR |= 0x000000FF; /* Enable non-secure access to coprocessors CP0-CP7 */
206204

207-
// ACCESS_CONTROL_LOCK = (1 << 0) | (1 << 1) | (1 << 3);
205+
/* Lock access control */
206+
ACCESS_CONTROL_LOCK = non_secure_fl | secure_fl;
208207
}
209208
#endif
210209

hal/rp2350.ld

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323

2424
MEMORY
2525
{
26-
FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 256k
27-
RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 24k
28-
SCRATCH_X(rwx) : ORIGIN = 0x20006000, LENGTH = 4k
29-
SCRATCH_Y(rwx) : ORIGIN = 0x20007000, LENGTH = 4k
26+
FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 0x38000
27+
FLASH_NSC(rx) : ORIGIN = 0x10038000, LENGTH = 0x8000
28+
29+
RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 0x3E000
30+
SCRATCH_X(rwx) : ORIGIN = 0x2003E000, LENGTH = 4k
31+
SCRATCH_Y(rwx) : ORIGIN = 0x2003F000, LENGTH = 4k
3032
}
3133

3234
ENTRY(_entry_point)
@@ -100,6 +102,12 @@ SECTIONS
100102
. = ALIGN(4);
101103
} > FLASH
102104

105+
.gnu.sgstubs :
106+
{
107+
*(.gnu.sgstubs*) /* Secure Gateway stubs */
108+
. = ALIGN(4);
109+
} >FLASH_NSC
110+
103111
/* Note the boot2 section is optional, and should be discarded if there is
104112
no reference to it *inside* the binary, as it is not called by the
105113
bootrom. (The bootrom performs a simple best-effort XIP setup and

0 commit comments

Comments
 (0)