@@ -172,6 +172,14 @@ bool UpdaterClass::end(bool evenIfRemaining){
172172#endif
173173 }
174174
175+ if (!_verifyEnd ()) {
176+ #ifdef DEBUG_UPDATER
177+ printError (DEBUG_UPDATER);
178+ #endif
179+ _reset ();
180+ return false ;
181+ }
182+
175183 if (_command == U_FLASH) {
176184 eboot_command ebcmd;
177185 ebcmd.action = ACTION_COPY_RAW;
@@ -246,12 +254,70 @@ size_t UpdaterClass::write(uint8_t *data, size_t len) {
246254 return len;
247255}
248256
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+
249307size_t UpdaterClass::writeStream (Stream &data) {
250308 size_t written = 0 ;
251309 size_t toRead = 0 ;
252310 if (hasError () || !isRunning ())
253311 return 0 ;
254312
313+ if (!_verifyHeader (data.peek ())) {
314+ #ifdef DEBUG_UPDATER
315+ printError (DEBUG_UPDATER);
316+ #endif
317+ _reset ();
318+ return 0 ;
319+ }
320+
255321 while (remaining ()) {
256322 toRead = data.readBytes (_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen));
257323 if (toRead == 0 ) { // Timeout
@@ -263,8 +329,9 @@ size_t UpdaterClass::writeStream(Stream &data) {
263329#ifdef DEBUG_UPDATER
264330 printError (DEBUG_UPDATER);
265331#endif
332+ _reset ();
333+ return written;
266334 }
267- return written;
268335 }
269336 _bufferLen += toRead;
270337 if ((_bufferLen == remaining () || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer ())
@@ -283,6 +350,8 @@ void UpdaterClass::printError(Stream &out){
283350 out.println (" Flash Write Failed" );
284351 } else if (_error == UPDATE_ERROR_ERASE){
285352 out.println (" Flash Erase Failed" );
353+ } else if (_error == UPDATE_ERROR_READ){
354+ out.println (" Flash Read Failed" );
286355 } else if (_error == UPDATE_ERROR_SPACE){
287356 out.println (" Not Enough Space" );
288357 } else if (_error == UPDATE_ERROR_SIZE){
@@ -293,6 +362,10 @@ void UpdaterClass::printError(Stream &out){
293362 out.println (" MD5 Check Failed" );
294363 } else if (_error == UPDATE_ERROR_FLASH_CONFIG){
295364 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" );
296369 } else {
297370 out.println (" UNKNOWN" );
298371 }
0 commit comments