project_include.cmake 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_ELF2IMAGE_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_ELF2IMAGE_FLASH_OPTIONS} ${ESPTOOLPY_EXTRA_FLASH_OPTIONS} ${ESPTOOLPY_COMPRESSED_OPT}")
  37. if(CONFIG_SECURE_BOOT_ENABLED AND
  38. NOT CONFIG_SECURE_BOOT_ALLOW_SHORT_APP_PARTITION AND
  39. NOT BOOTLOADER_BUILD)
  40. set(ESPTOOLPY_ELF2IMAGE_FLASH_OPTIONS
  41. ${ESPTOOLPY_ELF2IMAGE_FLASH_OPTIONS} --secure-pad)
  42. endif()
  43. if(NOT BOOTLOADER_BUILD)
  44. set(ESPTOOLPY_ELF2IMAGE_OPTIONS --elf-sha256-offset 0xb0)
  45. endif()
  46. if(CONFIG_ESPTOOLPY_FLASHSIZE_DETECT)
  47. # Set ESPFLASHSIZE to 'detect' *after* elf2image options are generated,
  48. # as elf2image can't have 'detect' as an option...
  49. set(ESPFLASHSIZE detect)
  50. endif()
  51. idf_build_get_property(build_dir BUILD_DIR)
  52. idf_build_get_property(elf_name EXECUTABLE_NAME GENERATOR_EXPRESSION)
  53. idf_build_get_property(elf EXECUTABLE GENERATOR_EXPRESSION)
  54. if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES AND NOT BOOTLOADER_BUILD)
  55. set(unsigned_project_binary "${elf_name}-unsigned.bin")
  56. else()
  57. set(unsigned_project_binary "${elf_name}.bin")
  58. endif()
  59. set(PROJECT_BIN "${elf_name}.bin")
  60. #
  61. # Add 'app.bin' target - generates with elf2image
  62. #
  63. add_custom_command(OUTPUT "${build_dir}/.app_hash"
  64. COMMAND ${ESPTOOLPY} elf2image ${ESPTOOLPY_ELF2IMAGE_FLASH_OPTIONS} ${ESPTOOLPY_ELF2IMAGE_OPTIONS}
  65. -o "${build_dir}/${unsigned_project_binary}" "${elf}"
  66. COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${unsigned_project_binary}" > "${build_dir}/.app_hash"
  67. DEPENDS ${elf}
  68. VERBATIM
  69. WORKING_DIRECTORY ${build_dir}
  70. )
  71. add_custom_target(gen_project_binary DEPENDS "${build_dir}/.app_hash")
  72. if(NOT BOOTLOADER_BUILD AND
  73. CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
  74. # for locally signed secure boot image, add a signing step to get from unsigned app to signed app
  75. add_custom_command(OUTPUT "${build_dir}/.signed_app_hash"
  76. COMMAND ${ESPSECUREPY} sign_data --keyfile ${secure_boot_signing_key}
  77. -o "${build_dir}/${PROJECT_BIN}" "${build_dir}/${unsigned_project_binary}"
  78. COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${PROJECT_BIN}" > "${build_dir}/.signed_app_hash"
  79. DEPENDS "${build_dir}/.app_hash"
  80. VERBATIM
  81. )
  82. add_custom_target(gen_signed_project_binary DEPENDS "${build_dir}/.signed_app_hash")
  83. add_dependencies(gen_project_binary gen_signed_project_binary)
  84. endif()
  85. if(NOT BOOTLOADER_BUILD)
  86. add_custom_target(app ALL DEPENDS gen_project_binary)
  87. else()
  88. add_custom_target(bootloader ALL DEPENDS gen_project_binary)
  89. endif()
  90. if(NOT BOOTLOADER_BUILD AND
  91. CONFIG_SECURE_BOOT_ENABLED AND
  92. NOT CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
  93. add_custom_command(TARGET app POST_BUILD
  94. COMMAND ${CMAKE_COMMAND} -E echo
  95. "App built but not signed. Sign app before flashing"
  96. COMMAND ${CMAKE_COMMAND} -E echo
  97. "\t${ESPSECUREPY} sign_data --keyfile KEYFILE ${build_dir}/${elf_bin}"
  98. VERBATIM)
  99. endif()
  100. #
  101. # Add 'flash' target - not all build systems can run this directly
  102. #
  103. function(esptool_py_custom_target target_name flasher_filename dependencies)
  104. idf_build_get_property(idf_path IDF_PATH)
  105. add_custom_target(${target_name} DEPENDS ${dependencies}
  106. COMMAND ${CMAKE_COMMAND}
  107. -D IDF_PATH="${idf_path}"
  108. -D ESPTOOLPY="${ESPTOOLPY}"
  109. -D ESPTOOL_ARGS="write_flash;@flash_${flasher_filename}_args"
  110. -D ESPTOOL_WORKING_DIR="${build_dir}"
  111. -P run_esptool.cmake
  112. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  113. USES_TERMINAL
  114. )
  115. endfunction()
  116. esptool_py_custom_target(flash project "app;partition_table;bootloader")
  117. esptool_py_custom_target(app-flash app "app")
  118. esptool_py_custom_target(bootloader-flash bootloader "bootloader")
  119. add_custom_target(flash_project_args_target)
  120. # esptool_py_flash_project_args
  121. #
  122. # Add file to the flasher args list, to be flashed at a particular offset
  123. function(esptool_py_flash_project_args entry offset image)
  124. set(options FLASH_IN_PROJECT) # flash the image when flashing the project
  125. set(single_value FLASH_FILE_TEMPLATE) # template file to use to be able to
  126. # flash the image individually using esptool
  127. cmake_parse_arguments(_ "${options}" "${single_value}" "" "${ARGN}")
  128. idf_build_get_property(build_dir BUILD_DIR)
  129. get_property(flash_project_entries TARGET flash_project_args_target PROPERTY FLASH_PROJECT_ENTRIES)
  130. if(${entry} IN_LIST flash_project_entries)
  131. message(FATAL_ERROR "entry '${entry}' has already been added to flash project entries")
  132. endif()
  133. list(APPEND flash_project_entries "${entry}")
  134. set_property(TARGET flash_project_args_target PROPERTY FLASH_PROJECT_ENTRIES "${flash_project_entries}")
  135. file(RELATIVE_PATH image ${CMAKE_BINARY_DIR} ${image})
  136. # Generate the standalone flash file to flash the image individually using esptool
  137. set(entry_flash_args ${build_dir}/flash_${entry}_args)
  138. if(NOT __FLASH_FILE_TEMPLATE)
  139. file(GENERATE OUTPUT ${entry_flash_args} CONTENT "${offset} ${image}")
  140. else()
  141. get_filename_component(template "${__FLASH_FILE_TEMPLATE}" ABSOLUTE)
  142. file(GENERATE OUTPUT ${entry_flash_args} INPUT ${template})
  143. endif()
  144. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  145. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${entry_flash_args})
  146. # Generate standalone entries in the flasher args json file
  147. get_property(flash_project_args_entry_json TARGET
  148. flash_project_args_target PROPERTY FLASH_PROJECT_ARGS_ENTRY_JSON)
  149. list(APPEND flash_project_args_entry_json
  150. "\"${entry}\" : { \"offset\" : \"${offset}\", \"file\" : \"${image}\" }")
  151. set_property(TARGET flash_project_args_target
  152. PROPERTY FLASH_PROJECT_ARGS_ENTRY_JSON "${flash_project_args_entry_json}")
  153. # Generate entries in the flasher args json file
  154. if(__FLASH_IN_PROJECT)
  155. get_property(flash_project_args TARGET flash_project_args_target PROPERTY FLASH_PROJECT_ARGS)
  156. list(APPEND flash_project_args "${offset} ${image}")
  157. set_property(TARGET flash_project_args_target PROPERTY FLASH_PROJECT_ARGS "${flash_project_args}")
  158. get_property(flash_project_args_json TARGET flash_project_args_target PROPERTY FLASH_PROJECT_ARGS_JSON)
  159. list(APPEND flash_project_args_json "\"${offset}\" : \"${image}\"")
  160. set_property(TARGET flash_project_args_target PROPERTY FLASH_PROJECT_ARGS_JSON "${flash_project_args_json}")
  161. endif()
  162. endfunction()