esp_efuse.h 29 KB

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