From 4c704a9440ce445c85beabaa2fd9cfffc67668bc Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Thu, 9 Apr 2026 16:36:38 +0200 Subject: [PATCH] boot: fail closed on flash protect errors --- src/update_disk.c | 5 ++++- src/update_flash.c | 5 ++++- src/update_flash_hwswap.c | 3 ++- src/update_ram.c | 5 ++++- tools/unit-tests/unit-update-flash.c | 2 ++ 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/update_disk.c b/src/update_disk.c index 11381c91..7e52fead 100644 --- a/src/update_disk.c +++ b/src/update_disk.c @@ -549,7 +549,10 @@ void RAMFUNCTION wolfBoot_start(void) (void)hal_hsm_server_cleanup(); #endif #ifndef TZEN - (void)hal_flash_protect(WOLFBOOT_ORIGIN, BOOTLOADER_PARTITION_SIZE); + if (hal_flash_protect(WOLFBOOT_ORIGIN, BOOTLOADER_PARTITION_SIZE) < 0) { + wolfBoot_printf("Error protecting bootloader flash region\r\n"); + wolfBoot_panic(); + } #endif hal_prepare_boot(); diff --git a/src/update_flash.c b/src/update_flash.c index f8c3339c..382c20bb 100644 --- a/src/update_flash.c +++ b/src/update_flash.c @@ -1503,7 +1503,10 @@ void RAMFUNCTION wolfBoot_start(void) #endif #ifndef TZEN - (void)hal_flash_protect(WOLFBOOT_ORIGIN, BOOTLOADER_PARTITION_SIZE); + if (hal_flash_protect(WOLFBOOT_ORIGIN, BOOTLOADER_PARTITION_SIZE) < 0) { + wolfBoot_printf("Error protecting bootloader flash region\n"); + wolfBoot_panic(); + } #endif hal_prepare_boot(); diff --git a/src/update_flash_hwswap.c b/src/update_flash_hwswap.c index 53efb494..07c95862 100644 --- a/src/update_flash_hwswap.c +++ b/src/update_flash_hwswap.c @@ -107,7 +107,8 @@ void RAMFUNCTION wolfBoot_start(void) (void)hal_hsm_server_cleanup(); #endif #ifndef TZEN - (void)hal_flash_protect(WOLFBOOT_ORIGIN, BOOTLOADER_PARTITION_SIZE); + if (hal_flash_protect(WOLFBOOT_ORIGIN, BOOTLOADER_PARTITION_SIZE) < 0) + boot_panic(); #endif hal_prepare_boot(); #ifdef WOLFBOOT_HOOK_BOOT diff --git a/src/update_ram.c b/src/update_ram.c index c76063e0..b2f0d772 100644 --- a/src/update_ram.c +++ b/src/update_ram.c @@ -391,7 +391,10 @@ backup_on_failure: #endif #ifndef TZEN - (void)hal_flash_protect(WOLFBOOT_ORIGIN, BOOTLOADER_PARTITION_SIZE); + if (hal_flash_protect(WOLFBOOT_ORIGIN, BOOTLOADER_PARTITION_SIZE) < 0) { + wolfBoot_printf("Error protecting bootloader flash region\n"); + wolfBoot_panic(); + } #endif hal_prepare_boot(); diff --git a/tools/unit-tests/unit-update-flash.c b/tools/unit-tests/unit-update-flash.c index b564b3b2..aeddb337 100644 --- a/tools/unit-tests/unit-update-flash.c +++ b/tools/unit-tests/unit-update-flash.c @@ -137,8 +137,10 @@ int hal_flash_protect(haladdr_t address, int len) static void reset_mock_stats(void) { wolfBoot_staged_ok = 0; +#ifdef CUSTOM_ENCRYPT_KEY mock_set_encrypt_key_ret = 0; mock_set_encrypt_key_calls = 0; +#endif #ifndef ARCH_SIM wolfBoot_panicked = 0; #endif