Compact Flash I/O test

Dependencies:   mbed

Committer:
emh203
Date:
Fri Dec 30 21:02:16 2011 +0000
Revision:
1:dc171f34db9b
Parent:
0:6b1e6c9e48ba

        

Who changed what in which revision?

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