test_partitions.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // Test for spi_flash_{read,write}.
  7. #include <assert.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <sys/param.h>
  12. #include <unity.h>
  13. #include <test_utils.h>
  14. #include <esp_image_format.h>
  15. #include <esp_log.h>
  16. #include <esp_partition.h>
  17. #include <esp_attr.h>
  18. #include "esp_flash.h"
  19. #include "spi_flash_mmap.h"
  20. TEST_CASE("Test erase partition", "[spi_flash][esp_flash]")
  21. {
  22. const esp_partition_t *part = get_test_data_partition();
  23. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  24. esp_flash_reset_counters();
  25. #endif
  26. // erase whole partition
  27. ESP_ERROR_CHECK( esp_partition_erase_range(part, 0, part->size) );
  28. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  29. esp_flash_dump_counters(stdout);
  30. #endif
  31. // put some dummy data on sector boundaries
  32. const static DRAM_ATTR char some_data[] = "abcdefghijklmn";
  33. for (int i = 0; i < part->size; i+= 4096) {
  34. ESP_ERROR_CHECK( esp_partition_write(part, i, some_data, strlen(some_data)) );
  35. }
  36. // check it's there!
  37. char buf[strlen(some_data)];
  38. for (int i = 0; i < part->size; i+= 4096) {
  39. memset(buf, 0x00, sizeof(buf));
  40. ESP_ERROR_CHECK( esp_partition_read(part, i, buf, sizeof(buf)) );
  41. TEST_ASSERT_EQUAL_INT(0, strncmp(buf, some_data, sizeof(buf)));
  42. }
  43. // erase the whole thing again
  44. ESP_ERROR_CHECK( esp_partition_erase_range(part, 0, part->size) );
  45. // check it's gone
  46. for (int i = 0; i < part->size; i+= 4096) {
  47. memset(buf, 0x00, sizeof(buf));
  48. ESP_ERROR_CHECK( esp_partition_read(part, i, buf, sizeof(buf)) );
  49. for (int i = 0; i < sizeof(buf); i++) {
  50. TEST_ASSERT_EQUAL_HEX8(0xFF, buf[i]);
  51. }
  52. }
  53. }
  54. static bool s_test_nonzero_sha_of_partition(const esp_partition_t *part, bool allow_invalid_image)
  55. {
  56. uint8_t sha256[32] = { 0 };
  57. TEST_ASSERT_NOT_NULL(part);
  58. esp_err_t err = esp_partition_get_sha256(part, sha256);
  59. if (allow_invalid_image && err == ESP_ERR_IMAGE_INVALID) {
  60. printf("App partition at 0x%x doesn't hold a valid app\n", part->address);
  61. return false;
  62. }
  63. // Otherwise, err should be ESP_OK
  64. ESP_ERROR_CHECK(err);
  65. ESP_LOG_BUFFER_HEX("sha", sha256, sizeof(sha256));
  66. for (int i = 0; i < sizeof(sha256); i++) {
  67. if (sha256[i] != 0) {
  68. return true; // At least one non-zero byte!
  69. }
  70. }
  71. TEST_FAIL_MESSAGE("SHA-256 of data partition should not be all zeroes");
  72. abort(); // unreachable
  73. }
  74. TEST_CASE("Test esp_partition_get_sha256() with data", "[spi_flash]")
  75. {
  76. const esp_partition_t *part = get_test_data_partition();
  77. s_test_nonzero_sha_of_partition(part, false);
  78. }
  79. TEST_CASE("Test esp_partition_get_sha256() with app", "[spi_flash]")
  80. {
  81. bool found_valid_app = false;
  82. esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP,
  83. ESP_PARTITION_SUBTYPE_ANY,
  84. NULL);
  85. TEST_ASSERT_NOT_NULL(it); /* has to be at least one app partition */
  86. while (it != NULL) {
  87. const esp_partition_t *part = esp_partition_get(it);
  88. printf("Hashing app partition at 0x%x\n", part->address);
  89. bool valid = s_test_nonzero_sha_of_partition(part, true);
  90. found_valid_app |= valid;
  91. it = esp_partition_next(it);
  92. }
  93. TEST_ASSERT_MESSAGE(found_valid_app, "At least one app partition should be a valid app partition");
  94. }
  95. TEST_CASE("Test esp_partition_get_sha256() that it can handle a big partition", "[spi_flash]")
  96. {
  97. /* This test verifies the function 'esp_partition_get_sha256()' working correctly under the following conditions:
  98. * - there is only 1 MMU page left for memory mapping (ideal case)
  99. * - the partition to hash is significantly larger than a common use-case partition size
  100. * The test case is implemented as follows:
  101. * 1. SPI Flash space is mmapped by MMU page size chunks, one by one
  102. * 2. the iteration stops when either whole SPI Flash range is exhausted or the MMU page pool is fully occupied (ESP_ERR_NO_MEM)
  103. * 3. the last successfully mmaped MMU page is released, all the rest remains occupied
  104. * 4. pseudo partition of DATA type is created over all the SPI Flash capacity
  105. * 5. esp_partition_get_sha256() is calculated for the partition defined in 4. (printed to standard output on successful completion)
  106. * 6. all the resources allocated directly by the test are released
  107. * NOTE: the test is chip-agnostic
  108. * */
  109. uint32_t size_flash_chip;
  110. TEST_ESP_OK(esp_flash_get_size(NULL, &size_flash_chip));
  111. printf("flash size = %d bytes\n", size_flash_chip);
  112. uint32_t page_reservation_count = spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
  113. printf("available page pool = %d pages\n", page_reservation_count);
  114. spi_flash_mmap_handle_t* handles = malloc(page_reservation_count * sizeof(spi_flash_mmap_handle_t));
  115. TEST_ASSERT_NOT_NULL(handles);
  116. const void *ptr = NULL;
  117. size_t flash_offset = 0;
  118. size_t mapped_pages_count = 0;
  119. esp_err_t err = ESP_FAIL;
  120. for (; mapped_pages_count<page_reservation_count && flash_offset<size_flash_chip; mapped_pages_count++, flash_offset+=SPI_FLASH_MMU_PAGE_SIZE) {
  121. err = spi_flash_mmap(flash_offset, SPI_FLASH_MMU_PAGE_SIZE, SPI_FLASH_MMAP_DATA, &ptr, &handles[mapped_pages_count]);
  122. if (err != ESP_OK) break;
  123. TEST_ASSERT_NOT_NULL(ptr);
  124. ptr = NULL;
  125. }
  126. if (err == ESP_OK || err == ESP_ERR_NO_MEM) {
  127. TEST_ASSERT(mapped_pages_count>0);
  128. mapped_pages_count--;
  129. spi_flash_munmap(handles[mapped_pages_count]);
  130. }
  131. else {
  132. TEST_ESP_OK(err);
  133. }
  134. esp_partition_t partition = {
  135. .address = 0x00000000,
  136. .size = size_flash_chip,
  137. .type = ESP_PARTITION_TYPE_DATA
  138. };
  139. uint8_t sha256[32] = {0};
  140. TEST_ESP_OK(esp_partition_get_sha256(&partition, sha256));
  141. ESP_LOG_BUFFER_HEX("sha", sha256, sizeof(sha256));
  142. for(size_t y=0; y<mapped_pages_count; y++) {
  143. spi_flash_munmap(handles[y]);
  144. }
  145. free(handles);
  146. }