@@ -73,12 +73,25 @@ function(zephyr_mcuboot_tasks)
7373 return ()
7474 endif ()
7575
76- # Basic 'west sign' command and output format independent arguments.
77- separate_arguments (west_sign_extra UNIX_COMMAND ${CONFIG_MCUBOOT_CMAKE_WEST_SIGN_PARAMS} )
78- set (west_sign ${WEST} sign ${west_sign_extra}
79- --tool imgtool
80- --tool-path "${imgtool_path} "
81- --build -dir "${APPLICATION_BINARY_DIR} " )
76+ # Fetch devicetree details for flash and slot information
77+ dt_chosen(flash_node PROPERTY "zephyr,flash" )
78+ dt_nodelabel(slot0_flash NODELABEL "slot0_partition" )
79+ dt_prop(slot_size PATH "${slot0_flash} " PROPERTY "reg" INDEX 1)
80+ dt_prop(write_block_size PATH "${flash_node} " PROPERTY "write-block-size" )
81+
82+ # If single slot mode, or if in firmware updater mode and this is the firmware updater image,
83+ # use slot 0 information
84+ if (NOT CONFIG_MCUBOOT_BOOTLOADER_MODE_SINGLE_APP AND (NOT CONFIG_MCUBOOT_BOOTLOADER_MODE_FIRMWARE_UPDATER OR CONFIG_MCUBOOT_APPLICATION_FIRMWARE_UPDATER))
85+ # Slot 1 size is used instead of slot 0 size
86+ set (slot_size)
87+ dt_nodelabel(slot1_flash NODELABEL "slot1_partition" )
88+ dt_prop(slot_size PATH "${slot1_flash} " PROPERTY "reg" INDEX 1)
89+ endif ()
90+
91+ # Basic 'imgtool sign' command with known image information.
92+ set (imgtool_sign ${PYTHON_EXECUTABLE} ${imgtool_path} sign
93+ --version ${CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION} --header-size ${CONFIG_ROM_START_OFFSET}
94+ --slot-size ${slot_size} )
8295
8396 # Arguments to imgtool.
8497 if (NOT CONFIG_MCUBOOT_EXTRA_IMGTOOL_ARGS STREQUAL "" )
@@ -87,102 +100,90 @@ function(zephyr_mcuboot_tasks)
87100 #
88101 # Use UNIX_COMMAND syntax for uniform results across host
89102 # platforms.
90- separate_arguments (imgtool_extra UNIX_COMMAND ${CONFIG_MCUBOOT_EXTRA_IMGTOOL_ARGS} )
103+ separate_arguments (imgtool_args UNIX_COMMAND ${CONFIG_MCUBOOT_EXTRA_IMGTOOL_ARGS} )
91104 else ()
92- set (imgtool_extra )
105+ set (imgtool_args )
93106 endif ()
94107
95108 if (NOT "${keyfile} " STREQUAL "" )
96- set (imgtool_extra --key "${keyfile} " ${imgtool_extra } )
109+ set (imgtool_args --key "${keyfile} " ${imgtool_args } )
97110 endif ()
98111
99112 # Use overwrite-only instead of swap upgrades.
100113 if (CONFIG_MCUBOOT_IMGTOOL_OVERWRITE_ONLY)
101- set (imgtool_extra --overwrite-only --align 1 ${imgtool_extra} )
114+ set (imgtool_args --overwrite-only --align 1 ${imgtool_args} )
115+ else ()
116+ set (imgtool_args --align ${write_block_size} ${imgtool_args} )
102117 endif ()
103118
104- set (imgtool_args -- ${imgtool_extra} )
105-
106119 # Extensionless prefix of any output file.
107120 set (output ${ZEPHYR_BINARY_DIR} /${KERNEL_NAME} )
108121
109122 # List of additional build byproducts.
110123 set (byproducts )
111124
112- # 'west sign' arguments for confirmed, unconfirmed and encrypted images.
113- set (unconfirmed_args)
114- set (confirmed_args)
115- set (encrypted_args)
116-
117125 # Set up .bin outputs.
118126 if (CONFIG_BUILD_OUTPUT_BIN)
119- list (APPEND unconfirmed_args --bin --sbin ${output} .signed.bin)
120127 list (APPEND byproducts ${output} .signed.bin)
121128 zephyr_runner_file(bin ${output} .signed.bin)
122129 set (BYPRODUCT_KERNEL_SIGNED_BIN_NAME "${output} .signed.bin"
123130 CACHE FILEPATH "Signed kernel bin file" FORCE
124131 )
132+ set_property (GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
133+ ${imgtool_sign} ${imgtool_args} ${output} .bin ${output} .signed.bin)
125134
126135 if (CONFIG_MCUBOOT_GENERATE_CONFIRMED_IMAGE)
127- list (APPEND confirmed_args --bin --sbin ${output} .signed.confirmed.bin)
128136 list (APPEND byproducts ${output} .signed.confirmed.bin)
129137 set (BYPRODUCT_KERNEL_SIGNED_CONFIRMED_BIN_NAME "${output} .signed.confirmed.bin"
130138 CACHE FILEPATH "Signed and confirmed kernel bin file" FORCE
131139 )
140+ set_property (GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
141+ ${imgtool_sign} ${imgtool_args} --pad --confirm ${output} .bin
142+ ${output} .signed.confirmed.bin)
132143 endif ()
133144
134145 if (NOT "${keyfile_enc} " STREQUAL "" )
135- list (APPEND encrypted_args --bin --sbin ${output} .signed.encrypted.bin)
136146 list (APPEND byproducts ${output} .signed.encrypted.bin)
137147 set (BYPRODUCT_KERNEL_SIGNED_ENCRYPTED_BIN_NAME "${output} .signed.encrypted.bin"
138148 CACHE FILEPATH "Signed and encrypted kernel bin file" FORCE
139149 )
150+ set_property (GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
151+ ${imgtool_sign} ${imgtool_args} --encrypt "${keyfile_enc} " ${output} .bin
152+ ${output} .signed.encrypted.bin)
140153 endif ()
141154 endif ()
142155
143156 # Set up .hex outputs.
144157 if (CONFIG_BUILD_OUTPUT_HEX)
145- list (APPEND unconfirmed_args --hex --shex ${output} .signed.hex)
146158 list (APPEND byproducts ${output} .signed.hex)
147159 zephyr_runner_file(hex ${output} .signed.hex)
148160 set (BYPRODUCT_KERNEL_SIGNED_HEX_NAME "${output} .signed.hex"
149161 CACHE FILEPATH "Signed kernel hex file" FORCE
150162 )
163+ set_property (GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
164+ ${imgtool_sign} ${imgtool_args} ${output} .hex ${output} .signed.hex)
151165
152166 if (CONFIG_MCUBOOT_GENERATE_CONFIRMED_IMAGE)
153- list (APPEND confirmed_args --hex --shex ${output} .signed.confirmed.hex)
154167 list (APPEND byproducts ${output} .signed.confirmed.hex)
155168 set (BYPRODUCT_KERNEL_SIGNED_CONFIRMED_HEX_NAME "${output} .signed.confirmed.hex"
156169 CACHE FILEPATH "Signed and confirmed kernel hex file" FORCE
157170 )
171+ set_property (GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
172+ ${imgtool_sign} ${imgtool_args} --pad --confirm ${output} .hex
173+ ${output} .signed.confirmed.hex)
158174 endif ()
159175
160176 if (NOT "${keyfile_enc} " STREQUAL "" )
161- list (APPEND encrypted_args --hex --shex ${output} .signed.encrypted.hex)
162177 list (APPEND byproducts ${output} .signed.encrypted.hex)
163178 set (BYPRODUCT_KERNEL_SIGNED_ENCRYPTED_HEX_NAME "${output} .signed.encrypted.hex"
164179 CACHE FILEPATH "Signed and encrypted kernel hex file" FORCE
165180 )
181+ set_property (GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
182+ ${imgtool_sign} ${imgtool_args} --encrypt "${keyfile_enc} " ${output} .hex
183+ ${output} .signed.encrypted.hex)
166184 endif ()
167185 endif ()
168186
169- # Add the west sign calls and their byproducts to the post-processing
170- # steps for zephyr.elf.
171- #
172- # CMake guarantees that multiple COMMANDs given to
173- # add_custom_command() are run in order, so adding the 'west sign'
174- # calls to the "extra_post_build_commands" property ensures they run
175- # after the commands which generate the unsigned versions.
176- set_property (GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
177- ${west_sign} ${unconfirmed_args} ${imgtool_args} )
178- if (confirmed_args)
179- set_property (GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
180- ${west_sign} ${confirmed_args} ${imgtool_args} --pad --confirm)
181- endif ()
182- if (encrypted_args)
183- set_property (GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
184- ${west_sign} ${encrypted_args} ${imgtool_args} --encrypt "${keyfile_enc} " )
185- endif ()
186187 set_property (GLOBAL APPEND PROPERTY extra_post_build_byproducts ${byproducts} )
187188endfunction ()
188189
0 commit comments