From fca2d784591848cd162ed5938396f3fcef29dce4 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Sat, 25 Apr 2020 15:13:18 +1000 Subject: [PATCH] efuse: Add new esp_efuse_read_field_bit() convenience function --- .../bootloader_support/src/flash_encrypt.c | 19 +++++++++---------- components/efuse/include/esp_efuse.h | 17 +++++++++++++++++ components/efuse/src/esp_efuse_api.c | 8 ++++++++ components/efuse/test/test_efuse.c | 11 ++++++++--- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/components/bootloader_support/src/flash_encrypt.c b/components/bootloader_support/src/flash_encrypt.c index 408b1b6b88..9203106c93 100644 --- a/components/bootloader_support/src/flash_encrypt.c +++ b/components/bootloader_support/src/flash_encrypt.c @@ -33,8 +33,7 @@ void esp_flash_encryption_init_checks() #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE #ifdef CONFIG_SECURE_BOOT if (esp_secure_boot_enabled() && esp_flash_encryption_enabled()) { - uint8_t flash_crypt_cnt_wr_dis = 0; - esp_efuse_read_field_blob(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT, &flash_crypt_cnt_wr_dis, 1); + bool flash_crypt_cnt_wr_dis = esp_efuse_read_field_bit(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT); if (!flash_crypt_cnt_wr_dis) { uint8_t flash_crypt_cnt = 0; esp_efuse_read_field_blob(ESP_EFUSE_FLASH_CRYPT_CNT, &flash_crypt_cnt, @@ -82,20 +81,20 @@ esp_flash_enc_mode_t esp_get_flash_encryption_mode(void) if (esp_flash_encryption_enabled()) { /* Check if FLASH CRYPT CNT is write protected */ - - esp_efuse_read_field_blob(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT, &flash_crypt_cnt_wr_dis, 1); - if (!flash_crypt_cnt_wr_dis) { + efuse_flash_crypt_cnt_wr_protected = esp_efuse_read_field_bit(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT); + if (!efuse_flash_crypt_cnt_wr_protected) { uint8_t flash_crypt_cnt = 0; esp_efuse_read_field_blob(ESP_EFUSE_FLASH_CRYPT_CNT, &flash_crypt_cnt, ESP_EFUSE_FLASH_CRYPT_CNT[0]->bit_count); if (flash_crypt_cnt == (1 << (ESP_EFUSE_FLASH_CRYPT_CNT[0]->bit_count)) - 1) { - flash_crypt_cnt_wr_dis = 1; + efuse_flash_crypt_cnt_wr_protected = 1; // CRYPT_CNT at max is same as write protected } } - if (flash_crypt_cnt_wr_dis) { - esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_CACHE, &dis_dl_cache, 1); - esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_ENCRYPT, &dis_dl_enc, 1); - esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_DECRYPT, &dis_dl_dec, 1); + if (efuse_flash_crypt_cnt_wr_protected) { + dis_dl_cache = esp_efuse_read_field_bit(ESP_EFUSE_DISABLE_DL_CACHE); + dis_dl_enc = esp_efuse_read_field_bit(ESP_EFUSE_DISABLE_DL_ENCRYPT); + dis_dl_dec = esp_efuse_read_field_bit(ESP_EFUSE_DISABLE_DL_DECRYPT); + /* Check if DISABLE_DL_DECRYPT, DISABLE_DL_ENCRYPT & DISABLE_DL_CACHE are set */ if ( dis_dl_cache && dis_dl_enc && dis_dl_dec ) { mode = ESP_FLASH_ENC_MODE_RELEASE; diff --git a/components/efuse/include/esp_efuse.h b/components/efuse/include/esp_efuse.h index 69b766e697..fae2408792 100644 --- a/components/efuse/include/esp_efuse.h +++ b/components/efuse/include/esp_efuse.h @@ -61,6 +61,23 @@ typedef struct { */ esp_err_t esp_efuse_read_field_blob(const esp_efuse_desc_t* field[], void* dst, size_t dst_size_bits); + +/** + * @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 + * + * @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[]); + /** * @brief Reads bits from EFUSE field and returns number of bits programmed as "1". * diff --git a/components/efuse/src/esp_efuse_api.c b/components/efuse/src/esp_efuse_api.c index 361b8cd578..ce17a58cc2 100644 --- a/components/efuse/src/esp_efuse_api.c +++ b/components/efuse/src/esp_efuse_api.c @@ -54,6 +54,14 @@ esp_err_t esp_efuse_read_field_blob(const esp_efuse_desc_t* field[], void* dst, return err; } +bool esp_efuse_read_field_bit(const esp_efuse_desc_t *field[]) +{ + uint8_t value = 0; + esp_err_t err = esp_efuse_read_field_blob(field, &value, 1); + assert(err == ESP_OK); + return (err == ESP_OK) && value; +} + // read number of bits programmed as "1" in the particular field esp_err_t esp_efuse_read_field_cnt(const esp_efuse_desc_t* field[], size_t* out_cnt) { diff --git a/components/efuse/test/test_efuse.c b/components/efuse/test/test_efuse.c index caba83e1de..1ed1afe75b 100644 --- a/components/efuse/test/test_efuse.c +++ b/components/efuse/test/test_efuse.c @@ -267,7 +267,7 @@ TEST_CASE("efuse test write_field_cnt", "[efuse]") test_write_cnt(); } -TEST_CASE("efuse test write_field_bit", "[efuse]") +TEST_CASE("efuse test single bit functions", "[efuse]") { esp_efuse_utility_erase_virt_blocks(); esp_efuse_utility_debug_dump_blocks(); @@ -276,14 +276,19 @@ TEST_CASE("efuse test write_field_bit", "[efuse]") TEST_ESP_OK(esp_efuse_read_field_blob(ESP_EFUSE_TEST5_LEN_1, &test_bit, 1)); TEST_ASSERT_EQUAL_HEX8(0, test_bit); + test_bit = esp_efuse_read_field_bit(ESP_EFUSE_TEST5_LEN_1); + TEST_ASSERT_EQUAL_HEX8(0, test_bit); + TEST_ESP_OK(esp_efuse_write_field_bit(ESP_EFUSE_TEST5_LEN_1)); TEST_ESP_OK(esp_efuse_read_field_blob(ESP_EFUSE_TEST5_LEN_1, &test_bit, 1)); TEST_ASSERT_EQUAL_HEX8(1, test_bit); + test_bit = esp_efuse_read_field_bit(ESP_EFUSE_TEST5_LEN_1); + TEST_ASSERT_EQUAL_HEX8(1, test_bit); + // Can write the bit again and it's a no-op TEST_ESP_OK(esp_efuse_write_field_bit(ESP_EFUSE_TEST5_LEN_1)); - TEST_ESP_OK(esp_efuse_read_field_blob(ESP_EFUSE_TEST5_LEN_1, &test_bit, 1)); - TEST_ASSERT_EQUAL_HEX8(1, test_bit); + TEST_ASSERT_EQUAL_HEX8(1, esp_efuse_read_field_bit(ESP_EFUSE_TEST5_LEN_1)); esp_efuse_utility_debug_dump_blocks(); }