@@ -79,16 +79,36 @@ idf_build_set_property(COMPILE_DEFINITIONS "BOOTLOADER_BUILD=1" APPEND)
7979idf_build_set_property(COMPILE_DEFINITIONS "NON_OS_BUILD=1" APPEND )
8080idf_build_set_property(COMPILE_OPTIONS "-fno-stack-protector" APPEND )
8181
82+ # Set up the bootloader binary generation targets
83+ set (PROJECT_BIN "bootloader.bin" )
84+ if (CONFIG_SECURE_BOOT_V2_ENABLED AND CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
85+ set (bootloader_unsigned_bin "bootloader-unsigned.bin" )
86+ else ()
87+ set (bootloader_unsigned_bin "${PROJECT_BIN} " )
88+ endif ()
89+
90+ # Set the final binary name as a project property
91+ idf_build_set_property(PROJECT_BIN "${PROJECT_BIN} " )
92+
93+ # Generate the unsigned binary from the ELF file.
94+ if (CONFIG_APP_BUILD_GENERATE_BINARIES)
95+ set (target_name "gen_bootloader_binary" )
96+ __idf_build_binary("${bootloader_unsigned_bin} " "${target_name} " )
97+ endif ()
98+
8299idf_component_get_property(main_args esptool_py FLASH_ARGS)
83100idf_component_get_property(sub_args esptool_py FLASH_SUB_ARGS)
101+ idf_component_get_property(esptool_py_cmd esptool_py ESPTOOLPY_CMD)
102+ idf_component_get_property(espsecure_py_cmd esptool_py ESPSECUREPY_CMD)
103+ idf_component_get_property(espefuse_py_cmd esptool_py ESPEFUSEPY_CMD)
84104
85105# String for printing flash command
86106string (REPLACE ";" " " esptoolpy_write_flash
87- "${ESPTOOLPY } --port=(PORT) --baud=(BAUD) ${main_args} "
107+ "${esptool_py_cmd } --port=(PORT) --baud=(BAUD) ${main_args} "
88108 "write_flash ${sub_args} " )
89109
90- string (REPLACE ";" " " espsecurepy "${ESPSECUREPY } " )
91- string (REPLACE ";" " " espefusepy "${ESPEFUSEPY } " )
110+ string (REPLACE ";" " " espsecurepy "${espsecure_py_cmd } " )
111+ string (REPLACE ";" " " espefusepy "${espefuse_py_cmd } " )
92112
93113# Suppress warning: "Manually-specified variables were not used by the project: SECURE_BOOT_SIGNING_KEY"
94114set (ignore_signing_key "${SECURE_BOOT_SIGNING_KEY} " )
@@ -109,7 +129,7 @@ if(CONFIG_SECURE_BOOTLOADER_REFLASHABLE)
109129 ABSOLUTE BASE_DIR "${CMAKE_BINARY_DIR} " )
110130
111131 add_custom_command (OUTPUT "${secure_bootloader_key} "
112- COMMAND ${ESPSECUREPY } digest_private_key
132+ COMMAND ${espsecure_py_cmd } digest_private_key
113133 --keylen "${key_digest_len} "
114134 --keyfile "${SECURE_BOOT_SIGNING_KEY} "
115135 "${secure_bootloader_key} "
@@ -134,7 +154,7 @@ if(CONFIG_SECURE_BOOTLOADER_REFLASHABLE)
134154
135155 add_custom_command (OUTPUT "${bootloader_digest_bin} "
136156 COMMAND ${CMAKE_COMMAND} -E echo "DIGEST ${bootloader_digest_bin} "
137- COMMAND ${ESPSECUREPY } digest_secure_bootloader --keyfile "${secure_bootloader_key} "
157+ COMMAND ${espsecure_py_cmd } digest_secure_bootloader --keyfile "${secure_bootloader_key} "
138158 -o "${bootloader_digest_bin} " "${CMAKE_BINARY_DIR} /bootloader.bin"
139159 MAIN_DEPENDENCY "${CMAKE_BINARY_DIR} /.bin_timestamp"
140160 DEPENDS gen_secure_bootloader_key gen_project_binary
@@ -143,39 +163,34 @@ if(CONFIG_SECURE_BOOTLOADER_REFLASHABLE)
143163 add_custom_target (gen_bootloader_digest_bin ALL DEPENDS "${bootloader_digest_bin} " )
144164endif ()
145165
166+ # If secure boot is enabled, generate the signed binary from the unsigned one.
146167if (CONFIG_SECURE_BOOT_V2_ENABLED)
147- if (CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
148- get_filename_component (secure_boot_signing_key
149- "${SECURE_BOOT_SIGNING_KEY} " ABSOLUTE BASE_DIR "${project_dir} " )
168+ set (target_name "gen_signed_bootloader" )
150169
151- if (NOT EXISTS "${secure_boot_signing_key} " )
152- message (FATAL_ERROR
153- "Secure Boot Signing Key Not found."
154- "\n Generate the Secure Boot V2 RSA-PSS 3072 Key."
155- "\n To generate one, you can use this command:"
156- "\n\t ${espsecurepy} generate_signing_key --version 2 ${SECURE_BOOT_SIGNING_KEY} " )
170+ if (CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
171+ # The SECURE_BOOT_SIGNING_KEY is passed in from the parent build and
172+ # is already an absolute path.
173+ if (NOT EXISTS "${SECURE_BOOT_SIGNING_KEY} " )
174+ message (FATAL_ERROR
175+ "Secure Boot Signing Key Not found."
176+ "\n Generate the Secure Boot V2 RSA-PSS 3072 Key."
177+ "\n To generate one, you can use this command:"
178+ "\n\t ${espsecurepy} generate_signing_key --version 2 your_key.pem"
179+ )
157180 endif ()
158181
159- set (bootloader_unsigned_bin "bootloader-unsigned.bin" )
160- add_custom_command (OUTPUT ".signed_bin_timestamp"
161- COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR} /${PROJECT_BIN} "
162- "${CMAKE_BINARY_DIR} /${bootloader_unsigned_bin} "
163- COMMAND ${ESPSECUREPY} sign_data --version 2 --keyfile "${secure_boot_signing_key} "
164- -o "${CMAKE_BINARY_DIR} /${PROJECT_BIN} " "${CMAKE_BINARY_DIR} /${bootloader_unsigned_bin} "
165- COMMAND ${CMAKE_COMMAND} -E echo "Generated signed binary image ${build_dir} /${PROJECT_BIN} "
166- "from ${CMAKE_BINARY_DIR} /${bootloader_unsigned_bin} "
167- COMMAND ${CMAKE_COMMAND} -E md5sum "${CMAKE_BINARY_DIR} /${PROJECT_BIN} "
168- > "${CMAKE_BINARY_DIR} /.signed_bin_timestamp"
169- DEPENDS "${build_dir} /.bin_timestamp"
170- VERBATIM
171- COMMENT "Generated the signed Bootloader" )
182+ set (comment "Generated the signed Bootloader" )
183+ set (key_arg KEYFILE "${SECURE_BOOT_SIGNING_KEY} " )
172184 else ()
173- add_custom_command ( OUTPUT ".signed_bin_timestamp"
174- VERBATIM
175- COMMENT "Bootloader generated but not signed " )
185+ # If we are not building signed binaries, we don't pass a key.
186+ set ( comment "Bootloader generated but not signed" )
187+ set (key_arg " " )
176188 endif ()
177189
178- add_custom_target (gen_signed_bootloader ALL DEPENDS "${build_dir} /.signed_bin_timestamp" )
190+ __idf_build_secure_binary("${bootloader_unsigned_bin} " "${PROJECT_BIN} " "${target_name} "
191+ COMMENT "${comment} "
192+ ${key_arg}
193+ )
179194endif ()
180195
181196if (CONFIG_SECURE_BOOTLOADER_ONE_TIME_FLASH)
@@ -259,3 +274,19 @@ elseif(CONFIG_SECURE_BOOT_V2_ENABLED AND NOT CONFIG_SECURE_BOOT_FLASH_BOOTLOADER
259274 DEPENDS gen_signed_bootloader
260275 VERBATIM )
261276endif ()
277+
278+ # Generate bootloader post-build check of the bootloader size against the offset
279+ partition_table_add_check_bootloader_size_target(bootloader_check_size
280+ DEPENDS gen_project_binary
281+ BOOTLOADER_BINARY_PATH "${CMAKE_BINARY_DIR} /${PROJECT_BIN} "
282+ RESULT bootloader_check_size_command)
283+ add_dependencies (app bootloader_check_size)
284+
285+ if (CONFIG_SECURE_BOOT_V2_ENABLED AND CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
286+ # Check the size of the bootloader + signature block.
287+ partition_table_add_check_bootloader_size_target(bootloader_check_size_signed
288+ DEPENDS gen_signed_bootloader
289+ BOOTLOADER_BINARY_PATH "${CMAKE_BINARY_DIR} /${PROJECT_BIN} "
290+ RESULT bootloader_check_size_signed_command)
291+ add_dependencies (app bootloader_check_size_signed)
292+ endif ()
0 commit comments