ff.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* This file is part of ooFatFs, a customised version of FatFs
  2. * See https://github.com/micropython/oofatfs for details
  3. */
  4. /*----------------------------------------------------------------------------/
  5. / FatFs - Generic FAT file system module R0.12b /
  6. /-----------------------------------------------------------------------------/
  7. /
  8. / Copyright (C) 2016, ChaN, all right reserved.
  9. /
  10. / FatFs module is an open source software. Redistribution and use of FatFs in
  11. / source and binary forms, with or without modification, are permitted provided
  12. / that the following condition is met:
  13. / 1. Redistributions of source code must retain the above copyright notice,
  14. / this condition and the following disclaimer.
  15. /
  16. / This software is provided by the copyright holder and contributors "AS IS"
  17. / and any warranties related to this software are DISCLAIMED.
  18. / The copyright owner or contributors be NOT LIABLE for any damages caused
  19. / by use of this software.
  20. /----------------------------------------------------------------------------*/
  21. #ifndef _FATFS
  22. #define _FATFS 68020 /* Revision ID */
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #include <stdint.h>
  27. /* This type MUST be 8-bit */
  28. typedef uint8_t BYTE;
  29. /* These types MUST be 16-bit */
  30. typedef int16_t SHORT;
  31. typedef uint16_t WORD;
  32. typedef uint16_t WCHAR;
  33. /* These types MUST be 16-bit or 32-bit */
  34. typedef int INT;
  35. typedef unsigned int UINT;
  36. /* These types MUST be 32-bit */
  37. typedef int32_t LONG;
  38. typedef uint32_t DWORD;
  39. /* This type MUST be 64-bit (Remove this for C89 compatibility) */
  40. typedef uint64_t QWORD;
  41. #include FFCONF_H /* FatFs configuration options */
  42. #if _FATFS != _FFCONF
  43. #error Wrong configuration file (ffconf.h).
  44. #endif
  45. /* Definitions of volume management */
  46. #if _MULTI_PARTITION /* Multiple partition configuration */
  47. #define LD2PT(fs) (fs->part) /* Get partition index */
  48. #else /* Single partition configuration */
  49. #define LD2PT(fs) 0 /* Find first valid partition or in SFD */
  50. #endif
  51. /* Type of path name strings on FatFs API */
  52. #if _LFN_UNICODE /* Unicode (UTF-16) string */
  53. #if _USE_LFN == 0
  54. #error _LFN_UNICODE must be 0 at non-LFN cfg.
  55. #endif
  56. #ifndef _INC_TCHAR
  57. typedef WCHAR TCHAR;
  58. #define _T(x) L ## x
  59. #define _TEXT(x) L ## x
  60. #endif
  61. #else /* ANSI/OEM string */
  62. #ifndef _INC_TCHAR
  63. typedef char TCHAR;
  64. #define _T(x) x
  65. #define _TEXT(x) x
  66. #endif
  67. #endif
  68. /* Type of file size variables */
  69. #if _FS_EXFAT
  70. #if _USE_LFN == 0
  71. #error LFN must be enabled when enable exFAT
  72. #endif
  73. typedef QWORD FSIZE_t;
  74. #else
  75. typedef DWORD FSIZE_t;
  76. #endif
  77. /* File system object structure (FATFS) */
  78. typedef struct {
  79. void *drv; // block device underlying this filesystem
  80. #if _MULTI_PARTITION /* Multiple partition configuration */
  81. BYTE part; // Partition: 0:Auto detect, 1-4:Forced partition
  82. #endif
  83. BYTE fs_type; /* File system type (0:N/A) */
  84. BYTE n_fats; /* Number of FATs (1 or 2) */
  85. BYTE wflag; /* win[] flag (b0:dirty) */
  86. BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */
  87. WORD id; /* File system mount ID */
  88. WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
  89. WORD csize; /* Cluster size [sectors] */
  90. #if _MAX_SS != _MIN_SS
  91. WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */
  92. #endif
  93. #if _USE_LFN != 0
  94. WCHAR* lfnbuf; /* LFN working buffer */
  95. #endif
  96. #if _FS_EXFAT
  97. BYTE* dirbuf; /* Directory entry block scratchpad buffer */
  98. #endif
  99. #if _FS_REENTRANT
  100. _SYNC_t sobj; /* Identifier of sync object */
  101. #endif
  102. #if !_FS_READONLY
  103. DWORD last_clst; /* Last allocated cluster */
  104. DWORD free_clst; /* Number of free clusters */
  105. #endif
  106. #if _FS_RPATH != 0
  107. DWORD cdir; /* Current directory start cluster (0:root) */
  108. #if _FS_EXFAT
  109. DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */
  110. DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */
  111. DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */
  112. #endif
  113. #endif
  114. DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */
  115. DWORD fsize; /* Size of an FAT [sectors] */
  116. DWORD volbase; /* Volume base sector */
  117. DWORD fatbase; /* FAT base sector */
  118. DWORD dirbase; /* Root directory base sector/cluster */
  119. DWORD database; /* Data base sector */
  120. DWORD winsect; /* Current sector appearing in the win[] */
  121. BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
  122. } FATFS;
  123. /* Object ID and allocation information (_FDID) */
  124. typedef struct {
  125. FATFS* fs; /* Pointer to the owner file system object */
  126. WORD id; /* Owner file system mount ID */
  127. BYTE attr; /* Object attribute */
  128. BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous (no data on FAT), =3:got flagmented, b2:sub-directory stretched) */
  129. DWORD sclust; /* Object start cluster (0:no cluster or root directory) */
  130. FSIZE_t objsize; /* Object size (valid when sclust != 0) */
  131. #if _FS_EXFAT
  132. DWORD n_cont; /* Size of coutiguous part, clusters - 1 (valid when stat == 3) */
  133. DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */
  134. DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */
  135. DWORD c_ofs; /* Offset in the containing directory (valid when sclust != 0) */
  136. #endif
  137. #if _FS_LOCK != 0
  138. UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
  139. #endif
  140. } _FDID;
  141. /* File object structure (FIL) */
  142. typedef struct {
  143. _FDID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */
  144. BYTE flag; /* File status flags */
  145. BYTE err; /* Abort flag (error code) */
  146. FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */
  147. DWORD clust; /* Current cluster of fpter (invalid when fprt is 0) */
  148. DWORD sect; /* Sector number appearing in buf[] (0:invalid) */
  149. #if !_FS_READONLY
  150. DWORD dir_sect; /* Sector number containing the directory entry */
  151. BYTE* dir_ptr; /* Pointer to the directory entry in the win[] */
  152. #endif
  153. #if _USE_FASTSEEK
  154. DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */
  155. #endif
  156. #if !_FS_TINY
  157. BYTE buf[_MAX_SS]; /* File private data read/write window */
  158. #endif
  159. } FIL;
  160. /* Directory object structure (FF_DIR) */
  161. typedef struct {
  162. _FDID obj; /* Object identifier */
  163. DWORD dptr; /* Current read/write offset */
  164. DWORD clust; /* Current cluster */
  165. DWORD sect; /* Current sector */
  166. BYTE* dir; /* Pointer to the directory item in the win[] */
  167. BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */
  168. #if _USE_LFN != 0
  169. DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */
  170. #endif
  171. #if _USE_FIND
  172. const TCHAR* pat; /* Pointer to the name matching pattern */
  173. #endif
  174. } FF_DIR;
  175. /* File information structure (FILINFO) */
  176. typedef struct {
  177. FSIZE_t fsize; /* File size */
  178. WORD fdate; /* Modified date */
  179. WORD ftime; /* Modified time */
  180. BYTE fattrib; /* File attribute */
  181. #if _USE_LFN != 0
  182. TCHAR altname[13]; /* Altenative file name */
  183. TCHAR fname[_MAX_LFN + 1]; /* Primary file name */
  184. #else
  185. TCHAR fname[13]; /* File name */
  186. #endif
  187. } FILINFO;
  188. /* File function return code (FRESULT) */
  189. typedef enum {
  190. FR_OK = 0, /* (0) Succeeded */
  191. FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
  192. FR_INT_ERR, /* (2) Assertion failed */
  193. FR_NOT_READY, /* (3) The physical drive cannot work */
  194. FR_NO_FILE, /* (4) Could not find the file */
  195. FR_NO_PATH, /* (5) Could not find the path */
  196. FR_INVALID_NAME, /* (6) The path name format is invalid */
  197. FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
  198. FR_EXIST, /* (8) Access denied due to prohibited access */
  199. FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
  200. FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
  201. FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
  202. FR_NOT_ENABLED, /* (12) The volume has no work area */
  203. FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
  204. FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
  205. FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
  206. FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
  207. FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
  208. FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_LOCK */
  209. FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
  210. } FRESULT;
  211. /*--------------------------------------------------------------*/
  212. /* FatFs module application interface */
  213. FRESULT f_open (FATFS *fs, FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
  214. FRESULT f_close (FIL* fp); /* Close an open file object */
  215. FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */
  216. FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */
  217. FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */
  218. FRESULT f_truncate (FIL* fp); /* Truncate the file */
  219. FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */
  220. FRESULT f_opendir (FATFS *fs, FF_DIR* dp, const TCHAR* path); /* Open a directory */
  221. FRESULT f_closedir (FF_DIR* dp); /* Close an open directory */
  222. FRESULT f_readdir (FF_DIR* dp, FILINFO* fno); /* Read a directory item */
  223. FRESULT f_findfirst (FF_DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */
  224. FRESULT f_findnext (FF_DIR* dp, FILINFO* fno); /* Find next file */
  225. FRESULT f_mkdir (FATFS *fs, const TCHAR* path); /* Create a sub directory */
  226. FRESULT f_unlink (FATFS *fs, const TCHAR* path); /* Delete an existing file or directory */
  227. FRESULT f_rename (FATFS *fs, const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
  228. FRESULT f_stat (FATFS *fs, const TCHAR* path, FILINFO* fno); /* Get file status */
  229. FRESULT f_chmod (FATFS *fs, const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */
  230. FRESULT f_utime (FATFS *fs, const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */
  231. FRESULT f_chdir (FATFS *fs, const TCHAR* path); /* Change current directory */
  232. FRESULT f_getcwd (FATFS *fs, TCHAR* buff, UINT len); /* Get current directory */
  233. FRESULT f_getfree (FATFS *fs, DWORD* nclst); /* Get number of free clusters on the drive */
  234. FRESULT f_getlabel (FATFS *fs, TCHAR* label, DWORD* vsn); /* Get volume label */
  235. FRESULT f_setlabel (FATFS *fs, const TCHAR* label); /* Set volume label */
  236. FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
  237. FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt); /* Allocate a contiguous block to the file */
  238. FRESULT f_mount (FATFS* fs); /* Mount/Unmount a logical drive */
  239. FRESULT f_umount (FATFS* fs); /* Unmount a logical drive */
  240. FRESULT f_mkfs (FATFS *fs, BYTE opt, DWORD au, void* work, UINT len); /* Create a FAT volume */
  241. FRESULT f_fdisk (void *pdrv, const DWORD* szt, void* work); /* Divide a physical drive into some partitions */
  242. #define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
  243. #define f_error(fp) ((fp)->err)
  244. #define f_tell(fp) ((fp)->fptr)
  245. #define f_size(fp) ((fp)->obj.objsize)
  246. #define f_rewind(fp) f_lseek((fp), 0)
  247. #define f_rewinddir(dp) f_readdir((dp), 0)
  248. #ifndef EOF
  249. #define EOF (-1)
  250. #endif
  251. /*--------------------------------------------------------------*/
  252. /* Additional user defined functions */
  253. /* RTC function */
  254. #if !_FS_READONLY && !_FS_NORTC
  255. DWORD get_fattime (void);
  256. #endif
  257. /* Unicode support functions */
  258. #if _USE_LFN != 0 /* Unicode - OEM code conversion */
  259. WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */
  260. WCHAR ff_wtoupper (WCHAR chr); /* Unicode upper-case conversion */
  261. #if _USE_LFN == 3 /* Memory functions */
  262. void* ff_memalloc (UINT msize); /* Allocate memory block */
  263. void ff_memfree (void* mblock); /* Free memory block */
  264. #endif
  265. #endif
  266. /* Sync functions */
  267. #if _FS_REENTRANT
  268. int ff_cre_syncobj (FATFS *fatfs, _SYNC_t* sobj); /* Create a sync object */
  269. int ff_req_grant (_SYNC_t sobj); /* Lock sync object */
  270. void ff_rel_grant (_SYNC_t sobj); /* Unlock sync object */
  271. int ff_del_syncobj (_SYNC_t sobj); /* Delete a sync object */
  272. #endif
  273. /*--------------------------------------------------------------*/
  274. /* Flags and offset address */
  275. /* File access mode and open method flags (3rd argument of f_open) */
  276. #define FA_READ 0x01
  277. #define FA_WRITE 0x02
  278. #define FA_OPEN_EXISTING 0x00
  279. #define FA_CREATE_NEW 0x04
  280. #define FA_CREATE_ALWAYS 0x08
  281. #define FA_OPEN_ALWAYS 0x10
  282. #define FA_OPEN_APPEND 0x30
  283. /* Fast seek controls (2nd argument of f_lseek) */
  284. #define CREATE_LINKMAP ((FSIZE_t)0 - 1)
  285. /* Format options (2nd argument of f_mkfs) */
  286. #define FM_FAT 0x01
  287. #define FM_FAT32 0x02
  288. #define FM_EXFAT 0x04
  289. #define FM_ANY 0x07
  290. #define FM_SFD 0x08
  291. /* Filesystem type (FATFS.fs_type) */
  292. #define FS_FAT12 1
  293. #define FS_FAT16 2
  294. #define FS_FAT32 3
  295. #define FS_EXFAT 4
  296. /* File attribute bits for directory entry (FILINFO.fattrib) */
  297. #define AM_RDO 0x01 /* Read only */
  298. #define AM_HID 0x02 /* Hidden */
  299. #define AM_SYS 0x04 /* System */
  300. #define AM_DIR 0x10 /* Directory */
  301. #define AM_ARC 0x20 /* Archive */
  302. #ifdef __cplusplus
  303. }
  304. #endif
  305. #endif /* _FATFS */