esp_efuse.h 30 KB

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