From 7b287c25b159e40e2476c27c40337b75d67d9ece Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 6 Mar 2018 10:26:21 +0300 Subject: [PATCH 1/8] Bugfix for write size. The write size for encryption have to be rounded to 16 bytes. A wl_config structure size now ounded to 16. Flash Emulator updated to work with defined minimum size. Tests are updated. --- components/wear_levelling/WL_Flash.cpp | 4 ++-- .../wear_levelling/private_include/WL_Config.h | 9 ++++++++- .../test_wl_host/Flash_Emulator.cpp | 18 +++++++++++++++++- .../test_wl_host/Flash_Emulator.h | 3 ++- .../test_wl_host/wl_tests_host.cpp | 4 ++-- components/wear_levelling/wear_levelling.cpp | 11 ++++++----- 6 files changed, 37 insertions(+), 12 deletions(-) diff --git a/components/wear_levelling/WL_Flash.cpp b/components/wear_levelling/WL_Flash.cpp index eb10fe899e..7fbe3572cd 100644 --- a/components/wear_levelling/WL_Flash.cpp +++ b/components/wear_levelling/WL_Flash.cpp @@ -318,13 +318,13 @@ esp_err_t WL_Flash::updateWL() uint32_t byte_pos = this->state.pos * this->cfg.wr_size; this->used_bits = 0; // write state to mem. We updating only affected bits - result |= this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + byte_pos, &this->used_bits, 1); + result |= this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + byte_pos, &this->used_bits, this->cfg.wr_size); if (result != ESP_OK) { ESP_LOGE(TAG, "%s - update position 1 result=%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->addr_state2 + sizeof(wl_state_t) + byte_pos, &this->used_bits, 1); + result |= this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + byte_pos, &this->used_bits, this->cfg.wr_size); if (result != ESP_OK) { ESP_LOGE(TAG, "%s - update position 2 result=%08x", __func__, result); this->state.access_count = this->state.max_count - 1; // we will update next time diff --git a/components/wear_levelling/private_include/WL_Config.h b/components/wear_levelling/private_include/WL_Config.h index 66beb851d8..3726d028bf 100644 --- a/components/wear_levelling/private_include/WL_Config.h +++ b/components/wear_levelling/private_include/WL_Config.h @@ -29,8 +29,15 @@ typedef struct WL_Config_s { uint32_t wr_size; /*!< Minimum amount of bytes per one block at write operation: 1...*/ uint32_t version; /*!< A version of current implementatioon. To erase and reallocate complete memory this ID must be different from id before.*/ size_t temp_buff_size; /*!< Size of temporary allocated buffer to copy from one flash area to another. The best way, if this value will be equal to sector size.*/ + uint32_t reserved[3]; /*!< dummy array to make wl_config_t size compatible with flash encryption (divided by 16)*/ uint32_t crc; /*!< CRC for this config*/ - +public: + WL_Config_s() + { + for (int i=0 ; i< 3 ; i++) this->reserved[i] = 0; + } } wl_config_t; +_Static_assert((sizeof(wl_config_t) % 16) == 0, "Size of wl_config_t structure should be compatible with flash encryption"); + #endif // _WL_Config_H_ \ No newline at end of file diff --git a/components/wear_levelling/test_wl_host/Flash_Emulator.cpp b/components/wear_levelling/test_wl_host/Flash_Emulator.cpp index 71f1bb1b10..f4e888f3da 100644 --- a/components/wear_levelling/test_wl_host/Flash_Emulator.cpp +++ b/components/wear_levelling/test_wl_host/Flash_Emulator.cpp @@ -17,12 +17,16 @@ #include #include #include +#include "esp_log.h" -Flash_Emulator::Flash_Emulator(size_t size, size_t sector_sise) +static const char *TAG = "Flash_Emulator"; + +Flash_Emulator::Flash_Emulator(size_t size, size_t sector_sise, size_t min_size) { this->reset_count = 0x7fffffff; this->size = size; this->sector_sise = sector_sise; + this->min_size = min_size; this->buff = (uint8_t *)malloc(this->size); this->access_count = new uint32_t[this->size / this->sector_sise]; memset(this->access_count, 0, this->size / this->sector_sise * sizeof(uint32_t)); @@ -81,6 +85,18 @@ esp_err_t Flash_Emulator::erase_range(size_t start_address, size_t size) esp_err_t Flash_Emulator::write(size_t dest_addr, const void *src, size_t size) { esp_err_t result = ESP_OK; + if ((size % this->min_size) != 0) + { + result = ESP_ERR_INVALID_SIZE; + ESP_LOGE(TAG, "%s - wrond size, result=%08x, size=%08x", __func__, result, size); + return result; + } + if ((dest_addr % this->min_size) != 0) + { + result = ESP_ERR_INVALID_SIZE; + ESP_LOGE(TAG, "%s - wrong address, result=%08x, address=%08x", __func__, result, dest_addr); + return result; + } if ((this->reset_count != 0x7fffffff) && (this->reset_count != 0)) { this->reset_count--; } diff --git a/components/wear_levelling/test_wl_host/Flash_Emulator.h b/components/wear_levelling/test_wl_host/Flash_Emulator.h index c9ec3c0ad4..02a1be9825 100644 --- a/components/wear_levelling/test_wl_host/Flash_Emulator.h +++ b/components/wear_levelling/test_wl_host/Flash_Emulator.h @@ -25,7 +25,7 @@ class Flash_Emulator : public Flash_Access { public: - Flash_Emulator(size_t size, size_t sector_sise); + Flash_Emulator(size_t size, size_t sector_sise, size_t min_size); virtual size_t chip_size(); @@ -43,6 +43,7 @@ public: public: size_t size; size_t sector_sise; + size_t min_size; uint8_t *buff; uint32_t *access_count; diff --git a/components/wear_levelling/test_wl_host/wl_tests_host.cpp b/components/wear_levelling/test_wl_host/wl_tests_host.cpp index 05068ac754..84bb59fea2 100644 --- a/components/wear_levelling/test_wl_host/wl_tests_host.cpp +++ b/components/wear_levelling/test_wl_host/wl_tests_host.cpp @@ -25,7 +25,7 @@ #define FLASH_PAGE_SIZE FLASH_SECTOR_SIZE*1 #define FLASH_UPDATERATE 3 #define FLASH_TEMP_SIZE FLASH_SECTOR_SIZE -#define FLASH_WR_BLOCK_SIZE 2 +#define FLASH_WR_BLOCK_SIZE 16 static const char *TAG = "wl_test_host"; Flash_Access *s_flash; @@ -47,7 +47,7 @@ TEST_CASE("flash starts with all bytes == 0xff", "[spi_flash_emu]") wl->wr_size = FLASH_WR_BLOCK_SIZE; WL_Flash *wl_flash = new WL_Flash(); - Flash_Emulator *emul = new Flash_Emulator(FLASH_ACCESS_SIZE + FLASH_START_ADDR, FLASH_SECTOR_SIZE); + Flash_Emulator *emul = new Flash_Emulator(FLASH_ACCESS_SIZE + FLASH_START_ADDR, FLASH_SECTOR_SIZE, FLASH_WR_BLOCK_SIZE); CHECK(wl_flash->config(wl, emul) == ESP_OK); test_power_down(wl_flash, emul, TEST_COUNT_MAX); diff --git a/components/wear_levelling/wear_levelling.cpp b/components/wear_levelling/wear_levelling.cpp index 30d36ce209..9b636f8bfe 100644 --- a/components/wear_levelling/wear_levelling.cpp +++ b/components/wear_levelling/wear_levelling.cpp @@ -76,11 +76,6 @@ esp_err_t wl_mount(const esp_partition_t *partition, wl_handle_t *out_handle) break; } } - if (*out_handle == WL_INVALID_HANDLE) { - ESP_LOGE(TAG, "MAX_WL_HANDLES=%d instances already allocated", MAX_WL_HANDLES); - result = ESP_ERR_NO_MEM; - goto out; - } wl_ext_cfg_t cfg; cfg.full_mem_size = partition->size; @@ -94,6 +89,12 @@ esp_err_t wl_mount(const esp_partition_t *partition, wl_handle_t *out_handle) // FAT sector size by default will be 512 cfg.fat_sector_size = CONFIG_WL_SECTOR_SIZE; + if (*out_handle == WL_INVALID_HANDLE) { + ESP_LOGE(TAG, "MAX_WL_HANDLES=%d instances already allocated", MAX_WL_HANDLES); + result = ESP_ERR_NO_MEM; + goto out; + } + // Allocate memory for a Partition object, and then initialize the object // using placement new operator. This way we can recover from out of // memory condition. From 97539334a9ea788f54237a4e71cf30340ee1eacd Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 9 Mar 2018 07:58:01 +0300 Subject: [PATCH 2/8] Remove error from tests. --- .../wear_levelling/test_wl_host/Flash_Emulator.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/components/wear_levelling/test_wl_host/Flash_Emulator.cpp b/components/wear_levelling/test_wl_host/Flash_Emulator.cpp index f4e888f3da..5dc69a8bb4 100644 --- a/components/wear_levelling/test_wl_host/Flash_Emulator.cpp +++ b/components/wear_levelling/test_wl_host/Flash_Emulator.cpp @@ -17,9 +17,6 @@ #include #include #include -#include "esp_log.h" - -static const char *TAG = "Flash_Emulator"; Flash_Emulator::Flash_Emulator(size_t size, size_t sector_sise, size_t min_size) { @@ -85,16 +82,12 @@ esp_err_t Flash_Emulator::erase_range(size_t start_address, size_t size) esp_err_t Flash_Emulator::write(size_t dest_addr, const void *src, size_t size) { esp_err_t result = ESP_OK; - if ((size % this->min_size) != 0) - { + if ((size % this->min_size) != 0) { result = ESP_ERR_INVALID_SIZE; - ESP_LOGE(TAG, "%s - wrond size, result=%08x, size=%08x", __func__, result, size); return result; } - if ((dest_addr % this->min_size) != 0) - { + if ((dest_addr % this->min_size) != 0) { result = ESP_ERR_INVALID_SIZE; - ESP_LOGE(TAG, "%s - wrong address, result=%08x, address=%08x", __func__, result, dest_addr); return result; } if ((this->reset_count != 0x7fffffff) && (this->reset_count != 0)) { From 7f0b00ef9b627ba28c7268be458d2c5704f53c26 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 9 Mar 2018 08:40:24 +0300 Subject: [PATCH 3/8] Compiler dependency included for tests. --- components/wear_levelling/private_include/WL_Config.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/wear_levelling/private_include/WL_Config.h b/components/wear_levelling/private_include/WL_Config.h index 3726d028bf..cec5cca5ad 100644 --- a/components/wear_levelling/private_include/WL_Config.h +++ b/components/wear_levelling/private_include/WL_Config.h @@ -38,6 +38,8 @@ public: } } wl_config_t; -_Static_assert((sizeof(wl_config_t) % 16) == 0, "Size of wl_config_t structure should be compatible with flash encryption"); +#ifndef _MSC_VER // MSVS has different format for this define +static_assert(sizeof(wl_config_t) % 16 == 0, "Size of wl_config_t structure should be compatible with flash encryption"); +#endif // _MSC_VER #endif // _WL_Config_H_ \ No newline at end of file From 816d63211f762b3be5bdd2633ff0154217dc01b1 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 9 Mar 2018 09:17:32 +0300 Subject: [PATCH 4/8] Structure size updated to the 16 bytes * N --- components/wear_levelling/private_include/WL_Config.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/wear_levelling/private_include/WL_Config.h b/components/wear_levelling/private_include/WL_Config.h index cec5cca5ad..203c15a5e1 100644 --- a/components/wear_levelling/private_include/WL_Config.h +++ b/components/wear_levelling/private_include/WL_Config.h @@ -21,7 +21,7 @@ * */ typedef struct WL_Config_s { - size_t start_addr; /*!< start address in the flash*/ + size_t start_addr; /*!< start address in the flash*/ uint32_t full_mem_size; /*!< Amount of memory used to store data in bytes*/ uint32_t page_size; /*!< One page size in bytes. Page could be more then memory block. This parameter must be page_size >= N*block_size.*/ uint32_t sector_size; /*!< size of flash memory sector that will be erased and stored at once (erase)*/ @@ -29,12 +29,12 @@ typedef struct WL_Config_s { uint32_t wr_size; /*!< Minimum amount of bytes per one block at write operation: 1...*/ uint32_t version; /*!< A version of current implementatioon. To erase and reallocate complete memory this ID must be different from id before.*/ size_t temp_buff_size; /*!< Size of temporary allocated buffer to copy from one flash area to another. The best way, if this value will be equal to sector size.*/ - uint32_t reserved[3]; /*!< dummy array to make wl_config_t size compatible with flash encryption (divided by 16)*/ + uint32_t reserved[6]; /*!< dummy array to make wl_config_t size compatible with flash encryption (divided by 16)*/ uint32_t crc; /*!< CRC for this config*/ public: WL_Config_s() { - for (int i=0 ; i< 3 ; i++) this->reserved[i] = 0; + for (int i=0 ; i< 6 ; i++) this->reserved[i] = 0; } } wl_config_t; From 09704fe7caf4b623519f61f50b8e0f2bbe353217 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 12 Mar 2018 07:30:35 +0300 Subject: [PATCH 5/8] Size of structure ware updated. --- components/wear_levelling/private_include/WL_Config.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/wear_levelling/private_include/WL_Config.h b/components/wear_levelling/private_include/WL_Config.h index 203c15a5e1..4fd40b547f 100644 --- a/components/wear_levelling/private_include/WL_Config.h +++ b/components/wear_levelling/private_include/WL_Config.h @@ -29,12 +29,12 @@ typedef struct WL_Config_s { uint32_t wr_size; /*!< Minimum amount of bytes per one block at write operation: 1...*/ uint32_t version; /*!< A version of current implementatioon. To erase and reallocate complete memory this ID must be different from id before.*/ size_t temp_buff_size; /*!< Size of temporary allocated buffer to copy from one flash area to another. The best way, if this value will be equal to sector size.*/ - uint32_t reserved[6]; /*!< dummy array to make wl_config_t size compatible with flash encryption (divided by 16)*/ + uint32_t reserved[5]; /*!< dummy array to make wl_config_t size compatible with flash encryption (divided by 16)*/ uint32_t crc; /*!< CRC for this config*/ public: WL_Config_s() { - for (int i=0 ; i< 6 ; i++) this->reserved[i] = 0; + for (int i=0 ; i< 5 ; i++) this->reserved[i] = 0; } } wl_config_t; From 560753cfb1e890736db24ac51ef561dd122ba65d Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 12 Mar 2018 07:49:50 +0300 Subject: [PATCH 6/8] Size updated. --- components/wear_levelling/private_include/WL_Config.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/wear_levelling/private_include/WL_Config.h b/components/wear_levelling/private_include/WL_Config.h index 4fd40b547f..6c4d6080f4 100644 --- a/components/wear_levelling/private_include/WL_Config.h +++ b/components/wear_levelling/private_include/WL_Config.h @@ -29,12 +29,12 @@ typedef struct WL_Config_s { uint32_t wr_size; /*!< Minimum amount of bytes per one block at write operation: 1...*/ uint32_t version; /*!< A version of current implementatioon. To erase and reallocate complete memory this ID must be different from id before.*/ size_t temp_buff_size; /*!< Size of temporary allocated buffer to copy from one flash area to another. The best way, if this value will be equal to sector size.*/ - uint32_t reserved[5]; /*!< dummy array to make wl_config_t size compatible with flash encryption (divided by 16)*/ + uint32_t reserved[3]; /*!< dummy array to make wl_config_t size compatible with flash encryption (divided by 16)*/ uint32_t crc; /*!< CRC for this config*/ public: WL_Config_s() { - for (int i=0 ; i< 5 ; i++) this->reserved[i] = 0; + for (int i=0 ; i< 3 ; i++) this->reserved[i] = 0; } } wl_config_t; From 743969f333639d1eec75878f207ad6f8ebf168a2 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 12 Mar 2018 08:34:23 +0300 Subject: [PATCH 7/8] Size of structure made latform independent. --- components/wear_levelling/private_include/WL_Config.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/components/wear_levelling/private_include/WL_Config.h b/components/wear_levelling/private_include/WL_Config.h index 6c4d6080f4..62f10eec3c 100644 --- a/components/wear_levelling/private_include/WL_Config.h +++ b/components/wear_levelling/private_include/WL_Config.h @@ -20,6 +20,9 @@ * @brief This class is used as a structure to configure wear levelling module * */ +#ifndef wl_config_t_size +#define wl_config_t_size (int)((48 - (2*sizeof(size_t) + 7*sizeof(uint32_t)) + sizeof(uint32_t) - 1)/sizeof(uint32_t)) +#endif // wl_config_t_size typedef struct WL_Config_s { size_t start_addr; /*!< start address in the flash*/ uint32_t full_mem_size; /*!< Amount of memory used to store data in bytes*/ @@ -29,12 +32,12 @@ typedef struct WL_Config_s { uint32_t wr_size; /*!< Minimum amount of bytes per one block at write operation: 1...*/ uint32_t version; /*!< A version of current implementatioon. To erase and reallocate complete memory this ID must be different from id before.*/ size_t temp_buff_size; /*!< Size of temporary allocated buffer to copy from one flash area to another. The best way, if this value will be equal to sector size.*/ - uint32_t reserved[3]; /*!< dummy array to make wl_config_t size compatible with flash encryption (divided by 16)*/ + uint32_t reserved[wl_config_t_size]; /*!< dummy array to make wl_config_t size compatible with flash encryption (divided by 16)*/ uint32_t crc; /*!< CRC for this config*/ public: WL_Config_s() { - for (int i=0 ; i< 3 ; i++) this->reserved[i] = 0; + for (int i=0 ; i< wl_config_t_size ; i++) this->reserved[i] = 0; } } wl_config_t; From 5cbb1943acf9e4f93a7f8db215210827142e40f6 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 12 Mar 2018 15:34:16 +0300 Subject: [PATCH 8/8] Structure size alligned by compiler. --- .../private_include/WL_Config.h | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/components/wear_levelling/private_include/WL_Config.h b/components/wear_levelling/private_include/WL_Config.h index 62f10eec3c..a1daa32d92 100644 --- a/components/wear_levelling/private_include/WL_Config.h +++ b/components/wear_levelling/private_include/WL_Config.h @@ -20,11 +20,17 @@ * @brief This class is used as a structure to configure wear levelling module * */ -#ifndef wl_config_t_size -#define wl_config_t_size (int)((48 - (2*sizeof(size_t) + 7*sizeof(uint32_t)) + sizeof(uint32_t) - 1)/sizeof(uint32_t)) -#endif // wl_config_t_size -typedef struct WL_Config_s { - size_t start_addr; /*!< start address in the flash*/ + +#if defined(_MSC_VER) +#define ALIGNED_(x) __declspec(align(x)) +#else +#if defined(__GNUC__) +#define ALIGNED_(x) __attribute__ ((aligned(x))) +#endif +#endif + +typedef struct ALIGNED_(16) WL_Config_s { /*!< Size of wl_config_t structure should be divided by 16 for encryption*/ + size_t start_addr; /*!< start address in the flash*/ uint32_t full_mem_size; /*!< Amount of memory used to store data in bytes*/ uint32_t page_size; /*!< One page size in bytes. Page could be more then memory block. This parameter must be page_size >= N*block_size.*/ uint32_t sector_size; /*!< size of flash memory sector that will be erased and stored at once (erase)*/ @@ -32,13 +38,7 @@ typedef struct WL_Config_s { uint32_t wr_size; /*!< Minimum amount of bytes per one block at write operation: 1...*/ uint32_t version; /*!< A version of current implementatioon. To erase and reallocate complete memory this ID must be different from id before.*/ size_t temp_buff_size; /*!< Size of temporary allocated buffer to copy from one flash area to another. The best way, if this value will be equal to sector size.*/ - uint32_t reserved[wl_config_t_size]; /*!< dummy array to make wl_config_t size compatible with flash encryption (divided by 16)*/ uint32_t crc; /*!< CRC for this config*/ -public: - WL_Config_s() - { - for (int i=0 ; i< wl_config_t_size ; i++) this->reserved[i] = 0; - } } wl_config_t; #ifndef _MSC_VER // MSVS has different format for this define