fx_port.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /***************************************************************************
  2. * Copyright (c) 2024 Microsoft Corporation
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the MIT License which is available at
  6. * https://opensource.org/licenses/MIT.
  7. *
  8. * SPDX-License-Identifier: MIT
  9. **************************************************************************/
  10. /**************************************************************************/
  11. /**************************************************************************/
  12. /** */
  13. /** FileX Component */
  14. /** */
  15. /** Port Specific */
  16. /** */
  17. /**************************************************************************/
  18. /**************************************************************************/
  19. /**************************************************************************/
  20. /* */
  21. /* PORT SPECIFIC C INFORMATION RELEASE */
  22. /* */
  23. /* fx_port.h Linux/GCC */
  24. /* 6.3.0 */
  25. /* */
  26. /* AUTHOR */
  27. /* */
  28. /* William E. Lamie, Microsoft Corporation */
  29. /* */
  30. /* DESCRIPTION */
  31. /* */
  32. /* This file contains data type definitions that make the FileX FAT */
  33. /* compatible file system function identically on a variety of */
  34. /* different processor architectures. For example, the byte offset of */
  35. /* various entries in the boot record, and directory entries are */
  36. /* defined in this file. */
  37. /* */
  38. /* RELEASE HISTORY */
  39. /* */
  40. /* DATE NAME DESCRIPTION */
  41. /* */
  42. /* 09-30-2020 William E. Lamie Initial Version 6.1 */
  43. /* 03-02-2021 William E. Lamie Modified comment(s), and */
  44. /* added standalone support, */
  45. /* resulting in version 6.1.5 */
  46. /* 08-02-2021 William E. Lamie Modified comment(s), */
  47. /* resulting in version 6.1.8 */
  48. /* 10-31-2023 Xiuwen Cai Modified comment(s), */
  49. /* added basic types guards, */
  50. /* resulting in version 6.3.0 */
  51. /* */
  52. /**************************************************************************/
  53. #ifndef FX_PORT_H
  54. #define FX_PORT_H
  55. /* Determine if the optional FileX user define file should be used. */
  56. #ifdef FX_INCLUDE_USER_DEFINE_FILE
  57. /* Yes, include the user defines in fx_user.h. The defines in this file may
  58. alternately be defined on the command line. */
  59. #include "fx_user.h"
  60. #endif
  61. #include <stdio.h>
  62. /* Include the ThreadX api file. */
  63. #ifndef FX_STANDALONE_ENABLE
  64. #include "tx_api.h"
  65. #else
  66. /* Define compiler library include files. */
  67. #include <stdint.h>
  68. #include <stdlib.h>
  69. #ifndef VOID
  70. #define VOID void
  71. typedef char CHAR;
  72. typedef char BOOL;
  73. typedef unsigned char UCHAR;
  74. typedef int INT;
  75. typedef unsigned int UINT;
  76. typedef long LONG;
  77. typedef unsigned long ULONG;
  78. typedef short SHORT;
  79. typedef unsigned short USHORT;
  80. #endif
  81. #ifndef ULONG64_DEFINED
  82. #define ULONG64_DEFINED
  83. typedef unsigned long long ULONG64;
  84. #endif
  85. /* Define basic alignment type used in block and byte pool operations. This data type must
  86. be at least 32-bits in size and also be large enough to hold a pointer type. */
  87. #ifndef ALIGN_TYPE_DEFINED
  88. #define ALIGN_TYPE_DEFINED
  89. #define ALIGN_TYPE ULONG
  90. #endif
  91. #endif
  92. #ifdef FX_REGRESSION_TEST
  93. /* Define parameters for regression test suite. */
  94. #define FX_MAX_SECTOR_CACHE 256
  95. #define FX_MAX_FAT_CACHE 64
  96. #define FX_FAT_MAP_SIZE 1
  97. /* Define variables and macros used to introduce errors for the regression test suite. */
  98. extern ULONG _fx_ram_driver_io_error_request;
  99. extern ULONG _fx_ram_driver_io_request_count;
  100. extern ULONG _fx_file_open_max_file_size_request;
  101. extern ULONG _fx_directory_entry_read_count;
  102. extern ULONG _fx_directory_entry_read_error_request;
  103. extern ULONG _fx_directory_entry_write_count;
  104. extern ULONG _fx_directory_entry_write_error_request;
  105. extern ULONG _fx_utility_fat_entry_write_count;
  106. extern ULONG _fx_utility_fat_entry_write_error_request;
  107. extern ULONG _fx_utility_fat_entry_read_count;
  108. extern ULONG _fx_utility_fat_entry_read_error_request;
  109. extern ULONG _fx_utility_logical_sector_flush_count;
  110. extern ULONG _fx_utility_logical_sector_flush_error_request;
  111. extern ULONG _fx_utility_logical_sector_write_count;
  112. extern ULONG _fx_utility_logical_sector_write_error_request;
  113. extern ULONG _fx_utility_logical_sector_read_count;
  114. extern ULONG _fx_utility_logical_sector_read_error_request;
  115. extern ULONG _fx_utility_logical_sector_read_1_count;
  116. extern ULONG _fx_utility_logical_sector_read_1_error_request;
  117. #ifdef FX_ENABLE_FAULT_TOLERANT
  118. struct FX_MEDIA_STRUCT;
  119. extern VOID fault_tolerant_enable_callback(struct FX_MEDIA_STRUCT *media_ptr,
  120. UCHAR *fault_tolerant_memory_buffer,
  121. ULONG log_size);
  122. extern VOID fault_tolerant_apply_log_callback(struct FX_MEDIA_STRUCT *media_ptr,
  123. UCHAR *fault_tolerant_memory_buffer,
  124. ULONG log_size);
  125. #endif /* FX_ENABLE_FAULT_TOLERANT */
  126. #define FX_DIRECTORY_ENTRY_READ_EXTENSION _fx_directory_entry_read_count++; \
  127. if (_fx_directory_entry_read_error_request) \
  128. { \
  129. _fx_directory_entry_read_error_request--; \
  130. if (_fx_directory_entry_read_error_request == 0) \
  131. { \
  132. return(FX_IO_ERROR); \
  133. } \
  134. }
  135. #define FX_DIRECTORY_ENTRY_WRITE_EXTENSION _fx_directory_entry_write_count++; \
  136. if (_fx_directory_entry_write_error_request) \
  137. { \
  138. _fx_directory_entry_write_error_request--; \
  139. if (_fx_directory_entry_write_error_request == 0) \
  140. { \
  141. return(FX_IO_ERROR); \
  142. } \
  143. }
  144. #define FX_UTILITY_FAT_ENTRY_READ_EXTENSION _fx_utility_fat_entry_read_count++; \
  145. if (_fx_utility_fat_entry_read_error_request) \
  146. { \
  147. _fx_utility_fat_entry_read_error_request--; \
  148. if (_fx_utility_fat_entry_read_error_request == 0) \
  149. { \
  150. return(FX_IO_ERROR); \
  151. } \
  152. if (_fx_utility_fat_entry_read_error_request == 10000) \
  153. { \
  154. *entry_ptr = 1; \
  155. _fx_utility_fat_entry_read_error_request = 0; \
  156. return(FX_SUCCESS); \
  157. } \
  158. if (_fx_utility_fat_entry_read_error_request == 20000) \
  159. { \
  160. *entry_ptr = media_ptr -> fx_media_fat_reserved; \
  161. _fx_utility_fat_entry_read_error_request = 0; \
  162. return(FX_SUCCESS); \
  163. } \
  164. if (_fx_utility_fat_entry_read_error_request == 30000) \
  165. { \
  166. *entry_ptr = cluster; \
  167. _fx_utility_fat_entry_read_error_request = 0; \
  168. return(FX_SUCCESS); \
  169. } \
  170. if (_fx_utility_fat_entry_read_error_request == 40000) \
  171. { \
  172. media_ptr -> fx_media_total_clusters = 0; \
  173. _fx_utility_fat_entry_read_error_request = 0; \
  174. return(FX_SUCCESS); \
  175. } \
  176. }
  177. #define FX_UTILITY_FAT_ENTRY_WRITE_EXTENSION _fx_utility_fat_entry_write_count++; \
  178. if (_fx_utility_fat_entry_write_error_request) \
  179. { \
  180. _fx_utility_fat_entry_write_error_request--; \
  181. if (_fx_utility_fat_entry_write_error_request == 0) \
  182. { \
  183. return(FX_IO_ERROR); \
  184. } \
  185. }
  186. #define FX_UTILITY_LOGICAL_SECTOR_FLUSH_EXTENSION _fx_utility_logical_sector_flush_count++; \
  187. if (_fx_utility_logical_sector_flush_error_request) \
  188. { \
  189. _fx_utility_logical_sector_flush_error_request--; \
  190. if (_fx_utility_logical_sector_flush_error_request == 0) \
  191. { \
  192. return(FX_IO_ERROR); \
  193. } \
  194. }
  195. #define FX_UTILITY_LOGICAL_SECTOR_READ_EXTENSION _fx_utility_logical_sector_read_count++; \
  196. if (_fx_utility_logical_sector_read_error_request) \
  197. { \
  198. _fx_utility_logical_sector_read_error_request--; \
  199. if (_fx_utility_logical_sector_read_error_request == 0) \
  200. { \
  201. return(FX_IO_ERROR); \
  202. } \
  203. }
  204. #define FX_UTILITY_LOGICAL_SECTOR_READ_EXTENSION_1 _fx_utility_logical_sector_read_1_count++; \
  205. if (_fx_utility_logical_sector_read_1_error_request) \
  206. { \
  207. _fx_utility_logical_sector_read_1_error_request--; \
  208. if (_fx_utility_logical_sector_read_1_error_request == 0) \
  209. { \
  210. cache_entry = FX_NULL; \
  211. } \
  212. }
  213. #define FX_UTILITY_LOGICAL_SECTOR_WRITE_EXTENSION _fx_utility_logical_sector_write_count++; \
  214. if (_fx_utility_logical_sector_write_error_request) \
  215. { \
  216. _fx_utility_logical_sector_write_error_request--; \
  217. if (_fx_utility_logical_sector_write_error_request == 0) \
  218. { \
  219. return(FX_IO_ERROR); \
  220. } \
  221. }
  222. #define FX_FAULT_TOLERANT_ENABLE_EXTENSION fault_tolerant_enable_callback(media_ptr, media_ptr -> fx_media_fault_tolerant_memory_buffer, total_size);
  223. #define FX_FAULT_TOLERANT_APPLY_LOGS_EXTENSION fault_tolerant_apply_log_callback(media_ptr, media_ptr -> fx_media_fault_tolerant_memory_buffer, size);
  224. #endif /* FX_REGRESSION_TEST */
  225. /* Define FileX internal protection macros. If FX_SINGLE_THREAD is defined,
  226. these protection macros are effectively disabled. However, for multi-thread
  227. uses, the macros are setup to utilize a ThreadX mutex for multiple thread
  228. access control into an open media. */
  229. /* Reduce the mutex error checking for testing purpose. */
  230. #if defined(FX_SINGLE_THREAD) || defined(FX_STANDALONE_ENABLE)
  231. #define FX_PROTECT
  232. #define FX_UNPROTECT
  233. #else
  234. #define FX_PROTECT tx_mutex_get(&(media_ptr -> fx_media_protect), TX_WAIT_FOREVER);
  235. #define FX_UNPROTECT tx_mutex_put(&(media_ptr -> fx_media_protect));
  236. #endif
  237. /* Define interrupt lockout constructs to protect the system date/time from being updated
  238. while they are being read. */
  239. #ifndef FX_STANDALONE_ENABLE
  240. #define FX_INT_SAVE_AREA unsigned int old_interrupt_posture;
  241. #define FX_DISABLE_INTS old_interrupt_posture = tx_interrupt_control(TX_INT_DISABLE);
  242. #define FX_RESTORE_INTS tx_interrupt_control(old_interrupt_posture);
  243. #else
  244. /* Disable use of ThreadX protection in standalone mode for FileX */
  245. #ifndef FX_LEGACY_INTERRUPT_PROTECTION
  246. #define FX_LEGACY_INTERRUPT_PROTECTION
  247. #endif
  248. #define FX_INT_SAVE_AREA
  249. #define FX_DISABLE_INTS
  250. #define FX_RESTORE_INTS
  251. #endif
  252. /* Define the error checking logic to determine if there is a caller error in the FileX API.
  253. The default definitions assume ThreadX is being used. This code can be completely turned
  254. off by just defining these macros to white space. */
  255. #ifndef FX_STANDALONE_ENABLE
  256. #ifndef TX_TIMER_PROCESS_IN_ISR
  257. #define FX_CALLER_CHECKING_EXTERNS extern TX_THREAD *_tx_thread_current_ptr; \
  258. extern TX_THREAD _tx_timer_thread; \
  259. extern volatile ULONG _tx_thread_system_state;
  260. #define FX_CALLER_CHECKING_CODE if ((_tx_thread_system_state) || \
  261. (_tx_thread_current_ptr == TX_NULL) || \
  262. (_tx_thread_current_ptr == &_tx_timer_thread)) \
  263. return(FX_CALLER_ERROR);
  264. #else
  265. #define FX_CALLER_CHECKING_EXTERNS extern TX_THREAD *_tx_thread_current_ptr; \
  266. extern volatile ULONG _tx_thread_system_state;
  267. #define FX_CALLER_CHECKING_CODE if ((_tx_thread_system_state) || \
  268. (_tx_thread_current_ptr == TX_NULL)) \
  269. return(FX_CALLER_ERROR);
  270. #endif
  271. #else
  272. #define FX_CALLER_CHECKING_EXTERNS
  273. #define FX_CALLER_CHECKING_CODE
  274. #endif
  275. /* Define the update rate of the system timer. These values may also be defined at the command
  276. line when compiling the fx_system_initialize.c module in the FileX library build. Alternatively, they can
  277. be modified in this file. Note: the update rate must be an even number of seconds greater
  278. than or equal to 2, which is the minimal update rate for FAT time. */
  279. #ifndef FX_UPDATE_RATE_IN_SECONDS
  280. #define FX_UPDATE_RATE_IN_SECONDS 10 /* Update time at 10 second intervals */
  281. #endif
  282. #ifndef FX_UPDATE_RATE_IN_TICKS
  283. #define FX_UPDATE_RATE_IN_TICKS 1000 /* Same update rate, but in ticks */
  284. #endif
  285. /* Define the version ID of FileX. This may be utilized by the application. */
  286. #ifdef FX_SYSTEM_INIT
  287. CHAR _fx_version_id[] =
  288. "Copyright (c) 2024 Microsoft Corporation. * FileX Linux/GCC Version 6.4.0 *";
  289. #else
  290. extern CHAR _fx_version_id[];
  291. #endif
  292. #endif