From 035783039bec3bcd542d002bf3ee370a3047b6ed Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Tue, 7 Jul 2020 21:56:58 +0800 Subject: [PATCH] bootloader_support: Fix bootloader_common_get_sha256_of_partition, can handle a long image Closes: IDFGH-3594 --- .../src/bootloader_common.c | 18 ++------------ components/spi_flash/test/test_partitions.c | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/components/bootloader_support/src/bootloader_common.c b/components/bootloader_support/src/bootloader_common.c index 61d89f67e..3ab2af792 100644 --- a/components/bootloader_support/src/bootloader_common.c +++ b/components/bootloader_support/src/bootloader_common.c @@ -26,6 +26,7 @@ #include "esp_flash_partitions.h" #include "bootloader_flash.h" #include "bootloader_common.h" +#include "bootloader_utility.h" #include "soc/gpio_periph.h" #include "soc/efuse_reg.h" #include "soc/rtc.h" @@ -186,22 +187,7 @@ esp_err_t bootloader_common_get_sha256_of_partition (uint32_t address, uint32_t size = data.image_len; } // If image is type by data then hash is calculated for entire image. - const void *partition_bin = bootloader_mmap(address, size); - if (partition_bin == NULL) { - ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", address, size); - return ESP_FAIL; - } - bootloader_sha256_handle_t sha_handle = bootloader_sha256_start(); - if (sha_handle == NULL) { - bootloader_munmap(partition_bin); - return ESP_ERR_NO_MEM; - } - bootloader_sha256_data(sha_handle, partition_bin, size); - bootloader_sha256_finish(sha_handle, out_sha_256); - - bootloader_munmap(partition_bin); - - return ESP_OK; + return bootloader_sha256_flash_contents(address, size, out_sha_256); } int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max) diff --git a/components/spi_flash/test/test_partitions.c b/components/spi_flash/test/test_partitions.c index d859d3dd2..aa065d794 100644 --- a/components/spi_flash/test/test_partitions.c +++ b/components/spi_flash/test/test_partitions.c @@ -66,3 +66,27 @@ TEST_CASE("Test erase partition", "[spi_flash]") } } } + +TEST_CASE("Test esp_partition_get_sha256() that it can handle a big partition", "[spi_flash]") +{ + esp_partition_t partition; + const void *ptr; + spi_flash_mmap_handle_t handle; + + uint8_t sha256[32] = { 0 }; + size_t size_flash_chip = spi_flash_get_chip_size(); + + printf("size_flash_chip = %d bytes\n", size_flash_chip); + + ESP_ERROR_CHECK(spi_flash_mmap(0x00000000, size_flash_chip * 7 / 10, SPI_FLASH_MMAP_DATA, &ptr, &handle)); + TEST_ASSERT_NOT_NULL(ptr); + + partition.address = 0x00000000; + partition.size = size_flash_chip; + partition.type = ESP_PARTITION_TYPE_DATA; + + ESP_ERROR_CHECK(esp_partition_get_sha256(&partition, sha256)); + ESP_LOG_BUFFER_HEX("sha", sha256, sizeof(sha256)); + + spi_flash_munmap(handle); +}