source: thomson/code/C/HxCHost/pff/pff.c@ 645c148

main
Last change on this file since 645c148 was 645c148, checked in by Adrien Destugues <pulkomandy@…>, 12 years ago

Add WIP HxC host software for Thomson computers.
Not working yet because of timing problems...

git-svn-id: svn://localhost/thomson@3 85ae3b6b-dc8f-4344-a89d-598714f2e4e5

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