s

Fork of FATFileSystem by Chaiyaporn Boonyasathian

Committer:
583405000008
Date:
Tue Dec 06 07:22:42 2016 +0000
Revision:
1:6fa3f673d44c
Parent:
0:9296bb2a98a8
d;

Who changed what in which revision?

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