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