ef_iap.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * This file is part of the EasyFlash Library.
  3. *
  4. * Copyright (c) 2015-2017, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: IAP(In-Application Programming) operating interface.
  26. * Created on: 2015-01-05
  27. */
  28. #include <easyflash.h>
  29. #ifdef EF_USING_IAP
  30. /* IAP section backup application section start address in flash */
  31. static uint32_t bak_app_start_addr = 0;
  32. /**
  33. * Flash IAP function initialize.
  34. *
  35. * @return result
  36. */
  37. EfErrCode ef_iap_init(void) {
  38. EfErrCode result = EF_NO_ERR;
  39. bak_app_start_addr = EF_START_ADDR ;
  40. #if defined(EF_USING_ENV)
  41. bak_app_start_addr += ENV_AREA_SIZE;
  42. #endif
  43. #if defined(EF_USING_LOG)
  44. bak_app_start_addr += LOG_AREA_SIZE;
  45. #endif
  46. return result;
  47. }
  48. /**
  49. * Erase backup area application data.
  50. *
  51. * @param app_size application size
  52. *
  53. * @return result
  54. */
  55. EfErrCode ef_erase_bak_app(size_t app_size) {
  56. EfErrCode result = EF_NO_ERR;
  57. result = ef_port_erase(ef_get_bak_app_start_addr(), app_size);
  58. switch (result) {
  59. case EF_NO_ERR: {
  60. EF_INFO("Erased backup area application OK.\n");
  61. break;
  62. }
  63. case EF_ERASE_ERR: {
  64. EF_INFO("Warning: Erase backup area application fault!\n");
  65. /* will return when erase fault */
  66. return result;
  67. }
  68. }
  69. return result;
  70. }
  71. /**
  72. * Erase user old application by using specified erase function.
  73. *
  74. * @param user_app_addr application entry address
  75. * @param app_size application size
  76. * @param app_erase user specified application erase function
  77. *
  78. * @return result
  79. */
  80. EfErrCode ef_erase_spec_user_app(uint32_t user_app_addr, size_t app_size,
  81. EfErrCode (*app_erase)(uint32_t addr, size_t size)) {
  82. EfErrCode result = EF_NO_ERR;
  83. result = app_erase(user_app_addr, app_size);
  84. switch (result) {
  85. case EF_NO_ERR: {
  86. EF_INFO("Erased user application OK.\n");
  87. break;
  88. }
  89. case EF_ERASE_ERR: {
  90. EF_INFO("Warning: Erase user application fault!\n");
  91. /* will return when erase fault */
  92. return result;
  93. }
  94. }
  95. return result;
  96. }
  97. /**
  98. * Erase user old application by using default `ef_port_erase` function.
  99. *
  100. * @param user_app_addr application entry address
  101. * @param app_size application size
  102. *
  103. * @return result
  104. */
  105. EfErrCode ef_erase_user_app(uint32_t user_app_addr, size_t app_size) {
  106. return ef_erase_spec_user_app(user_app_addr, app_size, ef_port_erase);
  107. }
  108. /**
  109. * Erase old bootloader
  110. *
  111. * @param bl_addr bootloader entry address
  112. * @param bl_size bootloader size
  113. *
  114. * @return result
  115. */
  116. EfErrCode ef_erase_bl(uint32_t bl_addr, size_t bl_size) {
  117. EfErrCode result = EF_NO_ERR;
  118. result = ef_port_erase(bl_addr, bl_size);
  119. switch (result) {
  120. case EF_NO_ERR: {
  121. EF_INFO("Erased bootloader OK.\n");
  122. break;
  123. }
  124. case EF_ERASE_ERR: {
  125. EF_INFO("Warning: Erase bootloader fault!\n");
  126. /* will return when erase fault */
  127. return result;
  128. }
  129. }
  130. return result;
  131. }
  132. /**
  133. * Write data of application to backup area.
  134. *
  135. * @param data a part of application
  136. * @param size data size
  137. * @param cur_size current write application size
  138. * @param total_size application total size
  139. *
  140. * @return result
  141. */
  142. EfErrCode ef_write_data_to_bak(uint8_t *data, size_t size, size_t *cur_size,
  143. size_t total_size) {
  144. EfErrCode result = EF_NO_ERR;
  145. /* make sure don't write excess data */
  146. if (*cur_size + size > total_size) {
  147. size = total_size - *cur_size;
  148. }
  149. result = ef_port_write(ef_get_bak_app_start_addr() + *cur_size, (uint32_t *) data, size);
  150. switch (result) {
  151. case EF_NO_ERR: {
  152. *cur_size += size;
  153. EF_DEBUG("Write data to backup area OK.\n");
  154. break;
  155. }
  156. case EF_WRITE_ERR: {
  157. EF_INFO("Warning: Write data to backup area fault!\n");
  158. break;
  159. }
  160. }
  161. return result;
  162. }
  163. /**
  164. * Copy backup area application to application entry by using specified write function.
  165. *
  166. * @param user_app_addr application entry address
  167. * @param app_size application size
  168. * @param app_write user specified application write function
  169. *
  170. * @return result
  171. */
  172. EfErrCode ef_copy_spec_app_from_bak(uint32_t user_app_addr, size_t app_size,
  173. EfErrCode (*app_write)(uint32_t addr, const uint32_t *buf, size_t size)) {
  174. size_t cur_size;
  175. uint32_t app_cur_addr, bak_cur_addr;
  176. EfErrCode result = EF_NO_ERR;
  177. /* 32 words size buffer */
  178. uint32_t buff[32];
  179. /* cycle copy data */
  180. for (cur_size = 0; cur_size < app_size; cur_size += sizeof(buff)) {
  181. app_cur_addr = user_app_addr + cur_size;
  182. bak_cur_addr = ef_get_bak_app_start_addr() + cur_size;
  183. ef_port_read(bak_cur_addr, buff, sizeof(buff));
  184. result = app_write(app_cur_addr, buff, sizeof(buff));
  185. if (result != EF_NO_ERR) {
  186. break;
  187. }
  188. }
  189. switch (result) {
  190. case EF_NO_ERR: {
  191. EF_INFO("Write data to application entry OK.\n");
  192. break;
  193. }
  194. case EF_WRITE_ERR: {
  195. EF_INFO("Warning: Write data to application entry fault!\n");
  196. break;
  197. }
  198. }
  199. return result;
  200. }
  201. /**
  202. * Copy backup area application to application entry by using default `ef_port_write` function.
  203. *
  204. * @param user_app_addr application entry address
  205. * @param app_size application size
  206. *
  207. * @return result
  208. */
  209. EfErrCode ef_copy_app_from_bak(uint32_t user_app_addr, size_t app_size) {
  210. return ef_copy_spec_app_from_bak(user_app_addr, app_size, ef_port_write);
  211. }
  212. /**
  213. * Copy backup area bootloader to bootloader entry.
  214. *
  215. * @param bl_addr bootloader entry address
  216. * @param bl_size bootloader size
  217. *
  218. * @return result
  219. */
  220. EfErrCode ef_copy_bl_from_bak(uint32_t bl_addr, size_t bl_size) {
  221. size_t cur_size;
  222. uint32_t bl_cur_addr, bak_cur_addr;
  223. EfErrCode result = EF_NO_ERR;
  224. /* 32 words buffer */
  225. uint32_t buff[32];
  226. /* cycle copy data by 32bytes buffer */
  227. for (cur_size = 0; cur_size < bl_size; cur_size += sizeof(buff)) {
  228. bl_cur_addr = bl_addr + cur_size;
  229. bak_cur_addr = ef_get_bak_app_start_addr() + cur_size;
  230. ef_port_read(bak_cur_addr, buff, sizeof(buff));
  231. result = ef_port_write(bl_cur_addr, buff, sizeof(buff));
  232. if (result != EF_NO_ERR) {
  233. break;
  234. }
  235. }
  236. switch (result) {
  237. case EF_NO_ERR: {
  238. EF_INFO("Write data to bootloader entry OK.\n");
  239. break;
  240. }
  241. case EF_WRITE_ERR: {
  242. EF_INFO("Warning: Write data to bootloader entry fault!\n");
  243. break;
  244. }
  245. }
  246. return result;
  247. }
  248. /**
  249. * Get IAP section start address in flash.
  250. *
  251. * @return size
  252. */
  253. uint32_t ef_get_bak_app_start_addr(void) {
  254. return bak_app_start_addr;
  255. }
  256. #endif /* EF_USING_IAP */