2626#include " HTTPUpdate.h"
2727#include < StreamString.h>
2828
29+ #include < esp_partition.h>
30+ #include < esp_ota_ops.h> // get running partition
31+
2932// To do extern "C" uint32_t _SPIFFS_start;
3033// To do extern "C" uint32_t _SPIFFS_end;
3134
@@ -121,6 +124,32 @@ String HTTPUpdate::getLastErrorString(void)
121124}
122125
123126
127+ String getSketchSHA256 () {
128+ const size_t HASH_LEN = 32 ; // SHA-256 digest length
129+
130+ uint8_t sha_256[HASH_LEN] = { 0 };
131+
132+ // get sha256 digest for running partition
133+ if (esp_partition_get_sha256 (esp_ota_get_running_partition (), sha_256) == 0 ) {
134+ char buffer[2 * HASH_LEN + 1 ];
135+
136+ for (size_t index = 0 ; index < HASH_LEN; index++) {
137+ uint8_t nibble = (sha_256[index] & 0xf0 ) >> 4 ;
138+ buffer[2 * index] = nibble < 10 ? char (nibble + ' 0' ) : char (nibble - 10 + ' A' );
139+
140+ nibble = sha_256[index] & 0x0f ;
141+ buffer[2 * index + 1 ] = nibble < 10 ? char (nibble + ' 0' ) : char (nibble - 10 + ' A' );
142+ }
143+
144+ buffer[2 * HASH_LEN] = ' \0 ' ;
145+
146+ return String (buffer);
147+ } else {
148+
149+ return String ();
150+ }
151+ }
152+
124153/* *
125154 *
126155 * @param http HTTPClient *
@@ -139,10 +168,15 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
139168 http.addHeader (F (" Cache-Control" ), F (" no-cache" ));
140169 http.addHeader (F (" x-ESP32-STA-MAC" ), WiFi.macAddress ());
141170 http.addHeader (F (" x-ESP32-AP-MAC" ), WiFi.softAPmacAddress ());
142- // To do http.addHeader(F("x-ESP32-free-space"), String(ESP.getFreeSketchSpace()));
143- // To do http.addHeader(F("x-ESP32-sketch-size"), String(ESP.getSketchSize()));
171+ http.addHeader (F (" x-ESP32-free-space" ), String (ESP.getFreeSketchSpace ()));
172+ http.addHeader (F (" x-ESP32-sketch-size" ), String (ESP.getSketchSize ()));
144173// To do http.addHeader(F("x-ESP32-sketch-md5"), String(ESP.getSketchMD5()));
145- // To do http.addHeader(F("x-ESP32-chip-size"), String(ESP.getFlashChipRealSize()));
174+ // Sketch MD5 is not supported by the core, but SHA256 is, so add a SHA256 instead
175+ String sketchSHA256 = getSketchSHA256 ();
176+ if (sketchSHA256.length () != 0 ) {
177+ http.addHeader (F (" x-ESP32-sketch-sha256" ), sketchSHA256);
178+ }
179+ http.addHeader (F (" x-ESP32-chip-size" ), String (ESP.getFlashChipSize ()));
146180 http.addHeader (F (" x-ESP32-sdk-version" ), ESP.getSdkVersion ());
147181
148182 if (spiffs) {
@@ -183,8 +217,8 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
183217 }
184218
185219 log_d (" ESP32 info:\n " );
186- // To do log_d(" - free Space: %d\n", ESP.getFreeSketchSpace());
187- // To do log_d(" - current Sketch Size: %d\n", ESP.getSketchSize());
220+ log_d (" - free Space: %d\n " , ESP.getFreeSketchSpace ());
221+ log_d (" - current Sketch Size: %d\n " , ESP.getSketchSize ());
188222
189223 if (currentVersion && currentVersion[0 ] != 0x00 ) {
190224 log_d (" - current version: %s\n " , currentVersion.c_str () );
@@ -201,10 +235,10 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
201235// To do startUpdate = false;
202236// To do }
203237 } else {
204- // To do if(len > (int) ESP.getFreeSketchSpace()) {
205- // To do log_e("FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
206- // To do startUpdate = false;
207- // To do }
238+ if (len > (int ) ESP.getFreeSketchSpace ()) {
239+ log_e (" FreeSketchSpace to low (%d) needed: %d\n " , ESP.getFreeSketchSpace (), len);
240+ startUpdate = false ;
241+ }
208242 }
209243
210244 if (!startUpdate) {
0 commit comments