From c4f76ff8671d72f53d9bdad9c71758911ea54b9e Mon Sep 17 00:00:00 2001 From: Tiago Medicci Serrano Date: Fri, 19 Jan 2024 17:09:13 -0300 Subject: [PATCH] esp32c3: Fix building native MCUboot from sources This commit fixes building native MCUboot from sources by getting the required sources from `esp-hal-3rdparty` repository and enable building MCUboot and using it as the 2nd stage bootlaoder. --- arch/risc-v/src/esp32c3/Bootloader.mk | 47 ++++++++++++++++++++------- arch/risc-v/src/esp32c3/Kconfig | 5 +++ arch/risc-v/src/esp32c3/Make.defs | 31 +++++++++++++++++- 3 files changed, 70 insertions(+), 13 deletions(-) diff --git a/arch/risc-v/src/esp32c3/Bootloader.mk b/arch/risc-v/src/esp32c3/Bootloader.mk index 48e730b96f..bae150360e 100644 --- a/arch/risc-v/src/esp32c3/Bootloader.mk +++ b/arch/risc-v/src/esp32c3/Bootloader.mk @@ -20,16 +20,20 @@ ifeq ($(CONFIG_ESP32C3_BOOTLOADER_BUILD_FROM_SOURCE),y) +TOOLSDIR = $(TOPDIR)/tools/espressif CHIPDIR = $(TOPDIR)/arch/risc-v/src/chip +HALDIR = $(CHIPDIR)/esp-hal-3rdparty -BOOTLOADER_SRCDIR = $(CHIPDIR)/esp-nuttx-bootloader +BOOTLOADER_DIR = $(CHIPDIR)/bootloader +BOOTLOADER_SRCDIR = $(BOOTLOADER_DIR)/esp-nuttx-bootloader BOOTLOADER_VERSION = main BOOTLOADER_URL = https://github.com/espressif/esp-nuttx-bootloader BOOTLOADER_OUTDIR = out -BOOTLOADER_CONFIG = $(CHIPDIR)/bootloader.conf +BOOTLOADER_CONFIG = $(BOOTLOADER_DIR)/bootloader.conf -$(BOOTLOADER_SRCDIR): - $(Q) git clone $(BOOTLOADER_URL) $(BOOTLOADER_SRCDIR) -b $(BOOTLOADER_VERSION) +MCUBOOT_SRCDIR = $(BOOTLOADER_DIR)/mcuboot +MCUBOOT_ESPDIR = $(MCUBOOT_SRCDIR)/boot/espressif +MCUBOOT_URL = https://github.com/mcu-tools/mcuboot # Helpers for creating the configuration file @@ -87,13 +91,17 @@ ifeq ($(CONFIG_ESP32C3_APP_FORMAT_MCUBOOT),y) $(if $(CONFIG_ESP32C3_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC),$(call cfg_en,CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC)) \ $(if $(CONFIG_ESP32C3_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE),$(call cfg_en,CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE)) \ $(if $(CONFIG_ESP32C3_SECURE_FLASH_REQUIRE_ALREADY_ENABLED),$(call cfg_en,CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED)) \ + $(call cfg_val,CONFIG_ESP_BOOTLOADER_OFFSET,0x1000) \ $(call cfg_val,CONFIG_ESP_BOOTLOADER_SIZE,0xF000) \ - $(call cfg_val,CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS,$(CONFIG_ESP32C3_OTA_PRIMARY_SLOT_OFFSET)) \ + $(call cfg_val,CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS,$(CONFIG_ESP32C3_OTA_PRIMARY_SLOT_OFFSET)) \ $(call cfg_val,CONFIG_ESP_APPLICATION_SIZE,$(CONFIG_ESP32C3_OTA_SLOT_SIZE)) \ - $(call cfg_val,CONFIG_ESP_APPLICATION_SECONDARY_START_ADDRESS,$(CONFIG_ESP32C3_OTA_SECONDARY_SLOT_OFFSET)) \ + $(call cfg_val,CONFIG_ESP_IMAGE0_SECONDARY_START_ADDRESS,$(CONFIG_ESP32C3_OTA_SECONDARY_SLOT_OFFSET)) \ $(call cfg_en,CONFIG_ESP_MCUBOOT_WDT_ENABLE) \ $(call cfg_val,CONFIG_ESP_SCRATCH_OFFSET,$(CONFIG_ESP32C3_OTA_SCRATCH_OFFSET)) \ $(call cfg_val,CONFIG_ESP_SCRATCH_SIZE,$(CONFIG_ESP32C3_OTA_SCRATCH_SIZE)) \ + $(call cfg_en,CONFIG_ESP_CONSOLE_UART) \ + $(if $(CONFIG_UART0_SERIAL_CONSOLE),$(call cfg_val,CONFIG_ESP_CONSOLE_UART_NUM,0)) \ + $(if $(CONFIG_UART1_SERIAL_CONSOLE),$(call cfg_val,CONFIG_ESP_CONSOLE_UART_NUM,1)) \ } >> $(BOOTLOADER_CONFIG) else ifeq ($(CONFIG_ESP32C3_APP_FORMAT_LEGACY),y) $(Q) { \ @@ -109,12 +117,22 @@ ifeq ($(CONFIG_ESP32C3_APP_FORMAT_MCUBOOT),y) BOOTLOADER_BIN = $(TOPDIR)/mcuboot-esp32c3.bin BOOTLOADER_SIGNED_BIN = $(TOPDIR)/mcuboot-esp32c3.signed.bin -$(BOOTLOADER_BIN): $(BOOTLOADER_SRCDIR) $(BOOTLOADER_CONFIG) - $(Q) echo "Building Bootloader" - $(Q) $(BOOTLOADER_SRCDIR)/build_mcuboot.sh -c esp32c3 -s -f $(BOOTLOADER_CONFIG) - $(call COPYFILE, $(BOOTLOADER_SRCDIR)/$(BOOTLOADER_OUTDIR)/mcuboot-esp32c3.bin, $(TOPDIR)) +$(MCUBOOT_SRCDIR): + $(Q) echo "Cloning MCUboot" + $(Q) git clone --quiet $(MCUBOOT_URL) $(MCUBOOT_SRCDIR) + $(Q) git -C "$(MCUBOOT_SRCDIR)" checkout --quiet $(CONFIG_ESP32C3_MCUBOOT_VERSION) + $(Q) git -C "$(MCUBOOT_SRCDIR)" submodule --quiet update --init --recursive ext/mbedtls -bootloader: $(BOOTLOADER_CONFIG) $(BOOTLOADER_BIN) +$(BOOTLOADER_BIN): chip/$(ESP_HAL_3RDPARTY_REPO) $(MCUBOOT_SRCDIR) $(BOOTLOADER_CONFIG) + $(Q) echo "Building Bootloader" + $(Q) $(TOOLSDIR)/build_mcuboot.sh \ + -c esp32c3 \ + -f $(BOOTLOADER_CONFIG) \ + -p $(BOOTLOADER_DIR) \ + -e $(HALDIR) + $(call COPYFILE, $(BOOTLOADER_DIR)/$(BOOTLOADER_OUTDIR)/mcuboot-esp32c3.bin, $(TOPDIR)) + +bootloader: $(BOOTLOADER_BIN) ifeq ($(CONFIG_ESP32C3_SECURE_BOOT),y) $(eval KEYDIR := $(TOPDIR)/$(ESPSEC_KEYDIR)) $(eval BOOTLOADER_SIGN_KEY := $(abspath $(KEYDIR)/$(subst ",,$(CONFIG_ESP32C3_SECURE_BOOT_BOOTLOADER_SIGNING_KEY)))) @@ -139,13 +157,18 @@ endif endif clean_bootloader: - $(call DELDIR,$(BOOTLOADER_SRCDIR)) + $(call DELDIR,$(MCUBOOT_SRCDIR)) + $(call DELDIR,$(BOOTLOADER_DIR)/$(BOOTLOADER_OUTDIR)) $(call DELFILE,$(BOOTLOADER_CONFIG)) $(call DELFILE,$(BOOTLOADER_BIN)) $(if $(CONFIG_ESP32C3_SECURE_BOOT_BUILD_SIGNED_BINARIES),$(call DELFILE,$(BOOTLOADER_SIGNED_BIN))) else ifeq ($(CONFIG_ESP32C3_APP_FORMAT_LEGACY),y) +$(BOOTLOADER_SRCDIR): + $(Q) git clone $(BOOTLOADER_URL) $(BOOTLOADER_SRCDIR) -b $(BOOTLOADER_VERSION) + $(Q) git -C $(BOOTLOADER_SRCDIR) submodule update --init esp-idf + bootloader: $(BOOTLOADER_SRCDIR) $(BOOTLOADER_CONFIG) $(Q) echo "Building Bootloader binaries" $(Q) $(BOOTLOADER_SRCDIR)/build_idfboot.sh -c esp32c3 -s -f $(BOOTLOADER_CONFIG) diff --git a/arch/risc-v/src/esp32c3/Kconfig b/arch/risc-v/src/esp32c3/Kconfig index 88caa8a878..abef7a8e82 100644 --- a/arch/risc-v/src/esp32c3/Kconfig +++ b/arch/risc-v/src/esp32c3/Kconfig @@ -1093,6 +1093,11 @@ config ESP32C3_ESPTOOL_TARGET_SECONDARY endchoice +config ESP32C3_MCUBOOT_VERSION + string "MCUboot version" + depends on ESP32C3_APP_FORMAT_MCUBOOT + default "b206b99b1555ca15f790a3287e57dc98ef3df2ac" + config ESP32C3_APP_MCUBOOT_HEADER_SIZE int "Application image header size (in bytes)" default 32 diff --git a/arch/risc-v/src/esp32c3/Make.defs b/arch/risc-v/src/esp32c3/Make.defs index 3e05468804..80cd169179 100644 --- a/arch/risc-v/src/esp32c3/Make.defs +++ b/arch/risc-v/src/esp32c3/Make.defs @@ -18,7 +18,6 @@ # ############################################################################ -include chip/Bootloader.mk include common/Make.defs # Specify our HEAD assembly file. This will be linked as @@ -225,3 +224,33 @@ ifeq ($(CONFIG_ESP32C3_BLE),y) CHIP_CSRCS += esp32c3_ble_adapter.c esp32c3_ble.c EXTRA_LIBS += -lbtbb -lbtdm_app endif + +############################################################################# +# Espressif HAL for 3rd Party Platforms +############################################################################# + +# Fetch source files and add them to build + +ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty +ifndef ESP_HAL_3RDPARTY_VERSION + ESP_HAL_3RDPARTY_VERSION = 45c33111b441363e1267158186a60f42525228ca +endif + +ifndef ESP_HAL_3RDPARTY_URL + ESP_HAL_3RDPARTY_URL = https://github.com/espressif/esp-hal-3rdparty.git +endif + +chip/$(ESP_HAL_3RDPARTY_REPO): + $(Q) echo "Cloning Espressif HAL for 3rd Party Platforms" + $(Q) git clone --quiet $(ESP_HAL_3RDPARTY_URL) chip/$(ESP_HAL_3RDPARTY_REPO) + $(Q) echo "Espressif HAL for 3rd Party Platforms: ${ESP_HAL_3RDPARTY_VERSION}" + $(Q) git -C chip/$(ESP_HAL_3RDPARTY_REPO) checkout --quiet $(ESP_HAL_3RDPARTY_VERSION) + +# Silent preprocessor warnings + +CFLAGS += -Wno-undef -Wno-unused-variable + +include chip/Bootloader.mk + +distclean:: + $(call DELDIR,chip/$(ESP_HAL_3RDPARTY_REPO))