version 2.8
pff.c
1 /*----------------------------------------------------------------------------/
2 / Petit FatFs - FAT file system module R0.03 (C)ChaN, 2014
3 /-----------------------------------------------------------------------------/
4 / Petit FatFs module is a generic FAT file system module for small embedded
5 / systems. This is a free software that opened for education, research and
6 / commercial developments under license policy of following trems.
7 /
8 / Copyright (C) 2014, ChaN, all right reserved.
9 /
10 / * The Petit FatFs module is a free software and there is NO WARRANTY.
11 / * No restriction on use. You can use, modify and redistribute it for
12 / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
13 / * Redistributions of source code must retain the above copyright notice.
14 /
15 /-----------------------------------------------------------------------------/
16 / Jun 15,'09 R0.01a First release.
17 /
18 / Dec 14,'09 R0.02 Added multiple code page support.
19 / Added write funciton.
20 / Changed stream read mode interface.
21 / Dec 07,'10 R0.02a Added some configuration options.
22 / Fixed fails to open objects with DBCS character.
23 
24 / Jun 10,'14 R0.03 Separated out configuration options to pffconf.h.
25 / Added _USE_LCC option.
26 / Added _FS_FAT16 option.
27 /----------------------------------------------------------------------------*/
28 
29 #include "pff.h" /* Petit FatFs configurations and declarations */
30 #include "diskio.h" /* Declarations of low level disk I/O functions */
31 
32 
33 
34 /*--------------------------------------------------------------------------
35 
36  Module Private Definitions
37 
38 ---------------------------------------------------------------------------*/
39 
40 
41 #if _PFATFS != 4004 /* Revision ID */
42 #error Wrong include file (pff.h).
43 #endif
44 
45 #if _FS_FAT32
46 #if !_FS_FAT16 && !_FS_FAT12
47 #define _FS_32ONLY 1
48 #else
49 #define _FS_32ONLY 0
50 #endif
51 #else
52 #if !_FS_FAT16 && !_FS_FAT12
53 #error Wrong _FS_FATxx setting.
54 #endif
55 #define _FS_32ONLY 0
56 #endif
57 
58 #define ABORT(err) {fs->flag = 0; return err;}
59 
60 
61 
62 /*--------------------------------------------------------*/
63 /* DBCS code ranges and SBCS extend char conversion table */
64 /*--------------------------------------------------------*/
65 
66 #if _CODE_PAGE == 932 /* Japanese Shift-JIS */
67 #define _DF1S 0x81 /* DBC 1st byte range 1 start */
68 #define _DF1E 0x9F /* DBC 1st byte range 1 end */
69 #define _DF2S 0xE0 /* DBC 1st byte range 2 start */
70 #define _DF2E 0xFC /* DBC 1st byte range 2 end */
71 #define _DS1S 0x40 /* DBC 2nd byte range 1 start */
72 #define _DS1E 0x7E /* DBC 2nd byte range 1 end */
73 #define _DS2S 0x80 /* DBC 2nd byte range 2 start */
74 #define _DS2E 0xFC /* DBC 2nd byte range 2 end */
75 
76 #elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
77 #define _DF1S 0x81
78 #define _DF1E 0xFE
79 #define _DS1S 0x40
80 #define _DS1E 0x7E
81 #define _DS2S 0x80
82 #define _DS2E 0xFE
83 
84 #elif _CODE_PAGE == 949 /* Korean */
85 #define _DF1S 0x81
86 #define _DF1E 0xFE
87 #define _DS1S 0x41
88 #define _DS1E 0x5A
89 #define _DS2S 0x61
90 #define _DS2E 0x7A
91 #define _DS3S 0x81
92 #define _DS3E 0xFE
93 
94 #elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */
95 #define _DF1S 0x81
96 #define _DF1E 0xFE
97 #define _DS1S 0x40
98 #define _DS1E 0x7E
99 #define _DS2S 0xA1
100 #define _DS2E 0xFE
101 
102 #elif _CODE_PAGE == 437 /* U.S. (OEM) */
103 #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, \
104  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, \
105  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, \
106  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}
107 
108 #elif _CODE_PAGE == 720 /* Arabic (OEM) */
109 #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, \
110  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, \
111  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, \
112  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}
113 
114 #elif _CODE_PAGE == 737 /* Greek (OEM) */
115 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,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CC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
160  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}
161 
162 #elif _CODE_PAGE == 866 /* Russian (OEM) */
163 #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, \
164  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, \
165  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, \
166  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}
167 
168 #elif _CODE_PAGE == 874 /* Thai (OEM, Windows) */
169 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0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2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
178  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}
179 
180 #elif _CODE_PAGE == 1251 /* Cyrillic (Windows) */
181 #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, \
182  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, \
183  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, \
184  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}
185 
186 #elif _CODE_PAGE == 1252 /* Latin 1 (Windows) */
187 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8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D4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xA2,0xB8,0xB9,0xBA, \
196  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}
197 
198 #elif _CODE_PAGE == 1254 /* Turkish (Windows) */
199 #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, \
200  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, \
201  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, \
202  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}
203 
204 #elif _CODE_PAGE == 1255 /* Hebrew (Windows) */
205 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x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0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
214  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}
215 
216 #elif _CODE_PAGE == 1257 /* Baltic (Windows) */
217 #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, \
218  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, \
219  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, \
220  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}
221 
222 #elif _CODE_PAGE == 1258 /* Vietnam (OEM, Windows) */
223 #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, \
224  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, \
225  0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
226  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}
227 
228 #else
229 #error Unknown code page.
230 
231 #endif
232 
233 
234 
235 /* Character code support macros */
236 
237 #define IsUpper(c) (((c)>='A')&&((c)<='Z'))
238 #define IsLower(c) (((c)>='a')&&((c)<='z'))
239 
240 #ifndef _EXCVT /* DBCS configuration */
241 
242 #ifdef _DF2S /* Two 1st byte areas */
243 #define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
244 #else /* One 1st byte area */
245 #define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
246 #endif
247 
248 #ifdef _DS3S /* Three 2nd byte areas */
249 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
250 #else /* Two 2nd byte areas */
251 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
252 #endif
253 
254 #else /* SBCS configuration */
255 
256 #define IsDBCS1(c) 0
257 #define IsDBCS2(c) 0
258 
259 #endif /* _EXCVT */
260 
261 
262 /* FatFs refers the members in the FAT structures with byte offset instead
263 / of structure member because there are incompatibility of the packing option
264 / between various compilers. */
265 
266 #define BS_jmpBoot 0
267 #define BS_OEMName 3
268 #define BPB_BytsPerSec 11
269 #define BPB_SecPerClus 13
270 #define BPB_RsvdSecCnt 14
271 #define BPB_NumFATs 16
272 #define BPB_RootEntCnt 17
273 #define BPB_TotSec16 19
274 #define BPB_Media 21
275 #define BPB_FATSz16 22
276 #define BPB_SecPerTrk 24
277 #define BPB_NumHeads 26
278 #define BPB_HiddSec 28
279 #define BPB_TotSec32 32
280 #define BS_55AA 510
281 
282 #define BS_DrvNum 36
283 #define BS_BootSig 38
284 #define BS_VolID 39
285 #define BS_VolLab 43
286 #define BS_FilSysType 54
287 
288 #define BPB_FATSz32 36
289 #define BPB_ExtFlags 40
290 #define BPB_FSVer 42
291 #define BPB_RootClus 44
292 #define BPB_FSInfo 48
293 #define BPB_BkBootSec 50
294 #define BS_DrvNum32 64
295 #define BS_BootSig32 66
296 #define BS_VolID32 67
297 #define BS_VolLab32 71
298 #define BS_FilSysType32 82
299 
300 #define MBR_Table 446
301 
302 #define DIR_Name 0
303 #define DIR_Attr 11
304 #define DIR_NTres 12
305 #define DIR_CrtTime 14
306 #define DIR_CrtDate 16
307 #define DIR_FstClusHI 20
308 #define DIR_WrtTime 22
309 #define DIR_WrtDate 24
310 #define DIR_FstClusLO 26
311 #define DIR_FileSize 28
312 
313 
314 
315 /*--------------------------------------------------------------------------
316 
317  Private Functions
318 
319 ---------------------------------------------------------------------------*/
320 
321 
322 static
323 FATFS *FatFs; /* Pointer to the file system object (logical drive) */
324 
325 
326 /* Fill memory */
327 static
328 void mem_set (void* dst, int val, int cnt) {
329  char *d = (char*)dst;
330  while (cnt--) *d++ = (char)val;
331 }
332 
333 /* Compare memory to memory */
334 static
335 int mem_cmp (const void* dst, const void* src, int cnt) {
336  const char *d = (const char *)dst, *s = (const char *)src;
337  int r = 0;
338  while (cnt-- && (r = *d++ - *s++) == 0) ;
339  return r;
340 }
341 
342 
343 
344 /*-----------------------------------------------------------------------*/
345 /* FAT access - Read value of a FAT entry */
346 /*-----------------------------------------------------------------------*/
347 
348 static
349 CLUST get_fat ( /* 1:IO error, Else:Cluster status */
350  CLUST clst /* Cluster# to get the link information */
351 )
352 {
353  BYTE buf[4];
354  FATFS *fs = FatFs;
355 
356  if (clst < 2 || clst >= fs->n_fatent) /* Range check */
357  return 1;
358 
359  switch (fs->fs_type) {
360 #if _FS_FAT12
361  case FS_FAT12 : {
362  UINT wc, bc, ofs;
363 
364  bc = (UINT)clst; bc += bc / 2;
365  ofs = bc % 512; bc /= 512;
366  if (ofs != 511) {
367  if (disk_readp(buf, fs->fatbase + bc, ofs, 2)) break;
368  } else {
369  if (disk_readp(buf, fs->fatbase + bc, 511, 1)) break;
370  if (disk_readp(buf+1, fs->fatbase + bc + 1, 0, 1)) break;
371  }
372  wc = LD_WORD(buf);
373  return (clst & 1) ? (wc >> 4) : (wc & 0xFFF);
374  }
375 #endif
376 #if _FS_FAT16
377  case FS_FAT16 :
378  if (disk_readp(buf, fs->fatbase + clst / 256, ((UINT)clst % 256) * 2, 2)) break;
379  return LD_WORD(buf);
380 #endif
381 #if _FS_FAT32
382  case FS_FAT32 :
383  if (disk_readp(buf, fs->fatbase + clst / 128, ((UINT)clst % 128) * 4, 4)) break;
384  return LD_DWORD(buf) & 0x0FFFFFFF;
385 #endif
386  }
387 
388  return 1; /* An error occured at the disk I/O layer */
389 }
390 
391 
392 
393 
394 /*-----------------------------------------------------------------------*/
395 /* Get sector# from cluster# / Get cluster field from directory entry */
396 /*-----------------------------------------------------------------------*/
397 
398 static
399 DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */
400  CLUST clst /* Cluster# to be converted */
401 )
402 {
403  FATFS *fs = FatFs;
404 
405 
406  clst -= 2;
407  if (clst >= (fs->n_fatent - 2)) return 0; /* Invalid cluster# */
408  return (DWORD)clst * fs->csize + fs->database;
409 }
410 
411 
412 static
413 CLUST get_clust (
414  BYTE* dir /* Pointer to directory entry */
415 )
416 {
417  FATFS *fs = FatFs;
418  CLUST clst = 0;
419 
420 
421  if (_FS_32ONLY || (_FS_FAT32 && fs->fs_type == FS_FAT32)) {
422  clst = LD_WORD(dir+DIR_FstClusHI);
423  clst <<= 16;
424  }
425  clst |= LD_WORD(dir+DIR_FstClusLO);
426 
427  return clst;
428 }
429 
430 
431 /*-----------------------------------------------------------------------*/
432 /* Directory handling - Rewind directory index */
433 /*-----------------------------------------------------------------------*/
434 
435 static
436 FRESULT dir_rewind (
437  DIR *dj /* Pointer to directory object */
438 )
439 {
440  CLUST clst;
441  FATFS *fs = FatFs;
442 
443 
444  dj->index = 0;
445  clst = dj->sclust;
446  if (clst == 1 || clst >= fs->n_fatent) /* Check start cluster range */
447  return FR_DISK_ERR;
448  if (_FS_FAT32 && !clst && (_FS_32ONLY || fs->fs_type == FS_FAT32)) /* Replace cluster# 0 with root cluster# if in FAT32 */
449  clst = (CLUST)fs->dirbase;
450  dj->clust = clst; /* Current cluster */
451  dj->sect = (_FS_32ONLY || clst) ? clust2sect(clst) : fs->dirbase; /* Current sector */
452 
453  return FR_OK; /* Seek succeeded */
454 }
455 
456 
457 
458 
459 /*-----------------------------------------------------------------------*/
460 /* Directory handling - Move directory index next */
461 /*-----------------------------------------------------------------------*/
462 
463 static
464 FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table */
465  DIR *dj /* Pointer to directory object */
466 )
467 {
468  CLUST clst;
469  WORD i;
470  FATFS *fs = FatFs;
471 
472 
473  i = dj->index + 1;
474  if (!i || !dj->sect) /* Report EOT when index has reached 65535 */
475  return FR_NO_FILE;
476 
477  if (!(i % 16)) { /* Sector changed? */
478  dj->sect++; /* Next sector */
479 
480  if (dj->clust == 0) { /* Static table */
481  if (i >= fs->n_rootdir) /* Report EOT when end of table */
482  return FR_NO_FILE;
483  }
484  else { /* Dynamic table */
485  if (((i / 16) & (fs->csize - 1)) == 0) { /* Cluster changed? */
486  clst = get_fat(dj->clust); /* Get next cluster */
487  if (clst <= 1) return FR_DISK_ERR;
488  if (clst >= fs->n_fatent) /* When it reached end of dynamic table */
489  return FR_NO_FILE; /* Report EOT */
490  dj->clust = clst; /* Initialize data for new cluster */
491  dj->sect = clust2sect(clst);
492  }
493  }
494  }
495 
496  dj->index = i;
497 
498  return FR_OK;
499 }
500 
501 
502 
503 
504 /*-----------------------------------------------------------------------*/
505 /* Directory handling - Find an object in the directory */
506 /*-----------------------------------------------------------------------*/
507 
508 static
509 FRESULT dir_find (
510  DIR *dj, /* Pointer to the directory object linked to the file name */
511  BYTE *dir /* 32-byte working buffer */
512 )
513 {
514  FRESULT res;
515  BYTE c;
516 
517 
518  res = dir_rewind(dj); /* Rewind directory object */
519  if (res != FR_OK) return res;
520 
521  do {
522  res = disk_readp(dir, dj->sect, (dj->index % 16) * 32, 32) /* Read an entry */
523  ? FR_DISK_ERR : FR_OK;
524  if (res != FR_OK) break;
525  c = dir[DIR_Name]; /* First character */
526  if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
527  if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */
528  break;
529  res = dir_next(dj); /* Next entry */
530  } while (res == FR_OK);
531 
532  return res;
533 }
534 
535 
536 
537 
538 /*-----------------------------------------------------------------------*/
539 /* Read an object from the directory */
540 /*-----------------------------------------------------------------------*/
541 #if _USE_DIR
542 static
543 FRESULT dir_read (
544  DIR *dj, /* Pointer to the directory object to store read object name */
545  BYTE *dir /* 32-byte working buffer */
546 )
547 {
548  FRESULT res;
549  BYTE a, c;
550 
551 
552  res = FR_NO_FILE;
553  while (dj->sect) {
554  res = disk_readp(dir, dj->sect, (dj->index % 16) * 32, 32) /* Read an entry */
555  ? FR_DISK_ERR : FR_OK;
556  if (res != FR_OK) break;
557  c = dir[DIR_Name];
558  if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
559  a = dir[DIR_Attr] & AM_MASK;
560  if (c != 0xE5 && c != '.' && !(a & AM_VOL)) /* Is it a valid entry? */
561  break;
562  res = dir_next(dj); /* Next entry */
563  if (res != FR_OK) break;
564  }
565 
566  if (res != FR_OK) dj->sect = 0;
567 
568  return res;
569 }
570 #endif
571 
572 
573 
574 /*-----------------------------------------------------------------------*/
575 /* Pick a segment and create the object name in directory form */
576 /*-----------------------------------------------------------------------*/
577 
578 
579 static
580 FRESULT create_name (
581  DIR *dj, /* Pointer to the directory object */
582  const char **path /* Pointer to pointer to the segment in the path string */
583 )
584 {
585  BYTE c, ni, si, i, *sfn;
586  const char *p;
587 #if _USE_LCC
588 #ifdef _EXCVT
589  static const BYTE cvt[] = _EXCVT;
590 #endif
591 #endif
592 
593  /* Create file name in directory form */
594  sfn = dj->fn;
595  mem_set(sfn, ' ', 11);
596  si = i = 0; ni = 8;
597  p = *path;
598  for (;;) {
599  c = p[si++];
600  if (c <= ' ' || c == '/') break; /* Break on end of segment */
601  if (c == '.' || i >= ni) {
602  if (ni != 8 || c != '.') break;
603  i = 8; ni = 11;
604  continue;
605  }
606 #if _USE_LCC
607 #ifdef _EXCVT
608  if (c >= 0x80) /* To upper extended char (SBCS) */
609  c = cvt[c - 0x80];
610 #endif
611  if (IsDBCS1(c) && i < ni - 1) { /* DBC 1st byte? */
612  BYTE d = p[si++]; /* Get 2nd byte */
613  sfn[i++] = c;
614  sfn[i++] = d;
615  } else
616 #endif
617  { /* Single byte code */
618  if (_USE_LCC && IsLower(c)) c -= 0x20; /* toupper */
619  sfn[i++] = c;
620  }
621  }
622  *path = &p[si]; /* Rerurn pointer to the next segment */
623 
624  sfn[11] = (c <= ' ') ? 1 : 0; /* Set last segment flag if end of path */
625 
626  return FR_OK;
627 }
628 
629 
630 
631 
632 /*-----------------------------------------------------------------------*/
633 /* Get file information from directory entry */
634 /*-----------------------------------------------------------------------*/
635 #if _USE_DIR
636 static
637 void get_fileinfo ( /* No return code */
638  DIR *dj, /* Pointer to the directory object */
639  BYTE *dir, /* 32-byte working buffer */
640  FILINFO *fno /* Pointer to store the file information */
641 )
642 {
643  BYTE i, c;
644  char *p;
645 
646 
647  p = fno->fname;
648  if (dj->sect) {
649  for (i = 0; i < 8; i++) { /* Copy file name body */
650  c = dir[i];
651  if (c == ' ') break;
652  if (c == 0x05) c = 0xE5;
653  *p++ = c;
654  }
655  if (dir[8] != ' ') { /* Copy file name extension */
656  *p++ = '.';
657  for (i = 8; i < 11; i++) {
658  c = dir[i];
659  if (c == ' ') break;
660  *p++ = c;
661  }
662  }
663  fno->fattrib = dir[DIR_Attr]; /* Attribute */
664  fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */
665  fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */
666  fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */
667  }
668  *p = 0;
669 }
670 #endif /* _USE_DIR */
671 
672 
673 
674 /*-----------------------------------------------------------------------*/
675 /* Follow a file path */
676 /*-----------------------------------------------------------------------*/
677 
678 static
679 FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
680  DIR *dj, /* Directory object to return last directory and found object */
681  BYTE *dir, /* 32-byte working buffer */
682  const char *path /* Full-path string to find a file or directory */
683 )
684 {
685  FRESULT res;
686 
687 
688  while (*path == ' ') path++; /* Strip leading spaces */
689  if (*path == '/') path++; /* Strip heading separator if exist */
690  dj->sclust = 0; /* Set start directory (always root dir) */
691 
692  if ((BYTE)*path < ' ') { /* Null path means the root directory */
693  res = dir_rewind(dj);
694  dir[0] = 0;
695 
696  } else { /* Follow path */
697  for (;;) {
698  res = create_name(dj, &path); /* Get a segment */
699  if (res != FR_OK) break;
700  res = dir_find(dj, dir); /* Find it */
701  if (res != FR_OK) break; /* Could not find the object */
702  if (dj->fn[11]) break; /* Last segment match. Function completed. */
703  if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow path because it is a file */
704  res = FR_NO_FILE; break;
705  }
706  dj->sclust = get_clust(dir); /* Follow next */
707  }
708  }
709 
710  return res;
711 }
712 
713 
714 
715 
716 /*-----------------------------------------------------------------------*/
717 /* Check a sector if it is an FAT boot record */
718 /*-----------------------------------------------------------------------*/
719 
720 static
721 BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record, 3:Error */
722  BYTE *buf, /* Working buffer */
723  DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
724 )
725 {
726  if (disk_readp(buf, sect, 510, 2)) /* Read the boot record */
727  return 3;
728  if (LD_WORD(buf) != 0xAA55) /* Check record signature */
729  return 2;
730 
731  if (!_FS_32ONLY && !disk_readp(buf, sect, BS_FilSysType, 2) && LD_WORD(buf) == 0x4146) /* Check FAT12/16 */
732  return 0;
733  if (_FS_FAT32 && !disk_readp(buf, sect, BS_FilSysType32, 2) && LD_WORD(buf) == 0x4146) /* Check FAT32 */
734  return 0;
735  return 1;
736 }
737 
738 
739 
740 
741 /*--------------------------------------------------------------------------
742 
743  Public Functions
744 
745 --------------------------------------------------------------------------*/
746 
747 
748 
749 /*-----------------------------------------------------------------------*/
750 /* Mount/Unmount a Locical Drive */
751 /*-----------------------------------------------------------------------*/
752 
753 FRESULT pf_mount (
754  FATFS *fs /* Pointer to new file system object */
755 )
756 {
757  BYTE fmt, buf[36];
758  DWORD bsect, fsize, tsect, mclst;
759 
760 
761  FatFs = 0;
762 
763  if (disk_initialize() & STA_NOINIT) /* Check if the drive is ready or not */
764  return FR_NOT_READY;
765 
766  /* Search FAT partition on the drive */
767  bsect = 0;
768  fmt = check_fs(buf, bsect); /* Check sector 0 as an SFD format */
769  if (fmt == 1) { /* Not an FAT boot record, it may be FDISK format */
770  /* Check a partition listed in top of the partition table */
771  if (disk_readp(buf, bsect, MBR_Table, 16)) { /* 1st partition entry */
772  fmt = 3;
773  } else {
774  if (buf[4]) { /* Is the partition existing? */
775  bsect = LD_DWORD(&buf[8]); /* Partition offset in LBA */
776  fmt = check_fs(buf, bsect); /* Check the partition */
777  }
778  }
779  }
780  if (fmt == 3) return FR_DISK_ERR;
781  if (fmt) return FR_NO_FILESYSTEM; /* No valid FAT patition is found */
782 
783  /* Initialize the file system object */
784  if (disk_readp(buf, bsect, 13, sizeof (buf))) return FR_DISK_ERR;
785 
786  fsize = LD_WORD(buf+BPB_FATSz16-13); /* Number of sectors per FAT */
787  if (!fsize) fsize = LD_DWORD(buf+BPB_FATSz32-13);
788 
789  fsize *= buf[BPB_NumFATs-13]; /* Number of sectors in FAT area */
790  fs->fatbase = bsect + LD_WORD(buf+BPB_RsvdSecCnt-13); /* FAT start sector (lba) */
791  fs->csize = buf[BPB_SecPerClus-13]; /* Number of sectors per cluster */
792  fs->n_rootdir = LD_WORD(buf+BPB_RootEntCnt-13); /* Nmuber of root directory entries */
793  tsect = LD_WORD(buf+BPB_TotSec16-13); /* Number of sectors on the file system */
794  if (!tsect) tsect = LD_DWORD(buf+BPB_TotSec32-13);
795  mclst = (tsect /* Last cluster# + 1 */
796  - LD_WORD(buf+BPB_RsvdSecCnt-13) - fsize - fs->n_rootdir / 16
797  ) / fs->csize + 2;
798  fs->n_fatent = (CLUST)mclst;
799 
800  fmt = 0; /* Determine the FAT sub type */
801  if (_FS_FAT12 && mclst < 0xFF7)
802  fmt = FS_FAT12;
803  if (_FS_FAT16 && mclst >= 0xFF8 && mclst < 0xFFF7)
804  fmt = FS_FAT16;
805  if (_FS_FAT32 && mclst >= 0xFFF7)
806  fmt = FS_FAT32;
807  if (!fmt) return FR_NO_FILESYSTEM;
808  fs->fs_type = fmt;
809 
810  if (_FS_32ONLY || (_FS_FAT32 && fmt == FS_FAT32))
811  fs->dirbase = LD_DWORD(buf+(BPB_RootClus-13)); /* Root directory start cluster */
812  else
813  fs->dirbase = fs->fatbase + fsize; /* Root directory start sector (lba) */
814  fs->database = fs->fatbase + fsize + fs->n_rootdir / 16; /* Data start sector (lba) */
815 
816  fs->flag = 0;
817  FatFs = fs;
818 
819  return FR_OK;
820 }
821 
822 
823 
824 
825 /*-----------------------------------------------------------------------*/
826 /* Open or Create a File */
827 /*-----------------------------------------------------------------------*/
828 
829 FRESULT pf_open (
830  const char *path /* Pointer to the file name */
831 )
832 {
833  FRESULT res;
834  DIR dj;
835  BYTE sp[12], dir[32];
836  FATFS *fs = FatFs;
837 
838 
839  if (!fs) return FR_NOT_ENABLED; /* Check file system */
840 
841  fs->flag = 0;
842  dj.fn = sp;
843  res = follow_path(&dj, dir, path); /* Follow the file path */
844  if (res != FR_OK) return res; /* Follow failed */
845  if (!dir[0] || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */
846  return FR_NO_FILE;
847 
848  fs->org_clust = get_clust(dir); /* File start cluster */
849  fs->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */
850  fs->fptr = 0; /* File pointer */
851  fs->flag = FA_OPENED;
852 
853  return FR_OK;
854 }
855 
856 
857 
858 
859 /*-----------------------------------------------------------------------*/
860 /* Read File */
861 /*-----------------------------------------------------------------------*/
862 #if _USE_READ
863 
864 FRESULT pf_read (
865  void* buff, /* Pointer to the read buffer (NULL:Forward data to the stream)*/
866  UINT btr, /* Number of bytes to read */
867  UINT* br /* Pointer to number of bytes read */
868 )
869 {
870  DRESULT dr;
871  CLUST clst;
872  DWORD sect, remain;
873  UINT rcnt;
874  BYTE cs, *rbuff = buff;
875  FATFS *fs = FatFs;
876 
877 
878  *br = 0;
879  if (!fs) return FR_NOT_ENABLED; /* Check file system */
880  if (!(fs->flag & FA_OPENED)) /* Check if opened */
881  return FR_NOT_OPENED;
882 
883  remain = fs->fsize - fs->fptr;
884  if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
885 
886  while (btr) { /* Repeat until all data transferred */
887  if ((fs->fptr % 512) == 0) { /* On the sector boundary? */
888  cs = (BYTE)(fs->fptr / 512 & (fs->csize - 1)); /* Sector offset in the cluster */
889  if (!cs) { /* On the cluster boundary? */
890  if (fs->fptr == 0) /* On the top of the file? */
891  clst = fs->org_clust;
892  else
893  clst = get_fat(fs->curr_clust);
894  if (clst <= 1) ABORT(FR_DISK_ERR);
895  fs->curr_clust = clst; /* Update current cluster */
896  }
897  sect = clust2sect(fs->curr_clust); /* Get current sector */
898  if (!sect) ABORT(FR_DISK_ERR);
899  fs->dsect = sect + cs;
900  }
901  rcnt = 512 - (UINT)fs->fptr % 512; /* Get partial sector data from sector buffer */
902  if (rcnt > btr) rcnt = btr;
903  dr = disk_readp(!buff ? 0 : rbuff, fs->dsect, (UINT)fs->fptr % 512, rcnt);
904  if (dr) ABORT(FR_DISK_ERR);
905  fs->fptr += rcnt; rbuff += rcnt; /* Update pointers and counters */
906  btr -= rcnt; *br += rcnt;
907  }
908 
909  return FR_OK;
910 }
911 #endif
912 
913 
914 
915 /*-----------------------------------------------------------------------*/
916 /* Write File */
917 /*-----------------------------------------------------------------------*/
918 #if _USE_WRITE
919 
920 FRESULT pf_write (
921  const void* buff, /* Pointer to the data to be written */
922  UINT btw, /* Number of bytes to write (0:Finalize the current write operation) */
923  UINT* bw /* Pointer to number of bytes written */
924 )
925 {
926  CLUST clst;
927  DWORD sect, remain;
928  const BYTE *p = buff;
929  BYTE cs;
930  UINT wcnt;
931  FATFS *fs = FatFs;
932 
933 
934  *bw = 0;
935  if (!fs) return FR_NOT_ENABLED; /* Check file system */
936  if (!(fs->flag & FA_OPENED)) /* Check if opened */
937  return FR_NOT_OPENED;
938 
939  if (!btw) { /* Finalize request */
940  if ((fs->flag & FA__WIP) && disk_writep(0, 0)) ABORT(FR_DISK_ERR);
941  fs->flag &= ~FA__WIP;
942  return FR_OK;
943  } else { /* Write data request */
944  if (!(fs->flag & FA__WIP)) /* Round-down fptr to the sector boundary */
945  fs->fptr &= 0xFFFFFE00;
946  }
947  remain = fs->fsize - fs->fptr;
948  if (btw > remain) btw = (UINT)remain; /* Truncate btw by remaining bytes */
949 
950  while (btw) { /* Repeat until all data transferred */
951  if ((UINT)fs->fptr % 512 == 0) { /* On the sector boundary? */
952  cs = (BYTE)(fs->fptr / 512 & (fs->csize - 1)); /* Sector offset in the cluster */
953  if (!cs) { /* On the cluster boundary? */
954  if (fs->fptr == 0) /* On the top of the file? */
955  clst = fs->org_clust;
956  else
957  clst = get_fat(fs->curr_clust);
958  if (clst <= 1) ABORT(FR_DISK_ERR);
959  fs->curr_clust = clst; /* Update current cluster */
960  }
961  sect = clust2sect(fs->curr_clust); /* Get current sector */
962  if (!sect) ABORT(FR_DISK_ERR);
963  fs->dsect = sect + cs;
964  if (disk_writep(0, fs->dsect)) ABORT(FR_DISK_ERR); /* Initiate a sector write operation */
965  fs->flag |= FA__WIP;
966  }
967  wcnt = 512 - (UINT)fs->fptr % 512; /* Number of bytes to write to the sector */
968  if (wcnt > btw) wcnt = btw;
969  if (disk_writep(p, wcnt)) ABORT(FR_DISK_ERR); /* Send data to the sector */
970  fs->fptr += wcnt; p += wcnt; /* Update pointers and counters */
971  btw -= wcnt; *bw += wcnt;
972  if ((UINT)fs->fptr % 512 == 0) {
973  if (disk_writep(0, 0)) ABORT(FR_DISK_ERR); /* Finalize the currtent secter write operation */
974  fs->flag &= ~FA__WIP;
975  }
976  }
977 
978  return FR_OK;
979 }
980 #endif
981 
982 
983 
984 /*-----------------------------------------------------------------------*/
985 /* Seek File R/W Pointer */
986 /*-----------------------------------------------------------------------*/
987 #if _USE_LSEEK
988 
989 FRESULT pf_lseek (
990  DWORD ofs /* File pointer from top of file */
991 )
992 {
993  CLUST clst;
994  DWORD bcs, sect, ifptr;
995  FATFS *fs = FatFs;
996 
997 
998  if (!fs) return FR_NOT_ENABLED; /* Check file system */
999  if (!(fs->flag & FA_OPENED)) /* Check if opened */
1000  return FR_NOT_OPENED;
1001 
1002  if (ofs > fs->fsize) ofs = fs->fsize; /* Clip offset with the file size */
1003  ifptr = fs->fptr;
1004  fs->fptr = 0;
1005  if (ofs > 0) {
1006  bcs = (DWORD)fs->csize * 512; /* Cluster size (byte) */
1007  if (ifptr > 0 &&
1008  (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */
1009  fs->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */
1010  ofs -= fs->fptr;
1011  clst = fs->curr_clust;
1012  } else { /* When seek to back cluster, */
1013  clst = fs->org_clust; /* start from the first cluster */
1014  fs->curr_clust = clst;
1015  }
1016  while (ofs > bcs) { /* Cluster following loop */
1017  clst = get_fat(clst); /* Follow cluster chain */
1018  if (clst <= 1 || clst >= fs->n_fatent) ABORT(FR_DISK_ERR);
1019  fs->curr_clust = clst;
1020  fs->fptr += bcs;
1021  ofs -= bcs;
1022  }
1023  fs->fptr += ofs;
1024  sect = clust2sect(clst); /* Current sector */
1025  if (!sect) ABORT(FR_DISK_ERR);
1026  fs->dsect = sect + (fs->fptr / 512 & (fs->csize - 1));
1027  }
1028 
1029  return FR_OK;
1030 }
1031 #endif
1032 
1033 
1034 
1035 /*-----------------------------------------------------------------------*/
1036 /* Create a Directroy Object */
1037 /*-----------------------------------------------------------------------*/
1038 #if _USE_DIR
1039 
1040 FRESULT pf_opendir (
1041  DIR *dj, /* Pointer to directory object to create */
1042  const char *path /* Pointer to the directory path */
1043 )
1044 {
1045  FRESULT res;
1046  BYTE sp[12], dir[32];
1047  FATFS *fs = FatFs;
1048 
1049 
1050  if (!fs) { /* Check file system */
1051  res = FR_NOT_ENABLED;
1052  } else {
1053  dj->fn = sp;
1054  res = follow_path(dj, dir, path); /* Follow the path to the directory */
1055  if (res == FR_OK) { /* Follow completed */
1056  if (dir[0]) { /* It is not the root dir */
1057  if (dir[DIR_Attr] & AM_DIR) /* The object is a directory */
1058  dj->sclust = get_clust(dir);
1059  else /* The object is not a directory */
1060  res = FR_NO_FILE;
1061  }
1062  if (res == FR_OK)
1063  res = dir_rewind(dj); /* Rewind dir */
1064  }
1065  }
1066 
1067  return res;
1068 }
1069 
1070 
1071 
1072 
1073 /*-----------------------------------------------------------------------*/
1074 /* Read Directory Entry in Sequense */
1075 /*-----------------------------------------------------------------------*/
1076 
1077 FRESULT pf_readdir (
1078  DIR *dj, /* Pointer to the open directory object */
1079  FILINFO *fno /* Pointer to file information to return */
1080 )
1081 {
1082  FRESULT res;
1083  BYTE sp[12], dir[32];
1084  FATFS *fs = FatFs;
1085 
1086 
1087  if (!fs) { /* Check file system */
1088  res = FR_NOT_ENABLED;
1089  } else {
1090  dj->fn = sp;
1091  if (!fno) {
1092  res = dir_rewind(dj);
1093  } else {
1094  res = dir_read(dj, dir); /* Get current directory item */
1095  if (res == FR_NO_FILE) res = FR_OK;
1096  if (res == FR_OK) { /* A valid entry is found */
1097  get_fileinfo(dj, dir, fno); /* Get the object information */
1098  res = dir_next(dj); /* Increment read index for next */
1099  if (res == FR_NO_FILE) res = FR_OK;
1100  }
1101  }
1102  }
1103 
1104  return res;
1105 }
1106 
1107 #endif /* _USE_DIR */
1108