Skip to content

Commit 7f3c77e

Browse files
committed
boot: bootutil: Add write block size checking
Adds write block size checking functionality and includes a zephyr implementation, this will not throw an error or prevent upgrade but will emit a debug log with a discrepency message Signed-off-by: Jamie McCrae <[email protected]>
1 parent c40d237 commit 7f3c77e

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed

boot/bootutil/src/swap_move.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ boot_slots_compatible(struct boot_loader_state *state)
311311
}
312312
#endif
313313

314+
#if defined(MCUBOOT_SLOT0_EXPECTED_WRITE_SIZE) || defined(MCUBOOT_SLOT1_EXPECTED_WRITE_SIZE)
315+
if (!swap_write_block_size_check(state)) {
316+
BOOT_LOG_WRN("Cannot upgrade: slot write sizes are not compatible");
317+
return 0;
318+
}
319+
#endif
320+
314321
if (num_sectors_pri > num_sectors_sec) {
315322
if (sector_sz_pri != boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i)) {
316323
BOOT_LOG_WRN("Cannot upgrade: not same sector layout");

boot/bootutil/src/swap_priv.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ static inline size_t boot_scratch_area_size(const struct boot_loader_state *stat
101101

102102
#endif /* defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE) */
103103

104+
#if defined(MCUBOOT_SWAP_USING_MOVE)
105+
/**
106+
* Check if device write block sizes are as expected, function should emit an error if there is
107+
* a problem. If true is returned, the slots are marked as compatible, otherwise the slots are
108+
* marked as incompatible.
109+
*
110+
* Requires MCUBOOT_SLOT0_EXPECTED_WRITE_SIZE be set to the write block size of image 0 primary
111+
* slot and MCUBOOT_SLOT1_EXPECTED_WRITE_SIZE be set to the write block size of image 0 secondary
112+
* slot.
113+
*/
114+
bool swap_write_block_size_check(struct boot_loader_state *state);
115+
#endif /* defined(MCUBOOT_SWAP_USING_MOVE) */
116+
104117
/**
105118
* Returns the maximum size of an application that can be loaded to a slot.
106119
*/

boot/zephyr/CMakeLists.txt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,16 @@ dt_prop(slot0_size PATH "${slot0_flash}" PROPERTY "reg" INDEX 1)
380380
dt_get_parent(slot0_flash)
381381
dt_get_parent(slot0_flash)
382382
dt_prop(erase_size_slot0 PATH "${slot0_flash}" PROPERTY "erase-block-size")
383+
dt_prop(write_size_slot0 PATH "${slot0_flash}" PROPERTY "write-block-size")
383384

384-
if(CONFIG_BOOT_SWAP_USING_MOVE AND DEFINED erase_size_slot0)
385-
zephyr_compile_definitions("MCUBOOT_SLOT0_EXPECTED_ERASE_SIZE=${erase_size_slot0}")
385+
if(CONFIG_BOOT_SWAP_USING_MOVE)
386+
if(DEFINED erase_size_slot0)
387+
zephyr_compile_definitions("MCUBOOT_SLOT0_EXPECTED_ERASE_SIZE=${erase_size_slot0}")
388+
endif()
389+
390+
if(DEFINED write_size_slot0)
391+
zephyr_compile_definitions("MCUBOOT_SLOT0_EXPECTED_WRITE_SIZE=${write_size_slot0}")
392+
endif()
386393
endif()
387394

388395
if(NOT CONFIG_SINGLE_APPLICATION_SLOT)
@@ -391,9 +398,16 @@ if(NOT CONFIG_SINGLE_APPLICATION_SLOT)
391398
dt_get_parent(slot1_flash)
392399
dt_get_parent(slot1_flash)
393400
dt_prop(erase_size_slot1 PATH "${slot1_flash}" PROPERTY "erase-block-size")
401+
dt_prop(write_size_slot1 PATH "${slot1_flash}" PROPERTY "write-block-size")
402+
403+
if(CONFIG_BOOT_SWAP_USING_MOVE)
404+
if(DEFINED erase_size_slot1)
405+
zephyr_compile_definitions("MCUBOOT_SLOT1_EXPECTED_ERASE_SIZE=${erase_size_slot1}")
406+
endif()
394407

395-
if(CONFIG_BOOT_SWAP_USING_MOVE AND DEFINED erase_size_slot1)
396-
zephyr_compile_definitions("MCUBOOT_SLOT1_EXPECTED_ERASE_SIZE=${erase_size_slot1}")
408+
if(DEFINED write_size_slot1)
409+
zephyr_compile_definitions("MCUBOOT_SLOT1_EXPECTED_WRITE_SIZE=${write_size_slot1}")
410+
endif()
397411
endif()
398412
endif()
399413

@@ -428,6 +442,10 @@ if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO)
428442
endif()
429443
endif()
430444

445+
if((CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE) AND (DEFINED write_size_slot0 OR DEFINED write_size_slot1))
446+
zephyr_library_sources(flash_check.c)
447+
endif()
448+
431449
if(SYSBUILD)
432450
if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER OR CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_UPGRADE_ONLY OR CONFIG_BOOT_DIRECT_XIP OR CONFIG_BOOT_RAM_LOAD)
433451
# TODO: RAM LOAD support

boot/zephyr/flash_check.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/devicetree.h>
9+
#include <zephyr/drivers/flash.h>
10+
11+
#include <../../bootutil/src/bootutil_priv.h>
12+
#include "bootutil/bootutil_log.h"
13+
14+
#include "mcuboot_config/mcuboot_config.h"
15+
16+
#if defined(MCUBOOT_SLOT0_EXPECTED_WRITE_SIZE) || defined(MCUBOOT_SLOT1_EXPECTED_WRITE_SIZE)
17+
BOOT_LOG_MODULE_DECLARE(mcuboot);
18+
19+
bool swap_write_block_size_check(struct boot_loader_state *state)
20+
{
21+
#ifdef MCUBOOT_SLOT0_EXPECTED_WRITE_SIZE
22+
size_t flash_write_block_size_pri;
23+
#endif
24+
#ifdef MCUBOOT_SLOT1_EXPECTED_WRITE_SIZE
25+
size_t flash_write_block_size_sec;
26+
#endif
27+
28+
#ifdef MCUBOOT_SLOT0_EXPECTED_WRITE_SIZE
29+
flash_write_block_size_pri = flash_get_write_block_size(
30+
state->imgs[0][BOOT_PRIMARY_SLOT].area->fa_dev);
31+
32+
if (flash_write_block_size_pri != MCUBOOT_SLOT0_EXPECTED_WRITE_SIZE) {
33+
BOOT_LOG_DBG("Discrepancy, slot0 expected write block size: %d, actual: %d",
34+
MCUBOOT_SLOT0_EXPECTED_WRITE_SIZE, flash_write_block_size_pri);
35+
}
36+
#endif
37+
38+
#ifdef MCUBOOT_SLOT1_EXPECTED_WRITE_SIZE
39+
flash_write_block_size_sec = flash_get_write_block_size(
40+
state->imgs[0][BOOT_SECONDARY_SLOT].area->fa_dev);
41+
42+
if (flash_write_block_size_sec != MCUBOOT_SLOT1_EXPECTED_WRITE_SIZE) {
43+
BOOT_LOG_DBG("Discrepancy, slot1 expected write block size: %d, actual: %d",
44+
MCUBOOT_SLOT1_EXPECTED_WRITE_SIZE, flash_write_block_size_sec);
45+
}
46+
#endif
47+
48+
return true;
49+
}
50+
#endif

0 commit comments

Comments
 (0)