Skip to content

Commit c58012a

Browse files
committed
trusted-firmware-m: Set --max-sectors when signing
The --max-sectors will helps to catch problems with flash overlap when merging images. If there is a missalignment from flash partitions usually the merge process will fail. This pick information from zephyr flash partitions and flash controller to automatically determine the max sectors value and apply it when singing an image. Signed-off-by: BUDKE Gerson Fernando <[email protected]>
1 parent bc7bd49 commit c58012a

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

modules/trusted-firmware-m/CMakeLists.txt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ if (CONFIG_BUILD_WITH_TFM)
446446

447447
dt_chosen(chosen_flash PROPERTY "zephyr,flash")
448448
dt_prop(flash_write_block_size PATH ${chosen_flash} PROPERTY write-block-size)
449+
dt_prop(flash_erase_block_size PATH ${chosen_flash} PROPERTY erase-block-size)
449450
# The alignment is determined by the minimal amount of bytes necessary to
450451
# be written in the flash sector. Ex., assuming that the sector erase
451452
# operation is 1KiB and, on that sector, the minimal amount of bytes that
@@ -463,8 +464,15 @@ if (CONFIG_BUILD_WITH_TFM)
463464
else()
464465
set(image_alignment 1)
465466
endif()
467+
# Calculate the maximum number of sectors necessary to store the image.
468+
dt_nodelabel(s_partition_node NODELABEL "slot0_partition")
469+
dt_nodelabel(ns_partition_node NODELABEL "slot0_ns_partition")
470+
dt_reg_size(s_partition_size PATH ${s_partition_node})
471+
dt_reg_size(ns_partition_size PATH ${ns_partition_node})
472+
math(EXPR S_MAX_SECTORS "${s_partition_size} / ${flash_erase_block_size}")
473+
math(EXPR NS_MAX_SECTORS "${ns_partition_size} / ${flash_erase_block_size}")
466474

467-
function(tfm_sign OUT_ARG SUFFIX PAD INPUT_FILE OUTPUT_FILE)
475+
function(tfm_sign OUT_ARG SUFFIX PAD MAX_SECTORS INPUT_FILE OUTPUT_FILE)
468476
if(PAD)
469477
set(pad_args --pad --pad-header)
470478
endif()
@@ -484,6 +492,7 @@ if (CONFIG_BUILD_WITH_TFM)
484492
-k ${CONFIG_TFM_KEY_FILE_${SUFFIX}}
485493
--public-key-format ${TFM_PUBLIC_KEY_FORMAT}
486494
--align ${image_alignment}
495+
--max-sectors ${MAX_SECTORS}
487496
-v ${CONFIG_TFM_IMAGE_VERSION_${SUFFIX}}
488497
${pad_args}
489498
${HEX_ADDR_ARGS_${SUFFIX}}
@@ -524,7 +533,7 @@ if (CONFIG_BUILD_WITH_TFM)
524533
)
525534

526535
elseif(CONFIG_TFM_MCUBOOT_IMAGE_NUMBER STREQUAL "1")
527-
tfm_sign(sign_cmd S_NS TRUE ${S_NS_FILE} ${S_NS_SIGNED_FILE})
536+
tfm_sign(sign_cmd S_NS TRUE ${S_MAX_SECTORS} ${S_NS_FILE} ${S_NS_SIGNED_FILE})
528537

529538
set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
530539
COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/mergehex.py
@@ -549,12 +558,12 @@ if (CONFIG_BUILD_WITH_TFM)
549558

550559
else()
551560
if (CONFIG_TFM_USE_NS_APP)
552-
tfm_sign(sign_cmd_ns NS TRUE ${NS_APP_FILE} ${NS_SIGNED_FILE})
561+
tfm_sign(sign_cmd_ns NS TRUE ${NS_MAX_SECTORS} ${NS_APP_FILE} ${NS_SIGNED_FILE})
553562
else()
554-
tfm_sign(sign_cmd_ns NS FALSE ${NS_APP_FILE} ${NS_SIGNED_FILE})
563+
tfm_sign(sign_cmd_ns NS FALSE ${NS_MAX_SECTORS} ${NS_APP_FILE} ${NS_SIGNED_FILE})
555564
endif()
556565

557-
tfm_sign(sign_cmd_s S TRUE $<TARGET_PROPERTY:tfm,TFM_S_HEX_FILE> ${S_SIGNED_FILE})
566+
tfm_sign(sign_cmd_s S TRUE ${S_MAX_SECTORS} $<TARGET_PROPERTY:tfm,TFM_S_HEX_FILE> ${S_SIGNED_FILE})
558567

559568
#Create and sign for concatenated binary image, should align with the TF-M BL2
560569
set_property(GLOBAL APPEND PROPERTY extra_post_build_commands

0 commit comments

Comments
 (0)