From ecf1cb8a929957efa774f38ca0476ff7351d7a37 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 18 Nov 2025 11:04:43 -0800 Subject: [PATCH] Peer review fixes. Added new WOLFBOOT_RESTORE_CLOCK option --- arch.mk | 2 +- config/examples/vorago_va416x0.config | 4 ++-- docs/HAL.md | 3 ++- hal/nrf52.c | 2 ++ hal/pic32ck.c | 2 +- hal/pic32cz.c | 2 ++ hal/same51.c | 2 ++ hal/samr21.c | 2 ++ hal/stm32c0.c | 2 ++ hal/stm32f1.c | 2 ++ hal/stm32f4.c | 3 ++- hal/stm32f7.c | 3 ++- hal/stm32g0.c | 2 ++ hal/stm32h5.c | 2 ++ hal/stm32h7.c | 2 ++ hal/stm32l0.c | 2 ++ hal/stm32l4.c | 3 ++- hal/stm32l5.c | 2 ++ hal/stm32u5.c | 3 ++- hal/stm32wb.c | 2 ++ hal/va416x0.ld | 8 +++----- {include => hal}/vorago/board.h | 0 {include => hal}/vorago/hal_config.h | 0 options.mk | 5 +++++ 24 files changed, 46 insertions(+), 14 deletions(-) rename {include => hal}/vorago/board.h (100%) rename {include => hal}/vorago/hal_config.h (100%) diff --git a/arch.mk b/arch.mk index 3dfca22d..61d9313d 100644 --- a/arch.mk +++ b/arch.mk @@ -267,7 +267,7 @@ ifeq ($(ARCH),ARM) endif ifeq ($(TARGET),va416x0) - CFLAGS+=-I$(WOLFBOOT_ROOT)/include/vorago/ \ + CFLAGS+=-I$(WOLFBOOT_ROOT)/hal/vorago/ \ -I$(VORAGO_SDK_DIR)/common/drivers/hdr/ \ -I$(VORAGO_SDK_DIR)/common/mcu/hdr/ \ -I$(VORAGO_SDK_DIR)/common/utils/hdr/ diff --git a/config/examples/vorago_va416x0.config b/config/examples/vorago_va416x0.config index 4b94dbfa..bb2d6beb 100644 --- a/config/examples/vorago_va416x0.config +++ b/config/examples/vorago_va416x0.config @@ -75,5 +75,5 @@ USE_HAL_SPI_FRAM=1 #CFLAGS_EXTRA+=-DWOLFBOOT_EDAC_RAM_SCRUB=1000 #CFLAGS_EXTRA+=-DWOLFBOOT_EDAC_ROM_SCRUB=125 -# Optionally restore clock to heart-beat oscillator after boot -#CFLAGS_EXTRA+=-DWOLFBOOT_RESTORE_CLOCK \ No newline at end of file +# Leave clock at 100MHz (to restore clock to heart beat oscillator use =1) +WOLFBOOT_RESTORE_CLOCK?=0 diff --git a/docs/HAL.md b/docs/HAL.md index cecb0921..6cfabccc 100644 --- a/docs/HAL.md +++ b/docs/HAL.md @@ -69,7 +69,8 @@ the geometry of the flash sectors, and erase all the sectors in between. This function is called by the bootloader at a very late stage, before chain-loading the firmware in the next stage. This can be used to revert all the changes made to the clock settings, to ensure -that the state of the microcontroller is restored to its original settings. +that the state of the microcontroller is restored to its original settings. By default most targets will restore the +clock settings. Use the `WOLFBOOT_RESTORE_CLOCK=0` option to disable clock restoration. ### Optional support for external flash memory diff --git a/hal/nrf52.c b/hal/nrf52.c index 1ef70d95..981c83c2 100644 --- a/hal/nrf52.c +++ b/hal/nrf52.c @@ -124,7 +124,9 @@ void hal_init(void) void hal_prepare_boot(void) { +#ifdef WOLFBOOT_RESTORE_CLOCK TASKS_HFCLKSTOP = 1; +#endif } #endif /* TARGET_nrf52 */ diff --git a/hal/pic32ck.c b/hal/pic32ck.c index 2ec84db0..8c25bd62 100644 --- a/hal/pic32ck.c +++ b/hal/pic32ck.c @@ -73,7 +73,7 @@ void hal_init(void) void hal_prepare_boot(void) { -#ifndef TZEN +#if !defined(TZEN) && defined(WOLFBOOT_RESTORE_CLOCK) pic32_clock_reset(); #endif } diff --git a/hal/pic32cz.c b/hal/pic32cz.c index dde76056..1d79960a 100644 --- a/hal/pic32cz.c +++ b/hal/pic32cz.c @@ -104,5 +104,7 @@ void hal_init(void) void hal_prepare_boot(void) { +#ifdef WOLFBOOT_RESTORE_CLOCK pic32_clock_reset(); +#endif } diff --git a/hal/same51.c b/hal/same51.c index 9c761e8e..9c5469c1 100644 --- a/hal/same51.c +++ b/hal/same51.c @@ -317,6 +317,7 @@ void RAMFUNCTION hal_flash_dualbank_swap(void) void RAMFUNCTION hal_prepare_boot(void) { +#ifdef WOLFBOOT_RESTORE_CLOCK /* Reset clock controller */ GCLK_CTRLA |= CTRLA_SWRST; @@ -332,6 +333,7 @@ void RAMFUNCTION hal_prepare_boot(void) /* Clear PLL options */ OSCCTRL_DPLL0CTRLB = 0; +#endif } #endif /* __WOLFBOOT */ diff --git a/hal/samr21.c b/hal/samr21.c index a887f41c..37c2d7b7 100644 --- a/hal/samr21.c +++ b/hal/samr21.c @@ -144,6 +144,7 @@ void hal_init(void) void hal_prepare_boot(void) { +#ifdef WOLFBOOT_RESTORE_CLOCK /* Reset NVM wait states */ APBBMASK_REG |= APBBMASK_NVM_EN; NVMCTRLB_REG &= ~((WAITSTATES & 0x0f) << 1); @@ -152,6 +153,7 @@ void hal_prepare_boot(void) /* Reset clock controller */ GCLK_CTRL = GCLK_CTRL_RESET; GCLK_WAITBUSY(); +#endif } diff --git a/hal/stm32c0.c b/hal/stm32c0.c index ceb8231f..30a33576 100644 --- a/hal/stm32c0.c +++ b/hal/stm32c0.c @@ -301,7 +301,9 @@ void RAMFUNCTION hal_prepare_boot(void) #ifdef SPI_FLASH spi_flash_release(); #endif +#ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); +#endif #ifdef FLASH_SECURABLE_MEMORY_SUPPORT do_secure_boot(); #endif diff --git a/hal/stm32f1.c b/hal/stm32f1.c index 5874d63d..8b82b492 100644 --- a/hal/stm32f1.c +++ b/hal/stm32f1.c @@ -330,5 +330,7 @@ void hal_init(void) void hal_prepare_boot(void) { +#ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); +#endif } diff --git a/hal/stm32f4.c b/hal/stm32f4.c index bddd821d..0ac8d17a 100644 --- a/hal/stm32f4.c +++ b/hal/stm32f4.c @@ -345,7 +345,8 @@ void hal_prepare_boot(void) #ifdef SPI_FLASH spi_flash_release(); #endif - +#ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); +#endif } diff --git a/hal/stm32f7.c b/hal/stm32f7.c index 11c5c4c5..c68c0047 100644 --- a/hal/stm32f7.c +++ b/hal/stm32f7.c @@ -423,8 +423,9 @@ void RAMFUNCTION hal_prepare_boot(void) #ifdef SPI_FLASH spi_flash_release(); #endif - +#ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); +#endif } void RAMFUNCTION hal_erase_bank2(void) diff --git a/hal/stm32g0.c b/hal/stm32g0.c index d4e7b316..20ab6df2 100644 --- a/hal/stm32g0.c +++ b/hal/stm32g0.c @@ -333,7 +333,9 @@ void RAMFUNCTION hal_prepare_boot(void) #ifdef SPI_FLASH spi_flash_release(); #endif +#ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); +#endif #ifdef FLASH_SECURABLE_MEMORY_SUPPORT do_secure_boot(); #endif diff --git a/hal/stm32h5.c b/hal/stm32h5.c index aa83f514..157d4bb6 100644 --- a/hal/stm32h5.c +++ b/hal/stm32h5.c @@ -608,7 +608,9 @@ void hal_prepare_boot(void) #if (TZ_SECURE()) periph_unsecure(); #else + #ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); + #endif #endif } diff --git a/hal/stm32h7.c b/hal/stm32h7.c index 728d3967..d202c136 100644 --- a/hal/stm32h7.c +++ b/hal/stm32h7.c @@ -510,7 +510,9 @@ void hal_prepare_boot(void) #ifdef SPI_FLASH spi_flash_release(); #endif +#ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); +#endif } #ifdef FLASH_OTP_KEYSTORE diff --git a/hal/stm32l0.c b/hal/stm32l0.c index 66746223..1f7fe7c5 100644 --- a/hal/stm32l0.c +++ b/hal/stm32l0.c @@ -262,8 +262,10 @@ void hal_prepare_boot(void) spi_flash_release(); #endif hal_flash_lock(); +#ifdef WOLFBOOT_RESTORE_CLOCK if ((FLASH_PECR & FLASH_PECR_PELOCK) == 0) FLASH_PECR |= FLASH_PECR_PELOCK; clock_pll_off(); +#endif } diff --git a/hal/stm32l4.c b/hal/stm32l4.c index 426814c3..b4ddaece 100644 --- a/hal/stm32l4.c +++ b/hal/stm32l4.c @@ -279,8 +279,9 @@ void hal_prepare_boot(void) #ifdef SPI_FLASH spi_flash_release(); #endif - +#ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); +#endif } /* This value is unused, the function is never called diff --git a/hal/stm32l5.c b/hal/stm32l5.c index f45c9f77..dfb2e4d1 100644 --- a/hal/stm32l5.c +++ b/hal/stm32l5.c @@ -409,7 +409,9 @@ void hal_init(void) void hal_prepare_boot(void) { +#ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); + #endif #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) periph_unsecure(); #endif diff --git a/hal/stm32u5.c b/hal/stm32u5.c index 291dcd96..9156cd7f 100644 --- a/hal/stm32u5.c +++ b/hal/stm32u5.c @@ -520,8 +520,9 @@ void hal_init(void) void hal_prepare_boot(void) { +#ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); - +#endif #if TZ_SECURE() led_unsecure(); #endif diff --git a/hal/stm32wb.c b/hal/stm32wb.c index ea00d954..b51334ab 100644 --- a/hal/stm32wb.c +++ b/hal/stm32wb.c @@ -325,7 +325,9 @@ void hal_prepare_boot(void) #ifdef SPI_FLASH spi_flash_release(); #endif +#ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); +#endif } #ifdef WOLFSSL_STM32_PKA diff --git a/hal/va416x0.ld b/hal/va416x0.ld index ef13006f..922bb404 100644 --- a/hal/va416x0.ld +++ b/hal/va416x0.ld @@ -1,4 +1,4 @@ -_Min_Heap_Size = 0x00000200; /* required amount of heap */ +# no heap _Min_Stack_Size = 0x00006000; /* required amount of stack */ /* Memory areas */ @@ -140,14 +140,12 @@ SECTIONS *(dma_blk) } >RAM1 - /* User_heap_stack section, used to check that there is enough RAM left */ - ._user_heap_stack : + /* User_stack section, used to check that there is enough RAM left */ + ._user_stack : { . = ALIGN(8); PROVIDE ( end = . ); PROVIDE ( _end = . ); - PROVIDE ( _start_heap = . ); - . = . + _Min_Heap_Size; . = . + _Min_Stack_Size; . = ALIGN(8); PROVIDE ( END_STACK = . ); diff --git a/include/vorago/board.h b/hal/vorago/board.h similarity index 100% rename from include/vorago/board.h rename to hal/vorago/board.h diff --git a/include/vorago/hal_config.h b/hal/vorago/hal_config.h similarity index 100% rename from include/vorago/hal_config.h rename to hal/vorago/hal_config.h diff --git a/options.mk b/options.mk index 9a388b5b..5614127b 100644 --- a/options.mk +++ b/options.mk @@ -1040,3 +1040,8 @@ endif ifneq ($(WOLFBOOT_PARTITION_FILENAME),) CFLAGS += -DWOLFBOOT_PARTITION_FILENAME=$(WOLFBOOT_PARTITION_FILENAME) endif + +# Clock Restore Option (default on) +ifneq ($(WOLFBOOT_RESTORE_CLOCK),0) + CFLAGS += -DWOLFBOOT_RESTORE_CLOCK +endif