dd

Dependencies:   C12832 mbed

Committer:
pfe
Date:
Wed Apr 08 08:27:55 2015 +0000
Revision:
0:caecb0d1e7d3
ddd

Who changed what in which revision?

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