1 /*---------------------------------------------------------------------------/ 
   2 /  Petit FatFs - FAT file system module include file  R0.01a   (C)ChaN, 2010 
   3 /----------------------------------------------------------------------------/ 
   4 / Petit FatFs module is an open source software to implement FAT file system to 
   5 / small embedded systems. This is a free software and is opened for education, 
   6 / research and commercial developments under license policy of following trems. 
   8 /  Copyright (C) 2010, ChaN, all right reserved. 
  10 / * The Petit 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 use UNDER YOUR RESPONSIBILITY. 
  13 / * Redistributions of source code must retain the above copyright notice. 
  14 /----------------------------------------------------------------------------*/ 
  18 /*---------------------------------------------------------------------------/ 
  19 / Petit FatFs Configuration Options 
  21 / CAUTION! Do not forget to make clean the project after any changes to 
  22 / the configuration options. 
  24 /----------------------------------------------------------------------------*/ 
  28 #define _WORD_ACCESS    0 
  29 /* The _WORD_ACCESS option defines which access method is used to the word 
  30 /  data in the FAT structure. 
  32 /   0: Byte-by-byte access. Always compatible with all platforms. 
  33 /   1: Word access. Do not choose this unless following condition is met. 
  35 /  When the byte order on the memory is big-endian or address miss-aligned 
  36 /  word access results incorrect behavior, the _WORD_ACCESS must be set to 0. 
  37 /  If it is not the case, the value can also be set to 1 to improve the 
  38 /  performance and code efficiency. */ 
  42 /* To enable pf_opendir and pf_readdir function, set _USE_DIR to 1. */ 
  46 /* To enable pf_lseek function, set _USE_LSEEK to 1. */ 
  50 /* To enable FAT32 support, set _FS_FAT32 to 1. */ 
  53 /* End of configuration options. Do not change followings without care.     */ 
  54 /*--------------------------------------------------------------------------*/ 
  64 /* File system object structure */ 
  66 typedef struct _FATFS_ 
{ 
  67         BYTE    fs_type
;        /* FAT sub type */ 
  68         BYTE    csize
;          /* Number of sectors per cluster */ 
  69         BYTE    flag
;           /* File status flags */ 
  70         BYTE    csect
;          /* File sector address in the cluster */ 
  71         WORD    n_rootdir
;      /* Number of root directory entries (0 on FAT32) */ 
  72         BYTE
*   buf
;            /* Pointer to the disk access buffer */ 
  73         CLUST   max_clust
;      /* Maximum cluster# + 1. Number of clusters is max_clust - 2 */ 
  74         DWORD   fatbase
;        /* FAT start sector */ 
  75         DWORD   dirbase
;        /* Root directory start sector (Cluster# on FAT32) */ 
  76         DWORD   database
;       /* Data start sector */ 
  77         DWORD   fptr
;           /* File R/W pointer */ 
  78         DWORD   fsize
;          /* File size */ 
  79         CLUST   org_clust
;      /* File start cluster */ 
  80         CLUST   curr_clust
;     /* File current cluster */ 
  81         DWORD   dsect
;          /* File current data sector */ 
  86 /* Directory object structure */ 
  88 typedef struct _DIR_ 
{ 
  89         WORD    index
;          /* Current read/write index number */ 
  90         BYTE
*   fn
;                     /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */ 
  91         CLUST   sclust
;         /* Table start cluster (0:Static table) */ 
  92         CLUST   clust
;          /* Current cluster */ 
  93         DWORD   sect
;           /* Current sector */ 
  98 /* File status structure */ 
 100 typedef struct _FILINFO_ 
{ 
 101         DWORD   fsize
;          /* File size */ 
 102         WORD    fdate
;          /* Last modified date */ 
 103         WORD    ftime
;          /* Last modified time */ 
 104         BYTE    fattrib
;        /* Attribute */ 
 105         char    fname
[13];      /* File name */ 
 110 /* File function return code (FRESULT) */ 
 115         FR_NOT_READY
,           /* 2 */ 
 118         FR_INVALID_NAME
,        /* 5 */ 
 119         FR_STREAM_ERR
,          /* 6 */ 
 120         FR_INVALID_OBJECT
,      /* 7 */ 
 121         FR_NOT_ENABLED
,         /* 8 */ 
 122         FR_NO_FILESYSTEM        
/* 9 */ 
 127 /*--------------------------------------------------------------*/ 
 128 /* Petit FatFs module application interface                     */ 
 130 FRESULT 
pf_mount (FATFS
*);                              /* Mount/Unmount a logical drive */ 
 131 FRESULT 
pf_open (const char*);                  /* Open a file */ 
 132 FRESULT 
pf_read (void*, WORD
, WORD
*);   /* Read data from a file */ 
 133 FRESULT 
pf_lseek (DWORD
);                               /* Move file pointer of a file object */ 
 134 FRESULT 
pf_opendir (DIR*, const char*); /* Open an existing directory */ 
 135 FRESULT 
pf_readdir (DIR*, FILINFO
*);    /* Read a directory item */ 
 139 /*--------------------------------------------------------------*/ 
 140 /* Flags and offset address                                     */ 
 142 /* File status flag (FATFS.flag) */ 
 145 #define FA_STREAM       0x40 
 146 #define FA__ERROR       0x80 
 149 /* FAT sub type (FATFS.fs_type) */ 
 156 /* File attribute bits for directory entry */ 
 158 #define AM_RDO  0x01    /* Read only */ 
 159 #define AM_HID  0x02    /* Hidden */ 
 160 #define AM_SYS  0x04    /* System */ 
 161 #define AM_VOL  0x08    /* Volume label */ 
 162 #define AM_LFN  0x0F    /* LFN entry */ 
 163 #define AM_DIR  0x10    /* Directory */ 
 164 #define AM_ARC  0x20    /* Archive */ 
 165 #define AM_MASK 0x3F    /* Mask of defined bits */ 
 168 /* FatFs refers the members in the FAT structures with byte offset instead 
 169 / of structure member because there are incompatibility of the packing option 
 170 / between various compilers. */ 
 174 #define BPB_BytsPerSec          11 
 175 #define BPB_SecPerClus          13 
 176 #define BPB_RsvdSecCnt          14 
 177 #define BPB_NumFATs                     16 
 178 #define BPB_RootEntCnt          17 
 179 #define BPB_TotSec16            19 
 181 #define BPB_FATSz16                     22 
 182 #define BPB_SecPerTrk           24 
 183 #define BPB_NumHeads            26 
 184 #define BPB_HiddSec                     28 
 185 #define BPB_TotSec32            32 
 189 #define BS_BootSig                      38 
 192 #define BS_FilSysType           54 
 194 #define BPB_FATSz32                     36 
 195 #define BPB_ExtFlags            40 
 197 #define BPB_RootClus            44 
 198 #define BPB_FSInfo                      48 
 199 #define BPB_BkBootSec           50 
 200 #define BS_DrvNum32                     64 
 201 #define BS_BootSig32            66 
 202 #define BS_VolID32                      67 
 203 #define BS_VolLab32                     71 
 204 #define BS_FilSysType32         82 
 206 #define MBR_Table                       446 
 211 #define DIR_CrtTime                     14 
 212 #define DIR_CrtDate                     16 
 213 #define DIR_FstClusHI           20 
 214 #define DIR_WrtTime                     22 
 215 #define DIR_WrtDate                     24 
 216 #define DIR_FstClusLO           26 
 217 #define DIR_FileSize            28 
 221 /*--------------------------------*/ 
 222 /* Multi-byte word access macros  */ 
 224 #if _WORD_ACCESS == 1   /* Enable word access to the FAT structure */ 
 225 #define LD_WORD(ptr)            (WORD)(*(WORD*)(BYTE*)(ptr)) 
 226 #define LD_DWORD(ptr)           (DWORD)(*(DWORD*)(BYTE*)(ptr)) 
 227 #define ST_WORD(ptr,val)        *(WORD*)(BYTE*)(ptr)=(WORD)(val) 
 228 #define ST_DWORD(ptr,val)       *(DWORD*)(BYTE*)(ptr)=(DWORD)(val) 
 229 #else                                   /* Use byte-by-byte access to the FAT structure */ 
 230 #define LD_WORD(ptr)            (WORD)(((WORD)*(BYTE*)((ptr)+1)<<8)|(WORD)*(BYTE*)(ptr)) 
 231 #define LD_DWORD(ptr)           (DWORD)(((DWORD)*(BYTE*)((ptr)+3)<<24)|((DWORD)*(BYTE*)((ptr)+2)<<16)|((WORD)*(BYTE*)((ptr)+1)<<8)|*(BYTE*)(ptr)) 
 232 #define ST_WORD(ptr,val)        *(BYTE*)(ptr)=(BYTE)(val); *(BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8) 
 233 #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)