From 9288f411f97da2b9d4a8b246bf577e07be59bde0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 11 Jun 2018 23:57:37 +0200 Subject: [PATCH] Do not write to EEPROM (rotating) after OTA to prevent image corruption --- README.md | 2 +- code/espurna/ota.ino | 20 ++++++++++++++++++-- code/espurna/web.ino | 10 +++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b5d79f23..34df84ac 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ESPurna ("spark" in Catalan) is a custom firmware for ESP8285/ESP8266 based smart switches, lights and sensors. It uses the Arduino Core for ESP8266 framework and a number of 3rd party libraries. -[![version](https://img.shields.io/badge/version-1.12.7a-brightgreen.svg)](CHANGELOG.md) +[![version](https://img.shields.io/badge/version-1.13.0b-brightgreen.svg)](CHANGELOG.md) [![branch](https://img.shields.io/badge/branch-dev-orange.svg)](https://github.org/xoseperez/espurna/tree/dev/) [![travis](https://travis-ci.org/xoseperez/espurna.svg?branch=dev)](https://travis-ci.org/xoseperez/espurna) [![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/dev.svg)](https://www.codacy.com/app/xoseperez/espurna/dashboard) diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino index 172becd5..23559aaf 100644 --- a/code/espurna/ota.ino +++ b/code/espurna/ota.ino @@ -65,7 +65,8 @@ void _otaFrom(const char * host, unsigned int port, const char * url) { if (Update.end(true)){ DEBUG_MSG_P(PSTR("[OTA] Success: %u bytes\n"), _ota_size); - deferredReset(100, CUSTOM_RESET_OTA); + nice_delay(100); + ESP.restart(); } else { #ifdef DEBUG_PORT Update.printError(DEBUG_PORT); @@ -133,6 +134,13 @@ void _otaFrom(const char * host, unsigned int port, const char * url) { } #endif + // Cache current reset reason + resetReason(); + + // Set reset reason beforehand, + // to prevent writing to EEPROM after the upgrade + resetReason(CUSTOM_RESET_OTA); + // Backup EEPROM data to last sector eepromBackup(); @@ -217,6 +225,13 @@ void otaSetup() { ArduinoOTA.onStart([]() { + // Cache current reset reason + resetReason(); + + // Set reset reason beforehand, + // to prevent writing to EEPROM after the upgrade + resetReason(CUSTOM_RESET_OTA); + // Backup EEPROM data to last sector eepromBackup(); @@ -234,7 +249,8 @@ void otaSetup() { #if WEB_SUPPORT wsSend_P(PSTR("{\"action\": \"reload\"}")); #endif - deferredReset(100, CUSTOM_RESET_OTA); + nice_delay(100); + ESP.restart(); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 301bd792..09255f54 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -224,7 +224,8 @@ void _onUpgrade(AsyncWebServerRequest *request) { AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", buffer); response->addHeader("Connection", "close"); if (!Update.hasError()) { - deferredReset(100, CUSTOM_RESET_UPGRADE); + nice_delay(100); + ESP.restart(); } request->send(response); @@ -234,6 +235,13 @@ void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t inde if (!index) { + // Cache current reset reason + resetReason(); + + // Set reset reason beforehand, + // to prevent writing to EEPROM after the upgrade + resetReason(CUSTOM_RESET_UPGRADE); + // Backup EEPROM data to last sector eepromBackup();