From b9029a20185b9f69a27f03b0c6353f3153775f4a Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 18 Mar 2024 09:56:15 +0000 Subject: [PATCH] Fix esp8266 bootloader building and add diagnostic tools (#2734) This PR focuses on an issue discovered in #2727 when attempting to update the esp8266 boot partition from code during a firmware update. It was discovered that doing this renders the device unbootable because the boot sector header information is incorrect. This is because the required `SPI_SIZE`, `SPI_MODE` and `SPI_FREQ` values were never passed to the rboot makefile when building. When flashing the boot partition normally, these parameters get passed to esptool, which modifies the provided boot image. I wasn't aware until now that it did this! **Build rboot as variant** The solution to the main problem is to build rboot as a variant - see https://sming.readthedocs.io/en/latest/_inc/Sming/building.html#library-variants. The location of the bootloader firmware (in `out/Esp8266/debug/firmware/rboot.bin`) is unchanged, but is overwritten by the appropriate variant after every build. This should ensure consistency with selected settings. **Remove RBOOT_INTEGRATION** As we've forked rboot some time ago this isn't necessary, so we can simplify the code a bit. **Update esptool to v4.6.2 (from v4.4)** Version 4.7 has issues with earlier python versions and breaks with IDF 4.4/4.3. **Add `bootinfo` build target** Adds `make bootinfo` for esp architectures which shows details of the boot partition, including header fields so we can check it's correct. **Add `esptool` build target** Makes for easier access to esptool. --- Sming/Arch/Esp32/app.mk | 8 ++++ Sming/Components/esptool/component.mk | 8 ++++ Sming/Components/esptool/esptool | 2 +- Sming/Components/rboot/component.mk | 44 ++++++++++++++----- .../rboot/include/rboot-integration.h | 13 ------ Sming/Components/rboot/rboot | 2 +- 6 files changed, 51 insertions(+), 26 deletions(-) delete mode 100644 Sming/Components/rboot/include/rboot-integration.h diff --git a/Sming/Arch/Esp32/app.mk b/Sming/Arch/Esp32/app.mk index 8fc7a3c151..32f75955d9 100644 --- a/Sming/Arch/Esp32/app.mk +++ b/Sming/Arch/Esp32/app.mk @@ -38,3 +38,11 @@ endif $(TARGET_BIN): $(TARGET_OUT) $(Q) $(ESPTOOL_CMDLINE) elf2image --min-rev $(CHIP_REV_MIN) --elf-sha256-offset 0xb0 $(ESPTOOL_EXTRA_ARGS) $(flashimageoptions) -o $@ $< + + +##@Flashing + +.PHONY: bootinfo +bootinfo: $(FLASH_BOOT_LOADER) ##Show bootloader information + $(info $(FLASH_BOOT_LOADER):) + $(Q) $(ESPTOOL_CMDLINE) image_info -v2 $(FLASH_BOOT_LOADER) diff --git a/Sming/Components/esptool/component.mk b/Sming/Components/esptool/component.mk index 3f82c8e219..330f82a5b1 100644 --- a/Sming/Components/esptool/component.mk +++ b/Sming/Components/esptool/component.mk @@ -86,3 +86,11 @@ endef define EraseFlash $(call ESPTOOL_EXECUTE,erase_flash) endef + + +##@Flashing + +.PHONY: esptool +esptool: ##Pass options to esptool, e.g. `make esptool -- --help` or `make esptool image_info` + $(Q) $(ESPTOOL_CMDLINE) $(filter-out $@,$(MAKECMDGOALS)) + diff --git a/Sming/Components/esptool/esptool b/Sming/Components/esptool/esptool index b1fce755be..8c5f47f8d0 160000 --- a/Sming/Components/esptool/esptool +++ b/Sming/Components/esptool/esptool @@ -1 +1 @@ -Subproject commit b1fce755be2f2c66161e309649aefa4b21604de9 +Subproject commit 8c5f47f8d0be6bbfc2668791653fc8af75f3cd2b diff --git a/Sming/Components/rboot/component.mk b/Sming/Components/rboot/component.mk index a11795b443..6670b0f5af 100644 --- a/Sming/Components/rboot/component.mk +++ b/Sming/Components/rboot/component.mk @@ -30,6 +30,17 @@ endif # => APP +# Build bootloader variant based on these variable values +RBOOT_VARS := \ + PARTITION_TABLE_OFFSET \ + RBOOT_RTC_ENABLED \ + RBOOT_GPIO_ENABLED \ + RBOOT_GPIO_SKIP_ENABLED \ + RBOOT_SILENT \ + SPI_SPEED \ + SPI_MODE \ + SPI_SIZE + # rBoot options CONFIG_VARS += RBOOT_RTC_ENABLED RBOOT_GPIO_ENABLED RBOOT_GPIO_SKIP_ENABLED RBOOT_RTC_ENABLED ?= 0 @@ -102,12 +113,6 @@ RBOOT_ROM_1_BIN := $(FW_BASE)/$(RBOOT_ROM_1).bin COMPONENT_APPCODE := rboot/appcode -APP_CFLAGS += -DRBOOT_INTEGRATION - -# these are exported for use by the rBoot Makefile -export RBOOT_BUILD_BASE := $(abspath $(COMPONENT_BUILD_DIR)) -export RBOOT_FW_BASE := $(abspath $(FW_BASE)) -export ESPTOOL2 # multiple roms per 1mb block? ifeq ($(RBOOT_TWO_ROMS),1) @@ -143,12 +148,20 @@ ifdef RBOOT_EMULATION FLASH_BOOT_CHUNKS = 0x00000=$(BLANK_BIN) FLASH_RBOOT_ERASE_CONFIG_CHUNKS := 0x01000=$(BLANK_BIN) else -export RBOOT_ROM0_ADDR -export RBOOT_ROM1_ADDR +export ESPTOOL2 +export $(RBOOT_VARS) +RBOOT_VARSTR := $(foreach v,$(RBOOT_VARS),$v=$($v)) +RBOOT_HASH := $(call CalculateVariantHash,RBOOT_VARSTR) +export RBOOT_BUILD_BASE := $(COMPONENT_BUILD_BASE)/$(RBOOT_HASH) +RBOOT_VAR_BIN := $(RBOOT_BUILD_BASE)/rboot.bin RBOOT_BIN := $(FW_BASE)/rboot.bin -CUSTOM_TARGETS += $(RBOOT_BIN) -$(RBOOT_BIN): - $(Q) $(MAKE) -C $(RBOOT_DIR)/rboot PARTITION_TABLE_OFFSET=$(PARTITION_TABLE_OFFSET) $(RBOOT_CFLAGS) +CUSTOM_TARGETS += build_rboot +$(RBOOT_VAR_BIN): $(ESPTOOL2) + $(Q) $(MAKE) -C $(RBOOT_DIR)/rboot $(RBOOT_CFLAGS) + +.PHONY: build_rboot +build_rboot: $(RBOOT_VAR_BIN) + cp $(RBOOT_VAR_BIN) $(RBOOT_BIN) EXTRA_LDFLAGS := -u Cache_Read_Enable_New @@ -207,4 +220,13 @@ $(RBOOT_ROM_1_BIN): $(TARGET_OUT_1) endif + +##@Flashing + +.PHONY: bootinfo +bootinfo: ##Show bootloader information + $(info $(RBOOT_BIN):) + $(Q) $(ESPTOOL_CMDLINE) image_info -v2 $(RBOOT_BIN) + + endif # RBOOT_EMULATION diff --git a/Sming/Components/rboot/include/rboot-integration.h b/Sming/Components/rboot/include/rboot-integration.h deleted file mode 100644 index f67a97b8d1..0000000000 --- a/Sming/Components/rboot/include/rboot-integration.h +++ /dev/null @@ -1,13 +0,0 @@ -/**** - * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. - * Created 2015 by Skurydin Alexey - * http://github.com/SmingHub/Sming - * All files of the Sming Core are provided under the LGPL v3 license. - * - * rboot-integration.h - * - ****/ - -#pragma once - -#include diff --git a/Sming/Components/rboot/rboot b/Sming/Components/rboot/rboot index 4159458fe2..b397c5b0c7 160000 --- a/Sming/Components/rboot/rboot +++ b/Sming/Components/rboot/rboot @@ -1 +1 @@ -Subproject commit 4159458fe25f93399a204e73d99331c37536f288 +Subproject commit b397c5b0c75f911b2fe1ebdde20d1567ab42f756