Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /*---------------------------------------------------------------------------/ |
| 2 | / FatFs - FAT file system module include file R0.09b (C)ChaN, 2013 |
| 3 | /----------------------------------------------------------------------------/ |
| 4 | / FatFs module is a generic FAT file system module for small embedded systems. |
| 5 | / This is a free software that opened for education, research and commercial |
| 6 | / developments under license policy of following terms. |
| 7 | / |
| 8 | / Copyright (C) 2013, ChaN, all right reserved. |
| 9 | / |
| 10 | / * The 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 product UNDER YOUR RESPONSIBILITY. |
| 13 | / * Redistributions of source code must retain the above copyright notice. |
| 14 | / |
| 15 | /----------------------------------------------------------------------------*/ |
| 16 | |
| 17 | #ifndef _FATFS |
| 18 | #define _FATFS 82786 /* Revision ID */ |
| 19 | |
| 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | #include "integer.h" /* Basic integer types */ |
| 25 | #include "ffconf.h" /* FatFs configuration options */ |
| 26 | #include "tusb_config.h" |
| 27 | |
| 28 | #if _FATFS != _FFCONF |
| 29 | #error Wrong configuration file (ffconf.h). |
| 30 | #endif |
| 31 | |
| 32 | |
| 33 | |
| 34 | /* Definitions of volume management */ |
| 35 | |
| 36 | #if _MULTI_PARTITION /* Multiple partition configuration */ |
| 37 | typedef struct { |
| 38 | BYTE pd; /* Physical drive number */ |
| 39 | BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */ |
| 40 | } PARTITION; |
| 41 | extern PARTITION VolToPart[]; /* Volume - Partition resolution table */ |
| 42 | #define LD2PD(vol) (VolToPart[vol].pd) /* Get physical drive number */ |
| 43 | #define LD2PT(vol) (VolToPart[vol].pt) /* Get partition index */ |
| 44 | |
| 45 | #else /* Single partition configuration */ |
| 46 | #define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */ |
| 47 | #define LD2PT(vol) 0 /* Always mounts the 1st partition or in SFD */ |
| 48 | |
| 49 | #endif |
| 50 | |
| 51 | |
| 52 | |
| 53 | /* Type of path name strings on FatFs API */ |
| 54 | |
| 55 | #if _LFN_UNICODE /* Unicode string */ |
| 56 | #if !_USE_LFN |
| 57 | #error _LFN_UNICODE must be 0 in non-LFN cfg. |
| 58 | #endif |
| 59 | #ifndef _INC_TCHAR |
| 60 | typedef WCHAR TCHAR; |
| 61 | #define _T(x) L ## x |
| 62 | #define _TEXT(x) L ## x |
| 63 | #endif |
| 64 | |
| 65 | #else /* ANSI/OEM string */ |
| 66 | #ifndef _INC_TCHAR |
| 67 | typedef char TCHAR; |
| 68 | #define _T(x) x |
| 69 | #define _TEXT(x) x |
| 70 | #endif |
| 71 | |
| 72 | #endif |
| 73 | |
| 74 | |
| 75 | |
| 76 | /* File system object structure (FATFS) */ |
| 77 | |
| 78 | typedef struct { |
| 79 | BYTE fs_type; /* FAT sub-type (0:Not mounted) */ |
| 80 | BYTE drv; /* Physical drive number */ |
| 81 | BYTE csize; /* Sectors per cluster (1,2,4...128) */ |
| 82 | BYTE n_fats; /* Number of FAT copies (1,2) */ |
| 83 | BYTE wflag; /* win[] dirty flag (1:must be written back) */ |
| 84 | BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */ |
| 85 | WORD id; /* File system mount ID */ |
| 86 | WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ |
| 87 | #if _MAX_SS != 512 |
| 88 | WORD ssize; /* Bytes per sector (512, 1024, 2048 or 4096) */ |
| 89 | #endif |
| 90 | #if _FS_REENTRANT |
| 91 | _SYNC_t sobj; /* Identifier of sync object */ |
| 92 | #endif |
| 93 | #if !_FS_READONLY |
| 94 | DWORD last_clust; /* Last allocated cluster */ |
| 95 | DWORD free_clust; /* Number of free clusters */ |
| 96 | DWORD fsi_sector; /* fsinfo sector (FAT32) */ |
| 97 | #endif |
| 98 | #if _FS_RPATH |
| 99 | DWORD cdir; /* Current directory start cluster (0:root) */ |
| 100 | #endif |
| 101 | DWORD n_fatent; /* Number of FAT entries (= number of clusters + 2) */ |
| 102 | DWORD fsize; /* Sectors per FAT */ |
| 103 | DWORD volbase; /* Volume start sector */ |
| 104 | DWORD fatbase; /* FAT start sector */ |
| 105 | DWORD dirbase; /* Root directory start sector (FAT32:Cluster#) */ |
| 106 | DWORD database; /* Data start sector */ |
| 107 | DWORD winsect; /* Current sector appearing in the win[] */ |
| 108 | BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and Data on tiny cfg) */ |
| 109 | } FATFS; |
| 110 | |
| 111 | |
| 112 | |
| 113 | /* File object structure (FIL) */ |
| 114 | |
| 115 | typedef struct { |
| 116 | FATFS* fs; /* Pointer to the related file system object (**do not change order**) */ |
| 117 | WORD id; /* Owner file system mount ID (**do not change order**) */ |
| 118 | BYTE flag; /* File status flags */ |
| 119 | BYTE pad1; |
| 120 | DWORD fptr; /* File read/write pointer (0ed on file open) */ |
| 121 | DWORD fsize; /* File size */ |
| 122 | DWORD sclust; /* File data start cluster (0:no data cluster, always 0 when fsize is 0) */ |
| 123 | DWORD clust; /* Current cluster of fpter */ |
| 124 | DWORD dsect; /* Current data sector of fpter */ |
| 125 | #if !_FS_READONLY |
| 126 | DWORD dir_sect; /* Sector containing the directory entry */ |
| 127 | BYTE* dir_ptr; /* Pointer to the directory entry in the window */ |
| 128 | #endif |
| 129 | #if _USE_FASTSEEK |
| 130 | DWORD* cltbl; /* Pointer to the cluster link map table (null on file open) */ |
| 131 | #endif |
| 132 | #if _FS_LOCK |
| 133 | UINT lockid; /* File lock ID (index of file semaphore table Files[]) */ |
| 134 | #endif |
| 135 | #if !_FS_TINY |
| 136 | BYTE buf[_MAX_SS]; /* File data read/write buffer */ |
| 137 | #endif |
| 138 | } FIL; |
| 139 | |
| 140 | |
| 141 | |
| 142 | /* Directory object structure (DIR) */ |
| 143 | |
| 144 | typedef struct { |
| 145 | FATFS* fs; /* Pointer to the owner file system object (**do not change order**) */ |
| 146 | WORD id; /* Owner file system mount ID (**do not change order**) */ |
| 147 | WORD index; /* Current read/write index number */ |
| 148 | DWORD sclust; /* Table start cluster (0:Root dir) */ |
| 149 | DWORD clust; /* Current cluster */ |
| 150 | DWORD sect; /* Current sector */ |
| 151 | BYTE* dir; /* Pointer to the current SFN entry in the win[] */ |
| 152 | BYTE* fn; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */ |
| 153 | #if _USE_LFN |
| 154 | WCHAR* lfn; /* Pointer to the LFN working buffer */ |
| 155 | WORD lfn_idx; /* Last matched LFN index number (0xFFFF:No LFN) */ |
| 156 | #endif |
| 157 | } DIR; |
| 158 | |
| 159 | |
| 160 | |
| 161 | /* File status structure (FILINFO) */ |
| 162 | |
| 163 | typedef struct { |
| 164 | DWORD fsize; /* File size */ |
| 165 | WORD fdate; /* Last modified date */ |
| 166 | WORD ftime; /* Last modified time */ |
| 167 | BYTE fattrib; /* Attribute */ |
| 168 | TCHAR fname[13]; /* Short file name (8.3 format) */ |
| 169 | #if _USE_LFN |
| 170 | TCHAR* lfname; /* Pointer to the LFN buffer */ |
| 171 | UINT lfsize; /* Size of LFN buffer in TCHAR */ |
| 172 | #endif |
| 173 | } FILINFO; |
| 174 | |
| 175 | |
| 176 | |
| 177 | /* File function return code (FRESULT) */ |
| 178 | |
| 179 | typedef enum { |
| 180 | FR_OK = 0, /* (0) Succeeded */ |
| 181 | FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ |
| 182 | FR_INT_ERR, /* (2) Assertion failed */ |
| 183 | FR_NOT_READY, /* (3) The physical drive cannot work */ |
| 184 | FR_NO_FILE, /* (4) Could not find the file */ |
| 185 | FR_NO_PATH, /* (5) Could not find the path */ |
| 186 | FR_INVALID_NAME, /* (6) The path name format is invalid */ |
| 187 | FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ |
| 188 | FR_EXIST, /* (8) Access denied due to prohibited access */ |
| 189 | FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ |
| 190 | FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ |
| 191 | FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ |
| 192 | FR_NOT_ENABLED, /* (12) The volume has no work area */ |
| 193 | FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ |
| 194 | FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */ |
| 195 | FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ |
| 196 | FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ |
| 197 | FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ |
| 198 | FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_SHARE */ |
| 199 | FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ |
| 200 | } FRESULT; |
| 201 | |
| 202 | |
| 203 | |
| 204 | /*--------------------------------------------------------------*/ |
| 205 | /* FatFs module application interface */ |
| 206 | |
| 207 | FRESULT f_mount (BYTE vol, FATFS* fs); /* Mount/Unmount a logical drive */ |
| 208 | FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */ |
| 209 | FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from a file */ |
| 210 | FRESULT f_lseek (FIL* fp, DWORD ofs); /* Move file pointer of a file object */ |
| 211 | FRESULT f_close (FIL* fp); /* Close an open file object */ |
| 212 | FRESULT f_opendir (DIR* dj, const TCHAR* path); /* Open an existing directory */ |
| 213 | FRESULT f_readdir (DIR* dj, FILINFO* fno); /* Read a directory item */ |
| 214 | FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */ |
| 215 | FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to a file */ |
| 216 | FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */ |
| 217 | FRESULT f_truncate (FIL* fp); /* Truncate file */ |
| 218 | FRESULT f_sync (FIL* fp); /* Flush cached data of a writing file */ |
| 219 | FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */ |
| 220 | FRESULT f_mkdir (const TCHAR* path); /* Create a new directory */ |
| 221 | FRESULT f_chmod (const TCHAR* path, BYTE value, BYTE mask); /* Change attribute of the file/dir */ |
| 222 | FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change times-tamp of the file/dir */ |
| 223 | FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */ |
| 224 | FRESULT f_chdrive (BYTE drv); /* Change current drive */ |
| 225 | BYTE f_get_current_drive(void); |
| 226 | FRESULT f_chdir (const TCHAR* path); /* Change current directory */ |
| 227 | FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */ |
| 228 | FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* sn); /* Get volume label */ |
| 229 | FRESULT f_setlabel (const TCHAR* label); /* Set volume label */ |
| 230 | FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */ |
| 231 | FRESULT f_mkfs (BYTE vol, BYTE sfd, UINT au); /* Create a file system on the drive */ |
| 232 | FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work); /* Divide a physical drive into some partitions */ |
| 233 | int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */ |
| 234 | int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */ |
| 235 | int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */ |
| 236 | TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */ |
| 237 | |
| 238 | #define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0) |
| 239 | #define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0) |
| 240 | #define f_tell(fp) ((fp)->fptr) |
| 241 | #define f_size(fp) ((fp)->fsize) |
| 242 | |
| 243 | #ifndef EOF |
| 244 | #define EOF (-1) |
| 245 | #endif |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | /*--------------------------------------------------------------*/ |
| 251 | /* Additional user defined functions */ |
| 252 | |
| 253 | /* RTC function */ |
| 254 | #if !_FS_READONLY |
| 255 | DWORD get_fattime (void); |
| 256 | #endif |
| 257 | |
| 258 | /* Unicode support functions */ |
| 259 | #if _USE_LFN /* Unicode - OEM code conversion */ |
| 260 | WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */ |
| 261 | WCHAR ff_wtoupper (WCHAR chr); /* Unicode upper-case conversion */ |
| 262 | #if _USE_LFN == 3 /* Memory functions */ |
| 263 | void* ff_memalloc (UINT msize); /* Allocate memory block */ |
| 264 | void ff_memfree (void* mblock); /* Free memory block */ |
| 265 | #endif |
| 266 | #endif |
| 267 | |
| 268 | /* Sync functions */ |
| 269 | #if _FS_REENTRANT |
| 270 | int ff_cre_syncobj (BYTE vol, _SYNC_t* sobj); /* Create a sync object */ |
| 271 | int ff_req_grant (_SYNC_t sobj); /* Lock sync object */ |
| 272 | void ff_rel_grant (_SYNC_t sobj); /* Unlock sync object */ |
| 273 | int ff_del_syncobj (_SYNC_t sobj); /* Delete a sync object */ |
| 274 | #endif |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | /*--------------------------------------------------------------*/ |
| 280 | /* Flags and offset address */ |
| 281 | |
| 282 | |
| 283 | /* File access control and file status flags (FIL.flag) */ |
| 284 | |
| 285 | #define FA_READ 0x01 |
| 286 | #define FA_OPEN_EXISTING 0x00 |
| 287 | #define FA__ERROR 0x80 |
| 288 | |
| 289 | #if !_FS_READONLY |
| 290 | #define FA_WRITE 0x02 |
| 291 | #define FA_CREATE_NEW 0x04 |
| 292 | #define FA_CREATE_ALWAYS 0x08 |
| 293 | #define FA_OPEN_ALWAYS 0x10 |
| 294 | #define FA__WRITTEN 0x20 |
| 295 | #define FA__DIRTY 0x40 |
| 296 | #endif |
| 297 | |
| 298 | |
| 299 | /* FAT sub type (FATFS.fs_type) */ |
| 300 | |
| 301 | #define FS_FAT12 1 |
| 302 | #define FS_FAT16 2 |
| 303 | #define FS_FAT32 3 |
| 304 | |
| 305 | |
| 306 | /* File attribute bits for directory entry */ |
| 307 | |
| 308 | #define AM_RDO 0x01 /* Read only */ |
| 309 | #define AM_HID 0x02 /* Hidden */ |
| 310 | #define AM_SYS 0x04 /* System */ |
| 311 | #define AM_VOL 0x08 /* Volume label */ |
| 312 | #define AM_LFN 0x0F /* LFN entry */ |
| 313 | #define AM_DIR 0x10 /* Directory */ |
| 314 | #define AM_ARC 0x20 /* Archive */ |
| 315 | #define AM_MASK 0x3F /* Mask of defined bits */ |
| 316 | |
| 317 | |
| 318 | /* Fast seek feature */ |
| 319 | #define CREATE_LINKMAP 0xFFFFFFFF |
| 320 | |
| 321 | |
| 322 | |
| 323 | /*--------------------------------*/ |
| 324 | /* Multi-byte word access macros */ |
| 325 | |
| 326 | #if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */ |
| 327 | #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr)) |
| 328 | #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr)) |
| 329 | #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val) |
| 330 | #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val) |
| 331 | #else /* Use byte-by-byte access to the FAT structure */ |
| 332 | #define LD_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr)) |
| 333 | #define LD_DWORD(ptr) (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr)) |
| 334 | #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8) |
| 335 | #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) |
| 336 | #endif |
| 337 | |
| 338 | #ifdef __cplusplus |
| 339 | } |
| 340 | #endif |
| 341 | |
| 342 | #endif /* _FATFS */ |