ye tian / Mbed 2 deprecated missile_command

Dependencies:   4DGL-uLCD-SE mbed wave_player

Fork of missile_command by ECE 2035 TA

Committer:
arvahsu
Date:
Wed Nov 12 02:06:48 2014 +0000
Revision:
2:5e7876719560
Copy the SDFileSystem library due to the original author stop sharing the library.

Who changed what in which revision?

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