From: Konstantin Kondrashov Date: Fri, 7 Dec 2018 09:57:25 +0000 (+0800) Subject: efuse: Add a write/read protection X-Git-Tag: v3.3-beta2~2^2~6 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=303d17792a21350d582a1de73ec23793b6aa0f36;p=esp-idf efuse: Add a write/read protection --- diff --git a/components/efuse/include/esp_efuse.h b/components/efuse/include/esp_efuse.h index 349b6f7923..22f6879366 100644 --- a/components/efuse/include/esp_efuse.h +++ b/components/efuse/include/esp_efuse.h @@ -123,6 +123,36 @@ esp_err_t esp_efuse_write_field_blob(const esp_efuse_desc_t* field[], const void */ esp_err_t esp_efuse_write_field_cnt(const esp_efuse_desc_t* field[], size_t cnt); +/** + * @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); + /** * @brief Returns the number of bits used by field. * diff --git a/components/efuse/src/esp_efuse_api.c b/components/efuse/src/esp_efuse_api.c index 709985786d..09f6e5b1c0 100644 --- a/components/efuse/src/esp_efuse_api.c +++ b/components/efuse/src/esp_efuse_api.c @@ -17,6 +17,7 @@ #include "soc/efuse_reg.h" #include "assert.h" #include "sdkconfig.h" +#include "esp_efuse_table.h" const static char *TAG = "efuse"; @@ -113,6 +114,32 @@ esp_err_t esp_efuse_write_field_cnt(const esp_efuse_desc_t* field[], size_t cnt) return err; } +// Sets a write protection for the whole block. +esp_err_t esp_efuse_set_write_protect(esp_efuse_block_t blk) +{ + if (blk == EFUSE_BLK1) { + return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_BLK1, 1); + } else if (blk == EFUSE_BLK2) { + return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_BLK2, 1); + } else if (blk == EFUSE_BLK3) { + return esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_BLK3, 1); + } + return ESP_ERR_NOT_SUPPORTED; +} + +// read protect for blk. +esp_err_t esp_efuse_set_read_protect(esp_efuse_block_t blk) +{ + if (blk == EFUSE_BLK1) { + return esp_efuse_write_field_cnt(ESP_EFUSE_RD_DIS_BLK1, 1); + } else if (blk == EFUSE_BLK2) { + return esp_efuse_write_field_cnt(ESP_EFUSE_RD_DIS_BLK2, 1); + } else if (blk == EFUSE_BLK3) { + return esp_efuse_write_field_cnt(ESP_EFUSE_RD_DIS_BLK3, 1); + } + return ESP_ERR_NOT_SUPPORTED; +} + // get the length of the field in bits int esp_efuse_get_field_size(const esp_efuse_desc_t* field[]) { diff --git a/components/efuse/test/test_efuse.c b/components/efuse/test/test_efuse.c index c41e2b5e16..91fe35c85c 100644 --- a/components/efuse/test/test_efuse.c +++ b/components/efuse/test/test_efuse.c @@ -543,4 +543,52 @@ TEST_CASE("Test Bits are not empty. Write operation is forbidden", "[efuse]") printf("Test skipped. It is not applicable, the device has no written bits."); } } + +TEST_CASE("Test a write/read protection", "[efuse]") +{ + esp_efuse_utility_reset(); + esp_efuse_utility_erase_virt_blocks(); + + esp_efuse_utility_debug_dump_blocks(); + + TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, esp_efuse_set_write_protect(EFUSE_BLK0)); + TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, esp_efuse_set_read_protect(EFUSE_BLK0)); + + size_t out_cnt; + esp_efuse_read_field_cnt(ESP_EFUSE_WR_DIS_BLK1, &out_cnt); + TEST_ASSERT_EQUAL_INT(0, out_cnt); + TEST_ESP_OK(esp_efuse_set_write_protect(EFUSE_BLK1)); + esp_efuse_read_field_cnt(ESP_EFUSE_WR_DIS_BLK1, &out_cnt); + TEST_ASSERT_EQUAL_INT(1, out_cnt); + TEST_ESP_ERR(ESP_ERR_EFUSE_CNT_IS_FULL, esp_efuse_set_write_protect(EFUSE_BLK1)); + + TEST_ESP_OK(esp_efuse_set_write_protect(EFUSE_BLK2)); + esp_efuse_read_field_cnt(ESP_EFUSE_WR_DIS_BLK2, &out_cnt); + TEST_ASSERT_EQUAL_INT(1, out_cnt); + + TEST_ESP_OK(esp_efuse_set_write_protect(EFUSE_BLK3)); + esp_efuse_read_field_cnt(ESP_EFUSE_WR_DIS_BLK3, &out_cnt); + TEST_ASSERT_EQUAL_INT(1, out_cnt); + + esp_efuse_utility_debug_dump_blocks(); + + esp_efuse_read_field_cnt(ESP_EFUSE_RD_DIS_BLK1, &out_cnt); + TEST_ASSERT_EQUAL_INT(0, out_cnt); + TEST_ESP_OK(esp_efuse_set_read_protect(EFUSE_BLK1)); + esp_efuse_read_field_cnt(ESP_EFUSE_RD_DIS_BLK1, &out_cnt); + TEST_ASSERT_EQUAL_INT(1, out_cnt); + TEST_ESP_ERR(ESP_ERR_EFUSE_CNT_IS_FULL, esp_efuse_set_read_protect(EFUSE_BLK1)); + + TEST_ESP_OK(esp_efuse_set_read_protect(EFUSE_BLK2)); + esp_efuse_read_field_cnt(ESP_EFUSE_RD_DIS_BLK2, &out_cnt); + TEST_ASSERT_EQUAL_INT(1, out_cnt); + + TEST_ESP_OK(esp_efuse_set_read_protect(EFUSE_BLK3)); + esp_efuse_read_field_cnt(ESP_EFUSE_RD_DIS_BLK3, &out_cnt); + TEST_ASSERT_EQUAL_INT(1, out_cnt); + + esp_efuse_utility_debug_dump_blocks(); + esp_efuse_utility_reset(); + esp_efuse_utility_erase_virt_blocks(); +} #endif // #ifdef CONFIG_EFUSE_VIRTUAL