project_include.cmake 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 esp32)
  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. add_custom_command(OUTPUT "${build_dir}/.bin_timestamp"
  66. COMMAND ${ESPTOOLPY} elf2image ${ESPTOOLPY_FLASH_OPTIONS} ${ESPTOOLPY_ELF2IMAGE_OPTIONS}
  67. -o "${build_dir}/${unsigned_project_binary}" "${elf}"
  68. COMMAND ${CMAKE_COMMAND} -E echo "Generated ${build_dir}/${unsigned_project_binary}"
  69. COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${unsigned_project_binary}" > "${build_dir}/.bin_timestamp"
  70. DEPENDS ${elf}
  71. VERBATIM
  72. WORKING_DIRECTORY ${build_dir}
  73. COMMENT "Generating binary image from built executable"
  74. )
  75. add_custom_target(gen_project_binary DEPENDS "${build_dir}/.bin_timestamp")
  76. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  77. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
  78. "${build_dir}/${unsigned_project_binary}"
  79. )
  80. add_custom_target(app ALL DEPENDS gen_project_binary)
  81. if(NOT BOOTLOADER_BUILD AND CONFIG_SECURE_SIGNED_APPS)
  82. if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
  83. # for locally signed secure boot image, add a signing step to get from unsigned app to signed app
  84. add_custom_command(OUTPUT "${build_dir}/.signed_bin_timestamp"
  85. COMMAND ${ESPSECUREPY} sign_data --keyfile ${secure_boot_signing_key}
  86. -o "${build_dir}/${PROJECT_BIN}" "${build_dir}/${unsigned_project_binary}"
  87. COMMAND ${CMAKE_COMMAND} -E echo "Generated signed binary image ${build_dir}/${PROJECT_BIN}"
  88. "from ${build_dir}/${unsigned_project_binary}"
  89. COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${PROJECT_BIN}" > "${build_dir}/.signed_bin_timestamp"
  90. DEPENDS "${build_dir}/.bin_timestamp"
  91. VERBATIM
  92. COMMENT "Generating signed binary image"
  93. )
  94. add_custom_target(gen_signed_project_binary DEPENDS "${build_dir}/.signed_bin_timestamp")
  95. add_dependencies(gen_project_binary gen_signed_project_binary)
  96. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  97. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
  98. "${build_dir}/${PROJECT_BIN}"
  99. )
  100. else()
  101. string(REPLACE ";" " " espsecurepy "${ESPSECUREPY}")
  102. add_custom_command(TARGET app POST_BUILD
  103. COMMAND ${CMAKE_COMMAND} -E echo
  104. "App built but not signed. Sign app before flashing"
  105. COMMAND ${CMAKE_COMMAND} -E echo
  106. "\t${espsecurepy} sign_data --keyfile KEYFILE ${build_dir}/${PROJECT_BIN}"
  107. VERBATIM)
  108. endif()
  109. endif()
  110. #
  111. # Add 'flash' target - not all build systems can run this directly
  112. #
  113. function(esptool_py_custom_target target_name flasher_filename dependencies)
  114. idf_build_get_property(idf_path IDF_PATH)
  115. add_custom_target(${target_name} DEPENDS ${dependencies}
  116. COMMAND ${CMAKE_COMMAND}
  117. -D IDF_PATH="${idf_path}"
  118. -D ESPTOOLPY="${ESPTOOLPY}"
  119. -D ESPTOOL_ARGS="write_flash;@flash_${flasher_filename}_args"
  120. -D ESPTOOL_WORKING_DIR="${build_dir}"
  121. -P run_esptool.cmake
  122. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  123. USES_TERMINAL
  124. )
  125. endfunction()
  126. esptool_py_custom_target(flash project "app;partition_table;bootloader")
  127. esptool_py_custom_target(app-flash app "app")
  128. if(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT)
  129. esptool_py_custom_target(encrypted-flash encrypted_project "app;partition_table;bootloader")
  130. esptool_py_custom_target(encrypted-app-flash encrypted_app "app")
  131. endif()
  132. # esptool_py_flash_project_args
  133. #
  134. # Add file to the flasher args list, to be flashed at a particular offset.
  135. #
  136. # When a template FLASH_FILE_TEMPLATE is given, the variables OFFSET and IMAGE
  137. # hold the value of arguments offset and image, respectively.
  138. function(esptool_py_flash_project_args entry offset image)
  139. set(options FLASH_IN_PROJECT) # flash the image when flashing the project
  140. set(single_value FLASH_FILE_TEMPLATE) # template file to use to be able to
  141. # flash the image individually using esptool
  142. cmake_parse_arguments(_ "${options}" "${single_value}" "" "${ARGN}")
  143. if(${entry} IN_LIST flash_project_entries)
  144. message(FATAL_ERROR "entry '${entry}' has already been added to flash project entries")
  145. endif()
  146. idf_component_set_property(esptool_py FLASH_PROJECT_ENTRIES "${entry}" APPEND)
  147. idf_build_get_property(build_dir BUILD_DIR)
  148. file(RELATIVE_PATH image ${build_dir} ${image})
  149. # Generate the standalone flash file to flash the image individually using esptool
  150. set(entry_flash_args ${build_dir}/flash_${entry}_args)
  151. if(NOT __FLASH_FILE_TEMPLATE)
  152. file(GENERATE OUTPUT ${entry_flash_args} CONTENT "${offset} ${image}")
  153. else()
  154. set(OFFSET ${offset})
  155. set(IMAGE ${image})
  156. get_filename_component(template_in "${__FLASH_FILE_TEMPLATE}" ABSOLUTE)
  157. get_filename_component(template_name "${template_in}" NAME)
  158. set(template_partial "${CMAKE_CURRENT_BINARY_DIR}/${template_name}.in2")
  159. configure_file("${template_in}" "${template_partial}")
  160. file(GENERATE OUTPUT ${entry_flash_args} INPUT "${template_partial}")
  161. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  162. APPEND PROPERTY
  163. ADDITIONAL_MAKE_CLEAN_FILES "${template_partial}")
  164. unset(OFFSET)
  165. unset(IMAGE)
  166. endif()
  167. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  168. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${entry_flash_args})
  169. # Generate standalone entries in the flasher args json file
  170. idf_component_set_property(esptool_py FLASH_PROJECT_ARGS_ENTRY_JSON
  171. "\"${entry}\" : { \"offset\" : \"${offset}\", \"file\" : \"${image}\" }" APPEND)
  172. # Generate entries in the flasher args json file
  173. if(__FLASH_IN_PROJECT)
  174. idf_component_set_property(esptool_py FLASH_PROJECT_ARGS
  175. "${offset} ${image}" APPEND)
  176. idf_component_set_property(esptool_py FLASH_PROJECT_ARGS_JSON
  177. "\"${offset}\" : \"${image}\"" APPEND)
  178. endif()
  179. endfunction()