Michael Shimniok / Mbed 2 deprecated DataBus

Dependencies:   mbed Watchdog SDFileSystem DigoleSerialDisp

Committer:
shimniok
Date:
Mon May 27 13:26:03 2013 +0000
Revision:
0:a6a169de725f
Working version with priorities set and update time display

Who changed what in which revision?

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