efuse_main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* efuse example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_err.h"
  11. #include "esp_log.h"
  12. #include "esp_efuse.h"
  13. #include "esp_efuse_table.h"
  14. #include "esp_efuse_custom_table.h"
  15. #include "esp_secure_boot.h"
  16. #include "esp_flash_encrypt.h"
  17. #include "sdkconfig.h"
  18. static const char* TAG = "example";
  19. typedef struct {
  20. uint8_t module_version; /*!< Module version: length 8 bits */
  21. uint8_t device_role; /*!< Device role: length 3 bits */
  22. uint8_t setting_1; /*!< Setting 1: length 6 bits */
  23. uint8_t setting_2; /*!< Setting 2: length 5 bits */
  24. size_t custom_secure_version; /*!< Custom secure version: length 16 bits */
  25. uint16_t reserv; /*!< Reserv */
  26. } device_desc_t;
  27. static void print_device_desc(device_desc_t *desc)
  28. {
  29. ESP_LOGI(TAG, "module_version = %d", desc->module_version);
  30. if (desc->device_role == 0) {
  31. ESP_LOGI(TAG, "device_role = None");
  32. } else if (desc->device_role == 1) {
  33. ESP_LOGI(TAG, "device_role = Master");
  34. } else if (desc->device_role == 2) {
  35. ESP_LOGI(TAG, "device_role = Slave");
  36. } else {
  37. ESP_LOGI(TAG, "device_role = Not supported");
  38. }
  39. ESP_LOGI(TAG, "setting_1 = %d", desc->setting_1);
  40. ESP_LOGI(TAG, "setting_2 = %d", desc->setting_2);
  41. ESP_LOGI(TAG, "custom_secure_version = %d", desc->custom_secure_version);
  42. }
  43. static void read_device_desc_efuse_fields(device_desc_t *desc)
  44. {
  45. ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_MODULE_VERSION, &desc->module_version, 8));
  46. ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_DEVICE_ROLE, &desc->device_role, 3));
  47. ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_SETTING_1, &desc->setting_1, 6));
  48. ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_SETTING_2, &desc->setting_2, 5));
  49. ESP_ERROR_CHECK(esp_efuse_read_field_cnt(ESP_EFUSE_CUSTOM_SECURE_VERSION, &desc->custom_secure_version));
  50. print_device_desc(desc);
  51. }
  52. static void read_efuse_fields(device_desc_t *desc)
  53. {
  54. ESP_LOGI(TAG, "read efuse fields");
  55. uint8_t mac[6];
  56. ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, &mac, sizeof(mac) * 8));
  57. ESP_LOGI(TAG, "1. read MAC address: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  58. size_t secure_version = 0;
  59. ESP_ERROR_CHECK(esp_efuse_read_field_cnt(ESP_EFUSE_SECURE_VERSION, &secure_version));
  60. ESP_LOGI(TAG, "2. read secure_version: %d", secure_version);
  61. ESP_LOGI(TAG, "3. read custom fields");
  62. read_device_desc_efuse_fields(desc);
  63. }
  64. #if defined(CONFIG_EFUSE_VIRTUAL) || defined(CONFIG_EXAMPLE_TEST_RUN_USING_QEMU)
  65. static void write_efuse_fields(device_desc_t *desc, esp_efuse_coding_scheme_t coding_scheme)
  66. {
  67. #if CONFIG_IDF_TARGET_ESP32
  68. const esp_efuse_coding_scheme_t coding_scheme_for_batch_mode = EFUSE_CODING_SCHEME_3_4;
  69. #else
  70. const esp_efuse_coding_scheme_t coding_scheme_for_batch_mode = EFUSE_CODING_SCHEME_RS;
  71. #endif
  72. ESP_LOGI(TAG, "write custom efuse fields");
  73. if (coding_scheme == coding_scheme_for_batch_mode) {
  74. ESP_LOGI(TAG, "In the case of 3/4 or RS coding scheme, you cannot write efuse fields separately");
  75. ESP_LOGI(TAG, "You should use the batch mode of writing fields for this");
  76. ESP_ERROR_CHECK(esp_efuse_batch_write_begin());
  77. }
  78. ESP_ERROR_CHECK(esp_efuse_write_field_blob(ESP_EFUSE_MODULE_VERSION, &desc->module_version, 8));
  79. ESP_ERROR_CHECK(esp_efuse_write_field_blob(ESP_EFUSE_DEVICE_ROLE, &desc->device_role, 3));
  80. ESP_ERROR_CHECK(esp_efuse_write_field_blob(ESP_EFUSE_SETTING_1, &desc->setting_1, 6));
  81. ESP_ERROR_CHECK(esp_efuse_write_field_blob(ESP_EFUSE_SETTING_2, &desc->setting_2, 5));
  82. ESP_ERROR_CHECK(esp_efuse_write_field_cnt(ESP_EFUSE_CUSTOM_SECURE_VERSION, desc->custom_secure_version));
  83. if (coding_scheme == coding_scheme_for_batch_mode) {
  84. ESP_ERROR_CHECK(esp_efuse_batch_write_commit());
  85. }
  86. }
  87. #endif // defined(CONFIG_EFUSE_VIRTUAL) || defined(CONFIG_EXAMPLE_TEST_RUN_USING_QEMU)
  88. static esp_efuse_coding_scheme_t get_coding_scheme(void)
  89. {
  90. // The coding scheme is used for EFUSE_BLK1, EFUSE_BLK2 and EFUSE_BLK3.
  91. // We use EFUSE_BLK3 (custom block) to verify it.
  92. esp_efuse_coding_scheme_t coding_scheme = esp_efuse_get_coding_scheme(EFUSE_BLK3);
  93. if (coding_scheme == EFUSE_CODING_SCHEME_NONE) {
  94. ESP_LOGI(TAG, "Coding Scheme NONE");
  95. #if CONFIG_IDF_TARGET_ESP32
  96. } else if (coding_scheme == EFUSE_CODING_SCHEME_3_4) {
  97. ESP_LOGI(TAG, "Coding Scheme 3/4");
  98. } else {
  99. ESP_LOGI(TAG, "Coding Scheme REPEAT");
  100. }
  101. #else
  102. } else if (coding_scheme == EFUSE_CODING_SCHEME_RS) {
  103. ESP_LOGI(TAG, "Coding Scheme RS (Reed-Solomon coding)");
  104. }
  105. #endif
  106. return coding_scheme;
  107. }
  108. void app_main(void)
  109. {
  110. ESP_LOGI(TAG, "Start eFuse example");
  111. #ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
  112. if (esp_flash_encryption_cfg_verify_release_mode()) {
  113. ESP_LOGI(TAG, "Flash Encryption is in RELEASE mode");
  114. } else {
  115. ESP_LOGW(TAG, "Flash Encryption is NOT in RELEASE mode");
  116. }
  117. #endif
  118. #ifdef CONFIG_SECURE_BOOT
  119. if (esp_secure_boot_cfg_verify_release_mode()) {
  120. ESP_LOGI(TAG, "Secure Boot is in RELEASE mode");
  121. } else {
  122. ESP_LOGW(TAG, "Secure Boot is NOT in RELEASE mode");
  123. }
  124. #endif
  125. esp_efuse_coding_scheme_t coding_scheme = get_coding_scheme();
  126. (void) coding_scheme;
  127. device_desc_t device_desc = { 0 };
  128. read_efuse_fields(&device_desc);
  129. #if !CONFIG_EXAMPLE_TEST_RUN_USING_QEMU
  130. ESP_LOGW(TAG, "This example does not burn any efuse in reality only virtually");
  131. #endif
  132. #if CONFIG_IDF_TARGET_ESP32C2
  133. if (esp_secure_boot_enabled() || esp_flash_encryption_enabled()) {
  134. ESP_LOGW(TAG, "BLOCK3 is used for secure boot or/and flash encryption");
  135. ESP_LOGW(TAG, "eFuses from the custom eFuse table can not be used as they are placed in BLOCK3");
  136. ESP_LOGI(TAG, "Done");
  137. return;
  138. }
  139. #endif
  140. #if defined(CONFIG_EFUSE_VIRTUAL) || defined(CONFIG_EXAMPLE_TEST_RUN_USING_QEMU)
  141. #if !CONFIG_EXAMPLE_TEST_RUN_USING_QEMU
  142. ESP_LOGW(TAG, "Write operations in efuse fields are performed virtually");
  143. #endif
  144. if (device_desc.device_role == 0) {
  145. device_desc.module_version = 1;
  146. device_desc.device_role = 2;
  147. device_desc.setting_1 = 3;
  148. device_desc.setting_2 = 4;
  149. device_desc.custom_secure_version = 5;
  150. write_efuse_fields(&device_desc, coding_scheme);
  151. read_device_desc_efuse_fields(&device_desc);
  152. }
  153. #else
  154. ESP_LOGW(TAG, "The part of the code that writes efuse fields is disabled");
  155. #endif
  156. ESP_LOGI(TAG, "Done");
  157. }