From 4bcc9305351d0fc88f4adc2bb7d8365b2aa148f6 Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz Date: Wed, 16 Aug 2023 11:19:26 -0300 Subject: [PATCH] arch/xtensa/esp32s2: Update MCUboot build process Change the MCUboot build process to, in the future, deprecate the esp-nuttx-bootloaders repository. --- .../esp32s2/boards/esp32s2-saola-1/index.rst | 6 +- arch/xtensa/src/esp32s2/.gitignore | 4 +- arch/xtensa/src/esp32s2/Bootloader.mk | 56 +++++++++++++------ arch/xtensa/src/esp32s2/Kconfig | 5 ++ arch/xtensa/src/esp32s2/Make.defs | 31 +++++++++- .../configs/mcuboot_nsh/defconfig | 1 + 6 files changed, 82 insertions(+), 21 deletions(-) diff --git a/Documentation/platforms/xtensa/esp32s2/boards/esp32s2-saola-1/index.rst b/Documentation/platforms/xtensa/esp32s2/boards/esp32s2-saola-1/index.rst index 5e44593d36..0fe1cf2711 100644 --- a/Documentation/platforms/xtensa/esp32s2/boards/esp32s2-saola-1/index.rst +++ b/Documentation/platforms/xtensa/esp32s2/boards/esp32s2-saola-1/index.rst @@ -237,9 +237,9 @@ The corresponding output should show related debug information. mcuboot_nsh ----------- -Similar configuration as nsh, except that it enables booting from -MCUboot and the experimental features configuration. -You can find more information on the `example's documentation `_. +This configuration is the same as the ``nsh`` configuration, but it generates the application +image in a format that can be used by MCUboot. It also makes the ``make bootloader`` command to +build the MCUboot bootloader image using the Espressif HAL. nsh --- diff --git a/arch/xtensa/src/esp32s2/.gitignore b/arch/xtensa/src/esp32s2/.gitignore index c0bce18398..172cd927bd 100644 --- a/arch/xtensa/src/esp32s2/.gitignore +++ b/arch/xtensa/src/esp32s2/.gitignore @@ -1 +1,3 @@ -/esp-nuttx-bootloader \ No newline at end of file +/bootloader +/esp-hal-3rdparty +/esp-nuttx-bootloader diff --git a/arch/xtensa/src/esp32s2/Bootloader.mk b/arch/xtensa/src/esp32s2/Bootloader.mk index c516f7d174..54940e551d 100644 --- a/arch/xtensa/src/esp32s2/Bootloader.mk +++ b/arch/xtensa/src/esp32s2/Bootloader.mk @@ -18,18 +18,27 @@ # ############################################################################ +.PHONY: bootloader clean_bootloader + ifeq ($(CONFIG_ESP32S2_BOOTLOADER_BUILD_FROM_SOURCE),y) +TOOLSDIR = $(TOPDIR)/tools/espressif CHIPDIR = $(TOPDIR)/arch/xtensa/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 + +$(BOOTLOADER_DIR): + $(Q) mkdir -p $(BOOTLOADER_DIR) &>/dev/null # Helpers for creating the configuration file @@ -43,7 +52,7 @@ YELLOW = \033[1;33m BOLD = \033[1m RST = \033[0m -$(BOOTLOADER_CONFIG): $(TOPDIR)/.config +$(BOOTLOADER_CONFIG): $(TOPDIR)/.config $(BOOTLOADER_DIR) ifeq ($(CONFIG_ESP32S2_SECURE_BOOT),y) $(Q) if [ -z "$(ESPSEC_KEYDIR)" ]; then \ echo ""; \ @@ -86,13 +95,17 @@ ifeq ($(CONFIG_ESP32S2_APP_FORMAT_MCUBOOT),y) $(if $(CONFIG_ESP32S2_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC),$(call cfg_en,CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC)) \ $(if $(CONFIG_ESP32S2_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE),$(call cfg_en,CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE)) \ $(if $(CONFIG_ESP32S2_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_ESP32S2_OTA_PRIMARY_SLOT_OFFSET)) \ + $(call cfg_val,CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS,$(CONFIG_ESP32S2_OTA_PRIMARY_SLOT_OFFSET)) \ $(call cfg_val,CONFIG_ESP_APPLICATION_SIZE,$(CONFIG_ESP32S2_OTA_SLOT_SIZE)) \ - $(call cfg_val,CONFIG_ESP_APPLICATION_SECONDARY_START_ADDRESS,$(CONFIG_ESP32S2_OTA_SECONDARY_SLOT_OFFSET)) \ + $(call cfg_val,CONFIG_ESP_IMAGE0_SECONDARY_START_ADDRESS,$(CONFIG_ESP32S2_OTA_SECONDARY_SLOT_OFFSET)) \ $(call cfg_en,CONFIG_ESP_MCUBOOT_WDT_ENABLE) \ $(call cfg_val,CONFIG_ESP_SCRATCH_OFFSET,$(CONFIG_ESP32S2_OTA_SCRATCH_OFFSET)) \ $(call cfg_val,CONFIG_ESP_SCRATCH_SIZE,$(CONFIG_ESP32S2_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_ESP32S2_APP_FORMAT_LEGACY),y) $(Q) { \ @@ -107,12 +120,22 @@ ifeq ($(CONFIG_ESP32S2_APP_FORMAT_MCUBOOT),y) BOOTLOADER_BIN = $(TOPDIR)/mcuboot-esp32s2.bin BOOTLOADER_SIGNED_BIN = $(TOPDIR)/mcuboot-esp32s2.signed.bin -$(BOOTLOADER_BIN): $(BOOTLOADER_CONFIG) - $(Q) echo "Building Bootloader" - $(Q) $(BOOTLOADER_SRCDIR)/build_mcuboot.sh -c esp32s2 -s -f $(BOOTLOADER_CONFIG) - $(call COPYFILE, $(BOOTLOADER_SRCDIR)/$(BOOTLOADER_OUTDIR)/mcuboot-esp32s2.bin, $(TOPDIR)) +$(MCUBOOT_SRCDIR): $(BOOTLOADER_DIR) + $(Q) echo "Cloning MCUboot" + $(Q) git clone --quiet $(MCUBOOT_URL) $(MCUBOOT_SRCDIR) + $(Q) git -C "$(MCUBOOT_SRCDIR)" checkout --quiet $(CONFIG_ESP32S2_MCUBOOT_VERSION) + $(Q) git -C "$(MCUBOOT_SRCDIR)" submodule --quiet update --init --recursive ext/mbedtls -bootloader: $(BOOTLOADER_CONFIG) $(BOOTLOADER_SRCDIR) $(BOOTLOADER_BIN) +$(BOOTLOADER_BIN): chip/$(ESP_HAL_3RDPARTY_REPO) $(MCUBOOT_SRCDIR) $(BOOTLOADER_CONFIG) + $(Q) echo "Building Bootloader" + $(Q) $(TOOLSDIR)/build_mcuboot.sh \ + -c esp32s2 \ + -f $(BOOTLOADER_CONFIG) \ + -p $(BOOTLOADER_DIR) \ + -e $(HALDIR) + $(call COPYFILE, $(BOOTLOADER_DIR)/$(BOOTLOADER_OUTDIR)/mcuboot-esp32s2.bin, $(TOPDIR)) + +bootloader: $(BOOTLOADER_CONFIG) $(BOOTLOADER_BIN) ifeq ($(CONFIG_ESP32S2_SECURE_BOOT),y) $(eval KEYDIR := $(TOPDIR)/$(ESPSEC_KEYDIR)) $(eval BOOTLOADER_SIGN_KEY := $(abspath $(KEYDIR)/$(subst ",,$(CONFIG_ESP32S2_SECURE_BOOT_BOOTLOADER_SIGNING_KEY)))) @@ -137,13 +160,15 @@ endif endif clean_bootloader: - $(call DELDIR,$(BOOTLOADER_SRCDIR)) - $(call DELFILE,$(BOOTLOADER_CONFIG)) + $(call DELDIR,$(BOOTLOADER_DIR)) $(call DELFILE,$(BOOTLOADER_BIN)) $(if $(CONFIG_ESP32S2_SECURE_BOOT_BUILD_SIGNED_BINARIES),$(call DELFILE,$(BOOTLOADER_SIGNED_BIN))) else ifeq ($(CONFIG_ESP32S2_APP_FORMAT_LEGACY),y) +$(BOOTLOADER_SRCDIR): $(BOOTLOADER_DIR) + $(Q) git clone $(BOOTLOADER_URL) $(BOOTLOADER_SRCDIR) -b $(BOOTLOADER_VERSION) + bootloader: $(BOOTLOADER_SRCDIR) $(BOOTLOADER_CONFIG) $(Q) echo "Building Bootloader binaries" $(Q) $(BOOTLOADER_SRCDIR)/build_idfboot.sh -c esp32s2 -s -f $(BOOTLOADER_CONFIG) @@ -151,8 +176,7 @@ bootloader: $(BOOTLOADER_SRCDIR) $(BOOTLOADER_CONFIG) $(call COPYFILE,$(BOOTLOADER_SRCDIR)/$(BOOTLOADER_OUTDIR)/partition-table-esp32s2.bin,$(TOPDIR)) clean_bootloader: - $(call DELDIR,$(BOOTLOADER_SRCDIR)) - $(call DELFILE,$(BOOTLOADER_CONFIG)) + $(call DELDIR,$(BOOTLOADER_DIR)) $(call DELFILE,$(TOPDIR)/bootloader-esp32s2.bin) $(call DELFILE,$(TOPDIR)/partition-table-esp32s2.bin) diff --git a/arch/xtensa/src/esp32s2/Kconfig b/arch/xtensa/src/esp32s2/Kconfig index 3119a762a3..061303ca43 100644 --- a/arch/xtensa/src/esp32s2/Kconfig +++ b/arch/xtensa/src/esp32s2/Kconfig @@ -1074,6 +1074,11 @@ config ESP32S2_ESPTOOL_TARGET_SECONDARY endchoice +config ESP32S2_MCUBOOT_VERSION + string "MCUboot version" + default "b206b99b1555ca15f790a3287e57dc98ef3df2ac" + depends on ESP32S2_APP_FORMAT_MCUBOOT + config ESP32S2_APP_MCUBOOT_HEADER_SIZE int "Application image header size (in bytes)" default 32 diff --git a/arch/xtensa/src/esp32s2/Make.defs b/arch/xtensa/src/esp32s2/Make.defs index 1a61d1a5a4..ef8cd92ba0 100644 --- a/arch/xtensa/src/esp32s2/Make.defs +++ b/arch/xtensa/src/esp32s2/Make.defs @@ -18,7 +18,6 @@ # ############################################################################ -include chip/Bootloader.mk include common/Make.defs # The start-up, "head", file. May be either a .S or a .c file. @@ -118,3 +117,33 @@ ifeq ($(CONFIG_ESP32S2_SPIRAM),y) CHIP_CSRCS += esp32s2_spiram.c CHIP_CSRCS += esp32s2_psram.c 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)) diff --git a/boards/xtensa/esp32s2/esp32s2-saola-1/configs/mcuboot_nsh/defconfig b/boards/xtensa/esp32s2/esp32s2-saola-1/configs/mcuboot_nsh/defconfig index a0959c0c38..b4fc386845 100644 --- a/boards/xtensa/esp32s2/esp32s2-saola-1/configs/mcuboot_nsh/defconfig +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/configs/mcuboot_nsh/defconfig @@ -20,6 +20,7 @@ CONFIG_ARCH_XTENSA=y CONFIG_BOARD_LOOPSPERMSEC=16717 CONFIG_BUILTIN=y CONFIG_ESP32S2_APP_FORMAT_MCUBOOT=y +CONFIG_ESP32S2_BOOTLOADER_BUILD_FROM_SOURCE=y CONFIG_ESP32S2_SPIFLASH=y CONFIG_ESP32S2_UART0=y CONFIG_FS_PROCFS=y