Skip to content

Commit c4dfa3f

Browse files
dgarskedanielinux
authored andcommitted
PR cleanups based on peer review. Fixes to support < 10KB wolfBoot for either RSA2048 or ED25519. Partition scheme allows 10KB application partition or (6KB actual due to NVM write once). Made space using NO_MPU=1, RAM_CODE=0, USE_SLOW_SHA256 and !FLASH_SECURABLE_MEMORY_SUPPORT.
1 parent a1d584b commit c4dfa3f

File tree

9 files changed

+69
-154
lines changed

9 files changed

+69
-154
lines changed

arch.mk

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,14 @@ ifeq ($(ARCH),ARM)
8787
ifeq ($(TARGET),stm32c0)
8888
CORTEX_M0=1
8989
ARCH_FLASH_OFFSET=0x08000000
90-
91-
# Enable this feature for secure memory support
92-
# Makes the flash sectors for the bootloader unacessible from the application
93-
# Requires using the STM32CubeProgrammer to set FLASH_SECR -> SEC_SIZE pages
94-
CFLAGS+=-DFLASH_SECURABLE_MEMORY_SUPPORT
9590
endif
9691

9792
ifeq ($(TARGET),stm32g0)
9893
CORTEX_M0=1
9994
ARCH_FLASH_OFFSET=0x08000000
10095

10196
# Enable this feature for secure memory support
102-
# Makes the flash sectors for the bootloader unacessible from the application
97+
# Makes the flash sectors for the bootloader unaccessible from the application
10398
# Requires using the STM32CubeProgrammer to set FLASH_SECR -> SEC_SIZE pages
10499
CFLAGS+=-DFLASH_SECURABLE_MEMORY_SUPPORT
105100
endif

config/examples/stm32c0.config

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
11
ARCH?=ARM
22
TARGET?=stm32c0
3-
SIGN?=ED25519
4-
#Using SHA384 with ED25519 saves about 2kB without SHA256 code
5-
HASH?=SHA384
3+
#Supports ED25519/SHA384 or RSA2048/SHA256
4+
SIGN?=RSA2048
5+
HASH?=SHA256
66
DEBUG?=0
77
VTOR?=1
88
CORTEX_M0?=1
99
NO_ASM?=0
10+
NO_MPU?=1
1011
EXT_FLASH?=0
1112
SPI_FLASH?=0
1213
ALLOW_DOWNGRADE?=0
1314
NVM_FLASH_WRITEONCE?=1
1415
WOLFBOOT_VERSION?=0
1516
V?=0
1617
SPMATH?=1
17-
RAM_CODE?=1
1818
DUALBANK_SWAP?=0
19-
#Max APP size is 4kB
19+
20+
# Enable this to support wolfBoot self-update
21+
RAM_CODE?=0
22+
23+
# Use slightly smaller SHA2-256
24+
CFLAGS_EXTRA+=-DUSE_SLOW_SHA256
25+
26+
# Enable this feature for secure memory support
27+
# Makes the flash sectors for the bootloader unaccessible from the application
28+
# Requires using the STM32CubeProgrammer to set FLASH_SECR -> SEC_SIZE pages
29+
#CFLAGS_EXTRA+=-DFLASH_SECURABLE_MEMORY_SUPPORT
30+
31+
32+
#Partition: Boot: 10KB, App, 10KB
2033
WOLFBOOT_PARTITION_SIZE?=0x2000
2134
WOLFBOOT_SECTOR_SIZE?=0x800
22-
#Max WOLFBOOT size is 14kB. Currently only 10kB
23-
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08003800
24-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08005800
35+
#Max WOLFBOOT size is 10KB
36+
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08002800
37+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08005000
2538
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x08007800
2639

27-
#For a debug/not size optimized layout:
40+
#Example debugging partition for larger wolfBoot
41+
#Partition: Boot: 22KB, App, 4K
2842
#DEBUG?=1
2943
#WOLFBOOT_PARTITION_SIZE?=0x1000
3044
#WOLFBOOT_SECTOR_SIZE?=0x800
3145
#WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08005800
3246
#WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08006800
3347
#WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x08007800
34-

docs/HAL.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ and the application initiating the firmware upgrade through the application libr
88
ensuring that the MCU is running at full speed during boot (to optimize the
99
verification of the signatures).
1010

11-
The implementation of the hardware-specific calls for each platform are grouped in
11+
The implementation of the hardware-specific calls for each platform are grouped in
1212
a single c file in the [hal](../hal) directory.
1313

14-
The directory also contains a platform-specific linker script for each supported MCU,
15-
with the same name and the `.ld` extension. This is used to link the bootloader's
16-
firmware on the specific hardware, exporting all the necessary symbols for flash
14+
The directory also contains a platform-specific linker script for each supported MCU,
15+
with the same name and the `.ld` extension. This is used to link the bootloader's
16+
firmware on the specific hardware, exporting all the necessary symbols for flash
1717
and RAM boundaries.
1818

1919
## Supported platforms
2020

2121
The following platforms are supported in the current version:
22-
- STM32F4, STM32L5, STM32L0, STM32F7, STM32H7, STM32G0, STM32C0
22+
- STM32C0, STM32F4, STM32F7, STM32G0, STM32H7, STM32L5, STM32L0, STM32U5, STM32WB55
2323
- nRF52
2424
- Atmel samR21
2525
- TI cc26x2
@@ -34,8 +34,8 @@ be implemented for each supported target:
3434
`void hal_init(void)`
3535

3636
This function is called by the bootloader at the very beginning of the execution.
37-
Ideally, the implementation provided configures the clock settings for the target
38-
microcontroller, to ensure that it runs at at the required speed to shorten the
37+
Ideally, the implementation provided configures the clock settings for the target
38+
microcontroller, to ensure that it runs at at the required speed to shorten the
3939
time required for the cryptography primitives to verify the firmware images.
4040

4141
`void hal_flash_unlock(void)`
@@ -76,11 +76,11 @@ that the state of the microcontroller is restored to its original settings.
7676

7777
WolfBoot can be compiled with the makefile option `EXT_FLASH=1`. When the external flash support is
7878
enabled, update and swap partitions can be associated to an external memory, and will use alternative
79-
HAL function for read/write/erase access.
80-
To associate the update or the swap partition to an external memory, define `PART_UPDATE_EXT` and/or
79+
HAL function for read/write/erase access.
80+
To associate the update or the swap partition to an external memory, define `PART_UPDATE_EXT` and/or
8181
`PART_SWAP_EXT`, respectively.
8282

83-
The following functions are used to access the external memory, and must be defined when `EXT\_FLASH`
83+
The following functions are used to access the external memory, and must be defined when `EXT\_FLASH`
8484
is on:
8585

8686
`int ext_flash_write(uintptr_t address, const uint8_t *data, int len)`
@@ -96,15 +96,15 @@ or a negative value in case of failure.
9696
This function provides an indirect read of the external memory, using the
9797
driver's specific interface. `address` is the offset from the beginning of the
9898
addressable space in the device, `data` is a pointer where payload is stored upon a successful
99-
call, and `len` is the maximum size allowed for the payload. `ext_flash_read` should return 0
99+
call, and `len` is the maximum size allowed for the payload. `ext_flash_read` should return 0
100100
upon success, or a negative value in case of failure.
101101

102102
`int ext_flash_erase(uintptr_t address, int len)`
103103

104104
Called by the bootloader to erase part of the external memory.
105105
Erase operations must be performed via the specific interface of the target driver (e.g. SPI flash).
106-
`address` marks the start of the area relative to the device, that the bootloader wants to erase,
107-
and `len` specifies the size of the area to be erased. This function must take into account the
106+
`address` marks the start of the area relative to the device, that the bootloader wants to erase,
107+
and `len` specifies the size of the area to be erased. This function must take into account the
108108
geometry of the sectors, and erase all the sectors in between.
109109

110110
`void ext_flash_lock(void)`

docs/Targets.md

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -427,50 +427,46 @@ mon reset init
427427

428428
## STM32C0
429429

430-
Supports STM32C0x0/STM32C0x1.
430+
Supports STM32C0x0/STM32C0x1. Instructions are for the STM Nucleo-C031C6 dev board.
431431

432-
Example and instructions are for the STM Nucleo-C031C6 dev board using the
433-
STM32Cube
432+
Tested build configurations:
433+
* With RSA2048 and SHA2-256 the code size is 9724 and it boots in under 1 second.
434+
* With ED25519 and SHA2-384 the code size is 10024 and takes about 10 seconds for the LED to turn on.
434435

435436
Example 32KB partitioning on STM32-G070:
436437

437438
- Sector size: 2KB
438-
- Wolfboot partition size: 14KB
439-
- Application partition size: 8 KB
439+
- Wolfboot partition size: 10KB
440+
- Application partition size: 10 KB
440441
- Swap size 2KB
441442

442443
```C
443444
#define WOLFBOOT_SECTOR_SIZE 0x800 /* 2 KB */
444-
#define WOLFBOOT_PARTITION_BOOT_ADDRESS 0x08004000 /* offset 16kB to 24kB */
445-
#define WOLFBOOT_PARTITION_SIZE 0x2000 /* 8 KB */
446-
#define WOLFBOOT_PARTITION_UPDATE_ADDRESS 0x08006000 /* offset 24kB to 32kB */
447-
#define WOLFBOOT_PARTITION_SWAP_ADDRESS 0x08003800 /* offset 14kB to 16kB */
445+
#define WOLFBOOT_PARTITION_BOOT_ADDRESS 0x08002800 /* at 10KB */
446+
#define WOLFBOOT_PARTITION_SIZE 0x2800 /* 10 KB */
447+
#define WOLFBOOT_PARTITION_UPDATE_ADDRESS 0x08005000 /* at 20KB */
448+
#define WOLFBOOT_PARTITION_SWAP_ADDRESS 0x08007800 /* at 30KB */
448449
```
449450

450451
### Building STM32C0
451-
xxx
452-
Reference configuration (see [/config/examples/stm32g0.config](/config/examples/stm32g0.config)).
453-
You can copy this to wolfBoot root as `.config`: `cp ./config/examples/stm32g0.config .config`.
452+
453+
Reference configuration (see [/config/examples/stm32c0.config](/config/examples/stm32c0.config)).
454+
455+
You can copy this to wolfBoot root as `.config`: `cp ./config/examples/stm32c0.config .config`.
454456
To build you can use `make`.
455457

456-
The TARGET for this is `stm32g0`: `make TARGET=stm32g0`.
458+
The TARGET for this is `stm32c0`: `make TARGET=stm32c0`.
457459
The option `CORTEX_M0` is automatically selected for this target.
458460
The option `NVM_FLASH_WRITEONCE=1` is mandatory on this target, since the IAP driver does not support
459461
multiple writes after each erase operation.
460462

461-
This target also supports secure memory protection on the bootloader region
462-
using the `FLASH_CR:SEC_PROT` and `FLASH_SECT:SEC_SIZE` registers. This is the
463-
number of 2KB pages to block access to from the 0x8000000 base address.
464-
465463
```
466464
STM32_Programmer_CLI -c port=swd mode=hotplug -ob SEC_SIZE=0x10
467465
```
468466

469-
For RAMFUNCTION support (required for SEC_PROT) make sure `RAM_CODE=1`.
470-
471467
### STM32C0 Programming
472-
xxx
473-
Compile requirements: `make TARGET=stm32g0 NVM_FLASH_WRITEONCE=1`
468+
469+
Compile requirements: `make TARGET=stm32c0 NVM_FLASH_WRITEONCE=1`
474470

475471
The output is a single `factory.bin` that includes `wolfboot.bin` and `test-app/image_v1_signed.bin` combined together.
476472
This should be programmed to the flash start address `0x08000000`.
@@ -482,7 +478,7 @@ STM32_Programmer_CLI -c port=swd -d factory.bin 0x08000000
482478
```
483479

484480
### STM32C0 Debugging
485-
xxx
481+
486482
Use `make DEBUG=1` and program firmware again.
487483

488484
Start GDB server on port 3333:

hal/stm32c0.c

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
# error "wolfBoot STM32C0 HAL: no WRITEONCE support detected. Please define NVM_FLASH_WRITEONCE"
2727
#endif
2828

29-
/* XXX Debug only! */
30-
#define NO_FLASH_SEC_SIZE_CHECK 1
31-
3229
/* STM32 C0 register configuration */
3330

3431
/* Assembly helpers */
@@ -138,8 +135,6 @@ static void RAMFUNCTION flash_clear_errors(void)
138135
{
139136
/* Consider only writing here as there is no reason to read first (rc_w1),
140137
* unless other error bits are set*/
141-
/* FLASH_SR = (FLASH_SR_SIZERR | FLASH_SR_PGAERR | FLASH_SR_WRPERR |
142-
FLASH_SR_PROGERR); */
143138
FLASH_SR |= (FLASH_SR_SIZERR | FLASH_SR_PGAERR | FLASH_SR_WRPERR |
144139
FLASH_SR_PROGERR);
145140
}
@@ -233,17 +228,11 @@ static void clock_pll_off(void)
233228
reg32 &= ~((1 << 1) | (1 << 0));
234229
RCC_CFGR = (reg32 | RCC_CFGR_SW_HSISYS);
235230
DMB();
236-
#if 0
237-
/* Turn off PLL */
238-
RCC_CR &= ~RCC_CR_PLLON;
239-
DMB();
240-
#endif
241231
}
242232

243233
/* This implementation will setup HSI RC 48 MHz as System Clock Source, set
244-
* flash wait state to 2, and set all peripherals to 16MHz (div4)
245-
*
246-
* */
234+
* flash wait state to 1, and set all peripherals to 16MHz (div4)
235+
*/
247236
static void clock_pll_on(int powersave)
248237
{
249238
uint32_t reg32;
@@ -276,45 +265,6 @@ static void clock_pll_on(int powersave)
276265
RCC_CFGR = (reg32 | RCC_CFGR_SW_HSISYS);
277266
DMB();
278267

279-
#if 0
280-
/* Disable PLL */
281-
RCC_CR &= ~RCC_CR_PLLON;
282-
283-
/* Set prescalers for AHB, ADC, ABP1, ABP2. */
284-
reg32 = RCC_CFGR;
285-
reg32 &= ~(0xF0); /* don't change bits [0-3] that were previously set */
286-
RCC_CFGR = (reg32 | (hpre << 8)); /* RM0444 - 5.4.3 - RCC_CFGR */
287-
DMB();
288-
reg32 = RCC_CFGR;
289-
reg32 &= ~(0x1C00); /* don't change bits [0-14] */
290-
RCC_CFGR = (reg32 | (ppre << 12)); /* RM0444 - 5.4.3 - RCC_CFGR */
291-
DMB();
292-
293-
/* Set PLL config */
294-
reg32 = RCC_PLLCFGR;
295-
reg32 |= RCC_PLLCFGR_PLLSRC_HSI16;
296-
reg32 |= ((pllm - 1) << 4);
297-
reg32 |= plln << 8;
298-
reg32 |= ((pllp - 1) << 17);
299-
reg32 |= ((pllr - 1) << 29);
300-
RCC_PLLCFGR = reg32;
301-
302-
DMB();
303-
/* Enable PLL oscillator and wait for it to stabilize. */
304-
RCC_PLLCFGR |= RCC_PLLCFGR_PLLR_EN;
305-
RCC_CR |= RCC_CR_PLLON;
306-
DMB();
307-
while ((RCC_CR & RCC_CR_PLLRDY) == 0) {};
308-
309-
/* Select PLL as SYSCLK source. */
310-
reg32 = RCC_CFGR;
311-
reg32 &= ~((1 << 1) | (1 << 0));
312-
RCC_CFGR = (reg32 | RCC_CFGR_SW_PLL);
313-
DMB();
314-
315-
/* Wait for PLL clock to be selected. */
316-
while ((RCC_CFGR & ((1 << 1) | (1 << 0))) != RCC_CFGR_SW_PLL) {};
317-
#endif
318268
/* SYSCFG, COMP and VREFBUF clock enable */
319269
APB2_CLOCK_ER |= SYSCFG_APB2_CLOCK_ER_VAL;
320270
}

hal/stm32c0.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
MEMORY
22
{
33
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = @BOOTLOADER_PARTITION_SIZE@
4-
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00002FFF
4+
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 12K-1
55
}
66

77
SECTIONS

test-app/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ endif
267267
ifeq ($(TARGET),stm32g0)
268268
CFLAGS+=-DNVM_FLASH_WRITEONCE=1
269269
endif
270+
ifeq ($(TARGET),stm32c0)
271+
CFLAGS+=-DNVM_FLASH_WRITEONCE=1
272+
endif
270273

271274
ifeq ($(TARGET),hifive1.freedom)
272275
CFLAGS+=-I$(FREEDOM_E_SDK)/freedom-metal/ -D__METAL_MACHINE_HEADER=\"$(FREEDOM_E_SDK)/bsp/sifive-hifive1/metal.h\"

test-app/app_stm32c0.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* main.c
1+
/* app_stm32c0.c
22
*
33
* Test bare-metal boot-led-on application
44
*
@@ -36,4 +36,3 @@ void main(void) {
3636
;
3737
}
3838
#endif /* PLATFORM_stm32c0 */
39-

0 commit comments

Comments
 (0)