project_include.cmake 9.2 KB

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