esp_efuse.h 29 KB

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