WL_Flash.cpp 29 KB

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