nvs_partition_lookup.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "esp_partition.h"
  2. #include "nvs_partition_lookup.hpp"
  3. #ifdef CONFIG_NVS_ENCRYPTION
  4. #include "nvs_encrypted_partition.hpp"
  5. #endif // CONFIG_NVS_ENCRYPTION
  6. namespace nvs {
  7. namespace partition_lookup {
  8. esp_err_t lookup_nvs_partition(const char* label, NVSPartition **p)
  9. {
  10. const esp_partition_t* esp_partition = esp_partition_find_first(
  11. ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, label);
  12. if (esp_partition == nullptr) {
  13. return ESP_ERR_NOT_FOUND;
  14. }
  15. if (esp_partition->encrypted) {
  16. return ESP_ERR_NVS_WRONG_ENCRYPTION;
  17. }
  18. NVSPartition *partition = new (std::nothrow) NVSPartition(esp_partition);
  19. if (partition == nullptr) {
  20. return ESP_ERR_NO_MEM;
  21. }
  22. *p = partition;
  23. return ESP_OK;
  24. }
  25. #ifdef CONFIG_NVS_ENCRYPTION
  26. esp_err_t lookup_nvs_encrypted_partition(const char* label, nvs_sec_cfg_t* cfg, NVSPartition **p)
  27. {
  28. const esp_partition_t* esp_partition = esp_partition_find_first(
  29. ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, label);
  30. if (esp_partition == nullptr) {
  31. return ESP_ERR_NOT_FOUND;
  32. }
  33. if (esp_partition->encrypted) {
  34. return ESP_ERR_NVS_WRONG_ENCRYPTION;
  35. }
  36. NVSEncryptedPartition *enc_p = new (std::nothrow) NVSEncryptedPartition(esp_partition);
  37. if (enc_p == nullptr) {
  38. return ESP_ERR_NO_MEM;
  39. }
  40. esp_err_t result = enc_p->init(cfg);
  41. if (result != ESP_OK) {
  42. delete enc_p;
  43. return result;
  44. }
  45. *p = enc_p;
  46. return ESP_OK;
  47. }
  48. #endif // CONFIG_NVS_ENCRYPTION
  49. } // partition_lookup
  50. } // nvs