From f05f3fbde87a9ce45c6818f71b49cd13888fd457 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 7 Jun 2018 13:32:37 +0300 Subject: [PATCH] Added test for version update from V1 to V2. Problems for tests on host are fixed. Random function changed to esp_random() --- .../spi_flash/sim/stubs/log/include/esp_log.h | 6 +- components/wear_levelling/WL_Flash.cpp | 86 +++++++++--------- .../wear_levelling/doc/wl_sw_structure.rst | 7 +- .../wear_levelling/private_include/WL_Flash.h | 1 - .../wear_levelling/private_include/WL_State.h | 1 + components/wear_levelling/test/component.mk | 1 + .../wear_levelling/test/test_partition_v1.bin | Bin 0 -> 32768 bytes components/wear_levelling/test/test_wl.c | 54 ++++++++++- .../wear_levelling/test_wl_host/Makefile | 1 + .../test_wl_host/stubs/log/esp_random.c | 14 +++ .../stubs/log/include/esp_system.h | 15 +++ components/wear_levelling/wear_levelling.cpp | 1 - 12 files changed, 139 insertions(+), 48 deletions(-) create mode 100644 components/wear_levelling/test/test_partition_v1.bin create mode 100644 components/wear_levelling/test_wl_host/stubs/log/esp_random.c create mode 100644 components/wear_levelling/test_wl_host/stubs/log/include/esp_system.h diff --git a/components/spi_flash/sim/stubs/log/include/esp_log.h b/components/spi_flash/sim/stubs/log/include/esp_log.h index 6c9bb6f759..68cd216bce 100644 --- a/components/spi_flash/sim/stubs/log/include/esp_log.h +++ b/components/spi_flash/sim/stubs/log/include/esp_log.h @@ -44,11 +44,13 @@ void esp_log_write(esp_log_level_t level, const char* tag, const char* format, . #define ESP_LOGE( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_ERROR) { esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } -#define ESP_LOGV( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE) { esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define ESP_LOGW( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_WARN) { esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } + +#define ESP_LOGI( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } #define ESP_LOGD( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) { esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } -#define ESP_LOGW( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_WARN) { esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define ESP_LOGV( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE) { esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } // Assume that flash encryption is not enabled. Put here since in partition.c // esp_log.h is included later than esp_flash_encrypt.h. diff --git a/components/wear_levelling/WL_Flash.cpp b/components/wear_levelling/WL_Flash.cpp index fec527a627..28e5d64804 100644 --- a/components/wear_levelling/WL_Flash.cpp +++ b/components/wear_levelling/WL_Flash.cpp @@ -12,11 +12,13 @@ // limitations under the License. #include +#include "esp_system.h" #include "esp_log.h" #include "WL_Flash.h" #include #include "crc32.h" #include +#include static const char *TAG = "wl_flash"; #ifndef WL_CFG_CRC_CONST @@ -143,7 +145,7 @@ esp_err_t WL_Flash::init() this->state.pos, this->state.move_count); - ESP_LOGD(TAG, "%s starts: crc1=%i, crc2 = %i, this->state.crc=%i, state_copy->crc=%i, version=%i, read_version=%i", __func__, crc1, crc2, this->state.crc, state_copy->crc, this->cfg.version, this->state.version); + ESP_LOGD(TAG, "%s starts: crc1= 0x%08x, crc2 = 0x%08x, this->state.crc= 0x%08x, state_copy->crc= 0x%08x, version=%i, read_version=%i", __func__, crc1, crc2, this->state.crc, state_copy->crc, this->cfg.version, this->state.version); if ((crc1 == this->state.crc) && (crc2 == state_copy->crc)) { // The state is OK. Check the ID if (this->state.version != this->cfg.version) { @@ -158,7 +160,7 @@ esp_err_t WL_Flash::init() result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t)); WL_RESULT_CHECK(result); for (size_t i = 0; i < ((this->cfg.full_mem_size / this->cfg.sector_size)*this->cfg.wr_size); i++) { - bool pos_bits = 0; + bool pos_bits; result = this->flash_drv->read(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size); WL_RESULT_CHECK(result); pos_bits = this->OkBuffSet(i); @@ -169,14 +171,16 @@ esp_err_t WL_Flash::init() } } } - ESP_LOGD(TAG, "%s: crc1=%i, crc2 = %i, result=%i", __func__, crc1, crc2, result); + ESP_LOGD(TAG, "%s: crc1=0x%08x, crc2 = 0x%08x, result= 0x%08x", __func__, crc1, crc2, (uint32_t)result); result = this->recoverPos(); WL_RESULT_CHECK(result); } } else if ((crc1 != this->state.crc) && (crc2 != state_copy->crc)) { // This is just new flash or new version // Check if this is new version or just new instance of WL + ESP_LOGD(TAG, "%s: try to update version - crc1= 0x%08x, crc2 = 0x%08x, result= 0x%08x", __func__, (uint32_t)crc1, (uint32_t)crc2, (uint32_t)result); result = this->updateVersion(); if (result == ESP_FAIL) { + ESP_LOGD(TAG, "%s: init flash sections", __func__); result = this->initSections(); WL_RESULT_CHECK(result); } @@ -190,7 +194,7 @@ esp_err_t WL_Flash::init() result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t)); WL_RESULT_CHECK(result); for (size_t i = 0; i < ((this->cfg.full_mem_size / this->cfg.sector_size) * this->cfg.wr_size); i++) { - bool pos_bits = 0; + bool pos_bits; result = this->flash_drv->read(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size); WL_RESULT_CHECK(result); pos_bits = this->OkBuffSet(i); @@ -207,7 +211,7 @@ esp_err_t WL_Flash::init() result = this->flash_drv->write(this->addr_state1, state_copy, sizeof(wl_state_t)); WL_RESULT_CHECK(result); for (size_t i = 0; i < ((this->cfg.full_mem_size / this->cfg.sector_size) * this->cfg.wr_size); i++) { - bool pos_bits = 0; + bool pos_bits; result = this->flash_drv->read(this->addr_state2 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size); WL_RESULT_CHECK(result); @@ -230,11 +234,11 @@ esp_err_t WL_Flash::init() } if (result != ESP_OK) { this->initialized = false; - ESP_LOGE(TAG, "%s: returned 0x%x", __func__, (uint32_t)result); + ESP_LOGE(TAG, "%s: returned 0x%08x", __func__, (uint32_t)result); return result; } this->initialized = true; - ESP_LOGD(TAG, "%s - move_count=%08x", __func__, (uint32_t)this->state.move_count); + ESP_LOGD(TAG, "%s - move_count= 0x%08x", __func__, (uint32_t)this->state.move_count); return ESP_OK; } @@ -244,12 +248,12 @@ esp_err_t WL_Flash::recoverPos() size_t position = 0; ESP_LOGV(TAG, "%s start", __func__); for (size_t i = 0; i < this->state.max_pos; i++) { - bool pos_bits = false; + bool pos_bits; position = i; result = this->flash_drv->read(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size); pos_bits = this->OkBuffSet(i); WL_RESULT_CHECK(result); - ESP_LOGV(TAG, "%s - check pos: result=%i, position=%i, pos_bits=0x%08x", __func__, (uint32_t)result, (uint32_t)position, (uint32_t)pos_bits); + ESP_LOGV(TAG, "%s - check pos: result=0x%08x, position= %i, pos_bits= 0x%08x", __func__, (uint32_t)result, (uint32_t)position, (uint32_t)pos_bits); if (pos_bits == false) { break; // we have found position } @@ -259,8 +263,8 @@ esp_err_t WL_Flash::recoverPos() if (this->state.pos == this->state.max_pos) { this->state.pos--; } - ESP_LOGD(TAG, "%s - this->state.pos=0x%08x, position=0x%08x, result=%08x, max_pos=%08x", __func__, (uint32_t)this->state.pos, (uint32_t)position, (uint32_t)result, (uint32_t)this->state.max_pos); - ESP_LOGV(TAG, "%s done", __func__); + ESP_LOGD(TAG, "%s - this->state.pos= 0x%08x, position= 0x%08x, result= 0x%08x, max_pos= 0x%08x", __func__, (uint32_t)this->state.pos, (uint32_t)position, (uint32_t)result, (uint32_t)this->state.max_pos); + ESP_LOGV(TAG, "%s done", __func__); return result; } @@ -277,8 +281,8 @@ esp_err_t WL_Flash::initSections() } this->state.version = this->cfg.version; this->state.block_size = this->cfg.page_size; - this->used_bits = 0; - this->state.device_id = rand(); + this->state.device_id = esp_random(); + memset(this->state.reserved, 0, sizeof(this->state.reserved)); this->state.max_pos = 1 + this->flash_size / this->cfg.page_size; @@ -299,8 +303,8 @@ esp_err_t WL_Flash::initSections() result = this->flash_drv->write(this->addr_cfg, &this->cfg, sizeof(wl_config_t)); WL_RESULT_CHECK(result); - ESP_LOGD(TAG, "%s - this->state->max_count=%08x, this->state->max_pos=%08x", __func__, this->state.max_count, this->state.max_pos); - ESP_LOGD(TAG, "%s - result=%08x", __func__, result); + ESP_LOGD(TAG, "%s - this->state->max_count= 0x%08x, this->state->max_pos= 0x%08x", __func__, this->state.max_count, this->state.max_pos); + ESP_LOGD(TAG, "%s - result= 0x%08x", __func__, result); return result; } @@ -309,7 +313,7 @@ esp_err_t WL_Flash::updateVersion() esp_err_t result = ESP_OK; result = this->updateV1_V2(); - if (ESP_OK == result) return result; + if (result == ESP_OK) return result; // check next version return result; } @@ -319,7 +323,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, crc) - sizeof(uint32_t); + 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); // For V1 crc in place of device_id and version @@ -329,14 +333,14 @@ esp_err_t WL_Flash::updateV1_V2() if ((crc1 == v1_crc) && (this->state.version == 1)){ // Here we have to update all internal structures - ESP_LOGV(TAG, "%s Update from V1 to V2", __func__); + ESP_LOGI(TAG, "%s Update from V1 to V2", __func__); uint32_t pos = 0; for (size_t i = 0; i < this->state.max_pos; i++) { - uint8_t pos_bits = 0; + uint8_t pos_bits; result = this->flash_drv->read(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, &pos_bits, 1); WL_RESULT_CHECK(result); - ESP_LOGV(TAG, "%s- result=%i, pos=%i, pos_bits=0x%08x", __func__, (uint32_t)result, (uint32_t)pos, (uint32_t)pos_bits); + ESP_LOGV(TAG, "%s- result= 0x%08x, pos= %i, pos_bits= 0x%08x", __func__, (uint32_t)result, (uint32_t)pos, (uint32_t)pos_bits); pos = i; if (pos_bits == 0xff) { break; // we have found position @@ -350,7 +354,7 @@ 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 = rand(); + this->state.device_id = esp_random(); result = this->flash_drv->erase_range(this->addr_state1, this->state_size); WL_RESULT_CHECK(result); @@ -358,7 +362,7 @@ esp_err_t WL_Flash::updateV1_V2() WL_RESULT_CHECK(result); memset(this->temp_buff, 0, this->cfg.wr_size); - for (int i=0 ; i< pos; i++) { + for (uint32_t i=0 ; i<= pos; i++) { this->fillOkBuff(i); result = this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size); WL_RESULT_CHECK(result); @@ -368,10 +372,10 @@ esp_err_t WL_Flash::updateV1_V2() WL_RESULT_CHECK(result); result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t)); WL_RESULT_CHECK(result); - ESP_LOGD(TAG, "%s - move_count=%08x, pos=%08x, ", __func__, this->state.move_count, this->state.pos); + ESP_LOGD(TAG, "%s - move_count= 0x%08x, pos= 0x%08x", __func__, this->state.move_count, this->state.pos); memset(this->temp_buff, 0, this->cfg.wr_size); - for (int i=0 ; i< pos; i++) { + for (uint32_t i=0 ; i<= pos; i++) { this->fillOkBuff(i); result = this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size); WL_RESULT_CHECK(result); @@ -415,7 +419,7 @@ esp_err_t WL_Flash::updateWL() } // Here we have to move the block and increase the state this->state.access_count = 0; - ESP_LOGV(TAG, "%s - access_count=0x%08x, pos=0x%08x", __func__, this->state.access_count, this->state.pos); + ESP_LOGV(TAG, "%s - access_count= 0x%08x, pos= 0x%08x", __func__, this->state.access_count, this->state.pos); // copy data to dummy block size_t data_addr = this->state.pos + 1; // next block, [pos+1] copy to [pos] if (data_addr >= this->state.max_pos) { @@ -425,7 +429,7 @@ esp_err_t WL_Flash::updateWL() this->dummy_addr = this->cfg.start_addr + this->state.pos * this->cfg.page_size; result = this->flash_drv->erase_range(this->dummy_addr, this->cfg.page_size); if (result != ESP_OK) { - ESP_LOGE(TAG, "%s - erase wl dummy sector result=%08x", __func__, result); + ESP_LOGE(TAG, "%s - erase wl dummy sector result= 0x%08x", __func__, result); this->state.access_count = this->state.max_count - 1; // we will update next time return result; } @@ -434,13 +438,13 @@ esp_err_t WL_Flash::updateWL() for (size_t i = 0; i < copy_count; i++) { result = this->flash_drv->read(data_addr + i * this->cfg.temp_buff_size, this->temp_buff, this->cfg.temp_buff_size); if (result != ESP_OK) { - ESP_LOGE(TAG, "%s - not possible to read buffer, will try next time, result=%08x", __func__, result); + ESP_LOGE(TAG, "%s - not possible to read buffer, will try next time, result= 0x%08x", __func__, result); this->state.access_count = this->state.max_count - 1; // we will update next time return result; } result = this->flash_drv->write(this->dummy_addr + i * this->cfg.temp_buff_size, this->temp_buff, this->cfg.temp_buff_size); if (result != ESP_OK) { - ESP_LOGE(TAG, "%s - not possible to write buffer, will try next time, result=%08x", __func__, result); + ESP_LOGE(TAG, "%s - not possible to write buffer, will try next time, result= 0x%08x", __func__, result); this->state.access_count = this->state.max_count - 1; // we will update next time return result; } @@ -453,14 +457,14 @@ esp_err_t WL_Flash::updateWL() // write state to mem. We updating only affected bits result |= this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + byte_pos, this->temp_buff, this->cfg.wr_size); if (result != ESP_OK) { - ESP_LOGE(TAG, "%s - update position 1 result=%08x", __func__, result); + ESP_LOGE(TAG, "%s - update position 1 result= 0x%08x", __func__, result); this->state.access_count = this->state.max_count - 1; // we will update next time return result; } this->fillOkBuff(this->state.pos); result |= this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + byte_pos, this->temp_buff, this->cfg.wr_size); if (result != ESP_OK) { - ESP_LOGE(TAG, "%s - update position 2 result=%08x", __func__, result); + ESP_LOGE(TAG, "%s - update position 2 result= 0x%08x", __func__, result); this->state.access_count = this->state.max_count - 1; // we will update next time return result; } @@ -484,13 +488,13 @@ esp_err_t WL_Flash::updateWL() WL_RESULT_CHECK(result); result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t)); WL_RESULT_CHECK(result); - ESP_LOGD(TAG, "%s - move_count=%08x, pos=%08x, ", __func__, this->state.move_count, this->state.pos); + ESP_LOGD(TAG, "%s - move_count= 0x%08x, pos= 0x%08x, ", __func__, this->state.move_count, this->state.pos); } // Save structures to the flash... and check result if (result == ESP_OK) { - ESP_LOGV(TAG, "%s - result=%08x", __func__, result); + ESP_LOGV(TAG, "%s - result= 0x%08x", __func__, result); } else { - ESP_LOGE(TAG, "%s - result=%08x", __func__, result); + ESP_LOGE(TAG, "%s - result= 0x%08x", __func__, result); } return result; } @@ -503,7 +507,7 @@ size_t WL_Flash::calcAddr(size_t addr) } else { result += this->cfg.page_size; } - ESP_LOGV(TAG, "%s - addr=0x%08x -> result=0x%08x, dummy_addr=0x%08x", __func__, (uint32_t) addr, (uint32_t) result, (uint32_t)dummy_addr); + ESP_LOGV(TAG, "%s - addr= 0x%08x -> result= 0x%08x, dummy_addr= 0x%08x", __func__, (uint32_t) addr, (uint32_t) result, (uint32_t)dummy_addr); return result; } @@ -530,7 +534,7 @@ esp_err_t WL_Flash::erase_sector(size_t sector) if (!this->initialized) { return ESP_ERR_INVALID_STATE; } - ESP_LOGD(TAG, "%s - sector=0x%08x", __func__, (uint32_t) sector); + ESP_LOGD(TAG, "%s - sector= 0x%08x", __func__, (uint32_t) sector); result = this->updateWL(); WL_RESULT_CHECK(result); size_t virt_addr = this->calcAddr(sector * this->cfg.sector_size); @@ -544,14 +548,14 @@ esp_err_t WL_Flash::erase_range(size_t start_address, size_t size) if (!this->initialized) { return ESP_ERR_INVALID_STATE; } - ESP_LOGD(TAG, "%s - start_address=0x%08x, size=0x%08x", __func__, (uint32_t) start_address, (uint32_t) size); + ESP_LOGD(TAG, "%s - start_address= 0x%08x, size= 0x%08x", __func__, (uint32_t) start_address, (uint32_t) size); size_t erase_count = (size + this->cfg.sector_size - 1) / this->cfg.sector_size; size_t start_sector = start_address / this->cfg.sector_size; for (size_t i = 0; i < erase_count; i++) { result = this->erase_sector(start_sector + i); WL_RESULT_CHECK(result); } - ESP_LOGV(TAG, "%s - result=%08x", __func__, result); + ESP_LOGV(TAG, "%s - result= 0x%08x", __func__, result); return result; } @@ -561,7 +565,7 @@ esp_err_t WL_Flash::write(size_t dest_addr, const void *src, size_t size) if (!this->initialized) { return ESP_ERR_INVALID_STATE; } - ESP_LOGD(TAG, "%s - dest_addr=0x%08x, size=0x%08x", __func__, (uint32_t) dest_addr, (uint32_t) size); + ESP_LOGD(TAG, "%s - dest_addr= 0x%08x, size= 0x%08x", __func__, (uint32_t) dest_addr, (uint32_t) size); uint32_t count = (size - 1) / this->cfg.page_size; for (size_t i = 0; i < count; i++) { size_t virt_addr = this->calcAddr(dest_addr + i * this->cfg.page_size); @@ -580,11 +584,11 @@ esp_err_t WL_Flash::read(size_t src_addr, void *dest, size_t size) if (!this->initialized) { return ESP_ERR_INVALID_STATE; } - ESP_LOGD(TAG, "%s - src_addr=0x%08x, size=0x%08x", __func__, (uint32_t) src_addr, (uint32_t) size); + ESP_LOGD(TAG, "%s - src_addr= 0x%08x, size= 0x%08x", __func__, (uint32_t) src_addr, (uint32_t) size); uint32_t count = (size - 1) / this->cfg.page_size; for (size_t i = 0; i < count; i++) { size_t virt_addr = this->calcAddr(src_addr + i * this->cfg.page_size); - ESP_LOGV(TAG, "%s - real_addr=0x%08x, size=0x%08x", __func__, (uint32_t) this->cfg.start_addr + virt_addr, (uint32_t) size); + ESP_LOGV(TAG, "%s - real_addr= 0x%08x, size= 0x%08x", __func__, (uint32_t) (this->cfg.start_addr + virt_addr), (uint32_t) size); result = this->flash_drv->read(this->cfg.start_addr + virt_addr, &((uint8_t *)dest)[i * this->cfg.page_size], this->cfg.page_size); WL_RESULT_CHECK(result); } @@ -608,6 +612,6 @@ esp_err_t WL_Flash::flush() esp_err_t result = ESP_OK; this->state.access_count = this->state.max_count - 1; result = this->updateWL(); - ESP_LOGD(TAG, "%s - result=%08x, move_count=%08x", __func__, result, this->state.move_count); + ESP_LOGD(TAG, "%s - result= 0x%08x, move_count= 0x%08x", __func__, result, this->state.move_count); return result; } diff --git a/components/wear_levelling/doc/wl_sw_structure.rst b/components/wear_levelling/doc/wl_sw_structure.rst index d09b86b62e..9492140d26 100644 --- a/components/wear_levelling/doc/wl_sw_structure.rst +++ b/components/wear_levelling/doc/wl_sw_structure.rst @@ -5,6 +5,11 @@ Wear Levelling Component (WLC) it is a software component that is implemented to The WLC do not have internal cache. When write operation is finished, that means that data was really stored to the flash. As a parameter the WLC requires the driver to access the flash device. The driver has to implement Flash_Access interface. +The WLC Versioning and Compatibility +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +The WLC accept data formats from older version. Latest version of the WLC will update data format from older versions to the current one. +Current implementation of WLC has version 2. The data format from current version incompatible with data format from previous versions, and could not be +used with previous versions. The WLC Files ^^^^^^^^^^^^^^^ @@ -49,9 +54,9 @@ The wl_config_t contains configuration parameters for the WLC component. Internal Memory Organization ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The WLC divide the memory that are define by start_addr and full_mem_size to three regions: - - Configuration - Data - States + - Configuration The Configuration region used to store configuration information. The user can use it to recover the WLC from memory dump. The Data - is a region where user data stored. diff --git a/components/wear_levelling/private_include/WL_Flash.h b/components/wear_levelling/private_include/WL_Flash.h index fca72de24b..19a682e064 100644 --- a/components/wear_levelling/private_include/WL_Flash.h +++ b/components/wear_levelling/private_include/WL_Flash.h @@ -65,7 +65,6 @@ protected: uint32_t cfg_size; uint8_t *temp_buff = NULL; size_t dummy_addr; - uint8_t used_bits; uint32_t pos_data[4]; esp_err_t initSections(); diff --git a/components/wear_levelling/private_include/WL_State.h b/components/wear_levelling/private_include/WL_State.h index 7d232d51ef..a59de6d7ab 100644 --- a/components/wear_levelling/private_include/WL_State.h +++ b/components/wear_levelling/private_include/WL_State.h @@ -37,6 +37,7 @@ public: uint32_t block_size; /*!< size of move block*/ uint32_t version; /*!< state id used to identify the version of current libary implementaion*/ uint32_t device_id; /*!< ID of current WL instance*/ + uint32_t reserved[7]; /*!< Reserved space for future use*/ uint32_t crc; /*!< CRC of structure*/ } wl_state_t; diff --git a/components/wear_levelling/test/component.mk b/components/wear_levelling/test/component.mk index ce464a212a..47bab96485 100644 --- a/components/wear_levelling/test/component.mk +++ b/components/wear_levelling/test/component.mk @@ -1 +1,2 @@ COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive +COMPONENT_EMBED_FILES := test_partition_v1.bin diff --git a/components/wear_levelling/test/test_partition_v1.bin b/components/wear_levelling/test/test_partition_v1.bin new file mode 100644 index 0000000000000000000000000000000000000000..c4b2437d72b4b886fb502b9db011e3fbda61ac1b GIT binary patch literal 32768 zcmeI%L(pW47NF63W!tuG+qQX&w`|+CZQFL;vTxb8?Ow0D=RN7@=)r$7);WKt?i=nJi=_8`;T0PI8f(Jme)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og2Q`7B@| zi&)GOma>fHtY9UpSj`&NvX1p^U?ZE@%oet?jqU7UC%f3q9`>@2{T$#Rhd9g;{^2Oc zIL--9a*ETO;VkDk&jl{>FPFH?6|QoP>)hZbx46w6?sAX&Jm4XZc+3-?@{H%a;3cnk z%^TkGj`w`vBcJ%p7rye1@BH8=zxd4`0%bA(1SSYU2}W>25Ry=YCJbQ-M|dI-kw`=) z3Q>thbYc*bSi~j{afwHK5|EHYBqj+-Nk(!~kdjoSCJkvxM|v`lkxXPJ3t7oVc5;xD zT;wJXdC5n93Q&+j6s8D8DMoQhP?A!VrVM2%M|mnxkxEpi3RS5_b!t$PTGXZvb*V>v z8qknN{6%A$(3EC0rv-o0l2){)4Q**hdpgjOPIRUVUFk-5deDAZhTiM2TcCeFO>}C&p*~fkkaF9bB<_Q0Alw%y{1SdJgY0hw# zbDZY_7x|YW_xyE&FaFbiy<_>qc$9*2~kVib`2~T;(b6)V0SG?v8Z+XXiKJbxG zeC7*Z`NnsC@RMKs<`01!!Tx*t-$%s%UY-8qaxU`H!-3A6In%xRnw?dv{V1T~s{hmT zKLJNTfq()51p*2L6bL8~P#~Z{K!Jb)0R;jI1QZA;@c&(b|E~K1`~UxUKTE(D3Mddz zAfP}%fq()51p*2L6bL8~P#~Z{K!Jb)|5pqAcmEI6pa0(Y=YL=R=l%bLUV8m;MgKtS ze-uc$|9|~U@PGBg1Kup4KtO?j0s#dA3Ir4gC=gH}pg=%@fC2#p0ty5a_address, test_partition->size); + esp_partition_erase_range(test_partition, 0, test_partition->size); // test small partition: result should be error for (int i=0 ; i< 5 ; i++) { @@ -40,7 +40,6 @@ TEST_CASE("wl_mount check partition parameters", "[wear_levelling][ignore]") TEST_ESP_ERR(ESP_ERR_INVALID_ARG, wl_mount(&fake_partition, &handle)); size_after = xPortGetFreeHeapSize(); TEST_ASSERT_EQUAL_HEX32(size_before, size_after); - printf("Test for size 0x%08x passed\n", fake_partition.size); // currently this test leaks memory } @@ -236,3 +235,54 @@ TEST_CASE("multiple write is correct", "[wear_levelling]") free(buff); wl_unmount(handle); } + +extern const uint8_t test_partition_v1_bin_start[] asm("_binary_test_partition_v1_bin_start"); +extern const uint8_t test_partition_v1_bin_end[] asm("_binary_test_partition_v1_bin_end"); + +#define COMPARE_START_CONST 0x12340000 + +// We write to partition prepared image with V1 +// Then we convert image to new version and verifying the data + +TEST_CASE("Version update test", "[wear_levelling]") +{ + const esp_partition_t *partition = get_test_data_partition(); + esp_partition_t fake_partition; + memcpy(&fake_partition, partition, sizeof(fake_partition)); + + if (partition->encrypted) + { + printf("Update from V1 to V2 will not work.\n"); + return; + } + fake_partition.size = (size_t)(test_partition_v1_bin_end - test_partition_v1_bin_start); + + printf("Data file size = %i, partition address = 0x%08x, file addr=0x%08x\n", (uint32_t)fake_partition.size, (uint32_t)fake_partition.address, (uint32_t)test_partition_v1_bin_start); + + esp_partition_erase_range(&fake_partition, 0, fake_partition.size); + + esp_partition_write(&fake_partition, 0, test_partition_v1_bin_start, fake_partition.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; + + 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); + } + } + + free(buff); + wl_unmount(handle); +} diff --git a/components/wear_levelling/test_wl_host/Makefile b/components/wear_levelling/test_wl_host/Makefile index eaf5e7b3b7..496529ec7f 100644 --- a/components/wear_levelling/test_wl_host/Makefile +++ b/components/wear_levelling/test_wl_host/Makefile @@ -16,6 +16,7 @@ SPI_FLASH_SIM_LIB := libspi_flash.a include Makefile.files all: test + log/esp_random.c \ ifndef SDKCONFIG SDKCONFIG_DIR := $(dir $(realpath sdkconfig/sdkconfig.h)) diff --git a/components/wear_levelling/test_wl_host/stubs/log/esp_random.c b/components/wear_levelling/test_wl_host/stubs/log/esp_random.c new file mode 100644 index 0000000000..e664c561f0 --- /dev/null +++ b/components/wear_levelling/test_wl_host/stubs/log/esp_random.c @@ -0,0 +1,14 @@ +#include +#include +#include +#include +#include +#include + +#include "esp_system.h" + +uint32_t esp_random(void) +{ + return (uint32_t)rand(); +} + diff --git a/components/wear_levelling/test_wl_host/stubs/log/include/esp_system.h b/components/wear_levelling/test_wl_host/stubs/log/include/esp_system.h new file mode 100644 index 0000000000..74511d5ab9 --- /dev/null +++ b/components/wear_levelling/test_wl_host/stubs/log/include/esp_system.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +uint32_t esp_random(void); + +#ifdef __cplusplus +} +#endif + diff --git a/components/wear_levelling/wear_levelling.cpp b/components/wear_levelling/wear_levelling.cpp index b878ff9190..e5e5ffeb80 100644 --- a/components/wear_levelling/wear_levelling.cpp +++ b/components/wear_levelling/wear_levelling.cpp @@ -174,7 +174,6 @@ esp_err_t wl_unmount(wl_handle_t handle) _lock_acquire(&s_instances_lock); result = check_handle(handle, __func__); if (result == ESP_OK) { - ESP_LOGV(TAG, "deleting handle 0x%08x", handle); // We have to flush state of the component result = s_instances[handle].instance->flush(); // We use placement new in wl_mount, so call destructor directly