Bruno Cavalcanti / Mbed 2 deprecated brunoDrome

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Fri Mar 24 15:54:41 2017 +0000
Revision:
0:1c0a769988ee
Saidas digitais com pwm ok, entradas analogicas ok

Who changed what in which revision?

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