efuse.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ROM_EFUSE_H_
  7. #define _ROM_EFUSE_H_
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include <stdint.h>
  12. #include <stddef.h>
  13. #include <stdbool.h>
  14. /** \defgroup efuse_APIs efuse APIs
  15. * @brief ESP32 efuse read/write APIs
  16. * @attention
  17. *
  18. */
  19. /** @addtogroup efuse_APIs
  20. * @{
  21. */
  22. typedef enum {
  23. ETS_EFUSE_KEY_PURPOSE_USER = 0,
  24. ETS_EFUSE_KEY_PURPOSE_ECDSA_KEY = 1,
  25. ETS_EFUSE_KEY_PURPOSE_RESERVED = 2,
  26. ETS_EFUSE_KEY_PURPOSE_XTS_AES_128_KEY = 4,
  27. ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_ALL = 5,
  28. ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_JTAG = 6,
  29. ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_DIGITAL_SIGNATURE = 7,
  30. ETS_EFUSE_KEY_PURPOSE_HMAC_UP = 8,
  31. ETS_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST0 = 9,
  32. ETS_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST1 = 10,
  33. ETS_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST2 = 11,
  34. ETS_EFUSE_KEY_PURPOSE_MAX,
  35. } ets_efuse_purpose_t;
  36. typedef enum {
  37. ETS_EFUSE_BLOCK0 = 0,
  38. ETS_EFUSE_MAC_SPI_SYS_0 = 1,
  39. ETS_EFUSE_BLOCK_SYS_DATA = 2,
  40. ETS_EFUSE_BLOCK_USR_DATA = 3,
  41. ETS_EFUSE_BLOCK_KEY0 = 4,
  42. ETS_EFUSE_BLOCK_KEY1 = 5,
  43. ETS_EFUSE_BLOCK_KEY2 = 6,
  44. ETS_EFUSE_BLOCK_KEY3 = 7,
  45. ETS_EFUSE_BLOCK_KEY4 = 8,
  46. ETS_EFUSE_BLOCK_KEY5 = 9,
  47. ETS_EFUSE_BLOCK_KEY6 = 10,
  48. ETS_EFUSE_BLOCK_MAX,
  49. } ets_efuse_block_t;
  50. /**
  51. * @brief Efuse read operation: copies data from physical efuses to efuse read registers.
  52. *
  53. * @param null
  54. *
  55. * @return : 0 if success, others if apb clock is not accepted
  56. */
  57. int ets_efuse_read(void);
  58. /**
  59. * @brief Efuse write operation: Copies data from efuse write registers to efuse. Operates on a single block of efuses at a time.
  60. *
  61. * @note This function does not update read efuses, call ets_efuse_read() once all programming is complete.
  62. *
  63. * @return : 0 if success, others if apb clock is not accepted
  64. */
  65. int ets_efuse_program(ets_efuse_block_t block);
  66. /**
  67. * @brief Set all Efuse program registers to zero.
  68. *
  69. * Call this before writing new data to the program registers.
  70. */
  71. void ets_efuse_clear_program_registers(void);
  72. /**
  73. * @brief Program a block of key data to an efuse block
  74. *
  75. * @param key_block Block to read purpose for. Must be in range ETS_EFUSE_BLOCK_KEY0 to ETS_EFUSE_BLOCK_KEY6. Key block must be unused (@ref ets_efuse_key_block_unused).
  76. * @param purpose Purpose to set for this key. Purpose must be already unset.
  77. * @param data Pointer to data to write.
  78. * @param data_len Length of data to write.
  79. *
  80. * @note This function also calls ets_efuse_program() for the specified block, and for block 0 (setting the purpose)
  81. */
  82. int ets_efuse_write_key(ets_efuse_block_t key_block, ets_efuse_purpose_t purpose, const void *data, size_t data_len);
  83. /* @brief Return the address of a particular efuse block's first read register
  84. *
  85. * @param block Index of efuse block to look up
  86. *
  87. * @return 0 if block is invalid, otherwise a numeric read register address
  88. * of the first word in the block.
  89. */
  90. uint32_t ets_efuse_get_read_register_address(ets_efuse_block_t block);
  91. /**
  92. * @brief Return the current purpose set for an efuse key block
  93. *
  94. * @param key_block Block to read purpose for. Must be in range ETS_EFUSE_BLOCK_KEY0 to ETS_EFUSE_BLOCK_KEY6.
  95. */
  96. ets_efuse_purpose_t ets_efuse_get_key_purpose(ets_efuse_block_t key_block);
  97. /**
  98. * @brief Find a key block with the particular purpose set
  99. *
  100. * @param purpose Purpose to search for.
  101. * @param[out] key_block Pointer which will be set to the key block if found. Can be NULL, if only need to test the key block exists.
  102. * @return true if found, false if not found. If false, value at key_block pointer is unchanged.
  103. */
  104. bool ets_efuse_find_purpose(ets_efuse_purpose_t purpose, ets_efuse_block_t *key_block);
  105. /**
  106. * Return true if the key block is unused, false otherwise.
  107. *
  108. * An unused key block is all zero content, not read or write protected,
  109. * and has purpose 0 (ETS_EFUSE_KEY_PURPOSE_USER)
  110. *
  111. * @param key_block key block to check.
  112. *
  113. * @return true if key block is unused, false if key block or used
  114. * or the specified block index is not a key block.
  115. */
  116. bool ets_efuse_key_block_unused(ets_efuse_block_t key_block);
  117. /**
  118. * @brief Search for an unused key block and return the first one found.
  119. *
  120. * See @ref ets_efuse_key_block_unused for a description of an unused key block.
  121. *
  122. * @return First unused key block, or ETS_EFUSE_BLOCK_MAX if no unused key block is found.
  123. */
  124. ets_efuse_block_t ets_efuse_find_unused_key_block(void);
  125. /**
  126. * @brief Return the number of unused efuse key blocks (0-6)
  127. */
  128. unsigned ets_efuse_count_unused_key_blocks(void);
  129. /**
  130. * @brief Calculate Reed-Solomon Encoding values for a block of efuse data.
  131. *
  132. * @param data Pointer to data buffer (length 32 bytes)
  133. * @param rs_values Pointer to write encoded data to (length 12 bytes)
  134. */
  135. void ets_efuse_rs_calculate(const void *data, void *rs_values);
  136. /**
  137. * @brief Read if download mode disabled from Efuse
  138. *
  139. * @return
  140. * - true for efuse disable download mode.
  141. * - false for efuse doesn't disable download mode.
  142. */
  143. bool ets_efuse_download_modes_disabled(void);
  144. /**
  145. * @brief Read if uart print control value from Efuse
  146. *
  147. * @return
  148. * - 0 for uart force print.
  149. * - 1 for uart print when GPIO8 is low when digital reset.
  150. * 2 for uart print when GPIO8 is high when digital reset.
  151. * 3 for uart force slient
  152. */
  153. uint32_t ets_efuse_get_uart_print_control(void);
  154. /**
  155. * @brief Read if usb download mode disabled from Efuse
  156. *
  157. * (Also returns true if security download mode is enabled, as this mode
  158. * disables USB download.)
  159. *
  160. * @return
  161. * - true for efuse disable usb download mode.
  162. * - false for efuse doesn't disable usb download mode.
  163. */
  164. bool ets_efuse_usb_download_mode_disabled(void);
  165. /**
  166. * @brief Read if security download modes enabled from Efuse
  167. *
  168. * @return
  169. * - true for efuse enable security download mode.
  170. * - false for efuse doesn't enable security download mode.
  171. */
  172. bool ets_efuse_security_download_modes_enabled(void);
  173. /**
  174. * @brief Return true if secure boot is enabled in EFuse
  175. */
  176. bool ets_efuse_secure_boot_enabled(void);
  177. /**
  178. * @brief Return true if secure boot aggressive revoke is enabled in EFuse
  179. */
  180. bool ets_efuse_secure_boot_aggressive_revoke_enabled(void);
  181. /**
  182. * @brief Return true if cache encryption (flash, etc) is enabled from boot via EFuse
  183. */
  184. bool ets_efuse_cache_encryption_enabled(void);
  185. /**
  186. * @brief Return true if EFuse indicates to send a flash resume command.
  187. */
  188. bool ets_efuse_force_send_resume(void);
  189. /**
  190. * @brief return the time in us ROM boot need wait flash to power on from Efuse
  191. *
  192. * @return
  193. * - uint32_t the time in us.
  194. */
  195. uint32_t ets_efuse_get_flash_delay_us(void);
  196. #define EFUSE_SPICONFIG_SPI_DEFAULTS 0
  197. #define EFUSE_SPICONFIG_HSPI_DEFAULTS 1
  198. #define EFUSE_SPICONFIG_RET_SPICLK_MASK 0x3f
  199. #define EFUSE_SPICONFIG_RET_SPICLK_SHIFT 0
  200. #define EFUSE_SPICONFIG_RET_SPICLK(ret) (((ret) >> EFUSE_SPICONFIG_RET_SPICLK_SHIFT) & EFUSE_SPICONFIG_RET_SPICLK_MASK)
  201. #define EFUSE_SPICONFIG_RET_SPIQ_MASK 0x3f
  202. #define EFUSE_SPICONFIG_RET_SPIQ_SHIFT 6
  203. #define EFUSE_SPICONFIG_RET_SPIQ(ret) (((ret) >> EFUSE_SPICONFIG_RET_SPIQ_SHIFT) & EFUSE_SPICONFIG_RET_SPIQ_MASK)
  204. #define EFUSE_SPICONFIG_RET_SPID_MASK 0x3f
  205. #define EFUSE_SPICONFIG_RET_SPID_SHIFT 12
  206. #define EFUSE_SPICONFIG_RET_SPID(ret) (((ret) >> EFUSE_SPICONFIG_RET_SPID_SHIFT) & EFUSE_SPICONFIG_RET_SPID_MASK)
  207. #define EFUSE_SPICONFIG_RET_SPICS0_MASK 0x3f
  208. #define EFUSE_SPICONFIG_RET_SPICS0_SHIFT 18
  209. #define EFUSE_SPICONFIG_RET_SPICS0(ret) (((ret) >> EFUSE_SPICONFIG_RET_SPICS0_SHIFT) & EFUSE_SPICONFIG_RET_SPICS0_MASK)
  210. #define EFUSE_SPICONFIG_RET_SPIHD_MASK 0x3f
  211. #define EFUSE_SPICONFIG_RET_SPIHD_SHIFT 24
  212. #define EFUSE_SPICONFIG_RET_SPIHD(ret) (((ret) >> EFUSE_SPICONFIG_RET_SPIHD_SHIFT) & EFUSE_SPICONFIG_RET_SPIHD_MASK)
  213. /**
  214. * @brief Enable JTAG temporarily by writing a JTAG HMAC "key" into
  215. * the JTAG_CTRL registers.
  216. *
  217. * Works if JTAG has been "soft" disabled by burning the EFUSE_SOFT_DIS_JTAG efuse.
  218. *
  219. * Will enable the HMAC module to generate a "downstream" HMAC value from a key already saved in efuse, and then write the JTAG HMAC "key" which will enable JTAG if the two keys match.
  220. *
  221. * @param jtag_hmac_key Pointer to a 32 byte array containing a valid key. Supplied by user.
  222. * @param key_block Index of a key block containing the source for this key.
  223. *
  224. * @return ETS_FAILED if HMAC operation fails or invalid parameter, ETS_OK otherwise. ETS_OK doesn't necessarily mean that JTAG was enabled.
  225. */
  226. int ets_jtag_enable_temporarily(const uint8_t *jtag_hmac_key, ets_efuse_block_t key_block);
  227. /**
  228. * @brief A crc8 algorithm used for MAC addresses in efuse
  229. *
  230. * @param unsigned char const *p : Pointer to original data.
  231. *
  232. * @param unsigned int len : Data length in byte.
  233. *
  234. * @return unsigned char: Crc value.
  235. */
  236. unsigned char esp_crc8(unsigned char const *p, unsigned int len);
  237. /**
  238. * @}
  239. */
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243. #endif /* _ROM_EFUSE_H_ */