esp_efuse_utility.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "esp_efuse_utility.h"
  7. #include "soc/efuse_periph.h"
  8. #include "hal/efuse_hal.h"
  9. #include "esp_private/esp_clk.h"
  10. #include "esp_log.h"
  11. #include "assert.h"
  12. #include "sdkconfig.h"
  13. #include <sys/param.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_BLK0_RDATA0_REG, EFUSE_BLK0_RDATA6_REG}, // range address of EFUSE_BLK0
  21. {EFUSE_BLK1_RDATA0_REG, EFUSE_BLK1_RDATA7_REG}, // range address of EFUSE_BLK1
  22. {EFUSE_BLK2_RDATA0_REG, EFUSE_BLK2_RDATA7_REG}, // range address of EFUSE_BLK2
  23. {EFUSE_BLK3_RDATA0_REG, EFUSE_BLK3_RDATA7_REG} // range address of EFUSE_BLK3
  24. };
  25. static uint32_t write_mass_blocks[EFUSE_BLK_MAX][COUNT_EFUSE_REG_PER_BLOCK] = { 0 };
  26. /*Range addresses to write blocks (it is not real regs, it is a buffer) */
  27. const esp_efuse_range_addr_t range_write_addr_blocks[] = {
  28. {(uint32_t) &write_mass_blocks[EFUSE_BLK0][0], (uint32_t) &write_mass_blocks[EFUSE_BLK0][6]},
  29. {(uint32_t) &write_mass_blocks[EFUSE_BLK1][0], (uint32_t) &write_mass_blocks[EFUSE_BLK1][7]},
  30. {(uint32_t) &write_mass_blocks[EFUSE_BLK2][0], (uint32_t) &write_mass_blocks[EFUSE_BLK2][7]},
  31. {(uint32_t) &write_mass_blocks[EFUSE_BLK3][0], (uint32_t) &write_mass_blocks[EFUSE_BLK3][7]},
  32. };
  33. #ifndef CONFIG_EFUSE_VIRTUAL
  34. /* Addresses to write blocks*/
  35. const uint32_t start_write_addr[] = {
  36. EFUSE_BLK0_WDATA0_REG,
  37. EFUSE_BLK1_WDATA0_REG,
  38. EFUSE_BLK2_WDATA0_REG,
  39. EFUSE_BLK3_WDATA0_REG,
  40. };
  41. static void apply_repeat_encoding(const uint8_t *in_bytes, uint32_t *out_words, size_t in_bytes_len);
  42. // Update Efuse timing configuration
  43. static esp_err_t esp_efuse_set_timing(void)
  44. {
  45. uint32_t apb_freq_mhz = esp_clk_apb_freq() / 1000000;
  46. efuse_hal_set_timing(apb_freq_mhz);
  47. return ESP_OK;
  48. }
  49. #endif // ifndef CONFIG_EFUSE_VIRTUAL
  50. // Efuse read operation: copies data from physical efuses to efuse read registers.
  51. void esp_efuse_utility_clear_program_registers(void)
  52. {
  53. efuse_hal_clear_program_registers();
  54. }
  55. esp_err_t esp_efuse_utility_check_errors(void)
  56. {
  57. return ESP_OK;
  58. }
  59. // Burn values written to the efuse write registers
  60. esp_err_t esp_efuse_utility_burn_chip(void)
  61. {
  62. esp_err_t error = ESP_OK;
  63. #ifdef CONFIG_EFUSE_VIRTUAL
  64. ESP_LOGW(TAG, "Virtual efuses enabled: Not really burning eFuses");
  65. for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
  66. int subblock = 0;
  67. 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) {
  68. virt_blocks[num_block][subblock++] |= REG_READ(addr_wr_block);
  69. }
  70. }
  71. #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  72. esp_efuse_utility_write_efuses_to_flash();
  73. #endif
  74. #else // CONFIG_EFUSE_VIRTUAL
  75. if (esp_efuse_set_timing() != ESP_OK) {
  76. ESP_LOGE(TAG, "Efuse fields are not burnt");
  77. } else {
  78. // Permanently update values written to the efuse write registers
  79. // It is necessary to process blocks in the order from MAX-> EFUSE_BLK0, because EFUSE_BLK0 has protection bits for other blocks.
  80. for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
  81. esp_efuse_coding_scheme_t scheme = esp_efuse_get_coding_scheme(num_block);
  82. bool need_burn_block = false;
  83. 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) {
  84. if (REG_READ(addr_wr_block) != 0) {
  85. need_burn_block = true;
  86. break;
  87. }
  88. }
  89. if (!need_burn_block) {
  90. continue;
  91. }
  92. if (error) {
  93. // It is done for a use case: BLOCK2 (Flash encryption key) could have an error (incorrect written data)
  94. // in this case we can not burn any data into BLOCK0 because it might set read/write protections of BLOCK2.
  95. ESP_LOGE(TAG, "BLOCK%d can not be burned because a previous block got an error, skipped.", num_block);
  96. continue;
  97. }
  98. efuse_hal_clear_program_registers();
  99. unsigned w_data_len;
  100. unsigned r_data_len;
  101. if (scheme == EFUSE_CODING_SCHEME_3_4) {
  102. esp_efuse_utility_apply_34_encoding((void *)range_write_addr_blocks[num_block].start, (uint32_t *)start_write_addr[num_block], ESP_EFUSE_LEN_OF_3_4_SCHEME_BLOCK_IN_BYTES);
  103. r_data_len = ESP_EFUSE_LEN_OF_3_4_SCHEME_BLOCK_IN_BYTES;
  104. w_data_len = 32;
  105. } else if (scheme == EFUSE_CODING_SCHEME_REPEAT) {
  106. apply_repeat_encoding((void *)range_write_addr_blocks[num_block].start, (uint32_t *)start_write_addr[num_block], 16);
  107. r_data_len = ESP_EFUSE_LEN_OF_REPEAT_BLOCK_IN_BYTES;
  108. w_data_len = 32;
  109. } else {
  110. r_data_len = (range_read_addr_blocks[num_block].end - range_read_addr_blocks[num_block].start) + sizeof(uint32_t);
  111. w_data_len = (range_write_addr_blocks[num_block].end - range_write_addr_blocks[num_block].start) + sizeof(uint32_t);
  112. memcpy((void *)start_write_addr[num_block], (void *)range_write_addr_blocks[num_block].start, w_data_len);
  113. }
  114. uint32_t backup_write_data[8];
  115. memcpy(backup_write_data, (void *)start_write_addr[num_block], w_data_len);
  116. int repeat_burn_op = 1;
  117. bool correct_written_data;
  118. bool coding_error_before = efuse_hal_is_coding_error_in_block(num_block);
  119. if (coding_error_before) {
  120. ESP_LOGW(TAG, "BLOCK%d already has a coding error", num_block);
  121. }
  122. bool coding_error_occurred;
  123. do {
  124. ESP_LOGI(TAG, "BURN BLOCK%d", num_block);
  125. efuse_hal_program(0); // BURN a block
  126. bool coding_error_after = efuse_hal_is_coding_error_in_block(num_block);
  127. coding_error_occurred = (coding_error_before != coding_error_after) && coding_error_before == false;
  128. if (coding_error_occurred) {
  129. ESP_LOGW(TAG, "BLOCK%d got a coding error", num_block);
  130. }
  131. correct_written_data = esp_efuse_utility_is_correct_written_data(num_block, r_data_len);
  132. if (!correct_written_data || coding_error_occurred) {
  133. ESP_LOGW(TAG, "BLOCK%d: next retry to fix an error [%d/3]...", num_block, repeat_burn_op);
  134. memcpy((void *)start_write_addr[num_block], (void *)backup_write_data, w_data_len);
  135. }
  136. } while ((!correct_written_data || coding_error_occurred) && repeat_burn_op++ < 3);
  137. if (coding_error_occurred) {
  138. ESP_LOGW(TAG, "Coding error was not fixed");
  139. }
  140. if (!correct_written_data) {
  141. ESP_LOGE(TAG, "Written data are incorrect");
  142. error = ESP_FAIL;
  143. }
  144. }
  145. }
  146. #endif // CONFIG_EFUSE_VIRTUAL
  147. esp_efuse_utility_reset();
  148. return error;
  149. }
  150. esp_err_t esp_efuse_utility_apply_34_encoding(const uint8_t *in_bytes, uint32_t *out_words, size_t in_bytes_len)
  151. {
  152. if (in_bytes == NULL || out_words == NULL || in_bytes_len % 6 != 0) {
  153. return ESP_ERR_INVALID_ARG;
  154. }
  155. while (in_bytes_len > 0) {
  156. uint8_t out[8];
  157. uint8_t xor = 0;
  158. uint8_t mul = 0;
  159. for (int i = 0; i < 6; i++) {
  160. xor ^= in_bytes[i];
  161. mul += (i + 1) * __builtin_popcount(in_bytes[i]);
  162. }
  163. memcpy(out, in_bytes, 6); // Data bytes
  164. out[6] = xor;
  165. out[7] = mul;
  166. memcpy(out_words, out, 8);
  167. in_bytes_len -= 6;
  168. in_bytes += 6;
  169. out_words += 2;
  170. }
  171. return ESP_OK;
  172. }
  173. #ifndef CONFIG_EFUSE_VIRTUAL
  174. static void apply_repeat_encoding(const uint8_t *in_bytes, uint32_t *out_words, size_t in_bytes_len)
  175. {
  176. for (unsigned i = 0; i < 2; i++) {
  177. memcpy(&out_words[i * 4], (uint32_t *)in_bytes, in_bytes_len);
  178. }
  179. }
  180. #endif // CONFIG_EFUSE_VIRTUAL
  181. static void read_r_data(esp_efuse_block_t num_block, uint32_t* buf_r_data)
  182. {
  183. int i = 0;
  184. 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, ++i) {
  185. buf_r_data[i] = esp_efuse_utility_read_reg(num_block, i);
  186. }
  187. }
  188. // This function just checkes that given data for blocks will not rise a coding issue during the burn operation.
  189. // Encoded data will be set during the burn operation.
  190. esp_err_t esp_efuse_utility_apply_new_coding_scheme()
  191. {
  192. uint8_t buf_r_data[COUNT_EFUSE_REG_PER_BLOCK * 4];
  193. // start with EFUSE_BLK1. EFUSE_BLK0 - always uses EFUSE_CODING_SCHEME_NONE.
  194. for (int num_block = EFUSE_BLK1; num_block < EFUSE_BLK_MAX; num_block++) {
  195. esp_efuse_coding_scheme_t scheme = esp_efuse_get_coding_scheme(num_block);
  196. if (scheme != EFUSE_CODING_SCHEME_NONE) {
  197. bool is_write_data = false;
  198. 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) {
  199. if (REG_READ(addr_wr_block)) {
  200. is_write_data = true;
  201. break;
  202. }
  203. }
  204. if (is_write_data) {
  205. read_r_data(num_block, (uint32_t*)buf_r_data);
  206. uint8_t* buf_w_data = (uint8_t*)range_write_addr_blocks[num_block].start;
  207. if (scheme == EFUSE_CODING_SCHEME_3_4) {
  208. if (*((uint32_t*)buf_w_data + 6) != 0 || *((uint32_t*)buf_w_data + 7) != 0) {
  209. return ESP_ERR_CODING;
  210. }
  211. for (int i = 0; i < ESP_EFUSE_LEN_OF_3_4_SCHEME_BLOCK_IN_BYTES; ++i) {
  212. if (buf_w_data[i] != 0) {
  213. int st_offset_buf = (i / 6) * 6;
  214. // check that place is free.
  215. for (int n = st_offset_buf; n < st_offset_buf + 6; ++n) {
  216. if (buf_r_data[n] != 0) {
  217. ESP_LOGE(TAG, "Bits are not empty. Write operation is forbidden.");
  218. return ESP_ERR_CODING;
  219. }
  220. }
  221. }
  222. }
  223. } else if (scheme == EFUSE_CODING_SCHEME_REPEAT) {
  224. for (int i = 4; i < 8; ++i) {
  225. if (*((uint32_t*)buf_w_data + i) != 0) {
  226. return ESP_ERR_CODING;
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. return ESP_OK;
  234. }