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