Skip to content

Commit

Permalink
esp32_getOTAImageSHA256: simplify beautify and align style
Browse files Browse the repository at this point in the history
  • Loading branch information
pennam committed Jan 19, 2023
1 parent 1ab8b3f commit fce0018
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/utility/ota/OTA-esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,40 @@ int esp32_onOTARequest(char const * ota_url)

String esp32_getOTAImageSHA256()
{
SHA256 sha256;

uint32_t lengthLeft = ESP.getSketchSize();

const esp_partition_t *running = esp_ota_get_running_partition();
if (!running) {
DEBUG_ERROR("Partition could not be found");
DEBUG_ERROR("ESP32::SHA256 Running partition could not be found");
return String();
}
const size_t bufSize = SPI_FLASH_SEC_SIZE;
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
uint32_t offset = 0;
if(!buf.get()) {
DEBUG_ERROR("Not enough memory to allocate buffer");

uint8_t *b = (uint8_t*)malloc(SPI_FLASH_SEC_SIZE);
if(b == nullptr) {
DEBUG_ERROR("ESP32::SHA256 Not enough memory to allocate buffer");
return String();
}

SHA256 sha256;
uint32_t const app_start = running->address;
uint32_t const app_size = ESP.getSketchSize();
uint32_t read_bytes = 0;

sha256.begin();
while( lengthLeft > 0) {
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
if (!ESP.flashRead(running->address + offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
DEBUG_ERROR("Could not read buffer from flash");
for(uint32_t a = app_start; read_bytes < app_size; )
{
/* Check if we are reading last sector and compute used size */
uint32_t const read_size = read_bytes + SPI_FLASH_SEC_SIZE < app_size ? SPI_FLASH_SEC_SIZE : app_size - read_bytes;

/* Use always 4 bytes aligned reads */
if (!ESP.flashRead(a, reinterpret_cast<uint32_t*>(b), (read_size + 3) & ~3)) {
DEBUG_ERROR("ESP32::SHA256 Could not read data from flash");
return String();
}
sha256.update(buf.get(), readBytes);
lengthLeft -= readBytes;
offset += readBytes;
sha256.update(b, read_size);
a += read_size;
read_bytes += read_size;
}
free(b);

/* Retrieve the final hash string. */
uint8_t sha256_hash[SHA256::HASH_SIZE] = {0};
sha256.finalize(sha256_hash);
Expand All @@ -107,7 +116,7 @@ String esp32_getOTAImageSHA256()
snprintf(buf, 4, "%02X", elem);
sha256_str += buf;
});
DEBUG_VERBOSE("SHA256: %d bytes (of %d) read", ESP.getSketchSize() - lengthLeft, ESP.getSketchSize());
DEBUG_VERBOSE("SHA256: %d bytes (of %d) read", read_bytes, app_size);
return sha256_str;
}

Expand Down

0 comments on commit fce0018

Please sign in to comment.