esp_efuse_utility.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "hal/efuse_hal.h"
  14. static const char *TAG = "efuse";
  15. #ifdef CONFIG_EFUSE_VIRTUAL
  16. extern uint32_t virt_blocks[EFUSE_BLK_MAX][COUNT_EFUSE_REG_PER_BLOCK];
  17. #endif // CONFIG_EFUSE_VIRTUAL
  18. /*Range addresses to read blocks*/
  19. const esp_efuse_range_addr_t range_read_addr_blocks[] = {
  20. {EFUSE_RD_WR_DIS_REG, EFUSE_RD_REPEAT_DATA4_REG}, // range address of EFUSE_BLK0 REPEAT
  21. {EFUSE_RD_MAC_SPI_SYS_0_REG, EFUSE_RD_MAC_SPI_SYS_5_REG}, // range address of EFUSE_BLK1 MAC_SPI_8M
  22. {EFUSE_RD_SYS_PART1_DATA0_REG, EFUSE_RD_SYS_PART1_DATA7_REG}, // range address of EFUSE_BLK2 SYS_DATA
  23. {EFUSE_RD_USR_DATA0_REG, EFUSE_RD_USR_DATA7_REG}, // range address of EFUSE_BLK3 USR_DATA
  24. {EFUSE_RD_KEY0_DATA0_REG, EFUSE_RD_KEY0_DATA7_REG}, // range address of EFUSE_BLK4 KEY0
  25. {EFUSE_RD_KEY1_DATA0_REG, EFUSE_RD_KEY1_DATA7_REG}, // range address of EFUSE_BLK5 KEY1
  26. {EFUSE_RD_KEY2_DATA0_REG, EFUSE_RD_KEY2_DATA7_REG}, // range address of EFUSE_BLK6 KEY2
  27. {EFUSE_RD_KEY3_DATA0_REG, EFUSE_RD_KEY3_DATA7_REG}, // range address of EFUSE_BLK7 KEY3
  28. {EFUSE_RD_KEY4_DATA0_REG, EFUSE_RD_KEY4_DATA7_REG}, // range address of EFUSE_BLK8 KEY4
  29. {EFUSE_RD_KEY5_DATA0_REG, EFUSE_RD_KEY5_DATA7_REG}, // range address of EFUSE_BLK9 KEY5
  30. {EFUSE_RD_SYS_PART2_DATA0_REG, EFUSE_RD_SYS_PART2_DATA7_REG} // range address of EFUSE_BLK10 KEY6
  31. };
  32. static uint32_t write_mass_blocks[EFUSE_BLK_MAX][COUNT_EFUSE_REG_PER_BLOCK] = { 0 };
  33. /*Range addresses to write blocks (it is not real regs, it is buffer) */
  34. const esp_efuse_range_addr_t range_write_addr_blocks[] = {
  35. {(uint32_t) &write_mass_blocks[EFUSE_BLK0][0], (uint32_t) &write_mass_blocks[EFUSE_BLK0][5]},
  36. {(uint32_t) &write_mass_blocks[EFUSE_BLK1][0], (uint32_t) &write_mass_blocks[EFUSE_BLK1][5]},
  37. {(uint32_t) &write_mass_blocks[EFUSE_BLK2][0], (uint32_t) &write_mass_blocks[EFUSE_BLK2][7]},
  38. {(uint32_t) &write_mass_blocks[EFUSE_BLK3][0], (uint32_t) &write_mass_blocks[EFUSE_BLK3][7]},
  39. {(uint32_t) &write_mass_blocks[EFUSE_BLK4][0], (uint32_t) &write_mass_blocks[EFUSE_BLK4][7]},
  40. {(uint32_t) &write_mass_blocks[EFUSE_BLK5][0], (uint32_t) &write_mass_blocks[EFUSE_BLK5][7]},
  41. {(uint32_t) &write_mass_blocks[EFUSE_BLK6][0], (uint32_t) &write_mass_blocks[EFUSE_BLK6][7]},
  42. {(uint32_t) &write_mass_blocks[EFUSE_BLK7][0], (uint32_t) &write_mass_blocks[EFUSE_BLK7][7]},
  43. {(uint32_t) &write_mass_blocks[EFUSE_BLK8][0], (uint32_t) &write_mass_blocks[EFUSE_BLK8][7]},
  44. {(uint32_t) &write_mass_blocks[EFUSE_BLK9][0], (uint32_t) &write_mass_blocks[EFUSE_BLK9][7]},
  45. {(uint32_t) &write_mass_blocks[EFUSE_BLK10][0], (uint32_t) &write_mass_blocks[EFUSE_BLK10][7]},
  46. };
  47. #ifndef CONFIG_EFUSE_VIRTUAL
  48. // Update Efuse timing configuration
  49. static esp_err_t esp_efuse_set_timing(void)
  50. {
  51. uint32_t clock_hz = esp_clk_apb_freq();
  52. efuse_hal_set_timing(clock_hz);
  53. return ESP_OK;
  54. }
  55. #endif // ifndef CONFIG_EFUSE_VIRTUAL
  56. // Efuse read operation: copies data from physical efuses to efuse read registers.
  57. void esp_efuse_utility_clear_program_registers(void)
  58. {
  59. efuse_hal_read();
  60. efuse_hal_clear_program_registers();
  61. }
  62. esp_err_t esp_efuse_utility_check_errors(void)
  63. {
  64. return ESP_OK;
  65. }
  66. // Burn values written to the efuse write registers
  67. esp_err_t esp_efuse_utility_burn_chip(void)
  68. {
  69. esp_err_t error = ESP_OK;
  70. #ifdef CONFIG_EFUSE_VIRTUAL
  71. ESP_LOGW(TAG, "Virtual efuses enabled: Not really burning eFuses");
  72. for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
  73. int subblock = 0;
  74. 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) {
  75. virt_blocks[num_block][subblock++] |= REG_READ(addr_wr_block);
  76. }
  77. }
  78. #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  79. esp_efuse_utility_write_efuses_to_flash();
  80. #endif
  81. #else // CONFIG_EFUSE_VIRTUAL
  82. if (esp_efuse_set_timing() != ESP_OK) {
  83. ESP_LOGE(TAG, "Efuse fields are not burnt");
  84. } else {
  85. // Permanently update values written to the efuse write registers
  86. // It is necessary to process blocks in the order from MAX-> EFUSE_BLK0, because EFUSE_BLK0 has protection bits for other blocks.
  87. for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
  88. bool need_burn_block = false;
  89. 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) {
  90. if (REG_READ(addr_wr_block) != 0) {
  91. need_burn_block = true;
  92. break;
  93. }
  94. }
  95. if (!need_burn_block) {
  96. continue;
  97. }
  98. if (error) {
  99. // It is done for a use case: BLOCK2 (Flash encryption key) could have an error (incorrect written data)
  100. // in this case we can not burn any data into BLOCK0 because it might set read/write protections of BLOCK2.
  101. ESP_LOGE(TAG, "BLOCK%d can not be burned because a previous block got an error, skipped.", num_block);
  102. continue;
  103. }
  104. efuse_hal_clear_program_registers();
  105. if (esp_efuse_get_coding_scheme(num_block) == EFUSE_CODING_SCHEME_RS) {
  106. uint8_t block_rs[12];
  107. efuse_hal_rs_calculate((void *)range_write_addr_blocks[num_block].start, block_rs);
  108. #pragma GCC diagnostic push
  109. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  110. #pragma GCC diagnostic ignored "-Warray-bounds"
  111. memcpy((void *)EFUSE_PGM_CHECK_VALUE0_REG, block_rs, sizeof(block_rs));
  112. #pragma GCC diagnostic pop
  113. }
  114. unsigned r_data_len = (range_read_addr_blocks[num_block].end - range_read_addr_blocks[num_block].start) + sizeof(uint32_t);
  115. unsigned data_len = (range_write_addr_blocks[num_block].end - range_write_addr_blocks[num_block].start) + sizeof(uint32_t);
  116. memcpy((void *)EFUSE_PGM_DATA0_REG, (void *)range_write_addr_blocks[num_block].start, data_len);
  117. uint32_t backup_write_data[8 + 3]; // 8 words are data and 3 words are RS coding data
  118. #pragma GCC diagnostic push
  119. #if __GNUC__ >= 11
  120. #pragma GCC diagnostic ignored "-Wstringop-overread"
  121. #endif
  122. #pragma GCC diagnostic ignored "-Warray-bounds"
  123. memcpy(backup_write_data, (void *)EFUSE_PGM_DATA0_REG, sizeof(backup_write_data));
  124. #pragma GCC diagnostic pop
  125. int repeat_burn_op = 1;
  126. bool correct_written_data;
  127. bool coding_error_before = efuse_hal_is_coding_error_in_block(num_block);
  128. if (coding_error_before) {
  129. ESP_LOGW(TAG, "BLOCK%d already has a coding error", num_block);
  130. }
  131. bool coding_error_occurred;
  132. do {
  133. ESP_LOGI(TAG, "BURN BLOCK%d", num_block);
  134. efuse_hal_program(num_block); // BURN a block
  135. bool coding_error_after;
  136. for (unsigned i = 0; i < 5; i++) {
  137. efuse_hal_read();
  138. coding_error_after = efuse_hal_is_coding_error_in_block(num_block);
  139. if (coding_error_after == true) {
  140. break;
  141. }
  142. }
  143. coding_error_occurred = (coding_error_before != coding_error_after) && coding_error_before == false;
  144. if (coding_error_occurred) {
  145. ESP_LOGW(TAG, "BLOCK%d got a coding error", num_block);
  146. }
  147. correct_written_data = esp_efuse_utility_is_correct_written_data(num_block, r_data_len);
  148. if (!correct_written_data || coding_error_occurred) {
  149. ESP_LOGW(TAG, "BLOCK%d: next retry to fix an error [%d/3]...", num_block, repeat_burn_op);
  150. #pragma GCC diagnostic push
  151. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  152. #pragma GCC diagnostic ignored "-Warray-bounds"
  153. memcpy((void *)EFUSE_PGM_DATA0_REG, (void *)backup_write_data, sizeof(backup_write_data));
  154. #pragma GCC diagnostic pop
  155. }
  156. } while ((!correct_written_data || coding_error_occurred) && repeat_burn_op++ < 3);
  157. if (coding_error_occurred) {
  158. ESP_LOGW(TAG, "Coding error was not fixed");
  159. if (num_block == 0) {
  160. ESP_LOGE(TAG, "BLOCK0 got a coding error, which might be critical for security");
  161. error = ESP_FAIL;
  162. }
  163. }
  164. if (!correct_written_data) {
  165. ESP_LOGE(TAG, "Written data are incorrect");
  166. error = ESP_FAIL;
  167. }
  168. }
  169. }
  170. #endif // CONFIG_EFUSE_VIRTUAL
  171. esp_efuse_utility_reset();
  172. return error;
  173. }
  174. // After esp_efuse_write.. functions EFUSE_BLKx_WDATAx_REG were filled is not coded values.
  175. // This function reads EFUSE_BLKx_WDATAx_REG registers, and checks possible to write these data with RS coding scheme.
  176. // The RS coding scheme does not require data changes for the encoded data. esp32s2 has special registers for this.
  177. // They will be filled during the burn operation.
  178. esp_err_t esp_efuse_utility_apply_new_coding_scheme()
  179. {
  180. // start with EFUSE_BLK1. EFUSE_BLK0 - always uses EFUSE_CODING_SCHEME_NONE.
  181. for (int num_block = EFUSE_BLK1; num_block < EFUSE_BLK_MAX; num_block++) {
  182. if (esp_efuse_get_coding_scheme(num_block) == EFUSE_CODING_SCHEME_RS) {
  183. 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) {
  184. if (REG_READ(addr_wr_block)) {
  185. int num_reg = 0;
  186. 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) {
  187. if (esp_efuse_utility_read_reg(num_block, num_reg)) {
  188. ESP_LOGE(TAG, "Bits are not empty. Write operation is forbidden.");
  189. return ESP_ERR_CODING;
  190. }
  191. }
  192. break;
  193. }
  194. }
  195. }
  196. }
  197. return ESP_OK;
  198. }