project_include.cmake 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. set(BOOTLOADER_OFFSET ${CONFIG_BOOTLOADER_OFFSET_IN_FLASH})
  2. # Do not generate flash file when building bootloader
  3. if(BOOTLOADER_BUILD OR NOT CONFIG_APP_BUILD_BOOTLOADER)
  4. return()
  5. endif()
  6. # Glue to build the bootloader subproject binary as an external
  7. # cmake project under this one
  8. #
  9. #
  10. idf_build_get_property(build_dir BUILD_DIR)
  11. set(BOOTLOADER_BUILD_DIR "${build_dir}/bootloader")
  12. set(BOOTLOADER_ELF_FILE "${BOOTLOADER_BUILD_DIR}/bootloader.elf")
  13. set(bootloader_binary_files
  14. "${BOOTLOADER_ELF_FILE}"
  15. "${BOOTLOADER_BUILD_DIR}/bootloader.bin"
  16. "${BOOTLOADER_BUILD_DIR}/bootloader.map"
  17. )
  18. idf_build_get_property(project_dir PROJECT_DIR)
  19. # There are some additional processing when CONFIG_SECURE_SIGNED_APPS. This happens
  20. # when either CONFIG_SECURE_BOOT_V1_ENABLED or CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES.
  21. # For both cases, the user either sets binaries to be signed during build or not
  22. # using CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES.
  23. #
  24. # Regardless, pass the main project's keys (signing/verification) to the bootloader subproject
  25. # via config.
  26. if(CONFIG_SECURE_SIGNED_APPS)
  27. add_custom_target(gen_secure_boot_keys)
  28. if(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME)
  29. set(secure_apps_signing_version "1")
  30. elseif(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME OR CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME)
  31. set(secure_apps_signing_version "2")
  32. endif()
  33. if(CONFIG_SECURE_BOOT_V1_ENABLED)
  34. # Check that the configuration is sane
  35. if((CONFIG_SECURE_BOOTLOADER_REFLASHABLE AND CONFIG_SECURE_BOOTLOADER_ONE_TIME_FLASH) OR
  36. (NOT CONFIG_SECURE_BOOTLOADER_REFLASHABLE AND NOT CONFIG_SECURE_BOOTLOADER_ONE_TIME_FLASH))
  37. fail_at_build_time(bootloader "Invalid bootloader target: bad sdkconfig?")
  38. endif()
  39. if(CONFIG_SECURE_BOOTLOADER_REFLASHABLE)
  40. set(bootloader_binary_files
  41. ${bootloader_binary_files}
  42. "${BOOTLOADER_BUILD_DIR}/bootloader-reflash-digest.bin"
  43. "${BOOTLOADER_BUILD_DIR}/secure-bootloader-key-192.bin"
  44. "${BOOTLOADER_BUILD_DIR}/secure-bootloader-key-256.bin"
  45. )
  46. endif()
  47. endif()
  48. # Since keys are usually given relative to main project dir, get the absolute paths to the keys
  49. # for use by the bootloader subproject. Replace the values in config with these absolute paths,
  50. # so that bootloader subproject does not need to assume main project dir to obtain path to the keys.
  51. if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
  52. get_filename_component(secure_boot_signing_key
  53. "${CONFIG_SECURE_BOOT_SIGNING_KEY}"
  54. ABSOLUTE BASE_DIR "${project_dir}")
  55. if(NOT EXISTS ${secure_boot_signing_key})
  56. # If the signing key is not found, create a phony gen_secure_boot_signing_key target that
  57. # fails the build. fail_at_build_time causes a cmake run next time
  58. # (to pick up a new signing key if one exists, etc.)
  59. if(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME)
  60. fail_at_build_time(gen_secure_boot_signing_key
  61. "Secure Boot Signing Key ${CONFIG_SECURE_BOOT_SIGNING_KEY} does not exist. Generate using:"
  62. "\tespsecure.py generate_signing_key --version ${secure_apps_signing_version} \
  63. ${CONFIG_SECURE_BOOT_SIGNING_KEY}")
  64. else()
  65. if(CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_192_BITS)
  66. set(scheme "ecdsa192")
  67. elseif(CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_256_BITS)
  68. set(scheme "ecdsa256")
  69. endif()
  70. fail_at_build_time(gen_secure_boot_signing_key
  71. "Secure Boot Signing Key ${CONFIG_SECURE_BOOT_SIGNING_KEY} does not exist. Generate using:"
  72. "\tespsecure.py generate_signing_key --version ${secure_apps_signing_version} \
  73. --scheme ${scheme} ${CONFIG_SECURE_BOOT_SIGNING_KEY}")
  74. endif()
  75. else()
  76. add_custom_target(gen_secure_boot_signing_key)
  77. endif()
  78. set(SECURE_BOOT_SIGNING_KEY ${secure_boot_signing_key}) # needed by some other components
  79. set(sign_key_arg "-DSECURE_BOOT_SIGNING_KEY=${secure_boot_signing_key}")
  80. set(ver_key_arg)
  81. add_dependencies(gen_secure_boot_keys gen_secure_boot_signing_key)
  82. elseif(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME)
  83. get_filename_component(secure_boot_verification_key
  84. ${CONFIG_SECURE_BOOT_VERIFICATION_KEY}
  85. ABSOLUTE BASE_DIR "${project_dir}")
  86. if(NOT EXISTS ${secure_boot_verification_key})
  87. # If the verification key is not found, create a phony gen_secure_boot_verification_key target that
  88. # fails the build. fail_at_build_time causes a cmake run next time
  89. # (to pick up a new verification key if one exists, etc.)
  90. fail_at_build_time(gen_secure_boot_verification_key
  91. "Secure Boot Verification Public Key ${CONFIG_SECURE_BOOT_VERIFICATION_KEY} does not exist."
  92. "\tThis can be extracted from the private signing key."
  93. "\tSee docs/security/secure-boot-v1.rst for details.")
  94. else()
  95. add_custom_target(gen_secure_boot_verification_key)
  96. endif()
  97. set(sign_key_arg)
  98. set(ver_key_arg "-DSECURE_BOOT_VERIFICATION_KEY=${secure_boot_verification_key}")
  99. add_dependencies(gen_secure_boot_keys gen_secure_boot_verification_key)
  100. endif()
  101. else()
  102. set(sign_key_arg)
  103. set(ver_key_arg)
  104. endif()
  105. idf_build_get_property(idf_path IDF_PATH)
  106. idf_build_get_property(idf_target IDF_TARGET)
  107. idf_build_get_property(sdkconfig SDKCONFIG)
  108. idf_build_get_property(python PYTHON)
  109. idf_build_get_property(extra_cmake_args EXTRA_CMAKE_ARGS)
  110. # We cannot pass lists are a parameter to the external project without modifying the ';' spearator
  111. string(REPLACE ";" "|" BOOTLOADER_IGNORE_EXTRA_COMPONENT "${BOOTLOADER_IGNORE_EXTRA_COMPONENT}")
  112. externalproject_add(bootloader
  113. SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/subproject"
  114. BINARY_DIR "${BOOTLOADER_BUILD_DIR}"
  115. # Modiying the list separator for the arguments, as such, we won't need to manually
  116. # replace the new separator by the default ';' in the subproject
  117. LIST_SEPARATOR |
  118. CMAKE_ARGS -DSDKCONFIG=${sdkconfig} -DIDF_PATH=${idf_path} -DIDF_TARGET=${idf_target}
  119. -DPYTHON_DEPS_CHECKED=1 -DPYTHON=${python}
  120. -DEXTRA_COMPONENT_DIRS=${CMAKE_CURRENT_LIST_DIR}
  121. -DPROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}
  122. -DIGNORE_EXTRA_COMPONENT=${BOOTLOADER_IGNORE_EXTRA_COMPONENT}
  123. ${sign_key_arg} ${ver_key_arg}
  124. ${extra_cmake_args}
  125. INSTALL_COMMAND ""
  126. BUILD_ALWAYS 1 # no easy way around this...
  127. BUILD_BYPRODUCTS ${bootloader_binary_files}
  128. )
  129. if(CONFIG_SECURE_SIGNED_APPS)
  130. add_dependencies(bootloader gen_secure_boot_keys)
  131. endif()
  132. # this is a hack due to an (annoying) shortcoming in cmake, it can't
  133. # extend the 'clean' target to the external project
  134. # see thread: https://cmake.org/pipermail/cmake/2016-December/064660.html
  135. #
  136. # So for now we just have the top-level build remove the final build products...
  137. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
  138. ADDITIONAL_CLEAN_FILES
  139. ${bootloader_binary_files})