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