nvs_partition_manager.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "esp_partition.h"
  7. #include "nvs_partition_manager.hpp"
  8. #include "nvs_partition_lookup.hpp"
  9. #include "nvs_internal.h"
  10. #ifndef LINUX_TARGET
  11. #include "nvs_encrypted_partition.hpp"
  12. #endif // ! LINUX_TARGET
  13. namespace nvs {
  14. NVSPartitionManager* NVSPartitionManager::instance = nullptr;
  15. NVSPartitionManager* NVSPartitionManager::get_instance()
  16. {
  17. if (!instance) {
  18. instance = new (std::nothrow) NVSPartitionManager();
  19. }
  20. return instance;
  21. }
  22. #ifdef ESP_PLATFORM
  23. esp_err_t NVSPartitionManager::init_partition(const char *partition_label)
  24. {
  25. if (strlen(partition_label) > NVS_PART_NAME_MAX_SIZE) {
  26. return ESP_ERR_INVALID_ARG;
  27. }
  28. uint32_t size;
  29. Storage* mStorage;
  30. mStorage = lookup_storage_from_name(partition_label);
  31. if (mStorage) {
  32. return ESP_OK;
  33. }
  34. NVS_ASSERT_OR_RETURN(SPI_FLASH_SEC_SIZE != 0, ESP_FAIL);
  35. NVSPartition *p = nullptr;
  36. esp_err_t result = partition_lookup::lookup_nvs_partition(partition_label, &p);
  37. if (result != ESP_OK) {
  38. goto error;
  39. }
  40. size = p->get_size();
  41. result = init_custom(p, 0, size / SPI_FLASH_SEC_SIZE);
  42. if (result != ESP_OK) {
  43. goto error;
  44. }
  45. nvs_partition_list.push_back(p);
  46. return ESP_OK;
  47. error:
  48. delete p;
  49. return result;
  50. }
  51. #endif // ESP_PLATFORM
  52. esp_err_t NVSPartitionManager::init_custom(Partition *partition, uint32_t baseSector, uint32_t sectorCount)
  53. {
  54. Storage* new_storage = nullptr;
  55. Storage* storage = lookup_storage_from_name(partition->get_partition_name());
  56. if (storage == nullptr) {
  57. new_storage = new (std::nothrow) Storage(partition);
  58. if (new_storage == nullptr) {
  59. return ESP_ERR_NO_MEM;
  60. }
  61. storage = new_storage;
  62. } else {
  63. // if storage was initialized already, we don't need partition and hence delete it
  64. for (auto it = nvs_partition_list.begin(); it != nvs_partition_list.end(); ++it) {
  65. if (partition == it) {
  66. nvs_partition_list.erase(it);
  67. delete partition;
  68. break;
  69. }
  70. }
  71. }
  72. esp_err_t err = storage->init(baseSector, sectorCount);
  73. if (new_storage != nullptr) {
  74. if (err == ESP_OK) {
  75. nvs_storage_list.push_back(new_storage);
  76. } else {
  77. delete new_storage;
  78. }
  79. }
  80. return err;
  81. }
  82. #ifdef ESP_PLATFORM
  83. esp_err_t NVSPartitionManager::secure_init_partition(const char *part_name, nvs_sec_cfg_t* cfg)
  84. {
  85. if (strlen(part_name) > NVS_PART_NAME_MAX_SIZE) {
  86. return ESP_ERR_INVALID_ARG;
  87. }
  88. Storage* mStorage;
  89. mStorage = lookup_storage_from_name(part_name);
  90. if (mStorage != nullptr) {
  91. return ESP_OK;
  92. }
  93. NVSPartition *p;
  94. esp_err_t result;
  95. if (cfg != nullptr) {
  96. result = partition_lookup::lookup_nvs_encrypted_partition(part_name, cfg, &p);
  97. } else {
  98. result = partition_lookup::lookup_nvs_partition(part_name, &p);
  99. }
  100. if (result != ESP_OK) {
  101. return result;
  102. }
  103. uint32_t size = p->get_size();
  104. result = init_custom(p, 0, size / SPI_FLASH_SEC_SIZE);
  105. if (result != ESP_OK) {
  106. delete p;
  107. return result;
  108. }
  109. nvs_partition_list.push_back(p);
  110. return ESP_OK;
  111. }
  112. #endif // ESP_PLATFORM
  113. esp_err_t NVSPartitionManager::deinit_partition(const char *partition_label)
  114. {
  115. Storage* storage = lookup_storage_from_name(partition_label);
  116. if (!storage) {
  117. return ESP_ERR_NVS_NOT_INITIALIZED;
  118. }
  119. /* Clean up handles related to the storage being deinitialized */
  120. for (auto it = nvs_handles.begin(); it != nvs_handles.end(); ++it) {
  121. if (it->mStoragePtr == storage) {
  122. it->valid = false;
  123. nvs_handles.erase(it);
  124. }
  125. }
  126. /* Finally delete the storage and its partition */
  127. nvs_storage_list.erase(storage);
  128. delete storage;
  129. for (auto it = nvs_partition_list.begin(); it != nvs_partition_list.end(); ++it) {
  130. if (strcmp(it->get_partition_name(), partition_label) == 0) {
  131. NVSPartition *p = it;
  132. nvs_partition_list.erase(it);
  133. delete p;
  134. break;
  135. }
  136. }
  137. return ESP_OK;
  138. }
  139. esp_err_t NVSPartitionManager::open_handle(const char *part_name,
  140. const char *ns_name,
  141. nvs_open_mode_t open_mode,
  142. NVSHandleSimple** handle)
  143. {
  144. uint8_t nsIndex;
  145. Storage* sHandle;
  146. if (handle == nullptr) {
  147. return ESP_ERR_INVALID_ARG;
  148. }
  149. if (nvs_storage_list.empty()) {
  150. return ESP_ERR_NVS_NOT_INITIALIZED;
  151. }
  152. sHandle = lookup_storage_from_name(part_name);
  153. if (sHandle == nullptr) {
  154. return ESP_ERR_NVS_PART_NOT_FOUND;
  155. }
  156. esp_err_t err = sHandle->createOrOpenNamespace(ns_name, open_mode == NVS_READWRITE, nsIndex);
  157. if (err != ESP_OK) {
  158. return err;
  159. }
  160. NVSHandleSimple* new_handle = new (std::nothrow) NVSHandleSimple(open_mode==NVS_READONLY, nsIndex, sHandle);
  161. if (new_handle == nullptr) {
  162. return ESP_ERR_NO_MEM;
  163. }
  164. nvs_handles.push_back(new_handle);
  165. *handle = new_handle;
  166. return ESP_OK;
  167. }
  168. esp_err_t NVSPartitionManager::close_handle(NVSHandleSimple* handle) {
  169. for (auto it = nvs_handles.begin(); it != nvs_handles.end(); ++it) {
  170. if (it == intrusive_list<NVSHandleSimple>::iterator(handle)) {
  171. nvs_handles.erase(it);
  172. return ESP_OK;
  173. }
  174. }
  175. return ESP_ERR_NVS_INVALID_HANDLE;
  176. }
  177. size_t NVSPartitionManager::open_handles_size()
  178. {
  179. return nvs_handles.size();
  180. }
  181. Storage* NVSPartitionManager::lookup_storage_from_name(const char* name)
  182. {
  183. auto it = find_if(begin(nvs_storage_list), end(nvs_storage_list), [=](Storage& e) -> bool {
  184. return (strcmp(e.getPartName(), name) == 0);
  185. });
  186. if (it == end(nvs_storage_list)) {
  187. return nullptr;
  188. }
  189. return it;
  190. }
  191. } // nvs