Example SDFileSystem in which FATDirHandle exposes the file info struct of the current file/directory.

Committer:
uci1
Date:
Fri Nov 28 05:12:38 2014 +0000
Revision:
1:a7d8fc28a863
Parent:
0:687056ba3278
change SD init so it won't stall on failed initialization

Who changed what in which revision?

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