test_partitions.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. TEST_CASE("Test erase partition", "[spi_flash][esp_flash]")
  19. {
  20. const esp_partition_t *part = get_test_data_partition();
  21. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  22. spi_flash_reset_counters();
  23. #endif
  24. // erase whole partition
  25. ESP_ERROR_CHECK( esp_partition_erase_range(part, 0, part->size) );
  26. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  27. spi_flash_dump_counters();
  28. #endif
  29. // put some dummy data on sector boundaries
  30. const static DRAM_ATTR char some_data[] = "abcdefghijklmn";
  31. for (int i = 0; i < part->size; i+= 4096) {
  32. ESP_ERROR_CHECK( esp_partition_write(part, i, some_data, strlen(some_data)) );
  33. }
  34. // check it's there!
  35. char buf[strlen(some_data)];
  36. for (int i = 0; i < part->size; i+= 4096) {
  37. memset(buf, 0x00, sizeof(buf));
  38. ESP_ERROR_CHECK( esp_partition_read(part, i, buf, sizeof(buf)) );
  39. TEST_ASSERT_EQUAL_INT(0, strncmp(buf, some_data, sizeof(buf)));
  40. }
  41. // erase the whole thing again
  42. ESP_ERROR_CHECK( esp_partition_erase_range(part, 0, part->size) );
  43. // check it's gone
  44. for (int i = 0; i < part->size; i+= 4096) {
  45. memset(buf, 0x00, sizeof(buf));
  46. ESP_ERROR_CHECK( esp_partition_read(part, i, buf, sizeof(buf)) );
  47. for (int i = 0; i < sizeof(buf); i++) {
  48. TEST_ASSERT_EQUAL_HEX8(0xFF, buf[i]);
  49. }
  50. }
  51. }
  52. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  53. //IDF-5137
  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. esp_partition_t partition;
  98. const void *ptr;
  99. spi_flash_mmap_handle_t handle;
  100. uint8_t sha256[32] = { 0 };
  101. uint32_t size_flash_chip;
  102. esp_flash_get_size(NULL, &size_flash_chip);
  103. printf("size_flash_chip = %d bytes\n", size_flash_chip);
  104. ESP_ERROR_CHECK(spi_flash_mmap(0x00000000, size_flash_chip * 7 / 10, SPI_FLASH_MMAP_DATA, &ptr, &handle));
  105. TEST_ASSERT_NOT_NULL(ptr);
  106. partition.address = 0x00000000;
  107. partition.size = size_flash_chip;
  108. partition.type = ESP_PARTITION_TYPE_DATA;
  109. ESP_ERROR_CHECK(esp_partition_get_sha256(&partition, sha256));
  110. ESP_LOG_BUFFER_HEX("sha", sha256, sizeof(sha256));
  111. spi_flash_munmap(handle);
  112. }
  113. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)