Skip to content

Commit 023e53f

Browse files
committed
return true sketch size using ESP.getSketchSize() #2153
add MD5 for current binary ESP.getSketchMD5()
1 parent cbe8f7c commit 023e53f

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

cores/esp8266/Esp.cpp

Lines changed: 67 additions & 1 deletion
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,22 @@ 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+
uint8_t buff[16] = {0};
448+
449+
if (spi_flash_read(pos, (uint32_t*) buff, 16)) {
450+
return 0;
451+
}
452+
uint8_t index = 0;
453+
for (index = 0; index < 16; index++) {
454+
if (buff[index] == 255) { break; }
455+
}
456+
#ifdef DEBUG_SERIAL
457+
DEBUG_SERIAL.printf("Last 16bytes: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\r\n",
458+
buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff[7],
459+
buff[8],buff[9],buff[10],buff[11],buff[12],buff[13],buff[14],buff[15]);
460+
DEBUG_SERIAL.printf("end offset index = %u\r\n", index );
461+
#endif
462+
result = pos + index;
447463
return result;
448464
}
449465

@@ -519,3 +535,53 @@ bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size) {
519535
ets_isr_unmask(FLASH_INT_MASK);
520536
return rc == 0;
521537
}
538+
539+
540+
541+
String EspClass::getSketchMD5()
542+
{
543+
544+
const int buf_size = 512;
545+
uint32_t offset = 0;
546+
uint32_t maxLengthLeft = getSketchSize();
547+
uint8_t * buf = (uint8_t*) malloc(buf_size);
548+
uint8_t remainder = 0;
549+
550+
if(!buf) {
551+
return "0";
552+
}
553+
554+
MD5Builder md5;
555+
md5.begin();
556+
557+
while( maxLengthLeft > 0) {
558+
559+
size_t readBytes = maxLengthLeft;
560+
561+
if (readBytes > buf_size) {
562+
readBytes = buf_size;
563+
}
564+
565+
if (readBytes < 4) {
566+
remainder = readBytes;
567+
readBytes = 4;
568+
}
569+
570+
if ( flashRead(offset, (uint32_t*)buf, readBytes) ) {
571+
if (!remainder) {
572+
md5.add(buf, readBytes);
573+
} else {
574+
md5.add(buf, remainder);
575+
}
576+
offset += readBytes;
577+
maxLengthLeft -= readBytes;
578+
579+
}
580+
581+
}
582+
583+
md5.calculate();
584+
585+
return md5.toString();
586+
}
587+

cores/esp8266/Esp.h

Lines changed: 1 addition & 0 deletions
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)