ff.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*----------------------------------------------------------------------------/
  2. / FatFs - Generic FAT Filesystem module R0.16 /
  3. /-----------------------------------------------------------------------------/
  4. /
  5. / Copyright (C) 2025, ChaN, all right reserved.
  6. /
  7. / FatFs module is an open source software. Redistribution and use of FatFs in
  8. / source and binary forms, with or without modification, are permitted provided
  9. / that the following condition is met:
  10. / 1. Redistributions of source code must retain the above copyright notice,
  11. / this condition and the following disclaimer.
  12. /
  13. / This software is provided by the copyright holder and contributors "AS IS"
  14. / and any warranties related to this software are DISCLAIMED.
  15. / The copyright owner or contributors be NOT LIABLE for any damages caused
  16. / by use of this software.
  17. /
  18. /----------------------------------------------------------------------------*/
  19. #ifndef FF_DEFINED
  20. #define FF_DEFINED 80386 /* Revision ID */
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #if !defined(FFCONF_DEF)
  25. #include "ffconf.h" /* FatFs configuration options */
  26. #endif
  27. #if FF_DEFINED != FFCONF_DEF
  28. #error Wrong configuration file (ffconf.h).
  29. #endif
  30. /* Integer types used for FatFs API */
  31. #if defined(_WIN32) /* Windows VC++ (for development only) */
  32. #define FF_INTDEF 2
  33. #include <windows.h>
  34. typedef unsigned __int64 QWORD;
  35. #include <float.h>
  36. #define isnan(v) _isnan(v)
  37. #define isinf(v) (!_finite(v))
  38. #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) /* C99 or later */
  39. #define FF_INTDEF 2
  40. #include <stdint.h>
  41. typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
  42. typedef unsigned char BYTE; /* char must be 8-bit */
  43. typedef uint16_t WORD; /* 16-bit unsigned */
  44. typedef uint32_t DWORD; /* 32-bit unsigned */
  45. typedef uint64_t QWORD; /* 64-bit unsigned */
  46. typedef WORD WCHAR; /* UTF-16 code unit */
  47. #else /* Earlier than C99 */
  48. #define FF_INTDEF 1
  49. typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
  50. typedef unsigned char BYTE; /* char must be 8-bit */
  51. typedef unsigned short WORD; /* short must be 16-bit */
  52. typedef unsigned long DWORD; /* long must be 32-bit */
  53. typedef WORD WCHAR; /* UTF-16 code unit */
  54. #endif
  55. /* Type of file size and LBA variables */
  56. #if FF_FS_EXFAT
  57. #if FF_INTDEF != 2
  58. #error exFAT feature wants C99 or later
  59. #endif
  60. typedef QWORD FSIZE_t;
  61. #if FF_LBA64
  62. typedef QWORD LBA_t;
  63. #else
  64. typedef DWORD LBA_t;
  65. #endif
  66. #else
  67. #if FF_LBA64
  68. #error exFAT needs to be enabled when enable 64-bit LBA
  69. #endif
  70. typedef DWORD FSIZE_t;
  71. typedef DWORD LBA_t;
  72. #endif
  73. /* Type of path name strings on FatFs API (TCHAR) */
  74. #if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */
  75. typedef WCHAR TCHAR;
  76. #define _T(x) L ## x
  77. #define _TEXT(x) L ## x
  78. #elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */
  79. typedef char TCHAR;
  80. #define _T(x) u8 ## x
  81. #define _TEXT(x) u8 ## x
  82. #elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */
  83. typedef DWORD TCHAR;
  84. #define _T(x) U ## x
  85. #define _TEXT(x) U ## x
  86. #elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3)
  87. #error Wrong FF_LFN_UNICODE setting
  88. #else /* ANSI/OEM code in SBCS/DBCS */
  89. typedef char TCHAR;
  90. #define _T(x) x
  91. #define _TEXT(x) x
  92. #endif
  93. /* Definitions of volume management */
  94. #if FF_MULTI_PARTITION /* Multiple partition configuration */
  95. typedef struct {
  96. BYTE pd; /* Associated physical drive */
  97. BYTE pt; /* Associated partition (0:Auto detect, 1-4:Forced partition) */
  98. } PARTITION;
  99. extern PARTITION VolToPart[]; /* Volume to partition mapping table */
  100. #endif
  101. #if FF_STR_VOLUME_ID
  102. #ifndef FF_VOLUME_STRS
  103. extern const char* VolumeStr[FF_VOLUMES]; /* User defined volume ID table */
  104. #endif
  105. #endif
  106. /* Current working directory structure (FFXCWDS) */
  107. #if FF_FS_EXFAT && FF_FS_RPATH
  108. #if FF_PATH_DEPTH < 1
  109. #error FF_PATH_DEPTH must not be zero
  110. #endif
  111. typedef struct {
  112. DWORD d_scl; /* Directory start cluster (0:root dir) */
  113. DWORD d_size; /* Size of directory (b7-b0: cluster chain status) (invalid if d_scl == 0) */
  114. DWORD nxt_ofs; /* Offset of entry of next dir in this directory (invalid if last link) */
  115. } FFXCWDL;
  116. typedef struct {
  117. UINT depth; /* Current directory depth (0:root dir) */
  118. FFXCWDL tbl[FF_PATH_DEPTH + 1]; /* Directory chain of current working directory path */
  119. } FFXCWDS;
  120. #endif
  121. /* Filesystem object structure (FATFS) */
  122. typedef struct {
  123. BYTE fs_type; /* Filesystem type (0:not mounted) */
  124. BYTE pdrv; /* Physical drive that holds this volume */
  125. BYTE ldrv; /* Logical drive number (used only when FF_FS_REENTRANT) */
  126. BYTE n_fats; /* Number of FATs (1 or 2) */
  127. BYTE wflag; /* win[] status (b0:dirty) */
  128. BYTE fsi_flag; /* Allocation information control (b7:disabled, b0:dirty) */
  129. WORD id; /* Volume mount ID */
  130. WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
  131. WORD csize; /* Cluster size [sectors] */
  132. #if FF_MAX_SS != FF_MIN_SS
  133. WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */
  134. #endif
  135. #if FF_USE_LFN
  136. WCHAR* lfnbuf; /* Pointer to LFN working buffer */
  137. #endif
  138. #if !FF_FS_READONLY
  139. DWORD last_clst; /* Last allocated cluster (invalid if >=n_fatent) */
  140. DWORD free_clst; /* Number of free clusters (invalid if >=fs->n_fatent-2) */
  141. #endif
  142. #if FF_FS_RPATH
  143. DWORD cdir; /* Current directory start cluster (0:root) */
  144. #endif
  145. DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */
  146. DWORD fsize; /* Number of sectors per FAT */
  147. LBA_t winsect; /* Current sector appearing in the win[] */
  148. LBA_t volbase; /* Volume base sector */
  149. LBA_t fatbase; /* FAT base sector */
  150. LBA_t dirbase; /* Root directory base sector (FAT12/16) or cluster (FAT32/exFAT) */
  151. LBA_t database; /* Data base sector */
  152. #if FF_FS_EXFAT
  153. LBA_t bitbase; /* Allocation bitmap base sector */
  154. BYTE* dirbuf; /* Pointer to directory entry block buffer */
  155. #if FF_FS_RPATH
  156. FFXCWDS xcwds; /* Crrent working directory structure */
  157. FFXCWDS xcwds2; /* Working buffer to follow the path */
  158. #endif
  159. #endif
  160. BYTE win[FF_MAX_SS]; /* Disk access window for directory, FAT (and file data in tiny cfg) */
  161. } FATFS;
  162. /* Object ID and allocation information (FFOBJID) */
  163. typedef struct {
  164. FATFS* fs; /* Pointer to the volume holding this object */
  165. WORD id; /* Volume mount ID when this object was opened */
  166. BYTE attr; /* Object attribute */
  167. BYTE stat; /* Object chain status (exFAT: b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */
  168. DWORD sclust; /* Object data cluster (0:no data or root directory) */
  169. FSIZE_t objsize; /* Object size (valid when sclust != 0) */
  170. #if FF_FS_EXFAT
  171. DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */
  172. DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */
  173. DWORD c_scl; /* Cluster of directory holding this object (valid when sclust != 0) */
  174. DWORD c_size; /* Size of directory holding this object (b7-b0: allocation status, valid when c_scl != 0) */
  175. DWORD c_ofs; /* Offset of entry in the holding directory */
  176. #endif
  177. #if FF_FS_LOCK
  178. UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
  179. #endif
  180. } FFOBJID;
  181. /* File object structure (FIL) */
  182. typedef struct {
  183. FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */
  184. BYTE flag; /* File status flags */
  185. BYTE err; /* Abort flag (error code) */
  186. FSIZE_t fptr; /* File read/write pointer (0 on open) */
  187. DWORD clust; /* Current cluster of fptr (invalid when fptr is 0) */
  188. LBA_t sect; /* Sector number appearing in buf[] (0:invalid) */
  189. #if !FF_FS_READONLY
  190. LBA_t dir_sect; /* Sector number containing the directory entry (not used in exFAT) */
  191. BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used in exFAT) */
  192. #endif
  193. #if FF_USE_FASTSEEK
  194. DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open; set by application) */
  195. #endif
  196. #if !FF_FS_TINY
  197. BYTE buf[FF_MAX_SS]; /* File private data read/write window */
  198. #endif
  199. } FIL;
  200. /* Directory object structure (DIR) */
  201. typedef struct {
  202. FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */
  203. DWORD dptr; /* Current read/write offset */
  204. DWORD clust; /* Current cluster */
  205. LBA_t sect; /* Current sector (0:no more item to read) */
  206. BYTE* dir; /* Pointer to the directory item in the win[] in filesystem object */
  207. BYTE fn[12]; /* SFN (in/out) {body[0-7],ext[8-10],status[11]} */
  208. #if FF_USE_LFN
  209. DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:invalid) */
  210. #endif
  211. #if FF_USE_FIND
  212. const TCHAR *pat; /* Pointer to the name matching pattern */
  213. #endif
  214. } DIR;
  215. /* File/directory information structure (FILINFO) */
  216. typedef struct {
  217. FSIZE_t fsize; /* File size (invalid for directory) */
  218. WORD fdate; /* Date of file modification or directory creation */
  219. WORD ftime; /* Time of file modification or directory creation */
  220. #if FF_FS_CRTIME
  221. WORD crdate; /* Date of object createion */
  222. WORD crtime; /* Time of object createion */
  223. #endif
  224. BYTE fattrib; /* Object attribute */
  225. #if FF_USE_LFN
  226. TCHAR altname[FF_SFN_BUF + 1];/* Alternative object name */
  227. TCHAR fname[FF_LFN_BUF + 1]; /* Primary object name */
  228. #else
  229. TCHAR fname[12 + 1]; /* Object name */
  230. #endif
  231. } FILINFO;
  232. /* Format parameter structure (MKFS_PARM) used for f_mkfs() */
  233. typedef struct {
  234. BYTE fmt; /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */
  235. BYTE n_fat; /* Number of FATs */
  236. UINT align; /* Data area alignment (sector) */
  237. UINT n_root; /* Number of root directory entries */
  238. DWORD au_size; /* Cluster size (byte) */
  239. } MKFS_PARM;
  240. /* File function return code (FRESULT) */
  241. typedef enum {
  242. FR_OK = 0, /* (0) Function succeeded */
  243. FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
  244. FR_INT_ERR, /* (2) Assertion failed */
  245. FR_NOT_READY, /* (3) The physical drive does not work */
  246. FR_NO_FILE, /* (4) Could not find the file */
  247. FR_NO_PATH, /* (5) Could not find the path */
  248. FR_INVALID_NAME, /* (6) The path name format is invalid */
  249. FR_DENIED, /* (7) Access denied due to a prohibited access or directory full */
  250. FR_EXIST, /* (8) Access denied due to a prohibited access */
  251. FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
  252. FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
  253. FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
  254. FR_NOT_ENABLED, /* (12) The volume has no work area */
  255. FR_NO_FILESYSTEM, /* (13) Could not find a valid FAT volume */
  256. FR_MKFS_ABORTED, /* (14) The f_mkfs function aborted due to some problem */
  257. FR_TIMEOUT, /* (15) Could not take control of the volume within defined period */
  258. FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
  259. FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated, given buffer size is insufficient or too deep path */
  260. FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
  261. FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
  262. } FRESULT;
  263. /*--------------------------------------------------------------*/
  264. /* FatFs Module Application Interface */
  265. /*--------------------------------------------------------------*/
  266. FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
  267. FRESULT f_close (FIL* fp); /* Close an open file object */
  268. FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */
  269. FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */
  270. FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */
  271. FRESULT f_truncate (FIL* fp); /* Truncate the file */
  272. FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */
  273. FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */
  274. FRESULT f_closedir (DIR* dp); /* Close an open directory */
  275. FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
  276. FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */
  277. FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */
  278. FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */
  279. FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
  280. FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
  281. FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
  282. FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */
  283. FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */
  284. FRESULT f_chdir (const TCHAR* path); /* Change current directory */
  285. FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
  286. FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
  287. FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
  288. FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
  289. FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
  290. FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
  291. FRESULT f_expand (FIL* fp, FSIZE_t fsz, BYTE opt); /* Allocate a contiguous block to the file */
  292. FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
  293. FRESULT f_mkfs (const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len); /* Create a FAT volume */
  294. FRESULT f_fdisk (BYTE pdrv, const LBA_t ptbl[], void* work); /* Divide a physical drive into some partitions */
  295. FRESULT f_setcp (WORD cp); /* Set current code page */
  296. int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
  297. int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */
  298. int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
  299. TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
  300. /* Some API fucntions are implemented as macro */
  301. #define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
  302. #define f_error(fp) ((fp)->err)
  303. #define f_tell(fp) ((fp)->fptr)
  304. #define f_size(fp) ((fp)->obj.objsize)
  305. #define f_rewind(fp) f_lseek((fp), 0)
  306. #define f_rewinddir(dp) f_readdir((dp), 0)
  307. #define f_rmdir(path) f_unlink(path)
  308. #define f_unmount(path) f_mount(0, path, 0)
  309. /*--------------------------------------------------------------*/
  310. /* Additional Functions */
  311. /*--------------------------------------------------------------*/
  312. /* RTC function (provided by user) */
  313. #if !FF_FS_READONLY && !FF_FS_NORTC
  314. DWORD get_fattime (void); /* Get current time */
  315. #endif
  316. /* LFN support functions (defined in ffunicode.c) */
  317. #if FF_USE_LFN >= 1
  318. WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */
  319. WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */
  320. DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */
  321. #endif
  322. /* O/S dependent functions (samples available in ffsystem.c) */
  323. #if FF_USE_LFN == 3 /* Dynamic memory allocation */
  324. void* ff_memalloc (UINT msize); /* Allocate memory block */
  325. void ff_memfree (void* mblock); /* Free memory block */
  326. #endif
  327. #if FF_FS_REENTRANT /* Sync functions */
  328. int ff_mutex_create (int vol); /* Create a sync object */
  329. void ff_mutex_delete (int vol); /* Delete a sync object */
  330. int ff_mutex_take (int vol); /* Lock sync object */
  331. void ff_mutex_give (int vol); /* Unlock sync object */
  332. #endif
  333. /*--------------------------------------------------------------*/
  334. /* Flags and Offset Address */
  335. /*--------------------------------------------------------------*/
  336. /* File access mode and open method flags (3rd argument of f_open function) */
  337. #define FA_READ 0x01
  338. #define FA_WRITE 0x02
  339. #define FA_OPEN_EXISTING 0x00
  340. #define FA_CREATE_NEW 0x04
  341. #define FA_CREATE_ALWAYS 0x08
  342. #define FA_OPEN_ALWAYS 0x10
  343. #define FA_OPEN_APPEND 0x30
  344. /* Fast seek controls (2nd argument of f_lseek function) */
  345. #define CREATE_LINKMAP ((FSIZE_t)0 - 1)
  346. /* Format options (2nd argument of f_mkfs function) */
  347. #define FM_FAT 0x01
  348. #define FM_FAT32 0x02
  349. #define FM_EXFAT 0x04
  350. #define FM_ANY 0x07
  351. #define FM_SFD 0x08
  352. /* Filesystem type (FATFS.fs_type) */
  353. #define FS_FAT12 1
  354. #define FS_FAT16 2
  355. #define FS_FAT32 3
  356. #define FS_EXFAT 4
  357. /* File attribute bits for directory entry (FILINFO.fattrib) */
  358. #define AM_RDO 0x01 /* Read only */
  359. #define AM_HID 0x02 /* Hidden */
  360. #define AM_SYS 0x04 /* System */
  361. #define AM_DIR 0x10 /* Directory */
  362. #define AM_ARC 0x20 /* Archive */
  363. #ifdef __cplusplus
  364. }
  365. #endif
  366. #endif /* FF_DEFINED */