source: uKadecot/trunk/pfatfs/pff.c

Last change on this file was 154, checked in by coas-nagasima, 8 years ago

SDカードの中身を/~/でアクセスできるよう変更

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-chdr; charset=SHIFT_JIS
File size: 56.4 KB
Line 
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/* DBCS code ranges and SBCS extend char conversion table */
62/*--------------------------------------------------------*/
63
64#if _CODE_PAGE == 932 /* Japanese Shift-JIS */
65#define _DF1S 0x81 /* DBC 1st byte range 1 start */
66#define _DF1E 0x9F /* DBC 1st byte range 1 end */
67#define _DF2S 0xE0 /* DBC 1st byte range 2 start */
68#define _DF2E 0xFC /* DBC 1st byte range 2 end */
69#define _DS1S 0x40 /* DBC 2nd byte range 1 start */
70#define _DS1E 0x7E /* DBC 2nd byte range 1 end */
71#define _DS2S 0x80 /* DBC 2nd byte range 2 start */
72#define _DS2E 0xFC /* DBC 2nd byte range 2 end */
73
74#elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
75#define _DF1S 0x81
76#define _DF1E 0xFE
77#define _DS1S 0x40
78#define _DS1E 0x7E
79#define _DS2S 0x80
80#define _DS2E 0xFE
81
82#elif _CODE_PAGE == 949 /* Korean */
83#define _DF1S 0x81
84#define _DF1E 0xFE
85#define _DS1S 0x41
86#define _DS1E 0x5A
87#define _DS2S 0x61
88#define _DS2E 0x7A
89#define _DS3S 0x81
90#define _DS3E 0xFE
91
92#elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */
93#define _DF1S 0x81
94#define _DF1E 0xFE
95#define _DS1S 0x40
96#define _DS1E 0x7E
97#define _DS2S 0xA1
98#define _DS2E 0xFE
99
100#elif _CODE_PAGE == 437 /* U.S. (OEM) */
101#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, \
102 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,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OEM) */
113#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, \
114 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, \
115 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, \
116 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}
117
118#elif _CODE_PAGE == 775 /* Baltic (OEM) */
119#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, \
120 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, \
121 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0x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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, \
158 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}
159
160#elif _CODE_PAGE == 866 /* Russian (OEM) */
161#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, \
162 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, \
163 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, \
164 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}
165
166#elif _CODE_PAGE == 874 /* Thai (OEM, Windows) */
167#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88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0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
176 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}
177
178#elif _CODE_PAGE == 1251 /* Cyrillic (Windows) */
179#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, \
180 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, \
181 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, \
182 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}
183
184#elif _CODE_PAGE == 1252 /* Latin 1 (Windows) */
185#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0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2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xA2,0xB8,0xB9,0xBA, \
194 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}
195
196#elif _CODE_PAGE == 1254 /* Turkish (Windows) */
197#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, \
198 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, \
199 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, \
200 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}
201
202#elif _CODE_PAGE == 1255 /* Hebrew (Windows) */
203#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90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0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
212 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}
213
214#elif _CODE_PAGE == 1257 /* Baltic (Windows) */
215#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, \
216 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, \
217 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, \
218 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}
219
220#elif _CODE_PAGE == 1258 /* Vietnam (OEM, Windows) */
221#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, \
222 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, \
223 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, \
224 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}
225
226#else
227#error Unknown code page.
228
229#endif
230
231
232
233/* Character code support macros */
234
235#define IsUpper(c) (((c)>='A')&&((c)<='Z'))
236#define IsLower(c) (((c)>='a')&&((c)<='z'))
237
238#ifndef _EXCVT /* DBCS configuration */
239
240#ifdef _DF2S /* Two 1st byte areas */
241#define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
242#else /* One 1st byte area */
243#define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
244#endif
245
246#ifdef _DS3S /* Three 2nd byte areas */
247#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
248#else /* Two 2nd byte areas */
249#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
250#endif
251
252#else /* SBCS configuration */
253
254#define IsDBCS1(c) 0
255#define IsDBCS2(c) 0
256
257#endif /* _EXCVT */
258
259/* Name status flags */
260#define NSFLAG 11 /* Index of name status byte in fn[] */
261#define NS_LOSS 0x01 /* Out of 8.3 format */
262#define NS_LFN 0x02 /* Force to create LFN entry */
263#define NS_LAST 0x04 /* Last segment */
264#define NS_BODY 0x08 /* Lower case flag (body) */
265#define NS_EXT 0x10 /* Lower case flag (ext) */
266#define NS_DOT 0x20 /* Dot entry */
267
268/* FatFs refers the members in the FAT structures with byte offset instead
269/ of structure member because there are incompatibility of the packing option
270/ between various compilers. */
271
272#define BS_jmpBoot 0
273#define BS_OEMName 3
274#define BPB_BytsPerSec 11
275#define BPB_SecPerClus 13
276#define BPB_RsvdSecCnt 14
277#define BPB_NumFATs 16
278#define BPB_RootEntCnt 17
279#define BPB_TotSec16 19
280#define BPB_Media 21
281#define BPB_FATSz16 22
282#define BPB_SecPerTrk 24
283#define BPB_NumHeads 26
284#define BPB_HiddSec 28
285#define BPB_TotSec32 32
286#define BS_55AA 510
287
288#define BS_DrvNum 36
289#define BS_BootSig 38
290#define BS_VolID 39
291#define BS_VolLab 43
292#define BS_FilSysType 54
293
294#define BPB_FATSz32 36
295#define BPB_ExtFlags 40
296#define BPB_FSVer 42
297#define BPB_RootClus 44
298#define BPB_FSInfo 48
299#define BPB_BkBootSec 50
300#define BS_DrvNum32 64
301#define BS_BootSig32 66
302#define BS_VolID32 67
303#define BS_VolLab32 71
304#define BS_FilSysType32 82
305
306#define MBR_Table 446
307
308#define DIR_Name 0
309#define DIR_Attr 11
310#define DIR_NTres 12
311#define DIR_CrtTime 14
312#define DIR_CrtDate 16
313#define DIR_FstClusHI 20
314#define DIR_WrtTime 22
315#define DIR_WrtDate 24
316#define DIR_FstClusLO 26
317#define DIR_FileSize 28
318#define LDIR_Ord 0 /* LFN entry order and LLE flag (1) */
319#define LDIR_Attr 11 /* LFN attribute (1) */
320#define LDIR_Type 12 /* LFN type (1) */
321#define LDIR_Chksum 13 /* Sum of corresponding SFN entry */
322#define LDIR_FstClusLO 26 /* Filled by zero (0) */
323#define SZ_DIR 32 /* Size of a directory entry */
324#define LLE 0x40 /* Last long entry flag in LDIR_Ord */
325#define DDE 0xE5 /* Deleted directory entry mark in DIR_Name[0] */
326#define NDDE 0x05 /* Replacement of the character collides with DDE */
327
328/*--------------------------------------------------------------------------
329
330 Private Functions
331
332---------------------------------------------------------------------------*/
333
334
335/* Fill memory */
336static
337void mem_set (void* dst, int val, int cnt) {
338 char *d = (char*)dst;
339 while (cnt--) *d++ = (char)val;
340}
341
342/* Compare memory to memory */
343static
344int mem_cmp (const void* dst, const void* src, int cnt) {
345 const char *d = (const char *)dst, *s = (const char *)src;
346 int r = 0;
347 while (cnt-- && (r = *d++ - *s++) == 0) ;
348 return r;
349}
350
351/* Check if chr is contained in the string */
352static
353int chk_chr (const char* str, int chr) {
354 while (*str && *str != chr) str++;
355 return *str;
356}
357
358static
359const WCHAR uni2sjis[] = {
360 /* dummy... */
361 0, 0
362};
363
364WCHAR ff_convert ( /* Converted code, 0 means conversion error */
365 WCHAR chr, /* Character code to be converted */
366 UINT dir /* 0: Unicode to OEMCP, 1: OEMCP to Unicode */
367)
368{
369 const WCHAR *p;
370 WCHAR c;
371 int i, n, li, hi;
372
373
374 if (chr <= 0x80) { /* ASCII */
375 c = chr;
376 } else {
377 if (dir) { /* OEMCP to unicode (Incremental search)*/
378 p = &uni2sjis[1];
379 do {
380 c = *p;
381 p += 2;
382 } while (c && c != chr);
383 p -= 3;
384 c = *p;
385 } else { /* Unicode to OEMCP */
386 li = 0; hi = sizeof uni2sjis / 4 - 1;
387 for (n = 16; n; n--) {
388 i = li + (hi - li) / 2;
389 if (chr == uni2sjis[i * 2]) break;
390 if (chr > uni2sjis[i * 2])
391 li = i;
392 else
393 hi = i;
394 }
395 c = n ? uni2sjis[i * 2 + 1] : 0;
396 }
397 }
398
399 return c;
400}
401
402WCHAR ff_wtoupper ( /* Upper converted character */
403 WCHAR chr /* Input character */
404)
405{
406 static const WCHAR tbl_lower[] = { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0xA1, 0x00A2, 0x00A3, 0x00A5, 0x00AC, 0x00AF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0x0FF, 0x101, 0x103, 0x105, 0x107, 0x109, 0x10B, 0x10D, 0x10F, 0x111, 0x113, 0x115, 0x117, 0x119, 0x11B, 0x11D, 0x11F, 0x121, 0x123, 0x125, 0x127, 0x129, 0x12B, 0x12D, 0x12F, 0x131, 0x133, 0x135, 0x137, 0x13A, 0x13C, 0x13E, 0x140, 0x142, 0x144, 0x146, 0x148, 0x14B, 0x14D, 0x14F, 0x151, 0x153, 0x155, 0x157, 0x159, 0x15B, 0x15D, 0x15F, 0x161, 0x163, 0x165, 0x167, 0x169, 0x16B, 0x16D, 0x16F, 0x171, 0x173, 0x175, 0x177, 0x17A, 0x17C, 0x17E, 0x192, 0x3B1, 0x3B2, 0x3B3, 0x3B4, 0x3B5, 0x3B6, 0x3B7, 0x3B8, 0x3B9, 0x3BA, 0x3BB, 0x3BC, 0x3BD, 0x3BE, 0x3BF, 0x3C0, 0x3C1, 0x3C3, 0x3C4, 0x3C5, 0x3C6, 0x3C7, 0x3C8, 0x3C9, 0x3CA, 0x430, 0x431, 0x432, 0x433, 0x434, 0x435, 0x436, 0x437, 0x438, 0x439, 0x43A, 0x43B, 0x43C, 0x43D, 0x43E, 0x43F, 0x440, 0x441, 0x442, 0x443, 0x444, 0x445, 0x446, 0x447, 0x448, 0x449, 0x44A, 0x44B, 0x44C, 0x44D, 0x44E, 0x44F, 0x451, 0x452, 0x453, 0x454, 0x455, 0x456, 0x457, 0x458, 0x459, 0x45A, 0x45B, 0x45C, 0x45E, 0x45F, 0x2170, 0x2171, 0x2172, 0x2173, 0x2174, 0x2175, 0x2176, 0x2177, 0x2178, 0x2179, 0x217A, 0x217B, 0x217C, 0x217D, 0x217E, 0x217F, 0xFF41, 0xFF42, 0xFF43, 0xFF44, 0xFF45, 0xFF46, 0xFF47, 0xFF48, 0xFF49, 0xFF4A, 0xFF4B, 0xFF4C, 0xFF4D, 0xFF4E, 0xFF4F, 0xFF50, 0xFF51, 0xFF52, 0xFF53, 0xFF54, 0xFF55, 0xFF56, 0xFF57, 0xFF58, 0xFF59, 0xFF5A, 0 };
407 static const WCHAR tbl_upper[] = { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x21, 0xFFE0, 0xFFE1, 0xFFE5, 0xFFE2, 0xFFE3, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0x178, 0x100, 0x102, 0x104, 0x106, 0x108, 0x10A, 0x10C, 0x10E, 0x110, 0x112, 0x114, 0x116, 0x118, 0x11A, 0x11C, 0x11E, 0x120, 0x122, 0x124, 0x126, 0x128, 0x12A, 0x12C, 0x12E, 0x130, 0x132, 0x134, 0x136, 0x139, 0x13B, 0x13D, 0x13F, 0x141, 0x143, 0x145, 0x147, 0x14A, 0x14C, 0x14E, 0x150, 0x152, 0x154, 0x156, 0x158, 0x15A, 0x15C, 0x15E, 0x160, 0x162, 0x164, 0x166, 0x168, 0x16A, 0x16C, 0x16E, 0x170, 0x172, 0x174, 0x176, 0x179, 0x17B, 0x17D, 0x191, 0x391, 0x392, 0x393, 0x394, 0x395, 0x396, 0x397, 0x398, 0x399, 0x39A, 0x39B, 0x39C, 0x39D, 0x39E, 0x39F, 0x3A0, 0x3A1, 0x3A3, 0x3A4, 0x3A5, 0x3A6, 0x3A7, 0x3A8, 0x3A9, 0x3AA, 0x410, 0x411, 0x412, 0x413, 0x414, 0x415, 0x416, 0x417, 0x418, 0x419, 0x41A, 0x41B, 0x41C, 0x41D, 0x41E, 0x41F, 0x420, 0x421, 0x422, 0x423, 0x424, 0x425, 0x426, 0x427, 0x428, 0x429, 0x42A, 0x42B, 0x42C, 0x42D, 0x42E, 0x42F, 0x401, 0x402, 0x403, 0x404, 0x405, 0x406, 0x407, 0x408, 0x409, 0x40A, 0x40B, 0x40C, 0x40E, 0x40F, 0x2160, 0x2161, 0x2162, 0x2163, 0x2164, 0x2165, 0x2166, 0x2167, 0x2168, 0x2169, 0x216A, 0x216B, 0x216C, 0x216D, 0x216E, 0x216F, 0xFF21, 0xFF22, 0xFF23, 0xFF24, 0xFF25, 0xFF26, 0xFF27, 0xFF28, 0xFF29, 0xFF2A, 0xFF2B, 0xFF2C, 0xFF2D, 0xFF2E, 0xFF2F, 0xFF30, 0xFF31, 0xFF32, 0xFF33, 0xFF34, 0xFF35, 0xFF36, 0xFF37, 0xFF38, 0xFF39, 0xFF3A, 0 };
408 int i;
409
410
411 for (i = 0; tbl_lower[i] && chr != tbl_lower[i]; i++) ;
412
413 return tbl_lower[i] ? tbl_upper[i] : chr;
414}
415
416/*-----------------------------------------------------------------------*/
417/* LFN handling - Test/Pick/Fit an LFN segment from/to directory entry */
418/*-----------------------------------------------------------------------*/
419#if _USE_LFN
420static
421const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN characters in the directory entry */
422
423
424static
425int cmp_lfn ( /* 1:Matched, 0:Not matched */
426 WCHAR* lfnbuf, /* Pointer to the LFN to be compared */
427 BYTE* dir /* Pointer to the directory entry containing a part of LFN */
428)
429{
430 UINT i, s;
431 WCHAR wc, uc;
432
433 i = ((dir[LDIR_Ord] & ~LLE) - 1) * 13; /* Get offset in the LFN buffer */
434 s = 0; wc = 1;
435 do {
436 uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
437 if (wc) { /* Last character has not been processed */
438 wc = ff_wtoupper(uc); /* Convert it to upper case */
439 if (i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++])) /* Compare it */
440 return 0; /* Not matched */
441 } else {
442 if (uc != 0xFFFF) return 0; /* Check filler */
443 }
444 } while (++s < 13); /* Repeat until all characters in the entry are checked */
445
446 if ((dir[LDIR_Ord] & LLE) && wc && lfnbuf[i]) /* Last segment matched but different length */
447 return 0;
448
449 return 1; /* The part of LFN matched */
450}
451
452static
453int pick_lfn ( /* 1:Succeeded, 0:Buffer overflow */
454 WCHAR* lfnbuf, /* Pointer to the Unicode-LFN buffer */
455 BYTE* dir /* Pointer to the directory entry */
456)
457{
458 UINT i, s;
459 WCHAR wc, uc;
460
461
462 i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
463
464 s = 0; wc = 1;
465 do {
466 uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
467 if (wc) { /* Last character has not been processed */
468 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
469 lfnbuf[i++] = wc = uc; /* Store it */
470 } else {
471 if (uc != 0xFFFF) return 0; /* Check filler */
472 }
473 } while (++s < 13); /* Read all character in the entry */
474
475 if (dir[LDIR_Ord] & LLE) { /* Put terminator if it is the last LFN part */
476 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
477 lfnbuf[i] = 0;
478 }
479
480 return 1;
481}
482#endif
483
484/*-----------------------------------------------------------------------*/
485/* FAT access - Read value of a FAT entry */
486/*-----------------------------------------------------------------------*/
487
488static
489CLUST get_fat ( /* 1:IO error, Else:Cluster status */
490 FATFS *fs,
491 CLUST clst /* Cluster# to get the link information */
492)
493{
494 BYTE buf[4];
495
496 if (clst < 2 || clst >= fs->n_fatent) /* Range check */
497 return 1;
498
499 switch (fs->fs_type) {
500#if _FS_FAT12
501 case FS_FAT12 : {
502 UINT wc, bc, ofs;
503
504 bc = (UINT)clst; bc += bc / 2;
505 ofs = bc % 512; bc /= 512;
506 if (ofs != 511) {
507 if (fs->disk_readp(buf, fs->fatbase + bc, ofs, 2)) break;
508 } else {
509 if (fs->disk_readp(buf, fs->fatbase + bc, 511, 1)) break;
510 if (fs->disk_readp(buf+1, fs->fatbase + bc + 1, 0, 1)) break;
511 }
512 wc = LD_WORD(buf);
513 return (clst & 1) ? (wc >> 4) : (wc & 0xFFF);
514 }
515#endif
516#if _FS_FAT16
517 case FS_FAT16 :
518 if (fs->disk_readp(buf, fs->fatbase + clst / 256, ((UINT)clst % 256) * 2, 2)) break;
519 return LD_WORD(buf);
520#endif
521#if _FS_FAT32
522 case FS_FAT32 :
523 if (fs->disk_readp(buf, fs->fatbase + clst / 128, ((UINT)clst % 128) * 4, 4)) break;
524 return LD_DWORD(buf) & 0x0FFFFFFF;
525#endif
526 }
527
528 return 1; /* An error occured at the disk I/O layer */
529}
530
531
532
533
534/*-----------------------------------------------------------------------*/
535/* Get sector# from cluster# / Get cluster field from directory entry */
536/*-----------------------------------------------------------------------*/
537
538static
539DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */
540 FATFS *fs,
541 CLUST clst /* Cluster# to be converted */
542)
543{
544 clst -= 2;
545 if (clst >= (fs->n_fatent - 2)) return 0; /* Invalid cluster# */
546 return (DWORD)clst * fs->csize + fs->database;
547}
548
549
550static
551CLUST get_clust (
552 FATFS *fs,
553 BYTE* dir /* Pointer to directory entry */
554)
555{
556 CLUST clst = 0;
557
558
559 if (_FS_32ONLY || (_FS_FAT32 && fs->fs_type == FS_FAT32)) {
560 clst = LD_WORD(dir+DIR_FstClusHI);
561 clst <<= 16;
562 }
563 clst |= LD_WORD(dir+DIR_FstClusLO);
564
565 return clst;
566}
567
568
569/*-----------------------------------------------------------------------*/
570/* Directory handling - Rewind directory index */
571/*-----------------------------------------------------------------------*/
572
573static
574FRESULT dir_rewind (
575 DIR *dj /* Pointer to directory object */
576)
577{
578 CLUST clst;
579 FATFS *fs = dj->fs;
580
581
582 dj->index = 0;
583 clst = dj->sclust;
584 if (clst == 1 || clst >= fs->n_fatent) /* Check start cluster range */
585 return FR_DISK_ERR;
586 if (_FS_FAT32 && !clst && (_FS_32ONLY || fs->fs_type == FS_FAT32)) /* Replace cluster# 0 with root cluster# if in FAT32 */
587 clst = (CLUST)fs->dirbase;
588 dj->clust = clst; /* Current cluster */
589 dj->sect = (_FS_32ONLY || clst) ? clust2sect(fs, clst) : fs->dirbase; /* Current sector */
590
591 return FR_OK; /* Seek succeeded */
592}
593
594
595
596
597/*-----------------------------------------------------------------------*/
598/* Directory handling - Move directory index next */
599/*-----------------------------------------------------------------------*/
600
601static
602FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table */
603 DIR *dj /* Pointer to directory object */
604)
605{
606 CLUST clst;
607 WORD i;
608 FATFS *fs = dj->fs;
609
610
611 i = dj->index + 1;
612 if (!i || !dj->sect) /* Report EOT when index has reached 65535 */
613 return FR_NO_FILE;
614
615 if (!(i % 16)) { /* Sector changed? */
616 dj->sect++; /* Next sector */
617
618 if (dj->clust == 0) { /* Static table */
619 if (i >= fs->n_rootdir) /* Report EOT when end of table */
620 return FR_NO_FILE;
621 }
622 else { /* Dynamic table */
623 if (((i / 16) & (fs->csize - 1)) == 0) { /* Cluster changed? */
624 clst = get_fat(fs, dj->clust); /* Get next cluster */
625 if (clst <= 1) return FR_DISK_ERR;
626 if (clst >= fs->n_fatent) /* When it reached end of dynamic table */
627 return FR_NO_FILE; /* Report EOT */
628 dj->clust = clst; /* Initialize data for new cluster */
629 dj->sect = clust2sect(fs, clst);
630 }
631 }
632 }
633
634 dj->index = i;
635
636 return FR_OK;
637}
638
639/*-----------------------------------------------------------------------*/
640/* Calculate sum of an SFN */
641/*-----------------------------------------------------------------------*/
642#if _USE_LFN
643static
644BYTE sum_sfn (
645 const BYTE* dir /* Pointer to the SFN entry */
646)
647{
648 BYTE sum = 0;
649 UINT n = 11;
650
651 do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);
652 return sum;
653}
654#endif
655
656/*-----------------------------------------------------------------------*/
657/* Directory handling - Find an object in the directory */
658/*-----------------------------------------------------------------------*/
659
660static
661FRESULT dir_find (
662 DIR *dj, /* Pointer to the directory object linked to the file name */
663 BYTE *dir /* 32-byte working buffer */
664)
665{
666 FRESULT res;
667 BYTE c;
668#if _USE_LFN
669 BYTE a, ord, sum;
670#endif
671 FATFS *fs = dj->fs;
672
673 res = dir_rewind(dj); /* Rewind directory object */
674 if (res != FR_OK) return res;
675
676#if _USE_LFN
677 ord = sum = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */
678#endif
679 do {
680 res = fs->disk_readp(dir, dj->sect, (dj->index % 16) * 32, 32) /* Read an entry */
681 ? FR_DISK_ERR : FR_OK;
682 if (res != FR_OK) break;
683 c = dir[DIR_Name]; /* First character */
684 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
685#if _USE_LFN /* LFN configuration */
686 a = dir[DIR_Attr] & AM_MASK;
687 if (c == DDE || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
688 ord = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */
689 } else {
690 if (a == AM_LFN) { /* An LFN entry is found */
691 if (dj->lfn) {
692 if (c & LLE) { /* Is it start of LFN sequence? */
693 sum = dir[LDIR_Chksum];
694 c &= ~LLE; ord = c; /* LFN start order */
695 dj->lfn_idx = dj->index; /* Start index of LFN */
696 }
697 /* Check validity of the LFN entry and compare it with given name */
698 ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
699 }
700 } else { /* An SFN entry is found */
701 if (!ord && sum == sum_sfn(dir)) break; /* LFN matched? */
702 if (!(dj->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dir, dj->fn, 11)) break; /* SFN matched? */
703 ord = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */
704 }
705 }
706#else /* Non LFN configuration */
707 if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */
708 break;
709#endif
710 res = dir_next(dj); /* Next entry */
711 } while (res == FR_OK);
712
713 return res;
714}
715
716
717
718
719/*-----------------------------------------------------------------------*/
720/* Read an object from the directory */
721/*-----------------------------------------------------------------------*/
722#if _USE_DIR
723static
724FRESULT dir_read (
725 DIR *dj, /* Pointer to the directory object to store read object name */
726 BYTE *dir /* 32-byte working buffer */
727)
728{
729 FRESULT res;
730 BYTE a, c;
731#if _USE_LFN
732 BYTE ord = 0xFF, sum = 0xFF;
733#endif
734 FATFS *fs = dj->fs;
735
736 res = FR_NO_FILE;
737 while (dj->sect) {
738 res = fs->disk_readp(dir, dj->sect, (dj->index % 16) * 32, 32) /* Read an entry */
739 ? FR_DISK_ERR : FR_OK;
740 if (res != FR_OK) break;
741 c = dir[DIR_Name];
742 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
743 a = dir[DIR_Attr] & AM_MASK;
744#if _USE_LFN /* LFN configuration */
745 if (c == DDE || c == '.' || ((a & ~AM_ARC) == AM_VOL)) { /* An entry without valid data */
746 ord = 0xFF;
747 } else {
748 if (a == AM_LFN) { /* An LFN entry is found */
749 if (c & LLE) { /* Is it start of LFN sequence? */
750 sum = dir[LDIR_Chksum];
751 c &= ~LLE; ord = c;
752 dj->lfn_idx = dj->index;
753 }
754 /* Check LFN validity and capture it */
755 ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
756 } else { /* An SFN entry is found */
757 if (ord || sum != sum_sfn(dir)) /* Is there a valid LFN? */
758 dj->lfn_idx = 0xFFFF; /* It has no LFN. */
759 break;
760 }
761 }
762#else /* Non LFN configuration */
763 if (c != DDE && c != '.' && !((a & ~AM_ARC) == AM_VOL)) /* Is it a valid entry? */
764 break;
765#endif
766 res = dir_next(dj); /* Next entry */
767 if (res != FR_OK) break;
768 }
769
770 if (res != FR_OK) dj->sect = 0;
771
772 return res;
773}
774#endif
775
776
777
778/*-----------------------------------------------------------------------*/
779/* Pick a segment and create the object name in directory form */
780/*-----------------------------------------------------------------------*/
781
782
783static
784FRESULT create_name (
785 DIR* dj, /* Pointer to the directory object */
786 const char** path /* Pointer to pointer to the segment in the path string */
787)
788{
789#if _USE_LFN /* LFN configuration */
790 BYTE b, cf;
791 WCHAR w, *lfn;
792 UINT i, ni, si, di;
793 const char *p;
794
795 /* Create LFN in Unicode */
796 for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
797 lfn = dj->lfn;
798 si = di = 0;
799 for (;;) {
800 w = p[si++]; /* Get a character */
801 if (w < ' ' || w == '/' || w == '\\') break; /* Break on end of segment */
802 if (di >= _MAX_LFN) /* Reject too long name */
803 return FR_INVALID_NAME;
804#if !_LFN_UNICODE
805 w &= 0xFF;
806 if (IsDBCS1(w)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
807 b = (BYTE)p[si++]; /* Get 2nd byte */
808 if (!IsDBCS2(b))
809 return FR_INVALID_NAME; /* Reject invalid sequence */
810 w = (w << 8) + b; /* Create a DBC */
811 }
812 w = ff_convert(w, 1); /* Convert ANSI/OEM to Unicode */
813 if (!w) return FR_INVALID_NAME; /* Reject invalid code */
814#endif
815 if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) /* Reject illegal characters for LFN */
816 return FR_INVALID_NAME;
817 lfn[di++] = w; /* Store the Unicode character */
818 }
819 *path = &p[si]; /* Return pointer to the next segment */
820 cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
821 while (di) { /* Strip trailing spaces and dots */
822 w = lfn[di-1];
823 if (w != ' ' && w != '.') break;
824 di--;
825 }
826 if (!di) return FR_INVALID_NAME; /* Reject nul string */
827
828 lfn[di] = 0; /* LFN is created */
829
830 /* Create SFN in directory form */
831 mem_set(dj->fn, ' ', 11);
832 for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */
833 if (si) cf |= NS_LOSS | NS_LFN;
834 while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */
835
836 b = i = 0; ni = 8;
837 for (;;) {
838 w = lfn[si++]; /* Get an LFN character */
839 if (!w) break; /* Break on end of the LFN */
840 if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */
841 cf |= NS_LOSS | NS_LFN; continue;
842 }
843
844 if (i >= ni || si == di) { /* Extension or end of SFN */
845 if (ni == 11) { /* Long extension */
846 cf |= NS_LOSS | NS_LFN; break;
847 }
848 if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */
849 if (si > di) break; /* No extension */
850 si = di; i = 8; ni = 11; /* Enter extension section */
851 b <<= 2; continue;
852 }
853
854 if (w >= 0x80) { /* Non ASCII character */
855#ifdef _EXCVT
856 w = ff_convert(w, 0); /* Unicode -> OEM code */
857 if (w) w = ExCvt[w - 0x80]; /* Convert extended character to upper (SBCS) */
858#else
859 w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */
860#endif
861 cf |= NS_LFN; /* Force create LFN entry */
862 }
863
864 if (_DF1S && w >= 0x100) { /* Double byte character (always false on SBCS cfg) */
865 if (i >= ni - 1) {
866 cf |= NS_LOSS | NS_LFN; i = ni; continue;
867 }
868 dj->fn[i++] = (BYTE)(w >> 8);
869 } else { /* Single byte character */
870 if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal characters for SFN */
871 w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */
872 } else {
873 if (IsUpper(w)) { /* ASCII large capital */
874 b |= 2;
875 } else {
876 if (IsLower(w)) { /* ASCII small capital */
877 b |= 1; w -= 0x20;
878 }
879 }
880 }
881 }
882 dj->fn[i++] = (BYTE)w;
883 }
884
885 if (dj->fn[0] == DDE) dj->fn[0] = NDDE; /* If the first character collides with deleted mark, replace it with 0x05 */
886
887 if (ni == 8) b <<= 2;
888 if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) /* Create LFN entry when there are composite capitals */
889 cf |= NS_LFN;
890 if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended character, NT flags are created */
891 if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */
892 if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */
893 }
894
895 dj->fn[NSFLAG] = cf; /* SFN is created */
896
897 return FR_OK;
898
899
900#else /* Non-LFN configuration */
901 BYTE c, ni, si, i, *sfn;
902 const char *p;
903#if _USE_LCC
904#ifdef _EXCVT
905 static const BYTE cvt[] = _EXCVT;
906#endif
907#endif
908
909 /* Create file name in directory form */
910 sfn = dj->fn;
911 mem_set(sfn, ' ', 11);
912 si = i = 0; ni = 8;
913 p = *path;
914 for (;;) {
915 c = p[si++];
916 if (c <= ' ' || c == '/') break; /* Break on end of segment */
917 if (c == '.' || i >= ni) {
918 if (ni != 8 || c != '.') break;
919 i = 8; ni = 11;
920 continue;
921 }
922#if _USE_LCC
923#ifdef _EXCVT
924 if (c >= 0x80) /* To upper extended char (SBCS) */
925 c = cvt[c - 0x80];
926#endif
927 if (IsDBCS1(c) && i < ni - 1) { /* DBC 1st byte? */
928 BYTE d = p[si++]; /* Get 2nd byte */
929 sfn[i++] = c;
930 sfn[i++] = d;
931 } else
932#endif
933 { /* Single byte code */
934 if (_USE_LCC && IsLower(c)) c -= 0x20; /* toupper */
935 sfn[i++] = c;
936 }
937 }
938 *path = &p[si]; /* Rerurn pointer to the next segment */
939
940 sfn[11] = (c <= ' ') ? 1 : 0; /* Set last segment flag if end of path */
941
942 return FR_OK;
943#endif
944}
945
946
947
948
949/*-----------------------------------------------------------------------*/
950/* Get file information from directory entry */
951/*-----------------------------------------------------------------------*/
952#if _USE_DIR
953static
954void get_fileinfo ( /* No return code */
955 DIR *dj, /* Pointer to the directory object */
956 BYTE *dir, /* 32-byte working buffer */
957 FILINFO *fno /* Pointer to store the file information */
958)
959{
960 BYTE i, c;
961 char *p;
962
963
964 p = fno->fname;
965 if (dj->sect) {
966 i = 0;
967 while (i < 11) { /* Copy name body and extension */
968 c = dir[i++];
969 if (c == ' ') continue; /* Skip padding spaces */
970 if (c == NDDE) c = DDE; /* Restore replaced DDE character */
971 if (i == 9) *p++ = '.'; /* Insert a . if extension is exist */
972#if _USE_LFN
973 if (IsUpper(c) && (dir[DIR_NTres] & (i >= 9 ? NS_EXT : NS_BODY)))
974 c += 0x20; /* To lower */
975#endif
976 *p++ = c;
977 }
978 fno->fattrib = dir[DIR_Attr]; /* Attribute */
979 fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */
980 fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */
981 fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */
982 }
983 *p = 0;
984
985#if _USE_LFN
986 if (fno->lfname) {
987 WCHAR w, *lfn;
988
989 i = 0; p = fno->lfname;
990 if (dj->sect && fno->lfsize && dj->lfn_idx != 0xFFFF) { /* Get LFN if available */
991 lfn = dj->lfn;
992 while ((w = *lfn++) != 0) { /* Get an LFN character */
993#if !_LFN_UNICODE
994 w = ff_convert(w, 0); /* Unicode -> OEM */
995 if (!w) { i = 0; break; } /* No LFN if it could not be converted */
996 if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC (always false on SBCS cfg) */
997 p[i++] = (w >> 8);
998#endif
999 if (i >= fno->lfsize - 1) { i = 0; break; } /* No LFN if buffer overflow */
1000 p[i++] = w;
1001 }
1002 }
1003 p[i] = 0; /* Terminate LFN string by a \0 */
1004 }
1005#endif
1006}
1007#endif /* _USE_DIR */
1008
1009
1010
1011/*-----------------------------------------------------------------------*/
1012/* Follow a file path */
1013/*-----------------------------------------------------------------------*/
1014
1015static
1016FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
1017 DIR* dj, /* Directory object to return last directory and found object */
1018 BYTE *dir, /* 32-byte working buffer */
1019 const char* path /* Full-path string to find a file or directory */
1020)
1021{
1022 FRESULT res;
1023 BYTE ns;
1024 FATFS *fs = dj->fs;
1025
1026
1027 while (*path == ' ') path++; /* Strip leading spaces */
1028 if (*path == '/') path++; /* Strip heading separator if exist */
1029 dj->sclust = 0; /* Set start directory (always root dir) */
1030
1031 if ((BYTE)*path < ' ') { /* Null path means the root directory */
1032 res = dir_rewind(dj);
1033 dir[0] = 0;
1034
1035 } else { /* Follow path */
1036 for (;;) {
1037 res = create_name(dj, &path); /* Get a segment */
1038 if (res != FR_OK) break;
1039 res = dir_find(dj, dir); /* Find it */
1040 ns = dj->fn[NSFLAG];
1041 if (res != FR_OK) { /* Failed to find the object */
1042 if (res == FR_NO_FILE) { /* Object is not found */
1043 if (!(ns & NS_LAST)) res = FR_NO_PATH; /* Adjust error code if not last segment */
1044 }
1045 break;
1046 }
1047 if (ns & NS_LAST) break; /* Last segment matched. Function completed. */
1048 if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow path because it is a file */
1049 res = FR_NO_PATH; break;
1050 }
1051 dj->sclust = get_clust(fs, dir); /* Follow next */
1052 }
1053 }
1054
1055 return res;
1056}
1057
1058
1059
1060
1061/*-----------------------------------------------------------------------*/
1062/* Check a sector if it is an FAT boot record */
1063/*-----------------------------------------------------------------------*/
1064
1065static
1066BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record, 3:Error */
1067 FATFS *fs,
1068 BYTE *buf, /* Working buffer */
1069 DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
1070)
1071{
1072 if (fs->disk_readp(buf, sect, 510, 2)) /* Read the boot record */
1073 return 3;
1074 if (LD_WORD(buf) != 0xAA55) /* Check record signature */
1075 return 2;
1076
1077 if (!_FS_32ONLY && !fs->disk_readp(buf, sect, BS_FilSysType, 2) && LD_WORD(buf) == 0x4146) /* Check FAT12/16 */
1078 return 0;
1079 if (_FS_FAT32 && !fs->disk_readp(buf, sect, BS_FilSysType32, 2) && LD_WORD(buf) == 0x4146) /* Check FAT32 */
1080 return 0;
1081 return 1;
1082}
1083
1084
1085
1086
1087/*--------------------------------------------------------------------------
1088
1089 Public Functions
1090
1091--------------------------------------------------------------------------*/
1092
1093
1094
1095/*-----------------------------------------------------------------------*/
1096/* Mount/Unmount a Locical Drive */
1097/*-----------------------------------------------------------------------*/
1098
1099FRESULT pf_mount (
1100 FATFS *fs /* Pointer to new file system object */
1101)
1102{
1103 BYTE fmt, buf[36];
1104 DWORD bsect, fsize, tsect, mclst;
1105
1106 if (fs->disk_get_status() & STA_NOINIT) /* Check if the drive is ready or not */
1107 return FR_NOT_READY;
1108
1109 /* Search FAT partition on the drive */
1110 bsect = 0;
1111 fmt = check_fs(fs, buf, bsect); /* Check sector 0 as an SFD format */
1112 if (fmt == 1) { /* Not an FAT boot record, it may be FDISK format */
1113 /* Check a partition listed in top of the partition table */
1114 if (fs->disk_readp(buf, bsect, MBR_Table, 16)) { /* 1st partition entry */
1115 fmt = 3;
1116 } else {
1117 if (buf[4]) { /* Is the partition existing? */
1118 bsect = LD_DWORD(&buf[8]); /* Partition offset in LBA */
1119 fmt = check_fs(fs, buf, bsect); /* Check the partition */
1120 }
1121 }
1122 }
1123 if (fmt == 3) return FR_DISK_ERR;
1124 if (fmt) return FR_NO_FILESYSTEM; /* No valid FAT patition is found */
1125
1126 /* Initialize the file system object */
1127 if (fs->disk_readp(buf, bsect, 13, sizeof (buf))) return FR_DISK_ERR;
1128
1129 fsize = LD_WORD(buf+BPB_FATSz16-13); /* Number of sectors per FAT */
1130 if (!fsize) fsize = LD_DWORD(buf+BPB_FATSz32-13);
1131
1132 fsize *= buf[BPB_NumFATs-13]; /* Number of sectors in FAT area */
1133 fs->fatbase = bsect + LD_WORD(buf+BPB_RsvdSecCnt-13); /* FAT start sector (lba) */
1134 fs->csize = buf[BPB_SecPerClus-13]; /* Number of sectors per cluster */
1135 fs->n_rootdir = LD_WORD(buf+BPB_RootEntCnt-13); /* Nmuber of root directory entries */
1136 tsect = LD_WORD(buf+BPB_TotSec16-13); /* Number of sectors on the file system */
1137 if (!tsect) tsect = LD_DWORD(buf+BPB_TotSec32-13);
1138 mclst = (tsect /* Last cluster# + 1 */
1139 - LD_WORD(buf+BPB_RsvdSecCnt-13) - fsize - fs->n_rootdir / 16
1140 ) / fs->csize + 2;
1141 fs->n_fatent = (CLUST)mclst;
1142
1143 fmt = 0; /* Determine the FAT sub type */
1144 if (_FS_FAT12 && mclst < 0xFF7)
1145 fmt = FS_FAT12;
1146 if (_FS_FAT16 && mclst >= 0xFF8 && mclst < 0xFFF7)
1147 fmt = FS_FAT16;
1148 if (_FS_FAT32 && mclst >= 0xFFF7)
1149 fmt = FS_FAT32;
1150 if (!fmt) return FR_NO_FILESYSTEM;
1151 fs->fs_type = fmt;
1152
1153 if (_FS_32ONLY || (_FS_FAT32 && fmt == FS_FAT32))
1154 fs->dirbase = LD_DWORD(buf+(BPB_RootClus-13)); /* Root directory start cluster */
1155 else
1156 fs->dirbase = fs->fatbase + fsize; /* Root directory start sector (lba) */
1157 fs->database = fs->fatbase + fsize + fs->n_rootdir / 16; /* Data start sector (lba) */
1158
1159 fs->flag = 0;
1160
1161 return FR_OK;
1162}
1163
1164
1165
1166
1167/*-----------------------------------------------------------------------*/
1168/* Open or Create a File */
1169/*-----------------------------------------------------------------------*/
1170
1171FRESULT pf_open (
1172 FATFS *fs,
1173 const char *path /* Pointer to the file name */
1174)
1175{
1176 FRESULT res;
1177 DIR dj;
1178 BYTE sp[12], dir[32];
1179
1180
1181 if (!fs) return FR_NOT_ENABLED; /* Check file system */
1182
1183 fs->flag = 0;
1184 dj.fs = fs;
1185 dj.fn = sp;
1186#if _USE_LFN
1187 dj.lfn = fs->LfnBuf;
1188#endif
1189 res = follow_path(&dj, dir, path); /* Follow the file path */
1190 if (res != FR_OK) return res; /* Follow failed */
1191 if (!dir[0] || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */
1192 return FR_NO_FILE;
1193
1194 fs->org_clust = get_clust(fs, dir); /* File start cluster */
1195 fs->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */
1196 fs->fptr = 0; /* File pointer */
1197 fs->flag = FA_OPENED;
1198
1199 return FR_OK;
1200}
1201
1202
1203
1204
1205/*-----------------------------------------------------------------------*/
1206/* Read File */
1207/*-----------------------------------------------------------------------*/
1208#if _USE_READ
1209
1210FRESULT pf_read (
1211 FATFS *fs,
1212 void* buff, /* Pointer to the read buffer (NULL:Forward data to the stream)*/
1213 UINT btr, /* Number of bytes to read */
1214 UINT* br /* Pointer to number of bytes read */
1215)
1216{
1217 DRESULT dr;
1218 CLUST clst;
1219 DWORD sect, remain;
1220 UINT rcnt;
1221 BYTE cs, *rbuff = buff;
1222
1223
1224 *br = 0;
1225 if (!fs) return FR_NOT_ENABLED; /* Check file system */
1226 if (!(fs->flag & FA_OPENED)) /* Check if opened */
1227 return FR_NOT_OPENED;
1228
1229 remain = fs->fsize - fs->fptr;
1230 if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
1231
1232 while (btr) { /* Repeat until all data transferred */
1233 if ((fs->fptr % 512) == 0) { /* On the sector boundary? */
1234 cs = (BYTE)(fs->fptr / 512 & (fs->csize - 1)); /* Sector offset in the cluster */
1235 if (!cs) { /* On the cluster boundary? */
1236 if (fs->fptr == 0) /* On the top of the file? */
1237 clst = fs->org_clust;
1238 else
1239 clst = get_fat(fs, fs->curr_clust);
1240 if (clst <= 1) ABORT(FR_DISK_ERR);
1241 fs->curr_clust = clst; /* Update current cluster */
1242 }
1243 sect = clust2sect(fs, fs->curr_clust); /* Get current sector */
1244 if (!sect) ABORT(FR_DISK_ERR);
1245 fs->dsect = sect + cs;
1246 }
1247 rcnt = 512 - (UINT)fs->fptr % 512; /* Get partial sector data from sector buffer */
1248 if (rcnt > btr) rcnt = btr;
1249 dr = fs->disk_readp(!buff ? 0 : rbuff, fs->dsect, (UINT)fs->fptr % 512, rcnt);
1250 if (dr) ABORT(FR_DISK_ERR);
1251 fs->fptr += rcnt; rbuff += rcnt; /* Update pointers and counters */
1252 btr -= rcnt; *br += rcnt;
1253 }
1254
1255 return FR_OK;
1256}
1257#endif
1258
1259
1260
1261/*-----------------------------------------------------------------------*/
1262/* Write File */
1263/*-----------------------------------------------------------------------*/
1264#if _USE_WRITE
1265
1266FRESULT pf_write (
1267 FATFS *fs,
1268 const void* buff, /* Pointer to the data to be written */
1269 UINT btw, /* Number of bytes to write (0:Finalize the current write operation) */
1270 UINT* bw /* Pointer to number of bytes written */
1271)
1272{
1273 CLUST clst;
1274 DWORD sect, remain;
1275 const BYTE *p = buff;
1276 BYTE cs;
1277 UINT wcnt;
1278
1279
1280 *bw = 0;
1281 if (!fs) return FR_NOT_ENABLED; /* Check file system */
1282 if (!(fs->flag & FA_OPENED)) /* Check if opened */
1283 return FR_NOT_OPENED;
1284
1285 if (!btw) { /* Finalize request */
1286 if ((fs->flag & FA__WIP) && fs->disk_writep(0, 0)) ABORT(FR_DISK_ERR);
1287 fs->flag &= ~FA__WIP;
1288 return FR_OK;
1289 } else { /* Write data request */
1290 if (!(fs->flag & FA__WIP)) /* Round-down fptr to the sector boundary */
1291 fs->fptr &= 0xFFFFFE00;
1292 }
1293 remain = fs->fsize - fs->fptr;
1294 if (btw > remain) btw = (UINT)remain; /* Truncate btw by remaining bytes */
1295
1296 while (btw) { /* Repeat until all data transferred */
1297 if ((UINT)fs->fptr % 512 == 0) { /* On the sector boundary? */
1298 cs = (BYTE)(fs->fptr / 512 & (fs->csize - 1)); /* Sector offset in the cluster */
1299 if (!cs) { /* On the cluster boundary? */
1300 if (fs->fptr == 0) /* On the top of the file? */
1301 clst = fs->org_clust;
1302 else
1303 clst = get_fat(fs, fs->curr_clust);
1304 if (clst <= 1) ABORT(FR_DISK_ERR);
1305 fs->curr_clust = clst; /* Update current cluster */
1306 }
1307 sect = clust2sect(fs, fs->curr_clust); /* Get current sector */
1308 if (!sect) ABORT(FR_DISK_ERR);
1309 fs->dsect = sect + cs;
1310 if (fs->disk_writep(0, fs->dsect)) ABORT(FR_DISK_ERR); /* Initiate a sector write operation */
1311 fs->flag |= FA__WIP;
1312 }
1313 wcnt = 512 - (UINT)fs->fptr % 512; /* Number of bytes to write to the sector */
1314 if (wcnt > btw) wcnt = btw;
1315 if (fs->disk_writep(p, wcnt)) ABORT(FR_DISK_ERR); /* Send data to the sector */
1316 fs->fptr += wcnt; p += wcnt; /* Update pointers and counters */
1317 btw -= wcnt; *bw += wcnt;
1318 if ((UINT)fs->fptr % 512 == 0) {
1319 if (fs->disk_writep(0, 0)) ABORT(FR_DISK_ERR); /* Finalize the currtent secter write operation */
1320 fs->flag &= ~FA__WIP;
1321 }
1322 }
1323
1324 return FR_OK;
1325}
1326#endif
1327
1328
1329
1330/*-----------------------------------------------------------------------*/
1331/* Seek File R/W Pointer */
1332/*-----------------------------------------------------------------------*/
1333#if _USE_LSEEK
1334
1335FRESULT pf_lseek (
1336 FATFS *fs,
1337 DWORD ofs /* File pointer from top of file */
1338)
1339{
1340 CLUST clst;
1341 DWORD bcs, sect, ifptr;
1342
1343
1344 if (!fs) return FR_NOT_ENABLED; /* Check file system */
1345 if (!(fs->flag & FA_OPENED)) /* Check if opened */
1346 return FR_NOT_OPENED;
1347
1348 if (ofs > fs->fsize) ofs = fs->fsize; /* Clip offset with the file size */
1349 ifptr = fs->fptr;
1350 fs->fptr = 0;
1351 if (ofs > 0) {
1352 bcs = (DWORD)fs->csize * 512; /* Cluster size (byte) */
1353 if (ifptr > 0 &&
1354 (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */
1355 fs->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */
1356 ofs -= fs->fptr;
1357 clst = fs->curr_clust;
1358 } else { /* When seek to back cluster, */
1359 clst = fs->org_clust; /* start from the first cluster */
1360 fs->curr_clust = clst;
1361 }
1362 while (ofs > bcs) { /* Cluster following loop */
1363 clst = get_fat(fs, clst); /* Follow cluster chain */
1364 if (clst <= 1 || clst >= fs->n_fatent) ABORT(FR_DISK_ERR);
1365 fs->curr_clust = clst;
1366 fs->fptr += bcs;
1367 ofs -= bcs;
1368 }
1369 fs->fptr += ofs;
1370 sect = clust2sect(fs, clst); /* Current sector */
1371 if (!sect) ABORT(FR_DISK_ERR);
1372 fs->dsect = sect + (fs->fptr / 512 & (fs->csize - 1));
1373 }
1374
1375 return FR_OK;
1376}
1377#endif
1378
1379
1380
1381/*-----------------------------------------------------------------------*/
1382/* Create a Directroy Object */
1383/*-----------------------------------------------------------------------*/
1384#if _USE_DIR
1385
1386FRESULT pf_opendir (
1387 DIR *dj, /* Pointer to directory object to create */
1388 const char *path /* Pointer to the directory path */
1389)
1390{
1391 FRESULT res;
1392 BYTE sp[12], dir[32];
1393 FATFS *fs = dj->fs;
1394
1395
1396 if (!fs) { /* Check file system */
1397 res = FR_NOT_ENABLED;
1398 } else {
1399 dj->fn = sp;
1400#if _USE_LFN
1401 dj->lfn = fs->LfnBuf;
1402#endif
1403 res = follow_path(dj, dir, path); /* Follow the path to the directory */
1404 if (res == FR_OK) { /* Follow completed */
1405 if (dir[0]) { /* It is not the root dir */
1406 if (dir[DIR_Attr] & AM_DIR) /* The object is a directory */
1407 dj->sclust = get_clust(fs, dir);
1408 else /* The object is not a directory */
1409 res = FR_NO_FILE;
1410 }
1411 if (res == FR_OK)
1412 res = dir_rewind(dj); /* Rewind dir */
1413 }
1414 }
1415
1416 return res;
1417}
1418
1419
1420
1421
1422/*-----------------------------------------------------------------------*/
1423/* Read Directory Entry in Sequense */
1424/*-----------------------------------------------------------------------*/
1425
1426FRESULT pf_readdir (
1427 DIR *dj, /* Pointer to the open directory object */
1428 FILINFO *fno /* Pointer to file information to return */
1429)
1430{
1431 FRESULT res;
1432 BYTE sp[12], dir[32];
1433 FATFS *fs = dj->fs;
1434
1435
1436 if (!fs) { /* Check file system */
1437 res = FR_NOT_ENABLED;
1438 } else {
1439 dj->fn = sp;
1440 if (!fno) {
1441 res = dir_rewind(dj);
1442 } else {
1443 res = dir_read(dj, dir); /* Get current directory item */
1444 if (res == FR_NO_FILE) res = FR_OK;
1445 if (res == FR_OK) { /* A valid entry is found */
1446 get_fileinfo(dj, dir, fno); /* Get the object information */
1447 res = dir_next(dj); /* Increment read index for next */
1448 if (res == FR_NO_FILE) res = FR_OK;
1449 }
1450 }
1451 }
1452
1453 return res;
1454}
1455
1456#endif /* _USE_DIR */
1457
Note: See TracBrowser for help on using the repository browser.