Skip to content

Commit 1289f4b

Browse files
Jeroen88me-no-dev
Jeroen88
authored andcommitted
Add MD5 computation to Esp and add request header with MD5 hash of the running sketch to HTTPUpdate (#2176)
1 parent 70f000d commit 1289f4b

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

Diff for: cores/esp32/Esp.cpp

+43-2
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,56 @@ 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) {
182+
log_e("Partition could not be found");
183+
184+
return String();
185+
}
186+
const size_t bufSize = SPI_FLASH_SEC_SIZE;
187+
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
188+
uint32_t offset = 0;
189+
if(!buf.get()) {
190+
log_e("Not enough memory to allocate buffer");
191+
192+
return String();
193+
}
194+
MD5Builder md5;
195+
md5.begin();
196+
while( lengthLeft > 0) {
197+
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
198+
if (!ESP.flashRead(running->address + offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
199+
log_e("Could not read buffer from flash");
200+
201+
return String();
202+
}
203+
md5.add(buf.get(), readBytes);
204+
lengthLeft -= readBytes;
205+
offset += readBytes;
206+
}
207+
md5.calculate();
208+
result = md5.toString();
209+
return result;
210+
}
211+
171212
uint32_t EspClass::getFreeSketchSpace () {
172213
const esp_partition_t* _partition = esp_ota_get_next_update_partition(NULL);
173214
if(!_partition){

Diff for: cores/esp32/Esp.h

+1
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);

Diff for: libraries/HTTPUpdate/src/HTTPUpdate.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,11 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
181181
http.addHeader("x-ESP32-AP-MAC", WiFi.softAPmacAddress());
182182
http.addHeader("x-ESP32-free-space", String(ESP.getFreeSketchSpace()));
183183
http.addHeader("x-ESP32-sketch-size", String(ESP.getSketchSize()));
184-
// To do http.addHeader("x-ESP32-sketch-md5", String(ESP.getSketchMD5()));
185-
// Sketch MD5 is not supported by the core, but SHA256 is, so add a SHA256 instead
184+
String sketchMD5 = ESP.getSketchMD5();
185+
if(sketchMD5.length() != 0) {
186+
http.addHeader("x-ESP32-sketch-md5", sketchMD5);
187+
}
188+
// Add also a SHA256
186189
String sketchSHA256 = getSketchSHA256();
187190
if(sketchSHA256.length() != 0) {
188191
http.addHeader("x-ESP32-sketch-sha256", sketchSHA256);

0 commit comments

Comments
 (0)