WL_Flash.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. #include <stdio.h>
  14. #include "esp_system.h"
  15. #include "esp_log.h"
  16. #include "WL_Flash.h"
  17. #include <stdlib.h>
  18. #include "crc32.h"
  19. #include <string.h>
  20. #include <stddef.h>
  21. static const char *TAG = "wl_flash";
  22. #ifndef WL_CFG_CRC_CONST
  23. #define WL_CFG_CRC_CONST UINT32_MAX
  24. #endif // WL_CFG_CRC_CONST
  25. #define WL_RESULT_CHECK(result) \
  26. if (result != ESP_OK) { \
  27. ESP_LOGE(TAG,"%s(%d): result = 0x%08x", __FUNCTION__, __LINE__, result); \
  28. return (result); \
  29. }
  30. #ifndef _MSC_VER // MSVS has different format for this define
  31. static_assert(sizeof(wl_state_t) % 32 == 0, "wl_state_t structure size must be multiple of flash encryption unit size");
  32. #endif // _MSC_VER
  33. WL_Flash::WL_Flash()
  34. {
  35. }
  36. WL_Flash::~WL_Flash()
  37. {
  38. free(this->temp_buff);
  39. }
  40. esp_err_t WL_Flash::config(wl_config_t *cfg, Flash_Access *flash_drv)
  41. {
  42. ESP_LOGV(TAG, "%s start_addr=0x%08x, full_mem_size=0x%08x, page_size=0x%08x, sector_size=0x%08x, updaterate=0x%08x, wr_size=0x%08x, version=0x%08x, temp_buff_size=0x%08x", __func__,
  43. (uint32_t) cfg->start_addr,
  44. cfg->full_mem_size,
  45. cfg->page_size,
  46. cfg->sector_size,
  47. cfg->updaterate,
  48. cfg->wr_size,
  49. cfg->version,
  50. (uint32_t) cfg->temp_buff_size);
  51. cfg->crc = crc32::crc32_le(WL_CFG_CRC_CONST, (const unsigned char *)cfg, offsetof(wl_config_t, crc));
  52. esp_err_t result = ESP_OK;
  53. memcpy(&this->cfg, cfg, sizeof(wl_config_t));
  54. if (this->cfg.temp_buff_size < this->cfg.wr_size) {
  55. this->cfg.temp_buff_size = this->cfg.wr_size;
  56. }
  57. this->configured = false;
  58. if (cfg == NULL) {
  59. result = ESP_ERR_INVALID_ARG;
  60. }
  61. this->flash_drv = flash_drv;
  62. if (flash_drv == NULL) {
  63. result = ESP_ERR_INVALID_ARG;
  64. }
  65. if ((this->cfg.sector_size % this->cfg.temp_buff_size) != 0) {
  66. result = ESP_ERR_INVALID_ARG;
  67. }
  68. if (this->cfg.page_size < this->cfg.sector_size) {
  69. result = ESP_ERR_INVALID_ARG;
  70. }
  71. WL_RESULT_CHECK(result);
  72. this->state_size = this->cfg.sector_size;
  73. if (this->state_size < (sizeof(wl_state_t) + (this->cfg.full_mem_size / this->cfg.sector_size)*this->cfg.wr_size)) {
  74. this->state_size = ((sizeof(wl_state_t) + (this->cfg.full_mem_size / this->cfg.sector_size) * this->cfg.wr_size) + this->cfg.sector_size - 1) / this->cfg.sector_size;
  75. this->state_size = this->state_size * this->cfg.sector_size;
  76. }
  77. this->cfg_size = (sizeof(wl_config_t) + this->cfg.sector_size - 1) / this->cfg.sector_size;
  78. this->cfg_size = cfg_size * this->cfg.sector_size;
  79. this->addr_cfg = this->cfg.start_addr + this->cfg.full_mem_size - this->cfg_size;
  80. this->addr_state1 = this->cfg.start_addr + this->cfg.full_mem_size - this->state_size * 2 - this->cfg_size; // allocate data at the end of memory
  81. this->addr_state2 = this->cfg.start_addr + this->cfg.full_mem_size - this->state_size * 1 - this->cfg_size; // allocate data at the end of memory
  82. ptrdiff_t flash_sz = ((this->cfg.full_mem_size - this->state_size * 2 - this->cfg_size) / this->cfg.page_size - 1) * this->cfg.page_size; // -1 remove dummy block
  83. this->flash_size = ((this->cfg.full_mem_size - this->state_size * 2 - this->cfg_size) / this->cfg.page_size - 1) * this->cfg.page_size; // -1 remove dummy block
  84. ESP_LOGD(TAG, "%s - config result: state_size=0x%08x, cfg_size=0x%08x, addr_cfg=0x%08x, addr_state1=0x%08x, addr_state2=0x%08x, flash_size=0x%08x", __func__,
  85. (uint32_t) this->state_size,
  86. (uint32_t) this->cfg_size,
  87. (uint32_t) this->addr_cfg,
  88. (uint32_t) this->addr_state1,
  89. (uint32_t) this->addr_state2,
  90. (uint32_t) this->flash_size
  91. );
  92. if (flash_sz <= 0) {
  93. result = ESP_ERR_INVALID_ARG;
  94. }
  95. WL_RESULT_CHECK(result);
  96. this->temp_buff = (uint8_t *)malloc(this->cfg.temp_buff_size);
  97. if (this->temp_buff == NULL) {
  98. result = ESP_ERR_NO_MEM;
  99. }
  100. WL_RESULT_CHECK(result);
  101. this->configured = true;
  102. return ESP_OK;
  103. }
  104. esp_err_t WL_Flash::init()
  105. {
  106. esp_err_t result = ESP_OK;
  107. if (this->configured == false) {
  108. ESP_LOGW(TAG, "WL_Flash: not configured, call config() first");
  109. return ESP_ERR_INVALID_STATE;
  110. }
  111. // If flow will be interrupted by error, then this flag will be false
  112. this->initialized = false;
  113. // Init states if it is first time...
  114. this->flash_drv->read(this->addr_state1, &this->state, sizeof(wl_state_t));
  115. wl_state_t sa_copy;
  116. wl_state_t *state_copy = &sa_copy;
  117. result = this->flash_drv->read(this->addr_state2, state_copy, sizeof(wl_state_t));
  118. WL_RESULT_CHECK(result);
  119. int check_size = WL_STATE_CRC_LEN_V2;
  120. // Chech CRC and recover state
  121. uint32_t crc1 = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, check_size);
  122. uint32_t crc2 = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)state_copy, check_size);
  123. ESP_LOGD(TAG, "%s - config ID=%i, stored ID=%i, access_count=%i, block_size=%i, max_count=%i, pos=%i, move_count=0x%8.8X",
  124. __func__,
  125. this->cfg.version,
  126. this->state.version,
  127. this->state.access_count,
  128. this->state.block_size,
  129. this->state.max_count,
  130. this->state.pos,
  131. this->state.move_count);
  132. 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);
  133. if ((crc1 == this->state.crc) && (crc2 == state_copy->crc)) {
  134. // The state is OK. Check the ID
  135. if (this->state.version != this->cfg.version) {
  136. result = this->initSections();
  137. WL_RESULT_CHECK(result);
  138. result = this->recoverPos();
  139. WL_RESULT_CHECK(result);
  140. } else {
  141. if (crc1 != crc2) {// we did not update second structure.
  142. result = this->flash_drv->erase_range(this->addr_state2, this->state_size);
  143. WL_RESULT_CHECK(result);
  144. result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t));
  145. WL_RESULT_CHECK(result);
  146. for (size_t i = 0; i < ((this->cfg.full_mem_size / this->cfg.sector_size)*this->cfg.wr_size); i++) {
  147. bool pos_bits;
  148. result = this->flash_drv->read(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size);
  149. WL_RESULT_CHECK(result);
  150. pos_bits = this->OkBuffSet(i);
  151. if (pos_bits == true) {
  152. //this->fillOkBuff(i);
  153. result = this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size);
  154. WL_RESULT_CHECK(result);
  155. }
  156. }
  157. }
  158. ESP_LOGD(TAG, "%s: crc1=0x%08x, crc2 = 0x%08x, result= 0x%08x", __func__, crc1, crc2, (uint32_t)result);
  159. result = this->recoverPos();
  160. WL_RESULT_CHECK(result);
  161. }
  162. } else if ((crc1 != this->state.crc) && (crc2 != state_copy->crc)) { // This is just new flash or new version
  163. // Check if this is new version or just new instance of WL
  164. 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);
  165. result = this->updateVersion();
  166. if (result == ESP_FAIL) {
  167. ESP_LOGD(TAG, "%s: init flash sections", __func__);
  168. result = this->initSections();
  169. WL_RESULT_CHECK(result);
  170. }
  171. result = this->recoverPos();
  172. WL_RESULT_CHECK(result);
  173. } else {
  174. // recover broken state
  175. if (crc1 == this->state.crc) {// we have to recover state 2
  176. result = this->flash_drv->erase_range(this->addr_state2, this->state_size);
  177. WL_RESULT_CHECK(result);
  178. result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t));
  179. WL_RESULT_CHECK(result);
  180. for (size_t i = 0; i < ((this->cfg.full_mem_size / this->cfg.sector_size) * this->cfg.wr_size); i++) {
  181. bool pos_bits;
  182. result = this->flash_drv->read(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size);
  183. WL_RESULT_CHECK(result);
  184. pos_bits = this->OkBuffSet(i);
  185. if (pos_bits == true) {
  186. result = this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size);
  187. WL_RESULT_CHECK(result);
  188. }
  189. }
  190. result = this->flash_drv->read(this->addr_state2, &this->state, sizeof(wl_state_t));
  191. WL_RESULT_CHECK(result);
  192. } else { // we have to recover state 1
  193. result = this->flash_drv->erase_range(this->addr_state1, this->state_size);
  194. WL_RESULT_CHECK(result);
  195. result = this->flash_drv->write(this->addr_state1, state_copy, sizeof(wl_state_t));
  196. WL_RESULT_CHECK(result);
  197. for (size_t i = 0; i < ((this->cfg.full_mem_size / this->cfg.sector_size) * this->cfg.wr_size); i++) {
  198. bool pos_bits;
  199. result = this->flash_drv->read(this->addr_state2 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size);
  200. WL_RESULT_CHECK(result);
  201. pos_bits = this->OkBuffSet(i);
  202. if (pos_bits == true) {
  203. result = this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size);
  204. WL_RESULT_CHECK(result);
  205. }
  206. }
  207. result = this->flash_drv->read(this->addr_state1, &this->state, sizeof(wl_state_t));
  208. WL_RESULT_CHECK(result);
  209. this->state.pos = this->state.max_pos - 1;
  210. }
  211. // done. We have recovered the state
  212. // If we have a new configuration, we will overwrite it
  213. if (this->state.version != this->cfg.version) {
  214. result = this->initSections();
  215. WL_RESULT_CHECK(result);
  216. }
  217. }
  218. if (result != ESP_OK) {
  219. this->initialized = false;
  220. ESP_LOGE(TAG, "%s: returned 0x%08x", __func__, (uint32_t)result);
  221. return result;
  222. }
  223. this->initialized = true;
  224. ESP_LOGD(TAG, "%s - move_count= 0x%08x", __func__, (uint32_t)this->state.move_count);
  225. return ESP_OK;
  226. }
  227. esp_err_t WL_Flash::recoverPos()
  228. {
  229. esp_err_t result = ESP_OK;
  230. size_t position = 0;
  231. ESP_LOGV(TAG, "%s start", __func__);
  232. for (size_t i = 0; i < this->state.max_pos; i++) {
  233. bool pos_bits;
  234. position = i;
  235. result = this->flash_drv->read(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size);
  236. pos_bits = this->OkBuffSet(i);
  237. WL_RESULT_CHECK(result);
  238. 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);
  239. if (pos_bits == false) {
  240. break; // we have found position
  241. }
  242. }
  243. this->state.pos = position;
  244. if (this->state.pos == this->state.max_pos) {
  245. this->state.pos--;
  246. }
  247. 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);
  248. ESP_LOGV(TAG, "%s done", __func__);
  249. return result;
  250. }
  251. esp_err_t WL_Flash::initSections()
  252. {
  253. esp_err_t result = ESP_OK;
  254. this->state.pos = 0;
  255. this->state.access_count = 0;
  256. this->state.move_count = 0;
  257. // max count
  258. this->state.max_count = this->flash_size / this->state_size * this->cfg.updaterate;
  259. if (this->cfg.updaterate != 0) {
  260. this->state.max_count = this->cfg.updaterate;
  261. }
  262. this->state.version = this->cfg.version;
  263. this->state.block_size = this->cfg.page_size;
  264. this->state.device_id = esp_random();
  265. memset(this->state.reserved, 0, sizeof(this->state.reserved));
  266. this->state.max_pos = 1 + this->flash_size / this->cfg.page_size;
  267. this->state.crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, WL_STATE_CRC_LEN_V2);
  268. result = this->flash_drv->erase_range(this->addr_state1, this->state_size);
  269. WL_RESULT_CHECK(result);
  270. result = this->flash_drv->write(this->addr_state1, &this->state, sizeof(wl_state_t));
  271. WL_RESULT_CHECK(result);
  272. // write state copy
  273. result = this->flash_drv->erase_range(this->addr_state2, this->state_size);
  274. WL_RESULT_CHECK(result);
  275. result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t));
  276. WL_RESULT_CHECK(result);
  277. result = this->flash_drv->erase_range(this->addr_cfg, this->cfg_size);
  278. WL_RESULT_CHECK(result);
  279. result = this->flash_drv->write(this->addr_cfg, &this->cfg, sizeof(wl_config_t));
  280. WL_RESULT_CHECK(result);
  281. 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);
  282. ESP_LOGD(TAG, "%s - result= 0x%08x", __func__, result);
  283. return result;
  284. }
  285. esp_err_t WL_Flash::updateVersion()
  286. {
  287. esp_err_t result = ESP_OK;
  288. result = this->updateV1_V2();
  289. if (result == ESP_OK) {
  290. return result;
  291. }
  292. // check next version
  293. return result;
  294. }
  295. esp_err_t WL_Flash::updateV1_V2()
  296. {
  297. esp_err_t result = ESP_OK;
  298. // Check crc for old version and old version
  299. ESP_LOGV(TAG, "%s start", __func__);
  300. int check_size = WL_STATE_CRC_LEN_V1;
  301. // Chech CRC and recover state
  302. uint32_t crc1 = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, check_size);
  303. wl_state_t sa_copy;
  304. wl_state_t *state_copy = &sa_copy;
  305. result = this->flash_drv->read(this->addr_state2, state_copy, sizeof(wl_state_t));
  306. WL_RESULT_CHECK(result);
  307. uint32_t crc2 = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)state_copy, check_size);
  308. // For V1 crc in place of device_id and version
  309. uint32_t v1_crc1 = this->state.device_id;
  310. uint32_t v1_crc2 = state_copy->device_id;
  311. ESP_LOGD(TAG, "%s - process crc1=0x%08x, crc2=0x%08x, v1_crc1=0x%08x, v1_crc2=0x%08x, version=%i", __func__, crc1, crc2, v1_crc1, v1_crc2, this->state.version);
  312. if ((crc1 == v1_crc1) && (crc2 == v1_crc2) && (v1_crc1 == v1_crc2) && (this->state.version == 1) && (state_copy->version == 1)) {
  313. // Here we have to update all internal structures
  314. ESP_LOGI(TAG, "%s Update from V1 to V2, crc=0x%08x, ", __func__, crc1);
  315. uint32_t pos = 0;
  316. for (size_t i = 0; i < this->state.max_pos; i++) {
  317. uint8_t pos_bits;
  318. result = this->flash_drv->read(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, &pos_bits, 1);
  319. WL_RESULT_CHECK(result);
  320. ESP_LOGV(TAG, "%s- result= 0x%08x, pos= %i, pos_bits= 0x%08x", __func__, (uint32_t)result, (uint32_t)pos, (uint32_t)pos_bits);
  321. pos = i;
  322. if (pos_bits == 0xff) {
  323. break; // we have found position
  324. }
  325. }
  326. ESP_LOGI(TAG, "%s max_pos=%i, pos=%i, state.ver=%i, state2.ver=%i", __func__, (uint32_t)this->state.max_pos, (uint32_t)pos, (uint32_t)this->state.version, (uint32_t)state_copy->version);
  327. if (pos == this->state.max_pos) {
  328. pos--;
  329. }
  330. WL_RESULT_CHECK(result);
  331. this->state.version = 2;
  332. this->state.pos = 0;
  333. this->state.device_id = esp_random();
  334. memset(this->state.reserved, 0, sizeof(this->state.reserved));
  335. this->state.crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, WL_STATE_CRC_LEN_V2);
  336. result = this->flash_drv->erase_range(this->addr_state1, this->state_size);
  337. WL_RESULT_CHECK(result);
  338. result = this->flash_drv->write(this->addr_state1, &this->state, sizeof(wl_state_t));
  339. WL_RESULT_CHECK(result);
  340. memset(this->temp_buff, 0, this->cfg.wr_size);
  341. for (uint32_t i = 0 ; i <= pos; i++) {
  342. this->fillOkBuff(i);
  343. result = this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size);
  344. WL_RESULT_CHECK(result);
  345. }
  346. result = this->flash_drv->erase_range(this->addr_state2, this->state_size);
  347. WL_RESULT_CHECK(result);
  348. result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t));
  349. WL_RESULT_CHECK(result);
  350. ESP_LOGD(TAG, "%s - move_count= 0x%08x, pos= 0x%08x", __func__, this->state.move_count, this->state.pos);
  351. memset(this->temp_buff, 0, this->cfg.wr_size);
  352. for (uint32_t i = 0 ; i <= pos; i++) {
  353. this->fillOkBuff(i);
  354. result = this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + i * this->cfg.wr_size, this->temp_buff, this->cfg.wr_size);
  355. WL_RESULT_CHECK(result);
  356. }
  357. this->state.pos = pos;
  358. return result;
  359. }
  360. return ESP_FAIL;
  361. }
  362. void WL_Flash::fillOkBuff(int n)
  363. {
  364. uint32_t *buff = (uint32_t *)this->temp_buff;
  365. for (int i = 0 ; i < 4 ; i++) {
  366. buff[i] = this->state.device_id + n * 4 + i;
  367. buff[i] = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&buff[i], sizeof(uint32_t));
  368. }
  369. }
  370. bool WL_Flash::OkBuffSet(int n)
  371. {
  372. bool result = true;
  373. uint32_t *data_buff = (uint32_t *)this->temp_buff;
  374. for (int i = 0 ; i < 4 ; i++) {
  375. uint32_t data = this->state.device_id + n * 4 + i;
  376. uint32_t crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&data, sizeof(uint32_t));
  377. if (crc != data_buff[i]) {
  378. result = false;
  379. }
  380. }
  381. return result;
  382. }
  383. esp_err_t WL_Flash::updateWL()
  384. {
  385. esp_err_t result = ESP_OK;
  386. this->state.access_count++;
  387. if (this->state.access_count < this->state.max_count) {
  388. return result;
  389. }
  390. // Here we have to move the block and increase the state
  391. this->state.access_count = 0;
  392. ESP_LOGV(TAG, "%s - access_count= 0x%08x, pos= 0x%08x", __func__, this->state.access_count, this->state.pos);
  393. // copy data to dummy block
  394. size_t data_addr = this->state.pos + 1; // next block, [pos+1] copy to [pos]
  395. if (data_addr >= this->state.max_pos) {
  396. data_addr = 0;
  397. }
  398. data_addr = this->cfg.start_addr + data_addr * this->cfg.page_size;
  399. this->dummy_addr = this->cfg.start_addr + this->state.pos * this->cfg.page_size;
  400. result = this->flash_drv->erase_range(this->dummy_addr, this->cfg.page_size);
  401. if (result != ESP_OK) {
  402. ESP_LOGE(TAG, "%s - erase wl dummy sector result= 0x%08x", __func__, result);
  403. this->state.access_count = this->state.max_count - 1; // we will update next time
  404. return result;
  405. }
  406. size_t copy_count = this->cfg.page_size / this->cfg.temp_buff_size;
  407. for (size_t i = 0; i < copy_count; i++) {
  408. result = this->flash_drv->read(data_addr + i * this->cfg.temp_buff_size, this->temp_buff, this->cfg.temp_buff_size);
  409. if (result != ESP_OK) {
  410. ESP_LOGE(TAG, "%s - not possible to read buffer, will try next time, result= 0x%08x", __func__, result);
  411. this->state.access_count = this->state.max_count - 1; // we will update next time
  412. return result;
  413. }
  414. result = this->flash_drv->write(this->dummy_addr + i * this->cfg.temp_buff_size, this->temp_buff, this->cfg.temp_buff_size);
  415. if (result != ESP_OK) {
  416. ESP_LOGE(TAG, "%s - not possible to write buffer, will try next time, result= 0x%08x", __func__, result);
  417. this->state.access_count = this->state.max_count - 1; // we will update next time
  418. return result;
  419. }
  420. }
  421. // done... block moved.
  422. // Here we will update structures...
  423. // Update bits and save to flash:
  424. uint32_t byte_pos = this->state.pos * this->cfg.wr_size;
  425. this->fillOkBuff(this->state.pos);
  426. // write state to mem. We updating only affected bits
  427. result |= this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + byte_pos, this->temp_buff, this->cfg.wr_size);
  428. if (result != ESP_OK) {
  429. ESP_LOGE(TAG, "%s - update position 1 result= 0x%08x", __func__, result);
  430. this->state.access_count = this->state.max_count - 1; // we will update next time
  431. return result;
  432. }
  433. this->fillOkBuff(this->state.pos);
  434. result |= this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + byte_pos, this->temp_buff, this->cfg.wr_size);
  435. if (result != ESP_OK) {
  436. ESP_LOGE(TAG, "%s - update position 2 result= 0x%08x", __func__, result);
  437. this->state.access_count = this->state.max_count - 1; // we will update next time
  438. return result;
  439. }
  440. this->state.pos++;
  441. if (this->state.pos >= this->state.max_pos) {
  442. this->state.pos = 0;
  443. // one loop more
  444. this->state.move_count++;
  445. if (this->state.move_count >= (this->state.max_pos - 1)) {
  446. this->state.move_count = 0;
  447. }
  448. // write main state
  449. this->state.crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, WL_STATE_CRC_LEN_V2);
  450. result = this->flash_drv->erase_range(this->addr_state1, this->state_size);
  451. WL_RESULT_CHECK(result);
  452. result = this->flash_drv->write(this->addr_state1, &this->state, sizeof(wl_state_t));
  453. WL_RESULT_CHECK(result);
  454. result = this->flash_drv->erase_range(this->addr_state2, this->state_size);
  455. WL_RESULT_CHECK(result);
  456. result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t));
  457. WL_RESULT_CHECK(result);
  458. ESP_LOGD(TAG, "%s - move_count= 0x%08x, pos= 0x%08x, ", __func__, this->state.move_count, this->state.pos);
  459. }
  460. // Save structures to the flash... and check result
  461. if (result == ESP_OK) {
  462. ESP_LOGV(TAG, "%s - result= 0x%08x", __func__, result);
  463. } else {
  464. ESP_LOGE(TAG, "%s - result= 0x%08x", __func__, result);
  465. }
  466. return result;
  467. }
  468. size_t WL_Flash::calcAddr(size_t addr)
  469. {
  470. size_t result = (this->flash_size - this->state.move_count * this->cfg.page_size + addr) % this->flash_size;
  471. size_t dummy_addr = this->state.pos * this->cfg.page_size;
  472. if (result < dummy_addr) {
  473. } else {
  474. result += this->cfg.page_size;
  475. }
  476. 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);
  477. return result;
  478. }
  479. size_t WL_Flash::chip_size()
  480. {
  481. if (!this->configured) {
  482. return 0;
  483. }
  484. return this->flash_size;
  485. }
  486. size_t WL_Flash::sector_size()
  487. {
  488. if (!this->configured) {
  489. return 0;
  490. }
  491. return this->cfg.sector_size;
  492. }
  493. esp_err_t WL_Flash::erase_sector(size_t sector)
  494. {
  495. esp_err_t result = ESP_OK;
  496. if (!this->initialized) {
  497. return ESP_ERR_INVALID_STATE;
  498. }
  499. ESP_LOGD(TAG, "%s - sector= 0x%08x", __func__, (uint32_t) sector);
  500. result = this->updateWL();
  501. WL_RESULT_CHECK(result);
  502. size_t virt_addr = this->calcAddr(sector * this->cfg.sector_size);
  503. result = this->flash_drv->erase_sector((this->cfg.start_addr + virt_addr) / this->cfg.sector_size);
  504. WL_RESULT_CHECK(result);
  505. return result;
  506. }
  507. esp_err_t WL_Flash::erase_range(size_t start_address, size_t size)
  508. {
  509. esp_err_t result = ESP_OK;
  510. if (!this->initialized) {
  511. return ESP_ERR_INVALID_STATE;
  512. }
  513. ESP_LOGD(TAG, "%s - start_address= 0x%08x, size= 0x%08x", __func__, (uint32_t) start_address, (uint32_t) size);
  514. size_t erase_count = (size + this->cfg.sector_size - 1) / this->cfg.sector_size;
  515. size_t start_sector = start_address / this->cfg.sector_size;
  516. for (size_t i = 0; i < erase_count; i++) {
  517. result = this->erase_sector(start_sector + i);
  518. WL_RESULT_CHECK(result);
  519. }
  520. ESP_LOGV(TAG, "%s - result= 0x%08x", __func__, result);
  521. return result;
  522. }
  523. esp_err_t WL_Flash::write(size_t dest_addr, const void *src, size_t size)
  524. {
  525. esp_err_t result = ESP_OK;
  526. if (!this->initialized) {
  527. return ESP_ERR_INVALID_STATE;
  528. }
  529. ESP_LOGD(TAG, "%s - dest_addr= 0x%08x, size= 0x%08x", __func__, (uint32_t) dest_addr, (uint32_t) size);
  530. uint32_t count = (size - 1) / this->cfg.page_size;
  531. for (size_t i = 0; i < count; i++) {
  532. size_t virt_addr = this->calcAddr(dest_addr + i * this->cfg.page_size);
  533. result = this->flash_drv->write(this->cfg.start_addr + virt_addr, &((uint8_t *)src)[i * this->cfg.page_size], this->cfg.page_size);
  534. WL_RESULT_CHECK(result);
  535. }
  536. size_t virt_addr_last = this->calcAddr(dest_addr + count * this->cfg.page_size);
  537. result = this->flash_drv->write(this->cfg.start_addr + virt_addr_last, &((uint8_t *)src)[count * this->cfg.page_size], size - count * this->cfg.page_size);
  538. WL_RESULT_CHECK(result);
  539. return result;
  540. }
  541. esp_err_t WL_Flash::read(size_t src_addr, void *dest, size_t size)
  542. {
  543. esp_err_t result = ESP_OK;
  544. if (!this->initialized) {
  545. return ESP_ERR_INVALID_STATE;
  546. }
  547. ESP_LOGD(TAG, "%s - src_addr= 0x%08x, size= 0x%08x", __func__, (uint32_t) src_addr, (uint32_t) size);
  548. uint32_t count = (size - 1) / this->cfg.page_size;
  549. for (size_t i = 0; i < count; i++) {
  550. size_t virt_addr = this->calcAddr(src_addr + i * this->cfg.page_size);
  551. ESP_LOGV(TAG, "%s - real_addr= 0x%08x, size= 0x%08x", __func__, (uint32_t) (this->cfg.start_addr + virt_addr), (uint32_t) size);
  552. result = this->flash_drv->read(this->cfg.start_addr + virt_addr, &((uint8_t *)dest)[i * this->cfg.page_size], this->cfg.page_size);
  553. WL_RESULT_CHECK(result);
  554. }
  555. size_t virt_addr_last = this->calcAddr(src_addr + count * this->cfg.page_size);
  556. result = this->flash_drv->read(this->cfg.start_addr + virt_addr_last, &((uint8_t *)dest)[count * this->cfg.page_size], size - count * this->cfg.page_size);
  557. WL_RESULT_CHECK(result);
  558. return result;
  559. }
  560. Flash_Access *WL_Flash::get_drv()
  561. {
  562. return this->flash_drv;
  563. }
  564. wl_config_t *WL_Flash::get_cfg()
  565. {
  566. return &this->cfg;
  567. }
  568. esp_err_t WL_Flash::flush()
  569. {
  570. esp_err_t result = ESP_OK;
  571. this->state.access_count = this->state.max_count - 1;
  572. result = this->updateWL();
  573. ESP_LOGD(TAG, "%s - result= 0x%08x, move_count= 0x%08x", __func__, result, this->state.move_count);
  574. return result;
  575. }