source: uKadecot/trunk/pfatfs/pff.c@ 107

Last change on this file since 107 was 107, checked in by coas-nagasima, 9 years ago

SHIFT_JISのコードにcharsetプロパティを付けた

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/plain; charset=SHIFT_JIS
File size: 56.3 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#if _USE_LFN
61static WCHAR LfnBuf[_MAX_LFN+1];
62#endif
63
64/*--------------------------------------------------------*/
65/* DBCS code ranges and SBCS extend char conversion table */
66/*--------------------------------------------------------*/
67
68#if _CODE_PAGE == 932 /* Japanese Shift-JIS */
69#define _DF1S 0x81 /* DBC 1st byte range 1 start */
70#define _DF1E 0x9F /* DBC 1st byte range 1 end */
71#define _DF2S 0xE0 /* DBC 1st byte range 2 start */
72#define _DF2E 0xFC /* DBC 1st byte range 2 end */
73#define _DS1S 0x40 /* DBC 2nd byte range 1 start */
74#define _DS1E 0x7E /* DBC 2nd byte range 1 end */
75#define _DS2S 0x80 /* DBC 2nd byte range 2 start */
76#define _DS2E 0xFC /* DBC 2nd byte range 2 end */
77
78#elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
79#define _DF1S 0x81
80#define _DF1E 0xFE
81#define _DS1S 0x40
82#define _DS1E 0x7E
83#define _DS2S 0x80
84#define _DS2E 0xFE
85
86#elif _CODE_PAGE == 949 /* Korean */
87#define _DF1S 0x81
88#define _DF1E 0xFE
89#define _DS1S 0x41
90#define _DS1E 0x5A
91#define _DS2S 0x61
92#define _DS2E 0x7A
93#define _DS3S 0x81
94#define _DS3E 0xFE
95
96#elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */
97#define _DF1S 0x81
98#define _DF1E 0xFE
99#define _DS1S 0x40
100#define _DS1E 0x7E
101#define _DS2S 0xA1
102#define _DS2E 0xFE
103
104#elif _CODE_PAGE == 437 /* U.S. (OEM) */
105#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, \
106 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5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0xFB,0xFC,0xFD,0xFE,0xFF}
115
116#elif _CODE_PAGE == 737 /* Greek (OEM) */
117#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, \
118 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, \
119 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, \
120 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}
121
122#elif _CODE_PAGE == 775 /* Baltic (OEM) */
123#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, \
124 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,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
152#elif _CODE_PAGE == 858 /* Multilingual Latin 1 + Euro (OEM) */
153#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, \
154 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, \
155 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, \
156 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}
157
158#elif _CODE_PAGE == 862 /* Hebrew (OEM) */
159#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, \
160 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,0xB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Windows) */
171#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, \
172 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, \
173 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, \
174 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}
175
176#elif _CODE_PAGE == 1250 /* Central Europe (Windows) */
177#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, \
178 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, \
179 0xC0,0xC1,0xC2,0xC3,0xC4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,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
216 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}
217
218#elif _CODE_PAGE == 1257 /* Baltic (Windows) */
219#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, \
220 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, \
221 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, \
222 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}
223
224#elif _CODE_PAGE == 1258 /* Vietnam (OEM, Windows) */
225#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, \
226 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, \
227 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, \
228 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}
229
230#else
231#error Unknown code page.
232
233#endif
234
235
236
237/* Character code support macros */
238
239#define IsUpper(c) (((c)>='A')&&((c)<='Z'))
240#define IsLower(c) (((c)>='a')&&((c)<='z'))
241
242#ifndef _EXCVT /* DBCS configuration */
243
244#ifdef _DF2S /* Two 1st byte areas */
245#define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
246#else /* One 1st byte area */
247#define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
248#endif
249
250#ifdef _DS3S /* Three 2nd byte areas */
251#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
252#else /* Two 2nd byte areas */
253#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
254#endif
255
256#else /* SBCS configuration */
257
258#define IsDBCS1(c) 0
259#define IsDBCS2(c) 0
260
261#endif /* _EXCVT */
262
263/* Name status flags */
264#define NSFLAG 11 /* Index of name status byte in fn[] */
265#define NS_LOSS 0x01 /* Out of 8.3 format */
266#define NS_LFN 0x02 /* Force to create LFN entry */
267#define NS_LAST 0x04 /* Last segment */
268#define NS_BODY 0x08 /* Lower case flag (body) */
269#define NS_EXT 0x10 /* Lower case flag (ext) */
270#define NS_DOT 0x20 /* Dot entry */
271
272/* FatFs refers the members in the FAT structures with byte offset instead
273/ of structure member because there are incompatibility of the packing option
274/ between various compilers. */
275
276#define BS_jmpBoot 0
277#define BS_OEMName 3
278#define BPB_BytsPerSec 11
279#define BPB_SecPerClus 13
280#define BPB_RsvdSecCnt 14
281#define BPB_NumFATs 16
282#define BPB_RootEntCnt 17
283#define BPB_TotSec16 19
284#define BPB_Media 21
285#define BPB_FATSz16 22
286#define BPB_SecPerTrk 24
287#define BPB_NumHeads 26
288#define BPB_HiddSec 28
289#define BPB_TotSec32 32
290#define BS_55AA 510
291
292#define BS_DrvNum 36
293#define BS_BootSig 38
294#define BS_VolID 39
295#define BS_VolLab 43
296#define BS_FilSysType 54
297
298#define BPB_FATSz32 36
299#define BPB_ExtFlags 40
300#define BPB_FSVer 42
301#define BPB_RootClus 44
302#define BPB_FSInfo 48
303#define BPB_BkBootSec 50
304#define BS_DrvNum32 64
305#define BS_BootSig32 66
306#define BS_VolID32 67
307#define BS_VolLab32 71
308#define BS_FilSysType32 82
309
310#define MBR_Table 446
311
312#define DIR_Name 0
313#define DIR_Attr 11
314#define DIR_NTres 12
315#define DIR_CrtTime 14
316#define DIR_CrtDate 16
317#define DIR_FstClusHI 20
318#define DIR_WrtTime 22
319#define DIR_WrtDate 24
320#define DIR_FstClusLO 26
321#define DIR_FileSize 28
322#define LDIR_Ord 0 /* LFN entry order and LLE flag (1) */
323#define LDIR_Attr 11 /* LFN attribute (1) */
324#define LDIR_Type 12 /* LFN type (1) */
325#define LDIR_Chksum 13 /* Sum of corresponding SFN entry */
326#define LDIR_FstClusLO 26 /* Filled by zero (0) */
327#define SZ_DIR 32 /* Size of a directory entry */
328#define LLE 0x40 /* Last long entry flag in LDIR_Ord */
329#define DDE 0xE5 /* Deleted directory entry mark in DIR_Name[0] */
330#define NDDE 0x05 /* Replacement of the character collides with DDE */
331
332/*--------------------------------------------------------------------------
333
334 Private Functions
335
336---------------------------------------------------------------------------*/
337
338
339/* Fill memory */
340static
341void mem_set (void* dst, int val, int cnt) {
342 char *d = (char*)dst;
343 while (cnt--) *d++ = (char)val;
344}
345
346/* Compare memory to memory */
347static
348int mem_cmp (const void* dst, const void* src, int cnt) {
349 const char *d = (const char *)dst, *s = (const char *)src;
350 int r = 0;
351 while (cnt-- && (r = *d++ - *s++) == 0) ;
352 return r;
353}
354
355/* Check if chr is contained in the string */
356static
357int chk_chr (const char* str, int chr) {
358 while (*str && *str != chr) str++;
359 return *str;
360}
361
362static
363const WCHAR uni2sjis[] = {
364 /* dummy... */
365 0, 0
366};
367
368WCHAR ff_convert ( /* Converted code, 0 means conversion error */
369 WCHAR chr, /* Character code to be converted */
370 UINT dir /* 0: Unicode to OEMCP, 1: OEMCP to Unicode */
371)
372{
373 const WCHAR *p;
374 WCHAR c;
375 int i, n, li, hi;
376
377
378 if (chr <= 0x80) { /* ASCII */
379 c = chr;
380 } else {
381 if (dir) { /* OEMCP to unicode (Incremental search)*/
382 p = &uni2sjis[1];
383 do {
384 c = *p;
385 p += 2;
386 } while (c && c != chr);
387 p -= 3;
388 c = *p;
389 } else { /* Unicode to OEMCP */
390 li = 0; hi = sizeof uni2sjis / 4 - 1;
391 for (n = 16; n; n--) {
392 i = li + (hi - li) / 2;
393 if (chr == uni2sjis[i * 2]) break;
394 if (chr > uni2sjis[i * 2])
395 li = i;
396 else
397 hi = i;
398 }
399 c = n ? uni2sjis[i * 2 + 1] : 0;
400 }
401 }
402
403 return c;
404}
405
406WCHAR ff_wtoupper ( /* Upper converted character */
407 WCHAR chr /* Input character */
408)
409{
410 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 };
411 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 };
412 int i;
413
414
415 for (i = 0; tbl_lower[i] && chr != tbl_lower[i]; i++) ;
416
417 return tbl_lower[i] ? tbl_upper[i] : chr;
418}
419
420/*-----------------------------------------------------------------------*/
421/* LFN handling - Test/Pick/Fit an LFN segment from/to directory entry */
422/*-----------------------------------------------------------------------*/
423#if _USE_LFN
424static
425const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN characters in the directory entry */
426
427
428static
429int cmp_lfn ( /* 1:Matched, 0:Not matched */
430 WCHAR* lfnbuf, /* Pointer to the LFN to be compared */
431 BYTE* dir /* Pointer to the directory entry containing a part of LFN */
432)
433{
434 UINT i, s;
435 WCHAR wc, uc;
436
437 i = ((dir[LDIR_Ord] & ~LLE) - 1) * 13; /* Get offset in the LFN buffer */
438 s = 0; wc = 1;
439 do {
440 uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
441 if (wc) { /* Last character has not been processed */
442 wc = ff_wtoupper(uc); /* Convert it to upper case */
443 if (i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++])) /* Compare it */
444 return 0; /* Not matched */
445 } else {
446 if (uc != 0xFFFF) return 0; /* Check filler */
447 }
448 } while (++s < 13); /* Repeat until all characters in the entry are checked */
449
450 if ((dir[LDIR_Ord] & LLE) && wc && lfnbuf[i]) /* Last segment matched but different length */
451 return 0;
452
453 return 1; /* The part of LFN matched */
454}
455
456static
457int pick_lfn ( /* 1:Succeeded, 0:Buffer overflow */
458 WCHAR* lfnbuf, /* Pointer to the Unicode-LFN buffer */
459 BYTE* dir /* Pointer to the directory entry */
460)
461{
462 UINT i, s;
463 WCHAR wc, uc;
464
465
466 i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
467
468 s = 0; wc = 1;
469 do {
470 uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
471 if (wc) { /* Last character has not been processed */
472 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
473 lfnbuf[i++] = wc = uc; /* Store it */
474 } else {
475 if (uc != 0xFFFF) return 0; /* Check filler */
476 }
477 } while (++s < 13); /* Read all character in the entry */
478
479 if (dir[LDIR_Ord] & LLE) { /* Put terminator if it is the last LFN part */
480 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
481 lfnbuf[i] = 0;
482 }
483
484 return 1;
485}
486#endif
487
488/*-----------------------------------------------------------------------*/
489/* FAT access - Read value of a FAT entry */
490/*-----------------------------------------------------------------------*/
491
492static
493CLUST get_fat ( /* 1:IO error, Else:Cluster status */
494 FATFS *fs,
495 CLUST clst /* Cluster# to get the link information */
496)
497{
498 BYTE buf[4];
499
500 if (clst < 2 || clst >= fs->n_fatent) /* Range check */
501 return 1;
502
503 switch (fs->fs_type) {
504#if _FS_FAT12
505 case FS_FAT12 : {
506 UINT wc, bc, ofs;
507
508 bc = (UINT)clst; bc += bc / 2;
509 ofs = bc % 512; bc /= 512;
510 if (ofs != 511) {
511 if (disk_readp(buf, fs->fatbase + bc, ofs, 2)) break;
512 } else {
513 if (disk_readp(buf, fs->fatbase + bc, 511, 1)) break;
514 if (disk_readp(buf+1, fs->fatbase + bc + 1, 0, 1)) break;
515 }
516 wc = LD_WORD(buf);
517 return (clst & 1) ? (wc >> 4) : (wc & 0xFFF);
518 }
519#endif
520#if _FS_FAT16
521 case FS_FAT16 :
522 if (disk_readp(buf, fs->fatbase + clst / 256, ((UINT)clst % 256) * 2, 2)) break;
523 return LD_WORD(buf);
524#endif
525#if _FS_FAT32
526 case FS_FAT32 :
527 if (disk_readp(buf, fs->fatbase + clst / 128, ((UINT)clst % 128) * 4, 4)) break;
528 return LD_DWORD(buf) & 0x0FFFFFFF;
529#endif
530 }
531
532 return 1; /* An error occured at the disk I/O layer */
533}
534
535
536
537
538/*-----------------------------------------------------------------------*/
539/* Get sector# from cluster# / Get cluster field from directory entry */
540/*-----------------------------------------------------------------------*/
541
542static
543DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */
544 FATFS *fs,
545 CLUST clst /* Cluster# to be converted */
546)
547{
548 clst -= 2;
549 if (clst >= (fs->n_fatent - 2)) return 0; /* Invalid cluster# */
550 return (DWORD)clst * fs->csize + fs->database;
551}
552
553
554static
555CLUST get_clust (
556 FATFS *fs,
557 BYTE* dir /* Pointer to directory entry */
558)
559{
560 CLUST clst = 0;
561
562
563 if (_FS_32ONLY || (_FS_FAT32 && fs->fs_type == FS_FAT32)) {
564 clst = LD_WORD(dir+DIR_FstClusHI);
565 clst <<= 16;
566 }
567 clst |= LD_WORD(dir+DIR_FstClusLO);
568
569 return clst;
570}
571
572
573/*-----------------------------------------------------------------------*/
574/* Directory handling - Rewind directory index */
575/*-----------------------------------------------------------------------*/
576
577static
578FRESULT dir_rewind (
579 DIR *dj /* Pointer to directory object */
580)
581{
582 CLUST clst;
583 FATFS *fs = dj->fs;
584
585
586 dj->index = 0;
587 clst = dj->sclust;
588 if (clst == 1 || clst >= fs->n_fatent) /* Check start cluster range */
589 return FR_DISK_ERR;
590 if (_FS_FAT32 && !clst && (_FS_32ONLY || fs->fs_type == FS_FAT32)) /* Replace cluster# 0 with root cluster# if in FAT32 */
591 clst = (CLUST)fs->dirbase;
592 dj->clust = clst; /* Current cluster */
593 dj->sect = (_FS_32ONLY || clst) ? clust2sect(fs, clst) : fs->dirbase; /* Current sector */
594
595 return FR_OK; /* Seek succeeded */
596}
597
598
599
600
601/*-----------------------------------------------------------------------*/
602/* Directory handling - Move directory index next */
603/*-----------------------------------------------------------------------*/
604
605static
606FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table */
607 DIR *dj /* Pointer to directory object */
608)
609{
610 CLUST clst;
611 WORD i;
612 FATFS *fs = dj->fs;
613
614
615 i = dj->index + 1;
616 if (!i || !dj->sect) /* Report EOT when index has reached 65535 */
617 return FR_NO_FILE;
618
619 if (!(i % 16)) { /* Sector changed? */
620 dj->sect++; /* Next sector */
621
622 if (dj->clust == 0) { /* Static table */
623 if (i >= fs->n_rootdir) /* Report EOT when end of table */
624 return FR_NO_FILE;
625 }
626 else { /* Dynamic table */
627 if (((i / 16) & (fs->csize - 1)) == 0) { /* Cluster changed? */
628 clst = get_fat(fs, dj->clust); /* Get next cluster */
629 if (clst <= 1) return FR_DISK_ERR;
630 if (clst >= fs->n_fatent) /* When it reached end of dynamic table */
631 return FR_NO_FILE; /* Report EOT */
632 dj->clust = clst; /* Initialize data for new cluster */
633 dj->sect = clust2sect(fs, clst);
634 }
635 }
636 }
637
638 dj->index = i;
639
640 return FR_OK;
641}
642
643/*-----------------------------------------------------------------------*/
644/* Calculate sum of an SFN */
645/*-----------------------------------------------------------------------*/
646#if _USE_LFN
647static
648BYTE sum_sfn (
649 const BYTE* dir /* Pointer to the SFN entry */
650)
651{
652 BYTE sum = 0;
653 UINT n = 11;
654
655 do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);
656 return sum;
657}
658#endif
659
660/*-----------------------------------------------------------------------*/
661/* Directory handling - Find an object in the directory */
662/*-----------------------------------------------------------------------*/
663
664static
665FRESULT dir_find (
666 DIR *dj, /* Pointer to the directory object linked to the file name */
667 BYTE *dir /* 32-byte working buffer */
668)
669{
670 FRESULT res;
671 BYTE c;
672#if _USE_LFN
673 BYTE a, ord, sum;
674#endif
675
676 res = dir_rewind(dj); /* Rewind directory object */
677 if (res != FR_OK) return res;
678
679#if _USE_LFN
680 ord = sum = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */
681#endif
682 do {
683 res = disk_readp(dir, dj->sect, (dj->index % 16) * 32, 32) /* Read an entry */
684 ? FR_DISK_ERR : FR_OK;
685 if (res != FR_OK) break;
686 c = dir[DIR_Name]; /* First character */
687 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
688#if _USE_LFN /* LFN configuration */
689 a = dir[DIR_Attr] & AM_MASK;
690 if (c == DDE || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
691 ord = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */
692 } else {
693 if (a == AM_LFN) { /* An LFN entry is found */
694 if (dj->lfn) {
695 if (c & LLE) { /* Is it start of LFN sequence? */
696 sum = dir[LDIR_Chksum];
697 c &= ~LLE; ord = c; /* LFN start order */
698 dj->lfn_idx = dj->index; /* Start index of LFN */
699 }
700 /* Check validity of the LFN entry and compare it with given name */
701 ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
702 }
703 } else { /* An SFN entry is found */
704 if (!ord && sum == sum_sfn(dir)) break; /* LFN matched? */
705 if (!(dj->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dir, dj->fn, 11)) break; /* SFN matched? */
706 ord = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */
707 }
708 }
709#else /* Non LFN configuration */
710 if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */
711 break;
712#endif
713 res = dir_next(dj); /* Next entry */
714 } while (res == FR_OK);
715
716 return res;
717}
718
719
720
721
722/*-----------------------------------------------------------------------*/
723/* Read an object from the directory */
724/*-----------------------------------------------------------------------*/
725#if _USE_DIR
726static
727FRESULT dir_read (
728 DIR *dj, /* Pointer to the directory object to store read object name */
729 BYTE *dir /* 32-byte working buffer */
730)
731{
732 FRESULT res;
733 BYTE a, c;
734#if _USE_LFN
735 BYTE ord = 0xFF, sum = 0xFF;
736#endif
737
738 res = FR_NO_FILE;
739 while (dj->sect) {
740 res = disk_readp(dir, dj->sect, (dj->index % 16) * 32, 32) /* Read an entry */
741 ? FR_DISK_ERR : FR_OK;
742 if (res != FR_OK) break;
743 c = dir[DIR_Name];
744 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
745 a = dir[DIR_Attr] & AM_MASK;
746#if _USE_LFN /* LFN configuration */
747 if (c == DDE || c == '.' || ((a & ~AM_ARC) == AM_VOL)) { /* An entry without valid data */
748 ord = 0xFF;
749 } else {
750 if (a == AM_LFN) { /* An LFN entry is found */
751 if (c & LLE) { /* Is it start of LFN sequence? */
752 sum = dir[LDIR_Chksum];
753 c &= ~LLE; ord = c;
754 dj->lfn_idx = dj->index;
755 }
756 /* Check LFN validity and capture it */
757 ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
758 } else { /* An SFN entry is found */
759 if (ord || sum != sum_sfn(dir)) /* Is there a valid LFN? */
760 dj->lfn_idx = 0xFFFF; /* It has no LFN. */
761 break;
762 }
763 }
764#else /* Non LFN configuration */
765 if (c != DDE && c != '.' && !((a & ~AM_ARC) == AM_VOL)) /* Is it a valid entry? */
766 break;
767#endif
768 res = dir_next(dj); /* Next entry */
769 if (res != FR_OK) break;
770 }
771
772 if (res != FR_OK) dj->sect = 0;
773
774 return res;
775}
776#endif
777
778
779
780/*-----------------------------------------------------------------------*/
781/* Pick a segment and create the object name in directory form */
782/*-----------------------------------------------------------------------*/
783
784
785static
786FRESULT create_name (
787 DIR* dj, /* Pointer to the directory object */
788 const char** path /* Pointer to pointer to the segment in the path string */
789)
790{
791#if _USE_LFN /* LFN configuration */
792 BYTE b, cf;
793 WCHAR w, *lfn;
794 UINT i, ni, si, di;
795 const char *p;
796
797 /* Create LFN in Unicode */
798 for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
799 lfn = dj->lfn;
800 si = di = 0;
801 for (;;) {
802 w = p[si++]; /* Get a character */
803 if (w < ' ' || w == '/' || w == '\\') break; /* Break on end of segment */
804 if (di >= _MAX_LFN) /* Reject too long name */
805 return FR_INVALID_NAME;
806#if !_LFN_UNICODE
807 w &= 0xFF;
808 if (IsDBCS1(w)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
809 b = (BYTE)p[si++]; /* Get 2nd byte */
810 if (!IsDBCS2(b))
811 return FR_INVALID_NAME; /* Reject invalid sequence */
812 w = (w << 8) + b; /* Create a DBC */
813 }
814 w = ff_convert(w, 1); /* Convert ANSI/OEM to Unicode */
815 if (!w) return FR_INVALID_NAME; /* Reject invalid code */
816#endif
817 if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) /* Reject illegal characters for LFN */
818 return FR_INVALID_NAME;
819 lfn[di++] = w; /* Store the Unicode character */
820 }
821 *path = &p[si]; /* Return pointer to the next segment */
822 cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
823 while (di) { /* Strip trailing spaces and dots */
824 w = lfn[di-1];
825 if (w != ' ' && w != '.') break;
826 di--;
827 }
828 if (!di) return FR_INVALID_NAME; /* Reject nul string */
829
830 lfn[di] = 0; /* LFN is created */
831
832 /* Create SFN in directory form */
833 mem_set(dj->fn, ' ', 11);
834 for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */
835 if (si) cf |= NS_LOSS | NS_LFN;
836 while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */
837
838 b = i = 0; ni = 8;
839 for (;;) {
840 w = lfn[si++]; /* Get an LFN character */
841 if (!w) break; /* Break on end of the LFN */
842 if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */
843 cf |= NS_LOSS | NS_LFN; continue;
844 }
845
846 if (i >= ni || si == di) { /* Extension or end of SFN */
847 if (ni == 11) { /* Long extension */
848 cf |= NS_LOSS | NS_LFN; break;
849 }
850 if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */
851 if (si > di) break; /* No extension */
852 si = di; i = 8; ni = 11; /* Enter extension section */
853 b <<= 2; continue;
854 }
855
856 if (w >= 0x80) { /* Non ASCII character */
857#ifdef _EXCVT
858 w = ff_convert(w, 0); /* Unicode -> OEM code */
859 if (w) w = ExCvt[w - 0x80]; /* Convert extended character to upper (SBCS) */
860#else
861 w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */
862#endif
863 cf |= NS_LFN; /* Force create LFN entry */
864 }
865
866 if (_DF1S && w >= 0x100) { /* Double byte character (always false on SBCS cfg) */
867 if (i >= ni - 1) {
868 cf |= NS_LOSS | NS_LFN; i = ni; continue;
869 }
870 dj->fn[i++] = (BYTE)(w >> 8);
871 } else { /* Single byte character */
872 if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal characters for SFN */
873 w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */
874 } else {
875 if (IsUpper(w)) { /* ASCII large capital */
876 b |= 2;
877 } else {
878 if (IsLower(w)) { /* ASCII small capital */
879 b |= 1; w -= 0x20;
880 }
881 }
882 }
883 }
884 dj->fn[i++] = (BYTE)w;
885 }
886
887 if (dj->fn[0] == DDE) dj->fn[0] = NDDE; /* If the first character collides with deleted mark, replace it with 0x05 */
888
889 if (ni == 8) b <<= 2;
890 if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) /* Create LFN entry when there are composite capitals */
891 cf |= NS_LFN;
892 if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended character, NT flags are created */
893 if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */
894 if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */
895 }
896
897 dj->fn[NSFLAG] = cf; /* SFN is created */
898
899 return FR_OK;
900
901
902#else /* Non-LFN configuration */
903 BYTE c, ni, si, i, *sfn;
904 const char *p;
905#if _USE_LCC
906#ifdef _EXCVT
907 static const BYTE cvt[] = _EXCVT;
908#endif
909#endif
910
911 /* Create file name in directory form */
912 sfn = dj->fn;
913 mem_set(sfn, ' ', 11);
914 si = i = 0; ni = 8;
915 p = *path;
916 for (;;) {
917 c = p[si++];
918 if (c <= ' ' || c == '/') break; /* Break on end of segment */
919 if (c == '.' || i >= ni) {
920 if (ni != 8 || c != '.') break;
921 i = 8; ni = 11;
922 continue;
923 }
924#if _USE_LCC
925#ifdef _EXCVT
926 if (c >= 0x80) /* To upper extended char (SBCS) */
927 c = cvt[c - 0x80];
928#endif
929 if (IsDBCS1(c) && i < ni - 1) { /* DBC 1st byte? */
930 BYTE d = p[si++]; /* Get 2nd byte */
931 sfn[i++] = c;
932 sfn[i++] = d;
933 } else
934#endif
935 { /* Single byte code */
936 if (_USE_LCC && IsLower(c)) c -= 0x20; /* toupper */
937 sfn[i++] = c;
938 }
939 }
940 *path = &p[si]; /* Rerurn pointer to the next segment */
941
942 sfn[11] = (c <= ' ') ? 1 : 0; /* Set last segment flag if end of path */
943
944 return FR_OK;
945#endif
946}
947
948
949
950
951/*-----------------------------------------------------------------------*/
952/* Get file information from directory entry */
953/*-----------------------------------------------------------------------*/
954#if _USE_DIR
955static
956void get_fileinfo ( /* No return code */
957 DIR *dj, /* Pointer to the directory object */
958 BYTE *dir, /* 32-byte working buffer */
959 FILINFO *fno /* Pointer to store the file information */
960)
961{
962 BYTE i, c;
963 char *p;
964
965
966 p = fno->fname;
967 if (dj->sect) {
968 i = 0;
969 while (i < 11) { /* Copy name body and extension */
970 c = dir[i++];
971 if (c == ' ') continue; /* Skip padding spaces */
972 if (c == NDDE) c = DDE; /* Restore replaced DDE character */
973 if (i == 9) *p++ = '.'; /* Insert a . if extension is exist */
974#if _USE_LFN
975 if (IsUpper(c) && (dir[DIR_NTres] & (i >= 9 ? NS_EXT : NS_BODY)))
976 c += 0x20; /* To lower */
977#endif
978 *p++ = c;
979 }
980 fno->fattrib = dir[DIR_Attr]; /* Attribute */
981 fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */
982 fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */
983 fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */
984 }
985 *p = 0;
986
987#if _USE_LFN
988 if (fno->lfname) {
989 WCHAR w, *lfn;
990
991 i = 0; p = fno->lfname;
992 if (dj->sect && fno->lfsize && dj->lfn_idx != 0xFFFF) { /* Get LFN if available */
993 lfn = dj->lfn;
994 while ((w = *lfn++) != 0) { /* Get an LFN character */
995#if !_LFN_UNICODE
996 w = ff_convert(w, 0); /* Unicode -> OEM */
997 if (!w) { i = 0; break; } /* No LFN if it could not be converted */
998 if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC (always false on SBCS cfg) */
999 p[i++] = (w >> 8);
1000#endif
1001 if (i >= fno->lfsize - 1) { i = 0; break; } /* No LFN if buffer overflow */
1002 p[i++] = w;
1003 }
1004 }
1005 p[i] = 0; /* Terminate LFN string by a \0 */
1006 }
1007#endif
1008}
1009#endif /* _USE_DIR */
1010
1011
1012
1013/*-----------------------------------------------------------------------*/
1014/* Follow a file path */
1015/*-----------------------------------------------------------------------*/
1016
1017static
1018FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
1019 DIR* dj, /* Directory object to return last directory and found object */
1020 BYTE *dir, /* 32-byte working buffer */
1021 const char* path /* Full-path string to find a file or directory */
1022)
1023{
1024 FRESULT res;
1025 BYTE ns;
1026 FATFS *fs = dj->fs;
1027
1028
1029 while (*path == ' ') path++; /* Strip leading spaces */
1030 if (*path == '/') path++; /* Strip heading separator if exist */
1031 dj->sclust = 0; /* Set start directory (always root dir) */
1032
1033 if ((BYTE)*path < ' ') { /* Null path means the root directory */
1034 res = dir_rewind(dj);
1035 dir[0] = 0;
1036
1037 } else { /* Follow path */
1038 for (;;) {
1039 res = create_name(dj, &path); /* Get a segment */
1040 if (res != FR_OK) break;
1041 res = dir_find(dj, dir); /* Find it */
1042 ns = dj->fn[NSFLAG];
1043 if (res != FR_OK) { /* Failed to find the object */
1044 if (res == FR_NO_FILE) { /* Object is not found */
1045 if (!(ns & NS_LAST)) res = FR_NO_PATH; /* Adjust error code if not last segment */
1046 }
1047 break;
1048 }
1049 if (ns & NS_LAST) break; /* Last segment matched. Function completed. */
1050 if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow path because it is a file */
1051 res = FR_NO_PATH; break;
1052 }
1053 dj->sclust = get_clust(fs, dir); /* Follow next */
1054 }
1055 }
1056
1057 return res;
1058}
1059
1060
1061
1062
1063/*-----------------------------------------------------------------------*/
1064/* Check a sector if it is an FAT boot record */
1065/*-----------------------------------------------------------------------*/
1066
1067static
1068BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record, 3:Error */
1069 FATFS *fs,
1070 BYTE *buf, /* Working buffer */
1071 DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
1072)
1073{
1074 if (disk_readp(buf, sect, 510, 2)) /* Read the boot record */
1075 return 3;
1076 if (LD_WORD(buf) != 0xAA55) /* Check record signature */
1077 return 2;
1078
1079 if (!_FS_32ONLY && !disk_readp(buf, sect, BS_FilSysType, 2) && LD_WORD(buf) == 0x4146) /* Check FAT12/16 */
1080 return 0;
1081 if (_FS_FAT32 && !disk_readp(buf, sect, BS_FilSysType32, 2) && LD_WORD(buf) == 0x4146) /* Check FAT32 */
1082 return 0;
1083 return 1;
1084}
1085
1086
1087
1088
1089/*--------------------------------------------------------------------------
1090
1091 Public Functions
1092
1093--------------------------------------------------------------------------*/
1094
1095
1096
1097/*-----------------------------------------------------------------------*/
1098/* Mount/Unmount a Locical Drive */
1099/*-----------------------------------------------------------------------*/
1100
1101FRESULT pf_mount (
1102 FATFS *fs /* Pointer to new file system object */
1103)
1104{
1105 BYTE fmt, buf[36];
1106 DWORD bsect, fsize, tsect, mclst;
1107
1108 if (disk_get_status() & STA_NOINIT) /* Check if the drive is ready or not */
1109 return FR_NOT_READY;
1110
1111 /* Search FAT partition on the drive */
1112 bsect = 0;
1113 fmt = check_fs(fs, buf, bsect); /* Check sector 0 as an SFD format */
1114 if (fmt == 1) { /* Not an FAT boot record, it may be FDISK format */
1115 /* Check a partition listed in top of the partition table */
1116 if (disk_readp(buf, bsect, MBR_Table, 16)) { /* 1st partition entry */
1117 fmt = 3;
1118 } else {
1119 if (buf[4]) { /* Is the partition existing? */
1120 bsect = LD_DWORD(&buf[8]); /* Partition offset in LBA */
1121 fmt = check_fs(fs, buf, bsect); /* Check the partition */
1122 }
1123 }
1124 }
1125 if (fmt == 3) return FR_DISK_ERR;
1126 if (fmt) return FR_NO_FILESYSTEM; /* No valid FAT patition is found */
1127
1128 /* Initialize the file system object */
1129 if (disk_readp(buf, bsect, 13, sizeof (buf))) return FR_DISK_ERR;
1130
1131 fsize = LD_WORD(buf+BPB_FATSz16-13); /* Number of sectors per FAT */
1132 if (!fsize) fsize = LD_DWORD(buf+BPB_FATSz32-13);
1133
1134 fsize *= buf[BPB_NumFATs-13]; /* Number of sectors in FAT area */
1135 fs->fatbase = bsect + LD_WORD(buf+BPB_RsvdSecCnt-13); /* FAT start sector (lba) */
1136 fs->csize = buf[BPB_SecPerClus-13]; /* Number of sectors per cluster */
1137 fs->n_rootdir = LD_WORD(buf+BPB_RootEntCnt-13); /* Nmuber of root directory entries */
1138 tsect = LD_WORD(buf+BPB_TotSec16-13); /* Number of sectors on the file system */
1139 if (!tsect) tsect = LD_DWORD(buf+BPB_TotSec32-13);
1140 mclst = (tsect /* Last cluster# + 1 */
1141 - LD_WORD(buf+BPB_RsvdSecCnt-13) - fsize - fs->n_rootdir / 16
1142 ) / fs->csize + 2;
1143 fs->n_fatent = (CLUST)mclst;
1144
1145 fmt = 0; /* Determine the FAT sub type */
1146 if (_FS_FAT12 && mclst < 0xFF7)
1147 fmt = FS_FAT12;
1148 if (_FS_FAT16 && mclst >= 0xFF8 && mclst < 0xFFF7)
1149 fmt = FS_FAT16;
1150 if (_FS_FAT32 && mclst >= 0xFFF7)
1151 fmt = FS_FAT32;
1152 if (!fmt) return FR_NO_FILESYSTEM;
1153 fs->fs_type = fmt;
1154
1155 if (_FS_32ONLY || (_FS_FAT32 && fmt == FS_FAT32))
1156 fs->dirbase = LD_DWORD(buf+(BPB_RootClus-13)); /* Root directory start cluster */
1157 else
1158 fs->dirbase = fs->fatbase + fsize; /* Root directory start sector (lba) */
1159 fs->database = fs->fatbase + fsize + fs->n_rootdir / 16; /* Data start sector (lba) */
1160
1161 fs->flag = 0;
1162
1163 return FR_OK;
1164}
1165
1166
1167
1168
1169/*-----------------------------------------------------------------------*/
1170/* Open or Create a File */
1171/*-----------------------------------------------------------------------*/
1172
1173FRESULT pf_open (
1174 FATFS *fs,
1175 const char *path /* Pointer to the file name */
1176)
1177{
1178 FRESULT res;
1179 DIR dj;
1180 BYTE sp[12], dir[32];
1181
1182
1183 if (!fs) return FR_NOT_ENABLED; /* Check file system */
1184
1185 fs->flag = 0;
1186 dj.fs = fs;
1187 dj.fn = sp;
1188#if _USE_LFN
1189 dj.lfn = LfnBuf;
1190#endif
1191 res = follow_path(&dj, dir, path); /* Follow the file path */
1192 if (res != FR_OK) return res; /* Follow failed */
1193 if (!dir[0] || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */
1194 return FR_NO_FILE;
1195
1196 fs->org_clust = get_clust(fs, dir); /* File start cluster */
1197 fs->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */
1198 fs->fptr = 0; /* File pointer */
1199 fs->flag = FA_OPENED;
1200
1201 return FR_OK;
1202}
1203
1204
1205
1206
1207/*-----------------------------------------------------------------------*/
1208/* Read File */
1209/*-----------------------------------------------------------------------*/
1210#if _USE_READ
1211
1212FRESULT pf_read (
1213 FATFS *fs,
1214 void* buff, /* Pointer to the read buffer (NULL:Forward data to the stream)*/
1215 UINT btr, /* Number of bytes to read */
1216 UINT* br /* Pointer to number of bytes read */
1217)
1218{
1219 DRESULT dr;
1220 CLUST clst;
1221 DWORD sect, remain;
1222 UINT rcnt;
1223 BYTE cs, *rbuff = buff;
1224
1225
1226 *br = 0;
1227 if (!fs) return FR_NOT_ENABLED; /* Check file system */
1228 if (!(fs->flag & FA_OPENED)) /* Check if opened */
1229 return FR_NOT_OPENED;
1230
1231 remain = fs->fsize - fs->fptr;
1232 if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
1233
1234 while (btr) { /* Repeat until all data transferred */
1235 if ((fs->fptr % 512) == 0) { /* On the sector boundary? */
1236 cs = (BYTE)(fs->fptr / 512 & (fs->csize - 1)); /* Sector offset in the cluster */
1237 if (!cs) { /* On the cluster boundary? */
1238 if (fs->fptr == 0) /* On the top of the file? */
1239 clst = fs->org_clust;
1240 else
1241 clst = get_fat(fs, fs->curr_clust);
1242 if (clst <= 1) ABORT(FR_DISK_ERR);
1243 fs->curr_clust = clst; /* Update current cluster */
1244 }
1245 sect = clust2sect(fs, fs->curr_clust); /* Get current sector */
1246 if (!sect) ABORT(FR_DISK_ERR);
1247 fs->dsect = sect + cs;
1248 }
1249 rcnt = 512 - (UINT)fs->fptr % 512; /* Get partial sector data from sector buffer */
1250 if (rcnt > btr) rcnt = btr;
1251 dr = disk_readp(!buff ? 0 : rbuff, fs->dsect, (UINT)fs->fptr % 512, rcnt);
1252 if (dr) ABORT(FR_DISK_ERR);
1253 fs->fptr += rcnt; rbuff += rcnt; /* Update pointers and counters */
1254 btr -= rcnt; *br += rcnt;
1255 }
1256
1257 return FR_OK;
1258}
1259#endif
1260
1261
1262
1263/*-----------------------------------------------------------------------*/
1264/* Write File */
1265/*-----------------------------------------------------------------------*/
1266#if _USE_WRITE
1267
1268FRESULT pf_write (
1269 FATFS *fs,
1270 const void* buff, /* Pointer to the data to be written */
1271 UINT btw, /* Number of bytes to write (0:Finalize the current write operation) */
1272 UINT* bw /* Pointer to number of bytes written */
1273)
1274{
1275 CLUST clst;
1276 DWORD sect, remain;
1277 const BYTE *p = buff;
1278 BYTE cs;
1279 UINT wcnt;
1280
1281
1282 *bw = 0;
1283 if (!fs) return FR_NOT_ENABLED; /* Check file system */
1284 if (!(fs->flag & FA_OPENED)) /* Check if opened */
1285 return FR_NOT_OPENED;
1286
1287 if (!btw) { /* Finalize request */
1288 if ((fs->flag & FA__WIP) && disk_writep(0, 0)) ABORT(FR_DISK_ERR);
1289 fs->flag &= ~FA__WIP;
1290 return FR_OK;
1291 } else { /* Write data request */
1292 if (!(fs->flag & FA__WIP)) /* Round-down fptr to the sector boundary */
1293 fs->fptr &= 0xFFFFFE00;
1294 }
1295 remain = fs->fsize - fs->fptr;
1296 if (btw > remain) btw = (UINT)remain; /* Truncate btw by remaining bytes */
1297
1298 while (btw) { /* Repeat until all data transferred */
1299 if ((UINT)fs->fptr % 512 == 0) { /* On the sector boundary? */
1300 cs = (BYTE)(fs->fptr / 512 & (fs->csize - 1)); /* Sector offset in the cluster */
1301 if (!cs) { /* On the cluster boundary? */
1302 if (fs->fptr == 0) /* On the top of the file? */
1303 clst = fs->org_clust;
1304 else
1305 clst = get_fat(fs, fs->curr_clust);
1306 if (clst <= 1) ABORT(FR_DISK_ERR);
1307 fs->curr_clust = clst; /* Update current cluster */
1308 }
1309 sect = clust2sect(fs, fs->curr_clust); /* Get current sector */
1310 if (!sect) ABORT(FR_DISK_ERR);
1311 fs->dsect = sect + cs;
1312 if (disk_writep(0, fs->dsect)) ABORT(FR_DISK_ERR); /* Initiate a sector write operation */
1313 fs->flag |= FA__WIP;
1314 }
1315 wcnt = 512 - (UINT)fs->fptr % 512; /* Number of bytes to write to the sector */
1316 if (wcnt > btw) wcnt = btw;
1317 if (disk_writep(p, wcnt)) ABORT(FR_DISK_ERR); /* Send data to the sector */
1318 fs->fptr += wcnt; p += wcnt; /* Update pointers and counters */
1319 btw -= wcnt; *bw += wcnt;
1320 if ((UINT)fs->fptr % 512 == 0) {
1321 if (disk_writep(0, 0)) ABORT(FR_DISK_ERR); /* Finalize the currtent secter write operation */
1322 fs->flag &= ~FA__WIP;
1323 }
1324 }
1325
1326 return FR_OK;
1327}
1328#endif
1329
1330
1331
1332/*-----------------------------------------------------------------------*/
1333/* Seek File R/W Pointer */
1334/*-----------------------------------------------------------------------*/
1335#if _USE_LSEEK
1336
1337FRESULT pf_lseek (
1338 FATFS *fs,
1339 DWORD ofs /* File pointer from top of file */
1340)
1341{
1342 CLUST clst;
1343 DWORD bcs, sect, ifptr;
1344
1345
1346 if (!fs) return FR_NOT_ENABLED; /* Check file system */
1347 if (!(fs->flag & FA_OPENED)) /* Check if opened */
1348 return FR_NOT_OPENED;
1349
1350 if (ofs > fs->fsize) ofs = fs->fsize; /* Clip offset with the file size */
1351 ifptr = fs->fptr;
1352 fs->fptr = 0;
1353 if (ofs > 0) {
1354 bcs = (DWORD)fs->csize * 512; /* Cluster size (byte) */
1355 if (ifptr > 0 &&
1356 (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */
1357 fs->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */
1358 ofs -= fs->fptr;
1359 clst = fs->curr_clust;
1360 } else { /* When seek to back cluster, */
1361 clst = fs->org_clust; /* start from the first cluster */
1362 fs->curr_clust = clst;
1363 }
1364 while (ofs > bcs) { /* Cluster following loop */
1365 clst = get_fat(fs, clst); /* Follow cluster chain */
1366 if (clst <= 1 || clst >= fs->n_fatent) ABORT(FR_DISK_ERR);
1367 fs->curr_clust = clst;
1368 fs->fptr += bcs;
1369 ofs -= bcs;
1370 }
1371 fs->fptr += ofs;
1372 sect = clust2sect(fs, clst); /* Current sector */
1373 if (!sect) ABORT(FR_DISK_ERR);
1374 fs->dsect = sect + (fs->fptr / 512 & (fs->csize - 1));
1375 }
1376
1377 return FR_OK;
1378}
1379#endif
1380
1381
1382
1383/*-----------------------------------------------------------------------*/
1384/* Create a Directroy Object */
1385/*-----------------------------------------------------------------------*/
1386#if _USE_DIR
1387
1388FRESULT pf_opendir (
1389 DIR *dj, /* Pointer to directory object to create */
1390 const char *path /* Pointer to the directory path */
1391)
1392{
1393 FRESULT res;
1394 BYTE sp[12], dir[32];
1395 FATFS *fs = dj->fs;
1396
1397
1398 if (!fs) { /* Check file system */
1399 res = FR_NOT_ENABLED;
1400 } else {
1401 dj->fn = sp;
1402#if _USE_LFN
1403 dj->lfn = LfnBuf;
1404#endif
1405 res = follow_path(dj, dir, path); /* Follow the path to the directory */
1406 if (res == FR_OK) { /* Follow completed */
1407 if (dir[0]) { /* It is not the root dir */
1408 if (dir[DIR_Attr] & AM_DIR) /* The object is a directory */
1409 dj->sclust = get_clust(fs, dir);
1410 else /* The object is not a directory */
1411 res = FR_NO_FILE;
1412 }
1413 if (res == FR_OK)
1414 res = dir_rewind(dj); /* Rewind dir */
1415 }
1416 }
1417
1418 return res;
1419}
1420
1421
1422
1423
1424/*-----------------------------------------------------------------------*/
1425/* Read Directory Entry in Sequense */
1426/*-----------------------------------------------------------------------*/
1427
1428FRESULT pf_readdir (
1429 DIR *dj, /* Pointer to the open directory object */
1430 FILINFO *fno /* Pointer to file information to return */
1431)
1432{
1433 FRESULT res;
1434 BYTE sp[12], dir[32];
1435 FATFS *fs = dj->fs;
1436
1437
1438 if (!fs) { /* Check file system */
1439 res = FR_NOT_ENABLED;
1440 } else {
1441 dj->fn = sp;
1442 if (!fno) {
1443 res = dir_rewind(dj);
1444 } else {
1445 res = dir_read(dj, dir); /* Get current directory item */
1446 if (res == FR_NO_FILE) res = FR_OK;
1447 if (res == FR_OK) { /* A valid entry is found */
1448 get_fileinfo(dj, dir, fno); /* Get the object information */
1449 res = dir_next(dj); /* Increment read index for next */
1450 if (res == FR_NO_FILE) res = FR_OK;
1451 }
1452 }
1453 }
1454
1455 return res;
1456}
1457
1458#endif /* _USE_DIR */
1459
Note: See TracBrowser for help on using the repository browser.