]> granicus.if.org Git - esp-idf/commitdiff
efuse: Add a write/read protection
authorKonstantin Kondrashov <konstantin@espressif.com>
Fri, 7 Dec 2018 09:57:25 +0000 (17:57 +0800)
committerbot <bot@espressif.com>
Thu, 28 Feb 2019 07:31:29 +0000 (07:31 +0000)
components/efuse/include/esp_efuse.h
components/efuse/src/esp_efuse_api.c
components/efuse/test/test_efuse.c

index 349b6f7923ddfc97f09b0dcc218bf82dc630cfcb..22f6879366be54120fac571e55f42ed2544b728c 100644 (file)
@@ -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.
  *
index 709985786d49209975b409ffd83c97ac8bae33b8..09f6e5b1c019ba13f0e83c1ac26cfee7dd0aab47 100644 (file)
@@ -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[])
 {
index c41e2b5e161c72d3c06368ddde02fdac4e784917..91fe35c85c035723ed1b30b5798af3d08cc8e1ca 100644 (file)
@@ -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