Skip to content

Commit 5dd5974

Browse files
committed
trusted-firmware-m: Set --align when signing
The current version of TF-M script that sign MCUboot image uses a default alignment of 1. This value varies between flash devices and not all accept the default 1. This improve the script picking the write-block-size property from the current flash controller and pass as the --align parameter when signing an image. Note: This solution works out-of-box for the vast majority of devices in the Zephyr tree and an exception will throw when a device is not supported. Signed-off-by: BUDKE Gerson Fernando <[email protected]>
1 parent a27d5a0 commit 5dd5974

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

modules/trusted-firmware-m/CMakeLists.txt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,39 @@ if (CONFIG_BUILD_WITH_TFM)
450450
set(HEX_ADDR_ARGS_NS "--hex-addr=${TFM_HEX_BASE_ADDRESS_NS}")
451451
endif()
452452

453+
if(CONFIG_TFM_BL2)
454+
set(image_alignment 1)
455+
set(flash_write_block_size 1)
456+
457+
dt_chosen(chosen_flash PROPERTY "zephyr,flash")
458+
if(DEFINED chosen_flash AND chosen_flash)
459+
dt_prop(flash_write_block_size PATH ${chosen_flash} PROPERTY write-block-size)
460+
else()
461+
message(WARNING
462+
"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+
)
466+
endif()
467+
468+
# The alignment is determined by the minimal amount of bytes necessary to
469+
# be written in the flash sector. Ex., assuming that the sector erase
470+
# operation is 1KiB and, on that sector, the minimum amount of bytes that
471+
# must be written is 8 bytes then the alignment is 8.
472+
#
473+
# Current MCUboot maximum alignment is 32 bytes.
474+
if(flash_write_block_size GREATER 0)
475+
if(flash_write_block_size GREATER 32)
476+
message(WARNING
477+
"imgtool max alignment is 32 and current value is ${flash_write_block_size}.
478+
Keep default image alignment of 1."
479+
)
480+
else()
481+
set(image_alignment ${flash_write_block_size})
482+
endif()
483+
endif()
484+
endif()
485+
453486
function(tfm_sign OUT_ARG SUFFIX PAD INPUT_FILE OUTPUT_FILE)
454487
if(PAD)
455488
set(pad_args --pad --pad-header)
@@ -469,7 +502,7 @@ if (CONFIG_BUILD_WITH_TFM)
469502
--layout ${layout_file}
470503
-k ${CONFIG_TFM_KEY_FILE_${SUFFIX}}
471504
--public-key-format ${TFM_PUBLIC_KEY_FORMAT}
472-
--align 1
505+
--align ${image_alignment}
473506
-v ${CONFIG_TFM_IMAGE_VERSION_${SUFFIX}}
474507
${pad_args}
475508
${HEX_ADDR_ARGS_${SUFFIX}}

0 commit comments

Comments
 (0)