project_include.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_scheme "1")
  29. elseif(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME)
  30. set(secure_apps_signing_scheme "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. fail_at_build_time(gen_secure_boot_signing_key
  59. "Secure Boot Signing Key ${CONFIG_SECURE_BOOT_SIGNING_KEY} does not exist. Generate using:"
  60. "\tespsecure.py generate_signing_key --version ${secure_apps_signing_scheme} \
  61. ${CONFIG_SECURE_BOOT_SIGNING_KEY}")
  62. else()
  63. add_custom_target(gen_secure_boot_signing_key)
  64. endif()
  65. set(SECURE_BOOT_SIGNING_KEY ${secure_boot_signing_key}) # needed by some other components
  66. set(sign_key_arg "-DSECURE_BOOT_SIGNING_KEY=${secure_boot_signing_key}")
  67. set(ver_key_arg)
  68. add_dependencies(gen_secure_boot_keys gen_secure_boot_signing_key)
  69. elseif(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME)
  70. get_filename_component(secure_boot_verification_key
  71. ${CONFIG_SECURE_BOOT_VERIFICATION_KEY}
  72. ABSOLUTE BASE_DIR "${project_dir}")
  73. if(NOT EXISTS ${secure_boot_verification_key})
  74. # If the verification key is not found, create a phony gen_secure_boot_verification_key target that
  75. # fails the build. fail_at_build_time causes a cmake run next time
  76. # (to pick up a new verification key if one exists, etc.)
  77. fail_at_build_time(gen_secure_boot_verification_key
  78. "Secure Boot Verification Public Key ${CONFIG_SECURE_BOOT_VERIFICATION_KEY} does not exist."
  79. "\tThis can be extracted from the private signing key."
  80. "\tSee docs/security/secure-boot-v1.rst for details.")
  81. else()
  82. add_custom_target(gen_secure_boot_verification_key)
  83. endif()
  84. set(sign_key_arg)
  85. set(ver_key_arg "-DSECURE_BOOT_VERIFICATION_KEY=${secure_boot_verification_key}")
  86. add_dependencies(gen_secure_boot_keys gen_secure_boot_verification_key)
  87. endif()
  88. else()
  89. set(sign_key_arg)
  90. set(ver_key_arg)
  91. endif()
  92. idf_build_get_property(idf_path IDF_PATH)
  93. idf_build_get_property(idf_target IDF_TARGET)
  94. idf_build_get_property(sdkconfig SDKCONFIG)
  95. idf_build_get_property(python PYTHON)
  96. idf_build_get_property(extra_cmake_args EXTRA_CMAKE_ARGS)
  97. externalproject_add(bootloader
  98. SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/subproject"
  99. BINARY_DIR "${BOOTLOADER_BUILD_DIR}"
  100. CMAKE_ARGS -DSDKCONFIG=${sdkconfig} -DIDF_PATH=${idf_path} -DIDF_TARGET=${idf_target}
  101. -DPYTHON_DEPS_CHECKED=1 -DPYTHON=${python}
  102. -DEXTRA_COMPONENT_DIRS=${CMAKE_CURRENT_LIST_DIR}
  103. -DPROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}
  104. ${sign_key_arg} ${ver_key_arg}
  105. # LEGACY_INCLUDE_COMMON_HEADERS has to be passed in via cache variable since
  106. # the bootloader common component requirements depends on this and
  107. # config variables are not available before project() call.
  108. -DLEGACY_INCLUDE_COMMON_HEADERS=${CONFIG_LEGACY_INCLUDE_COMMON_HEADERS}
  109. ${extra_cmake_args}
  110. INSTALL_COMMAND ""
  111. BUILD_ALWAYS 1 # no easy way around this...
  112. BUILD_BYPRODUCTS ${bootloader_binary_files}
  113. )
  114. if(CONFIG_SECURE_SIGNED_APPS)
  115. add_dependencies(bootloader gen_secure_boot_keys)
  116. endif()
  117. # this is a hack due to an (annoying) shortcoming in cmake, it can't
  118. # extend the 'clean' target to the external project
  119. # see thread: https://cmake.org/pipermail/cmake/2016-December/064660.html
  120. #
  121. # So for now we just have the top-level build remove the final build products...
  122. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
  123. ADDITIONAL_MAKE_CLEAN_FILES
  124. ${bootloader_binary_files})