From 4cf4f3212bc5f6e5d2692da3298a16f9c4acb962 Mon Sep 17 00:00:00 2001 From: Jamie McCrae Date: Tue, 10 Sep 2024 14:49:16 +0100 Subject: [PATCH] boot: zephyr: Add check for unexpected flash sector size Prints a debug log message if the device has a write block size for a flash device in DTS that is not the same as what the flash driver reports at run-time, this can be used to see if there is a faulty configuration as these compile-time values are used for various calculations Signed-off-by: Jamie McCrae --- boot/bootutil/src/swap_move.c | 13 +++++++++++++ boot/zephyr/CMakeLists.txt | 34 ++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/boot/bootutil/src/swap_move.c b/boot/bootutil/src/swap_move.c index 8999403c5..ece638d77 100644 --- a/boot/bootutil/src/swap_move.c +++ b/boot/bootutil/src/swap_move.c @@ -298,6 +298,19 @@ boot_slots_compatible(struct boot_loader_state *state) } } +#ifdef MCUBOOT_SLOT0_EXPECTED_SECTOR_SIZE + if (sector_sz_pri != MCUBOOT_SLOT0_EXPECTED_SECTOR_SIZE) { + BOOT_LOG_DBG("Discrepancy, slot0 expected sector size: %d, actual: %d", + MCUBOOT_SLOT0_EXPECTED_SECTOR_SIZE, sector_sz_pri); + } +#endif +#ifdef MCUBOOT_SLOT1_EXPECTED_SECTOR_SIZE + if (sector_sz_sec != MCUBOOT_SLOT1_EXPECTED_SECTOR_SIZE) { + BOOT_LOG_DBG("Discrepancy, slot1 expected sector size: %d, actual: %d", + MCUBOOT_SLOT1_EXPECTED_SECTOR_SIZE, sector_sz_sec); + } +#endif + if (num_sectors_pri > num_sectors_sec) { if (sector_sz_pri != boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i)) { BOOT_LOG_WRN("Cannot upgrade: not same sector layout"); diff --git a/boot/zephyr/CMakeLists.txt b/boot/zephyr/CMakeLists.txt index d02f93a40..e71968b5f 100644 --- a/boot/zephyr/CMakeLists.txt +++ b/boot/zephyr/CMakeLists.txt @@ -379,13 +379,29 @@ function(dt_get_parent node) set(${node} "${${node}}" PARENT_SCOPE) endfunction() -if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO) - dt_nodelabel(slot0_flash NODELABEL "slot0_partition") - dt_prop(slot0_size PATH "${slot0_flash}" PROPERTY "reg" INDEX 1) - dt_get_parent(slot0_flash) - dt_get_parent(slot0_flash) - dt_prop(erase_size_slot0 PATH "${slot0_flash}" PROPERTY "erase-block-size") +dt_nodelabel(slot0_flash NODELABEL "slot0_partition") +dt_prop(slot0_size PATH "${slot0_flash}" PROPERTY "reg" INDEX 1) +dt_get_parent(slot0_flash) +dt_get_parent(slot0_flash) +dt_prop(erase_size_slot0 PATH "${slot0_flash}" PROPERTY "erase-block-size") + +if(CONFIG_BOOT_SWAP_USING_MOVE AND DEFINED erase_size_slot0) + zephyr_compile_definitions("MCUBOOT_SLOT0_EXPECTED_SECTOR_SIZE=${erase_size_slot0}") +endif() + +if(NOT CONFIG_SINGLE_APPLICATION_SLOT) + dt_nodelabel(slot1_flash NODELABEL "slot1_partition") + dt_prop(slot1_size PATH "${slot1_flash}" PROPERTY "reg" INDEX 1) + dt_get_parent(slot1_flash) + dt_get_parent(slot1_flash) + dt_prop(erase_size_slot1 PATH "${slot1_flash}" PROPERTY "erase-block-size") + + if(CONFIG_BOOT_SWAP_USING_MOVE AND DEFINED erase_size_slot1) + zephyr_compile_definitions("MCUBOOT_SLOT1_EXPECTED_SECTOR_SIZE=${erase_size_slot1}") + endif() +endif() +if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO) if(NOT DEFINED slot0_size) message(WARNING "Unable to determine size of slot0 partition, cannot calculate minimum sector usage") elseif(NOT DEFINED erase_size_slot0) @@ -395,12 +411,6 @@ if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO) endif() if(NOT CONFIG_SINGLE_APPLICATION_SLOT) - dt_nodelabel(slot1_flash NODELABEL "slot1_partition") - dt_prop(slot1_size PATH "${slot1_flash}" PROPERTY "reg" INDEX 1) - dt_get_parent(slot1_flash) - dt_get_parent(slot1_flash) - dt_prop(erase_size_slot1 PATH "${slot1_flash}" PROPERTY "erase-block-size") - if(NOT DEFINED slot1_size) message(WARNING "Unable to determine size of slot1 partition, cannot calculate minimum sector usage") elseif(NOT DEFINED erase_size_slot1)