test_fixtures.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2015-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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "nvs_partition.hpp"
  14. #include "nvs_encrypted_partition.hpp"
  15. #include "spi_flash_emulation.h"
  16. #include "nvs.h"
  17. class PartitionEmulation : public nvs::Partition {
  18. public:
  19. PartitionEmulation(SpiFlashEmulator *spi_flash_emulator,
  20. uint32_t address,
  21. uint32_t size,
  22. const char *partition_name = NVS_DEFAULT_PART_NAME)
  23. : partition_name(partition_name), flash_emu(spi_flash_emulator), address(address), size(size)
  24. {
  25. assert(partition_name);
  26. assert(flash_emu);
  27. assert(size);
  28. }
  29. const char *get_partition_name() override
  30. {
  31. return partition_name;
  32. }
  33. esp_err_t read_raw(size_t src_offset, void* dst, size_t size) override
  34. {
  35. if (!flash_emu->read(reinterpret_cast<uint32_t*>(dst), src_offset, size)) {
  36. return ESP_ERR_FLASH_OP_FAIL;
  37. }
  38. return ESP_OK;
  39. }
  40. esp_err_t read(size_t src_offset, void* dst, size_t size) override
  41. {
  42. if (!flash_emu->read(reinterpret_cast<uint32_t*>(dst), src_offset, size)) {
  43. return ESP_ERR_FLASH_OP_FAIL;
  44. }
  45. return ESP_OK;
  46. }
  47. esp_err_t write_raw(size_t dst_offset, const void* src, size_t size) override
  48. {
  49. if (!flash_emu->write(dst_offset, reinterpret_cast<const uint32_t*>(src), size)) {
  50. return ESP_ERR_FLASH_OP_FAIL;
  51. }
  52. return ESP_OK;
  53. }
  54. esp_err_t write(size_t dst_offset, const void* src, size_t size) override
  55. {
  56. if (!flash_emu->write(dst_offset, reinterpret_cast<const uint32_t*>(src), size)) {
  57. return ESP_ERR_FLASH_OP_FAIL;
  58. }
  59. return ESP_OK;
  60. }
  61. esp_err_t erase_range(size_t dst_offset, size_t size) override
  62. {
  63. if (size % SPI_FLASH_SEC_SIZE != 0) {
  64. return ESP_ERR_INVALID_SIZE;
  65. }
  66. if (dst_offset % SPI_FLASH_SEC_SIZE != 0) {
  67. return ESP_ERR_INVALID_ARG;
  68. }
  69. size_t start_sector = dst_offset / SPI_FLASH_SEC_SIZE;
  70. size_t num_sectors = size / SPI_FLASH_SEC_SIZE;
  71. for (size_t sector = start_sector; sector < (start_sector + num_sectors); sector++) {
  72. if (!flash_emu->erase(sector)) {
  73. return ESP_ERR_FLASH_OP_FAIL;
  74. }
  75. }
  76. return ESP_OK;
  77. }
  78. uint32_t get_address() override
  79. {
  80. return address;
  81. }
  82. uint32_t get_size() override
  83. {
  84. return size;
  85. }
  86. private:
  87. const char *partition_name;
  88. SpiFlashEmulator *flash_emu;
  89. uint32_t address;
  90. uint32_t size;
  91. };
  92. struct PartitionEmulationFixture {
  93. PartitionEmulationFixture(uint32_t start_sector = 0,
  94. uint32_t sector_size = 1,
  95. const char *partition_name = NVS_DEFAULT_PART_NAME)
  96. : emu(start_sector + sector_size),
  97. part(&emu, start_sector * SPI_FLASH_SEC_SIZE, sector_size * SPI_FLASH_SEC_SIZE, partition_name) {
  98. }
  99. ~PartitionEmulationFixture() { }
  100. SpiFlashEmulator emu;
  101. PartitionEmulation part;
  102. };
  103. struct EncryptedPartitionFixture {
  104. EncryptedPartitionFixture(nvs_sec_cfg_t *cfg,
  105. uint32_t start_sector = 0,
  106. uint32_t sector_size = 1,
  107. const char *partition_name = NVS_DEFAULT_PART_NAME)
  108. : esp_partition(), emu(start_sector + sector_size),
  109. part(&esp_partition) {
  110. esp_partition.address = start_sector * SPI_FLASH_SEC_SIZE;
  111. esp_partition.size = sector_size * SPI_FLASH_SEC_SIZE;
  112. strncpy(esp_partition.label, partition_name, PART_NAME_MAX_SIZE);
  113. assert(part.init(cfg) == ESP_OK);
  114. }
  115. ~EncryptedPartitionFixture() { }
  116. esp_partition_t esp_partition;
  117. SpiFlashEmulator emu;
  118. nvs::NVSEncryptedPartition part;
  119. };