project_include.cmake 22 KB

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