-

Dependents:   RSALB_hbridge_helloworld RSALB_lobster WavPlayer_test AudioCODEC_HelloWorld ... more

Fork of FatFileSystemCpp by Igor Skochinsky

Committer:
p07gbar
Date:
Wed Sep 19 11:03:35 2012 +0000
Revision:
2:e869ee8f3c3d
Parent:
1:88f22c32a456
-

Who changed what in which revision?

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