]> granicus.if.org Git - esp-idf/commitdiff
Version update from V1 to V2 now done in correct way.
authorDmitry <Dmitry@espressif.com>
Thu, 25 Oct 2018 12:31:28 +0000 (15:31 +0300)
committerDmitry <Dmitry@espressif.com>
Fri, 26 Oct 2018 06:21:30 +0000 (09:21 +0300)
Before this works only first time.
The source and test updated.
State length was changed to macro.

components/wear_levelling/WL_Flash.cpp
components/wear_levelling/private_include/WL_State.h
components/wear_levelling/test/test_wl.c

index 06d3da5ef7e5e4deceeb49065b3ac82d6190d994..36fe6b131ce11d1592b16b7504084d8d5466d2fd 100644 (file)
@@ -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);
index 7464ba9b8b123e090e4fb5b8901538f816a85e9c..7b6d3f7ed8833d1092d58e643c11c913f36aaa97 100644 (file)
@@ -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_
index 02dba8f78e6e9d3aaa41eaac20c009cdcce02598..0c2bf39dcce133014c6db90743207f6d12c9b35a 100644 (file)
@@ -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);
-
-    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);
+    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);
+
+        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);
             }
-            TEST_ASSERT_EQUAL( buff[i], compare_val);
         }
+        free(buff);
+        wl_unmount(handle);
     }
-
-    free(buff);
-    wl_unmount(handle);
 }