project_include.cmake 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # Set some global esptool.py variables
  2. #
  3. # Many of these are read when generating flash_app_args & flash_project_args
  4. idf_build_get_property(target IDF_TARGET)
  5. idf_build_get_property(python PYTHON)
  6. set(ESPTOOLPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/esptool.py" --chip ${target})
  7. set(ESPSECUREPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/espsecure.py")
  8. set(ESPEFUSEPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/espefuse.py")
  9. set(ESPFLASHMODE ${CONFIG_ESPTOOLPY_FLASHMODE})
  10. set(ESPFLASHFREQ ${CONFIG_ESPTOOLPY_FLASHFREQ})
  11. set(ESPFLASHSIZE ${CONFIG_ESPTOOLPY_FLASHSIZE})
  12. set(ESPTOOLPY_BEFORE "${CONFIG_ESPTOOLPY_BEFORE}")
  13. set(ESPTOOLPY_AFTER "${CONFIG_ESPTOOLPY_AFTER}")
  14. set(ESPTOOLPY_CHIP "${target}")
  15. set(ESPTOOLPY_WITH_STUB TRUE)
  16. if(CONFIG_SECURE_BOOT_ENABLED OR CONFIG_SECURE_FLASH_ENC_ENABLED)
  17. # If security enabled then override post flash option
  18. set(ESPTOOLPY_AFTER "no_reset")
  19. endif()
  20. set(ESPTOOLPY_SERIAL "${ESPTOOLPY}"
  21. --port "${ESPPORT}"
  22. --baud ${ESPBAUD}
  23. --before "${ESPTOOLPY_BEFORE}"
  24. --after "${ESPTOOLPY_AFTER}"
  25. )
  26. if(CONFIG_ESPTOOLPY_COMPRESSED)
  27. set(ESPTOOLPY_COMPRESSED_OPT -z)
  28. else()
  29. set(ESPTOOLPY_COMPRESSED_OPT -u)
  30. endif()
  31. set(ESPTOOLPY_FLASH_OPTIONS
  32. --flash_mode ${ESPFLASHMODE}
  33. --flash_freq ${ESPFLASHFREQ}
  34. --flash_size ${ESPFLASHSIZE}
  35. )
  36. # String for printing flash command
  37. string(REPLACE ";" " " ESPTOOLPY_WRITE_FLASH_STR
  38. "${ESPTOOLPY} --port (PORT) --baud (BAUD) --before ${ESPTOOLPY_BEFORE} --after ${ESPTOOLPY_AFTER} "
  39. "write_flash ${ESPTOOLPY_FLASH_OPTIONS} ${ESPTOOLPY_EXTRA_FLASH_OPTIONS} ${ESPTOOLPY_COMPRESSED_OPT}")
  40. if(NOT BOOTLOADER_BUILD)
  41. set(ESPTOOLPY_ELF2IMAGE_OPTIONS --elf-sha256-offset 0xb0)
  42. endif()
  43. if(CONFIG_SECURE_BOOT_ENABLED AND
  44. NOT CONFIG_SECURE_BOOT_ALLOW_SHORT_APP_PARTITION
  45. AND NOT BOOTLOADER_BUILD)
  46. set(ESPTOOLPY_ELF2IMAGE_OPTIONS ${ESPTOOLPY_ELF2IMAGE_OPTIONS} --secure-pad)
  47. endif()
  48. if(CONFIG_ESP32_REV_MIN)
  49. set(ESPTOOLPY_ELF2IMAGE_OPTIONS ${ESPTOOLPY_ELF2IMAGE_OPTIONS} --min-rev ${CONFIG_ESP32_REV_MIN})
  50. endif()
  51. if(CONFIG_ESPTOOLPY_FLASHSIZE_DETECT)
  52. # Set ESPFLASHSIZE to 'detect' *after* elf2image options are generated,
  53. # as elf2image can't have 'detect' as an option...
  54. set(ESPFLASHSIZE detect)
  55. endif()
  56. idf_build_get_property(build_dir BUILD_DIR)
  57. idf_build_get_property(elf_name EXECUTABLE_NAME GENERATOR_EXPRESSION)
  58. idf_build_get_property(elf EXECUTABLE GENERATOR_EXPRESSION)
  59. if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES AND NOT BOOTLOADER_BUILD)
  60. set(unsigned_project_binary "${elf_name}-unsigned.bin")
  61. else()
  62. set(unsigned_project_binary "${elf_name}.bin")
  63. endif()
  64. set(PROJECT_BIN "${elf_name}.bin")
  65. #
  66. # Add 'app.bin' target - generates with elf2image
  67. #
  68. if(CONFIG_APP_BUILD_GENERATE_BINARIES)
  69. add_custom_command(OUTPUT "${build_dir}/.bin_timestamp"
  70. COMMAND ${ESPTOOLPY} elf2image ${ESPTOOLPY_FLASH_OPTIONS} ${ESPTOOLPY_ELF2IMAGE_OPTIONS}
  71. -o "${build_dir}/${unsigned_project_binary}" "${elf}"
  72. COMMAND ${CMAKE_COMMAND} -E echo "Generated ${build_dir}/${unsigned_project_binary}"
  73. COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${unsigned_project_binary}" > "${build_dir}/.bin_timestamp"
  74. DEPENDS ${elf}
  75. VERBATIM
  76. WORKING_DIRECTORY ${build_dir}
  77. COMMENT "Generating binary image from built executable"
  78. )
  79. add_custom_target(gen_project_binary DEPENDS "${build_dir}/.bin_timestamp")
  80. endif()
  81. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  82. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
  83. "${build_dir}/${unsigned_project_binary}"
  84. )
  85. if(CONFIG_APP_BUILD_GENERATE_BINARIES)
  86. add_custom_target(app ALL DEPENDS gen_project_binary)
  87. endif()
  88. if(NOT BOOTLOADER_BUILD AND CONFIG_SECURE_SIGNED_APPS)
  89. if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
  90. # for locally signed secure boot image, add a signing step to get from unsigned app to signed app
  91. add_custom_command(OUTPUT "${build_dir}/.signed_bin_timestamp"
  92. COMMAND ${ESPSECUREPY} sign_data --keyfile ${secure_boot_signing_key}
  93. -o "${build_dir}/${PROJECT_BIN}" "${build_dir}/${unsigned_project_binary}"
  94. COMMAND ${CMAKE_COMMAND} -E echo "Generated signed binary image ${build_dir}/${PROJECT_BIN}"
  95. "from ${build_dir}/${unsigned_project_binary}"
  96. COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${PROJECT_BIN}" > "${build_dir}/.signed_bin_timestamp"
  97. DEPENDS "${build_dir}/.bin_timestamp"
  98. VERBATIM
  99. COMMENT "Generating signed binary image"
  100. )
  101. add_custom_target(gen_signed_project_binary DEPENDS "${build_dir}/.signed_bin_timestamp")
  102. add_dependencies(gen_project_binary gen_signed_project_binary)
  103. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  104. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
  105. "${build_dir}/${PROJECT_BIN}"
  106. )
  107. else()
  108. string(REPLACE ";" " " espsecurepy "${ESPSECUREPY}")
  109. add_custom_command(TARGET app POST_BUILD
  110. COMMAND ${CMAKE_COMMAND} -E echo
  111. "App built but not signed. Sign app before flashing"
  112. COMMAND ${CMAKE_COMMAND} -E echo
  113. "\t${espsecurepy} sign_data --keyfile KEYFILE ${build_dir}/${PROJECT_BIN}"
  114. VERBATIM)
  115. endif()
  116. endif()
  117. #
  118. # Add 'flash' target - not all build systems can run this directly
  119. #
  120. function(esptool_py_custom_target target_name flasher_filename dependencies)
  121. idf_build_get_property(idf_path IDF_PATH)
  122. idf_component_get_property(esptool_py_dir esptool_py COMPONENT_DIR)
  123. add_custom_target(${target_name} DEPENDS ${dependencies}
  124. COMMAND ${CMAKE_COMMAND}
  125. -D IDF_PATH="${idf_path}"
  126. -D ESPTOOLPY="${ESPTOOLPY}"
  127. -D ESPTOOL_ARGS="write_flash;@flash_${flasher_filename}_args"
  128. -D WORKING_DIRECTORY="${build_dir}"
  129. -P ${esptool_py_dir}/run_esptool.cmake
  130. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  131. USES_TERMINAL
  132. )
  133. endfunction()
  134. add_custom_target(erase_flash
  135. COMMAND ${CMAKE_COMMAND}
  136. -D IDF_PATH="${idf_path}"
  137. -D ESPTOOLPY="${ESPTOOLPY}"
  138. -D ESPTOOL_ARGS="erase_flash"
  139. -P run_esptool.cmake
  140. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  141. USES_TERMINAL
  142. )
  143. add_custom_target(monitor
  144. COMMAND ${CMAKE_COMMAND}
  145. -D IDF_PATH="${idf_path}"
  146. -D IDF_MONITOR="${idf_path}/tools/idf_monitor.py"
  147. -D ELF_FILE="${elf}"
  148. -D WORKING_DIRECTORY="${build_dir}"
  149. -P run_idf_monitor.cmake
  150. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  151. USES_TERMINAL
  152. )
  153. esptool_py_custom_target(flash project "app;partition_table;bootloader")
  154. esptool_py_custom_target(app-flash app "app")
  155. if(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT)
  156. esptool_py_custom_target(encrypted-flash encrypted_project "app;partition_table;bootloader")
  157. esptool_py_custom_target(encrypted-app-flash encrypted_app "app")
  158. endif()
  159. # esptool_py_flash_project_args
  160. #
  161. # Add file to the flasher args list, to be flashed at a particular offset.
  162. #
  163. # When a template FLASH_FILE_TEMPLATE is given, the variables OFFSET and IMAGE
  164. # hold the value of arguments offset and image, respectively.
  165. function(esptool_py_flash_project_args entry offset image)
  166. set(options FLASH_IN_PROJECT) # flash the image when flashing the project
  167. set(single_value FLASH_FILE_TEMPLATE) # template file to use to be able to
  168. # flash the image individually using esptool
  169. cmake_parse_arguments(_ "${options}" "${single_value}" "" "${ARGN}")
  170. if(${entry} IN_LIST flash_project_entries)
  171. message(FATAL_ERROR "entry '${entry}' has already been added to flash project entries")
  172. endif()
  173. idf_component_set_property(esptool_py FLASH_PROJECT_ENTRIES "${entry}" APPEND)
  174. idf_build_get_property(build_dir BUILD_DIR)
  175. file(RELATIVE_PATH image ${build_dir} ${image})
  176. # Generate the standalone flash file to flash the image individually using esptool
  177. set(entry_flash_args ${build_dir}/flash_${entry}_args)
  178. if(NOT __FLASH_FILE_TEMPLATE)
  179. file(GENERATE OUTPUT ${entry_flash_args} CONTENT "${offset} ${image}")
  180. else()
  181. set(OFFSET ${offset})
  182. set(IMAGE ${image})
  183. get_filename_component(template_in "${__FLASH_FILE_TEMPLATE}" ABSOLUTE)
  184. get_filename_component(template_name "${template_in}" NAME)
  185. set(template_partial "${CMAKE_CURRENT_BINARY_DIR}/${template_name}.in2")
  186. configure_file("${template_in}" "${template_partial}")
  187. file(GENERATE OUTPUT ${entry_flash_args} INPUT "${template_partial}")
  188. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  189. APPEND PROPERTY
  190. ADDITIONAL_MAKE_CLEAN_FILES "${template_partial}")
  191. unset(OFFSET)
  192. unset(IMAGE)
  193. endif()
  194. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  195. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${entry_flash_args})
  196. # Generate standalone entries in the flasher args json file
  197. idf_component_set_property(esptool_py FLASH_PROJECT_ARGS_ENTRY_JSON
  198. "\"${entry}\" : { \"offset\" : \"${offset}\", \"file\" : \"${image}\" }" APPEND)
  199. # Generate entries in the flasher args json file
  200. if(__FLASH_IN_PROJECT)
  201. idf_component_set_property(esptool_py FLASH_PROJECT_ARGS
  202. "${offset} ${image}" APPEND)
  203. idf_component_set_property(esptool_py FLASH_PROJECT_ARGS_JSON
  204. "\"${offset}\" : \"${image}\"" APPEND)
  205. endif()
  206. endfunction()