From 13b17afba152e3b936d6cbe87fe11fa3d0f7a885 Mon Sep 17 00:00:00 2001 From: Jamie McCrae Date: Tue, 10 Dec 2024 08:39:49 +0000 Subject: [PATCH] bootloader: Add support for cleaning up RAM before application boot Adds a new Kconfig which, when enabled, will clear all RAM to 0 before loading into the application to chain load, this can be enabled with CONFIG_SB_CLEANUP_RAM Signed-off-by: Jamie McCrae --- subsys/bootloader/Kconfig | 7 +++++ subsys/bootloader/bl_boot/bl_boot.c | 27 +++++++++++++++++++ subsys/fw_info/Kconfig | 5 ++++ .../fw_info/Kconfig.template.fw_info_ext_api | 1 + 4 files changed, 40 insertions(+) diff --git a/subsys/bootloader/Kconfig b/subsys/bootloader/Kconfig index a513b99ddf51..bb6ceaae98be 100644 --- a/subsys/bootloader/Kconfig +++ b/subsys/bootloader/Kconfig @@ -210,6 +210,13 @@ config SB_BPROT_IN_DEBUG default y depends on (HAS_HW_NRF_BPROT || HAS_HW_NRF_MPU) +config SB_CLEANUP_RAM + bool "Perform RAM cleanup" + depends on !FW_INFO_PROVIDE_ENABLE + depends on CPU_CORTEX_M4 || CPU_CORTEX_M33 + help + Sets contents of memory to 0 before jumping to application. + endif # IS_SECURE_BOOTLOADER config IS_BOOTLOADER_IMG diff --git a/subsys/bootloader/bl_boot/bl_boot.c b/subsys/bootloader/bl_boot/bl_boot.c index 414b0e7d3047..a70a92e10dd3 100644 --- a/subsys/bootloader/bl_boot/bl_boot.c +++ b/subsys/bootloader/bl_boot/bl_boot.c @@ -156,7 +156,34 @@ void bl_boot(const struct fw_info *fw_info) __set_MSP(vector_table[0]); __set_PSP(0); +#if CONFIG_SB_CLEANUP_RAM + __asm__ volatile ( + /* vector_table[1] -> r0 */ + " mov r0, %0\n" + /* Base to write -> r1 */ + " mov r1, %1\n" + /* Size to write -> r2 */ + " mov r2, %2\n" + /* Value to write -> r3 */ + " mov r3, %3\n" + "clear:\n" + " str r3, [r1]\n" + " add r1, r1, #4\n" + " sub r2, r2, #4\n" + " cbz r2, out\n" + " b clear\n" + "out:\n" + " dsb\n" + /* Jump to reset vector of an app */ + " bx r0\n" + : + : "r" (vector_table[1]), "i" (CONFIG_SRAM_BASE_ADDRESS), + "i" (CONFIG_SRAM_SIZE * 1024), "i" (0) + : "r0", "r1", "r2", "r3", "memory" + ); +#else /* Call reset handler. */ ((void (*)(void))vector_table[1])(); +#endif CODE_UNREACHABLE; } diff --git a/subsys/fw_info/Kconfig b/subsys/fw_info/Kconfig index 2de7d5a03bfa..536e2d3657ff 100644 --- a/subsys/fw_info/Kconfig +++ b/subsys/fw_info/Kconfig @@ -118,6 +118,11 @@ config FW_INFO_VALID_VAL help The value fw_info::valid will have when valid. +config FW_INFO_PROVIDE_ENABLE + bool + help + Hidden option, set if at least one *_EXT_API_ENABLED option is enabled. + EXT_API = EXT_API_PROVIDE id = 0x1200 flags = 0 diff --git a/subsys/fw_info/Kconfig.template.fw_info_ext_api b/subsys/fw_info/Kconfig.template.fw_info_ext_api index 2f7fa1b59922..1957f511e5ce 100644 --- a/subsys/fw_info/Kconfig.template.fw_info_ext_api +++ b/subsys/fw_info/Kconfig.template.fw_info_ext_api @@ -41,6 +41,7 @@ config $(EXT_API)_EXT_API_ATLEAST_REQUIRED config $(EXT_API)_EXT_API_ENABLED bool "Provide the $(EXT_API) EXT_API to other images" + select FW_INFO_PROVIDE_ENABLE help Provide this EXT_API to other images.