@@ -52,10 +52,7 @@ bool UpdaterClass::begin(size_t size, int command) {
52
52
*/
53
53
int boot_mode = (GPI >> 16 ) & 0xf ;
54
54
if (boot_mode == 1 ) {
55
- _error = UPDATE_ERROR_BOOTSTRAP;
56
- #ifdef DEBUG_UPDATER
57
- printError (DEBUG_UPDATER);
58
- #endif
55
+ _setError (UPDATE_ERROR_BOOTSTRAP);
59
56
return false ;
60
57
}
61
58
@@ -66,23 +63,17 @@ bool UpdaterClass::begin(size_t size, int command) {
66
63
#endif
67
64
68
65
if (size == 0 ) {
69
- _error = UPDATE_ERROR_SIZE;
70
- #ifdef DEBUG_UPDATER
71
- printError (DEBUG_UPDATER);
72
- #endif
66
+ _setError (UPDATE_ERROR_SIZE);
73
67
return false ;
74
68
}
75
69
76
70
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);
81
72
return false ;
82
73
}
83
74
84
75
_reset ();
85
- _error = 0 ;
76
+ clearError (); // _error = 0
86
77
87
78
wifi_set_sleep_type (NONE_SLEEP_T);
88
79
@@ -105,10 +96,7 @@ bool UpdaterClass::begin(size_t size, int command) {
105
96
106
97
// make sure that the size of both sketches is less than the total space (updateEndAddress)
107
98
if (updateStartAddress < currentSketchSize) {
108
- _error = UPDATE_ERROR_SPACE;
109
- #ifdef DEBUG_UPDATER
110
- printError (DEBUG_UPDATER);
111
- #endif
99
+ _setError (UPDATE_ERROR_SPACE);
112
100
return false ;
113
101
}
114
102
}
@@ -181,10 +169,7 @@ bool UpdaterClass::end(bool evenIfRemaining){
181
169
_md5.calculate ();
182
170
if (_target_md5.length ()) {
183
171
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);
188
173
_reset ();
189
174
return false ;
190
175
}
@@ -194,9 +179,6 @@ bool UpdaterClass::end(bool evenIfRemaining){
194
179
}
195
180
196
181
if (!_verifyEnd ()) {
197
- #ifdef DEBUG_UPDATER
198
- printError (DEBUG_UPDATER);
199
- #endif
200
182
_reset ();
201
183
return false ;
202
184
}
@@ -223,23 +205,24 @@ bool UpdaterClass::end(bool evenIfRemaining){
223
205
224
206
bool UpdaterClass::_writeBuffer (){
225
207
226
- bool result = true ;
208
+ bool eraseResult = true , writeResult = true ;
227
209
if (_currentAddress % FLASH_SECTOR_SIZE == 0 ) {
228
210
if (!_async) yield ();
229
- result = ESP.flashEraseSector (_currentAddress/FLASH_SECTOR_SIZE);
211
+ eraseResult = ESP.flashEraseSector (_currentAddress/FLASH_SECTOR_SIZE);
230
212
}
231
213
232
- if (result ) {
214
+ if (eraseResult ) {
233
215
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 ;
235
221
}
236
222
237
- if (!result) {
238
- _error = UPDATE_ERROR_WRITE;
223
+ if (!writeResult) {
239
224
_currentAddress = (_startAddress + _size);
240
- #ifdef DEBUG_UPDATER
241
- printError (DEBUG_UPDATER);
242
- #endif
225
+ _setError (UPDATE_ERROR_WRITE);
243
226
return false ;
244
227
}
245
228
_md5.add (_buffer, _bufferLen);
@@ -255,7 +238,7 @@ size_t UpdaterClass::write(uint8_t *data, size_t len) {
255
238
if (len > remaining ()){
256
239
// len = remaining();
257
240
// fail instead
258
- _error = UPDATE_ERROR_SPACE;
241
+ _setError ( UPDATE_ERROR_SPACE) ;
259
242
return 0 ;
260
243
}
261
244
@@ -287,8 +270,8 @@ bool UpdaterClass::_verifyHeader(uint8_t data) {
287
270
if (_command == U_FLASH) {
288
271
// check for valid first magic byte (is always 0xE9)
289
272
if (data != 0xE9 ) {
290
- _error = UPDATE_ERROR_MAGIC_BYTE;
291
273
_currentAddress = (_startAddress + _size);
274
+ _setError (UPDATE_ERROR_MAGIC_BYTE);
292
275
return false ;
293
276
}
294
277
return true ;
@@ -304,24 +287,24 @@ bool UpdaterClass::_verifyEnd() {
304
287
305
288
uint8_t buf[4 ];
306
289
if (!ESP.flashRead (_startAddress, (uint32_t *) &buf[0 ], 4 )) {
307
- _error = UPDATE_ERROR_READ;
308
290
_currentAddress = (_startAddress);
291
+ _setError (UPDATE_ERROR_READ);
309
292
return false ;
310
293
}
311
294
312
295
// check for valid first magic byte
313
296
if (buf[0 ] != 0xE9 ) {
314
- _error = UPDATE_ERROR_MAGIC_BYTE;
315
297
_currentAddress = (_startAddress);
298
+ _setError (UPDATE_ERROR_MAGIC_BYTE);
316
299
return false ;
317
300
}
318
301
319
302
uint32_t bin_flash_size = ESP.magicFlashChipSize ((buf[3 ] & 0xf0 ) >> 4 );
320
303
321
304
// check if new bin fits to SPI flash
322
305
if (bin_flash_size > ESP.getFlashChipRealSize ()) {
323
- _error = UPDATE_ERROR_NEW_FLASH_CONFIG;
324
306
_currentAddress = (_startAddress);
307
+ _setError (UPDATE_ERROR_NEW_FLASH_CONFIG);
325
308
return false ;
326
309
}
327
310
@@ -353,11 +336,8 @@ size_t UpdaterClass::writeStream(Stream &data) {
353
336
delay (100 );
354
337
toRead = data.readBytes (_buffer + _bufferLen, (_bufferSize - _bufferLen));
355
338
if (toRead == 0 ) { // Timeout
356
- _error = UPDATE_ERROR_STREAM;
357
339
_currentAddress = (_startAddress + _size);
358
- #ifdef DEBUG_UPDATER
359
- printError (DEBUG_UPDATER);
360
- #endif
340
+ _setError (UPDATE_ERROR_STREAM);
361
341
_reset ();
362
342
return written;
363
343
}
@@ -371,6 +351,13 @@ size_t UpdaterClass::writeStream(Stream &data) {
371
351
return written;
372
352
}
373
353
354
+ void UpdaterClass::_setError (int error){
355
+ _error = error;
356
+ #ifdef DEBUG_UPDATER
357
+ printError (DEBUG_UPDATER);
358
+ #endif
359
+ }
360
+
374
361
void UpdaterClass::printError (Print &out){
375
362
out.printf_P (PSTR (" ERROR[%u]: " ), _error);
376
363
if (_error == UPDATE_ERROR_OK){
@@ -388,7 +375,8 @@ void UpdaterClass::printError(Print &out){
388
375
} else if (_error == UPDATE_ERROR_STREAM){
389
376
out.println (F (" Stream Read Timeout" ));
390
377
} 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 ());
392
380
} else if (_error == UPDATE_ERROR_FLASH_CONFIG){
393
381
out.printf_P (PSTR (" Flash config wrong real: %d IDE: %d\n " ), ESP.getFlashChipRealSize (), ESP.getFlashChipSize ());
394
382
} else if (_error == UPDATE_ERROR_NEW_FLASH_CONFIG){
@@ -402,4 +390,4 @@ void UpdaterClass::printError(Print &out){
402
390
}
403
391
}
404
392
405
- UpdaterClass Update;
393
+ UpdaterClass Update;
0 commit comments