Fix: #926 - Historical data lost when upgrading ESP8266 between 2.4.x versions

This commit is contained in:
Gunnar Skjold 2025-02-27 14:27:39 +01:00
parent a7324d828a
commit f02c951638
4 changed files with 37 additions and 25 deletions

View File

@ -17,6 +17,9 @@
#define AMS_PARTITION_MIN_SPIFFS_SIZE 0x20000
#elif defined(ESP8266)
#include <ESP8266HTTPClient.h>
#define AMS_FLASH_SKETCH_SIZE 0xFEFF0
#define AMS_FLASH_OTA_START AMS_FLASH_OTA_SIZE
#endif
#if defined(AMS_REMOTE_DEBUG)

View File

@ -1128,7 +1128,7 @@ bool AmsFirmwareUpdater::moveLittleFsFromApp1ToNew() {
}
#elif defined(ESP8266)
uintptr_t AmsFirmwareUpdater::getFirmwareUpdateStart() {
return FS_start - 0x40200000;
return (AMS_FLASH_SKETCH_SIZE + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
}
bool AmsFirmwareUpdater::isFlashReadyForNextUpdateVersion(uint32_t size) {
@ -1137,6 +1137,14 @@ bool AmsFirmwareUpdater::isFlashReadyForNextUpdateVersion(uint32_t size) {
#endif
debugger->printf_P(PSTR("Checking if we can upgrade\n"));
if(FS_PHYS_ADDR < (getFirmwareUpdateStart() + AMS_FLASH_SKETCH_SIZE)) {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("No room for OTA update\n"));
return false;
}
if(!ESP.checkFlashConfig(false)) {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
@ -1145,24 +1153,12 @@ bool AmsFirmwareUpdater::isFlashReadyForNextUpdateVersion(uint32_t size) {
return false;
}
//size of current sketch rounded to a sector
size_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
//size of the update rounded to a sector
size_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
//address of the end of the space available for sketch and update
uintptr_t updateEndAddress = FS_start - 0x40200000;
uintptr_t updateStartAddress = (updateEndAddress > roundedSize) ? (updateEndAddress - roundedSize) : 0;
//make sure that the size of both sketches is less than the total space (updateEndAddress)
if(updateStartAddress < currentSketchSize) {
if(size > AMS_FLASH_SKETCH_SIZE) {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("New firmware does not fit flash\n"));
return false;
return false;
}
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
@ -1180,14 +1176,28 @@ bool AmsFirmwareUpdater::writeBufferToFlash() {
uint32_t offset = updateStatus.block_position * UPDATE_BUF_SIZE;
uintptr_t currentAddress = getFirmwareUpdateStart() + offset;
uint32_t sector = currentAddress/FLASH_SECTOR_SIZE;
if(!ESP.flashEraseSector(sector)) {
if (currentAddress % FLASH_SECTOR_SIZE == 0) {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("flashEraseSector(%lu) failed\n"), sector);
updateStatus.errorCode = AMS_UPDATE_ERR_ERASE;
return false;
debugger->printf_P(PSTR("flashEraseSector(%lu)\n"), sector);
yield();
if(!ESP.flashEraseSector(sector)) {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("flashEraseSector(%lu) failed\n"), sector);
updateStatus.errorCode = AMS_UPDATE_ERR_ERASE;
return false;
}
}
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("flashWrite(%lu)\n"), sector);
yield();
if(!ESP.flashWrite(currentAddress, buf, UPDATE_BUF_SIZE)) {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))

View File

@ -89,9 +89,9 @@ void AmsWebServer::setup(AmsConfiguration* config, GpioConfig* gpioConfig, AmsDa
context = "";
} else {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Using context path: '%s'\n"), context.c_str());
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Using context path: '%s'\n"), context.c_str());
}
}
@ -814,7 +814,6 @@ void AmsWebServer::indexHtml() {
}
void AmsWebServer::indexCss() {
if(!checkSecurity(2))
return;

View File

@ -22,7 +22,7 @@ build_flags =
lib_deps = WiFi, Ethernet, ESPmDNS, WiFiClientSecure, HTTPClient, FS, WebServer, ESP32 Async UDP, ESP32SSDP, mulmer89/ESPRandom@1.5.0, ${common.lib_deps}, CloudConnector, SvelteUi
[env:esp8266]
platform = espressif8266@4.2.0
platform = espressif8266@4.2.1
framework = arduino
board = esp12e
board_build.ldscript = eagle.flash.4m2m.ld