ISP example program.

Dependencies:   SLCD mbed USBLocalFileSystem

/media/uploads/va009039/lpc81isp-360x240.jpg

FRDM-KL46ZLPC810
UART RXDPTE23p2(P0_4)
UART TXDPTE22p8(P0_0)
nRESETD6p1(P0_5)
nISPD8p5(P0_1)
GNDGNDp7
3.3VP3V3p6

Copy binary image to the disk called LPC81ISP.
Push sw1 or sw3, start write to LPC810 flash.

Committer:
va009039
Date:
Sun Feb 16 12:56:12 2014 +0000
Revision:
1:cccfc461c61f
Parent:
0:ad2b1fc04955
add virtual COM.

Who changed what in which revision?

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