Skip to content

Commit d5c43f0

Browse files
egemenertugruldevyte
egemenertugrul
authored andcommitted
Utilized UPDATE_ERROR_ERASE, added _setError function and refactored code (#4190)
* Added _setError function in the header file _setError function wraps a few lines to eliminate repetitiveness when debugging for errors. * Added _setError function _setError function wraps a few lines to eliminate repetitiveness when debugging for errors.
1 parent b08ff10 commit d5c43f0

File tree

2 files changed

+36
-46
lines changed

2 files changed

+36
-46
lines changed

cores/esp8266/Updater.cpp

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ bool UpdaterClass::begin(size_t size, int command) {
5252
*/
5353
int boot_mode = (GPI >> 16) & 0xf;
5454
if (boot_mode == 1) {
55-
_error = UPDATE_ERROR_BOOTSTRAP;
56-
#ifdef DEBUG_UPDATER
57-
printError(DEBUG_UPDATER);
58-
#endif
55+
_setError(UPDATE_ERROR_BOOTSTRAP);
5956
return false;
6057
}
6158

@@ -66,23 +63,17 @@ bool UpdaterClass::begin(size_t size, int command) {
6663
#endif
6764

6865
if(size == 0) {
69-
_error = UPDATE_ERROR_SIZE;
70-
#ifdef DEBUG_UPDATER
71-
printError(DEBUG_UPDATER);
72-
#endif
66+
_setError(UPDATE_ERROR_SIZE);
7367
return false;
7468
}
7569

7670
if(!ESP.checkFlashConfig(false)) {
77-
_error = UPDATE_ERROR_FLASH_CONFIG;
78-
#ifdef DEBUG_UPDATER
79-
printError(DEBUG_UPDATER);
80-
#endif
71+
_setError(UPDATE_ERROR_FLASH_CONFIG);
8172
return false;
8273
}
8374

8475
_reset();
85-
_error = 0;
76+
clearError(); // _error = 0
8677

8778
wifi_set_sleep_type(NONE_SLEEP_T);
8879

@@ -105,10 +96,7 @@ bool UpdaterClass::begin(size_t size, int command) {
10596

10697
//make sure that the size of both sketches is less than the total space (updateEndAddress)
10798
if(updateStartAddress < currentSketchSize) {
108-
_error = UPDATE_ERROR_SPACE;
109-
#ifdef DEBUG_UPDATER
110-
printError(DEBUG_UPDATER);
111-
#endif
99+
_setError(UPDATE_ERROR_SPACE);
112100
return false;
113101
}
114102
}
@@ -181,10 +169,7 @@ bool UpdaterClass::end(bool evenIfRemaining){
181169
_md5.calculate();
182170
if(_target_md5.length()) {
183171
if(_target_md5 != _md5.toString()){
184-
_error = UPDATE_ERROR_MD5;
185-
#ifdef DEBUG_UPDATER
186-
DEBUG_UPDATER.printf("MD5 Failed: expected:%s, calculated:%s\n", _target_md5.c_str(), _md5.toString().c_str());
187-
#endif
172+
_setError(UPDATE_ERROR_MD5);
188173
_reset();
189174
return false;
190175
}
@@ -194,9 +179,6 @@ bool UpdaterClass::end(bool evenIfRemaining){
194179
}
195180

196181
if(!_verifyEnd()) {
197-
#ifdef DEBUG_UPDATER
198-
printError(DEBUG_UPDATER);
199-
#endif
200182
_reset();
201183
return false;
202184
}
@@ -223,23 +205,24 @@ bool UpdaterClass::end(bool evenIfRemaining){
223205

224206
bool UpdaterClass::_writeBuffer(){
225207

226-
bool result = true;
208+
bool eraseResult = true, writeResult = true;
227209
if (_currentAddress % FLASH_SECTOR_SIZE == 0) {
228210
if(!_async) yield();
229-
result = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE);
211+
eraseResult = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE);
230212
}
231213

232-
if (result) {
214+
if (eraseResult) {
233215
if(!_async) yield();
234-
result = ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen);
216+
writeResult = ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen);
217+
} else { // if erase was unsuccessful
218+
_currentAddress = (_startAddress + _size);
219+
_setError(UPDATE_ERROR_ERASE);
220+
return false;
235221
}
236222

237-
if (!result) {
238-
_error = UPDATE_ERROR_WRITE;
223+
if (!writeResult) {
239224
_currentAddress = (_startAddress + _size);
240-
#ifdef DEBUG_UPDATER
241-
printError(DEBUG_UPDATER);
242-
#endif
225+
_setError(UPDATE_ERROR_WRITE);
243226
return false;
244227
}
245228
_md5.add(_buffer, _bufferLen);
@@ -255,7 +238,7 @@ size_t UpdaterClass::write(uint8_t *data, size_t len) {
255238
if(len > remaining()){
256239
//len = remaining();
257240
//fail instead
258-
_error = UPDATE_ERROR_SPACE;
241+
_setError(UPDATE_ERROR_SPACE);
259242
return 0;
260243
}
261244

@@ -287,8 +270,8 @@ bool UpdaterClass::_verifyHeader(uint8_t data) {
287270
if(_command == U_FLASH) {
288271
// check for valid first magic byte (is always 0xE9)
289272
if(data != 0xE9) {
290-
_error = UPDATE_ERROR_MAGIC_BYTE;
291273
_currentAddress = (_startAddress + _size);
274+
_setError(UPDATE_ERROR_MAGIC_BYTE);
292275
return false;
293276
}
294277
return true;
@@ -304,24 +287,24 @@ bool UpdaterClass::_verifyEnd() {
304287

305288
uint8_t buf[4];
306289
if(!ESP.flashRead(_startAddress, (uint32_t *) &buf[0], 4)) {
307-
_error = UPDATE_ERROR_READ;
308290
_currentAddress = (_startAddress);
291+
_setError(UPDATE_ERROR_READ);
309292
return false;
310293
}
311294

312295
// check for valid first magic byte
313296
if(buf[0] != 0xE9) {
314-
_error = UPDATE_ERROR_MAGIC_BYTE;
315297
_currentAddress = (_startAddress);
298+
_setError(UPDATE_ERROR_MAGIC_BYTE);
316299
return false;
317300
}
318301

319302
uint32_t bin_flash_size = ESP.magicFlashChipSize((buf[3] & 0xf0) >> 4);
320303

321304
// check if new bin fits to SPI flash
322305
if(bin_flash_size > ESP.getFlashChipRealSize()) {
323-
_error = UPDATE_ERROR_NEW_FLASH_CONFIG;
324306
_currentAddress = (_startAddress);
307+
_setError(UPDATE_ERROR_NEW_FLASH_CONFIG);
325308
return false;
326309
}
327310

@@ -353,11 +336,8 @@ size_t UpdaterClass::writeStream(Stream &data) {
353336
delay(100);
354337
toRead = data.readBytes(_buffer + _bufferLen, (_bufferSize - _bufferLen));
355338
if(toRead == 0) { //Timeout
356-
_error = UPDATE_ERROR_STREAM;
357339
_currentAddress = (_startAddress + _size);
358-
#ifdef DEBUG_UPDATER
359-
printError(DEBUG_UPDATER);
360-
#endif
340+
_setError(UPDATE_ERROR_STREAM);
361341
_reset();
362342
return written;
363343
}
@@ -371,6 +351,13 @@ size_t UpdaterClass::writeStream(Stream &data) {
371351
return written;
372352
}
373353

354+
void UpdaterClass::_setError(int error){
355+
_error = error;
356+
#ifdef DEBUG_UPDATER
357+
printError(DEBUG_UPDATER);
358+
#endif
359+
}
360+
374361
void UpdaterClass::printError(Print &out){
375362
out.printf_P(PSTR("ERROR[%u]: "), _error);
376363
if(_error == UPDATE_ERROR_OK){
@@ -388,7 +375,8 @@ void UpdaterClass::printError(Print &out){
388375
} else if(_error == UPDATE_ERROR_STREAM){
389376
out.println(F("Stream Read Timeout"));
390377
} else if(_error == UPDATE_ERROR_MD5){
391-
out.println(F("MD5 Check Failed"));
378+
//out.println(F("MD5 Check Failed"));
379+
out.printf("MD5 Failed: expected:%s, calculated:%s\n", _target_md5.c_str(), _md5.toString().c_str());
392380
} else if(_error == UPDATE_ERROR_FLASH_CONFIG){
393381
out.printf_P(PSTR("Flash config wrong real: %d IDE: %d\n"), ESP.getFlashChipRealSize(), ESP.getFlashChipSize());
394382
} else if(_error == UPDATE_ERROR_NEW_FLASH_CONFIG){
@@ -402,4 +390,4 @@ void UpdaterClass::printError(Print &out){
402390
}
403391
}
404392

405-
UpdaterClass Update;
393+
UpdaterClass Update;

cores/esp8266/Updater.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ class UpdaterClass {
6161
If all bytes are written
6262
this call will write the config to eboot
6363
and return true
64-
If there is already an update running but is not finished and !evenIfRemainanig
64+
If there is already an update running but is not finished and !evenIfRemaining
6565
or there is an error
6666
this will clear everything and return false
6767
the last error is available through getError()
68-
evenIfRemaining is helpfull when you update without knowing the final size first
68+
evenIfRemaining is helpful when you update without knowing the final size first
6969
*/
7070
bool end(bool evenIfRemaining = false);
7171

@@ -148,6 +148,8 @@ class UpdaterClass {
148148
bool _verifyHeader(uint8_t data);
149149
bool _verifyEnd();
150150

151+
void _setError(int error);
152+
151153
bool _async;
152154
uint8_t _error;
153155
uint8_t *_buffer;

0 commit comments

Comments
 (0)