Skip to content

Commit 0902ffa

Browse files
author
Jeroen88
committed
Add getSketchMD5() to ESP.h and ESP.cpp
1 parent 067b82c commit 0902ffa

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

cores/esp32/Esp.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
extern "C" {
3131
#include <esp_image_format.h>
3232
}
33+
#include <MD5Builder.h>
3334

3435
/**
3536
* User-defined Literals
@@ -158,16 +159,48 @@ static uint32_t sketchSize(sketchSize_t response) {
158159
data.start_addr = running_pos.offset;
159160
esp_image_verify(ESP_IMAGE_VERIFY, &running_pos, &data);
160161
if (response) {
161-
return running_pos.size - data.image_len;
162+
return running_pos.size - data.image_len;
162163
} else {
163-
return data.image_len;
164+
return data.image_len;
164165
}
165166
}
166167

167168
uint32_t EspClass::getSketchSize () {
168169
return sketchSize(SKETCH_SIZE_TOTAL);
169170
}
170171

172+
String EspClass::getSketchMD5()
173+
{
174+
static String result;
175+
if (result.length()) {
176+
return result;
177+
}
178+
uint32_t lengthLeft = getSketchSize();
179+
180+
const esp_partition_t *running = esp_ota_get_running_partition();
181+
if (!running) return String();
182+
const size_t bufSize = SPI_FLASH_SEC_SIZE;
183+
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
184+
uint32_t offset = 0;
185+
if(!buf.get()) {
186+
return String();
187+
}
188+
MD5Builder md5;
189+
md5.begin();
190+
while( lengthLeft > 0) {
191+
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
192+
if (!ESP.flashRead(running->address + offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
193+
return String();
194+
}
195+
md5.add(buf.get(), readBytes);
196+
lengthLeft -= readBytes;
197+
offset += readBytes;
198+
}
199+
md5.calculate();
200+
result = md5.toString();
201+
return result;
202+
}
203+
171204
uint32_t EspClass::getFreeSketchSpace () {
172205
const esp_partition_t* _partition = esp_ota_get_next_update_partition(NULL);
173206
if(!_partition){

cores/esp32/Esp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class EspClass
9090
FlashMode_t magicFlashChipMode(uint8_t byte);
9191

9292
uint32_t getSketchSize();
93+
String getSketchMD5();
9394
uint32_t getFreeSketchSpace();
9495

9596
bool flashEraseSector(uint32_t sector);

0 commit comments

Comments
 (0)