From 537aca747b4adf815a2f5f4a89f6e6dafc748a32 Mon Sep 17 00:00:00 2001 From: Martin Vychodil Date: Thu, 24 Nov 2022 22:21:09 +0100 Subject: [PATCH] esp_partition: fixed esp_partition_get_sha256() test for big partitions esp_partition_get_sha256() test for big partitions has been improved in allocation of MMU pages --- .../esp_partition/test/test_partitions.c | 64 +++++++++++++++---- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/components/esp_partition/test/test_partitions.c b/components/esp_partition/test/test_partitions.c index a3fe7ebca1..cf9fd2ac63 100644 --- a/components/esp_partition/test/test_partitions.c +++ b/components/esp_partition/test/test_partitions.c @@ -118,25 +118,63 @@ TEST_CASE("Test esp_partition_get_sha256() with app", "[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; + /* This test verifies the function 'esp_partition_get_sha256()' working correctly under the following conditions: + * - there is only 1 MMU page left for memory mapping (ideal case) + * - the partition to hash is significantly larger than a common use-case partition size + * The test case is implemented as follows: + * 1. SPI Flash space is mmapped by MMU page size chunks, one by one + * 2. the iteration stops when either whole SPI Flash range is exhausted or the MMU page pool is fully occupied (ESP_ERR_NO_MEM) + * 3. the last successfully mmaped MMU page is released, all the rest remains occupied + * 4. pseudo partition of DATA type is created over all the SPI Flash capacity + * 5. esp_partition_get_sha256() is calculated for the partition defined in 4. (printed to standard output on successful completion) + * 6. all the resources allocated directly by the test are released + * NOTE: the test is chip-agnostic + * */ - uint8_t sha256[32] = { 0 }; uint32_t size_flash_chip; - esp_flash_get_size(NULL, &size_flash_chip); + TEST_ESP_OK(esp_flash_get_size(NULL, &size_flash_chip)); + printf("flash size = %d bytes\n", size_flash_chip); - printf("size_flash_chip = %d bytes\n", size_flash_chip); + uint32_t page_reservation_count = spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA); + printf("available page pool = %d pages\n", page_reservation_count); - ESP_ERROR_CHECK(spi_flash_mmap(0x00000000, size_flash_chip * 7 / 10, SPI_FLASH_MMAP_DATA, &ptr, &handle)); - TEST_ASSERT_NOT_NULL(ptr); + spi_flash_mmap_handle_t* handles = malloc(page_reservation_count * sizeof(spi_flash_mmap_handle_t)); + TEST_ASSERT_NOT_NULL(handles); - partition.address = 0x00000000; - partition.size = size_flash_chip; - partition.type = ESP_PARTITION_TYPE_DATA; + const void *ptr = NULL; + size_t flash_offset = 0; + size_t mapped_pages_count = 0; - ESP_ERROR_CHECK(esp_partition_get_sha256(&partition, sha256)); + esp_err_t err = ESP_FAIL; + for (; mapped_pages_count0); + mapped_pages_count--; + spi_flash_munmap(handles[mapped_pages_count]); + } + else { + TEST_ESP_OK(err); + } + + esp_partition_t partition = { + .address = 0x00000000, + .size = size_flash_chip, + .type = ESP_PARTITION_TYPE_DATA + }; + + uint8_t sha256[32] = {0}; + TEST_ESP_OK(esp_partition_get_sha256(&partition, sha256)); ESP_LOG_BUFFER_HEX("sha", sha256, sizeof(sha256)); - spi_flash_munmap(handle); + for(size_t y=0; y