Skip to content

Commit 7c75795

Browse files
refactor(esptool_py): Move binary generation to project level and add utility functions
This commit refactors the esptool_py component to provide utility functions for binary file generation targets instead of creating the targets. Binary generation targets are now moved to the respective projects. The following changes were done in this commit: - Added __idf_build_binary() function to esptool_py to create the binary file generation target. - Added __idf_build_secure_binary() as the secure boot equivalent of the above function. - Top level project build now creates its own binary targets in idf_build_executable() in build.cmake. - Bootloader and esp_tee subprojects create their binary file generation targets in their respective CMakeLists.txt files. - All post-build targets such as the app_size_check target are now created by the respective projects and not esptool_py. - General clean-up of the esptool_py cmake files.
1 parent ef4d646 commit 7c75795

File tree

5 files changed

+322
-180
lines changed

5 files changed

+322
-180
lines changed

components/bootloader/subproject/CMakeLists.txt

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,36 @@ idf_build_set_property(COMPILE_DEFINITIONS "BOOTLOADER_BUILD=1" APPEND)
7575
idf_build_set_property(COMPILE_DEFINITIONS "NON_OS_BUILD=1" APPEND)
7676
idf_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+
7895
idf_component_get_property(main_args esptool_py FLASH_ARGS)
7996
idf_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
82102
string(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"
90110
set(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}")
140160
endif()
141161

162+
# If secure boot is enabled, generate the signed binary from the unsigned one.
142163
if(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-
"\nGenerate the Secure Boot V2 RSA-PSS 3072 Key."
151-
"\nTo 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+
"\nGenerate the Secure Boot V2 RSA-PSS 3072 Key."
173+
"\nTo 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+
)
175190
endif()
176191

177192
if(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)
257272
endif()
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()

components/esp_tee/subproject/CMakeLists.txt

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,51 @@ idf_build_set_property(COMPILE_DEFINITIONS "ESP_TEE_BUILD=1" APPEND)
5151
idf_build_set_property(COMPILE_DEFINITIONS "NON_OS_BUILD=1" APPEND)
5252
idf_build_set_property(COMPILE_OPTIONS "-fno-stack-protector" APPEND)
5353

54+
# Set up the TEE binary generation targets
55+
set(project_bin "esp_tee.bin")
56+
if(CONFIG_SECURE_BOOT_V2_ENABLED AND CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
57+
set(esp_tee_unsigned_bin "esp_tee-unsigned.bin")
58+
else()
59+
set(esp_tee_unsigned_bin "${project_bin}")
60+
endif()
61+
62+
# Set the final binary name as a project property.
63+
idf_build_set_property(PROJECT_BIN "${project_bin}")
64+
65+
# Generate the unsigned binary from the ELF file.
66+
if(CONFIG_APP_BUILD_GENERATE_BINARIES)
67+
set(target_name "gen_esp_tee_binary")
68+
__idf_build_binary("${esp_tee_unsigned_bin}" "${target_name}")
69+
endif()
70+
71+
idf_component_get_property(espsecure_py_cmd esptool_py ESPSECUREPY_CMD)
72+
73+
# If secure boot is enabled, generate the signed binary from the unsigned one.
5474
if(CONFIG_SECURE_BOOT_V2_ENABLED)
75+
set(target_name "gen_signed_esp_tee_binary")
76+
5577
if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
56-
get_filename_component(secure_boot_signing_key
57-
"${SECURE_BOOT_SIGNING_KEY}" ABSOLUTE BASE_DIR "${project_dir}")
58-
59-
if(NOT EXISTS "${secure_boot_signing_key}")
60-
message(FATAL_ERROR
61-
"Secure Boot Signing Key Not found."
62-
"\nGenerate the Secure Boot V2 RSA-PSS 3072 Key."
63-
"\nTo generate one, you can use this command:"
64-
"\n\t${espsecurepy} generate_signing_key --version 2 ${SECURE_BOOT_SIGNING_KEY}")
78+
# The SECURE_BOOT_SIGNING_KEY is passed in from the parent build and
79+
# is already an absolute path.
80+
if(NOT EXISTS "${SECURE_BOOT_SIGNING_KEY}")
81+
message(FATAL_ERROR
82+
"Secure Boot Signing Key Not found."
83+
"\nGenerate the Secure Boot V2 RSA-PSS 3072 Key."
84+
"\nTo generate one, you can use this command:"
85+
"\n\t${espsecure_py_cmd} generate_signing_key --version 2 your_key.pem"
86+
)
6587
endif()
6688

67-
set(esp_tee_unsigned_bin "esp_tee-unsigned.bin")
68-
add_custom_command(OUTPUT ".signed_bin_timestamp"
69-
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR}/${PROJECT_BIN}"
70-
"${CMAKE_BINARY_DIR}/${esp_tee_unsigned_bin}"
71-
COMMAND ${ESPSECUREPY} sign_data --version 2 --keyfile "${secure_boot_signing_key}"
72-
-o "${CMAKE_BINARY_DIR}/${PROJECT_BIN}" "${CMAKE_BINARY_DIR}/${esp_tee_unsigned_bin}"
73-
COMMAND ${CMAKE_COMMAND} -E echo "Generated signed binary image ${build_dir}/${PROJECT_BIN}"
74-
"from ${CMAKE_BINARY_DIR}/${esp_tee_unsigned_bin}"
75-
COMMAND ${CMAKE_COMMAND} -E md5sum "${CMAKE_BINARY_DIR}/${PROJECT_BIN}"
76-
> "${CMAKE_BINARY_DIR}/.signed_bin_timestamp"
77-
DEPENDS "${build_dir}/.bin_timestamp"
78-
VERBATIM
79-
COMMENT "Generated the signed TEE")
89+
set(comment "Generated the signed TEE")
90+
set(key_arg KEYFILE "${SECURE_BOOT_SIGNING_KEY}")
8091
else()
81-
add_custom_command(OUTPUT ".signed_bin_timestamp"
82-
VERBATIM
83-
COMMENT "TEE generated but not signed")
92+
# If we are not building signed binaries, we don't pass a key.
93+
set(comment "TEE generated but not signed")
94+
set(key_arg "")
8495
endif()
8596

86-
add_custom_target(gen_signed_esp_tee ALL DEPENDS "${build_dir}/.signed_bin_timestamp")
97+
__idf_build_secure_binary("${esp_tee_unsigned_bin}" "${project_bin}" "${target_name}"
98+
COMMENT "${comment}"
99+
${key_arg}
100+
)
87101
endif()

components/esptool_py/CMakeLists.txt

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ if(NOT BOOTLOADER_BUILD)
1717

1818

1919

20-
# If anti-rollback option is set then factory partition should not be in Partition Table.
21-
# In this case, should be used the partition table with two ota app without the factory.
22-
partition_table_get_partition_info(factory_offset "--partition-type app --partition-subtype factory" "offset")
23-
partition_table_get_partition_info(test_offset "--partition-type app --partition-subtype test" "offset")
24-
if(CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK AND (factory_offset OR test_offset))
25-
fail_at_build_time(check_table_contents "\
26-
ERROR: Anti-rollback option is enabled. Partition table should \
27-
consist of two ota app without factory or test partitions.")
28-
add_dependencies(app check_table_contents)
29-
endif()
3020

3121
# Generate flasher_args.json for tools that need it. The variables below are used
3222
# in configuring the template flasher_args.json.in.
@@ -54,31 +44,5 @@ consist of two ota app without factory or test partitions.")
5444
CONTENT "${flasher_args_content}")
5545
file_generate("${CMAKE_BINARY_DIR}/flasher_args.json"
5646
INPUT "${CMAKE_CURRENT_BINARY_DIR}/flasher_args.json.in")
57-
if(CONFIG_APP_BUILD_TYPE_APP_2NDBOOT)
58-
# Generate app_check_size_command target to check the app size against the partition table parameters
59-
partition_table_add_check_size_target(app_check_size
60-
DEPENDS gen_project_binary
61-
BINARY_PATH "${build_dir}/${PROJECT_BIN}"
62-
PARTITION_TYPE app)
63-
add_dependencies(app app_check_size)
64-
endif()
6547
endif()
6648
endif() # NOT BOOTLOADER_BUILD
67-
68-
if(BOOTLOADER_BUILD)
69-
# Generate bootloader post-build check of the bootloader size against the offset
70-
partition_table_add_check_bootloader_size_target(bootloader_check_size
71-
DEPENDS gen_project_binary
72-
BOOTLOADER_BINARY_PATH "${build_dir}/${PROJECT_BIN}"
73-
RESULT bootloader_check_size_command)
74-
add_dependencies(app bootloader_check_size) # note: in the subproject, so the target is 'app'...
75-
76-
if(CONFIG_SECURE_BOOT_V2_ENABLED AND CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
77-
# Check the size of the bootloader + signature block.
78-
partition_table_add_check_bootloader_size_target(bootloader_check_size_signed
79-
DEPENDS gen_signed_bootloader
80-
BOOTLOADER_BINARY_PATH "${build_dir}/${PROJECT_BIN}"
81-
RESULT bootloader_check_size_signed_command)
82-
add_dependencies(app bootloader_check_size_signed) # note: in the subproject, so the target is 'app'...
83-
endif()
84-
endif()

0 commit comments

Comments
 (0)