test_efuse.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include <errno.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include "unity.h"
  12. #include "esp_log.h"
  13. #include <string.h>
  14. #include "esp_efuse.h"
  15. #include "esp_efuse_table.h"
  16. #include "esp_efuse_utility.h"
  17. #include "esp_efuse_test_table.h"
  18. #include "freertos/FreeRTOS.h"
  19. #include "freertos/task.h"
  20. #include "freertos/semphr.h"
  21. #include "sdkconfig.h"
  22. #include "esp_rom_efuse.h"
  23. __attribute__((unused)) static const char* TAG = "efuse_test";
  24. #ifdef CONFIG_EFUSE_VIRTUAL
  25. TEST_CASE("Test a write protection", "[efuse]")
  26. {
  27. esp_efuse_utility_reset();
  28. esp_efuse_utility_erase_virt_blocks();
  29. esp_efuse_utility_debug_dump_blocks();
  30. TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, esp_efuse_set_write_protect(EFUSE_BLK0));
  31. TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, esp_efuse_set_read_protect(EFUSE_BLK0));
  32. size_t out_cnt;
  33. esp_efuse_read_field_cnt(ESP_EFUSE_WR_DIS, &out_cnt);
  34. TEST_ASSERT_EQUAL_INT(0, out_cnt);
  35. TEST_ESP_OK(esp_efuse_set_write_protect(EFUSE_BLK_KEY0));
  36. esp_efuse_read_field_cnt(ESP_EFUSE_WR_DIS, &out_cnt);
  37. TEST_ASSERT_EQUAL_INT(1, out_cnt);
  38. TEST_ESP_ERR(ESP_ERR_EFUSE_CNT_IS_FULL, esp_efuse_set_write_protect(EFUSE_BLK_KEY0));
  39. esp_efuse_utility_debug_dump_blocks();
  40. esp_efuse_utility_reset();
  41. esp_efuse_utility_erase_virt_blocks();
  42. }
  43. TEST_CASE("Test a read protection", "[efuse]")
  44. {
  45. esp_efuse_utility_reset();
  46. esp_efuse_utility_erase_virt_blocks();
  47. esp_efuse_utility_debug_dump_blocks();
  48. TEST_ESP_OK(esp_efuse_write_field_bit(ESP_EFUSE_XTS_KEY_LENGTH_256));
  49. size_t out_cnt;
  50. esp_efuse_read_field_cnt(ESP_EFUSE_RD_DIS, &out_cnt);
  51. TEST_ASSERT_EQUAL_INT(0, out_cnt);
  52. TEST_ESP_OK(esp_efuse_set_read_protect(EFUSE_BLK_KEY0));
  53. esp_efuse_read_field_cnt(ESP_EFUSE_RD_DIS, &out_cnt);
  54. TEST_ASSERT_EQUAL_INT(2, out_cnt);
  55. esp_efuse_utility_debug_dump_blocks();
  56. esp_efuse_utility_reset();
  57. esp_efuse_utility_erase_virt_blocks();
  58. }
  59. TEST_CASE("Test a key read protection 1", "[efuse]")
  60. {
  61. esp_efuse_utility_reset();
  62. esp_efuse_utility_erase_virt_blocks();
  63. esp_efuse_utility_debug_dump_blocks();
  64. size_t out_cnt;
  65. esp_efuse_read_field_cnt(ESP_EFUSE_RD_DIS, &out_cnt);
  66. TEST_ASSERT_EQUAL_INT(0, out_cnt);
  67. TEST_ASSERT_FALSE(esp_efuse_get_key_dis_read(EFUSE_BLK_KEY0));
  68. TEST_ESP_OK(esp_efuse_set_key_dis_read(EFUSE_BLK_KEY0));
  69. TEST_ASSERT_TRUE(esp_efuse_get_key_dis_read(EFUSE_BLK_KEY0));
  70. esp_efuse_read_field_cnt(ESP_EFUSE_RD_DIS, &out_cnt);
  71. TEST_ASSERT_EQUAL_INT(1, out_cnt);
  72. esp_efuse_utility_debug_dump_blocks();
  73. esp_efuse_utility_reset();
  74. esp_efuse_utility_erase_virt_blocks();
  75. }
  76. TEST_CASE("Test a key read protection 2", "[efuse]")
  77. {
  78. esp_efuse_utility_reset();
  79. esp_efuse_utility_erase_virt_blocks();
  80. esp_efuse_utility_debug_dump_blocks();
  81. TEST_ESP_OK(esp_efuse_write_field_bit(ESP_EFUSE_XTS_KEY_LENGTH_256));
  82. size_t out_cnt;
  83. esp_efuse_read_field_cnt(ESP_EFUSE_RD_DIS, &out_cnt);
  84. TEST_ASSERT_EQUAL_INT(0, out_cnt);
  85. TEST_ASSERT_FALSE(esp_efuse_get_key_dis_read(EFUSE_BLK_KEY0));
  86. TEST_ESP_OK(esp_efuse_set_key_dis_read(EFUSE_BLK_KEY0));
  87. TEST_ASSERT_TRUE(esp_efuse_get_key_dis_read(EFUSE_BLK_KEY0));
  88. esp_efuse_read_field_cnt(ESP_EFUSE_RD_DIS, &out_cnt);
  89. TEST_ASSERT_EQUAL_INT(2, out_cnt);
  90. esp_efuse_utility_debug_dump_blocks();
  91. esp_efuse_utility_reset();
  92. esp_efuse_utility_erase_virt_blocks();
  93. }
  94. #endif // CONFIG_EFUSE_VIRTUAL
  95. #ifdef CONFIG_EFUSE_FPGA_TEST
  96. TEST_CASE("Test a real write (FPGA)2", "[efuse]")
  97. {
  98. esp_efuse_utility_debug_dump_blocks();
  99. ESP_LOGI(TAG, "1. Write KEY3");
  100. uint8_t key[32] = {0};
  101. TEST_ESP_OK(esp_efuse_read_field_blob(ESP_EFUSE_KEY0, &key, 256));
  102. for (int i = 0; i < sizeof(key); ++i) {
  103. TEST_ASSERT_EQUAL_INT(0, key[i]);
  104. }
  105. uint8_t new_key[32] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  106. 10, 11, 12, 12, 14, 15, 16, 17, 18, 19,
  107. 20, 21, 22, 22, 24, 25, 26, 27, 28, 29,
  108. 30, 31};
  109. TEST_ESP_OK(esp_efuse_write_field_blob(ESP_EFUSE_KEY0, &new_key, 256));
  110. TEST_ESP_OK(esp_efuse_read_field_blob(ESP_EFUSE_KEY0, &key, 256));
  111. TEST_ASSERT_EQUAL_HEX8_ARRAY(new_key, key, sizeof(key));
  112. esp_efuse_utility_debug_dump_blocks();
  113. ESP_LOGI(TAG, "2. Set a read protection for KEY0");
  114. TEST_ESP_OK(esp_efuse_set_read_protect(EFUSE_BLK_KEY0));
  115. TEST_ESP_OK(esp_efuse_read_field_blob(ESP_EFUSE_KEY0, &key, 256));
  116. #ifndef CONFIG_EFUSE_VIRTUAL
  117. TEST_ASSERT_EACH_EQUAL_HEX8(0, key, sizeof(key));
  118. #else
  119. TEST_ASSERT_EQUAL_HEX8_ARRAY(new_key, key, sizeof(key));
  120. #endif // CONFIG_EFUSE_VIRTUAL
  121. esp_efuse_utility_debug_dump_blocks();
  122. }
  123. #endif // CONFIG_EFUSE_FPGA_TEST