modified to compile for me

Fork of FATFileSystem by mbed official

Committer:
screamer
Date:
Mon Mar 17 14:09:00 2014 +0000
Revision:
3:e960e2b81a3c
Parent:
1:46ce1e16c870
Child:
4:3ff2606d5713
Added code documentation and fixed compile warning

Who changed what in which revision?

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