From 82eca97300410dd743de7f116836879cf3cdc6ba Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 25 Oct 2018 15:31:28 +0300 Subject: [PATCH] Version update from V1 to V2 now done in correct way. Before this works only first time. The source and test updated. State length was changed to macro. --- components/wear_levelling/WL_Flash.cpp | 10 ++--- .../wear_levelling/private_include/WL_State.h | 2 + components/wear_levelling/test/test_wl.c | 38 ++++++++++--------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/components/wear_levelling/WL_Flash.cpp b/components/wear_levelling/WL_Flash.cpp index 06d3da5ef..36fe6b131 100644 --- a/components/wear_levelling/WL_Flash.cpp +++ b/components/wear_levelling/WL_Flash.cpp @@ -132,7 +132,7 @@ esp_err_t WL_Flash::init() result = this->flash_drv->read(this->addr_state2, state_copy, sizeof(wl_state_t)); WL_RESULT_CHECK(result); - int check_size = offsetof(wl_state_t, crc); + int check_size = WL_STATE_CRC_LEN_V2; // Chech CRC and recover state uint32_t crc1 = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, check_size); uint32_t crc2 = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)state_copy, check_size); @@ -288,7 +288,7 @@ esp_err_t WL_Flash::initSections() this->state.max_pos = 1 + this->flash_size / this->cfg.page_size; - this->state.crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, offsetof(wl_state_t, crc)); + this->state.crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, WL_STATE_CRC_LEN_V2); result = this->flash_drv->erase_range(this->addr_state1, this->state_size); WL_RESULT_CHECK(result); @@ -327,7 +327,7 @@ esp_err_t WL_Flash::updateV1_V2() esp_err_t result = ESP_OK; // Check crc for old version and old version ESP_LOGV(TAG, "%s start", __func__); - int check_size = offsetof(wl_state_t, device_id); + int check_size = WL_STATE_CRC_LEN_V1; // 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; @@ -365,9 +365,9 @@ esp_err_t WL_Flash::updateV1_V2() this->state.version = 2; this->state.pos = 0; - this->state.crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, offsetof(wl_state_t, crc)); this->state.device_id = esp_random(); memset(this->state.reserved, 0, sizeof(this->state.reserved)); + this->state.crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, WL_STATE_CRC_LEN_V2); result = this->flash_drv->erase_range(this->addr_state1, this->state_size); WL_RESULT_CHECK(result); @@ -493,7 +493,7 @@ esp_err_t WL_Flash::updateWL() this->state.move_count = 0; } // write main state - this->state.crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, offsetof(wl_state_t, crc)); + this->state.crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, WL_STATE_CRC_LEN_V2); result = this->flash_drv->erase_range(this->addr_state1, this->state_size); WL_RESULT_CHECK(result); diff --git a/components/wear_levelling/private_include/WL_State.h b/components/wear_levelling/private_include/WL_State.h index 7464ba9b8..7b6d3f7ed 100644 --- a/components/wear_levelling/private_include/WL_State.h +++ b/components/wear_levelling/private_include/WL_State.h @@ -45,5 +45,7 @@ public: static_assert(sizeof(wl_state_t) % 16 == 0, "Size of wl_state_t structure should be compatible with flash encryption"); #endif // _MSC_VER +#define WL_STATE_CRC_LEN_V1 offsetof(wl_state_t, device_id) +#define WL_STATE_CRC_LEN_V2 offsetof(wl_state_t, crc) #endif // _WL_State_H_ diff --git a/components/wear_levelling/test/test_wl.c b/components/wear_levelling/test/test_wl.c index 02dba8f78..0c2bf39dc 100644 --- a/components/wear_levelling/test/test_wl.c +++ b/components/wear_levelling/test/test_wl.c @@ -279,27 +279,29 @@ TEST_CASE("Version update test", "[wear_levelling]") esp_partition_erase_range(&fake_partition, 0, fake_partition.size); esp_partition_write(&fake_partition, 0, test_partition_v1_bin_start, fake_partition.size); + for (int i=0 ; i< 3 ; i++) + { + printf("Pass %i\n", i); + wl_handle_t handle; + TEST_ESP_OK(wl_mount(&fake_partition, &handle)); + size_t sector_size = wl_sector_size(handle); + uint32_t* buff = (uint32_t*)malloc(sector_size); - wl_handle_t handle; - TEST_ESP_OK(wl_mount(&fake_partition, &handle)); - size_t sector_size = wl_sector_size(handle); - uint32_t* buff = (uint32_t*)malloc(sector_size); + uint32_t init_val = COMPARE_START_CONST; + int test_count = fake_partition.size/sector_size - 4; - uint32_t init_val = COMPARE_START_CONST; - int test_count = fake_partition.size/sector_size - 4; - - for (int m=0 ; m < test_count; m++) { - TEST_ESP_OK(wl_read(handle, sector_size * m, buff, sector_size)); - for (int i=0 ; i< sector_size/sizeof(uint32_t) ; i++) { - uint32_t compare_val = init_val + i + m*sector_size; - if (buff[i] != compare_val) - { - printf("error compare: 0x%08x != 0x%08x \n", buff[i], compare_val); + for (int m=0 ; m < test_count; m++) { + TEST_ESP_OK(wl_read(handle, sector_size * m, buff, sector_size)); + for (int i=0 ; i< sector_size/sizeof(uint32_t) ; i++) { + uint32_t compare_val = init_val + i + m*sector_size; + if (buff[i] != compare_val) + { + printf("error compare: 0x%08x != 0x%08x \n", buff[i], compare_val); + } + TEST_ASSERT_EQUAL( buff[i], compare_val); } - TEST_ASSERT_EQUAL( buff[i], compare_val); } + free(buff); + wl_unmount(handle); } - - free(buff); - wl_unmount(handle); }