Skip to content

Commit eaae650

Browse files
committed
zephyr: Add support for automatically calculcating max sectors
Adds a feature that will calculate the maximum number of sectors that are needed for a build. Can be disabled to revert back to the old behaviour by disabling CONFIG_BOOT_MAX_IMG_SECTORS_AUTO Signed-off-by: Jamie McCrae <[email protected]>
1 parent 6fe259b commit eaae650

File tree

3 files changed

+77
-14
lines changed

3 files changed

+77
-14
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -363,23 +363,66 @@ if(CONFIG_MCUBOOT_BOOT_BANNER)
363363
zephyr_sources(kernel/banner.c)
364364
endif()
365365

366-
if(SYSBUILD)
367-
function(align_up num align result)
368-
math(EXPR out "(((${num}) + ((${align}) - 1)) & ~((${align}) - 1))")
369-
set(${result} "${out}" PARENT_SCOPE)
370-
endfunction()
366+
function(align_up num align result)
367+
math(EXPR out "(((${num}) + ((${align}) - 1)) & ~((${align}) - 1))")
368+
set(${result} "${out}" PARENT_SCOPE)
369+
endfunction()
370+
371+
function(dt_get_parent node)
372+
string(FIND "${${node}}" "/" pos REVERSE)
373+
374+
if(pos EQUAL -1)
375+
message(ERROR "Unable to get parent of node: ${${node}}")
376+
endif()
371377

372-
function(dt_get_parent node)
373-
string(FIND "${${node}}" "/" pos REVERSE)
378+
string(SUBSTRING "${${node}}" 0 ${pos} ${node})
379+
set(${node} "${${node}}" PARENT_SCOPE)
380+
endfunction()
381+
382+
if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO)
383+
dt_nodelabel(slot0_flash NODELABEL "slot0_partition")
384+
dt_prop(slot0_size PATH "${slot0_flash}" PROPERTY "reg" INDEX 1)
385+
dt_get_parent(slot0_flash)
386+
dt_get_parent(slot0_flash)
387+
dt_prop(erase_size_slot0 PATH "${slot0_flash}" PROPERTY "erase-block-size")
388+
389+
if(NOT DEFINED slot0_size)
390+
message(WARNING "Unable to determine size of slot0 partition, cannot calculate minimum sector usage")
391+
elseif(NOT DEFINED erase_size_slot0)
392+
message(WARNING "Unable to determine erase size of slot0 partition, cannot calculate minimum sector usage")
393+
else()
394+
math(EXPR slot_min_sectors "${slot0_size} / ${erase_size_slot0}")
395+
endif()
396+
397+
if(NOT CONFIG_SINGLE_APPLICATION_SLOT)
398+
dt_nodelabel(slot1_flash NODELABEL "slot1_partition")
399+
dt_prop(slot1_size PATH "${slot1_flash}" PROPERTY "reg" INDEX 1)
400+
dt_get_parent(slot1_flash)
401+
dt_get_parent(slot1_flash)
402+
dt_prop(erase_size_slot1 PATH "${slot1_flash}" PROPERTY "erase-block-size")
403+
404+
if(NOT DEFINED slot1_size)
405+
message(WARNING "Unable to determine size of slot1 partition, cannot calculate minimum sector usage")
406+
elseif(NOT DEFINED erase_size_slot1)
407+
message(WARNING "Unable to determine erase size of slot1 partition, cannot calculate minimum sector usage")
408+
else()
409+
math(EXPR slot1_min_sectors "${slot1_size} / ${erase_size_slot1}")
374410

375-
if(pos EQUAL -1)
376-
message(ERROR "Unable to get parent of node: ${${node}}")
411+
if("${slot1_min_sectors}" GREATER "${slot_min_sectors}")
412+
set(slot_min_sectors ${slot1_min_sectors})
413+
endif()
377414
endif()
415+
endif()
378416

379-
string(SUBSTRING "${${node}}" 0 ${pos} ${node})
380-
set(${node} "${${node}}" PARENT_SCOPE)
381-
endfunction()
417+
if(DEFINED slot_min_sectors AND "${slot_min_sectors}" GREATER "0")
418+
zephyr_compile_definitions("MIN_SECTOR_COUNT=${slot_min_sectors}")
419+
message("Calculated maximum number of sectors: ${slot_min_sectors}")
420+
else()
421+
message(WARNING "Unable to calculate minimum number of sector sizes, falling back to 128 sector default. Please disable CONFIG_BOOT_MAX_IMG_SECTORS_AUTO and set CONFIG_BOOT_MAX_IMG_SECTORS to the required value")
422+
endif()
423+
endif()
382424

425+
if(SYSBUILD)
383426
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)
384427
# TODO: RAM LOAD support
385428
dt_nodelabel(slot0_flash NODELABEL "slot0_partition")
@@ -495,7 +538,11 @@ if(SYSBUILD)
495538
endif()
496539

497540
if(CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE)
498-
math(EXPR boot_status_data_size "${CONFIG_BOOT_MAX_IMG_SECTORS} * (3 * ${write_size})")
541+
if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO AND DEFINED slot_min_sectors AND "${slot_min_sectors}" GREATER "0")
542+
math(EXPR boot_status_data_size "${slot_min_sectors} * (3 * ${write_size})")
543+
else()
544+
math(EXPR boot_status_data_size "${CONFIG_BOOT_MAX_IMG_SECTORS} * (3 * ${write_size})")
545+
endif()
499546
else()
500547
set(boot_status_data_size 0)
501548
endif()

boot/zephyr/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,21 @@ config BOOT_ENCRYPTION_KEY_FILE
380380
with the public key information will be written in a format expected by
381381
MCUboot.
382382

383+
config BOOT_MAX_IMG_SECTORS_AUTO
384+
bool "Calculate maximum sectors automatically"
385+
default y
386+
help
387+
If this option is enabled then the maximum number of supported sectors per image will
388+
be calculated automatically from the flash erase sizes and size of each partition for
389+
the first image.
390+
391+
If this information is not available, or multiple images are used, then this option
392+
should be disabled and BOOT_MAX_IMG_SECTORS should be set instead
393+
383394
config BOOT_MAX_IMG_SECTORS
384395
int "Maximum number of sectors per image slot"
385396
default 128
397+
depends on !BOOT_MAX_IMG_SECTORS_AUTO
386398
help
387399
This option controls the maximum number of sectors that each of
388400
the two image areas can contain. Smaller values reduce MCUboot's

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,11 @@
270270
# endif
271271
#endif
272272

273-
#ifdef CONFIG_BOOT_MAX_IMG_SECTORS
273+
#if defined(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO) && defined(MIN_SECTOR_COUNT)
274+
275+
#define MCUBOOT_MAX_IMG_SECTORS MIN_SECTOR_COUNT
276+
277+
#elif defined(CONFIG_BOOT_MAX_IMG_SECTORS)
274278

275279
#define MCUBOOT_MAX_IMG_SECTORS CONFIG_BOOT_MAX_IMG_SECTORS
276280

0 commit comments

Comments
 (0)