further pfusch :) as usual, see difflog for details
[ppcskel.git] / ff.h
1 /*---------------------------------------------------------------------------/
2 /  FatFs - FAT file system module include file  R0.07        (C)ChaN, 2009
3 /----------------------------------------------------------------------------/
4 / FatFs module is an open source project to implement FAT file system to small
5 / embedded systems. It is opened for education, research and development under
6 / license policy of following trems.
7 /
8 /  Copyright (C) 2009, ChaN, all right reserved.
9 /
10 / * The FatFs module is a free software and there is no warranty.
11 / * You can use, modify and/or redistribute it for personal, non-profit or
12 /   commercial use without any restriction under your responsibility.
13 / * Redistributions of source code must retain the above copyright notice.
14 /
15 /----------------------------------------------------------------------------*/
16
17 #include "types.h"
18
19 /*---------------------------------------------------------------------------/
20 / FatFs Configuration Options
21 /
22 / CAUTION! Do not forget to make clean the project after any changes to
23 / the configuration options.
24 /
25 /----------------------------------------------------------------------------*/
26 #ifndef _FATFS
27 #define _FATFS
28
29 #define _WORD_ACCESS    0
30 /* The _WORD_ACCESS option defines which access method is used to the word
31 /  data in the FAT structure.
32 /
33 /   0: Byte-by-byte access. Always compatible with all platforms.
34 /   1: Word access. Do not choose this unless following condition is met.
35 /
36 /  When the byte order on the memory is big-endian or address miss-aligned
37 /  word access results incorrect behavior, the _WORD_ACCESS must be set to 0.
38 /  If it is not the case, the value can also be set to 1 to improve the
39 /  performance and code efficiency. */
40
41
42 #define _FS_READONLY    0
43 /* Setting _FS_READONLY to 1 defines read only configuration. This removes
44 /  writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename,
45 /  f_truncate and useless f_getfree. */
46
47
48 #define _FS_MINIMIZE    0
49 /* The _FS_MINIMIZE option defines minimization level to remove some functions.
50 /
51 /   0: Full function.
52 /   1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename
53 /      are removed.
54 /   2: f_opendir and f_readdir are removed in addition to level 1.
55 /   3: f_lseek is removed in addition to level 2. */
56
57
58 #define _FS_TINY        0
59 /* When _FS_TINY is set to 1, FatFs uses the sector buffer in the file system
60 /  object instead of the sector buffer in the individual file object for file
61 /  data transfer. This reduces memory consumption 512 bytes each file object. */
62
63
64 #define _DRIVES         1
65 /* Number of volumes (logical drives) to be used. */
66
67
68 #define _USE_STRFUNC    0
69 /* To enable string functions, set _USE_STRFUNC to 1 or 2. */
70
71
72 #define _USE_MKFS       1
73 /* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */
74
75
76 #define _USE_FORWARD    0
77 /* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */
78
79
80 #define _USE_LFN        0
81 #define _MAX_LFN        255             /* Maximum LFN length to handle (max:255) */
82 /* The _USE_LFN option switches the LFN support.
83 /
84 /   0: Disable LFN.
85 /   1: Enable LFN with static working buffer on the bss. Not re-entrant.
86 /   2: Enable LFN with dynamic working buffer on the caller's 'stack'.
87 /
88 /  The working buffer occupies (_MAX_LFN + 1) * 2 bytes. When enable LFN,
89 /  a Unicode - OEM code conversion function ff_convert() must be linked. */
90
91
92 #define _CODE_PAGE      437
93 /* The _CODE_PAGE specifies the OEM code page to be used on the target system.
94 /  When it is non LFN configuration, there is no difference between SBCS code
95 /  pages. When LFN is enabled, the code page must always be set correctly.
96 /   437 - U.S.
97 /   720 - Arabic
98 /   737 - Greek
99 /   775 - Baltic
100 /   850 - Multilingual Latin 1
101 /   852 - Latin 2
102 /   855 - Cyrillic
103 /   857 - Turkish
104 /   858 - Multilingual Latin 1 + Euro
105 /   862 - Hebrew
106 /   866 - Russian
107 /   874 - Thai
108 /   932 - Japanese Shift-JIS       (DBCS)
109 /   936 - Simplified Chinese GBK   (DBCS)
110 /   949 - Korean                   (DBCS)
111 /   950 - Traditional Chinese Big5 (DBCS)
112 /   1258 - Vietnam
113 */
114
115
116 #define _MULTI_PARTITION        0
117 /* When _MULTI_PARTITION is set to 0, each volume is bound to same physical
118 / drive number and can mount only 1st primaly partition. When it is set to 1,
119 / each volume is tied to the partition listed in Drives[]. */
120
121
122 #define _FS_REENTRANT   0
123 #define _TIMEOUT                1000
124 /* To make the FatFs module re-entrant, set 1 and re-write platform dependent
125 /  lock out code that defined arownd _FS_REENTRANT. The _TIMEOUT defines the
126 /  time out value in unit of milliseconds on the multi access exclusion. */
127
128
129 #define _EXCLUDE_LIB    0
130 /* When _EXCLUDE_LIB is set to 1, FatFs module does not use standard library. */
131
132
133
134 /* End of configuration options. Do not change followings without care.     */
135 /*--------------------------------------------------------------------------*/
136
137
138
139 /* Definitions corresponds to multiple sector size (Not tested) */
140
141 #define MAX_SS  512U    /* Do not change */
142 #if MAX_SS > 512U
143 #define SS(fs)  ((fs)->s_size)
144 #else
145 #define SS(fs)  512U
146 #endif
147
148
149
150 /* File system object structure */
151
152 typedef struct _FATFS {
153         BYTE    fs_type;        /* FAT sub type */
154         BYTE    drive;          /* Physical drive number */
155         BYTE    csize;          /* Number of sectors per cluster */
156         BYTE    n_fats;         /* Number of FAT copies */
157         BYTE    wflag;          /* win[] dirty flag (1:must be written back) */
158         BYTE    pad1;
159         WORD    id;                     /* File system mount ID */
160         WORD    n_rootdir;      /* Number of root directory entries (0 on FAT32) */
161 #if _FS_REENTRANT
162         HANDLE  h_mutex;        /* Handle to the mutex (Platform dependent) */
163 #endif
164 #if MAX_SS > 512U
165         WORD    s_size;         /* Sector size */
166 #endif
167 #if !_FS_READONLY
168         BYTE    fsi_flag;       /* fsinfo dirty flag (1:must be written back) */
169         BYTE    pad2;
170         DWORD   last_clust;     /* Last allocated cluster */
171         DWORD   free_clust;     /* Number of free clusters */
172         DWORD   fsi_sector;     /* fsinfo sector */
173 #endif
174         DWORD   sects_fat;      /* Sectors per fat */
175         DWORD   max_clust;      /* Maximum cluster# + 1. Number of clusters is max_clust - 2 */
176         DWORD   fatbase;        /* FAT start sector */
177         DWORD   dirbase;        /* Root directory start sector (Cluster# on FAT32) */
178         DWORD   database;       /* Data start sector */
179         DWORD   winsect;        /* Current sector appearing in the win[] */
180         BYTE    win[MAX_SS];/* Disk access window for Directory/FAT */
181 } FATFS;
182
183
184
185 /* Directory object structure */
186
187 typedef struct _DIR {
188         WORD    id;                     /* Owner file system mount ID */
189         WORD    index;          /* Current index number */
190         FATFS*  fs;                     /* Pointer to the owner file system object */
191         DWORD   sclust;         /* Table start cluster (0:Static table) */
192         DWORD   clust;          /* Current cluster */
193         DWORD   sect;           /* Current sector */
194         BYTE*   dir;            /* Pointer to the current SFN entry in the win[] */
195         BYTE*   fn;                     /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
196 #if _USE_LFN
197         WCHAR*  lfn;            /* Pointer to the LFN working buffer */
198         WORD    lfn_idx;        /* Last matched LFN index (0xFFFF:No LFN) */
199 #endif
200 } DIR;
201
202
203
204 /* File object structure */
205
206 typedef struct _FIL {
207         FATFS*  fs;                     /* Pointer to the owner file system object */
208         WORD    id;                     /* Owner file system mount ID */
209         BYTE    flag;           /* File status flags */
210         BYTE    csect;          /* Sector address in the cluster */
211         DWORD   fptr;           /* File R/W pointer */
212         DWORD   fsize;          /* File size */
213         DWORD   org_clust;      /* File start cluster */
214         DWORD   curr_clust;     /* Current cluster */
215         DWORD   dsect;          /* Current data sector */
216 #if _FS_READONLY == 0
217         DWORD   dir_sect;       /* Sector containing the directory entry */
218         BYTE*   dir_ptr;        /* Ponter to the directory entry in the window */
219 #endif
220 #if !_FS_TINY
221         BYTE    buf[MAX_SS];/* File R/W buffer */
222 #endif
223 } FIL;
224
225
226
227 /* File status structure */
228
229 typedef struct _FILINFO {
230         DWORD fsize;            /* File size */
231         WORD fdate;                     /* Last modified date */
232         WORD ftime;                     /* Last modified time */
233         BYTE fattrib;           /* Attribute */
234         char fname[13];         /* Short file name (8.3 format) */
235 #if _USE_LFN
236         char *lfname;           /* Pointer to the LFN buffer */
237         int lfsize;                     /* Size of LFN buffer [bytes] */
238 #endif
239 } FILINFO;
240
241
242
243 /* DBCS code ranges */
244
245 #if _CODE_PAGE == 932   /* CP932 (Japanese Shift-JIS) */
246 #define _DF1S   0x81    /* DBC 1st byte range 1 start */
247 #define _DF1E   0x9F    /* DBC 1st byte range 1 end */
248 #define _DF2S   0xE0    /* DBC 1st byte range 2 start */
249 #define _DF2E   0xFC    /* DBC 1st byte range 2 end */
250 #define _DS1S   0x40    /* DBC 2nd byte range 1 start */
251 #define _DS1E   0x7E    /* DBC 2nd byte range 1 end */
252 #define _DS2S   0x80    /* DBC 2nd byte range 2 start */
253 #define _DS2E   0xFC    /* DBC 2nd byte range 2 end */
254
255 #elif _CODE_PAGE == 936 /* CP936 (Simplified Chinese GBK) */
256 #define _DF1S   0x81
257 #define _DF1E   0xFE
258 #define _DS1S   0x40
259 #define _DS1E   0x7E
260 #define _DS2S   0x80
261 #define _DS2E   0xFE
262
263 #elif _CODE_PAGE == 949 /* CP949 (Korean) */
264 #define _DF1S   0x81
265 #define _DF1E   0xFE
266 #define _DS1S   0x41
267 #define _DS1E   0x5A
268 #define _DS2S   0x61
269 #define _DS2E   0x7A
270 #define _DS3S   0x81
271 #define _DS3E   0xFE
272
273 #elif _CODE_PAGE == 950 /* CP950 (Traditional Chinese Big5) */
274 #define _DF1S   0x81
275 #define _DF1E   0xFE
276 #define _DS1S   0x40
277 #define _DS1E   0x7E
278 #define _DS2S   0xA1
279 #define _DS2E   0xFE
280
281 #else                                   /* SBCS code pages */
282 #define _DF1S   0
283
284 #endif
285
286
287
288 /* Character code support macros */
289
290 #define IsUpper(c)      (((c)>='A')&&((c)<='Z'))
291 #define IsLower(c)      (((c)>='a')&&((c)<='z'))
292 #define IsDigit(c)      (((c)>='0')&&((c)<='9'))
293
294 #if _DF1S       /* DBCS configuration */
295
296 #if _DF2S       /* Two 1st byte areas */
297 #define IsDBCS1(c)      (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
298 #else           /* One 1st byte area */
299 #define IsDBCS1(c)      ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
300 #endif
301
302 #if _DS3S       /* Three 2nd byte areas */
303 #define IsDBCS2(c)      (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
304 #else           /* Two 2nd byte areas */
305 #define IsDBCS2(c)      (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
306 #endif
307
308 #else           /* SBCS configuration */
309
310 #define IsDBCS1(c)      0
311 #define IsDBCS2(c)      0
312
313 #endif /* _DF1S */
314
315
316
317 /* Definitions corresponds to multi partition */
318
319 #if _MULTI_PARTITION            /* Multiple partition configuration */
320
321 typedef struct _PARTITION {
322         BYTE pd;        /* Physical drive# */
323         BYTE pt;        /* Partition # (0-3) */
324 } PARTITION;
325
326 extern
327 const PARTITION Drives[];                       /* Logical drive# to physical location conversion table */
328 #define LD2PD(drv) (Drives[drv].pd)     /* Get physical drive# */
329 #define LD2PT(drv) (Drives[drv].pt)     /* Get partition# */
330
331 #else                                           /* Single partition configuration */
332
333 #define LD2PD(drv) (drv)        /* Physical drive# is equal to the logical drive# */
334 #define LD2PT(drv) 0            /* Always mounts the 1st partition */
335
336 #endif
337
338
339
340 /* File function return code (FRESULT) */
341
342 typedef enum {
343         FR_OK = 0,                      /* 0 */
344         FR_DISK_ERR,            /* 1 */
345         FR_INT_ERR,                     /* 2 */
346         FR_NOT_READY,           /* 3 */
347         FR_NO_FILE,                     /* 4 */
348         FR_NO_PATH,                     /* 5 */
349         FR_INVALID_NAME,        /* 6 */
350         FR_DENIED,                      /* 7 */
351         FR_EXIST,                       /* 8 */
352         FR_INVALID_OBJECT,      /* 9 */
353         FR_WRITE_PROTECTED,     /* 10 */
354         FR_INVALID_DRIVE,       /* 11 */
355         FR_NOT_ENABLED,         /* 12 */
356         FR_NO_FILESYSTEM,       /* 13 */
357         FR_MKFS_ABORTED,        /* 14 */
358         FR_TIMEOUT                      /* 15 */
359 } FRESULT;
360
361
362
363 /*--------------------------------------------------------------*/
364 /* FatFs module application interface                           */
365
366 FRESULT f_mount (BYTE, FATFS*);                                         /* Mount/Unmount a logical drive */
367 FRESULT f_open (FIL*, const char*, BYTE);                       /* Open or create a file */
368 FRESULT f_read (FIL*, void*, UINT, UINT*);                      /* Read data from a file */
369 FRESULT f_write (FIL*, const void*, UINT, UINT*);       /* Write data to a file */
370 FRESULT f_lseek (FIL*, DWORD);                                          /* Move file pointer of a file object */
371 FRESULT f_close (FIL*);                                                         /* Close an open file object */
372 FRESULT f_opendir (DIR*, const char*);                          /* Open an existing directory */
373 FRESULT f_readdir (DIR*, FILINFO*);                                     /* Read a directory item */
374 FRESULT f_stat (const char*, FILINFO*);                         /* Get file status */
375 FRESULT f_getfree (const char*, DWORD*, FATFS**);       /* Get number of free clusters on the drive */
376 FRESULT f_truncate (FIL*);                                                      /* Truncate file */
377 FRESULT f_sync (FIL*);                                                          /* Flush cached data of a writing file */
378 FRESULT f_unlink (const char*);                                         /* Delete an existing file or directory */
379 FRESULT f_mkdir (const char*);                                          /* Create a new directory */
380 FRESULT f_chmod (const char*, BYTE, BYTE);                      /* Change attriburte of the file/dir */
381 FRESULT f_utime (const char*, const FILINFO*);          /* Change timestamp of the file/dir */
382 FRESULT f_rename (const char*, const char*);            /* Rename/Move a file or directory */
383 FRESULT f_forward (FIL*, UINT(*)(const BYTE*,UINT), UINT, UINT*);       /* Forward data to the stream */
384 FRESULT f_mkfs (BYTE, BYTE, WORD);                                      /* Create a file system on the drive */
385
386 #if _USE_STRFUNC
387 int f_putc (int, FIL*);                                                         /* Put a character to the file */
388 int f_puts (const char*, FIL*);                                         /* Put a string to the file */
389 int f_printf (FIL*, const char*, ...);                          /* Put a formatted string to the file */
390 char* f_gets (char*, int, FIL*);                                        /* Get a string from the file */
391 #define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
392 #define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
393 #ifndef EOF
394 #define EOF -1
395 #endif
396 #endif
397
398
399 /*--------------------------------------------------------------*/
400 /* User defined functions                                       */
401
402
403 /* Real time clock */
404 DWORD get_fattime (void);       /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
405                                                         /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
406
407 /* Unicode - OEM code conversion */
408 #if _USE_LFN
409 WCHAR ff_convert (WCHAR, UINT);
410 #endif
411
412 DWORD clust2sect(FATFS *fs, DWORD clust);
413 DWORD f_check_contig(FIL *fp);
414
415
416
417 /*--------------------------------------------------------------*/
418 /* Flags and offset address                                     */
419
420
421 /* File access control and file status flags (FIL.flag) */
422
423 #define FA_READ                         0x01
424 #define FA_OPEN_EXISTING        0x00
425 #if _FS_READONLY == 0
426 #define FA_WRITE                        0x02
427 #define FA_CREATE_NEW           0x04
428 #define FA_CREATE_ALWAYS        0x08
429 #define FA_OPEN_ALWAYS          0x10
430 #define FA__WRITTEN                     0x20
431 #define FA__DIRTY                       0x40
432 #endif
433 #define FA__ERROR                       0x80
434
435
436 /* FAT sub type (FATFS.fs_type) */
437
438 #define FS_FAT12        1
439 #define FS_FAT16        2
440 #define FS_FAT32        3
441
442
443 /* File attribute bits for directory entry */
444
445 #define AM_RDO  0x01    /* Read only */
446 #define AM_HID  0x02    /* Hidden */
447 #define AM_SYS  0x04    /* System */
448 #define AM_VOL  0x08    /* Volume label */
449 #define AM_LFN  0x0F    /* LFN entry */
450 #define AM_DIR  0x10    /* Directory */
451 #define AM_ARC  0x20    /* Archive */
452 #define AM_MASK 0x3F    /* Mask of defined bits */
453
454
455 /* FatFs refers the members in the FAT structures with byte offset instead
456 / of structure member because there are incompatibility of the packing option
457 / between various compilers. */
458
459 #define BS_jmpBoot                      0
460 #define BS_OEMName                      3
461 #define BPB_BytsPerSec          11
462 #define BPB_SecPerClus          13
463 #define BPB_RsvdSecCnt          14
464 #define BPB_NumFATs                     16
465 #define BPB_RootEntCnt          17
466 #define BPB_TotSec16            19
467 #define BPB_Media                       21
468 #define BPB_FATSz16                     22
469 #define BPB_SecPerTrk           24
470 #define BPB_NumHeads            26
471 #define BPB_HiddSec                     28
472 #define BPB_TotSec32            32
473 #define BS_55AA                         510
474
475 #define BS_DrvNum                       36
476 #define BS_BootSig                      38
477 #define BS_VolID                        39
478 #define BS_VolLab                       43
479 #define BS_FilSysType           54
480
481 #define BPB_FATSz32                     36
482 #define BPB_ExtFlags            40
483 #define BPB_FSVer                       42
484 #define BPB_RootClus            44
485 #define BPB_FSInfo                      48
486 #define BPB_BkBootSec           50
487 #define BS_DrvNum32                     64
488 #define BS_BootSig32            66
489 #define BS_VolID32                      67
490 #define BS_VolLab32                     71
491 #define BS_FilSysType32         82
492
493 #define FSI_LeadSig                     0
494 #define FSI_StrucSig            484
495 #define FSI_Free_Count          488
496 #define FSI_Nxt_Free            492
497
498 #define MBR_Table                       446
499
500 #define DIR_Name                        0
501 #define DIR_Attr                        11
502 #define DIR_NTres                       12
503 #define DIR_CrtTime                     14
504 #define DIR_CrtDate                     16
505 #define DIR_FstClusHI           20
506 #define DIR_WrtTime                     22
507 #define DIR_WrtDate                     24
508 #define DIR_FstClusLO           26
509 #define DIR_FileSize            28
510 #define LDIR_Ord                        0
511 #define LDIR_Attr                       11
512 #define LDIR_Type                       12
513 #define LDIR_Chksum                     13
514 #define LDIR_FstClusLO          26
515
516
517
518 /*--------------------------------*/
519 /* Multi-byte word access macros  */
520
521 #if _WORD_ACCESS == 1   /* Enable word access to the FAT structure */
522 #define LD_WORD(ptr)            (WORD)(*(WORD*)(BYTE*)(ptr))
523 #define LD_DWORD(ptr)           (DWORD)(*(DWORD*)(BYTE*)(ptr))
524 #define ST_WORD(ptr,val)        *(WORD*)(BYTE*)(ptr)=(WORD)(val)
525 #define ST_DWORD(ptr,val)       *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
526 #else                                   /* Use byte-by-byte access to the FAT structure */
527 #define LD_WORD(ptr)            (WORD)(((WORD)*(volatile BYTE*)((ptr)+1)<<8)|(WORD)*(volatile BYTE*)(ptr))
528 #define LD_DWORD(ptr)           (DWORD)(((DWORD)*(volatile BYTE*)((ptr)+3)<<24)|((DWORD)*(volatile BYTE*)((ptr)+2)<<16)|((WORD)*(volatile BYTE*)((ptr)+1)<<8)|*(volatile BYTE*)(ptr))
529 #define ST_WORD(ptr,val)        *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8)
530 #define ST_DWORD(ptr,val)       *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8); *(volatile BYTE*)((ptr)+2)=(BYTE)((DWORD)(val)>>16); *(volatile BYTE*)((ptr)+3)=(BYTE)((DWORD)(val)>>24)
531 #endif
532
533
534 #endif /* _FATFS */