Skip to content

Commit 7fe5574

Browse files
nandojvecfriedt
authored andcommitted
trusted-firmware-m: Use cmake_parse_arguments in tfm_sign
Use cmake_parse_arguments() for more idiomatic code. This makes the code more readable and easier to extend with new options. Signed-off-by: BUDKE Gerson Fernando <gerson.budke@leica-geosystems.com>
1 parent 5b4cd27 commit 7fe5574

File tree

1 file changed

+66
-31
lines changed

1 file changed

+66
-31
lines changed

modules/trusted-firmware-m/CMakeLists.txt

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -499,45 +499,67 @@ if (CONFIG_BUILD_WITH_TFM)
499499
endif()
500500
endif()
501501

502-
function(tfm_sign OUT_ARG SUFFIX HEADER TRAILER CONFIRM MAX_SECTORS INPUT_FILE OUTPUT_FILE)
503-
if(HEADER AND TRAILER)
502+
function(tfm_sign OUT_ARG)
503+
set(options HEADER TRAILER CONFIRM)
504+
set(oneValueArgs SUFFIX MAX_SECTORS INPUT_FILE OUTPUT_FILE)
505+
set(multiValueArgs "")
506+
507+
cmake_parse_arguments(
508+
TFM_SIGN_ARG
509+
"${options}"
510+
"${oneValueArgs}"
511+
"${multiValueArgs}"
512+
${ARGN}
513+
)
514+
515+
if(NOT DEFINED TFM_SIGN_ARG_SUFFIX OR
516+
NOT DEFINED TFM_SIGN_ARG_INPUT_FILE OR
517+
NOT DEFINED TFM_SIGN_ARG_OUTPUT_FILE)
518+
message(FATAL_ERROR "SUFFIX, INPUT_FILE and OUTPUT_FILE are required arguments")
519+
endif()
520+
521+
set(pad_args "")
522+
if(TFM_SIGN_ARG_HEADER AND TFM_SIGN_ARG_TRAILER)
504523
set(pad_args --pad --pad-header)
505-
elseif(HEADER)
524+
elseif(TFM_SIGN_ARG_HEADER)
506525
set(pad_args --pad-header)
507-
elseif(TRAILER)
526+
elseif(TFM_SIGN_ARG_TRAILER)
508527
set(pad_args --pad)
509528
endif()
510-
if(CONFIRM)
511-
# --confirm imply PAD
529+
530+
set(confirm "")
531+
if(TFM_SIGN_ARG_CONFIRM)
512532
set(confirm --confirm)
513533
endif()
534+
514535
# Secure + Non-secure images are signed the same way as a secure only
515536
# build, but with a different layout file.
516-
set(layout_file ${PREPROCESSED_FILE_${SUFFIX}})
517-
if(SUFFIX STREQUAL "S_NS")
518-
set(SUFFIX "S")
537+
set(layout_file ${PREPROCESSED_FILE_${TFM_SIGN_ARG_SUFFIX}})
538+
if(TFM_SIGN_ARG_SUFFIX STREQUAL "S_NS")
539+
set(TFM_SIGN_ARG_SUFFIX "S")
519540
endif()
520-
set (${OUT_ARG}
541+
542+
set(${OUT_ARG}
521543
# Add the MCUBoot script to the path so that if there is a version of imgtool in there then
522544
# it gets used over the system imgtool. Used so that imgtool from upstream
523545
# mcuboot is preferred over system imgtool
524546
${CMAKE_COMMAND} -E env PYTHONPATH=${ZEPHYR_MCUBOOT_MODULE_DIR}/scripts
525547
${PYTHON_EXECUTABLE} ${TFM_MCUBOOT_DIR}/scripts/wrapper/wrapper.py
526548
--layout ${layout_file}
527-
-k ${CONFIG_TFM_KEY_FILE_${SUFFIX}}
549+
-k ${CONFIG_TFM_KEY_FILE_${TFM_SIGN_ARG_SUFFIX}}
528550
--public-key-format ${TFM_PUBLIC_KEY_FORMAT}
529551
--align ${image_alignment}
530-
--max-sectors ${MAX_SECTORS}
531-
-v ${CONFIG_TFM_IMAGE_VERSION_${SUFFIX}}
552+
--max-sectors ${TFM_SIGN_ARG_MAX_SECTORS}
553+
-v ${CONFIG_TFM_IMAGE_VERSION_${TFM_SIGN_ARG_SUFFIX}}
532554
${pad_args}
533555
${confirm}
534-
${HEX_ADDR_ARGS_${SUFFIX}}
535-
${ADD_${SUFFIX}_IMAGE_MIN_VER}
556+
${HEX_ADDR_ARGS_${TFM_SIGN_ARG_SUFFIX}}
557+
${ADD_${TFM_SIGN_ARG_SUFFIX}_IMAGE_MIN_VER}
536558
-s ${CONFIG_TFM_IMAGE_SECURITY_COUNTER}
537559
--measured-boot-record
538560
-H ${CONFIG_ROM_START_OFFSET}
539-
${INPUT_FILE}
540-
${OUTPUT_FILE}
561+
${TFM_SIGN_ARG_INPUT_FILE}
562+
${TFM_SIGN_ARG_OUTPUT_FILE}
541563
PARENT_SCOPE)
542564
endfunction()
543565

@@ -573,8 +595,9 @@ if (CONFIG_BUILD_WITH_TFM)
573595
)
574596

575597
elseif(CONFIG_TFM_MCUBOOT_IMAGE_NUMBER STREQUAL "1")
576-
tfm_sign(sign_cmd_s_ns_hex S_NS TRUE TRUE TRUE ${S_NS_MAX_SECTORS} ${S_NS_HEX_FILE}
577-
${S_NS_SIGNED_HEX_FILE})
598+
tfm_sign(sign_cmd_s_ns_hex SUFFIX "S_NS"
599+
HEADER TRAILER CONFIRM MAX_SECTORS ${S_NS_MAX_SECTORS}
600+
INPUT_FILE ${S_NS_HEX_FILE} OUTPUT_FILE ${S_NS_SIGNED_HEX_FILE})
578601

579602
set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
580603
COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/mergehex.py
@@ -599,21 +622,33 @@ if (CONFIG_BUILD_WITH_TFM)
599622

600623
else()
601624
if (CONFIG_TFM_USE_NS_APP)
602-
tfm_sign(sign_cmd_ns_hex NS TRUE TRUE TRUE ${S_NS_MAX_SECTORS} ${NS_HEX_APP_FILE}
603-
${NS_SIGNED_HEX_FILE})
604-
tfm_sign(sign_cmd_ns_bin NS TRUE TRUE FALSE ${S_NS_MAX_SECTORS} ${NS_BIN_APP_FILE}
605-
${NS_SIGNED_BIN_FILE})
625+
tfm_sign(sign_cmd_ns_hex SUFFIX "NS"
626+
HEADER TRAILER CONFIRM MAX_SECTORS ${S_NS_MAX_SECTORS}
627+
INPUT_FILE ${NS_HEX_APP_FILE}
628+
OUTPUT_FILE ${NS_SIGNED_HEX_FILE})
629+
tfm_sign(sign_cmd_ns_bin SUFFIX "NS"
630+
HEADER TRAILER MAX_SECTORS ${S_NS_MAX_SECTORS}
631+
INPUT_FILE ${NS_BIN_APP_FILE}
632+
OUTPUT_FILE ${NS_SIGNED_BIN_FILE})
606633
else()
607-
tfm_sign(sign_cmd_ns NS FALSE TRUE TRUE ${S_NS_MAX_SECTORS} ${NS_HEX_APP_FILE}
608-
${NS_SIGNED_HEX_FILE})
609-
tfm_sign(sign_cmd_ns_bin NS FALSE FALSE FALSE ${S_NS_MAX_SECTORS} ${NS_BIN_APP_FILE}
610-
${NS_SIGNED_BIN_FILE})
634+
tfm_sign(sign_cmd_ns_hex SUFFIX "NS"
635+
TRAILER CONFIRM MAX_SECTORS ${S_NS_MAX_SECTORS}
636+
INPUT_FILE ${NS_HEX_APP_FILE}
637+
OUTPUT_FILE ${NS_SIGNED_HEX_FILE})
638+
tfm_sign(sign_cmd_ns_bin SUFFIX "NS"
639+
MAX_SECTORS ${S_NS_MAX_SECTORS}
640+
INPUT_FILE ${NS_BIN_APP_FILE}
641+
OUTPUT_FILE ${NS_SIGNED_BIN_FILE})
611642
endif()
612643

613-
tfm_sign(sign_cmd_s_hex S TRUE TRUE TRUE ${S_NS_MAX_SECTORS}
614-
$<TARGET_PROPERTY:tfm,TFM_S_HEX_FILE> ${S_SIGNED_HEX_FILE})
615-
tfm_sign(sign_cmd_s_bin S TRUE TRUE FALSE ${S_NS_MAX_SECTORS}
616-
$<TARGET_PROPERTY:tfm,TFM_S_BIN_FILE> ${S_SIGNED_BIN_FILE})
644+
tfm_sign(sign_cmd_s_hex SUFFIX "S"
645+
HEADER TRAILER CONFIRM MAX_SECTORS ${S_NS_MAX_SECTORS}
646+
INPUT_FILE $<TARGET_PROPERTY:tfm,TFM_S_HEX_FILE>
647+
OUTPUT_FILE ${S_SIGNED_HEX_FILE})
648+
tfm_sign(sign_cmd_s_bin SUFFIX "S"
649+
HEADER TRAILER MAX_SECTORS ${S_NS_MAX_SECTORS}
650+
INPUT_FILE $<TARGET_PROPERTY:tfm,TFM_S_BIN_FILE>
651+
OUTPUT_FILE ${S_SIGNED_BIN_FILE})
617652

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

0 commit comments

Comments
 (0)