fds_internal_defs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * Copyright (c) 2015 - 2018, 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. #ifndef FDS_INTERNAL_DEFS_H__
  41. #define FDS_INTERNAL_DEFS_H__
  42. #include "sdk_config.h"
  43. #include <stdint.h>
  44. #include <stdbool.h>
  45. #if defined (FDS_THREADS)
  46. #include "nrf_soc.h"
  47. #include "app_util_platform.h"
  48. #endif
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. #define FDS_PAGE_TAG_SIZE (2) // Page tag size, in 4-byte words.
  53. #define FDS_PAGE_TAG_WORD_0 (0) // Offset of the first word in the page tag from the page address.
  54. #define FDS_PAGE_TAG_WORD_1 (1) // Offset of the second word in the page tag from the page address.
  55. // Page tag constants
  56. #define FDS_PAGE_TAG_MAGIC (0xDEADC0DE)
  57. #define FDS_PAGE_TAG_SWAP (0xF11E01FF)
  58. #define FDS_PAGE_TAG_DATA (0xF11E01FE)
  59. #define FDS_ERASED_WORD (0xFFFFFFFF)
  60. #define FDS_OFFSET_TL (0) // Offset of TL from the record base address, in 4-byte words.
  61. #define FDS_OFFSET_IC (1) // Offset of IC from the record base address, in 4-byte words.
  62. #define FDS_OFFSET_ID (2) // Offset of ID from the record base address, in 4-byte words.
  63. #define FDS_OFFSET_DATA (3) // Offset of the data from the record base address, in 4-byte words.
  64. #define FDS_HEADER_SIZE_TL (1) // Size of the TL part of the header, in 4-byte words.
  65. #define FDS_HEADER_SIZE_IC (1) // Size of the IC part of the header, in 4-byte words.
  66. #define FDS_HEADER_SIZE_ID (1) // Size of the record ID in the header, in 4-byte words.
  67. #define FDS_HEADER_SIZE (3) // Size of the whole header, in 4-byte words.
  68. #define FDS_OP_EXECUTING (NRF_SUCCESS)
  69. #define FDS_OP_COMPLETED (0x1D1D)
  70. #define NRF_FSTORAGE_NVMC 1
  71. #define NRF_FSTORAGE_SD 2
  72. // The size of a physical page, in 4-byte words.
  73. #if defined(NRF51)
  74. #define FDS_PHY_PAGE_SIZE (256)
  75. #else
  76. #define FDS_PHY_PAGE_SIZE (1024)
  77. #endif
  78. // The number of physical pages to be used. This value is configured indirectly.
  79. #define FDS_PHY_PAGES ((FDS_VIRTUAL_PAGES * FDS_VIRTUAL_PAGE_SIZE) / FDS_PHY_PAGE_SIZE)
  80. // The size of a virtual page, in number of physical pages.
  81. #define FDS_PHY_PAGES_IN_VPAGE (FDS_VIRTUAL_PAGE_SIZE / FDS_PHY_PAGE_SIZE)
  82. // The number of pages available to store data; which is the total minus one (the swap).
  83. #define FDS_DATA_PAGES (FDS_VIRTUAL_PAGES - 1)
  84. // Just a shorter name for the size, in words, of a virtual page.
  85. #define FDS_PAGE_SIZE (FDS_VIRTUAL_PAGE_SIZE)
  86. #if (FDS_VIRTUAL_PAGE_SIZE % FDS_PHY_PAGE_SIZE != 0)
  87. #error "FDS_VIRTUAL_PAGE_SIZE must be a multiple of the size of a physical page."
  88. #endif
  89. #if (FDS_VIRTUAL_PAGES < 2)
  90. #error "FDS requires at least two virtual pages."
  91. #endif
  92. // Page types.
  93. typedef enum
  94. {
  95. FDS_PAGE_DATA, // Page is ready for storage.
  96. FDS_PAGE_SWAP, // Page is reserved for garbage collection.
  97. FDS_PAGE_ERASED, // Page is erased.
  98. FDS_PAGE_UNDEFINED, // Undefined page type.
  99. } fds_page_type_t;
  100. typedef enum
  101. {
  102. FDS_HEADER_VALID, // Valid header.
  103. FDS_HEADER_DIRTY, // Header is incomplete, or record has been deleted.
  104. FDS_HEADER_CORRUPT // Header contains corrupt information, not related to CRC.
  105. } fds_header_status_t;
  106. typedef struct
  107. {
  108. fds_page_type_t page_type; // The page type.
  109. uint32_t const * p_addr; // The address of the page.
  110. uint16_t write_offset; // The page write offset, in 4-byte words.
  111. uint16_t words_reserved; // The amount of words reserved.
  112. uint32_t volatile records_open; // The number of open records.
  113. bool can_gc; // Indicates that there are some records that have been deleted.
  114. } fds_page_t;
  115. typedef struct
  116. {
  117. uint32_t const * p_addr;
  118. uint16_t write_offset;
  119. } fds_swap_page_t;
  120. // FDS op-codes.
  121. typedef enum
  122. {
  123. FDS_OP_NONE,
  124. FDS_OP_INIT, // Initialize the module.
  125. FDS_OP_WRITE, // Write a record to flash.
  126. FDS_OP_UPDATE, // Update a record.
  127. FDS_OP_DEL_RECORD, // Delete a record.
  128. FDS_OP_DEL_FILE, // Delete a file.
  129. FDS_OP_GC // Run garbage collection.
  130. } fds_op_code_t;
  131. typedef enum
  132. {
  133. FDS_OP_INIT_TAG_SWAP,
  134. FDS_OP_INIT_TAG_DATA,
  135. FDS_OP_INIT_ERASE_SWAP,
  136. FDS_OP_INIT_PROMOTE_SWAP,
  137. } fds_init_step_t;
  138. typedef enum
  139. {
  140. FDS_OP_WRITE_HEADER_BEGIN, // Write the record key and length.
  141. FDS_OP_WRITE_HEADER_FINALIZE, // Write the file ID and CRC.
  142. FDS_OP_WRITE_RECORD_ID, // Write the record ID.
  143. FDS_OP_WRITE_DATA, // Write the record data.
  144. FDS_OP_WRITE_FIND_RECORD,
  145. FDS_OP_WRITE_FLAG_DIRTY, // Flag a record as dirty (as part of an update operation).
  146. FDS_OP_WRITE_DONE,
  147. } fds_write_step_t;
  148. typedef enum
  149. {
  150. FDS_OP_DEL_RECORD_FLAG_DIRTY, // Flag a record as dirty.
  151. FDS_OP_DEL_FILE_FLAG_DIRTY, // Flag multiple records as dirty.
  152. FDS_OP_DEL_DONE,
  153. } fds_delete_step_t;
  154. #if defined(__CC_ARM)
  155. #pragma push
  156. #pragma anon_unions
  157. #elif defined(__ICCARM__)
  158. #pragma language=extended
  159. #elif defined(__GNUC__)
  160. // anonymous unions are enabled by default
  161. #endif
  162. typedef struct
  163. {
  164. fds_op_code_t op_code; // The opcode for the operation.
  165. union
  166. {
  167. struct
  168. {
  169. fds_init_step_t step; // The current step the operation is at.
  170. } init;
  171. struct
  172. {
  173. fds_header_t header;
  174. void const * p_data;
  175. uint16_t page; // The page the flash space for this command was reserved.
  176. fds_write_step_t step; // The current step the operation is at.
  177. uint32_t record_to_delete; // The record to delete in case this is an update.
  178. } write;
  179. struct
  180. {
  181. fds_delete_step_t step;
  182. uint16_t file_id;
  183. uint16_t record_key;
  184. uint32_t record_to_delete;
  185. } del;
  186. };
  187. } fds_op_t;
  188. #if defined(__CC_ARM)
  189. #pragma pop
  190. #elif defined(__ICCARM__)
  191. // leave anonymous unions enabled
  192. #elif defined(__GNUC__)
  193. // anonymous unions are enabled by default
  194. #endif
  195. enum
  196. {
  197. PAGE_ERASED = 0x1, // One or more erased pages found.
  198. PAGE_DATA = 0x2, // One or more data pages found.
  199. PAGE_SWAP_CLEAN = 0x4, // A clean (empty) swap page was found.
  200. PAGE_SWAP_DIRTY = 0x8, // A dirty (non-empty) swap page was found.
  201. };
  202. typedef enum
  203. {
  204. // No erased pages or FDS pages found.
  205. // This is a fatal error.
  206. NO_PAGES,
  207. // The filesystem can not be garbage collected.
  208. // This is a fatal error.
  209. NO_SWAP = (PAGE_DATA),
  210. // Perform a fresh installation.
  211. FRESH_INSTALL = (PAGE_ERASED),
  212. // Tag an erased page as swap.
  213. TAG_SWAP = (PAGE_ERASED | PAGE_DATA),
  214. // Tag all erased pages as data.
  215. TAG_DATA = (PAGE_ERASED | PAGE_SWAP_CLEAN),
  216. // Tag all remaining erased pages as data.
  217. TAG_DATA_INST = (PAGE_ERASED | PAGE_DATA | PAGE_SWAP_CLEAN),
  218. // The swap is dirty, likely because the device powered off during GC.
  219. // Because there is also an erased page, assume that that page has been garbage collected.
  220. // Hence, tag the swap as data (promote), an erased page as swap and remaining pages as data.
  221. PROMOTE_SWAP = (PAGE_ERASED | PAGE_SWAP_DIRTY),
  222. // Tag the swap as data (promote), an erased page as swap and remaining pages as data.
  223. PROMOTE_SWAP_INST = (PAGE_ERASED | PAGE_DATA | PAGE_SWAP_DIRTY),
  224. // The swap is dirty (written) and there are no erased pages. It is likely that the device
  225. // powered off during GC. It is safe to discard (erase) the swap, since data that was
  226. // swapped out still lies in one of the valid pages.
  227. DISCARD_SWAP = (PAGE_DATA | PAGE_SWAP_DIRTY),
  228. // Do nothing.
  229. ALREADY_INSTALLED = (PAGE_DATA | PAGE_SWAP_CLEAN),
  230. } fds_init_opts_t;
  231. typedef enum
  232. {
  233. GC_BEGIN, // Begin GC.
  234. GC_NEXT_PAGE, // GC a page.
  235. GC_FIND_NEXT_RECORD, // Find a valid record to copy.
  236. GC_COPY_RECORD, // Copy a valid record to swap.
  237. GC_ERASE_PAGE, // Erase the page being garbage collected.
  238. GC_DISCARD_SWAP, // Erase (discard) the swap page.
  239. GC_PROMOTE_SWAP, // Tag the swap as valid.
  240. GC_TAG_NEW_SWAP // Tag a freshly erased (GCed) page as swap.
  241. } fds_gc_state_t;
  242. // Holds garbage collection status and related data.
  243. typedef struct
  244. {
  245. fds_gc_state_t state; // The current GC step.
  246. uint16_t cur_page; // The current page being garbage collected.
  247. uint32_t const * p_record_src; // The current record being copied to swap.
  248. uint16_t run_count; // Total number of times GC was run.
  249. bool do_gc_page[FDS_DATA_PAGES]; // Controls which pages to garbage collect.
  250. bool resume; // Whether or not GC should be resumed.
  251. } fds_gc_data_t;
  252. // Macros to enable and disable application interrupts.
  253. #if defined (FDS_THREADS)
  254. #define CRITICAL_SECTION_ENTER() CRITICAL_REGION_ENTER()
  255. #define CRITICAL_SECTION_EXIT() CRITICAL_REGION_EXIT()
  256. #else
  257. #define CRITICAL_SECTION_ENTER()
  258. #define CRITICAL_SECTION_EXIT()
  259. #endif
  260. #ifdef __cplusplus
  261. }
  262. #endif
  263. #endif // FDS_INTERNAL_DEFS_H__