test_partition.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "unity.h"
  9. #include "test_utils.h"
  10. #include "esp_partition.h"
  11. TEST_CASE("Can read partition table", "[partition]")
  12. {
  13. const esp_partition_t *p = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, NULL);
  14. TEST_ASSERT_NOT_NULL(p);
  15. TEST_ASSERT_EQUAL(0x20000, p->address);
  16. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, p->subtype);
  17. esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, NULL);
  18. TEST_ASSERT_NOT_NULL(it);
  19. int count = 0;
  20. const esp_partition_t* prev = NULL;
  21. for (; it != NULL; it = esp_partition_next(it)) {
  22. const esp_partition_t *p = esp_partition_get(it);
  23. TEST_ASSERT_NOT_NULL(p);
  24. if (prev) {
  25. TEST_ASSERT_TRUE_MESSAGE(prev->address < p->address, "incorrect partition order");
  26. }
  27. prev = p;
  28. ++count;
  29. }
  30. esp_partition_iterator_release(it);
  31. TEST_ASSERT_EQUAL(5, count);
  32. it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
  33. TEST_ASSERT_NOT_NULL(it);
  34. count = 0;
  35. for (; it != NULL; it = esp_partition_next(it)) {
  36. ++count;
  37. }
  38. esp_partition_iterator_release(it);
  39. TEST_ASSERT_EQUAL(8, count);
  40. }
  41. TEST_CASE("Can write, read, mmap partition", "[partition][ignore]")
  42. {
  43. const esp_partition_t *p = get_test_data_partition();
  44. printf("Using partition %s at 0x%x, size 0x%x\n", p->label, p->address, p->size);
  45. TEST_ASSERT_NOT_NULL(p);
  46. const size_t max_size = 2 * p->erase_size;
  47. uint8_t *data = (uint8_t *) malloc(max_size);
  48. TEST_ASSERT_NOT_NULL(data);
  49. TEST_ASSERT_EQUAL(ESP_OK, esp_partition_erase_range(p, 0, p->size));
  50. srand(0);
  51. size_t block_size;
  52. for (size_t offset = 0; offset < p->size; offset += block_size) {
  53. block_size = ((rand() + 4) % max_size) & (~0x3);
  54. size_t left = p->size - offset;
  55. if (block_size > left) {
  56. block_size = left;
  57. }
  58. for (size_t i = 0; i < block_size / 4; ++i) {
  59. ((uint32_t *) (data))[i] = rand();
  60. }
  61. TEST_ASSERT_EQUAL(ESP_OK, esp_partition_write(p, offset, data, block_size));
  62. }
  63. srand(0);
  64. for (size_t offset = 0; offset < p->size; offset += block_size) {
  65. block_size = ((rand() + 4) % max_size) & (~0x3);
  66. size_t left = p->size - offset;
  67. if (block_size > left) {
  68. block_size = left;
  69. }
  70. TEST_ASSERT_EQUAL(ESP_OK, esp_partition_read(p, offset, data, block_size));
  71. for (size_t i = 0; i < block_size / 4; ++i) {
  72. TEST_ASSERT_EQUAL(rand(), ((uint32_t *) data)[i]);
  73. }
  74. }
  75. free(data);
  76. const uint32_t *mmap_data;
  77. esp_partition_mmap_handle_t mmap_handle;
  78. size_t begin = 3000;
  79. size_t size = 64000; //chosen so size is smaller than 64K but the mmap straddles 2 MMU blocks
  80. TEST_ASSERT_EQUAL(ESP_OK, esp_partition_mmap(p, begin, size, ESP_PARTITION_MMAP_DATA,
  81. (const void **)&mmap_data, &mmap_handle));
  82. srand(0);
  83. for (size_t offset = 0; offset < p->size; offset += block_size) {
  84. block_size = ((rand() + 4) % max_size) & (~0x3);
  85. size_t left = p->size - offset;
  86. if (block_size > left) {
  87. block_size = left;
  88. }
  89. for (size_t i = 0; i < block_size / 4; ++i) {
  90. size_t pos = offset + i * 4;
  91. uint32_t expected = rand();
  92. if (pos < begin || pos >= (begin + size)) {
  93. continue;
  94. }
  95. TEST_ASSERT_EQUAL(expected, mmap_data[(pos - begin) / 4]);
  96. }
  97. }
  98. esp_partition_munmap(mmap_handle);
  99. }