Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
248 Kevin 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,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \
100
				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, \
101
				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, \
102
				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}
103
 
104
#elif _CODE_PAGE == 775	/* Baltic (OEM) */
105
#define _DF1S	0
106
#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, \
107
				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, \
108
				0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
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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+ Euro (OEM) */
140
#define _DF1S	0
141
#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, \
142
				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, \
143
				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, \
144
				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}
145
 
146
#elif _CODE_PAGE == 862	/* Hebrew (OEM) */
147
#define _DF1S	0
148
#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, \
149
				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, \
150
				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, \
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,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, \
163
				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, \
164
				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, \
165
				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}
166
 
167
#elif _CODE_PAGE == 1250 /* Central Europe (Windows) */
168
#define _DF1S	0
169
#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, \
170
				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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xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF}
180
 
181
#elif _CODE_PAGE == 1252 /* Latin 1 (Windows) */
182
#define _DF1S	0
183
#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0xAd,0x9B,0x8C,0x9D,0xAE,0x9F, \
184
				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, \
185
				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, \
186
				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}
187
 
188
#elif _CODE_PAGE == 1253 /* Greek (Windows) */
189
#define _DF1S	0
190
#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, \
191
				0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,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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 _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, \
205
				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, \
206
				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, \
207
				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}
208
 
209
#elif _CODE_PAGE == 1256 /* Arabic (Windows) */
210
#define _DF1S	0
211
#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x8C,0x9D,0x9E,0x9F, \
212
				0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,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CD,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
 
327
static
328
FATFS *FatFs;	/* Pointer to the file system object (logical drive) */
329
 
330
 
331
/* Fill memory */
332
static
333
void 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 */
339
static
340
int 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
 
353
static
354
CLUST get_fat (	/* 1:IO error, Else:Cluster status */
355
	CLUST clst	/* Cluster# to get the link information */
356
)
357
{
358
	WORD wc, bc, ofs;
359
	BYTE buf[4];
360
	FATFS *fs = FatFs;
361
 
362
 
363
	if (clst < 2 || clst >= fs->n_fatent)	/* Range check */
364
		return 1;
365
 
366
	switch (fs->fs_type) {
367
#if _FS_FAT12
368
	case FS_FAT12 :
369
		bc = (WORD)clst; bc += bc / 2;
370
		ofs = bc % 512; bc /= 512;
371
		if (ofs != 511) {
372
			if (disk_readp(buf, fs->fatbase + bc, ofs, 2)) break;
373
		} else {
374
			if (disk_readp(buf, fs->fatbase + bc, 511, 1)) break;
375
			if (disk_readp(buf+1, fs->fatbase + bc + 1, 0, 1)) break;
376
		}
377
		wc = LD_WORD(buf);
378
		return (clst & 1) ? (wc >> 4) : (wc & 0xFFF);
379
#endif
380
	case FS_FAT16 :
381
		if (disk_readp(buf, fs->fatbase + clst / 256, (WORD)(((WORD)clst % 256) * 2), 2)) break;
382
		return LD_WORD(buf);
383
#if _FS_FAT32
384
	case FS_FAT32 :
385
		if (disk_readp(buf, fs->fatbase + clst / 128, (WORD)(((WORD)clst % 128) * 4), 4)) break;
386
		return LD_DWORD(buf) & 0x0FFFFFFF;
387
#endif
388
	}
389
 
390
	return 1;	/* An error occured at the disk I/O layer */
391
}
392
 
393
 
394
 
395
 
396
/*-----------------------------------------------------------------------*/
397
/* Get sector# from cluster#                                             */
398
/*-----------------------------------------------------------------------*/
399
 
400
static
401
DWORD clust2sect (	/* !=0: Sector number, 0: Failed - invalid cluster# */
402
	CLUST clst		/* Cluster# to be converted */
403
)
404
{
405
	FATFS *fs = FatFs;
406
 
407
 
408
	clst -= 2;
409
	if (clst >= (fs->n_fatent - 2)) return 0;		/* Invalid cluster# */
410
	return (DWORD)clst * fs->csize + fs->database;
411
}
412
 
413
 
414
 
415
 
416
/*-----------------------------------------------------------------------*/
417
/* Directory handling - Rewind directory index                           */
418
/*-----------------------------------------------------------------------*/
419
 
420
static
421
FRESULT dir_rewind (
422
	DIR *dj			/* Pointer to directory object */
423
)
424
{
425
	CLUST clst;
426
	FATFS *fs = FatFs;
427
 
428
 
429
	dj->index = 0;
430
	clst = dj->sclust;
431
	if (clst == 1 || clst >= fs->n_fatent)	/* Check start cluster range */
432
		return FR_DISK_ERR;
433
	if (_FS_FAT32 && !clst && fs->fs_type == FS_FAT32)	/* Replace cluster# 0 with root cluster# if in FAT32 */
434
		clst = (CLUST)fs->dirbase;
435
	dj->clust = clst;						/* Current cluster */
436
	dj->sect = clst ? clust2sect(clst) : fs->dirbase;	/* Current sector */
437
 
438
	return FR_OK;	/* Seek succeeded */
439
}
440
 
441
 
442
 
443
 
444
/*-----------------------------------------------------------------------*/
445
/* Directory handling - Move directory index next                        */
446
/*-----------------------------------------------------------------------*/
447
 
448
static
449
FRESULT dir_next (	/* FR_OK:Succeeded, FR_NO_FILE:End of table */
450
	DIR *dj			/* Pointer to directory object */
451
)
452
{
453
	CLUST clst;
454
	WORD i;
455
	FATFS *fs = FatFs;
456
 
457
 
458
	i = dj->index + 1;
459
	if (!i || !dj->sect)	/* Report EOT when index has reached 65535 */
460
		return FR_NO_FILE;
461
 
462
	if (!(i % 16)) {		/* Sector changed? */
463
		dj->sect++;			/* Next sector */
464
 
465
		if (dj->clust == 0) {	/* Static table */
466
			if (i >= fs->n_rootdir)	/* Report EOT when end of table */
467
				return FR_NO_FILE;
468
		}
469
		else {					/* Dynamic table */
470
			if (((i / 16) & (fs->csize-1)) == 0) {	/* Cluster changed? */
471
				clst = get_fat(dj->clust);		/* Get next cluster */
472
				if (clst <= 1) return FR_DISK_ERR;
473
				if (clst >= fs->n_fatent)		/* When it reached end of dynamic table */
474
					return FR_NO_FILE;			/* Report EOT */
475
				dj->clust = clst;				/* Initialize data for new cluster */
476
				dj->sect = clust2sect(clst);
477
			}
478
		}
479
	}
480
 
481
	dj->index = i;
482
 
483
	return FR_OK;
484
}
485
 
486
 
487
 
488
 
489
/*-----------------------------------------------------------------------*/
490
/* Directory handling - Find an object in the directory                  */
491
/*-----------------------------------------------------------------------*/
492
 
493
static
494
FRESULT dir_find (
495
	DIR *dj,		/* Pointer to the directory object linked to the file name */
496
	BYTE *dir		/* 32-byte working buffer */
497
)
498
{
499
	FRESULT res;
500
	BYTE c;
501
 
502
 
503
	res = dir_rewind(dj);			/* Rewind directory object */
504
	if (res != FR_OK) return res;
505
 
506
	do {
507
		res = disk_readp(dir, dj->sect, (WORD)((dj->index % 16) * 32), 32)	/* Read an entry */
508
			? FR_DISK_ERR : FR_OK;
509
		if (res != FR_OK) break;
510
		c = dir[DIR_Name];	/* First character */
511
		if (c == 0) { res = FR_NO_FILE; break; }	/* Reached to end of table */
512
		if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */
513
			break;
514
		res = dir_next(dj);					/* Next entry */
515
	} while (res == FR_OK);
516
 
517
	return res;
518
}
519
 
520
 
521
 
522
 
523
/*-----------------------------------------------------------------------*/
524
/* Read an object from the directory                                     */
525
/*-----------------------------------------------------------------------*/
526
#if _USE_DIR
527
static
528
FRESULT dir_read (
529
	DIR *dj,		/* Pointer to the directory object to store read object name */
530
	BYTE *dir		/* 32-byte working buffer */
531
)
532
{
533
	FRESULT res;
534
	BYTE a, c;
535
 
536
 
537
	res = FR_NO_FILE;
538
	while (dj->sect) {
539
		res = disk_readp(dir, dj->sect, (WORD)((dj->index % 16) * 32), 32)	/* Read an entry */
540
			? FR_DISK_ERR : FR_OK;
541
		if (res != FR_OK) break;
542
		c = dir[DIR_Name];
543
		if (c == 0) { res = FR_NO_FILE; break; }	/* Reached to end of table */
544
		a = dir[DIR_Attr] & AM_MASK;
545
		if (c != 0xE5 && c != '.' && !(a & AM_VOL))	/* Is it a valid entry? */
546
			break;
547
		res = dir_next(dj);			/* Next entry */
548
		if (res != FR_OK) break;
549
	}
550
 
551
	if (res != FR_OK) dj->sect = 0;
552
 
553
	return res;
554
}
555
#endif
556
 
557
 
558
 
559
/*-----------------------------------------------------------------------*/
560
/* Pick a segment and create the object name in directory form           */
561
/*-----------------------------------------------------------------------*/
562
 
563
#ifdef _EXCVT
564
	static const BYTE cvt[] = _EXCVT;
565
#endif
566
 
567
static
568
FRESULT create_name (
569
	DIR *dj,			/* Pointer to the directory object */
570
	const char **path	/* Pointer to pointer to the segment in the path string */
571
)
572
{
573
	BYTE c, d, ni, si, i, *sfn;
574
	const char *p;
575
 
576
	/* Create file name in directory form */
577
	sfn = dj->fn;
578
	mem_set(sfn, ' ', 11);
579
	si = i = 0; ni = 8;
580
	p = *path;
581
	for (;;) {
582
		c = p[si++];
583
		if (c <= ' ' || c == '/') break;	/* Break on end of segment */
584
		if (c == '.' || i >= ni) {
585
			if (ni != 8 || c != '.') break;
586
			i = 8; ni = 11;
587
			continue;
588
		}
589
#ifdef _EXCVT
590
		if (c >= 0x80)					/* To upper extended char (SBCS) */
591
			c = cvt[c - 0x80];
592
#endif
593
		if (IsDBCS1(c) && i < ni - 1) {	/* DBC 1st byte? */
594
			d = p[si++];				/* Get 2nd byte */
595
			sfn[i++] = c;
596
			sfn[i++] = d;
597
		} else {						/* Single byte code */
598
			if (IsLower(c)) c -= 0x20;	/* toupper */
599
			sfn[i++] = c;
600
		}
601
	}
602
	*path = &p[si];						/* Rerurn pointer to the next segment */
603
 
604
	sfn[11] = (c <= ' ') ? 1 : 0;		/* Set last segment flag if end of path */
605
 
606
	return FR_OK;
607
}
608
 
609
 
610
 
611
 
612
/*-----------------------------------------------------------------------*/
613
/* Get file information from directory entry                             */
614
/*-----------------------------------------------------------------------*/
615
#if _USE_DIR
616
static
617
void get_fileinfo (		/* No return code */
618
	DIR *dj,			/* Pointer to the directory object */
619
	BYTE *dir,			/* 32-byte working buffer */
620
	FILINFO *fno	 	/* Pointer to store the file information */
621
)
622
{
623
	BYTE i, c;
624
	char *p;
625
 
626
 
627
	p = fno->fname;
628
	if (dj->sect) {
629
		for (i = 0; i < 8; i++) {	/* Copy file name body */
630
			c = dir[i];
631
			if (c == ' ') break;
632
			if (c == 0x05) c = 0xE5;
633
			*p++ = c;
634
		}
635
		if (dir[8] != ' ') {		/* Copy file name extension */
636
			*p++ = '.';
637
			for (i = 8; i < 11; i++) {
638
				c = dir[i];
639
				if (c == ' ') break;
640
				*p++ = c;
641
			}
642
		}
643
		fno->fattrib = dir[DIR_Attr];				/* Attribute */
644
		fno->fsize = LD_DWORD(dir+DIR_FileSize);	/* Size */
645
		fno->fdate = LD_WORD(dir+DIR_WrtDate);		/* Date */
646
		fno->ftime = LD_WORD(dir+DIR_WrtTime);		/* Time */
647
	}
648
	*p = 0;
649
}
650
#endif /* _USE_DIR */
651
 
652
 
653
 
654
/*-----------------------------------------------------------------------*/
655
/* Follow a file path                                                    */
656
/*-----------------------------------------------------------------------*/
657
 
658
static
659
FRESULT follow_path (	/* FR_OK(0): successful, !=0: error code */
660
	DIR *dj,			/* Directory object to return last directory and found object */
661
	BYTE *dir,			/* 32-byte working buffer */
662
	const char *path	/* Full-path string to find a file or directory */
663
)
664
{
665
	FRESULT res;
666
 
667
 
668
	while (*path == ' ') path++;		/* Skip leading spaces */
669
	if (*path == '/') path++;			/* Strip heading separator */
670
	dj->sclust = 0;						/* Set start directory (always root dir) */
671
 
672
	if ((BYTE)*path <= ' ') {			/* Null path means the root directory */
673
		res = dir_rewind(dj);
674
		dir[0] = 0;
675
 
676
	} else {							/* Follow path */
677
		for (;;) {
678
			res = create_name(dj, &path);	/* Get a segment */
679
			if (res != FR_OK) break;
680
			res = dir_find(dj, dir);		/* Find it */
681
			if (res != FR_OK) {				/* Could not find the object */
682
				if (res == FR_NO_FILE && !*(dj->fn+11))
683
					res = FR_NO_PATH;
684
				break;
685
			}
686
			if (*(dj->fn+11)) break;		/* Last segment match. Function completed. */
687
			if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow because it is a file */
688
				res = FR_NO_PATH; break;
689
			}
690
			dj->sclust = LD_CLUST(dir);
691
		}
692
	}
693
 
694
	return res;
695
}
696
 
697
 
698
 
699
 
700
/*-----------------------------------------------------------------------*/
701
/* Check a sector if it is an FAT boot record                            */
702
/*-----------------------------------------------------------------------*/
703
 
704
static
705
BYTE check_fs (	/* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record, 3:Error */
706
	BYTE *buf,	/* Working buffer */
707
	DWORD sect	/* Sector# (lba) to check if it is an FAT boot record or not */
708
)
709
{
710
	if (disk_readp(buf, sect, 510, 2))		/* Read the boot sector */
711
		return 3;
712
	if (LD_WORD(buf) != 0xAA55)				/* Check record signature */
713
		return 2;
714
 
715
	if (!disk_readp(buf, sect, BS_FilSysType, 2) && LD_WORD(buf) == 0x4146)	/* Check FAT12/16 */
716
		return 0;
717
	if (_FS_FAT32 && !disk_readp(buf, sect, BS_FilSysType32, 2) && LD_WORD(buf) == 0x4146)	/* Check FAT32 */
718
		return 0;
719
	return 1;
720
}
721
 
722
 
723
 
724
 
725
/*--------------------------------------------------------------------------
726
 
727
   Public Functions
728
 
729
--------------------------------------------------------------------------*/
730
 
731
 
732
 
733
/*-----------------------------------------------------------------------*/
734
/* Mount/Unmount a Locical Drive                                         */
735
/*-----------------------------------------------------------------------*/
736
 
737
FRESULT pf_mount (
738
	FATFS *fs		/* Pointer to new file system object (NULL: Unmount) */
739
)
740
{
741
	BYTE fmt, buf[36];
742
	DWORD bsect, fsize, tsect, mclst;
743
 
744
 
745
	FatFs = 0;
746
	if (!fs) return FR_OK;				/* Unregister fs object */
747
 
748
	if (disk_initialize() & STA_NOINIT)	/* Check if the drive is ready or not */
749
		return FR_NOT_READY;
750
 
751
	/* Search FAT partition on the drive */
752
	bsect = 0;
753
	fmt = check_fs(buf, bsect);			/* Check sector 0 as an SFD format */
754
	if (fmt == 1) {						/* Not an FAT boot record, it may be FDISK format */
755
		/* Check a partition listed in top of the partition table */
756
		if (disk_readp(buf, bsect, MBR_Table, 16)) {	/* 1st partition entry */
757
			fmt = 3;
758
		} else {
759
			if (buf[4]) {					/* Is the partition existing? */
760
				bsect = LD_DWORD(&buf[8]);	/* Partition offset in LBA */
761
				fmt = check_fs(buf, bsect);	/* Check the partition */
762
			}
763
		}
764
	}
765
	if (fmt == 3) return FR_DISK_ERR;
766
	if (fmt) return FR_NO_FILESYSTEM;	/* No valid FAT patition is found */
767
 
768
	/* Initialize the file system object */
769
	if (disk_readp(buf, bsect, 13, sizeof(buf))) return FR_DISK_ERR;
770
 
771
	fsize = LD_WORD(buf+BPB_FATSz16-13);				/* Number of sectors per FAT */
772
	if (!fsize) fsize = LD_DWORD(buf+BPB_FATSz32-13);
773
 
774
	fsize *= buf[BPB_NumFATs-13];						/* Number of sectors in FAT area */
775
	fs->fatbase = bsect + LD_WORD(buf+BPB_RsvdSecCnt-13); /* FAT start sector (lba) */
776
	fs->csize = buf[BPB_SecPerClus-13];					/* Number of sectors per cluster */
777
	fs->n_rootdir = LD_WORD(buf+BPB_RootEntCnt-13);		/* Nmuber of root directory entries */
778
	tsect = LD_WORD(buf+BPB_TotSec16-13);				/* Number of sectors on the file system */
779
	if (!tsect) tsect = LD_DWORD(buf+BPB_TotSec32-13);
780
	mclst = (tsect						/* Last cluster# + 1 */
781
		- LD_WORD(buf+BPB_RsvdSecCnt-13) - fsize - fs->n_rootdir / 16
782
		) / fs->csize + 2;
783
	fs->n_fatent = (CLUST)mclst;
784
 
785
	fmt = FS_FAT16;							/* Determine the FAT sub type */
786
	if (mclst < 0xFF7) 						/* Number of clusters < 0xFF5 */
787
#if _FS_FAT12
788
		fmt = FS_FAT12;
789
#else
790
		return FR_NO_FILESYSTEM;
791
#endif
792
	if (mclst >= 0xFFF7)					/* Number of clusters >= 0xFFF5 */
793
#if _FS_FAT32
794
		fmt = FS_FAT32;
795
#else
796
		return FR_NO_FILESYSTEM;
797
#endif
798
 
799
	fs->fs_type = fmt;		/* FAT sub-type */
800
	if (_FS_FAT32 && fmt == FS_FAT32)
801
		fs->dirbase = LD_DWORD(buf+(BPB_RootClus-13));	/* Root directory start cluster */
802
	else
803
		fs->dirbase = fs->fatbase + fsize;				/* Root directory start sector (lba) */
804
	fs->database = fs->fatbase + fsize + fs->n_rootdir / 16;	/* Data start sector (lba) */
805
 
806
	fs->flag = 0;
807
	FatFs = fs;
808
 
809
	return FR_OK;
810
}
811
 
812
 
813
 
814
 
815
/*-----------------------------------------------------------------------*/
816
/* Open or Create a File                                                 */
817
/*-----------------------------------------------------------------------*/
818
 
819
FRESULT pf_open (
820
	const char *path	/* Pointer to the file name */
821
)
822
{
823
	FRESULT res;
824
	DIR dj;
825
	BYTE sp[12], dir[32];
826
	FATFS *fs = FatFs;
827
 
828
 
829
	if (!fs)						/* Check file system */
830
		return FR_NOT_ENABLED;
831
 
832
	fs->flag = 0;
833
	dj.fn = sp;
834
	res = follow_path(&dj, dir, path);	/* Follow the file path */
835
	if (res != FR_OK) return res;		/* Follow failed */
836
	if (!dir[0] || (dir[DIR_Attr] & AM_DIR))	/* It is a directory */
837
		return FR_NO_FILE;
838
 
839
	fs->org_clust = LD_CLUST(dir);			/* File start cluster */
840
	fs->fsize = LD_DWORD(dir+DIR_FileSize);	/* File size */
841
	fs->fptr = 0;						/* File pointer */
842
	fs->flag = FA_OPENED;
843
 
844
	return FR_OK;
845
}
846
 
847
 
848
 
849
 
850
/*-----------------------------------------------------------------------*/
851
/* Read File                                                             */
852
/*-----------------------------------------------------------------------*/
853
#if _USE_READ
854
 
855
FRESULT pf_read (
856
	void* buff,		/* Pointer to the read buffer (NULL:Forward data to the stream)*/
857
	WORD btr,		/* Number of bytes to read */
858
	WORD* br		/* Pointer to number of bytes read */
859
)
860
{
861
	DRESULT dr;
862
	CLUST clst;
863
	DWORD sect, remain;
864
	WORD rcnt;
865
	BYTE cs, *rbuff = buff;
866
	FATFS *fs = FatFs;
867
 
868
 
869
	*br = 0;
870
	if (!fs) return FR_NOT_ENABLED;		/* Check file system */
871
	if (!(fs->flag & FA_OPENED))		/* Check if opened */
872
		return FR_NOT_OPENED;
873
 
874
	remain = fs->fsize - fs->fptr;
875
	if (btr > remain) btr = (WORD)remain;			/* Truncate btr by remaining bytes */
876
 
877
	while (btr)	{									/* Repeat until all data transferred */
878
		if ((fs->fptr % 512) == 0) {				/* On the sector boundary? */
879
			cs = (BYTE)(fs->fptr / 512 & (fs->csize - 1));	/* Sector offset in the cluster */
880
			if (!cs) {								/* On the cluster boundary? */
881
				clst = (fs->fptr == 0) ?			/* On the top of the file? */
882
					fs->org_clust : get_fat(fs->curr_clust);
883
				if (clst <= 1) goto fr_abort;
884
				fs->curr_clust = clst;				/* Update current cluster */
885
			}
886
			sect = clust2sect(fs->curr_clust);		/* Get current sector */
887
			if (!sect) goto fr_abort;
888
			fs->dsect = sect + cs;
889
		}
890
		rcnt = (WORD)(512 - (fs->fptr % 512));		/* Get partial sector data from sector buffer */
891
		if (rcnt > btr) rcnt = btr;
892
		dr = disk_readp(!buff ? 0 : rbuff, fs->dsect, (WORD)(fs->fptr % 512), rcnt);
893
		if (dr) goto fr_abort;
894
		fs->fptr += rcnt; rbuff += rcnt;			/* Update pointers and counters */
895
		btr -= rcnt; *br += rcnt;
896
	}
897
 
898
	return FR_OK;
899
 
900
fr_abort:
901
	fs->flag = 0;
902
	return FR_DISK_ERR;
903
}
904
#endif
905
 
906
 
907
 
908
/*-----------------------------------------------------------------------*/
909
/* Write File                                                            */
910
/*-----------------------------------------------------------------------*/
911
#if _USE_WRITE
912
 
913
FRESULT pf_write (
914
	const void* buff,	/* Pointer to the data to be written */
915
	WORD btw,			/* Number of bytes to write (0:Finalize the current write operation) */
916
	WORD* bw			/* Pointer to number of bytes written */
917
)
918
{
919
	CLUST clst;
920
	DWORD sect, remain;
921
	const BYTE *p = buff;
922
	BYTE cs;
923
	WORD wcnt;
924
	FATFS *fs = FatFs;
925
 
926
 
927
	*bw = 0;
928
	if (!fs) return FR_NOT_ENABLED;		/* Check file system */
929
	if (!(fs->flag & FA_OPENED))		/* Check if opened */
930
		return FR_NOT_OPENED;
931
 
932
	if (!btw) {		/* Finalize request */
933
		if ((fs->flag & FA__WIP) && disk_writep(0, 0)) goto fw_abort;
934
		fs->flag &= ~FA__WIP;
935
		return FR_OK;
936
	} else {		/* Write data request */
937
		if (!(fs->flag & FA__WIP))		/* Round-down fptr to the sector boundary */
938
			fs->fptr &= 0xFFFFFE00;
939
	}
940
	remain = fs->fsize - fs->fptr;
941
	if (btw > remain) btw = (WORD)remain;			/* Truncate btw by remaining bytes */
942
 
943
	while (btw)	{									/* Repeat until all data transferred */
944
		if (((WORD)fs->fptr % 512) == 0) {			/* On the sector boundary? */
945
			cs = (BYTE)(fs->fptr / 512 & (fs->csize - 1));	/* Sector offset in the cluster */
946
			if (!cs) {								/* On the cluster boundary? */
947
				clst = (fs->fptr == 0) ?			/* On the top of the file? */
948
					fs->org_clust : get_fat(fs->curr_clust);
949
				if (clst <= 1) goto fw_abort;
950
				fs->curr_clust = clst;				/* Update current cluster */
951
			}
952
			sect = clust2sect(fs->curr_clust);		/* Get current sector */
953
			if (!sect) goto fw_abort;
954
			fs->dsect = sect + cs;
955
			if (disk_writep(0, fs->dsect)) goto fw_abort;	/* Initiate a sector write operation */
956
			fs->flag |= FA__WIP;
957
		}
958
		wcnt = 512 - ((WORD)fs->fptr % 512);		/* Number of bytes to write to the sector */
959
		if (wcnt > btw) wcnt = btw;
960
		if (disk_writep(p, wcnt)) goto fw_abort;	/* Send data to the sector */
961
		fs->fptr += wcnt; p += wcnt;				/* Update pointers and counters */
962
		btw -= wcnt; *bw += wcnt;
963
		if (((WORD)fs->fptr % 512) == 0) {
964
			if (disk_writep(0, 0)) goto fw_abort;	/* Finalize the currtent secter write operation */
965
			fs->flag &= ~FA__WIP;
966
		}
967
	}
968
 
969
	return FR_OK;
970
 
971
fw_abort:
972
	fs->flag = 0;
973
	return FR_DISK_ERR;
974
}
975
#endif
976
 
977
 
978
 
979
/*-----------------------------------------------------------------------*/
980
/* Seek File R/W Pointer                                                 */
981
/*-----------------------------------------------------------------------*/
982
#if _USE_LSEEK
983
 
984
FRESULT pf_lseek (
985
	DWORD ofs		/* File pointer from top of file */
986
)
987
{
988
	CLUST clst;
989
	DWORD bcs, sect, ifptr;
990
	FATFS *fs = FatFs;
991
 
992
 
993
	if (!fs) return FR_NOT_ENABLED;		/* Check file system */
994
	if (!(fs->flag & FA_OPENED))		/* Check if opened */
995
			return FR_NOT_OPENED;
996
 
997
	if (ofs > fs->fsize) ofs = fs->fsize;	/* Clip offset with the file size */
998
	ifptr = fs->fptr;
999
	fs->fptr = 0;
1000
	if (ofs > 0) {
1001
		bcs = (DWORD)fs->csize * 512;	/* Cluster size (byte) */
1002
		if (ifptr > 0 &&
1003
			(ofs - 1) / bcs >= (ifptr - 1) / bcs) {	/* When seek to same or following cluster, */
1004
			fs->fptr = (ifptr - 1) & ~(bcs - 1);	/* start from the current cluster */
1005
			ofs -= fs->fptr;
1006
			clst = fs->curr_clust;
1007
		} else {							/* When seek to back cluster, */
1008
			clst = fs->org_clust;			/* start from the first cluster */
1009
			fs->curr_clust = clst;
1010
		}
1011
		while (ofs > bcs) {				/* Cluster following loop */
1012
			clst = get_fat(clst);		/* Follow cluster chain */
1013
			if (clst <= 1 || clst >= fs->n_fatent) goto fe_abort;
1014
			fs->curr_clust = clst;
1015
			fs->fptr += bcs;
1016
			ofs -= bcs;
1017
		}
1018
		fs->fptr += ofs;
1019
		sect = clust2sect(clst);		/* Current sector */
1020
		if (!sect) goto fe_abort;
1021
		fs->dsect = sect + (fs->fptr / 512 & (fs->csize - 1));
1022
	}
1023
 
1024
	return FR_OK;
1025
 
1026
fe_abort:
1027
	fs->flag = 0;
1028
	return FR_DISK_ERR;
1029
}
1030
#endif
1031
 
1032
 
1033
 
1034
/*-----------------------------------------------------------------------*/
1035
/* Create a Directroy Object                                             */
1036
/*-----------------------------------------------------------------------*/
1037
#if _USE_DIR
1038
 
1039
FRESULT pf_opendir (
1040
	DIR *dj,			/* Pointer to directory object to create */
1041
	const char *path	/* Pointer to the directory path */
1042
)
1043
{
1044
	FRESULT res;
1045
	BYTE sp[12], dir[32];
1046
	FATFS *fs = FatFs;
1047
 
1048
 
1049
	if (!fs) {				/* Check file system */
1050
		res = FR_NOT_ENABLED;
1051
	} else {
1052
		dj->fn = sp;
1053
		res = follow_path(dj, dir, path);		/* Follow the path to the directory */
1054
		if (res == FR_OK) {						/* Follow completed */
1055
			if (dir[0]) {						/* It is not the root dir */
1056
				if (dir[DIR_Attr] & AM_DIR)		/* The object is a directory */
1057
					dj->sclust = LD_CLUST(dir);
1058
				else							/* The object is not a directory */
1059
					res = FR_NO_PATH;
1060
			}
1061
			if (res == FR_OK)
1062
				res = dir_rewind(dj);			/* Rewind dir */
1063
		}
1064
		if (res == FR_NO_FILE) res = FR_NO_PATH;
1065
	}
1066
 
1067
	return res;
1068
}
1069
 
1070
 
1071
 
1072
 
1073
/*-----------------------------------------------------------------------*/
1074
/* Read Directory Entry in Sequense                                      */
1075
/*-----------------------------------------------------------------------*/
1076
 
1077
FRESULT pf_readdir (
1078
	DIR *dj,			/* Pointer to the open directory object */
1079
	FILINFO *fno		/* Pointer to file information to return */
1080
)
1081
{
1082
	FRESULT res;
1083
	BYTE sp[12], dir[32];
1084
	FATFS *fs = FatFs;
1085
 
1086
 
1087
	if (!fs) {				/* Check file system */
1088
		res = FR_NOT_ENABLED;
1089
	} else {
1090
		dj->fn = sp;
1091
		if (!fno) {
1092
			res = dir_rewind(dj);
1093
		} else {
1094
			res = dir_read(dj, dir);
1095
			if (res == FR_NO_FILE) {
1096
				dj->sect = 0;
1097
				res = FR_OK;
1098
			}
1099
			if (res == FR_OK) {				/* A valid entry is found */
1100
				get_fileinfo(dj, dir, fno);	/* Get the object information */
1101
				res = dir_next(dj);			/* Increment index for next */
1102
				if (res == FR_NO_FILE) {
1103
					dj->sect = 0;
1104
					res = FR_OK;
1105
				}
1106
			}
1107
		}
1108
	}
1109
 
1110
	return res;
1111
}
1112
 
1113
#endif /* _USE_DIR */
1114