Skip to content

Commit cc0db8b

Browse files
committed
Merge pull request #1259 from Links2004/httpUpdate
add magic byte check for Update class
2 parents 4333634 + fa7f7bd commit cc0db8b

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

cores/esp8266/Updater.cpp

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
249307
size_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
}

cores/esp8266/Updater.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
#include "flash_utils.h"
66
#include "MD5Builder.h"
77

8-
#define UPDATE_ERROR_OK (0)
9-
#define UPDATE_ERROR_WRITE (1)
10-
#define UPDATE_ERROR_ERASE (2)
11-
#define UPDATE_ERROR_SPACE (3)
12-
#define UPDATE_ERROR_SIZE (4)
13-
#define UPDATE_ERROR_STREAM (5)
14-
#define UPDATE_ERROR_MD5 (6)
15-
#define UPDATE_ERROR_FLASH_CONFIG (7)
8+
#define UPDATE_ERROR_OK (0)
9+
#define UPDATE_ERROR_WRITE (1)
10+
#define UPDATE_ERROR_ERASE (2)
11+
#define UPDATE_ERROR_READ (3)
12+
#define UPDATE_ERROR_SPACE (4)
13+
#define UPDATE_ERROR_SIZE (5)
14+
#define UPDATE_ERROR_STREAM (6)
15+
#define UPDATE_ERROR_MD5 (7)
16+
#define UPDATE_ERROR_FLASH_CONFIG (8)
17+
#define UPDATE_ERROR_NEW_FLASH_CONFIG (9)
18+
#define UPDATE_ERROR_MAGIC_BYTE (10)
1619

1720

1821
#define U_FLASH 0
@@ -133,6 +136,9 @@ class UpdaterClass {
133136
void _reset();
134137
bool _writeBuffer();
135138

139+
bool _verifyHeader(uint8_t data);
140+
bool _verifyEnd();
141+
136142
uint8_t _error;
137143
uint8_t *_buffer;
138144
size_t _bufferLen;

0 commit comments

Comments
 (0)