test_partitions.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Test for spi_flash_{read,write}.
  15. #include <assert.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/param.h>
  20. #include <unity.h>
  21. #include <test_utils.h>
  22. #include <esp_image_format.h>
  23. #include <esp_log.h>
  24. #include <esp_partition.h>
  25. #include <esp_attr.h>
  26. TEST_CASE("Test erase partition", "[spi_flash][esp_flash]")
  27. {
  28. const esp_partition_t *part = get_test_data_partition();
  29. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  30. spi_flash_reset_counters();
  31. #endif
  32. // erase whole partition
  33. ESP_ERROR_CHECK( esp_partition_erase_range(part, 0, part->size) );
  34. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  35. spi_flash_dump_counters();
  36. #endif
  37. // put some dummy data on sector boundaries
  38. const static DRAM_ATTR char some_data[] = "abcdefghijklmn";
  39. for (int i = 0; i < part->size; i+= 4096) {
  40. ESP_ERROR_CHECK( esp_partition_write(part, i, some_data, strlen(some_data)) );
  41. }
  42. // check it's there!
  43. char buf[strlen(some_data)];
  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. TEST_ASSERT_EQUAL_INT(0, strncmp(buf, some_data, sizeof(buf)));
  48. }
  49. // erase the whole thing again
  50. ESP_ERROR_CHECK( esp_partition_erase_range(part, 0, part->size) );
  51. // check it's gone
  52. for (int i = 0; i < part->size; i+= 4096) {
  53. memset(buf, 0x00, sizeof(buf));
  54. ESP_ERROR_CHECK( esp_partition_read(part, i, buf, sizeof(buf)) );
  55. for (int i = 0; i < sizeof(buf); i++) {
  56. TEST_ASSERT_EQUAL_HEX8(0xFF, buf[i]);
  57. }
  58. }
  59. }
  60. static bool s_test_nonzero_sha_of_partition(const esp_partition_t *part, bool allow_invalid_image)
  61. {
  62. uint8_t sha256[32] = { 0 };
  63. TEST_ASSERT_NOT_NULL(part);
  64. esp_err_t err = esp_partition_get_sha256(part, sha256);
  65. if (allow_invalid_image && err == ESP_ERR_IMAGE_INVALID) {
  66. printf("App partition at 0x%x doesn't hold a valid app\n", part->address);
  67. return false;
  68. }
  69. // Otherwise, err should be ESP_OK
  70. ESP_ERROR_CHECK(err);
  71. ESP_LOG_BUFFER_HEX("sha", sha256, sizeof(sha256));
  72. for (int i = 0; i < sizeof(sha256); i++) {
  73. if (sha256[i] != 0) {
  74. return true; // At least one non-zero byte!
  75. }
  76. }
  77. TEST_FAIL_MESSAGE("SHA-256 of data partition should not be all zeroes");
  78. abort(); // unreachable
  79. }
  80. TEST_CASE("Test esp_partition_get_sha256() with data", "[spi_flash]")
  81. {
  82. const esp_partition_t *part = get_test_data_partition();
  83. s_test_nonzero_sha_of_partition(part, false);
  84. }
  85. TEST_CASE("Test esp_partition_get_sha256() with app", "[spi_flash]")
  86. {
  87. bool found_valid_app = false;
  88. esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP,
  89. ESP_PARTITION_SUBTYPE_ANY,
  90. NULL);
  91. TEST_ASSERT_NOT_NULL(it); /* has to be at least one app partition */
  92. while (it != NULL) {
  93. const esp_partition_t *part = esp_partition_get(it);
  94. printf("Hashing app partition at 0x%x\n", part->address);
  95. bool valid = s_test_nonzero_sha_of_partition(part, true);
  96. found_valid_app |= valid;
  97. it = esp_partition_next(it);
  98. }
  99. TEST_ASSERT_MESSAGE(found_valid_app, "At least one app partition should be a valid app partition");
  100. }
  101. TEST_CASE("Test esp_partition_get_sha256() that it can handle a big partition", "[spi_flash]")
  102. {
  103. esp_partition_t partition;
  104. const void *ptr;
  105. spi_flash_mmap_handle_t handle;
  106. uint8_t sha256[32] = { 0 };
  107. size_t size_flash_chip = spi_flash_get_chip_size();
  108. printf("size_flash_chip = %d bytes\n", size_flash_chip);
  109. ESP_ERROR_CHECK(spi_flash_mmap(0x00000000, size_flash_chip * 7 / 10, SPI_FLASH_MMAP_DATA, &ptr, &handle));
  110. TEST_ASSERT_NOT_NULL(ptr);
  111. partition.address = 0x00000000;
  112. partition.size = size_flash_chip;
  113. partition.type = ESP_PARTITION_TYPE_DATA;
  114. ESP_ERROR_CHECK(esp_partition_get_sha256(&partition, sha256));
  115. ESP_LOG_BUFFER_HEX("sha", sha256, sizeof(sha256));
  116. spi_flash_munmap(handle);
  117. }