00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _FATFS
00024 #define _FATFS
00025
00026 #include "integer.h"
00027 #include "cfg/cfg_fat.h"
00028
00029 #ifndef _WORD_ACCESS
00030 #define _WORD_ACCESS 0
00031 #endif
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef _FS_READONLY
00044 #define _FS_READONLY 0
00045 #endif
00046
00047
00048
00049
00050 #ifndef _FS_MINIMIZE
00051 #define _FS_MINIMIZE 0
00052 #endif
00053
00054
00055
00056
00057
00058
00059
00060
00061 #ifndef _FS_TINY
00062 #define _FS_TINY 0
00063 #endif
00064
00065
00066
00067
00068 #ifndef _USE_STRFUNC
00069 #define _USE_STRFUNC 0
00070 #endif
00071
00072
00073 #ifndef _USE_MKFS
00074 #define _USE_MKFS 0
00075 #endif
00076
00077
00078
00079 #ifndef _USE_FORWARD
00080 #define _USE_FORWARD 0
00081 #endif
00082
00083
00084
00085 #ifndef _DRIVES
00086 #define _DRIVES 1
00087 #endif
00088
00089
00090 #ifndef _MAX_SS
00091 #define _MAX_SS 512
00092 #endif
00093
00094
00095
00096 #ifndef _MULTI_PARTITION
00097 #define _MULTI_PARTITION 0
00098 #endif
00099
00100
00101
00102
00103 #ifndef _CODE_PAGE
00104 #define _CODE_PAGE 932
00105 #endif
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 #ifndef _USE_LFN
00129 #define _USE_LFN 0
00130 #endif
00131 #ifndef _MAX_LFN
00132 #define _MAX_LFN 255
00133 #endif
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 #ifndef _FS_REENTRANT
00145 #define _FS_REENTRANT 0
00146 #endif
00147 #ifndef _TIMEOUT
00148 #define _TIMEOUT 1000
00149 #endif
00150 #ifndef _SYNC_t
00151 #define _SYNC_t HANDLE
00152
00153 #endif
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 #if _MAX_SS == 512
00168 #define SS(fs) 512
00169 #else
00170 #if _MAX_SS == 1024 || _MAX_SS == 2048 || _MAX_SS == 4096
00171 #define SS(fs) ((fs)->s_size)
00172 #else
00173 #error Sector size must be 512, 1024, 2048 or 4096.
00174 #endif
00175 #endif
00176
00177
00178
00179
00180
00181 typedef struct _FATFS {
00182 BYTE fs_type;
00183 BYTE drive;
00184 BYTE csize;
00185 BYTE n_fats;
00186 BYTE wflag;
00187 BYTE pad1;
00188 WORD id;
00189 WORD n_rootdir;
00190 #if _FS_REENTRANT
00191 _SYNC_t sobj;
00192 #endif
00193 #if _MAX_SS != 512U
00194 WORD s_size;
00195 #endif
00196 #if !_FS_READONLY
00197 BYTE fsi_flag;
00198 BYTE pad2;
00199 DWORD last_clust;
00200 DWORD free_clust;
00201 DWORD fsi_sector;
00202 #endif
00203 DWORD sects_fat;
00204 DWORD max_clust;
00205 DWORD fatbase;
00206 DWORD dirbase;
00207 DWORD database;
00208 DWORD winsect;
00209 BYTE win[_MAX_SS];
00210 } FATFS;
00211
00212
00213
00214
00215
00216 typedef struct _DIR {
00217 WORD id;
00218 WORD index;
00219 FATFS* fs;
00220 DWORD sclust;
00221 DWORD clust;
00222 DWORD sect;
00223 BYTE* dir;
00224 BYTE* fn;
00225 #if _USE_LFN
00226 WCHAR* lfn;
00227 WORD lfn_idx;
00228 #endif
00229 } DIR;
00230
00231
00232
00233
00234
00235 typedef struct _FIL {
00236 FATFS* fs;
00237 WORD id;
00238 BYTE flag;
00239 BYTE csect;
00240 DWORD fptr;
00241 DWORD fsize;
00242 DWORD org_clust;
00243 DWORD curr_clust;
00244 DWORD dsect;
00245 #if !_FS_READONLY
00246 DWORD dir_sect;
00247 BYTE* dir_ptr;
00248 #endif
00249 #if !_FS_TINY
00250 BYTE buf[_MAX_SS];
00251 #endif
00252 } FIL;
00253
00254
00255
00256
00257
00258 typedef struct _FILINFO {
00259 DWORD fsize;
00260 WORD fdate;
00261 WORD ftime;
00262 BYTE fattrib;
00263 char fname[13];
00264 #if _USE_LFN
00265 char *lfname;
00266 int lfsize;
00267 #endif
00268 } FILINFO;
00269
00270
00271
00272
00273
00274 #if _CODE_PAGE == 932
00275 #define _DF1S 0x81
00276 #define _DF1E 0x9F
00277 #define _DF2S 0xE0
00278 #define _DF2E 0xFC
00279 #define _DS1S 0x40
00280 #define _DS1E 0x7E
00281 #define _DS2S 0x80
00282 #define _DS2E 0xFC
00283
00284 #elif _CODE_PAGE == 936
00285 #define _DF1S 0x81
00286 #define _DF1E 0xFE
00287 #define _DS1S 0x40
00288 #define _DS1E 0x7E
00289 #define _DS2S 0x80
00290 #define _DS2E 0xFE
00291
00292 #elif _CODE_PAGE == 949
00293 #define _DF1S 0x81
00294 #define _DF1E 0xFE
00295 #define _DS1S 0x41
00296 #define _DS1E 0x5A
00297 #define _DS2S 0x61
00298 #define _DS2E 0x7A
00299 #define _DS3S 0x81
00300 #define _DS3E 0xFE
00301
00302 #elif _CODE_PAGE == 950
00303 #define _DF1S 0x81
00304 #define _DF1E 0xFE
00305 #define _DS1S 0x40
00306 #define _DS1E 0x7E
00307 #define _DS2S 0xA1
00308 #define _DS2E 0xFE
00309
00310 #else
00311 #define _DF1S 0
00312
00313 #endif
00314
00315
00316
00317
00318
00319 #define IsUpper(c) (((c)>='A')&&((c)<='Z'))
00320 #define IsLower(c) (((c)>='a')&&((c)<='z'))
00321 #define IsDigit(c) (((c)>='0')&&((c)<='9'))
00322
00323 #if _DF1S
00324
00325 #if _DF2S
00326 #define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
00327 #else
00328 #define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
00329 #endif
00330
00331 #if _DS3S
00332 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
00333 #else
00334 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
00335 #endif
00336
00337 #else
00338
00339 #define IsDBCS1(c) 0
00340 #define IsDBCS2(c) 0
00341
00342 #endif
00343
00344
00345
00346
00347
00348 #if _MULTI_PARTITION
00349
00350 typedef struct _PARTITION {
00351 BYTE pd;
00352 BYTE pt;
00353 } PARTITION;
00354
00355 extern
00356 const PARTITION Drives[];
00357 #define LD2PD(drv) (Drives[drv].pd)
00358 #define LD2PT(drv) (Drives[drv].pt)
00359
00360 #else
00361
00362 #define LD2PD(drv) (drv)
00363 #define LD2PT(drv) 0
00364
00365 #endif
00366
00367
00368
00369
00370
00371 typedef enum {
00372 FR_OK = 0,
00373 FR_DISK_ERR,
00374 FR_INT_ERR,
00375 FR_NOT_READY,
00376 FR_NO_FILE,
00377 FR_NO_PATH,
00378 FR_INVALID_NAME,
00379 FR_DENIED,
00380 FR_EXIST,
00381 FR_INVALID_OBJECT,
00382 FR_WRITE_PROTECTED,
00383 FR_INVALID_DRIVE,
00384 FR_NOT_ENABLED,
00385 FR_NO_FILESYSTEM,
00386 FR_MKFS_ABORTED,
00387 FR_TIMEOUT
00388 } FRESULT;
00389
00390
00391
00392
00393
00394
00395 FRESULT f_mount (BYTE, FATFS*);
00396 FRESULT f_open (FIL*, const char*, BYTE);
00397 FRESULT f_read (FIL*, void*, UINT, UINT*);
00398 FRESULT f_write (FIL*, const void*, UINT, UINT*);
00399 FRESULT f_lseek (FIL*, DWORD);
00400 FRESULT f_close (FIL*);
00401 FRESULT f_opendir (DIR*, const char*);
00402 FRESULT f_readdir (DIR*, FILINFO*);
00403 FRESULT f_stat (const char*, FILINFO*);
00404 FRESULT f_getfree (const char*, DWORD*, FATFS**);
00405 FRESULT f_truncate (FIL*);
00406 FRESULT f_sync (FIL*);
00407 FRESULT f_unlink (const char*);
00408 FRESULT f_mkdir (const char*);
00409 FRESULT f_chmod (const char*, BYTE, BYTE);
00410 FRESULT f_utime (const char*, const FILINFO*);
00411 FRESULT f_rename (const char*, const char*);
00412 FRESULT f_forward (FIL*, UINT(*)(const BYTE*,UINT), UINT, UINT*);
00413 FRESULT f_mkfs (BYTE, BYTE, WORD);
00414
00415 #if _USE_STRFUNC
00416 int f_putc (int, FIL*);
00417 int f_puts (const char*, FIL*);
00418 int f_printf (FIL*, const char*, ...);
00419 char* f_gets (char*, int, FIL*);
00420 #define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
00421 #define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
00422 #ifndef EOF
00423 #define EOF -1
00424 #endif
00425 #endif
00426
00427
00428
00429
00430
00431
00432
00433 #if !_FS_READONLY
00434 DWORD get_fattime (void);
00435
00436 #endif
00437
00438
00439 #if _USE_LFN
00440 WCHAR ff_convert (WCHAR, UINT);
00441 #endif
00442
00443
00444 #if _FS_REENTRANT
00445 BOOL ff_cre_syncobj(BYTE, _SYNC_t*);
00446 BOOL ff_del_syncobj(_SYNC_t);
00447 BOOL ff_req_grant(_SYNC_t);
00448 void ff_rel_grant(_SYNC_t);
00449 #endif
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459 #define FA_READ 0x01
00460 #define FA_OPEN_EXISTING 0x00
00461 #if _FS_READONLY == 0
00462 #define FA_WRITE 0x02
00463 #define FA_CREATE_NEW 0x04
00464 #define FA_CREATE_ALWAYS 0x08
00465 #define FA_OPEN_ALWAYS 0x10
00466 #define FA__WRITTEN 0x20
00467 #define FA__DIRTY 0x40
00468 #endif
00469 #define FA__ERROR 0x80
00470
00471
00472
00473
00474 #define FS_FAT12 1
00475 #define FS_FAT16 2
00476 #define FS_FAT32 3
00477
00478
00479
00480
00481 #define AM_RDO 0x01
00482 #define AM_HID 0x02
00483 #define AM_SYS 0x04
00484 #define AM_VOL 0x08
00485 #define AM_LFN 0x0F
00486 #define AM_DIR 0x10
00487 #define AM_ARC 0x20
00488 #define AM_MASK 0x3F
00489
00490
00491
00492
00493
00494
00495 #define BS_jmpBoot 0
00496 #define BS_OEMName 3
00497 #define BPB_BytsPerSec 11
00498 #define BPB_SecPerClus 13
00499 #define BPB_RsvdSecCnt 14
00500 #define BPB_NumFATs 16
00501 #define BPB_RootEntCnt 17
00502 #define BPB_TotSec16 19
00503 #define BPB_Media 21
00504 #define BPB_FATSz16 22
00505 #define BPB_SecPerTrk 24
00506 #define BPB_NumHeads 26
00507 #define BPB_HiddSec 28
00508 #define BPB_TotSec32 32
00509 #define BS_55AA 510
00510
00511 #define BS_DrvNum 36
00512 #define BS_BootSig 38
00513 #define BS_VolID 39
00514 #define BS_VolLab 43
00515 #define BS_FilSysType 54
00516
00517 #define BPB_FATSz32 36
00518 #define BPB_ExtFlags 40
00519 #define BPB_FSVer 42
00520 #define BPB_RootClus 44
00521 #define BPB_FSInfo 48
00522 #define BPB_BkBootSec 50
00523 #define BS_DrvNum32 64
00524 #define BS_BootSig32 66
00525 #define BS_VolID32 67
00526 #define BS_VolLab32 71
00527 #define BS_FilSysType32 82
00528
00529 #define FSI_LeadSig 0
00530 #define FSI_StrucSig 484
00531 #define FSI_Free_Count 488
00532 #define FSI_Nxt_Free 492
00533
00534 #define MBR_Table 446
00535
00536 #define DIR_Name 0
00537 #define DIR_Attr 11
00538 #define DIR_NTres 12
00539 #define DIR_CrtTime 14
00540 #define DIR_CrtDate 16
00541 #define DIR_FstClusHI 20
00542 #define DIR_WrtTime 22
00543 #define DIR_WrtDate 24
00544 #define DIR_FstClusLO 26
00545 #define DIR_FileSize 28
00546 #define LDIR_Ord 0
00547 #define LDIR_Attr 11
00548 #define LDIR_Type 12
00549 #define LDIR_Chksum 13
00550 #define LDIR_FstClusLO 26
00551
00552
00553
00554
00555
00556
00557 #if _WORD_ACCESS == 1
00558 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
00559 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
00560 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
00561 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
00562 #else
00563 #define LD_WORD(ptr) (WORD)(((WORD)*(BYTE*)((ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
00564 #define LD_DWORD(ptr) (DWORD)(((DWORD)*(BYTE*)((ptr)+3)<<24)|((DWORD)*(BYTE*)((ptr)+2)<<16)|((WORD)*(BYTE*)((ptr)+1)<<8)|*(BYTE*)(ptr))
00565 #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *(BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8)
00566 #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)
00567 #endif
00568
00569
00570 #endif