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