[101] | 1 | /*----------------------------------------------------------------------------/
|
---|
| 2 | / FatFs - FAT file system module R0.10c (C)ChaN, 2014
|
---|
| 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) 2014, 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 products UNDER YOUR RESPONSIBILITY.
|
---|
| 13 | / * Redistributions of source code must retain the above copyright notice.
|
---|
| 14 | /
|
---|
| 15 | /-----------------------------------------------------------------------------/
|
---|
| 16 | / Feb 26,'06 R0.00 Prototype.
|
---|
| 17 | /
|
---|
| 18 | / Apr 29,'06 R0.01 First stable version.
|
---|
| 19 | /
|
---|
| 20 | / Jun 01,'06 R0.02 Added FAT12 support.
|
---|
| 21 | / Removed unbuffered mode.
|
---|
| 22 | / Fixed a problem on small (<32M) partition.
|
---|
| 23 | / Jun 10,'06 R0.02a Added a configuration option (_FS_MINIMUM).
|
---|
| 24 | /
|
---|
| 25 | / Sep 22,'06 R0.03 Added f_rename().
|
---|
| 26 | / Changed option _FS_MINIMUM to _FS_MINIMIZE.
|
---|
| 27 | / Dec 11,'06 R0.03a Improved cluster scan algorithm to write files fast.
|
---|
| 28 | / Fixed f_mkdir() creates incorrect directory on FAT32.
|
---|
| 29 | /
|
---|
| 30 | / Feb 04,'07 R0.04 Supported multiple drive system.
|
---|
| 31 | / Changed some interfaces for multiple drive system.
|
---|
| 32 | / Changed f_mountdrv() to f_mount().
|
---|
| 33 | / Added f_mkfs().
|
---|
| 34 | / Apr 01,'07 R0.04a Supported multiple partitions on a physical drive.
|
---|
| 35 | / Added a capability of extending file size to f_lseek().
|
---|
| 36 | / Added minimization level 3.
|
---|
| 37 | / Fixed an endian sensitive code in f_mkfs().
|
---|
| 38 | / May 05,'07 R0.04b Added a configuration option _USE_NTFLAG.
|
---|
| 39 | / Added FSINFO support.
|
---|
| 40 | / Fixed DBCS name can result FR_INVALID_NAME.
|
---|
| 41 | / Fixed short seek (<= csize) collapses the file object.
|
---|
| 42 | /
|
---|
| 43 | / Aug 25,'07 R0.05 Changed arguments of f_read(), f_write() and f_mkfs().
|
---|
| 44 | / Fixed f_mkfs() on FAT32 creates incorrect FSINFO.
|
---|
| 45 | / Fixed f_mkdir() on FAT32 creates incorrect directory.
|
---|
| 46 | / Feb 03,'08 R0.05a Added f_truncate() and f_utime().
|
---|
| 47 | / Fixed off by one error at FAT sub-type determination.
|
---|
| 48 | / Fixed btr in f_read() can be mistruncated.
|
---|
| 49 | / Fixed cached sector is not flushed when create and close without write.
|
---|
| 50 | /
|
---|
| 51 | / Apr 01,'08 R0.06 Added fputc(), fputs(), fprintf() and fgets().
|
---|
| 52 | / Improved performance of f_lseek() on moving to the same or following cluster.
|
---|
| 53 | /
|
---|
| 54 | / Apr 01,'09 R0.07 Merged Tiny-FatFs as a configuration option. (_FS_TINY)
|
---|
| 55 | / Added long file name feature.
|
---|
| 56 | / Added multiple code page feature.
|
---|
| 57 | / Added re-entrancy for multitask operation.
|
---|
| 58 | / Added auto cluster size selection to f_mkfs().
|
---|
| 59 | / Added rewind option to f_readdir().
|
---|
| 60 | / Changed result code of critical errors.
|
---|
| 61 | / Renamed string functions to avoid name collision.
|
---|
| 62 | / Apr 14,'09 R0.07a Separated out OS dependent code on reentrant cfg.
|
---|
| 63 | / Added multiple sector size feature.
|
---|
| 64 | / Jun 21,'09 R0.07c Fixed f_unlink() can return FR_OK on error.
|
---|
| 65 | / Fixed wrong cache control in f_lseek().
|
---|
| 66 | / Added relative path feature.
|
---|
| 67 | / Added f_chdir() and f_chdrive().
|
---|
| 68 | / Added proper case conversion to extended character.
|
---|
| 69 | / Nov 03,'09 R0.07e Separated out configuration options from ff.h to ffconf.h.
|
---|
| 70 | / Fixed f_unlink() fails to remove a sub-directory on _FS_RPATH.
|
---|
| 71 | / Fixed name matching error on the 13 character boundary.
|
---|
| 72 | / Added a configuration option, _LFN_UNICODE.
|
---|
| 73 | / Changed f_readdir() to return the SFN with always upper case on non-LFN cfg.
|
---|
| 74 | /
|
---|
| 75 | / May 15,'10 R0.08 Added a memory configuration option. (_USE_LFN = 3)
|
---|
| 76 | / Added file lock feature. (_FS_SHARE)
|
---|
| 77 | / Added fast seek feature. (_USE_FASTSEEK)
|
---|
| 78 | / Changed some types on the API, XCHAR->TCHAR.
|
---|
| 79 | / Changed .fname in the FILINFO structure on Unicode cfg.
|
---|
| 80 | / String functions support UTF-8 encoding files on Unicode cfg.
|
---|
| 81 | / Aug 16,'10 R0.08a Added f_getcwd().
|
---|
| 82 | / Added sector erase feature. (_USE_ERASE)
|
---|
| 83 | / Moved file lock semaphore table from fs object to the bss.
|
---|
| 84 | / Fixed a wrong directory entry is created on non-LFN cfg when the given name contains ';'.
|
---|
| 85 | / Fixed f_mkfs() creates wrong FAT32 volume.
|
---|
| 86 | / Jan 15,'11 R0.08b Fast seek feature is also applied to f_read() and f_write().
|
---|
| 87 | / f_lseek() reports required table size on creating CLMP.
|
---|
| 88 | / Extended format syntax of f_printf().
|
---|
| 89 | / Ignores duplicated directory separators in given path name.
|
---|
| 90 | /
|
---|
| 91 | / Sep 06,'11 R0.09 f_mkfs() supports multiple partition to complete the multiple partition feature.
|
---|
| 92 | / Added f_fdisk().
|
---|
| 93 | / Aug 27,'12 R0.09a Changed f_open() and f_opendir() reject null object pointer to avoid crash.
|
---|
| 94 | / Changed option name _FS_SHARE to _FS_LOCK.
|
---|
| 95 | / Fixed assertion failure due to OS/2 EA on FAT12/16 volume.
|
---|
| 96 | / Jan 24,'13 R0.09b Added f_setlabel() and f_getlabel().
|
---|
| 97 | /
|
---|
| 98 | / Oct 02,'13 R0.10 Added selection of character encoding on the file. (_STRF_ENCODE)
|
---|
| 99 | / Added f_closedir().
|
---|
| 100 | / Added forced full FAT scan for f_getfree(). (_FS_NOFSINFO)
|
---|
| 101 | / Added forced mount feature with changes of f_mount().
|
---|
| 102 | / Improved behavior of volume auto detection.
|
---|
| 103 | / Improved write throughput of f_puts() and f_printf().
|
---|
| 104 | / Changed argument of f_chdrive(), f_mkfs(), disk_read() and disk_write().
|
---|
| 105 | / Fixed f_write() can be truncated when the file size is close to 4GB.
|
---|
| 106 | / Fixed f_open(), f_mkdir() and f_setlabel() can return incorrect error code.
|
---|
| 107 | / Jan 15,'14 R0.10a Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID)
|
---|
| 108 | / Added a configuration option of minimum sector size. (_MIN_SS)
|
---|
| 109 | / 2nd argument of f_rename() can have a drive number and it will be ignored.
|
---|
| 110 | / Fixed f_mount() with forced mount fails when drive number is >= 1.
|
---|
| 111 | / Fixed f_close() invalidates the file object without volume lock.
|
---|
| 112 | / Fixed f_closedir() returns but the volume lock is left acquired.
|
---|
| 113 | / Fixed creation of an entry with LFN fails on too many SFN collisions.
|
---|
| 114 | / May 19,'14 R0.10b Fixed a hard error in the disk I/O layer can collapse the directory entry.
|
---|
| 115 | / Fixed LFN entry is not deleted on delete/rename an object with lossy converted SFN.
|
---|
| 116 | / Nov 09,'14 R0.10c Added a configuration option for the platforms without RTC. (_FS_NORTC)
|
---|
| 117 | / Fixed volume label created by Mac OS X cannot be retrieved with f_getlabel().
|
---|
| 118 | / Fixed a potential problem of FAT access that can appear on disk error.
|
---|
| 119 | / Fixed null pointer dereference on attempting to delete the root direcotry.
|
---|
| 120 | / Patch1 applied.
|
---|
| 121 | / Patch2 applied.
|
---|
| 122 | /---------------------------------------------------------------------------*/
|
---|
| 123 |
|
---|
| 124 | #include "ff.h" /* Declarations of FatFs API */
|
---|
| 125 | #include "diskio.h" /* Declarations of disk I/O functions */
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 | /*--------------------------------------------------------------------------
|
---|
| 131 |
|
---|
| 132 | Module Private Definitions
|
---|
| 133 |
|
---|
| 134 | ---------------------------------------------------------------------------*/
|
---|
| 135 |
|
---|
| 136 | #if _FATFS != 80376 /* Revision ID */
|
---|
| 137 | #error Wrong include file (ff.h).
|
---|
| 138 | #endif
|
---|
| 139 |
|
---|
| 140 |
|
---|
| 141 | /* Reentrancy related */
|
---|
| 142 | #if _FS_REENTRANT
|
---|
| 143 | #if _USE_LFN == 1
|
---|
| 144 | #error Static LFN work area cannot be used at thread-safe configuration
|
---|
| 145 | #endif
|
---|
| 146 | #define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; }
|
---|
| 147 | #define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }
|
---|
| 148 | #else
|
---|
| 149 | #define ENTER_FF(fs)
|
---|
| 150 | #define LEAVE_FF(fs, res) return res
|
---|
| 151 | #endif
|
---|
| 152 |
|
---|
| 153 | #define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); }
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | /* Definitions of sector size */
|
---|
| 157 | #if (_MAX_SS < _MIN_SS) || (_MAX_SS != 512 && _MAX_SS != 1024 && _MAX_SS != 2048 && _MAX_SS != 4096) || (_MIN_SS != 512 && _MIN_SS != 1024 && _MIN_SS != 2048 && _MIN_SS != 4096)
|
---|
| 158 | #error Wrong sector size configuration
|
---|
| 159 | #endif
|
---|
| 160 | #if _MAX_SS == _MIN_SS
|
---|
| 161 | #define SS(fs) ((UINT)_MAX_SS) /* Fixed sector size */
|
---|
| 162 | #else
|
---|
| 163 | #define SS(fs) ((fs)->ssize) /* Variable sector size */
|
---|
| 164 | #endif
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 | /* Timestamp feature */
|
---|
| 168 | #if _FS_NORTC == 1
|
---|
| 169 | #if _NORTC_YEAR < 1980 || _NORTC_YEAR > 2107 || _NORTC_MON < 1 || _NORTC_MON > 12 || _NORTC_MDAY < 1 || _NORTC_MDAY > 31
|
---|
| 170 | #error Invalid _FS_NORTC settings
|
---|
| 171 | #endif
|
---|
| 172 | #define GET_FATTIME() ((DWORD)(_NORTC_YEAR - 1980) << 25 | (DWORD)_NORTC_MON << 21 | (DWORD)_NORTC_MDAY << 16)
|
---|
| 173 | #else
|
---|
| 174 | #define GET_FATTIME() get_fattime()
|
---|
| 175 | #endif
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | /* File access control feature */
|
---|
| 179 | #if _FS_LOCK
|
---|
| 180 | #if _FS_READONLY
|
---|
| 181 | #error _FS_LOCK must be 0 at read-only configuration
|
---|
| 182 | #endif
|
---|
| 183 | typedef struct {
|
---|
| 184 | FATFS *fs; /* Object ID 1, volume (NULL:blank entry) */
|
---|
| 185 | DWORD clu; /* Object ID 2, directory (0:root) */
|
---|
| 186 | WORD idx; /* Object ID 3, directory index */
|
---|
| 187 | WORD ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */
|
---|
| 188 | } FILESEM;
|
---|
| 189 | #endif
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 |
|
---|
| 193 | /* DBCS code ranges and SBCS extend character conversion table */
|
---|
| 194 |
|
---|
| 195 | #if _CODE_PAGE == 932 /* Japanese Shift-JIS */
|
---|
| 196 | #define _DF1S 0x81 /* DBC 1st byte range 1 start */
|
---|
| 197 | #define _DF1E 0x9F /* DBC 1st byte range 1 end */
|
---|
| 198 | #define _DF2S 0xE0 /* DBC 1st byte range 2 start */
|
---|
| 199 | #define _DF2E 0xFC /* DBC 1st byte range 2 end */
|
---|
| 200 | #define _DS1S 0x40 /* DBC 2nd byte range 1 start */
|
---|
| 201 | #define _DS1E 0x7E /* DBC 2nd byte range 1 end */
|
---|
| 202 | #define _DS2S 0x80 /* DBC 2nd byte range 2 start */
|
---|
| 203 | #define _DS2E 0xFC /* DBC 2nd byte range 2 end */
|
---|
| 204 |
|
---|
| 205 | #elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
|
---|
| 206 | #define _DF1S 0x81
|
---|
| 207 | #define _DF1E 0xFE
|
---|
| 208 | #define _DS1S 0x40
|
---|
| 209 | #define _DS1E 0x7E
|
---|
| 210 | #define _DS2S 0x80
|
---|
| 211 | #define _DS2E 0xFE
|
---|
| 212 |
|
---|
| 213 | #elif _CODE_PAGE == 949 /* Korean */
|
---|
| 214 | #define _DF1S 0x81
|
---|
| 215 | #define _DF1E 0xFE
|
---|
| 216 | #define _DS1S 0x41
|
---|
| 217 | #define _DS1E 0x5A
|
---|
| 218 | #define _DS2S 0x61
|
---|
| 219 | #define _DS2E 0x7A
|
---|
| 220 | #define _DS3S 0x81
|
---|
| 221 | #define _DS3E 0xFE
|
---|
| 222 |
|
---|
| 223 | #elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */
|
---|
| 224 | #define _DF1S 0x81
|
---|
| 225 | #define _DF1E 0xFE
|
---|
| 226 | #define _DS1S 0x40
|
---|
| 227 | #define _DS1E 0x7E
|
---|
| 228 | #define _DS2S 0xA1
|
---|
| 229 | #define _DS2E 0xFE
|
---|
| 230 |
|
---|
| 231 | #elif _CODE_PAGE == 437 /* U.S. (OEM) */
|
---|
| 232 | #define _DF1S 0
|
---|
| 233 | #define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F,0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
| 234 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 235 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 236 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
| 237 |
|
---|
| 238 | #elif _CODE_PAGE == 720 /* Arabic (OEM) */
|
---|
| 239 | #define _DF1S 0
|
---|
| 240 | #define _EXCVT {0x80,0x81,0x45,0x41,0x84,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x8E,0x8F,0x90,0x92,0x92,0x93,0x94,0x95,0x49,0x49,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
| 241 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 242 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 243 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
| 244 |
|
---|
| 245 | #elif _CODE_PAGE == 737 /* Greek (OEM) */
|
---|
| 246 | #define _DF1S 0
|
---|
| 247 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \
|
---|
| 248 | 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 249 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 250 | 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xE7,0xE8,0xF1,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
| 251 |
|
---|
| 252 | #elif _CODE_PAGE == 775 /* Baltic (OEM) */
|
---|
| 253 | #define _DF1S 0
|
---|
| 254 | #define _EXCVT {0x80,0x9A,0x91,0xA0,0x8E,0x95,0x8F,0x80,0xAD,0xED,0x8A,0x8A,0xA1,0x8D,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0x95,0x96,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
|
---|
| 255 | 0xA0,0xA1,0xE0,0xA3,0xA3,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 256 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 257 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE3,0xE8,0xE8,0xEA,0xEA,0xEE,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
| 258 |
|
---|
| 259 | #elif _CODE_PAGE == 850 /* Multilingual Latin 1 (OEM) */
|
---|
| 260 | #define _DF1S 0
|
---|
| 261 | #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0xDE,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x59,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
|
---|
| 262 | 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 263 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 264 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE7,0xE9,0xEA,0xEB,0xED,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
| 265 |
|
---|
| 266 | #elif _CODE_PAGE == 852 /* Latin 2 (OEM) */
|
---|
| 267 | #define _DF1S 0
|
---|
| 268 | #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F,0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0x9F, \
|
---|
| 269 | 0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA,0x8D,0xAC,0xB8,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBF, \
|
---|
| 270 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC6,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD2,0xD3,0xD2,0xD5,0xD6,0xD7,0xB7,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 271 | 0xE0,0xE1,0xE2,0xE3,0xE3,0xD5,0xE6,0xE6,0xE8,0xE9,0xE8,0xEB,0xED,0xED,0xDD,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xEB,0xFC,0xFC,0xFE,0xFF}
|
---|
| 272 |
|
---|
| 273 | #elif _CODE_PAGE == 855 /* Cyrillic (OEM) */
|
---|
| 274 | #define _DF1S 0
|
---|
| 275 | #define _EXCVT {0x81,0x81,0x83,0x83,0x85,0x85,0x87,0x87,0x89,0x89,0x8B,0x8B,0x8D,0x8D,0x8F,0x8F,0x91,0x91,0x93,0x93,0x95,0x95,0x97,0x97,0x99,0x99,0x9B,0x9B,0x9D,0x9D,0x9F,0x9F, \
|
---|
| 276 | 0xA1,0xA1,0xA3,0xA3,0xA5,0xA5,0xA7,0xA7,0xA9,0xA9,0xAB,0xAB,0xAD,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB6,0xB6,0xB8,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBE,0xBF, \
|
---|
| 277 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD3,0xD3,0xD5,0xD5,0xD7,0xD7,0xDD,0xD9,0xDA,0xDB,0xDC,0xDD,0xE0,0xDF, \
|
---|
| 278 | 0xE0,0xE2,0xE2,0xE4,0xE4,0xE6,0xE6,0xE8,0xE8,0xEA,0xEA,0xEC,0xEC,0xEE,0xEE,0xEF,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFD,0xFE,0xFF}
|
---|
| 279 |
|
---|
| 280 | #elif _CODE_PAGE == 857 /* Turkish (OEM) */
|
---|
| 281 | #define _DF1S 0
|
---|
| 282 | #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0x98,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x98,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9E, \
|
---|
| 283 | 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 284 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 285 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xDE,0x59,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
| 286 |
|
---|
| 287 | #elif _CODE_PAGE == 858 /* Multilingual Latin 1 + Euro (OEM) */
|
---|
| 288 | #define _DF1S 0
|
---|
| 289 | #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0xDE,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x59,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
|
---|
| 290 | 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 291 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 292 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE7,0xE9,0xEA,0xEB,0xED,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
| 293 |
|
---|
| 294 | #elif _CODE_PAGE == 862 /* Hebrew (OEM) */
|
---|
| 295 | #define _DF1S 0
|
---|
| 296 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
| 297 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 298 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 299 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
| 300 |
|
---|
| 301 | #elif _CODE_PAGE == 866 /* Russian (OEM) */
|
---|
| 302 | #define _DF1S 0
|
---|
| 303 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
| 304 | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 305 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 306 | 0x90,0x91,0x92,0x93,0x9d,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
| 307 |
|
---|
| 308 | #elif _CODE_PAGE == 874 /* Thai (OEM, Windows) */
|
---|
| 309 | #define _DF1S 0
|
---|
| 310 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
| 311 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 312 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 313 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
| 314 |
|
---|
| 315 | #elif _CODE_PAGE == 1250 /* Central Europe (Windows) */
|
---|
| 316 | #define _DF1S 0
|
---|
| 317 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x8D,0x8E,0x8F, \
|
---|
| 318 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xA3,0xB4,0xB5,0xB6,0xB7,0xB8,0xA5,0xAA,0xBB,0xBC,0xBD,0xBC,0xAF, \
|
---|
| 319 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 320 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF}
|
---|
| 321 |
|
---|
| 322 | #elif _CODE_PAGE == 1251 /* Cyrillic (Windows) */
|
---|
| 323 | #define _DF1S 0
|
---|
| 324 | #define _EXCVT {0x80,0x81,0x82,0x82,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x80,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x8D,0x8E,0x8F, \
|
---|
| 325 | 0xA0,0xA2,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB2,0xA5,0xB5,0xB6,0xB7,0xA8,0xB9,0xAA,0xBB,0xA3,0xBD,0xBD,0xAF, \
|
---|
| 326 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 327 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF}
|
---|
| 328 |
|
---|
| 329 | #elif _CODE_PAGE == 1252 /* Latin 1 (Windows) */
|
---|
| 330 | #define _DF1S 0
|
---|
| 331 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0xAd,0x9B,0x8C,0x9D,0xAE,0x9F, \
|
---|
| 332 | 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 333 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 334 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F}
|
---|
| 335 |
|
---|
| 336 | #elif _CODE_PAGE == 1253 /* Greek (Windows) */
|
---|
| 337 | #define _DF1S 0
|
---|
| 338 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
| 339 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 340 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xA2,0xB8,0xB9,0xBA, \
|
---|
| 341 | 0xE0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xFB,0xBC,0xFD,0xBF,0xFF}
|
---|
| 342 |
|
---|
| 343 | #elif _CODE_PAGE == 1254 /* Turkish (Windows) */
|
---|
| 344 | #define _DF1S 0
|
---|
| 345 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x9D,0x9E,0x9F, \
|
---|
| 346 | 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 347 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 348 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F}
|
---|
| 349 |
|
---|
| 350 | #elif _CODE_PAGE == 1255 /* Hebrew (Windows) */
|
---|
| 351 | #define _DF1S 0
|
---|
| 352 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
| 353 | 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 354 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 355 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
| 356 |
|
---|
| 357 | #elif _CODE_PAGE == 1256 /* Arabic (Windows) */
|
---|
| 358 | #define _DF1S 0
|
---|
| 359 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x8C,0x9D,0x9E,0x9F, \
|
---|
| 360 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 361 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 362 | 0x41,0xE1,0x41,0xE3,0xE4,0xE5,0xE6,0x43,0x45,0x45,0x45,0x45,0xEC,0xED,0x49,0x49,0xF0,0xF1,0xF2,0xF3,0x4F,0xF5,0xF6,0xF7,0xF8,0x55,0xFA,0x55,0x55,0xFD,0xFE,0xFF}
|
---|
| 363 |
|
---|
| 364 | #elif _CODE_PAGE == 1257 /* Baltic (Windows) */
|
---|
| 365 | #define _DF1S 0
|
---|
| 366 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
| 367 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xA8,0xB9,0xAA,0xBB,0xBC,0xBD,0xBE,0xAF, \
|
---|
| 368 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 369 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF}
|
---|
| 370 |
|
---|
| 371 | #elif _CODE_PAGE == 1258 /* Vietnam (OEM, Windows) */
|
---|
| 372 | #define _DF1S 0
|
---|
| 373 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0xAC,0x9D,0x9E,0x9F, \
|
---|
| 374 | 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
| 375 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
| 376 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xEC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xFE,0x9F}
|
---|
| 377 |
|
---|
| 378 | #elif _CODE_PAGE == 1 /* ASCII (for only non-LFN cfg) */
|
---|
| 379 | #if _USE_LFN
|
---|
| 380 | #error Cannot use LFN feature without valid code page.
|
---|
| 381 | #endif
|
---|
| 382 | #define _DF1S 0
|
---|
| 383 |
|
---|
| 384 | #else
|
---|
| 385 | #error Unknown code page
|
---|
| 386 |
|
---|
| 387 | #endif
|
---|
| 388 |
|
---|
| 389 |
|
---|
| 390 | /* Character code support macros */
|
---|
| 391 | #define IsUpper(c) (((c)>='A')&&((c)<='Z'))
|
---|
| 392 | #define IsLower(c) (((c)>='a')&&((c)<='z'))
|
---|
| 393 | #define IsDigit(c) (((c)>='0')&&((c)<='9'))
|
---|
| 394 |
|
---|
| 395 | #if _DF1S /* Code page is DBCS */
|
---|
| 396 |
|
---|
| 397 | #ifdef _DF2S /* Two 1st byte areas */
|
---|
| 398 | #define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
|
---|
| 399 | #else /* One 1st byte area */
|
---|
| 400 | #define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
|
---|
| 401 | #endif
|
---|
| 402 |
|
---|
| 403 | #ifdef _DS3S /* Three 2nd byte areas */
|
---|
| 404 | #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
|
---|
| 405 | #else /* Two 2nd byte areas */
|
---|
| 406 | #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
|
---|
| 407 | #endif
|
---|
| 408 |
|
---|
| 409 | #else /* Code page is SBCS */
|
---|
| 410 |
|
---|
| 411 | #define IsDBCS1(c) 0
|
---|
| 412 | #define IsDBCS2(c) 0
|
---|
| 413 |
|
---|
| 414 | #endif /* _DF1S */
|
---|
| 415 |
|
---|
| 416 |
|
---|
| 417 | /* Name status flags */
|
---|
| 418 | #define NSFLAG 11 /* Index of name status byte in fn[] */
|
---|
| 419 | #define NS_LOSS 0x01 /* Out of 8.3 format */
|
---|
| 420 | #define NS_LFN 0x02 /* Force to create LFN entry */
|
---|
| 421 | #define NS_LAST 0x04 /* Last segment */
|
---|
| 422 | #define NS_BODY 0x08 /* Lower case flag (body) */
|
---|
| 423 | #define NS_EXT 0x10 /* Lower case flag (ext) */
|
---|
| 424 | #define NS_DOT 0x20 /* Dot entry */
|
---|
| 425 |
|
---|
| 426 |
|
---|
| 427 | /* FAT sub-type boundaries (Differ from specs but correct for real DOS/Windows) */
|
---|
| 428 | #define MIN_FAT16 4086U /* Minimum number of clusters as FAT16 */
|
---|
| 429 | #define MIN_FAT32 65526U /* Minimum number of clusters as FAT32 */
|
---|
| 430 |
|
---|
| 431 |
|
---|
| 432 | /* FatFs refers the members in the FAT structures as byte array instead of
|
---|
| 433 | / structure member because the structure is not binary compatible between
|
---|
| 434 | / different platforms */
|
---|
| 435 |
|
---|
| 436 | #define BS_jmpBoot 0 /* x86 jump instruction (3) */
|
---|
| 437 | #define BS_OEMName 3 /* OEM name (8) */
|
---|
| 438 | #define BPB_BytsPerSec 11 /* Sector size [byte] (2) */
|
---|
| 439 | #define BPB_SecPerClus 13 /* Cluster size [sector] (1) */
|
---|
| 440 | #define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (2) */
|
---|
| 441 | #define BPB_NumFATs 16 /* Number of FAT copies (1) */
|
---|
| 442 | #define BPB_RootEntCnt 17 /* Number of root directory entries for FAT12/16 (2) */
|
---|
| 443 | #define BPB_TotSec16 19 /* Volume size [sector] (2) */
|
---|
| 444 | #define BPB_Media 21 /* Media descriptor (1) */
|
---|
| 445 | #define BPB_FATSz16 22 /* FAT size [sector] (2) */
|
---|
| 446 | #define BPB_SecPerTrk 24 /* Track size [sector] (2) */
|
---|
| 447 | #define BPB_NumHeads 26 /* Number of heads (2) */
|
---|
| 448 | #define BPB_HiddSec 28 /* Number of special hidden sectors (4) */
|
---|
| 449 | #define BPB_TotSec32 32 /* Volume size [sector] (4) */
|
---|
| 450 | #define BS_DrvNum 36 /* Physical drive number (2) */
|
---|
| 451 | #define BS_BootSig 38 /* Extended boot signature (1) */
|
---|
| 452 | #define BS_VolID 39 /* Volume serial number (4) */
|
---|
| 453 | #define BS_VolLab 43 /* Volume label (8) */
|
---|
| 454 | #define BS_FilSysType 54 /* File system type (1) */
|
---|
| 455 | #define BPB_FATSz32 36 /* FAT size [sector] (4) */
|
---|
| 456 | #define BPB_ExtFlags 40 /* Extended flags (2) */
|
---|
| 457 | #define BPB_FSVer 42 /* File system version (2) */
|
---|
| 458 | #define BPB_RootClus 44 /* Root directory first cluster (4) */
|
---|
| 459 | #define BPB_FSInfo 48 /* Offset of FSINFO sector (2) */
|
---|
| 460 | #define BPB_BkBootSec 50 /* Offset of backup boot sector (2) */
|
---|
| 461 | #define BS_DrvNum32 64 /* Physical drive number (2) */
|
---|
| 462 | #define BS_BootSig32 66 /* Extended boot signature (1) */
|
---|
| 463 | #define BS_VolID32 67 /* Volume serial number (4) */
|
---|
| 464 | #define BS_VolLab32 71 /* Volume label (8) */
|
---|
| 465 | #define BS_FilSysType32 82 /* File system type (1) */
|
---|
| 466 | #define FSI_LeadSig 0 /* FSI: Leading signature (4) */
|
---|
| 467 | #define FSI_StrucSig 484 /* FSI: Structure signature (4) */
|
---|
| 468 | #define FSI_Free_Count 488 /* FSI: Number of free clusters (4) */
|
---|
| 469 | #define FSI_Nxt_Free 492 /* FSI: Last allocated cluster (4) */
|
---|
| 470 | #define MBR_Table 446 /* MBR: Partition table offset (2) */
|
---|
| 471 | #define SZ_PTE 16 /* MBR: Size of a partition table entry */
|
---|
| 472 | #define BS_55AA 510 /* Signature word (2) */
|
---|
| 473 |
|
---|
| 474 | #define DIR_Name 0 /* Short file name (11) */
|
---|
| 475 | #define DIR_Attr 11 /* Attribute (1) */
|
---|
| 476 | #define DIR_NTres 12 /* NT flag (1) */
|
---|
| 477 | #define DIR_CrtTimeTenth 13 /* Created time sub-second (1) */
|
---|
| 478 | #define DIR_CrtTime 14 /* Created time (2) */
|
---|
| 479 | #define DIR_CrtDate 16 /* Created date (2) */
|
---|
| 480 | #define DIR_LstAccDate 18 /* Last accessed date (2) */
|
---|
| 481 | #define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (2) */
|
---|
| 482 | #define DIR_WrtTime 22 /* Modified time (2) */
|
---|
| 483 | #define DIR_WrtDate 24 /* Modified date (2) */
|
---|
| 484 | #define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (2) */
|
---|
| 485 | #define DIR_FileSize 28 /* File size (4) */
|
---|
| 486 | #define LDIR_Ord 0 /* LFN entry order and LLE flag (1) */
|
---|
| 487 | #define LDIR_Attr 11 /* LFN attribute (1) */
|
---|
| 488 | #define LDIR_Type 12 /* LFN type (1) */
|
---|
| 489 | #define LDIR_Chksum 13 /* Sum of corresponding SFN entry */
|
---|
| 490 | #define LDIR_FstClusLO 26 /* Filled by zero (0) */
|
---|
| 491 | #define SZ_DIR 32 /* Size of a directory entry */
|
---|
| 492 | #define LLE 0x40 /* Last long entry flag in LDIR_Ord */
|
---|
| 493 | #define DDE 0xE5 /* Deleted directory entry mark in DIR_Name[0] */
|
---|
| 494 | #define NDDE 0x05 /* Replacement of the character collides with DDE */
|
---|
| 495 |
|
---|
| 496 |
|
---|
| 497 |
|
---|
| 498 |
|
---|
| 499 | /*------------------------------------------------------------*/
|
---|
| 500 | /* Module private work area */
|
---|
| 501 | /*------------------------------------------------------------*/
|
---|
| 502 | /* Remark: Uninitialized variables with static duration are
|
---|
| 503 | / guaranteed zero/null at start-up. If not, either the linker
|
---|
| 504 | / or start-up routine being used is out of ANSI-C standard.
|
---|
| 505 | */
|
---|
| 506 |
|
---|
| 507 | #if _VOLUMES < 1 || _VOLUMES > 9
|
---|
| 508 | #error Wrong _VOLUMES setting
|
---|
| 509 | #endif
|
---|
| 510 | static FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical drives) */
|
---|
| 511 | static WORD Fsid; /* File system mount ID */
|
---|
| 512 |
|
---|
| 513 | #if _FS_RPATH && _VOLUMES >= 2
|
---|
| 514 | static BYTE CurrVol; /* Current drive */
|
---|
| 515 | #endif
|
---|
| 516 |
|
---|
| 517 | #if _FS_LOCK
|
---|
| 518 | static FILESEM Files[_FS_LOCK]; /* Open object lock semaphores */
|
---|
| 519 | #endif
|
---|
| 520 |
|
---|
| 521 | #if _USE_LFN == 0 /* Non LFN feature */
|
---|
| 522 | #define DEF_NAMEBUF BYTE sfn[12]
|
---|
| 523 | #define INIT_BUF(dobj) (dobj).fn = sfn
|
---|
| 524 | #define FREE_BUF()
|
---|
| 525 | #else
|
---|
| 526 | #if _MAX_LFN < 12 || _MAX_LFN > 255
|
---|
| 527 | #error Wrong _MAX_LFN setting
|
---|
| 528 | #endif
|
---|
| 529 | #if _USE_LFN == 1 /* LFN feature with static working buffer */
|
---|
| 530 | static WCHAR LfnBuf[_MAX_LFN+1];
|
---|
| 531 | #define DEF_NAMEBUF BYTE sfn[12]
|
---|
| 532 | #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = LfnBuf; }
|
---|
| 533 | #define FREE_BUF()
|
---|
| 534 | #elif _USE_LFN == 2 /* LFN feature with dynamic working buffer on the stack */
|
---|
| 535 | #define DEF_NAMEBUF BYTE sfn[12]; WCHAR lbuf[_MAX_LFN+1]
|
---|
| 536 | #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = lbuf; }
|
---|
| 537 | #define FREE_BUF()
|
---|
| 538 | #elif _USE_LFN == 3 /* LFN feature with dynamic working buffer on the heap */
|
---|
| 539 | #define DEF_NAMEBUF BYTE sfn[12]; WCHAR *lfn
|
---|
| 540 | #define INIT_BUF(dobj) { lfn = ff_memalloc((_MAX_LFN + 1) * 2); if (!lfn) LEAVE_FF((dobj).fs, FR_NOT_ENOUGH_CORE); (dobj).lfn = lfn; (dobj).fn = sfn; }
|
---|
| 541 | #define FREE_BUF() ff_memfree(lfn)
|
---|
| 542 | #else
|
---|
| 543 | #error Wrong _USE_LFN setting
|
---|
| 544 | #endif
|
---|
| 545 | #endif
|
---|
| 546 |
|
---|
| 547 | #ifdef _EXCVT
|
---|
| 548 | static const BYTE ExCvt[] = _EXCVT; /* Upper conversion table for extended characters */
|
---|
| 549 | #endif
|
---|
| 550 |
|
---|
| 551 |
|
---|
| 552 |
|
---|
| 553 |
|
---|
| 554 |
|
---|
| 555 |
|
---|
| 556 | /*--------------------------------------------------------------------------
|
---|
| 557 |
|
---|
| 558 | Module Private Functions
|
---|
| 559 |
|
---|
| 560 | ---------------------------------------------------------------------------*/
|
---|
| 561 |
|
---|
| 562 |
|
---|
| 563 | /*-----------------------------------------------------------------------*/
|
---|
| 564 | /* String functions */
|
---|
| 565 | /*-----------------------------------------------------------------------*/
|
---|
| 566 |
|
---|
| 567 | /* Copy memory to memory */
|
---|
| 568 | static
|
---|
| 569 | void mem_cpy (void* dst, const void* src, UINT cnt) {
|
---|
| 570 | BYTE *d = (BYTE*)dst;
|
---|
| 571 | const BYTE *s = (const BYTE*)src;
|
---|
| 572 |
|
---|
| 573 | #if _WORD_ACCESS == 1
|
---|
| 574 | while (cnt >= sizeof (int)) {
|
---|
| 575 | *(int*)d = *(int*)s;
|
---|
| 576 | d += sizeof (int); s += sizeof (int);
|
---|
| 577 | cnt -= sizeof (int);
|
---|
| 578 | }
|
---|
| 579 | #endif
|
---|
| 580 | while (cnt--)
|
---|
| 581 | *d++ = *s++;
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | /* Fill memory */
|
---|
| 585 | static
|
---|
| 586 | void mem_set (void* dst, int val, UINT cnt) {
|
---|
| 587 | BYTE *d = (BYTE*)dst;
|
---|
| 588 |
|
---|
| 589 | while (cnt--)
|
---|
| 590 | *d++ = (BYTE)val;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | /* Compare memory to memory */
|
---|
| 594 | static
|
---|
| 595 | int mem_cmp (const void* dst, const void* src, UINT cnt) {
|
---|
| 596 | const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
|
---|
| 597 | int r = 0;
|
---|
| 598 |
|
---|
| 599 | while (cnt-- && (r = *d++ - *s++) == 0) ;
|
---|
| 600 | return r;
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | /* Check if chr is contained in the string */
|
---|
| 604 | static
|
---|
| 605 | int chk_chr (const char* str, int chr) {
|
---|
| 606 | while (*str && *str != chr) str++;
|
---|
| 607 | return *str;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 |
|
---|
| 611 |
|
---|
| 612 |
|
---|
| 613 | /*-----------------------------------------------------------------------*/
|
---|
| 614 | /* Request/Release grant to access the volume */
|
---|
| 615 | /*-----------------------------------------------------------------------*/
|
---|
| 616 | #if _FS_REENTRANT
|
---|
| 617 | static
|
---|
| 618 | int lock_fs (
|
---|
| 619 | FATFS* fs /* File system object */
|
---|
| 620 | )
|
---|
| 621 | {
|
---|
| 622 | return ff_req_grant(fs->sobj);
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 |
|
---|
| 626 | static
|
---|
| 627 | void unlock_fs (
|
---|
| 628 | FATFS* fs, /* File system object */
|
---|
| 629 | FRESULT res /* Result code to be returned */
|
---|
| 630 | )
|
---|
| 631 | {
|
---|
| 632 | if (fs &&
|
---|
| 633 | res != FR_NOT_ENABLED &&
|
---|
| 634 | res != FR_INVALID_DRIVE &&
|
---|
| 635 | res != FR_INVALID_OBJECT &&
|
---|
| 636 | res != FR_TIMEOUT) {
|
---|
| 637 | ff_rel_grant(fs->sobj);
|
---|
| 638 | }
|
---|
| 639 | }
|
---|
| 640 | #endif
|
---|
| 641 |
|
---|
| 642 |
|
---|
| 643 |
|
---|
| 644 |
|
---|
| 645 | /*-----------------------------------------------------------------------*/
|
---|
| 646 | /* File lock control functions */
|
---|
| 647 | /*-----------------------------------------------------------------------*/
|
---|
| 648 | #if _FS_LOCK
|
---|
| 649 |
|
---|
| 650 | static
|
---|
| 651 | FRESULT chk_lock ( /* Check if the file can be accessed */
|
---|
| 652 | DIR* dp, /* Directory object pointing the file to be checked */
|
---|
| 653 | int acc /* Desired access type (0:Read, 1:Write, 2:Delete/Rename) */
|
---|
| 654 | )
|
---|
| 655 | {
|
---|
| 656 | UINT i, be;
|
---|
| 657 |
|
---|
| 658 | /* Search file semaphore table */
|
---|
| 659 | for (i = be = 0; i < _FS_LOCK; i++) {
|
---|
| 660 | if (Files[i].fs) { /* Existing entry */
|
---|
| 661 | if (Files[i].fs == dp->fs && /* Check if the object matched with an open object */
|
---|
| 662 | Files[i].clu == dp->sclust &&
|
---|
| 663 | Files[i].idx == dp->index) break;
|
---|
| 664 | } else { /* Blank entry */
|
---|
| 665 | be = 1;
|
---|
| 666 | }
|
---|
| 667 | }
|
---|
| 668 | if (i == _FS_LOCK) /* The object is not opened */
|
---|
| 669 | return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES; /* Is there a blank entry for new object? */
|
---|
| 670 |
|
---|
| 671 | /* The object has been opened. Reject any open against writing file and all write mode open */
|
---|
| 672 | return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 |
|
---|
| 676 | static
|
---|
| 677 | int enq_lock (void) /* Check if an entry is available for a new object */
|
---|
| 678 | {
|
---|
| 679 | UINT i;
|
---|
| 680 |
|
---|
| 681 | for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
|
---|
| 682 | return (i == _FS_LOCK) ? 0 : 1;
|
---|
| 683 | }
|
---|
| 684 |
|
---|
| 685 |
|
---|
| 686 | static
|
---|
| 687 | UINT inc_lock ( /* Increment object open counter and returns its index (0:Internal error) */
|
---|
| 688 | DIR* dp, /* Directory object pointing the file to register or increment */
|
---|
| 689 | int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */
|
---|
| 690 | )
|
---|
| 691 | {
|
---|
| 692 | UINT i;
|
---|
| 693 |
|
---|
| 694 |
|
---|
| 695 | for (i = 0; i < _FS_LOCK; i++) { /* Find the object */
|
---|
| 696 | if (Files[i].fs == dp->fs &&
|
---|
| 697 | Files[i].clu == dp->sclust &&
|
---|
| 698 | Files[i].idx == dp->index) break;
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 | if (i == _FS_LOCK) { /* Not opened. Register it as new. */
|
---|
| 702 | for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
|
---|
| 703 | if (i == _FS_LOCK) return 0; /* No free entry to register (int err) */
|
---|
| 704 | Files[i].fs = dp->fs;
|
---|
| 705 | Files[i].clu = dp->sclust;
|
---|
| 706 | Files[i].idx = dp->index;
|
---|
| 707 | Files[i].ctr = 0;
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | if (acc && Files[i].ctr) return 0; /* Access violation (int err) */
|
---|
| 711 |
|
---|
| 712 | Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */
|
---|
| 713 |
|
---|
| 714 | return i + 1;
|
---|
| 715 | }
|
---|
| 716 |
|
---|
| 717 |
|
---|
| 718 | static
|
---|
| 719 | FRESULT dec_lock ( /* Decrement object open counter */
|
---|
| 720 | UINT i /* Semaphore index (1..) */
|
---|
| 721 | )
|
---|
| 722 | {
|
---|
| 723 | WORD n;
|
---|
| 724 | FRESULT res;
|
---|
| 725 |
|
---|
| 726 |
|
---|
| 727 | if (--i < _FS_LOCK) { /* Shift index number origin from 0 */
|
---|
| 728 | n = Files[i].ctr;
|
---|
| 729 | if (n == 0x100) n = 0; /* If write mode open, delete the entry */
|
---|
| 730 | if (n) n--; /* Decrement read mode open count */
|
---|
| 731 | Files[i].ctr = n;
|
---|
| 732 | if (!n) Files[i].fs = 0; /* Delete the entry if open count gets zero */
|
---|
| 733 | res = FR_OK;
|
---|
| 734 | } else {
|
---|
| 735 | res = FR_INT_ERR; /* Invalid index nunber */
|
---|
| 736 | }
|
---|
| 737 | return res;
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 |
|
---|
| 741 | static
|
---|
| 742 | void clear_lock ( /* Clear lock entries of the volume */
|
---|
| 743 | FATFS *fs
|
---|
| 744 | )
|
---|
| 745 | {
|
---|
| 746 | UINT i;
|
---|
| 747 |
|
---|
| 748 | for (i = 0; i < _FS_LOCK; i++) {
|
---|
| 749 | if (Files[i].fs == fs) Files[i].fs = 0;
|
---|
| 750 | }
|
---|
| 751 | }
|
---|
| 752 | #endif
|
---|
| 753 |
|
---|
| 754 |
|
---|
| 755 |
|
---|
| 756 |
|
---|
| 757 | /*-----------------------------------------------------------------------*/
|
---|
| 758 | /* Move/Flush disk access window in the file system object */
|
---|
| 759 | /*-----------------------------------------------------------------------*/
|
---|
| 760 | #if !_FS_READONLY
|
---|
| 761 | static
|
---|
| 762 | FRESULT sync_window (
|
---|
| 763 | FATFS* fs /* File system object */
|
---|
| 764 | )
|
---|
| 765 | {
|
---|
| 766 | DWORD wsect;
|
---|
| 767 | UINT nf;
|
---|
| 768 | FRESULT res = FR_OK;
|
---|
| 769 |
|
---|
| 770 |
|
---|
| 771 | if (fs->wflag) { /* Write back the sector if it is dirty */
|
---|
| 772 | wsect = fs->winsect; /* Current sector number */
|
---|
| 773 | if (disk_write(fs->drv, fs->win, wsect, 1) != RES_OK) {
|
---|
| 774 | res = FR_DISK_ERR;
|
---|
| 775 | } else {
|
---|
| 776 | fs->wflag = 0;
|
---|
| 777 | if (wsect - fs->fatbase < fs->fsize) { /* Is it in the FAT area? */
|
---|
| 778 | for (nf = fs->n_fats; nf >= 2; nf--) { /* Reflect the change to all FAT copies */
|
---|
| 779 | wsect += fs->fsize;
|
---|
| 780 | disk_write(fs->drv, fs->win, wsect, 1);
|
---|
| 781 | }
|
---|
| 782 | }
|
---|
| 783 | }
|
---|
| 784 | }
|
---|
| 785 | return res;
|
---|
| 786 | }
|
---|
| 787 | #endif
|
---|
| 788 |
|
---|
| 789 |
|
---|
| 790 | static
|
---|
| 791 | FRESULT move_window (
|
---|
| 792 | FATFS* fs, /* File system object */
|
---|
| 793 | DWORD sector /* Sector number to make appearance in the fs->win[] */
|
---|
| 794 | )
|
---|
| 795 | {
|
---|
| 796 | FRESULT res = FR_OK;
|
---|
| 797 |
|
---|
| 798 |
|
---|
| 799 | if (sector != fs->winsect) { /* Window offset changed? */
|
---|
| 800 | #if !_FS_READONLY
|
---|
| 801 | res = sync_window(fs); /* Write-back changes */
|
---|
| 802 | #endif
|
---|
| 803 | if (res == FR_OK) { /* Fill sector window with new data */
|
---|
| 804 | if (disk_read(fs->drv, fs->win, sector, 1) != RES_OK) {
|
---|
| 805 | sector = 0xFFFFFFFF; /* Invalidate window if data is not reliable */
|
---|
| 806 | res = FR_DISK_ERR;
|
---|
| 807 | }
|
---|
| 808 | fs->winsect = sector;
|
---|
| 809 | }
|
---|
| 810 | }
|
---|
| 811 | return res;
|
---|
| 812 | }
|
---|
| 813 |
|
---|
| 814 |
|
---|
| 815 |
|
---|
| 816 |
|
---|
| 817 | /*-----------------------------------------------------------------------*/
|
---|
| 818 | /* Synchronize file system and strage device */
|
---|
| 819 | /*-----------------------------------------------------------------------*/
|
---|
| 820 | #if !_FS_READONLY
|
---|
| 821 | static
|
---|
| 822 | FRESULT sync_fs ( /* FR_OK: successful, FR_DISK_ERR: failed */
|
---|
| 823 | FATFS* fs /* File system object */
|
---|
| 824 | )
|
---|
| 825 | {
|
---|
| 826 | FRESULT res;
|
---|
| 827 |
|
---|
| 828 |
|
---|
| 829 | res = sync_window(fs);
|
---|
| 830 | if (res == FR_OK) {
|
---|
| 831 | /* Update FSINFO sector if needed */
|
---|
| 832 | if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) {
|
---|
| 833 | /* Create FSINFO structure */
|
---|
| 834 | mem_set(fs->win, 0, SS(fs));
|
---|
| 835 | ST_WORD(fs->win+BS_55AA, 0xAA55);
|
---|
| 836 | ST_DWORD(fs->win+FSI_LeadSig, 0x41615252);
|
---|
| 837 | ST_DWORD(fs->win+FSI_StrucSig, 0x61417272);
|
---|
| 838 | ST_DWORD(fs->win+FSI_Free_Count, fs->free_clust);
|
---|
| 839 | ST_DWORD(fs->win+FSI_Nxt_Free, fs->last_clust);
|
---|
| 840 | /* Write it into the FSINFO sector */
|
---|
| 841 | fs->winsect = fs->volbase + 1;
|
---|
| 842 | disk_write(fs->drv, fs->win, fs->winsect, 1);
|
---|
| 843 | fs->fsi_flag = 0;
|
---|
| 844 | }
|
---|
| 845 | /* Make sure that no pending write process in the physical drive */
|
---|
| 846 | if (disk_ioctl(fs->drv, CTRL_SYNC, 0) != RES_OK)
|
---|
| 847 | res = FR_DISK_ERR;
|
---|
| 848 | }
|
---|
| 849 |
|
---|
| 850 | return res;
|
---|
| 851 | }
|
---|
| 852 | #endif
|
---|
| 853 |
|
---|
| 854 |
|
---|
| 855 |
|
---|
| 856 |
|
---|
| 857 | /*-----------------------------------------------------------------------*/
|
---|
| 858 | /* Get sector# from cluster# */
|
---|
| 859 | /*-----------------------------------------------------------------------*/
|
---|
| 860 | /* Hidden API for hacks and disk tools */
|
---|
| 861 |
|
---|
| 862 | DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */
|
---|
| 863 | FATFS* fs, /* File system object */
|
---|
| 864 | DWORD clst /* Cluster# to be converted */
|
---|
| 865 | )
|
---|
| 866 | {
|
---|
| 867 | clst -= 2;
|
---|
| 868 | if (clst >= fs->n_fatent - 2) return 0; /* Invalid cluster# */
|
---|
| 869 | return clst * fs->csize + fs->database;
|
---|
| 870 | }
|
---|
| 871 |
|
---|
| 872 |
|
---|
| 873 |
|
---|
| 874 |
|
---|
| 875 | /*-----------------------------------------------------------------------*/
|
---|
| 876 | /* FAT access - Read value of a FAT entry */
|
---|
| 877 | /*-----------------------------------------------------------------------*/
|
---|
| 878 | /* Hidden API for hacks and disk tools */
|
---|
| 879 |
|
---|
| 880 | DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x0FFFFFFF:Cluster status */
|
---|
| 881 | FATFS* fs, /* File system object */
|
---|
| 882 | DWORD clst /* FAT item index (cluster#) to get the value */
|
---|
| 883 | )
|
---|
| 884 | {
|
---|
| 885 | UINT wc, bc;
|
---|
| 886 | BYTE *p;
|
---|
| 887 | DWORD val;
|
---|
| 888 |
|
---|
| 889 |
|
---|
| 890 | if (clst < 2 || clst >= fs->n_fatent) { /* Check range */
|
---|
| 891 | val = 1; /* Internal error */
|
---|
| 892 |
|
---|
| 893 | } else {
|
---|
| 894 | val = 0xFFFFFFFF; /* Default value falls on disk error */
|
---|
| 895 |
|
---|
| 896 | switch (fs->fs_type) {
|
---|
| 897 | case FS_FAT12 :
|
---|
| 898 | bc = (UINT)clst; bc += bc / 2;
|
---|
| 899 | if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;
|
---|
| 900 | wc = fs->win[bc++ % SS(fs)];
|
---|
| 901 | if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;
|
---|
| 902 | wc |= fs->win[bc % SS(fs)] << 8;
|
---|
| 903 | val = clst & 1 ? wc >> 4 : (wc & 0xFFF);
|
---|
| 904 | break;
|
---|
| 905 |
|
---|
| 906 | case FS_FAT16 :
|
---|
| 907 | if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))) != FR_OK) break;
|
---|
| 908 | p = &fs->win[clst * 2 % SS(fs)];
|
---|
| 909 | val = LD_WORD(p);
|
---|
| 910 | break;
|
---|
| 911 |
|
---|
| 912 | case FS_FAT32 :
|
---|
| 913 | if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;
|
---|
| 914 | p = &fs->win[clst * 4 % SS(fs)];
|
---|
| 915 | val = LD_DWORD(p) & 0x0FFFFFFF;
|
---|
| 916 | break;
|
---|
| 917 |
|
---|
| 918 | default:
|
---|
| 919 | val = 1; /* Internal error */
|
---|
| 920 | }
|
---|
| 921 | }
|
---|
| 922 |
|
---|
| 923 | return val;
|
---|
| 924 | }
|
---|
| 925 |
|
---|
| 926 |
|
---|
| 927 |
|
---|
| 928 |
|
---|
| 929 | /*-----------------------------------------------------------------------*/
|
---|
| 930 | /* FAT access - Change value of a FAT entry */
|
---|
| 931 | /*-----------------------------------------------------------------------*/
|
---|
| 932 | /* Hidden API for hacks and disk tools */
|
---|
| 933 |
|
---|
| 934 | #if !_FS_READONLY
|
---|
| 935 | FRESULT put_fat (
|
---|
| 936 | FATFS* fs, /* File system object */
|
---|
| 937 | DWORD clst, /* FAT item index (cluster#) to be set */
|
---|
| 938 | DWORD val /* New value to mark the cluster */
|
---|
| 939 | )
|
---|
| 940 | {
|
---|
| 941 | UINT bc;
|
---|
| 942 | BYTE *p;
|
---|
| 943 | FRESULT res;
|
---|
| 944 |
|
---|
| 945 |
|
---|
| 946 | if (clst < 2 || clst >= fs->n_fatent) { /* Check range */
|
---|
| 947 | res = FR_INT_ERR;
|
---|
| 948 |
|
---|
| 949 | } else {
|
---|
| 950 | switch (fs->fs_type) {
|
---|
| 951 | case FS_FAT12 :
|
---|
| 952 | bc = (UINT)clst; bc += bc / 2;
|
---|
| 953 | res = move_window(fs, fs->fatbase + (bc / SS(fs)));
|
---|
| 954 | if (res != FR_OK) break;
|
---|
| 955 | p = &fs->win[bc++ % SS(fs)];
|
---|
| 956 | *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
|
---|
| 957 | fs->wflag = 1;
|
---|
| 958 | res = move_window(fs, fs->fatbase + (bc / SS(fs)));
|
---|
| 959 | if (res != FR_OK) break;
|
---|
| 960 | p = &fs->win[bc % SS(fs)];
|
---|
| 961 | *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
|
---|
| 962 | fs->wflag = 1;
|
---|
| 963 | break;
|
---|
| 964 |
|
---|
| 965 | case FS_FAT16 :
|
---|
| 966 | res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
|
---|
| 967 | if (res != FR_OK) break;
|
---|
| 968 | p = &fs->win[clst * 2 % SS(fs)];
|
---|
| 969 | ST_WORD(p, (WORD)val);
|
---|
| 970 | fs->wflag = 1;
|
---|
| 971 | break;
|
---|
| 972 |
|
---|
| 973 | case FS_FAT32 :
|
---|
| 974 | res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
|
---|
| 975 | if (res != FR_OK) break;
|
---|
| 976 | p = &fs->win[clst * 4 % SS(fs)];
|
---|
| 977 | val |= LD_DWORD(p) & 0xF0000000;
|
---|
| 978 | ST_DWORD(p, val);
|
---|
| 979 | fs->wflag = 1;
|
---|
| 980 | break;
|
---|
| 981 |
|
---|
| 982 | default :
|
---|
| 983 | res = FR_INT_ERR;
|
---|
| 984 | }
|
---|
| 985 | }
|
---|
| 986 |
|
---|
| 987 | return res;
|
---|
| 988 | }
|
---|
| 989 | #endif /* !_FS_READONLY */
|
---|
| 990 |
|
---|
| 991 |
|
---|
| 992 |
|
---|
| 993 |
|
---|
| 994 | /*-----------------------------------------------------------------------*/
|
---|
| 995 | /* FAT handling - Remove a cluster chain */
|
---|
| 996 | /*-----------------------------------------------------------------------*/
|
---|
| 997 | #if !_FS_READONLY
|
---|
| 998 | static
|
---|
| 999 | FRESULT remove_chain (
|
---|
| 1000 | FATFS* fs, /* File system object */
|
---|
| 1001 | DWORD clst /* Cluster# to remove a chain from */
|
---|
| 1002 | )
|
---|
| 1003 | {
|
---|
| 1004 | FRESULT res;
|
---|
| 1005 | DWORD nxt;
|
---|
| 1006 | #if _USE_TRIM
|
---|
| 1007 | DWORD scl = clst, ecl = clst, rt[2];
|
---|
| 1008 | #endif
|
---|
| 1009 |
|
---|
| 1010 | if (clst < 2 || clst >= fs->n_fatent) { /* Check range */
|
---|
| 1011 | res = FR_INT_ERR;
|
---|
| 1012 |
|
---|
| 1013 | } else {
|
---|
| 1014 | res = FR_OK;
|
---|
| 1015 | while (clst < fs->n_fatent) { /* Not a last link? */
|
---|
| 1016 | nxt = get_fat(fs, clst); /* Get cluster status */
|
---|
| 1017 | if (nxt == 0) break; /* Empty cluster? */
|
---|
| 1018 | if (nxt == 1) { res = FR_INT_ERR; break; } /* Internal error? */
|
---|
| 1019 | if (nxt == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } /* Disk error? */
|
---|
| 1020 | res = put_fat(fs, clst, 0); /* Mark the cluster "empty" */
|
---|
| 1021 | if (res != FR_OK) break;
|
---|
| 1022 | if (fs->free_clust != 0xFFFFFFFF) { /* Update FSINFO */
|
---|
| 1023 | fs->free_clust++;
|
---|
| 1024 | fs->fsi_flag |= 1;
|
---|
| 1025 | }
|
---|
| 1026 | #if _USE_TRIM
|
---|
| 1027 | if (ecl + 1 == nxt) { /* Is next cluster contiguous? */
|
---|
| 1028 | ecl = nxt;
|
---|
| 1029 | } else { /* End of contiguous clusters */
|
---|
| 1030 | rt[0] = clust2sect(fs, scl); /* Start sector */
|
---|
| 1031 | rt[1] = clust2sect(fs, ecl) + fs->csize - 1; /* End sector */
|
---|
| 1032 | disk_ioctl(fs->drv, CTRL_TRIM, rt); /* Erase the block */
|
---|
| 1033 | scl = ecl = nxt;
|
---|
| 1034 | }
|
---|
| 1035 | #endif
|
---|
| 1036 | clst = nxt; /* Next cluster */
|
---|
| 1037 | }
|
---|
| 1038 | }
|
---|
| 1039 |
|
---|
| 1040 | return res;
|
---|
| 1041 | }
|
---|
| 1042 | #endif
|
---|
| 1043 |
|
---|
| 1044 |
|
---|
| 1045 |
|
---|
| 1046 |
|
---|
| 1047 | /*-----------------------------------------------------------------------*/
|
---|
| 1048 | /* FAT handling - Stretch or Create a cluster chain */
|
---|
| 1049 | /*-----------------------------------------------------------------------*/
|
---|
| 1050 | #if !_FS_READONLY
|
---|
| 1051 | static
|
---|
| 1052 | DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */
|
---|
| 1053 | FATFS* fs, /* File system object */
|
---|
| 1054 | DWORD clst /* Cluster# to stretch. 0 means create a new chain. */
|
---|
| 1055 | )
|
---|
| 1056 | {
|
---|
| 1057 | DWORD cs, ncl, scl;
|
---|
| 1058 | FRESULT res;
|
---|
| 1059 |
|
---|
| 1060 |
|
---|
| 1061 | if (clst == 0) { /* Create a new chain */
|
---|
| 1062 | scl = fs->last_clust; /* Get suggested start point */
|
---|
| 1063 | if (!scl || scl >= fs->n_fatent) scl = 1;
|
---|
| 1064 | }
|
---|
| 1065 | else { /* Stretch the current chain */
|
---|
| 1066 | cs = get_fat(fs, clst); /* Check the cluster status */
|
---|
| 1067 | if (cs < 2) return 1; /* Invalid value */
|
---|
| 1068 | if (cs == 0xFFFFFFFF) return cs; /* A disk error occurred */
|
---|
| 1069 | if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */
|
---|
| 1070 | scl = clst;
|
---|
| 1071 | }
|
---|
| 1072 |
|
---|
| 1073 | ncl = scl; /* Start cluster */
|
---|
| 1074 | for (;;) {
|
---|
| 1075 | ncl++; /* Next cluster */
|
---|
| 1076 | if (ncl >= fs->n_fatent) { /* Check wrap around */
|
---|
| 1077 | ncl = 2;
|
---|
| 1078 | if (ncl > scl) return 0; /* No free cluster */
|
---|
| 1079 | }
|
---|
| 1080 | cs = get_fat(fs, ncl); /* Get the cluster status */
|
---|
| 1081 | if (cs == 0) break; /* Found a free cluster */
|
---|
| 1082 | if (cs == 0xFFFFFFFF || cs == 1)/* An error occurred */
|
---|
| 1083 | return cs;
|
---|
| 1084 | if (ncl == scl) return 0; /* No free cluster */
|
---|
| 1085 | }
|
---|
| 1086 |
|
---|
| 1087 | res = put_fat(fs, ncl, 0x0FFFFFFF); /* Mark the new cluster "last link" */
|
---|
| 1088 | if (res == FR_OK && clst != 0) {
|
---|
| 1089 | res = put_fat(fs, clst, ncl); /* Link it to the previous one if needed */
|
---|
| 1090 | }
|
---|
| 1091 | if (res == FR_OK) {
|
---|
| 1092 | fs->last_clust = ncl; /* Update FSINFO */
|
---|
| 1093 | if (fs->free_clust != 0xFFFFFFFF) {
|
---|
| 1094 | fs->free_clust--;
|
---|
| 1095 | fs->fsi_flag |= 1;
|
---|
| 1096 | }
|
---|
| 1097 | } else {
|
---|
| 1098 | ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1;
|
---|
| 1099 | }
|
---|
| 1100 |
|
---|
| 1101 | return ncl; /* Return new cluster number or error code */
|
---|
| 1102 | }
|
---|
| 1103 | #endif /* !_FS_READONLY */
|
---|
| 1104 |
|
---|
| 1105 |
|
---|
| 1106 |
|
---|
| 1107 |
|
---|
| 1108 | /*-----------------------------------------------------------------------*/
|
---|
| 1109 | /* FAT handling - Convert offset into cluster with link map table */
|
---|
| 1110 | /*-----------------------------------------------------------------------*/
|
---|
| 1111 |
|
---|
| 1112 | #if _USE_FASTSEEK
|
---|
| 1113 | static
|
---|
| 1114 | DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */
|
---|
| 1115 | FIL* fp, /* Pointer to the file object */
|
---|
| 1116 | DWORD ofs /* File offset to be converted to cluster# */
|
---|
| 1117 | )
|
---|
| 1118 | {
|
---|
| 1119 | DWORD cl, ncl, *tbl;
|
---|
| 1120 |
|
---|
| 1121 |
|
---|
| 1122 | tbl = fp->cltbl + 1; /* Top of CLMT */
|
---|
| 1123 | cl = ofs / SS(fp->fs) / fp->fs->csize; /* Cluster order from top of the file */
|
---|
| 1124 | for (;;) {
|
---|
| 1125 | ncl = *tbl++; /* Number of cluters in the fragment */
|
---|
| 1126 | if (!ncl) return 0; /* End of table? (error) */
|
---|
| 1127 | if (cl < ncl) break; /* In this fragment? */
|
---|
| 1128 | cl -= ncl; tbl++; /* Next fragment */
|
---|
| 1129 | }
|
---|
| 1130 | return cl + *tbl; /* Return the cluster number */
|
---|
| 1131 | }
|
---|
| 1132 | #endif /* _USE_FASTSEEK */
|
---|
| 1133 |
|
---|
| 1134 |
|
---|
| 1135 |
|
---|
| 1136 |
|
---|
| 1137 | /*-----------------------------------------------------------------------*/
|
---|
| 1138 | /* Directory handling - Set directory index */
|
---|
| 1139 | /*-----------------------------------------------------------------------*/
|
---|
| 1140 |
|
---|
| 1141 | static
|
---|
| 1142 | FRESULT dir_sdi (
|
---|
| 1143 | DIR* dp, /* Pointer to directory object */
|
---|
| 1144 | UINT idx /* Index of directory table */
|
---|
| 1145 | )
|
---|
| 1146 | {
|
---|
| 1147 | DWORD clst, sect;
|
---|
| 1148 | UINT ic;
|
---|
| 1149 |
|
---|
| 1150 |
|
---|
| 1151 | dp->index = (WORD)idx; /* Current index */
|
---|
| 1152 | clst = dp->sclust; /* Table start cluster (0:root) */
|
---|
| 1153 | if (clst == 1 || clst >= dp->fs->n_fatent) /* Check start cluster range */
|
---|
| 1154 | return FR_INT_ERR;
|
---|
| 1155 | if (!clst && dp->fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */
|
---|
| 1156 | clst = dp->fs->dirbase;
|
---|
| 1157 |
|
---|
| 1158 | if (clst == 0) { /* Static table (root-directory in FAT12/16) */
|
---|
| 1159 | if (idx >= dp->fs->n_rootdir) /* Is index out of range? */
|
---|
| 1160 | return FR_INT_ERR;
|
---|
| 1161 | sect = dp->fs->dirbase;
|
---|
| 1162 | }
|
---|
| 1163 | else { /* Dynamic table (root-directory in FAT32 or sub-directory) */
|
---|
| 1164 | ic = SS(dp->fs) / SZ_DIR * dp->fs->csize; /* Entries per cluster */
|
---|
| 1165 | while (idx >= ic) { /* Follow cluster chain */
|
---|
| 1166 | clst = get_fat(dp->fs, clst); /* Get next cluster */
|
---|
| 1167 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
|
---|
| 1168 | if (clst < 2 || clst >= dp->fs->n_fatent) /* Reached to end of table or internal error */
|
---|
| 1169 | return FR_INT_ERR;
|
---|
| 1170 | idx -= ic;
|
---|
| 1171 | }
|
---|
| 1172 | sect = clust2sect(dp->fs, clst);
|
---|
| 1173 | }
|
---|
| 1174 | dp->clust = clst; /* Current cluster# */
|
---|
| 1175 | if (!sect) return FR_INT_ERR;
|
---|
| 1176 | dp->sect = sect + idx / (SS(dp->fs) / SZ_DIR); /* Sector# of the directory entry */
|
---|
| 1177 | dp->dir = dp->fs->win + (idx % (SS(dp->fs) / SZ_DIR)) * SZ_DIR; /* Ptr to the entry in the sector */
|
---|
| 1178 |
|
---|
| 1179 | return FR_OK;
|
---|
| 1180 | }
|
---|
| 1181 |
|
---|
| 1182 |
|
---|
| 1183 |
|
---|
| 1184 |
|
---|
| 1185 | /*-----------------------------------------------------------------------*/
|
---|
| 1186 | /* Directory handling - Move directory table index next */
|
---|
| 1187 | /*-----------------------------------------------------------------------*/
|
---|
| 1188 |
|
---|
| 1189 | static
|
---|
| 1190 | FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:Could not stretch */
|
---|
| 1191 | DIR* dp, /* Pointer to the directory object */
|
---|
| 1192 | int stretch /* 0: Do not stretch table, 1: Stretch table if needed */
|
---|
| 1193 | )
|
---|
| 1194 | {
|
---|
| 1195 | DWORD clst;
|
---|
| 1196 | UINT i;
|
---|
| 1197 |
|
---|
| 1198 |
|
---|
| 1199 | i = dp->index + 1;
|
---|
| 1200 | if (!(i & 0xFFFF) || !dp->sect) /* Report EOT when index has reached 65535 */
|
---|
| 1201 | return FR_NO_FILE;
|
---|
| 1202 |
|
---|
| 1203 | if (!(i % (SS(dp->fs) / SZ_DIR))) { /* Sector changed? */
|
---|
| 1204 | dp->sect++; /* Next sector */
|
---|
| 1205 |
|
---|
| 1206 | if (!dp->clust) { /* Static table */
|
---|
| 1207 | if (i >= dp->fs->n_rootdir) /* Report EOT if it reached end of static table */
|
---|
| 1208 | return FR_NO_FILE;
|
---|
| 1209 | }
|
---|
| 1210 | else { /* Dynamic table */
|
---|
| 1211 | if (((i / (SS(dp->fs) / SZ_DIR)) & (dp->fs->csize - 1)) == 0) { /* Cluster changed? */
|
---|
| 1212 | clst = get_fat(dp->fs, dp->clust); /* Get next cluster */
|
---|
| 1213 | if (clst <= 1) return FR_INT_ERR;
|
---|
| 1214 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
|
---|
| 1215 | if (clst >= dp->fs->n_fatent) { /* If it reached end of dynamic table, */
|
---|
| 1216 | #if !_FS_READONLY
|
---|
| 1217 | UINT c;
|
---|
| 1218 | if (!stretch) return FR_NO_FILE; /* If do not stretch, report EOT */
|
---|
| 1219 | clst = create_chain(dp->fs, dp->clust); /* Stretch cluster chain */
|
---|
| 1220 | if (clst == 0) return FR_DENIED; /* No free cluster */
|
---|
| 1221 | if (clst == 1) return FR_INT_ERR;
|
---|
| 1222 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
|
---|
| 1223 | /* Clean-up stretched table */
|
---|
| 1224 | if (sync_window(dp->fs)) return FR_DISK_ERR;/* Flush disk access window */
|
---|
| 1225 | mem_set(dp->fs->win, 0, SS(dp->fs)); /* Clear window buffer */
|
---|
| 1226 | dp->fs->winsect = clust2sect(dp->fs, clst); /* Cluster start sector */
|
---|
| 1227 | for (c = 0; c < dp->fs->csize; c++) { /* Fill the new cluster with 0 */
|
---|
| 1228 | dp->fs->wflag = 1;
|
---|
| 1229 | if (sync_window(dp->fs)) return FR_DISK_ERR;
|
---|
| 1230 | dp->fs->winsect++;
|
---|
| 1231 | }
|
---|
| 1232 | dp->fs->winsect -= c; /* Rewind window offset */
|
---|
| 1233 | #else
|
---|
| 1234 | if (!stretch) return FR_NO_FILE; /* If do not stretch, report EOT (this is to suppress warning) */
|
---|
| 1235 | return FR_NO_FILE; /* Report EOT */
|
---|
| 1236 | #endif
|
---|
| 1237 | }
|
---|
| 1238 | dp->clust = clst; /* Initialize data for new cluster */
|
---|
| 1239 | dp->sect = clust2sect(dp->fs, clst);
|
---|
| 1240 | }
|
---|
| 1241 | }
|
---|
| 1242 | }
|
---|
| 1243 |
|
---|
| 1244 | dp->index = (WORD)i; /* Current index */
|
---|
| 1245 | dp->dir = dp->fs->win + (i % (SS(dp->fs) / SZ_DIR)) * SZ_DIR; /* Current entry in the window */
|
---|
| 1246 |
|
---|
| 1247 | return FR_OK;
|
---|
| 1248 | }
|
---|
| 1249 |
|
---|
| 1250 |
|
---|
| 1251 |
|
---|
| 1252 |
|
---|
| 1253 | /*-----------------------------------------------------------------------*/
|
---|
| 1254 | /* Directory handling - Reserve directory entry */
|
---|
| 1255 | /*-----------------------------------------------------------------------*/
|
---|
| 1256 |
|
---|
| 1257 | #if !_FS_READONLY
|
---|
| 1258 | static
|
---|
| 1259 | FRESULT dir_alloc (
|
---|
| 1260 | DIR* dp, /* Pointer to the directory object */
|
---|
| 1261 | UINT nent /* Number of contiguous entries to allocate (1-21) */
|
---|
| 1262 | )
|
---|
| 1263 | {
|
---|
| 1264 | FRESULT res;
|
---|
| 1265 | UINT n;
|
---|
| 1266 |
|
---|
| 1267 |
|
---|
| 1268 | res = dir_sdi(dp, 0);
|
---|
| 1269 | if (res == FR_OK) {
|
---|
| 1270 | n = 0;
|
---|
| 1271 | do {
|
---|
| 1272 | res = move_window(dp->fs, dp->sect);
|
---|
| 1273 | if (res != FR_OK) break;
|
---|
| 1274 | if (dp->dir[0] == DDE || dp->dir[0] == 0) { /* Is it a free entry? */
|
---|
| 1275 | if (++n == nent) break; /* A block of contiguous free entries is found */
|
---|
| 1276 | } else {
|
---|
| 1277 | n = 0; /* Not a blank entry. Restart to search */
|
---|
| 1278 | }
|
---|
| 1279 | res = dir_next(dp, 1); /* Next entry with table stretch enabled */
|
---|
| 1280 | } while (res == FR_OK);
|
---|
| 1281 | }
|
---|
| 1282 | if (res == FR_NO_FILE) res = FR_DENIED; /* No directory entry to allocate */
|
---|
| 1283 | return res;
|
---|
| 1284 | }
|
---|
| 1285 | #endif
|
---|
| 1286 |
|
---|
| 1287 |
|
---|
| 1288 |
|
---|
| 1289 |
|
---|
| 1290 | /*-----------------------------------------------------------------------*/
|
---|
| 1291 | /* Directory handling - Load/Store start cluster number */
|
---|
| 1292 | /*-----------------------------------------------------------------------*/
|
---|
| 1293 |
|
---|
| 1294 | static
|
---|
| 1295 | DWORD ld_clust (
|
---|
| 1296 | FATFS* fs, /* Pointer to the fs object */
|
---|
| 1297 | BYTE* dir /* Pointer to the directory entry */
|
---|
| 1298 | )
|
---|
| 1299 | {
|
---|
| 1300 | DWORD cl;
|
---|
| 1301 |
|
---|
| 1302 | cl = LD_WORD(dir+DIR_FstClusLO);
|
---|
| 1303 | if (fs->fs_type == FS_FAT32)
|
---|
| 1304 | cl |= (DWORD)LD_WORD(dir+DIR_FstClusHI) << 16;
|
---|
| 1305 |
|
---|
| 1306 | return cl;
|
---|
| 1307 | }
|
---|
| 1308 |
|
---|
| 1309 |
|
---|
| 1310 | #if !_FS_READONLY
|
---|
| 1311 | static
|
---|
| 1312 | void st_clust (
|
---|
| 1313 | BYTE* dir, /* Pointer to the directory entry */
|
---|
| 1314 | DWORD cl /* Value to be set */
|
---|
| 1315 | )
|
---|
| 1316 | {
|
---|
| 1317 | ST_WORD(dir+DIR_FstClusLO, cl);
|
---|
| 1318 | ST_WORD(dir+DIR_FstClusHI, cl >> 16);
|
---|
| 1319 | }
|
---|
| 1320 | #endif
|
---|
| 1321 |
|
---|
| 1322 |
|
---|
| 1323 |
|
---|
| 1324 |
|
---|
| 1325 | /*-----------------------------------------------------------------------*/
|
---|
| 1326 | /* LFN handling - Test/Pick/Fit an LFN segment from/to directory entry */
|
---|
| 1327 | /*-----------------------------------------------------------------------*/
|
---|
| 1328 | #if _USE_LFN
|
---|
| 1329 | static
|
---|
| 1330 | const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN characters in the directory entry */
|
---|
| 1331 |
|
---|
| 1332 |
|
---|
| 1333 | static
|
---|
| 1334 | int cmp_lfn ( /* 1:Matched, 0:Not matched */
|
---|
| 1335 | WCHAR* lfnbuf, /* Pointer to the LFN to be compared */
|
---|
| 1336 | BYTE* dir /* Pointer to the directory entry containing a part of LFN */
|
---|
| 1337 | )
|
---|
| 1338 | {
|
---|
| 1339 | UINT i, s;
|
---|
| 1340 | WCHAR wc, uc;
|
---|
| 1341 |
|
---|
| 1342 |
|
---|
| 1343 | i = ((dir[LDIR_Ord] & ~LLE) - 1) * 13; /* Get offset in the LFN buffer */
|
---|
| 1344 | s = 0; wc = 1;
|
---|
| 1345 | do {
|
---|
| 1346 | uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
|
---|
| 1347 | if (wc) { /* Last character has not been processed */
|
---|
| 1348 | wc = ff_wtoupper(uc); /* Convert it to upper case */
|
---|
| 1349 | if (i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++])) /* Compare it */
|
---|
| 1350 | return 0; /* Not matched */
|
---|
| 1351 | } else {
|
---|
| 1352 | if (uc != 0xFFFF) return 0; /* Check filler */
|
---|
| 1353 | }
|
---|
| 1354 | } while (++s < 13); /* Repeat until all characters in the entry are checked */
|
---|
| 1355 |
|
---|
| 1356 | if ((dir[LDIR_Ord] & LLE) && wc && lfnbuf[i]) /* Last segment matched but different length */
|
---|
| 1357 | return 0;
|
---|
| 1358 |
|
---|
| 1359 | return 1; /* The part of LFN matched */
|
---|
| 1360 | }
|
---|
| 1361 |
|
---|
| 1362 |
|
---|
| 1363 |
|
---|
| 1364 | static
|
---|
| 1365 | int pick_lfn ( /* 1:Succeeded, 0:Buffer overflow */
|
---|
| 1366 | WCHAR* lfnbuf, /* Pointer to the Unicode-LFN buffer */
|
---|
| 1367 | BYTE* dir /* Pointer to the directory entry */
|
---|
| 1368 | )
|
---|
| 1369 | {
|
---|
| 1370 | UINT i, s;
|
---|
| 1371 | WCHAR wc, uc;
|
---|
| 1372 |
|
---|
| 1373 |
|
---|
| 1374 | i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
|
---|
| 1375 |
|
---|
| 1376 | s = 0; wc = 1;
|
---|
| 1377 | do {
|
---|
| 1378 | uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
|
---|
| 1379 | if (wc) { /* Last character has not been processed */
|
---|
| 1380 | if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
|
---|
| 1381 | lfnbuf[i++] = wc = uc; /* Store it */
|
---|
| 1382 | } else {
|
---|
| 1383 | if (uc != 0xFFFF) return 0; /* Check filler */
|
---|
| 1384 | }
|
---|
| 1385 | } while (++s < 13); /* Read all character in the entry */
|
---|
| 1386 |
|
---|
| 1387 | if (dir[LDIR_Ord] & LLE) { /* Put terminator if it is the last LFN part */
|
---|
| 1388 | if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
|
---|
| 1389 | lfnbuf[i] = 0;
|
---|
| 1390 | }
|
---|
| 1391 |
|
---|
| 1392 | return 1;
|
---|
| 1393 | }
|
---|
| 1394 |
|
---|
| 1395 |
|
---|
| 1396 | #if !_FS_READONLY
|
---|
| 1397 | static
|
---|
| 1398 | void fit_lfn (
|
---|
| 1399 | const WCHAR* lfnbuf, /* Pointer to the LFN buffer */
|
---|
| 1400 | BYTE* dir, /* Pointer to the directory entry */
|
---|
| 1401 | BYTE ord, /* LFN order (1-20) */
|
---|
| 1402 | BYTE sum /* SFN sum */
|
---|
| 1403 | )
|
---|
| 1404 | {
|
---|
| 1405 | UINT i, s;
|
---|
| 1406 | WCHAR wc;
|
---|
| 1407 |
|
---|
| 1408 |
|
---|
| 1409 | dir[LDIR_Chksum] = sum; /* Set check sum */
|
---|
| 1410 | dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */
|
---|
| 1411 | dir[LDIR_Type] = 0;
|
---|
| 1412 | ST_WORD(dir+LDIR_FstClusLO, 0);
|
---|
| 1413 |
|
---|
| 1414 | i = (ord - 1) * 13; /* Get offset in the LFN buffer */
|
---|
| 1415 | s = wc = 0;
|
---|
| 1416 | do {
|
---|
| 1417 | if (wc != 0xFFFF) wc = lfnbuf[i++]; /* Get an effective character */
|
---|
| 1418 | ST_WORD(dir+LfnOfs[s], wc); /* Put it */
|
---|
| 1419 | if (!wc) wc = 0xFFFF; /* Padding characters following last character */
|
---|
| 1420 | } while (++s < 13);
|
---|
| 1421 | if (wc == 0xFFFF || !lfnbuf[i]) ord |= LLE; /* Bottom LFN part is the start of LFN sequence */
|
---|
| 1422 | dir[LDIR_Ord] = ord; /* Set the LFN order */
|
---|
| 1423 | }
|
---|
| 1424 |
|
---|
| 1425 | #endif
|
---|
| 1426 | #endif
|
---|
| 1427 |
|
---|
| 1428 |
|
---|
| 1429 |
|
---|
| 1430 |
|
---|
| 1431 | /*-----------------------------------------------------------------------*/
|
---|
| 1432 | /* Create numbered name */
|
---|
| 1433 | /*-----------------------------------------------------------------------*/
|
---|
| 1434 | #if _USE_LFN
|
---|
| 1435 | static
|
---|
| 1436 | void gen_numname (
|
---|
| 1437 | BYTE* dst, /* Pointer to the buffer to store numbered SFN */
|
---|
| 1438 | const BYTE* src, /* Pointer to SFN */
|
---|
| 1439 | const WCHAR* lfn, /* Pointer to LFN */
|
---|
| 1440 | UINT seq /* Sequence number */
|
---|
| 1441 | )
|
---|
| 1442 | {
|
---|
| 1443 | BYTE ns[8], c;
|
---|
| 1444 | UINT i, j;
|
---|
| 1445 |
|
---|
| 1446 |
|
---|
| 1447 | mem_cpy(dst, src, 11);
|
---|
| 1448 |
|
---|
| 1449 | if (seq > 5) { /* On many collisions, generate a hash number instead of sequential number */
|
---|
| 1450 | WCHAR wc;
|
---|
| 1451 | DWORD sr = seq;
|
---|
| 1452 |
|
---|
| 1453 | while (*lfn) { /* Create a CRC */
|
---|
| 1454 | wc = *lfn++;
|
---|
| 1455 | for (i = 0; i < 16; i++) {
|
---|
| 1456 | sr = (sr << 1) + (wc & 1);
|
---|
| 1457 | wc >>= 1;
|
---|
| 1458 | if (sr & 0x10000) sr ^= 0x11021;
|
---|
| 1459 | }
|
---|
| 1460 | }
|
---|
| 1461 | seq = (UINT)sr;
|
---|
| 1462 | }
|
---|
| 1463 |
|
---|
| 1464 | /* itoa (hexdecimal) */
|
---|
| 1465 | i = 7;
|
---|
| 1466 | do {
|
---|
| 1467 | c = (seq % 16) + '0';
|
---|
| 1468 | if (c > '9') c += 7;
|
---|
| 1469 | ns[i--] = c;
|
---|
| 1470 | seq /= 16;
|
---|
| 1471 | } while (seq);
|
---|
| 1472 | ns[i] = '~';
|
---|
| 1473 |
|
---|
| 1474 | /* Append the number */
|
---|
| 1475 | for (j = 0; j < i && dst[j] != ' '; j++) {
|
---|
| 1476 | if (IsDBCS1(dst[j])) {
|
---|
| 1477 | if (j == i - 1) break;
|
---|
| 1478 | j++;
|
---|
| 1479 | }
|
---|
| 1480 | }
|
---|
| 1481 | do {
|
---|
| 1482 | dst[j++] = (i < 8) ? ns[i++] : ' ';
|
---|
| 1483 | } while (j < 8);
|
---|
| 1484 | }
|
---|
| 1485 | #endif
|
---|
| 1486 |
|
---|
| 1487 |
|
---|
| 1488 |
|
---|
| 1489 |
|
---|
| 1490 | /*-----------------------------------------------------------------------*/
|
---|
| 1491 | /* Calculate sum of an SFN */
|
---|
| 1492 | /*-----------------------------------------------------------------------*/
|
---|
| 1493 | #if _USE_LFN
|
---|
| 1494 | static
|
---|
| 1495 | BYTE sum_sfn (
|
---|
| 1496 | const BYTE* dir /* Pointer to the SFN entry */
|
---|
| 1497 | )
|
---|
| 1498 | {
|
---|
| 1499 | BYTE sum = 0;
|
---|
| 1500 | UINT n = 11;
|
---|
| 1501 |
|
---|
| 1502 | do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);
|
---|
| 1503 | return sum;
|
---|
| 1504 | }
|
---|
| 1505 | #endif
|
---|
| 1506 |
|
---|
| 1507 |
|
---|
| 1508 |
|
---|
| 1509 |
|
---|
| 1510 | /*-----------------------------------------------------------------------*/
|
---|
| 1511 | /* Directory handling - Find an object in the directory */
|
---|
| 1512 | /*-----------------------------------------------------------------------*/
|
---|
| 1513 |
|
---|
| 1514 | static
|
---|
| 1515 | FRESULT dir_find (
|
---|
| 1516 | DIR* dp /* Pointer to the directory object linked to the file name */
|
---|
| 1517 | )
|
---|
| 1518 | {
|
---|
| 1519 | FRESULT res;
|
---|
| 1520 | BYTE c, *dir;
|
---|
| 1521 | #if _USE_LFN
|
---|
| 1522 | BYTE a, ord, sum;
|
---|
| 1523 | #endif
|
---|
| 1524 |
|
---|
| 1525 | res = dir_sdi(dp, 0); /* Rewind directory object */
|
---|
| 1526 | if (res != FR_OK) return res;
|
---|
| 1527 |
|
---|
| 1528 | #if _USE_LFN
|
---|
| 1529 | ord = sum = 0xFF; dp->lfn_idx = 0xFFFF; /* Reset LFN sequence */
|
---|
| 1530 | #endif
|
---|
| 1531 | do {
|
---|
| 1532 | res = move_window(dp->fs, dp->sect);
|
---|
| 1533 | if (res != FR_OK) break;
|
---|
| 1534 | dir = dp->dir; /* Ptr to the directory entry of current index */
|
---|
| 1535 | c = dir[DIR_Name];
|
---|
| 1536 | if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
|
---|
| 1537 | #if _USE_LFN /* LFN configuration */
|
---|
| 1538 | a = dir[DIR_Attr] & AM_MASK;
|
---|
| 1539 | if (c == DDE || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
|
---|
| 1540 | ord = 0xFF; dp->lfn_idx = 0xFFFF; /* Reset LFN sequence */
|
---|
| 1541 | } else {
|
---|
| 1542 | if (a == AM_LFN) { /* An LFN entry is found */
|
---|
| 1543 | if (dp->lfn) {
|
---|
| 1544 | if (c & LLE) { /* Is it start of LFN sequence? */
|
---|
| 1545 | sum = dir[LDIR_Chksum];
|
---|
| 1546 | c &= ~LLE; ord = c; /* LFN start order */
|
---|
| 1547 | dp->lfn_idx = dp->index; /* Start index of LFN */
|
---|
| 1548 | }
|
---|
| 1549 | /* Check validity of the LFN entry and compare it with given name */
|
---|
| 1550 | ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dp->lfn, dir)) ? ord - 1 : 0xFF;
|
---|
| 1551 | }
|
---|
| 1552 | } else { /* An SFN entry is found */
|
---|
| 1553 | if (!ord && sum == sum_sfn(dir)) break; /* LFN matched? */
|
---|
| 1554 | if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dir, dp->fn, 11)) break; /* SFN matched? */
|
---|
| 1555 | ord = 0xFF; dp->lfn_idx = 0xFFFF; /* Reset LFN sequence */
|
---|
| 1556 | }
|
---|
| 1557 | }
|
---|
| 1558 | #else /* Non LFN configuration */
|
---|
| 1559 | if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dp->fn, 11)) /* Is it a valid entry? */
|
---|
| 1560 | break;
|
---|
| 1561 | #endif
|
---|
| 1562 | res = dir_next(dp, 0); /* Next entry */
|
---|
| 1563 | } while (res == FR_OK);
|
---|
| 1564 |
|
---|
| 1565 | return res;
|
---|
| 1566 | }
|
---|
| 1567 |
|
---|
| 1568 |
|
---|
| 1569 |
|
---|
| 1570 |
|
---|
| 1571 | /*-----------------------------------------------------------------------*/
|
---|
| 1572 | /* Read an object from the directory */
|
---|
| 1573 | /*-----------------------------------------------------------------------*/
|
---|
| 1574 | #if _FS_MINIMIZE <= 1 || _USE_LABEL || _FS_RPATH >= 2
|
---|
| 1575 | static
|
---|
| 1576 | FRESULT dir_read (
|
---|
| 1577 | DIR* dp, /* Pointer to the directory object */
|
---|
| 1578 | int vol /* Filtered by 0:file/directory or 1:volume label */
|
---|
| 1579 | )
|
---|
| 1580 | {
|
---|
| 1581 | FRESULT res;
|
---|
| 1582 | BYTE a, c, *dir;
|
---|
| 1583 | #if _USE_LFN
|
---|
| 1584 | BYTE ord = 0xFF, sum = 0xFF;
|
---|
| 1585 | #endif
|
---|
| 1586 |
|
---|
| 1587 | res = FR_NO_FILE;
|
---|
| 1588 | while (dp->sect) {
|
---|
| 1589 | res = move_window(dp->fs, dp->sect);
|
---|
| 1590 | if (res != FR_OK) break;
|
---|
| 1591 | dir = dp->dir; /* Ptr to the directory entry of current index */
|
---|
| 1592 | c = dir[DIR_Name];
|
---|
| 1593 | if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
|
---|
| 1594 | a = dir[DIR_Attr] & AM_MASK;
|
---|
| 1595 | #if _USE_LFN /* LFN configuration */
|
---|
| 1596 | if (c == DDE || (!_FS_RPATH && c == '.') || (int)((a & ~AM_ARC) == AM_VOL) != vol) { /* An entry without valid data */
|
---|
| 1597 | ord = 0xFF;
|
---|
| 1598 | } else {
|
---|
| 1599 | if (a == AM_LFN) { /* An LFN entry is found */
|
---|
| 1600 | if (c & LLE) { /* Is it start of LFN sequence? */
|
---|
| 1601 | sum = dir[LDIR_Chksum];
|
---|
| 1602 | c &= ~LLE; ord = c;
|
---|
| 1603 | dp->lfn_idx = dp->index;
|
---|
| 1604 | }
|
---|
| 1605 | /* Check LFN validity and capture it */
|
---|
| 1606 | ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dp->lfn, dir)) ? ord - 1 : 0xFF;
|
---|
| 1607 | } else { /* An SFN entry is found */
|
---|
| 1608 | if (ord || sum != sum_sfn(dir)) /* Is there a valid LFN? */
|
---|
| 1609 | dp->lfn_idx = 0xFFFF; /* It has no LFN. */
|
---|
| 1610 | break;
|
---|
| 1611 | }
|
---|
| 1612 | }
|
---|
| 1613 | #else /* Non LFN configuration */
|
---|
| 1614 | if (c != DDE && (_FS_RPATH || c != '.') && a != AM_LFN && (int)((a & ~AM_ARC) == AM_VOL) == vol) /* Is it a valid entry? */
|
---|
| 1615 | break;
|
---|
| 1616 | #endif
|
---|
| 1617 | res = dir_next(dp, 0); /* Next entry */
|
---|
| 1618 | if (res != FR_OK) break;
|
---|
| 1619 | }
|
---|
| 1620 |
|
---|
| 1621 | if (res != FR_OK) dp->sect = 0;
|
---|
| 1622 |
|
---|
| 1623 | return res;
|
---|
| 1624 | }
|
---|
| 1625 | #endif /* _FS_MINIMIZE <= 1 || _USE_LABEL || _FS_RPATH >= 2 */
|
---|
| 1626 |
|
---|
| 1627 |
|
---|
| 1628 |
|
---|
| 1629 |
|
---|
| 1630 | /*-----------------------------------------------------------------------*/
|
---|
| 1631 | /* Register an object to the directory */
|
---|
| 1632 | /*-----------------------------------------------------------------------*/
|
---|
| 1633 | #if !_FS_READONLY
|
---|
| 1634 | static
|
---|
| 1635 | FRESULT dir_register ( /* FR_OK:Successful, FR_DENIED:No free entry or too many SFN collision, FR_DISK_ERR:Disk error */
|
---|
| 1636 | DIR* dp /* Target directory with object name to be created */
|
---|
| 1637 | )
|
---|
| 1638 | {
|
---|
| 1639 | FRESULT res;
|
---|
| 1640 | #if _USE_LFN /* LFN configuration */
|
---|
| 1641 | UINT n, nent;
|
---|
| 1642 | BYTE sn[12], *fn, sum;
|
---|
| 1643 | WCHAR *lfn;
|
---|
| 1644 |
|
---|
| 1645 |
|
---|
| 1646 | fn = dp->fn; lfn = dp->lfn;
|
---|
| 1647 | mem_cpy(sn, fn, 12);
|
---|
| 1648 |
|
---|
| 1649 | if (_FS_RPATH && (sn[NSFLAG] & NS_DOT)) /* Cannot create dot entry */
|
---|
| 1650 | return FR_INVALID_NAME;
|
---|
| 1651 |
|
---|
| 1652 | if (sn[NSFLAG] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */
|
---|
| 1653 | fn[NSFLAG] = 0; dp->lfn = 0; /* Find only SFN */
|
---|
| 1654 | for (n = 1; n < 100; n++) {
|
---|
| 1655 | gen_numname(fn, sn, lfn, n); /* Generate a numbered name */
|
---|
| 1656 | res = dir_find(dp); /* Check if the name collides with existing SFN */
|
---|
| 1657 | if (res != FR_OK) break;
|
---|
| 1658 | }
|
---|
| 1659 | if (n == 100) return FR_DENIED; /* Abort if too many collisions */
|
---|
| 1660 | if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */
|
---|
| 1661 | fn[NSFLAG] = sn[NSFLAG]; dp->lfn = lfn;
|
---|
| 1662 | }
|
---|
| 1663 |
|
---|
| 1664 | if (sn[NSFLAG] & NS_LFN) { /* When LFN is to be created, allocate entries for an SFN + LFNs. */
|
---|
| 1665 | for (n = 0; lfn[n]; n++) ;
|
---|
| 1666 | nent = (n + 25) / 13;
|
---|
| 1667 | } else { /* Otherwise allocate an entry for an SFN */
|
---|
| 1668 | nent = 1;
|
---|
| 1669 | }
|
---|
| 1670 | res = dir_alloc(dp, nent); /* Allocate entries */
|
---|
| 1671 |
|
---|
| 1672 | if (res == FR_OK && --nent) { /* Set LFN entry if needed */
|
---|
| 1673 | res = dir_sdi(dp, dp->index - nent);
|
---|
| 1674 | if (res == FR_OK) {
|
---|
| 1675 | sum = sum_sfn(dp->fn); /* Sum value of the SFN tied to the LFN */
|
---|
| 1676 | do { /* Store LFN entries in bottom first */
|
---|
| 1677 | res = move_window(dp->fs, dp->sect);
|
---|
| 1678 | if (res != FR_OK) break;
|
---|
| 1679 | fit_lfn(dp->lfn, dp->dir, (BYTE)nent, sum);
|
---|
| 1680 | dp->fs->wflag = 1;
|
---|
| 1681 | res = dir_next(dp, 0); /* Next entry */
|
---|
| 1682 | } while (res == FR_OK && --nent);
|
---|
| 1683 | }
|
---|
| 1684 | }
|
---|
| 1685 | #else /* Non LFN configuration */
|
---|
| 1686 | res = dir_alloc(dp, 1); /* Allocate an entry for SFN */
|
---|
| 1687 | #endif
|
---|
| 1688 |
|
---|
| 1689 | if (res == FR_OK) { /* Set SFN entry */
|
---|
| 1690 | res = move_window(dp->fs, dp->sect);
|
---|
| 1691 | if (res == FR_OK) {
|
---|
| 1692 | mem_set(dp->dir, 0, SZ_DIR); /* Clean the entry */
|
---|
| 1693 | mem_cpy(dp->dir, dp->fn, 11); /* Put SFN */
|
---|
| 1694 | #if _USE_LFN
|
---|
| 1695 | dp->dir[DIR_NTres] = dp->fn[NSFLAG] & (NS_BODY | NS_EXT); /* Put NT flag */
|
---|
| 1696 | #endif
|
---|
| 1697 | dp->fs->wflag = 1;
|
---|
| 1698 | }
|
---|
| 1699 | }
|
---|
| 1700 |
|
---|
| 1701 | return res;
|
---|
| 1702 | }
|
---|
| 1703 | #endif /* !_FS_READONLY */
|
---|
| 1704 |
|
---|
| 1705 |
|
---|
| 1706 |
|
---|
| 1707 |
|
---|
| 1708 | /*-----------------------------------------------------------------------*/
|
---|
| 1709 | /* Remove an object from the directory */
|
---|
| 1710 | /*-----------------------------------------------------------------------*/
|
---|
| 1711 | #if !_FS_READONLY && !_FS_MINIMIZE
|
---|
| 1712 | static
|
---|
| 1713 | FRESULT dir_remove ( /* FR_OK: Successful, FR_DISK_ERR: A disk error */
|
---|
| 1714 | DIR* dp /* Directory object pointing the entry to be removed */
|
---|
| 1715 | )
|
---|
| 1716 | {
|
---|
| 1717 | FRESULT res;
|
---|
| 1718 | #if _USE_LFN /* LFN configuration */
|
---|
| 1719 | UINT i;
|
---|
| 1720 |
|
---|
| 1721 | i = dp->index; /* SFN index */
|
---|
| 1722 | res = dir_sdi(dp, (dp->lfn_idx == 0xFFFF) ? i : dp->lfn_idx); /* Goto the SFN or top of the LFN entries */
|
---|
| 1723 | if (res == FR_OK) {
|
---|
| 1724 | do {
|
---|
| 1725 | res = move_window(dp->fs, dp->sect);
|
---|
| 1726 | if (res != FR_OK) break;
|
---|
| 1727 | mem_set(dp->dir, 0, SZ_DIR); /* Clear and mark the entry "deleted" */
|
---|
| 1728 | *dp->dir = DDE;
|
---|
| 1729 | dp->fs->wflag = 1;
|
---|
| 1730 | if (dp->index >= i) break; /* When reached SFN, all entries of the object has been deleted. */
|
---|
| 1731 | res = dir_next(dp, 0); /* Next entry */
|
---|
| 1732 | } while (res == FR_OK);
|
---|
| 1733 | if (res == FR_NO_FILE) res = FR_INT_ERR;
|
---|
| 1734 | }
|
---|
| 1735 |
|
---|
| 1736 | #else /* Non LFN configuration */
|
---|
| 1737 | res = dir_sdi(dp, dp->index);
|
---|
| 1738 | if (res == FR_OK) {
|
---|
| 1739 | res = move_window(dp->fs, dp->sect);
|
---|
| 1740 | if (res == FR_OK) {
|
---|
| 1741 | mem_set(dp->dir, 0, SZ_DIR); /* Clear and mark the entry "deleted" */
|
---|
| 1742 | *dp->dir = DDE;
|
---|
| 1743 | dp->fs->wflag = 1;
|
---|
| 1744 | }
|
---|
| 1745 | }
|
---|
| 1746 | #endif
|
---|
| 1747 |
|
---|
| 1748 | return res;
|
---|
| 1749 | }
|
---|
| 1750 | #endif /* !_FS_READONLY */
|
---|
| 1751 |
|
---|
| 1752 |
|
---|
| 1753 |
|
---|
| 1754 |
|
---|
| 1755 | /*-----------------------------------------------------------------------*/
|
---|
| 1756 | /* Get file information from directory entry */
|
---|
| 1757 | /*-----------------------------------------------------------------------*/
|
---|
| 1758 | #if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2
|
---|
| 1759 | static
|
---|
| 1760 | void get_fileinfo ( /* No return code */
|
---|
| 1761 | DIR* dp, /* Pointer to the directory object */
|
---|
| 1762 | FILINFO* fno /* Pointer to the file information to be filled */
|
---|
| 1763 | )
|
---|
| 1764 | {
|
---|
| 1765 | UINT i;
|
---|
| 1766 | TCHAR *p, c;
|
---|
| 1767 |
|
---|
| 1768 |
|
---|
| 1769 | p = fno->fname;
|
---|
| 1770 | if (dp->sect) { /* Get SFN */
|
---|
| 1771 | BYTE *dir = dp->dir;
|
---|
| 1772 |
|
---|
| 1773 | i = 0;
|
---|
| 1774 | while (i < 11) { /* Copy name body and extension */
|
---|
| 1775 | c = (TCHAR)dir[i++];
|
---|
| 1776 | if (c == ' ') continue; /* Skip padding spaces */
|
---|
| 1777 | if (c == NDDE) c = (TCHAR)DDE; /* Restore replaced DDE character */
|
---|
| 1778 | if (i == 9) *p++ = '.'; /* Insert a . if extension is exist */
|
---|
| 1779 | #if _USE_LFN
|
---|
| 1780 | if (IsUpper(c) && (dir[DIR_NTres] & (i >= 9 ? NS_EXT : NS_BODY)))
|
---|
| 1781 | c += 0x20; /* To lower */
|
---|
| 1782 | #if _LFN_UNICODE
|
---|
| 1783 | if (IsDBCS1(c) && i != 8 && i != 11 && IsDBCS2(dir[i]))
|
---|
| 1784 | c = c << 8 | dir[i++];
|
---|
| 1785 | c = ff_convert(c, 1); /* OEM -> Unicode */
|
---|
| 1786 | if (!c) c = '?';
|
---|
| 1787 | #endif
|
---|
| 1788 | #endif
|
---|
| 1789 | *p++ = c;
|
---|
| 1790 | }
|
---|
| 1791 | fno->fattrib = dir[DIR_Attr]; /* Attribute */
|
---|
| 1792 | fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */
|
---|
| 1793 | fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */
|
---|
| 1794 | fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */
|
---|
| 1795 | }
|
---|
| 1796 | *p = 0; /* Terminate SFN string by a \0 */
|
---|
| 1797 |
|
---|
| 1798 | #if _USE_LFN
|
---|
| 1799 | if (fno->lfname) {
|
---|
| 1800 | WCHAR w, *lfn;
|
---|
| 1801 |
|
---|
| 1802 | i = 0; p = fno->lfname;
|
---|
| 1803 | if (dp->sect && fno->lfsize && dp->lfn_idx != 0xFFFF) { /* Get LFN if available */
|
---|
| 1804 | lfn = dp->lfn;
|
---|
| 1805 | while ((w = *lfn++) != 0) { /* Get an LFN character */
|
---|
| 1806 | #if !_LFN_UNICODE
|
---|
| 1807 | w = ff_convert(w, 0); /* Unicode -> OEM */
|
---|
| 1808 | if (!w) { i = 0; break; } /* No LFN if it could not be converted */
|
---|
| 1809 | if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC (always false on SBCS cfg) */
|
---|
| 1810 | p[i++] = (TCHAR)(w >> 8);
|
---|
| 1811 | #endif
|
---|
| 1812 | if (i >= fno->lfsize - 1) { i = 0; break; } /* No LFN if buffer overflow */
|
---|
| 1813 | p[i++] = (TCHAR)w;
|
---|
| 1814 | }
|
---|
| 1815 | }
|
---|
| 1816 | p[i] = 0; /* Terminate LFN string by a \0 */
|
---|
| 1817 | }
|
---|
| 1818 | #endif
|
---|
| 1819 | }
|
---|
| 1820 | #endif /* _FS_MINIMIZE <= 1 || _FS_RPATH >= 2*/
|
---|
| 1821 |
|
---|
| 1822 |
|
---|
| 1823 |
|
---|
| 1824 |
|
---|
| 1825 | /*-----------------------------------------------------------------------*/
|
---|
| 1826 | /* Pick a segment and create the object name in directory form */
|
---|
| 1827 | /*-----------------------------------------------------------------------*/
|
---|
| 1828 |
|
---|
| 1829 | static
|
---|
| 1830 | FRESULT create_name (
|
---|
| 1831 | DIR* dp, /* Pointer to the directory object */
|
---|
| 1832 | const TCHAR** path /* Pointer to pointer to the segment in the path string */
|
---|
| 1833 | )
|
---|
| 1834 | {
|
---|
| 1835 | #if _USE_LFN /* LFN configuration */
|
---|
| 1836 | BYTE b, cf;
|
---|
| 1837 | WCHAR w, *lfn;
|
---|
| 1838 | UINT i, ni, si, di;
|
---|
| 1839 | const TCHAR *p;
|
---|
| 1840 |
|
---|
| 1841 | /* Create LFN in Unicode */
|
---|
| 1842 | for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
|
---|
| 1843 | lfn = dp->lfn;
|
---|
| 1844 | si = di = 0;
|
---|
| 1845 | for (;;) {
|
---|
| 1846 | w = p[si++]; /* Get a character */
|
---|
| 1847 | if (w < ' ' || w == '/' || w == '\\') break; /* Break on end of segment */
|
---|
| 1848 | if (di >= _MAX_LFN) /* Reject too long name */
|
---|
| 1849 | return FR_INVALID_NAME;
|
---|
| 1850 | #if !_LFN_UNICODE
|
---|
| 1851 | w &= 0xFF;
|
---|
| 1852 | if (IsDBCS1(w)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
|
---|
| 1853 | b = (BYTE)p[si++]; /* Get 2nd byte */
|
---|
| 1854 | if (!IsDBCS2(b))
|
---|
| 1855 | return FR_INVALID_NAME; /* Reject invalid sequence */
|
---|
| 1856 | w = (w << 8) + b; /* Create a DBC */
|
---|
| 1857 | }
|
---|
| 1858 | w = ff_convert(w, 1); /* Convert ANSI/OEM to Unicode */
|
---|
| 1859 | if (!w) return FR_INVALID_NAME; /* Reject invalid code */
|
---|
| 1860 | #endif
|
---|
| 1861 | if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) /* Reject illegal characters for LFN */
|
---|
| 1862 | return FR_INVALID_NAME;
|
---|
| 1863 | lfn[di++] = w; /* Store the Unicode character */
|
---|
| 1864 | }
|
---|
| 1865 | *path = &p[si]; /* Return pointer to the next segment */
|
---|
| 1866 | cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
|
---|
| 1867 | #if _FS_RPATH
|
---|
| 1868 | if ((di == 1 && lfn[di-1] == '.') || /* Is this a dot entry? */
|
---|
| 1869 | (di == 2 && lfn[di-1] == '.' && lfn[di-2] == '.')) {
|
---|
| 1870 | lfn[di] = 0;
|
---|
| 1871 | for (i = 0; i < 11; i++)
|
---|
| 1872 | dp->fn[i] = (i < di) ? '.' : ' ';
|
---|
| 1873 | dp->fn[i] = cf | NS_DOT; /* This is a dot entry */
|
---|
| 1874 | return FR_OK;
|
---|
| 1875 | }
|
---|
| 1876 | #endif
|
---|
| 1877 | while (di) { /* Strip trailing spaces and dots */
|
---|
| 1878 | w = lfn[di-1];
|
---|
| 1879 | if (w != ' ' && w != '.') break;
|
---|
| 1880 | di--;
|
---|
| 1881 | }
|
---|
| 1882 | if (!di) return FR_INVALID_NAME; /* Reject nul string */
|
---|
| 1883 |
|
---|
| 1884 | lfn[di] = 0; /* LFN is created */
|
---|
| 1885 |
|
---|
| 1886 | /* Create SFN in directory form */
|
---|
| 1887 | mem_set(dp->fn, ' ', 11);
|
---|
| 1888 | for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */
|
---|
| 1889 | if (si) cf |= NS_LOSS | NS_LFN;
|
---|
| 1890 | while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */
|
---|
| 1891 |
|
---|
| 1892 | b = i = 0; ni = 8;
|
---|
| 1893 | for (;;) {
|
---|
| 1894 | w = lfn[si++]; /* Get an LFN character */
|
---|
| 1895 | if (!w) break; /* Break on end of the LFN */
|
---|
| 1896 | if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */
|
---|
| 1897 | cf |= NS_LOSS | NS_LFN; continue;
|
---|
| 1898 | }
|
---|
| 1899 |
|
---|
| 1900 | if (i >= ni || si == di) { /* Extension or end of SFN */
|
---|
| 1901 | if (ni == 11) { /* Long extension */
|
---|
| 1902 | cf |= NS_LOSS | NS_LFN; break;
|
---|
| 1903 | }
|
---|
| 1904 | if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */
|
---|
| 1905 | if (si > di) break; /* No extension */
|
---|
| 1906 | si = di; i = 8; ni = 11; /* Enter extension section */
|
---|
| 1907 | b <<= 2; continue;
|
---|
| 1908 | }
|
---|
| 1909 |
|
---|
| 1910 | if (w >= 0x80) { /* Non ASCII character */
|
---|
| 1911 | #ifdef _EXCVT
|
---|
| 1912 | w = ff_convert(w, 0); /* Unicode -> OEM code */
|
---|
| 1913 | if (w) w = ExCvt[w - 0x80]; /* Convert extended character to upper (SBCS) */
|
---|
| 1914 | #else
|
---|
| 1915 | w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */
|
---|
| 1916 | #endif
|
---|
| 1917 | cf |= NS_LFN; /* Force create LFN entry */
|
---|
| 1918 | }
|
---|
| 1919 |
|
---|
| 1920 | if (_DF1S && w >= 0x100) { /* Double byte character (always false on SBCS cfg) */
|
---|
| 1921 | if (i >= ni - 1) {
|
---|
| 1922 | cf |= NS_LOSS | NS_LFN; i = ni; continue;
|
---|
| 1923 | }
|
---|
| 1924 | dp->fn[i++] = (BYTE)(w >> 8);
|
---|
| 1925 | } else { /* Single byte character */
|
---|
| 1926 | if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal characters for SFN */
|
---|
| 1927 | w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */
|
---|
| 1928 | } else {
|
---|
| 1929 | if (IsUpper(w)) { /* ASCII large capital */
|
---|
| 1930 | b |= 2;
|
---|
| 1931 | } else {
|
---|
| 1932 | if (IsLower(w)) { /* ASCII small capital */
|
---|
| 1933 | b |= 1; w -= 0x20;
|
---|
| 1934 | }
|
---|
| 1935 | }
|
---|
| 1936 | }
|
---|
| 1937 | }
|
---|
| 1938 | dp->fn[i++] = (BYTE)w;
|
---|
| 1939 | }
|
---|
| 1940 |
|
---|
| 1941 | if (dp->fn[0] == DDE) dp->fn[0] = NDDE; /* If the first character collides with deleted mark, replace it with 0x05 */
|
---|
| 1942 |
|
---|
| 1943 | if (ni == 8) b <<= 2;
|
---|
| 1944 | if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) /* Create LFN entry when there are composite capitals */
|
---|
| 1945 | cf |= NS_LFN;
|
---|
| 1946 | if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended character, NT flags are created */
|
---|
| 1947 | if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */
|
---|
| 1948 | if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */
|
---|
| 1949 | }
|
---|
| 1950 |
|
---|
| 1951 | dp->fn[NSFLAG] = cf; /* SFN is created */
|
---|
| 1952 |
|
---|
| 1953 | return FR_OK;
|
---|
| 1954 |
|
---|
| 1955 |
|
---|
| 1956 | #else /* Non-LFN configuration */
|
---|
| 1957 | BYTE b, c, d, *sfn;
|
---|
| 1958 | UINT ni, si, i;
|
---|
| 1959 | const char *p;
|
---|
| 1960 |
|
---|
| 1961 | /* Create file name in directory form */
|
---|
| 1962 | for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
|
---|
| 1963 | sfn = dp->fn;
|
---|
| 1964 | mem_set(sfn, ' ', 11);
|
---|
| 1965 | si = i = b = 0; ni = 8;
|
---|
| 1966 | #if _FS_RPATH
|
---|
| 1967 | if (p[si] == '.') { /* Is this a dot entry? */
|
---|
| 1968 | for (;;) {
|
---|
| 1969 | c = (BYTE)p[si++];
|
---|
| 1970 | if (c != '.' || si >= 3) break;
|
---|
| 1971 | sfn[i++] = c;
|
---|
| 1972 | }
|
---|
| 1973 | if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;
|
---|
| 1974 | *path = &p[si]; /* Return pointer to the next segment */
|
---|
| 1975 | sfn[NSFLAG] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of path */
|
---|
| 1976 | return FR_OK;
|
---|
| 1977 | }
|
---|
| 1978 | #endif
|
---|
| 1979 | for (;;) {
|
---|
| 1980 | c = (BYTE)p[si++];
|
---|
| 1981 | if (c <= ' ' || c == '/' || c == '\\') break; /* Break on end of segment */
|
---|
| 1982 | if (c == '.' || i >= ni) {
|
---|
| 1983 | if (ni != 8 || c != '.') return FR_INVALID_NAME;
|
---|
| 1984 | i = 8; ni = 11;
|
---|
| 1985 | b <<= 2; continue;
|
---|
| 1986 | }
|
---|
| 1987 | if (c >= 0x80) { /* Extended character? */
|
---|
| 1988 | b |= 3; /* Eliminate NT flag */
|
---|
| 1989 | #ifdef _EXCVT
|
---|
| 1990 | c = ExCvt[c - 0x80]; /* To upper extended characters (SBCS cfg) */
|
---|
| 1991 | #else
|
---|
| 1992 | #if !_DF1S
|
---|
| 1993 | return FR_INVALID_NAME; /* Reject extended characters (ASCII cfg) */
|
---|
| 1994 | #endif
|
---|
| 1995 | #endif
|
---|
| 1996 | }
|
---|
| 1997 | if (IsDBCS1(c)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
|
---|
| 1998 | d = (BYTE)p[si++]; /* Get 2nd byte */
|
---|
| 1999 | if (!IsDBCS2(d) || i >= ni - 1) /* Reject invalid DBC */
|
---|
| 2000 | return FR_INVALID_NAME;
|
---|
| 2001 | sfn[i++] = c;
|
---|
| 2002 | sfn[i++] = d;
|
---|
| 2003 | } else { /* Single byte code */
|
---|
| 2004 | if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) /* Reject illegal chrs for SFN */
|
---|
| 2005 | return FR_INVALID_NAME;
|
---|
| 2006 | if (IsUpper(c)) { /* ASCII large capital? */
|
---|
| 2007 | b |= 2;
|
---|
| 2008 | } else {
|
---|
| 2009 | if (IsLower(c)) { /* ASCII small capital? */
|
---|
| 2010 | b |= 1; c -= 0x20;
|
---|
| 2011 | }
|
---|
| 2012 | }
|
---|
| 2013 | sfn[i++] = c;
|
---|
| 2014 | }
|
---|
| 2015 | }
|
---|
| 2016 | *path = &p[si]; /* Return pointer to the next segment */
|
---|
| 2017 | c = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
|
---|
| 2018 |
|
---|
| 2019 | if (!i) return FR_INVALID_NAME; /* Reject nul string */
|
---|
| 2020 | if (sfn[0] == DDE) sfn[0] = NDDE; /* When first character collides with DDE, replace it with 0x05 */
|
---|
| 2021 |
|
---|
| 2022 | if (ni == 8) b <<= 2;
|
---|
| 2023 | if ((b & 0x03) == 0x01) c |= NS_EXT; /* NT flag (Name extension has only small capital) */
|
---|
| 2024 | if ((b & 0x0C) == 0x04) c |= NS_BODY; /* NT flag (Name body has only small capital) */
|
---|
| 2025 |
|
---|
| 2026 | sfn[NSFLAG] = c; /* Store NT flag, File name is created */
|
---|
| 2027 |
|
---|
| 2028 | return FR_OK;
|
---|
| 2029 | #endif
|
---|
| 2030 | }
|
---|
| 2031 |
|
---|
| 2032 |
|
---|
| 2033 |
|
---|
| 2034 |
|
---|
| 2035 | /*-----------------------------------------------------------------------*/
|
---|
| 2036 | /* Follow a file path */
|
---|
| 2037 | /*-----------------------------------------------------------------------*/
|
---|
| 2038 |
|
---|
| 2039 | static
|
---|
| 2040 | FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
|
---|
| 2041 | DIR* dp, /* Directory object to return last directory and found object */
|
---|
| 2042 | const TCHAR* path /* Full-path string to find a file or directory */
|
---|
| 2043 | )
|
---|
| 2044 | {
|
---|
| 2045 | FRESULT res;
|
---|
| 2046 | BYTE *dir, ns;
|
---|
| 2047 |
|
---|
| 2048 |
|
---|
| 2049 | #if _FS_RPATH
|
---|
| 2050 | if (*path == '/' || *path == '\\') { /* There is a heading separator */
|
---|
| 2051 | path++; dp->sclust = 0; /* Strip it and start from the root directory */
|
---|
| 2052 | } else { /* No heading separator */
|
---|
| 2053 | dp->sclust = dp->fs->cdir; /* Start from the current directory */
|
---|
| 2054 | }
|
---|
| 2055 | #else
|
---|
| 2056 | if (*path == '/' || *path == '\\') /* Strip heading separator if exist */
|
---|
| 2057 | path++;
|
---|
| 2058 | dp->sclust = 0; /* Always start from the root directory */
|
---|
| 2059 | #endif
|
---|
| 2060 |
|
---|
| 2061 | if ((UINT)*path < ' ') { /* Null path name is the origin directory itself */
|
---|
| 2062 | res = dir_sdi(dp, 0);
|
---|
| 2063 | dp->dir = 0;
|
---|
| 2064 | } else { /* Follow path */
|
---|
| 2065 | for (;;) {
|
---|
| 2066 | res = create_name(dp, &path); /* Get a segment name of the path */
|
---|
| 2067 | if (res != FR_OK) break;
|
---|
| 2068 | res = dir_find(dp); /* Find an object with the sagment name */
|
---|
| 2069 | ns = dp->fn[NSFLAG];
|
---|
| 2070 | if (res != FR_OK) { /* Failed to find the object */
|
---|
| 2071 | if (res == FR_NO_FILE) { /* Object is not found */
|
---|
| 2072 | if (_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exist, */
|
---|
| 2073 | dp->sclust = 0; dp->dir = 0; /* it is the root directory and stay there */
|
---|
| 2074 | if (!(ns & NS_LAST)) continue; /* Continue to follow if not last segment */
|
---|
| 2075 | res = FR_OK; /* Ended at the root directroy. Function completed. */
|
---|
| 2076 | } else { /* Could not find the object */
|
---|
| 2077 | if (!(ns & NS_LAST)) res = FR_NO_PATH; /* Adjust error code if not last segment */
|
---|
| 2078 | }
|
---|
| 2079 | }
|
---|
| 2080 | break;
|
---|
| 2081 | }
|
---|
| 2082 | if (ns & NS_LAST) break; /* Last segment matched. Function completed. */
|
---|
| 2083 | dir = dp->dir; /* Follow the sub-directory */
|
---|
| 2084 | if (!(dir[DIR_Attr] & AM_DIR)) { /* It is not a sub-directory and cannot follow */
|
---|
| 2085 | res = FR_NO_PATH; break;
|
---|
| 2086 | }
|
---|
| 2087 | dp->sclust = ld_clust(dp->fs, dir);
|
---|
| 2088 | }
|
---|
| 2089 | }
|
---|
| 2090 |
|
---|
| 2091 | return res;
|
---|
| 2092 | }
|
---|
| 2093 |
|
---|
| 2094 |
|
---|
| 2095 |
|
---|
| 2096 |
|
---|
| 2097 | /*-----------------------------------------------------------------------*/
|
---|
| 2098 | /* Get logical drive number from path name */
|
---|
| 2099 | /*-----------------------------------------------------------------------*/
|
---|
| 2100 |
|
---|
| 2101 | static
|
---|
| 2102 | int get_ldnumber ( /* Returns logical drive number (-1:invalid drive) */
|
---|
| 2103 | const TCHAR** path /* Pointer to pointer to the path name */
|
---|
| 2104 | )
|
---|
| 2105 | {
|
---|
| 2106 | const TCHAR *tp, *tt;
|
---|
| 2107 | UINT i;
|
---|
| 2108 | int vol = -1;
|
---|
| 2109 |
|
---|
| 2110 |
|
---|
| 2111 | if (*path) { /* If the pointer is not a null */
|
---|
| 2112 | for (tt = *path; (UINT)*tt >= (_USE_LFN ? ' ' : '!') && *tt != ':'; tt++) ; /* Find ':' in the path */
|
---|
| 2113 | if (*tt == ':') { /* If a ':' is exist in the path name */
|
---|
| 2114 | tp = *path;
|
---|
| 2115 | i = *tp++ - '0';
|
---|
| 2116 | if (i < 10 && tp == tt) { /* Is there a numeric drive id? */
|
---|
| 2117 | if (i < _VOLUMES) { /* If a drive id is found, get the value and strip it */
|
---|
| 2118 | vol = (int)i;
|
---|
| 2119 | *path = ++tt;
|
---|
| 2120 | }
|
---|
| 2121 | } else { /* No numeric drive number */
|
---|
| 2122 | #if _STR_VOLUME_ID /* Find string drive id */
|
---|
| 2123 | static const char* const str[] = {_VOLUME_STRS};
|
---|
| 2124 | const char *sp;
|
---|
| 2125 | char c;
|
---|
| 2126 | TCHAR tc;
|
---|
| 2127 |
|
---|
| 2128 | i = 0; tt++;
|
---|
| 2129 | do {
|
---|
| 2130 | sp = str[i]; tp = *path;
|
---|
| 2131 | do { /* Compare a string drive id with path name */
|
---|
| 2132 | c = *sp++; tc = *tp++;
|
---|
| 2133 | if (IsLower(tc)) tc -= 0x20;
|
---|
| 2134 | } while (c && (TCHAR)c == tc);
|
---|
| 2135 | } while ((c || tp != tt) && ++i < _VOLUMES); /* Repeat for each id until pattern match */
|
---|
| 2136 | if (i < _VOLUMES) { /* If a drive id is found, get the value and strip it */
|
---|
| 2137 | vol = (int)i;
|
---|
| 2138 | *path = tt;
|
---|
| 2139 | }
|
---|
| 2140 | #endif
|
---|
| 2141 | }
|
---|
| 2142 | return vol;
|
---|
| 2143 | }
|
---|
| 2144 | #if _FS_RPATH && _VOLUMES >= 2
|
---|
| 2145 | vol = CurrVol; /* Current drive */
|
---|
| 2146 | #else
|
---|
| 2147 | vol = 0; /* Drive 0 */
|
---|
| 2148 | #endif
|
---|
| 2149 | }
|
---|
| 2150 | return vol;
|
---|
| 2151 | }
|
---|
| 2152 |
|
---|
| 2153 |
|
---|
| 2154 |
|
---|
| 2155 |
|
---|
| 2156 | /*-----------------------------------------------------------------------*/
|
---|
| 2157 | /* Load a sector and check if it is an FAT boot sector */
|
---|
| 2158 | /*-----------------------------------------------------------------------*/
|
---|
| 2159 |
|
---|
| 2160 | static
|
---|
| 2161 | BYTE check_fs ( /* 0:FAT boor sector, 1:Valid boor sector but not FAT, 2:Not a boot sector, 3:Disk error */
|
---|
| 2162 | FATFS* fs, /* File system object */
|
---|
| 2163 | DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
|
---|
| 2164 | )
|
---|
| 2165 | {
|
---|
| 2166 | fs->wflag = 0; fs->winsect = 0xFFFFFFFF; /* Invaidate window */
|
---|
| 2167 | if (move_window(fs, sect) != FR_OK) /* Load boot record */
|
---|
| 2168 | return 3;
|
---|
| 2169 |
|
---|
| 2170 | if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check boot record signature (always placed at offset 510 even if the sector size is >512) */
|
---|
| 2171 | return 2;
|
---|
| 2172 |
|
---|
| 2173 | if ((LD_DWORD(&fs->win[BS_FilSysType]) & 0xFFFFFF) == 0x544146) /* Check "FAT" string */
|
---|
| 2174 | return 0;
|
---|
| 2175 | if ((LD_DWORD(&fs->win[BS_FilSysType32]) & 0xFFFFFF) == 0x544146) /* Check "FAT" string */
|
---|
| 2176 | return 0;
|
---|
| 2177 |
|
---|
| 2178 | return 1;
|
---|
| 2179 | }
|
---|
| 2180 |
|
---|
| 2181 |
|
---|
| 2182 |
|
---|
| 2183 |
|
---|
| 2184 | /*-----------------------------------------------------------------------*/
|
---|
| 2185 | /* Find logical drive and check if the volume is mounted */
|
---|
| 2186 | /*-----------------------------------------------------------------------*/
|
---|
| 2187 |
|
---|
| 2188 | static
|
---|
| 2189 | FRESULT find_volume ( /* FR_OK(0): successful, !=0: any error occurred */
|
---|
| 2190 | FATFS** rfs, /* Pointer to pointer to the found file system object */
|
---|
| 2191 | const TCHAR** path, /* Pointer to pointer to the path name (drive number) */
|
---|
| 2192 | BYTE wmode /* !=0: Check write protection for write access */
|
---|
| 2193 | )
|
---|
| 2194 | {
|
---|
| 2195 | BYTE fmt;
|
---|
| 2196 | int vol;
|
---|
| 2197 | DSTATUS stat;
|
---|
| 2198 | DWORD bsect, fasize, tsect, sysect, nclst, szbfat;
|
---|
| 2199 | WORD nrsv;
|
---|
| 2200 | FATFS *fs;
|
---|
| 2201 |
|
---|
| 2202 |
|
---|
| 2203 | /* Get logical drive number from the path name */
|
---|
| 2204 | *rfs = 0;
|
---|
| 2205 | vol = get_ldnumber(path);
|
---|
| 2206 | if (vol < 0) return FR_INVALID_DRIVE;
|
---|
| 2207 |
|
---|
| 2208 | /* Check if the file system object is valid or not */
|
---|
| 2209 | fs = FatFs[vol]; /* Get pointer to the file system object */
|
---|
| 2210 | if (!fs) return FR_NOT_ENABLED; /* Is the file system object available? */
|
---|
| 2211 |
|
---|
| 2212 | ENTER_FF(fs); /* Lock the volume */
|
---|
| 2213 | *rfs = fs; /* Return pointer to the file system object */
|
---|
| 2214 |
|
---|
| 2215 | if (fs->fs_type) { /* If the volume has been mounted */
|
---|
| 2216 | stat = disk_status(fs->drv);
|
---|
| 2217 | if (!(stat & STA_NOINIT)) { /* and the physical drive is kept initialized */
|
---|
| 2218 | if (!_FS_READONLY && wmode && (stat & STA_PROTECT)) /* Check write protection if needed */
|
---|
| 2219 | return FR_WRITE_PROTECTED;
|
---|
| 2220 | return FR_OK; /* The file system object is valid */
|
---|
| 2221 | }
|
---|
| 2222 | }
|
---|
| 2223 |
|
---|
| 2224 | /* The file system object is not valid. */
|
---|
| 2225 | /* Following code attempts to mount the volume. (analyze BPB and initialize the fs object) */
|
---|
| 2226 |
|
---|
| 2227 | fs->fs_type = 0; /* Clear the file system object */
|
---|
| 2228 | fs->drv = LD2PD(vol); /* Bind the logical drive and a physical drive */
|
---|
| 2229 | stat = disk_initialize(fs->drv); /* Initialize the physical drive */
|
---|
| 2230 | if (stat & STA_NOINIT) /* Check if the initialization succeeded */
|
---|
| 2231 | return FR_NOT_READY; /* Failed to initialize due to no medium or hard error */
|
---|
| 2232 | if (!_FS_READONLY && wmode && (stat & STA_PROTECT)) /* Check disk write protection if needed */
|
---|
| 2233 | return FR_WRITE_PROTECTED;
|
---|
| 2234 | #if _MAX_SS != _MIN_SS /* Get sector size (multiple sector size cfg only) */
|
---|
| 2235 | if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
|
---|
| 2236 | || SS(fs) < _MIN_SS || SS(fs) > _MAX_SS) return FR_DISK_ERR;
|
---|
| 2237 | #endif
|
---|
| 2238 | /* Find an FAT partition on the drive. Supports only generic partitioning, FDISK and SFD. */
|
---|
| 2239 | bsect = 0;
|
---|
| 2240 | fmt = check_fs(fs, bsect); /* Load sector 0 and check if it is an FAT boot sector as SFD */
|
---|
| 2241 | if (fmt == 1 || (!fmt && (LD2PT(vol)))) { /* Not an FAT boot sector or forced partition number */
|
---|
| 2242 | UINT i;
|
---|
| 2243 | DWORD br[4];
|
---|
| 2244 |
|
---|
| 2245 | for (i = 0; i < 4; i++) { /* Get partition offset */
|
---|
| 2246 | BYTE *pt = fs->win+MBR_Table + i * SZ_PTE;
|
---|
| 2247 | br[i] = pt[4] ? LD_DWORD(&pt[8]) : 0;
|
---|
| 2248 | }
|
---|
| 2249 | i = LD2PT(vol); /* Partition number: 0:auto, 1-4:forced */
|
---|
| 2250 | if (i) i--;
|
---|
| 2251 | do { /* Find an FAT volume */
|
---|
| 2252 | bsect = br[i];
|
---|
| 2253 | fmt = bsect ? check_fs(fs, bsect) : 2; /* Check the partition */
|
---|
| 2254 | } while (!LD2PT(vol) && fmt && ++i < 4);
|
---|
| 2255 | }
|
---|
| 2256 | if (fmt == 3) return FR_DISK_ERR; /* An error occured in the disk I/O layer */
|
---|
| 2257 | if (fmt) return FR_NO_FILESYSTEM; /* No FAT volume is found */
|
---|
| 2258 |
|
---|
| 2259 | /* An FAT volume is found. Following code initializes the file system object */
|
---|
| 2260 |
|
---|
| 2261 | if (LD_WORD(fs->win+BPB_BytsPerSec) != SS(fs)) /* (BPB_BytsPerSec must be equal to the physical sector size) */
|
---|
| 2262 | return FR_NO_FILESYSTEM;
|
---|
| 2263 |
|
---|
| 2264 | fasize = LD_WORD(fs->win+BPB_FATSz16); /* Number of sectors per FAT */
|
---|
| 2265 | if (!fasize) fasize = LD_DWORD(fs->win+BPB_FATSz32);
|
---|
| 2266 | fs->fsize = fasize;
|
---|
| 2267 |
|
---|
| 2268 | fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FAT copies */
|
---|
| 2269 | if (fs->n_fats != 1 && fs->n_fats != 2) /* (Must be 1 or 2) */
|
---|
| 2270 | return FR_NO_FILESYSTEM;
|
---|
| 2271 | fasize *= fs->n_fats; /* Number of sectors for FAT area */
|
---|
| 2272 |
|
---|
| 2273 | fs->csize = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
|
---|
| 2274 | if (!fs->csize || (fs->csize & (fs->csize - 1))) /* (Must be power of 2) */
|
---|
| 2275 | return FR_NO_FILESYSTEM;
|
---|
| 2276 |
|
---|
| 2277 | fs->n_rootdir = LD_WORD(fs->win+BPB_RootEntCnt); /* Number of root directory entries */
|
---|
| 2278 | if (fs->n_rootdir % (SS(fs) / SZ_DIR)) /* (Must be sector aligned) */
|
---|
| 2279 | return FR_NO_FILESYSTEM;
|
---|
| 2280 |
|
---|
| 2281 | tsect = LD_WORD(fs->win+BPB_TotSec16); /* Number of sectors on the volume */
|
---|
| 2282 | if (!tsect) tsect = LD_DWORD(fs->win+BPB_TotSec32);
|
---|
| 2283 |
|
---|
| 2284 | nrsv = LD_WORD(fs->win+BPB_RsvdSecCnt); /* Number of reserved sectors */
|
---|
| 2285 | if (!nrsv) return FR_NO_FILESYSTEM; /* (Must not be 0) */
|
---|
| 2286 |
|
---|
| 2287 | /* Determine the FAT sub type */
|
---|
| 2288 | sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZ_DIR); /* RSV+FAT+DIR */
|
---|
| 2289 | if (tsect < sysect) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
|
---|
| 2290 | nclst = (tsect - sysect) / fs->csize; /* Number of clusters */
|
---|
| 2291 | if (!nclst) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
|
---|
| 2292 | fmt = FS_FAT12;
|
---|
| 2293 | if (nclst >= MIN_FAT16) fmt = FS_FAT16;
|
---|
| 2294 | if (nclst >= MIN_FAT32) fmt = FS_FAT32;
|
---|
| 2295 |
|
---|
| 2296 | /* Boundaries and Limits */
|
---|
| 2297 | fs->n_fatent = nclst + 2; /* Number of FAT entries */
|
---|
| 2298 | fs->volbase = bsect; /* Volume start sector */
|
---|
| 2299 | fs->fatbase = bsect + nrsv; /* FAT start sector */
|
---|
| 2300 | fs->database = bsect + sysect; /* Data start sector */
|
---|
| 2301 | if (fmt == FS_FAT32) {
|
---|
| 2302 | if (fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */
|
---|
| 2303 | fs->dirbase = LD_DWORD(fs->win+BPB_RootClus); /* Root directory start cluster */
|
---|
| 2304 | szbfat = fs->n_fatent * 4; /* (Needed FAT size) */
|
---|
| 2305 | } else {
|
---|
| 2306 | if (!fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must not be 0) */
|
---|
| 2307 | fs->dirbase = fs->fatbase + fasize; /* Root directory start sector */
|
---|
| 2308 | szbfat = (fmt == FS_FAT16) ? /* (Needed FAT size) */
|
---|
| 2309 | fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
|
---|
| 2310 | }
|
---|
| 2311 | if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs)) /* (BPB_FATSz must not be less than the size needed) */
|
---|
| 2312 | return FR_NO_FILESYSTEM;
|
---|
| 2313 |
|
---|
| 2314 | #if !_FS_READONLY
|
---|
| 2315 | /* Initialize cluster allocation information */
|
---|
| 2316 | fs->last_clust = fs->free_clust = 0xFFFFFFFF;
|
---|
| 2317 |
|
---|
| 2318 | /* Get fsinfo if available */
|
---|
| 2319 | fs->fsi_flag = 0x80;
|
---|
| 2320 | #if (_FS_NOFSINFO & 3) != 3
|
---|
| 2321 | if (fmt == FS_FAT32 /* Enable FSINFO only if FAT32 and BPB_FSInfo is 1 */
|
---|
| 2322 | && LD_WORD(fs->win+BPB_FSInfo) == 1
|
---|
| 2323 | && move_window(fs, bsect + 1) == FR_OK)
|
---|
| 2324 | {
|
---|
| 2325 | fs->fsi_flag = 0;
|
---|
| 2326 | if (LD_WORD(fs->win+BS_55AA) == 0xAA55 /* Load FSINFO data if available */
|
---|
| 2327 | && LD_DWORD(fs->win+FSI_LeadSig) == 0x41615252
|
---|
| 2328 | && LD_DWORD(fs->win+FSI_StrucSig) == 0x61417272)
|
---|
| 2329 | {
|
---|
| 2330 | #if (_FS_NOFSINFO & 1) == 0
|
---|
| 2331 | fs->free_clust = LD_DWORD(fs->win+FSI_Free_Count);
|
---|
| 2332 | #endif
|
---|
| 2333 | #if (_FS_NOFSINFO & 2) == 0
|
---|
| 2334 | fs->last_clust = LD_DWORD(fs->win+FSI_Nxt_Free);
|
---|
| 2335 | #endif
|
---|
| 2336 | }
|
---|
| 2337 | }
|
---|
| 2338 | #endif
|
---|
| 2339 | #endif
|
---|
| 2340 | fs->fs_type = fmt; /* FAT sub-type */
|
---|
| 2341 | fs->id = ++Fsid; /* File system mount ID */
|
---|
| 2342 | #if _FS_RPATH
|
---|
| 2343 | fs->cdir = 0; /* Set current directory to root */
|
---|
| 2344 | #endif
|
---|
| 2345 | #if _FS_LOCK /* Clear file lock semaphores */
|
---|
| 2346 | clear_lock(fs);
|
---|
| 2347 | #endif
|
---|
| 2348 |
|
---|
| 2349 | return FR_OK;
|
---|
| 2350 | }
|
---|
| 2351 |
|
---|
| 2352 |
|
---|
| 2353 |
|
---|
| 2354 |
|
---|
| 2355 | /*-----------------------------------------------------------------------*/
|
---|
| 2356 | /* Check if the file/directory object is valid or not */
|
---|
| 2357 | /*-----------------------------------------------------------------------*/
|
---|
| 2358 |
|
---|
| 2359 | static
|
---|
| 2360 | FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */
|
---|
| 2361 | void* obj /* Pointer to the object FIL/DIR to check validity */
|
---|
| 2362 | )
|
---|
| 2363 | {
|
---|
| 2364 | FIL *fil = (FIL*)obj; /* Assuming offset of .fs and .id in the FIL/DIR structure is identical */
|
---|
| 2365 |
|
---|
| 2366 |
|
---|
| 2367 | if (!fil || !fil->fs || !fil->fs->fs_type || fil->fs->id != fil->id)
|
---|
| 2368 | return FR_INVALID_OBJECT;
|
---|
| 2369 |
|
---|
| 2370 | ENTER_FF(fil->fs); /* Lock file system */
|
---|
| 2371 |
|
---|
| 2372 | if (disk_status(fil->fs->drv) & STA_NOINIT)
|
---|
| 2373 | return FR_NOT_READY;
|
---|
| 2374 |
|
---|
| 2375 | return FR_OK;
|
---|
| 2376 | }
|
---|
| 2377 |
|
---|
| 2378 |
|
---|
| 2379 |
|
---|
| 2380 |
|
---|
| 2381 | /*--------------------------------------------------------------------------
|
---|
| 2382 |
|
---|
| 2383 | Public Functions
|
---|
| 2384 |
|
---|
| 2385 | --------------------------------------------------------------------------*/
|
---|
| 2386 |
|
---|
| 2387 |
|
---|
| 2388 |
|
---|
| 2389 | /*-----------------------------------------------------------------------*/
|
---|
| 2390 | /* Mount/Unmount a Logical Drive */
|
---|
| 2391 | /*-----------------------------------------------------------------------*/
|
---|
| 2392 |
|
---|
| 2393 | FRESULT f_mount (
|
---|
| 2394 | FATFS* fs, /* Pointer to the file system object (NULL:unmount)*/
|
---|
| 2395 | const TCHAR* path, /* Logical drive number to be mounted/unmounted */
|
---|
| 2396 | BYTE opt /* 0:Do not mount (delayed mount), 1:Mount immediately */
|
---|
| 2397 | )
|
---|
| 2398 | {
|
---|
| 2399 | FATFS *cfs;
|
---|
| 2400 | int vol;
|
---|
| 2401 | FRESULT res;
|
---|
| 2402 | const TCHAR *rp = path;
|
---|
| 2403 |
|
---|
| 2404 |
|
---|
| 2405 | vol = get_ldnumber(&rp);
|
---|
| 2406 | if (vol < 0) return FR_INVALID_DRIVE;
|
---|
| 2407 | cfs = FatFs[vol]; /* Pointer to fs object */
|
---|
| 2408 |
|
---|
| 2409 | if (cfs) {
|
---|
| 2410 | #if _FS_LOCK
|
---|
| 2411 | clear_lock(cfs);
|
---|
| 2412 | #endif
|
---|
| 2413 | #if _FS_REENTRANT /* Discard sync object of the current volume */
|
---|
| 2414 | if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR;
|
---|
| 2415 | #endif
|
---|
| 2416 | cfs->fs_type = 0; /* Clear old fs object */
|
---|
| 2417 | }
|
---|
| 2418 |
|
---|
| 2419 | if (fs) {
|
---|
| 2420 | fs->fs_type = 0; /* Clear new fs object */
|
---|
| 2421 | #if _FS_REENTRANT /* Create sync object for the new volume */
|
---|
| 2422 | if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR;
|
---|
| 2423 | #endif
|
---|
| 2424 | }
|
---|
| 2425 | FatFs[vol] = fs; /* Register new fs object */
|
---|
| 2426 |
|
---|
| 2427 | if (!fs || opt != 1) return FR_OK; /* Do not mount now, it will be mounted later */
|
---|
| 2428 |
|
---|
| 2429 | res = find_volume(&fs, &path, 0); /* Force mounted the volume */
|
---|
| 2430 | LEAVE_FF(fs, res);
|
---|
| 2431 | }
|
---|
| 2432 |
|
---|
| 2433 |
|
---|
| 2434 |
|
---|
| 2435 |
|
---|
| 2436 | /*-----------------------------------------------------------------------*/
|
---|
| 2437 | /* Open or Create a File */
|
---|
| 2438 | /*-----------------------------------------------------------------------*/
|
---|
| 2439 |
|
---|
| 2440 | FRESULT f_open (
|
---|
| 2441 | FIL* fp, /* Pointer to the blank file object */
|
---|
| 2442 | const TCHAR* path, /* Pointer to the file name */
|
---|
| 2443 | BYTE mode /* Access mode and file open mode flags */
|
---|
| 2444 | )
|
---|
| 2445 | {
|
---|
| 2446 | FRESULT res;
|
---|
| 2447 | DIR dj;
|
---|
| 2448 | BYTE *dir;
|
---|
| 2449 | DEF_NAMEBUF;
|
---|
| 2450 |
|
---|
| 2451 |
|
---|
| 2452 | if (!fp) return FR_INVALID_OBJECT;
|
---|
| 2453 | fp->fs = 0; /* Clear file object */
|
---|
| 2454 |
|
---|
| 2455 | /* Get logical drive number */
|
---|
| 2456 | #if !_FS_READONLY
|
---|
| 2457 | mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW;
|
---|
| 2458 | res = find_volume(&dj.fs, &path, (BYTE)(mode & ~FA_READ));
|
---|
| 2459 | #else
|
---|
| 2460 | mode &= FA_READ;
|
---|
| 2461 | res = find_volume(&dj.fs, &path, 0);
|
---|
| 2462 | #endif
|
---|
| 2463 | if (res == FR_OK) {
|
---|
| 2464 | INIT_BUF(dj);
|
---|
| 2465 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
| 2466 | dir = dj.dir;
|
---|
| 2467 | #if !_FS_READONLY /* R/W configuration */
|
---|
| 2468 | if (res == FR_OK) {
|
---|
| 2469 | if (!dir) /* Default directory itself */
|
---|
| 2470 | res = FR_INVALID_NAME;
|
---|
| 2471 | #if _FS_LOCK
|
---|
| 2472 | else
|
---|
| 2473 | res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
|
---|
| 2474 | #endif
|
---|
| 2475 | }
|
---|
| 2476 | /* Create or Open a file */
|
---|
| 2477 | if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
|
---|
| 2478 | DWORD dw, cl;
|
---|
| 2479 |
|
---|
| 2480 | if (res != FR_OK) { /* No file, create new */
|
---|
| 2481 | if (res == FR_NO_FILE) /* There is no file to open, create a new entry */
|
---|
| 2482 | #if _FS_LOCK
|
---|
| 2483 | res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
|
---|
| 2484 | #else
|
---|
| 2485 | res = dir_register(&dj);
|
---|
| 2486 | #endif
|
---|
| 2487 | mode |= FA_CREATE_ALWAYS; /* File is created */
|
---|
| 2488 | dir = dj.dir; /* New entry */
|
---|
| 2489 | }
|
---|
| 2490 | else { /* Any object is already existing */
|
---|
| 2491 | if (dir[DIR_Attr] & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or DIR) */
|
---|
| 2492 | res = FR_DENIED;
|
---|
| 2493 | } else {
|
---|
| 2494 | if (mode & FA_CREATE_NEW) /* Cannot create as new file */
|
---|
| 2495 | res = FR_EXIST;
|
---|
| 2496 | }
|
---|
| 2497 | }
|
---|
| 2498 | if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate it if overwrite mode */
|
---|
| 2499 | dw = GET_FATTIME(); /* Created time */
|
---|
| 2500 | ST_DWORD(dir+DIR_CrtTime, dw);
|
---|
| 2501 | dir[DIR_Attr] = 0; /* Reset attribute */
|
---|
| 2502 | ST_DWORD(dir+DIR_FileSize, 0); /* size = 0 */
|
---|
| 2503 | cl = ld_clust(dj.fs, dir); /* Get start cluster */
|
---|
| 2504 | st_clust(dir, 0); /* cluster = 0 */
|
---|
| 2505 | dj.fs->wflag = 1;
|
---|
| 2506 | if (cl) { /* Remove the cluster chain if exist */
|
---|
| 2507 | dw = dj.fs->winsect;
|
---|
| 2508 | res = remove_chain(dj.fs, cl);
|
---|
| 2509 | if (res == FR_OK) {
|
---|
| 2510 | dj.fs->last_clust = cl - 1; /* Reuse the cluster hole */
|
---|
| 2511 | res = move_window(dj.fs, dw);
|
---|
| 2512 | }
|
---|
| 2513 | }
|
---|
| 2514 | }
|
---|
| 2515 | }
|
---|
| 2516 | else { /* Open an existing file */
|
---|
| 2517 | if (res == FR_OK) { /* Follow succeeded */
|
---|
| 2518 | if (dir[DIR_Attr] & AM_DIR) { /* It is a directory */
|
---|
| 2519 | res = FR_NO_FILE;
|
---|
| 2520 | } else {
|
---|
| 2521 | if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
|
---|
| 2522 | res = FR_DENIED;
|
---|
| 2523 | }
|
---|
| 2524 | }
|
---|
| 2525 | }
|
---|
| 2526 | if (res == FR_OK) {
|
---|
| 2527 | if (mode & FA_CREATE_ALWAYS) /* Set file change flag if created or overwritten */
|
---|
| 2528 | mode |= FA__WRITTEN;
|
---|
| 2529 | fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
|
---|
| 2530 | fp->dir_ptr = dir;
|
---|
| 2531 | #if _FS_LOCK
|
---|
| 2532 | fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
|
---|
| 2533 | if (!fp->lockid) res = FR_INT_ERR;
|
---|
| 2534 | #endif
|
---|
| 2535 | }
|
---|
| 2536 |
|
---|
| 2537 | #else /* R/O configuration */
|
---|
| 2538 | if (res == FR_OK) { /* Follow succeeded */
|
---|
| 2539 | dir = dj.dir;
|
---|
| 2540 | if (!dir) { /* Current directory itself */
|
---|
| 2541 | res = FR_INVALID_NAME;
|
---|
| 2542 | } else {
|
---|
| 2543 | if (dir[DIR_Attr] & AM_DIR) /* It is a directory */
|
---|
| 2544 | res = FR_NO_FILE;
|
---|
| 2545 | }
|
---|
| 2546 | }
|
---|
| 2547 | #endif
|
---|
| 2548 | FREE_BUF();
|
---|
| 2549 |
|
---|
| 2550 | if (res == FR_OK) {
|
---|
| 2551 | fp->flag = mode; /* File access mode */
|
---|
| 2552 | fp->err = 0; /* Clear error flag */
|
---|
| 2553 | fp->sclust = ld_clust(dj.fs, dir); /* File start cluster */
|
---|
| 2554 | fp->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */
|
---|
| 2555 | fp->fptr = 0; /* File pointer */
|
---|
| 2556 | fp->dsect = 0;
|
---|
| 2557 | #if _USE_FASTSEEK
|
---|
| 2558 | fp->cltbl = 0; /* Normal seek mode */
|
---|
| 2559 | #endif
|
---|
| 2560 | fp->fs = dj.fs; /* Validate file object */
|
---|
| 2561 | fp->id = fp->fs->id;
|
---|
| 2562 | }
|
---|
| 2563 | }
|
---|
| 2564 |
|
---|
| 2565 | LEAVE_FF(dj.fs, res);
|
---|
| 2566 | }
|
---|
| 2567 |
|
---|
| 2568 |
|
---|
| 2569 |
|
---|
| 2570 |
|
---|
| 2571 | /*-----------------------------------------------------------------------*/
|
---|
| 2572 | /* Read File */
|
---|
| 2573 | /*-----------------------------------------------------------------------*/
|
---|
| 2574 |
|
---|
| 2575 | FRESULT f_read (
|
---|
| 2576 | FIL* fp, /* Pointer to the file object */
|
---|
| 2577 | void* buff, /* Pointer to data buffer */
|
---|
| 2578 | UINT btr, /* Number of bytes to read */
|
---|
| 2579 | UINT* br /* Pointer to number of bytes read */
|
---|
| 2580 | )
|
---|
| 2581 | {
|
---|
| 2582 | FRESULT res;
|
---|
| 2583 | DWORD clst, sect, remain;
|
---|
| 2584 | UINT rcnt, cc;
|
---|
| 2585 | BYTE csect, *rbuff = (BYTE*)buff;
|
---|
| 2586 |
|
---|
| 2587 |
|
---|
| 2588 | *br = 0; /* Clear read byte counter */
|
---|
| 2589 |
|
---|
| 2590 | res = validate(fp); /* Check validity */
|
---|
| 2591 | if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
---|
| 2592 | if (fp->err) /* Check error */
|
---|
| 2593 | LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
---|
| 2594 | if (!(fp->flag & FA_READ)) /* Check access mode */
|
---|
| 2595 | LEAVE_FF(fp->fs, FR_DENIED);
|
---|
| 2596 | remain = fp->fsize - fp->fptr;
|
---|
| 2597 | if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
|
---|
| 2598 |
|
---|
| 2599 | for ( ; btr; /* Repeat until all data read */
|
---|
| 2600 | rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
|
---|
| 2601 | if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
|
---|
| 2602 | csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
|
---|
| 2603 | if (!csect) { /* On the cluster boundary? */
|
---|
| 2604 | if (fp->fptr == 0) { /* On the top of the file? */
|
---|
| 2605 | clst = fp->sclust; /* Follow from the origin */
|
---|
| 2606 | } else { /* Middle or end of the file */
|
---|
| 2607 | #if _USE_FASTSEEK
|
---|
| 2608 | if (fp->cltbl)
|
---|
| 2609 | clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
|
---|
| 2610 | else
|
---|
| 2611 | #endif
|
---|
| 2612 | clst = get_fat(fp->fs, fp->clust); /* Follow cluster chain on the FAT */
|
---|
| 2613 | }
|
---|
| 2614 | if (clst < 2) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 2615 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2616 | fp->clust = clst; /* Update current cluster */
|
---|
| 2617 | }
|
---|
| 2618 | sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
|
---|
| 2619 | if (!sect) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 2620 | sect += csect;
|
---|
| 2621 | cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */
|
---|
| 2622 | if (cc) { /* Read maximum contiguous sectors directly */
|
---|
| 2623 | if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
|
---|
| 2624 | cc = fp->fs->csize - csect;
|
---|
| 2625 | if (disk_read(fp->fs->drv, rbuff, sect, cc) != RES_OK)
|
---|
| 2626 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2627 | #if !_FS_READONLY && _FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */
|
---|
| 2628 | #if _FS_TINY
|
---|
| 2629 | if (fp->fs->wflag && fp->fs->winsect - sect < cc)
|
---|
| 2630 | mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));
|
---|
| 2631 | #else
|
---|
| 2632 | if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc)
|
---|
| 2633 | mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));
|
---|
| 2634 | #endif
|
---|
| 2635 | #endif
|
---|
| 2636 | rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
|
---|
| 2637 | continue;
|
---|
| 2638 | }
|
---|
| 2639 | #if !_FS_TINY
|
---|
| 2640 | if (fp->dsect != sect) { /* Load data sector if not in cache */
|
---|
| 2641 | #if !_FS_READONLY
|
---|
| 2642 | if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
|
---|
| 2643 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
| 2644 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2645 | fp->flag &= ~FA__DIRTY;
|
---|
| 2646 | }
|
---|
| 2647 | #endif
|
---|
| 2648 | if (disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK) /* Fill sector cache */
|
---|
| 2649 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2650 | }
|
---|
| 2651 | #endif
|
---|
| 2652 | fp->dsect = sect;
|
---|
| 2653 | }
|
---|
| 2654 | rcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs)); /* Get partial sector data from sector buffer */
|
---|
| 2655 | if (rcnt > btr) rcnt = btr;
|
---|
| 2656 | #if _FS_TINY
|
---|
| 2657 | if (move_window(fp->fs, fp->dsect) != FR_OK) /* Move sector window */
|
---|
| 2658 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2659 | mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
|
---|
| 2660 | #else
|
---|
| 2661 | mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
|
---|
| 2662 | #endif
|
---|
| 2663 | }
|
---|
| 2664 |
|
---|
| 2665 | LEAVE_FF(fp->fs, FR_OK);
|
---|
| 2666 | }
|
---|
| 2667 |
|
---|
| 2668 |
|
---|
| 2669 |
|
---|
| 2670 |
|
---|
| 2671 | #if !_FS_READONLY
|
---|
| 2672 | /*-----------------------------------------------------------------------*/
|
---|
| 2673 | /* Write File */
|
---|
| 2674 | /*-----------------------------------------------------------------------*/
|
---|
| 2675 |
|
---|
| 2676 | FRESULT f_write (
|
---|
| 2677 | FIL* fp, /* Pointer to the file object */
|
---|
| 2678 | const void *buff, /* Pointer to the data to be written */
|
---|
| 2679 | UINT btw, /* Number of bytes to write */
|
---|
| 2680 | UINT* bw /* Pointer to number of bytes written */
|
---|
| 2681 | )
|
---|
| 2682 | {
|
---|
| 2683 | FRESULT res;
|
---|
| 2684 | DWORD clst, sect;
|
---|
| 2685 | UINT wcnt, cc;
|
---|
| 2686 | const BYTE *wbuff = (const BYTE*)buff;
|
---|
| 2687 | BYTE csect;
|
---|
| 2688 |
|
---|
| 2689 |
|
---|
| 2690 | *bw = 0; /* Clear write byte counter */
|
---|
| 2691 |
|
---|
| 2692 | res = validate(fp); /* Check validity */
|
---|
| 2693 | if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
---|
| 2694 | if (fp->err) /* Check error */
|
---|
| 2695 | LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
---|
| 2696 | if (!(fp->flag & FA_WRITE)) /* Check access mode */
|
---|
| 2697 | LEAVE_FF(fp->fs, FR_DENIED);
|
---|
| 2698 | if (fp->fptr + btw < fp->fptr) btw = 0; /* File size cannot reach 4GB */
|
---|
| 2699 |
|
---|
| 2700 | for ( ; btw; /* Repeat until all data written */
|
---|
| 2701 | wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
|
---|
| 2702 | if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
|
---|
| 2703 | csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
|
---|
| 2704 | if (!csect) { /* On the cluster boundary? */
|
---|
| 2705 | if (fp->fptr == 0) { /* On the top of the file? */
|
---|
| 2706 | clst = fp->sclust; /* Follow from the origin */
|
---|
| 2707 | if (clst == 0) /* When no cluster is allocated, */
|
---|
| 2708 | clst = create_chain(fp->fs, 0); /* Create a new cluster chain */
|
---|
| 2709 | } else { /* Middle or end of the file */
|
---|
| 2710 | #if _USE_FASTSEEK
|
---|
| 2711 | if (fp->cltbl)
|
---|
| 2712 | clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
|
---|
| 2713 | else
|
---|
| 2714 | #endif
|
---|
| 2715 | clst = create_chain(fp->fs, fp->clust); /* Follow or stretch cluster chain on the FAT */
|
---|
| 2716 | }
|
---|
| 2717 | if (clst == 0) break; /* Could not allocate a new cluster (disk full) */
|
---|
| 2718 | if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 2719 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2720 | fp->clust = clst; /* Update current cluster */
|
---|
| 2721 | if (fp->sclust == 0) fp->sclust = clst; /* Set start cluster if the first write */
|
---|
| 2722 | }
|
---|
| 2723 | #if _FS_TINY
|
---|
| 2724 | if (fp->fs->winsect == fp->dsect && sync_window(fp->fs)) /* Write-back sector cache */
|
---|
| 2725 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2726 | #else
|
---|
| 2727 | if (fp->flag & FA__DIRTY) { /* Write-back sector cache */
|
---|
| 2728 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
| 2729 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2730 | fp->flag &= ~FA__DIRTY;
|
---|
| 2731 | }
|
---|
| 2732 | #endif
|
---|
| 2733 | sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
|
---|
| 2734 | if (!sect) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 2735 | sect += csect;
|
---|
| 2736 | cc = btw / SS(fp->fs); /* When remaining bytes >= sector size, */
|
---|
| 2737 | if (cc) { /* Write maximum contiguous sectors directly */
|
---|
| 2738 | if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
|
---|
| 2739 | cc = fp->fs->csize - csect;
|
---|
| 2740 | if (disk_write(fp->fs->drv, wbuff, sect, cc) != RES_OK)
|
---|
| 2741 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2742 | #if _FS_MINIMIZE <= 2
|
---|
| 2743 | #if _FS_TINY
|
---|
| 2744 | if (fp->fs->winsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
|
---|
| 2745 | mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
|
---|
| 2746 | fp->fs->wflag = 0;
|
---|
| 2747 | }
|
---|
| 2748 | #else
|
---|
| 2749 | if (fp->dsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
|
---|
| 2750 | mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
|
---|
| 2751 | fp->flag &= ~FA__DIRTY;
|
---|
| 2752 | }
|
---|
| 2753 | #endif
|
---|
| 2754 | #endif
|
---|
| 2755 | wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
|
---|
| 2756 | continue;
|
---|
| 2757 | }
|
---|
| 2758 | #if _FS_TINY
|
---|
| 2759 | if (fp->fptr >= fp->fsize) { /* Avoid silly cache filling at growing edge */
|
---|
| 2760 | if (sync_window(fp->fs)) ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2761 | fp->fs->winsect = sect;
|
---|
| 2762 | }
|
---|
| 2763 | #else
|
---|
| 2764 | if (fp->dsect != sect) { /* Fill sector cache with file data */
|
---|
| 2765 | if (fp->fptr < fp->fsize &&
|
---|
| 2766 | disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK)
|
---|
| 2767 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2768 | }
|
---|
| 2769 | #endif
|
---|
| 2770 | fp->dsect = sect;
|
---|
| 2771 | }
|
---|
| 2772 | wcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs));/* Put partial sector into file I/O buffer */
|
---|
| 2773 | if (wcnt > btw) wcnt = btw;
|
---|
| 2774 | #if _FS_TINY
|
---|
| 2775 | if (move_window(fp->fs, fp->dsect) != FR_OK) /* Move sector window */
|
---|
| 2776 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 2777 | mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
|
---|
| 2778 | fp->fs->wflag = 1;
|
---|
| 2779 | #else
|
---|
| 2780 | mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
|
---|
| 2781 | fp->flag |= FA__DIRTY;
|
---|
| 2782 | #endif
|
---|
| 2783 | }
|
---|
| 2784 |
|
---|
| 2785 | if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
|
---|
| 2786 | fp->flag |= FA__WRITTEN; /* Set file change flag */
|
---|
| 2787 |
|
---|
| 2788 | LEAVE_FF(fp->fs, FR_OK);
|
---|
| 2789 | }
|
---|
| 2790 |
|
---|
| 2791 |
|
---|
| 2792 |
|
---|
| 2793 |
|
---|
| 2794 | /*-----------------------------------------------------------------------*/
|
---|
| 2795 | /* Synchronize the File */
|
---|
| 2796 | /*-----------------------------------------------------------------------*/
|
---|
| 2797 |
|
---|
| 2798 | FRESULT f_sync (
|
---|
| 2799 | FIL* fp /* Pointer to the file object */
|
---|
| 2800 | )
|
---|
| 2801 | {
|
---|
| 2802 | FRESULT res;
|
---|
| 2803 | DWORD tm;
|
---|
| 2804 | BYTE *dir;
|
---|
| 2805 |
|
---|
| 2806 |
|
---|
| 2807 | res = validate(fp); /* Check validity of the object */
|
---|
| 2808 | if (res == FR_OK) {
|
---|
| 2809 | if (fp->flag & FA__WRITTEN) { /* Has the file been written? */
|
---|
| 2810 | /* Write-back dirty buffer */
|
---|
| 2811 | #if !_FS_TINY
|
---|
| 2812 | if (fp->flag & FA__DIRTY) {
|
---|
| 2813 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
| 2814 | LEAVE_FF(fp->fs, FR_DISK_ERR);
|
---|
| 2815 | fp->flag &= ~FA__DIRTY;
|
---|
| 2816 | }
|
---|
| 2817 | #endif
|
---|
| 2818 | /* Update the directory entry */
|
---|
| 2819 | res = move_window(fp->fs, fp->dir_sect);
|
---|
| 2820 | if (res == FR_OK) {
|
---|
| 2821 | dir = fp->dir_ptr;
|
---|
| 2822 | dir[DIR_Attr] |= AM_ARC; /* Set archive bit */
|
---|
| 2823 | ST_DWORD(dir+DIR_FileSize, fp->fsize); /* Update file size */
|
---|
| 2824 | st_clust(dir, fp->sclust); /* Update start cluster */
|
---|
| 2825 | tm = GET_FATTIME(); /* Update updated time */
|
---|
| 2826 | ST_DWORD(dir+DIR_WrtTime, tm);
|
---|
| 2827 | ST_WORD(dir+DIR_LstAccDate, 0);
|
---|
| 2828 | fp->flag &= ~FA__WRITTEN;
|
---|
| 2829 | fp->fs->wflag = 1;
|
---|
| 2830 | res = sync_fs(fp->fs);
|
---|
| 2831 | }
|
---|
| 2832 | }
|
---|
| 2833 | }
|
---|
| 2834 |
|
---|
| 2835 | LEAVE_FF(fp->fs, res);
|
---|
| 2836 | }
|
---|
| 2837 |
|
---|
| 2838 | #endif /* !_FS_READONLY */
|
---|
| 2839 |
|
---|
| 2840 |
|
---|
| 2841 |
|
---|
| 2842 |
|
---|
| 2843 | /*-----------------------------------------------------------------------*/
|
---|
| 2844 | /* Close File */
|
---|
| 2845 | /*-----------------------------------------------------------------------*/
|
---|
| 2846 |
|
---|
| 2847 | FRESULT f_close (
|
---|
| 2848 | FIL *fp /* Pointer to the file object to be closed */
|
---|
| 2849 | )
|
---|
| 2850 | {
|
---|
| 2851 | FRESULT res;
|
---|
| 2852 |
|
---|
| 2853 |
|
---|
| 2854 | #if !_FS_READONLY
|
---|
| 2855 | res = f_sync(fp); /* Flush cached data */
|
---|
| 2856 | if (res == FR_OK)
|
---|
| 2857 | #endif
|
---|
| 2858 | {
|
---|
| 2859 | res = validate(fp); /* Lock volume */
|
---|
| 2860 | if (res == FR_OK) {
|
---|
| 2861 | #if _FS_REENTRANT
|
---|
| 2862 | FATFS *fs = fp->fs;
|
---|
| 2863 | #endif
|
---|
| 2864 | #if _FS_LOCK
|
---|
| 2865 | res = dec_lock(fp->lockid); /* Decrement file open counter */
|
---|
| 2866 | if (res == FR_OK)
|
---|
| 2867 | #endif
|
---|
| 2868 | fp->fs = 0; /* Invalidate file object */
|
---|
| 2869 | #if _FS_REENTRANT
|
---|
| 2870 | unlock_fs(fs, FR_OK); /* Unlock volume */
|
---|
| 2871 | #endif
|
---|
| 2872 | }
|
---|
| 2873 | }
|
---|
| 2874 | return res;
|
---|
| 2875 | }
|
---|
| 2876 |
|
---|
| 2877 |
|
---|
| 2878 |
|
---|
| 2879 |
|
---|
| 2880 | /*-----------------------------------------------------------------------*/
|
---|
| 2881 | /* Change Current Directory or Current Drive, Get Current Directory */
|
---|
| 2882 | /*-----------------------------------------------------------------------*/
|
---|
| 2883 |
|
---|
| 2884 | #if _FS_RPATH >= 1
|
---|
| 2885 | #if _VOLUMES >= 2
|
---|
| 2886 | FRESULT f_chdrive (
|
---|
| 2887 | const TCHAR* path /* Drive number */
|
---|
| 2888 | )
|
---|
| 2889 | {
|
---|
| 2890 | int vol;
|
---|
| 2891 |
|
---|
| 2892 |
|
---|
| 2893 | vol = get_ldnumber(&path);
|
---|
| 2894 | if (vol < 0) return FR_INVALID_DRIVE;
|
---|
| 2895 |
|
---|
| 2896 | CurrVol = (BYTE)vol;
|
---|
| 2897 |
|
---|
| 2898 | return FR_OK;
|
---|
| 2899 | }
|
---|
| 2900 | #endif
|
---|
| 2901 |
|
---|
| 2902 |
|
---|
| 2903 | FRESULT f_chdir (
|
---|
| 2904 | const TCHAR* path /* Pointer to the directory path */
|
---|
| 2905 | )
|
---|
| 2906 | {
|
---|
| 2907 | FRESULT res;
|
---|
| 2908 | DIR dj;
|
---|
| 2909 | DEF_NAMEBUF;
|
---|
| 2910 |
|
---|
| 2911 |
|
---|
| 2912 | /* Get logical drive number */
|
---|
| 2913 | res = find_volume(&dj.fs, &path, 0);
|
---|
| 2914 | if (res == FR_OK) {
|
---|
| 2915 | INIT_BUF(dj);
|
---|
| 2916 | res = follow_path(&dj, path); /* Follow the path */
|
---|
| 2917 | FREE_BUF();
|
---|
| 2918 | if (res == FR_OK) { /* Follow completed */
|
---|
| 2919 | if (!dj.dir) {
|
---|
| 2920 | dj.fs->cdir = dj.sclust; /* Start directory itself */
|
---|
| 2921 | } else {
|
---|
| 2922 | if (dj.dir[DIR_Attr] & AM_DIR) /* Reached to the directory */
|
---|
| 2923 | dj.fs->cdir = ld_clust(dj.fs, dj.dir);
|
---|
| 2924 | else
|
---|
| 2925 | res = FR_NO_PATH; /* Reached but a file */
|
---|
| 2926 | }
|
---|
| 2927 | }
|
---|
| 2928 | if (res == FR_NO_FILE) res = FR_NO_PATH;
|
---|
| 2929 | }
|
---|
| 2930 |
|
---|
| 2931 | LEAVE_FF(dj.fs, res);
|
---|
| 2932 | }
|
---|
| 2933 |
|
---|
| 2934 |
|
---|
| 2935 | #if _FS_RPATH >= 2
|
---|
| 2936 | FRESULT f_getcwd (
|
---|
| 2937 | TCHAR* buff, /* Pointer to the directory path */
|
---|
| 2938 | UINT len /* Size of path */
|
---|
| 2939 | )
|
---|
| 2940 | {
|
---|
| 2941 | FRESULT res;
|
---|
| 2942 | DIR dj;
|
---|
| 2943 | UINT i, n;
|
---|
| 2944 | DWORD ccl;
|
---|
| 2945 | TCHAR *tp;
|
---|
| 2946 | FILINFO fno;
|
---|
| 2947 | DEF_NAMEBUF;
|
---|
| 2948 |
|
---|
| 2949 |
|
---|
| 2950 | *buff = 0;
|
---|
| 2951 | /* Get logical drive number */
|
---|
| 2952 | res = find_volume(&dj.fs, (const TCHAR**)&buff, 0); /* Get current volume */
|
---|
| 2953 | if (res == FR_OK) {
|
---|
| 2954 | INIT_BUF(dj);
|
---|
| 2955 | i = len; /* Bottom of buffer (directory stack base) */
|
---|
| 2956 | dj.sclust = dj.fs->cdir; /* Start to follow upper directory from current directory */
|
---|
| 2957 | while ((ccl = dj.sclust) != 0) { /* Repeat while current directory is a sub-directory */
|
---|
| 2958 | res = dir_sdi(&dj, 1); /* Get parent directory */
|
---|
| 2959 | if (res != FR_OK) break;
|
---|
| 2960 | res = dir_read(&dj, 0);
|
---|
| 2961 | if (res != FR_OK) break;
|
---|
| 2962 | dj.sclust = ld_clust(dj.fs, dj.dir); /* Goto parent directory */
|
---|
| 2963 | res = dir_sdi(&dj, 0);
|
---|
| 2964 | if (res != FR_OK) break;
|
---|
| 2965 | do { /* Find the entry links to the child directory */
|
---|
| 2966 | res = dir_read(&dj, 0);
|
---|
| 2967 | if (res != FR_OK) break;
|
---|
| 2968 | if (ccl == ld_clust(dj.fs, dj.dir)) break; /* Found the entry */
|
---|
| 2969 | res = dir_next(&dj, 0);
|
---|
| 2970 | } while (res == FR_OK);
|
---|
| 2971 | if (res == FR_NO_FILE) res = FR_INT_ERR;/* It cannot be 'not found'. */
|
---|
| 2972 | if (res != FR_OK) break;
|
---|
| 2973 | #if _USE_LFN
|
---|
| 2974 | fno.lfname = buff;
|
---|
| 2975 | fno.lfsize = i;
|
---|
| 2976 | #endif
|
---|
| 2977 | get_fileinfo(&dj, &fno); /* Get the directory name and push it to the buffer */
|
---|
| 2978 | tp = fno.fname;
|
---|
| 2979 | #if _USE_LFN
|
---|
| 2980 | if (*buff) tp = buff;
|
---|
| 2981 | #endif
|
---|
| 2982 | for (n = 0; tp[n]; n++) ;
|
---|
| 2983 | if (i < n + 3) {
|
---|
| 2984 | res = FR_NOT_ENOUGH_CORE; break;
|
---|
| 2985 | }
|
---|
| 2986 | while (n) buff[--i] = tp[--n];
|
---|
| 2987 | buff[--i] = '/';
|
---|
| 2988 | }
|
---|
| 2989 | tp = buff;
|
---|
| 2990 | if (res == FR_OK) {
|
---|
| 2991 | #if _VOLUMES >= 2
|
---|
| 2992 | *tp++ = '0' + CurrVol; /* Put drive number */
|
---|
| 2993 | *tp++ = ':';
|
---|
| 2994 | #endif
|
---|
| 2995 | if (i == len) { /* Root-directory */
|
---|
| 2996 | *tp++ = '/';
|
---|
| 2997 | } else { /* Sub-directroy */
|
---|
| 2998 | do /* Add stacked path str */
|
---|
| 2999 | *tp++ = buff[i++];
|
---|
| 3000 | while (i < len);
|
---|
| 3001 | }
|
---|
| 3002 | }
|
---|
| 3003 | *tp = 0;
|
---|
| 3004 | FREE_BUF();
|
---|
| 3005 | }
|
---|
| 3006 |
|
---|
| 3007 | LEAVE_FF(dj.fs, res);
|
---|
| 3008 | }
|
---|
| 3009 | #endif /* _FS_RPATH >= 2 */
|
---|
| 3010 | #endif /* _FS_RPATH >= 1 */
|
---|
| 3011 |
|
---|
| 3012 |
|
---|
| 3013 |
|
---|
| 3014 | #if _FS_MINIMIZE <= 2
|
---|
| 3015 | /*-----------------------------------------------------------------------*/
|
---|
| 3016 | /* Seek File R/W Pointer */
|
---|
| 3017 | /*-----------------------------------------------------------------------*/
|
---|
| 3018 |
|
---|
| 3019 | FRESULT f_lseek (
|
---|
| 3020 | FIL* fp, /* Pointer to the file object */
|
---|
| 3021 | DWORD ofs /* File pointer from top of file */
|
---|
| 3022 | )
|
---|
| 3023 | {
|
---|
| 3024 | FRESULT res;
|
---|
| 3025 |
|
---|
| 3026 |
|
---|
| 3027 | res = validate(fp); /* Check validity of the object */
|
---|
| 3028 | if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
---|
| 3029 | if (fp->err) /* Check error */
|
---|
| 3030 | LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
---|
| 3031 |
|
---|
| 3032 | #if _USE_FASTSEEK
|
---|
| 3033 | if (fp->cltbl) { /* Fast seek */
|
---|
| 3034 | DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;
|
---|
| 3035 |
|
---|
| 3036 | if (ofs == CREATE_LINKMAP) { /* Create CLMT */
|
---|
| 3037 | tbl = fp->cltbl;
|
---|
| 3038 | tlen = *tbl++; ulen = 2; /* Given table size and required table size */
|
---|
| 3039 | cl = fp->sclust; /* Top of the chain */
|
---|
| 3040 | if (cl) {
|
---|
| 3041 | do {
|
---|
| 3042 | /* Get a fragment */
|
---|
| 3043 | tcl = cl; ncl = 0; ulen += 2; /* Top, length and used items */
|
---|
| 3044 | do {
|
---|
| 3045 | pcl = cl; ncl++;
|
---|
| 3046 | cl = get_fat(fp->fs, cl);
|
---|
| 3047 | if (cl <= 1) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 3048 | if (cl == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 3049 | } while (cl == pcl + 1);
|
---|
| 3050 | if (ulen <= tlen) { /* Store the length and top of the fragment */
|
---|
| 3051 | *tbl++ = ncl; *tbl++ = tcl;
|
---|
| 3052 | }
|
---|
| 3053 | } while (cl < fp->fs->n_fatent); /* Repeat until end of chain */
|
---|
| 3054 | }
|
---|
| 3055 | *fp->cltbl = ulen; /* Number of items used */
|
---|
| 3056 | if (ulen <= tlen)
|
---|
| 3057 | *tbl = 0; /* Terminate table */
|
---|
| 3058 | else
|
---|
| 3059 | res = FR_NOT_ENOUGH_CORE; /* Given table size is smaller than required */
|
---|
| 3060 |
|
---|
| 3061 | } else { /* Fast seek */
|
---|
| 3062 | if (ofs > fp->fsize) /* Clip offset at the file size */
|
---|
| 3063 | ofs = fp->fsize;
|
---|
| 3064 | fp->fptr = ofs; /* Set file pointer */
|
---|
| 3065 | if (ofs) {
|
---|
| 3066 | fp->clust = clmt_clust(fp, ofs - 1);
|
---|
| 3067 | dsc = clust2sect(fp->fs, fp->clust);
|
---|
| 3068 | if (!dsc) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 3069 | dsc += (ofs - 1) / SS(fp->fs) & (fp->fs->csize - 1);
|
---|
| 3070 | if (fp->fptr % SS(fp->fs) && dsc != fp->dsect) { /* Refill sector cache if needed */
|
---|
| 3071 | #if !_FS_TINY
|
---|
| 3072 | #if !_FS_READONLY
|
---|
| 3073 | if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
|
---|
| 3074 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
| 3075 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 3076 | fp->flag &= ~FA__DIRTY;
|
---|
| 3077 | }
|
---|
| 3078 | #endif
|
---|
| 3079 | if (disk_read(fp->fs->drv, fp->buf, dsc, 1) != RES_OK) /* Load current sector */
|
---|
| 3080 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 3081 | #endif
|
---|
| 3082 | fp->dsect = dsc;
|
---|
| 3083 | }
|
---|
| 3084 | }
|
---|
| 3085 | }
|
---|
| 3086 | } else
|
---|
| 3087 | #endif
|
---|
| 3088 |
|
---|
| 3089 | /* Normal Seek */
|
---|
| 3090 | {
|
---|
| 3091 | DWORD clst, bcs, nsect, ifptr;
|
---|
| 3092 |
|
---|
| 3093 | if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */
|
---|
| 3094 | #if !_FS_READONLY
|
---|
| 3095 | && !(fp->flag & FA_WRITE)
|
---|
| 3096 | #endif
|
---|
| 3097 | ) ofs = fp->fsize;
|
---|
| 3098 |
|
---|
| 3099 | ifptr = fp->fptr;
|
---|
| 3100 | fp->fptr = nsect = 0;
|
---|
| 3101 | if (ofs) {
|
---|
| 3102 | bcs = (DWORD)fp->fs->csize * SS(fp->fs); /* Cluster size (byte) */
|
---|
| 3103 | if (ifptr > 0 &&
|
---|
| 3104 | (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */
|
---|
| 3105 | fp->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */
|
---|
| 3106 | ofs -= fp->fptr;
|
---|
| 3107 | clst = fp->clust;
|
---|
| 3108 | } else { /* When seek to back cluster, */
|
---|
| 3109 | clst = fp->sclust; /* start from the first cluster */
|
---|
| 3110 | #if !_FS_READONLY
|
---|
| 3111 | if (clst == 0) { /* If no cluster chain, create a new chain */
|
---|
| 3112 | clst = create_chain(fp->fs, 0);
|
---|
| 3113 | if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 3114 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 3115 | fp->sclust = clst;
|
---|
| 3116 | }
|
---|
| 3117 | #endif
|
---|
| 3118 | fp->clust = clst;
|
---|
| 3119 | }
|
---|
| 3120 | if (clst != 0) {
|
---|
| 3121 | while (ofs > bcs) { /* Cluster following loop */
|
---|
| 3122 | #if !_FS_READONLY
|
---|
| 3123 | if (fp->flag & FA_WRITE) { /* Check if in write mode or not */
|
---|
| 3124 | clst = create_chain(fp->fs, clst); /* Force stretch if in write mode */
|
---|
| 3125 | if (clst == 0) { /* When disk gets full, clip file size */
|
---|
| 3126 | ofs = bcs; break;
|
---|
| 3127 | }
|
---|
| 3128 | } else
|
---|
| 3129 | #endif
|
---|
| 3130 | clst = get_fat(fp->fs, clst); /* Follow cluster chain if not in write mode */
|
---|
| 3131 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 3132 | if (clst <= 1 || clst >= fp->fs->n_fatent) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 3133 | fp->clust = clst;
|
---|
| 3134 | fp->fptr += bcs;
|
---|
| 3135 | ofs -= bcs;
|
---|
| 3136 | }
|
---|
| 3137 | fp->fptr += ofs;
|
---|
| 3138 | if (ofs % SS(fp->fs)) {
|
---|
| 3139 | nsect = clust2sect(fp->fs, clst); /* Current sector */
|
---|
| 3140 | if (!nsect) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 3141 | nsect += ofs / SS(fp->fs);
|
---|
| 3142 | }
|
---|
| 3143 | }
|
---|
| 3144 | }
|
---|
| 3145 | if (fp->fptr % SS(fp->fs) && nsect != fp->dsect) { /* Fill sector cache if needed */
|
---|
| 3146 | #if !_FS_TINY
|
---|
| 3147 | #if !_FS_READONLY
|
---|
| 3148 | if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
|
---|
| 3149 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
| 3150 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 3151 | fp->flag &= ~FA__DIRTY;
|
---|
| 3152 | }
|
---|
| 3153 | #endif
|
---|
| 3154 | if (disk_read(fp->fs->drv, fp->buf, nsect, 1) != RES_OK) /* Fill sector cache */
|
---|
| 3155 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 3156 | #endif
|
---|
| 3157 | fp->dsect = nsect;
|
---|
| 3158 | }
|
---|
| 3159 | #if !_FS_READONLY
|
---|
| 3160 | if (fp->fptr > fp->fsize) { /* Set file change flag if the file size is extended */
|
---|
| 3161 | fp->fsize = fp->fptr;
|
---|
| 3162 | fp->flag |= FA__WRITTEN;
|
---|
| 3163 | }
|
---|
| 3164 | #endif
|
---|
| 3165 | }
|
---|
| 3166 |
|
---|
| 3167 | LEAVE_FF(fp->fs, res);
|
---|
| 3168 | }
|
---|
| 3169 |
|
---|
| 3170 |
|
---|
| 3171 |
|
---|
| 3172 | #if _FS_MINIMIZE <= 1
|
---|
| 3173 | /*-----------------------------------------------------------------------*/
|
---|
| 3174 | /* Create a Directory Object */
|
---|
| 3175 | /*-----------------------------------------------------------------------*/
|
---|
| 3176 |
|
---|
| 3177 | FRESULT f_opendir (
|
---|
| 3178 | DIR* dp, /* Pointer to directory object to create */
|
---|
| 3179 | const TCHAR* path /* Pointer to the directory path */
|
---|
| 3180 | )
|
---|
| 3181 | {
|
---|
| 3182 | FRESULT res;
|
---|
| 3183 | FATFS* fs;
|
---|
| 3184 | DEF_NAMEBUF;
|
---|
| 3185 |
|
---|
| 3186 |
|
---|
| 3187 | if (!dp) return FR_INVALID_OBJECT;
|
---|
| 3188 |
|
---|
| 3189 | /* Get logical drive number */
|
---|
| 3190 | res = find_volume(&fs, &path, 0);
|
---|
| 3191 | if (res == FR_OK) {
|
---|
| 3192 | dp->fs = fs;
|
---|
| 3193 | INIT_BUF(*dp);
|
---|
| 3194 | res = follow_path(dp, path); /* Follow the path to the directory */
|
---|
| 3195 | FREE_BUF();
|
---|
| 3196 | if (res == FR_OK) { /* Follow completed */
|
---|
| 3197 | if (dp->dir) { /* It is not the origin directory itself */
|
---|
| 3198 | if (dp->dir[DIR_Attr] & AM_DIR) /* The object is a sub directory */
|
---|
| 3199 | dp->sclust = ld_clust(fs, dp->dir);
|
---|
| 3200 | else /* The object is a file */
|
---|
| 3201 | res = FR_NO_PATH;
|
---|
| 3202 | }
|
---|
| 3203 | if (res == FR_OK) {
|
---|
| 3204 | dp->id = fs->id;
|
---|
| 3205 | res = dir_sdi(dp, 0); /* Rewind directory */
|
---|
| 3206 | #if _FS_LOCK
|
---|
| 3207 | if (res == FR_OK) {
|
---|
| 3208 | if (dp->sclust) {
|
---|
| 3209 | dp->lockid = inc_lock(dp, 0); /* Lock the sub directory */
|
---|
| 3210 | if (!dp->lockid)
|
---|
| 3211 | res = FR_TOO_MANY_OPEN_FILES;
|
---|
| 3212 | } else {
|
---|
| 3213 | dp->lockid = 0; /* Root directory need not to be locked */
|
---|
| 3214 | }
|
---|
| 3215 | }
|
---|
| 3216 | #endif
|
---|
| 3217 | }
|
---|
| 3218 | }
|
---|
| 3219 | if (res == FR_NO_FILE) res = FR_NO_PATH;
|
---|
| 3220 | }
|
---|
| 3221 | if (res != FR_OK) dp->fs = 0; /* Invalidate the directory object if function faild */
|
---|
| 3222 |
|
---|
| 3223 | LEAVE_FF(fs, res);
|
---|
| 3224 | }
|
---|
| 3225 |
|
---|
| 3226 |
|
---|
| 3227 |
|
---|
| 3228 |
|
---|
| 3229 | /*-----------------------------------------------------------------------*/
|
---|
| 3230 | /* Close Directory */
|
---|
| 3231 | /*-----------------------------------------------------------------------*/
|
---|
| 3232 |
|
---|
| 3233 | FRESULT f_closedir (
|
---|
| 3234 | DIR *dp /* Pointer to the directory object to be closed */
|
---|
| 3235 | )
|
---|
| 3236 | {
|
---|
| 3237 | FRESULT res;
|
---|
| 3238 |
|
---|
| 3239 |
|
---|
| 3240 | res = validate(dp);
|
---|
| 3241 | if (res == FR_OK) {
|
---|
| 3242 | #if _FS_REENTRANT
|
---|
| 3243 | FATFS *fs = dp->fs;
|
---|
| 3244 | #endif
|
---|
| 3245 | #if _FS_LOCK
|
---|
| 3246 | if (dp->lockid) /* Decrement sub-directory open counter */
|
---|
| 3247 | res = dec_lock(dp->lockid);
|
---|
| 3248 | if (res == FR_OK)
|
---|
| 3249 | #endif
|
---|
| 3250 | dp->fs = 0; /* Invalidate directory object */
|
---|
| 3251 | #if _FS_REENTRANT
|
---|
| 3252 | unlock_fs(fs, FR_OK); /* Unlock volume */
|
---|
| 3253 | #endif
|
---|
| 3254 | }
|
---|
| 3255 | return res;
|
---|
| 3256 | }
|
---|
| 3257 |
|
---|
| 3258 |
|
---|
| 3259 |
|
---|
| 3260 |
|
---|
| 3261 | /*-----------------------------------------------------------------------*/
|
---|
| 3262 | /* Read Directory Entries in Sequence */
|
---|
| 3263 | /*-----------------------------------------------------------------------*/
|
---|
| 3264 |
|
---|
| 3265 | FRESULT f_readdir (
|
---|
| 3266 | DIR* dp, /* Pointer to the open directory object */
|
---|
| 3267 | FILINFO* fno /* Pointer to file information to return */
|
---|
| 3268 | )
|
---|
| 3269 | {
|
---|
| 3270 | FRESULT res;
|
---|
| 3271 | DEF_NAMEBUF;
|
---|
| 3272 |
|
---|
| 3273 |
|
---|
| 3274 | res = validate(dp); /* Check validity of the object */
|
---|
| 3275 | if (res == FR_OK) {
|
---|
| 3276 | if (!fno) {
|
---|
| 3277 | res = dir_sdi(dp, 0); /* Rewind the directory object */
|
---|
| 3278 | } else {
|
---|
| 3279 | INIT_BUF(*dp);
|
---|
| 3280 | res = dir_read(dp, 0); /* Read an item */
|
---|
| 3281 | if (res == FR_NO_FILE) { /* Reached end of directory */
|
---|
| 3282 | dp->sect = 0;
|
---|
| 3283 | res = FR_OK;
|
---|
| 3284 | }
|
---|
| 3285 | if (res == FR_OK) { /* A valid entry is found */
|
---|
| 3286 | get_fileinfo(dp, fno); /* Get the object information */
|
---|
| 3287 | res = dir_next(dp, 0); /* Increment index for next */
|
---|
| 3288 | if (res == FR_NO_FILE) {
|
---|
| 3289 | dp->sect = 0;
|
---|
| 3290 | res = FR_OK;
|
---|
| 3291 | }
|
---|
| 3292 | }
|
---|
| 3293 | FREE_BUF();
|
---|
| 3294 | }
|
---|
| 3295 | }
|
---|
| 3296 |
|
---|
| 3297 | LEAVE_FF(dp->fs, res);
|
---|
| 3298 | }
|
---|
| 3299 |
|
---|
| 3300 |
|
---|
| 3301 |
|
---|
| 3302 | #if _FS_MINIMIZE == 0
|
---|
| 3303 | /*-----------------------------------------------------------------------*/
|
---|
| 3304 | /* Get File Status */
|
---|
| 3305 | /*-----------------------------------------------------------------------*/
|
---|
| 3306 |
|
---|
| 3307 | FRESULT f_stat (
|
---|
| 3308 | const TCHAR* path, /* Pointer to the file path */
|
---|
| 3309 | FILINFO* fno /* Pointer to file information to return */
|
---|
| 3310 | )
|
---|
| 3311 | {
|
---|
| 3312 | FRESULT res;
|
---|
| 3313 | DIR dj;
|
---|
| 3314 | DEF_NAMEBUF;
|
---|
| 3315 |
|
---|
| 3316 |
|
---|
| 3317 | /* Get logical drive number */
|
---|
| 3318 | res = find_volume(&dj.fs, &path, 0);
|
---|
| 3319 | if (res == FR_OK) {
|
---|
| 3320 | INIT_BUF(dj);
|
---|
| 3321 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
| 3322 | if (res == FR_OK) { /* Follow completed */
|
---|
| 3323 | if (dj.dir) { /* Found an object */
|
---|
| 3324 | if (fno) get_fileinfo(&dj, fno);
|
---|
| 3325 | } else { /* It is root directory */
|
---|
| 3326 | res = FR_INVALID_NAME;
|
---|
| 3327 | }
|
---|
| 3328 | }
|
---|
| 3329 | FREE_BUF();
|
---|
| 3330 | }
|
---|
| 3331 |
|
---|
| 3332 | LEAVE_FF(dj.fs, res);
|
---|
| 3333 | }
|
---|
| 3334 |
|
---|
| 3335 |
|
---|
| 3336 |
|
---|
| 3337 | #if !_FS_READONLY
|
---|
| 3338 | /*-----------------------------------------------------------------------*/
|
---|
| 3339 | /* Get Number of Free Clusters */
|
---|
| 3340 | /*-----------------------------------------------------------------------*/
|
---|
| 3341 |
|
---|
| 3342 | FRESULT f_getfree (
|
---|
| 3343 | const TCHAR* path, /* Path name of the logical drive number */
|
---|
| 3344 | DWORD* nclst, /* Pointer to a variable to return number of free clusters */
|
---|
| 3345 | FATFS** fatfs /* Pointer to return pointer to corresponding file system object */
|
---|
| 3346 | )
|
---|
| 3347 | {
|
---|
| 3348 | FRESULT res;
|
---|
| 3349 | FATFS *fs;
|
---|
| 3350 | DWORD n, clst, sect, stat;
|
---|
| 3351 | UINT i;
|
---|
| 3352 | BYTE fat, *p;
|
---|
| 3353 |
|
---|
| 3354 |
|
---|
| 3355 | /* Get logical drive number */
|
---|
| 3356 | res = find_volume(fatfs, &path, 0);
|
---|
| 3357 | fs = *fatfs;
|
---|
| 3358 | if (res == FR_OK) {
|
---|
| 3359 | /* If free_clust is valid, return it without full cluster scan */
|
---|
| 3360 | if (fs->free_clust <= fs->n_fatent - 2) {
|
---|
| 3361 | *nclst = fs->free_clust;
|
---|
| 3362 | } else {
|
---|
| 3363 | /* Get number of free clusters */
|
---|
| 3364 | fat = fs->fs_type;
|
---|
| 3365 | n = 0;
|
---|
| 3366 | if (fat == FS_FAT12) {
|
---|
| 3367 | clst = 2;
|
---|
| 3368 | do {
|
---|
| 3369 | stat = get_fat(fs, clst);
|
---|
| 3370 | if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
|
---|
| 3371 | if (stat == 1) { res = FR_INT_ERR; break; }
|
---|
| 3372 | if (stat == 0) n++;
|
---|
| 3373 | } while (++clst < fs->n_fatent);
|
---|
| 3374 | } else {
|
---|
| 3375 | clst = fs->n_fatent;
|
---|
| 3376 | sect = fs->fatbase;
|
---|
| 3377 | i = 0; p = 0;
|
---|
| 3378 | do {
|
---|
| 3379 | if (!i) {
|
---|
| 3380 | res = move_window(fs, sect++);
|
---|
| 3381 | if (res != FR_OK) break;
|
---|
| 3382 | p = fs->win;
|
---|
| 3383 | i = SS(fs);
|
---|
| 3384 | }
|
---|
| 3385 | if (fat == FS_FAT16) {
|
---|
| 3386 | if (LD_WORD(p) == 0) n++;
|
---|
| 3387 | p += 2; i -= 2;
|
---|
| 3388 | } else {
|
---|
| 3389 | if ((LD_DWORD(p) & 0x0FFFFFFF) == 0) n++;
|
---|
| 3390 | p += 4; i -= 4;
|
---|
| 3391 | }
|
---|
| 3392 | } while (--clst);
|
---|
| 3393 | }
|
---|
| 3394 | fs->free_clust = n;
|
---|
| 3395 | fs->fsi_flag |= 1;
|
---|
| 3396 | *nclst = n;
|
---|
| 3397 | }
|
---|
| 3398 | }
|
---|
| 3399 | LEAVE_FF(fs, res);
|
---|
| 3400 | }
|
---|
| 3401 |
|
---|
| 3402 |
|
---|
| 3403 |
|
---|
| 3404 |
|
---|
| 3405 | /*-----------------------------------------------------------------------*/
|
---|
| 3406 | /* Truncate File */
|
---|
| 3407 | /*-----------------------------------------------------------------------*/
|
---|
| 3408 |
|
---|
| 3409 | FRESULT f_truncate (
|
---|
| 3410 | FIL* fp /* Pointer to the file object */
|
---|
| 3411 | )
|
---|
| 3412 | {
|
---|
| 3413 | FRESULT res;
|
---|
| 3414 | DWORD ncl;
|
---|
| 3415 |
|
---|
| 3416 |
|
---|
| 3417 | res = validate(fp); /* Check validity of the object */
|
---|
| 3418 | if (res == FR_OK) {
|
---|
| 3419 | if (fp->err) { /* Check error */
|
---|
| 3420 | res = (FRESULT)fp->err;
|
---|
| 3421 | } else {
|
---|
| 3422 | if (!(fp->flag & FA_WRITE)) /* Check access mode */
|
---|
| 3423 | res = FR_DENIED;
|
---|
| 3424 | }
|
---|
| 3425 | }
|
---|
| 3426 | if (res == FR_OK) {
|
---|
| 3427 | if (fp->fsize > fp->fptr) {
|
---|
| 3428 | fp->fsize = fp->fptr; /* Set file size to current R/W point */
|
---|
| 3429 | fp->flag |= FA__WRITTEN;
|
---|
| 3430 | if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */
|
---|
| 3431 | res = remove_chain(fp->fs, fp->sclust);
|
---|
| 3432 | fp->sclust = 0;
|
---|
| 3433 | } else { /* When truncate a part of the file, remove remaining clusters */
|
---|
| 3434 | ncl = get_fat(fp->fs, fp->clust);
|
---|
| 3435 | res = FR_OK;
|
---|
| 3436 | if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;
|
---|
| 3437 | if (ncl == 1) res = FR_INT_ERR;
|
---|
| 3438 | if (res == FR_OK && ncl < fp->fs->n_fatent) {
|
---|
| 3439 | res = put_fat(fp->fs, fp->clust, 0x0FFFFFFF);
|
---|
| 3440 | if (res == FR_OK) res = remove_chain(fp->fs, ncl);
|
---|
| 3441 | }
|
---|
| 3442 | }
|
---|
| 3443 | #if !_FS_TINY
|
---|
| 3444 | if (res == FR_OK && (fp->flag & FA__DIRTY)) {
|
---|
| 3445 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
| 3446 | res = FR_DISK_ERR;
|
---|
| 3447 | else
|
---|
| 3448 | fp->flag &= ~FA__DIRTY;
|
---|
| 3449 | }
|
---|
| 3450 | #endif
|
---|
| 3451 | }
|
---|
| 3452 | if (res != FR_OK) fp->err = (FRESULT)res;
|
---|
| 3453 | }
|
---|
| 3454 |
|
---|
| 3455 | LEAVE_FF(fp->fs, res);
|
---|
| 3456 | }
|
---|
| 3457 |
|
---|
| 3458 |
|
---|
| 3459 |
|
---|
| 3460 |
|
---|
| 3461 | /*-----------------------------------------------------------------------*/
|
---|
| 3462 | /* Delete a File or Directory */
|
---|
| 3463 | /*-----------------------------------------------------------------------*/
|
---|
| 3464 |
|
---|
| 3465 | FRESULT f_unlink (
|
---|
| 3466 | const TCHAR* path /* Pointer to the file or directory path */
|
---|
| 3467 | )
|
---|
| 3468 | {
|
---|
| 3469 | FRESULT res;
|
---|
| 3470 | DIR dj, sdj;
|
---|
| 3471 | BYTE *dir;
|
---|
| 3472 | DWORD dclst = 0;
|
---|
| 3473 | DEF_NAMEBUF;
|
---|
| 3474 |
|
---|
| 3475 |
|
---|
| 3476 | /* Get logical drive number */
|
---|
| 3477 | res = find_volume(&dj.fs, &path, 1);
|
---|
| 3478 | if (res == FR_OK) {
|
---|
| 3479 | INIT_BUF(dj);
|
---|
| 3480 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
| 3481 | if (_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT))
|
---|
| 3482 | res = FR_INVALID_NAME; /* Cannot remove dot entry */
|
---|
| 3483 | #if _FS_LOCK
|
---|
| 3484 | if (res == FR_OK) res = chk_lock(&dj, 2); /* Cannot remove open object */
|
---|
| 3485 | #endif
|
---|
| 3486 | if (res == FR_OK) { /* The object is accessible */
|
---|
| 3487 | dir = dj.dir;
|
---|
| 3488 | if (!dir) {
|
---|
| 3489 | res = FR_INVALID_NAME; /* Cannot remove the origin directory */
|
---|
| 3490 | } else {
|
---|
| 3491 | if (dir[DIR_Attr] & AM_RDO)
|
---|
| 3492 | res = FR_DENIED; /* Cannot remove R/O object */
|
---|
| 3493 | }
|
---|
| 3494 | if (res == FR_OK) {
|
---|
| 3495 | dclst = ld_clust(dj.fs, dir);
|
---|
| 3496 | if (dir[DIR_Attr] & AM_DIR) { /* Is it a sub-dir? */
|
---|
| 3497 | if (!dclst) {
|
---|
| 3498 | res = FR_INT_ERR;
|
---|
| 3499 | } else { /* Make sure the sub-directory is empty */
|
---|
| 3500 | mem_cpy(&sdj, &dj, sizeof (DIR));
|
---|
| 3501 | sdj.sclust = dclst;
|
---|
| 3502 | res = dir_sdi(&sdj, 2); /* Exclude dot entries */
|
---|
| 3503 | if (res == FR_OK) {
|
---|
| 3504 | res = dir_read(&sdj, 0); /* Read an item */
|
---|
| 3505 | if (res == FR_OK /* Not empty directory */
|
---|
| 3506 | #if _FS_RPATH
|
---|
| 3507 | || dclst == dj.fs->cdir /* or current directory */
|
---|
| 3508 | #endif
|
---|
| 3509 | ) res = FR_DENIED;
|
---|
| 3510 | if (res == FR_NO_FILE) res = FR_OK; /* It is empty */
|
---|
| 3511 | }
|
---|
| 3512 | }
|
---|
| 3513 | }
|
---|
| 3514 | }
|
---|
| 3515 | if (res == FR_OK) {
|
---|
| 3516 | res = dir_remove(&dj); /* Remove the directory entry */
|
---|
| 3517 | if (res == FR_OK && dclst) /* Remove the cluster chain if exist */
|
---|
| 3518 | res = remove_chain(dj.fs, dclst);
|
---|
| 3519 | if (res == FR_OK) res = sync_fs(dj.fs);
|
---|
| 3520 | }
|
---|
| 3521 | }
|
---|
| 3522 | FREE_BUF();
|
---|
| 3523 | }
|
---|
| 3524 |
|
---|
| 3525 | LEAVE_FF(dj.fs, res);
|
---|
| 3526 | }
|
---|
| 3527 |
|
---|
| 3528 |
|
---|
| 3529 |
|
---|
| 3530 |
|
---|
| 3531 | /*-----------------------------------------------------------------------*/
|
---|
| 3532 | /* Create a Directory */
|
---|
| 3533 | /*-----------------------------------------------------------------------*/
|
---|
| 3534 |
|
---|
| 3535 | FRESULT f_mkdir (
|
---|
| 3536 | const TCHAR* path /* Pointer to the directory path */
|
---|
| 3537 | )
|
---|
| 3538 | {
|
---|
| 3539 | FRESULT res;
|
---|
| 3540 | DIR dj;
|
---|
| 3541 | BYTE *dir, n;
|
---|
| 3542 | DWORD dsc, dcl, pcl, tm = GET_FATTIME();
|
---|
| 3543 | DEF_NAMEBUF;
|
---|
| 3544 |
|
---|
| 3545 |
|
---|
| 3546 | /* Get logical drive number */
|
---|
| 3547 | res = find_volume(&dj.fs, &path, 1);
|
---|
| 3548 | if (res == FR_OK) {
|
---|
| 3549 | INIT_BUF(dj);
|
---|
| 3550 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
| 3551 | if (res == FR_OK) res = FR_EXIST; /* Any object with same name is already existing */
|
---|
| 3552 | if (_FS_RPATH && res == FR_NO_FILE && (dj.fn[NSFLAG] & NS_DOT))
|
---|
| 3553 | res = FR_INVALID_NAME;
|
---|
| 3554 | if (res == FR_NO_FILE) { /* Can create a new directory */
|
---|
| 3555 | dcl = create_chain(dj.fs, 0); /* Allocate a cluster for the new directory table */
|
---|
| 3556 | res = FR_OK;
|
---|
| 3557 | if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster */
|
---|
| 3558 | if (dcl == 1) res = FR_INT_ERR;
|
---|
| 3559 | if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
|
---|
| 3560 | if (res == FR_OK) /* Flush FAT */
|
---|
| 3561 | res = sync_window(dj.fs);
|
---|
| 3562 | if (res == FR_OK) { /* Initialize the new directory table */
|
---|
| 3563 | dsc = clust2sect(dj.fs, dcl);
|
---|
| 3564 | dir = dj.fs->win;
|
---|
| 3565 | mem_set(dir, 0, SS(dj.fs));
|
---|
| 3566 | mem_set(dir+DIR_Name, ' ', 11); /* Create "." entry */
|
---|
| 3567 | dir[DIR_Name] = '.';
|
---|
| 3568 | dir[DIR_Attr] = AM_DIR;
|
---|
| 3569 | ST_DWORD(dir+DIR_WrtTime, tm);
|
---|
| 3570 | st_clust(dir, dcl);
|
---|
| 3571 | mem_cpy(dir+SZ_DIR, dir, SZ_DIR); /* Create ".." entry */
|
---|
| 3572 | dir[SZ_DIR+1] = '.'; pcl = dj.sclust;
|
---|
| 3573 | if (dj.fs->fs_type == FS_FAT32 && pcl == dj.fs->dirbase)
|
---|
| 3574 | pcl = 0;
|
---|
| 3575 | st_clust(dir+SZ_DIR, pcl);
|
---|
| 3576 | for (n = dj.fs->csize; n; n--) { /* Write dot entries and clear following sectors */
|
---|
| 3577 | dj.fs->winsect = dsc++;
|
---|
| 3578 | dj.fs->wflag = 1;
|
---|
| 3579 | res = sync_window(dj.fs);
|
---|
| 3580 | if (res != FR_OK) break;
|
---|
| 3581 | mem_set(dir, 0, SS(dj.fs));
|
---|
| 3582 | }
|
---|
| 3583 | }
|
---|
| 3584 | if (res == FR_OK) res = dir_register(&dj); /* Register the object to the directoy */
|
---|
| 3585 | if (res != FR_OK) {
|
---|
| 3586 | remove_chain(dj.fs, dcl); /* Could not register, remove cluster chain */
|
---|
| 3587 | } else {
|
---|
| 3588 | dir = dj.dir;
|
---|
| 3589 | dir[DIR_Attr] = AM_DIR; /* Attribute */
|
---|
| 3590 | ST_DWORD(dir+DIR_WrtTime, tm); /* Created time */
|
---|
| 3591 | st_clust(dir, dcl); /* Table start cluster */
|
---|
| 3592 | dj.fs->wflag = 1;
|
---|
| 3593 | res = sync_fs(dj.fs);
|
---|
| 3594 | }
|
---|
| 3595 | }
|
---|
| 3596 | FREE_BUF();
|
---|
| 3597 | }
|
---|
| 3598 |
|
---|
| 3599 | LEAVE_FF(dj.fs, res);
|
---|
| 3600 | }
|
---|
| 3601 |
|
---|
| 3602 |
|
---|
| 3603 |
|
---|
| 3604 |
|
---|
| 3605 | /*-----------------------------------------------------------------------*/
|
---|
| 3606 | /* Change Attribute */
|
---|
| 3607 | /*-----------------------------------------------------------------------*/
|
---|
| 3608 |
|
---|
| 3609 | FRESULT f_chmod (
|
---|
| 3610 | const TCHAR* path, /* Pointer to the file path */
|
---|
| 3611 | BYTE value, /* Attribute bits */
|
---|
| 3612 | BYTE mask /* Attribute mask to change */
|
---|
| 3613 | )
|
---|
| 3614 | {
|
---|
| 3615 | FRESULT res;
|
---|
| 3616 | DIR dj;
|
---|
| 3617 | BYTE *dir;
|
---|
| 3618 | DEF_NAMEBUF;
|
---|
| 3619 |
|
---|
| 3620 |
|
---|
| 3621 | /* Get logical drive number */
|
---|
| 3622 | res = find_volume(&dj.fs, &path, 1);
|
---|
| 3623 | if (res == FR_OK) {
|
---|
| 3624 | INIT_BUF(dj);
|
---|
| 3625 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
| 3626 | FREE_BUF();
|
---|
| 3627 | if (_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT))
|
---|
| 3628 | res = FR_INVALID_NAME;
|
---|
| 3629 | if (res == FR_OK) {
|
---|
| 3630 | dir = dj.dir;
|
---|
| 3631 | if (!dir) { /* Is it a root directory? */
|
---|
| 3632 | res = FR_INVALID_NAME;
|
---|
| 3633 | } else { /* File or sub directory */
|
---|
| 3634 | mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */
|
---|
| 3635 | dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */
|
---|
| 3636 | dj.fs->wflag = 1;
|
---|
| 3637 | res = sync_fs(dj.fs);
|
---|
| 3638 | }
|
---|
| 3639 | }
|
---|
| 3640 | }
|
---|
| 3641 |
|
---|
| 3642 | LEAVE_FF(dj.fs, res);
|
---|
| 3643 | }
|
---|
| 3644 |
|
---|
| 3645 |
|
---|
| 3646 |
|
---|
| 3647 |
|
---|
| 3648 | /*-----------------------------------------------------------------------*/
|
---|
| 3649 | /* Rename File/Directory */
|
---|
| 3650 | /*-----------------------------------------------------------------------*/
|
---|
| 3651 |
|
---|
| 3652 | FRESULT f_rename (
|
---|
| 3653 | const TCHAR* path_old, /* Pointer to the object to be renamed */
|
---|
| 3654 | const TCHAR* path_new /* Pointer to the new name */
|
---|
| 3655 | )
|
---|
| 3656 | {
|
---|
| 3657 | FRESULT res;
|
---|
| 3658 | DIR djo, djn;
|
---|
| 3659 | BYTE buf[21], *dir;
|
---|
| 3660 | DWORD dw;
|
---|
| 3661 | DEF_NAMEBUF;
|
---|
| 3662 |
|
---|
| 3663 |
|
---|
| 3664 | /* Get logical drive number of the source object */
|
---|
| 3665 | res = find_volume(&djo.fs, &path_old, 1);
|
---|
| 3666 | if (res == FR_OK) {
|
---|
| 3667 | djn.fs = djo.fs;
|
---|
| 3668 | INIT_BUF(djo);
|
---|
| 3669 | res = follow_path(&djo, path_old); /* Check old object */
|
---|
| 3670 | if (_FS_RPATH && res == FR_OK && (djo.fn[NSFLAG] & NS_DOT))
|
---|
| 3671 | res = FR_INVALID_NAME;
|
---|
| 3672 | #if _FS_LOCK
|
---|
| 3673 | if (res == FR_OK) res = chk_lock(&djo, 2);
|
---|
| 3674 | #endif
|
---|
| 3675 | if (res == FR_OK) { /* Old object is found */
|
---|
| 3676 | if (!djo.dir) { /* Is root dir? */
|
---|
| 3677 | res = FR_NO_FILE;
|
---|
| 3678 | } else {
|
---|
| 3679 | mem_cpy(buf, djo.dir+DIR_Attr, 21); /* Save the object information except name */
|
---|
| 3680 | mem_cpy(&djn, &djo, sizeof (DIR)); /* Duplicate the directory object */
|
---|
| 3681 | if (get_ldnumber(&path_new) >= 0) /* Snip drive number off and ignore it */
|
---|
| 3682 | res = follow_path(&djn, path_new); /* and make sure if new object name is not conflicting */
|
---|
| 3683 | else
|
---|
| 3684 | res = FR_INVALID_DRIVE;
|
---|
| 3685 | if (res == FR_OK) res = FR_EXIST; /* The new object name is already existing */
|
---|
| 3686 | if (res == FR_NO_FILE) { /* It is a valid path and no name collision */
|
---|
| 3687 | /* Start of critical section that any interruption can cause a cross-link */
|
---|
| 3688 | res = dir_register(&djn); /* Register the new entry */
|
---|
| 3689 | if (res == FR_OK) {
|
---|
| 3690 | dir = djn.dir; /* Copy object information except name */
|
---|
| 3691 | mem_cpy(dir+13, buf+2, 19);
|
---|
| 3692 | dir[DIR_Attr] = buf[0] | AM_ARC;
|
---|
| 3693 | djo.fs->wflag = 1;
|
---|
| 3694 | if ((dir[DIR_Attr] & AM_DIR) && djo.sclust != djn.sclust) { /* Update .. entry in the directory if needed */
|
---|
| 3695 | dw = clust2sect(djo.fs, ld_clust(djo.fs, dir));
|
---|
| 3696 | if (!dw) {
|
---|
| 3697 | res = FR_INT_ERR;
|
---|
| 3698 | } else {
|
---|
| 3699 | res = move_window(djo.fs, dw);
|
---|
| 3700 | dir = djo.fs->win+SZ_DIR; /* .. entry */
|
---|
| 3701 | if (res == FR_OK && dir[1] == '.') {
|
---|
| 3702 | st_clust(dir, djn.sclust);
|
---|
| 3703 | djo.fs->wflag = 1;
|
---|
| 3704 | }
|
---|
| 3705 | }
|
---|
| 3706 | }
|
---|
| 3707 | if (res == FR_OK) {
|
---|
| 3708 | res = dir_remove(&djo); /* Remove old entry */
|
---|
| 3709 | if (res == FR_OK)
|
---|
| 3710 | res = sync_fs(djo.fs);
|
---|
| 3711 | }
|
---|
| 3712 | }
|
---|
| 3713 | /* End of critical section */
|
---|
| 3714 | }
|
---|
| 3715 | }
|
---|
| 3716 | }
|
---|
| 3717 | FREE_BUF();
|
---|
| 3718 | }
|
---|
| 3719 |
|
---|
| 3720 | LEAVE_FF(djo.fs, res);
|
---|
| 3721 | }
|
---|
| 3722 |
|
---|
| 3723 |
|
---|
| 3724 |
|
---|
| 3725 |
|
---|
| 3726 | /*-----------------------------------------------------------------------*/
|
---|
| 3727 | /* Change Timestamp */
|
---|
| 3728 | /*-----------------------------------------------------------------------*/
|
---|
| 3729 |
|
---|
| 3730 | FRESULT f_utime (
|
---|
| 3731 | const TCHAR* path, /* Pointer to the file/directory name */
|
---|
| 3732 | const FILINFO* fno /* Pointer to the time stamp to be set */
|
---|
| 3733 | )
|
---|
| 3734 | {
|
---|
| 3735 | FRESULT res;
|
---|
| 3736 | DIR dj;
|
---|
| 3737 | BYTE *dir;
|
---|
| 3738 | DEF_NAMEBUF;
|
---|
| 3739 |
|
---|
| 3740 |
|
---|
| 3741 | /* Get logical drive number */
|
---|
| 3742 | res = find_volume(&dj.fs, &path, 1);
|
---|
| 3743 | if (res == FR_OK) {
|
---|
| 3744 | INIT_BUF(dj);
|
---|
| 3745 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
| 3746 | FREE_BUF();
|
---|
| 3747 | if (_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT))
|
---|
| 3748 | res = FR_INVALID_NAME;
|
---|
| 3749 | if (res == FR_OK) {
|
---|
| 3750 | dir = dj.dir;
|
---|
| 3751 | if (!dir) { /* Root directory */
|
---|
| 3752 | res = FR_INVALID_NAME;
|
---|
| 3753 | } else { /* File or sub-directory */
|
---|
| 3754 | ST_WORD(dir+DIR_WrtTime, fno->ftime);
|
---|
| 3755 | ST_WORD(dir+DIR_WrtDate, fno->fdate);
|
---|
| 3756 | dj.fs->wflag = 1;
|
---|
| 3757 | res = sync_fs(dj.fs);
|
---|
| 3758 | }
|
---|
| 3759 | }
|
---|
| 3760 | }
|
---|
| 3761 |
|
---|
| 3762 | LEAVE_FF(dj.fs, res);
|
---|
| 3763 | }
|
---|
| 3764 |
|
---|
| 3765 | #endif /* !_FS_READONLY */
|
---|
| 3766 | #endif /* _FS_MINIMIZE == 0 */
|
---|
| 3767 | #endif /* _FS_MINIMIZE <= 1 */
|
---|
| 3768 | #endif /* _FS_MINIMIZE <= 2 */
|
---|
| 3769 |
|
---|
| 3770 |
|
---|
| 3771 |
|
---|
| 3772 | #if _USE_LABEL
|
---|
| 3773 | /*-----------------------------------------------------------------------*/
|
---|
| 3774 | /* Get volume label */
|
---|
| 3775 | /*-----------------------------------------------------------------------*/
|
---|
| 3776 |
|
---|
| 3777 | FRESULT f_getlabel (
|
---|
| 3778 | const TCHAR* path, /* Path name of the logical drive number */
|
---|
| 3779 | TCHAR* label, /* Pointer to a buffer to return the volume label */
|
---|
| 3780 | DWORD* vsn /* Pointer to a variable to return the volume serial number */
|
---|
| 3781 | )
|
---|
| 3782 | {
|
---|
| 3783 | FRESULT res;
|
---|
| 3784 | DIR dj;
|
---|
| 3785 | UINT i, j;
|
---|
| 3786 |
|
---|
| 3787 |
|
---|
| 3788 | /* Get logical drive number */
|
---|
| 3789 | res = find_volume(&dj.fs, &path, 0);
|
---|
| 3790 |
|
---|
| 3791 | /* Get volume label */
|
---|
| 3792 | if (res == FR_OK && label) {
|
---|
| 3793 | dj.sclust = 0; /* Open root directory */
|
---|
| 3794 | res = dir_sdi(&dj, 0);
|
---|
| 3795 | if (res == FR_OK) {
|
---|
| 3796 | res = dir_read(&dj, 1); /* Get an entry with AM_VOL */
|
---|
| 3797 | if (res == FR_OK) { /* A volume label is exist */
|
---|
| 3798 | #if _USE_LFN && _LFN_UNICODE
|
---|
| 3799 | WCHAR w;
|
---|
| 3800 | i = j = 0;
|
---|
| 3801 | do {
|
---|
| 3802 | w = (i < 11) ? dj.dir[i++] : ' ';
|
---|
| 3803 | if (IsDBCS1(w) && i < 11 && IsDBCS2(dj.dir[i]))
|
---|
| 3804 | w = w << 8 | dj.dir[i++];
|
---|
| 3805 | label[j++] = ff_convert(w, 1); /* OEM -> Unicode */
|
---|
| 3806 | } while (j < 11);
|
---|
| 3807 | #else
|
---|
| 3808 | mem_cpy(label, dj.dir, 11);
|
---|
| 3809 | #endif
|
---|
| 3810 | j = 11;
|
---|
| 3811 | do {
|
---|
| 3812 | label[j] = 0;
|
---|
| 3813 | if (!j) break;
|
---|
| 3814 | } while (label[--j] == ' ');
|
---|
| 3815 | }
|
---|
| 3816 | if (res == FR_NO_FILE) { /* No label, return nul string */
|
---|
| 3817 | label[0] = 0;
|
---|
| 3818 | res = FR_OK;
|
---|
| 3819 | }
|
---|
| 3820 | }
|
---|
| 3821 | }
|
---|
| 3822 |
|
---|
| 3823 | /* Get volume serial number */
|
---|
| 3824 | if (res == FR_OK && vsn) {
|
---|
| 3825 | res = move_window(dj.fs, dj.fs->volbase);
|
---|
| 3826 | if (res == FR_OK) {
|
---|
| 3827 | i = dj.fs->fs_type == FS_FAT32 ? BS_VolID32 : BS_VolID;
|
---|
| 3828 | *vsn = LD_DWORD(&dj.fs->win[i]);
|
---|
| 3829 | }
|
---|
| 3830 | }
|
---|
| 3831 |
|
---|
| 3832 | LEAVE_FF(dj.fs, res);
|
---|
| 3833 | }
|
---|
| 3834 |
|
---|
| 3835 |
|
---|
| 3836 |
|
---|
| 3837 | #if !_FS_READONLY
|
---|
| 3838 | /*-----------------------------------------------------------------------*/
|
---|
| 3839 | /* Set volume label */
|
---|
| 3840 | /*-----------------------------------------------------------------------*/
|
---|
| 3841 |
|
---|
| 3842 | FRESULT f_setlabel (
|
---|
| 3843 | const TCHAR* label /* Pointer to the volume label to set */
|
---|
| 3844 | )
|
---|
| 3845 | {
|
---|
| 3846 | FRESULT res;
|
---|
| 3847 | DIR dj;
|
---|
| 3848 | BYTE vn[11];
|
---|
| 3849 | UINT i, j, sl;
|
---|
| 3850 | WCHAR w;
|
---|
| 3851 | DWORD tm;
|
---|
| 3852 |
|
---|
| 3853 |
|
---|
| 3854 | /* Get logical drive number */
|
---|
| 3855 | res = find_volume(&dj.fs, &label, 1);
|
---|
| 3856 | if (res) LEAVE_FF(dj.fs, res);
|
---|
| 3857 |
|
---|
| 3858 | /* Create a volume label in directory form */
|
---|
| 3859 | vn[0] = 0;
|
---|
| 3860 | for (sl = 0; label[sl]; sl++) ; /* Get name length */
|
---|
| 3861 | for ( ; sl && label[sl-1] == ' '; sl--) ; /* Remove trailing spaces */
|
---|
| 3862 | if (sl) { /* Create volume label in directory form */
|
---|
| 3863 | i = j = 0;
|
---|
| 3864 | do {
|
---|
| 3865 | #if _USE_LFN && _LFN_UNICODE
|
---|
| 3866 | w = ff_convert(ff_wtoupper(label[i++]), 0);
|
---|
| 3867 | #else
|
---|
| 3868 | w = (BYTE)label[i++];
|
---|
| 3869 | if (IsDBCS1(w))
|
---|
| 3870 | w = (j < 10 && i < sl && IsDBCS2(label[i])) ? w << 8 | (BYTE)label[i++] : 0;
|
---|
| 3871 | #if _USE_LFN
|
---|
| 3872 | w = ff_convert(ff_wtoupper(ff_convert(w, 1)), 0);
|
---|
| 3873 | #else
|
---|
| 3874 | if (IsLower(w)) w -= 0x20; /* To upper ASCII characters */
|
---|
| 3875 | #ifdef _EXCVT
|
---|
| 3876 | if (w >= 0x80) w = ExCvt[w - 0x80]; /* To upper extended characters (SBCS cfg) */
|
---|
| 3877 | #else
|
---|
| 3878 | if (!_DF1S && w >= 0x80) w = 0; /* Reject extended characters (ASCII cfg) */
|
---|
| 3879 | #endif
|
---|
| 3880 | #endif
|
---|
| 3881 | #endif
|
---|
| 3882 | if (!w || chk_chr("\"*+,.:;<=>\?[]|\x7F", w) || j >= (UINT)((w >= 0x100) ? 10 : 11)) /* Reject invalid characters for volume label */
|
---|
| 3883 | LEAVE_FF(dj.fs, FR_INVALID_NAME);
|
---|
| 3884 | if (w >= 0x100) vn[j++] = (BYTE)(w >> 8);
|
---|
| 3885 | vn[j++] = (BYTE)w;
|
---|
| 3886 | } while (i < sl);
|
---|
| 3887 | while (j < 11) vn[j++] = ' ';
|
---|
| 3888 | }
|
---|
| 3889 |
|
---|
| 3890 | /* Set volume label */
|
---|
| 3891 | dj.sclust = 0; /* Open root directory */
|
---|
| 3892 | res = dir_sdi(&dj, 0);
|
---|
| 3893 | if (res == FR_OK) {
|
---|
| 3894 | res = dir_read(&dj, 1); /* Get an entry with AM_VOL */
|
---|
| 3895 | if (res == FR_OK) { /* A volume label is found */
|
---|
| 3896 | if (vn[0]) {
|
---|
| 3897 | mem_cpy(dj.dir, vn, 11); /* Change the volume label name */
|
---|
| 3898 | tm = GET_FATTIME();
|
---|
| 3899 | ST_DWORD(dj.dir+DIR_WrtTime, tm);
|
---|
| 3900 | } else {
|
---|
| 3901 | dj.dir[0] = DDE; /* Remove the volume label */
|
---|
| 3902 | }
|
---|
| 3903 | dj.fs->wflag = 1;
|
---|
| 3904 | res = sync_fs(dj.fs);
|
---|
| 3905 | } else { /* No volume label is found or error */
|
---|
| 3906 | if (res == FR_NO_FILE) {
|
---|
| 3907 | res = FR_OK;
|
---|
| 3908 | if (vn[0]) { /* Create volume label as new */
|
---|
| 3909 | res = dir_alloc(&dj, 1); /* Allocate an entry for volume label */
|
---|
| 3910 | if (res == FR_OK) {
|
---|
| 3911 | mem_set(dj.dir, 0, SZ_DIR); /* Set volume label */
|
---|
| 3912 | mem_cpy(dj.dir, vn, 11);
|
---|
| 3913 | dj.dir[DIR_Attr] = AM_VOL;
|
---|
| 3914 | tm = GET_FATTIME();
|
---|
| 3915 | ST_DWORD(dj.dir+DIR_WrtTime, tm);
|
---|
| 3916 | dj.fs->wflag = 1;
|
---|
| 3917 | res = sync_fs(dj.fs);
|
---|
| 3918 | }
|
---|
| 3919 | }
|
---|
| 3920 | }
|
---|
| 3921 | }
|
---|
| 3922 | }
|
---|
| 3923 |
|
---|
| 3924 | LEAVE_FF(dj.fs, res);
|
---|
| 3925 | }
|
---|
| 3926 |
|
---|
| 3927 | #endif /* !_FS_READONLY */
|
---|
| 3928 | #endif /* _USE_LABEL */
|
---|
| 3929 |
|
---|
| 3930 |
|
---|
| 3931 |
|
---|
| 3932 | /*-----------------------------------------------------------------------*/
|
---|
| 3933 | /* Forward data to the stream directly (available on only tiny cfg) */
|
---|
| 3934 | /*-----------------------------------------------------------------------*/
|
---|
| 3935 | #if _USE_FORWARD && _FS_TINY
|
---|
| 3936 |
|
---|
| 3937 | FRESULT f_forward (
|
---|
| 3938 | FIL* fp, /* Pointer to the file object */
|
---|
| 3939 | UINT (*func)(const BYTE*,UINT), /* Pointer to the streaming function */
|
---|
| 3940 | UINT btf, /* Number of bytes to forward */
|
---|
| 3941 | UINT* bf /* Pointer to number of bytes forwarded */
|
---|
| 3942 | )
|
---|
| 3943 | {
|
---|
| 3944 | FRESULT res;
|
---|
| 3945 | DWORD remain, clst, sect;
|
---|
| 3946 | UINT rcnt;
|
---|
| 3947 | BYTE csect;
|
---|
| 3948 |
|
---|
| 3949 |
|
---|
| 3950 | *bf = 0; /* Clear transfer byte counter */
|
---|
| 3951 |
|
---|
| 3952 | res = validate(fp); /* Check validity of the object */
|
---|
| 3953 | if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
---|
| 3954 | if (fp->err) /* Check error */
|
---|
| 3955 | LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
---|
| 3956 | if (!(fp->flag & FA_READ)) /* Check access mode */
|
---|
| 3957 | LEAVE_FF(fp->fs, FR_DENIED);
|
---|
| 3958 |
|
---|
| 3959 | remain = fp->fsize - fp->fptr;
|
---|
| 3960 | if (btf > remain) btf = (UINT)remain; /* Truncate btf by remaining bytes */
|
---|
| 3961 |
|
---|
| 3962 | for ( ; btf && (*func)(0, 0); /* Repeat until all data transferred or stream becomes busy */
|
---|
| 3963 | fp->fptr += rcnt, *bf += rcnt, btf -= rcnt) {
|
---|
| 3964 | csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
|
---|
| 3965 | if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
|
---|
| 3966 | if (!csect) { /* On the cluster boundary? */
|
---|
| 3967 | clst = (fp->fptr == 0) ? /* On the top of the file? */
|
---|
| 3968 | fp->sclust : get_fat(fp->fs, fp->clust);
|
---|
| 3969 | if (clst <= 1) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 3970 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 3971 | fp->clust = clst; /* Update current cluster */
|
---|
| 3972 | }
|
---|
| 3973 | }
|
---|
| 3974 | sect = clust2sect(fp->fs, fp->clust); /* Get current data sector */
|
---|
| 3975 | if (!sect) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 3976 | sect += csect;
|
---|
| 3977 | if (move_window(fp->fs, sect) != FR_OK) /* Move sector window */
|
---|
| 3978 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
| 3979 | fp->dsect = sect;
|
---|
| 3980 | rcnt = SS(fp->fs) - (WORD)(fp->fptr % SS(fp->fs)); /* Forward data from sector window */
|
---|
| 3981 | if (rcnt > btf) rcnt = btf;
|
---|
| 3982 | rcnt = (*func)(&fp->fs->win[(WORD)fp->fptr % SS(fp->fs)], rcnt);
|
---|
| 3983 | if (!rcnt) ABORT(fp->fs, FR_INT_ERR);
|
---|
| 3984 | }
|
---|
| 3985 |
|
---|
| 3986 | LEAVE_FF(fp->fs, FR_OK);
|
---|
| 3987 | }
|
---|
| 3988 | #endif /* _USE_FORWARD */
|
---|
| 3989 |
|
---|
| 3990 |
|
---|
| 3991 |
|
---|
| 3992 | #if _USE_MKFS && !_FS_READONLY
|
---|
| 3993 | /*-----------------------------------------------------------------------*/
|
---|
| 3994 | /* Create File System on the Drive */
|
---|
| 3995 | /*-----------------------------------------------------------------------*/
|
---|
| 3996 | #define N_ROOTDIR 512 /* Number of root directory entries for FAT12/16 */
|
---|
| 3997 | #define N_FATS 1 /* Number of FATs (1 or 2) */
|
---|
| 3998 |
|
---|
| 3999 |
|
---|
| 4000 | FRESULT f_mkfs (
|
---|
| 4001 | const TCHAR* path, /* Logical drive number */
|
---|
| 4002 | BYTE sfd, /* Partitioning rule 0:FDISK, 1:SFD */
|
---|
| 4003 | UINT au /* Size of allocation unit in unit of byte or sector */
|
---|
| 4004 | )
|
---|
| 4005 | {
|
---|
| 4006 | static const WORD vst[] = { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0};
|
---|
| 4007 | static const WORD cst[] = {32768, 16384, 8192, 4096, 2048, 16384, 8192, 4096, 2048, 1024, 512};
|
---|
| 4008 | int vol;
|
---|
| 4009 | BYTE fmt, md, sys, *tbl, pdrv, part;
|
---|
| 4010 | DWORD n_clst, vs, n, wsect;
|
---|
| 4011 | UINT i;
|
---|
| 4012 | DWORD b_vol, b_fat, b_dir, b_data; /* LBA */
|
---|
| 4013 | DWORD n_vol, n_rsv, n_fat, n_dir; /* Size */
|
---|
| 4014 | FATFS *fs;
|
---|
| 4015 | DSTATUS stat;
|
---|
| 4016 |
|
---|
| 4017 |
|
---|
| 4018 | /* Check mounted drive and clear work area */
|
---|
| 4019 | if (sfd > 1) return FR_INVALID_PARAMETER;
|
---|
| 4020 | vol = get_ldnumber(&path);
|
---|
| 4021 | if (vol < 0) return FR_INVALID_DRIVE;
|
---|
| 4022 | fs = FatFs[vol];
|
---|
| 4023 | if (!fs) return FR_NOT_ENABLED;
|
---|
| 4024 | fs->fs_type = 0;
|
---|
| 4025 | pdrv = LD2PD(vol); /* Physical drive */
|
---|
| 4026 | part = LD2PT(vol); /* Partition (0:auto detect, 1-4:get from partition table)*/
|
---|
| 4027 |
|
---|
| 4028 | /* Get disk statics */
|
---|
| 4029 | stat = disk_initialize(pdrv);
|
---|
| 4030 | if (stat & STA_NOINIT) return FR_NOT_READY;
|
---|
| 4031 | if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
|
---|
| 4032 | #if _MAX_SS != _MIN_SS /* Get disk sector size */
|
---|
| 4033 | if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS || SS(fs) < _MIN_SS)
|
---|
| 4034 | return FR_DISK_ERR;
|
---|
| 4035 | #endif
|
---|
| 4036 | if (_MULTI_PARTITION && part) {
|
---|
| 4037 | /* Get partition information from partition table in the MBR */
|
---|
| 4038 | if (disk_read(pdrv, fs->win, 0, 1) != RES_OK) return FR_DISK_ERR;
|
---|
| 4039 | if (LD_WORD(fs->win+BS_55AA) != 0xAA55) return FR_MKFS_ABORTED;
|
---|
| 4040 | tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
|
---|
| 4041 | if (!tbl[4]) return FR_MKFS_ABORTED; /* No partition? */
|
---|
| 4042 | b_vol = LD_DWORD(tbl+8); /* Volume start sector */
|
---|
| 4043 | n_vol = LD_DWORD(tbl+12); /* Volume size */
|
---|
| 4044 | } else {
|
---|
| 4045 | /* Create a partition in this function */
|
---|
| 4046 | if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &n_vol) != RES_OK || n_vol < 128)
|
---|
| 4047 | return FR_DISK_ERR;
|
---|
| 4048 | b_vol = (sfd) ? 0 : 63; /* Volume start sector */
|
---|
| 4049 | n_vol -= b_vol; /* Volume size */
|
---|
| 4050 | }
|
---|
| 4051 |
|
---|
| 4052 | if (au & (au - 1)) au = 0;
|
---|
| 4053 | if (!au) { /* AU auto selection */
|
---|
| 4054 | vs = n_vol / (2000 / (SS(fs) / 512));
|
---|
| 4055 | for (i = 0; vs < vst[i]; i++) ;
|
---|
| 4056 | au = cst[i];
|
---|
| 4057 | }
|
---|
| 4058 | if (au >= _MIN_SS) au /= SS(fs); /* Number of sectors per cluster */
|
---|
| 4059 | if (!au) au = 1;
|
---|
| 4060 | if (au > 128) au = 128;
|
---|
| 4061 |
|
---|
| 4062 | /* Pre-compute number of clusters and FAT sub-type */
|
---|
| 4063 | n_clst = n_vol / au;
|
---|
| 4064 | fmt = FS_FAT12;
|
---|
| 4065 | if (n_clst >= MIN_FAT16) fmt = FS_FAT16;
|
---|
| 4066 | if (n_clst >= MIN_FAT32) fmt = FS_FAT32;
|
---|
| 4067 |
|
---|
| 4068 | /* Determine offset and size of FAT structure */
|
---|
| 4069 | if (fmt == FS_FAT32) {
|
---|
| 4070 | n_fat = ((n_clst * 4) + 8 + SS(fs) - 1) / SS(fs);
|
---|
| 4071 | n_rsv = 32;
|
---|
| 4072 | n_dir = 0;
|
---|
| 4073 | } else {
|
---|
| 4074 | n_fat = (fmt == FS_FAT12) ? (n_clst * 3 + 1) / 2 + 3 : (n_clst * 2) + 4;
|
---|
| 4075 | n_fat = (n_fat + SS(fs) - 1) / SS(fs);
|
---|
| 4076 | n_rsv = 1;
|
---|
| 4077 | n_dir = (DWORD)N_ROOTDIR * SZ_DIR / SS(fs);
|
---|
| 4078 | }
|
---|
| 4079 | b_fat = b_vol + n_rsv; /* FAT area start sector */
|
---|
| 4080 | b_dir = b_fat + n_fat * N_FATS; /* Directory area start sector */
|
---|
| 4081 | b_data = b_dir + n_dir; /* Data area start sector */
|
---|
| 4082 | if (n_vol < b_data + au - b_vol) return FR_MKFS_ABORTED; /* Too small volume */
|
---|
| 4083 |
|
---|
| 4084 | /* Align data start sector to erase block boundary (for flash memory media) */
|
---|
| 4085 | if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &n) != RES_OK || !n || n > 32768) n = 1;
|
---|
| 4086 | n = (b_data + n - 1) & ~(n - 1); /* Next nearest erase block from current data start */
|
---|
| 4087 | n = (n - b_data) / N_FATS;
|
---|
| 4088 | if (fmt == FS_FAT32) { /* FAT32: Move FAT offset */
|
---|
| 4089 | n_rsv += n;
|
---|
| 4090 | b_fat += n;
|
---|
| 4091 | } else { /* FAT12/16: Expand FAT size */
|
---|
| 4092 | n_fat += n;
|
---|
| 4093 | }
|
---|
| 4094 |
|
---|
| 4095 | /* Determine number of clusters and final check of validity of the FAT sub-type */
|
---|
| 4096 | n_clst = (n_vol - n_rsv - n_fat * N_FATS - n_dir) / au;
|
---|
| 4097 | if ( (fmt == FS_FAT16 && n_clst < MIN_FAT16)
|
---|
| 4098 | || (fmt == FS_FAT32 && n_clst < MIN_FAT32))
|
---|
| 4099 | return FR_MKFS_ABORTED;
|
---|
| 4100 |
|
---|
| 4101 | /* Determine system ID in the partition table */
|
---|
| 4102 | if (fmt == FS_FAT32) {
|
---|
| 4103 | sys = 0x0C; /* FAT32X */
|
---|
| 4104 | } else {
|
---|
| 4105 | if (fmt == FS_FAT12 && n_vol < 0x10000) {
|
---|
| 4106 | sys = 0x01; /* FAT12(<65536) */
|
---|
| 4107 | } else {
|
---|
| 4108 | sys = (n_vol < 0x10000) ? 0x04 : 0x06; /* FAT16(<65536) : FAT12/16(>=65536) */
|
---|
| 4109 | }
|
---|
| 4110 | }
|
---|
| 4111 |
|
---|
| 4112 | if (_MULTI_PARTITION && part) {
|
---|
| 4113 | /* Update system ID in the partition table */
|
---|
| 4114 | tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
|
---|
| 4115 | tbl[4] = sys;
|
---|
| 4116 | if (disk_write(pdrv, fs->win, 0, 1) != RES_OK) /* Write it to teh MBR */
|
---|
| 4117 | return FR_DISK_ERR;
|
---|
| 4118 | md = 0xF8;
|
---|
| 4119 | } else {
|
---|
| 4120 | if (sfd) { /* No partition table (SFD) */
|
---|
| 4121 | md = 0xF0;
|
---|
| 4122 | } else { /* Create partition table (FDISK) */
|
---|
| 4123 | mem_set(fs->win, 0, SS(fs));
|
---|
| 4124 | tbl = fs->win+MBR_Table; /* Create partition table for single partition in the drive */
|
---|
| 4125 | tbl[1] = 1; /* Partition start head */
|
---|
| 4126 | tbl[2] = 1; /* Partition start sector */
|
---|
| 4127 | tbl[3] = 0; /* Partition start cylinder */
|
---|
| 4128 | tbl[4] = sys; /* System type */
|
---|
| 4129 | tbl[5] = 254; /* Partition end head */
|
---|
| 4130 | n = (b_vol + n_vol) / 63 / 255;
|
---|
| 4131 | tbl[6] = (BYTE)(n >> 2 | 63); /* Partition end sector */
|
---|
| 4132 | tbl[7] = (BYTE)n; /* End cylinder */
|
---|
| 4133 | ST_DWORD(tbl+8, 63); /* Partition start in LBA */
|
---|
| 4134 | ST_DWORD(tbl+12, n_vol); /* Partition size in LBA */
|
---|
| 4135 | ST_WORD(fs->win+BS_55AA, 0xAA55); /* MBR signature */
|
---|
| 4136 | if (disk_write(pdrv, fs->win, 0, 1) != RES_OK) /* Write it to the MBR */
|
---|
| 4137 | return FR_DISK_ERR;
|
---|
| 4138 | md = 0xF8;
|
---|
| 4139 | }
|
---|
| 4140 | }
|
---|
| 4141 |
|
---|
| 4142 | /* Create BPB in the VBR */
|
---|
| 4143 | tbl = fs->win; /* Clear sector */
|
---|
| 4144 | mem_set(tbl, 0, SS(fs));
|
---|
| 4145 | mem_cpy(tbl, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code, OEM name */
|
---|
| 4146 | i = SS(fs); /* Sector size */
|
---|
| 4147 | ST_WORD(tbl+BPB_BytsPerSec, i);
|
---|
| 4148 | tbl[BPB_SecPerClus] = (BYTE)au; /* Sectors per cluster */
|
---|
| 4149 | ST_WORD(tbl+BPB_RsvdSecCnt, n_rsv); /* Reserved sectors */
|
---|
| 4150 | tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */
|
---|
| 4151 | i = (fmt == FS_FAT32) ? 0 : N_ROOTDIR; /* Number of root directory entries */
|
---|
| 4152 | ST_WORD(tbl+BPB_RootEntCnt, i);
|
---|
| 4153 | if (n_vol < 0x10000) { /* Number of total sectors */
|
---|
| 4154 | ST_WORD(tbl+BPB_TotSec16, n_vol);
|
---|
| 4155 | } else {
|
---|
| 4156 | ST_DWORD(tbl+BPB_TotSec32, n_vol);
|
---|
| 4157 | }
|
---|
| 4158 | tbl[BPB_Media] = md; /* Media descriptor */
|
---|
| 4159 | ST_WORD(tbl+BPB_SecPerTrk, 63); /* Number of sectors per track */
|
---|
| 4160 | ST_WORD(tbl+BPB_NumHeads, 255); /* Number of heads */
|
---|
| 4161 | ST_DWORD(tbl+BPB_HiddSec, b_vol); /* Hidden sectors */
|
---|
| 4162 | n = GET_FATTIME(); /* Use current time as VSN */
|
---|
| 4163 | if (fmt == FS_FAT32) {
|
---|
| 4164 | ST_DWORD(tbl+BS_VolID32, n); /* VSN */
|
---|
| 4165 | ST_DWORD(tbl+BPB_FATSz32, n_fat); /* Number of sectors per FAT */
|
---|
| 4166 | ST_DWORD(tbl+BPB_RootClus, 2); /* Root directory start cluster (2) */
|
---|
| 4167 | ST_WORD(tbl+BPB_FSInfo, 1); /* FSINFO record offset (VBR+1) */
|
---|
| 4168 | ST_WORD(tbl+BPB_BkBootSec, 6); /* Backup boot record offset (VBR+6) */
|
---|
| 4169 | tbl[BS_DrvNum32] = 0x80; /* Drive number */
|
---|
| 4170 | tbl[BS_BootSig32] = 0x29; /* Extended boot signature */
|
---|
| 4171 | mem_cpy(tbl+BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */
|
---|
| 4172 | } else {
|
---|
| 4173 | ST_DWORD(tbl+BS_VolID, n); /* VSN */
|
---|
| 4174 | ST_WORD(tbl+BPB_FATSz16, n_fat); /* Number of sectors per FAT */
|
---|
| 4175 | tbl[BS_DrvNum] = 0x80; /* Drive number */
|
---|
| 4176 | tbl[BS_BootSig] = 0x29; /* Extended boot signature */
|
---|
| 4177 | mem_cpy(tbl+BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */
|
---|
| 4178 | }
|
---|
| 4179 | ST_WORD(tbl+BS_55AA, 0xAA55); /* Signature (Offset is fixed here regardless of sector size) */
|
---|
| 4180 | if (disk_write(pdrv, tbl, b_vol, 1) != RES_OK) /* Write it to the VBR sector */
|
---|
| 4181 | return FR_DISK_ERR;
|
---|
| 4182 | if (fmt == FS_FAT32) /* Write backup VBR if needed (VBR+6) */
|
---|
| 4183 | disk_write(pdrv, tbl, b_vol + 6, 1);
|
---|
| 4184 |
|
---|
| 4185 | /* Initialize FAT area */
|
---|
| 4186 | wsect = b_fat;
|
---|
| 4187 | for (i = 0; i < N_FATS; i++) { /* Initialize each FAT copy */
|
---|
| 4188 | mem_set(tbl, 0, SS(fs)); /* 1st sector of the FAT */
|
---|
| 4189 | n = md; /* Media descriptor byte */
|
---|
| 4190 | if (fmt != FS_FAT32) {
|
---|
| 4191 | n |= (fmt == FS_FAT12) ? 0x00FFFF00 : 0xFFFFFF00;
|
---|
| 4192 | ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT12/16) */
|
---|
| 4193 | } else {
|
---|
| 4194 | n |= 0xFFFFFF00;
|
---|
| 4195 | ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT32) */
|
---|
| 4196 | ST_DWORD(tbl+4, 0xFFFFFFFF);
|
---|
| 4197 | ST_DWORD(tbl+8, 0x0FFFFFFF); /* Reserve cluster #2 for root directory */
|
---|
| 4198 | }
|
---|
| 4199 | if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
|
---|
| 4200 | return FR_DISK_ERR;
|
---|
| 4201 | mem_set(tbl, 0, SS(fs)); /* Fill following FAT entries with zero */
|
---|
| 4202 | for (n = 1; n < n_fat; n++) { /* This loop may take a time on FAT32 volume due to many single sector writes */
|
---|
| 4203 | if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
|
---|
| 4204 | return FR_DISK_ERR;
|
---|
| 4205 | }
|
---|
| 4206 | }
|
---|
| 4207 |
|
---|
| 4208 | /* Initialize root directory */
|
---|
| 4209 | i = (fmt == FS_FAT32) ? au : (UINT)n_dir;
|
---|
| 4210 | do {
|
---|
| 4211 | if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
|
---|
| 4212 | return FR_DISK_ERR;
|
---|
| 4213 | } while (--i);
|
---|
| 4214 |
|
---|
| 4215 | #if _USE_TRIM /* Erase data area if needed */
|
---|
| 4216 | {
|
---|
| 4217 | DWORD eb[2];
|
---|
| 4218 |
|
---|
| 4219 | eb[0] = wsect; eb[1] = wsect + (n_clst - ((fmt == FS_FAT32) ? 1 : 0)) * au - 1;
|
---|
| 4220 | disk_ioctl(pdrv, CTRL_TRIM, eb);
|
---|
| 4221 | }
|
---|
| 4222 | #endif
|
---|
| 4223 |
|
---|
| 4224 | /* Create FSINFO if needed */
|
---|
| 4225 | if (fmt == FS_FAT32) {
|
---|
| 4226 | ST_DWORD(tbl+FSI_LeadSig, 0x41615252);
|
---|
| 4227 | ST_DWORD(tbl+FSI_StrucSig, 0x61417272);
|
---|
| 4228 | ST_DWORD(tbl+FSI_Free_Count, n_clst - 1); /* Number of free clusters */
|
---|
| 4229 | ST_DWORD(tbl+FSI_Nxt_Free, 2); /* Last allocated cluster# */
|
---|
| 4230 | ST_WORD(tbl+BS_55AA, 0xAA55);
|
---|
| 4231 | disk_write(pdrv, tbl, b_vol + 1, 1); /* Write original (VBR+1) */
|
---|
| 4232 | disk_write(pdrv, tbl, b_vol + 7, 1); /* Write backup (VBR+7) */
|
---|
| 4233 | }
|
---|
| 4234 |
|
---|
| 4235 | return (disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR;
|
---|
| 4236 | }
|
---|
| 4237 |
|
---|
| 4238 |
|
---|
| 4239 |
|
---|
| 4240 | #if _MULTI_PARTITION
|
---|
| 4241 | /*-----------------------------------------------------------------------*/
|
---|
| 4242 | /* Divide Physical Drive */
|
---|
| 4243 | /*-----------------------------------------------------------------------*/
|
---|
| 4244 |
|
---|
| 4245 | FRESULT f_fdisk (
|
---|
| 4246 | BYTE pdrv, /* Physical drive number */
|
---|
| 4247 | const DWORD szt[], /* Pointer to the size table for each partitions */
|
---|
| 4248 | void* work /* Pointer to the working buffer */
|
---|
| 4249 | )
|
---|
| 4250 | {
|
---|
| 4251 | UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl;
|
---|
| 4252 | BYTE s_hd, e_hd, *p, *buf = (BYTE*)work;
|
---|
| 4253 | DSTATUS stat;
|
---|
| 4254 | DWORD sz_disk, sz_part, s_part;
|
---|
| 4255 |
|
---|
| 4256 |
|
---|
| 4257 | stat = disk_initialize(pdrv);
|
---|
| 4258 | if (stat & STA_NOINIT) return FR_NOT_READY;
|
---|
| 4259 | if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
|
---|
| 4260 | if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk)) return FR_DISK_ERR;
|
---|
| 4261 |
|
---|
| 4262 | /* Determine CHS in the table regardless of the drive geometry */
|
---|
| 4263 | for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ;
|
---|
| 4264 | if (n == 256) n--;
|
---|
| 4265 | e_hd = n - 1;
|
---|
| 4266 | sz_cyl = 63 * n;
|
---|
| 4267 | tot_cyl = sz_disk / sz_cyl;
|
---|
| 4268 |
|
---|
| 4269 | /* Create partition table */
|
---|
| 4270 | mem_set(buf, 0, _MAX_SS);
|
---|
| 4271 | p = buf + MBR_Table; b_cyl = 0;
|
---|
| 4272 | for (i = 0; i < 4; i++, p += SZ_PTE) {
|
---|
| 4273 | p_cyl = (szt[i] <= 100U) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl;
|
---|
| 4274 | if (!p_cyl) continue;
|
---|
| 4275 | s_part = (DWORD)sz_cyl * b_cyl;
|
---|
| 4276 | sz_part = (DWORD)sz_cyl * p_cyl;
|
---|
| 4277 | if (i == 0) { /* Exclude first track of cylinder 0 */
|
---|
| 4278 | s_hd = 1;
|
---|
| 4279 | s_part += 63; sz_part -= 63;
|
---|
| 4280 | } else {
|
---|
| 4281 | s_hd = 0;
|
---|
| 4282 | }
|
---|
| 4283 | e_cyl = b_cyl + p_cyl - 1;
|
---|
| 4284 | if (e_cyl >= tot_cyl) return FR_INVALID_PARAMETER;
|
---|
| 4285 |
|
---|
| 4286 | /* Set partition table */
|
---|
| 4287 | p[1] = s_hd; /* Start head */
|
---|
| 4288 | p[2] = (BYTE)((b_cyl >> 2) + 1); /* Start sector */
|
---|
| 4289 | p[3] = (BYTE)b_cyl; /* Start cylinder */
|
---|
| 4290 | p[4] = 0x06; /* System type (temporary setting) */
|
---|
| 4291 | p[5] = e_hd; /* End head */
|
---|
| 4292 | p[6] = (BYTE)((e_cyl >> 2) + 63); /* End sector */
|
---|
| 4293 | p[7] = (BYTE)e_cyl; /* End cylinder */
|
---|
| 4294 | ST_DWORD(p + 8, s_part); /* Start sector in LBA */
|
---|
| 4295 | ST_DWORD(p + 12, sz_part); /* Partition size */
|
---|
| 4296 |
|
---|
| 4297 | /* Next partition */
|
---|
| 4298 | b_cyl += p_cyl;
|
---|
| 4299 | }
|
---|
| 4300 | ST_WORD(p, 0xAA55);
|
---|
| 4301 |
|
---|
| 4302 | /* Write it to the MBR */
|
---|
| 4303 | return (disk_write(pdrv, buf, 0, 1) != RES_OK || disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) ? FR_DISK_ERR : FR_OK;
|
---|
| 4304 | }
|
---|
| 4305 |
|
---|
| 4306 |
|
---|
| 4307 | #endif /* _MULTI_PARTITION */
|
---|
| 4308 | #endif /* _USE_MKFS && !_FS_READONLY */
|
---|
| 4309 |
|
---|
| 4310 |
|
---|
| 4311 |
|
---|
| 4312 |
|
---|
| 4313 | #if _USE_STRFUNC
|
---|
| 4314 | /*-----------------------------------------------------------------------*/
|
---|
| 4315 | /* Get a string from the file */
|
---|
| 4316 | /*-----------------------------------------------------------------------*/
|
---|
| 4317 |
|
---|
| 4318 | TCHAR* f_gets (
|
---|
| 4319 | TCHAR* buff, /* Pointer to the string buffer to read */
|
---|
| 4320 | int len, /* Size of string buffer (characters) */
|
---|
| 4321 | FIL* fp /* Pointer to the file object */
|
---|
| 4322 | )
|
---|
| 4323 | {
|
---|
| 4324 | int n = 0;
|
---|
| 4325 | TCHAR c, *p = buff;
|
---|
| 4326 | BYTE s[2];
|
---|
| 4327 | UINT rc;
|
---|
| 4328 |
|
---|
| 4329 |
|
---|
| 4330 | while (n < len - 1) { /* Read characters until buffer gets filled */
|
---|
| 4331 | #if _USE_LFN && _LFN_UNICODE
|
---|
| 4332 | #if _STRF_ENCODE == 3 /* Read a character in UTF-8 */
|
---|
| 4333 | f_read(fp, s, 1, &rc);
|
---|
| 4334 | if (rc != 1) break;
|
---|
| 4335 | c = s[0];
|
---|
| 4336 | if (c >= 0x80) {
|
---|
| 4337 | if (c < 0xC0) continue; /* Skip stray trailer */
|
---|
| 4338 | if (c < 0xE0) { /* Two-byte sequence */
|
---|
| 4339 | f_read(fp, s, 1, &rc);
|
---|
| 4340 | if (rc != 1) break;
|
---|
| 4341 | c = (c & 0x1F) << 6 | (s[0] & 0x3F);
|
---|
| 4342 | if (c < 0x80) c = '?';
|
---|
| 4343 | } else {
|
---|
| 4344 | if (c < 0xF0) { /* Three-byte sequence */
|
---|
| 4345 | f_read(fp, s, 2, &rc);
|
---|
| 4346 | if (rc != 2) break;
|
---|
| 4347 | c = c << 12 | (s[0] & 0x3F) << 6 | (s[1] & 0x3F);
|
---|
| 4348 | if (c < 0x800) c = '?';
|
---|
| 4349 | } else { /* Reject four-byte sequence */
|
---|
| 4350 | c = '?';
|
---|
| 4351 | }
|
---|
| 4352 | }
|
---|
| 4353 | }
|
---|
| 4354 | #elif _STRF_ENCODE == 2 /* Read a character in UTF-16BE */
|
---|
| 4355 | f_read(fp, s, 2, &rc);
|
---|
| 4356 | if (rc != 2) break;
|
---|
| 4357 | c = s[1] + (s[0] << 8);
|
---|
| 4358 | #elif _STRF_ENCODE == 1 /* Read a character in UTF-16LE */
|
---|
| 4359 | f_read(fp, s, 2, &rc);
|
---|
| 4360 | if (rc != 2) break;
|
---|
| 4361 | c = s[0] + (s[1] << 8);
|
---|
| 4362 | #else /* Read a character in ANSI/OEM */
|
---|
| 4363 | f_read(fp, s, 1, &rc);
|
---|
| 4364 | if (rc != 1) break;
|
---|
| 4365 | c = s[0];
|
---|
| 4366 | if (IsDBCS1(c)) {
|
---|
| 4367 | f_read(fp, s, 1, &rc);
|
---|
| 4368 | if (rc != 1) break;
|
---|
| 4369 | c = (c << 8) + s[0];
|
---|
| 4370 | }
|
---|
| 4371 | c = ff_convert(c, 1); /* OEM -> Unicode */
|
---|
| 4372 | if (!c) c = '?';
|
---|
| 4373 | #endif
|
---|
| 4374 | #else /* Read a character without conversion */
|
---|
| 4375 | f_read(fp, s, 1, &rc);
|
---|
| 4376 | if (rc != 1) break;
|
---|
| 4377 | c = s[0];
|
---|
| 4378 | #endif
|
---|
| 4379 | if (_USE_STRFUNC == 2 && c == '\r') continue; /* Strip '\r' */
|
---|
| 4380 | *p++ = c;
|
---|
| 4381 | n++;
|
---|
| 4382 | if (c == '\n') break; /* Break on EOL */
|
---|
| 4383 | }
|
---|
| 4384 | *p = 0;
|
---|
| 4385 | return n ? buff : 0; /* When no data read (eof or error), return with error. */
|
---|
| 4386 | }
|
---|
| 4387 |
|
---|
| 4388 |
|
---|
| 4389 |
|
---|
| 4390 | #if !_FS_READONLY
|
---|
| 4391 | #include <stdarg.h>
|
---|
| 4392 | /*-----------------------------------------------------------------------*/
|
---|
| 4393 | /* Put a character to the file */
|
---|
| 4394 | /*-----------------------------------------------------------------------*/
|
---|
| 4395 |
|
---|
| 4396 | typedef struct {
|
---|
| 4397 | FIL* fp;
|
---|
| 4398 | int idx, nchr;
|
---|
| 4399 | BYTE buf[64];
|
---|
| 4400 | } putbuff;
|
---|
| 4401 |
|
---|
| 4402 |
|
---|
| 4403 | static
|
---|
| 4404 | void putc_bfd (
|
---|
| 4405 | putbuff* pb,
|
---|
| 4406 | TCHAR c
|
---|
| 4407 | )
|
---|
| 4408 | {
|
---|
| 4409 | UINT bw;
|
---|
| 4410 | int i;
|
---|
| 4411 |
|
---|
| 4412 |
|
---|
| 4413 | if (_USE_STRFUNC == 2 && c == '\n') /* LF -> CRLF conversion */
|
---|
| 4414 | putc_bfd(pb, '\r');
|
---|
| 4415 |
|
---|
| 4416 | i = pb->idx; /* Buffer write index (-1:error) */
|
---|
| 4417 | if (i < 0) return;
|
---|
| 4418 |
|
---|
| 4419 | #if _USE_LFN && _LFN_UNICODE
|
---|
| 4420 | #if _STRF_ENCODE == 3 /* Write a character in UTF-8 */
|
---|
| 4421 | if (c < 0x80) { /* 7-bit */
|
---|
| 4422 | pb->buf[i++] = (BYTE)c;
|
---|
| 4423 | } else {
|
---|
| 4424 | if (c < 0x800) { /* 11-bit */
|
---|
| 4425 | pb->buf[i++] = (BYTE)(0xC0 | c >> 6);
|
---|
| 4426 | } else { /* 16-bit */
|
---|
| 4427 | pb->buf[i++] = (BYTE)(0xE0 | c >> 12);
|
---|
| 4428 | pb->buf[i++] = (BYTE)(0x80 | (c >> 6 & 0x3F));
|
---|
| 4429 | }
|
---|
| 4430 | pb->buf[i++] = (BYTE)(0x80 | (c & 0x3F));
|
---|
| 4431 | }
|
---|
| 4432 | #elif _STRF_ENCODE == 2 /* Write a character in UTF-16BE */
|
---|
| 4433 | pb->buf[i++] = (BYTE)(c >> 8);
|
---|
| 4434 | pb->buf[i++] = (BYTE)c;
|
---|
| 4435 | #elif _STRF_ENCODE == 1 /* Write a character in UTF-16LE */
|
---|
| 4436 | pb->buf[i++] = (BYTE)c;
|
---|
| 4437 | pb->buf[i++] = (BYTE)(c >> 8);
|
---|
| 4438 | #else /* Write a character in ANSI/OEM */
|
---|
| 4439 | c = ff_convert(c, 0); /* Unicode -> OEM */
|
---|
| 4440 | if (!c) c = '?';
|
---|
| 4441 | if (c >= 0x100)
|
---|
| 4442 | pb->buf[i++] = (BYTE)(c >> 8);
|
---|
| 4443 | pb->buf[i++] = (BYTE)c;
|
---|
| 4444 | #endif
|
---|
| 4445 | #else /* Write a character without conversion */
|
---|
| 4446 | pb->buf[i++] = (BYTE)c;
|
---|
| 4447 | #endif
|
---|
| 4448 |
|
---|
| 4449 | if (i >= (int)(sizeof pb->buf) - 3) { /* Write buffered characters to the file */
|
---|
| 4450 | f_write(pb->fp, pb->buf, (UINT)i, &bw);
|
---|
| 4451 | i = (bw == (UINT)i) ? 0 : -1;
|
---|
| 4452 | }
|
---|
| 4453 | pb->idx = i;
|
---|
| 4454 | pb->nchr++;
|
---|
| 4455 | }
|
---|
| 4456 |
|
---|
| 4457 |
|
---|
| 4458 |
|
---|
| 4459 | int f_putc (
|
---|
| 4460 | TCHAR c, /* A character to be output */
|
---|
| 4461 | FIL* fp /* Pointer to the file object */
|
---|
| 4462 | )
|
---|
| 4463 | {
|
---|
| 4464 | putbuff pb;
|
---|
| 4465 | UINT nw;
|
---|
| 4466 |
|
---|
| 4467 |
|
---|
| 4468 | pb.fp = fp; /* Initialize output buffer */
|
---|
| 4469 | pb.nchr = pb.idx = 0;
|
---|
| 4470 |
|
---|
| 4471 | putc_bfd(&pb, c); /* Put a character */
|
---|
| 4472 |
|
---|
| 4473 | if ( pb.idx >= 0 /* Flush buffered characters to the file */
|
---|
| 4474 | && f_write(pb.fp, pb.buf, (UINT)pb.idx, &nw) == FR_OK
|
---|
| 4475 | && (UINT)pb.idx == nw) return pb.nchr;
|
---|
| 4476 | return EOF;
|
---|
| 4477 | }
|
---|
| 4478 |
|
---|
| 4479 |
|
---|
| 4480 |
|
---|
| 4481 |
|
---|
| 4482 | /*-----------------------------------------------------------------------*/
|
---|
| 4483 | /* Put a string to the file */
|
---|
| 4484 | /*-----------------------------------------------------------------------*/
|
---|
| 4485 |
|
---|
| 4486 | int f_puts (
|
---|
| 4487 | const TCHAR* str, /* Pointer to the string to be output */
|
---|
| 4488 | FIL* fp /* Pointer to the file object */
|
---|
| 4489 | )
|
---|
| 4490 | {
|
---|
| 4491 | putbuff pb;
|
---|
| 4492 | UINT nw;
|
---|
| 4493 |
|
---|
| 4494 |
|
---|
| 4495 | pb.fp = fp; /* Initialize output buffer */
|
---|
| 4496 | pb.nchr = pb.idx = 0;
|
---|
| 4497 |
|
---|
| 4498 | while (*str) /* Put the string */
|
---|
| 4499 | putc_bfd(&pb, *str++);
|
---|
| 4500 |
|
---|
| 4501 | if ( pb.idx >= 0 /* Flush buffered characters to the file */
|
---|
| 4502 | && f_write(pb.fp, pb.buf, (UINT)pb.idx, &nw) == FR_OK
|
---|
| 4503 | && (UINT)pb.idx == nw) return pb.nchr;
|
---|
| 4504 | return EOF;
|
---|
| 4505 | }
|
---|
| 4506 |
|
---|
| 4507 |
|
---|
| 4508 |
|
---|
| 4509 |
|
---|
| 4510 | /*-----------------------------------------------------------------------*/
|
---|
| 4511 | /* Put a formatted string to the file */
|
---|
| 4512 | /*-----------------------------------------------------------------------*/
|
---|
| 4513 |
|
---|
| 4514 | int f_printf (
|
---|
| 4515 | FIL* fp, /* Pointer to the file object */
|
---|
| 4516 | const TCHAR* fmt, /* Pointer to the format string */
|
---|
| 4517 | ... /* Optional arguments... */
|
---|
| 4518 | )
|
---|
| 4519 | {
|
---|
| 4520 | va_list arp;
|
---|
| 4521 | BYTE f, r;
|
---|
| 4522 | UINT nw, i, j, w;
|
---|
| 4523 | DWORD v;
|
---|
| 4524 | TCHAR c, d, s[16], *p;
|
---|
| 4525 | putbuff pb;
|
---|
| 4526 |
|
---|
| 4527 |
|
---|
| 4528 | pb.fp = fp; /* Initialize output buffer */
|
---|
| 4529 | pb.nchr = pb.idx = 0;
|
---|
| 4530 |
|
---|
| 4531 | va_start(arp, fmt);
|
---|
| 4532 |
|
---|
| 4533 | for (;;) {
|
---|
| 4534 | c = *fmt++;
|
---|
| 4535 | if (c == 0) break; /* End of string */
|
---|
| 4536 | if (c != '%') { /* Non escape character */
|
---|
| 4537 | putc_bfd(&pb, c);
|
---|
| 4538 | continue;
|
---|
| 4539 | }
|
---|
| 4540 | w = f = 0;
|
---|
| 4541 | c = *fmt++;
|
---|
| 4542 | if (c == '0') { /* Flag: '0' padding */
|
---|
| 4543 | f = 1; c = *fmt++;
|
---|
| 4544 | } else {
|
---|
| 4545 | if (c == '-') { /* Flag: left justified */
|
---|
| 4546 | f = 2; c = *fmt++;
|
---|
| 4547 | }
|
---|
| 4548 | }
|
---|
| 4549 | while (IsDigit(c)) { /* Precision */
|
---|
| 4550 | w = w * 10 + c - '0';
|
---|
| 4551 | c = *fmt++;
|
---|
| 4552 | }
|
---|
| 4553 | if (c == 'l' || c == 'L') { /* Prefix: Size is long int */
|
---|
| 4554 | f |= 4; c = *fmt++;
|
---|
| 4555 | }
|
---|
| 4556 | if (!c) break;
|
---|
| 4557 | d = c;
|
---|
| 4558 | if (IsLower(d)) d -= 0x20;
|
---|
| 4559 | switch (d) { /* Type is... */
|
---|
| 4560 | case 'S' : /* String */
|
---|
| 4561 | p = va_arg(arp, TCHAR*);
|
---|
| 4562 | for (j = 0; p[j]; j++) ;
|
---|
| 4563 | if (!(f & 2)) {
|
---|
| 4564 | while (j++ < w) putc_bfd(&pb, ' ');
|
---|
| 4565 | }
|
---|
| 4566 | while (*p) putc_bfd(&pb, *p++);
|
---|
| 4567 | while (j++ < w) putc_bfd(&pb, ' ');
|
---|
| 4568 | continue;
|
---|
| 4569 | case 'C' : /* Character */
|
---|
| 4570 | putc_bfd(&pb, (TCHAR)va_arg(arp, int)); continue;
|
---|
| 4571 | case 'B' : /* Binary */
|
---|
| 4572 | r = 2; break;
|
---|
| 4573 | case 'O' : /* Octal */
|
---|
| 4574 | r = 8; break;
|
---|
| 4575 | case 'D' : /* Signed decimal */
|
---|
| 4576 | case 'U' : /* Unsigned decimal */
|
---|
| 4577 | r = 10; break;
|
---|
| 4578 | case 'X' : /* Hexdecimal */
|
---|
| 4579 | r = 16; break;
|
---|
| 4580 | default: /* Unknown type (pass-through) */
|
---|
| 4581 | putc_bfd(&pb, c); continue;
|
---|
| 4582 | }
|
---|
| 4583 |
|
---|
| 4584 | /* Get an argument and put it in numeral */
|
---|
| 4585 | v = (f & 4) ? (DWORD)va_arg(arp, long) : ((d == 'D') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int));
|
---|
| 4586 | if (d == 'D' && (v & 0x80000000)) {
|
---|
| 4587 | v = 0 - v;
|
---|
| 4588 | f |= 8;
|
---|
| 4589 | }
|
---|
| 4590 | i = 0;
|
---|
| 4591 | do {
|
---|
| 4592 | d = (TCHAR)(v % r); v /= r;
|
---|
| 4593 | if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
|
---|
| 4594 | s[i++] = d + '0';
|
---|
| 4595 | } while (v && i < sizeof s / sizeof s[0]);
|
---|
| 4596 | if (f & 8) s[i++] = '-';
|
---|
| 4597 | j = i; d = (f & 1) ? '0' : ' ';
|
---|
| 4598 | while (!(f & 2) && j++ < w) putc_bfd(&pb, d);
|
---|
| 4599 | do putc_bfd(&pb, s[--i]); while (i);
|
---|
| 4600 | while (j++ < w) putc_bfd(&pb, d);
|
---|
| 4601 | }
|
---|
| 4602 |
|
---|
| 4603 | va_end(arp);
|
---|
| 4604 |
|
---|
| 4605 | if ( pb.idx >= 0 /* Flush buffered characters to the file */
|
---|
| 4606 | && f_write(pb.fp, pb.buf, (UINT)pb.idx, &nw) == FR_OK
|
---|
| 4607 | && (UINT)pb.idx == nw) return pb.nchr;
|
---|
| 4608 | return EOF;
|
---|
| 4609 | }
|
---|
| 4610 |
|
---|
| 4611 | #endif /* !_FS_READONLY */
|
---|
| 4612 | #endif /* _USE_STRFUNC */
|
---|