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