-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Regarding Slow Hash Computation of Running Partition on ESP32-C2 (IDFGH-14435) #15215
Comments
The main difference is that the bootloader maps the application image into CPU address space using Flash MMU, and then reads the parts of the binary via the MMU. In your implementation, every call to esp_partition_read has a significant overhead, as Flash reads (which aren't done via the MMU) require preemption of all the code in the system which runs from Flash. I would recommend checking esp_partition_mmap function. You can map one 64kB page at a time, calculate the hash, unmap, map the next one, etc. This should work faster than your current approach. |
I tried bootloader_sha256_flash_contents() and also with esp_partition_mmap() which uses Flash MMU but still hash computation time is high ~30 sec to 35 sec for the running partition in firmware application |
Going a bit back to the original problem, could you please clarify the need for computing the hash of the running partition? You can query the hash appended to the image itself, without re-calculating it — since it is verified when starting the app: #include "esp_log.h"
#include "esp_image_format.h"
#include "esp_ota_ops.h"
#include "esp_partition.h"
static const char *TAG = "example";
void app_main(void)
{
const esp_partition_t* running = esp_ota_get_running_partition();
const esp_partition_pos_t part_pos = {
.offset = running->address,
.size = running->size,
};
esp_image_metadata_t img_metadata = {0};
ESP_ERROR_CHECK(esp_image_get_metadata(&part_pos, &img_metadata));
ESP_LOGI(TAG, "Image SHA256 hash:");
ESP_LOG_BUFFER_HEX(TAG, img_metadata.image_digest, sizeof(img_metadata.image_digest));
} |
For C2 case, we have a SHA performance test here, you may try running it. Performance benchmark with hardware acceleration enabled case is set to to 14 MB/sec (reference). Even with hardware acceleration disabled case, performance should be close to 2-3 MB/sec. As Ivan mentioned, problem could be how the data is read and supplied to SHA module. Only difference w.r.t. bootloader that I can think of is the code execution from internal memory vs. external flash (XIP). Using API |
I figured out that it was not an issue with hash computation.I had an issue with my application code. So closing this issue |
Answers checklist.
General issue report
I am currently working on an ESP32-C2 target and have observed an issue with hash computation performance. Specifically, computing the SHA256 hash of the running partition (size 1MB) takes approximately 40 to 60 seconds due to the absence of a DMA engine for SPI flash. However, the same hash computation performs efficiently during the bootloader's secure boot signature validation process when software secure boot is enabled using the SDK configuration.
For reference, I have attached the pseudo code I am using in my application. This implementation works well on other ESP32 targets, but on ESP32-C2 (Chip rev 1, IDF v5.1), the computation takes significantly longer. I also tried using the bootloader API, bootloader_sha256_flash_contents(), to compute the hash, but it did not result in a noticeable improvement in execution time.
pseudo code:
`bool device_compute_fw_hash(uint32_t fw_size,
uint8_t *hash_buff, uint16_t hash_buff_len)
{
bool ret_val = false;
uint8_t rd_buff[1024] = {0};
}
`
I would like to understand how the SHA256 computation is implemented in the bootloader during secure boot validation. Are there any optimizations or differences in the approach compared to hash computation on the running partition? Additionally, could you please provide recommendations or configurations to improve the hash computation speed for the running partition?
The text was updated successfully, but these errors were encountered: