diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index 7711e8fa58..3eca7b13a4 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -264,7 +264,6 @@ const int TIM_DIV265 __attribute__((deprecated, weak)) = TIM_DIV256; #include "HardwareSerial.h" #include "Esp.h" -#include "Updater.h" #include "debug.h" using std::min; diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index a21dc4bc0f..a4a619e457 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -534,41 +534,6 @@ uint32_t EspClass::getFreeSketchSpace() { return freeSpaceEnd - freeSpaceStart; } -bool EspClass::updateSketch(Stream& in, uint32_t size, bool restartOnFail, bool restartOnSuccess) { - if(!Update.begin(size)){ -#ifdef DEBUG_SERIAL - DEBUG_SERIAL.print("Update "); - Update.printError(DEBUG_SERIAL); -#endif - if(restartOnFail) ESP.restart(); - return false; - } - - if(Update.writeStream(in) != size){ -#ifdef DEBUG_SERIAL - DEBUG_SERIAL.print("Update "); - Update.printError(DEBUG_SERIAL); -#endif - if(restartOnFail) ESP.restart(); - return false; - } - - if(!Update.end()){ -#ifdef DEBUG_SERIAL - DEBUG_SERIAL.print("Update "); - Update.printError(DEBUG_SERIAL); -#endif - if(restartOnFail) ESP.restart(); - return false; - } - -#ifdef DEBUG_SERIAL - DEBUG_SERIAL.println("Update SUCCESS"); -#endif - if(restartOnSuccess) ESP.restart(); - return true; -} - static const int FLASH_INT_MASK = ((B10 << 8) | B00111010); bool EspClass::flashEraseSector(uint32_t sector) { diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index bc241eb392..bf3e6ec20a 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -192,7 +192,6 @@ class EspClass { uint32_t getSketchSize(); String getSketchMD5(); uint32_t getFreeSketchSpace(); - bool updateSketch(Stream& in, uint32_t size, bool restartOnFail = false, bool restartOnSuccess = true); String getResetReason(); String getResetInfo(); diff --git a/libraries/Updater/library.properties b/libraries/Updater/library.properties new file mode 100644 index 0000000000..b310464c60 --- /dev/null +++ b/libraries/Updater/library.properties @@ -0,0 +1,10 @@ +name=Updater +version=1.0 +author=me-no-dev +maintainer=me-no-dev +sentence=Flash Updater Back-end +paragraph=Manages the updating of firmware on the ESP8266. It is mainly used by ArduinoOTA and other similar online flash updating libraries +category=Other +url= +architectures=esp8266 +dot_a_linkage=true diff --git a/cores/esp8266/Updater.cpp b/libraries/Updater/src/Updater.cpp similarity index 94% rename from cores/esp8266/Updater.cpp rename to libraries/Updater/src/Updater.cpp index 2992d3c954..05e1272f54 100644 --- a/cores/esp8266/Updater.cpp +++ b/libraries/Updater/src/Updater.cpp @@ -1,6 +1,5 @@ #include "Updater.h" -#include "Arduino.h" -#include "eboot_command.h" +#include #include //#define DEBUG_UPDATER Serial @@ -11,16 +10,16 @@ #endif #if ARDUINO_SIGNING - #include "../../libraries/ESP8266WiFi/src/BearSSLHelpers.h" + #include "../../../libraries/ESP8266WiFi/src/BearSSLHelpers.h" static BearSSL::PublicKey signPubKey(signing_pubkey); static BearSSL::HashSHA256 hash; static BearSSL::SigningVerifier sign(&signPubKey); #endif extern "C" { - #include "c_types.h" - #include "spi_flash.h" - #include "user_interface.h" + #include + #include + #include } extern "C" uint32_t _FS_start; @@ -525,4 +524,39 @@ void UpdaterClass::printError(Print &out){ } } +bool UpdaterClass::updateSketch(Stream& in, uint32_t size, bool restartOnFail, bool restartOnSuccess) { + if(!begin(size)){ +#ifdef DEBUG_SERIAL + DEBUG_SERIAL.print("Update "); + printError(DEBUG_SERIAL); +#endif + if(restartOnFail) ESP.restart(); + return false; + } + + if(writeStream(in) != size){ +#ifdef DEBUG_SERIAL + DEBUG_SERIAL.print("Update "); + printError(DEBUG_SERIAL); +#endif + if(restartOnFail) ESP.restart(); + return false; + } + + if(!end()){ +#ifdef DEBUG_SERIAL + DEBUG_SERIAL.print("Update "); + printError(DEBUG_SERIAL); +#endif + if(restartOnFail) ESP.restart(); + return false; + } + +#ifdef DEBUG_SERIAL + DEBUG_SERIAL.println("Update SUCCESS"); +#endif + if(restartOnSuccess) ESP.restart(); + return true; +} + UpdaterClass Update; diff --git a/cores/esp8266/Updater.h b/libraries/Updater/src/Updater.h similarity index 98% rename from cores/esp8266/Updater.h rename to libraries/Updater/src/Updater.h index d47c613330..f58777316c 100644 --- a/cores/esp8266/Updater.h +++ b/libraries/Updater/src/Updater.h @@ -172,6 +172,8 @@ class UpdaterClass { return written; } + bool updateSketch(Stream& in, uint32_t size, bool restartOnFail = false, bool restartOnSuccess = true); + private: void _reset(); bool _writeBuffer(); diff --git a/cores/esp8266/Updater_Signing.h b/libraries/Updater/src/Updater_Signing.h similarity index 100% rename from cores/esp8266/Updater_Signing.h rename to libraries/Updater/src/Updater_Signing.h diff --git a/tests/host/Makefile b/tests/host/Makefile index 3768c11b5e..fa90e97a8e 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -240,9 +240,11 @@ $(OUTPUT_BINARY): $(CPP_OBJECTS_TESTS) $(BINDIR)/core.a ARDUINO_LIBS := \ $(addprefix $(CORE_PATH)/,\ IPAddress.cpp \ - Updater.cpp \ base64.cpp \ ) \ + $(addprefix ../../libraries/Updater/src/,\ + Updater.cpp \ + ) \ $(addprefix ../../libraries/ESP8266WiFi/src/,\ ESP8266WiFi.cpp \ ESP8266WiFiAP.cpp \