2018-01-18 23:47:49 -05:00
|
|
|
# Set some global esptool.py variables
|
2018-01-23 01:08:28 -05:00
|
|
|
#
|
|
|
|
# Many of these are read when generating flash_app_args & flash_project_args
|
2019-11-28 08:09:45 -05:00
|
|
|
idf_build_get_property(target IDF_TARGET)
|
2019-05-09 22:53:08 -04:00
|
|
|
idf_build_get_property(python PYTHON)
|
2019-12-19 20:55:02 -05:00
|
|
|
|
2019-11-28 08:09:45 -05:00
|
|
|
set(ESPTOOLPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/esptool.py" --chip ${target})
|
2019-05-09 22:53:08 -04:00
|
|
|
set(ESPSECUREPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/espsecure.py")
|
|
|
|
set(ESPEFUSEPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/espefuse.py")
|
2018-01-18 23:47:49 -05:00
|
|
|
|
2020-01-22 11:40:10 -05:00
|
|
|
set(ESPFLASHMODE ${CONFIG_ESPTOOLPY_FLASHMODE})
|
|
|
|
set(ESPFLASHFREQ ${CONFIG_ESPTOOLPY_FLASHFREQ})
|
|
|
|
set(ESPFLASHSIZE ${CONFIG_ESPTOOLPY_FLASHSIZE})
|
|
|
|
|
|
|
|
set(ESPTOOLPY_FLASH_OPTIONS
|
|
|
|
--flash_mode ${ESPFLASHMODE}
|
|
|
|
--flash_freq ${ESPFLASHFREQ}
|
|
|
|
--flash_size ${ESPFLASHSIZE}
|
|
|
|
)
|
|
|
|
|
2019-03-15 08:02:16 -04:00
|
|
|
if(NOT BOOTLOADER_BUILD)
|
2019-12-19 20:55:02 -05:00
|
|
|
set(esptool_elf2image_args --elf-sha256-offset 0xb0)
|
2019-03-15 08:02:16 -04:00
|
|
|
endif()
|
2019-01-09 07:06:01 -05:00
|
|
|
|
2020-02-24 14:51:41 -05:00
|
|
|
if(NOT CONFIG_SECURE_BOOT_ALLOW_SHORT_APP_PARTITION AND
|
|
|
|
NOT BOOTLOADER_BUILD)
|
|
|
|
if(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME)
|
|
|
|
list(APPEND esptool_elf2image_args --secure-pad)
|
|
|
|
elseif(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME)
|
|
|
|
list(APPEND esptool_elf2image_args --secure-pad-v2)
|
|
|
|
endif()
|
2019-05-10 06:40:46 -04:00
|
|
|
endif()
|
|
|
|
|
2019-09-15 23:47:23 -04:00
|
|
|
if(CONFIG_ESP32_REV_MIN)
|
2019-12-19 20:55:02 -05:00
|
|
|
list(APPEND esptool_elf2image_args --min-rev ${CONFIG_ESP32_REV_MIN})
|
2019-09-15 23:47:23 -04:00
|
|
|
endif()
|
|
|
|
|
2018-01-23 01:08:28 -05:00
|
|
|
if(CONFIG_ESPTOOLPY_FLASHSIZE_DETECT)
|
2018-02-26 23:45:30 -05:00
|
|
|
# Set ESPFLASHSIZE to 'detect' *after* elf2image options are generated,
|
|
|
|
# as elf2image can't have 'detect' as an option...
|
|
|
|
set(ESPFLASHSIZE detect)
|
2018-01-23 01:08:28 -05:00
|
|
|
endif()
|
2018-01-18 23:47:49 -05:00
|
|
|
|
2020-02-24 14:51:41 -05:00
|
|
|
if(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME)
|
|
|
|
set(ESPFLASHSIZE keep)
|
|
|
|
endif()
|
|
|
|
|
2019-05-09 22:53:08 -04:00
|
|
|
idf_build_get_property(build_dir BUILD_DIR)
|
|
|
|
|
|
|
|
idf_build_get_property(elf_name EXECUTABLE_NAME GENERATOR_EXPRESSION)
|
|
|
|
idf_build_get_property(elf EXECUTABLE GENERATOR_EXPRESSION)
|
|
|
|
|
2018-10-19 15:02:55 -04:00
|
|
|
if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES AND NOT BOOTLOADER_BUILD)
|
2019-05-09 22:53:08 -04:00
|
|
|
set(unsigned_project_binary "${elf_name}-unsigned.bin")
|
2018-10-19 15:02:55 -04:00
|
|
|
else()
|
2019-05-09 22:53:08 -04:00
|
|
|
set(unsigned_project_binary "${elf_name}.bin")
|
2018-10-19 15:02:55 -04:00
|
|
|
endif()
|
|
|
|
|
2019-05-09 22:53:08 -04:00
|
|
|
set(PROJECT_BIN "${elf_name}.bin")
|
|
|
|
|
2018-01-18 23:47:49 -05:00
|
|
|
#
|
2018-01-23 01:08:28 -05:00
|
|
|
# Add 'app.bin' target - generates with elf2image
|
2018-01-18 23:47:49 -05:00
|
|
|
#
|
2019-07-22 10:04:03 -04:00
|
|
|
if(CONFIG_APP_BUILD_GENERATE_BINARIES)
|
|
|
|
add_custom_command(OUTPUT "${build_dir}/.bin_timestamp"
|
2019-12-19 20:55:02 -05:00
|
|
|
COMMAND ${ESPTOOLPY} elf2image ${ESPTOOLPY_FLASH_OPTIONS} ${esptool_elf2image_args}
|
2019-07-22 10:04:03 -04:00
|
|
|
-o "${build_dir}/${unsigned_project_binary}" "${elf}"
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo "Generated ${build_dir}/${unsigned_project_binary}"
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${unsigned_project_binary}" > "${build_dir}/.bin_timestamp"
|
|
|
|
DEPENDS ${elf}
|
|
|
|
VERBATIM
|
|
|
|
WORKING_DIRECTORY ${build_dir}
|
|
|
|
COMMENT "Generating binary image from built executable"
|
|
|
|
)
|
|
|
|
add_custom_target(gen_project_binary DEPENDS "${build_dir}/.bin_timestamp")
|
|
|
|
endif()
|
2018-10-19 15:02:55 -04:00
|
|
|
|
2019-05-10 06:40:46 -04:00
|
|
|
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
|
|
|
APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
|
|
|
|
"${build_dir}/${unsigned_project_binary}"
|
|
|
|
)
|
2018-10-19 15:02:55 -04:00
|
|
|
|
2019-07-22 10:04:03 -04:00
|
|
|
if(CONFIG_APP_BUILD_GENERATE_BINARIES)
|
|
|
|
add_custom_target(app ALL DEPENDS gen_project_binary)
|
|
|
|
endif()
|
2019-06-17 00:20:12 -04:00
|
|
|
|
2020-02-24 14:51:41 -05:00
|
|
|
if(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME)
|
|
|
|
set(secure_boot_version "1")
|
|
|
|
elseif(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME)
|
|
|
|
set(secure_boot_version "2")
|
|
|
|
endif()
|
|
|
|
|
2019-05-10 03:25:25 -04:00
|
|
|
if(NOT BOOTLOADER_BUILD AND CONFIG_SECURE_SIGNED_APPS)
|
|
|
|
if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
|
|
|
|
# for locally signed secure boot image, add a signing step to get from unsigned app to signed app
|
|
|
|
add_custom_command(OUTPUT "${build_dir}/.signed_bin_timestamp"
|
2020-02-24 14:51:41 -05:00
|
|
|
COMMAND ${ESPSECUREPY} sign_data --version ${secure_boot_version} --keyfile ${secure_boot_signing_key}
|
2019-05-10 03:25:25 -04:00
|
|
|
-o "${build_dir}/${PROJECT_BIN}" "${build_dir}/${unsigned_project_binary}"
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo "Generated signed binary image ${build_dir}/${PROJECT_BIN}"
|
|
|
|
"from ${build_dir}/${unsigned_project_binary}"
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${PROJECT_BIN}" > "${build_dir}/.signed_bin_timestamp"
|
|
|
|
DEPENDS "${build_dir}/.bin_timestamp"
|
|
|
|
VERBATIM
|
|
|
|
COMMENT "Generating signed binary image"
|
|
|
|
)
|
|
|
|
add_custom_target(gen_signed_project_binary DEPENDS "${build_dir}/.signed_bin_timestamp")
|
|
|
|
add_dependencies(gen_project_binary gen_signed_project_binary)
|
|
|
|
|
|
|
|
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
|
|
|
APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
|
|
|
|
"${build_dir}/${PROJECT_BIN}"
|
|
|
|
)
|
|
|
|
else()
|
|
|
|
string(REPLACE ";" " " espsecurepy "${ESPSECUREPY}")
|
|
|
|
add_custom_command(TARGET app POST_BUILD
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo
|
|
|
|
"App built but not signed. Sign app before flashing"
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo
|
2020-02-24 14:51:41 -05:00
|
|
|
"\t${espsecurepy} sign_data --keyfile KEYFILE --version ${secure_boot_version} \
|
|
|
|
${build_dir}/${PROJECT_BIN}"
|
2019-05-10 03:25:25 -04:00
|
|
|
VERBATIM)
|
|
|
|
endif()
|
2018-10-19 15:02:55 -04:00
|
|
|
endif()
|
2018-01-18 23:47:49 -05:00
|
|
|
|
2019-08-20 04:09:24 -04:00
|
|
|
add_custom_target(erase_flash
|
|
|
|
COMMAND ${CMAKE_COMMAND}
|
|
|
|
-D IDF_PATH="${idf_path}"
|
|
|
|
-D ESPTOOLPY="${ESPTOOLPY}"
|
|
|
|
-D ESPTOOL_ARGS="erase_flash"
|
|
|
|
-P run_esptool.cmake
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
|
|
|
|
USES_TERMINAL
|
|
|
|
)
|
|
|
|
|
|
|
|
add_custom_target(monitor
|
|
|
|
COMMAND ${CMAKE_COMMAND}
|
|
|
|
-D IDF_PATH="${idf_path}"
|
|
|
|
-D IDF_MONITOR="${idf_path}/tools/idf_monitor.py"
|
|
|
|
-D ELF_FILE="${elf}"
|
|
|
|
-D WORKING_DIRECTORY="${build_dir}"
|
|
|
|
-P run_idf_monitor.cmake
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
|
|
|
|
USES_TERMINAL
|
|
|
|
)
|
|
|
|
|
2019-12-19 20:55:02 -05:00
|
|
|
set(esptool_flash_main_args "--before=${CONFIG_ESPTOOLPY_BEFORE}")
|
2019-01-21 22:45:45 -05:00
|
|
|
|
2020-02-24 14:51:41 -05:00
|
|
|
if(CONFIG_SECURE_BOOT OR CONFIG_SECURE_FLASH_ENC_ENABLED)
|
2019-12-19 20:55:02 -05:00
|
|
|
# If security enabled then override post flash option
|
|
|
|
list(APPEND esptool_flash_main_args "--after=no_reset")
|
|
|
|
else()
|
|
|
|
list(APPEND esptool_flash_main_args "--after=${CONFIG_ESPTOOLPY_AFTER}")
|
2019-01-21 09:14:56 -05:00
|
|
|
endif()
|
2019-01-21 22:45:45 -05:00
|
|
|
|
2019-12-19 20:55:02 -05:00
|
|
|
|
|
|
|
idf_component_set_property(esptool_py FLASH_ARGS "${esptool_flash_main_args}")
|
2020-01-22 11:40:10 -05:00
|
|
|
idf_component_set_property(esptool_py FLASH_SUB_ARGS "${ESPTOOLPY_FLASH_OPTIONS}")
|
2019-01-21 22:45:45 -05:00
|
|
|
|
2019-12-19 20:55:02 -05:00
|
|
|
function(esptool_py_flash_target_image target_name image_name offset image)
|
|
|
|
idf_build_get_property(build_dir BUILD_DIR)
|
|
|
|
file(RELATIVE_PATH image ${build_dir} ${image})
|
|
|
|
|
|
|
|
set_property(TARGET ${target_name} APPEND PROPERTY FLASH_FILE
|
|
|
|
"\"${offset}\" : \"${image}\"")
|
|
|
|
set_property(TARGET ${target_name} APPEND PROPERTY FLASH_ENTRY
|
|
|
|
"\"${image_name}\" : { \"offset\" : \"${offset}\", \"file\" : \"${image}\" }")
|
|
|
|
set_property(TARGET ${target_name} APPEND PROPERTY IMAGES "${offset} ${image}")
|
|
|
|
|
|
|
|
if(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT)
|
|
|
|
set_property(TARGET encrypted-${target_name} APPEND PROPERTY FLASH_FILE
|
|
|
|
"\"${offset}\" : \"${image}\"")
|
|
|
|
set_property(TARGET encrypted-${target_name} APPEND PROPERTY FLASH_ENTRY
|
|
|
|
"\"${image_name}\" : { \"offset\" : \"${offset}\", \"file\" : \"${image}\" }")
|
|
|
|
set_property(TARGET encrypted-${target_name} APPEND PROPERTY IMAGES "${offset} ${image}")
|
2019-01-21 22:45:45 -05:00
|
|
|
endif()
|
2019-12-19 20:55:02 -05:00
|
|
|
endfunction()
|
2019-01-21 22:45:45 -05:00
|
|
|
|
2019-12-19 20:55:02 -05:00
|
|
|
function(esptool_py_flash_target target_name main_args sub_args)
|
|
|
|
set(single_value OFFSET IMAGE) # template file to use to be able to
|
|
|
|
# flash the image individually using esptool
|
|
|
|
cmake_parse_arguments(_ "" "${single_value}" "" "${ARGN}")
|
2019-01-21 22:45:45 -05:00
|
|
|
|
2019-12-19 20:55:02 -05:00
|
|
|
idf_build_get_property(idf_path IDF_PATH)
|
2019-06-21 00:18:29 -04:00
|
|
|
idf_build_get_property(build_dir BUILD_DIR)
|
2019-12-19 20:55:02 -05:00
|
|
|
idf_component_get_property(esptool_py_dir esptool_py COMPONENT_DIR)
|
2019-01-21 22:45:45 -05:00
|
|
|
|
2019-12-19 20:55:02 -05:00
|
|
|
add_custom_target(${target_name}
|
|
|
|
COMMAND ${CMAKE_COMMAND}
|
|
|
|
-D IDF_PATH="${idf_path}"
|
|
|
|
-D ESPTOOLPY="${ESPTOOLPY}"
|
|
|
|
-D ESPTOOL_ARGS="${main_args};write_flash;@${target_name}_args"
|
|
|
|
-D WORKING_DIRECTORY="${build_dir}"
|
|
|
|
-P ${esptool_py_dir}/run_esptool.cmake
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
|
|
|
|
USES_TERMINAL
|
|
|
|
)
|
|
|
|
|
|
|
|
set_target_properties(${target_name} PROPERTIES SUB_ARGS "${sub_args}")
|
|
|
|
|
|
|
|
set(flash_args_content "$<JOIN:$<TARGET_PROPERTY:${target_name},SUB_ARGS>, >\n\
|
|
|
|
$<JOIN:$<TARGET_PROPERTY:${target_name},IMAGES>,\n>")
|
|
|
|
|
|
|
|
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${target_name}_args.in"
|
|
|
|
CONTENT "${flash_args_content}")
|
|
|
|
file(GENERATE OUTPUT "${build_dir}/${target_name}_args"
|
|
|
|
INPUT "${CMAKE_CURRENT_BINARY_DIR}/${target_name}_args.in")
|
|
|
|
|
|
|
|
if(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT)
|
|
|
|
add_custom_target(encrypted-${target_name}
|
|
|
|
COMMAND ${CMAKE_COMMAND}
|
|
|
|
-D IDF_PATH="${idf_path}"
|
|
|
|
-D ESPTOOLPY="${ESPTOOLPY}"
|
|
|
|
-D ESPTOOL_ARGS="${main_args};write_flash;@encrypted_${target_name}_args"
|
|
|
|
-D WORKING_DIRECTORY="${build_dir}"
|
|
|
|
-P ${esptool_py_dir}/run_esptool.cmake
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
|
|
|
|
USES_TERMINAL
|
|
|
|
)
|
|
|
|
|
|
|
|
set_target_properties(encrypted-${target_name} PROPERTIES SUB_ARGS "${sub_args};--encrypt")
|
|
|
|
|
|
|
|
set(flash_args_content "$<JOIN:$<TARGET_PROPERTY:encrypted-${target_name},SUB_ARGS>, >\n\
|
|
|
|
$<JOIN:$<TARGET_PROPERTY:encrypted-${target_name},IMAGES>,\n>")
|
|
|
|
|
|
|
|
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/encrypted_${target_name}_args.in"
|
|
|
|
CONTENT "${flash_args_content}")
|
|
|
|
file(GENERATE OUTPUT "${build_dir}/encrypted_${target_name}_args"
|
|
|
|
INPUT "${CMAKE_CURRENT_BINARY_DIR}/encrypted_${target_name}_args.in")
|
2020-02-14 01:13:47 -05:00
|
|
|
else()
|
|
|
|
fail_target(encrypted-${target_name} "Error: The target encrypted-${target_name} requires"
|
|
|
|
"CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT to be enabled.")
|
|
|
|
|
2019-01-21 22:45:45 -05:00
|
|
|
endif()
|
2020-02-14 01:13:47 -05:00
|
|
|
|
2019-12-19 20:55:02 -05:00
|
|
|
endfunction()
|
|
|
|
|
|
|
|
|
|
|
|
function(esptool_py_custom_target target_name flasher_filename dependencies)
|
|
|
|
idf_component_get_property(main_args esptool_py FLASH_ARGS)
|
|
|
|
idf_component_get_property(sub_args esptool_py FLASH_SUB_ARGS)
|
2019-01-21 22:45:45 -05:00
|
|
|
|
2019-12-19 20:55:02 -05:00
|
|
|
idf_build_get_property(build_dir BUILD_DIR)
|
|
|
|
|
|
|
|
esptool_py_flash_target(${target_name} "${main_args}" "${sub_args}")
|
2019-01-21 22:45:45 -05:00
|
|
|
|
2019-12-19 20:55:02 -05:00
|
|
|
# Copy the file to flash_xxx_args for compatibility for select target
|
|
|
|
file_generate("${build_dir}/flash_${flasher_filename}_args"
|
|
|
|
INPUT "${build_dir}/${target_name}_args")
|
2019-01-21 22:45:45 -05:00
|
|
|
|
2019-12-19 20:55:02 -05:00
|
|
|
add_dependencies(${target_name} ${dependencies})
|
2019-01-21 22:45:45 -05:00
|
|
|
|
2019-12-19 20:55:02 -05:00
|
|
|
if(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT)
|
|
|
|
file_generate("${build_dir}/flash_encrypted_${flasher_filename}_args"
|
|
|
|
INPUT "${build_dir}/encrypted_${target_name}_args")
|
|
|
|
|
|
|
|
add_dependencies(encrypted-${target_name} ${dependencies})
|
2019-01-21 22:45:45 -05:00
|
|
|
endif()
|
2019-05-09 08:10:35 -04:00
|
|
|
endfunction()
|
2019-12-19 20:55:02 -05:00
|
|
|
|
2020-01-06 01:17:00 -05:00
|
|
|
if(NOT BOOTLOADER_BUILD)
|
|
|
|
set(flash_deps "partition_table")
|
|
|
|
|
|
|
|
if(CONFIG_APP_BUILD_GENERATE_BINARIES)
|
|
|
|
list(APPEND flash_deps "app")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(CONFIG_APP_BUILD_BOOTLOADER)
|
|
|
|
list(APPEND flash_deps "bootloader")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
esptool_py_custom_target(flash project "${flash_deps}")
|
2019-12-19 20:55:02 -05:00
|
|
|
endif()
|