Fork of Adam Green's library with .cpp fix for current compiler

Dependents:   MSCUsbHost mpod_nhk_english mpod_picasa_photoframe mpod_nhk_english_spxml ... more

Fork of FatFileSystem by Adam Green

Committer:
igorsk
Date:
Mon Jul 30 13:45:05 2012 +0000
Revision:
1:88f22c32a456
Parent:
ff.c@0:6ceefe1c53e4
renamed .c to .cpp

Who changed what in which revision?

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