esp_efuse.h 15 KB

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