nvs_flash.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #ifndef nvs_flash_h
  14. #define nvs_flash_h
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "nvs.h"
  19. #include "esp_partition.h"
  20. #define NVS_KEY_SIZE 32 // AES-256
  21. /**
  22. * @brief Key for encryption and decryption
  23. */
  24. typedef struct {
  25. uint8_t eky[NVS_KEY_SIZE]; /*!< XTS encryption and decryption key*/
  26. uint8_t tky[NVS_KEY_SIZE]; /*!< XTS tweak key */
  27. } nvs_sec_cfg_t;
  28. /**
  29. * @brief Initialize the default NVS partition.
  30. *
  31. * This API initialises the default NVS partition. The default NVS partition
  32. * is the one that is labeled "nvs" in the partition table.
  33. *
  34. * @return
  35. * - ESP_OK if storage was successfully initialized.
  36. * - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
  37. * (which may happen if NVS partition was truncated)
  38. * - ESP_ERR_NOT_FOUND if no partition with label "nvs" is found in the partition table
  39. * - one of the error codes from the underlying flash storage driver
  40. */
  41. esp_err_t nvs_flash_init(void);
  42. /**
  43. * @brief Initialize NVS flash storage for the specified partition.
  44. *
  45. * @param[in] partition_label Label of the partition. Must be no longer than 16 characters.
  46. *
  47. * @return
  48. * - ESP_OK if storage was successfully initialized.
  49. * - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
  50. * (which may happen if NVS partition was truncated)
  51. * - ESP_ERR_NOT_FOUND if specified partition is not found in the partition table
  52. * - one of the error codes from the underlying flash storage driver
  53. */
  54. esp_err_t nvs_flash_init_partition(const char *partition_label);
  55. /**
  56. * @brief Initialize NVS flash storage for the partition specified by partition pointer.
  57. *
  58. * @param[in] partition pointer to a partition obtained by the ESP partition API.
  59. *
  60. * @return
  61. * - ESP_OK if storage was successfully initialized
  62. * - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
  63. * (which may happen if NVS partition was truncated)
  64. * - ESP_ERR_INVALID_ARG in case partition is NULL
  65. * - one of the error codes from the underlying flash storage driver
  66. */
  67. esp_err_t nvs_flash_init_partition_ptr(const esp_partition_t *partition);
  68. /**
  69. * @brief Deinitialize NVS storage for the default NVS partition
  70. *
  71. * Default NVS partition is the partition with "nvs" label in the partition table.
  72. *
  73. * @return
  74. * - ESP_OK on success (storage was deinitialized)
  75. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage was not initialized prior to this call
  76. */
  77. esp_err_t nvs_flash_deinit(void);
  78. /**
  79. * @brief Deinitialize NVS storage for the given NVS partition
  80. *
  81. * @param[in] partition_label Label of the partition
  82. *
  83. * @return
  84. * - ESP_OK on success
  85. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage for given partition was not
  86. * initialized prior to this call
  87. */
  88. esp_err_t nvs_flash_deinit_partition(const char* partition_label);
  89. /**
  90. * @brief Erase the default NVS partition
  91. *
  92. * Erases all contents of the default NVS partition (one with label "nvs").
  93. *
  94. * @note If the partition is initialized, this function first de-initializes it. Afterwards, the partition has to
  95. * be initialized again to be used.
  96. *
  97. * @return
  98. * - ESP_OK on success
  99. * - ESP_ERR_NOT_FOUND if there is no NVS partition labeled "nvs" in the
  100. * partition table
  101. * - different error in case de-initialization fails (shouldn't happen)
  102. */
  103. esp_err_t nvs_flash_erase(void);
  104. /**
  105. * @brief Erase specified NVS partition
  106. *
  107. * Erase all content of a specified NVS partition
  108. *
  109. * @note If the partition is initialized, this function first de-initializes it. Afterwards, the partition has to
  110. * be initialized again to be used.
  111. *
  112. * @param[in] part_name Name (label) of the partition which should be erased
  113. *
  114. * @return
  115. * - ESP_OK on success
  116. * - ESP_ERR_NOT_FOUND if there is no NVS partition with the specified name
  117. * in the partition table
  118. * - different error in case de-initialization fails (shouldn't happen)
  119. */
  120. esp_err_t nvs_flash_erase_partition(const char *part_name);
  121. /**
  122. * @brief Erase custom partition.
  123. *
  124. * Erase all content of specified custom partition.
  125. *
  126. * @note
  127. * If the partition is initialized, this function first de-initializes it.
  128. * Afterwards, the partition has to be initialized again to be used.
  129. *
  130. * @param[in] partition pointer to a partition obtained by the ESP partition API.
  131. *
  132. * @return
  133. * - ESP_OK on success
  134. * - ESP_ERR_NOT_FOUND if there is no partition with the specified
  135. * parameters in the partition table
  136. * - ESP_ERR_INVALID_ARG in case partition is NULL
  137. * - one of the error codes from the underlying flash storage driver
  138. */
  139. esp_err_t nvs_flash_erase_partition_ptr(const esp_partition_t *partition);
  140. /**
  141. * @brief Initialize the default NVS partition.
  142. *
  143. * This API initialises the default NVS partition. The default NVS partition
  144. * is the one that is labeled "nvs" in the partition table.
  145. *
  146. * @param[in] cfg Security configuration (keys) to be used for NVS encryption/decryption.
  147. * If cfg is NULL, no encryption is used.
  148. *
  149. * @return
  150. * - ESP_OK if storage was successfully initialized.
  151. * - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
  152. * (which may happen if NVS partition was truncated)
  153. * - ESP_ERR_NOT_FOUND if no partition with label "nvs" is found in the partition table
  154. * - one of the error codes from the underlying flash storage driver
  155. */
  156. esp_err_t nvs_flash_secure_init(nvs_sec_cfg_t* cfg);
  157. /**
  158. * @brief Initialize NVS flash storage for the specified partition.
  159. *
  160. * @param[in] partition_label Label of the partition. Note that internally a reference to
  161. * passed value is kept and it should be accessible for future operations
  162. *
  163. * @param[in] cfg Security configuration (keys) to be used for NVS encryption/decryption.
  164. * If cfg is null, no encryption/decryption is used.
  165. * @return
  166. * - ESP_OK if storage was successfully initialized.
  167. * - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
  168. * (which may happen if NVS partition was truncated)
  169. * - ESP_ERR_NOT_FOUND if specified partition is not found in the partition table
  170. * - one of the error codes from the underlying flash storage driver
  171. */
  172. esp_err_t nvs_flash_secure_init_partition(const char *partition_label, nvs_sec_cfg_t* cfg);
  173. /**
  174. * @brief Generate and store NVS keys in the provided esp partition
  175. *
  176. * @param[in] partition Pointer to partition structure obtained using
  177. * esp_partition_find_first or esp_partition_get.
  178. * Must be non-NULL.
  179. * @param[out] cfg Pointer to nvs security configuration structure.
  180. * Pointer must be non-NULL.
  181. * Generated keys will be populated in this structure.
  182. *
  183. *
  184. * @return
  185. * -ESP_OK, if cfg was read successfully;
  186. * -or error codes from esp_partition_write/erase APIs.
  187. */
  188. esp_err_t nvs_flash_generate_keys(const esp_partition_t* partition, nvs_sec_cfg_t* cfg);
  189. /**
  190. * @brief Read NVS security configuration from a partition.
  191. *
  192. * @param[in] partition Pointer to partition structure obtained using
  193. * esp_partition_find_first or esp_partition_get.
  194. * Must be non-NULL.
  195. * @param[out] cfg Pointer to nvs security configuration structure.
  196. * Pointer must be non-NULL.
  197. *
  198. * @note Provided parition is assumed to be marked 'encrypted'.
  199. *
  200. * @return
  201. * -ESP_OK, if cfg was read successfully;
  202. * -ESP_ERR_NVS_KEYS_NOT_INITIALIZED, if the partition is not yet written with keys.
  203. * -ESP_ERR_NVS_CORRUPT_KEY_PART, if the partition containing keys is found to be corrupt
  204. * -or error codes from esp_partition_read API.
  205. */
  206. esp_err_t nvs_flash_read_security_cfg(const esp_partition_t* partition, nvs_sec_cfg_t* cfg);
  207. #ifdef __cplusplus
  208. }
  209. #endif
  210. #endif /* nvs_flash_h */