fds.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "fds.h"
  13. #include "fds_config.h"
  14. #include "fds_internal_defs.h"
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <stdbool.h>
  18. #include "fstorage.h"
  19. #include "app_util.h"
  20. #include "nrf_error.h"
  21. #if defined(FDS_CRC_ENABLED)
  22. #include "crc16.h"
  23. #endif
  24. static void fs_event_handler(fs_evt_t const * const evt, fs_ret_t result);
  25. // Our fstorage configuration.
  26. FS_REGISTER_CFG(fs_config_t fs_config) =
  27. {
  28. .callback = fs_event_handler,
  29. .num_pages = FDS_PHY_PAGES,
  30. // We register with the highest priority in order to be assigned
  31. // the pages with the highest memory address (closest to the bootloader).
  32. .priority = 0xFF
  33. };
  34. // Used to flag a record as dirty, i.e. ready for garbage collection.
  35. static fds_tl_t const m_fds_tl_dirty =
  36. {
  37. .record_key = FDS_RECORD_KEY_DIRTY,
  38. .length_words = 0xFFFF // Leave the record length field unchanged in flash.
  39. };
  40. // Internal status flags.
  41. static uint8_t m_flags;
  42. // The number of registered users and their callback functions.
  43. static uint8_t m_users;
  44. static fds_cb_t m_cb_table[FDS_MAX_USERS];
  45. // The latest (largest) record ID written so far.
  46. static uint32_t m_latest_rec_id;
  47. // The internal queues.
  48. static fds_op_queue_t m_op_queue;
  49. static fds_chunk_queue_t m_chunk_queue;
  50. // Structures used to hold informations about virtual pages.
  51. static fds_page_t m_pages[FDS_MAX_PAGES];
  52. static fds_swap_page_t m_swap_page;
  53. // Garbage collection data.
  54. static fds_gc_data_t m_gc;
  55. static void flag_set(fds_flags_t flag)
  56. {
  57. CRITICAL_SECTION_ENTER();
  58. m_flags |= flag;
  59. CRITICAL_SECTION_EXIT();
  60. }
  61. static void flag_clear(fds_flags_t flag)
  62. {
  63. CRITICAL_SECTION_ENTER();
  64. m_flags &= ~(flag);
  65. CRITICAL_SECTION_EXIT();
  66. }
  67. static bool flag_is_set(fds_flags_t flag)
  68. {
  69. return (m_flags & flag);
  70. }
  71. static void event_send(fds_evt_t const * const p_evt)
  72. {
  73. for (uint32_t user = 0; user < FDS_MAX_USERS; user++)
  74. {
  75. if (m_cb_table[user] != NULL)
  76. {
  77. m_cb_table[user](p_evt);
  78. }
  79. }
  80. }
  81. static void event_prepare(fds_op_t const * const p_op, fds_evt_t * const p_evt)
  82. {
  83. switch (p_op->op_code)
  84. {
  85. case FDS_OP_INIT:
  86. p_evt->id = FDS_EVT_INIT;
  87. break;
  88. case FDS_OP_WRITE:
  89. p_evt->id = FDS_EVT_WRITE;
  90. p_evt->write.file_id = p_op->write.header.ic.file_id;
  91. p_evt->write.record_key = p_op->write.header.tl.record_key;
  92. p_evt->write.record_id = p_op->write.header.record_id;
  93. break;
  94. case FDS_OP_UPDATE:
  95. p_evt->id = FDS_EVT_UPDATE;
  96. p_evt->write.file_id = p_op->write.header.ic.file_id;
  97. p_evt->write.record_key = p_op->write.header.tl.record_key;
  98. p_evt->write.record_id = p_op->write.header.record_id;
  99. p_evt->write.is_record_updated = (p_op->write.step == FDS_OP_WRITE_DONE);
  100. break;
  101. case FDS_OP_DEL_RECORD:
  102. p_evt->id = FDS_EVT_DEL_RECORD;
  103. p_evt->del.file_id = p_op->del.file_id;
  104. p_evt->del.record_key = p_op->del.record_key;
  105. p_evt->del.record_id = p_op->del.record_to_delete;
  106. break;
  107. case FDS_OP_DEL_FILE:
  108. p_evt->id = FDS_EVT_DEL_FILE;
  109. p_evt->del.file_id = p_op->del.file_id;
  110. p_evt->del.record_key = FDS_RECORD_KEY_DIRTY;
  111. break;
  112. case FDS_OP_GC:
  113. p_evt->id = FDS_EVT_GC;
  114. break;
  115. default:
  116. // Should not happen.
  117. break;
  118. }
  119. }
  120. static bool header_is_valid(fds_header_t const * const p_header)
  121. {
  122. return ((p_header->ic.file_id != FDS_FILE_ID_INVALID) &&
  123. (p_header->tl.record_key != FDS_RECORD_KEY_DIRTY));
  124. }
  125. static bool address_is_valid(uint32_t const * const p_addr)
  126. {
  127. return ((p_addr != NULL) &&
  128. (p_addr >= fs_config.p_start_addr) &&
  129. (p_addr <= fs_config.p_end_addr) &&
  130. (is_word_aligned(p_addr)));
  131. }
  132. static bool chunk_is_aligned(fds_record_chunk_t const * const p_chunk, uint32_t num_chunks)
  133. {
  134. for (uint32_t i = 0; i < num_chunks; i++)
  135. {
  136. if (!is_word_aligned(p_chunk[i].p_data))
  137. {
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. // Reads a page tag, and determines if the page is used to store data or as swap.
  144. static fds_page_type_t page_identify(uint32_t const * const p_page_addr)
  145. {
  146. if (p_page_addr[FDS_PAGE_TAG_WORD_0] != FDS_PAGE_TAG_MAGIC)
  147. {
  148. return FDS_PAGE_UNDEFINED;
  149. }
  150. switch (p_page_addr[FDS_PAGE_TAG_WORD_1])
  151. {
  152. case FDS_PAGE_TAG_SWAP:
  153. return FDS_PAGE_SWAP;
  154. case FDS_PAGE_TAG_DATA:
  155. return FDS_PAGE_DATA;
  156. default:
  157. return FDS_PAGE_UNDEFINED;
  158. }
  159. }
  160. static bool page_is_erased(uint32_t const * const p_page_addr)
  161. {
  162. for (uint32_t i = 0; i < FDS_PAGE_SIZE; i++)
  163. {
  164. if (*(p_page_addr + i) != FDS_ERASED_WORD)
  165. {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. // NOTE: Must be called from within a critical section.
  172. static bool page_has_space(uint16_t page, uint16_t length_words)
  173. {
  174. length_words += m_pages[page].write_offset;
  175. length_words += m_pages[page].words_reserved;
  176. return (length_words < FDS_PAGE_SIZE);
  177. }
  178. // Given a pointer to a record, find the index of the page on which it is stored.
  179. // Returns FDS_SUCCESS if the page is found, FDS_ERR_NOT_FOUND otherwise.
  180. static ret_code_t page_from_record(uint16_t * const p_page, uint32_t const * const p_rec)
  181. {
  182. ret_code_t ret = FDS_ERR_NOT_FOUND;
  183. CRITICAL_SECTION_ENTER();
  184. for (uint16_t i = 0; i < FDS_MAX_PAGES; i++)
  185. {
  186. if ((p_rec > m_pages[i].p_addr) &&
  187. (p_rec < m_pages[i].p_addr + FDS_PAGE_SIZE))
  188. {
  189. ret = FDS_SUCCESS;
  190. *p_page = i;
  191. break;
  192. }
  193. }
  194. CRITICAL_SECTION_EXIT();
  195. return ret;
  196. }
  197. // Scan a page to determine how many words have been written to it.
  198. // This information is used to set the page write offset during initialization.
  199. // Additionally, this function updates the latest record ID as it proceeds.
  200. // If an invalid record header is found, the can_gc argument is set to true.
  201. static void page_scan(uint32_t const * p_addr,
  202. uint16_t * const words_written,
  203. bool * const can_gc)
  204. {
  205. uint32_t const * const p_end_addr = p_addr + FDS_PAGE_SIZE;
  206. bool dirty_record_found = false;
  207. p_addr += FDS_PAGE_TAG_SIZE;
  208. *words_written = FDS_PAGE_TAG_SIZE;
  209. while ((p_addr < p_end_addr) && (*p_addr != FDS_ERASED_WORD))
  210. {
  211. // NOTE: Skip records with a dirty key or with a missing file ID.
  212. fds_header_t const * const p_header = (fds_header_t*)p_addr;
  213. if (!header_is_valid(p_header))
  214. {
  215. dirty_record_found = true;
  216. }
  217. else
  218. {
  219. // Update the latest (largest) record ID.
  220. if (p_header->record_id > m_latest_rec_id)
  221. {
  222. m_latest_rec_id = p_header->record_id;
  223. }
  224. }
  225. // Jump to the next record.
  226. p_addr += (FDS_HEADER_SIZE + p_header->tl.length_words);
  227. *words_written += (FDS_HEADER_SIZE + p_header->tl.length_words);
  228. }
  229. if (can_gc != NULL)
  230. {
  231. *can_gc = dirty_record_found;
  232. }
  233. }
  234. static void page_offsets_update(fds_page_t * const p_page, uint16_t length_words)
  235. {
  236. p_page->write_offset += (FDS_HEADER_SIZE + length_words);
  237. p_page->words_reserved -= (FDS_HEADER_SIZE + length_words);
  238. }
  239. // Tags a page as swap, i.e., reserved for GC.
  240. static ret_code_t page_tag_write_swap()
  241. {
  242. // Needs to be statically allocated since it will be written to flash.
  243. static uint32_t const page_tag_swap[] = {FDS_PAGE_TAG_MAGIC, FDS_PAGE_TAG_SWAP};
  244. return fs_store(&fs_config, m_swap_page.p_addr, page_tag_swap, FDS_PAGE_TAG_SIZE);
  245. }
  246. // Tags a page as data, i.e, ready for storage.
  247. static ret_code_t page_tag_write_data(uint32_t const * const p_page_addr)
  248. {
  249. // Needs to be statically allocated since it will be written to flash.
  250. static uint32_t const page_tag_data[] = {FDS_PAGE_TAG_MAGIC, FDS_PAGE_TAG_DATA};
  251. return fs_store(&fs_config, p_page_addr, page_tag_data, FDS_PAGE_TAG_SIZE);
  252. }
  253. // Reserve space on a page.
  254. // NOTE: this function takes into the account the space required for the record header.
  255. static ret_code_t write_space_reserve(uint16_t length_words, uint16_t * p_page)
  256. {
  257. bool space_reserved = false;
  258. uint16_t const total_len_words = length_words + FDS_HEADER_SIZE;
  259. if (total_len_words >= FDS_PAGE_SIZE - FDS_PAGE_TAG_SIZE)
  260. {
  261. return FDS_ERR_RECORD_TOO_LARGE;
  262. }
  263. CRITICAL_SECTION_ENTER();
  264. for (uint16_t page = 0; page < FDS_MAX_PAGES; page++)
  265. {
  266. if ((m_pages[page].page_type == FDS_PAGE_DATA) &&
  267. (page_has_space(page, total_len_words)))
  268. {
  269. space_reserved = true;
  270. *p_page = page;
  271. m_pages[page].words_reserved += total_len_words;
  272. break;
  273. }
  274. }
  275. CRITICAL_SECTION_EXIT();
  276. return (space_reserved) ? FDS_SUCCESS : FDS_ERR_NO_SPACE_IN_FLASH;
  277. }
  278. // Undo a write_space_reserve() call.
  279. // NOTE: Must be called within a critical section.
  280. static void write_space_free(uint16_t length_words, uint16_t page)
  281. {
  282. m_pages[page].words_reserved -= (length_words + FDS_HEADER_SIZE);
  283. }
  284. static uint32_t record_id_new(void)
  285. {
  286. CRITICAL_SECTION_ENTER();
  287. m_latest_rec_id++;
  288. CRITICAL_SECTION_EXIT();
  289. return m_latest_rec_id;
  290. }
  291. // Given a page and a record, finds the next valid record on that page. If p_record is NULL,
  292. // search from the beginning of the page, otherwise, resume searching from the address
  293. // pointed by p_record. Returns true if a record is found, returns false otherwise.
  294. // If no record is found, p_record is unchanged.
  295. static bool record_find_next(uint16_t page, uint32_t const ** p_record)
  296. {
  297. fds_header_t const * p_header;
  298. uint32_t const * p_next_rec = (*p_record);
  299. // If this is not the first invocation on this page, then jump to the next record.
  300. // Otherwise, start searching from the beginning of the page.
  301. if (p_next_rec != NULL)
  302. {
  303. p_header = ((fds_header_t*)p_next_rec);
  304. p_next_rec += (FDS_HEADER_SIZE + p_header->tl.length_words);
  305. }
  306. else
  307. {
  308. p_next_rec = m_pages[page].p_addr + FDS_PAGE_TAG_SIZE;
  309. }
  310. // Read records from the page, until a valid record is found or the end of the page is
  311. // reached. The argument p_record is only updated if a valid record is found.
  312. while ((p_next_rec < (m_pages[page].p_addr + FDS_PAGE_SIZE) &&
  313. *p_next_rec != FDS_ERASED_WORD))
  314. {
  315. p_header = (fds_header_t*)p_next_rec;
  316. if (header_is_valid(p_header))
  317. {
  318. *p_record = p_next_rec;
  319. return true;
  320. }
  321. else
  322. {
  323. // The record is not valid, jump to the next.
  324. p_next_rec += (FDS_HEADER_SIZE + (p_header->tl.length_words));
  325. }
  326. }
  327. // No more valid records on this page.
  328. return false;
  329. }
  330. // Find a record given its descriptor and retrive the page in which the record is stored.
  331. // NOTE: Do not pass NULL as an argument for p_page.
  332. static bool record_find_by_desc(fds_record_desc_t * const p_desc, uint16_t * const p_page)
  333. {
  334. // If the gc_run_count field in the descriptor matches our counter, then the record has
  335. // not been moved. If the address is valid, and the record ID matches, there is no need
  336. // to find the record again. Only lookup the page in which the record is stored.
  337. if ((address_is_valid(p_desc->p_record)) &&
  338. (p_desc->gc_run_count == m_gc.run_count) &&
  339. (p_desc->record_id == ((fds_header_t*)p_desc->p_record)->record_id))
  340. {
  341. return (page_from_record(p_page, p_desc->p_record) == FDS_SUCCESS);
  342. }
  343. // Otherwise, find the record in flash.
  344. for (*p_page = 0; *p_page < FDS_MAX_PAGES; (*p_page)++)
  345. {
  346. // Set p_record to NULL to make record_find_next() search from the beginning of the page.
  347. uint32_t const * p_record = NULL;
  348. while (record_find_next(*p_page, &p_record))
  349. {
  350. fds_header_t const * const p_header = (fds_header_t*)p_record;
  351. if (p_header->record_id == p_desc->record_id)
  352. {
  353. p_desc->p_record = p_record;
  354. p_desc->gc_run_count = m_gc.run_count;
  355. return true;
  356. }
  357. }
  358. }
  359. return false;
  360. }
  361. // Search for a record and return its descriptor.
  362. // If p_file_id is NULL, only the record key will be used for matching.
  363. // If p_record_key is NULL, only the file ID will be used for matching.
  364. // If both are NULL, it will iterate through all records.
  365. static ret_code_t record_find(uint16_t const * const p_file_id,
  366. uint16_t const * const p_record_key,
  367. fds_record_desc_t * const p_desc,
  368. fds_find_token_t * const p_token)
  369. {
  370. if (!flag_is_set(FDS_FLAG_INITIALIZED))
  371. {
  372. return FDS_ERR_NOT_INITIALIZED;
  373. }
  374. if (p_desc == NULL || p_token == NULL)
  375. {
  376. return FDS_ERR_NULL_ARG;
  377. }
  378. // Begin (or resume) searching for a record.
  379. for (; p_token->page < FDS_MAX_PAGES; p_token->page++)
  380. {
  381. if (m_pages[p_token->page].page_type != FDS_PAGE_DATA)
  382. {
  383. // Skip this page.
  384. continue;
  385. }
  386. while (record_find_next(p_token->page, &p_token->p_addr))
  387. {
  388. fds_header_t const * const p_header = (fds_header_t*)p_token->p_addr;
  389. // A valid record was found, check its header for a match.
  390. if ((p_file_id != NULL) &&
  391. (p_header->ic.file_id != *p_file_id))
  392. {
  393. continue;
  394. }
  395. if ((p_record_key != NULL) &&
  396. (p_header->tl.record_key != *p_record_key))
  397. {
  398. continue;
  399. }
  400. // Record found; update the descriptor.
  401. p_desc->record_id = p_header->record_id;
  402. p_desc->p_record = p_token->p_addr;
  403. p_desc->gc_run_count = m_gc.run_count;
  404. return FDS_SUCCESS;
  405. }
  406. // We have scanned an entire page. Set the address in the token to NULL
  407. // so that it will be updated in the next iteration.
  408. p_token->p_addr = NULL;
  409. }
  410. return FDS_ERR_NOT_FOUND;
  411. }
  412. // Retrieve basic statistics about dirty records on a page.
  413. static void dirty_records_stat(uint16_t page,
  414. uint16_t * const p_dirty_records,
  415. uint16_t * const p_word_count)
  416. {
  417. fds_header_t const * p_header;
  418. uint32_t const * p_rec;
  419. p_rec = m_pages[page].p_addr + FDS_PAGE_TAG_SIZE;
  420. while ((p_rec < (m_pages[page].p_addr + FDS_PAGE_SIZE)) &&
  421. (*p_rec != FDS_ERASED_WORD))
  422. {
  423. p_header = (fds_header_t*)p_rec;
  424. if (!header_is_valid(p_header))
  425. {
  426. (*p_dirty_records) += 1;
  427. (*p_word_count) += p_header->tl.length_words;
  428. }
  429. else
  430. {
  431. p_rec += (FDS_HEADER_SIZE + (p_header->tl.length_words));
  432. }
  433. }
  434. }
  435. // Advances one position in the queue.
  436. // Returns true if the queue is not empty.
  437. static bool queue_advance(void)
  438. {
  439. // Reset the current element.
  440. memset(&m_op_queue.op[m_op_queue.rp], 0x00, sizeof(fds_op_t));
  441. if (m_op_queue.count != 0)
  442. {
  443. // Advance in the queue, wrapping around if necessary.
  444. m_op_queue.rp = (m_op_queue.rp + 1) % FDS_OP_QUEUE_SIZE;
  445. m_op_queue.count--;
  446. }
  447. return (m_op_queue.count != 0);
  448. }
  449. // Given a pointer to an element in the chunk queue, computes the pointer to
  450. // the next element in the queue. Handles wrap around.
  451. void chunk_queue_next(fds_record_chunk_t ** pp_chunk)
  452. {
  453. if ((*pp_chunk) != &m_chunk_queue.chunk[FDS_CHUNK_QUEUE_SIZE - 1])
  454. {
  455. (*pp_chunk)++;
  456. return;
  457. }
  458. *pp_chunk = &m_chunk_queue.chunk[0];
  459. }
  460. // Retrieve the current chunk, and advance the queue.
  461. static void chunk_queue_get_and_advance(fds_record_chunk_t ** pp_chunk)
  462. {
  463. if (m_chunk_queue.count != 0)
  464. {
  465. // Point to the current chunk and advance the queue.
  466. *pp_chunk = &m_chunk_queue.chunk[m_chunk_queue.rp];
  467. m_chunk_queue.rp = (m_chunk_queue.rp + 1) % FDS_CHUNK_QUEUE_SIZE;
  468. m_chunk_queue.count--;
  469. }
  470. }
  471. static void chunk_queue_skip(fds_op_t const * const p_op)
  472. {
  473. if ((p_op->op_code == FDS_OP_WRITE) ||
  474. (p_op->op_code == FDS_OP_UPDATE))
  475. {
  476. m_chunk_queue.rp += p_op->write.chunk_count;
  477. m_chunk_queue.count -= p_op->write.chunk_count;
  478. }
  479. }
  480. // Enqueue an operation.
  481. static bool op_enqueue(fds_op_t const * const p_op,
  482. uint32_t num_chunks,
  483. fds_record_chunk_t const * const p_chunk)
  484. {
  485. uint32_t idx;
  486. bool ret = false;
  487. CRITICAL_SECTION_ENTER();
  488. if ((m_op_queue.count <= FDS_OP_QUEUE_SIZE - 1) &&
  489. (m_chunk_queue.count <= FDS_CHUNK_QUEUE_SIZE - num_chunks))
  490. {
  491. idx = (m_op_queue.count + m_op_queue.rp) % FDS_OP_QUEUE_SIZE;
  492. m_op_queue.op[idx] = *p_op;
  493. m_op_queue.count++;
  494. if (num_chunks != 0)
  495. {
  496. idx = (m_chunk_queue.count + m_chunk_queue.rp) % FDS_CHUNK_QUEUE_SIZE;
  497. fds_record_chunk_t * p_chunk_dst;
  498. p_chunk_dst = &m_chunk_queue.chunk[idx];
  499. for (uint32_t i = 0; i < num_chunks; i++)
  500. {
  501. *p_chunk_dst = p_chunk[i];
  502. chunk_queue_next(&p_chunk_dst);
  503. }
  504. m_chunk_queue.count += num_chunks;
  505. }
  506. ret = true;
  507. }
  508. CRITICAL_SECTION_EXIT();
  509. return ret;
  510. }
  511. // This function is called during initialization to setup the page structure (m_pages) and
  512. // provide additional information regarding eventual further initialization steps.
  513. static fds_init_opts_t pages_init()
  514. {
  515. uint32_t ret = NO_PAGES;
  516. // The index of the page being initialized in m_pages[].
  517. uint16_t page = 0;
  518. for (uint16_t i = 0; i < FDS_VIRTUAL_PAGES; i++)
  519. {
  520. uint32_t const * const p_page_addr = fs_config.p_start_addr + (i * FDS_PAGE_SIZE);
  521. fds_page_type_t const page_type = page_identify(p_page_addr);
  522. switch (page_type)
  523. {
  524. case FDS_PAGE_UNDEFINED:
  525. if (page_is_erased(p_page_addr))
  526. {
  527. if (m_swap_page.p_addr != NULL)
  528. {
  529. // If a swap page is already set, flag the page as erased (in m_pages)
  530. // and try to tag it as data (in flash) later on during initialization.
  531. m_pages[page].page_type = FDS_PAGE_ERASED;
  532. m_pages[page].p_addr = p_page_addr;
  533. m_pages[page].write_offset = FDS_PAGE_TAG_SIZE;
  534. // This is a candidate for a potential new swap page, in case the
  535. // current swap is going to be promoted to complete a GC instance.
  536. m_gc.cur_page = page;
  537. page++;
  538. }
  539. else
  540. {
  541. // If there is no swap page yet, use this one.
  542. m_swap_page.p_addr = p_page_addr;
  543. m_swap_page.write_offset = FDS_PAGE_TAG_SIZE;
  544. }
  545. ret |= PAGE_ERASED;
  546. }
  547. break;
  548. case FDS_PAGE_DATA:
  549. m_pages[page].page_type = FDS_PAGE_DATA;
  550. m_pages[page].p_addr = p_page_addr;
  551. // Scan the page to compute its write offset and determine whether or not the page
  552. // can be garbage collected. Additionally, update the latest kwown record ID.
  553. page_scan(p_page_addr, &m_pages[page].write_offset, &m_pages[page].can_gc);
  554. ret |= PAGE_DATA;
  555. page++;
  556. break;
  557. case FDS_PAGE_SWAP:
  558. m_swap_page.p_addr = p_page_addr;
  559. // If the swap is promoted, this offset should be kept, otherwise,
  560. // it should be set to FDS_PAGE_TAG_SIZE.
  561. page_scan(p_page_addr, &m_swap_page.write_offset, NULL);
  562. ret |= (m_swap_page.write_offset == FDS_PAGE_TAG_SIZE) ?
  563. SWAP_EMPTY : SWAP_DIRTY;
  564. break;
  565. default:
  566. // Shouldn't happen.
  567. break;
  568. }
  569. }
  570. return (fds_init_opts_t)ret;
  571. }
  572. // Write the first part of a record header (the key and length).
  573. static ret_code_t record_header_write_begin(fds_op_t * const p_op, uint32_t * const p_addr)
  574. {
  575. ret_code_t ret;
  576. ret = fs_store(&fs_config, p_addr + FDS_OFFSET_TL,
  577. (uint32_t*)&p_op->write.header.tl, FDS_HEADER_SIZE_TL);
  578. // Write the record ID next.
  579. p_op->write.step = FDS_OP_WRITE_RECORD_ID;
  580. return (ret == FS_SUCCESS) ? FDS_SUCCESS : FDS_ERR_BUSY;
  581. }
  582. static ret_code_t record_header_write_id(fds_op_t * const p_op, uint32_t * const p_addr)
  583. {
  584. ret_code_t ret;
  585. ret = fs_store(&fs_config, p_addr + FDS_OFFSET_ID,
  586. (uint32_t*)&p_op->write.header.record_id, FDS_HEADER_SIZE_ID);
  587. // If this record has zero chunk, write the last part of the header directly.
  588. // Otherwise, write the record chunks next.
  589. p_op->write.step = (p_op->write.chunk_count != 0) ? FDS_OP_WRITE_CHUNKS :
  590. FDS_OP_WRITE_HEADER_FINALIZE;
  591. return (ret == FS_SUCCESS) ? FDS_SUCCESS : FDS_ERR_BUSY;
  592. }
  593. static ret_code_t record_header_write_finalize(fds_op_t * const p_op, uint32_t * const p_addr)
  594. {
  595. ret_code_t ret;
  596. ret = fs_store(&fs_config, p_addr + FDS_OFFSET_IC,
  597. (uint32_t*)&p_op->write.header.ic, FDS_HEADER_SIZE_IC);
  598. // If this is a simple write operation, then this is the last step.
  599. // If this is an update instead, delete the old record next.
  600. p_op->write.step = (p_op->op_code == FDS_OP_UPDATE) ? FDS_OP_WRITE_FLAG_DIRTY :
  601. FDS_OP_WRITE_DONE;
  602. return (ret == FS_SUCCESS) ? FDS_SUCCESS : FDS_ERR_BUSY;
  603. }
  604. static ret_code_t record_header_flag_dirty(uint32_t * const p_record)
  605. {
  606. // Flag the record as dirty.
  607. fs_ret_t ret = fs_store(&fs_config, p_record,
  608. (uint32_t*)&m_fds_tl_dirty, FDS_HEADER_SIZE_TL);
  609. return (ret == FS_SUCCESS) ? FDS_SUCCESS : FDS_ERR_BUSY;
  610. }
  611. static ret_code_t record_find_and_delete(fds_op_t * const p_op)
  612. {
  613. ret_code_t ret;
  614. uint16_t page;
  615. fds_record_desc_t desc = {0};
  616. desc.record_id = p_op->del.record_to_delete;
  617. if (record_find_by_desc(&desc, &page))
  618. {
  619. fds_header_t const * const p_header = (fds_header_t const *)desc.p_record;
  620. // Copy the record key and file ID, so that they can be returned in the event.
  621. // In case this function is run as part of an update, there is no need to copy
  622. // the file ID and record key since they are present in the header stored
  623. // in the queue element.
  624. p_op->del.file_id = p_header->ic.file_id;
  625. p_op->del.record_key = p_header->tl.record_key;
  626. // Flag the record as dirty.
  627. ret = record_header_flag_dirty((uint32_t*)desc.p_record);
  628. // This page can now be garbage collected.
  629. m_pages[page].can_gc = true;
  630. }
  631. else
  632. {
  633. // The record never existed, or it has already been deleted.
  634. ret = FDS_ERR_NOT_FOUND;
  635. }
  636. return ret;
  637. }
  638. // Finds a record within a file and flags it as dirty.
  639. static ret_code_t file_find_and_delete(fds_op_t * const p_op)
  640. {
  641. ret_code_t ret;
  642. fds_record_desc_t desc;
  643. // This token must persist across calls.
  644. static fds_find_token_t tok = {0};
  645. // Pass NULL to ignore the record key.
  646. ret = record_find(&p_op->del.file_id, NULL, &desc, &tok);
  647. if (ret == FDS_SUCCESS)
  648. {
  649. // A record was found: flag it as dirty.
  650. ret = record_header_flag_dirty((uint32_t*)desc.p_record);
  651. // This page can now be garbage collected.
  652. m_pages[tok.page].can_gc = true;
  653. }
  654. else // FDS_ERR_NOT_FOUND
  655. {
  656. // No more records were found. Zero the token, so that it can be reused.
  657. memset(&tok, 0x00, sizeof(fds_find_token_t));
  658. }
  659. return ret;
  660. }
  661. // Writes a record chunk to flash and advances the chunk queue. Additionally, decrements
  662. // the number of chunks left to write for this operation and accumulates the offset.
  663. static ret_code_t record_write_chunk(fds_op_t * const p_op, uint32_t * const p_addr)
  664. {
  665. ret_code_t ret;
  666. fds_record_chunk_t * p_chunk = NULL;
  667. // Retrieve the next chunk to be written.
  668. chunk_queue_get_and_advance(&p_chunk);
  669. ret = fs_store(&fs_config, p_addr + p_op->write.chunk_offset,
  670. p_chunk->p_data, p_chunk->length_words);
  671. // Accumulate the offset.
  672. p_op->write.chunk_offset += p_chunk->length_words;
  673. // Decrement the number of chunks left to write.
  674. // NOTE: If chunk_count is initially zero, this function is not called
  675. // because this step is skipped entirely. See record_header_write_id().
  676. p_op->write.chunk_count--;
  677. if (p_op->write.chunk_count == 0)
  678. {
  679. // All record chunks have been written; write the last part of
  680. // the record header to finalize the write operation.
  681. p_op->write.step = FDS_OP_WRITE_HEADER_FINALIZE;
  682. }
  683. return (ret == NRF_SUCCESS) ? FDS_SUCCESS : FDS_ERR_BUSY;
  684. }
  685. #if defined(FDS_CRC_ENABLED)
  686. static bool crc_verify_success(uint16_t crc, uint16_t len_words, uint32_t const * const p_data)
  687. {
  688. uint16_t computed_crc;
  689. // The CRC is computed on the entire record, except the CRC field itself.
  690. // The record header is 12 bytes, out of these we have to skip bytes 6 to 8 where the
  691. // CRC itself is stored. Then we compute the CRC for the rest of the record, from byte 8 of
  692. // the header (where the record ID begins) to the end of the record data.
  693. computed_crc = crc16_compute((uint8_t const *)p_data, 6, NULL);
  694. computed_crc = crc16_compute((uint8_t const *)p_data + 8,
  695. (FDS_HEADER_SIZE_ID + len_words) * sizeof(uint32_t),
  696. &computed_crc);
  697. return (computed_crc == crc);
  698. }
  699. #endif
  700. static void gc_init(void)
  701. {
  702. m_gc.run_count++;
  703. m_gc.cur_page = 0;
  704. m_gc.resume = false;
  705. // Setup which pages to GC. Defer checking for open records and the can_gc flag,
  706. // as other operations might change those while GC is running.
  707. for (uint16_t i = 0; i < FDS_MAX_PAGES; i++)
  708. {
  709. m_gc.do_gc_page[i] = (m_pages[i].page_type == FDS_PAGE_DATA);
  710. }
  711. }
  712. // Obtain the next page to be garbage collected.
  713. // Returns true if there are pages left to garbage collect, returns false otherwise.
  714. static bool gc_page_next(uint16_t * const p_next_page)
  715. {
  716. bool ret = false;
  717. for (uint16_t i = 0; i < FDS_MAX_PAGES; i++)
  718. {
  719. if (m_gc.do_gc_page[i])
  720. {
  721. // Do not attempt to GC this page again.
  722. m_gc.do_gc_page[i] = false;
  723. // Only GC pages with no open records and with some records which have been deleted.
  724. if ((m_pages[i].records_open == 0) && (m_pages[i].can_gc == true))
  725. {
  726. *p_next_page = i;
  727. ret = true;
  728. break;
  729. }
  730. }
  731. }
  732. return ret;
  733. }
  734. static ret_code_t gc_swap_erase(void)
  735. {
  736. m_gc.state = GC_DISCARD_SWAP;
  737. m_swap_page.write_offset = FDS_PAGE_TAG_SIZE;
  738. return fs_erase(&fs_config, m_swap_page.p_addr, FDS_PHY_PAGES_IN_VPAGE);
  739. }
  740. // Erase the page being garbage collected, or erase the swap in case there are any open
  741. // records on the page being garbage collected.
  742. static ret_code_t gc_page_erase(void)
  743. {
  744. uint32_t ret;
  745. uint16_t const gc = m_gc.cur_page;
  746. if (m_pages[gc].records_open == 0)
  747. {
  748. ret = fs_erase(&fs_config, m_pages[gc].p_addr, FDS_PHY_PAGES_IN_VPAGE);
  749. m_gc.state = GC_ERASE_PAGE;
  750. }
  751. else
  752. {
  753. // If there are open records, stop garbage collection on this page.
  754. // Discard the swap and try to garbage collect another page.
  755. ret = gc_swap_erase();
  756. }
  757. return ret;
  758. }
  759. // Copy the current record to swap.
  760. static ret_code_t gc_record_copy(void)
  761. {
  762. fds_header_t const * const p_header = (fds_header_t*)m_gc.p_record_src;
  763. uint32_t const * const p_dest = m_swap_page.p_addr + m_swap_page.write_offset;
  764. uint16_t const record_len = FDS_HEADER_SIZE + p_header->tl.length_words;
  765. m_gc.state = GC_COPY_RECORD;
  766. // Copy the record to swap; it is guaranteed to fit in the destination page,
  767. // so there is no need to check its size. This will either succeed or timeout.
  768. return fs_store(&fs_config, p_dest, m_gc.p_record_src, record_len);
  769. }
  770. static ret_code_t gc_record_find_next(void)
  771. {
  772. ret_code_t ret;
  773. // Find the next valid record to copy.
  774. if (record_find_next(m_gc.cur_page, &m_gc.p_record_src))
  775. {
  776. ret = gc_record_copy();
  777. }
  778. else
  779. {
  780. // No more records left to copy on this page; swap pages.
  781. ret = gc_page_erase();
  782. }
  783. return ret;
  784. }
  785. // Promote the swap by tagging it as a data page.
  786. static ret_code_t gc_swap_promote(void)
  787. {
  788. m_gc.state = GC_PROMOTE_SWAP;
  789. return page_tag_write_data(m_pages[m_gc.cur_page].p_addr);
  790. }
  791. // Tag the page just garbage collected as swap.
  792. static ret_code_t gc_tag_new_swap(void)
  793. {
  794. m_gc.state = GC_TAG_NEW_SWAP;
  795. m_gc.p_record_src = NULL;
  796. return page_tag_write_swap();
  797. }
  798. static ret_code_t gc_next_page(void)
  799. {
  800. if (!gc_page_next(&m_gc.cur_page))
  801. {
  802. // No pages left to GC; GC has terminated. Reset the state.
  803. m_gc.state = GC_BEGIN;
  804. m_gc.cur_page = 0;
  805. m_gc.p_record_src = NULL;
  806. return FDS_OP_COMPLETED;
  807. }
  808. return gc_record_find_next();
  809. }
  810. // Update the swap page offeset after a record has been successfully copied to it.
  811. static void gc_update_swap_offset(void)
  812. {
  813. fds_header_t const * const p_header = (fds_header_t*)m_gc.p_record_src;
  814. uint16_t const record_len = FDS_HEADER_SIZE + p_header->tl.length_words;
  815. m_swap_page.write_offset += record_len;
  816. }
  817. static void gc_swap_pages(void)
  818. {
  819. // The page being garbage collected will be the new swap page,
  820. // and the current swap will be used as a data page (promoted).
  821. uint32_t const * const p_addr = m_swap_page.p_addr;
  822. m_swap_page.p_addr = m_pages[m_gc.cur_page].p_addr;
  823. m_pages[m_gc.cur_page].p_addr = p_addr;
  824. // Keep the offset for this page, but reset it for the swap.
  825. m_pages[m_gc.cur_page].write_offset = m_swap_page.write_offset;
  826. m_swap_page.write_offset = FDS_PAGE_TAG_SIZE;
  827. }
  828. static void gc_state_advance(void)
  829. {
  830. switch (m_gc.state)
  831. {
  832. case GC_BEGIN:
  833. gc_init();
  834. m_gc.state = GC_NEXT_PAGE;
  835. break;
  836. // A record was successfully copied.
  837. case GC_COPY_RECORD:
  838. gc_update_swap_offset();
  839. m_gc.state = GC_FIND_NEXT_RECORD;
  840. break;
  841. // A page was successfully erased. Prepare to promote the swap.
  842. case GC_ERASE_PAGE:
  843. gc_swap_pages();
  844. m_gc.state = GC_PROMOTE_SWAP;
  845. break;
  846. // Swap was discarded because the page being GC'ed had open records.
  847. case GC_DISCARD_SWAP:
  848. // Swap was sucessfully promoted.
  849. case GC_PROMOTE_SWAP:
  850. // Prepare to tag the page just GC'ed as swap.
  851. m_gc.state = GC_TAG_NEW_SWAP;
  852. break;
  853. case GC_TAG_NEW_SWAP:
  854. m_gc.state = GC_NEXT_PAGE;
  855. break;
  856. default:
  857. // Should not happen.
  858. break;
  859. }
  860. }
  861. // Initialize the filesystem.
  862. static ret_code_t init_execute(uint32_t prev_ret, fds_op_t * const p_op)
  863. {
  864. ret_code_t ret = FDS_ERR_INTERNAL;
  865. if (prev_ret != FS_SUCCESS)
  866. {
  867. // A previous operation has timed out.
  868. flag_clear(FDS_FLAG_INITIALIZING);
  869. return FDS_ERR_OPERATION_TIMEOUT;
  870. }
  871. switch (p_op->init.step)
  872. {
  873. case FDS_OP_INIT_TAG_SWAP:
  874. // The page write offset was determined previously by pages_init().
  875. ret = page_tag_write_swap();
  876. p_op->init.step = FDS_OP_INIT_TAG_DATA;
  877. break;
  878. case FDS_OP_INIT_TAG_DATA:
  879. {
  880. // Tag remaining erased pages as data.
  881. bool write_reqd = false;
  882. for (uint16_t i = 0; i < FDS_MAX_PAGES; i++)
  883. {
  884. if (m_pages[i].page_type == FDS_PAGE_ERASED)
  885. {
  886. ret = page_tag_write_data(m_pages[i].p_addr);
  887. m_pages[i].page_type = FDS_PAGE_DATA;
  888. write_reqd = true;
  889. break;
  890. }
  891. }
  892. if (!write_reqd)
  893. {
  894. flag_set(FDS_FLAG_INITIALIZED);
  895. flag_clear(FDS_FLAG_INITIALIZING);
  896. return FDS_OP_COMPLETED;
  897. }
  898. }
  899. break;
  900. case FDS_OP_INIT_ERASE_SWAP:
  901. ret = fs_erase(&fs_config, m_swap_page.p_addr, FDS_PHY_PAGES_IN_VPAGE);
  902. // If the swap is going to be discarded then reset its write_offset.
  903. m_swap_page.write_offset = FDS_PAGE_TAG_SIZE;
  904. p_op->init.step = FDS_OP_INIT_TAG_SWAP;
  905. break;
  906. case FDS_OP_INIT_PROMOTE_SWAP:
  907. {
  908. // When promoting the swap, keep the write_offset set by pages_init().
  909. ret = page_tag_write_data(m_swap_page.p_addr);
  910. uint16_t const gc = m_gc.cur_page;
  911. uint32_t const * const p_old_swap = m_swap_page.p_addr;
  912. // Execute the swap.
  913. m_swap_page.p_addr = m_pages[gc].p_addr;
  914. m_pages[gc].p_addr = p_old_swap;
  915. // Copy the offset from the swap to the new page.
  916. m_pages[gc].write_offset = m_swap_page.write_offset;
  917. m_swap_page.write_offset = FDS_PAGE_TAG_SIZE;
  918. m_pages[gc].page_type = FDS_PAGE_DATA;
  919. p_op->init.step = FDS_OP_INIT_TAG_SWAP;
  920. }
  921. break;
  922. default:
  923. // Should not happen.
  924. break;
  925. }
  926. if (ret != FDS_SUCCESS)
  927. {
  928. // fstorage queue was full.
  929. flag_clear(FDS_FLAG_INITIALIZING);
  930. return FDS_ERR_BUSY;
  931. }
  932. return FDS_OP_EXECUTING;
  933. }
  934. // Executes write and update operations.
  935. static ret_code_t write_execute(uint32_t prev_ret, fds_op_t * const p_op)
  936. {
  937. ret_code_t ret;
  938. uint32_t * p_write_addr;
  939. fds_page_t * const p_page = &m_pages[p_op->write.page];
  940. // This must persist across calls.
  941. static fds_record_desc_t desc = {0};
  942. if (prev_ret != FS_SUCCESS)
  943. {
  944. // The previous operation has timed out, update offsets.
  945. page_offsets_update(p_page, p_op->write.header.tl.length_words);
  946. return FDS_ERR_OPERATION_TIMEOUT;
  947. }
  948. // Compute the address where to write data.
  949. p_write_addr = (uint32_t*)(p_page->p_addr + p_page->write_offset);
  950. // Execute the current step of the operation, and set one to be executed next.
  951. switch (p_op->write.step)
  952. {
  953. case FDS_OP_WRITE_FIND_RECORD:
  954. {
  955. // The first step of updating a record constists of locating the copy to be deleted.
  956. // If the old copy couldn't be found for any reason then the update should fail.
  957. // This prevents duplicates when queuing multiple updates of the same record.
  958. uint16_t page;
  959. desc.p_record = NULL;
  960. desc.record_id = p_op->write.record_to_delete;
  961. if (!record_find_by_desc(&desc, &page))
  962. {
  963. return FDS_ERR_NOT_FOUND;
  964. }
  965. // Setting the step is redundant since we are falling through.
  966. }
  967. // Fallthrough to FDS_OP_WRITE_HEADER_BEGIN.
  968. case FDS_OP_WRITE_HEADER_BEGIN:
  969. ret = record_header_write_begin(p_op, p_write_addr);
  970. break;
  971. case FDS_OP_WRITE_RECORD_ID:
  972. ret = record_header_write_id(p_op, p_write_addr);
  973. break;
  974. case FDS_OP_WRITE_CHUNKS:
  975. ret = record_write_chunk(p_op, p_write_addr);
  976. break;
  977. case FDS_OP_WRITE_HEADER_FINALIZE:
  978. ret = record_header_write_finalize(p_op, p_write_addr);
  979. break;
  980. case FDS_OP_WRITE_FLAG_DIRTY:
  981. ret = record_header_flag_dirty((uint32_t*)desc.p_record);
  982. p_op->write.step = FDS_OP_WRITE_DONE;
  983. break;
  984. case FDS_OP_WRITE_DONE:
  985. ret = FDS_OP_COMPLETED;
  986. #if defined(FDS_CRC_ENABLED)
  987. if (flag_is_set(FDS_FLAG_VERIFY_CRC))
  988. {
  989. if (!crc_verify_success(p_op->write.header.ic.crc16,
  990. p_op->write.header.tl.length_words,
  991. p_write_addr))
  992. {
  993. ret = FDS_ERR_CRC_CHECK_FAILED;
  994. }
  995. }
  996. #endif
  997. break;
  998. default:
  999. ret = FDS_ERR_INTERNAL;
  1000. break;
  1001. }
  1002. // An operation has either completed or failed. It may have failed because fstorage
  1003. // ran out of memory, or because the user tried to delete a record which did not exist.
  1004. if (ret != FDS_OP_EXECUTING)
  1005. {
  1006. // There won't be another callback for this operation, so update the page offset now.
  1007. page_offsets_update(p_page, p_op->write.header.tl.length_words);
  1008. }
  1009. return ret;
  1010. }
  1011. static ret_code_t delete_execute(uint32_t prev_ret, fds_op_t * const p_op)
  1012. {
  1013. ret_code_t ret;
  1014. if (prev_ret != FS_SUCCESS)
  1015. {
  1016. return FDS_ERR_OPERATION_TIMEOUT;
  1017. }
  1018. switch (p_op->del.step)
  1019. {
  1020. case FDS_OP_DEL_RECORD_FLAG_DIRTY:
  1021. ret = record_find_and_delete(p_op);
  1022. p_op->del.step = FDS_OP_DEL_DONE;
  1023. break;
  1024. case FDS_OP_DEL_FILE_FLAG_DIRTY:
  1025. ret = file_find_and_delete(p_op);
  1026. if (ret == FDS_ERR_NOT_FOUND)
  1027. {
  1028. // No more records could be found.
  1029. // There won't be another callback for this operation, so return now.
  1030. ret = FDS_OP_COMPLETED;
  1031. }
  1032. break;
  1033. case FDS_OP_DEL_DONE:
  1034. ret = FDS_OP_COMPLETED;
  1035. break;
  1036. default:
  1037. ret = FDS_ERR_INTERNAL;
  1038. break;
  1039. }
  1040. return ret;
  1041. }
  1042. static ret_code_t gc_execute(uint32_t prev_ret)
  1043. {
  1044. ret_code_t ret;
  1045. if (prev_ret != FS_SUCCESS)
  1046. {
  1047. return FDS_ERR_OPERATION_TIMEOUT;
  1048. }
  1049. if (m_gc.resume)
  1050. {
  1051. m_gc.resume = false;
  1052. }
  1053. else
  1054. {
  1055. gc_state_advance();
  1056. }
  1057. switch (m_gc.state)
  1058. {
  1059. case GC_NEXT_PAGE:
  1060. ret = gc_next_page();
  1061. break;
  1062. case GC_FIND_NEXT_RECORD:
  1063. ret = gc_record_find_next();
  1064. break;
  1065. case GC_COPY_RECORD:
  1066. ret = gc_record_copy();
  1067. break;
  1068. case GC_ERASE_PAGE:
  1069. ret = gc_page_erase();
  1070. break;
  1071. case GC_PROMOTE_SWAP:
  1072. ret = gc_swap_promote();
  1073. break;
  1074. case GC_TAG_NEW_SWAP:
  1075. ret = gc_tag_new_swap();
  1076. break;
  1077. default:
  1078. // Should not happen.
  1079. ret = FDS_ERR_INTERNAL;
  1080. break;
  1081. }
  1082. // Either FDS_OP_EXECUTING, FDS_OP_COMPLETED, FDS_ERR_BUSY or FDS_ERR_INTERNAL.
  1083. return ret;
  1084. }
  1085. static void queue_process(fs_ret_t result)
  1086. {
  1087. ret_code_t ret;
  1088. fds_op_t * const p_op = &m_op_queue.op[m_op_queue.rp];
  1089. switch (p_op->op_code)
  1090. {
  1091. case FDS_OP_INIT:
  1092. ret = init_execute(result, p_op);
  1093. break;
  1094. case FDS_OP_WRITE:
  1095. case FDS_OP_UPDATE:
  1096. ret = write_execute(result, p_op);
  1097. break;
  1098. case FDS_OP_DEL_RECORD:
  1099. case FDS_OP_DEL_FILE:
  1100. ret = delete_execute(result, p_op);
  1101. break;
  1102. case FDS_OP_GC:
  1103. ret = gc_execute(result);
  1104. break;
  1105. default:
  1106. ret = FDS_ERR_INTERNAL;
  1107. break;
  1108. }
  1109. if (ret != FDS_OP_EXECUTING)
  1110. {
  1111. fds_evt_t evt;
  1112. if (ret == FDS_OP_COMPLETED)
  1113. {
  1114. evt.result = FDS_SUCCESS;
  1115. }
  1116. else
  1117. {
  1118. // Either FDS_ERR_BUSY, FDS_ERR_OPERATION_TIMEOUT,
  1119. // FDS_ERR_CRC_CHECK_FAILED or FDS_ERR_NOT_FOUND.
  1120. evt.result = ret;
  1121. // If this operation had any chunks in the queue, skip them.
  1122. chunk_queue_skip(p_op);
  1123. }
  1124. event_prepare(p_op, &evt);
  1125. event_send(&evt);
  1126. // Advance the queue, and if there are any queued operations, process them.
  1127. if (queue_advance())
  1128. {
  1129. queue_process(FS_SUCCESS);
  1130. }
  1131. else
  1132. {
  1133. // No more elements in the queue. Clear the FDS_FLAG_PROCESSING flag,
  1134. // so that new operation can start processing the queue.
  1135. flag_clear(FDS_FLAG_PROCESSING);
  1136. }
  1137. }
  1138. }
  1139. static void queue_start(void)
  1140. {
  1141. if (!flag_is_set(FDS_FLAG_PROCESSING))
  1142. {
  1143. flag_set(FDS_FLAG_PROCESSING);
  1144. queue_process(FS_SUCCESS);
  1145. }
  1146. }
  1147. static void fs_event_handler(fs_evt_t const * const p_evt, fs_ret_t result)
  1148. {
  1149. queue_process(result);
  1150. }
  1151. // Enqueues write and update operations.
  1152. static ret_code_t write_enqueue(fds_record_desc_t * const p_desc,
  1153. fds_record_t const * const p_record,
  1154. fds_reserve_token_t const * const p_tok,
  1155. fds_op_code_t op_code)
  1156. {
  1157. ret_code_t ret;
  1158. fds_op_t op;
  1159. uint16_t page;
  1160. uint16_t crc = 0;
  1161. uint16_t length_words = 0;
  1162. if (!flag_is_set(FDS_FLAG_INITIALIZED))
  1163. {
  1164. return FDS_ERR_NOT_INITIALIZED;
  1165. }
  1166. if (p_record == NULL)
  1167. {
  1168. return FDS_ERR_NULL_ARG;
  1169. }
  1170. if ((p_record->file_id == FDS_FILE_ID_INVALID) ||
  1171. (p_record->key == FDS_RECORD_KEY_DIRTY))
  1172. {
  1173. return FDS_ERR_INVALID_ARG;
  1174. }
  1175. if (!chunk_is_aligned(p_record->data.p_chunks,
  1176. p_record->data.num_chunks))
  1177. {
  1178. return FDS_ERR_UNALIGNED_ADDR;
  1179. }
  1180. // No space was previously reserved for this operation.
  1181. if (p_tok == NULL)
  1182. {
  1183. // Compute the total length of the record.
  1184. for (uint32_t i = 0; i < p_record->data.num_chunks; i++)
  1185. {
  1186. length_words += p_record->data.p_chunks[i].length_words;
  1187. }
  1188. // Find a page where to write data.
  1189. ret = write_space_reserve(length_words, &page);
  1190. if (ret != FDS_SUCCESS)
  1191. {
  1192. // There is either not enough flash space available (FDS_ERR_NO_SPACE_IN_FLASH) or
  1193. // the record exceeds the virtual page size (FDS_ERR_RECORD_TOO_LARGE).
  1194. return ret;
  1195. }
  1196. }
  1197. else
  1198. {
  1199. page = p_tok->page;
  1200. length_words = p_tok->length_words;
  1201. }
  1202. // Initialize the operation.
  1203. op.op_code = op_code;
  1204. op.write.step = FDS_OP_WRITE_HEADER_BEGIN;
  1205. op.write.page = page;
  1206. op.write.chunk_count = p_record->data.num_chunks;
  1207. op.write.chunk_offset = FDS_OFFSET_DATA;
  1208. op.write.header.record_id = record_id_new();
  1209. op.write.header.ic.file_id = p_record->file_id;
  1210. op.write.header.tl.record_key = p_record->key;
  1211. op.write.header.tl.length_words = length_words;
  1212. if (op_code == FDS_OP_UPDATE)
  1213. {
  1214. op.write.step = FDS_OP_WRITE_FIND_RECORD;
  1215. // Save the record ID of the record to be updated.
  1216. op.write.record_to_delete = p_desc->record_id;
  1217. }
  1218. #if defined (FDS_CRC_ENABLED)
  1219. // First, compute the CRC for the first 6 bytes of the header which contain the
  1220. // record key, length and file ID, then, compute the CRC of the record ID (4 bytes).
  1221. crc = crc16_compute((uint8_t*)&op.write.header, 6, NULL);
  1222. crc = crc16_compute((uint8_t*)&op.write.header.record_id, 4, &crc);
  1223. for (uint32_t i = 0; i < p_record->data.num_chunks; i++)
  1224. {
  1225. // Compute the CRC for the record data.
  1226. crc = crc16_compute((uint8_t*)p_record->data.p_chunks[i].p_data,
  1227. p_record->data.p_chunks[i].length_words * sizeof(uint32_t), &crc);
  1228. }
  1229. #endif
  1230. op.write.header.ic.crc16 = crc;
  1231. // Attempt to enqueue the operation.
  1232. if (!op_enqueue(&op, p_record->data.num_chunks, p_record->data.p_chunks))
  1233. {
  1234. // No space availble in the queues. Cancel the reservation of flash space.
  1235. CRITICAL_SECTION_ENTER();
  1236. write_space_free(length_words, page);
  1237. CRITICAL_SECTION_EXIT();
  1238. return FDS_ERR_NO_SPACE_IN_QUEUES;
  1239. }
  1240. // Initialize the record descriptor, if provided.
  1241. if (p_desc != NULL)
  1242. {
  1243. p_desc->p_record = NULL;
  1244. // Don't invoke record_id_new() again !
  1245. p_desc->record_id = op.write.header.record_id;
  1246. p_desc->record_is_open = false;
  1247. p_desc->gc_run_count = m_gc.run_count;
  1248. }
  1249. // Start processing the queue, if necessary.
  1250. queue_start();
  1251. return FDS_SUCCESS;
  1252. }
  1253. ret_code_t fds_register(fds_cb_t cb)
  1254. {
  1255. ret_code_t ret;
  1256. CRITICAL_SECTION_ENTER();
  1257. if (m_users == FDS_MAX_USERS)
  1258. {
  1259. ret = FDS_ERR_USER_LIMIT_REACHED;
  1260. }
  1261. else
  1262. {
  1263. m_cb_table[m_users] = cb;
  1264. m_users++;
  1265. ret = FDS_SUCCESS;
  1266. }
  1267. CRITICAL_SECTION_EXIT();
  1268. return ret;
  1269. }
  1270. ret_code_t fds_init(void)
  1271. {
  1272. fds_evt_t const evt_success = { .id = FDS_EVT_INIT, .result = FDS_SUCCESS };
  1273. // No initialization is necessary. Notify the application immediately.
  1274. if (flag_is_set(FDS_FLAG_INITIALIZED))
  1275. {
  1276. event_send(&evt_success);
  1277. return FDS_SUCCESS;
  1278. }
  1279. if (flag_is_set(FDS_FLAG_INITIALIZING))
  1280. {
  1281. return FDS_SUCCESS;
  1282. }
  1283. flag_set(FDS_FLAG_INITIALIZING);
  1284. (void)fs_init();
  1285. // Initialize the page structure (m_pages), and determine which
  1286. // initialization steps are required given the current state of the filesystem.
  1287. fds_init_opts_t init_opts = pages_init();
  1288. if (init_opts == NO_PAGES)
  1289. {
  1290. return FDS_ERR_NO_PAGES;
  1291. }
  1292. if (init_opts == ALREADY_INSTALLED)
  1293. {
  1294. // No initialization is necessary. Notify the application immediately.
  1295. flag_set(FDS_FLAG_INITIALIZED);
  1296. flag_clear(FDS_FLAG_INITIALIZING);
  1297. event_send(&evt_success);
  1298. return FDS_SUCCESS;
  1299. }
  1300. fds_op_t op;
  1301. op.op_code = FDS_OP_INIT;
  1302. switch (init_opts)
  1303. {
  1304. case FRESH_INSTALL:
  1305. case TAG_SWAP:
  1306. op.init.step = FDS_OP_INIT_TAG_SWAP;
  1307. break;
  1308. case PROMOTE_SWAP:
  1309. case PROMOTE_SWAP_INST:
  1310. op.init.step = FDS_OP_INIT_PROMOTE_SWAP;
  1311. break;
  1312. case DISCARD_SWAP:
  1313. op.init.step = FDS_OP_INIT_ERASE_SWAP;
  1314. break;
  1315. case TAG_DATA:
  1316. case TAG_DATA_INST:
  1317. op.init.step = FDS_OP_INIT_TAG_DATA;
  1318. break;
  1319. default:
  1320. // Should not happen.
  1321. break;
  1322. }
  1323. // This cannot fail since it will be the first operation in the queue.
  1324. (void)op_enqueue(&op, 0, NULL);
  1325. queue_start();
  1326. return FDS_SUCCESS;
  1327. }
  1328. ret_code_t fds_record_open(fds_record_desc_t * const p_desc,
  1329. fds_flash_record_t * const p_flash_rec)
  1330. {
  1331. uint16_t page;
  1332. if ((p_desc == NULL) || (p_flash_rec == NULL))
  1333. {
  1334. return FDS_ERR_NULL_ARG;
  1335. }
  1336. // Find the record if necessary.
  1337. if (record_find_by_desc(p_desc, &page))
  1338. {
  1339. fds_header_t const * const p_header = (fds_header_t*)p_desc->p_record;
  1340. #if defined(FDS_CRC_ENABLED)
  1341. if (!crc_verify_success(p_header->ic.crc16,
  1342. p_header->tl.length_words,
  1343. p_desc->p_record))
  1344. {
  1345. return FDS_ERR_CRC_CHECK_FAILED;
  1346. }
  1347. #endif
  1348. CRITICAL_SECTION_ENTER();
  1349. m_pages[page].records_open++;
  1350. CRITICAL_SECTION_EXIT();
  1351. // Initialize p_flash_rec.
  1352. p_flash_rec->p_header = p_header;
  1353. p_flash_rec->p_data = (p_desc->p_record + FDS_HEADER_SIZE);
  1354. // Set the record as open in the descriptor.
  1355. p_desc->record_is_open = true;
  1356. return FDS_SUCCESS;
  1357. }
  1358. // The record could not be found.
  1359. // It either never existed or it has been deleted.
  1360. return FDS_ERR_NOT_FOUND;
  1361. }
  1362. ret_code_t fds_record_close(fds_record_desc_t * const p_desc)
  1363. {
  1364. ret_code_t ret;
  1365. uint16_t page;
  1366. if (p_desc == NULL)
  1367. {
  1368. return FDS_ERR_NULL_ARG;
  1369. }
  1370. if (record_find_by_desc((fds_record_desc_t*)p_desc, &page))
  1371. {
  1372. CRITICAL_SECTION_ENTER();
  1373. if ((m_pages[page].records_open > 0) && (p_desc->record_is_open))
  1374. {
  1375. m_pages[page].records_open--;
  1376. p_desc->record_is_open = false;
  1377. ret = FDS_SUCCESS;
  1378. }
  1379. else
  1380. {
  1381. ret = FDS_ERR_NO_OPEN_RECORDS;
  1382. }
  1383. CRITICAL_SECTION_EXIT();
  1384. }
  1385. else
  1386. {
  1387. ret = FDS_ERR_NOT_FOUND;
  1388. }
  1389. return ret;
  1390. }
  1391. ret_code_t fds_reserve(fds_reserve_token_t * const p_tok, uint16_t length_words)
  1392. {
  1393. ret_code_t ret;
  1394. uint16_t page;
  1395. if (!flag_is_set(FDS_FLAG_INITIALIZED))
  1396. {
  1397. return FDS_ERR_NOT_INITIALIZED;
  1398. }
  1399. if (p_tok == NULL)
  1400. {
  1401. return FDS_ERR_NULL_ARG;
  1402. }
  1403. ret = write_space_reserve(length_words, &page);
  1404. if (ret == FDS_SUCCESS)
  1405. {
  1406. p_tok->page = page;
  1407. p_tok->length_words = length_words;
  1408. }
  1409. return ret;
  1410. }
  1411. ret_code_t fds_reserve_cancel(fds_reserve_token_t * const p_tok)
  1412. {
  1413. ret_code_t ret;
  1414. if (!flag_is_set(FDS_FLAG_INITIALIZED))
  1415. {
  1416. return FDS_ERR_NOT_INITIALIZED;
  1417. }
  1418. if (p_tok == NULL)
  1419. {
  1420. return FDS_ERR_NULL_ARG;
  1421. }
  1422. if (p_tok->page > FDS_MAX_PAGES)
  1423. {
  1424. // The page does not exist. This shouldn't happen.
  1425. return FDS_ERR_INVALID_ARG;
  1426. }
  1427. fds_page_t const * const p_page = &m_pages[p_tok->page];
  1428. CRITICAL_SECTION_ENTER();
  1429. if (p_page->words_reserved - (FDS_HEADER_SIZE + p_tok->length_words) >= 0)
  1430. {
  1431. // Free reserved space.
  1432. write_space_free(p_tok->length_words, p_tok->page);
  1433. // Clean the token.
  1434. p_tok->page = 0;
  1435. p_tok->length_words = 0;
  1436. ret = FDS_SUCCESS;
  1437. }
  1438. else
  1439. {
  1440. // We are trying to cancel a reservation of more words than how many are
  1441. // currently reserved on the page. Clearly, this shouldn't happen.
  1442. ret = FDS_ERR_INVALID_ARG;
  1443. }
  1444. CRITICAL_SECTION_EXIT();
  1445. return ret;
  1446. }
  1447. ret_code_t fds_record_write(fds_record_desc_t * const p_desc,
  1448. fds_record_t const * const p_record)
  1449. {
  1450. return write_enqueue(p_desc, p_record, NULL, FDS_OP_WRITE);
  1451. }
  1452. ret_code_t fds_record_write_reserved(fds_record_desc_t * const p_desc,
  1453. fds_record_t const * const p_record,
  1454. fds_reserve_token_t const * const p_tok)
  1455. {
  1456. // A NULL token is not allowed when writing to a reserved space.
  1457. if (p_tok == NULL)
  1458. {
  1459. return FDS_ERR_NULL_ARG;
  1460. }
  1461. return write_enqueue(p_desc, p_record, p_tok, FDS_OP_WRITE);
  1462. }
  1463. ret_code_t fds_record_update(fds_record_desc_t * const p_desc,
  1464. fds_record_t const * const p_record)
  1465. {
  1466. // A NULL descriptor is not allowed when updating a record.
  1467. if (p_desc == NULL)
  1468. {
  1469. return FDS_ERR_NULL_ARG;
  1470. }
  1471. return write_enqueue(p_desc, p_record, NULL, FDS_OP_UPDATE);
  1472. }
  1473. ret_code_t fds_record_delete(fds_record_desc_t * const p_desc)
  1474. {
  1475. fds_op_t op;
  1476. if (!flag_is_set(FDS_FLAG_INITIALIZED))
  1477. {
  1478. return FDS_ERR_NOT_INITIALIZED;
  1479. }
  1480. if (p_desc == NULL)
  1481. {
  1482. return FDS_ERR_NULL_ARG;
  1483. }
  1484. op.op_code = FDS_OP_DEL_RECORD;
  1485. op.del.step = FDS_OP_DEL_RECORD_FLAG_DIRTY;
  1486. op.del.record_to_delete = p_desc->record_id;
  1487. if (op_enqueue(&op, 0, NULL))
  1488. {
  1489. queue_start();
  1490. return FDS_SUCCESS;
  1491. }
  1492. return FDS_ERR_NO_SPACE_IN_QUEUES;
  1493. }
  1494. ret_code_t fds_file_delete(uint16_t file_id)
  1495. {
  1496. fds_op_t op;
  1497. if (!flag_is_set(FDS_FLAG_INITIALIZED))
  1498. {
  1499. return FDS_ERR_NOT_INITIALIZED;
  1500. }
  1501. if (file_id == FDS_FILE_ID_INVALID)
  1502. {
  1503. return FDS_ERR_INVALID_ARG;
  1504. }
  1505. op.op_code = FDS_OP_DEL_FILE;
  1506. op.del.step = FDS_OP_DEL_FILE_FLAG_DIRTY;
  1507. op.del.file_id = file_id;
  1508. if (op_enqueue(&op, 0, NULL))
  1509. {
  1510. queue_start();
  1511. return FDS_SUCCESS;
  1512. }
  1513. return FDS_ERR_NO_SPACE_IN_QUEUES;
  1514. }
  1515. ret_code_t fds_gc(void)
  1516. {
  1517. fds_op_t op;
  1518. if (!flag_is_set(FDS_FLAG_INITIALIZED))
  1519. {
  1520. return FDS_ERR_NOT_INITIALIZED;
  1521. }
  1522. op.op_code = FDS_OP_GC;
  1523. if (op_enqueue(&op, 0, NULL))
  1524. {
  1525. if (m_gc.state != GC_BEGIN)
  1526. {
  1527. // Resume GC by retrying the last step.
  1528. m_gc.resume = true;
  1529. }
  1530. queue_start();
  1531. return FDS_SUCCESS;
  1532. }
  1533. return FDS_ERR_NO_SPACE_IN_QUEUES;
  1534. }
  1535. ret_code_t fds_record_iterate(fds_record_desc_t * const p_desc,
  1536. fds_find_token_t * const p_token)
  1537. {
  1538. return record_find(NULL, NULL, p_desc, p_token);
  1539. }
  1540. ret_code_t fds_record_find(uint16_t file_id,
  1541. uint16_t record_key,
  1542. fds_record_desc_t * const p_desc,
  1543. fds_find_token_t * const p_token)
  1544. {
  1545. return record_find(&file_id, &record_key, p_desc, p_token);
  1546. }
  1547. ret_code_t fds_record_find_by_key(uint16_t record_key,
  1548. fds_record_desc_t * const p_desc,
  1549. fds_find_token_t * const p_token)
  1550. {
  1551. return record_find(NULL, &record_key, p_desc, p_token);
  1552. }
  1553. ret_code_t fds_record_find_in_file(uint16_t file_id,
  1554. fds_record_desc_t * const p_desc,
  1555. fds_find_token_t * const p_token)
  1556. {
  1557. return record_find(&file_id, NULL, p_desc, p_token);
  1558. }
  1559. ret_code_t fds_descriptor_from_rec_id(fds_record_desc_t * const p_desc,
  1560. uint32_t record_id)
  1561. {
  1562. if (p_desc == NULL)
  1563. {
  1564. return FDS_ERR_NULL_ARG;
  1565. }
  1566. // Zero the descriptor and set the record_id field.
  1567. memset(p_desc, 0x00, sizeof(fds_record_desc_t));
  1568. p_desc->record_id = record_id;
  1569. return FDS_SUCCESS;
  1570. }
  1571. ret_code_t fds_record_id_from_desc(fds_record_desc_t const * const p_desc,
  1572. uint32_t * const p_record_id)
  1573. {
  1574. if ((p_desc == NULL) || (p_record_id == NULL))
  1575. {
  1576. return FDS_ERR_NULL_ARG;
  1577. }
  1578. *p_record_id = p_desc->record_id;
  1579. return FDS_SUCCESS;
  1580. }
  1581. ret_code_t fds_stat(fds_stat_t * const p_stat)
  1582. {
  1583. uint16_t const words_in_page = FDS_PAGE_SIZE - FDS_PAGE_TAG_SIZE;
  1584. // The largest number of free contiguous words on any page.
  1585. uint16_t contig_words = 0;
  1586. if (!flag_is_set(FDS_FLAG_INITIALIZED))
  1587. {
  1588. return FDS_ERR_NOT_INITIALIZED;
  1589. }
  1590. if (p_stat == NULL)
  1591. {
  1592. return FDS_ERR_NULL_ARG;
  1593. }
  1594. memset(p_stat, 0x00, sizeof(fds_stat_t));
  1595. for (uint16_t i = 0; i < FDS_MAX_PAGES; i++)
  1596. {
  1597. uint32_t const * p_record = NULL;
  1598. uint16_t const words_used = m_pages[i].write_offset + m_pages[i].words_reserved;
  1599. p_stat->open_records += m_pages[i].records_open;
  1600. p_stat->words_used += words_used;
  1601. contig_words = (words_in_page - words_used);
  1602. if (contig_words > p_stat->largest_contig)
  1603. {
  1604. p_stat->largest_contig = contig_words;
  1605. }
  1606. while (record_find_next(i, &p_record))
  1607. {
  1608. p_stat->valid_records++;
  1609. }
  1610. dirty_records_stat(i, &p_stat->dirty_records, &p_stat->freeable_words);
  1611. }
  1612. return FDS_SUCCESS;
  1613. }
  1614. #if defined(FDS_CRC_ENABLED)
  1615. ret_code_t fds_verify_crc_on_writes(bool enable)
  1616. {
  1617. if (enable)
  1618. {
  1619. flag_set(FDS_FLAG_VERIFY_CRC);
  1620. }
  1621. else
  1622. {
  1623. flag_clear(FDS_FLAG_VERIFY_CRC);
  1624. }
  1625. return FDS_SUCCESS;
  1626. }
  1627. #endif