diff --git a/.github/workflows/test-configs.yml b/.github/workflows/test-configs.yml index fe28089fd5..8373c4e2b0 100644 --- a/.github/workflows/test-configs.yml +++ b/.github/workflows/test-configs.yml @@ -1614,6 +1614,13 @@ jobs: config-file: ./config/examples/stm32wb.config make-args: WOLFHAL=1 BOARD=stm32wb_nucleo UART_FLASH=1 + stm32wb_wolfhal_uart_flash_encryption_test: + uses: ./.github/workflows/test-build.yml + with: + arch: arm + config-file: ./config/examples/stm32wb-uart-flash-encryption.config + make-args: WOLFHAL=1 BOARD=stm32wb_nucleo + # SPI_FLASH=1 variant: ext_flash_* is mapped to src/spi_flash.c in # include/hal.h, so hal/wolfhal.c must not build its own. stm32wb_wolfhal_spi_flash_test: diff --git a/Makefile b/Makefile index 92e58da67e..9fa276fbec 100644 --- a/Makefile +++ b/Makefile @@ -264,14 +264,14 @@ ifeq ($(WOLFHAL),1) ifeq ($(strip $(BOARD)),) $(error WOLFHAL=1 requires BOARD to be set, e.g. BOARD=stm32wb_nucleo) endif - ifeq ($(wildcard hal/boards/$(BOARD)/board.mk),) - $(error BOARD=$(BOARD) has no hal/boards/$(BOARD)/board.mk) + ifeq ($(wildcard $(BOARD_DIR)/board.mk),) + $(error BOARD=$(BOARD) has no $(BOARD_DIR)/board.mk) endif # wolfHAL backend: hal/wolfhal.o replaces hal/$(TARGET).o above. The # board's wolfHAL_board.c provides hal_init/hal_prepare_boot and the wolfHAL # device handles; board.mk pulls in chip drivers. - OBJS+=./hal/boards/$(BOARD)/wolfHAL_board.o - include hal/boards/$(BOARD)/board.mk + OBJS+=$(BOARD_DIR)/wolfHAL_board.o + include $(BOARD_DIR)/board.mk endif # Set only here, so that host-tool sub-makes which also include options.mk diff --git a/arch.mk b/arch.mk index f20e910bf3..3f988e7add 100644 --- a/arch.mk +++ b/arch.mk @@ -1375,7 +1375,9 @@ endif ifeq ($(WOLFHAL),1) WOLFHAL_ROOT?=$(WOLFBOOT_ROOT)/lib/wolfHAL - CFLAGS+=-I$(WOLFHAL_ROOT) -Ihal/boards/$(BOARD) + BOARD_DIR?=hal/boards/$(BOARD) + override BOARD_DIR:=$(if $(filter /%,$(BOARD_DIR)),$(BOARD_DIR),$(WOLFBOOT_ROOT)/$(BOARD_DIR)) + CFLAGS+=-DWOLFHAL -I$(WOLFHAL_ROOT) -I$(BOARD_DIR) endif diff --git a/docs/wolfHAL.md b/docs/wolfHAL.md index 8d7e55b156..5548e7c26b 100644 --- a/docs/wolfHAL.md +++ b/docs/wolfHAL.md @@ -52,12 +52,12 @@ config/examples/.config └─ TARGET= BOARD= WOLFHAL=1 arch.mk - └─ Sets WOLFHAL_ROOT, CFLAGS += -Ihal/boards/$(BOARD) + └─ Sets WOLFHAL_ROOT, BOARD_DIR, CFLAGS += -I$(BOARD_DIR) Makefile └─ OBJS += hal/wolfhal.o (replaces hal/$(TARGET).o) - └─ OBJS += hal/boards/$(BOARD)/wolfHAL_board.o - └─ include hal/boards/$(BOARD)/board.mk + └─ OBJS += $(BOARD_DIR)/wolfHAL_board.o + └─ include $(BOARD_DIR)/board.mk hal/wolfhal.c (generic — calls whal_Flash_Write, whal_Uart_Send, etc.) └─ #include "wolfHAL_board.h" (resolved via -I to the board directory) @@ -87,14 +87,36 @@ WOLFHAL=1 - `TARGET` keeps its usual meaning (the chip family). - `WOLFHAL=1` swaps the legacy `hal/$(TARGET).c` for the generic wolfHAL shim (`hal/wolfhal.c`) and includes the per-board build pieces. -- `BOARD` selects the board directory under `hal/boards/`. +- `BOARD` names the board. By default its files are read from + `hal/boards/$(BOARD)/`. +- `BOARD_DIR` (optional) overrides the board directory, so board files can live + outside the wolfBoot tree. See [External Boards](#external-boards). See `config/examples/stm32wba.config` and `config/examples/versal2_vek385.config` for complete examples. +### External Boards + +Set `BOARD_DIR` to a directory containing the same three files as an in-tree +board (`wolfHAL_board.h`, `wolfHAL_board.c` and `board.mk`): + +``` +make WOLFHAL=1 BOARD=my_board BOARD_DIR=/path/to/my_board +``` + +A relative `BOARD_DIR` is resolved against the wolfBoot root, for both the +bootloader and the test application. Notes for an external `board.mk`: + +- Paths relative to the wolfBoot root, such as wolfBoot's own linker scripts + (`$(call LSCRIPT_IN_DEFAULT,hal/stm32wb.ld)`), still work. Refer to files + shipped with the board as `$(BOARD_DIR)/...`. +- `wolfHAL_board.o` is built inside `BOARD_DIR`. Pass the same `BOARD` and + `BOARD_DIR` to `make clean` to remove it. + ## Adding a New Board -To add a new board, create a directory `hal/boards//` with three files: +To add a new board, create a directory `hal/boards//` (or an +external directory passed as `BOARD_DIR`) with three files: ### 1. `wolfHAL_board.h` — Driver Headers and Pin Enums @@ -274,6 +296,133 @@ that are substituted at build time. When `RAM_CODE=1`, these expand to flash into the `.data` section (loaded to RAM at startup). When `RAM_CODE` is not set, all code remains in flash as normal. +## UART Flash + +wolfHAL boards can place the update and swap partitions on a remote host over +UART with `UART_FLASH=1` (see [remote_flash.md](remote_flash.md)). The target +side uses the generic `src/uart_flash.c` protocol, and the host side is +`tools/uart-flash-server`. + +### How It Works + +`hal/wolfhal.c` provides the UART functions that `src/uart_flash.c` calls, on +top of the wolfHAL UART API and `BOARD_UART_FLASH_DEV`: + +| Function | Behavior | +|---|---| +| `uart_init` | No-op; the board configures the UART in `hal_init`, so `UART_FLASH_BITRATE` is ignored and the board's baud rate setting applies | +| `uart_tx` | Sends one byte with `whal_Uart_Send`; returns `1` on success, `-1` on error | +| `uart_rx` | Receives one byte with `whal_Uart_Recv`; returns `1` on success, `0` on timeout | + +With `WOLFHAL=1`, the build defines `WOLFHAL` and does not link the legacy +`hal/uart/uart_drv_.c` driver. + +On legacy targets, `uart_rx` returns immediately and `src/uart_flash.c` builds +its timeouts by calling it in a loop. With `WOLFHAL` defined, `uart_rx` waits +on the board's UART timeout instead, so `src/uart_flash.c` counts board +timeouts rather than loop iterations. Each ACK and each read byte get one board +timeout, and the final erase ACK gets `ERASE_TIMEOUT` (5) board timeouts. +While waiting for an ACK, bytes other than ACK are skipped rather than ending +the wait. `ext_flash_lock` and `ext_flash_unlock` also wait for an ACK that +`uart-flash-server` does not send, so each one costs a full timeout. + +### Board Requirements + +1. **`BOARD_UART_FLASH_DEV`.** Define it in `wolfHAL_board.h`. It can be the + debug UART: + + ```c + #define BOARD_UART_FLASH_DEV BOARD_UART_DEV + ``` + + If it is the same UART as debug output, do not combine `UART_FLASH=1` with + `DEBUG_UART=1`, since log text would corrupt the protocol stream. +2. **UART enabled without `DEBUG_UART`.** Initialize the UART clocks, pins and + device in `hal_init` (and deinitialize them in `hal_prepare_boot`) when + either `DEBUG_UART` or `UART_FLASH` is set, and link the GPIO and UART + drivers in `board.mk` for both: + + ```makefile + ifneq (,$(filter 1,$(DEBUG_UART) $(UART_FLASH))) + WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/gpio/_gpio.o + WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/uart/_uart.o + endif + ``` +3. **Timeouts enabled.** Do not define `WHAL_CFG_NO_TIMEOUT` when + `UART_FLASH=1`: + + ```makefile + ifneq ($(UART_FLASH),1) + CFLAGS+=-DWHAL_CFG_NO_TIMEOUT + endif + ``` +4. **A UART timeout.** Point the UART config's `.timeout` at a `whal_Timeout`. + Without one, `whal_Uart_Recv` waits forever and boot hangs at the first + unanswered wait. The timeout bounds both `Send` and `Recv`, so it must be + longer than one byte time and long enough for the host to answer. + +### Timeout Example + +The `stm32wb_nucleo` board counts register polls rather than time, which needs +no timer peripheral. wolfHAL calls `GetTick` once per status register read, so +`timeoutTicks` is a poll count. 500000 polls matches the legacy loop count. + +`wolfHAL_board.h`: + +```c +#ifdef UART_FLASH +extern whal_Timeout g_wbUartTimeout; +#define BOARD_UART_TIMEOUT (&g_wbUartTimeout) +#else +#define BOARD_UART_TIMEOUT NULL +#endif + +#define WHAL_CFG_STM32WB_UART_DEV { \ + .base = WHAL_STM32WB55_UART1_BASE, \ + .cfg = (void *)&(const whal_Stm32wb_Uart_Cfg){ \ + .brr = WHAL_STM32WB_UART_BRR(64000000, 115200), \ + .timeout = BOARD_UART_TIMEOUT, \ + }, \ +} +``` + +`wolfHAL_board.c`: + +```c +#ifdef UART_FLASH +#define UART_FLASH_TIMEOUT_POLLS 500000 + +static uint32_t g_pollCount; + +static uint32_t board_poll_tick(void) +{ + return ++g_pollCount; +} + +whal_Timeout g_wbUartTimeout = { + .timeoutTicks = UART_FLASH_TIMEOUT_POLLS, + .GetTick = board_poll_tick, +}; +#endif +``` + +A poll count depends on CPU speed. A board that needs a timeout in real time +can provide a time-based `GetTick`, such as one driven by SysTick, instead. + +### Building and Testing + +``` +cp config/examples/stm32wb-uart-flash-encryption.config .config +make WOLFHAL=1 BOARD=stm32wb_nucleo +``` + +To run an encrypted update over UART flash on a NUCLEO-WB55 board with +`st-flash` and `uart-flash-server`: + +``` +make test-enc-update WOLFHAL=1 BOARD=stm32wb_nucleo +``` + ## Test Application The generic test application (`test-app/app_wolfhal.c`) demonstrates using wolfHAL diff --git a/hal/boards/stm32wb_nucleo/board.mk b/hal/boards/stm32wb_nucleo/board.mk index ce7244075f..4ef2fc751a 100644 --- a/hal/boards/stm32wb_nucleo/board.mk +++ b/hal/boards/stm32wb_nucleo/board.mk @@ -1,7 +1,9 @@ ARCH_FLASH_OFFSET=0x08000000 $(call LSCRIPT_IN_DEFAULT,hal/stm32wb.ld) -CFLAGS+=-DWHAL_CFG_NO_TIMEOUT +ifneq ($(UART_FLASH),1) + CFLAGS+=-DWHAL_CFG_NO_TIMEOUT +endif # Upstream wolfHAL drivers from lib/wolfHAL/src/. wolfBoot's hal_flash_* # contract is satisfied by hal/wolfhal.c (added automatically because @@ -16,7 +18,7 @@ CFLAGS+=-DWHAL_CFG_STM32WB_UART_SINGLE_INSTANCE WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/reg.o WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/flash/stm32wb_flash.o -ifeq ($(DEBUG_UART),1) +ifneq (,$(filter 1,$(DEBUG_UART) $(UART_FLASH))) WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/gpio/stm32wb_gpio.o WOLFHAL_OBJS+=$(WOLFHAL_ROOT)/src/uart/stm32wb_uart.o endif diff --git a/hal/boards/stm32wb_nucleo/wolfHAL_board.c b/hal/boards/stm32wb_nucleo/wolfHAL_board.c index 088e60b5d3..3255767ef3 100644 --- a/hal/boards/stm32wb_nucleo/wolfHAL_board.c +++ b/hal/boards/stm32wb_nucleo/wolfHAL_board.c @@ -29,7 +29,7 @@ #include "hal.h" #include "wolfHAL_board.h" -#ifdef DEBUG_UART +#if defined(DEBUG_UART) || defined(UART_FLASH) static const whal_Stm32wb_Rcc_PeriphClk g_periphClks[] = { {WHAL_STM32WB55_GPIOB_GATE}, {WHAL_STM32WB55_UART1_GATE}, @@ -37,6 +37,23 @@ static const whal_Stm32wb_Rcc_PeriphClk g_periphClks[] = { #define PERIPH_CLK_COUNT (sizeof(g_periphClks) / sizeof(g_periphClks[0])) #endif +#ifdef UART_FLASH +#define UART_FLASH_TIMEOUT_POLLS 500000 + +static uint32_t g_pollCount; + +/* One tick per register poll, so timeoutTicks counts polls. */ +static uint32_t board_poll_tick(void) +{ + return ++g_pollCount; +} + +whal_Timeout g_wbUartTimeout = { + .timeoutTicks = UART_FLASH_TIMEOUT_POLLS, + .GetTick = board_poll_tick, +}; +#endif + /* MSI 4 MHz -> PLL VCO 128 MHz -> PLLR /2 = 64 MHz -> SYSCLK */ static void pll_clock_on(void) { @@ -60,7 +77,7 @@ void hal_init(void) pll_clock_on(); whal_Flash_Init(BOARD_FLASH_DEV); -#ifdef DEBUG_UART +#if defined(DEBUG_UART) || defined(UART_FLASH) for (size_t i = 0; i < PERIPH_CLK_COUNT; i++) { whal_Stm32wb_Rcc_EnablePeriphClk(&g_periphClks[i]); } @@ -72,7 +89,7 @@ void hal_init(void) void hal_prepare_boot(void) { -#ifdef DEBUG_UART +#if defined(DEBUG_UART) || defined(UART_FLASH) whal_Uart_Deinit(BOARD_UART_DEV); whal_Gpio_Deinit(BOARD_GPIO_DEV); diff --git a/hal/boards/stm32wb_nucleo/wolfHAL_board.h b/hal/boards/stm32wb_nucleo/wolfHAL_board.h index 42f7fa362b..b9ae1a5c89 100644 --- a/hal/boards/stm32wb_nucleo/wolfHAL_board.h +++ b/hal/boards/stm32wb_nucleo/wolfHAL_board.h @@ -56,8 +56,16 @@ extern const whal_Uart whal_Stm32wb_Uart_Dev; * without API churn. */ #define BOARD_GPIO_DEV WHAL_INTERNAL_DEV #define BOARD_UART_DEV WHAL_INTERNAL_DEV +#define BOARD_UART_FLASH_DEV BOARD_UART_DEV #define BOARD_FLASH_DEV WHAL_INTERNAL_DEV +#ifdef UART_FLASH +extern whal_Timeout g_wbUartTimeout; +#define BOARD_UART_TIMEOUT (&g_wbUartTimeout) +#else +#define BOARD_UART_TIMEOUT NULL +#endif + /* GPIO singleton initializer — instantiated by stm32wb_gpio.c. */ #define WHAL_CFG_STM32WB_GPIO_DEV { \ .base = WHAL_STM32WB55_GPIO_BASE, \ @@ -95,6 +103,7 @@ extern const whal_Uart whal_Stm32wb_Uart_Dev; .base = WHAL_STM32WB55_UART1_BASE, \ .cfg = (void *)&(const whal_Stm32wb_Uart_Cfg){ \ .brr = WHAL_STM32WB_UART_BRR(64000000, 115200), \ + .timeout = BOARD_UART_TIMEOUT, \ }, \ } diff --git a/hal/wolfhal.c b/hal/wolfhal.c index 133a6bdcdb..ad7aa0ec44 100644 --- a/hal/wolfhal.c +++ b/hal/wolfhal.c @@ -35,6 +35,7 @@ #include #include "hal.h" +#include "uart_drv.h" #include "wolfboot/wolfboot.h" #include "wolfHAL_board.h" @@ -228,6 +229,31 @@ void ext_flash_unlock(void) #endif /* BOARD_EXT_FLASH_DEV */ #endif /* EXT_FLASH */ +#ifdef UART_FLASH +int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop) +{ + (void)bitrate; + (void)data; + (void)parity; + (void)stop; + return 0; +} + +int uart_tx(const uint8_t c) +{ + if (whal_Uart_Send(BOARD_UART_FLASH_DEV, &c, 1) != WHAL_SUCCESS) + return -1; + return 1; +} + +int uart_rx(uint8_t *c) +{ + if (whal_Uart_Recv(BOARD_UART_FLASH_DEV, c, 1) != WHAL_SUCCESS) + return 0; + return 1; +} +#endif + #ifdef DEBUG_UART #ifdef WOLFHAL_UART_CRLF /* Send len bytes, with '\n' sent as "\r\n" */ diff --git a/options.mk b/options.mk index 181492fb43..d29b117cac 100644 --- a/options.mk +++ b/options.mk @@ -1255,7 +1255,9 @@ ifeq ($(EXT_FLASH),1) ifeq ($(UART_FLASH),1) CFLAGS+=-D"UART_FLASH=1" OBJS+=src/uart_flash.o - WOLFCRYPT_OBJS+=hal/uart/uart_drv_$(UART_TARGET).o + ifneq ($(WOLFHAL),1) + WOLFCRYPT_OBJS+=hal/uart/uart_drv_$(UART_TARGET).o + endif endif endif diff --git a/src/uart_flash.c b/src/uart_flash.c index 7e7b3b98f8..e2edf8a981 100644 --- a/src/uart_flash.c +++ b/src/uart_flash.c @@ -50,11 +50,24 @@ int uart_rx(uint8_t *c); static int wait_ack_cycles(int cycles) { uint8_t c; + +#ifdef WOLFHAL + int timeouts = cycles / WAIT_CYCLES; + int count = 0; + while ((timeouts > 0) && (++count < cycles)) { + if (uart_rx(&c) != 1) + timeouts--; + else if (c == CMD_ACK) + return 0; + } +#else volatile int count = 0; while(++count < cycles) { if ((uart_rx(&c) == 1) && (c == CMD_ACK)) return 0; } +#endif + return -1; } @@ -122,8 +135,13 @@ int ext_flash_read(uintptr_t address, uint8_t *data, int len) return -1; } for (i = 0; i < len; i++) { +#ifdef WOLFHAL + if (uart_rx(&data[i]) != 1) + return -1; +#else if (uart_rx_timeout(&data[i]) != 0) return -1; +#endif uart_tx(CMD_ACK); } return i; diff --git a/test-app/Makefile b/test-app/Makefile index 6d57fdd50a..4c689257b4 100644 --- a/test-app/Makefile +++ b/test-app/Makefile @@ -1083,19 +1083,22 @@ ifeq ($(WOLFHAL),1) ifeq ($(strip $(BOARD)),) $(error WOLFHAL=1 requires BOARD to be set, e.g. BOARD=stm32wb_nucleo) endif - ifeq ($(wildcard $(WOLFBOOT_ROOT)/hal/boards/$(BOARD)/board.mk),) - $(error BOARD=$(BOARD) has no hal/boards/$(BOARD)/board.mk) + ifeq ($(wildcard $(BOARD_DIR)/board.mk),) + $(error BOARD=$(BOARD) has no $(BOARD_DIR)/board.mk) endif WOLFHAL_ROOT?=$(WOLFBOOT_ROOT)/lib/wolfHAL - CFLAGS+=-I$(WOLFHAL_ROOT) -I$(WOLFBOOT_ROOT)/hal/boards/$(BOARD) + CFLAGS+=-I$(WOLFHAL_ROOT) -I$(BOARD_DIR) DEBUG_UART=1 APP_OBJS+=board_$(BOARD).o - include $(WOLFBOOT_ROOT)/hal/boards/$(BOARD)/board.mk + include $(BOARD_DIR)/board.mk endif ifeq ($(UART_FLASH),1) CFLAGS+=-D"UART_FLASH=1" - APP_OBJS+= ../src/uart_flash.o ../hal/uart/uart_drv_$(UART_TARGET).o + APP_OBJS+=../src/uart_flash.o + ifneq ($(WOLFHAL),1) + APP_OBJS+=../hal/uart/uart_drv_$(UART_TARGET).o + endif else ifeq ($(TARGET),stm32wb) # With WOLFHAL=1, hal/wolfhal.c + the wolfHAL UART driver provide # uart_write; the legacy driver would collide. @@ -2007,7 +2010,7 @@ lpc55s69_ns/%.o: %.c $(Q)mkdir -p $(dir $@) $(Q)$(CC) $(CFLAGS) -c $(OUTPUT_FLAG) $@ $< -board_$(BOARD).o: ../hal/boards/$(BOARD)/wolfHAL_board.c +board_$(BOARD).o: $(BOARD_DIR)/wolfHAL_board.c @echo "\t[CC-$(ARCH)] $@" $(Q)$(CC) $(CFLAGS) -DDEBUG_UART -c $(OUTPUT_FLAG) $@ $<