From 3e8058948a7c67a2f36a4804ffc602c4fe477caa Mon Sep 17 00:00:00 2001 From: Alex Lanzano Date: Mon, 26 Jan 2026 17:13:05 -0500 Subject: [PATCH] Integrate wolfHAL --- .gitmodules | 3 + Makefile | 10 +++ arch.mk | 6 ++ config/examples/stm32wb-wolfhal.config | 14 ++++ config/wolfHAL/stm32wb55_nucleo.c | 101 +++++++++++++++++++++++++ config/wolfHAL/stm32wb55_nucleo.mk | 25 ++++++ hal/uart/uart_drv_wolfhal.c | 72 ++++++++++++++++++ hal/wolfhal.c | 89 ++++++++++++++++++++++ lib/wolfHAL | 1 + options.mk | 8 ++ test-app/Makefile | 7 ++ test-app/app_wolfhal.c | 83 ++++++++++++++++++++ 12 files changed, 419 insertions(+) create mode 100644 config/examples/stm32wb-wolfhal.config create mode 100644 config/wolfHAL/stm32wb55_nucleo.c create mode 100644 config/wolfHAL/stm32wb55_nucleo.mk create mode 100644 hal/uart/uart_drv_wolfhal.c create mode 100644 hal/wolfhal.c create mode 160000 lib/wolfHAL create mode 100644 test-app/app_wolfhal.c diff --git a/.gitmodules b/.gitmodules index 6cd81662fe..13396fd2ec 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,6 @@ [submodule "lib/wolfPSA"] path = lib/wolfPSA url = https://github.com/wolfSSL/wolfPSA.git +[submodule "lib/wolfHAL"] + path = lib/wolfHAL + url = https://github.com/wolfSSL/wolfHAL.git diff --git a/Makefile b/Makefile index 1d72f3285e..88d2984840 100644 --- a/Makefile +++ b/Makefile @@ -142,6 +142,7 @@ WOLFBOOT_LIB_WOLFTPM?=lib/wolfTPM WOLFBOOT_LIB_WOLFPKCS11?=lib/wolfPKCS11 WOLFBOOT_LIB_WOLFPSA?=lib/wolfPSA WOLFBOOT_LIB_WOLFHSM?=lib/wolfHSM +WOLFBOOT_LIB_WOLFHAL?=lib/wolfHAL # Convert to absolute paths using abspath function WOLFBOOT_LIB_WOLFSSL:=$(abspath $(WOLFBOOT_LIB_WOLFSSL)) @@ -149,6 +150,7 @@ WOLFBOOT_LIB_WOLFTPM:=$(abspath $(WOLFBOOT_LIB_WOLFTPM)) WOLFBOOT_LIB_WOLFPKCS11:=$(abspath $(WOLFBOOT_LIB_WOLFPKCS11)) WOLFBOOT_LIB_WOLFPSA:=$(abspath $(WOLFBOOT_LIB_WOLFPSA)) WOLFBOOT_LIB_WOLFHSM:=$(abspath $(WOLFBOOT_LIB_WOLFHSM)) +WOLFBOOT_LIB_WOLFHAL:=$(abspath $(WOLFBOOT_LIB_WOLFHAL)) # Export variables so they are available to sub-makefiles export WOLFBOOT_LIB_WOLFSSL @@ -156,6 +158,7 @@ export WOLFBOOT_LIB_WOLFTPM export WOLFBOOT_LIB_WOLFPKCS11 export WOLFBOOT_LIB_WOLFPSA export WOLFBOOT_LIB_WOLFHSM +export WOLFBOOT_LIB_WOLFHAL ## Architecture/CPU configuration include arch.mk @@ -227,6 +230,13 @@ ifeq ($(TARGET),stm32h5) endif endif # TZEN=1 +ifeq ($(TARGET),wolfhal) + LSCRIPT_IN:=hal/$(WOLFHAL_TARGET).ld + CFLAGS+= -I$(WOLFBOOT_LIB_WOLFHAL) + OBJS+=config/wolfHAL/$(WOLFHAL_BOARD).o + include config/wolfHAL/$(WOLFHAL_BOARD).mk +endif + ifeq ($(TARGET),x86_64_efi) MAIN_TARGET:=wolfboot.efi endif diff --git a/arch.mk b/arch.mk index d4467bf886..34acaebd6f 100644 --- a/arch.mk +++ b/arch.mk @@ -203,6 +203,12 @@ ifeq ($(ARCH),ARM) endif endif + ifeq ($(TARGET),wolfhal) + ifeq ($(WOLFHAL_TARGET),stm32wb) + ARCH_FLASH_OFFSET=0x08000000 + SPI_TARGET=stm32 + endif + endif ifeq ($(TARGET),stm32l5) CORTEX_M33=1 diff --git a/config/examples/stm32wb-wolfhal.config b/config/examples/stm32wb-wolfhal.config new file mode 100644 index 0000000000..6e8ba2d7fd --- /dev/null +++ b/config/examples/stm32wb-wolfhal.config @@ -0,0 +1,14 @@ +TARGET=wolfhal +SIGN=ECC256 +HASH=SHA256 +WOLFBOOT_SECTOR_SIZE=0x1000 +WOLFBOOT_PARTITION_SIZE=0x20000 +WOLFBOOT_PARTITION_BOOT_ADDRESS=0x08010000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x08028000 +WOLFBOOT_PARTITION_SWAP_ADDRESS=0x08048000 +NVM_FLASH_WRITEONCE=1 +PKA=0 +WOLFHAL_TARGET=stm32wb +WOLFHAL_BOARD=stm32wb55_nucleo +WOLFHAL_FLASH_SIZE=0x100000 +WOLFHAL_INIT_HOOKS=1 diff --git a/config/wolfHAL/stm32wb55_nucleo.c b/config/wolfHAL/stm32wb55_nucleo.c new file mode 100644 index 0000000000..3db7ca23ec --- /dev/null +++ b/config/wolfHAL/stm32wb55_nucleo.c @@ -0,0 +1,101 @@ +#include + +whal_StRcc_PeriphClk periphClkEn[] = +{ + WHAL_ST_RCC_PERIPH_GPIOA, + WHAL_ST_RCC_PERIPH_GPIOB, + WHAL_ST_RCC_PERIPH_LPUART1, + WHAL_ST_RCC_PERIPH_FLASH, +}; + +whal_Clock wbClock = { + WHAL_STM32WB55_RCC_DEVICE, + + .cfg = &(whal_StRcc_Cfg){ + .sysClkSrc = WHAL_ST_RCC_SYSCLK_SRC_PLL, + .sysClkCfg.pll = + { + .clkSrc = WHAL_ST_RCC_PLLCLK_SRC_MSI, + /* 64 MHz */ + .n = 32, + .m = 0, + .r = 1, + .q = 0, + .p = 0, + }, + .periphClkEn = periphClkEn, + .periphClkEnCount = sizeof(periphClkEn) / sizeof(whal_StRcc_PeriphClk), + }, +}; + +whal_Gpio wbGpio = { + WHAL_STM32WB55_GPIO_DEVICE, + + .pinCfg = &(whal_StGpio_Cfg[3]){ + { /* LED */ + .port = WHAL_STGPIO_PORT_B, + .pin = 5, + .mode = WHAL_STGPIO_MODE_OUT, + .outType = WHAL_STGPIO_OUTTYPE_PUSHPULL, + .speed = WHAL_STGPIO_SPEED_LOW, + .pull = WHAL_STGPIO_PULL_UP, + .altFn = 0, + }, + { /* LPUART1 TX */ + .port = WHAL_STGPIO_PORT_A, + .pin = 2, + .mode = WHAL_STGPIO_MODE_ALTFN, + .outType = WHAL_STGPIO_OUTTYPE_PUSHPULL, + .speed = WHAL_STGPIO_SPEED_FAST, + .pull = WHAL_STGPIO_PULL_UP, + .altFn = 8, + }, + { /* LPUART1 RX */ + .port = WHAL_STGPIO_PORT_A, + .pin = 3, + .mode = WHAL_STGPIO_MODE_ALTFN, + .outType = WHAL_STGPIO_OUTTYPE_PUSHPULL, + .speed = WHAL_STGPIO_SPEED_FAST, + .pull = WHAL_STGPIO_PULL_UP, + .altFn = 8, + }, + }, + .pinCount = 3, +}; + +whal_Uart wbUart = { + WHAL_STM32WB55_LPUART1_DEVICE, + + .cfg = &(whal_StUart_Cfg){ + .baud = 115200, + .sysClk = &wbClock, + }, +}; + +whal_Flash wbFlash = { + WHAL_STM32WB55_FLASH_DEVICE, + + .cfg = &(whal_StFlash_Cfg) { + .startAddr = 0x08000000, + .size = 0x100000, + }, +}; + +void hal_pre_init() +{ + /* The flash read latency needs to be set prior to + * calling whal_Clock_Enable in hal_init() */ + + whal_StFlash_SetLatencyArgs wbFlashLatency = { + .latency = WHAL_ST_FLASH_LATENCY_3 + }; + whal_Flash_Cmd(&wbFlash, WHAL_ST_FLASH_CMD_SET_LATENCY, &wbFlashLatency); +} + +void hal_post_prepare_boot() +{ + whal_StFlash_SetLatencyArgs wbFlashLatency = { + .latency = WHAL_ST_FLASH_LATENCY_0 + }; + whal_Flash_Cmd(&wbFlash, WHAL_ST_FLASH_CMD_SET_LATENCY, &wbFlashLatency); +} diff --git a/config/wolfHAL/stm32wb55_nucleo.mk b/config/wolfHAL/stm32wb55_nucleo.mk new file mode 100644 index 0000000000..9c64d23602 --- /dev/null +++ b/config/wolfHAL/stm32wb55_nucleo.mk @@ -0,0 +1,25 @@ +OBJS += $(WOLFBOOT_LIB_WOLFHAL)/src/reg.o + +OBJS += $(WOLFBOOT_LIB_WOLFHAL)/src/clock/clock.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/uart/uart.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/gpio/gpio.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/flash/flash.o + + +OBJS += $(WOLFBOOT_LIB_WOLFHAL)/src/clock/st_rcc.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/uart/st_uart.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/gpio/st_gpio.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/flash/st_flash.o + +APP_OBJS += $(WOLFBOOT_LIB_WOLFHAL)/src/reg.o + +APP_OBJS += $(WOLFBOOT_LIB_WOLFHAL)/src/clock/clock.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/uart/uart.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/gpio/gpio.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/flash/flash.o + + +APP_OBJS += $(WOLFBOOT_LIB_WOLFHAL)/src/clock/st_rcc.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/uart/st_uart.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/gpio/st_gpio.o \ + $(WOLFBOOT_LIB_WOLFHAL)/src/flash/st_flash.o diff --git a/hal/uart/uart_drv_wolfhal.c b/hal/uart/uart_drv_wolfhal.c new file mode 100644 index 0000000000..ca85eded9b --- /dev/null +++ b/hal/uart/uart_drv_wolfhal.c @@ -0,0 +1,72 @@ +/* uart_drv_stm32wb.c + * + * Driver for the back-end of the UART_FLASH module. + * + * Example implementation for stm32WB, using UART1. + * + * Pinout: RX=PB7, TX=PB6 (VCOM port UART1 -> STLINK USB) + * + * Copyright (C) 2025 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#ifdef TARGET_wolfhal + +#include +#include + +extern whal_Uart wbUart; + +int uart_tx(const uint8_t c) +{ + whal_Uart_Send(&wbUart, &c, 1); + return 1; +} + +int uart_rx(uint8_t *c) +{ + whal_Uart_Recv(&wbUart, c, 1); + /* ALEX NOTE: this function also returns zero if no data is available... */ + return 1; +} + +int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop) +{ + /* Handle these configure options in the wolfHAL UART config */ + (void)bitrate; + (void)data; + (void)parity; + (void)stop; + + whal_Error err; + err = whal_Uart_Init(&wbUart); + if (err) { + return 1; + } + + return 0; +} + +#ifdef DEBUG_UART +void uart_write(const char *buf, unsigned int len) +{ + whal_Uart_Send(&wbUart, (uint8_t *)buf, len); +} +#endif + +#endif /* TARGET_wolfhal */ diff --git a/hal/wolfhal.c b/hal/wolfhal.c new file mode 100644 index 0000000000..82ca2a178a --- /dev/null +++ b/hal/wolfhal.c @@ -0,0 +1,89 @@ +/* stm32wb.c + * + * Copyright (C) 2025 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include "image.h" + +extern whal_Clock wbClock; +extern whal_Gpio wbGpio; +extern whal_Uart wbUart; +extern whal_Flash wbFlash; + +#ifdef WOLFHAL_INIT_HOOKS +extern void hal_pre_init(); +extern void hal_post_prepare_boot(); +#endif /* WOLFHAL_INIT_HOOKS */ + +void RAMFUNCTION hal_flash_unlock(void) +{ + whal_Flash_Unlock(&wbFlash, + 0, WOLFHAL_FLASH_SIZE); +} + +void RAMFUNCTION hal_flash_lock(void) +{ + whal_Flash_Lock(&wbFlash, + 0, WOLFHAL_FLASH_SIZE); +} + +int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len) +{ + whal_Flash_Write(&wbFlash, address, data, len); + return 0; +} + +int RAMFUNCTION hal_flash_erase(uint32_t address, int len) +{ + if (len == 0) + return -1; + + whal_Flash_Erase(&wbFlash, address, len); + return 0; +} + +void hal_init(void) +{ +#ifdef WOLFHAL_INIT_HOOKS + hal_pre_init(); +#endif /* WOLFHAL_INIT_HOOKS */ + + whal_Clock_Init(&wbClock); + whal_Clock_Enable(&wbClock); + + whal_Gpio_Init(&wbGpio); + whal_Uart_Init(&wbUart); + whal_Flash_Init(&wbFlash); +} + +void hal_prepare_boot(void) +{ + whal_Flash_Deinit(&wbFlash); + whal_Uart_Deinit(&wbUart); + whal_Gpio_Deinit(&wbGpio); + + whal_Clock_Disable(&wbClock); + whal_Clock_Deinit(&wbClock); + +#ifdef WOLFHAL_INIT_HOOKS + hal_post_prepare_boot(); +#endif /* WOLFHAL_INIT_HOOKS */ +} diff --git a/lib/wolfHAL b/lib/wolfHAL new file mode 160000 index 0000000000..6a463b2f06 --- /dev/null +++ b/lib/wolfHAL @@ -0,0 +1 @@ +Subproject commit 6a463b2f06b13edebb5920b18893b6bd3fc9be43 diff --git a/options.mk b/options.mk index d55dd160c4..1e9c713cd0 100644 --- a/options.mk +++ b/options.mk @@ -1145,3 +1145,11 @@ endif ifeq ($(TZEN),1) CFLAGS+=-DTZEN endif + +ifneq ($(WOLFHAL_FLASH_SIZE),) + CFLAGS+=-DWOLFHAL_FLASH_SIZE=$(WOLFHAL_FLASH_SIZE) +endif + +ifeq ($(WOLFHAL_INIT_HOOKS),1) + CFLAGS+=-DWOLFHAL_INIT_HOOKS=$(WOLFHAL_INIT_HOOKS) +endif diff --git a/test-app/Makefile b/test-app/Makefile index d2e39d218b..bef588b1d8 100644 --- a/test-app/Makefile +++ b/test-app/Makefile @@ -64,6 +64,13 @@ else endif endif +ifeq ($(TARGET),wolfhal) + WOLFBOOT_LIB_WOLFHAL?=../lib/wolfHAL + CFLAGS += -I$(WOLFBOOT_LIB_WOLFHAL) + APP_OBJS += ../config/wolfHAL/$(WOLFHAL_BOARD).o + include ../config/wolfHAL/$(WOLFHAL_BOARD).mk +endif + include ../arch.mk # Setup default linker flags diff --git a/test-app/app_wolfhal.c b/test-app/app_wolfhal.c new file mode 100644 index 0000000000..e8b932f39e --- /dev/null +++ b/test-app/app_wolfhal.c @@ -0,0 +1,83 @@ +/* main.c + * + * Test bare-metal boot-led-on application + * + * Copyright (C) 2025 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ +#define WOLFBOOT_FIXED_PARTITIONS + +#include +#include +#include +#include "wolfboot/wolfboot.h" +#include +#include "target.h" +#include "hal.h" + +#ifdef TARGET_wolfhal + +#define BOOT_LED_PIN 0 + +extern whal_Clock wbClock; +extern whal_Gpio wbGpio; +extern whal_Uart wbUart; +extern whal_Flash wbFlash; + +/* Matches all keys: + * - chacha (32 + 12) + * - aes128 (16 + 16) + * - aes256 (32 + 16) + */ +/* Longest key possible: AES256 (32 key + 16 IV = 48) */ +char enc_key[] = "0123456789abcdef0123456789abcdef" + "0123456789abcdef"; + +volatile uint32_t time_elapsed = 0; +void main(void) { + uint32_t version; + uint32_t l = 0; + uint32_t updv; + + hal_init(); + + whal_Gpio_Set(&wbGpio, BOOT_LED_PIN, 1); + + version = wolfBoot_current_firmware_version(); + updv = wolfBoot_update_firmware_version(); + + whal_Uart_Send(&wbUart, "*", 1); + whal_Uart_Send(&wbUart, (uint8_t *)&version, 4); + + if ((version == 1) && (updv != 8)) { + uint32_t sz; + whal_Gpio_Set(&wbGpio, BOOT_LED_PIN, 0); +#if EXT_ENCRYPTED + wolfBoot_set_encrypt_key((uint8_t *)enc_key,(uint8_t *)(enc_key + 32)); +#endif + wolfBoot_update_trigger(); + whal_Gpio_Set(&wbGpio, BOOT_LED_PIN, 1); + } else { + if (version != 7) + wolfBoot_success(); + } + /* Wait for reboot */ + while(1) + asm volatile("wfi"); +} +#endif /** PLATFROM_stm32wb **/