esp_efuse_utility.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/param.h>
  7. #include "sdkconfig.h"
  8. #include "esp_log.h"
  9. #include "assert.h"
  10. #include "esp_efuse_utility.h"
  11. #include "soc/efuse_periph.h"
  12. #include "esp_private/esp_clk.h"
  13. #include "esp32c2/rom/efuse.h"
  14. #include "hal/efuse_hal.h"
  15. static const char *TAG = "efuse";
  16. #ifdef CONFIG_EFUSE_VIRTUAL
  17. extern uint32_t virt_blocks[EFUSE_BLK_MAX][COUNT_EFUSE_REG_PER_BLOCK];
  18. #endif // CONFIG_EFUSE_VIRTUAL
  19. /*Range addresses to read blocks*/
  20. const esp_efuse_range_addr_t range_read_addr_blocks[] = {
  21. {EFUSE_RD_WR_DIS_REG, EFUSE_RD_REPEAT_DATA0_REG}, // range address of EFUSE_BLK0 (2 regs) REPEAT
  22. {EFUSE_RD_BLK1_DATA0_REG, EFUSE_RD_BLK1_DATA2_REG}, // range address of EFUSE_BLK1 (3 regs) SYS_DATA_PART0
  23. {EFUSE_RD_BLK2_DATA0_REG, EFUSE_RD_BLK2_DATA7_REG}, // range address of EFUSE_BLK2 (8 regs) SYS_DATA_PART_1
  24. {EFUSE_RD_BLK3_DATA0_REG, EFUSE_RD_BLK3_DATA7_REG}, // range address of EFUSE_BLK3 (8 regs) KEY0
  25. };
  26. static uint32_t write_mass_blocks[EFUSE_BLK_MAX][COUNT_EFUSE_REG_PER_BLOCK] = { 0 };
  27. /*Range addresses to write blocks (it is not real regs, it is buffer) */
  28. const esp_efuse_range_addr_t range_write_addr_blocks[] = {
  29. {(uint32_t) &write_mass_blocks[EFUSE_BLK0][0], (uint32_t) &write_mass_blocks[EFUSE_BLK0][1]},
  30. {(uint32_t) &write_mass_blocks[EFUSE_BLK1][0], (uint32_t) &write_mass_blocks[EFUSE_BLK1][2]},
  31. {(uint32_t) &write_mass_blocks[EFUSE_BLK2][0], (uint32_t) &write_mass_blocks[EFUSE_BLK2][7]},
  32. {(uint32_t) &write_mass_blocks[EFUSE_BLK3][0], (uint32_t) &write_mass_blocks[EFUSE_BLK3][7]},
  33. };
  34. #ifndef CONFIG_EFUSE_VIRTUAL
  35. // Update Efuse timing configuration
  36. static esp_err_t esp_efuse_set_timing(void)
  37. {
  38. efuse_hal_set_timing(0);
  39. return ESP_OK;
  40. }
  41. #endif // ifndef CONFIG_EFUSE_VIRTUAL
  42. // Efuse read operation: copies data from physical efuses to efuse read registers.
  43. void esp_efuse_utility_clear_program_registers(void)
  44. {
  45. ets_efuse_read();
  46. ets_efuse_clear_program_registers();
  47. }
  48. esp_err_t esp_efuse_utility_check_errors(void)
  49. {
  50. return ESP_OK;
  51. }
  52. // Burn values written to the efuse write registers
  53. esp_err_t esp_efuse_utility_burn_chip(void)
  54. {
  55. esp_err_t error = ESP_OK;
  56. #ifdef CONFIG_EFUSE_VIRTUAL
  57. ESP_LOGW(TAG, "Virtual efuses enabled: Not really burning eFuses");
  58. for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
  59. int subblock = 0;
  60. for (uint32_t addr_wr_block = range_write_addr_blocks[num_block].start; addr_wr_block <= range_write_addr_blocks[num_block].end; addr_wr_block += 4) {
  61. virt_blocks[num_block][subblock++] |= REG_READ(addr_wr_block);
  62. }
  63. }
  64. #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  65. esp_efuse_utility_write_efuses_to_flash();
  66. #endif
  67. #else // CONFIG_EFUSE_VIRTUAL
  68. if (esp_efuse_set_timing() != ESP_OK) {
  69. ESP_LOGE(TAG, "Efuse fields are not burnt");
  70. } else {
  71. // Permanently update values written to the efuse write registers
  72. // It is necessary to process blocks in the order from MAX-> EFUSE_BLK0, because EFUSE_BLK0 has protection bits for other blocks.
  73. for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
  74. bool need_burn_block = false;
  75. for (uint32_t addr_wr_block = range_write_addr_blocks[num_block].start; addr_wr_block <= range_write_addr_blocks[num_block].end; addr_wr_block += 4) {
  76. if (REG_READ(addr_wr_block) != 0) {
  77. need_burn_block = true;
  78. break;
  79. }
  80. }
  81. if (!need_burn_block) {
  82. continue;
  83. }
  84. if (error) {
  85. // It is done for a use case: BLOCK2 (Flash encryption key) could have an error (incorrect written data)
  86. // in this case we can not burn any data into BLOCK0 because it might set read/write protections of BLOCK2.
  87. ESP_LOGE(TAG, "BLOCK%d can not be burned because a previous block got an error, skipped.", num_block);
  88. continue;
  89. }
  90. efuse_hal_clear_program_registers();
  91. if (esp_efuse_get_coding_scheme(num_block) == EFUSE_CODING_SCHEME_RS) {
  92. uint8_t block_rs[12];
  93. efuse_hal_rs_calculate((void *)range_write_addr_blocks[num_block].start, block_rs);
  94. hal_memcpy((void *)EFUSE_PGM_CHECK_VALUE0_REG, block_rs, sizeof(block_rs));
  95. }
  96. unsigned r_data_len = (range_read_addr_blocks[num_block].end - range_read_addr_blocks[num_block].start) + sizeof(uint32_t);
  97. unsigned data_len = (range_write_addr_blocks[num_block].end - range_write_addr_blocks[num_block].start) + sizeof(uint32_t);
  98. memcpy((void *)EFUSE_PGM_DATA0_REG, (void *)range_write_addr_blocks[num_block].start, data_len);
  99. uint32_t backup_write_data[8 + 3]; // 8 words are data and 3 words are RS coding data
  100. hal_memcpy(backup_write_data, (void *)EFUSE_PGM_DATA0_REG, sizeof(backup_write_data));
  101. int repeat_burn_op = 1;
  102. bool correct_written_data;
  103. bool coding_error_before = efuse_hal_is_coding_error_in_block(num_block);
  104. if (coding_error_before) {
  105. ESP_LOGW(TAG, "BLOCK%d already has a coding error", num_block);
  106. }
  107. bool coding_error_occurred;
  108. do {
  109. ESP_LOGI(TAG, "BURN BLOCK%d", num_block);
  110. efuse_hal_program(num_block); // BURN a block
  111. bool coding_error_after;
  112. for (unsigned i = 0; i < 5; i++) {
  113. efuse_hal_read();
  114. coding_error_after = efuse_hal_is_coding_error_in_block(num_block);
  115. if (coding_error_after == true) {
  116. break;
  117. }
  118. }
  119. coding_error_occurred = (coding_error_before != coding_error_after) && coding_error_before == false;
  120. if (coding_error_occurred) {
  121. ESP_LOGW(TAG, "BLOCK%d got a coding error", num_block);
  122. }
  123. correct_written_data = esp_efuse_utility_is_correct_written_data(num_block, r_data_len);
  124. if (!correct_written_data || coding_error_occurred) {
  125. ESP_LOGW(TAG, "BLOCK%d: next retry to fix an error [%d/3]...", num_block, repeat_burn_op);
  126. hal_memcpy((void *)EFUSE_PGM_DATA0_REG, (void *)backup_write_data, sizeof(backup_write_data));
  127. }
  128. } while ((!correct_written_data || coding_error_occurred) && repeat_burn_op++ < 3);
  129. if (coding_error_occurred) {
  130. ESP_LOGW(TAG, "Coding error was not fixed");
  131. if (num_block == 0) {
  132. ESP_LOGE(TAG, "BLOCK0 got a coding error, which might be critical for security");
  133. error = ESP_FAIL;
  134. }
  135. }
  136. if (!correct_written_data) {
  137. ESP_LOGE(TAG, "Written data are incorrect");
  138. error = ESP_FAIL;
  139. }
  140. }
  141. }
  142. #endif // CONFIG_EFUSE_VIRTUAL
  143. esp_efuse_utility_reset();
  144. return error;
  145. }
  146. // After esp_efuse_write.. functions EFUSE_BLKx_WDATAx_REG were filled is not coded values.
  147. // This function reads EFUSE_BLKx_WDATAx_REG registers, and checks possible to write these data with RS coding scheme.
  148. // The RS coding scheme does not require data changes for the encoded data. esp32s2 has special registers for this.
  149. // They will be filled during the burn operation.
  150. esp_err_t esp_efuse_utility_apply_new_coding_scheme()
  151. {
  152. // start with EFUSE_BLK1. EFUSE_BLK0 - always uses EFUSE_CODING_SCHEME_NONE.
  153. for (int num_block = EFUSE_BLK1; num_block < EFUSE_BLK_MAX; num_block++) {
  154. if (esp_efuse_get_coding_scheme(num_block) == EFUSE_CODING_SCHEME_RS) {
  155. for (uint32_t addr_wr_block = range_write_addr_blocks[num_block].start; addr_wr_block <= range_write_addr_blocks[num_block].end; addr_wr_block += 4) {
  156. if (REG_READ(addr_wr_block)) {
  157. int num_reg = 0;
  158. for (uint32_t addr_rd_block = range_read_addr_blocks[num_block].start; addr_rd_block <= range_read_addr_blocks[num_block].end; addr_rd_block += 4, ++num_reg) {
  159. if (esp_efuse_utility_read_reg(num_block, num_reg)) {
  160. ESP_LOGE(TAG, "Bits are not empty. Write operation is forbidden.");
  161. return ESP_ERR_CODING;
  162. }
  163. }
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. return ESP_OK;
  170. }