For Terrance

Dependencies:   mbed

Committer:
emh203
Date:
Wed Jun 13 15:10:06 2012 +0000
Revision:
0:085749c8446f

        

Who changed what in which revision?

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