@@ -124,6 +124,12 @@ String ESP8266HTTPUpdate::getLastErrorString(void) {
124124 return String (" Forbidden (403)" );
125125 case HTTP_UE_SERVER_WRONG_HTTP_CODE:
126126 return String (" Wrong HTTP code" );
127+ case HTTP_UE_SERVER_FAULTY_MD5:
128+ return String (" Faulty MD5" );
129+ case HTTP_UE_BIN_VERIFY_HEADER_FAILED:
130+ return String (" Verify bin header failed" );
131+ case HTTP_UE_BIN_FOR_WRONG_FLASH:
132+ return String (" bin for wrong flash size" );
127133 }
128134
129135 return String ();
@@ -232,6 +238,33 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
232238 DEBUG_HTTP_UPDATE (" [httpUpdate] runUpdate flash...\n " );
233239 }
234240
241+ uint8_t buf[4 ];
242+ if (tcp->peekBytes (&buf[0 ], 4 ) != 4 ) {
243+ DEBUG_HTTP_UPDATE (" [httpUpdate] peekBytes magic header failed\n " );
244+ lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
245+ http->end ();
246+ return HTTP_UPDATE_FAILED;
247+ }
248+
249+ // check for valid first magic byte
250+ if (buf[0 ] != 0xE9 ) {
251+ DEBUG_HTTP_UPDATE (" [httpUpdate] magic header not starts with 0xE9\n " );
252+ lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
253+ http->end ();
254+ return HTTP_UPDATE_FAILED;
255+
256+ }
257+
258+ uint32_t bin_flash_size = ESP.magicFlashChipSize ((buf[3 ] & 0xf0 ) >> 4 );
259+
260+ // check if new bin fits to SPI flash
261+ if (bin_flash_size > ESP.getFlashChipRealSize ()) {
262+ DEBUG_HTTP_UPDATE (" [httpUpdate] magic header, new bin not fits SPI Flash\n " );
263+ lastError = HTTP_UE_BIN_FOR_WRONG_FLASH;
264+ http->end ();
265+ return HTTP_UPDATE_FAILED;
266+ }
267+
235268 if (runUpdate (*tcp, len, http->header (" x-MD5" ), command)) {
236269 ret = HTTP_UPDATE_OK;
237270 DEBUG_HTTP_UPDATE (" [httpUpdate] Update ok\n " );
0 commit comments