kopia lustrzana https://github.com/espressif/esp-idf
efuse: Add a write/read protection
rodzic
b8141f3ad8
commit
303d17792a
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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[])
|
||||
{
|
||||
|
|
|
@ -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
|
||||
|
|
Ładowanie…
Reference in New Issue