Uses a more recent version of official mbed libraries than that of Neil Thiessen

Dependencies:   FATFileSystem

Dependents:   ThermalWake STM32F407VET6_SDCard STM32F407VET6_SD_HTTP_Server ZZ_SSL_Main_L476 ... more

Fork of SDFileSystem by Neil Thiessen

Committer:
neilt6
Date:
Thu Aug 14 22:27:07 2014 +0000
Revision:
11:67ddc53e3983
Major performance improvements with custom FATFileSystem

Who changed what in which revision?

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