ef_log.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * This file is part of the EasyFlash Library.
  3. *
  4. * Copyright (c) 2015-2019, 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: Save logs to flash.
  26. * Created on: 2015-06-04
  27. */
  28. #include <easyflash.h>
  29. #ifdef EF_USING_LOG
  30. #if defined(EF_USING_LOG) && !defined(LOG_AREA_SIZE)
  31. #error "Please configure log area size (in ef_cfg.h)"
  32. #endif
  33. /* magic code on every sector header. 'EF' is 0xEF30EF30 */
  34. #define LOG_SECTOR_MAGIC 0xEF30EF30
  35. /* sector header size, includes the sector magic code and status magic code */
  36. #define LOG_SECTOR_HEADER_SIZE 12
  37. /* sector header word size,what is equivalent to the total number of sectors header index */
  38. #define LOG_SECTOR_HEADER_WORD_SIZE 3
  39. /**
  40. * Sector status magic code
  41. * The sector status is 8B after LOG_SECTOR_MAGIC at every sector header.
  42. * ==============================================
  43. * | header(12B) | status |
  44. * ----------------------------------------------
  45. * | 0xEF30EF30 0xFFFFFFFF 0xFFFFFFFF | empty |
  46. * | 0xEF30EF30 0xFEFEFEFE 0xFFFFFFFF | using |
  47. * | 0xEF30EF30 0xFEFEFEFE 0xFCFCFCFC | full |
  48. * ==============================================
  49. *
  50. * State transition relationship: empty->using->full
  51. * The FULL status will change to EMPTY after sector clean.
  52. */
  53. #define SECTOR_STATUS_MAGIC_EMPUT 0xFFFFFFFF
  54. #define SECTOR_STATUS_MAGIC_USING 0xFEFEFEFE
  55. #define SECTOR_STATUS_MAGIC_FULL 0xFCFCFCFC
  56. typedef enum {
  57. SECTOR_STATUS_EMPUT,
  58. SECTOR_STATUS_USING,
  59. SECTOR_STATUS_FULL,
  60. SECTOR_STATUS_HEADER_ERROR,
  61. } SectorStatus;
  62. typedef enum {
  63. SECTOR_HEADER_MAGIC_INDEX,
  64. SECTOR_HEADER_USING_INDEX,
  65. SECTOR_HEADER_FULL_INDEX,
  66. } SectorHeaderIndex;
  67. /* the stored logs start address and end address. It's like a ring buffer implemented on flash. */
  68. static uint32_t log_start_addr = 0, log_end_addr = 0;
  69. /* saved log area address for flash */
  70. static uint32_t log_area_start_addr = 0;
  71. /* initialize OK flag */
  72. static bool init_ok = false;
  73. static void find_start_and_end_addr(void);
  74. static uint32_t get_next_flash_sec_addr(uint32_t cur_addr);
  75. /**
  76. * The flash save log function initialize.
  77. *
  78. * @return result
  79. */
  80. EfErrCode ef_log_init(void) {
  81. EfErrCode result = EF_NO_ERR;
  82. EF_ASSERT(LOG_AREA_SIZE);
  83. EF_ASSERT(EF_ERASE_MIN_SIZE);
  84. /* the log area size must be an integral multiple of erase minimum size. */
  85. EF_ASSERT(LOG_AREA_SIZE % EF_ERASE_MIN_SIZE == 0);
  86. /* the log area size must be more than twice of EF_ERASE_MIN_SIZE */
  87. EF_ASSERT(LOG_AREA_SIZE / EF_ERASE_MIN_SIZE >= 2);
  88. #ifdef EF_USING_ENV
  89. log_area_start_addr = EF_START_ADDR + ENV_AREA_SIZE;
  90. #else
  91. log_area_start_addr = EF_START_ADDR;
  92. #endif
  93. /* find the log store start address and end address */
  94. find_start_and_end_addr();
  95. /* initialize OK */
  96. init_ok = true;
  97. return result;
  98. }
  99. /**
  100. * Get flash sector current status.
  101. *
  102. * @param addr sector address, this function will auto calculate the sector header address by this address.
  103. *
  104. * @return the flash sector current status
  105. */
  106. static SectorStatus get_sector_status(uint32_t addr) {
  107. uint32_t header_buf[LOG_SECTOR_HEADER_WORD_SIZE] = {0}, header_addr = 0;
  108. uint32_t sector_header_magic = 0;
  109. uint32_t status_full_magic = 0, status_use_magic = 0;
  110. /* calculate the sector header address */
  111. header_addr = addr & (~(EF_ERASE_MIN_SIZE - 1));
  112. if (ef_port_read(header_addr, header_buf, sizeof(header_buf)) == EF_NO_ERR) {
  113. sector_header_magic = header_buf[SECTOR_HEADER_MAGIC_INDEX];
  114. status_use_magic = header_buf[SECTOR_HEADER_USING_INDEX];
  115. status_full_magic = header_buf[SECTOR_HEADER_FULL_INDEX];
  116. } else {
  117. EF_DEBUG("Error: Read sector header data error.\n");
  118. return SECTOR_STATUS_HEADER_ERROR;
  119. }
  120. /* compare header magic code */
  121. if(sector_header_magic == LOG_SECTOR_MAGIC){
  122. if((status_use_magic == SECTOR_STATUS_MAGIC_EMPUT) && (status_full_magic == SECTOR_STATUS_MAGIC_EMPUT)) {
  123. return SECTOR_STATUS_EMPUT;
  124. } else if((status_use_magic == SECTOR_STATUS_MAGIC_USING) && (status_full_magic == SECTOR_STATUS_MAGIC_EMPUT)) {
  125. return SECTOR_STATUS_USING;
  126. } else if((status_use_magic == SECTOR_STATUS_MAGIC_USING) && (status_full_magic == SECTOR_STATUS_MAGIC_FULL)) {
  127. return SECTOR_STATUS_FULL;
  128. } else {
  129. return SECTOR_STATUS_HEADER_ERROR;
  130. }
  131. } else {
  132. return SECTOR_STATUS_HEADER_ERROR;
  133. }
  134. }
  135. /**
  136. * Write flash sector current status.
  137. *
  138. * @param addr sector address, this function will auto calculate the sector header address by this address.
  139. * @param status sector cur status
  140. *
  141. * @return result
  142. */
  143. static EfErrCode write_sector_status(uint32_t addr, SectorStatus status) {
  144. uint32_t header, header_addr = 0;
  145. /* calculate the sector header address */
  146. header_addr = addr & (~(EF_ERASE_MIN_SIZE - 1));
  147. /* calculate the sector staus magic */
  148. switch (status) {
  149. case SECTOR_STATUS_EMPUT: {
  150. header = LOG_SECTOR_MAGIC;
  151. return ef_port_write(header_addr, &header, sizeof(header));
  152. }
  153. case SECTOR_STATUS_USING: {
  154. header = SECTOR_STATUS_MAGIC_USING;
  155. return ef_port_write(header_addr + sizeof(header), &header, sizeof(header));
  156. }
  157. case SECTOR_STATUS_FULL: {
  158. header = SECTOR_STATUS_MAGIC_FULL;
  159. return ef_port_write(header_addr + sizeof(header) * 2, &header, sizeof(header));
  160. }
  161. default:
  162. return EF_WRITE_ERR;
  163. }
  164. }
  165. /**
  166. * Find the current flash sector using end address by continuous 0xFF.
  167. *
  168. * @param addr sector address
  169. *
  170. * @return current flash sector using end address
  171. */
  172. static uint32_t find_sec_using_end_addr(uint32_t addr) {
  173. /* read section data buffer size */
  174. #define READ_BUF_SIZE 32
  175. uint32_t sector_start = addr, data_start = addr, continue_ff = 0, read_buf_size = 0, i;
  176. uint8_t buf[READ_BUF_SIZE];
  177. EF_ASSERT(READ_BUF_SIZE % 4 == 0);
  178. /* calculate the sector start and data start address */
  179. sector_start = addr & (~(EF_ERASE_MIN_SIZE - 1));
  180. data_start = sector_start + LOG_SECTOR_HEADER_SIZE;
  181. /* counts continuous 0xFF which is end of sector */
  182. while (data_start < sector_start + EF_ERASE_MIN_SIZE) {
  183. if (data_start + READ_BUF_SIZE < sector_start + EF_ERASE_MIN_SIZE) {
  184. read_buf_size = READ_BUF_SIZE;
  185. } else {
  186. read_buf_size = sector_start + EF_ERASE_MIN_SIZE - data_start;
  187. }
  188. ef_port_read(data_start, (uint32_t *)buf, read_buf_size);
  189. for (i = 0; i < read_buf_size; i++) {
  190. if (buf[i] == 0xFF) {
  191. continue_ff++;
  192. } else {
  193. continue_ff = 0;
  194. }
  195. }
  196. data_start += read_buf_size;
  197. }
  198. /* calculate current flash sector using end address */
  199. if (continue_ff >= EF_ERASE_MIN_SIZE - LOG_SECTOR_HEADER_SIZE) {
  200. /* from 0 to sec_size all sector is 0xFF, so the sector is empty */
  201. return sector_start + LOG_SECTOR_HEADER_SIZE;
  202. } else if (continue_ff >= 4) {
  203. /* form end_addr - 4 to sec_size length all area is 0xFF, so it's used part of the sector.
  204. * the address must be word alignment. */
  205. if (continue_ff % 4 != 0) {
  206. continue_ff = (continue_ff / 4 + 1) * 4;
  207. }
  208. return sector_start + EF_ERASE_MIN_SIZE - continue_ff;
  209. } else {
  210. /* all sector not has continuous 0xFF, so the sector is full */
  211. return sector_start + EF_ERASE_MIN_SIZE;
  212. }
  213. }
  214. /**
  215. * Find the log store start address and end address.
  216. * It's like a ring buffer implemented on flash.
  217. * The flash log area can be in two states depending on start address and end address:
  218. * state 1 state 2
  219. * |============| |============|
  220. * log area start--> |############| <-- start address |############| <-- end address
  221. * |############| | empty |
  222. * |------------| |------------|
  223. * |############| |############| <-- start address
  224. * |############| |############|
  225. * |------------| |------------|
  226. * | . | | . |
  227. * | . | | . |
  228. * | . | | . |
  229. * |------------| |------------|
  230. * |############| <-- end address |############|
  231. * | empty | |############|
  232. * log area end --> |============| |============|
  233. *
  234. * LOG_AREA_SIZE = log area end - log area star
  235. *
  236. */
  237. static void find_start_and_end_addr(void) {
  238. size_t cur_size = 0;
  239. SectorStatus cur_sec_status, last_sec_status;
  240. uint32_t cur_using_sec_addr = 0;
  241. /* all status sector counts */
  242. size_t empty_sec_counts = 0, using_sec_counts = 0, full_sector_counts = 0;
  243. /* total sector number */
  244. size_t total_sec_num = LOG_AREA_SIZE / EF_ERASE_MIN_SIZE;
  245. /* see comment of find_start_and_end_addr function */
  246. uint8_t cur_log_sec_state = 0;
  247. /* get the first sector status */
  248. cur_sec_status = get_sector_status(log_area_start_addr);
  249. last_sec_status = cur_sec_status;
  250. for (cur_size = EF_ERASE_MIN_SIZE; cur_size < LOG_AREA_SIZE; cur_size += EF_ERASE_MIN_SIZE) {
  251. /* get current sector status */
  252. cur_sec_status = get_sector_status(log_area_start_addr + cur_size);
  253. /* compare last and current status */
  254. switch (last_sec_status) {
  255. case SECTOR_STATUS_EMPUT: {
  256. switch (cur_sec_status) {
  257. case SECTOR_STATUS_EMPUT:
  258. break;
  259. case SECTOR_STATUS_USING:
  260. EF_DEBUG("Error: Log area error! Now will clean all log area.\n");
  261. ef_log_clean();
  262. return;
  263. case SECTOR_STATUS_FULL:
  264. EF_DEBUG("Error: Log area error! Now will clean all log area.\n");
  265. ef_log_clean();
  266. return;
  267. }
  268. empty_sec_counts++;
  269. break;
  270. }
  271. case SECTOR_STATUS_USING: {
  272. switch (cur_sec_status) {
  273. case SECTOR_STATUS_EMPUT:
  274. /* like state 1 */
  275. cur_log_sec_state = 1;
  276. log_start_addr = log_area_start_addr;
  277. cur_using_sec_addr = log_area_start_addr + cur_size - EF_ERASE_MIN_SIZE;
  278. break;
  279. case SECTOR_STATUS_USING:
  280. EF_DEBUG("Error: Log area error! Now will clean all log area.\n");
  281. ef_log_clean();
  282. return;
  283. case SECTOR_STATUS_FULL:
  284. /* like state 2 */
  285. cur_log_sec_state = 2;
  286. log_start_addr = log_area_start_addr + cur_size;
  287. cur_using_sec_addr = log_area_start_addr + cur_size - EF_ERASE_MIN_SIZE;
  288. break;
  289. }
  290. using_sec_counts++;
  291. break;
  292. }
  293. case SECTOR_STATUS_FULL: {
  294. switch (cur_sec_status) {
  295. case SECTOR_STATUS_EMPUT:
  296. /* like state 1 */
  297. if (cur_log_sec_state == 2) {
  298. EF_DEBUG("Error: Log area error! Now will clean all log area.\n");
  299. ef_log_clean();
  300. return;
  301. } else {
  302. cur_log_sec_state = 1;
  303. log_start_addr = log_area_start_addr;
  304. log_end_addr = log_area_start_addr + cur_size;
  305. cur_using_sec_addr = log_area_start_addr + cur_size - EF_ERASE_MIN_SIZE;
  306. }
  307. break;
  308. case SECTOR_STATUS_USING:
  309. if(total_sec_num <= 2) {
  310. /* like state 1 */
  311. cur_log_sec_state = 1;
  312. log_start_addr = log_area_start_addr;
  313. cur_using_sec_addr = log_area_start_addr + cur_size;
  314. } else {
  315. /* like state 2 when the sector is the last one */
  316. if (cur_size + EF_ERASE_MIN_SIZE >= LOG_AREA_SIZE) {
  317. cur_log_sec_state = 2;
  318. log_start_addr = get_next_flash_sec_addr(log_area_start_addr + cur_size);
  319. cur_using_sec_addr = log_area_start_addr + cur_size;
  320. }
  321. }
  322. break;
  323. case SECTOR_STATUS_FULL:
  324. break;
  325. }
  326. full_sector_counts++;
  327. break;
  328. }
  329. case SECTOR_STATUS_HEADER_ERROR:
  330. EF_DEBUG("Error: Log sector header error! Now will clean all log area.\n");
  331. ef_log_clean();
  332. return;
  333. }
  334. last_sec_status = cur_sec_status;
  335. }
  336. /* the last sector status counts */
  337. if (cur_sec_status == SECTOR_STATUS_EMPUT) {
  338. empty_sec_counts++;
  339. } else if (cur_sec_status == SECTOR_STATUS_USING) {
  340. using_sec_counts++;
  341. } else if (cur_sec_status == SECTOR_STATUS_FULL) {
  342. full_sector_counts++;
  343. } else if (cur_sec_status == SECTOR_STATUS_HEADER_ERROR) {
  344. EF_DEBUG("Error: Log sector header error! Now will clean all log area.\n");
  345. ef_log_clean();
  346. return;
  347. }
  348. if (using_sec_counts != 1) {
  349. /* this state is almost impossible */
  350. EF_DEBUG("Error: There must be only one sector status is USING! Now will clean all log area.\n");
  351. ef_log_clean();
  352. } else {
  353. /* find the end address */
  354. log_end_addr = find_sec_using_end_addr(cur_using_sec_addr);
  355. }
  356. }
  357. /**
  358. * Get log used flash total size.
  359. *
  360. * @return log used flash total size. @note NOT contain sector headers
  361. */
  362. size_t ef_log_get_used_size(void) {
  363. size_t header_total_num = 0, physical_size = 0;
  364. /* must be call this function after initialize OK */
  365. if (!init_ok) {
  366. return 0;
  367. }
  368. if (log_start_addr < log_end_addr) {
  369. physical_size = log_end_addr - log_start_addr;
  370. } else {
  371. physical_size = LOG_AREA_SIZE - (log_start_addr - log_end_addr);
  372. }
  373. header_total_num = physical_size / EF_ERASE_MIN_SIZE + 1;
  374. return physical_size - header_total_num * LOG_SECTOR_HEADER_SIZE;
  375. }
  376. /**
  377. * Sequential reading log data. It will ignore sector headers.
  378. *
  379. * @param addr address
  380. * @param log log buffer
  381. * @param size log size, not contain sector headers.
  382. *
  383. * @return result
  384. */
  385. static EfErrCode log_seq_read(uint32_t addr, uint32_t *log, size_t size) {
  386. EfErrCode result = EF_NO_ERR;
  387. size_t read_size = 0, read_size_temp = 0;
  388. while (size) {
  389. /* move to sector data address */
  390. if ((addr + read_size) % EF_ERASE_MIN_SIZE == 0) {
  391. addr += LOG_SECTOR_HEADER_SIZE;
  392. }
  393. /* calculate current sector last data size */
  394. read_size_temp = EF_ERASE_MIN_SIZE - (addr % EF_ERASE_MIN_SIZE);
  395. if (size < read_size_temp) {
  396. read_size_temp = size;
  397. }
  398. result = ef_port_read(addr + read_size, log + read_size / 4, read_size_temp);
  399. if (result != EF_NO_ERR) {
  400. return result;
  401. }
  402. read_size += read_size_temp;
  403. size -= read_size_temp;
  404. }
  405. return result;
  406. }
  407. /**
  408. * Calculate flash physical address by log index.
  409. *
  410. * @param index log index
  411. *
  412. * @return flash physical address
  413. */
  414. static uint32_t log_index2addr(size_t index) {
  415. size_t header_total_offset = 0;
  416. /* total include sector number */
  417. size_t sector_num = index / (EF_ERASE_MIN_SIZE - LOG_SECTOR_HEADER_SIZE) + 1;
  418. header_total_offset = sector_num * LOG_SECTOR_HEADER_SIZE;
  419. if (log_start_addr < log_end_addr) {
  420. return log_start_addr + index + header_total_offset;
  421. } else {
  422. if (log_start_addr + index + header_total_offset < log_area_start_addr + LOG_AREA_SIZE) {
  423. return log_start_addr + index + header_total_offset;
  424. } else {
  425. return log_start_addr + index + header_total_offset - LOG_AREA_SIZE;
  426. }
  427. }
  428. }
  429. /**
  430. * Read log from flash.
  431. *
  432. * @param index index for saved log.
  433. * Minimum index is 0.
  434. * Maximum index is ef_log_get_used_size() - 1.
  435. * @param log the log which will read from flash
  436. * @param size read bytes size
  437. *
  438. * @return result
  439. */
  440. EfErrCode ef_log_read(size_t index, uint32_t *log, size_t size) {
  441. EfErrCode result = EF_NO_ERR;
  442. size_t cur_using_size = ef_log_get_used_size();
  443. size_t read_size_temp = 0;
  444. size_t header_total_num = 0;
  445. if (!size) {
  446. return result;
  447. }
  448. EF_ASSERT(size % 4 == 0);
  449. EF_ASSERT(index < cur_using_size);
  450. if (index + size > cur_using_size) {
  451. EF_DEBUG("Warning: Log read size out of bound. Cut read size.\n");
  452. size = cur_using_size - index;
  453. }
  454. /* must be call this function after initialize OK */
  455. if (!init_ok) {
  456. return EF_ENV_INIT_FAILED;
  457. }
  458. if (log_start_addr < log_end_addr) {
  459. log_seq_read(log_index2addr(index), log, size);
  460. } else {
  461. if (log_index2addr(index) + size <= log_area_start_addr + LOG_AREA_SIZE) {
  462. /* Flash log area
  463. * |--------------|
  464. * log_area_start_addr --> |##############|
  465. * |##############|
  466. * |##############|
  467. * |--------------|
  468. * |##############|
  469. * |##############|
  470. * |##############| <-- log_end_addr
  471. * |--------------|
  472. * log_start_addr --> |##############|
  473. * read start --> |**************| <-- read end
  474. * |##############|
  475. * |--------------|
  476. *
  477. * read from (log_start_addr + log_index2addr(index)) to (log_start_addr + index + log_index2addr(index))
  478. */
  479. result = log_seq_read(log_index2addr(index), log, size);
  480. } else if (log_index2addr(index) < log_area_start_addr + LOG_AREA_SIZE) {
  481. /* Flash log area
  482. * |--------------|
  483. * log_area_start_addr --> |**************| <-- read end
  484. * |##############|
  485. * |##############|
  486. * |--------------|
  487. * |##############|
  488. * |##############|
  489. * |##############| <-- log_end_addr
  490. * |--------------|
  491. * log_start_addr --> |##############|
  492. * read start --> |**************|
  493. * |**************|
  494. * |--------------|
  495. * read will by 2 steps
  496. * step1: read from (log_start_addr + log_index2addr(index)) to flash log area end address
  497. * step2: read from flash log area start address to read size's end address
  498. */
  499. read_size_temp = (log_area_start_addr + LOG_AREA_SIZE) - log_index2addr(index);
  500. header_total_num = read_size_temp / EF_ERASE_MIN_SIZE;
  501. /* Minus some ignored bytes */
  502. read_size_temp -= header_total_num * LOG_SECTOR_HEADER_SIZE;
  503. result = log_seq_read(log_index2addr(index), log, read_size_temp);
  504. if (result == EF_NO_ERR) {
  505. result = log_seq_read(log_area_start_addr, log + read_size_temp / 4, size - read_size_temp);
  506. }
  507. } else {
  508. /* Flash log area
  509. * |--------------|
  510. * log_area_start_addr --> |##############|
  511. * read start --> |**************|
  512. * |**************| <-- read end
  513. * |--------------|
  514. * |##############|
  515. * |##############|
  516. * |##############| <-- log_end_addr
  517. * |--------------|
  518. * log_start_addr --> |##############|
  519. * |##############|
  520. * |##############|
  521. * |--------------|
  522. * read from (log_start_addr + log_index2addr(index) - LOG_AREA_SIZE) to read size's end address
  523. */
  524. result = log_seq_read(log_index2addr(index) - LOG_AREA_SIZE, log, size);
  525. }
  526. }
  527. return result;
  528. }
  529. /**
  530. * Write log to flash.
  531. *
  532. * @param log the log which will be write to flash
  533. * @param size write bytes size
  534. *
  535. * @return result
  536. */
  537. EfErrCode ef_log_write(const uint32_t *log, size_t size) {
  538. EfErrCode result = EF_NO_ERR;
  539. size_t write_size = 0, writable_size = 0;
  540. uint32_t write_addr = log_end_addr, erase_addr;
  541. SectorStatus sector_status;
  542. EF_ASSERT(size % 4 == 0);
  543. /* must be call this function after initialize OK */
  544. if (!init_ok) {
  545. return EF_ENV_INIT_FAILED;
  546. }
  547. if ((sector_status = get_sector_status(write_addr)) == SECTOR_STATUS_HEADER_ERROR) {
  548. return EF_WRITE_ERR;
  549. }
  550. /* write some log when current sector status is USING and EMPTY */
  551. if ((sector_status == SECTOR_STATUS_USING) || (sector_status == SECTOR_STATUS_EMPUT)) {
  552. /* write the already erased but not used area */
  553. writable_size = EF_ERASE_MIN_SIZE - ((write_addr - log_area_start_addr) % EF_ERASE_MIN_SIZE);
  554. if (size >= writable_size) {
  555. result = ef_port_write(write_addr, log, writable_size);
  556. if (result != EF_NO_ERR) {
  557. goto exit;
  558. }
  559. /* change the current sector status to FULL */
  560. result = write_sector_status(write_addr, SECTOR_STATUS_FULL);
  561. if (result != EF_NO_ERR) {
  562. goto exit;
  563. }
  564. write_size += writable_size;
  565. } else {
  566. result = ef_port_write(write_addr, log, size);
  567. log_end_addr = write_addr + size;
  568. goto exit;
  569. }
  570. }
  571. /* erase and write remain log */
  572. while (true) {
  573. /* calculate next available sector address */
  574. erase_addr = write_addr = get_next_flash_sec_addr(write_addr - 4);
  575. /* move the flash log start address to next available sector address */
  576. if (log_start_addr == erase_addr) {
  577. log_start_addr = get_next_flash_sec_addr(log_start_addr);
  578. }
  579. /* erase sector */
  580. result = ef_port_erase(erase_addr, EF_ERASE_MIN_SIZE);
  581. if (result != EF_NO_ERR) {
  582. goto exit;
  583. }
  584. /* change the sector status to EMPTY and USING when write begin sector start address */
  585. result = write_sector_status(write_addr, SECTOR_STATUS_EMPUT);
  586. result = write_sector_status(write_addr, SECTOR_STATUS_USING);
  587. if (result == EF_NO_ERR) {
  588. write_addr += LOG_SECTOR_HEADER_SIZE;
  589. } else {
  590. goto exit;
  591. }
  592. /* calculate current sector writable data size */
  593. writable_size = EF_ERASE_MIN_SIZE - LOG_SECTOR_HEADER_SIZE;
  594. if (size - write_size >= writable_size) {
  595. result = ef_port_write(write_addr, log + write_size / 4, writable_size);
  596. if (result != EF_NO_ERR) {
  597. goto exit;
  598. }
  599. /* change the current sector status to FULL */
  600. result = write_sector_status(write_addr, SECTOR_STATUS_FULL);
  601. if (result != EF_NO_ERR) {
  602. goto exit;
  603. }
  604. log_end_addr = write_addr + writable_size;
  605. write_size += writable_size;
  606. write_addr += writable_size;
  607. } else {
  608. result = ef_port_write(write_addr, log + write_size / 4, size - write_size);
  609. if (result != EF_NO_ERR) {
  610. goto exit;
  611. }
  612. log_end_addr = write_addr + (size - write_size);
  613. break;
  614. }
  615. }
  616. exit:
  617. return result;
  618. }
  619. /**
  620. * Get next flash sector address.The log total sector like ring buffer which implement by flash.
  621. *
  622. * @param cur_addr cur flash address
  623. *
  624. * @return next flash sector address
  625. */
  626. static uint32_t get_next_flash_sec_addr(uint32_t cur_addr) {
  627. size_t cur_sec_id = (cur_addr - log_area_start_addr) / EF_ERASE_MIN_SIZE;
  628. size_t sec_total_num = LOG_AREA_SIZE / EF_ERASE_MIN_SIZE;
  629. if (cur_sec_id + 1 >= sec_total_num) {
  630. /* return to ring head */
  631. return log_area_start_addr;
  632. } else {
  633. return log_area_start_addr + (cur_sec_id + 1) * EF_ERASE_MIN_SIZE;
  634. }
  635. }
  636. /**
  637. * Clean all log which in flash.
  638. *
  639. * @return result
  640. */
  641. EfErrCode ef_log_clean(void) {
  642. EfErrCode result = EF_NO_ERR;
  643. uint32_t write_addr = log_area_start_addr;
  644. /* clean address */
  645. log_start_addr = log_area_start_addr;
  646. log_end_addr = log_start_addr + LOG_SECTOR_HEADER_SIZE;
  647. /* erase log flash area */
  648. result = ef_port_erase(log_area_start_addr, LOG_AREA_SIZE);
  649. if (result != EF_NO_ERR) {
  650. goto exit;
  651. }
  652. /* setting first sector is EMPTY to USING */
  653. write_sector_status(write_addr, SECTOR_STATUS_EMPUT);
  654. write_sector_status(write_addr, SECTOR_STATUS_USING);
  655. if (result != EF_NO_ERR) {
  656. goto exit;
  657. }
  658. write_addr += EF_ERASE_MIN_SIZE;
  659. /* add sector header */
  660. while (true) {
  661. write_sector_status(write_addr, SECTOR_STATUS_EMPUT);
  662. if (result != EF_NO_ERR) {
  663. goto exit;
  664. }
  665. write_addr += EF_ERASE_MIN_SIZE;
  666. if (write_addr >= log_area_start_addr + LOG_AREA_SIZE) {
  667. break;
  668. }
  669. }
  670. exit:
  671. return result;
  672. }
  673. #endif /* EF_USING_LOG */