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