From 023e53fcb10d86921dd3ab7cc6a83172256ee005 Mon Sep 17 00:00:00 2001 From: sticilface Date: Wed, 15 Jun 2016 15:59:35 +0100 Subject: [PATCH 1/2] return true sketch size using ESP.getSketchSize() https://github.com/esp8266/Arduino/issues/2153 add MD5 for current binary ESP.getSketchMD5() --- cores/esp8266/Esp.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++- cores/esp8266/Esp.h | 1 + 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index cd0786a08f..404c468990 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -23,6 +23,7 @@ #include "eboot_command.h" #include #include "interrupts.h" +#include "MD5Builder.h" extern "C" { #include "user_interface.h" @@ -443,7 +444,22 @@ uint32_t EspClass::getSketchSize() { DEBUG_SERIAL.printf("section=%u size=%u pos=%u\r\n", section_index, section_header.size, pos); #endif } - result = pos; + uint8_t buff[16] = {0}; + + if (spi_flash_read(pos, (uint32_t*) buff, 16)) { + return 0; + } + uint8_t index = 0; + for (index = 0; index < 16; index++) { + if (buff[index] == 255) { break; } + } +#ifdef DEBUG_SERIAL + DEBUG_SERIAL.printf("Last 16bytes: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\r\n", + buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff[7], + buff[8],buff[9],buff[10],buff[11],buff[12],buff[13],buff[14],buff[15]); + DEBUG_SERIAL.printf("end offset index = %u\r\n", index ); +#endif + result = pos + index; return result; } @@ -519,3 +535,53 @@ bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size) { ets_isr_unmask(FLASH_INT_MASK); return rc == 0; } + + + +String EspClass::getSketchMD5() +{ + + const int buf_size = 512; + uint32_t offset = 0; + uint32_t maxLengthLeft = getSketchSize(); + uint8_t * buf = (uint8_t*) malloc(buf_size); + uint8_t remainder = 0; + + if(!buf) { + return "0"; + } + + MD5Builder md5; + md5.begin(); + + while( maxLengthLeft > 0) { + + size_t readBytes = maxLengthLeft; + + if (readBytes > buf_size) { + readBytes = buf_size; + } + + if (readBytes < 4) { + remainder = readBytes; + readBytes = 4; + } + + if ( flashRead(offset, (uint32_t*)buf, readBytes) ) { + if (!remainder) { + md5.add(buf, readBytes); + } else { + md5.add(buf, remainder); + } + offset += readBytes; + maxLengthLeft -= readBytes; + + } + + } + + md5.calculate(); + + return md5.toString(); +} + diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index c506a525f1..e6754019db 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -133,6 +133,7 @@ class EspClass { bool flashRead(uint32_t offset, uint32_t *data, size_t size); uint32_t getSketchSize(); + String getSketchMD5(); uint32_t getFreeSketchSpace(); bool updateSketch(Stream& in, uint32_t size, bool restartOnFail = false, bool restartOnSuccess = true); From 5ece8c7f596c39b28697c666d6ef5e514c9bd9a1 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 16 Jun 2016 15:00:01 +0800 Subject: [PATCH 2/2] Simplify ESP.getSketchSize, fix ESP.getSketchMD5 --- cores/esp8266/Esp.cpp | 75 ++++++++++++------------------------------- 1 file changed, 20 insertions(+), 55 deletions(-) diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index 404c468990..876fb24935 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -444,22 +444,7 @@ uint32_t EspClass::getSketchSize() { DEBUG_SERIAL.printf("section=%u size=%u pos=%u\r\n", section_index, section_header.size, pos); #endif } - uint8_t buff[16] = {0}; - - if (spi_flash_read(pos, (uint32_t*) buff, 16)) { - return 0; - } - uint8_t index = 0; - for (index = 0; index < 16; index++) { - if (buff[index] == 255) { break; } - } -#ifdef DEBUG_SERIAL - DEBUG_SERIAL.printf("Last 16bytes: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\r\n", - buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff[7], - buff[8],buff[9],buff[10],buff[11],buff[12],buff[13],buff[14],buff[15]); - DEBUG_SERIAL.printf("end offset index = %u\r\n", index ); -#endif - result = pos + index; + result = (pos + 16) & ~15; return result; } @@ -536,52 +521,32 @@ bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size) { return rc == 0; } - - String EspClass::getSketchMD5() { - - const int buf_size = 512; - uint32_t offset = 0; - uint32_t maxLengthLeft = getSketchSize(); - uint8_t * buf = (uint8_t*) malloc(buf_size); - uint8_t remainder = 0; - - if(!buf) { - return "0"; + static String result; + if (result.length()) { + return result; + } + uint32_t lengthLeft = getSketchSize(); + const size_t bufSize = 512; + std::unique_ptr buf(new uint8_t[bufSize]); + uint32_t offset = 0; + if(!buf.get()) { + return String(); } - MD5Builder md5; md5.begin(); - - while( maxLengthLeft > 0) { - - size_t readBytes = maxLengthLeft; - - if (readBytes > buf_size) { - readBytes = buf_size; - } - - if (readBytes < 4) { - remainder = readBytes; - readBytes = 4; - } - - if ( flashRead(offset, (uint32_t*)buf, readBytes) ) { - if (!remainder) { - md5.add(buf, readBytes); - } else { - md5.add(buf, remainder); + while( lengthLeft > 0) { + size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize; + if (!flashRead(offset, reinterpret_cast(buf.get()), (readBytes + 3) & ~3)) { + return String(); } - offset += readBytes; - maxLengthLeft -= readBytes; - - } - + md5.add(buf.get(), readBytes); + lengthLeft -= readBytes; + offset += readBytes; } - md5.calculate(); - - return md5.toString(); + result = md5.toString(); + return result; }