fds.c 63 KB

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