Skip to content

Commit 1640cc3

Browse files
authored
Fix ESP.getSketchSize, add ESP.getSketchMD5 (#2158)
* return true sketch size using ESP.getSketchSize() #2153 add MD5 for current binary ESP.getSketchMD5() * Simplify ESP.getSketchSize, fix ESP.getSketchMD5
1 parent 44d2722 commit 1640cc3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

cores/esp8266/Esp.cpp

+32-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "eboot_command.h"
2424
#include <memory>
2525
#include "interrupts.h"
26+
#include "MD5Builder.h"
2627

2728
extern "C" {
2829
#include "user_interface.h"
@@ -443,7 +444,7 @@ uint32_t EspClass::getSketchSize() {
443444
DEBUG_SERIAL.printf("section=%u size=%u pos=%u\r\n", section_index, section_header.size, pos);
444445
#endif
445446
}
446-
result = pos;
447+
result = (pos + 16) & ~15;
447448
return result;
448449
}
449450

@@ -519,3 +520,33 @@ bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size) {
519520
ets_isr_unmask(FLASH_INT_MASK);
520521
return rc == 0;
521522
}
523+
524+
String EspClass::getSketchMD5()
525+
{
526+
static String result;
527+
if (result.length()) {
528+
return result;
529+
}
530+
uint32_t lengthLeft = getSketchSize();
531+
const size_t bufSize = 512;
532+
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
533+
uint32_t offset = 0;
534+
if(!buf.get()) {
535+
return String();
536+
}
537+
MD5Builder md5;
538+
md5.begin();
539+
while( lengthLeft > 0) {
540+
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
541+
if (!flashRead(offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
542+
return String();
543+
}
544+
md5.add(buf.get(), readBytes);
545+
lengthLeft -= readBytes;
546+
offset += readBytes;
547+
}
548+
md5.calculate();
549+
result = md5.toString();
550+
return result;
551+
}
552+

cores/esp8266/Esp.h

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class EspClass {
133133
bool flashRead(uint32_t offset, uint32_t *data, size_t size);
134134

135135
uint32_t getSketchSize();
136+
String getSketchMD5();
136137
uint32_t getFreeSketchSpace();
137138
bool updateSketch(Stream& in, uint32_t size, bool restartOnFail = false, bool restartOnSuccess = true);
138139

0 commit comments

Comments
 (0)