Fix HTTPUpdate flash size check and add SPIFFS size check (#2161)

* Fix error in PR #2048: if ::available() is called before ::connect() _rxBuffer is not initialised

* Fixed flash size check and added SPIFFS size check

* Rewriting ESP.getFreeSketchSpace(), moving code from HTTPUpdate.cpp
This commit is contained in:
Jeroen88 2018-12-06 20:39:52 +01:00 committed by Me No Dev
parent bb7dea1566
commit 884e417a49
3 changed files with 29 additions and 8 deletions

View File

@ -169,7 +169,12 @@ uint32_t EspClass::getSketchSize () {
} }
uint32_t EspClass::getFreeSketchSpace () { uint32_t EspClass::getFreeSketchSpace () {
return sketchSize(SKETCH_SIZE_FREE); const esp_partition_t* _partition = esp_ota_get_next_update_partition(NULL);
if(!_partition){
return 0;
}
return _partition->size;
} }
uint8_t EspClass::getChipRevision(void) uint8_t EspClass::getChipRevision(void)

View File

@ -127,6 +127,8 @@ String HTTPUpdate::getLastErrorString(void)
return "Verify Bin Header Failed"; return "Verify Bin Header Failed";
case HTTP_UE_BIN_FOR_WRONG_FLASH: case HTTP_UE_BIN_FOR_WRONG_FLASH:
return "New Binary Does Not Fit Flash Size"; return "New Binary Does Not Fit Flash Size";
case HTTP_UE_NO_PARTITION:
return "Partition Could Not be Found";
} }
return String(); return String();
@ -238,14 +240,25 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
if(len > 0) { if(len > 0) {
bool startUpdate = true; bool startUpdate = true;
if(spiffs) { if(spiffs) {
// To do size_t spiffsSize = ((size_t) &_SPIFFS_end - (size_t) &_SPIFFS_start); const esp_partition_t* _partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, NULL);
// To do if(len > (int) spiffsSize) { if(!_partition){
// To do log_e("spiffsSize to low (%d) needed: %d\n", spiffsSize, len); _lastError = HTTP_UE_NO_PARTITION;
// To do startUpdate = false; return HTTP_UPDATE_FAILED;
// To do } }
if(len > _partition->size) {
log_e("spiffsSize to low (%d) needed: %d\n", _partition->size, len);
startUpdate = false;
}
} else { } else {
if(len > (int) ESP.getFreeSketchSpace()) { int sketchFreeSpace = ESP.getFreeSketchSpace();
log_e("FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len); if(!sketchFreeSpace){
_lastError = HTTP_UE_NO_PARTITION;
return HTTP_UPDATE_FAILED;
}
if(len > sketchFreeSpace) {
log_e("FreeSketchSpace to low (%d) needed: %d\n", sketchFreeSpace, len);
startUpdate = false; startUpdate = false;
} }
} }
@ -375,6 +388,8 @@ bool HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command)
} }
} }
// To do: the SHA256 could be checked if the server sends it
if(Update.writeStream(in) != size) { if(Update.writeStream(in) != size) {
_lastError = Update.getError(); _lastError = Update.getError();
Update.printError(error); Update.printError(error);

View File

@ -42,6 +42,7 @@
#define HTTP_UE_SERVER_FAULTY_MD5 (-105) #define HTTP_UE_SERVER_FAULTY_MD5 (-105)
#define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106) #define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106)
#define HTTP_UE_BIN_FOR_WRONG_FLASH (-107) #define HTTP_UE_BIN_FOR_WRONG_FLASH (-107)
#define HTTP_UE_NO_PARTITION (-108)
enum HTTPUpdateResult { enum HTTPUpdateResult {
HTTP_UPDATE_FAILED, HTTP_UPDATE_FAILED,