Skip to content

Commit

Permalink
Fix ESP.getSketchSize, add ESP.getSketchMD5 (esp8266#2158)
Browse files Browse the repository at this point in the history
* return true sketch size using ESP.getSketchSize() esp8266#2153
add MD5 for current binary ESP.getSketchMD5()

* Simplify ESP.getSketchSize, fix ESP.getSketchMD5
  • Loading branch information
igrr authored Jun 16, 2016
1 parent 44d2722 commit 1640cc3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
33 changes: 32 additions & 1 deletion cores/esp8266/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "eboot_command.h"
#include <memory>
#include "interrupts.h"
#include "MD5Builder.h"

extern "C" {
#include "user_interface.h"
Expand Down Expand Up @@ -443,7 +444,7 @@ uint32_t EspClass::getSketchSize() {
DEBUG_SERIAL.printf("section=%u size=%u pos=%u\r\n", section_index, section_header.size, pos);
#endif
}
result = pos;
result = (pos + 16) & ~15;
return result;
}

Expand Down Expand Up @@ -519,3 +520,33 @@ bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size) {
ets_isr_unmask(FLASH_INT_MASK);
return rc == 0;
}

String EspClass::getSketchMD5()
{
static String result;
if (result.length()) {
return result;
}
uint32_t lengthLeft = getSketchSize();
const size_t bufSize = 512;
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
uint32_t offset = 0;
if(!buf.get()) {
return String();
}
MD5Builder md5;
md5.begin();
while( lengthLeft > 0) {
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
if (!flashRead(offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
return String();
}
md5.add(buf.get(), readBytes);
lengthLeft -= readBytes;
offset += readBytes;
}
md5.calculate();
result = md5.toString();
return result;
}

1 change: 1 addition & 0 deletions cores/esp8266/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 1640cc3

Please sign in to comment.