@@ -75,16 +75,36 @@ idf_build_set_property(COMPILE_DEFINITIONS "BOOTLOADER_BUILD=1" APPEND)
7575idf_build_set_property(COMPILE_DEFINITIONS "NON_OS_BUILD=1" APPEND )
7676idf_build_set_property(COMPILE_OPTIONS "-fno-stack-protector" APPEND )
7777
78+ # Set up the bootloader binary generation targets
79+ set (PROJECT_BIN "bootloader.bin" )
80+ if (CONFIG_SECURE_BOOT_V2_ENABLED AND CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
81+ set (bootloader_unsigned_bin "bootloader-unsigned.bin" )
82+ else ()
83+ set (bootloader_unsigned_bin "${PROJECT_BIN} " )
84+ endif ()
85+
86+ # Set the final binary name as a project property
87+ idf_build_set_property(PROJECT_BIN "${PROJECT_BIN} " )
88+
89+ # Generate the unsigned binary from the ELF file.
90+ if (CONFIG_APP_BUILD_GENERATE_BINARIES)
91+ set (target_name "gen_bootloader_binary" )
92+ __idf_build_binary("${bootloader_unsigned_bin} " "${target_name} " )
93+ endif ()
94+
7895idf_component_get_property(main_args esptool_py FLASH_ARGS)
7996idf_component_get_property(sub_args esptool_py FLASH_SUB_ARGS)
97+ idf_component_get_property(esptool_py_cmd esptool_py ESPTOOLPY_CMD)
98+ idf_component_get_property(espsecure_py_cmd esptool_py ESPSECUREPY_CMD)
99+ idf_component_get_property(espefuse_py_cmd esptool_py ESPEFUSEPY_CMD)
80100
81101# String for printing flash command
82102string (REPLACE ";" " " esptoolpy_write_flash
83- "${ESPTOOLPY } --port=(PORT) --baud=(BAUD) ${main_args} "
103+ "${esptool_py_cmd } --port=(PORT) --baud=(BAUD) ${main_args} "
84104 "write_flash ${sub_args} " )
85105
86- string (REPLACE ";" " " espsecurepy "${ESPSECUREPY } " )
87- string (REPLACE ";" " " espefusepy "${ESPEFUSEPY } " )
106+ string (REPLACE ";" " " espsecurepy "${espsecure_py_cmd } " )
107+ string (REPLACE ";" " " espefusepy "${espefuse_py_cmd } " )
88108
89109# Suppress warning: "Manually-specified variables were not used by the project: SECURE_BOOT_SIGNING_KEY"
90110set (ignore_signing_key "${SECURE_BOOT_SIGNING_KEY} " )
@@ -105,7 +125,7 @@ if(CONFIG_SECURE_BOOTLOADER_REFLASHABLE)
105125 ABSOLUTE BASE_DIR "${CMAKE_BINARY_DIR} " )
106126
107127 add_custom_command (OUTPUT "${secure_bootloader_key} "
108- COMMAND ${ESPSECUREPY } digest_private_key
128+ COMMAND ${espsecure_py_cmd } digest_private_key
109129 --keylen "${key_digest_len} "
110130 --keyfile "${SECURE_BOOT_SIGNING_KEY} "
111131 "${secure_bootloader_key} "
@@ -130,7 +150,7 @@ if(CONFIG_SECURE_BOOTLOADER_REFLASHABLE)
130150
131151 add_custom_command (OUTPUT "${bootloader_digest_bin} "
132152 COMMAND ${CMAKE_COMMAND} -E echo "DIGEST ${bootloader_digest_bin} "
133- COMMAND ${ESPSECUREPY } digest_secure_bootloader --keyfile "${secure_bootloader_key} "
153+ COMMAND ${espsecure_py_cmd } digest_secure_bootloader --keyfile "${secure_bootloader_key} "
134154 -o "${bootloader_digest_bin} " "${CMAKE_BINARY_DIR} /bootloader.bin"
135155 MAIN_DEPENDENCY "${CMAKE_BINARY_DIR} /.bin_timestamp"
136156 DEPENDS gen_secure_bootloader_key gen_project_binary
@@ -139,39 +159,34 @@ if(CONFIG_SECURE_BOOTLOADER_REFLASHABLE)
139159 add_custom_target (gen_bootloader_digest_bin ALL DEPENDS "${bootloader_digest_bin} " )
140160endif ()
141161
162+ # If secure boot is enabled, generate the signed binary from the unsigned one.
142163if (CONFIG_SECURE_BOOT_V2_ENABLED)
143- if (CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
144- get_filename_component (secure_boot_signing_key
145- "${SECURE_BOOT_SIGNING_KEY} " ABSOLUTE BASE_DIR "${project_dir} " )
164+ set (target_name "gen_signed_bootloader" )
146165
147- if (NOT EXISTS "${secure_boot_signing_key} " )
148- message (FATAL_ERROR
149- "Secure Boot Signing Key Not found."
150- "\n Generate the Secure Boot V2 RSA-PSS 3072 Key."
151- "\n To generate one, you can use this command:"
152- "\n\t ${espsecurepy} generate_signing_key --version 2 ${SECURE_BOOT_SIGNING_KEY} " )
166+ if (CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
167+ # The SECURE_BOOT_SIGNING_KEY is passed in from the parent build and
168+ # is already an absolute path.
169+ if (NOT EXISTS "${SECURE_BOOT_SIGNING_KEY} " )
170+ message (FATAL_ERROR
171+ "Secure Boot Signing Key Not found."
172+ "\n Generate the Secure Boot V2 RSA-PSS 3072 Key."
173+ "\n To generate one, you can use this command:"
174+ "\n\t ${espsecurepy} generate_signing_key --version 2 your_key.pem"
175+ )
153176 endif ()
154177
155- set (bootloader_unsigned_bin "bootloader-unsigned.bin" )
156- add_custom_command (OUTPUT ".signed_bin_timestamp"
157- COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR} /${PROJECT_BIN} "
158- "${CMAKE_BINARY_DIR} /${bootloader_unsigned_bin} "
159- COMMAND ${ESPSECUREPY} sign_data --version 2 --keyfile "${secure_boot_signing_key} "
160- -o "${CMAKE_BINARY_DIR} /${PROJECT_BIN} " "${CMAKE_BINARY_DIR} /${bootloader_unsigned_bin} "
161- COMMAND ${CMAKE_COMMAND} -E echo "Generated signed binary image ${build_dir} /${PROJECT_BIN} "
162- "from ${CMAKE_BINARY_DIR} /${bootloader_unsigned_bin} "
163- COMMAND ${CMAKE_COMMAND} -E md5sum "${CMAKE_BINARY_DIR} /${PROJECT_BIN} "
164- > "${CMAKE_BINARY_DIR} /.signed_bin_timestamp"
165- DEPENDS "${build_dir} /.bin_timestamp"
166- VERBATIM
167- COMMENT "Generated the signed Bootloader" )
178+ set (comment "Generated the signed Bootloader" )
179+ set (key_arg KEYFILE "${SECURE_BOOT_SIGNING_KEY} " )
168180 else ()
169- add_custom_command ( OUTPUT ".signed_bin_timestamp"
170- VERBATIM
171- COMMENT "Bootloader generated but not signed " )
181+ # If we are not building signed binaries, we don't pass a key.
182+ set ( comment "Bootloader generated but not signed" )
183+ set (key_arg " " )
172184 endif ()
173185
174- add_custom_target (gen_signed_bootloader ALL DEPENDS "${build_dir} /.signed_bin_timestamp" )
186+ __idf_build_secure_binary("${bootloader_unsigned_bin} " "${PROJECT_BIN} " "${target_name} "
187+ COMMENT "${comment} "
188+ ${key_arg}
189+ )
175190endif ()
176191
177192if (CONFIG_SECURE_BOOTLOADER_ONE_TIME_FLASH)
@@ -255,3 +270,19 @@ elseif(CONFIG_SECURE_BOOT_V2_ENABLED AND NOT CONFIG_SECURE_BOOT_FLASH_BOOTLOADER
255270 DEPENDS gen_signed_bootloader
256271 VERBATIM )
257272endif ()
273+
274+ # Generate bootloader post-build check of the bootloader size against the offset
275+ partition_table_add_check_bootloader_size_target(bootloader_check_size
276+ DEPENDS gen_project_binary
277+ BOOTLOADER_BINARY_PATH "${CMAKE_BINARY_DIR} /${PROJECT_BIN} "
278+ RESULT bootloader_check_size_command)
279+ add_dependencies (app bootloader_check_size)
280+
281+ if (CONFIG_SECURE_BOOT_V2_ENABLED AND CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
282+ # Check the size of the bootloader + signature block.
283+ partition_table_add_check_bootloader_size_target(bootloader_check_size_signed
284+ DEPENDS gen_signed_bootloader
285+ BOOTLOADER_BINARY_PATH "${CMAKE_BINARY_DIR} /${PROJECT_BIN} "
286+ RESULT bootloader_check_size_signed_command)
287+ add_dependencies (app bootloader_check_size_signed)
288+ endif ()
0 commit comments