@@ -215,8 +215,44 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
215215 */
216216 String const sha256_str = FlashSHA256::calc (XIP_BASE, 0x100000 );
217217#elif defined(ARDUINO_ARCH_ESP32)
218- # warning "Compute image SHA256"
219- String const sha256_str;
218+ SHA256 sha256;
219+
220+ uint32_t lengthLeft = ESP.getSketchSize ();
221+
222+ const esp_partition_t *running = esp_ota_get_running_partition ();
223+ if (!running) {
224+ DEBUG_ERROR (" Partition could not be found" );
225+ }
226+ const size_t bufSize = SPI_FLASH_SEC_SIZE;
227+ std::unique_ptr<uint8_t []> buf (new uint8_t [bufSize]);
228+ uint32_t offset = 0 ;
229+ if (!buf.get ()) {
230+ DEBUG_ERROR (" Not enough memory to allocate buffer" );
231+ }
232+
233+ sha256.begin ();
234+ while ( lengthLeft > 0 ) {
235+ size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
236+ if (!ESP.flashRead (running->address + offset, reinterpret_cast <uint32_t *>(buf.get ()), (readBytes + 3 ) & ~3 )) {
237+ DEBUG_ERROR (" Could not read buffer from flash" );
238+ }
239+ sha256.update (buf.get (), readBytes);
240+ lengthLeft -= readBytes;
241+ offset += readBytes;
242+ }
243+ /* Retrieve the final hash string. */
244+ uint8_t sha256_hash[SHA256::HASH_SIZE] = {0 };
245+ sha256.finalize (sha256_hash);
246+ String sha256_str;
247+ std::for_each (sha256_hash,
248+ sha256_hash + SHA256::HASH_SIZE,
249+ [&sha256_str](uint8_t const elem)
250+ {
251+ char buf[4 ];
252+ snprintf (buf, 4 , " %02X" , elem);
253+ sha256_str += buf;
254+ });
255+ DEBUG_VERBOSE (" SHA256: %d bytes (of %d) read" , ESP.getSketchSize () - lengthLeft, ESP.getSketchSize ());
220256#else
221257# error "No method for SHA256 checksum calculation over application image defined for this architecture."
222258#endif
0 commit comments