mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-31 11:17:20 +02:00
efuse: Improve messages during burn operation
This commit is contained in:
@@ -135,6 +135,7 @@ void esp_efuse_utility_clear_program_registers(void)
|
|||||||
// Burn values written to the efuse write registers
|
// Burn values written to the efuse write registers
|
||||||
esp_err_t esp_efuse_utility_burn_efuses(void)
|
esp_err_t esp_efuse_utility_burn_efuses(void)
|
||||||
{
|
{
|
||||||
|
esp_err_t error = ESP_OK;
|
||||||
#ifdef CONFIG_EFUSE_VIRTUAL
|
#ifdef CONFIG_EFUSE_VIRTUAL
|
||||||
ESP_LOGW(TAG, "Virtual efuses enabled: Not really burning eFuses");
|
ESP_LOGW(TAG, "Virtual efuses enabled: Not really burning eFuses");
|
||||||
for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
|
for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
|
||||||
@@ -151,8 +152,22 @@ esp_err_t esp_efuse_utility_burn_efuses(void)
|
|||||||
// It is necessary to process blocks in the order from MAX-> EFUSE_BLK0, because EFUSE_BLK0 has protection bits for other blocks.
|
// It is necessary to process blocks in the order from MAX-> EFUSE_BLK0, because EFUSE_BLK0 has protection bits for other blocks.
|
||||||
for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
|
for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
|
||||||
esp_efuse_coding_scheme_t scheme = esp_efuse_get_coding_scheme(num_block);
|
esp_efuse_coding_scheme_t scheme = esp_efuse_get_coding_scheme(num_block);
|
||||||
|
bool need_burn_block = false;
|
||||||
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) {
|
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) {
|
||||||
if (REG_READ(addr_wr_block) != 0) {
|
if (REG_READ(addr_wr_block) != 0) {
|
||||||
|
need_burn_block = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!need_burn_block) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (error) {
|
||||||
|
// It is done for a use case: BLOCK2 (Flash encryption key) could have an error (incorrect written data)
|
||||||
|
// in this case we can not burn any data into BLOCK0 because it might set read/write protections of BLOCK2.
|
||||||
|
ESP_LOGE(TAG, "BLOCK%d can not be burned because a previous block got an error, skipped.", num_block);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
efuse_hal_clear_program_registers();
|
efuse_hal_clear_program_registers();
|
||||||
unsigned w_data_len;
|
unsigned w_data_len;
|
||||||
unsigned r_data_len;
|
unsigned r_data_len;
|
||||||
@@ -175,6 +190,9 @@ esp_err_t esp_efuse_utility_burn_efuses(void)
|
|||||||
int repeat_burn_op = 1;
|
int repeat_burn_op = 1;
|
||||||
bool correct_written_data;
|
bool correct_written_data;
|
||||||
bool coding_error_before = efuse_hal_is_coding_error_in_block(num_block);
|
bool coding_error_before = efuse_hal_is_coding_error_in_block(num_block);
|
||||||
|
if (coding_error_before) {
|
||||||
|
ESP_LOGW(TAG, "BLOCK%d already has a coding error", num_block);
|
||||||
|
}
|
||||||
bool coding_error_occurred;
|
bool coding_error_occurred;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@@ -190,31 +208,29 @@ esp_err_t esp_efuse_utility_burn_efuses(void)
|
|||||||
bool coding_error_after = efuse_hal_is_coding_error_in_block(num_block);
|
bool coding_error_after = efuse_hal_is_coding_error_in_block(num_block);
|
||||||
coding_error_occurred = (coding_error_before != coding_error_after) && coding_error_before == false;
|
coding_error_occurred = (coding_error_before != coding_error_after) && coding_error_before == false;
|
||||||
if (coding_error_occurred) {
|
if (coding_error_occurred) {
|
||||||
ESP_LOGE(TAG, "BLOCK%d has an error", num_block);
|
ESP_LOGW(TAG, "BLOCK%d got a coding error", num_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
correct_written_data = esp_efuse_utility_is_correct_written_data(num_block, r_data_len);
|
correct_written_data = esp_efuse_utility_is_correct_written_data(num_block, r_data_len);
|
||||||
if (!correct_written_data || coding_error_occurred) {
|
if (!correct_written_data || coding_error_occurred) {
|
||||||
ESP_LOGW(TAG, "BLOCK%d: next retry [%d/3]...", num_block, repeat_burn_op);
|
ESP_LOGW(TAG, "BLOCK%d: next retry to fix an error [%d/3]...", num_block, repeat_burn_op);
|
||||||
memcpy((void *)start_write_addr[num_block], (void *)backup_write_data, w_data_len);
|
memcpy((void *)start_write_addr[num_block], (void *)backup_write_data, w_data_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
} while ((!correct_written_data || coding_error_occurred) && repeat_burn_op++ < 3);
|
} while ((!correct_written_data || coding_error_occurred) && repeat_burn_op++ < 3);
|
||||||
|
|
||||||
if (coding_error_occurred) {
|
if (coding_error_occurred) {
|
||||||
ESP_LOGE(TAG, "Coding error occurred in block");
|
ESP_LOGW(TAG, "Coding error was not fixed");
|
||||||
}
|
}
|
||||||
if (!correct_written_data) {
|
if (!correct_written_data) {
|
||||||
ESP_LOGE(TAG, "Written data are incorrect");
|
ESP_LOGE(TAG, "Written data are incorrect");
|
||||||
return ESP_FAIL;
|
error = ESP_FAIL;
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif // CONFIG_EFUSE_VIRTUAL
|
#endif // CONFIG_EFUSE_VIRTUAL
|
||||||
esp_efuse_utility_reset();
|
esp_efuse_utility_reset();
|
||||||
return ESP_OK;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t esp_efuse_utility_apply_34_encoding(const uint8_t *in_bytes, uint32_t *out_words, size_t in_bytes_len)
|
esp_err_t esp_efuse_utility_apply_34_encoding(const uint8_t *in_bytes, uint32_t *out_words, size_t in_bytes_len)
|
||||||
|
@@ -99,6 +99,7 @@ void esp_efuse_utility_clear_program_registers(void)
|
|||||||
// Burn values written to the efuse write registers
|
// Burn values written to the efuse write registers
|
||||||
esp_err_t esp_efuse_utility_burn_efuses(void)
|
esp_err_t esp_efuse_utility_burn_efuses(void)
|
||||||
{
|
{
|
||||||
|
esp_err_t error = ESP_OK;
|
||||||
#ifdef CONFIG_EFUSE_VIRTUAL
|
#ifdef CONFIG_EFUSE_VIRTUAL
|
||||||
ESP_LOGW(TAG, "Virtual efuses enabled: Not really burning eFuses");
|
ESP_LOGW(TAG, "Virtual efuses enabled: Not really burning eFuses");
|
||||||
for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
|
for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
|
||||||
@@ -114,8 +115,22 @@ esp_err_t esp_efuse_utility_burn_efuses(void)
|
|||||||
// Permanently update values written to the efuse write registers
|
// Permanently update values written to the efuse write registers
|
||||||
// It is necessary to process blocks in the order from MAX-> EFUSE_BLK0, because EFUSE_BLK0 has protection bits for other blocks.
|
// It is necessary to process blocks in the order from MAX-> EFUSE_BLK0, because EFUSE_BLK0 has protection bits for other blocks.
|
||||||
for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
|
for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
|
||||||
|
bool need_burn_block = false;
|
||||||
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) {
|
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) {
|
||||||
if (REG_READ(addr_wr_block) != 0) {
|
if (REG_READ(addr_wr_block) != 0) {
|
||||||
|
need_burn_block = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!need_burn_block) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (error) {
|
||||||
|
// It is done for a use case: BLOCK2 (Flash encryption key) could have an error (incorrect written data)
|
||||||
|
// in this case we can not burn any data into BLOCK0 because it might set read/write protections of BLOCK2.
|
||||||
|
ESP_LOGE(TAG, "BLOCK%d can not be burned because a previous block got an error, skipped.", num_block);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
ets_efuse_clear_program_registers();
|
ets_efuse_clear_program_registers();
|
||||||
if (esp_efuse_get_coding_scheme(num_block) == EFUSE_CODING_SCHEME_RS) {
|
if (esp_efuse_get_coding_scheme(num_block) == EFUSE_CODING_SCHEME_RS) {
|
||||||
uint8_t block_rs[12];
|
uint8_t block_rs[12];
|
||||||
@@ -131,6 +146,9 @@ esp_err_t esp_efuse_utility_burn_efuses(void)
|
|||||||
int repeat_burn_op = 1;
|
int repeat_burn_op = 1;
|
||||||
bool correct_written_data;
|
bool correct_written_data;
|
||||||
bool coding_error_before = efuse_hal_is_coding_error_in_block(num_block);
|
bool coding_error_before = efuse_hal_is_coding_error_in_block(num_block);
|
||||||
|
if (coding_error_before) {
|
||||||
|
ESP_LOGW(TAG, "BLOCK%d already has a coding error", num_block);
|
||||||
|
}
|
||||||
bool coding_error_occurred;
|
bool coding_error_occurred;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@@ -147,31 +165,33 @@ esp_err_t esp_efuse_utility_burn_efuses(void)
|
|||||||
}
|
}
|
||||||
coding_error_occurred = (coding_error_before != coding_error_after) && coding_error_before == false;
|
coding_error_occurred = (coding_error_before != coding_error_after) && coding_error_before == false;
|
||||||
if (coding_error_occurred) {
|
if (coding_error_occurred) {
|
||||||
ESP_LOGE(TAG, "BLOCK%d has an error", num_block);
|
ESP_LOGW(TAG, "BLOCK%d got a coding error", num_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
correct_written_data = esp_efuse_utility_is_correct_written_data(num_block, r_data_len);
|
correct_written_data = esp_efuse_utility_is_correct_written_data(num_block, r_data_len);
|
||||||
if (!correct_written_data || coding_error_occurred) {
|
if (!correct_written_data || coding_error_occurred) {
|
||||||
ESP_LOGW(TAG, "BLOCK%d: next retry [%d/3]...", num_block, repeat_burn_op);
|
ESP_LOGW(TAG, "BLOCK%d: next retry to fix an error [%d/3]...", num_block, repeat_burn_op);
|
||||||
memcpy((void *)EFUSE_PGM_DATA0_REG, (void *)backup_write_data, sizeof(backup_write_data));
|
memcpy((void *)EFUSE_PGM_DATA0_REG, (void *)backup_write_data, sizeof(backup_write_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
} while ((!correct_written_data || coding_error_occurred) && repeat_burn_op++ < 3);
|
} while ((!correct_written_data || coding_error_occurred) && repeat_burn_op++ < 3);
|
||||||
|
|
||||||
if (coding_error_occurred) {
|
if (coding_error_occurred) {
|
||||||
ESP_LOGE(TAG, "Coding error occurred in block");
|
ESP_LOGW(TAG, "Coding error was not fixed");
|
||||||
|
if (num_block == 0) {
|
||||||
|
ESP_LOGE(TAG, "BLOCK0 got a coding error, which might be critical for security");
|
||||||
|
error = ESP_FAIL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!correct_written_data) {
|
if (!correct_written_data) {
|
||||||
ESP_LOGE(TAG, "Written data are incorrect");
|
ESP_LOGE(TAG, "Written data are incorrect");
|
||||||
return ESP_FAIL;
|
error = ESP_FAIL;
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif // CONFIG_EFUSE_VIRTUAL
|
#endif // CONFIG_EFUSE_VIRTUAL
|
||||||
esp_efuse_utility_reset();
|
esp_efuse_utility_reset();
|
||||||
return ESP_OK;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// After esp_efuse_write.. functions EFUSE_BLKx_WDATAx_REG were filled is not coded values.
|
// After esp_efuse_write.. functions EFUSE_BLKx_WDATAx_REG were filled is not coded values.
|
||||||
|
@@ -376,7 +376,7 @@ bool esp_efuse_utility_is_correct_written_data(esp_efuse_block_t block, unsigned
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!correct_written_data) {
|
if (!correct_written_data) {
|
||||||
ESP_LOGE(TAG, "BURN BLOCK%d - was not successful", block);
|
ESP_LOGE(TAG, "BURN BLOCK%d - ERROR (written bits != read bits)", block);
|
||||||
}
|
}
|
||||||
return correct_written_data;
|
return correct_written_data;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user