project_include.cmake 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. idf_build_get_property(idf_path IDF_PATH)
  7. set(chip_model ${target})
  8. # TODO: remove this if block when esp32h4 beta1 is no longer supported and we have h4 target in esptool
  9. if(target STREQUAL "esp32h4")
  10. if(CONFIG_IDF_TARGET_ESP32H4_BETA_VERSION_1)
  11. set(chip_model esp32h2beta1)
  12. elseif(CONFIG_IDF_TARGET_ESP32H4_BETA_VERSION_2)
  13. set(chip_model esp32h2beta2)
  14. endif()
  15. endif()
  16. set(ESPTOOLPY ${python} "$ENV{ESPTOOL_WRAPPER}" "${CMAKE_CURRENT_LIST_DIR}/esptool/esptool.py" --chip ${chip_model})
  17. set(ESPSECUREPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/espsecure.py")
  18. set(ESPEFUSEPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/espefuse.py")
  19. set(ESPMONITOR ${python} -m esp_idf_monitor)
  20. set(ESPTOOLPY_CHIP "${chip_model}")
  21. if(NOT CONFIG_APP_BUILD_TYPE_RAM AND CONFIG_APP_BUILD_GENERATE_BINARIES)
  22. if(CONFIG_SPI_FLASH_HPM_ENABLE)
  23. # When set flash frequency to 120M, must keep 1st bootloader work under ``DOUT`` mode
  24. # because on some flash chips, 120M will modify the status register,
  25. # which will make ROM won't work.
  26. # This change intends to be for esptool only and the bootloader should keep use
  27. # ``DOUT`` mode.
  28. set(ESPFLASHMODE "dout")
  29. message("Note: HPM is enabled for the flash, force the ROM bootloader into DOUT mode for stable boot on")
  30. else()
  31. set(ESPFLASHMODE ${CONFIG_ESPTOOLPY_FLASHMODE})
  32. endif()
  33. set(ESPFLASHFREQ ${CONFIG_ESPTOOLPY_FLASHFREQ})
  34. set(ESPFLASHSIZE ${CONFIG_ESPTOOLPY_FLASHSIZE})
  35. set(esptool_elf2image_args
  36. --flash_mode ${ESPFLASHMODE}
  37. --flash_freq ${ESPFLASHFREQ}
  38. --flash_size ${ESPFLASHSIZE}
  39. )
  40. if(BOOTLOADER_BUILD AND CONFIG_SECURE_BOOT_V2_ENABLED)
  41. # The bootloader binary needs to be 4KB aligned in order to append a secure boot V2 signature block.
  42. # If CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES is NOT set, the bootloader
  43. # image generated is not 4KB aligned for external HSM to sign it readily.
  44. # Following esptool option --pad-to-size 4KB generates a 4K aligned bootloader image.
  45. # In case of signing during build, espsecure.py "sign_data" operation handles the 4K alignment of the image.
  46. if(NOT CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
  47. list(APPEND esptool_elf2image_args --pad-to-size 4KB)
  48. endif()
  49. endif()
  50. set(MMU_PAGE_SIZE ${CONFIG_MMU_PAGE_MODE})
  51. if(NOT BOOTLOADER_BUILD)
  52. list(APPEND esptool_elf2image_args --elf-sha256-offset 0xb0)
  53. # For chips that support configurable MMU page size feature
  54. # If page size is configured to values other than the default "64KB" in menuconfig,
  55. # then we need to pass the actual size to flash-mmu-page-size arg
  56. if(NOT MMU_PAGE_SIZE STREQUAL "64KB")
  57. list(APPEND esptool_elf2image_args --flash-mmu-page-size ${MMU_PAGE_SIZE})
  58. endif()
  59. endif()
  60. if(NOT CONFIG_SECURE_BOOT_ALLOW_SHORT_APP_PARTITION AND
  61. NOT BOOTLOADER_BUILD)
  62. if(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME)
  63. list(APPEND esptool_elf2image_args --secure-pad)
  64. elseif(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME OR CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME)
  65. list(APPEND esptool_elf2image_args --secure-pad-v2)
  66. endif()
  67. endif()
  68. if(CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE)
  69. # Set ESPFLASHSIZE to 'detect' *after* esptool_elf2image_args are generated,
  70. # as elf2image can't have 'detect' as an option...
  71. set(ESPFLASHSIZE detect)
  72. # Flash size detection updates the image header which would invalidate the appended
  73. # SHA256 digest. Therefore, a digest is not appended in that case.
  74. # This argument requires esptool>=4.1.
  75. list(APPEND esptool_elf2image_args --dont-append-digest)
  76. endif()
  77. if(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME)
  78. set(ESPFLASHSIZE keep)
  79. endif()
  80. endif()
  81. # We still set "--min-rev" to keep the app compatible with older booloaders where this field is controlled.
  82. if(CONFIG_IDF_TARGET_ESP32)
  83. # for this chip min_rev is major revision
  84. math(EXPR min_rev "${CONFIG_ESP_REV_MIN_FULL} / 100")
  85. endif()
  86. if(CONFIG_IDF_TARGET_ESP32C3)
  87. # for this chip min_rev is minor revision
  88. math(EXPR min_rev "${CONFIG_ESP_REV_MIN_FULL} % 100")
  89. endif()
  90. if(min_rev)
  91. list(APPEND esptool_elf2image_args --min-rev ${min_rev})
  92. endif()
  93. list(APPEND esptool_elf2image_args --min-rev-full ${CONFIG_ESP_REV_MIN_FULL})
  94. list(APPEND esptool_elf2image_args --max-rev-full ${CONFIG_ESP_REV_MAX_FULL})
  95. set(monitor_rev_args "--revision;${CONFIG_ESP_REV_MIN_FULL}")
  96. if(CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE)
  97. # Set ESPFLASHSIZE to 'detect' *after* esptool_elf2image_args are generated,
  98. # as elf2image can't have 'detect' as an option...
  99. set(ESPFLASHSIZE detect)
  100. # Flash size detection updates the image header which would invalidate the appended
  101. # SHA256 digest. Therefore, a digest is not appended in that case.
  102. # This argument requires esptool>=4.1.
  103. list(APPEND esptool_elf2image_args --dont-append-digest)
  104. endif()
  105. if(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME)
  106. set(ESPFLASHSIZE keep)
  107. endif()
  108. idf_build_get_property(build_dir BUILD_DIR)
  109. idf_build_get_property(elf_name EXECUTABLE_NAME GENERATOR_EXPRESSION)
  110. idf_build_get_property(elf EXECUTABLE GENERATOR_EXPRESSION)
  111. idf_build_get_property(elf_dir EXECUTABLE_DIR GENERATOR_EXPRESSION)
  112. if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES AND NOT BOOTLOADER_BUILD)
  113. set(unsigned_project_binary "${elf_name}-unsigned.bin")
  114. else()
  115. set(unsigned_project_binary "${elf_name}.bin")
  116. endif()
  117. set(PROJECT_BIN "${elf_name}.bin")
  118. #
  119. # Add 'app.bin' target - generates with elf2image
  120. #
  121. if(CONFIG_APP_BUILD_GENERATE_BINARIES)
  122. add_custom_command(OUTPUT "${build_dir}/.bin_timestamp"
  123. COMMAND ${ESPTOOLPY} elf2image ${esptool_elf2image_args}
  124. -o "${build_dir}/${unsigned_project_binary}" "${elf_dir}/${elf}"
  125. COMMAND ${CMAKE_COMMAND} -E echo "Generated ${build_dir}/${unsigned_project_binary}"
  126. COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${unsigned_project_binary}" > "${build_dir}/.bin_timestamp"
  127. DEPENDS ${elf}
  128. VERBATIM
  129. WORKING_DIRECTORY ${build_dir}
  130. COMMENT "Generating binary image from built executable"
  131. )
  132. add_custom_target(gen_project_binary DEPENDS "${build_dir}/.bin_timestamp")
  133. endif()
  134. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  135. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
  136. "${build_dir}/${unsigned_project_binary}"
  137. )
  138. if(CONFIG_APP_BUILD_GENERATE_BINARIES)
  139. add_custom_target(app ALL DEPENDS gen_project_binary)
  140. endif()
  141. if(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME)
  142. set(secure_boot_version "1")
  143. elseif(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME OR CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME)
  144. set(secure_boot_version "2")
  145. endif()
  146. if(NOT BOOTLOADER_BUILD AND CONFIG_SECURE_SIGNED_APPS)
  147. if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
  148. # for locally signed secure boot image, add a signing step to get from unsigned app to signed app
  149. get_filename_component(secure_boot_signing_key "${CONFIG_SECURE_BOOT_SIGNING_KEY}"
  150. ABSOLUTE BASE_DIR "${project_dir}")
  151. add_custom_command(OUTPUT "${build_dir}/.signed_bin_timestamp"
  152. COMMAND ${ESPSECUREPY} sign_data --version ${secure_boot_version} --keyfile ${secure_boot_signing_key}
  153. -o "${build_dir}/${PROJECT_BIN}" "${build_dir}/${unsigned_project_binary}"
  154. COMMAND ${CMAKE_COMMAND} -E echo "Generated signed binary image ${build_dir}/${PROJECT_BIN}"
  155. "from ${build_dir}/${unsigned_project_binary}"
  156. COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${PROJECT_BIN}" > "${build_dir}/.signed_bin_timestamp"
  157. DEPENDS "${build_dir}/.bin_timestamp"
  158. VERBATIM
  159. COMMENT "Generating signed binary image"
  160. )
  161. add_custom_target(gen_signed_project_binary DEPENDS "${build_dir}/.signed_bin_timestamp")
  162. add_dependencies(gen_project_binary gen_signed_project_binary)
  163. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  164. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
  165. "${build_dir}/${PROJECT_BIN}"
  166. )
  167. else()
  168. string(REPLACE ";" " " espsecurepy "${ESPSECUREPY}")
  169. add_custom_command(TARGET app POST_BUILD
  170. COMMAND ${CMAKE_COMMAND} -E echo
  171. "App built but not signed. Sign app before flashing"
  172. COMMAND ${CMAKE_COMMAND} -E echo
  173. "\t${espsecurepy} sign_data --keyfile KEYFILE --version ${secure_boot_version} \
  174. ${build_dir}/${PROJECT_BIN}"
  175. VERBATIM)
  176. endif()
  177. endif()
  178. add_custom_target(erase_flash
  179. COMMAND ${CMAKE_COMMAND}
  180. -D "IDF_PATH=${idf_path}"
  181. -D "SERIAL_TOOL=${ESPTOOLPY}"
  182. -D "SERIAL_TOOL_ARGS=erase_flash"
  183. -P run_serial_tool.cmake
  184. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  185. USES_TERMINAL
  186. VERBATIM
  187. )
  188. add_custom_target(monitor
  189. COMMAND ${CMAKE_COMMAND}
  190. -D "IDF_PATH=${idf_path}"
  191. -D "SERIAL_TOOL=${ESPMONITOR}"
  192. -D "SERIAL_TOOL_ARGS=--target;${target};${monitor_rev_args};${elf_dir}/${elf}"
  193. -D "WORKING_DIRECTORY=${build_dir}"
  194. -P run_serial_tool.cmake
  195. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  196. USES_TERMINAL
  197. VERBATIM
  198. )
  199. set(esptool_flash_main_args "--before=${CONFIG_ESPTOOLPY_BEFORE}")
  200. if(CONFIG_SECURE_BOOT OR CONFIG_SECURE_FLASH_ENC_ENABLED)
  201. # If security enabled then override post flash option
  202. list(APPEND esptool_flash_main_args "--after=no_reset")
  203. else()
  204. list(APPEND esptool_flash_main_args "--after=${CONFIG_ESPTOOLPY_AFTER}")
  205. endif()
  206. if(CONFIG_ESPTOOLPY_NO_STUB)
  207. list(APPEND esptool_flash_main_args "--no-stub")
  208. endif()
  209. idf_component_set_property(esptool_py FLASH_ARGS "${esptool_flash_main_args}")
  210. idf_component_set_property(esptool_py FLASH_SUB_ARGS "--flash_mode ${ESPFLASHMODE} --flash_freq ${ESPFLASHFREQ} \
  211. --flash_size ${ESPFLASHSIZE}")
  212. function(esptool_py_partition_needs_encryption retencrypted partition_name)
  213. # Check if encryption is enabled
  214. if(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT)
  215. # Encryption is enabled, get partition type, subtype and encrypted flag
  216. # to determine whether it needs encryption or not.
  217. partition_table_get_partition_info(type "--partition-name ${partition_name}" "type")
  218. partition_table_get_partition_info(subtype "--partition-name ${partition_name}" "subtype")
  219. partition_table_get_partition_info(encrypted "--partition-name ${partition_name}" "encrypted")
  220. # As defined in gen_esp32part.py file:
  221. # Types:
  222. # - APP 0x00
  223. # - DATA 0x01
  224. # Subtypes:
  225. # - ota 0x00
  226. # - nvs 0x02
  227. # If the partition is an app, an OTA or an NVS partition, then it should
  228. # be encrypted
  229. if(
  230. (${type} EQUAL 0) OR
  231. (${type} EQUAL 1 AND ${subtype} EQUAL 0) OR
  232. (${type} EQUAL 1 AND ${subtype} EQUAL 2)
  233. )
  234. set(encrypted TRUE)
  235. endif()
  236. # Return 'encrypted' value to the caller
  237. set(${retencrypted} ${encrypted} PARENT_SCOPE)
  238. else()
  239. # Encryption not enabled, return false
  240. set(${retencrypted} FALSE PARENT_SCOPE)
  241. endif()
  242. endfunction()
  243. function(esptool_py_flash_to_partition target_name partition_name binary_path)
  244. # Retrieve the offset for the partition to flash the image on
  245. partition_table_get_partition_info(offset "--partition-name ${partition_name}" "offset")
  246. if(NOT offset)
  247. message(FATAL_ERROR "Could not find offset of partition ${partition_name}")
  248. endif()
  249. # Check whether the partition needs encryption or not
  250. esptool_py_partition_needs_encryption(encrypted ${partition_name})
  251. # If the image should not be encrypted, we pass the option
  252. # ALWAYS_PLAINTEXT to the function esptool_py_flash_target_image
  253. if(NOT ${encrypted})
  254. set(option ALWAYS_PLAINTEXT)
  255. endif()
  256. # The image name is also the partition name
  257. esptool_py_flash_target_image(${target_name} ${partition_name} ${offset}
  258. ${binary_path} ${option})
  259. endfunction()
  260. # This function takes a fifth optional named parameter: "ALWAYS_PLAINTEXT". As
  261. # its name states, it marks whether the image should be flashed as plain text or
  262. # not. If build macro CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT is set and
  263. # this parameter is provided, then the image will be flahsed as plain text
  264. # (not encrypted) on the target. This parameter will be ignored if build macro
  265. # CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT is not set.
  266. function(esptool_py_flash_target_image target_name image_name offset image)
  267. set(options ALWAYS_PLAINTEXT)
  268. idf_build_get_property(build_dir BUILD_DIR)
  269. file(RELATIVE_PATH image ${build_dir} ${image})
  270. cmake_parse_arguments(arg "${options}" "" "" "${ARGN}")
  271. # Check if the image has to be plain text or not, depending on the macro
  272. # CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT and the parameter
  273. # ALWAYS_PLAINTEXT
  274. set(encrypted false)
  275. if(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT AND
  276. NOT arg_ALWAYS_PLAINTEXT)
  277. set(encrypted true)
  278. endif()
  279. # In the following snippet of code, some properties are defined for our
  280. # current target. These properties will be used to generate the actual
  281. # target_name_args files using the target_name_args.in files.
  282. # Please see function esptool_py_flash_target above for more information
  283. # about these properties and how they are used.
  284. # Add the image file, with its offset, to the list of files to
  285. # flash to the target. No matter whether flash encryption is
  286. # enabled or not, plain binaries (non-encrypted) need to be
  287. # generated
  288. set_property(TARGET ${target_name} APPEND PROPERTY FLASH_FILE
  289. "\"${offset}\" : \"${image}\"")
  290. set_property(TARGET ${target_name} APPEND PROPERTY FLASH_ENTRY
  291. "\"${image_name}\" : { \"offset\" : \"${offset}\", \"file\" : \"${image}\",\
  292. \"encrypted\" : \"${encrypted}\" }")
  293. set_property(TARGET ${target_name} APPEND PROPERTY IMAGES "${offset} ${image}")
  294. if(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT)
  295. # When flash encryption mode is enabled, if the current binary needs to
  296. # be encrypted, do the same as previously but prefixing target names
  297. # with "encrypted-".
  298. if(encrypted)
  299. set_property(TARGET encrypted-${target_name} APPEND PROPERTY FLASH_FILE
  300. "\"${offset}\" : \"${image}\"")
  301. set_property(TARGET encrypted-${target_name} APPEND PROPERTY FLASH_ENTRY
  302. "\"${image_name}\" : { \"offset\" : \"${offset}\", \"file\" : \"${image}\" , \
  303. \"encrypted\" : \"${encrypted}\" }")
  304. set_property(TARGET encrypted-${target_name} APPEND PROPERTY ENCRYPTED_IMAGES "${offset} ${image}")
  305. else()
  306. # The target doesn't need to be encrypted, thus, add the current
  307. # file to the NON_ENCRYPTED_IMAGES property
  308. set_property(TARGET encrypted-${target_name} APPEND PROPERTY NON_ENCRYPTED_IMAGES "${offset} ${image}")
  309. endif()
  310. endif()
  311. endfunction()
  312. function(esptool_py_flash_target target_name main_args sub_args)
  313. set(single_value OFFSET IMAGE) # template file to use to be able to
  314. # flash the image individually using esptool
  315. set(options ALWAYS_PLAINTEXT)
  316. cmake_parse_arguments(_ "${options}" "${single_value}" "" "${ARGN}")
  317. idf_build_get_property(idf_path IDF_PATH)
  318. idf_build_get_property(build_dir BUILD_DIR)
  319. idf_component_get_property(esptool_py_dir esptool_py COMPONENT_DIR)
  320. add_custom_target(${target_name}
  321. COMMAND ${CMAKE_COMMAND}
  322. -D "IDF_PATH=${idf_path}"
  323. -D "SERIAL_TOOL=${ESPTOOLPY}"
  324. -D "SERIAL_TOOL_ARGS=${main_args};write_flash;@${target_name}_args"
  325. -D "WORKING_DIRECTORY=${build_dir}"
  326. -P ${esptool_py_dir}/run_serial_tool.cmake
  327. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  328. USES_TERMINAL
  329. VERBATIM
  330. )
  331. set_target_properties(${target_name} PROPERTIES SUB_ARGS "${sub_args}")
  332. # Create the expression that contains the list of file names to pass
  333. # to esptool script
  334. set(flash_args_content "$<JOIN:$<TARGET_PROPERTY:${target_name},SUB_ARGS>, >\n\
  335. $<JOIN:$<TARGET_PROPERTY:${target_name},IMAGES>,\n>")
  336. # Write the previous expression to the target_name_args.in file
  337. file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${target_name}_args.in"
  338. CONTENT "${flash_args_content}")
  339. # Generate the actual expression value from the content of the file
  340. # we just wrote
  341. file(GENERATE OUTPUT "${build_dir}/${target_name}_args"
  342. INPUT "${CMAKE_CURRENT_BINARY_DIR}/${target_name}_args.in")
  343. # Check if the target has to be plain text or not, depending on the macro
  344. # CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT and the parameter
  345. # ALWAYS_PLAINTEXT
  346. set(encrypted FALSE)
  347. if(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT AND
  348. NOT __ALWAYS_PLAINTEXT)
  349. set(encrypted TRUE)
  350. endif()
  351. # If the file needs to be encrypted, create a target file that lets the user
  352. # flash this partition independently from other files.
  353. # For example, if 'target_name' is app-flash and 'encrypted' is TRUE,
  354. # 'build' directory will contain a file name 'encrypted_app-flash_args'
  355. if(${encrypted})
  356. add_custom_target(encrypted-${target_name}
  357. COMMAND ${CMAKE_COMMAND}
  358. -D "IDF_PATH=${idf_path}"
  359. -D "SERIAL_TOOL=${ESPTOOLPY}"
  360. -D "SERIAL_TOOL_ARGS=${main_args};write_flash;@encrypted_${target_name}_args"
  361. -D "WORKING_DIRECTORY=${build_dir}"
  362. -P ${esptool_py_dir}/run_serial_tool.cmake
  363. WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  364. USES_TERMINAL
  365. VERBATIM
  366. )
  367. # Generate the parameters for esptool.py command
  368. # In case we have both non encrypted and encrypted files to flash, we
  369. # can use --encrypt-files parameter to specify which ones should be
  370. # encrypted.
  371. # If we only have encrypted images to flash, we must use legacy
  372. # --encrypt parameter.
  373. # As the properties ENCRYPTED_IMAGES and NON_ENCRYPTED_IMAGES have not
  374. # been geenrated yet, we must use CMake expression generator to test
  375. # which esptool.py options we can use.
  376. # The variable has_non_encrypted_image will be evaluated to "1" if some
  377. # images must not be encrypted. This variable will be used in the next
  378. # expression
  379. set(has_non_encrypted_images "$<BOOL:$<TARGET_PROPERTY:\
  380. encrypted-${target_name},NON_ENCRYPTED_IMAGES>>")
  381. # Prepare esptool arguments (--encrypt or --encrypt-files)
  382. set(if_non_enc_expr "$<IF:${has_non_encrypted_images},,--encrypt>")
  383. set_target_properties(encrypted-${target_name} PROPERTIES SUB_ARGS
  384. "${sub_args}; ${if_non_enc_expr}")
  385. # Generate the list of files to pass to esptool
  386. set(encrypted_files "$<JOIN:$<TARGET_PROPERTY\
  387. :encrypted-${target_name},ENCRYPTED_IMAGES>,\n>")
  388. set(non_encrypted_files "$<JOIN:$<TARGET_PROPERTY:\
  389. encrypted-${target_name},NON_ENCRYPTED_IMAGES>,\n>")
  390. # Put both lists together, use --encrypted-files if we do also have
  391. # plain images to flash
  392. set(if_enc_expr "$<IF:${has_non_encrypted_images},--encrypt-files\n,>")
  393. set(flash_args_content "$<JOIN:$<TARGET_PROPERTY:\
  394. encrypted-${target_name},SUB_ARGS>, >\
  395. ${non_encrypted_files}\n\
  396. ${if_enc_expr}\
  397. ${encrypted_files}")
  398. # The expression is ready to be geenrated, write it to the file which
  399. # extension is .in
  400. file_generate("${CMAKE_CURRENT_BINARY_DIR}/encrypted_${target_name}_args.in"
  401. CONTENT "${flash_args_content}")
  402. # Generate the actual string from the content of the file we just wrote
  403. file_generate("${build_dir}/encrypted_${target_name}_args"
  404. INPUT "${CMAKE_CURRENT_BINARY_DIR}/encrypted_${target_name}_args.in")
  405. else()
  406. fail_target(encrypted-${target_name} "Error: The target encrypted-${target_name} requires"
  407. "CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT to be enabled.")
  408. endif()
  409. endfunction()
  410. function(esptool_py_custom_target target_name flasher_filename dependencies)
  411. idf_component_get_property(main_args esptool_py FLASH_ARGS)
  412. idf_component_get_property(sub_args esptool_py FLASH_SUB_ARGS)
  413. idf_build_get_property(build_dir BUILD_DIR)
  414. esptool_py_flash_target(${target_name} "${main_args}" "${sub_args}")
  415. # Copy the file to flash_xxx_args for compatibility for select target
  416. file_generate("${build_dir}/flash_${flasher_filename}_args"
  417. INPUT "${build_dir}/${target_name}_args")
  418. add_dependencies(${target_name} ${dependencies})
  419. if(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT)
  420. file_generate("${build_dir}/flash_encrypted_${flasher_filename}_args"
  421. INPUT "${build_dir}/encrypted_${target_name}_args")
  422. add_dependencies(encrypted-${target_name} ${dependencies})
  423. endif()
  424. endfunction()
  425. if(NOT BOOTLOADER_BUILD)
  426. set(flash_deps "")
  427. if(CONFIG_APP_BUILD_TYPE_APP_2NDBOOT)
  428. list(APPEND flash_deps "partition_table_bin")
  429. endif()
  430. if(CONFIG_APP_BUILD_GENERATE_BINARIES)
  431. list(APPEND flash_deps "app")
  432. endif()
  433. if(CONFIG_APP_BUILD_BOOTLOADER)
  434. list(APPEND flash_deps "bootloader")
  435. endif()
  436. esptool_py_custom_target(flash project "${flash_deps}")
  437. endif()
  438. # Adds espefuse functions for global use
  439. idf_component_get_property(esptool_py_dir esptool_py COMPONENT_DIR)
  440. include(${esptool_py_dir}/espefuse.cmake)