Working SDHOST/FatFS, boot partition mounts, some other minor fixes too.
[rpi-open-firmware.git] / arm_chainloader / drivers / fatfs / ff.c
1 /*----------------------------------------------------------------------------/
2 / FatFs - FAT file system module R0.12 (C)ChaN, 2016 /
3 /-----------------------------------------------------------------------------/
4 / FatFs module is a free software that opened under license policy of
5 / following conditions.
6 /
7 / Copyright (C) 2016, ChaN, all right reserved.
8 /
9 / 1. Redistributions of source code must retain the above copyright notice,
10 / this condition and the following disclaimer.
11 /
12 / This software is provided by the copyright holder and contributors "AS IS"
13 / and any warranties related to this software are DISCLAIMED.
14 / The copyright owner or contributors be NOT LIABLE for any damages caused
15 / by use of this software.
16 /----------------------------------------------------------------------------*/
17
18
19 #include "ff.h" /* Declarations of FatFs API */
20 #include "diskio.h" /* Declarations of disk I/O functions */
21
22
23 /*--------------------------------------------------------------------------
24
25 Module Private Definitions
26
27 ---------------------------------------------------------------------------*/
28
29 #if _FATFS != 88100 /* Revision ID */
30 #error Wrong include file (ff.h).
31 #endif
32
33
34 /* Reentrancy related */
35 #if _FS_REENTRANT
36 #if _USE_LFN == 1
37 #error Static LFN work area cannot be used at thread-safe configuration
38 #endif
39 #define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; }
40 #define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }
41 #else
42 #define ENTER_FF(fs)
43 #define LEAVE_FF(fs, res) return res
44 #endif
45
46 #define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); }
47
48
49 /* Definitions of sector size */
50 #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)
51 #error Wrong sector size configuration
52 #endif
53 #if _MAX_SS == _MIN_SS
54 #define SS(fs) ((UINT)_MAX_SS) /* Fixed sector size */
55 #else
56 #define SS(fs) ((fs)->ssize) /* Variable sector size */
57 #endif
58
59
60 /* Timestamp */
61 #if _FS_NORTC == 1
62 #if _NORTC_YEAR < 1980 || _NORTC_YEAR > 2107 || _NORTC_MON < 1 || _NORTC_MON > 12 || _NORTC_MDAY < 1 || _NORTC_MDAY > 31
63 #error Invalid _FS_NORTC settings
64 #endif
65 #define GET_FATTIME() ((DWORD)(_NORTC_YEAR - 1980) << 25 | (DWORD)_NORTC_MON << 21 | (DWORD)_NORTC_MDAY << 16)
66 #else
67 #define GET_FATTIME() get_fattime()
68 #endif
69
70
71 /* File lock controls */
72 #if _FS_LOCK != 0
73 #if _FS_READONLY
74 #error _FS_LOCK must be 0 at read-only configuration
75 #endif
76 typedef struct {
77 FATFS *fs; /* Object ID 1, volume (NULL:blank entry) */
78 DWORD clu; /* Object ID 2, directory (0:root) */
79 DWORD ofs; /* Object ID 3, directory offset */
80 WORD ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */
81 } FILESEM;
82 #endif
83
84
85
86 /* DBCS code ranges and SBCS upper conversion tables */
87
88 #if _CODE_PAGE == 932 /* Japanese Shift-JIS */
89 #define _DF1S 0x81 /* DBC 1st byte range 1 start */
90 #define _DF1E 0x9F /* DBC 1st byte range 1 end */
91 #define _DF2S 0xE0 /* DBC 1st byte range 2 start */
92 #define _DF2E 0xFC /* DBC 1st byte range 2 end */
93 #define _DS1S 0x40 /* DBC 2nd byte range 1 start */
94 #define _DS1E 0x7E /* DBC 2nd byte range 1 end */
95 #define _DS2S 0x80 /* DBC 2nd byte range 2 start */
96 #define _DS2E 0xFC /* DBC 2nd byte range 2 end */
97
98 #elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
99 #define _DF1S 0x81
100 #define _DF1E 0xFE
101 #define _DS1S 0x40
102 #define _DS1E 0x7E
103 #define _DS2S 0x80
104 #define _DS2E 0xFE
105
106 #elif _CODE_PAGE == 949 /* Korean */
107 #define _DF1S 0x81
108 #define _DF1E 0xFE
109 #define _DS1S 0x41
110 #define _DS1E 0x5A
111 #define _DS2S 0x61
112 #define _DS2E 0x7A
113 #define _DS3S 0x81
114 #define _DS3E 0xFE
115
116 #elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */
117 #define _DF1S 0x81
118 #define _DF1E 0xFE
119 #define _DS1S 0x40
120 #define _DS1E 0x7E
121 #define _DS2S 0xA1
122 #define _DS2E 0xFE
123
124 #elif _CODE_PAGE == 437 /* U.S. */
125 #define _DF1S 0
126 #define _EXCVT {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \
127 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
128 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
129 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
130 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
131 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
132 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
133 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
134
135 #elif _CODE_PAGE == 720 /* Arabic */
136 #define _DF1S 0
137 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
138 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
139 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
140 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
141 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
142 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
143 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
144 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
145
146 #elif _CODE_PAGE == 737 /* Greek */
147 #define _DF1S 0
148 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
149 0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \
150 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96, \
151 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
152 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
153 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
154 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xEF,0xF5,0xF0,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
155 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
156
157 #elif _CODE_PAGE == 771 /* KBL */
158 #define _DF1S 0
159 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
160 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
161 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
162 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
163 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
164 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDC,0xDE,0xDE, \
165 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
166 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFE,0xFF}
167
168 #elif _CODE_PAGE == 775 /* Baltic */
169 #define _DF1S 0
170 #define _EXCVT {0x80,0x9A,0x91,0xA0,0x8E,0x95,0x8F,0x80,0xAD,0xED,0x8A,0x8A,0xA1,0x8D,0x8E,0x8F, \
171 0x90,0x92,0x92,0xE2,0x99,0x95,0x96,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
172 0xA0,0xA1,0xE0,0xA3,0xA3,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
173 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
174 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
175 0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
176 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE3,0xE8,0xE8,0xEA,0xEA,0xEE,0xED,0xEE,0xEF, \
177 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
178
179 #elif _CODE_PAGE == 850 /* Latin 1 */
180 #define _DF1S 0
181 #define _EXCVT {0x43,0x55,0x45,0x41,0x41,0x41,0x41,0x43,0x45,0x45,0x45,0x49,0x49,0x49,0x41,0x41, \
182 0x45,0x92,0x92,0x4F,0x4F,0x4F,0x55,0x55,0x59,0x4F,0x55,0x4F,0x9C,0x4F,0x9E,0x9F, \
183 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
184 0xB0,0xB1,0xB2,0xB3,0xB4,0x41,0x41,0x41,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
185 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0x41,0x41,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
186 0xD1,0xD1,0x45,0x45,0x45,0x49,0x49,0x49,0x49,0xD9,0xDA,0xDB,0xDC,0xDD,0x49,0xDF, \
187 0x4F,0xE1,0x4F,0x4F,0x4F,0x4F,0xE6,0xE8,0xE8,0x55,0x55,0x55,0x59,0x59,0xEE,0xEF, \
188 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
189
190 #elif _CODE_PAGE == 852 /* Latin 2 */
191 #define _DF1S 0
192 #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F, \
193 0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0xAC, \
194 0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA,0x8D,0xAC,0xB8,0xAE,0xAF, \
195 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBF, \
196 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC6,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
197 0xD1,0xD1,0xD2,0xD3,0xD2,0xD5,0xD6,0xD7,0xB7,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
198 0xE0,0xE1,0xE2,0xE3,0xE3,0xD5,0xE6,0xE6,0xE8,0xE9,0xE8,0xEB,0xED,0xED,0xDD,0xEF, \
199 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xEB,0xFC,0xFC,0xFE,0xFF}
200
201 #elif _CODE_PAGE == 855 /* Cyrillic */
202 #define _DF1S 0
203 #define _EXCVT {0x81,0x81,0x83,0x83,0x85,0x85,0x87,0x87,0x89,0x89,0x8B,0x8B,0x8D,0x8D,0x8F,0x8F, \
204 0x91,0x91,0x93,0x93,0x95,0x95,0x97,0x97,0x99,0x99,0x9B,0x9B,0x9D,0x9D,0x9F,0x9F, \
205 0xA1,0xA1,0xA3,0xA3,0xA5,0xA5,0xA7,0xA7,0xA9,0xA9,0xAB,0xAB,0xAD,0xAD,0xAE,0xAF, \
206 0xB0,0xB1,0xB2,0xB3,0xB4,0xB6,0xB6,0xB8,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBE,0xBF, \
207 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
208 0xD1,0xD1,0xD3,0xD3,0xD5,0xD5,0xD7,0xD7,0xDD,0xD9,0xDA,0xDB,0xDC,0xDD,0xE0,0xDF, \
209 0xE0,0xE2,0xE2,0xE4,0xE4,0xE6,0xE6,0xE8,0xE8,0xEA,0xEA,0xEC,0xEC,0xEE,0xEE,0xEF, \
210 0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFD,0xFE,0xFF}
211
212 #elif _CODE_PAGE == 857 /* Turkish */
213 #define _DF1S 0
214 #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0x49,0x8E,0x8F, \
215 0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x98,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9E, \
216 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
217 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
218 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
219 0xD0,0xD1,0xD2,0xD3,0xD4,0x49,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
220 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xDE,0xED,0xEE,0xEF, \
221 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
222
223 #elif _CODE_PAGE == 860 /* Portuguese */
224 #define _DF1S 0
225 #define _EXCVT {0x80,0x9A,0x90,0x8F,0x8E,0x91,0x86,0x80,0x89,0x89,0x92,0x8B,0x8C,0x98,0x8E,0x8F, \
226 0x90,0x91,0x92,0x8C,0x99,0xA9,0x96,0x9D,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
227 0x86,0x8B,0x9F,0x96,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
228 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
229 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
230 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
231 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
232 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
233
234 #elif _CODE_PAGE == 861 /* Icelandic */
235 #define _DF1S 0
236 #define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x8B,0x8B,0x8D,0x8E,0x8F, \
237 0x90,0x92,0x92,0x4F,0x99,0x8D,0x55,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
238 0xA4,0xA5,0xA6,0xA7,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
239 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
240 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
241 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
242 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
243 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
244
245 #elif _CODE_PAGE == 862 /* Hebrew */
246 #define _DF1S 0
247 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
248 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
249 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
250 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
251 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
252 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
253 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
254 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
255
256 #elif _CODE_PAGE == 863 /* Canadian-French */
257 #define _DF1S 0
258 #define _EXCVT {0x43,0x55,0x45,0x41,0x41,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x41,0x8F, \
259 0x45,0x45,0x45,0x4F,0x45,0x49,0x55,0x55,0x98,0x4F,0x55,0x9B,0x9C,0x55,0x55,0x9F, \
260 0xA0,0xA1,0x4F,0x55,0xA4,0xA5,0xA6,0xA7,0x49,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
261 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
262 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
263 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
264 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
265 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
266
267 #elif _CODE_PAGE == 864 /* Arabic */
268 #define _DF1S 0
269 #define _EXCVT {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \
270 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
271 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
272 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
273 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
274 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
275 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
276 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
277
278 #elif _CODE_PAGE == 865 /* Nordic */
279 #define _DF1S 0
280 #define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \
281 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
282 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
283 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
284 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
285 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
286 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
287 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
288
289 #elif _CODE_PAGE == 866 /* Russian */
290 #define _DF1S 0
291 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
292 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
293 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
294 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
295 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
296 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
297 0x90,0x91,0x92,0x93,0x9d,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
298 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
299
300 #elif _CODE_PAGE == 869 /* Greek 2 */
301 #define _DF1S 0
302 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
303 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x86,0x9C,0x8D,0x8F,0x90, \
304 0x91,0x90,0x92,0x95,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
305 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
306 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
307 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xA4,0xA5,0xA6,0xD9,0xDA,0xDB,0xDC,0xA7,0xA8,0xDF, \
308 0xA9,0xAA,0xAC,0xAD,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xCF,0xCF,0xD0,0xEF, \
309 0xF0,0xF1,0xD1,0xD2,0xD3,0xF5,0xD4,0xF7,0xF8,0xF9,0xD5,0x96,0x95,0x98,0xFE,0xFF}
310
311 #elif _CODE_PAGE == 1 /* ASCII (for only non-LFN cfg) */
312 #if _USE_LFN != 0
313 #error Cannot enable LFN without valid code page.
314 #endif
315 #define _DF1S 0
316
317 #else
318 #error Unknown code page
319
320 #endif
321
322
323 /* Character code support macros */
324 #define IsUpper(c) (((c)>='A')&&((c)<='Z'))
325 #define IsLower(c) (((c)>='a')&&((c)<='z'))
326 #define IsDigit(c) (((c)>='0')&&((c)<='9'))
327
328 #if _DF1S != 0 /* Code page is DBCS */
329
330 #ifdef _DF2S /* Two 1st byte areas */
331 #define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
332 #else /* One 1st byte area */
333 #define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
334 #endif
335
336 #ifdef _DS3S /* Three 2nd byte areas */
337 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
338 #else /* Two 2nd byte areas */
339 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
340 #endif
341
342 #else /* Code page is SBCS */
343
344 #define IsDBCS1(c) 0
345 #define IsDBCS2(c) 0
346
347 #endif /* _DF1S */
348
349
350 /* Name status flags */
351 #define NSFLAG 11 /* Index of name status byte in fn[] */
352 #define NS_LOSS 0x01 /* Out of 8.3 format */
353 #define NS_LFN 0x02 /* Force to create LFN entry */
354 #define NS_LAST 0x04 /* Last segment */
355 #define NS_BODY 0x08 /* Lower case flag (body) */
356 #define NS_EXT 0x10 /* Lower case flag (ext) */
357 #define NS_DOT 0x20 /* Dot entry */
358 #define NS_NONAME 0x80 /* Not followed */
359
360
361 /* Limits and Boundaries (Differ from specs but correct for real DOS/Windows) */
362 #define MIN_FAT16 4086U /* Minimum number of clusters of FAT16 */
363 #define MIN_FAT32 65526U /* Minimum number of clusters of FAT32 */
364 #define MAX_DIR 0x200000 /* Maximum size of FAT directory */
365 #define MAX_DIR_EX 0x10000000 /* Maximum size of exFAT directory */
366
367
368 /* FatFs refers the members in the FAT structures as byte array instead of
369 / structure members because the structure is not binary compatible between
370 / different platforms */
371
372 #define BS_jmpBoot 0 /* x86 jump instruction (3-byte) */
373 #define BS_OEMName 3 /* OEM name (8-byte) */
374 #define BPB_BytsPerSec 11 /* Sector size [byte] (WORD) */
375 #define BPB_SecPerClus 13 /* Cluster size [sector] (BYTE) */
376 #define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (WORD) */
377 #define BPB_NumFATs 16 /* Number of FATs (BYTE) */
378 #define BPB_RootEntCnt 17 /* Size of root directory area for FAT12/16 [entry] (WORD) */
379 #define BPB_TotSec16 19 /* Volume size (16-bit) [sector] (WORD) */
380 #define BPB_Media 21 /* Media descriptor (BYTE) */
381 #define BPB_FATSz16 22 /* FAT size (16-bit) [sector] (WORD) */
382 #define BPB_SecPerTrk 24 /* Track size for int13h [sector] (WORD) */
383 #define BPB_NumHeads 26 /* Number of heads for int13h (WORD) */
384 #define BPB_HiddSec 28 /* Volume offset from top of the drive (DWORD) */
385 #define BPB_TotSec32 32 /* Volume size (32-bit) [sector] (DWORD) */
386 #define BS_DrvNum 36 /* Physical drive number for int13h (BYTE) */
387 #define BS_NTres 37 /* Error flag (BYTE) */
388 #define BS_BootSig 38 /* Extended boot signature (BYTE) */
389 #define BS_VolID 39 /* Volume serial number (DWORD) */
390 #define BS_VolLab 43 /* Volume label string (8-byte) */
391 #define BS_FilSysType 54 /* File system type string (8-byte) */
392 #define BPB_FATSz32 36 /* FAT size (32-bit) [sector] (DWORD) */
393 #define BPB_ExtFlags 40 /* Extended flags (WORD) */
394
395 #define BPB_FSVer32 42 /* FAT32: File system version (WORD) */
396 #define BPB_RootClus32 44 /* FAT32: Root directory cluster (DWORD) */
397 #define BPB_FSInfo32 48 /* FAT32: Offset of FSINFO sector (WORD) */
398 #define BPB_BkBootSec32 50 /* FAT32: Offset of backup boot sector (WORD) */
399 #define BS_DrvNum32 64 /* FAT32: Physical drive number for int13h (BYTE) */
400 #define BS_NTres32 65 /* FAT32: Error flag (BYTE) */
401 #define BS_BootSig32 66 /* FAT32: Extended boot signature (BYTE) */
402 #define BS_VolID32 67 /* FAT32: Volume serial number (DWORD) */
403 #define BS_VolLab32 71 /* FAT32: Volume label string (8-byte) */
404 #define BS_FilSysType32 82 /* FAT32: File system type string (8-byte) */
405
406 #define BPB_ZeroedEx 11 /* exFAT: Must be zero (35-byte) */
407 #define BPB_VolOfsEx 64 /* exFAT: Volume offset from top of the drive [sector] (QWORD) */
408 #define BPB_TotSecEx 72 /* exFAT: Volume size [sector] (QWORD) */
409 #define BPB_FatOfsEx 80 /* exFAT: FAT offset from top of the volume [sector] (DWORD) */
410 #define BPB_FatSzEx 84 /* exFAT: FAT size [sector] (DWORD) */
411 #define BPB_DataOfsEx 88 /* exFAT: Data offset from top of the volume [sector] (DWORD) */
412 #define BPB_NumClusEx 92 /* exFAT: Number of clusters (DWORD) */
413 #define BPB_RootClusEx 96 /* exFAT: Root directory cluster (DWORD) */
414 #define BPB_VolIDEx 100 /* exFAT: Volume serial number (DWORD) */
415 #define BPB_FSVerEx 104 /* exFAT: File system version (WORD) */
416 #define BPB_VolFlagEx 106 /* exFAT: Volume flags (BYTE) */
417 #define BPB_ActFatEx 107 /* exFAT: Active FAT flags (BYTE) */
418 #define BPB_BytsPerSecEx 108 /* exFAT: Log2 of sector size in byte (BYTE) */
419 #define BPB_SecPerClusEx 109 /* exFAT: Log2 of cluster size in sector (BYTE) */
420 #define BPB_NumFATsEx 110 /* exFAT: Number of FATs (BYTE) */
421 #define BPB_DrvNumEx 111 /* exFAT: Physical drive number for int13h (BYTE) */
422 #define BPB_PercInUseEx 112 /* exFAT: Percent in use (BYTE) */
423
424 #define FSI_LeadSig 0 /* FAT32 FSI: Leading signature (DWORD) */
425 #define FSI_StrucSig 484 /* FAT32 FSI: Structure signature (DWORD) */
426 #define FSI_Free_Count 488 /* FAT32 FSI: Number of free clusters (DWORD) */
427 #define FSI_Nxt_Free 492 /* FAT32 FSI: Last allocated cluster (DWORD) */
428
429 #define MBR_Table 446 /* MBR: Partition table offset */
430 #define SZ_PTE 16 /* MBR: Size of a partition table entry */
431
432 #define BS_55AA 510 /* Signature word (WORD) */
433
434 #define DIR_Name 0 /* Short file name (11) */
435 #define DIR_Attr 11 /* Attribute (1) */
436 #define DIR_NTres 12 /* Lower case flag (1) */
437 #define DIR_CrtTime10 13 /* Created time sub-second (1) */
438 #define DIR_CrtTime 14 /* Created time (2) */
439 #define DIR_CrtDate 16 /* Created date (2) */
440 #define DIR_LstAccDate 18 /* Last accessed date (2) */
441 #define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (WORD) */
442 #define DIR_WrtTime 22 /* Modified time (2) */
443 #define DIR_WrtDate 24 /* Modified date (2) */
444 #define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (WORD) */
445 #define DIR_FileSize 28 /* File size (DWORD) */
446 #define LDIR_Ord 0 /* LFN entry order and LLE flag (1) */
447 #define LDIR_Attr 11 /* LFN attribute (1) */
448 #define LDIR_Type 12 /* LFN type (1) */
449 #define LDIR_Chksum 13 /* Checksum of the SFN entry */
450 #define LDIR_FstClusLO 26 /* Must be zero (WORD) */
451 #define XDIR_Type 0 /* Type of exFAT directory entry (BYTE) */
452 #define XDIR_NumLabel 1 /* Number of volume label characters (BYTE) */
453 #define XDIR_Label 2 /* Volume label (11-WORD) */
454 #define XDIR_CaseSum 4 /* Sum of case conversion table (DWORD) */
455 #define XDIR_NumSec 1 /* Number of secondary entries (BYTE) */
456 #define XDIR_SetSum 2 /* Sum of the set of directory entries (WORD) */
457 #define XDIR_Attr 4 /* File attribute (WORD) */
458 #define XDIR_CrtTime 8 /* Created time (4) */
459 #define XDIR_ModTime 12 /* Modified time (4) */
460 #define XDIR_AccTime 16 /* Last accessed time (4) */
461 #define XDIR_CrtTime10 20 /* Created time subsecond (1) */
462 #define XDIR_ModTime10 21 /* Modified time subsecond (1) */
463 #define XDIR_CrtTZ 22 /* Created timezone (1) */
464 #define XDIR_ModTZ 23 /* Modified timezone (1) */
465 #define XDIR_AccTZ 24 /* Last accessed timezone (1) */
466 #define XDIR_GenFlags 33 /* Gneral flags (1) */
467 #define XDIR_NumName 35 /* Number of file name characters (BYTE) */
468 #define XDIR_NameHash 36 /* Hash of file name (WORD) */
469 #define XDIR_ValidFileSize 40 /* Valid file size (QWORD) */
470 #define XDIR_FstClus 52 /* First cluster of the File/Directory (DWORD) */
471 #define XDIR_FileSize 56 /* File/Directory size (QWORD) */
472
473 #define SZDIRE 32 /* Size of a directory entry */
474 #define LLEF 0x40 /* Last long entry flag in LDIR_Ord */
475 #define DDEM 0xE5 /* Deleted directory entry mark at DIR_Name[0] */
476 #define RDDEM 0x05 /* Replacement of the character collides with DDEM */
477
478
479
480
481
482 /*--------------------------------------------------------------------------
483
484 Module Private Work Area
485
486 ---------------------------------------------------------------------------*/
487
488 /* Remark: Variables here without initial value shall be guaranteed zero/null
489 / at start-up. If not, either the linker or start-up routine being used is
490 / not compliance with C standard. */
491
492 #if _VOLUMES < 1 || _VOLUMES > 9
493 #error Wrong _VOLUMES setting
494 #endif
495 static FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical drives) */
496 static WORD Fsid; /* File system mount ID */
497
498 #if _FS_RPATH != 0 && _VOLUMES >= 2
499 static BYTE CurrVol; /* Current drive */
500 #endif
501
502 #if _FS_LOCK != 0
503 static FILESEM Files[_FS_LOCK]; /* Open object lock semaphores */
504 #endif
505
506 #if _USE_LFN == 0 /* Non-LFN configuration */
507 #define DEF_NAMBUF BYTE sfn[12]
508 #define INIT_NAMBUF(dobj) (dobj).fn = sfn
509 #define FREE_NAMBUF()
510 #define DEF_DIRBUF
511 #define INIT_DIRBUF(fs)
512 #define FREE_DIRBUF()
513 #else
514 #if _MAX_LFN < 12 || _MAX_LFN > 255
515 #error Wrong _MAX_LFN setting
516 #endif
517
518 #if _USE_LFN == 1 /* LFN enabled with static working buffer */
519 #if _FS_EXFAT
520 static BYTE DirBuf[SZDIRE*19]; /* Directory entry block scratchpad buffer (19 entries in size) */
521 #endif
522 static WCHAR LfnBuf[_MAX_LFN+1]; /* LFN enabled with static working buffer */
523 #define DEF_NAMBUF BYTE sfn[12]
524 #define INIT_NAMBUF(dj) { (dj).fn = sfn; (dj).lfn = LfnBuf; }
525 #define FREE_NAMBUF()
526 #define DEF_DIRBUF
527 #define INIT_DIRBUF(fs)
528 #define FREE_DIRBUF()
529
530 #elif _USE_LFN == 2 /* LFN enabled with dynamic working buffer on the stack */
531 #if _FS_EXFAT
532 #define DEF_NAMBUF BYTE sfn[12]; WCHAR lbuf[_MAX_LFN+1]; BYTE dbuf[SZDIRE*19]
533 #define INIT_NAMBUF(dj) { (dj).fn = sfn; (dj).lfn = lbuf; (dj).obj.fs->dirbuf = dbuf; }
534 #define FREE_NAMBUF()
535 #define DEF_DIRBUF BYTE dbuf[SZDIRE*19]
536 #define INIT_DIRBUF(fs) fs->dirbuf = dbuf
537 #define FREE_DIRBUF()
538 #else
539 #define DEF_NAMBUF BYTE sfn[12]; WCHAR lbuf[_MAX_LFN+1]
540 #define INIT_NAMBUF(dj) { (dj).fn = sfn; (dj).lfn = lbuf; }
541 #define FREE_NAMBUF()
542 #define DEF_DIRBUF
543 #define INIT_DIRBUF(fs)
544 #define FREE_DIRBUF()
545 #endif
546
547 #elif _USE_LFN == 3 /* LFN enabled with dynamic working buffer on the heap */
548 #if _FS_EXFAT
549 #define DEF_NAMBUF BYTE sfn[12]; WCHAR *lfn
550 #define INIT_NAMBUF(dj) { lfn = ff_memalloc((_MAX_LFN+1)*2 + SZDIRE*19); if (!lfn) LEAVE_FF((dj).obj.fs, FR_NOT_ENOUGH_CORE); (dj).fn = sfn; (dj).lfn = lfn; (dj).obj.fs->dirbuf = (BYTE*)(lfn+_MAX_LFN+1); }
551 #define FREE_NAMBUF() ff_memfree(lfn)
552 #define DEF_DIRBUF BYTE *dirb
553 #define INIT_DIRBUF(fs) { dirb = ff_memalloc(SZDIRE*19); if (!dirb) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); fs->dirbuf = dirb; }
554 #define FREE_DIRBUF() ff_memfree(dirb)
555 #else
556 #define DEF_NAMBUF BYTE sfn[12]; WCHAR *lfn
557 #define INIT_NAMBUF(dj) { lfn = ff_memalloc((_MAX_LFN+1)*2); if (!lfn) LEAVE_FF((dj).obj.fs, FR_NOT_ENOUGH_CORE); (dj).fn = sfn; (dj).lfn = lfn; }
558 #define FREE_NAMBUF() ff_memfree(lfn)
559 #define DEF_DIRBUF
560 #define INIT_DIRBUF(fs)
561 #define FREE_DIRBUF()
562 #endif
563
564 #else
565 #error Wrong _USE_LFN setting
566 #endif
567 #endif
568
569 #ifdef _EXCVT
570 static const BYTE ExCvt[] = _EXCVT; /* Upper conversion table for SBCS extended characters */
571 #endif
572
573
574
575
576
577
578 /*--------------------------------------------------------------------------
579
580 Module Private Functions
581
582 ---------------------------------------------------------------------------*/
583
584
585 /*-----------------------------------------------------------------------*/
586 /* Load/Store multi-byte word in the FAT structure */
587 /*-----------------------------------------------------------------------*/
588
589 static
590 WORD ld_word (const BYTE* ptr) /* Load a 2-byte little-endian word */
591 {
592 WORD rv;
593
594 rv = ptr[1];
595 rv = rv << 8 | ptr[0];
596 return rv;
597 }
598
599 static
600 DWORD ld_dword (const BYTE* ptr) /* Load a 4-byte little-endian word */
601 {
602 DWORD rv;
603
604 rv = ptr[3];
605 rv = rv << 8 | ptr[2];
606 rv = rv << 8 | ptr[1];
607 rv = rv << 8 | ptr[0];
608 return rv;
609 }
610
611 #if _FS_EXFAT
612 static
613 QWORD ld_qword (const BYTE* ptr) /* Load an 8-byte little-endian word */
614 {
615 QWORD rv;
616
617 rv = ptr[7];
618 rv = rv << 8 | ptr[6];
619 rv = rv << 8 | ptr[5];
620 rv = rv << 8 | ptr[4];
621 rv = rv << 8 | ptr[3];
622 rv = rv << 8 | ptr[2];
623 rv = rv << 8 | ptr[1];
624 rv = rv << 8 | ptr[0];
625 return rv;
626 }
627 #endif
628
629 #if !_FS_READONLY
630 static
631 void st_word (BYTE* ptr, WORD val) /* Store a 2-byte word in little-endian */
632 {
633 *ptr++ = (BYTE)val; val >>= 8;
634 *ptr++ = (BYTE)val;
635 }
636
637 static
638 void st_dword (BYTE* ptr, DWORD val) /* Store a 4-byte word in little-endian */
639 {
640 *ptr++ = (BYTE)val; val >>= 8;
641 *ptr++ = (BYTE)val; val >>= 8;
642 *ptr++ = (BYTE)val; val >>= 8;
643 *ptr++ = (BYTE)val;
644 }
645
646 #if _FS_EXFAT
647 static
648 void st_qword (BYTE* ptr, QWORD val) /* Store an 8-byte word in little-endian */
649 {
650 *ptr++ = (BYTE)val; val >>= 8;
651 *ptr++ = (BYTE)val; val >>= 8;
652 *ptr++ = (BYTE)val; val >>= 8;
653 *ptr++ = (BYTE)val; val >>= 8;
654 *ptr++ = (BYTE)val; val >>= 8;
655 *ptr++ = (BYTE)val; val >>= 8;
656 *ptr++ = (BYTE)val; val >>= 8;
657 *ptr++ = (BYTE)val;
658 }
659 #endif
660 #endif /* !_FS_READONLY */
661
662
663
664 /*-----------------------------------------------------------------------*/
665 /* String functions */
666 /*-----------------------------------------------------------------------*/
667
668 /* Copy memory to memory */
669 static
670 void mem_cpy (void* dst, const void* src, UINT cnt) {
671 BYTE *d = (BYTE*)dst;
672 const BYTE *s = (const BYTE*)src;
673
674 if (cnt) {
675 do *d++ = *s++; while (--cnt);
676 }
677 }
678
679 /* Fill memory block */
680 static
681 void mem_set (void* dst, int val, UINT cnt) {
682 BYTE *d = (BYTE*)dst;
683
684 do *d++ = (BYTE)val; while (--cnt);
685 }
686
687 /* Compare memory block */
688 static
689 int mem_cmp (const void* dst, const void* src, UINT cnt) { /* ZR:same, NZ:different */
690 const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
691 int r = 0;
692
693 do {
694 r = *d++ - *s++;
695 } while (--cnt && r == 0);
696
697 return r;
698 }
699
700 /* Check if chr is contained in the string */
701 static
702 int chk_chr (const char* str, int chr) { /* NZ:contained, ZR:not contained */
703 while (*str && *str != chr) str++;
704 return *str;
705 }
706
707
708
709
710 /*-----------------------------------------------------------------------*/
711 /* Request/Release grant to access the volume */
712 /*-----------------------------------------------------------------------*/
713 #if _FS_REENTRANT
714 static
715 int lock_fs (
716 FATFS* fs /* File system object */
717 )
718 {
719 return ff_req_grant(fs->sobj);
720 }
721
722
723 static
724 void unlock_fs (
725 FATFS* fs, /* File system object */
726 FRESULT res /* Result code to be returned */
727 )
728 {
729 if (fs && res != FR_NOT_ENABLED && res != FR_INVALID_DRIVE && res != FR_TIMEOUT) {
730 ff_rel_grant(fs->sobj);
731 }
732 }
733 #endif
734
735
736
737
738 /*-----------------------------------------------------------------------*/
739 /* File lock control functions */
740 /*-----------------------------------------------------------------------*/
741 #if _FS_LOCK != 0
742
743 static
744 FRESULT chk_lock ( /* Check if the file can be accessed */
745 DIR* dp, /* Directory object pointing the file to be checked */
746 int acc /* Desired access type (0:Read, 1:Write, 2:Delete/Rename) */
747 )
748 {
749 UINT i, be;
750
751 /* Search file semaphore table */
752 for (i = be = 0; i < _FS_LOCK; i++) {
753 if (Files[i].fs) { /* Existing entry */
754 if (Files[i].fs == dp->obj.fs && /* Check if the object matched with an open object */
755 Files[i].clu == dp->obj.sclust &&
756 Files[i].ofs == dp->dptr) break;
757 } else { /* Blank entry */
758 be = 1;
759 }
760 }
761 if (i == _FS_LOCK) { /* The object is not opened */
762 return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES; /* Is there a blank entry for new object? */
763 }
764
765 /* The object has been opened. Reject any open against writing file and all write mode open */
766 return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
767 }
768
769
770 static
771 int enq_lock (void) /* Check if an entry is available for a new object */
772 {
773 UINT i;
774
775 for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
776 return (i == _FS_LOCK) ? 0 : 1;
777 }
778
779
780 static
781 UINT inc_lock ( /* Increment object open counter and returns its index (0:Internal error) */
782 DIR* dp, /* Directory object pointing the file to register or increment */
783 int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */
784 )
785 {
786 UINT i;
787
788
789 for (i = 0; i < _FS_LOCK; i++) { /* Find the object */
790 if (Files[i].fs == dp->obj.fs &&
791 Files[i].clu == dp->obj.sclust &&
792 Files[i].ofs == dp->dptr) break;
793 }
794
795 if (i == _FS_LOCK) { /* Not opened. Register it as new. */
796 for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
797 if (i == _FS_LOCK) return 0; /* No free entry to register (int err) */
798 Files[i].fs = dp->obj.fs;
799 Files[i].clu = dp->obj.sclust;
800 Files[i].ofs = dp->dptr;
801 Files[i].ctr = 0;
802 }
803
804 if (acc && Files[i].ctr) return 0; /* Access violation (int err) */
805
806 Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */
807
808 return i + 1;
809 }
810
811
812 static
813 FRESULT dec_lock ( /* Decrement object open counter */
814 UINT i /* Semaphore index (1..) */
815 )
816 {
817 WORD n;
818 FRESULT res;
819
820
821 if (--i < _FS_LOCK) { /* Shift index number origin from 0 */
822 n = Files[i].ctr;
823 if (n == 0x100) n = 0; /* If write mode open, delete the entry */
824 if (n > 0) n--; /* Decrement read mode open count */
825 Files[i].ctr = n;
826 if (n == 0) Files[i].fs = 0; /* Delete the entry if open count gets zero */
827 res = FR_OK;
828 } else {
829 res = FR_INT_ERR; /* Invalid index nunber */
830 }
831 return res;
832 }
833
834
835 static
836 void clear_lock ( /* Clear lock entries of the volume */
837 FATFS *fs
838 )
839 {
840 UINT i;
841
842 for (i = 0; i < _FS_LOCK; i++) {
843 if (Files[i].fs == fs) Files[i].fs = 0;
844 }
845 }
846 #endif
847
848
849
850
851 /*-----------------------------------------------------------------------*/
852 /* Move/Flush disk access window in the file system object */
853 /*-----------------------------------------------------------------------*/
854 #if !_FS_READONLY
855 static
856 FRESULT sync_window ( /* Returns FR_OK or FR_DISK_ERROR */
857 FATFS* fs /* File system object */
858 )
859 {
860 DWORD wsect;
861 UINT nf;
862 FRESULT res = FR_OK;
863
864
865 if (fs->wflag) { /* Write back the sector if it is dirty */
866 wsect = fs->winsect; /* Current sector number */
867 if (disk_write(fs->drv, fs->win, wsect, 1) != RES_OK) {
868 res = FR_DISK_ERR;
869 } else {
870 fs->wflag = 0;
871 if (wsect - fs->fatbase < fs->fsize) { /* Is it in the FAT area? */
872 for (nf = fs->n_fats; nf >= 2; nf--) { /* Reflect the change to all FAT copies */
873 wsect += fs->fsize;
874 disk_write(fs->drv, fs->win, wsect, 1);
875 }
876 }
877 }
878 }
879 return res;
880 }
881 #endif
882
883
884 static
885 FRESULT move_window ( /* Returns FR_OK or FR_DISK_ERROR */
886 FATFS* fs, /* File system object */
887 DWORD sector /* Sector number to make appearance in the fs->win[] */
888 )
889 {
890 FRESULT res = FR_OK;
891
892
893 if (sector != fs->winsect) { /* Window offset changed? */
894 #if !_FS_READONLY
895 res = sync_window(fs); /* Write-back changes */
896 #endif
897 if (res == FR_OK) { /* Fill sector window with new data */
898 if (disk_read(fs->drv, fs->win, sector, 1) != RES_OK) {
899 sector = 0xFFFFFFFF; /* Invalidate window if data is not reliable */
900 res = FR_DISK_ERR;
901 }
902 fs->winsect = sector;
903 }
904 }
905 return res;
906 }
907
908
909
910
911 /*-----------------------------------------------------------------------*/
912 /* Synchronize file system and strage device */
913 /*-----------------------------------------------------------------------*/
914 #if !_FS_READONLY
915 static
916 FRESULT sync_fs ( /* FR_OK:succeeded, !=0:error */
917 FATFS* fs /* File system object */
918 )
919 {
920 FRESULT res;
921
922
923 res = sync_window(fs);
924 if (res == FR_OK) {
925 /* Update FSInfo sector if needed */
926 if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) {
927 /* Create FSInfo structure */
928 mem_set(fs->win, 0, SS(fs));
929 st_word(fs->win + BS_55AA, 0xAA55);
930 st_dword(fs->win + FSI_LeadSig, 0x41615252);
931 st_dword(fs->win + FSI_StrucSig, 0x61417272);
932 st_dword(fs->win + FSI_Free_Count, fs->free_clst);
933 st_dword(fs->win + FSI_Nxt_Free, fs->last_clst);
934 /* Write it into the FSInfo sector */
935 fs->winsect = fs->volbase + 1;
936 disk_write(fs->drv, fs->win, fs->winsect, 1);
937 fs->fsi_flag = 0;
938 }
939 /* Make sure that no pending write process in the physical drive */
940 if (disk_ioctl(fs->drv, CTRL_SYNC, 0) != RES_OK) res = FR_DISK_ERR;
941 }
942
943 return res;
944 }
945 #endif
946
947
948
949
950 /*-----------------------------------------------------------------------*/
951 /* Get sector# from cluster# */
952 /*-----------------------------------------------------------------------*/
953
954 static
955 DWORD clust2sect ( /* !=0:Sector number, 0:Failed (invalid cluster#) */
956 FATFS* fs, /* File system object */
957 DWORD clst /* Cluster# to be converted */
958 )
959 {
960 clst -= 2;
961 if (clst >= fs->n_fatent - 2) return 0; /* Invalid cluster# */
962 return clst * fs->csize + fs->database;
963 }
964
965
966
967
968 /*-----------------------------------------------------------------------*/
969 /* FAT access - Read value of a FAT entry */
970 /*-----------------------------------------------------------------------*/
971
972 static
973 DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFFF:Cluster status */
974 _FDID* obj, /* Corresponding object */
975 DWORD clst /* Cluster number to get the value */
976 )
977 {
978 UINT wc, bc;
979 DWORD val;
980 FATFS *fs = obj->fs;
981
982
983 if (clst < 2 || clst >= fs->n_fatent) { /* Check if in valid range */
984 val = 1; /* Internal error */
985
986 } else {
987 val = 0xFFFFFFFF; /* Default value falls on disk error */
988
989 switch (fs->fs_type) {
990 case FS_FAT12 :
991 bc = (UINT)clst; bc += bc / 2;
992 if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;
993 wc = fs->win[bc++ % SS(fs)];
994 if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;
995 wc |= fs->win[bc % SS(fs)] << 8;
996 val = clst & 1 ? wc >> 4 : (wc & 0xFFF);
997 break;
998
999 case FS_FAT16 :
1000 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))) != FR_OK) break;
1001 val = ld_word(&fs->win[clst * 2 % SS(fs)]);
1002 break;
1003
1004 case FS_FAT32 :
1005 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;
1006 val = ld_dword(&fs->win[clst * 4 % SS(fs)]) & 0x0FFFFFFF;
1007 break;
1008 #if _FS_EXFAT
1009 case FS_EXFAT :
1010 if (obj->objsize) {
1011 DWORD cofs = clst - obj->sclust; /* Offset from start cluster */
1012 DWORD clen = (DWORD)((obj->objsize - 1) / SS(fs)) / fs->csize; /* Number of clusters - 1 */
1013
1014 if (obj->stat == 2) { /* Is there no valid chain on the FAT? */
1015 if (cofs <= clen) {
1016 val = (cofs == clen) ? 0x7FFFFFFF : clst + 1; /* Generate the value */
1017 break;
1018 }
1019 }
1020 if (obj->stat == 3 && cofs < obj->n_cont) { /* Is it in the contiguous part? */
1021 val = clst + 1; /* Generate the value */
1022 break;
1023 }
1024 if (obj->stat != 2) { /* Get value from FAT if FAT chain is valid */
1025 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;
1026 val = ld_dword(&fs->win[clst * 4 % SS(fs)]) & 0x7FFFFFFF;
1027 break;
1028 }
1029 }
1030 /* Go default */
1031 #endif
1032 default:
1033 val = 1; /* Internal error */
1034 }
1035 }
1036
1037 return val;
1038 }
1039
1040
1041
1042
1043 /*-----------------------------------------------------------------------*/
1044 /* FAT access - Change value of a FAT entry */
1045 /*-----------------------------------------------------------------------*/
1046
1047 #if !_FS_READONLY
1048 static
1049 FRESULT put_fat ( /* FR_OK(0):succeeded, !=0:error */
1050 FATFS* fs, /* Corresponding object */
1051 DWORD clst, /* FAT index number (cluster number) to be changed */
1052 DWORD val /* New value to be set to the entry */
1053 )
1054 {
1055 UINT bc;
1056 BYTE *p;
1057 FRESULT res = FR_INT_ERR;
1058
1059
1060 if (clst >= 2 && clst < fs->n_fatent) { /* Check if in valid range */
1061 switch (fs->fs_type) {
1062 case FS_FAT12 : /* Bitfield items */
1063 bc = (UINT)clst; bc += bc / 2;
1064 res = move_window(fs, fs->fatbase + (bc / SS(fs)));
1065 if (res != FR_OK) break;
1066 p = &fs->win[bc++ % SS(fs)];
1067 *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
1068 fs->wflag = 1;
1069 res = move_window(fs, fs->fatbase + (bc / SS(fs)));
1070 if (res != FR_OK) break;
1071 p = &fs->win[bc % SS(fs)];
1072 *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
1073 fs->wflag = 1;
1074 break;
1075
1076 case FS_FAT16 : /* WORD aligned items */
1077 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
1078 if (res != FR_OK) break;
1079 st_word(&fs->win[clst * 2 % SS(fs)], (WORD)val);
1080 fs->wflag = 1;
1081 break;
1082
1083 case FS_FAT32 : /* DWORD aligned items */
1084 #if _FS_EXFAT
1085 case FS_EXFAT :
1086 #endif
1087 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
1088 if (res != FR_OK) break;
1089 if (!_FS_EXFAT || fs->fs_type != FS_EXFAT) {
1090 val = (val & 0x0FFFFFFF) | (ld_dword(&fs->win[clst * 4 % SS(fs)]) & 0xF0000000);
1091 }
1092 st_dword(&fs->win[clst * 4 % SS(fs)], val);
1093 fs->wflag = 1;
1094 break;
1095 }
1096 }
1097 return res;
1098 }
1099 #endif /* !_FS_READONLY */
1100
1101
1102
1103
1104 #if _FS_EXFAT && !_FS_READONLY
1105 /*-----------------------------------------------------------------------*/
1106 /* exFAT: Accessing FAT and Allocation Bitmap */
1107 /*-----------------------------------------------------------------------*/
1108
1109 /*---------------------------------------------*/
1110 /* exFAT: Find a contiguous free cluster block */
1111 /*---------------------------------------------*/
1112 static
1113 DWORD find_bitmap ( /* 0:No free cluster, 2..:Free cluster found, 0xFFFFFFFF:Disk error */
1114 FATFS* fs, /* File system object */
1115 DWORD clst, /* Cluster number to scan from */
1116 DWORD ncl /* Number of contiguous clusters to find (1..) */
1117 )
1118 {
1119 BYTE bm, bv;
1120 UINT i;
1121 DWORD val, scl, ctr;
1122
1123
1124 clst -= 2; /* The first bit in the bitmap corresponds to cluster #2 */
1125 if (clst >= fs->n_fatent - 2) clst = 0;
1126 scl = val = clst; ctr = 0;
1127 for (;;) {
1128 if (move_window(fs, fs->database + val / 8 / SS(fs)) != FR_OK) return 0xFFFFFFFF;
1129 i = val / 8 & (SS(fs) - 1); bm = 1 << (val % 8);
1130 do {
1131 do {
1132 bv = fs->win[i] & bm; bm <<= 1; /* Get bit value */
1133 if (++val >= fs->n_fatent - 2) { /* Next cluster (with wrap-around) */
1134 val = 0; bm = 0; i = 4096;
1135 }
1136 if (!bv) { /* Is it a free cluster? */
1137 if (++ctr == ncl) return scl + 2; /* Check run length */
1138 } else {
1139 scl = val; ctr = 0; /* Encountered a live cluster, restart to scan */
1140 }
1141 if (val == clst) return 0; /* All cluster scanned? */
1142 } while (bm);
1143 bm = 1;
1144 } while (++i < SS(fs));
1145 }
1146 }
1147
1148 /*------------------------------------*/
1149 /* exFAT: Set/Clear a block of bitmap */
1150 /*------------------------------------*/
1151 static
1152 FRESULT change_bitmap (
1153 FATFS* fs, /* File system object */
1154 DWORD clst, /* Cluster number to change from */
1155 DWORD ncl, /* Number of clusters to be changed */
1156 int bv /* bit value to be set (0 or 1) */
1157 )
1158 {
1159 BYTE bm;
1160 UINT i;
1161 DWORD sect;
1162
1163
1164 clst -= 2; /* The first bit corresponds to cluster #2 */
1165 sect = fs->database + clst / 8 / SS(fs); /* Sector address */
1166 i = clst / 8 & (SS(fs) - 1); /* Byte offset in the sector */
1167 bm = 1 << (clst % 8); /* Bit mask in the byte */
1168 for (;;) {
1169 if (move_window(fs, sect++) != FR_OK) return FR_DISK_ERR;
1170 do {
1171 do {
1172 if (bv == (int)((fs->win[i] & bm) != 0)) return FR_INT_ERR; /* Is the bit expected value? */
1173 fs->win[i] ^= bm; /* Flip the bit */
1174 fs->wflag = 1;
1175 if (--ncl == 0) return FR_OK; /* All bits processed? */
1176 } while (bm <<= 1); /* Next bit */
1177 bm = 1;
1178 } while (++i < SS(fs)); /* Next byte */
1179 }
1180 }
1181
1182
1183 /*---------------------------------------------*/
1184 /* Complement contiguous part of the FAT chain */
1185 /*---------------------------------------------*/
1186 static
1187 FRESULT fill_fat_chain (
1188 _FDID* obj /* Pointer to the corresponding object */
1189 )
1190 {
1191 FRESULT res;
1192 DWORD cl, n;
1193
1194 if (obj->stat == 3) { /* Has the object got fragmented? */
1195 for (cl = obj->sclust, n = obj->n_cont; n; cl++, n--) { /* Create cluster chain on the FAT */
1196 res = put_fat(obj->fs, cl, cl + 1);
1197 if (res != FR_OK) return res;
1198 }
1199 obj->stat = 0; /* Change status 'FAT chain is valid' */
1200 }
1201 return FR_OK;
1202 }
1203
1204 #endif
1205
1206
1207
1208 /*-----------------------------------------------------------------------*/
1209 /* FAT handling - Remove a cluster chain */
1210 /*-----------------------------------------------------------------------*/
1211 #if !_FS_READONLY
1212 static
1213 FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */
1214 _FDID* obj, /* Corresponding object */
1215 DWORD clst, /* Cluster to remove a chain from */
1216 DWORD pclst /* Previous cluster of clst (0:an entire chain) */
1217 )
1218 {
1219 FRESULT res = FR_OK;
1220 DWORD nxt;
1221 FATFS *fs = obj->fs;
1222 #if _FS_EXFAT || _USE_TRIM
1223 DWORD scl = clst, ecl = clst;
1224 #endif
1225 #if _USE_TRIM
1226 DWORD rt[2];
1227 #endif
1228
1229 if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Check if in valid range */
1230
1231 /* Mark the previous cluster 'EOC' on the FAT if it exists */
1232 if (pclst && (!_FS_EXFAT || fs->fs_type != FS_EXFAT || obj->stat != 2)) {
1233 res = put_fat(fs, pclst, 0xFFFFFFFF);
1234 if (res != FR_OK) return res;
1235 }
1236
1237 /* Remove the chain */
1238 do {
1239 nxt = get_fat(obj, clst); /* Get cluster status */
1240 if (nxt == 0) break; /* Empty cluster? */
1241 if (nxt == 1) return FR_INT_ERR; /* Internal error? */
1242 if (nxt == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error? */
1243 if (!_FS_EXFAT || fs->fs_type != FS_EXFAT) {
1244 res = put_fat(fs, clst, 0); /* Mark the cluster 'free' on the FAT */
1245 if (res != FR_OK) return res;
1246 }
1247 if (fs->free_clst != 0xFFFFFFFF) { /* Update FSINFO */
1248 fs->free_clst++;
1249 fs->fsi_flag |= 1;
1250 }
1251 #if _FS_EXFAT || _USE_TRIM
1252 if (ecl + 1 == nxt) { /* Is next cluster contiguous? */
1253 ecl = nxt;
1254 } else { /* End of contiguous cluster block */
1255 #if _FS_EXFAT
1256 if (fs->fs_type == FS_EXFAT) {
1257 res = change_bitmap(fs, scl, ecl - scl + 1, 0); /* Mark the cluster block 'free' on the bitmap */
1258 if (res != FR_OK) return res;
1259 }
1260 #endif
1261 #if _USE_TRIM
1262 rt[0] = clust2sect(fs, scl); /* Start sector */
1263 rt[1] = clust2sect(fs, ecl) + fs->csize - 1; /* End sector */
1264 disk_ioctl(fs->drv, CTRL_TRIM, rt); /* Inform device the block can be erased */
1265 #endif
1266 scl = ecl = nxt;
1267 }
1268 #endif
1269 clst = nxt; /* Next cluster */
1270 } while (clst < fs->n_fatent); /* Repeat while not the last link */
1271
1272 #if _FS_EXFAT
1273 if (fs->fs_type == FS_EXFAT) {
1274 if (pclst == 0) { /* Does object have no chain? */
1275 obj->stat = 0; /* Change the object status 'initial' */
1276 } else {
1277 if (obj->stat == 3 && pclst >= obj->sclust && pclst <= obj->sclust + obj->n_cont) { /* Did the chain got contiguous? */
1278 obj->stat = 2; /* Change the object status 'contiguous' */
1279 }
1280 }
1281 }
1282 #endif
1283 return FR_OK;
1284 }
1285 #endif
1286
1287
1288
1289
1290 /*-----------------------------------------------------------------------*/
1291 /* FAT handling - Stretch a chain or Create a new chain */
1292 /*-----------------------------------------------------------------------*/
1293 #if !_FS_READONLY
1294 static
1295 DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */
1296 _FDID* obj, /* Corresponding object */
1297 DWORD clst /* Cluster# to stretch, 0:Create a new chain */
1298 )
1299 {
1300 DWORD cs, ncl, scl;
1301 FRESULT res;
1302 FATFS *fs = obj->fs;
1303
1304
1305 if (clst == 0) { /* Create a new chain */
1306 scl = fs->last_clst; /* Get suggested cluster to start at */
1307 if (scl == 0 || scl >= fs->n_fatent) scl = 1;
1308 }
1309 else { /* Stretch current chain */
1310 cs = get_fat(obj, clst); /* Check the cluster status */
1311 if (cs < 2) return 1; /* Invalid value */
1312 if (cs == 0xFFFFFFFF) return cs; /* A disk error occurred */
1313 if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */
1314 scl = clst;
1315 }
1316
1317 #if _FS_EXFAT
1318 if (fs->fs_type == FS_EXFAT) { /* At the exFAT */
1319 ncl = find_bitmap(fs, scl, 1); /* Find a free cluster */
1320 if (ncl == 0 || ncl == 0xFFFFFFFF) return ncl; /* No free cluster or hard error? */
1321 res = change_bitmap(fs, ncl, 1, 1); /* Mark the cluster 'in use' */
1322 if (res == FR_INT_ERR) return 1;
1323 if (res == FR_DISK_ERR) return 0xFFFFFFFF;
1324 if (clst == 0) { /* Is it a new chain? */
1325 obj->stat = 2; /* Set status 'contiguous chain' */
1326 } else { /* This is a stretched chain */
1327 if (obj->stat == 2 && ncl != scl + 1) { /* Is the chain got fragmented? */
1328 obj->n_cont = scl - obj->sclust; /* Set size of the contiguous part */
1329 obj->stat = 3; /* Change status 'just fragmented' */
1330 }
1331 }
1332 } else
1333 #endif
1334 { /* At the FAT12/16/32 */
1335 ncl = scl; /* Start cluster */
1336 for (;;) {
1337 ncl++; /* Next cluster */
1338 if (ncl >= fs->n_fatent) { /* Check wrap-around */
1339 ncl = 2;
1340 if (ncl > scl) return 0; /* No free cluster */
1341 }
1342 cs = get_fat(obj, ncl); /* Get the cluster status */
1343 if (cs == 0) break; /* Found a free cluster */
1344 if (cs == 1 || cs == 0xFFFFFFFF) return cs; /* An error occurred */
1345 if (ncl == scl) return 0; /* No free cluster */
1346 }
1347 }
1348
1349 if (_FS_EXFAT && fs->fs_type == FS_EXFAT && obj->stat == 2) { /* Is it a contiguous chain? */
1350 res = FR_OK; /* FAT does not need to be written */
1351 } else {
1352 res = put_fat(fs, ncl, 0xFFFFFFFF); /* Mark the new cluster 'EOC' */
1353 if (res == FR_OK && clst) {
1354 res = put_fat(fs, clst, ncl); /* Link it from the previous one if needed */
1355 }
1356 }
1357
1358 if (res == FR_OK) { /* Update FSINFO if function succeeded. */
1359 fs->last_clst = ncl;
1360 if (fs->free_clst < fs->n_fatent - 2) fs->free_clst--;
1361 fs->fsi_flag |= 1;
1362 } else {
1363 ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1; /* Failed. Create error status */
1364 }
1365
1366 return ncl; /* Return new cluster number or error status */
1367 }
1368 #endif /* !_FS_READONLY */
1369
1370
1371
1372
1373 /*-----------------------------------------------------------------------*/
1374 /* FAT handling - Convert offset into cluster with link map table */
1375 /*-----------------------------------------------------------------------*/
1376
1377 #if _USE_FASTSEEK
1378 static
1379 DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */
1380 FIL* fp, /* Pointer to the file object */
1381 DWORD ofs /* File offset to be converted to cluster# */
1382 )
1383 {
1384 DWORD cl, ncl, *tbl;
1385 FATFS *fs = fp->obj.fs;
1386
1387
1388 tbl = fp->cltbl + 1; /* Top of CLMT */
1389 cl = ofs / SS(fs) / fs->csize; /* Cluster order from top of the file */
1390 for (;;) {
1391 ncl = *tbl++; /* Number of cluters in the fragment */
1392 if (ncl == 0) return 0; /* End of table? (error) */
1393 if (cl < ncl) break; /* In this fragment? */
1394 cl -= ncl; tbl++; /* Next fragment */
1395 }
1396 return cl + *tbl; /* Return the cluster number */
1397 }
1398 #endif /* _USE_FASTSEEK */
1399
1400
1401
1402 /*-----------------------------------------------------------------------*/
1403 /* Directory handling - Set directory index */
1404 /*-----------------------------------------------------------------------*/
1405
1406 static
1407 FRESULT dir_sdi ( /* FR_OK(0):succeeded, !=0:error */
1408 DIR* dp, /* Pointer to directory object */
1409 DWORD ofs /* Offset of directory table */
1410 )
1411 {
1412 DWORD csz, clst;
1413 FATFS *fs = dp->obj.fs;
1414
1415
1416 if (ofs >= (DWORD)((_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR) || ofs % SZDIRE) { /* Check range of offset and alignment */
1417 return FR_INT_ERR;
1418 }
1419 dp->dptr = ofs; /* Set current offset */
1420 clst = dp->obj.sclust; /* Table start cluster (0:root) */
1421 if (clst == 0 && fs->fs_type >= FS_FAT32) { /* Replace cluster# 0 with root cluster# */
1422 clst = fs->dirbase;
1423 if (_FS_EXFAT) dp->obj.stat = 0; /* exFAT: Root dir has an FAT chain */
1424 }
1425
1426 if (clst == 0) { /* Static table (root-directory in FAT12/16) */
1427 if (ofs / SZDIRE >= fs->n_rootdir) return FR_INT_ERR; /* Is index out of range? */
1428 dp->sect = fs->dirbase;
1429
1430 } else { /* Dynamic table (sub-directory or root-directory in FAT32+) */
1431 csz = (DWORD)fs->csize * SS(fs); /* Bytes per cluster */
1432 while (ofs >= csz) { /* Follow cluster chain */
1433 clst = get_fat(&dp->obj, clst); /* Get next cluster */
1434 if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
1435 if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Reached to end of table or internal error */
1436 ofs -= csz;
1437 }
1438 dp->sect = clust2sect(fs, clst);
1439 }
1440 dp->clust = clst; /* Current cluster# */
1441 if (!dp->sect) return FR_INT_ERR;
1442 dp->sect += ofs / SS(fs); /* Sector# of the directory entry */
1443 dp->dir = fs->win + (ofs % SS(fs)); /* Pointer to the entry in the win[] */
1444
1445 return FR_OK;
1446 }
1447
1448
1449
1450
1451 /*-----------------------------------------------------------------------*/
1452 /* Directory handling - Move directory table index next */
1453 /*-----------------------------------------------------------------------*/
1454
1455 static
1456 FRESULT dir_next ( /* FR_OK(0):succeeded, FR_NO_FILE:End of table, FR_DENIED:Could not stretch */
1457 DIR* dp, /* Pointer to the directory object */
1458 int stretch /* 0: Do not stretch table, 1: Stretch table if needed */
1459 )
1460 {
1461 DWORD ofs, clst;
1462 FATFS *fs = dp->obj.fs;
1463 #if !_FS_READONLY
1464 UINT n;
1465 #endif
1466
1467 ofs = dp->dptr + SZDIRE; /* Next entry */
1468 if (!dp->sect || ofs >= (DWORD)((_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR)) return FR_NO_FILE; /* Report EOT when offset has reached max value */
1469
1470 if (ofs % SS(fs) == 0) { /* Sector changed? */
1471 dp->sect++; /* Next sector */
1472
1473 if (!dp->clust) { /* Static table */
1474 if (ofs / SZDIRE >= fs->n_rootdir) { /* Report EOT if it reached end of static table */
1475 dp->sect = 0; return FR_NO_FILE;
1476 }
1477 }
1478 else { /* Dynamic table */
1479 if ((ofs / SS(fs) & (fs->csize - 1)) == 0) { /* Cluster changed? */
1480 clst = get_fat(&dp->obj, dp->clust); /* Get next cluster */
1481 if (clst <= 1) return FR_INT_ERR; /* Internal error */
1482 if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
1483 if (clst >= fs->n_fatent) { /* Reached end of dynamic table */
1484 #if !_FS_READONLY
1485 if (!stretch) { /* If no stretch, report EOT */
1486 dp->sect = 0; return FR_NO_FILE;
1487 }
1488 clst = create_chain(&dp->obj, dp->clust); /* Allocate a cluster */
1489 if (clst == 0) return FR_DENIED; /* No free cluster */
1490 if (clst == 1) return FR_INT_ERR; /* Internal error */
1491 if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
1492 /* Clean-up the stretched table */
1493 if (_FS_EXFAT) dp->obj.stat |= 4; /* The directory needs to be updated */
1494 if (sync_window(fs) != FR_OK) return FR_DISK_ERR; /* Flush disk access window */
1495 mem_set(fs->win, 0, SS(fs)); /* Clear window buffer */
1496 for (n = 0, fs->winsect = clust2sect(fs, clst); n < fs->csize; n++, fs->winsect++) { /* Fill the new cluster with 0 */
1497 fs->wflag = 1;
1498 if (sync_window(fs) != FR_OK) return FR_DISK_ERR;
1499 }
1500 fs->winsect -= n; /* Restore window offset */
1501 #else
1502 if (!stretch) dp->sect = 0; /* If no stretch, report EOT (this is to suppress warning) */
1503 dp->sect = 0; return FR_NO_FILE; /* Report EOT */
1504 #endif
1505 }
1506 dp->clust = clst; /* Initialize data for new cluster */
1507 dp->sect = clust2sect(fs, clst);
1508 }
1509 }
1510 }
1511 dp->dptr = ofs; /* Current entry */
1512 dp->dir = &fs->win[ofs % SS(fs)]; /* Pointer to the entry in the win[] */
1513
1514 return FR_OK;
1515 }
1516
1517
1518
1519
1520 /*-----------------------------------------------------------------------*/
1521 /* Directory handling - Reserve a block of directory entries */
1522 /*-----------------------------------------------------------------------*/
1523
1524 #if !_FS_READONLY
1525 static
1526 FRESULT dir_alloc ( /* FR_OK(0):succeeded, !=0:error */
1527 DIR* dp, /* Pointer to the directory object */
1528 UINT nent /* Number of contiguous entries to allocate */
1529 )
1530 {
1531 FRESULT res;
1532 UINT n;
1533 FATFS *fs = dp->obj.fs;
1534
1535
1536 res = dir_sdi(dp, 0);
1537 if (res == FR_OK) {
1538 n = 0;
1539 do {
1540 res = move_window(fs, dp->sect);
1541 if (res != FR_OK) break;
1542 #if _FS_EXFAT
1543 if (fs->fs_type == FS_EXFAT ? (int)((dp->dir[XDIR_Type] & 0x80) == 0) : (int)(dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0)) {
1544 #else
1545 if (dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0) {
1546 #endif
1547 if (++n == nent) break; /* A block of contiguous free entries is found */
1548 } else {
1549 n = 0; /* Not a blank entry. Restart to search */
1550 }
1551 res = dir_next(dp, 1);
1552 } while (res == FR_OK); /* Next entry with table stretch enabled */
1553 }
1554
1555 if (res == FR_NO_FILE) res = FR_DENIED; /* No directory entry to allocate */
1556 return res;
1557 }
1558 #endif
1559
1560
1561
1562
1563 /*-----------------------------------------------------------------------*/
1564 /* FAT: Directory handling - Load/Store start cluster number */
1565 /*-----------------------------------------------------------------------*/
1566
1567 static
1568 DWORD ld_clust ( /* Returns the top cluster value of the SFN entry */
1569 FATFS* fs, /* Pointer to the fs object */
1570 const BYTE* dir /* Pointer to the key entry */
1571 )
1572 {
1573 DWORD cl;
1574
1575 cl = ld_word(dir + DIR_FstClusLO);
1576 if (fs->fs_type == FS_FAT32) {
1577 cl |= (DWORD)ld_word(dir + DIR_FstClusHI) << 16;
1578 }
1579
1580 return cl;
1581 }
1582
1583
1584 #if !_FS_READONLY
1585 static
1586 void st_clust (
1587 FATFS* fs, /* Pointer to the fs object */
1588 BYTE* dir, /* Pointer to the key entry */
1589 DWORD cl /* Value to be set */
1590 )
1591 {
1592 st_word(dir + DIR_FstClusLO, (WORD)cl);
1593 if (fs->fs_type == FS_FAT32) {
1594 st_word(dir + DIR_FstClusHI, (WORD)(cl >> 16));
1595 }
1596 }
1597 #endif
1598
1599
1600
1601
1602 /*------------------------------------------------------------------------*/
1603 /* FAT-LFN: LFN handling */
1604 /*------------------------------------------------------------------------*/
1605 #if _USE_LFN != 0
1606 static
1607 const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN characters in the directory entry */
1608
1609 /*--------------------------------------------------------*/
1610 /* FAT-LFN: Compare a part of file name with an LFN entry */
1611 /*--------------------------------------------------------*/
1612 static
1613 int cmp_lfn ( /* 1:matched, 0:not matched */
1614 const WCHAR* lfnbuf, /* Pointer to the LFN working buffer to be compared */
1615 BYTE* dir /* Pointer to the directory entry containing the part of LFN */
1616 )
1617 {
1618 UINT i, s;
1619 WCHAR wc, uc;
1620
1621
1622 if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO */
1623
1624 i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
1625
1626 for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */
1627 uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */
1628 if (wc) {
1629 if (i >= _MAX_LFN || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) { /* Compare it */
1630 return 0; /* Not matched */
1631 }
1632 wc = uc;
1633 } else {
1634 if (uc != 0xFFFF) return 0; /* Check filler */
1635 }
1636 }
1637
1638 if ((dir[LDIR_Ord] & LLEF) && wc && lfnbuf[i]) return 0; /* Last segment matched but different length */
1639
1640 return 1; /* The part of LFN matched */
1641 }
1642
1643
1644
1645 #if _FS_MINIMIZE <= 1 || _FS_EXFAT
1646 /*-----------------------------------------------------*/
1647 /* FAT-LFN: Pick a part of file name from an LFN entry */
1648 /*-----------------------------------------------------*/
1649 static
1650 int pick_lfn ( /* 1:succeeded, 0:buffer overflow or invalid LFN entry */
1651 WCHAR* lfnbuf, /* Pointer to the LFN working buffer */
1652 BYTE* dir /* Pointer to the LFN entry */
1653 )
1654 {
1655 UINT i, s;
1656 WCHAR wc, uc;
1657
1658
1659 if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO */
1660
1661 i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
1662
1663 for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */
1664 uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */
1665 if (wc) {
1666 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
1667 lfnbuf[i++] = wc = uc; /* Store it */
1668 } else {
1669 if (uc != 0xFFFF) return 0; /* Check filler */
1670 }
1671 }
1672
1673 if (dir[LDIR_Ord] & LLEF) { /* Put terminator if it is the last LFN part */
1674 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
1675 lfnbuf[i] = 0;
1676 }
1677
1678 return 1; /* The part of LFN is valid */
1679 }
1680 #endif
1681
1682 #if !_FS_READONLY
1683 /*-----------------------------------------*/
1684 /* FAT-LFN: Create an entry of LFN entries */
1685 /*-----------------------------------------*/
1686 static
1687 void put_lfn (
1688 const WCHAR* lfn, /* Pointer to the LFN */
1689 BYTE* dir, /* Pointer to the LFN entry to be created */
1690 BYTE ord, /* LFN order (1-20) */
1691 BYTE sum /* Checksum of the corresponding SFN */
1692 )
1693 {
1694 UINT i, s;
1695 WCHAR wc;
1696
1697
1698 dir[LDIR_Chksum] = sum; /* Set checksum */
1699 dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */
1700 dir[LDIR_Type] = 0;
1701 st_word(dir + LDIR_FstClusLO, 0);
1702
1703 i = (ord - 1) * 13; /* Get offset in the LFN working buffer */
1704 s = wc = 0;
1705 do {
1706 if (wc != 0xFFFF) wc = lfn[i++]; /* Get an effective character */
1707 st_word(dir + LfnOfs[s], wc); /* Put it */
1708 if (wc == 0) wc = 0xFFFF; /* Padding characters for left locations */
1709 } while (++s < 13);
1710 if (wc == 0xFFFF || !lfn[i]) ord |= LLEF; /* Last LFN part is the start of LFN sequence */
1711 dir[LDIR_Ord] = ord; /* Set the LFN order */
1712 }
1713
1714 #endif
1715 #endif
1716
1717
1718
1719 /*-----------------------------------------------------------------------*/
1720 /* FAT-LFN: Create a Numbered SFN */
1721 /*-----------------------------------------------------------------------*/
1722 #if _USE_LFN != 0 && !_FS_READONLY
1723 static
1724 void gen_numname (
1725 BYTE* dst, /* Pointer to the buffer to store numbered SFN */
1726 const BYTE* src, /* Pointer to SFN */
1727 const WCHAR* lfn, /* Pointer to LFN */
1728 UINT seq /* Sequence number */
1729 )
1730 {
1731 BYTE ns[8], c;
1732 UINT i, j;
1733 WCHAR wc;
1734 DWORD sr;
1735
1736
1737 mem_cpy(dst, src, 11);
1738
1739 if (seq > 5) { /* In case of many collisions, generate a hash number instead of sequential number */
1740 sr = seq;
1741 while (*lfn) { /* Create a CRC */
1742 wc = *lfn++;
1743 for (i = 0; i < 16; i++) {
1744 sr = (sr << 1) + (wc & 1);
1745 wc >>= 1;
1746 if (sr & 0x10000) sr ^= 0x11021;
1747 }
1748 }
1749 seq = (UINT)sr;
1750 }
1751
1752 /* itoa (hexdecimal) */
1753 i = 7;
1754 do {
1755 c = (seq % 16) + '0';
1756 if (c > '9') c += 7;
1757 ns[i--] = c;
1758 seq /= 16;
1759 } while (seq);
1760 ns[i] = '~';
1761
1762 /* Append the number */
1763 for (j = 0; j < i && dst[j] != ' '; j++) {
1764 if (IsDBCS1(dst[j])) {
1765 if (j == i - 1) break;
1766 j++;
1767 }
1768 }
1769 do {
1770 dst[j++] = (i < 8) ? ns[i++] : ' ';
1771 } while (j < 8);
1772 }
1773 #endif
1774
1775
1776
1777 /*-----------------------------------------------------------------------*/
1778 /* FAT-LFN: Calculate checksum of an SFN entry */
1779 /*-----------------------------------------------------------------------*/
1780 #if _USE_LFN != 0
1781 static
1782 BYTE sum_sfn (
1783 const BYTE* dir /* Pointer to the SFN entry */
1784 )
1785 {
1786 BYTE sum = 0;
1787 UINT n = 11;
1788
1789 do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);
1790 return sum;
1791 }
1792 #endif
1793
1794
1795
1796
1797 #if _FS_EXFAT
1798 /*-----------------------------------------------------------------------*/
1799 /* exFAT: Directory handling - Load/Store a block of directory entries */
1800 /*-----------------------------------------------------------------------*/
1801
1802 static
1803 WORD xdir_sum ( /* Get checksum of the directoly block */
1804 const BYTE* dir /* Directory entry block to be calculated */
1805 )
1806 {
1807 UINT i, szblk;
1808 WORD sum;
1809
1810
1811 szblk = (dir[XDIR_NumSec] + 1) * SZDIRE;
1812 for (i = sum = 0; i < szblk; i++) {
1813 if (i == XDIR_SetSum) { /* Skip sum field */
1814 i++;
1815 } else {
1816 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + dir[i];
1817 }
1818 }
1819 return sum;
1820 }
1821
1822
1823
1824 static
1825 WORD xname_sum ( /* Get check sum (to be used as hash) of the name */
1826 const WCHAR* name /* File name to be calculated */
1827 )
1828 {
1829 WCHAR chr;
1830 WORD sum = 0;
1831
1832
1833 while ((chr = *name++) != 0) {
1834 chr = ff_wtoupper(chr); /* File name needs to be ignored case */
1835 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr & 0xFF);
1836 sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr >> 8);
1837 }
1838 return sum;
1839 }
1840
1841
1842 /*------------------------------------------------------*/
1843 /* exFAT: Get object information from a directory block */
1844 /*------------------------------------------------------*/
1845 static
1846 void get_xdir_info (
1847 BYTE* dirb, /* Pointer to the direcotry entry block 85+C0+C1s */
1848 FILINFO* fno /* Buffer to store the extracted file information */
1849 )
1850 {
1851 UINT di, si, nc;
1852 WCHAR w;
1853
1854 /* Get file name */
1855 #if _LFN_UNICODE
1856 if (dirb[XDIR_NumName] <= _MAX_LFN) {
1857 for (si = SZDIRE * 2, di = 0; di < dirb[XDIR_NumName]; si += 2, di++) {
1858 if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */
1859 w = ld_word(dirb + si); /* Get a character */
1860 fno->fname[di] = w; /* Store it */
1861 }
1862 } else {
1863 di = 0; /* Buffer overflow and inaccessible object */
1864 }
1865 #else
1866 for (si = SZDIRE * 2, di = nc = 0; nc < dirb[XDIR_NumName]; si += 2, nc++) {
1867 if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */
1868 w = ld_word(dirb + si); /* Get a character */
1869 w = ff_convert(w, 0); /* Unicode -> OEM */
1870 if (w == 0) { di = 0; break; } /* Could not be converted and inaccessible object */
1871 if (_DF1S && w >= 0x100) { /* Put 1st byte if it is a DBC (always false at SBCS cfg) */
1872 fno->fname[di++] = (char)(w >> 8);
1873 }
1874 if (di >= _MAX_LFN) { di = 0; break; } /* Buffer overflow and inaccessible object */
1875 fno->fname[di++] = (char)w;
1876 }
1877 #endif
1878 if (di == 0) fno->fname[di++] = '?'; /* Inaccessible object? */
1879 fno->fname[di] = 0; /* Terminate file name */
1880
1881 fno->altname[0] = 0; /* No SFN */
1882 fno->fattrib = dirb[XDIR_Attr]; /* Attribute */
1883 fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_qword(dirb + XDIR_FileSize); /* Size */
1884 fno->ftime = ld_word(dirb + XDIR_ModTime + 0); /* Time */
1885 fno->fdate = ld_word(dirb + XDIR_ModTime + 2); /* Date */
1886 }
1887
1888
1889 /*-----------------------------------*/
1890 /* exFAT: Get a directry entry block */
1891 /*-----------------------------------*/
1892 static
1893 FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */
1894 DIR* dp /* Pointer to the reading direcotry object pointing the 85 entry */
1895 )
1896 {
1897 FRESULT res;
1898 UINT i, nent;
1899 BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the on-memory direcotry entry block 85+C0+C1s */
1900
1901
1902 /* Load 85 entry */
1903 res = move_window(dp->obj.fs, dp->sect);
1904 if (res != FR_OK) return res;
1905 if (dp->dir[XDIR_Type] != 0x85) return FR_INT_ERR;
1906 mem_cpy(&dirb[0], dp->dir, SZDIRE);
1907 nent = dirb[XDIR_NumSec] + 1;
1908
1909 /* Load C0 entry */
1910 res = dir_next(dp, 0);
1911 if (res != FR_OK) return res;
1912 res = move_window(dp->obj.fs, dp->sect);
1913 if (res != FR_OK) return res;
1914 if (dp->dir[XDIR_Type] != 0xC0) return FR_INT_ERR;
1915 mem_cpy(dirb + SZDIRE, dp->dir, SZDIRE);
1916
1917 /* Load C1 entries */
1918 if (nent < 3 || nent > 19) return FR_NO_FILE;
1919 i = SZDIRE * 2; nent *= SZDIRE;
1920 do {
1921 res = dir_next(dp, 0);
1922 if (res != FR_OK) return res;
1923 res = move_window(dp->obj.fs, dp->sect);
1924 if (res != FR_OK) return res;
1925 if (dp->dir[XDIR_Type] != 0xC1) return FR_INT_ERR;
1926 mem_cpy(dirb + i, dp->dir, SZDIRE);
1927 i += SZDIRE;
1928 } while (i < nent);
1929
1930 /* Sanity check */
1931 if (xdir_sum(dirb) != ld_word(dirb + XDIR_SetSum)) return FR_INT_ERR;
1932
1933 return FR_OK;
1934 }
1935
1936
1937 #if !_FS_READONLY || _FS_RPATH != 0
1938 /*------------------------------------------------*/
1939 /* exFAT: Load the object's directory entry block */
1940 /*------------------------------------------------*/
1941 static
1942 FRESULT load_obj_dir (
1943 DIR* dp, /* Blank directory object to be used to access containing direcotry */
1944 const _FDID* obj /* Object with containing directory information */
1945 )
1946 {
1947 FRESULT res;
1948
1949
1950 /* Open object containing directory */
1951 dp->obj.fs = obj->fs;
1952 dp->obj.sclust = obj->c_scl;
1953 dp->obj.stat = (BYTE)obj->c_size;
1954 dp->obj.objsize = obj->c_size & 0xFFFFFF00;
1955 dp->blk_ofs = obj->c_ofs;
1956
1957 res = dir_sdi(dp, dp->blk_ofs); /* Goto the block location */
1958 if (res == FR_OK) {
1959 res = load_xdir(dp); /* Load the object's entry block */
1960 }
1961 return res;
1962 }
1963 #endif
1964
1965
1966 #if !_FS_READONLY
1967 /*-----------------------------------------------*/
1968 /* exFAT: Store the directory block to the media */
1969 /*-----------------------------------------------*/
1970 static
1971 FRESULT store_xdir (
1972 DIR* dp /* Pointer to the direcotry object */
1973 )
1974 {
1975 FRESULT res;
1976 UINT nent;
1977 WORD sum;
1978 BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the direcotry entry block 85+C0+C1s */
1979
1980 /* Create set sum */
1981 sum = xdir_sum(dirb);
1982 st_word(dirb + XDIR_SetSum, sum);
1983 nent = dirb[XDIR_NumSec] + 1;
1984
1985 res = dir_sdi(dp, dp->blk_ofs);
1986 while (res == FR_OK && (res = move_window(dp->obj.fs, dp->sect)) == FR_OK) {
1987 mem_cpy(dp->dir, dirb, SZDIRE);
1988 dp->obj.fs->wflag = 1;
1989 if (--nent == 0) break;
1990 dirb += SZDIRE;
1991 res = dir_next(dp, 0);
1992 }
1993 return (res == FR_OK || res == FR_DISK_ERR) ? res : FR_INT_ERR;
1994 }
1995
1996
1997 /*-------------------------------------------*/
1998 /* exFAT: Create a new directory enrty block */
1999 /*-------------------------------------------*/
2000 static
2001 void create_xdir (
2002 BYTE* dirb, /* Pointer to the direcotry entry block buffer */
2003 const WCHAR* lfn /* Pointer to the nul terminated file name */
2004 )
2005 {
2006 UINT i;
2007 BYTE nb, nc;
2008 WCHAR chr;
2009 WORD hash;
2010
2011
2012 mem_set(dirb, 0, 2 * SZDIRE); /* Initialize 85+C0 entry */
2013 dirb[XDIR_Type] = 0x85;
2014 dirb[XDIR_Type + SZDIRE] = 0xC0;
2015 hash = xname_sum(lfn);
2016 st_word(dirb + XDIR_NameHash, hash); /* Set name hash */
2017
2018 i = SZDIRE * 2; /* C1 offset */
2019 nc = 0; nb = 1; chr = 1;
2020 do {
2021 dirb[i++] = 0xC1; dirb[i++] = 0; /* Entry type C1 */
2022 do { /* Fill name field */
2023 if (chr && (chr = lfn[nc]) != 0) nc++; /* Get a character if exist */
2024 st_word(dirb + i, chr); i += 2; /* Store it */
2025 } while (i % SZDIRE);
2026 nb++;
2027 } while (lfn[nc]); /* Fill next entry if any char follows */
2028
2029 dirb[XDIR_NumName] = nc; /* Set name length */
2030 dirb[XDIR_NumSec] = nb; /* Set number of C0+C1s */
2031 }
2032 #endif
2033 #endif
2034
2035
2036
2037 /*-----------------------------------------------------------------------*/
2038 /* Read an object from the directory */
2039 /*-----------------------------------------------------------------------*/
2040 #if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 || _USE_LABEL || _FS_EXFAT
2041 static
2042 FRESULT dir_read (
2043 DIR* dp, /* Pointer to the directory object */
2044 int vol /* Filtered by 0:file/directory or 1:volume label */
2045 )
2046 {
2047 FRESULT res = FR_NO_FILE;
2048 FATFS *fs = dp->obj.fs;
2049 BYTE a, c;
2050 #if _USE_LFN != 0
2051 BYTE ord = 0xFF, sum = 0xFF;
2052 #endif
2053
2054 while (dp->sect) {
2055 res = move_window(fs, dp->sect);
2056 if (res != FR_OK) break;
2057 c = dp->dir[DIR_Name]; /* Test for the entry type */
2058 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of the directory */
2059 #if _FS_EXFAT
2060 if (fs->fs_type == FS_EXFAT) { /* At the exFAT */
2061 if (_USE_LABEL && vol) {
2062 if (c == 0x83) break; /* Volume label entry? */
2063 } else {
2064 if (c == 0x85) { /* Start of the entry block? */
2065 dp->blk_ofs = dp->dptr; /* Set location of block */
2066 res = load_xdir(dp); /* Load the entry block */
2067 if (res == FR_OK) {
2068 dp->obj.attr = fs->dirbuf[XDIR_Attr] & AM_MASK; /* Get attribute */
2069 }
2070 break;
2071 }
2072 }
2073 } else
2074 #endif
2075 { /* At the FAT12/16/32 */
2076 dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK; /* Get attribute */
2077 #if _USE_LFN != 0 /* LFN configuration */
2078 if (c == DDEM || c == '.' || (int)((a & ~AM_ARC) == AM_VOL) != vol) { /* An entry without valid data */
2079 ord = 0xFF;
2080 } else {
2081 if (a == AM_LFN) { /* An LFN entry is found */
2082 if (c & LLEF) { /* Is it start of an LFN sequence? */
2083 sum = dp->dir[LDIR_Chksum];
2084 c &= ~LLEF; ord = c;
2085 dp->blk_ofs = dp->dptr;
2086 }
2087 /* Check LFN validity and capture it */
2088 ord = (c == ord && sum == dp->dir[LDIR_Chksum] && pick_lfn(dp->lfn, dp->dir)) ? ord - 1 : 0xFF;
2089 } else { /* An SFN entry is found */
2090 if (ord || sum != sum_sfn(dp->dir)) { /* Is there a valid LFN? */
2091 dp->blk_ofs = 0xFFFFFFFF; /* It has no LFN. */
2092 }
2093 break;
2094 }
2095 }
2096 #else /* Non LFN configuration */
2097 if (c != DDEM && c != '.' && a != AM_LFN && (int)((a & ~AM_ARC) == AM_VOL) == vol) { /* Is it a valid entry? */
2098 break;
2099 }
2100 #endif
2101 }
2102 res = dir_next(dp, 0); /* Next entry */
2103 if (res != FR_OK) break;
2104 }
2105
2106 if (res != FR_OK) dp->sect = 0; /* Terminate the read operation on error or EOT */
2107 return res;
2108 }
2109 #endif /* _FS_MINIMIZE <= 1 || _USE_LABEL || _FS_RPATH >= 2 */
2110
2111
2112
2113 /*-----------------------------------------------------------------------*/
2114 /* Directory handling - Find an object in the directory */
2115 /*-----------------------------------------------------------------------*/
2116
2117 static
2118 FRESULT dir_find ( /* FR_OK(0):succeeded, !=0:error */
2119 DIR* dp /* Pointer to the directory object with the file name */
2120 )
2121 {
2122 FRESULT res;
2123 FATFS *fs = dp->obj.fs;
2124 BYTE c;
2125 #if _USE_LFN != 0
2126 BYTE a, ord, sum;
2127 #endif
2128
2129 res = dir_sdi(dp, 0); /* Rewind directory object */
2130 if (res != FR_OK) return res;
2131 #if _FS_EXFAT
2132 if (fs->fs_type == FS_EXFAT) { /* At the exFAT */
2133 BYTE nc;
2134 UINT di, ni;
2135 WORD hash = xname_sum(dp->lfn); /* Hash value of the name to find */
2136
2137 while ((res = dir_read(dp, 0)) == FR_OK) { /* Read an item */
2138 if (ld_word(fs->dirbuf + XDIR_NameHash) != hash) continue; /* Skip the comparison if hash value mismatched */
2139 for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) { /* Compare the name */
2140 if ((di % SZDIRE) == 0) di += 2;
2141 if (ff_wtoupper(ld_word(fs->dirbuf + di)) != ff_wtoupper(dp->lfn[ni])) break;
2142 }
2143 if (nc == 0 && !dp->lfn[ni]) break; /* Name matched? */
2144 }
2145 return res;
2146 }
2147 #endif
2148 /* At the FAT12/16/32 */
2149 #if _USE_LFN != 0
2150 ord = sum = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */
2151 #endif
2152 do {
2153 res = move_window(fs, dp->sect);
2154 if (res != FR_OK) break;
2155 c = dp->dir[DIR_Name];
2156 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
2157 #if _USE_LFN != 0 /* LFN configuration */
2158 dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK;
2159 if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
2160 ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */
2161 } else {
2162 if (a == AM_LFN) { /* An LFN entry is found */
2163 if (dp->lfn) {
2164 if (c & LLEF) { /* Is it start of LFN sequence? */
2165 sum = dp->dir[LDIR_Chksum];
2166 c &= ~LLEF; ord = c; /* LFN start order */
2167 dp->blk_ofs = dp->dptr; /* Start offset of LFN */
2168 }
2169 /* Check validity of the LFN entry and compare it with given name */
2170 ord = (c == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(dp->lfn, dp->dir)) ? ord - 1 : 0xFF;
2171 }
2172 } else { /* An SFN entry is found */
2173 if (!ord && sum == sum_sfn(dp->dir)) break; /* LFN matched? */
2174 if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* SFN matched? */
2175 ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */
2176 }
2177 }
2178 #else /* Non LFN configuration */
2179 dp->obj.attr = dp->dir[DIR_Attr] & AM_MASK;
2180 if (!(dp->dir[DIR_Attr] & AM_VOL) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* Is it a valid entry? */
2181 #endif
2182 res = dir_next(dp, 0); /* Next entry */
2183 } while (res == FR_OK);
2184
2185 return res;
2186 }
2187
2188
2189
2190
2191 /*-----------------------------------------------------------------------*/
2192 /* Register an object to the directory */
2193 /*-----------------------------------------------------------------------*/
2194 #if !_FS_READONLY
2195 static
2196 FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too many SFN collision, FR_DISK_ERR:disk error */
2197 DIR* dp /* Target directory with object name to be created */
2198 )
2199 {
2200 FRESULT res;
2201 FATFS *fs = dp->obj.fs;
2202 #if _USE_LFN != 0 /* LFN configuration */
2203 UINT n, nlen, nent;
2204 BYTE sn[12], *fn, sum;
2205 WCHAR *lfn;
2206
2207
2208 fn = dp->fn; lfn = dp->lfn;
2209 if (fn[NSFLAG] & (NS_DOT | NS_NONAME)) return FR_INVALID_NAME; /* Check name validity */
2210 for (nlen = 0; lfn[nlen]; nlen++) ; /* Get lfn length */
2211
2212 #if _FS_EXFAT
2213 if (fs->fs_type == FS_EXFAT) { /* At the exFAT */
2214 DIR dj;
2215
2216 nent = (nlen + 14) / 15 + 2; /* Number of entries to allocate (85+C0+C1s) */
2217 res = dir_alloc(dp, nent); /* Allocate entries */
2218 if (res != FR_OK) return res;
2219 dp->blk_ofs = dp->dptr - SZDIRE * (nent - 1); /* Set block position */
2220
2221 if (dp->obj.stat & 4) { /* Has the sub-directory been stretched? */
2222 dp->obj.stat &= 3;
2223 dp->obj.objsize += (DWORD)fs->csize * SS(fs); /* Increase object size by cluster size */
2224 res = fill_fat_chain(&dp->obj); /* Complement FAT chain if needed */
2225 if (res != FR_OK) return res;
2226 res = load_obj_dir(&dj, &dp->obj);
2227 if (res != FR_OK) return res; /* Load the object status */
2228 st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize); /* Update the allocation status */
2229 st_qword(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize);
2230 fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1;
2231 res = store_xdir(&dj); /* Store the object status */
2232 if (res != FR_OK) return res;
2233 }
2234
2235 create_xdir(fs->dirbuf, lfn); /* Create on-memory directory block to be written later */
2236 return FR_OK;
2237 }
2238 #endif
2239 /* At the FAT12/16/32 */
2240 mem_cpy(sn, fn, 12);
2241 if (sn[NSFLAG] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */
2242 fn[NSFLAG] = 0; dp->lfn = 0; /* Find only SFN */
2243 for (n = 1; n < 100; n++) {
2244 gen_numname(fn, sn, lfn, n); /* Generate a numbered name */
2245 res = dir_find(dp); /* Check if the name collides with existing SFN */
2246 if (res != FR_OK) break;
2247 }
2248 if (n == 100) return FR_DENIED; /* Abort if too many collisions */
2249 if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */
2250 fn[NSFLAG] = sn[NSFLAG]; dp->lfn = lfn;
2251 }
2252
2253 /* Create an SFN with/without LFNs. */
2254 nent = (sn[NSFLAG] & NS_LFN) ? (nlen + 12) / 13 + 1 : 1; /* Number of entries to allocate */
2255 res = dir_alloc(dp, nent); /* Allocate entries */
2256 if (res == FR_OK && --nent) { /* Set LFN entry if needed */
2257 res = dir_sdi(dp, dp->dptr - nent * SZDIRE);
2258 if (res == FR_OK) {
2259 sum = sum_sfn(dp->fn); /* Checksum value of the SFN tied to the LFN */
2260 do { /* Store LFN entries in bottom first */
2261 res = move_window(fs, dp->sect);
2262 if (res != FR_OK) break;
2263 put_lfn(dp->lfn, dp->dir, (BYTE)nent, sum);
2264 fs->wflag = 1;
2265 res = dir_next(dp, 0); /* Next entry */
2266 } while (res == FR_OK && --nent);
2267 }
2268 }
2269
2270 #else /* Non LFN configuration */
2271 res = dir_alloc(dp, 1); /* Allocate an entry for SFN */
2272
2273 #endif
2274
2275 /* Set SFN entry */
2276 if (res == FR_OK) {
2277 res = move_window(fs, dp->sect);
2278 if (res == FR_OK) {
2279 mem_set(dp->dir, 0, SZDIRE); /* Clean the entry */
2280 mem_cpy(dp->dir + DIR_Name, dp->fn, 11); /* Put SFN */
2281 #if _USE_LFN != 0
2282 dp->dir[DIR_NTres] = dp->fn[NSFLAG] & (NS_BODY | NS_EXT); /* Put NT flag */
2283 #endif
2284 fs->wflag = 1;
2285 }
2286 }
2287
2288 return res;
2289 }
2290 #endif /* !_FS_READONLY */
2291
2292
2293
2294
2295 /*-----------------------------------------------------------------------*/
2296 /* Remove an object from the directory */
2297 /*-----------------------------------------------------------------------*/
2298 #if !_FS_READONLY && !_FS_MINIMIZE
2299 static
2300 FRESULT dir_remove ( /* FR_OK:Succeeded, FR_DISK_ERR:A disk error */
2301 DIR* dp /* Directory object pointing the entry to be removed */
2302 )
2303 {
2304 FRESULT res;
2305 FATFS *fs = dp->obj.fs;
2306 #if _USE_LFN != 0 /* LFN configuration */
2307 DWORD last = dp->dptr;
2308
2309 res = dp->blk_ofs == 0xFFFFFFFF ? FR_OK : dir_sdi(dp, dp->blk_ofs); /* Goto top of the entry block if LFN is exist */
2310 if (res == FR_OK) {
2311 do {
2312 res = move_window(fs, dp->sect);
2313 if (res != FR_OK) break;
2314 /* Mark an entry 'deleted' */
2315 if (_FS_EXFAT && fs->fs_type == FS_EXFAT) { /* At the exFAT */
2316 dp->dir[XDIR_Type] &= 0x7F;
2317 } else { /* At the FAT12/16/32 */
2318 dp->dir[DIR_Name] = DDEM;
2319 }
2320 fs->wflag = 1;
2321 if (dp->dptr >= last) break; /* If reached last entry then all entries of the object has been deleted. */
2322 res = dir_next(dp, 0); /* Next entry */
2323 } while (res == FR_OK);
2324 if (res == FR_NO_FILE) res = FR_INT_ERR;
2325 }
2326 #else /* Non LFN configuration */
2327
2328 res = move_window(fs, dp->sect);
2329 if (res == FR_OK) {
2330 dp->dir[DIR_Name] = DDEM;
2331 fs->wflag = 1;
2332 }
2333 #endif
2334
2335 return res;
2336 }
2337 #endif /* !_FS_READONLY */
2338
2339
2340
2341
2342 /*-----------------------------------------------------------------------*/
2343 /* Get file information from directory entry */
2344 /*-----------------------------------------------------------------------*/
2345 #if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2
2346 static
2347 void get_fileinfo ( /* No return code */
2348 DIR* dp, /* Pointer to the directory object */
2349 FILINFO* fno /* Pointer to the file information to be filled */
2350 )
2351 {
2352 UINT i, j;
2353 TCHAR c;
2354 #if _USE_LFN != 0
2355 WCHAR w, *lfn;
2356 WCHAR lfv;
2357 #endif
2358
2359
2360 fno->fname[0] = 0; /* Invaidate file info */
2361 if (!dp->sect) return; /* Exit if read pointer has reached end of directory */
2362
2363 #if _USE_LFN != 0 /* LFN configuration */
2364 #if _FS_EXFAT
2365 if (dp->obj.fs->fs_type == FS_EXFAT) { /* At the exFAT */
2366 get_xdir_info(dp->obj.fs->dirbuf, fno);
2367 return;
2368 } else
2369 #endif
2370 { /* At the FAT12/16/32 */
2371 if (dp->blk_ofs != 0xFFFFFFFF) { /* Get LFN if available */
2372 i = 0; lfn = dp->lfn;
2373 while ((w = *lfn++) != 0) { /* Get an LFN character */
2374 #if !_LFN_UNICODE
2375 w = ff_convert(w, 0); /* Unicode -> OEM */
2376 if (w == 0) { i = 0; break; } /* No LFN if it could not be converted */
2377 if (_DF1S && w >= 0x100) { /* Put 1st byte if it is a DBC (always false at SBCS cfg) */
2378 fno->fname[i++] = (char)(w >> 8);
2379 }
2380 #endif
2381 if (i >= _MAX_LFN) { i = 0; break; } /* No LFN if buffer overflow */
2382 fno->fname[i++] = (char)w;
2383 }
2384 fno->fname[i] = 0; /* Terminate the LFN */
2385 }
2386 }
2387
2388 i = j = 0;
2389 lfv = fno->fname[i]; /* LFN is exist if non-zero */
2390 while (i < 11) { /* Copy name body and extension */
2391 c = (TCHAR)dp->dir[i++];
2392 if (c == ' ') continue; /* Skip padding spaces */
2393 if (c == RDDEM) c = (TCHAR)DDEM; /* Restore replaced DDEM character */
2394 if (i == 9) { /* Insert a . if extension is exist */
2395 if (!lfv) fno->fname[j] = '.';
2396 fno->altname[j++] = '.';
2397 }
2398 #if _LFN_UNICODE
2399 if (IsDBCS1(c) && i != 8 && i != 11 && IsDBCS2(dp->dir[i])) {
2400 c = c << 8 | dp->dir[i++];
2401 }
2402 c = ff_convert(c, 1); /* OEM -> Unicode */
2403 if (!c) c = '?';
2404 #endif
2405 fno->altname[j] = c;
2406 if (!lfv) {
2407 if (IsUpper(c) && (dp->dir[DIR_NTres] & (i >= 9 ? NS_EXT : NS_BODY))) {
2408 c += 0x20; /* To lower */
2409 }
2410 fno->fname[j] = c;
2411 }
2412 j++;
2413 }
2414 if (!lfv) {
2415 fno->fname[j] = 0;
2416 if (!dp->dir[DIR_NTres]) j = 0; /* Altname is no longer needed if neither LFN nor case info is exist. */
2417 }
2418 fno->altname[j] = 0; /* Terminate the SFN */
2419
2420 #else /* Non-LFN configuration */
2421 i = j = 0;
2422 while (i < 11) { /* Copy name body and extension */
2423 c = (TCHAR)dp->dir[i++];
2424 if (c == ' ') continue; /* Skip padding spaces */
2425 if (c == RDDEM) c = (TCHAR)DDEM; /* Restore replaced DDEM character */
2426 if (i == 9) fno->fname[j++] = '.'; /* Insert a . if extension is exist */
2427 fno->fname[j++] = c;
2428 }
2429 fno->fname[j] = 0;
2430 #endif
2431
2432 fno->fattrib = dp->dir[DIR_Attr]; /* Attribute */
2433 fno->fsize = ld_dword(dp->dir + DIR_FileSize); /* Size */
2434 fno->fdate = ld_word(dp->dir + DIR_WrtDate); /* Date */
2435 fno->ftime = ld_word(dp->dir + DIR_WrtTime); /* Time */
2436 }
2437 #endif /* _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 */
2438
2439
2440
2441
2442 /*-----------------------------------------------------------------------*/
2443 /* Pattern matching */
2444 /*-----------------------------------------------------------------------*/
2445 #if _USE_FIND && _FS_MINIMIZE <= 1
2446 static
2447 WCHAR get_achar ( /* Get a character and advances ptr 1 or 2 */
2448 const TCHAR** ptr /* Pointer to pointer to the SBCS/DBCS/Unicode string */
2449 )
2450 {
2451 #if !_LFN_UNICODE
2452 WCHAR chr;
2453
2454 chr = (BYTE)*(*ptr)++; /* Get a byte */
2455 if (IsLower(chr)) chr -= 0x20; /* To upper ASCII char */
2456 #ifdef _EXCVT
2457 if (chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper SBCS extended char */
2458 #else
2459 if (IsDBCS1(chr) && IsDBCS2(**ptr)) { /* Get DBC 2nd byte if needed */
2460 chr = chr << 8 | (BYTE)*(*ptr)++;
2461 }
2462 #endif
2463 return chr;
2464 #else
2465 return ff_wtoupper(*(*ptr)++); /* Get a word and to upper */
2466 #endif
2467 }
2468
2469
2470 static
2471 int pattern_matching ( /* 0:not matched, 1:matched */
2472 const TCHAR* pat, /* Matching pattern */
2473 const TCHAR* nam, /* String to be tested */
2474 int skip, /* Number of pre-skip chars (number of ?s) */
2475 int inf /* Infinite search (* specified) */
2476 )
2477 {
2478 const TCHAR *pp, *np;
2479 WCHAR pc, nc;
2480 int nm, nx;
2481
2482
2483 while (skip--) { /* Pre-skip name chars */
2484 if (!get_achar(&nam)) return 0; /* Branch mismatched if less name chars */
2485 }
2486 if (!*pat && inf) return 1; /* (short circuit) */
2487
2488 do {
2489 pp = pat; np = nam; /* Top of pattern and name to match */
2490 for (;;) {
2491 if (*pp == '?' || *pp == '*') { /* Wildcard? */
2492 nm = nx = 0;
2493 do { /* Analyze the wildcard chars */
2494 if (*pp++ == '?') nm++; else nx = 1;
2495 } while (*pp == '?' || *pp == '*');
2496 if (pattern_matching(pp, np, nm, nx)) return 1; /* Test new branch (recursions upto number of wildcard blocks in the pattern) */
2497 nc = *np; break; /* Branch mismatched */
2498 }
2499 pc = get_achar(&pp); /* Get a pattern char */
2500 nc = get_achar(&np); /* Get a name char */
2501 if (pc != nc) break; /* Branch mismatched? */
2502 if (pc == 0) return 1; /* Branch matched? (matched at end of both strings) */
2503 }
2504 get_achar(&nam); /* nam++ */
2505 } while (inf && nc); /* Retry until end of name if infinite search is specified */
2506
2507 return 0;
2508 }
2509 #endif /* _USE_FIND && _FS_MINIMIZE <= 1 */
2510
2511
2512
2513
2514 /*-----------------------------------------------------------------------*/
2515 /* Pick a top segment and create the object name in directory form */
2516 /*-----------------------------------------------------------------------*/
2517
2518 static
2519 FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not create */
2520 DIR* dp, /* Pointer to the directory object */
2521 const TCHAR** path /* Pointer to pointer to the segment in the path string */
2522 )
2523 {
2524 #if _USE_LFN != 0 /* LFN configuration */
2525 BYTE b, cf;
2526 WCHAR w, *lfn;
2527 UINT i, ni, si, di;
2528 const TCHAR *p;
2529
2530 /* Create LFN in Unicode */
2531 p = *path; lfn = dp->lfn; si = di = 0;
2532 for (;;) {
2533 w = p[si++]; /* Get a character */
2534 if (w < ' ' || w == '/' || w == '\\') { /* Break on end of segment */
2535 while (p[si] == '/' || p[si] == '\\') si++; /* Skip duplicated separator */
2536 break;
2537 }
2538 if (di >= _MAX_LFN) return FR_INVALID_NAME; /* Reject too long name */
2539 #if !_LFN_UNICODE
2540 w &= 0xFF;
2541 if (IsDBCS1(w)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
2542 b = (BYTE)p[si++]; /* Get 2nd byte */
2543 w = (w << 8) + b; /* Create a DBC */
2544 if (!IsDBCS2(b)) return FR_INVALID_NAME; /* Reject invalid sequence */
2545 }
2546 w = ff_convert(w, 1); /* Convert ANSI/OEM to Unicode */
2547 if (!w) return FR_INVALID_NAME; /* Reject invalid code */
2548 #endif
2549 if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) return FR_INVALID_NAME; /* Reject illegal characters for LFN */
2550 lfn[di++] = w; /* Store the Unicode character */
2551 }
2552 *path = &p[si]; /* Return pointer to the next segment */
2553 cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
2554 #if _FS_RPATH != 0
2555 if ((di == 1 && lfn[di - 1] == '.') ||
2556 (di == 2 && lfn[di - 1] == '.' && lfn[di - 2] == '.')) { /* Is this segment a dot name? */
2557 lfn[di] = 0;
2558 for (i = 0; i < 11; i++) /* Create dot name for SFN entry */
2559 dp->fn[i] = (i < di) ? '.' : ' ';
2560 dp->fn[i] = cf | NS_DOT; /* This is a dot entry */
2561 return FR_OK;
2562 }
2563 #endif
2564 while (di) { /* Snip off trailing spaces and dots if exist */
2565 w = lfn[di - 1];
2566 if (w != ' ' && w != '.') break;
2567 di--;
2568 }
2569 lfn[di] = 0; /* LFN is created */
2570 if (di == 0) return FR_INVALID_NAME; /* Reject nul name */
2571
2572 /* Create SFN in directory form */
2573 mem_set(dp->fn, ' ', 11);
2574 for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */
2575 if (si) cf |= NS_LOSS | NS_LFN;
2576 while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */
2577
2578 b = i = 0; ni = 8;
2579 for (;;) {
2580 w = lfn[si++]; /* Get an LFN character */
2581 if (!w) break; /* Break on end of the LFN */
2582 if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */
2583 cf |= NS_LOSS | NS_LFN; continue;
2584 }
2585
2586 if (i >= ni || si == di) { /* Extension or end of SFN */
2587 if (ni == 11) { /* Long extension */
2588 cf |= NS_LOSS | NS_LFN; break;
2589 }
2590 if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */
2591 if (si > di) break; /* No extension */
2592 si = di; i = 8; ni = 11; /* Enter extension section */
2593 b <<= 2; continue;
2594 }
2595
2596 if (w >= 0x80) { /* Non ASCII character */
2597 #ifdef _EXCVT
2598 w = ff_convert(w, 0); /* Unicode -> OEM code */
2599 if (w) w = ExCvt[w - 0x80]; /* Convert extended character to upper (SBCS) */
2600 #else
2601 w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */
2602 #endif
2603 cf |= NS_LFN; /* Force create LFN entry */
2604 }
2605
2606 if (_DF1S && w >= 0x100) { /* Is this DBC? (always false at SBCS cfg) */
2607 if (i >= ni - 1) {
2608 cf |= NS_LOSS | NS_LFN; i = ni; continue;
2609 }
2610 dp->fn[i++] = (BYTE)(w >> 8);
2611 } else { /* SBC */
2612 if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal characters for SFN */
2613 w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */
2614 } else {
2615 if (IsUpper(w)) { /* ASCII large capital */
2616 b |= 2;
2617 } else {
2618 if (IsLower(w)) { /* ASCII small capital */
2619 b |= 1; w -= 0x20;
2620 }
2621 }
2622 }
2623 }
2624 dp->fn[i++] = (BYTE)w;
2625 }
2626
2627 if (dp->fn[0] == DDEM) dp->fn[0] = RDDEM; /* If the first character collides with DDEM, replace it with RDDEM */
2628
2629 if (ni == 8) b <<= 2;
2630 if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) cf |= NS_LFN; /* Create LFN entry when there are composite capitals */
2631 if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended character, NT flags are created */
2632 if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */
2633 if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */
2634 }
2635
2636 dp->fn[NSFLAG] = cf; /* SFN is created */
2637
2638 return FR_OK;
2639
2640
2641 #else /* Non-LFN configuration */
2642 BYTE b, c, d, *sfn;
2643 UINT ni, si, i;
2644 const char *p;
2645
2646 /* Create file name in directory form */
2647 p = *path; sfn = dp->fn;
2648 mem_set(sfn, ' ', 11);
2649 si = i = b = 0; ni = 8;
2650 #if _FS_RPATH != 0
2651 if (p[si] == '.') { /* Is this a dot entry? */
2652 for (;;) {
2653 c = (BYTE)p[si++];
2654 if (c != '.' || si >= 3) break;
2655 sfn[i++] = c;
2656 }
2657 if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;
2658 *path = &p[si]; /* Return pointer to the next segment */
2659 sfn[NSFLAG] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of path */
2660 return FR_OK;
2661 }
2662 #endif
2663 for (;;) {
2664 c = (BYTE)p[si++];
2665 if (c <= ' ' || c == '/' || c == '\\') { /* Break on end of segment */
2666 while (p[si] == '/' || p[si] == '\\') si++; /* Skip duplicated separator */
2667 break;
2668 }
2669 if (c == '.' || i >= ni) {
2670 if (ni != 8 || c != '.') return FR_INVALID_NAME;
2671 i = 8; ni = 11;
2672 b <<= 2; continue;
2673 }
2674 if (c >= 0x80) { /* Extended character? */
2675 b |= 3; /* Eliminate NT flag */
2676 #ifdef _EXCVT
2677 c = ExCvt[c - 0x80]; /* To upper extended characters (SBCS cfg) */
2678 #else
2679 #if !_DF1S
2680 return FR_INVALID_NAME; /* Reject extended characters (ASCII cfg) */
2681 #endif
2682 #endif
2683 }
2684 if (IsDBCS1(c)) { /* Check if it is a DBC 1st byte (always false at SBCS cfg.) */
2685 d = (BYTE)p[si++]; /* Get 2nd byte */
2686 if (!IsDBCS2(d) || i >= ni - 1) return FR_INVALID_NAME; /* Reject invalid DBC */
2687 sfn[i++] = c;
2688 sfn[i++] = d;
2689 } else { /* SBC */
2690 if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) return FR_INVALID_NAME; /* Reject illegal chrs for SFN */
2691 if (IsUpper(c)) { /* ASCII large capital? */
2692 b |= 2;
2693 } else {
2694 if (IsLower(c)) { /* ASCII small capital? */
2695 b |= 1; c -= 0x20;
2696 }
2697 }
2698 sfn[i++] = c;
2699 }
2700 }
2701 *path = &p[si]; /* Return pointer to the next segment */
2702 c = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
2703
2704 if (i == 0) return FR_INVALID_NAME; /* Reject nul string */
2705 if (sfn[0] == DDEM) sfn[0] = RDDEM; /* When first character collides with DDEM, replace it with RDDEM */
2706
2707 if (ni == 8) b <<= 2;
2708 if ((b & 0x03) == 0x01) c |= NS_EXT; /* NT flag (Name extension has only small capital) */
2709 if ((b & 0x0C) == 0x04) c |= NS_BODY; /* NT flag (Name body has only small capital) */
2710
2711 sfn[NSFLAG] = c; /* Store NT flag, File name is created */
2712
2713 return FR_OK;
2714 #endif
2715 }
2716
2717
2718
2719
2720 /*-----------------------------------------------------------------------*/
2721 /* Follow a file path */
2722 /*-----------------------------------------------------------------------*/
2723
2724 static
2725 FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
2726 DIR* dp, /* Directory object to return last directory and found object */
2727 const TCHAR* path /* Full-path string to find a file or directory */
2728 )
2729 {
2730 FRESULT res;
2731 BYTE ns;
2732 _FDID *obj = &dp->obj;
2733 FATFS *fs = obj->fs;
2734
2735
2736 #if _FS_RPATH != 0
2737 if (*path != '/' && *path != '\\') { /* Without heading separator */
2738 obj->sclust = fs->cdir; /* Start from the current directory */
2739 } else
2740 #endif
2741 { /* With heading separator */
2742 while (*path == '/' || *path == '\\') path++; /* Strip heading separator */
2743 obj->sclust = 0; /* Start from the root directory */
2744 }
2745 #if _FS_EXFAT && _FS_RPATH != 0
2746 if (fs->fs_type == FS_EXFAT && obj->sclust) { /* Retrieve the sub-directory status if needed */
2747 DIR dj;
2748
2749 obj->c_scl = fs->cdc_scl;
2750 obj->c_size = fs->cdc_size;
2751 obj->c_ofs = fs->cdc_ofs;
2752 res = load_obj_dir(&dj, obj);
2753 if (res != FR_OK) return res;
2754 obj->objsize = ld_dword(fs->dirbuf + XDIR_FileSize);
2755 obj->stat = fs->dirbuf[XDIR_GenFlags] & 2;
2756 }
2757 #endif
2758
2759 if ((UINT)*path < ' ') { /* Null path name is the origin directory itself */
2760 dp->fn[NSFLAG] = NS_NONAME;
2761 res = dir_sdi(dp, 0);
2762
2763 } else { /* Follow path */
2764 for (;;) {
2765 res = create_name(dp, &path); /* Get a segment name of the path */
2766 if (res != FR_OK) break;
2767 res = dir_find(dp); /* Find an object with the segment name */
2768 ns = dp->fn[NSFLAG];
2769 if (res != FR_OK) { /* Failed to find the object */
2770 if (res == FR_NO_FILE) { /* Object is not found */
2771 if (_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exist, stay there */
2772 if (!(ns & NS_LAST)) continue; /* Continue to follow if not last segment */
2773 dp->fn[NSFLAG] = NS_NONAME;
2774 res = FR_OK;
2775 } else { /* Could not find the object */
2776 if (!(ns & NS_LAST)) res = FR_NO_PATH; /* Adjust error code if not last segment */
2777 }
2778 }
2779 break;
2780 }
2781 if (ns & NS_LAST) break; /* Last segment matched. Function completed. */
2782 /* Get into the sub-directory */
2783 if (!(obj->attr & AM_DIR)) { /* It is not a sub-directory and cannot follow */
2784 res = FR_NO_PATH; break;
2785 }
2786 #if _FS_EXFAT
2787 if (fs->fs_type == FS_EXFAT) {
2788 obj->c_scl = obj->sclust; /* Save containing directory information for next dir */
2789 obj->c_size = ((DWORD)obj->objsize & 0xFFFFFF00) | obj->stat;
2790 obj->c_ofs = dp->blk_ofs;
2791 obj->sclust = ld_dword(fs->dirbuf + XDIR_FstClus); /* Open next directory */
2792 obj->stat = fs->dirbuf[XDIR_GenFlags] & 2;
2793 obj->objsize = ld_qword(fs->dirbuf + XDIR_FileSize);
2794 } else
2795 #endif
2796 {
2797 obj->sclust = ld_clust(fs, &fs->win[dp->dptr % SS(fs)]); /* Open next directory */
2798 }
2799 }
2800 }
2801
2802 return res;
2803 }
2804
2805
2806
2807
2808 /*-----------------------------------------------------------------------*/
2809 /* Get logical drive number from path name */
2810 /*-----------------------------------------------------------------------*/
2811
2812 static
2813 int get_ldnumber ( /* Returns logical drive number (-1:invalid drive) */
2814 const TCHAR** path /* Pointer to pointer to the path name */
2815 )
2816 {
2817 const TCHAR *tp, *tt;
2818 UINT i;
2819 int vol = -1;
2820 #if _STR_VOLUME_ID /* Find string drive id */
2821 static const char* const str[] = {_VOLUME_STRS};
2822 const char *sp;
2823 char c;
2824 TCHAR tc;
2825 #endif
2826
2827
2828 if (*path) { /* If the pointer is not a null */
2829 for (tt = *path; (UINT)*tt >= (_USE_LFN ? ' ' : '!') && *tt != ':'; tt++) ; /* Find ':' in the path */
2830 if (*tt == ':') { /* If a ':' is exist in the path name */
2831 tp = *path;
2832 i = *tp++ - '0';
2833 if (i < 10 && tp == tt) { /* Is there a numeric drive id? */
2834 if (i < _VOLUMES) { /* If a drive id is found, get the value and strip it */
2835 vol = (int)i;
2836 *path = ++tt;
2837 }
2838 }
2839 #if _STR_VOLUME_ID
2840 else { /* No numeric drive number, find string drive id */
2841 i = 0; tt++;
2842 do {
2843 sp = str[i]; tp = *path;
2844 do { /* Compare a string drive id with path name */
2845 c = *sp++; tc = *tp++;
2846 if (IsLower(tc)) tc -= 0x20;
2847 } while (c && (TCHAR)c == tc);
2848 } while ((c || tp != tt) && ++i < _VOLUMES); /* Repeat for each id until pattern match */
2849 if (i < _VOLUMES) { /* If a drive id is found, get the value and strip it */
2850 vol = (int)i;
2851 *path = tt;
2852 }
2853 }
2854 #endif
2855 return vol;
2856 }
2857 #if _FS_RPATH != 0 && _VOLUMES >= 2