From 1c58002fbdce3a53ff0d8d2aea2e9f4b07b80eb8 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Wed, 20 Nov 2024 17:53:23 +0000 Subject: [PATCH] zephyr: Add CONFIG_MCUBOOT_CLEANUP_RAM Add Kconfig option to cleanup RAM in MCUboot before passing control to an application. Signed-off-by: Dominik Ermel --- boot/zephyr/Kconfig | 6 ++++++ boot/zephyr/main.c | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/boot/zephyr/Kconfig b/boot/zephyr/Kconfig index 7ff11ee90..d93014192 100644 --- a/boot/zephyr/Kconfig +++ b/boot/zephyr/Kconfig @@ -219,6 +219,12 @@ config MCUBOOT_CLEANUP_ARM_CORE start-up code which can cause a module fault and potentially make the module irrecoverable. +config MCUBOOT_CLEANUP_RAM + bool "Perform RAM cleanup" + depends on CPU_CORTEX_M4 || CPU_CORTEX_M33 + help + Sets contents of memory to 0 before jumping to application. + config MBEDTLS_CFG_FILE default "mcuboot-mbedtls-cfg.h" diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c index 30f8447a9..c84f18a1d 100644 --- a/boot/zephyr/main.c +++ b/boot/zephyr/main.c @@ -236,7 +236,34 @@ static void do_boot(struct boot_rsp *rsp) __set_CONTROL(0x00); /* application will configures core on its own */ __ISB(); #endif +#if CONFIG_MCUBOOT_CLEANUP_RAM + __asm__ volatile ( + /* vt->reset -> 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" (vt->reset), "i" (CONFIG_SRAM_BASE_ADDRESS), + "i" (CONFIG_SRAM_SIZE * 1024), "i" (0) + : "r0", "r1", "r2", "r3", "memory" + ); +#else ((void (*)(void))vt->reset)(); +#endif } #elif defined(CONFIG_XTENSA) || defined(CONFIG_RISCV)