fds.c 59 KB

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