EA BaseBoard, playing wav, PC see\'s SD-card through USB port.

Dependencies:   mbed

Committer:
Lerche
Date:
Tue Nov 22 05:45:58 2011 +0000
Revision:
0:fef366d2ed20
Thanks to those who provided EA_WavPlayer and USB_MSC

Who changed what in which revision?

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