Makefile.projbuild 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Bootloader component (top-level project parts)
  2. #
  3. # The bootloader is not a real component that gets linked into the project.
  4. # Instead it is an entire standalone project (in subproject/) that gets
  5. # built in the upper project's build directory. This Makefile.projbuild provides
  6. # the glue to build the bootloader project from the original project. It
  7. # basically runs Make in the subproject/ directory but it needs to
  8. # zero some variables the ESP-IDF project.mk makefile exports first, to not
  9. # let them interfere.
  10. #
  11. BOOTLOADER_COMPONENT_PATH := $(COMPONENT_PATH)
  12. BOOTLOADER_BUILD_DIR=$(abspath $(BUILD_DIR_BASE)/bootloader)
  13. BOOTLOADER_BIN=$(BOOTLOADER_BUILD_DIR)/bootloader.bin
  14. # signing key path is resolved relative to the project directory
  15. CONFIG_SECURE_BOOT_SIGNING_KEY ?=
  16. SECURE_BOOT_SIGNING_KEY=$(abspath $(call dequote,$(CONFIG_SECURE_BOOT_SIGNING_KEY)))
  17. export SECURE_BOOT_SIGNING_KEY # used by bootloader_support component
  18. BOOTLOADER_SIGNED_BIN ?=
  19. # Has a matching value in bootloader_support esp_flash_partitions.h
  20. BOOTLOADER_OFFSET := 0x1000
  21. # Custom recursive make for bootloader sub-project
  22. #
  23. # NB: Some variables are cleared in the environment, not
  24. # overriden, because they need to be re-defined in the child
  25. # project.
  26. #
  27. # Pass PROJECT_PATH variable, it will let the subproject look
  28. # for user defined bootloader component(s).
  29. BOOTLOADER_MAKE= +\
  30. PROJECT_PATH= \
  31. COMPONENT_DIRS= \
  32. $(MAKE) -C $(BOOTLOADER_COMPONENT_PATH)/subproject \
  33. V=$(V) \
  34. BUILD_DIR_BASE=$(BOOTLOADER_BUILD_DIR) \
  35. TEST_COMPONENTS= \
  36. TESTS_ALL= \
  37. EXCLUDE_COMPONENTS= \
  38. PROJECT_SOURCE_DIR=$(PROJECT_PATH)
  39. .PHONY: bootloader-clean bootloader-flash bootloader-list-components bootloader $(BOOTLOADER_BIN)
  40. $(BOOTLOADER_BIN): $(SDKCONFIG_MAKEFILE)
  41. $(BOOTLOADER_MAKE) $@
  42. clean: bootloader-clean
  43. bootloader-list-components:
  44. $(BOOTLOADER_MAKE) list-components
  45. ifndef CONFIG_SECURE_BOOT
  46. # If secure boot disabled, bootloader flashing is integrated
  47. # with 'make flash' and no warnings are printed.
  48. bootloader: $(BOOTLOADER_BIN) | check_python_dependencies
  49. @echo $(SEPARATOR)
  50. @echo "Bootloader built. Default flash command is:"
  51. @echo "$(ESPTOOLPY_WRITE_FLASH) $(BOOTLOADER_OFFSET) $^"
  52. ESPTOOL_ALL_FLASH_ARGS += $(BOOTLOADER_OFFSET) $(BOOTLOADER_BIN)
  53. UF2_ADD_BINARIES += $(BOOTLOADER_OFFSET) $(BOOTLOADER_BIN)
  54. bootloader-flash: $(BOOTLOADER_BIN) $(call prereq_if_explicit,erase_flash) | check_python_dependencies
  55. $(ESPTOOLPY_WRITE_FLASH) 0x1000 $^
  56. else ifdef CONFIG_SECURE_BOOTLOADER_ONE_TIME_FLASH
  57. # One time flashing requires user to run esptool.py command themselves,
  58. # and warning is printed about inability to reflash.
  59. #
  60. # The flashing command is deliberately printed without an auto-reset
  61. # step, so the device doesn't immediately reset to flash itself.
  62. bootloader: $(BOOTLOADER_BIN) | check_python_dependencies
  63. @echo $(SEPARATOR)
  64. @echo "Bootloader built. One-time flash command is:"
  65. @echo "$(subst hard_reset,no_reset,$(ESPTOOLPY_WRITE_FLASH)) $(BOOTLOADER_OFFSET) $(BOOTLOADER_BIN)"
  66. @echo $(SEPARATOR)
  67. @echo "* IMPORTANT: After first boot, BOOTLOADER CANNOT BE RE-FLASHED on same device"
  68. else ifdef CONFIG_SECURE_BOOTLOADER_REFLASHABLE
  69. # Reflashable secure bootloader
  70. # generates a digest binary (bootloader + digest)
  71. ifdef CONFIG_SECURE_BOOTLOADER_KEY_ENCODING_192BIT
  72. KEY_DIGEST_LEN=192
  73. else
  74. KEY_DIGEST_LEN=256
  75. endif
  76. BOOTLOADER_DIGEST_BIN := $(BOOTLOADER_BUILD_DIR)/bootloader-reflash-digest.bin
  77. SECURE_BOOTLOADER_KEY := $(BOOTLOADER_BUILD_DIR)/secure-bootloader-key-$(KEY_DIGEST_LEN).bin
  78. ifdef CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES
  79. $(SECURE_BOOTLOADER_KEY): $(SECURE_BOOT_SIGNING_KEY) | check_python_dependencies
  80. $(ESPSECUREPY) digest_private_key --keylen $(KEY_DIGEST_LEN) -k $< $@
  81. else
  82. $(SECURE_BOOTLOADER_KEY):
  83. @echo "No pre-generated key for a reflashable secure bootloader is available, due to signing configuration."
  84. @echo "To generate one, you can use this command:"
  85. @echo "espsecure.py generate_flash_encryption_key $@"
  86. @echo "then re-run make."
  87. exit 1
  88. endif
  89. bootloader: $(BOOTLOADER_DIGEST_BIN)
  90. @echo $(SEPARATOR)
  91. @echo "Bootloader built and secure digest generated. First time flash command is:"
  92. @echo "$(ESPEFUSEPY) burn_key secure_boot_v1 $(SECURE_BOOTLOADER_KEY)"
  93. @echo "$(ESPTOOLPY_WRITE_FLASH) $(BOOTLOADER_OFFSET) $(BOOTLOADER_BIN)"
  94. @echo $(SEPARATOR)
  95. @echo "To reflash the bootloader after initial flash:"
  96. @echo "$(ESPTOOLPY_WRITE_FLASH) 0x0 $(BOOTLOADER_DIGEST_BIN)"
  97. @echo $(SEPARATOR)
  98. @echo "* After first boot, only re-flashes of this kind (with same key) will be accepted."
  99. @echo "* Not recommended to re-use the same secure boot keyfile on multiple production devices."
  100. $(BOOTLOADER_DIGEST_BIN): $(BOOTLOADER_BIN) $(SECURE_BOOTLOADER_KEY) | check_python_dependencies
  101. @echo "DIGEST $(notdir $@)"
  102. $(ESPSECUREPY) digest_secure_bootloader -k $(SECURE_BOOTLOADER_KEY) -o $@ $<
  103. else ifdef CONFIG_SECURE_BOOT_V2_ENABLED
  104. BOOTLOADER_SIGNED_BIN := $(BOOTLOADER_BUILD_DIR)/bootloader-signed.bin
  105. ifdef CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES
  106. bootloader: $(BOOTLOADER_BIN) $(SDKCONFIG_MAKEFILE) | check_python_dependencies
  107. $(ESPSECUREPY) sign_data --keyfile $(SECURE_BOOT_SIGNING_KEY) --version 2 \
  108. -o $(BOOTLOADER_SIGNED_BIN) $(BOOTLOADER_BIN)
  109. else
  110. bootloader: $(BOOTLOADER_BIN) $(SDKCONFIG_MAKEFILE) | check_python_dependencies
  111. @echo "Bootloader not signed. Sign the bootloader before flashing."
  112. @echo "To sign the bootloader, you can use this command:"
  113. @echo "espsecure.py sign_data --keyfile SECURE_BOOT_SIGNING_KEY --version 2 $(BOOTLOADER_BIN)"
  114. endif
  115. @echo $(SEPARATOR)
  116. @echo "Use the following command to flash the bootloader:"
  117. ifdef CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES
  118. @echo "$(ESPTOOLPY_WRITE_FLASH) $(BOOTLOADER_OFFSET) $(BOOTLOADER_SIGNED_BIN)"
  119. else
  120. @echo "$(ESPTOOLPY_WRITE_FLASH) $(BOOTLOADER_OFFSET) $(BOOTLOADER_BIN)"
  121. endif
  122. @echo $(SEPARATOR)
  123. else # CONFIG_SECURE_BOOT && !CONFIG_SECURE_BOOTLOADER_REFLASHABLE \
  124. && !CONFIG_SECURE_BOOTLOADER_ONE_TIME_FLASH && !CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME
  125. bootloader:
  126. @echo "Invalid bootloader target: bad sdkconfig?"
  127. @exit 1
  128. endif
  129. ifndef CONFIG_SECURE_BOOT
  130. # don't build bootloader by default if secure boot is enabled
  131. all_binaries: $(BOOTLOADER_BIN)
  132. endif
  133. bootloader-clean: $(SDKCONFIG_MAKEFILE)
  134. $(BOOTLOADER_MAKE) app-clean
  135. ifdef CONFIG_SECURE_BOOTLOADER_REFLASHABLE
  136. rm -f $(SECURE_BOOTLOADER_KEY) $(BOOTLOADER_DIGEST_BIN)
  137. endif
  138. ifdef CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME
  139. ifdef CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES
  140. rm -f $(BOOTLOADER_SIGNED_BIN)
  141. endif
  142. endif