nvs_flash.h 10 KB

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