1 /*--------------------------------------------------------------------------/
2 / FatFs - FAT file system module include file R0.04b (C)ChaN, 2007
3 /---------------------------------------------------------------------------/
4 / FatFs module is an experimenal project to implement FAT file system to
5 / cheap microcontrollers. This is a free software and is opened for education,
6 / research and development under license policy of following trems.
8 / Copyright (C) 2007, ChaN, all right reserved.
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 / profit use without any restriction under your responsibility.
13 / * Redistributions of source code must retain the above copyright notice.
15 /---------------------------------------------------------------------------*/
21 /* The _MCU_ENDIAN defines which access method is used to the FAT structure.
22 / 1: Enable word access.
23 / 2: Disable word access and use byte-by-byte access instead.
24 / When the architectural byte order of the MCU is big-endian and/or address
25 / miss-aligned access is prohibited, the _MCU_ENDIAN must be set to 2.
26 / If it is not the case, it can be set to 1 for good code efficiency. */
28 #define _FS_READONLY 0
29 /* Setting _FS_READONLY to 1 defines read only configuration. This removes
30 / writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename
31 / and useless f_getfree. */
33 #define _FS_MINIMIZE 0
34 /* The _FS_MINIMIZE option defines minimization level to remove some functions.
36 / 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod and f_rename are removed.
37 / 2: f_opendir and f_readdir are removed in addition to level 1.
38 / 3: f_lseek is removed in addition to level 2. */
41 /* Number of logical drives to be used. This affects the size of internal table. */
44 /* When _USE_MKFS is set to 1 and _FS_READONLY is set to 0, f_mkfs function is
47 #define _MULTI_PARTITION 0
48 /* When _MULTI_PARTITION is set to 0, each logical drive is bound to same
49 / physical drive number and can mount only 1st primaly partition. When it is
50 / set to 1, each logical drive can mount a partition listed in Drives[]. */
53 /* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */
56 /* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise
57 / only US-ASCII(7bit) code can be accepted as file/directory name. */
60 /* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved.
61 / Note that the files are always accessed in case insensitive. */
66 // Definitions corresponds to multiple sector size (not tested)
68 #define S_MAX_SIZ 512 /* Do not change */
70 #define S_SIZ (fs->s_size)
76 // File system object structure
80 U16 id; /* File system mount ID */
81 U16 n_rootdir; /* Number of root directory entries */
82 U32 winsect; /* Current sector appearing in the win[] */
83 U32 sects_fat; /* Sectors per fat */
84 U32 max_clust; /* Maximum cluster# + 1 */
85 U32 fatbase; /* FAT start sector */
86 U32 dirbase; /* Root directory start sector (cluster# for FAT32) */
87 U32 database; /* Data start sector */
89 U32 last_clust; /* Last allocated cluster */
90 U32 free_clust; /* Number of free clusters */
92 U32 fsi_sector; /* fsinfo sector */
93 U8 fsi_flag; /* fsinfo dirty flag (1:must be written back) */
97 U8 fs_type; /* FAT sub type */
98 U8 sects_clust; /* Sectors per cluster */
100 U16 s_size; /* Sector size */
102 U8 n_fats; /* Number of FAT copies */
103 U8 drive; /* Physical drive number */
104 U8 winflag; /* win[] dirty flag (1:must be written back) */
106 U8 win[S_MAX_SIZ]; /* Disk access window for Directory/FAT */
108 /* __attribute__ ((packed)) */ FATFS;
111 // Directory object structure
115 U16 id; /* Owner file system mount ID */
116 U16 index; /* Current index */
117 FATFS* fs; /* Pointer to the owner file system object */
118 U32 sclust; /* Start cluster */
119 U32 clust; /* Current cluster */
120 U32 sect; /* Current sector */
122 /* __attribute__ ((packed)) */ DIR;
125 // File object structure
129 U16 id; /* Owner file system mount ID */
130 U8 flag; /* File status flags */
131 U8 sect_clust; /* Left sectors in cluster */
132 FATFS* fs; /* Pointer to the owner file system object */
133 U32 fptr; /* File R/W pointer */
134 U32 fsize; /* File size */
135 U32 org_clust; /* File start cluster */
136 U32 curr_clust; /* Current cluster */
137 U32 curr_sect; /* Current sector */
138 #if _FS_READONLY == 0
139 U32 dir_sect; /* Sector containing the directory entry */
140 U8* dir_ptr; /* Ponter to the directory entry in the window */
142 U8 buffer [S_MAX_SIZ]; /* File R/W buffer */
144 /* __attribute__ ((packed)) */ FIL;
147 // File status structure
149 typedef struct _FILINFO
151 U32 fsize; /* Size */
152 U16 fdate; /* Date */
153 U16 ftime; /* Time */
154 U8 fattrib; /* Attribute */
155 char fname [8+1+3+1]; /* Name (8.3 format) */
157 /* __attribute__ ((packed)) */ FILINFO;
160 // Definitions corresponds to multi partition
162 #if _MULTI_PARTITION != 0 /* Multiple partition cfg */
164 typedef struct _PARTITION
166 U8 pd; /* Physical drive # (0-255) */
167 U8 pt; /* Partition # (0-3) */
169 /* __attribute__ ((packed)) */ PARTITION;
171 extern const PARTITION Drives []; /* Logical drive# to physical location conversion table */
172 #define LD2PD(drv) (Drives[drv].pd) /* Get physical drive# */
173 #define LD2PT(drv) (Drives[drv].pt) /* Get partition# */
175 #else /* Single partition cfg */
177 #define LD2PD(drv) (drv) /* Physical drive# is equal to logical drive# */
178 #define LD2PT(drv) 0 /* Always mounts the 1st partition */
183 // File function return code (FRESULT)
188 FR_NOT_READY, /* 1 */
191 FR_INVALID_NAME, /* 4 */
192 FR_INVALID_DRIVE, /* 5 */
196 FR_WRITE_PROTECTED, /* 9 */
197 FR_NOT_ENABLED, /* 10 */
198 FR_NO_FILESYSTEM, /* 11 */
199 FR_INVALID_OBJECT, /* 12 */
200 FR_MKFS_ABORTED /* 13 */
205 // FatFs module application interface
207 void f_printerror (FRESULT f); /* Print error code */
208 FRESULT f_mount (U8, FATFS*); /* Mount/Unmount a logical drive */
209 FRESULT f_open (FIL*, const char*, U8); /* Open or create a file */
210 FRESULT f_read (FIL*, void*, U16, U16*); /* Read data from a file */
211 FRESULT f_write (FIL*, const void*, U16, U16*); /* Write data to a file */
212 FRESULT f_lseek (FIL*, U32); /* Move file pointer of a file object */
213 FRESULT f_close (FIL*); /* Close an open file object */
214 FRESULT f_opendir (DIR*, const char*); /* Open an existing directory */
215 FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */
216 FRESULT f_stat (const char*, FILINFO*); /* Get file status */
217 FRESULT f_getfree (const char*, U32*, FATFS**); /* Get number of free clusters on the drive */
218 FRESULT f_sync (FIL*); /* Flush cached data of a writing file */
219 FRESULT f_unlink (const char*); /* Delete an existing file or directory */
220 FRESULT f_mkdir (const char*); /* Create a new directory */
221 FRESULT f_chmod (const char*, U8, U8); /* Change file/dir attriburte */
222 FRESULT f_rename (const char*, const char*); /* Rename/Move a file or directory */
223 FRESULT f_mkfs (U8, U8, U8); /* Create a file system on the drive */
226 // User defined function to give a current time to fatfs module
228 U32 get_fattime (void); /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
229 /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
232 // File access control and file status flags (FIL.flag)
234 #define FA_OPEN_EXISTING 0x00
236 #if _FS_READONLY == 0
237 #define FA_WRITE 0x02
238 #define FA_CREATE_NEW 0x04
239 #define FA_CREATE_ALWAYS 0x08
240 #define FA_OPEN_ALWAYS 0x10
241 #define FA__WRITTEN 0x20
242 #define FA__DIRTY 0x40
244 #define FA__ERROR 0x80
247 // FAT sub type (FATFS.fs_type)
254 // File attribute bits for directory entry
256 #define AM_RDO 0x01 /* Read only */
257 #define AM_HID 0x02 /* Hidden */
258 #define AM_SYS 0x04 /* System */
259 #define AM_VOL 0x08 /* Volume label */
260 #define AM_LFN 0x0F /* LFN entry */
261 #define AM_DIR 0x10 /* Directory */
262 #define AM_ARC 0x20 /* Archive */
265 // Offset of FAT structure members
269 #define BPB_BytsPerSec 11
270 #define BPB_SecPerClus 13
271 #define BPB_RsvdSecCnt 14
272 #define BPB_NumFATs 16
273 #define BPB_RootEntCnt 17
274 #define BPB_TotSec16 19
276 #define BPB_FATSz16 22
277 #define BPB_SecPerTrk 24
278 #define BPB_NumHeads 26
279 #define BPB_HiddSec 28
280 #define BPB_TotSec32 32
284 #define BS_BootSig 38
287 #define BS_FilSysType 54
289 #define BPB_FATSz32 36
290 #define BPB_ExtFlags 40
292 #define BPB_RootClus 44
293 #define BPB_FSInfo 48
294 #define BPB_BkBootSec 50
295 #define BS_DrvNum32 64
296 #define BS_BootSig32 66
297 #define BS_VolID32 67
298 #define BS_VolLab32 71
299 #define BS_FilSysType32 82
301 #define FSI_LeadSig 0
302 #define FSI_StrucSig 484
303 #define FSI_Free_Count 488
304 #define FSI_Nxt_Free 492
306 #define MBR_Table 446
311 #define DIR_CrtTime 14
312 #define DIR_CrtDate 16
313 #define DIR_FstClusHI 20
314 #define DIR_WrtTime 22
315 #define DIR_WrtDate 24
316 #define DIR_FstClusLO 26
317 #define DIR_FileSize 28
320 // Multi-byte word access macros
322 #if _MCU_ENDIAN == 1 /* Use word access */
323 #define LD_U16(ptr) (U16)(*(U16*)(U8*)(ptr))
324 #define LD_U32(ptr) (U32)(*(U32*)(U8*)(ptr))
325 #define ST_U16(ptr,val) *(U16*)(U8*)(ptr)=(U16)(val)
326 #define ST_U32(ptr,val) *(U32*)(U8*)(ptr)=(U32)(val)
328 #if _MCU_ENDIAN == 2 /* Use byte-by-byte access */
329 #define LD_U16(ptr) (U16)(((U16)*(U8*)((ptr)+1)<<8)|(U16)*(U8*)(ptr))
330 #define LD_U32(ptr) (U32)(((U32)*(U8*)((ptr)+3)<<24)|((U32)*(U8*)((ptr)+2)<<16)|((U16)*(U8*)((ptr)+1)<<8)|*(U8*)(ptr))
331 #define ST_U16(ptr,val) *(U8*)(ptr)=(U8)(val); *(U8*)((ptr)+1)=(U8)((U16)(val)>>8)
332 #define ST_U32(ptr,val) *(U8*)(ptr)=(U8)(val); *(U8*)((ptr)+1)=(U8)((U16)(val)>>8); *(U8*)((ptr)+2)=(U8)((U32)(val)>>16); *(U8*)((ptr)+3)=(U8)((U32)(val)>>24)
334 #error Do not forget to set _MCU_ENDIAN properly!