esp_efuse.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include <stdint.h>
  11. #include "esp_err.h"
  12. #include "esp_log.h"
  13. #include "soc/soc_caps.h"
  14. #include "sdkconfig.h"
  15. #include_next "esp_efuse.h"
  16. #if CONFIG_IDF_TARGET_ESP32
  17. #include "esp32/rom/secure_boot.h"
  18. #elif CONFIG_IDF_TARGET_ESP32S2
  19. #include "esp32s2/rom/secure_boot.h"
  20. #elif CONFIG_IDF_TARGET_ESP32C3
  21. #include "esp32c3/rom/secure_boot.h"
  22. #elif CONFIG_IDF_TARGET_ESP32S3
  23. #include "esp32s3/rom/secure_boot.h"
  24. #elif CONFIG_IDF_TARGET_ESP32H2
  25. #include "esp32h2/rom/secure_boot.h"
  26. #elif CONFIG_IDF_TARGET_ESP8684
  27. #include "esp8684/rom/secure_boot.h"
  28. #endif
  29. #define ESP_ERR_EFUSE 0x1600 /*!< Base error code for efuse api. */
  30. #define ESP_OK_EFUSE_CNT (ESP_ERR_EFUSE + 0x01) /*!< OK the required number of bits is set. */
  31. #define ESP_ERR_EFUSE_CNT_IS_FULL (ESP_ERR_EFUSE + 0x02) /*!< Error field is full. */
  32. #define ESP_ERR_EFUSE_REPEATED_PROG (ESP_ERR_EFUSE + 0x03) /*!< Error repeated programming of programmed bits is strictly forbidden. */
  33. #define ESP_ERR_CODING (ESP_ERR_EFUSE + 0x04) /*!< Error while a encoding operation. */
  34. #define ESP_ERR_NOT_ENOUGH_UNUSED_KEY_BLOCKS (ESP_ERR_EFUSE + 0x05) /*!< Error not enough unused key blocks available */
  35. #define ESP_ERR_DAMAGED_READING (ESP_ERR_EFUSE + 0x06) /*!< Error. Burn or reset was done during a reading operation leads to damage read data. This error is internal to the efuse component and not returned by any public API. */
  36. /**
  37. * @brief Type definition for an eFuse field
  38. */
  39. typedef struct {
  40. esp_efuse_block_t efuse_block: 8; /**< Block of eFuse */
  41. uint8_t bit_start; /**< Start bit [0..255] */
  42. uint16_t bit_count; /**< Length of bit field [1..-]*/
  43. } esp_efuse_desc_t;
  44. /**
  45. * @brief Type definition for ROM log scheme
  46. */
  47. typedef enum {
  48. ESP_EFUSE_ROM_LOG_ALWAYS_ON, /**< Always enable ROM logging */
  49. ESP_EFUSE_ROM_LOG_ON_GPIO_LOW, /**< ROM logging is enabled when specific GPIO level is low during start up */
  50. ESP_EFUSE_ROM_LOG_ON_GPIO_HIGH, /**< ROM logging is enabled when specific GPIO level is high during start up */
  51. ESP_EFUSE_ROM_LOG_ALWAYS_OFF /**< Disable ROM logging permanently */
  52. } esp_efuse_rom_log_scheme_t;
  53. /**
  54. * @brief Reads bits from EFUSE field and writes it into an array.
  55. *
  56. * The number of read bits will be limited to the minimum value
  57. * from the description of the bits in "field" structure or "dst_size_bits" required size.
  58. * Use "esp_efuse_get_field_size()" function to determine the length of the field.
  59. *
  60. * @note Please note that reading in the batch mode does not show uncommitted changes.
  61. *
  62. * @param[in] field A pointer to the structure describing the fields of efuse.
  63. * @param[out] dst A pointer to array that will contain the result of reading.
  64. * @param[in] dst_size_bits The number of bits required to read.
  65. * If the requested number of bits is greater than the field,
  66. * the number will be limited to the field size.
  67. *
  68. * @return
  69. * - ESP_OK: The operation was successfully completed.
  70. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  71. */
  72. esp_err_t esp_efuse_read_field_blob(const esp_efuse_desc_t* field[], void* dst, size_t dst_size_bits);
  73. /**
  74. * @brief Read a single bit eFuse field as a boolean value.
  75. *
  76. * @note The value must exist and must be a single bit wide. If there is any possibility of an error
  77. * in the provided arguments, call esp_efuse_read_field_blob() and check the returned value instead.
  78. *
  79. * @note If assertions are enabled and the parameter is invalid, execution will abort
  80. * @note Please note that reading in the batch mode does not show uncommitted changes.
  81. *
  82. * @param[in] field A pointer to the structure describing the fields of efuse.
  83. * @return
  84. * - true: The field parameter is valid and the bit is set.
  85. * - false: The bit is not set, or the parameter is invalid and assertions are disabled.
  86. *
  87. */
  88. bool esp_efuse_read_field_bit(const esp_efuse_desc_t *field[]);
  89. /**
  90. * @brief Reads bits from EFUSE field and returns number of bits programmed as "1".
  91. *
  92. * If the bits are set not sequentially, they will still be counted.
  93. * @note Please note that reading in the batch mode does not show uncommitted changes.
  94. *
  95. * @param[in] field A pointer to the structure describing the fields of efuse.
  96. * @param[out] out_cnt A pointer that will contain the number of programmed as "1" bits.
  97. *
  98. * @return
  99. * - ESP_OK: The operation was successfully completed.
  100. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  101. */
  102. esp_err_t esp_efuse_read_field_cnt(const esp_efuse_desc_t* field[], size_t* out_cnt);
  103. /**
  104. * @brief Writes array to EFUSE field.
  105. *
  106. * The number of write bits will be limited to the minimum value
  107. * from the description of the bits in "field" structure or "src_size_bits" required size.
  108. * Use "esp_efuse_get_field_size()" function to determine the length of the field.
  109. * After the function is completed, the writing registers are cleared.
  110. * @param[in] field A pointer to the structure describing the fields of efuse.
  111. * @param[in] src A pointer to array that contains the data for writing.
  112. * @param[in] src_size_bits The number of bits required to write.
  113. *
  114. * @return
  115. * - ESP_OK: The operation was successfully completed.
  116. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  117. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  118. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  119. */
  120. esp_err_t esp_efuse_write_field_blob(const esp_efuse_desc_t* field[], const void* src, size_t src_size_bits);
  121. /**
  122. * @brief Writes a required count of bits as "1" to EFUSE field.
  123. *
  124. * If there are no free bits in the field to set the required number of bits to "1",
  125. * ESP_ERR_EFUSE_CNT_IS_FULL error is returned, the field will not be partially recorded.
  126. * After the function is completed, the writing registers are cleared.
  127. * @param[in] field A pointer to the structure describing the fields of efuse.
  128. * @param[in] cnt Required number of programmed as "1" bits.
  129. *
  130. * @return
  131. * - ESP_OK: The operation was successfully completed.
  132. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  133. * - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
  134. */
  135. esp_err_t esp_efuse_write_field_cnt(const esp_efuse_desc_t* field[], size_t cnt);
  136. /**
  137. * @brief Write a single bit eFuse field to 1
  138. *
  139. * For use with eFuse fields that are a single bit. This function will write the bit to value 1 if
  140. * it is not already set, or does nothing if the bit is already set.
  141. *
  142. * This is equivalent to calling esp_efuse_write_field_cnt() with the cnt parameter equal to 1,
  143. * except that it will return ESP_OK if the field is already set to 1.
  144. *
  145. * @param[in] field Pointer to the structure describing the efuse field.
  146. *
  147. * @return
  148. * - ESP_OK: The operation was successfully completed, or the bit was already set to value 1.
  149. * - ESP_ERR_INVALID_ARG: Error in the passed arugments, including if the efuse field is not 1 bit wide.
  150. */
  151. esp_err_t esp_efuse_write_field_bit(const esp_efuse_desc_t* field[]);
  152. /**
  153. * @brief Sets a write protection for the whole block.
  154. *
  155. * After that, it is impossible to write to this block.
  156. * The write protection does not apply to block 0.
  157. * @param[in] blk Block number of eFuse. (EFUSE_BLK1, EFUSE_BLK2 and EFUSE_BLK3)
  158. *
  159. * @return
  160. * - ESP_OK: The operation was successfully completed.
  161. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  162. * - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
  163. * - ESP_ERR_NOT_SUPPORTED: The block does not support this command.
  164. */
  165. esp_err_t esp_efuse_set_write_protect(esp_efuse_block_t blk);
  166. /**
  167. * @brief Sets a read protection for the whole block.
  168. *
  169. * After that, it is impossible to read from this block.
  170. * The read protection does not apply to block 0.
  171. * @param[in] blk Block number of eFuse. (EFUSE_BLK1, EFUSE_BLK2 and EFUSE_BLK3)
  172. *
  173. * @return
  174. * - ESP_OK: The operation was successfully completed.
  175. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  176. * - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
  177. * - ESP_ERR_NOT_SUPPORTED: The block does not support this command.
  178. */
  179. esp_err_t esp_efuse_set_read_protect(esp_efuse_block_t blk);
  180. /**
  181. * @brief Returns the number of bits used by field.
  182. *
  183. * @param[in] field A pointer to the structure describing the fields of efuse.
  184. *
  185. * @return Returns the number of bits used by field.
  186. */
  187. int esp_efuse_get_field_size(const esp_efuse_desc_t* field[]);
  188. /**
  189. * @brief Returns value of efuse register.
  190. *
  191. * This is a thread-safe implementation.
  192. * Example: EFUSE_BLK2_RDATA3_REG where (blk=2, num_reg=3)
  193. * @note Please note that reading in the batch mode does not show uncommitted changes.
  194. *
  195. * @param[in] blk Block number of eFuse.
  196. * @param[in] num_reg The register number in the block.
  197. *
  198. * @return Value of register
  199. */
  200. uint32_t esp_efuse_read_reg(esp_efuse_block_t blk, unsigned int num_reg);
  201. /**
  202. * @brief Write value to efuse register.
  203. *
  204. * Apply a coding scheme if necessary.
  205. * This is a thread-safe implementation.
  206. * Example: EFUSE_BLK3_WDATA0_REG where (blk=3, num_reg=0)
  207. * @param[in] blk Block number of eFuse.
  208. * @param[in] num_reg The register number in the block.
  209. * @param[in] val Value to write.
  210. *
  211. * @return
  212. * - ESP_OK: The operation was successfully completed.
  213. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  214. */
  215. esp_err_t esp_efuse_write_reg(esp_efuse_block_t blk, unsigned int num_reg, uint32_t val);
  216. /**
  217. * @brief Return efuse coding scheme for blocks.
  218. *
  219. * Note: The coding scheme is applicable only to 1, 2 and 3 blocks. For 0 block, the coding scheme is always ``NONE``.
  220. *
  221. * @param[in] blk Block number of eFuse.
  222. * @return Return efuse coding scheme for blocks
  223. */
  224. esp_efuse_coding_scheme_t esp_efuse_get_coding_scheme(esp_efuse_block_t blk);
  225. /**
  226. * @brief Read key to efuse block starting at the offset and the required size.
  227. *
  228. * @note Please note that reading in the batch mode does not show uncommitted changes.
  229. *
  230. * @param[in] blk Block number of eFuse.
  231. * @param[in] dst_key A pointer to array that will contain the result of reading.
  232. * @param[in] offset_in_bits Start bit in block.
  233. * @param[in] size_bits The number of bits required to read.
  234. *
  235. * @return
  236. * - ESP_OK: The operation was successfully completed.
  237. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  238. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  239. */
  240. esp_err_t esp_efuse_read_block(esp_efuse_block_t blk, void* dst_key, size_t offset_in_bits, size_t size_bits);
  241. /**
  242. * @brief Write key to efuse block starting at the offset and the required size.
  243. *
  244. * @param[in] blk Block number of eFuse.
  245. * @param[in] src_key A pointer to array that contains the key for writing.
  246. * @param[in] offset_in_bits Start bit in block.
  247. * @param[in] size_bits The number of bits required to write.
  248. *
  249. * @return
  250. * - ESP_OK: The operation was successfully completed.
  251. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  252. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  253. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits
  254. */
  255. 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);
  256. /**
  257. * @brief Returns chip version from efuse
  258. *
  259. * @return chip version
  260. */
  261. uint8_t esp_efuse_get_chip_ver(void);
  262. /**
  263. * @brief Returns chip package from efuse
  264. *
  265. * @return chip package
  266. */
  267. uint32_t esp_efuse_get_pkg_ver(void);
  268. /**
  269. * @brief Reset efuse write registers
  270. *
  271. * Efuse write registers are written to zero, to negate
  272. * any changes that have been staged here.
  273. *
  274. * @note This function is not threadsafe, if calling code updates
  275. * efuse values from multiple tasks then this is caller's
  276. * responsibility to serialise.
  277. */
  278. void esp_efuse_reset(void);
  279. #ifdef CONFIG_IDF_TARGET_ESP32
  280. /**
  281. * @brief Disable BASIC ROM Console via efuse
  282. *
  283. * By default, if booting from flash fails the ESP32 will boot a
  284. * BASIC console in ROM.
  285. *
  286. * Call this function (from bootloader or app) to permanently disable the console on this chip.
  287. *
  288. */
  289. void esp_efuse_disable_basic_rom_console(void);
  290. #endif
  291. /**
  292. * @brief Disable ROM Download Mode via eFuse
  293. *
  294. * Permanently disables the ROM Download Mode feature. Once disabled, if the SoC is booted with
  295. * strapping pins set for ROM Download Mode then an error is printed instead.
  296. *
  297. * @note Not all SoCs support this option. An error will be returned if called on an ESP32
  298. * with a silicon revision lower than 3, as these revisions do not support this option.
  299. *
  300. * @note If ROM Download Mode is already disabled, this function does nothing and returns success.
  301. *
  302. * @return
  303. * - ESP_OK If the eFuse was successfully burned, or had already been burned.
  304. * - ESP_ERR_NOT_SUPPORTED (ESP32 only) This SoC is not capable of disabling UART download mode
  305. * - ESP_ERR_INVALID_STATE (ESP32 only) This eFuse is write protected and cannot be written
  306. */
  307. esp_err_t esp_efuse_disable_rom_download_mode(void);
  308. /**
  309. * @brief Set boot ROM log scheme via eFuse
  310. *
  311. * @note By default, the boot ROM will always print to console. This API can be called to set the log scheme only once per chip,
  312. * once the value is changed from the default it can't be changed again.
  313. *
  314. * @param log_scheme Supported ROM log scheme
  315. * @return
  316. * - ESP_OK If the eFuse was successfully burned, or had already been burned.
  317. * - ESP_ERR_NOT_SUPPORTED (ESP32 only) This SoC is not capable of setting ROM log scheme
  318. * - ESP_ERR_INVALID_STATE This eFuse is write protected or has been burned already
  319. */
  320. esp_err_t esp_efuse_set_rom_log_scheme(esp_efuse_rom_log_scheme_t log_scheme);
  321. #if SOC_SUPPORTS_SECURE_DL_MODE
  322. /**
  323. * @brief Switch ROM Download Mode to Secure Download mode via eFuse
  324. *
  325. * Permanently enables Secure Download mode. This mode limits the use of ROM Download Mode functions
  326. * to simple flash read, write and erase operations, plus a command to return a summary of currently
  327. * enabled security features.
  328. *
  329. * @note If Secure Download mode is already enabled, this function does nothing and returns success.
  330. *
  331. * @note Disabling the ROM Download Mode also disables Secure Download Mode.
  332. *
  333. * @return
  334. * - ESP_OK If the eFuse was successfully burned, or had already been burned.
  335. * - ESP_ERR_INVALID_STATE ROM Download Mode has been disabled via eFuse, so Secure Download mode is unavailable.
  336. */
  337. esp_err_t esp_efuse_enable_rom_secure_download_mode(void);
  338. #endif
  339. /**
  340. * @brief Return secure_version from efuse field.
  341. * @return Secure version from efuse field
  342. */
  343. uint32_t esp_efuse_read_secure_version(void);
  344. /**
  345. * @brief Check secure_version from app and secure_version and from efuse field.
  346. *
  347. * @param secure_version Secure version from app.
  348. * @return
  349. * - True: If version of app is equal or more then secure_version from efuse.
  350. */
  351. bool esp_efuse_check_secure_version(uint32_t secure_version);
  352. /**
  353. * @brief Write efuse field by secure_version value.
  354. *
  355. * Update the secure_version value is available if the coding scheme is None.
  356. * Note: Do not use this function in your applications. This function is called as part of the other API.
  357. *
  358. * @param[in] secure_version Secure version from app.
  359. * @return
  360. * - ESP_OK: Successful.
  361. * - ESP_FAIL: secure version of app cannot be set to efuse field.
  362. * - ESP_ERR_NOT_SUPPORTED: Anti rollback is not supported with the 3/4 and Repeat coding scheme.
  363. */
  364. esp_err_t esp_efuse_update_secure_version(uint32_t secure_version);
  365. #if defined(BOOTLOADER_BUILD) && defined(CONFIG_EFUSE_VIRTUAL) && !defined(CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH)
  366. /**
  367. * @brief Initializes eFuses API to keep eFuses in RAM.
  368. *
  369. * This function just copies all eFuses to RAM. IDF eFuse APIs perform all operators with RAM instead of real eFuse.
  370. * (Used only in bootloader).
  371. */
  372. void esp_efuse_init_virtual_mode_in_ram(void);
  373. #endif
  374. #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  375. /**
  376. * @brief Initializes variables: offset and size to simulate the work of an eFuse.
  377. *
  378. * Note: To simulate the work of an eFuse need to set CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH option
  379. * and to add in the partition.csv file a line `efuse_em, data, efuse, , 0x2000,`.
  380. *
  381. * @param[in] offset The starting address of the partition where the eFuse data will be located.
  382. * @param[in] size The size of the partition.
  383. */
  384. void esp_efuse_init_virtual_mode_in_flash(uint32_t offset, uint32_t size);
  385. #endif
  386. /**
  387. * @brief Set the batch mode of writing fields.
  388. *
  389. * This mode allows you to write the fields in the batch mode when need to burn several efuses at one time.
  390. * To enable batch mode call begin() then perform as usually the necessary operations
  391. * read and write and at the end call commit() to actually burn all written efuses.
  392. * The batch mode can be used nested. The commit will be done by the last commit() function.
  393. * The number of begin() functions should be equal to the number of commit() functions.
  394. *
  395. * @note Please note that reading in the batch mode does not show uncommitted changes.
  396. *
  397. * Note: If batch mode is enabled by the first task, at this time the second task cannot write/read efuses.
  398. * The second task will wait for the first task to complete the batch operation.
  399. *
  400. * \code{c}
  401. * // Example of using the batch writing mode.
  402. *
  403. * // set the batch writing mode
  404. * esp_efuse_batch_write_begin();
  405. *
  406. * // use any writing functions as usual
  407. * esp_efuse_write_field_blob(ESP_EFUSE_...);
  408. * esp_efuse_write_field_cnt(ESP_EFUSE_...);
  409. * esp_efuse_set_write_protect(EFUSE_BLKx);
  410. * esp_efuse_write_reg(EFUSE_BLKx, ...);
  411. * esp_efuse_write_block(EFUSE_BLKx, ...);
  412. * esp_efuse_write(ESP_EFUSE_1, 3); // ESP_EFUSE_1 == 1, here we write a new value = 3. The changes will be burn by the commit() function.
  413. * esp_efuse_read_...(ESP_EFUSE_1); // this function returns ESP_EFUSE_1 == 1 because uncommitted changes are not readable, it will be available only after commit.
  414. * ...
  415. *
  416. * // esp_efuse_batch_write APIs can be called recursively.
  417. * esp_efuse_batch_write_begin();
  418. * esp_efuse_set_write_protect(EFUSE_BLKx);
  419. * esp_efuse_batch_write_commit(); // the burn will be skipped here, it will be done in the last commit().
  420. *
  421. * ...
  422. *
  423. * // Write all of these fields to the efuse registers
  424. * esp_efuse_batch_write_commit();
  425. * esp_efuse_read_...(ESP_EFUSE_1); // this function returns ESP_EFUSE_1 == 3.
  426. *
  427. * \endcode
  428. *
  429. * @return
  430. * - ESP_OK: Successful.
  431. */
  432. esp_err_t esp_efuse_batch_write_begin(void);
  433. /**
  434. * @brief Reset the batch mode of writing fields.
  435. *
  436. * It will reset the batch writing mode and any written changes.
  437. *
  438. * @return
  439. * - ESP_OK: Successful.
  440. * - ESP_ERR_INVALID_STATE: Tha batch mode was not set.
  441. */
  442. esp_err_t esp_efuse_batch_write_cancel(void);
  443. /**
  444. * @brief Writes all prepared data for the batch mode.
  445. *
  446. * Must be called to ensure changes are written to the efuse registers.
  447. * After this the batch writing mode will be reset.
  448. *
  449. * @return
  450. * - ESP_OK: Successful.
  451. * - ESP_ERR_INVALID_STATE: The deferred writing mode was not set.
  452. */
  453. esp_err_t esp_efuse_batch_write_commit(void);
  454. /**
  455. * @brief Checks that the given block is empty.
  456. *
  457. * @return
  458. * - True: The block is empty.
  459. * - False: The block is not empty or was an error.
  460. */
  461. bool esp_efuse_block_is_empty(esp_efuse_block_t block);
  462. /**
  463. * @brief Returns a read protection for the key block.
  464. *
  465. * @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
  466. *
  467. * @return True: The key block is read protected
  468. * False: The key block is readable.
  469. */
  470. bool esp_efuse_get_key_dis_read(esp_efuse_block_t block);
  471. /**
  472. * @brief Sets a read protection for the key block.
  473. *
  474. * @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
  475. *
  476. * @return
  477. * - ESP_OK: Successful.
  478. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  479. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  480. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  481. */
  482. esp_err_t esp_efuse_set_key_dis_read(esp_efuse_block_t block);
  483. /**
  484. * @brief Returns a write protection for the key block.
  485. *
  486. * @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
  487. *
  488. * @return True: The key block is write protected
  489. * False: The key block is writeable.
  490. */
  491. bool esp_efuse_get_key_dis_write(esp_efuse_block_t block);
  492. /**
  493. * @brief Sets a write protection for the key block.
  494. *
  495. * @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
  496. *
  497. * @return
  498. * - ESP_OK: Successful.
  499. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  500. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  501. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  502. */
  503. esp_err_t esp_efuse_set_key_dis_write(esp_efuse_block_t block);
  504. /**
  505. * @brief Returns true if the key block is unused, false otherwise.
  506. *
  507. * An unused key block is all zero content, not read or write protected,
  508. * and has purpose 0 (ESP_EFUSE_KEY_PURPOSE_USER)
  509. *
  510. * @param block key block to check.
  511. *
  512. * @return
  513. * - True if key block is unused,
  514. * - False if key block is used or the specified block index is not a key block.
  515. */
  516. bool esp_efuse_key_block_unused(esp_efuse_block_t block);
  517. /**
  518. * @brief Find a key block with the particular purpose set.
  519. *
  520. * @param[in] purpose Purpose to search for.
  521. * @param[out] block Pointer in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX which will be set to the key block if found.
  522. * Can be NULL, if only need to test the key block exists.
  523. *
  524. * @return
  525. * - True: If found,
  526. * - False: If not found (value at block pointer is unchanged).
  527. */
  528. bool esp_efuse_find_purpose(esp_efuse_purpose_t purpose, esp_efuse_block_t *block);
  529. /**
  530. * @brief Returns a write protection of the key purpose field for an efuse key block.
  531. *
  532. * @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
  533. *
  534. * @note For ESP32: no keypurpose, it returns always True.
  535. *
  536. * @return True: The key purpose is write protected.
  537. * False: The key purpose is writeable.
  538. */
  539. bool esp_efuse_get_keypurpose_dis_write(esp_efuse_block_t block);
  540. /**
  541. * @brief Returns the current purpose set for an efuse key block.
  542. *
  543. * @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
  544. *
  545. * @return
  546. * - Value: If Successful, it returns the value of the purpose related to the given key block.
  547. * - ESP_EFUSE_KEY_PURPOSE_MAX: Otherwise.
  548. */
  549. esp_efuse_purpose_t esp_efuse_get_key_purpose(esp_efuse_block_t block);
  550. #ifndef CONFIG_IDF_TARGET_ESP32
  551. /**
  552. * @brief Returns a pointer to a key purpose for an efuse key block.
  553. *
  554. * @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
  555. *
  556. * To get the value of this field use esp_efuse_read_field_blob() or esp_efuse_get_key_purpose().
  557. *
  558. * @return Pointer: If Successful returns a pointer to the corresponding efuse field otherwise NULL.
  559. */
  560. const esp_efuse_desc_t **esp_efuse_get_purpose_field(esp_efuse_block_t block);
  561. /**
  562. * @brief Returns a pointer to a key block.
  563. *
  564. * @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
  565. *
  566. * @return Pointer: If Successful returns a pointer to the corresponding efuse field otherwise NULL.
  567. */
  568. const esp_efuse_desc_t** esp_efuse_get_key(esp_efuse_block_t block);
  569. /**
  570. * @brief Sets a key purpose for an efuse key block.
  571. *
  572. * @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
  573. * @param[in] purpose Key purpose.
  574. *
  575. * @return
  576. * - ESP_OK: Successful.
  577. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  578. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  579. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  580. */
  581. esp_err_t esp_efuse_set_key_purpose(esp_efuse_block_t block, esp_efuse_purpose_t purpose);
  582. /**
  583. * @brief Sets a write protection of the key purpose field for an efuse key block.
  584. *
  585. * @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
  586. *
  587. * @return
  588. * - ESP_OK: Successful.
  589. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  590. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  591. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  592. */
  593. esp_err_t esp_efuse_set_keypurpose_dis_write(esp_efuse_block_t block);
  594. /**
  595. * @brief Search for an unused key block and return the first one found.
  596. *
  597. * See esp_efuse_key_block_unused for a description of an unused key block.
  598. *
  599. * @return First unused key block, or EFUSE_BLK_KEY_MAX if no unused key block is found.
  600. */
  601. esp_efuse_block_t esp_efuse_find_unused_key_block(void);
  602. /**
  603. * @brief Return the number of unused efuse key blocks in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
  604. */
  605. unsigned esp_efuse_count_unused_key_blocks(void);
  606. /**
  607. * @brief Returns the status of the Secure Boot public key digest revocation bit.
  608. *
  609. * @param[in] num_digest The number of digest in range 0..2
  610. *
  611. * @return
  612. * - True: If key digest is revoked,
  613. * - False; If key digest is not revoked.
  614. */
  615. bool esp_efuse_get_digest_revoke(unsigned num_digest);
  616. /**
  617. * @brief Sets the Secure Boot public key digest revocation bit.
  618. *
  619. * @param[in] num_digest The number of digest in range 0..2
  620. *
  621. * @return
  622. * - ESP_OK: Successful.
  623. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  624. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  625. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  626. */
  627. esp_err_t esp_efuse_set_digest_revoke(unsigned num_digest);
  628. /**
  629. * @brief Returns a write protection of the Secure Boot public key digest revocation bit.
  630. *
  631. * @param[in] num_digest The number of digest in range 0..2
  632. *
  633. * @return True: The revocation bit is write protected.
  634. * False: The revocation bit is writeable.
  635. */
  636. bool esp_efuse_get_write_protect_of_digest_revoke(unsigned num_digest);
  637. /**
  638. * @brief Sets a write protection of the Secure Boot public key digest revocation bit.
  639. *
  640. * @param[in] num_digest The number of digest in range 0..2
  641. *
  642. * @return
  643. * - ESP_OK: Successful.
  644. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  645. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  646. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  647. */
  648. esp_err_t esp_efuse_set_write_protect_of_digest_revoke(unsigned num_digest);
  649. #endif // not CONFIG_IDF_TARGET_ESP32
  650. /**
  651. * @brief Program a block of key data to an efuse block
  652. *
  653. * The burn of a key, protection bits, and a purpose happens in batch mode.
  654. *
  655. * @param[in] block Block to read purpose for. Must be in range EFUSE_BLK_KEY0 to EFUSE_BLK_KEY_MAX. Key block must be unused (esp_efuse_key_block_unused).
  656. * @param[in] purpose Purpose to set for this key. Purpose must be already unset.
  657. * @param[in] key Pointer to data to write.
  658. * @param[in] key_size_bytes Bytes length of data to write.
  659. *
  660. * @return
  661. * - ESP_OK: Successful.
  662. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  663. * - ESP_ERR_INVALID_STATE: Error in efuses state, unused block not found.
  664. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  665. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  666. */
  667. esp_err_t esp_efuse_write_key(esp_efuse_block_t block, esp_efuse_purpose_t purpose, const void *key, size_t key_size_bytes);
  668. /**
  669. * @brief Program keys to unused efuse blocks
  670. *
  671. * The burn of keys, protection bits, and purposes happens in batch mode.
  672. *
  673. * @param[in] purposes Array of purposes (purpose[number_of_keys]).
  674. * @param[in] keys Array of keys (uint8_t keys[number_of_keys][32]). Each key is 32 bytes long.
  675. * @param[in] number_of_keys The number of keys to write (up to 6 keys).
  676. *
  677. * @return
  678. * - ESP_OK: Successful.
  679. * - ESP_ERR_INVALID_ARG: Error in the passed arguments.
  680. * - ESP_ERR_INVALID_STATE: Error in efuses state, unused block not found.
  681. * - ESP_ERR_NOT_ENOUGH_UNUSED_KEY_BLOCKS: Error not enough unused key blocks available
  682. * - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
  683. * - ESP_ERR_CODING: Error range of data does not match the coding scheme.
  684. */
  685. esp_err_t esp_efuse_write_keys(const esp_efuse_purpose_t purposes[], uint8_t keys[][32], unsigned number_of_keys);
  686. #if CONFIG_ESP32_REV_MIN_3 || !CONFIG_IDF_TARGET_ESP32
  687. /**
  688. * @brief Read key digests from efuse. Any revoked/missing digests will be marked as NULL
  689. *
  690. * @param[out] trusted_keys The number of digest in range 0..2
  691. *
  692. * @return
  693. * - ESP_OK: Successful.
  694. * - ESP_FAIL: If trusted_keys is NULL or there is no valid digest.
  695. */
  696. esp_err_t esp_secure_boot_read_key_digests(ets_secure_boot_key_digests_t *trusted_keys);
  697. #endif
  698. #ifdef __cplusplus
  699. }
  700. #endif