nvs_flash.h 8.6 KB

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