P2-2 Harris Barton

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
DCchico
Date:
Fri Oct 23 16:18:39 2020 -0400
Revision:
1:10330bce85cb
shell-code

Who changed what in which revision?

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