This program is an example of of using the MSCUSBHost code with a raw build of ELM Chan Fat Fs. This was done to add both Long File Name Support along with proper time/date stamps (assuming you have a battery hooked up to keep time). This code exposes the Chan API (see main.cpp) and is NOT a c++ class: http://elm-chan.org/fsw/ff/00index_e.html The diskio.c file has the mapping needed to link the filesystem to the MSC stuff

Dependencies:   mbed

Committer:
emh203
Date:
Sun Jan 23 18:35:43 2011 +0000
Revision:
0:2dbbafe1b1fb
1st test version.   Test with raw mbed and Samtec USB-RA type A connector wired directly to pins.

Who changed what in which revision?

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