project_include.cmake 8.4 KB

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