From e834d6fffc23a6fcfc0d2e871c9235417a7fb48f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 9 Jul 2018 08:59:50 +0300 Subject: [PATCH] CRC check improved. --- components/wear_levelling/WL_Flash.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/components/wear_levelling/WL_Flash.cpp b/components/wear_levelling/WL_Flash.cpp index 6a279f95ce..37c57b150c 100644 --- a/components/wear_levelling/WL_Flash.cpp +++ b/components/wear_levelling/WL_Flash.cpp @@ -328,14 +328,21 @@ esp_err_t WL_Flash::updateV1_V2() int check_size = offsetof(wl_state_t, device_id); // Chech CRC and recover state uint32_t crc1 = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, check_size); + wl_state_t sa_copy; + wl_state_t *state_copy = &sa_copy; + result = this->flash_drv->read(this->addr_state2, state_copy, sizeof(wl_state_t)); + WL_RESULT_CHECK(result); + uint32_t crc2 = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)state_copy, check_size); + // For V1 crc in place of device_id and version - uint32_t v1_crc = this->state.device_id; + uint32_t v1_crc1 = this->state.device_id; + uint32_t v1_crc2 = state_copy->device_id; - ESP_LOGD(TAG, "%s - process crc1=0x%08x v1_crc=0x%08x, version=%i", __func__, crc1, v1_crc, this->state.version); + ESP_LOGD(TAG, "%s - process crc1=0x%08x, crc2=0x%08x, v1_crc1=0x%08x, v1_crc2=0x%08x, version=%i", __func__, crc1, crc2, v1_crc1, v1_crc2, this->state.version); - if ((crc1 == v1_crc) && (this->state.version == 1)){ + if ((crc1 == v1_crc1) && (crc2 == v1_crc2) && (v1_crc1 == v1_crc2) && (this->state.version == 1)){ // Here we have to update all internal structures - ESP_LOGI(TAG, "%s Update from V1 to V2", __func__); + ESP_LOGI(TAG, "%s Update from V1 to V2, crc=0x%08x, ", __func__, crc1); uint32_t pos = 0; for (size_t i = 0; i < this->state.max_pos; i++) { -- 2.40.0