Skip to content

Commit 5a4ced2

Browse files
committed
add check for magic header of bin before update is started.
see: #1250
1 parent 5333ebf commit 5a4ced2

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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");

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@
3939
#endif
4040

4141
/// note we use HTTP client errors too so we start at 100
42-
#define HTTP_UE_TOO_LESS_SPACE (-100)
43-
#define HTTP_UE_SERVER_NOT_REPORT_SIZE (-101)
44-
#define HTTP_UE_SERVER_FILE_NOT_FOUND (-102)
45-
#define HTTP_UE_SERVER_FORBIDDEN (-103)
46-
#define HTTP_UE_SERVER_WRONG_HTTP_CODE (-104)
47-
#define HTTP_UE_SERVER_FAULTY_MD5 (-105)
42+
#define HTTP_UE_TOO_LESS_SPACE (-100)
43+
#define HTTP_UE_SERVER_NOT_REPORT_SIZE (-101)
44+
#define HTTP_UE_SERVER_FILE_NOT_FOUND (-102)
45+
#define HTTP_UE_SERVER_FORBIDDEN (-103)
46+
#define HTTP_UE_SERVER_WRONG_HTTP_CODE (-104)
47+
#define HTTP_UE_SERVER_FAULTY_MD5 (-105)
48+
#define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106)
49+
#define HTTP_UE_BIN_FOR_WRONG_FLASH (-107)
4850

4951
typedef enum {
5052
HTTP_UPDATE_FAILED,

0 commit comments

Comments
 (0)