@@ -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,19 +254,67 @@ 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
255- // check for valid first magic byte (is always 0xE9)
256- if (data.peek () != 0xE9 ) {
257- _error = UPDATE_ERROR_MAGIC_BYTE;
258- _currentAddress = (_startAddress + _size);
313+ if (!_verifyHeader (data.peek ())) {
259314#ifdef DEBUG_UPDATER
260315 printError (DEBUG_UPDATER);
261316#endif
317+ _reset ();
262318 return 0 ;
263319 }
264320
@@ -273,8 +329,9 @@ size_t UpdaterClass::writeStream(Stream &data) {
273329#ifdef DEBUG_UPDATER
274330 printError (DEBUG_UPDATER);
275331#endif
332+ _reset ();
333+ return written;
276334 }
277- return written;
278335 }
279336 _bufferLen += toRead;
280337 if ((_bufferLen == remaining () || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer ())
@@ -293,6 +350,8 @@ void UpdaterClass::printError(Stream &out){
293350 out.println (" Flash Write Failed" );
294351 } else if (_error == UPDATE_ERROR_ERASE){
295352 out.println (" Flash Erase Failed" );
353+ } else if (_error == UPDATE_ERROR_READ){
354+ out.println (" Flash Read Failed" );
296355 } else if (_error == UPDATE_ERROR_SPACE){
297356 out.println (" Not Enough Space" );
298357 } else if (_error == UPDATE_ERROR_SIZE){
@@ -303,6 +362,8 @@ void UpdaterClass::printError(Stream &out){
303362 out.println (" MD5 Check Failed" );
304363 } else if (_error == UPDATE_ERROR_FLASH_CONFIG){
305364 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 ());
306367 } else if (_error == UPDATE_ERROR_MAGIC_BYTE){
307368 out.println (" Magic byte is wrong, not 0xE9" );
308369 } else {
0 commit comments