project_include.cmake 6.7 KB

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