test_efuse.c 4.9 KB

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