Skip to content

Commit 6bb538f

Browse files
committed
trusted-firmware-m: Set --max-sectors when signing
The --max-sectors option helps catch problems with flash overlap when merging images. If there is a misalignment in flash partitions, the merge process usually fails. This uses information from Zephyr flash partitions and the flash controller to automatically determine the max sectors value and apply it when signing an image. Signed-off-by: BUDKE Gerson Fernando <[email protected]>
1 parent 5dd5974 commit 6bb538f

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

modules/trusted-firmware-m/CMakeLists.txt

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -453,15 +453,17 @@ if (CONFIG_BUILD_WITH_TFM)
453453
if(CONFIG_TFM_BL2)
454454
set(image_alignment 1)
455455
set(flash_write_block_size 1)
456+
set(flash_erase_block_size 1)
456457

457458
dt_chosen(chosen_flash PROPERTY "zephyr,flash")
458459
if(DEFINED chosen_flash AND chosen_flash)
459460
dt_prop(flash_write_block_size PATH ${chosen_flash} PROPERTY write-block-size)
461+
dt_prop(flash_erase_block_size PATH ${chosen_flash} PROPERTY erase-block-size)
460462
else()
461463
message(WARNING
462464
"The 'zephyr,flash' chosen property is not defined!
463-
Using flash_write_block_size default value possible differs from
464-
TF-M board definitions resulting in improver sign."
465+
Using flash_write_block_size and flash_erase_block_size default values
466+
that may differ from TF-M board definitions resulting in invalid signatures."
465467
)
466468
endif()
467469

@@ -481,9 +483,26 @@ if (CONFIG_BUILD_WITH_TFM)
481483
set(image_alignment ${flash_write_block_size})
482484
endif()
483485
endif()
486+
487+
# Calculate the maximum number of sectors necessary to store the image.
488+
dt_nodelabel(s_partition_node NODELABEL "slot0_partition" REQUIRED)
489+
dt_nodelabel(ns_partition_node NODELABEL "slot0_ns_partition" REQUIRED)
490+
dt_reg_size(s_partition_size PATH ${s_partition_node})
491+
dt_reg_size(ns_partition_size PATH ${ns_partition_node})
492+
math(EXPR S_MAX_SECTORS "${s_partition_size} / ${flash_erase_block_size}")
493+
math(EXPR NS_MAX_SECTORS "${ns_partition_size} / ${flash_erase_block_size}")
494+
if(CONFIG_TFM_MCUBOOT_IMAGE_NUMBER STREQUAL "1")
495+
math(EXPR S_NS_MAX_SECTORS "${S_MAX_SECTORS} + ${NS_MAX_SECTORS}")
496+
else()
497+
if(${S_MAX_SECTORS} GREATER ${NS_MAX_SECTORS})
498+
set(S_NS_MAX_SECTORS ${S_MAX_SECTORS})
499+
else()
500+
set(S_NS_MAX_SECTORS ${NS_MAX_SECTORS})
501+
endif()
502+
endif()
484503
endif()
485504

486-
function(tfm_sign OUT_ARG SUFFIX PAD INPUT_FILE OUTPUT_FILE)
505+
function(tfm_sign OUT_ARG SUFFIX PAD MAX_SECTORS INPUT_FILE OUTPUT_FILE)
487506
if(PAD)
488507
set(pad_args --pad --pad-header)
489508
endif()
@@ -503,6 +522,7 @@ if (CONFIG_BUILD_WITH_TFM)
503522
-k ${CONFIG_TFM_KEY_FILE_${SUFFIX}}
504523
--public-key-format ${TFM_PUBLIC_KEY_FORMAT}
505524
--align ${image_alignment}
525+
--max-sectors ${MAX_SECTORS}
506526
-v ${CONFIG_TFM_IMAGE_VERSION_${SUFFIX}}
507527
${pad_args}
508528
${HEX_ADDR_ARGS_${SUFFIX}}
@@ -543,7 +563,7 @@ if (CONFIG_BUILD_WITH_TFM)
543563
)
544564

545565
elseif(CONFIG_TFM_MCUBOOT_IMAGE_NUMBER STREQUAL "1")
546-
tfm_sign(sign_cmd S_NS TRUE ${S_NS_FILE} ${S_NS_SIGNED_FILE})
566+
tfm_sign(sign_cmd S_NS TRUE ${S_NS_MAX_SECTORS} ${S_NS_FILE} ${S_NS_SIGNED_FILE})
547567

548568
set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
549569
COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/mergehex.py
@@ -568,12 +588,13 @@ if (CONFIG_BUILD_WITH_TFM)
568588

569589
else()
570590
if (CONFIG_TFM_USE_NS_APP)
571-
tfm_sign(sign_cmd_ns NS TRUE ${NS_APP_FILE} ${NS_SIGNED_FILE})
591+
tfm_sign(sign_cmd_ns NS TRUE ${S_NS_MAX_SECTORS} ${NS_APP_FILE} ${NS_SIGNED_FILE})
572592
else()
573-
tfm_sign(sign_cmd_ns NS FALSE ${NS_APP_FILE} ${NS_SIGNED_FILE})
593+
tfm_sign(sign_cmd_ns NS FALSE ${S_NS_MAX_SECTORS} ${NS_APP_FILE} ${NS_SIGNED_FILE})
574594
endif()
575595

576-
tfm_sign(sign_cmd_s S TRUE $<TARGET_PROPERTY:tfm,TFM_S_HEX_FILE> ${S_SIGNED_FILE})
596+
tfm_sign(sign_cmd_s S TRUE ${S_NS_MAX_SECTORS} $<TARGET_PROPERTY:tfm,TFM_S_HEX_FILE>
597+
${S_SIGNED_FILE})
577598

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

0 commit comments

Comments
 (0)