test

Committer:
emh203
Date:
Sun Feb 21 02:45:09 2010 +0000
Revision:
0:c1253c12d4bc

        

Who changed what in which revision?

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