]> granicus.if.org Git - esp-idf/commitdiff
Bugfix for write size. The write size for encryption have to be rounded to 16 bytes.
authorDmitry <Dmitry@espressif.com>
Tue, 6 Mar 2018 07:26:21 +0000 (10:26 +0300)
committerDmitry <Dmitry@espressif.com>
Tue, 6 Mar 2018 07:26:21 +0000 (10:26 +0300)
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
components/wear_levelling/private_include/WL_Config.h
components/wear_levelling/test_wl_host/Flash_Emulator.cpp
components/wear_levelling/test_wl_host/Flash_Emulator.h
components/wear_levelling/test_wl_host/wl_tests_host.cpp
components/wear_levelling/wear_levelling.cpp

index eb10fe899efc2c2b763745829fa4338f82a1162e..7fbe3572cd9735068524b7f18f9a797ded3b5d67 100644 (file)
@@ -318,13 +318,13 @@ esp_err_t WL_Flash::updateWL()
     uint32_t byte_pos = this->state.pos * this->cfg.wr_size;\r
     this->used_bits = 0;\r
     // write state to mem. We updating only affected bits\r
-    result |= this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + byte_pos, &this->used_bits, 1);\r
+    result |= this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + byte_pos, &this->used_bits, this->cfg.wr_size);\r
     if (result != ESP_OK) {\r
         ESP_LOGE(TAG, "%s - update position 1 result=%08x", __func__, result);\r
         this->state.access_count = this->state.max_count - 1; // we will update next time\r
         return result;\r
     }\r
-    result |= this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + byte_pos, &this->used_bits, 1);\r
+    result |= this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + byte_pos, &this->used_bits, this->cfg.wr_size);\r
     if (result != ESP_OK) {\r
         ESP_LOGE(TAG, "%s - update position 2 result=%08x", __func__, result);\r
         this->state.access_count = this->state.max_count - 1; // we will update next time\r
index 66beb851d8b26977de60c242afdf0f0657d11fba..3726d028bfcbbc98f652d51c933162f32d9af1c3 100644 (file)
@@ -29,8 +29,15 @@ typedef struct WL_Config_s {
     uint32_t wr_size;       /*!< Minimum amount of bytes per one block at write operation: 1...*/\r
     uint32_t version;       /*!< A version of current implementatioon. To erase and reallocate complete memory this ID must be different from id before.*/\r
     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.*/\r
+    uint32_t reserved[3];   /*!< dummy array to make wl_config_t size compatible with flash encryption (divided by 16)*/\r
     uint32_t crc;           /*!< CRC for this config*/\r
-\r
+public:\r
+    WL_Config_s()\r
+    {\r
+        for (int i=0 ; i< 3 ; i++) this->reserved[i] = 0;\r
+    }\r
 } wl_config_t;\r
 \r
+_Static_assert((sizeof(wl_config_t) % 16) == 0, "Size of wl_config_t structure should be compatible with flash encryption");\r
+\r
 #endif // _WL_Config_H_
\ No newline at end of file
index 71f1bb1b108d1463eb34198cbfbcf3460846e163..f4e888f3da8c174002d26b05824b797758bb1ef9 100644 (file)
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <string.h>\r
+#include "esp_log.h"\r
 \r
-Flash_Emulator::Flash_Emulator(size_t size, size_t sector_sise)\r
+static const char *TAG = "Flash_Emulator";\r
+\r
+Flash_Emulator::Flash_Emulator(size_t size, size_t sector_sise, size_t min_size)\r
 {\r
     this->reset_count = 0x7fffffff;\r
     this->size = size;\r
     this->sector_sise = sector_sise;\r
+    this->min_size = min_size;\r
     this->buff = (uint8_t *)malloc(this->size);\r
     this->access_count = new uint32_t[this->size / this->sector_sise];\r
     memset(this->access_count, 0, this->size / this->sector_sise * sizeof(uint32_t));\r
@@ -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)\r
 {\r
     esp_err_t result = ESP_OK;\r
+    if ((size % this->min_size) != 0)\r
+    {\r
+        result = ESP_ERR_INVALID_SIZE;\r
+        ESP_LOGE(TAG, "%s - wrond size, result=%08x, size=%08x", __func__, result, size);\r
+        return result;        \r
+    }\r
+    if ((dest_addr % this->min_size) != 0)\r
+    {\r
+        result = ESP_ERR_INVALID_SIZE;\r
+        ESP_LOGE(TAG, "%s - wrong address, result=%08x, address=%08x", __func__, result, dest_addr);\r
+        return result;        \r
+    }\r
     if ((this->reset_count != 0x7fffffff) && (this->reset_count != 0)) {\r
         this->reset_count--;\r
     }\r
index c9ec3c0ad49ac691273dd0bdd53a8515d4d73a3a..02a1be98250c6590b25e403a2edcca70002ccab7 100644 (file)
@@ -25,7 +25,7 @@ class Flash_Emulator : public Flash_Access
 {\r
 \r
 public:\r
-    Flash_Emulator(size_t size, size_t sector_sise);\r
+    Flash_Emulator(size_t size, size_t sector_sise, size_t min_size);\r
 \r
     virtual size_t chip_size();\r
 \r
@@ -43,6 +43,7 @@ public:
 public:\r
     size_t size;\r
     size_t sector_sise;\r
+    size_t min_size;\r
     uint8_t *buff;\r
 \r
     uint32_t *access_count;\r
index 05068ac7545d219058e23635da2f744d070928ba..84bb59fea21636708fcf65db181cc6508b42954a 100644 (file)
@@ -25,7 +25,7 @@
 #define FLASH_PAGE_SIZE         FLASH_SECTOR_SIZE*1\r
 #define FLASH_UPDATERATE        3\r
 #define FLASH_TEMP_SIZE         FLASH_SECTOR_SIZE\r
-#define FLASH_WR_BLOCK_SIZE     2\r
+#define FLASH_WR_BLOCK_SIZE     16\r
 \r
 static const char *TAG = "wl_test_host";\r
 Flash_Access *s_flash;\r
@@ -47,7 +47,7 @@ TEST_CASE("flash starts with all bytes == 0xff", "[spi_flash_emu]")
     wl->wr_size = FLASH_WR_BLOCK_SIZE;\r
 \r
     WL_Flash *wl_flash = new WL_Flash();\r
-    Flash_Emulator *emul = new Flash_Emulator(FLASH_ACCESS_SIZE + FLASH_START_ADDR, FLASH_SECTOR_SIZE);\r
+    Flash_Emulator *emul = new Flash_Emulator(FLASH_ACCESS_SIZE + FLASH_START_ADDR, FLASH_SECTOR_SIZE, FLASH_WR_BLOCK_SIZE);\r
     CHECK(wl_flash->config(wl, emul) == ESP_OK);\r
 \r
     test_power_down(wl_flash, emul, TEST_COUNT_MAX);\r
index 30d36ce2091e553114a5c22d83a4c5f37d9512d5..9b636f8bfe065db32f2fe35da394cfaca096446d 100644 (file)
@@ -76,11 +76,6 @@ esp_err_t wl_mount(const esp_partition_t *partition, wl_handle_t *out_handle)
             break;\r
         }\r
     }\r
-    if (*out_handle == WL_INVALID_HANDLE) {\r
-        ESP_LOGE(TAG, "MAX_WL_HANDLES=%d instances already allocated", MAX_WL_HANDLES);\r
-        result = ESP_ERR_NO_MEM;\r
-        goto out;\r
-    }\r
 \r
     wl_ext_cfg_t cfg;\r
     cfg.full_mem_size = partition->size;\r
@@ -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\r
     cfg.fat_sector_size = CONFIG_WL_SECTOR_SIZE;\r
 \r
+    if (*out_handle == WL_INVALID_HANDLE) {\r
+        ESP_LOGE(TAG, "MAX_WL_HANDLES=%d instances already allocated", MAX_WL_HANDLES);\r
+        result = ESP_ERR_NO_MEM;\r
+        goto out;\r
+    }\r
+\r
     // Allocate memory for a Partition object, and then initialize the object\r
     // using placement new operator. This way we can recover from out of\r
     // memory condition.\r