project_include.cmake 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. add_custom_target(${target_name} DEPENDS ${dependencies}
  123. COMMAND ${CMAKE_COMMAND}
  124. -D IDF_PATH="${idf_path}"
  125. -D ESPTOOLPY="${ESPTOOLPY}"
  126. -D ESPTOOL_ARGS="write_flash;@flash_${flasher_filename}_args"
  127. -D WORKING_DIRECTORY="${build_dir}"
  128. -P run_esptool.cmake
  129. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  130. USES_TERMINAL
  131. )
  132. endfunction()
  133. add_custom_target(erase_flash
  134. COMMAND ${CMAKE_COMMAND}
  135. -D IDF_PATH="${idf_path}"
  136. -D ESPTOOLPY="${ESPTOOLPY}"
  137. -D ESPTOOL_ARGS="erase_flash"
  138. -P run_esptool.cmake
  139. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  140. USES_TERMINAL
  141. )
  142. add_custom_target(monitor
  143. COMMAND ${CMAKE_COMMAND}
  144. -D IDF_PATH="${idf_path}"
  145. -D IDF_MONITOR="${idf_path}/tools/idf_monitor.py"
  146. -D ELF_FILE="${elf}"
  147. -D WORKING_DIRECTORY="${build_dir}"
  148. -P run_idf_monitor.cmake
  149. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  150. USES_TERMINAL
  151. )
  152. esptool_py_custom_target(flash project "app;partition_table;bootloader")
  153. esptool_py_custom_target(app-flash app "app")
  154. if(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT)
  155. esptool_py_custom_target(encrypted-flash encrypted_project "app;partition_table;bootloader")
  156. esptool_py_custom_target(encrypted-app-flash encrypted_app "app")
  157. endif()
  158. # esptool_py_flash_project_args
  159. #
  160. # Add file to the flasher args list, to be flashed at a particular offset.
  161. #
  162. # When a template FLASH_FILE_TEMPLATE is given, the variables OFFSET and IMAGE
  163. # hold the value of arguments offset and image, respectively.
  164. function(esptool_py_flash_project_args entry offset image)
  165. set(options FLASH_IN_PROJECT) # flash the image when flashing the project
  166. set(single_value FLASH_FILE_TEMPLATE) # template file to use to be able to
  167. # flash the image individually using esptool
  168. cmake_parse_arguments(_ "${options}" "${single_value}" "" "${ARGN}")
  169. if(${entry} IN_LIST flash_project_entries)
  170. message(FATAL_ERROR "entry '${entry}' has already been added to flash project entries")
  171. endif()
  172. idf_component_set_property(esptool_py FLASH_PROJECT_ENTRIES "${entry}" APPEND)
  173. idf_build_get_property(build_dir BUILD_DIR)
  174. file(RELATIVE_PATH image ${build_dir} ${image})
  175. # Generate the standalone flash file to flash the image individually using esptool
  176. set(entry_flash_args ${build_dir}/flash_${entry}_args)
  177. if(NOT __FLASH_FILE_TEMPLATE)
  178. file(GENERATE OUTPUT ${entry_flash_args} CONTENT "${offset} ${image}")
  179. else()
  180. set(OFFSET ${offset})
  181. set(IMAGE ${image})
  182. get_filename_component(template_in "${__FLASH_FILE_TEMPLATE}" ABSOLUTE)
  183. get_filename_component(template_name "${template_in}" NAME)
  184. set(template_partial "${CMAKE_CURRENT_BINARY_DIR}/${template_name}.in2")
  185. configure_file("${template_in}" "${template_partial}")
  186. file(GENERATE OUTPUT ${entry_flash_args} INPUT "${template_partial}")
  187. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  188. APPEND PROPERTY
  189. ADDITIONAL_MAKE_CLEAN_FILES "${template_partial}")
  190. unset(OFFSET)
  191. unset(IMAGE)
  192. endif()
  193. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  194. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${entry_flash_args})
  195. # Generate standalone entries in the flasher args json file
  196. idf_component_set_property(esptool_py FLASH_PROJECT_ARGS_ENTRY_JSON
  197. "\"${entry}\" : { \"offset\" : \"${offset}\", \"file\" : \"${image}\" }" APPEND)
  198. # Generate entries in the flasher args json file
  199. if(__FLASH_IN_PROJECT)
  200. idf_component_set_property(esptool_py FLASH_PROJECT_ARGS
  201. "${offset} ${image}" APPEND)
  202. idf_component_set_property(esptool_py FLASH_PROJECT_ARGS_JSON
  203. "\"${offset}\" : \"${image}\"" APPEND)
  204. endif()
  205. endfunction()