@@ -172,6 +172,14 @@ bool UpdaterClass::end(bool evenIfRemaining){
172
172
#endif
173
173
}
174
174
175
+ if (!_verifyEnd ()) {
176
+ #ifdef DEBUG_UPDATER
177
+ printError (DEBUG_UPDATER);
178
+ #endif
179
+ _reset ();
180
+ return false ;
181
+ }
182
+
175
183
if (_command == U_FLASH) {
176
184
eboot_command ebcmd;
177
185
ebcmd.action = ACTION_COPY_RAW;
@@ -246,12 +254,70 @@ size_t UpdaterClass::write(uint8_t *data, size_t len) {
246
254
return len;
247
255
}
248
256
257
+ bool UpdaterClass::_verifyHeader (uint8_t data) {
258
+ if (_command == U_FLASH) {
259
+ // check for valid first magic byte (is always 0xE9)
260
+ if (data != 0xE9 ) {
261
+ _error = UPDATE_ERROR_MAGIC_BYTE;
262
+ _currentAddress = (_startAddress + _size);
263
+ return false ;
264
+ }
265
+ return true ;
266
+ } else if (_command == U_SPIFFS) {
267
+ // no check of SPIFFS possible with first byte.
268
+ return true ;
269
+ }
270
+ return false ;
271
+ }
272
+
273
+ bool UpdaterClass::_verifyEnd () {
274
+ if (_command == U_FLASH) {
275
+
276
+ uint8_t buf[4 ];
277
+ if (!ESP.flashRead (_startAddress, (uint32_t *) &buf[0 ], 4 )) {
278
+ _error = UPDATE_ERROR_READ;
279
+ _currentAddress = (_startAddress);
280
+ return false ;
281
+ }
282
+
283
+ // check for valid first magic byte
284
+ if (buf[0 ] != 0xE9 ) {
285
+ _error = UPDATE_ERROR_MAGIC_BYTE;
286
+ _currentAddress = (_startAddress);
287
+ return false ;
288
+ }
289
+
290
+ uint32_t bin_flash_size = ESP.magicFlashChipSize ((buf[3 ] & 0xf0 ) >> 4 );
291
+
292
+ // check if new bin fits to SPI flash
293
+ if (bin_flash_size > ESP.getFlashChipRealSize ()) {
294
+ _error = UPDATE_ERROR_NEW_FLASH_CONFIG;
295
+ _currentAddress = (_startAddress);
296
+ return false ;
297
+ }
298
+
299
+ return true ;
300
+ } else if (_command == U_SPIFFS) {
301
+ // SPIFFS is already over written checks make no sense any more.
302
+ return true ;
303
+ }
304
+ return false ;
305
+ }
306
+
249
307
size_t UpdaterClass::writeStream (Stream &data) {
250
308
size_t written = 0 ;
251
309
size_t toRead = 0 ;
252
310
if (hasError () || !isRunning ())
253
311
return 0 ;
254
312
313
+ if (!_verifyHeader (data.peek ())) {
314
+ #ifdef DEBUG_UPDATER
315
+ printError (DEBUG_UPDATER);
316
+ #endif
317
+ _reset ();
318
+ return 0 ;
319
+ }
320
+
255
321
while (remaining ()) {
256
322
toRead = data.readBytes (_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen));
257
323
if (toRead == 0 ) { // Timeout
@@ -263,8 +329,9 @@ size_t UpdaterClass::writeStream(Stream &data) {
263
329
#ifdef DEBUG_UPDATER
264
330
printError (DEBUG_UPDATER);
265
331
#endif
332
+ _reset ();
333
+ return written;
266
334
}
267
- return written;
268
335
}
269
336
_bufferLen += toRead;
270
337
if ((_bufferLen == remaining () || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer ())
@@ -283,6 +350,8 @@ void UpdaterClass::printError(Stream &out){
283
350
out.println (" Flash Write Failed" );
284
351
} else if (_error == UPDATE_ERROR_ERASE){
285
352
out.println (" Flash Erase Failed" );
353
+ } else if (_error == UPDATE_ERROR_READ){
354
+ out.println (" Flash Read Failed" );
286
355
} else if (_error == UPDATE_ERROR_SPACE){
287
356
out.println (" Not Enough Space" );
288
357
} else if (_error == UPDATE_ERROR_SIZE){
@@ -293,6 +362,10 @@ void UpdaterClass::printError(Stream &out){
293
362
out.println (" MD5 Check Failed" );
294
363
} else if (_error == UPDATE_ERROR_FLASH_CONFIG){
295
364
out.printf (" Flash config wrong real: %d IDE: %d\n " , ESP.getFlashChipRealSize (), ESP.getFlashChipSize ());
365
+ } else if (_error == UPDATE_ERROR_NEW_FLASH_CONFIG){
366
+ out.printf (" new Flash config wrong real: %d\n " , ESP.getFlashChipRealSize ());
367
+ } else if (_error == UPDATE_ERROR_MAGIC_BYTE){
368
+ out.println (" Magic byte is wrong, not 0xE9" );
296
369
} else {
297
370
out.println (" UNKNOWN" );
298
371
}
0 commit comments