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 #define AMS_PARTITION_MIN_SPIFFS_SIZE 0x20000
#elif defined(ESP8266) #elif defined(ESP8266)
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#define AMS_FLASH_SKETCH_SIZE 0xFEFF0
#define AMS_FLASH_OTA_START AMS_FLASH_OTA_SIZE
#endif #endif
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)

View File

@ -1128,7 +1128,7 @@ bool AmsFirmwareUpdater::moveLittleFsFromApp1ToNew() {
} }
#elif defined(ESP8266) #elif defined(ESP8266)
uintptr_t AmsFirmwareUpdater::getFirmwareUpdateStart() { 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) { bool AmsFirmwareUpdater::isFlashReadyForNextUpdateVersion(uint32_t size) {
@ -1137,6 +1137,14 @@ bool AmsFirmwareUpdater::isFlashReadyForNextUpdateVersion(uint32_t size) {
#endif #endif
debugger->printf_P(PSTR("Checking if we can upgrade\n")); 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(!ESP.checkFlashConfig(false)) {
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR)) if (debugger->isActive(RemoteDebug::ERROR))
@ -1145,19 +1153,7 @@ bool AmsFirmwareUpdater::isFlashReadyForNextUpdateVersion(uint32_t size) {
return false; return false;
} }
//size of current sketch rounded to a sector if(size > AMS_FLASH_SKETCH_SIZE) {
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 defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR)) if (debugger->isActive(RemoteDebug::ERROR))
#endif #endif
@ -1180,6 +1176,13 @@ bool AmsFirmwareUpdater::writeBufferToFlash() {
uint32_t offset = updateStatus.block_position * UPDATE_BUF_SIZE; uint32_t offset = updateStatus.block_position * UPDATE_BUF_SIZE;
uintptr_t currentAddress = getFirmwareUpdateStart() + offset; uintptr_t currentAddress = getFirmwareUpdateStart() + offset;
uint32_t sector = currentAddress/FLASH_SECTOR_SIZE; uint32_t sector = currentAddress/FLASH_SECTOR_SIZE;
if (currentAddress % FLASH_SECTOR_SIZE == 0) {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("flashEraseSector(%lu)\n"), sector);
yield();
if(!ESP.flashEraseSector(sector)) { if(!ESP.flashEraseSector(sector)) {
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR)) if (debugger->isActive(RemoteDebug::ERROR))
@ -1188,6 +1191,13 @@ bool AmsFirmwareUpdater::writeBufferToFlash() {
updateStatus.errorCode = AMS_UPDATE_ERR_ERASE; updateStatus.errorCode = AMS_UPDATE_ERR_ERASE;
return false; 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(!ESP.flashWrite(currentAddress, buf, UPDATE_BUF_SIZE)) {
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR)) if (debugger->isActive(RemoteDebug::ERROR))

View File

@ -89,9 +89,9 @@ void AmsWebServer::setup(AmsConfiguration* config, GpioConfig* gpioConfig, AmsDa
context = ""; context = "";
} else { } else {
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO)) if (debugger->isActive(RemoteDebug::INFO))
#endif #endif
debugger->printf_P(PSTR("Using context path: '%s'\n"), context.c_str()); debugger->printf_P(PSTR("Using context path: '%s'\n"), context.c_str());
} }
} }
@ -814,7 +814,6 @@ void AmsWebServer::indexHtml() {
} }
void AmsWebServer::indexCss() { void AmsWebServer::indexCss() {
if(!checkSecurity(2)) if(!checkSecurity(2))
return; 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 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] [env:esp8266]
platform = espressif8266@4.2.0 platform = espressif8266@4.2.1
framework = arduino framework = arduino
board = esp12e board = esp12e
board_build.ldscript = eagle.flash.4m2m.ld board_build.ldscript = eagle.flash.4m2m.ld