2021-05-31 06:43:23 +02:00
/*
2024-02-22 23:48:37 +02:00
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
2021-05-31 06:43:23 +02:00
*
* SPDX-License-Identifier: Apache-2.0
*/
2018-11-19 11:46:21 +08:00
2019-06-06 10:57:29 +08:00
# pragma once
2018-11-19 11:46:21 +08:00
2021-11-11 18:04:26 -03:00
# include <stdbool.h>
2018-11-19 11:46:21 +08:00
# include <stdint.h>
2021-11-12 09:17:02 -03:00
2018-11-19 11:46:21 +08:00
# include "esp_err.h"
# include "esp_log.h"
2020-05-28 18:02:02 +10:00
# include "soc/soc_caps.h"
2019-06-18 19:34:05 +08:00
# include "sdkconfig.h"
2022-08-01 21:19:38 +08:00
# include "esp_efuse_chip.h"
2018-11-19 11:46:21 +08:00
2021-11-12 09:17:02 -03:00
# ifdef __cplusplus
extern " C " {
# endif
2018-11-19 11:46:21 +08:00
# define ESP_ERR_EFUSE 0x1600 /*!< Base error code for efuse api. */
# define ESP_OK_EFUSE_CNT (ESP_ERR_EFUSE + 0x01) /*!< OK the required number of bits is set. */
# define ESP_ERR_EFUSE_CNT_IS_FULL (ESP_ERR_EFUSE + 0x02) /*!< Error field is full. */
# define ESP_ERR_EFUSE_REPEATED_PROG (ESP_ERR_EFUSE + 0x03) /*!< Error repeated programming of programmed bits is strictly forbidden. */
# define ESP_ERR_CODING (ESP_ERR_EFUSE + 0x04) /*!< Error while a encoding operation. */
2020-12-04 10:42:57 +08:00
# define ESP_ERR_NOT_ENOUGH_UNUSED_KEY_BLOCKS (ESP_ERR_EFUSE + 0x05) /*!< Error not enough unused key blocks available */
2021-06-18 11:52:47 +08:00
# 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. */
2018-11-19 11:46:21 +08:00
/**
2020-12-04 10:42:57 +08:00
* @brief Type definition for an eFuse field
2018-11-19 11:46:21 +08:00
*/
2020-12-04 10:42:57 +08:00
typedef struct {
2018-12-14 14:00:59 +08:00
esp_efuse_block_t efuse_block : 8 ; /**< Block of eFuse */
uint8_t bit_start ; /**< Start bit [0..255] */
2018-12-11 14:39:32 +08:00
uint16_t bit_count ; /**< Length of bit field [1..-]*/
2020-12-04 10:42:57 +08:00
} esp_efuse_desc_t ;
2018-11-19 11:46:21 +08:00
2021-01-26 21:07:22 +08:00
/**
* @brief Type definition for ROM log scheme
*/
typedef enum {
ESP_EFUSE_ROM_LOG_ALWAYS_ON , /**< Always enable ROM logging */
ESP_EFUSE_ROM_LOG_ON_GPIO_LOW , /**< ROM logging is enabled when specific GPIO level is low during start up */
ESP_EFUSE_ROM_LOG_ON_GPIO_HIGH , /**< ROM logging is enabled when specific GPIO level is high during start up */
ESP_EFUSE_ROM_LOG_ALWAYS_OFF /**< Disable ROM logging permanently */
} esp_efuse_rom_log_scheme_t ;
2022-03-17 21:58:15 +08:00
# if CONFIG_ESP32_REV_MIN_FULL >= 300 || !CONFIG_IDF_TARGET_ESP32
2022-05-23 12:36:02 +08:00
/**
* @brief Pointers to the trusted key digests.
*
* The number of digests depends on the SOC's capabilities.
*/
typedef struct {
const void * key_digests [ SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS ] ; /**< Pointers to the key digests */
} esp_secure_boot_key_digests_t ;
# endif
2018-11-19 11:46:21 +08:00
/**
* @brief Reads bits from EFUSE field and writes it into an array.
*
* The number of read bits will be limited to the minimum value
* from the description of the bits in "field" structure or "dst_size_bits" required size.
* Use "esp_efuse_get_field_size()" function to determine the length of the field.
2020-12-04 10:42:57 +08:00
*
* @note Please note that reading in the batch mode does not show uncommitted changes.
*
2018-11-19 11:46:21 +08:00
* @param[in] field A pointer to the structure describing the fields of efuse.
* @param[out] dst A pointer to array that will contain the result of reading.
* @param[in] dst_size_bits The number of bits required to read.
* If the requested number of bits is greater than the field,
* the number will be limited to the field size.
*
* @return
* - ESP_OK: The operation was successfully completed.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
*/
esp_err_t esp_efuse_read_field_blob ( const esp_efuse_desc_t * field [ ] , void * dst , size_t dst_size_bits ) ;
2020-04-25 15:13:18 +10:00
/**
* @brief Read a single bit eFuse field as a boolean value.
*
* @note The value must exist and must be a single bit wide. If there is any possibility of an error
* in the provided arguments, call esp_efuse_read_field_blob() and check the returned value instead.
*
* @note If assertions are enabled and the parameter is invalid, execution will abort
2020-12-04 10:42:57 +08:00
* @note Please note that reading in the batch mode does not show uncommitted changes.
2020-04-25 15:13:18 +10:00
*
* @param[in] field A pointer to the structure describing the fields of efuse.
* @return
* - true: The field parameter is valid and the bit is set.
* - false: The bit is not set, or the parameter is invalid and assertions are disabled.
*
*/
bool esp_efuse_read_field_bit ( const esp_efuse_desc_t * field [ ] ) ;
2018-11-19 11:46:21 +08:00
/**
* @brief Reads bits from EFUSE field and returns number of bits programmed as "1".
*
* If the bits are set not sequentially, they will still be counted.
2020-12-04 10:42:57 +08:00
* @note Please note that reading in the batch mode does not show uncommitted changes.
*
2018-11-19 11:46:21 +08:00
* @param[in] field A pointer to the structure describing the fields of efuse.
* @param[out] out_cnt A pointer that will contain the number of programmed as "1" bits.
*
* @return
* - ESP_OK: The operation was successfully completed.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
*/
esp_err_t esp_efuse_read_field_cnt ( const esp_efuse_desc_t * field [ ] , size_t * out_cnt ) ;
/**
* @brief Writes array to EFUSE field.
*
* The number of write bits will be limited to the minimum value
* from the description of the bits in "field" structure or "src_size_bits" required size.
* Use "esp_efuse_get_field_size()" function to determine the length of the field.
* After the function is completed, the writing registers are cleared.
* @param[in] field A pointer to the structure describing the fields of efuse.
* @param[in] src A pointer to array that contains the data for writing.
* @param[in] src_size_bits The number of bits required to write.
*
* @return
* - ESP_OK: The operation was successfully completed.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
* - ESP_ERR_CODING: Error range of data does not match the coding scheme.
*/
esp_err_t esp_efuse_write_field_blob ( const esp_efuse_desc_t * field [ ] , const void * src , size_t src_size_bits ) ;
/**
* @brief Writes a required count of bits as "1" to EFUSE field.
*
* If there are no free bits in the field to set the required number of bits to "1",
* ESP_ERR_EFUSE_CNT_IS_FULL error is returned, the field will not be partially recorded.
* After the function is completed, the writing registers are cleared.
* @param[in] field A pointer to the structure describing the fields of efuse.
* @param[in] cnt Required number of programmed as "1" bits.
*
* @return
* - ESP_OK: The operation was successfully completed.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
*/
esp_err_t esp_efuse_write_field_cnt ( const esp_efuse_desc_t * field [ ] , size_t cnt ) ;
2020-04-25 14:58:30 +10:00
/**
* @brief Write a single bit eFuse field to 1
*
* For use with eFuse fields that are a single bit. This function will write the bit to value 1 if
* it is not already set, or does nothing if the bit is already set.
*
* This is equivalent to calling esp_efuse_write_field_cnt() with the cnt parameter equal to 1,
* except that it will return ESP_OK if the field is already set to 1.
*
* @param[in] field Pointer to the structure describing the efuse field.
*
* @return
* - ESP_OK: The operation was successfully completed, or the bit was already set to value 1.
* - ESP_ERR_INVALID_ARG: Error in the passed arugments, including if the efuse field is not 1 bit wide.
*/
esp_err_t esp_efuse_write_field_bit ( const esp_efuse_desc_t * field [ ] ) ;
2018-12-07 17:57:25 +08:00
/**
* @brief Sets a write protection for the whole block.
*
* After that, it is impossible to write to this block.
* The write protection does not apply to block 0.
* @param[in] blk Block number of eFuse. (EFUSE_BLK1, EFUSE_BLK2 and EFUSE_BLK3)
*
* @return
* - ESP_OK: The operation was successfully completed.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
* - ESP_ERR_NOT_SUPPORTED: The block does not support this command.
*/
esp_err_t esp_efuse_set_write_protect ( esp_efuse_block_t blk ) ;
/**
* @brief Sets a read protection for the whole block.
*
* After that, it is impossible to read from this block.
* The read protection does not apply to block 0.
* @param[in] blk Block number of eFuse. (EFUSE_BLK1, EFUSE_BLK2 and EFUSE_BLK3)
*
* @return
* - ESP_OK: The operation was successfully completed.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_EFUSE_CNT_IS_FULL: Not all requested cnt bits is set.
* - ESP_ERR_NOT_SUPPORTED: The block does not support this command.
*/
esp_err_t esp_efuse_set_read_protect ( esp_efuse_block_t blk ) ;
2018-11-19 11:46:21 +08:00
/**
* @brief Returns the number of bits used by field.
*
* @param[in] field A pointer to the structure describing the fields of efuse.
*
* @return Returns the number of bits used by field.
*/
2018-12-12 15:50:31 +08:00
int esp_efuse_get_field_size ( const esp_efuse_desc_t * field [ ] ) ;
2018-11-19 11:46:21 +08:00
/**
* @brief Returns value of efuse register.
*
* This is a thread-safe implementation.
* Example: EFUSE_BLK2_RDATA3_REG where (blk=2, num_reg=3)
2020-12-04 10:42:57 +08:00
* @note Please note that reading in the batch mode does not show uncommitted changes.
*
2018-11-19 11:46:21 +08:00
* @param[in] blk Block number of eFuse.
* @param[in] num_reg The register number in the block.
*
* @return Value of register
*/
uint32_t esp_efuse_read_reg ( esp_efuse_block_t blk , unsigned int num_reg ) ;
/**
* @brief Write value to efuse register.
*
* Apply a coding scheme if necessary.
* This is a thread-safe implementation.
* Example: EFUSE_BLK3_WDATA0_REG where (blk=3, num_reg=0)
* @param[in] blk Block number of eFuse.
* @param[in] num_reg The register number in the block.
* @param[in] val Value to write.
*
* @return
* - ESP_OK: The operation was successfully completed.
* - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
*/
esp_err_t esp_efuse_write_reg ( esp_efuse_block_t blk , unsigned int num_reg , uint32_t val ) ;
/**
2018-12-06 17:49:02 +08:00
* @brief Return efuse coding scheme for blocks.
*
2023-11-07 18:25:12 +05:30
* @note The coding scheme is applicable only to 1, 2 and 3 blocks. For 0 block, the coding scheme is always ``NONE``.
2018-11-19 11:46:21 +08:00
*
* @param[in] blk Block number of eFuse.
2018-12-06 17:49:02 +08:00
* @return Return efuse coding scheme for blocks
2018-11-19 11:46:21 +08:00
*/
esp_efuse_coding_scheme_t esp_efuse_get_coding_scheme ( esp_efuse_block_t blk ) ;
/**
* @brief Read key to efuse block starting at the offset and the required size.
*
2020-12-04 10:42:57 +08:00
* @note Please note that reading in the batch mode does not show uncommitted changes.
*
2018-11-19 11:46:21 +08:00
* @param[in] blk Block number of eFuse.
* @param[in] dst_key A pointer to array that will contain the result of reading.
* @param[in] offset_in_bits Start bit in block.
* @param[in] size_bits The number of bits required to read.
*
* @return
* - ESP_OK: The operation was successfully completed.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_CODING: Error range of data does not match the coding scheme.
*/
esp_err_t esp_efuse_read_block ( esp_efuse_block_t blk , void * dst_key , size_t offset_in_bits , size_t size_bits ) ;
/**
* @brief Write key to efuse block starting at the offset and the required size.
*
* @param[in] blk Block number of eFuse.
* @param[in] src_key A pointer to array that contains the key for writing.
* @param[in] offset_in_bits Start bit in block.
* @param[in] size_bits The number of bits required to write.
*
* @return
* - ESP_OK: The operation was successfully completed.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_CODING: Error range of data does not match the coding scheme.
* - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits
*/
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 ) ;
/**
* @brief Returns chip package from efuse
*
* @return chip package
*/
uint32_t esp_efuse_get_pkg_ver ( void ) ;
2021-01-08 15:29:26 +08:00
/**
* @brief Reset efuse write registers
2018-11-19 11:46:21 +08:00
*
* Efuse write registers are written to zero, to negate
* any changes that have been staged here.
*
* @note This function is not threadsafe, if calling code updates
* efuse values from multiple tasks then this is caller's
* responsibility to serialise.
*/
void esp_efuse_reset ( void ) ;
2020-04-25 14:30:26 +10:00
# ifdef CONFIG_IDF_TARGET_ESP32
2021-01-08 15:29:26 +08:00
/**
* @brief Disable BASIC ROM Console via efuse
2018-11-19 11:46:21 +08:00
*
* By default, if booting from flash fails the ESP32 will boot a
* BASIC console in ROM.
*
2020-04-25 16:36:53 +10:00
* Call this function (from bootloader or app) to permanently disable the console on this chip.
2020-04-25 14:30:26 +10:00
*
2018-11-19 11:46:21 +08:00
*/
void esp_efuse_disable_basic_rom_console ( void ) ;
2020-04-25 14:30:26 +10:00
# endif
2018-11-19 11:46:21 +08:00
2021-01-08 15:29:26 +08:00
/**
* @brief Disable ROM Download Mode via eFuse
2020-04-25 16:36:53 +10:00
*
* Permanently disables the ROM Download Mode feature. Once disabled, if the SoC is booted with
* strapping pins set for ROM Download Mode then an error is printed instead.
*
* @note Not all SoCs support this option. An error will be returned if called on an ESP32
* with a silicon revision lower than 3, as these revisions do not support this option.
*
* @note If ROM Download Mode is already disabled, this function does nothing and returns success.
*
* @return
* - ESP_OK If the eFuse was successfully burned, or had already been burned.
* - ESP_ERR_NOT_SUPPORTED (ESP32 only) This SoC is not capable of disabling UART download mode
* - ESP_ERR_INVALID_STATE (ESP32 only) This eFuse is write protected and cannot be written
*/
esp_err_t esp_efuse_disable_rom_download_mode ( void ) ;
2021-01-26 21:07:22 +08:00
/**
* @brief Set boot ROM log scheme via eFuse
*
* @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,
* once the value is changed from the default it can't be changed again.
*
* @param log_scheme Supported ROM log scheme
* @return
* - ESP_OK If the eFuse was successfully burned, or had already been burned.
* - ESP_ERR_NOT_SUPPORTED (ESP32 only) This SoC is not capable of setting ROM log scheme
* - ESP_ERR_INVALID_STATE This eFuse is write protected or has been burned already
*/
esp_err_t esp_efuse_set_rom_log_scheme ( esp_efuse_rom_log_scheme_t log_scheme ) ;
2020-05-28 18:02:02 +10:00
# if SOC_SUPPORTS_SECURE_DL_MODE
2021-01-08 15:29:26 +08:00
/**
* @brief Switch ROM Download Mode to Secure Download mode via eFuse
2020-04-25 16:36:53 +10:00
*
* Permanently enables Secure Download mode. This mode limits the use of ROM Download Mode functions
* to simple flash read, write and erase operations, plus a command to return a summary of currently
* enabled security features.
*
* @note If Secure Download mode is already enabled, this function does nothing and returns success.
*
* @note Disabling the ROM Download Mode also disables Secure Download Mode.
*
* @return
* - ESP_OK If the eFuse was successfully burned, or had already been burned.
* - ESP_ERR_INVALID_STATE ROM Download Mode has been disabled via eFuse, so Secure Download mode is unavailable.
*/
esp_err_t esp_efuse_enable_rom_secure_download_mode ( void ) ;
# endif
2018-11-19 11:46:21 +08:00
2021-01-08 15:29:26 +08:00
/**
* @brief Return secure_version from efuse field.
2018-12-18 22:42:10 +08:00
* @return Secure version from efuse field
*/
2019-07-16 16:33:30 +07:00
uint32_t esp_efuse_read_secure_version ( void ) ;
2018-12-18 22:42:10 +08:00
2021-01-08 15:29:26 +08:00
/**
* @brief Check secure_version from app and secure_version and from efuse field.
2018-12-18 22:42:10 +08:00
*
* @param secure_version Secure version from app.
* @return
* - True: If version of app is equal or more then secure_version from efuse.
*/
bool esp_efuse_check_secure_version ( uint32_t secure_version ) ;
2021-01-08 15:29:26 +08:00
/**
* @brief Write efuse field by secure_version value.
2018-12-18 22:42:10 +08:00
*
* Update the secure_version value is available if the coding scheme is None.
* Note: Do not use this function in your applications. This function is called as part of the other API.
*
* @param[in] secure_version Secure version from app.
* @return
* - ESP_OK: Successful.
* - ESP_FAIL: secure version of app cannot be set to efuse field.
* - ESP_ERR_NOT_SUPPORTED: Anti rollback is not supported with the 3/4 and Repeat coding scheme.
*/
esp_err_t esp_efuse_update_secure_version ( uint32_t secure_version ) ;
2021-06-17 07:21:36 +08:00
# if defined(BOOTLOADER_BUILD) && defined(CONFIG_EFUSE_VIRTUAL) && !defined(CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH)
/**
* @brief Initializes eFuses API to keep eFuses in RAM.
*
* This function just copies all eFuses to RAM. IDF eFuse APIs perform all operators with RAM instead of real eFuse.
* (Used only in bootloader).
*/
void esp_efuse_init_virtual_mode_in_ram ( void ) ;
# endif
# ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
2021-01-08 15:29:26 +08:00
/**
* @brief Initializes variables: offset and size to simulate the work of an eFuse.
2018-12-18 22:42:10 +08:00
*
2021-06-17 07:21:36 +08:00
* Note: To simulate the work of an eFuse need to set CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH option
2018-12-18 22:42:10 +08:00
* and to add in the partition.csv file a line `efuse_em, data, efuse, , 0x2000,`.
*
* @param[in] offset The starting address of the partition where the eFuse data will be located.
* @param[in] size The size of the partition.
*/
2021-06-17 07:21:36 +08:00
void esp_efuse_init_virtual_mode_in_flash ( uint32_t offset , uint32_t size ) ;
# endif
2018-12-18 22:42:10 +08:00
2020-12-04 10:42:57 +08:00
/**
* @brief Set the batch mode of writing fields.
2019-09-18 21:19:48 +08:00
*
2020-12-04 10:42:57 +08:00
* This mode allows you to write the fields in the batch mode when need to burn several efuses at one time.
* To enable batch mode call begin() then perform as usually the necessary operations
* read and write and at the end call commit() to actually burn all written efuses.
* The batch mode can be used nested. The commit will be done by the last commit() function.
* The number of begin() functions should be equal to the number of commit() functions.
*
* @note Please note that reading in the batch mode does not show uncommitted changes.
*
* Note: If batch mode is enabled by the first task, at this time the second task cannot write/read efuses.
* The second task will wait for the first task to complete the batch operation.
2019-09-18 21:19:48 +08:00
*
* \code{c}
* // Example of using the batch writing mode.
*
* // set the batch writing mode
* esp_efuse_batch_write_begin();
*
* // use any writing functions as usual
* esp_efuse_write_field_blob(ESP_EFUSE_...);
* esp_efuse_write_field_cnt(ESP_EFUSE_...);
* esp_efuse_set_write_protect(EFUSE_BLKx);
* esp_efuse_write_reg(EFUSE_BLKx, ...);
* esp_efuse_write_block(EFUSE_BLKx, ...);
2020-12-04 10:42:57 +08:00
* 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.
* 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.
* ...
*
* // esp_efuse_batch_write APIs can be called recursively.
* esp_efuse_batch_write_begin();
* esp_efuse_set_write_protect(EFUSE_BLKx);
* esp_efuse_batch_write_commit(); // the burn will be skipped here, it will be done in the last commit().
*
2019-09-18 21:19:48 +08:00
* ...
*
* // Write all of these fields to the efuse registers
* esp_efuse_batch_write_commit();
2020-12-04 10:42:57 +08:00
* esp_efuse_read_...(ESP_EFUSE_1); // this function returns ESP_EFUSE_1 == 3.
2019-09-18 21:19:48 +08:00
*
* \endcode
*
* @return
* - ESP_OK: Successful.
*/
esp_err_t esp_efuse_batch_write_begin ( void ) ;
2021-01-08 15:29:26 +08:00
/**
* @brief Reset the batch mode of writing fields.
2019-09-18 21:19:48 +08:00
*
* It will reset the batch writing mode and any written changes.
*
* @return
* - ESP_OK: Successful.
* - ESP_ERR_INVALID_STATE: Tha batch mode was not set.
*/
esp_err_t esp_efuse_batch_write_cancel ( void ) ;
2021-01-08 15:29:26 +08:00
/**
* @brief Writes all prepared data for the batch mode.
2019-09-18 21:19:48 +08:00
*
* Must be called to ensure changes are written to the efuse registers.
* After this the batch writing mode will be reset.
*
* @return
* - ESP_OK: Successful.
* - ESP_ERR_INVALID_STATE: The deferred writing mode was not set.
*/
esp_err_t esp_efuse_batch_write_commit ( void ) ;
2020-12-04 10:42:57 +08:00
/**
2021-01-26 04:27:03 +08:00
* @brief Checks that the given block is empty.
2020-12-04 10:42:57 +08:00
*
2021-01-26 04:27:03 +08:00
* @return
* - True: The block is empty.
* - False: The block is not empty or was an error.
2020-12-04 10:42:57 +08:00
*/
2021-01-26 04:27:03 +08:00
bool esp_efuse_block_is_empty ( esp_efuse_block_t block ) ;
2020-12-04 10:42:57 +08:00
/**
* @brief Returns a read protection for the key block.
*
* @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
*
* @return True: The key block is read protected
* False: The key block is readable.
*/
bool esp_efuse_get_key_dis_read ( esp_efuse_block_t block ) ;
/**
* @brief Sets a read protection for the key block.
*
* @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
*
* @return
* - ESP_OK: Successful.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
* - ESP_ERR_CODING: Error range of data does not match the coding scheme.
*/
esp_err_t esp_efuse_set_key_dis_read ( esp_efuse_block_t block ) ;
/**
* @brief Returns a write protection for the key block.
*
* @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
*
* @return True: The key block is write protected
* False: The key block is writeable.
*/
bool esp_efuse_get_key_dis_write ( esp_efuse_block_t block ) ;
/**
* @brief Sets a write protection for the key block.
*
* @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
*
* @return
* - ESP_OK: Successful.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
* - ESP_ERR_CODING: Error range of data does not match the coding scheme.
*/
esp_err_t esp_efuse_set_key_dis_write ( esp_efuse_block_t block ) ;
2021-01-26 04:27:03 +08:00
/**
* @brief Returns true if the key block is unused, false otherwise.
*
* An unused key block is all zero content, not read or write protected,
* and has purpose 0 (ESP_EFUSE_KEY_PURPOSE_USER)
*
* @param block key block to check.
*
* @return
* - True if key block is unused,
* - False if key block is used or the specified block index is not a key block.
*/
bool esp_efuse_key_block_unused ( esp_efuse_block_t block ) ;
2021-06-17 07:21:36 +08:00
/**
* @brief Find a key block with the particular purpose set.
*
* @param[in] purpose Purpose to search for.
* @param[out] block Pointer in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX which will be set to the key block if found.
* Can be NULL, if only need to test the key block exists.
*
* @return
* - True: If found,
* - False: If not found (value at block pointer is unchanged).
*/
bool esp_efuse_find_purpose ( esp_efuse_purpose_t purpose , esp_efuse_block_t * block ) ;
2021-01-26 04:27:03 +08:00
/**
2021-06-17 07:21:36 +08:00
* @brief Returns a write protection of the key purpose field for an efuse key block.
*
* @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
*
* @note For ESP32: no keypurpose, it returns always True.
*
* @return True: The key purpose is write protected.
* False: The key purpose is writeable.
2021-01-26 04:27:03 +08:00
*/
2021-06-17 07:21:36 +08:00
bool esp_efuse_get_keypurpose_dis_write ( esp_efuse_block_t block ) ;
2021-01-26 04:27:03 +08:00
2021-06-17 07:21:36 +08:00
/**
* @brief Returns the current purpose set for an efuse key block.
*
* @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
*
* @return
* - Value: If Successful, it returns the value of the purpose related to the given key block.
* - ESP_EFUSE_KEY_PURPOSE_MAX: Otherwise.
*/
esp_efuse_purpose_t esp_efuse_get_key_purpose ( esp_efuse_block_t block ) ;
2021-01-26 04:27:03 +08:00
2021-12-03 01:48:47 +08:00
# if SOC_EFUSE_KEY_PURPOSE_FIELD
2021-01-26 04:27:03 +08:00
/**
* @brief Returns a pointer to a key purpose for an efuse key block.
*
* @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
*
* To get the value of this field use esp_efuse_read_field_blob() or esp_efuse_get_key_purpose().
*
* @return Pointer: If Successful returns a pointer to the corresponding efuse field otherwise NULL.
*/
const esp_efuse_desc_t * * esp_efuse_get_purpose_field ( esp_efuse_block_t block ) ;
/**
* @brief Returns a pointer to a key block.
*
* @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
*
* @return Pointer: If Successful returns a pointer to the corresponding efuse field otherwise NULL.
*/
const esp_efuse_desc_t * * esp_efuse_get_key ( esp_efuse_block_t block ) ;
2020-12-04 10:42:57 +08:00
/**
* @brief Sets a key purpose for an efuse key block.
*
* @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
* @param[in] purpose Key purpose.
*
* @return
* - ESP_OK: Successful.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
* - ESP_ERR_CODING: Error range of data does not match the coding scheme.
*/
esp_err_t esp_efuse_set_key_purpose ( esp_efuse_block_t block , esp_efuse_purpose_t purpose ) ;
/**
* @brief Sets a write protection of the key purpose field for an efuse key block.
*
* @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
*
* @return
* - ESP_OK: Successful.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
* - ESP_ERR_CODING: Error range of data does not match the coding scheme.
*/
esp_err_t esp_efuse_set_keypurpose_dis_write ( esp_efuse_block_t block ) ;
/**
* @brief Search for an unused key block and return the first one found.
*
* See esp_efuse_key_block_unused for a description of an unused key block.
*
* @return First unused key block, or EFUSE_BLK_KEY_MAX if no unused key block is found.
*/
esp_efuse_block_t esp_efuse_find_unused_key_block ( void ) ;
/**
* @brief Return the number of unused efuse key blocks in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
*/
unsigned esp_efuse_count_unused_key_blocks ( void ) ;
2021-12-03 01:48:47 +08:00
# endif // SOC_EFUSE_KEY_PURPOSE_FIELD
# if SOC_SUPPORT_SECURE_BOOT_REVOKE_KEY
2020-12-04 10:42:57 +08:00
/**
* @brief Returns the status of the Secure Boot public key digest revocation bit.
*
* @param[in] num_digest The number of digest in range 0..2
*
* @return
* - True: If key digest is revoked,
* - False; If key digest is not revoked.
*/
bool esp_efuse_get_digest_revoke ( unsigned num_digest ) ;
/**
* @brief Sets the Secure Boot public key digest revocation bit.
*
* @param[in] num_digest The number of digest in range 0..2
*
* @return
* - ESP_OK: Successful.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
* - ESP_ERR_CODING: Error range of data does not match the coding scheme.
*/
esp_err_t esp_efuse_set_digest_revoke ( unsigned num_digest ) ;
/**
* @brief Returns a write protection of the Secure Boot public key digest revocation bit.
*
* @param[in] num_digest The number of digest in range 0..2
*
* @return True: The revocation bit is write protected.
* False: The revocation bit is writeable.
*/
bool esp_efuse_get_write_protect_of_digest_revoke ( unsigned num_digest ) ;
/**
* @brief Sets a write protection of the Secure Boot public key digest revocation bit.
*
* @param[in] num_digest The number of digest in range 0..2
*
* @return
* - ESP_OK: Successful.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
* - ESP_ERR_CODING: Error range of data does not match the coding scheme.
*/
esp_err_t esp_efuse_set_write_protect_of_digest_revoke ( unsigned num_digest ) ;
2021-12-03 01:48:47 +08:00
# endif // SOC_SUPPORT_SECURE_BOOT_REVOKE_KEY
2021-06-17 07:21:36 +08:00
2020-12-04 10:42:57 +08:00
/**
* @brief Program a block of key data to an efuse block
*
* The burn of a key, protection bits, and a purpose happens in batch mode.
*
2023-11-07 18:25:12 +05:30
* @note This API also enables the read protection efuse bit for certain key blocks like XTS-AES, HMAC, ECDSA etc.
* This ensures that the key is only accessible to hardware peripheral.
*
* @note For SoC's with capability `SOC_EFUSE_ECDSA_USE_HARDWARE_K` (e.g., ESP32-H2), this API writes an additional
* efuse bit for ECDSA key purpose to enforce hardware TRNG generated k mode in the peripheral.
*
2020-12-04 10:42:57 +08:00
* @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).
* @param[in] purpose Purpose to set for this key. Purpose must be already unset.
* @param[in] key Pointer to data to write.
* @param[in] key_size_bytes Bytes length of data to write.
*
* @return
* - ESP_OK: Successful.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_INVALID_STATE: Error in efuses state, unused block not found.
* - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
* - ESP_ERR_CODING: Error range of data does not match the coding scheme.
*/
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 ) ;
/**
* @brief Program keys to unused efuse blocks
*
* The burn of keys, protection bits, and purposes happens in batch mode.
*
2023-11-07 18:25:12 +05:30
* @note This API also enables the read protection efuse bit for certain key blocks like XTS-AES, HMAC, ECDSA etc.
* This ensures that the key is only accessible to hardware peripheral.
*
* @note For SoC's with capability `SOC_EFUSE_ECDSA_USE_HARDWARE_K` (e.g., ESP32-H2), this API writes an additional
* efuse bit for ECDSA key purpose to enforce hardware TRNG generated k mode in the peripheral.
*
2020-12-04 10:42:57 +08:00
* @param[in] purposes Array of purposes (purpose[number_of_keys]).
* @param[in] keys Array of keys (uint8_t keys[number_of_keys][32]). Each key is 32 bytes long.
* @param[in] number_of_keys The number of keys to write (up to 6 keys).
*
* @return
* - ESP_OK: Successful.
* - ESP_ERR_INVALID_ARG: Error in the passed arguments.
* - ESP_ERR_INVALID_STATE: Error in efuses state, unused block not found.
* - ESP_ERR_NOT_ENOUGH_UNUSED_KEY_BLOCKS: Error not enough unused key blocks available
* - ESP_ERR_EFUSE_REPEATED_PROG: Error repeated programming of programmed bits is strictly forbidden.
* - ESP_ERR_CODING: Error range of data does not match the coding scheme.
*/
2021-06-17 07:21:36 +08:00
esp_err_t esp_efuse_write_keys ( const esp_efuse_purpose_t purposes [ ] , uint8_t keys [ ] [ 32 ] , unsigned number_of_keys ) ;
2020-12-04 10:42:57 +08:00
2021-06-17 07:21:36 +08:00
2022-03-17 21:58:15 +08:00
# if CONFIG_ESP32_REV_MIN_FULL >= 300 || !CONFIG_IDF_TARGET_ESP32
2021-06-17 07:21:36 +08:00
/**
* @brief Read key digests from efuse. Any revoked/missing digests will be marked as NULL
*
2022-05-23 12:36:02 +08:00
* @param[out] trusted_key_digests Trusted keys digests, stored in this parameter after successfully
* completing this function.
* The number of digests depends on the SOC's capabilities.
2021-06-17 07:21:36 +08:00
*
* @return
* - ESP_OK: Successful.
* - ESP_FAIL: If trusted_keys is NULL or there is no valid digest.
*/
2022-05-23 12:36:02 +08:00
esp_err_t esp_secure_boot_read_key_digests ( esp_secure_boot_key_digests_t * trusted_key_digests ) ;
2021-06-17 07:21:36 +08:00
# endif
2020-12-04 10:42:57 +08:00
2021-08-11 19:06:48 +05:00
/**
* @brief Checks eFuse errors in BLOCK0.
*
* @note Refers to ESP32-C3 only.
*
* It does a BLOCK0 check if eFuse EFUSE_ERR_RST_ENABLE is set.
* If BLOCK0 has an error, it prints the error and returns ESP_FAIL, which should be treated as esp_restart.
*
* @return
* - ESP_OK: No errors in BLOCK0.
* - ESP_FAIL: Error in BLOCK0 requiring reboot.
*/
esp_err_t esp_efuse_check_errors ( void ) ;
2024-02-22 23:48:37 +02:00
/**
* @brief Destroys the data in the given efuse block, if possible.
*
* Data destruction occurs through the following steps:
* 1) Destroy data in the block:
* - If write protection is inactive for the block, then unset bits are burned.
* - If write protection is active, the block remains unaltered.
* 2) Set read protection for the block if possible (check write-protection for RD_DIS).
* In this case, data becomes inaccessible, and the software reads it as all zeros.
* If write protection is enabled and read protection can not be set,
* data in the block remains readable (returns an error).
*
* Do not use the batch mode with this function as it does the burning itself!
*
* @param[in] block A key block in the range EFUSE_BLK_KEY0..EFUSE_BLK_KEY_MAX
*
* @return
* - ESP_OK: Successful.
* - ESP_FAIL: Data remained readable because the block is write-protected
* and read protection can not be set.
*/
esp_err_t esp_efuse_destroy_block ( esp_efuse_block_t block ) ;
2018-11-19 11:46:21 +08:00
# ifdef __cplusplus
}
# endif