esp_efuse.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // Copyright 2017-2018 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <stdint.h>
  19. #include "esp_err.h"
  20. #include "esp_log.h"
  21. #include "sdkconfig.h"
  22. #if CONFIG_IDF_TARGET_ESP32
  23. #include "esp32/esp_efuse.h"
  24. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  25. #include "esp32s2beta/esp_efuse.h"
  26. #endif
  27. #define ESP_ERR_EFUSE 0x1600 /*!< Base error code for efuse api. */
  28. #define ESP_OK_EFUSE_CNT (ESP_ERR_EFUSE + 0x01) /*!< OK the required number of bits is set. */
  29. #define ESP_ERR_EFUSE_CNT_IS_FULL (ESP_ERR_EFUSE + 0x02) /*!< Error field is full. */
  30. #define ESP_ERR_EFUSE_REPEATED_PROG (ESP_ERR_EFUSE + 0x03) /*!< Error repeated programming of programmed bits is strictly forbidden. */
  31. #define ESP_ERR_CODING (ESP_ERR_EFUSE + 0x04) /*!< Error while a encoding operation. */
  32. /**
  33. * @brief Structure eFuse field
  34. */
  35. typedef struct {
  36. esp_efuse_block_t efuse_block: 8; /**< Block of eFuse */
  37. uint8_t bit_start; /**< Start bit [0..255] */
  38. uint16_t bit_count; /**< Length of bit field [1..-]*/
  39. } esp_efuse_desc_t;
  40. /**
  41. * @brief Reads bits from EFUSE field and writes it into an array.
  42. *
  43. * The number of read bits will be limited to the minimum value
  44. * from the description of the bits in "field" structure or "dst_size_bits" required size.
  45. * Use "esp_efuse_get_field_size()" function to determine the length of the field.
  46. * @param[in] field A pointer to the structure describing the fields of efuse.
  47. * @param[out] dst A pointer to array that will contain the result of reading.
  48. * @param[in] dst_size_bits The number of bits required to read.
  49. * If the requested number of bits is greater than the field,
  50. * the number will be limited to the field size.
  51. *
  52. * @return
  53. * - ESP_OK: The operation was successfully completed.
  54. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  55. */
  56. esp_err_t esp_efuse_read_field_blob(const esp_efuse_desc_t* field[], void* dst, size_t dst_size_bits);
  57. /**
  58. * @brief Reads bits from EFUSE field and returns number of bits programmed as "1".
  59. *
  60. * If the bits are set not sequentially, they will still be counted.
  61. * @param[in] field A pointer to the structure describing the fields of efuse.
  62. * @param[out] out_cnt A pointer that will contain the number of programmed as "1" bits.
  63. *
  64. * @return
  65. * - ESP_OK: The operation was successfully completed.
  66. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  67. */
  68. esp_err_t esp_efuse_read_field_cnt(const esp_efuse_desc_t* field[], size_t* out_cnt);
  69. /**
  70. * @brief Writes array to EFUSE field.
  71. *
  72. * The number of write bits will be limited to the minimum value
  73. * from the description of the bits in "field" structure or "src_size_bits" required size.
  74. * Use "esp_efuse_get_field_size()" function to determine the length of the field.
  75. * After the function is completed, the writing registers are cleared.
  76. * @param[in] field A pointer to the structure describing the fields of efuse.
  77. * @param[in] src A pointer to array that contains the data for writing.
  78. * @param[in] src_size_bits The number of bits required to write.
  79. *
  80. * @return
  81. * - ESP_OK: The operation was successfully completed.
  82. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  83. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  84. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  85. */
  86. esp_err_t esp_efuse_write_field_blob(const esp_efuse_desc_t* field[], const void* src, size_t src_size_bits);
  87. /**
  88. * @brief Writes a required count of bits as "1" to EFUSE field.
  89. *
  90. * If there are no free bits in the field to set the required number of bits to "1",
  91. * ESP_ERR_EFUSE_CNT_IS_FULL error is returned, the field will not be partially recorded.
  92. * After the function is completed, the writing registers are cleared.
  93. * @param[in] field A pointer to the structure describing the fields of efuse.
  94. * @param[in] cnt Required number of programmed as "1" bits.
  95. *
  96. * @return
  97. * - ESP_OK: The operation was successfully completed.
  98. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  99. * - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
  100. */
  101. esp_err_t esp_efuse_write_field_cnt(const esp_efuse_desc_t* field[], size_t cnt);
  102. /**
  103. * @brief Sets a write protection for the whole block.
  104. *
  105. * After that, it is impossible to write to this block.
  106. * The write protection does not apply to block 0.
  107. * @param[in] blk Block number of eFuse. (EFUSE_BLK1, EFUSE_BLK2 and EFUSE_BLK3)
  108. *
  109. * @return
  110. * - ESP_OK: The operation was successfully completed.
  111. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  112. * - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
  113. * - ESP_ERR_NOT_SUPPORTED: The block does not support this command.
  114. */
  115. esp_err_t esp_efuse_set_write_protect(esp_efuse_block_t blk);
  116. /**
  117. * @brief Sets a read protection for the whole block.
  118. *
  119. * After that, it is impossible to read from this block.
  120. * The read protection does not apply to block 0.
  121. * @param[in] blk Block number of eFuse. (EFUSE_BLK1, EFUSE_BLK2 and EFUSE_BLK3)
  122. *
  123. * @return
  124. * - ESP_OK: The operation was successfully completed.
  125. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  126. * - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
  127. * - ESP_ERR_NOT_SUPPORTED: The block does not support this command.
  128. */
  129. esp_err_t esp_efuse_set_read_protect(esp_efuse_block_t blk);
  130. /**
  131. * @brief Returns the number of bits used by field.
  132. *
  133. * @param[in] field A pointer to the structure describing the fields of efuse.
  134. *
  135. * @return Returns the number of bits used by field.
  136. */
  137. int esp_efuse_get_field_size(const esp_efuse_desc_t* field[]);
  138. /**
  139. * @brief Returns value of efuse register.
  140. *
  141. * This is a thread-safe implementation.
  142. * Example: EFUSE_BLK2_RDATA3_REG where (blk=2, num_reg=3)
  143. * @param[in] blk Block number of eFuse.
  144. * @param[in] num_reg The register number in the block.
  145. *
  146. * @return Value of register
  147. */
  148. uint32_t esp_efuse_read_reg(esp_efuse_block_t blk, unsigned int num_reg);
  149. /**
  150. * @brief Write value to efuse register.
  151. *
  152. * Apply a coding scheme if necessary.
  153. * This is a thread-safe implementation.
  154. * Example: EFUSE_BLK3_WDATA0_REG where (blk=3, num_reg=0)
  155. * @param[in] blk Block number of eFuse.
  156. * @param[in] num_reg The register number in the block.
  157. * @param[in] val Value to write.
  158. *
  159. * @return
  160. * - ESP_OK: The operation was successfully completed.
  161. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  162. */
  163. esp_err_t esp_efuse_write_reg(esp_efuse_block_t blk, unsigned int num_reg, uint32_t val);
  164. /**
  165. * @brief Return efuse coding scheme for blocks.
  166. *
  167. * Note: The coding scheme is applicable only to 1, 2 and 3 blocks. For 0 block, the coding scheme is always ``NONE``.
  168. *
  169. * @param[in] blk Block number of eFuse.
  170. * @return Return efuse coding scheme for blocks
  171. */
  172. esp_efuse_coding_scheme_t esp_efuse_get_coding_scheme(esp_efuse_block_t blk);
  173. /**
  174. * @brief Read key to efuse block starting at the offset and the required size.
  175. *
  176. * @param[in] blk Block number of eFuse.
  177. * @param[in] dst_key A pointer to array that will contain the result of reading.
  178. * @param[in] offset_in_bits Start bit in block.
  179. * @param[in] size_bits The number of bits required to read.
  180. *
  181. * @return
  182. * - ESP_OK: The operation was successfully completed.
  183. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  184. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  185. */
  186. esp_err_t esp_efuse_read_block(esp_efuse_block_t blk, void* dst_key, size_t offset_in_bits, size_t size_bits);
  187. /**
  188. * @brief Write key to efuse block starting at the offset and the required size.
  189. *
  190. * @param[in] blk Block number of eFuse.
  191. * @param[in] src_key A pointer to array that contains the key for writing.
  192. * @param[in] offset_in_bits Start bit in block.
  193. * @param[in] size_bits The number of bits required to write.
  194. *
  195. * @return
  196. * - ESP_OK: The operation was successfully completed.
  197. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  198. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  199. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits
  200. */
  201. esp_err_t esp_efuse_write_block(esp_efuse_block_t blk, const void* src_key, size_t offset_in_bits, size_t size_bits);
  202. /**
  203. * @brief Returns chip version from efuse
  204. *
  205. * @return chip version
  206. */
  207. uint8_t esp_efuse_get_chip_ver(void);
  208. /**
  209. * @brief Returns chip package from efuse
  210. *
  211. * @return chip package
  212. */
  213. uint32_t esp_efuse_get_pkg_ver(void);
  214. /* @brief Permanently update values written to the efuse write registers
  215. *
  216. * After updating EFUSE_BLKx_WDATAx_REG registers with new values to
  217. * write, call this function to permanently write them to efuse.
  218. *
  219. * @note Setting bits in efuse is permanent, they cannot be unset.
  220. *
  221. * @note Due to this restriction you don't need to copy values to
  222. * Efuse write registers from the matching read registers, bits which
  223. * are set in the read register but unset in the matching write
  224. * register will be unchanged when new values are burned.
  225. *
  226. * @note This function is not threadsafe, if calling code updates
  227. * efuse values from multiple tasks then this is caller's
  228. * responsibility to serialise.
  229. *
  230. * After burning new efuses, the read registers are updated to match
  231. * the new efuse values.
  232. */
  233. void esp_efuse_burn_new_values(void);
  234. /* @brief Reset efuse write registers
  235. *
  236. * Efuse write registers are written to zero, to negate
  237. * any changes that have been staged here.
  238. *
  239. * @note This function is not threadsafe, if calling code updates
  240. * efuse values from multiple tasks then this is caller's
  241. * responsibility to serialise.
  242. */
  243. void esp_efuse_reset(void);
  244. /* @brief Disable BASIC ROM Console via efuse
  245. *
  246. * By default, if booting from flash fails the ESP32 will boot a
  247. * BASIC console in ROM.
  248. *
  249. * Call this function (from bootloader or app) to permanently
  250. * disable the console on this chip.
  251. */
  252. void esp_efuse_disable_basic_rom_console(void);
  253. /* @brief Write random data to efuse key block write registers
  254. *
  255. * @note Caller is responsible for ensuring efuse
  256. * block is empty and not write protected, before calling.
  257. *
  258. * @note Behaviour depends on coding scheme: a 256-bit key is
  259. * generated and written for Coding Scheme "None", a 192-bit key
  260. * is generated, extended to 256-bits by the Coding Scheme,
  261. * and then writtten for 3/4 Coding Scheme.
  262. *
  263. * @note This function does not burn the new values, caller should
  264. * call esp_efuse_burn_new_values() when ready to do this.
  265. *
  266. * @param blk_wdata0_reg Address of the first data write register
  267. * in the block
  268. */
  269. void esp_efuse_write_random_key(uint32_t blk_wdata0_reg);
  270. /* @brief Return secure_version from efuse field.
  271. * @return Secure version from efuse field
  272. */
  273. uint32_t esp_efuse_read_secure_version(void);
  274. /* @brief Check secure_version from app and secure_version and from efuse field.
  275. *
  276. * @param secure_version Secure version from app.
  277. * @return
  278. * - True: If version of app is equal or more then secure_version from efuse.
  279. */
  280. bool esp_efuse_check_secure_version(uint32_t secure_version);
  281. /* @brief Write efuse field by secure_version value.
  282. *
  283. * Update the secure_version value is available if the coding scheme is None.
  284. * Note: Do not use this function in your applications. This function is called as part of the other API.
  285. *
  286. * @param[in] secure_version Secure version from app.
  287. * @return
  288. * - ESP_OK: Successful.
  289. * - ESP_FAIL: secure version of app cannot be set to efuse field.
  290. * - ESP_ERR_NOT_SUPPORTED: Anti rollback is not supported with the 3/4 and Repeat coding scheme.
  291. */
  292. esp_err_t esp_efuse_update_secure_version(uint32_t secure_version);
  293. /* @brief Initializes variables: offset and size to simulate the work of an eFuse.
  294. *
  295. * Note: To simulate the work of an eFuse need to set CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE option
  296. * and to add in the partition.csv file a line `efuse_em, data, efuse, , 0x2000,`.
  297. *
  298. * @param[in] offset The starting address of the partition where the eFuse data will be located.
  299. * @param[in] size The size of the partition.
  300. */
  301. void esp_efuse_init(uint32_t offset, uint32_t size);
  302. /* @brief Set the batch mode of writing fields.
  303. *
  304. * This mode allows you to write the fields in the batch mode.
  305. * If this mode is enabled, esp_efuse_batch_write_commit() must be called
  306. * to actually burn any written efuses.
  307. * In this mode, reading efuse is not possible.
  308. * This mode should be used when burning several efuses at one time.
  309. *
  310. * \code{c}
  311. * // Example of using the batch writing mode.
  312. *
  313. * // set the batch writing mode
  314. * esp_efuse_batch_write_begin();
  315. *
  316. * // use any writing functions as usual
  317. * esp_efuse_write_field_blob(ESP_EFUSE_...);
  318. * esp_efuse_write_field_cnt(ESP_EFUSE_...);
  319. * esp_efuse_set_write_protect(EFUSE_BLKx);
  320. * esp_efuse_write_reg(EFUSE_BLKx, ...);
  321. * esp_efuse_write_block(EFUSE_BLKx, ...);
  322. * ...
  323. *
  324. * // Write all of these fields to the efuse registers
  325. * esp_efuse_batch_write_commit();
  326. *
  327. * \endcode
  328. *
  329. * @return
  330. * - ESP_OK: Successful.
  331. */
  332. esp_err_t esp_efuse_batch_write_begin(void);
  333. /* @brief Reset the batch mode of writing fields.
  334. *
  335. * It will reset the batch writing mode and any written changes.
  336. *
  337. * @return
  338. * - ESP_OK: Successful.
  339. * - ESP_ERR_INVALID_STATE: Tha batch mode was not set.
  340. */
  341. esp_err_t esp_efuse_batch_write_cancel(void);
  342. /* @brief Writes all prepared data for the batch mode.
  343. *
  344. * Must be called to ensure changes are written to the efuse registers.
  345. * After this the batch writing mode will be reset.
  346. *
  347. * @return
  348. * - ESP_OK: Successful.
  349. * - ESP_ERR_INVALID_STATE: The deferred writing mode was not set.
  350. */
  351. esp_err_t esp_efuse_batch_write_commit(void);
  352. #ifdef __cplusplus
  353. }
  354. #endif