ff.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*---------------------------------------------------------------------------/
  2. / FatFs - FAT file system module include file R0.09b (C)ChaN, 2013
  3. /----------------------------------------------------------------------------/
  4. / FatFs module is a generic FAT file system module for small embedded systems.
  5. / This is a free software that opened for education, research and commercial
  6. / developments under license policy of following terms.
  7. /
  8. / Copyright (C) 2013, ChaN, all right reserved.
  9. /
  10. / * The FatFs module is a free software and there is NO WARRANTY.
  11. / * No restriction on use. You can use, modify and redistribute it for
  12. / personal, non-profit or commercial product UNDER YOUR RESPONSIBILITY.
  13. / * Redistributions of source code must retain the above copyright notice.
  14. /
  15. /----------------------------------------------------------------------------*/
  16. #ifndef _FATFS
  17. #define _FATFS 82786 /* Revision ID */
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "integer.h" /* Basic integer types */
  22. #include "ffconf.h" /* FatFs configuration options */
  23. #include "tusb_config.h"
  24. #if _FATFS != _FFCONF
  25. #error Wrong configuration file (ffconf.h).
  26. #endif
  27. /* Definitions of volume management */
  28. #if _MULTI_PARTITION /* Multiple partition configuration */
  29. typedef struct {
  30. BYTE pd; /* Physical drive number */
  31. BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
  32. } PARTITION;
  33. extern PARTITION VolToPart[]; /* Volume - Partition resolution table */
  34. #define LD2PD(vol) (VolToPart[vol].pd) /* Get physical drive number */
  35. #define LD2PT(vol) (VolToPart[vol].pt) /* Get partition index */
  36. #else /* Single partition configuration */
  37. #define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */
  38. #define LD2PT(vol) 0 /* Always mounts the 1st partition or in SFD */
  39. #endif
  40. /* Type of path name strings on FatFs API */
  41. #if _LFN_UNICODE /* Unicode string */
  42. #if !_USE_LFN
  43. #error _LFN_UNICODE must be 0 in non-LFN cfg.
  44. #endif
  45. #ifndef _INC_TCHAR
  46. typedef WCHAR TCHAR;
  47. #define _T(x) L ## x
  48. #define _TEXT(x) L ## x
  49. #endif
  50. #else /* ANSI/OEM string */
  51. #ifndef _INC_TCHAR
  52. typedef char TCHAR;
  53. #define _T(x) x
  54. #define _TEXT(x) x
  55. #endif
  56. #endif
  57. /* File system object structure (FATFS) */
  58. typedef struct {
  59. BYTE fs_type; /* FAT sub-type (0:Not mounted) */
  60. BYTE drv; /* Physical drive number */
  61. BYTE csize; /* Sectors per cluster (1,2,4...128) */
  62. BYTE n_fats; /* Number of FAT copies (1,2) */
  63. BYTE wflag; /* win[] dirty flag (1:must be written back) */
  64. BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */
  65. WORD id; /* File system mount ID */
  66. WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
  67. #if _MAX_SS != 512
  68. WORD ssize; /* Bytes per sector (512, 1024, 2048 or 4096) */
  69. #endif
  70. #if _FS_REENTRANT
  71. _SYNC_t sobj; /* Identifier of sync object */
  72. #endif
  73. #if !_FS_READONLY
  74. DWORD last_clust; /* Last allocated cluster */
  75. DWORD free_clust; /* Number of free clusters */
  76. DWORD fsi_sector; /* fsinfo sector (FAT32) */
  77. #endif
  78. #if _FS_RPATH
  79. DWORD cdir; /* Current directory start cluster (0:root) */
  80. #endif
  81. DWORD n_fatent; /* Number of FAT entries (= number of clusters + 2) */
  82. DWORD fsize; /* Sectors per FAT */
  83. DWORD volbase; /* Volume start sector */
  84. DWORD fatbase; /* FAT start sector */
  85. DWORD dirbase; /* Root directory start sector (FAT32:Cluster#) */
  86. DWORD database; /* Data start sector */
  87. DWORD winsect; /* Current sector appearing in the win[] */
  88. BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and Data on tiny cfg) */
  89. } FATFS;
  90. /* File object structure (FIL) */
  91. typedef struct {
  92. FATFS* fs; /* Pointer to the related file system object (**do not change order**) */
  93. WORD id; /* Owner file system mount ID (**do not change order**) */
  94. BYTE flag; /* File status flags */
  95. BYTE pad1;
  96. DWORD fptr; /* File read/write pointer (0ed on file open) */
  97. DWORD fsize; /* File size */
  98. DWORD sclust; /* File data start cluster (0:no data cluster, always 0 when fsize is 0) */
  99. DWORD clust; /* Current cluster of fpter */
  100. DWORD dsect; /* Current data sector of fpter */
  101. #if !_FS_READONLY
  102. DWORD dir_sect; /* Sector containing the directory entry */
  103. BYTE* dir_ptr; /* Pointer to the directory entry in the window */
  104. #endif
  105. #if _USE_FASTSEEK
  106. DWORD* cltbl; /* Pointer to the cluster link map table (null on file open) */
  107. #endif
  108. #if _FS_LOCK
  109. UINT lockid; /* File lock ID (index of file semaphore table Files[]) */
  110. #endif
  111. #if !_FS_TINY
  112. BYTE buf[_MAX_SS]; /* File data read/write buffer */
  113. #endif
  114. } FIL;
  115. /* Directory object structure (DIR) */
  116. typedef struct {
  117. FATFS* fs; /* Pointer to the owner file system object (**do not change order**) */
  118. WORD id; /* Owner file system mount ID (**do not change order**) */
  119. WORD index; /* Current read/write index number */
  120. DWORD sclust; /* Table start cluster (0:Root dir) */
  121. DWORD clust; /* Current cluster */
  122. DWORD sect; /* Current sector */
  123. BYTE* dir; /* Pointer to the current SFN entry in the win[] */
  124. BYTE* fn; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
  125. #if _USE_LFN
  126. WCHAR* lfn; /* Pointer to the LFN working buffer */
  127. WORD lfn_idx; /* Last matched LFN index number (0xFFFF:No LFN) */
  128. #endif
  129. } DIR;
  130. /* File status structure (FILINFO) */
  131. typedef struct {
  132. DWORD fsize; /* File size */
  133. WORD fdate; /* Last modified date */
  134. WORD ftime; /* Last modified time */
  135. BYTE fattrib; /* Attribute */
  136. TCHAR fname[13]; /* Short file name (8.3 format) */
  137. #if _USE_LFN
  138. TCHAR* lfname; /* Pointer to the LFN buffer */
  139. UINT lfsize; /* Size of LFN buffer in TCHAR */
  140. #endif
  141. } FILINFO;
  142. /* File function return code (FRESULT) */
  143. typedef enum {
  144. FR_OK = 0, /* (0) Succeeded */
  145. FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
  146. FR_INT_ERR, /* (2) Assertion failed */
  147. FR_NOT_READY, /* (3) The physical drive cannot work */
  148. FR_NO_FILE, /* (4) Could not find the file */
  149. FR_NO_PATH, /* (5) Could not find the path */
  150. FR_INVALID_NAME, /* (6) The path name format is invalid */
  151. FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
  152. FR_EXIST, /* (8) Access denied due to prohibited access */
  153. FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
  154. FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
  155. FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
  156. FR_NOT_ENABLED, /* (12) The volume has no work area */
  157. FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
  158. FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */
  159. FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
  160. FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
  161. FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
  162. FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_SHARE */
  163. FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
  164. } FRESULT;
  165. /*--------------------------------------------------------------*/
  166. /* FatFs module application interface */
  167. FRESULT f_mount (BYTE vol, FATFS* fs); /* Mount/Unmount a logical drive */
  168. FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
  169. FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from a file */
  170. FRESULT f_lseek (FIL* fp, DWORD ofs); /* Move file pointer of a file object */
  171. FRESULT f_close (FIL* fp); /* Close an open file object */
  172. FRESULT f_opendir (DIR* dj, const TCHAR* path); /* Open an existing directory */
  173. FRESULT f_readdir (DIR* dj, FILINFO* fno); /* Read a directory item */
  174. FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
  175. FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to a file */
  176. FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
  177. FRESULT f_truncate (FIL* fp); /* Truncate file */
  178. FRESULT f_sync (FIL* fp); /* Flush cached data of a writing file */
  179. FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
  180. FRESULT f_mkdir (const TCHAR* path); /* Create a new directory */
  181. FRESULT f_chmod (const TCHAR* path, BYTE value, BYTE mask); /* Change attribute of the file/dir */
  182. FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change times-tamp of the file/dir */
  183. FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
  184. FRESULT f_chdrive (BYTE drv); /* Change current drive */
  185. BYTE f_get_current_drive(void);
  186. FRESULT f_chdir (const TCHAR* path); /* Change current directory */
  187. FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
  188. FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* sn); /* Get volume label */
  189. FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
  190. FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
  191. FRESULT f_mkfs (BYTE vol, BYTE sfd, UINT au); /* Create a file system on the drive */
  192. FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work); /* Divide a physical drive into some partitions */
  193. int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
  194. int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */
  195. int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
  196. TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
  197. #define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
  198. #define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
  199. #define f_tell(fp) ((fp)->fptr)
  200. #define f_size(fp) ((fp)->fsize)
  201. #ifndef EOF
  202. #define EOF (-1)
  203. #endif
  204. /*--------------------------------------------------------------*/
  205. /* Additional user defined functions */
  206. /* RTC function */
  207. #if !_FS_READONLY
  208. DWORD get_fattime (void);
  209. #endif
  210. /* Unicode support functions */
  211. #if _USE_LFN /* Unicode - OEM code conversion */
  212. WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */
  213. WCHAR ff_wtoupper (WCHAR chr); /* Unicode upper-case conversion */
  214. #if _USE_LFN == 3 /* Memory functions */
  215. void* ff_memalloc (UINT msize); /* Allocate memory block */
  216. void ff_memfree (void* mblock); /* Free memory block */
  217. #endif
  218. #endif
  219. /* Sync functions */
  220. #if _FS_REENTRANT
  221. int ff_cre_syncobj (BYTE vol, _SYNC_t* sobj); /* Create a sync object */
  222. int ff_req_grant (_SYNC_t sobj); /* Lock sync object */
  223. void ff_rel_grant (_SYNC_t sobj); /* Unlock sync object */
  224. int ff_del_syncobj (_SYNC_t sobj); /* Delete a sync object */
  225. #endif
  226. /*--------------------------------------------------------------*/
  227. /* Flags and offset address */
  228. /* File access control and file status flags (FIL.flag) */
  229. #define FA_READ 0x01
  230. #define FA_OPEN_EXISTING 0x00
  231. #define FA__ERROR 0x80
  232. #if !_FS_READONLY
  233. #define FA_WRITE 0x02
  234. #define FA_CREATE_NEW 0x04
  235. #define FA_CREATE_ALWAYS 0x08
  236. #define FA_OPEN_ALWAYS 0x10
  237. #define FA__WRITTEN 0x20
  238. #define FA__DIRTY 0x40
  239. #endif
  240. /* FAT sub type (FATFS.fs_type) */
  241. #define FS_FAT12 1
  242. #define FS_FAT16 2
  243. #define FS_FAT32 3
  244. /* File attribute bits for directory entry */
  245. #define AM_RDO 0x01 /* Read only */
  246. #define AM_HID 0x02 /* Hidden */
  247. #define AM_SYS 0x04 /* System */
  248. #define AM_VOL 0x08 /* Volume label */
  249. #define AM_LFN 0x0F /* LFN entry */
  250. #define AM_DIR 0x10 /* Directory */
  251. #define AM_ARC 0x20 /* Archive */
  252. #define AM_MASK 0x3F /* Mask of defined bits */
  253. /* Fast seek feature */
  254. #define CREATE_LINKMAP 0xFFFFFFFF
  255. /*--------------------------------*/
  256. /* Multi-byte word access macros */
  257. #if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */
  258. #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
  259. #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
  260. #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
  261. #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
  262. #else /* Use byte-by-byte access to the FAT structure */
  263. #define LD_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
  264. #define LD_DWORD(ptr) (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
  265. #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
  266. #define ST_DWORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8); *((BYTE*)(ptr)+2)=(BYTE)((DWORD)(val)>>16); *((BYTE*)(ptr)+3)=(BYTE)((DWORD)(val)>>24)
  267. #endif
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* _FATFS */