1 // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #include "WL_Ext_Safe.h"
15 #include <stdlib.h>
16 #include "esp_log.h"
17 
18 static const char *TAG = "wl_ext_safe";
19 
20 #define WL_EXT_RESULT_CHECK(result) \
21     if (result != ESP_OK) { \
22         ESP_LOGE(TAG,"%s(%d): result = 0x%08x", __FUNCTION__, __LINE__, result); \
23         return (result); \
24     }
25 
26 #ifndef FLASH_ERASE_VALUE
27 #define FLASH_ERASE_VALUE 0xffffffff
28 #endif // FLASH_ERASE_VALUE
29 
30 
31 #ifndef WL_EXT_SAFE_OK
32 #define WL_EXT_SAFE_OK 0x12345678
33 #endif // WL_EXT_SAFE_OK
34 
35 #ifndef WL_EXT_SAFE_OFFSET
36 #define WL_EXT_SAFE_OFFSET 16
37 #endif // WL_EXT_SAFE_OFFSET
38 
39 
40 struct WL_Ext_Safe_State {
41 public:
42     uint32_t erase_begin;
43     uint32_t local_addr_base;
44     uint32_t local_addr_shift;
45     uint32_t count;
46 };
47 
WL_Ext_Safe()48 WL_Ext_Safe::WL_Ext_Safe(): WL_Ext_Perf()
49 {
50 }
51 
~WL_Ext_Safe()52 WL_Ext_Safe::~WL_Ext_Safe()
53 {
54 }
55 
config(WL_Config_s * cfg,Flash_Access * flash_drv)56 esp_err_t WL_Ext_Safe::config(WL_Config_s *cfg, Flash_Access *flash_drv)
57 {
58     esp_err_t result = ESP_OK;
59 
60     result = WL_Ext_Perf::config(cfg, flash_drv);
61     WL_EXT_RESULT_CHECK(result);
62     this->state_addr = WL_Flash::chip_size() - 2 * WL_Flash::sector_size();
63     this->dump_addr = WL_Flash::chip_size() - 1 * WL_Flash::sector_size();
64     return ESP_OK;
65 }
66 
init()67 esp_err_t WL_Ext_Safe::init()
68 {
69     esp_err_t result = ESP_OK;
70     ESP_LOGV(TAG, "%s", __func__);
71 
72     result = WL_Ext_Perf::init();
73     WL_EXT_RESULT_CHECK(result);
74 
75     result = this->recover();
76     return result;
77 }
78 
chip_size()79 size_t WL_Ext_Safe::chip_size()
80 {
81     ESP_LOGV(TAG, "%s size = %i", __func__, WL_Flash::chip_size() - 2 * this->flash_sector_size);
82     return WL_Flash::chip_size() - 2 * this->flash_sector_size;
83 }
84 
recover()85 esp_err_t WL_Ext_Safe::recover()
86 {
87     esp_err_t result = ESP_OK;
88 
89     WL_Ext_Safe_State state;
90     result = WL_Flash::read(this->state_addr, &state, sizeof(WL_Ext_Safe_State));
91     WL_EXT_RESULT_CHECK(result);
92     ESP_LOGV(TAG, "%s recover, start_addr = 0x%08x, local_addr_base = 0x%08x, local_addr_shift = %i, count=%i", __func__, state.erase_begin, state.local_addr_base, state.local_addr_shift, state.count);
93 
94     // check if we have transaction
95     if (state.erase_begin == WL_EXT_SAFE_OK) {
96 
97         result = this->read(this->dump_addr, this->sector_buffer, this->flash_sector_size);
98         WL_EXT_RESULT_CHECK(result);
99 
100         result = WL_Flash::erase_sector(state.local_addr_base); // erase comlete flash sector
101         WL_EXT_RESULT_CHECK(result);
102 
103         // And write back...
104         for (int i = 0; i < this->size_factor; i++) {
105             if ((i < state.local_addr_shift) || (i >= state.count + state.local_addr_shift)) {
106                 result = this->write(state.local_addr_base * this->flash_sector_size + i * this->fat_sector_size, &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)], this->fat_sector_size);
107                 WL_EXT_RESULT_CHECK(result);
108             }
109         }
110         // clear transaction
111         result = WL_Flash::erase_range(this->state_addr, this->flash_sector_size);
112     }
113     return result;
114 }
115 
erase_sector_fit(uint32_t start_sector,uint32_t count)116 esp_err_t WL_Ext_Safe::erase_sector_fit(uint32_t start_sector, uint32_t count)
117 {
118     esp_err_t result = ESP_OK;
119 
120     uint32_t local_addr_base = start_sector / this->size_factor;
121     uint32_t pre_check_start = start_sector % this->size_factor;
122     ESP_LOGV(TAG, "%s start_sector=0x%08x, count = %i", __func__, start_sector, count);
123     for (int i = 0; i < this->size_factor; i++) {
124         if ((i < pre_check_start) || (i >= count + pre_check_start)) {
125             result = this->read(start_sector / this->size_factor * this->flash_sector_size + i * this->fat_sector_size, &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)], this->fat_sector_size);
126             WL_EXT_RESULT_CHECK(result);
127         }
128     }
129 
130     result = WL_Flash::erase_sector(this->dump_addr / this->flash_sector_size);
131     WL_EXT_RESULT_CHECK(result);
132     result = WL_Flash::write(this->dump_addr, this->sector_buffer, this->flash_sector_size);
133     WL_EXT_RESULT_CHECK(result);
134 
135     WL_Ext_Safe_State state;
136     state.erase_begin = WL_EXT_SAFE_OK;
137     state.local_addr_base = local_addr_base;
138     state.local_addr_shift = pre_check_start;
139     state.count = count;
140 
141     result = WL_Flash::erase_sector(this->state_addr / this->flash_sector_size);
142     WL_EXT_RESULT_CHECK(result);
143     result = WL_Flash::write(this->state_addr + 0, &state, sizeof(WL_Ext_Safe_State));
144     WL_EXT_RESULT_CHECK(result);
145 
146     // Erase
147     result = WL_Flash::erase_sector(local_addr_base); // erase comlete flash sector
148     WL_EXT_RESULT_CHECK(result);
149     // And write back...
150     for (int i = 0; i < this->size_factor; i++) {
151         if ((i < pre_check_start) || (i >= count + pre_check_start)) {
152             result = this->write(local_addr_base * this->flash_sector_size + i * this->fat_sector_size, &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)], this->fat_sector_size);
153             WL_EXT_RESULT_CHECK(result);
154         }
155     }
156 
157     result = WL_Flash::erase_sector(this->state_addr / this->flash_sector_size);
158     WL_EXT_RESULT_CHECK(result);
159 
160     return ESP_OK;
161 }
162