WL_Ext_Safe.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "WL_Ext_Safe.h"
  14. #include <stdlib.h>
  15. #include "esp_log.h"
  16. static const char *TAG = "wl_ext_safe";
  17. #define WL_EXT_RESULT_CHECK(result) \
  18. if (result != ESP_OK) { \
  19. ESP_LOGE(TAG,"%s(%d): result = 0x%08x", __FUNCTION__, __LINE__, result); \
  20. return (result); \
  21. }
  22. #ifndef FLASH_ERASE_VALUE
  23. #define FLASH_ERASE_VALUE 0xffffffff
  24. #endif // FLASH_ERASE_VALUE
  25. #ifndef WL_EXT_SAFE_OK
  26. #define WL_EXT_SAFE_OK 0x12345678
  27. #endif // WL_EXT_SAFE_OK
  28. #ifndef WL_EXT_SAFE_OFFSET
  29. #define WL_EXT_SAFE_OFFSET 16
  30. #endif // WL_EXT_SAFE_OFFSET
  31. struct WL_Ext_Safe_State {
  32. public:
  33. uint32_t erase_begin;
  34. uint32_t local_addr_base;
  35. uint32_t local_addr_shift;
  36. uint32_t count;
  37. };
  38. WL_Ext_Safe::WL_Ext_Safe(): WL_Ext_Perf()
  39. {
  40. }
  41. WL_Ext_Safe::~WL_Ext_Safe()
  42. {
  43. }
  44. esp_err_t WL_Ext_Safe::config(WL_Config_s *cfg, Flash_Access *flash_drv)
  45. {
  46. esp_err_t result = ESP_OK;
  47. result = WL_Ext_Perf::config(cfg, flash_drv);
  48. WL_EXT_RESULT_CHECK(result);
  49. this->state_addr = WL_Flash::chip_size() - 2 * WL_Flash::sector_size();
  50. this->dump_addr = WL_Flash::chip_size() - 1 * WL_Flash::sector_size();
  51. return ESP_OK;
  52. }
  53. esp_err_t WL_Ext_Safe::init()
  54. {
  55. esp_err_t result = ESP_OK;
  56. ESP_LOGV(TAG, "%s", __func__);
  57. result = WL_Ext_Perf::init();
  58. WL_EXT_RESULT_CHECK(result);
  59. result = this->recover();
  60. return result;
  61. }
  62. size_t WL_Ext_Safe::chip_size()
  63. {
  64. ESP_LOGV(TAG, "%s size = %i", __func__, WL_Flash::chip_size() - 2 * this->flash_sector_size);
  65. return WL_Flash::chip_size() - 2 * this->flash_sector_size;
  66. }
  67. esp_err_t WL_Ext_Safe::recover()
  68. {
  69. esp_err_t result = ESP_OK;
  70. WL_Ext_Safe_State state;
  71. result = WL_Flash::read(this->state_addr, &state, sizeof(WL_Ext_Safe_State));
  72. WL_EXT_RESULT_CHECK(result);
  73. 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);
  74. // check if we have transaction
  75. if (state.erase_begin == WL_EXT_SAFE_OK) {
  76. result = this->read(this->dump_addr, this->sector_buffer, this->flash_sector_size);
  77. WL_EXT_RESULT_CHECK(result);
  78. result = WL_Flash::erase_sector(state.local_addr_base); // erase comlete flash sector
  79. WL_EXT_RESULT_CHECK(result);
  80. // And write back...
  81. for (int i = 0; i < this->size_factor; i++) {
  82. if ((i < state.local_addr_shift) || (i >= state.count + state.local_addr_shift)) {
  83. 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);
  84. WL_EXT_RESULT_CHECK(result);
  85. }
  86. }
  87. // clear transaction
  88. result = WL_Flash::erase_range(this->state_addr, this->flash_sector_size);
  89. }
  90. return result;
  91. }
  92. esp_err_t WL_Ext_Safe::erase_sector_fit(uint32_t start_sector, uint32_t count)
  93. {
  94. esp_err_t result = ESP_OK;
  95. uint32_t local_addr_base = start_sector / this->size_factor;
  96. uint32_t pre_check_start = start_sector % this->size_factor;
  97. ESP_LOGV(TAG, "%s start_sector=0x%08x, count = %i", __func__, start_sector, count);
  98. for (int i = 0; i < this->size_factor; i++) {
  99. if ((i < pre_check_start) || (i >= count + pre_check_start)) {
  100. 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);
  101. WL_EXT_RESULT_CHECK(result);
  102. }
  103. }
  104. result = WL_Flash::erase_sector(this->dump_addr / this->flash_sector_size);
  105. WL_EXT_RESULT_CHECK(result);
  106. result = WL_Flash::write(this->dump_addr, this->sector_buffer, this->flash_sector_size);
  107. WL_EXT_RESULT_CHECK(result);
  108. WL_Ext_Safe_State state;
  109. state.erase_begin = WL_EXT_SAFE_OK;
  110. state.local_addr_base = local_addr_base;
  111. state.local_addr_shift = pre_check_start;
  112. state.count = count;
  113. result = WL_Flash::erase_sector(this->state_addr / this->flash_sector_size);
  114. WL_EXT_RESULT_CHECK(result);
  115. result = WL_Flash::write(this->state_addr + 0, &state, sizeof(WL_Ext_Safe_State));
  116. WL_EXT_RESULT_CHECK(result);
  117. // Erase
  118. result = WL_Flash::erase_sector(local_addr_base); // erase comlete flash sector
  119. WL_EXT_RESULT_CHECK(result);
  120. // And write back...
  121. for (int i = 0; i < this->size_factor; i++) {
  122. if ((i < pre_check_start) || (i >= count + pre_check_start)) {
  123. 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);
  124. WL_EXT_RESULT_CHECK(result);
  125. }
  126. }
  127. result = WL_Flash::erase_sector(this->state_addr / this->flash_sector_size);
  128. WL_EXT_RESULT_CHECK(result);
  129. return ESP_OK;
  130. }