project_include.cmake 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. set(BOOTLOADER_OFFSET 0x1000)
  2. # Do not generate flash file when building bootloader
  3. if(BOOTLOADER_BUILD)
  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_CONFIG_SECURE_SIGNED_APPS. This happens
  19. # when either CONFIG_SECURE_BOOT_ENABLED or 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_BOOT_ENABLED)
  28. # Check that the configuration is sane
  29. if((CONFIG_SECURE_BOOTLOADER_REFLASHABLE AND CONFIG_SECURE_BOOTLOADER_ONE_TIME_FLASH) OR
  30. (NOT CONFIG_SECURE_BOOTLOADER_REFLASHABLE AND NOT CONFIG_SECURE_BOOTLOADER_ONE_TIME_FLASH))
  31. fail_at_build_time(bootloader "Invalid bootloader target: bad sdkconfig?")
  32. endif()
  33. if(CONFIG_SECURE_BOOTLOADER_REFLASHABLE)
  34. set(bootloader_binary_files
  35. ${bootloader_binary_files}
  36. "${BOOTLOADER_BUILD_DIR}/bootloader-reflash-digest.bin"
  37. "${BOOTLOADER_BUILD_DIR}/secure-bootloader-key-192.bin"
  38. "${BOOTLOADER_BUILD_DIR}/secure-bootloader-key-256.bin"
  39. )
  40. endif()
  41. endif()
  42. # Since keys are usually given relative to main project dir, get the absolute paths to the keys
  43. # for use by the bootloader subproject. Replace the values in config with these absolute paths,
  44. # so that bootloader subproject does not need to assume main project dir to obtain path to the keys.
  45. if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
  46. get_filename_component(secure_boot_signing_key
  47. "${CONFIG_SECURE_BOOT_SIGNING_KEY}"
  48. ABSOLUTE BASE_DIR "${project_dir}")
  49. if(NOT EXISTS ${secure_boot_signing_key})
  50. # If the signing key is not found, create a phony gen_secure_boot_signing_key target that
  51. # fails the build. fail_at_build_time causes a cmake run next time
  52. # (to pick up a new signing key if one exists, etc.)
  53. fail_at_build_time(gen_secure_boot_signing_key
  54. "Secure Boot Signing Key ${CONFIG_SECURE_BOOT_SIGNING_KEY} does not exist. Generate using:"
  55. "\tespsecure.py generate_signing_key ${CONFIG_SECURE_BOOT_SIGNING_KEY}")
  56. else()
  57. add_custom_target(gen_secure_boot_signing_key)
  58. endif()
  59. set(SECURE_BOOT_SIGNING_KEY ${secure_boot_signing_key}) # needed by some other components
  60. set(sign_key_arg "-DSECURE_BOOT_SIGNING_KEY=${secure_boot_signing_key}")
  61. add_dependencies(gen_secure_boot_keys gen_secure_boot_signing_key)
  62. else()
  63. get_filename_component(secure_boot_verification_key
  64. ${CONFIG_SECURE_BOOT_VERIFICATION_KEY}
  65. ABSOLUTE BASE_DIR "${project_dir}")
  66. if(NOT EXISTS ${secure_boot_verification_key})
  67. # If the verification key is not found, create a phony gen_secure_boot_verification_key target that
  68. # fails the build. fail_at_build_time causes a cmake run next time
  69. # (to pick up a new verification key if one exists, etc.)
  70. fail_at_build_time(gen_secure_boot_verification_key
  71. "Secure Boot Verification Public Key ${CONFIG_SECURE_BOOT_VERIFICATION_KEY} does not exist."
  72. "\tThis can be extracted from the private signing key."
  73. "\tSee docs/security/secure-boot.rst for details.")
  74. else()
  75. add_custom_target(gen_secure_boot_verification_key)
  76. endif()
  77. set(ver_key_arg "-DSECURE_BOOT_VERIFICATION_KEY=${secure_boot_verification_key}")
  78. add_dependencies(gen_secure_boot_keys gen_secure_boot_verification_key)
  79. endif()
  80. endif()
  81. idf_build_get_property(idf_path IDF_PATH)
  82. idf_build_get_property(idf_target IDF_TARGET)
  83. idf_build_get_property(sdkconfig SDKCONFIG)
  84. externalproject_add(bootloader
  85. SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/subproject"
  86. BINARY_DIR "${BOOTLOADER_BUILD_DIR}"
  87. CMAKE_ARGS -DSDKCONFIG=${sdkconfig} -DIDF_PATH=${idf_path} -DIDF_TARGET=${idf_target}
  88. -DPYTHON_DEPS_CHECKED=1
  89. -DEXTRA_COMPONENT_DIRS=${CMAKE_CURRENT_LIST_DIR}
  90. ${sign_key_arg} ${ver_key_arg}
  91. # LEGACY_INCLUDE_COMMON_HEADERS has to be passed in via cache variable since
  92. # the bootloader common component requirements depends on this and
  93. # config variables are not available before project() call.
  94. -DLEGACY_INCLUDE_COMMON_HEADERS=${CONFIG_LEGACY_INCLUDE_COMMON_HEADERS}
  95. INSTALL_COMMAND ""
  96. BUILD_ALWAYS 1 # no easy way around this...
  97. BUILD_BYPRODUCTS ${bootloader_binary_files}
  98. )
  99. if(CONFIG_SECURE_SIGNED_APPS)
  100. add_dependencies(bootloader gen_secure_boot_keys)
  101. endif()
  102. # this is a hack due to an (annoying) shortcoming in cmake, it can't
  103. # extend the 'clean' target to the external project
  104. # see thread: https://cmake.org/pipermail/cmake/2016-December/064660.html
  105. #
  106. # So for now we just have the top-level build remove the final build products...
  107. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
  108. ADDITIONAL_MAKE_CLEAN_FILES
  109. ${bootloader_binary_files})