1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Merge pull request #1259 from Links2004/httpUpdate

add magic byte check for Update class
This commit is contained in:
Markus 2015-12-20 13:08:05 +01:00
commit cc0db8b6a9
2 changed files with 88 additions and 9 deletions

View File

@ -172,6 +172,14 @@ bool UpdaterClass::end(bool evenIfRemaining){
#endif
}
if(!_verifyEnd()) {
#ifdef DEBUG_UPDATER
printError(DEBUG_UPDATER);
#endif
_reset();
return false;
}
if (_command == U_FLASH) {
eboot_command ebcmd;
ebcmd.action = ACTION_COPY_RAW;
@ -246,12 +254,70 @@ size_t UpdaterClass::write(uint8_t *data, size_t len) {
return len;
}
bool UpdaterClass::_verifyHeader(uint8_t data) {
if(_command == U_FLASH) {
// check for valid first magic byte (is always 0xE9)
if(data != 0xE9) {
_error = UPDATE_ERROR_MAGIC_BYTE;
_currentAddress = (_startAddress + _size);
return false;
}
return true;
} else if(_command == U_SPIFFS) {
// no check of SPIFFS possible with first byte.
return true;
}
return false;
}
bool UpdaterClass::_verifyEnd() {
if(_command == U_FLASH) {
uint8_t buf[4];
if(!ESP.flashRead(_startAddress, (uint32_t *) &buf[0], 4)) {
_error = UPDATE_ERROR_READ;
_currentAddress = (_startAddress);
return false;
}
// check for valid first magic byte
if(buf[0] != 0xE9) {
_error = UPDATE_ERROR_MAGIC_BYTE;
_currentAddress = (_startAddress);
return false;
}
uint32_t bin_flash_size = ESP.magicFlashChipSize((buf[3] & 0xf0) >> 4);
// check if new bin fits to SPI flash
if(bin_flash_size > ESP.getFlashChipRealSize()) {
_error = UPDATE_ERROR_NEW_FLASH_CONFIG;
_currentAddress = (_startAddress);
return false;
}
return true;
} else if(_command == U_SPIFFS) {
// SPIFFS is already over written checks make no sense any more.
return true;
}
return false;
}
size_t UpdaterClass::writeStream(Stream &data) {
size_t written = 0;
size_t toRead = 0;
if(hasError() || !isRunning())
return 0;
if(!_verifyHeader(data.peek())) {
#ifdef DEBUG_UPDATER
printError(DEBUG_UPDATER);
#endif
_reset();
return 0;
}
while(remaining()) {
toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen));
if(toRead == 0) { //Timeout
@ -263,8 +329,9 @@ size_t UpdaterClass::writeStream(Stream &data) {
#ifdef DEBUG_UPDATER
printError(DEBUG_UPDATER);
#endif
_reset();
return written;
}
return written;
}
_bufferLen += toRead;
if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer())
@ -283,6 +350,8 @@ void UpdaterClass::printError(Stream &out){
out.println("Flash Write Failed");
} else if(_error == UPDATE_ERROR_ERASE){
out.println("Flash Erase Failed");
} else if(_error == UPDATE_ERROR_READ){
out.println("Flash Read Failed");
} else if(_error == UPDATE_ERROR_SPACE){
out.println("Not Enough Space");
} else if(_error == UPDATE_ERROR_SIZE){
@ -293,6 +362,10 @@ void UpdaterClass::printError(Stream &out){
out.println("MD5 Check Failed");
} else if(_error == UPDATE_ERROR_FLASH_CONFIG){
out.printf("Flash config wrong real: %d IDE: %d\n", ESP.getFlashChipRealSize(), ESP.getFlashChipSize());
} else if(_error == UPDATE_ERROR_NEW_FLASH_CONFIG){
out.printf("new Flash config wrong real: %d\n", ESP.getFlashChipRealSize());
} else if(_error == UPDATE_ERROR_MAGIC_BYTE){
out.println("Magic byte is wrong, not 0xE9");
} else {
out.println("UNKNOWN");
}

View File

@ -5,14 +5,17 @@
#include "flash_utils.h"
#include "MD5Builder.h"
#define UPDATE_ERROR_OK (0)
#define UPDATE_ERROR_WRITE (1)
#define UPDATE_ERROR_ERASE (2)
#define UPDATE_ERROR_SPACE (3)
#define UPDATE_ERROR_SIZE (4)
#define UPDATE_ERROR_STREAM (5)
#define UPDATE_ERROR_MD5 (6)
#define UPDATE_ERROR_FLASH_CONFIG (7)
#define UPDATE_ERROR_OK (0)
#define UPDATE_ERROR_WRITE (1)
#define UPDATE_ERROR_ERASE (2)
#define UPDATE_ERROR_READ (3)
#define UPDATE_ERROR_SPACE (4)
#define UPDATE_ERROR_SIZE (5)
#define UPDATE_ERROR_STREAM (6)
#define UPDATE_ERROR_MD5 (7)
#define UPDATE_ERROR_FLASH_CONFIG (8)
#define UPDATE_ERROR_NEW_FLASH_CONFIG (9)
#define UPDATE_ERROR_MAGIC_BYTE (10)
#define U_FLASH 0
@ -133,6 +136,9 @@ class UpdaterClass {
void _reset();
bool _writeBuffer();
bool _verifyHeader(uint8_t data);
bool _verifyEnd();
uint8_t _error;
uint8_t *_buffer;
size_t _bufferLen;