ff.h 15 KB

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