|
30 | 30 | extern "C" {
|
31 | 31 | #include <esp_image_format.h>
|
32 | 32 | }
|
| 33 | +#include <MD5Builder.h> |
33 | 34 |
|
34 | 35 | /**
|
35 | 36 | * User-defined Literals
|
@@ -158,16 +159,56 @@ static uint32_t sketchSize(sketchSize_t response) {
|
158 | 159 | data.start_addr = running_pos.offset;
|
159 | 160 | esp_image_verify(ESP_IMAGE_VERIFY, &running_pos, &data);
|
160 | 161 | if (response) {
|
161 |
| - return running_pos.size - data.image_len; |
| 162 | + return running_pos.size - data.image_len; |
162 | 163 | } else {
|
163 |
| - return data.image_len; |
| 164 | + return data.image_len; |
164 | 165 | }
|
165 | 166 | }
|
166 | 167 |
|
167 | 168 | uint32_t EspClass::getSketchSize () {
|
168 | 169 | return sketchSize(SKETCH_SIZE_TOTAL);
|
169 | 170 | }
|
170 | 171 |
|
| 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 | + |
171 | 212 | uint32_t EspClass::getFreeSketchSpace () {
|
172 | 213 | const esp_partition_t* _partition = esp_ota_get_next_update_partition(NULL);
|
173 | 214 | if(!_partition){
|
|
0 commit comments