Skip to content

Commit fa7f7bd

Browse files
committed
add _verifyHeader and _verifyEnd
_verifyHeader is called before the beginning of the update progress to verify the first byte using peek _verifyEnd is called on the end before the eboot command is written to verify first byte + flash config add missing _reset() on timeout
1 parent d5e0c7e commit fa7f7bd

File tree

2 files changed

+80
-14
lines changed

2 files changed

+80
-14
lines changed

cores/esp8266/Updater.cpp

Lines changed: 66 additions & 5 deletions
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,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+
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

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 {

cores/esp8266/Updater.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +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)
16-
#define UPDATE_ERROR_MAGIC_BYTE (8)
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)
1719

1820

1921
#define U_FLASH 0
@@ -134,6 +136,9 @@ class UpdaterClass {
134136
void _reset();
135137
bool _writeBuffer();
136138

139+
bool _verifyHeader(uint8_t data);
140+
bool _verifyEnd();
141+
137142
uint8_t _error;
138143
uint8_t *_buffer;
139144
size_t _bufferLen;

0 commit comments

Comments
 (0)