1 | /*----------------------------------------------------------------------------/
|
---|
2 | / FatFs - FAT file system module R0.11a (C)ChaN, 2015 /
|
---|
3 | /-----------------------------------------------------------------------------/
|
---|
4 | / FatFs module is a free software that opened under license policy of
|
---|
5 | / following conditions.
|
---|
6 | /
|
---|
7 | / Copyright (C) 2015, ChaN, all right reserved.
|
---|
8 | /
|
---|
9 | / 1. Redistributions of source code must retain the above copyright notice,
|
---|
10 | / this condition and the following disclaimer.
|
---|
11 | /
|
---|
12 | / This software is provided by the copyright holder and contributors "AS IS"
|
---|
13 | / and any warranties related to this software are DISCLAIMED.
|
---|
14 | / The copyright owner or contributors be NOT LIABLE for any damages caused
|
---|
15 | / by use of this software.
|
---|
16 | /----------------------------------------------------------------------------*/
|
---|
17 |
|
---|
18 |
|
---|
19 | #include "shellif.h"
|
---|
20 | #include "ff.h" /* Declarations of FatFs API */
|
---|
21 | #include "diskio.h" /* Declarations of disk I/O functions */
|
---|
22 | #include "util/ntstdio.h"
|
---|
23 |
|
---|
24 | /*--------------------------------------------------------------------------
|
---|
25 |
|
---|
26 | Module Private Definitions
|
---|
27 |
|
---|
28 | ---------------------------------------------------------------------------*/
|
---|
29 |
|
---|
30 | #if _FATFS != 64180 /* Revision ID */
|
---|
31 | #error Wrong include file (ff.h).
|
---|
32 | #endif
|
---|
33 |
|
---|
34 |
|
---|
35 | /* Reentrancy related */
|
---|
36 | #if FF_FS_REENTRANT
|
---|
37 | #if FF_USE_LFN == 1
|
---|
38 | #error Static LFN work area cannot be used at thread-safe configuration
|
---|
39 | #endif
|
---|
40 | #define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; }
|
---|
41 | #define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }
|
---|
42 | #else
|
---|
43 | #define ENTER_FF(fs)
|
---|
44 | #define LEAVE_FF(fs, res) return res
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | #define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); }
|
---|
48 |
|
---|
49 |
|
---|
50 | /* Definitions of sector size */
|
---|
51 | #if (FF_MAX_SS < FF_MIN_SS) || (FF_MAX_SS != 512 && FF_MAX_SS != 1024 && FF_MAX_SS != 2048 && FF_MAX_SS != 4096) || (FF_MIN_SS != 512 && FF_MIN_SS != 1024 && FF_MIN_SS != 2048 && FF_MIN_SS != 4096)
|
---|
52 | #error Wrong sector size configuration
|
---|
53 | #endif
|
---|
54 | #if FF_MAX_SS == FF_MIN_SS
|
---|
55 | #define SS(fs) ((UINT)FF_MAX_SS) /* Fixed sector size */
|
---|
56 | #else
|
---|
57 | #define SS(fs) ((fs)->ssize) /* Variable sector size */
|
---|
58 | #endif
|
---|
59 |
|
---|
60 |
|
---|
61 | /* Timestamp feature */
|
---|
62 | #if FF_FS_NORTC == 1
|
---|
63 | #if FF_NORTC_YEAR < 1980 || FF_NORTC_YEAR > 2107 || FF_NORTC_MON < 1 || FF_NORTC_MON > 12 || FF_NORTC_MDAY < 1 || FF_NORTC_MDAY > 31
|
---|
64 | #error Invalid FF_FS_NORTC settings
|
---|
65 | #endif
|
---|
66 | #define GET_FATTIME() ((DWORD)(FF_NORTC_YEAR - 1980) << 25 | (DWORD)FF_NORTC_MON << 21 | (DWORD)FF_NORTC_MDAY << 16)
|
---|
67 | #else
|
---|
68 | #define GET_FATTIME() get_fattime()
|
---|
69 | #endif
|
---|
70 |
|
---|
71 |
|
---|
72 | /* File access control feature */
|
---|
73 | #if FF_FS_LOCK != 0
|
---|
74 | #if FF_FS_READONLY
|
---|
75 | #error FF_FS_LOCK must be 0 at read-only configuration
|
---|
76 | #endif
|
---|
77 | typedef struct {
|
---|
78 | FATFS *fs; /* Object ID 1, volume (NULL:blank entry) */
|
---|
79 | DWORD clu; /* Object ID 2, directory (0:root) */
|
---|
80 | WORD idx; /* Object ID 3, directory index */
|
---|
81 | WORD ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */
|
---|
82 | } FILESEM;
|
---|
83 | #endif
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 | /* DBCS code ranges and SBCS upper conversion tables */
|
---|
88 |
|
---|
89 | #if FF_CODE_PAGE == 932 /* Japanese Shift-JIS */
|
---|
90 | #define _DF1S 0x81 /* DBC 1st byte range 1 start */
|
---|
91 | #define _DF1E 0x9F /* DBC 1st byte range 1 end */
|
---|
92 | #define _DF2S 0xE0 /* DBC 1st byte range 2 start */
|
---|
93 | #define _DF2E 0xFC /* DBC 1st byte range 2 end */
|
---|
94 | #define _DS1S 0x40 /* DBC 2nd byte range 1 start */
|
---|
95 | #define _DS1E 0x7E /* DBC 2nd byte range 1 end */
|
---|
96 | #define _DS2S 0x80 /* DBC 2nd byte range 2 start */
|
---|
97 | #define _DS2E 0xFC /* DBC 2nd byte range 2 end */
|
---|
98 |
|
---|
99 | #elif FF_CODE_PAGE == 936 /* Simplified Chinese GBK */
|
---|
100 | #define _DF1S 0x81
|
---|
101 | #define _DF1E 0xFE
|
---|
102 | #define _DS1S 0x40
|
---|
103 | #define _DS1E 0x7E
|
---|
104 | #define _DS2S 0x80
|
---|
105 | #define _DS2E 0xFE
|
---|
106 |
|
---|
107 | #elif FF_CODE_PAGE == 949 /* Korean */
|
---|
108 | #define _DF1S 0x81
|
---|
109 | #define _DF1E 0xFE
|
---|
110 | #define _DS1S 0x41
|
---|
111 | #define _DS1E 0x5A
|
---|
112 | #define _DS2S 0x61
|
---|
113 | #define _DS2E 0x7A
|
---|
114 | #define _DS3S 0x81
|
---|
115 | #define _DS3E 0xFE
|
---|
116 |
|
---|
117 | #elif FF_CODE_PAGE == 950 /* Traditional Chinese Big5 */
|
---|
118 | #define _DF1S 0x81
|
---|
119 | #define _DF1E 0xFE
|
---|
120 | #define _DS1S 0x40
|
---|
121 | #define _DS1E 0x7E
|
---|
122 | #define _DS2S 0xA1
|
---|
123 | #define _DS2E 0xFE
|
---|
124 |
|
---|
125 | #elif FF_CODE_PAGE == 437 /* U.S. */
|
---|
126 | #define _DF1S 0
|
---|
127 | #define _EXCVT {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \
|
---|
128 | 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
129 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
130 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
131 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
132 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
133 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
|
---|
134 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
135 |
|
---|
136 | #elif FF_CODE_PAGE == 720 /* Arabic */
|
---|
137 | #define _DF1S 0
|
---|
138 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
---|
139 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
140 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
141 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
142 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
143 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
144 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
|
---|
145 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
146 |
|
---|
147 | #elif FF_CODE_PAGE == 737 /* Greek */
|
---|
148 | #define _DF1S 0
|
---|
149 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
---|
150 | 0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \
|
---|
151 | 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96, \
|
---|
152 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
153 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
154 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
155 | 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xEF,0xF5,0xF0,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
|
---|
156 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
157 |
|
---|
158 | #elif FF_CODE_PAGE == 771 /* KBL */
|
---|
159 | #define _DF1S 0
|
---|
160 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
---|
161 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
162 | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
---|
163 | 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, \
|
---|
165 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDC,0xDE,0xDE, \
|
---|
166 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
167 | 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFE,0xFF}
|
---|
168 |
|
---|
169 | #elif FF_CODE_PAGE == 775 /* Baltic */
|
---|
170 | #define _DF1S 0
|
---|
171 | #define _EXCVT {0x80,0x9A,0x91,0xA0,0x8E,0x95,0x8F,0x80,0xAD,0xED,0x8A,0x8A,0xA1,0x8D,0x8E,0x8F, \
|
---|
172 | 0x90,0x92,0x92,0xE2,0x99,0x95,0x96,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
|
---|
173 | 0xA0,0xA1,0xE0,0xA3,0xA3,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
174 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
175 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
176 | 0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
177 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE3,0xE8,0xE8,0xEA,0xEA,0xEE,0xED,0xEE,0xEF, \
|
---|
178 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
179 |
|
---|
180 | #elif FF_CODE_PAGE == 850 /* Latin 1 */
|
---|
181 | #define _DF1S 0
|
---|
182 | #define _EXCVT {0x43,0x55,0x45,0x41,0x41,0x41,0x41,0x43,0x45,0x45,0x45,0x49,0x49,0x49,0x41,0x41, \
|
---|
183 | 0x45,0x92,0x92,0x4F,0x4F,0x4F,0x55,0x55,0x59,0x4F,0x55,0x4F,0x9C,0x4F,0x9E,0x9F, \
|
---|
184 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
185 | 0xB0,0xB1,0xB2,0xB3,0xB4,0x41,0x41,0x41,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
186 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0x41,0x41,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
187 | 0xD1,0xD1,0x45,0x45,0x45,0x49,0x49,0x49,0x49,0xD9,0xDA,0xDB,0xDC,0xDD,0x49,0xDF, \
|
---|
188 | 0x4F,0xE1,0x4F,0x4F,0x4F,0x4F,0xE6,0xE8,0xE8,0x55,0x55,0x55,0x59,0x59,0xEE,0xEF, \
|
---|
189 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
190 |
|
---|
191 | #elif FF_CODE_PAGE == 852 /* Latin 2 */
|
---|
192 | #define _DF1S 0
|
---|
193 | #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F, \
|
---|
194 | 0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0xAC, \
|
---|
195 | 0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA,0x8D,0xAC,0xB8,0xAE,0xAF, \
|
---|
196 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBF, \
|
---|
197 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC6,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
198 | 0xD1,0xD1,0xD2,0xD3,0xD2,0xD5,0xD6,0xD7,0xB7,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
199 | 0xE0,0xE1,0xE2,0xE3,0xE3,0xD5,0xE6,0xE6,0xE8,0xE9,0xE8,0xEB,0xED,0xED,0xDD,0xEF, \
|
---|
200 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xEB,0xFC,0xFC,0xFE,0xFF}
|
---|
201 |
|
---|
202 | #elif FF_CODE_PAGE == 855 /* Cyrillic */
|
---|
203 | #define _DF1S 0
|
---|
204 | #define _EXCVT {0x81,0x81,0x83,0x83,0x85,0x85,0x87,0x87,0x89,0x89,0x8B,0x8B,0x8D,0x8D,0x8F,0x8F, \
|
---|
205 | 0x91,0x91,0x93,0x93,0x95,0x95,0x97,0x97,0x99,0x99,0x9B,0x9B,0x9D,0x9D,0x9F,0x9F, \
|
---|
206 | 0xA1,0xA1,0xA3,0xA3,0xA5,0xA5,0xA7,0xA7,0xA9,0xA9,0xAB,0xAB,0xAD,0xAD,0xAE,0xAF, \
|
---|
207 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB6,0xB6,0xB8,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBE,0xBF, \
|
---|
208 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
209 | 0xD1,0xD1,0xD3,0xD3,0xD5,0xD5,0xD7,0xD7,0xDD,0xD9,0xDA,0xDB,0xDC,0xDD,0xE0,0xDF, \
|
---|
210 | 0xE0,0xE2,0xE2,0xE4,0xE4,0xE6,0xE6,0xE8,0xE8,0xEA,0xEA,0xEC,0xEC,0xEE,0xEE,0xEF, \
|
---|
211 | 0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFD,0xFE,0xFF}
|
---|
212 |
|
---|
213 | #elif FF_CODE_PAGE == 857 /* Turkish */
|
---|
214 | #define _DF1S 0
|
---|
215 | #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0x49,0x8E,0x8F, \
|
---|
216 | 0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x98,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9E, \
|
---|
217 | 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
218 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
219 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
220 | 0xD0,0xD1,0xD2,0xD3,0xD4,0x49,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
221 | 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xDE,0xED,0xEE,0xEF, \
|
---|
222 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
223 |
|
---|
224 | #elif FF_CODE_PAGE == 860 /* Portuguese */
|
---|
225 | #define _DF1S 0
|
---|
226 | #define _EXCVT {0x80,0x9A,0x90,0x8F,0x8E,0x91,0x86,0x80,0x89,0x89,0x92,0x8B,0x8C,0x98,0x8E,0x8F, \
|
---|
227 | 0x90,0x91,0x92,0x8C,0x99,0xA9,0x96,0x9D,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
228 | 0x86,0x8B,0x9F,0x96,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
229 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
230 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
231 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
232 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
|
---|
233 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
234 |
|
---|
235 | #elif FF_CODE_PAGE == 861 /* Icelandic */
|
---|
236 | #define _DF1S 0
|
---|
237 | #define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x8B,0x8B,0x8D,0x8E,0x8F, \
|
---|
238 | 0x90,0x92,0x92,0x4F,0x99,0x8D,0x55,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
|
---|
239 | 0xA4,0xA5,0xA6,0xA7,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
240 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
241 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
242 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
243 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
|
---|
244 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
245 |
|
---|
246 | #elif FF_CODE_PAGE == 862 /* Hebrew */
|
---|
247 | #define _DF1S 0
|
---|
248 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
---|
249 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
250 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
251 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
252 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
253 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
254 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
|
---|
255 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
256 |
|
---|
257 | #elif FF_CODE_PAGE == 863 /* Canadian-French */
|
---|
258 | #define _DF1S 0
|
---|
259 | #define _EXCVT {0x43,0x55,0x45,0x41,0x41,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x41,0x8F, \
|
---|
260 | 0x45,0x45,0x45,0x4F,0x45,0x49,0x55,0x55,0x98,0x4F,0x55,0x9B,0x9C,0x55,0x55,0x9F, \
|
---|
261 | 0xA0,0xA1,0x4F,0x55,0xA4,0xA5,0xA6,0xA7,0x49,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
262 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
263 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
264 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
265 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
|
---|
266 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
267 |
|
---|
268 | #elif FF_CODE_PAGE == 864 /* Arabic */
|
---|
269 | #define _DF1S 0
|
---|
270 | #define _EXCVT {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \
|
---|
271 | 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
272 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
273 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
274 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
275 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
276 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
|
---|
277 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
278 |
|
---|
279 | #elif FF_CODE_PAGE == 865 /* Nordic */
|
---|
280 | #define _DF1S 0
|
---|
281 | #define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \
|
---|
282 | 0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
283 | 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
284 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
285 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
286 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
287 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
|
---|
288 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
289 |
|
---|
290 | #elif FF_CODE_PAGE == 866 /* Russian */
|
---|
291 | #define _DF1S 0
|
---|
292 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
---|
293 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
294 | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
---|
295 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
296 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
297 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
---|
298 | 0x90,0x91,0x92,0x93,0x9d,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
---|
299 | 0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
---|
300 |
|
---|
301 | #elif FF_CODE_PAGE == 869 /* Greek 2 */
|
---|
302 | #define _DF1S 0
|
---|
303 | #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
---|
304 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x86,0x9C,0x8D,0x8F,0x90, \
|
---|
305 | 0x91,0x90,0x92,0x95,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
---|
306 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
---|
307 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
---|
308 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xA4,0xA5,0xA6,0xD9,0xDA,0xDB,0xDC,0xA7,0xA8,0xDF, \
|
---|
309 | 0xA9,0xAA,0xAC,0xAD,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xCF,0xCF,0xD0,0xEF, \
|
---|
310 | 0xF0,0xF1,0xD1,0xD2,0xD3,0xF5,0xD4,0xF7,0xF8,0xF9,0xD5,0x96,0x95,0x98,0xFE,0xFF}
|
---|
311 |
|
---|
312 | #elif FF_CODE_PAGE == 1 /* ASCII (for only non-LFN cfg) */
|
---|
313 | #if FF_USE_LFN
|
---|
314 | #error Cannot use LFN feature without valid code page.
|
---|
315 | #endif
|
---|
316 | #define _DF1S 0
|
---|
317 |
|
---|
318 | #elif FF_CODE_PAGE == 65001 /* UTF-8 */
|
---|
319 | #if FF_LFN_UNICODE
|
---|
320 | #error Cannot use LFN_UNICODE feature without valid code page.
|
---|
321 | #endif
|
---|
322 | #define _DF1S 0
|
---|
323 |
|
---|
324 | void Utf16_to_Utf8(unsigned char *ret_code, int *ret_size, UINT chr)
|
---|
325 | {
|
---|
326 | ret_code[0] = ret_code[1] = ret_code[2] = ret_code[3] = 0;
|
---|
327 |
|
---|
328 | if (chr <= 0x7f) { // ASCII互換
|
---|
329 | ret_code[0] = chr;
|
---|
330 | *ret_size = 1;
|
---|
331 | return;
|
---|
332 | }
|
---|
333 |
|
---|
334 | if (chr <= 0x7ff) {
|
---|
335 | ret_code[1] = (0x80 | (chr & 0x3f));
|
---|
336 | ret_code[0] = (0xc0 | (chr >> 6));
|
---|
337 | *ret_size = 2;
|
---|
338 | return;
|
---|
339 | }
|
---|
340 |
|
---|
341 | if (chr <= 0xffff) {
|
---|
342 | ret_code[2] = (0x80 | (chr & 0x3f));
|
---|
343 | ret_code[1] = (0x80 | ((chr >> 6) & 0x3f));
|
---|
344 | ret_code[0] = (0xe0 | ((chr >> 12) & 0x0f));
|
---|
345 | *ret_size = 3;
|
---|
346 | return;
|
---|
347 | }
|
---|
348 |
|
---|
349 | if (chr <= 0x1fffff) {
|
---|
350 | ret_code[3] = (0x80 | (chr & 0x3f));
|
---|
351 | ret_code[2] = (0x80 | ((chr >> 6) & 0x3f));
|
---|
352 | ret_code[1] = (0x80 | ((chr >> 12) & 0x3f));
|
---|
353 | ret_code[0] = (0xf0 | ((chr >> 18) & 0x07));
|
---|
354 | *ret_size = 4;
|
---|
355 | return;
|
---|
356 | }
|
---|
357 |
|
---|
358 | if (chr <= 0x3ffffff) {
|
---|
359 | ret_code[4] = (0x80 | (chr & 0x3f));
|
---|
360 | ret_code[3] = (0x80 | ((chr >> 6) & 0x3f));
|
---|
361 | ret_code[2] = (0x80 | ((chr >> 12) & 0x3f));
|
---|
362 | ret_code[1] = (0x80 | ((chr >> 18) & 0x3f));
|
---|
363 | ret_code[0] = (0xf8 | ((chr >> 24) & 0x03));
|
---|
364 | *ret_size = 5;
|
---|
365 | return;
|
---|
366 | }
|
---|
367 |
|
---|
368 | ret_code[5] = (0x80 | (chr & 0x3f));
|
---|
369 | ret_code[4] = (0x80 | ((chr >> 6) & 0x3f));
|
---|
370 | ret_code[3] = (0x80 | ((chr >> 12) & 0x3f));
|
---|
371 | ret_code[2] = (0x80 | ((chr >> 18) & 0x3f));
|
---|
372 | ret_code[1] = (0x80 | ((chr >> 24) & 0x3f));
|
---|
373 | ret_code[0] = (0xfc | ((chr >> 30) & 0x01));
|
---|
374 | *ret_size = 6;
|
---|
375 | return;
|
---|
376 | }
|
---|
377 |
|
---|
378 | //2バイトのUTF-16コードが得られる
|
---|
379 | WCHAR Utf8_to_Utf16(const char *src, int *code_size)
|
---|
380 | {
|
---|
381 | int i;
|
---|
382 | unsigned int uc = 0;
|
---|
383 | unsigned char len = 0;
|
---|
384 |
|
---|
385 | len = 0;
|
---|
386 | if ((src[0] & 0x80) == 0) { uc = src[0] & 0x7F; len = 0; }
|
---|
387 | else if ((src[0] & 0xE0) == 0xC0) { uc = src[0] & 0x1F; len = 1; }
|
---|
388 | else if ((src[0] & 0xF0) == 0xE0) { uc = src[0] & 0x0F; len = 2; }
|
---|
389 | else if ((src[0] & 0xF8) == 0xF0) { uc = src[0] & 0x07; len = 3; }
|
---|
390 | else if ((src[0] & 0xFC) == 0xF8) { uc = src[0] & 0x03; len = 4; }
|
---|
391 | else if ((src[0] & 0xFE) == 0xFC) { uc = src[0] & 0x01; len = 5; }
|
---|
392 |
|
---|
393 | i = 1;
|
---|
394 | while ((i <= len) && ((src[i] & 0xC0) == 0x80)) {
|
---|
395 | uc = (uc << 6) | (src[i] & 0x3F);
|
---|
396 | i++;
|
---|
397 | }
|
---|
398 |
|
---|
399 | //消費文字数設定
|
---|
400 | *code_size = i;
|
---|
401 |
|
---|
402 | //現状、2バイト限定
|
---|
403 | return uc;
|
---|
404 | }
|
---|
405 |
|
---|
406 | #else
|
---|
407 | #error Unknown code page
|
---|
408 |
|
---|
409 | #endif
|
---|
410 |
|
---|
411 |
|
---|
412 | /* Character code support macros */
|
---|
413 | #define IsUpper(c) (((c)>='A')&&((c)<='Z'))
|
---|
414 | #define IsLower(c) (((c)>='a')&&((c)<='z'))
|
---|
415 | #define IsDigit(c) (((c)>='0')&&((c)<='9'))
|
---|
416 |
|
---|
417 | #if _DF1S /* Code page is DBCS */
|
---|
418 |
|
---|
419 | #ifdef _DF2S /* Two 1st byte areas */
|
---|
420 | #define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
|
---|
421 | #else /* One 1st byte area */
|
---|
422 | #define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
|
---|
423 | #endif
|
---|
424 |
|
---|
425 | #ifdef _DS3S /* Three 2nd byte areas */
|
---|
426 | #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
|
---|
427 | #else /* Two 2nd byte areas */
|
---|
428 | #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
|
---|
429 | #endif
|
---|
430 |
|
---|
431 | #else /* Code page is SBCS */
|
---|
432 |
|
---|
433 | #define IsDBCS1(c) 0
|
---|
434 | #define IsDBCS2(c) 0
|
---|
435 |
|
---|
436 | #endif /* _DF1S */
|
---|
437 |
|
---|
438 |
|
---|
439 | /* Name status flags */
|
---|
440 | #define NSFLAG 11 /* Index of name status byte in fn[] */
|
---|
441 | #define NS_LOSS 0x01 /* Out of 8.3 format */
|
---|
442 | #define NS_LFN 0x02 /* Force to create LFN entry */
|
---|
443 | #define NS_LAST 0x04 /* Last segment */
|
---|
444 | #define NS_BODY 0x08 /* Lower case flag (body) */
|
---|
445 | #define NS_EXT 0x10 /* Lower case flag (ext) */
|
---|
446 | #define NS_DOT 0x20 /* Dot entry */
|
---|
447 |
|
---|
448 |
|
---|
449 | /* FAT sub-type boundaries (Differ from specs but correct for real DOS/Windows) */
|
---|
450 | #define MIN_FAT16 4086U /* Minimum number of clusters of FAT16 */
|
---|
451 | #define MIN_FAT32 65526U /* Minimum number of clusters of FAT32 */
|
---|
452 |
|
---|
453 |
|
---|
454 | /* FatFs refers the members in the FAT structures as byte array instead of
|
---|
455 | / structure members because the structure is not binary compatible between
|
---|
456 | / different platforms */
|
---|
457 |
|
---|
458 | #define BS_jmpBoot 0 /* x86 jump instruction (3) */
|
---|
459 | #define BS_OEMName 3 /* OEM name (8) */
|
---|
460 | #define BPB_BytsPerSec 11 /* Sector size [byte] (2) */
|
---|
461 | #define BPB_SecPerClus 13 /* Cluster size [sector] (1) */
|
---|
462 | #define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (2) */
|
---|
463 | #define BPB_NumFATs 16 /* Number of FAT copies (1) */
|
---|
464 | #define BPB_RootEntCnt 17 /* Number of root directory entries for FAT12/16 (2) */
|
---|
465 | #define BPB_TotSec16 19 /* Volume size [sector] (2) */
|
---|
466 | #define BPB_Media 21 /* Media descriptor (1) */
|
---|
467 | #define BPB_FATSz16 22 /* FAT size [sector] (2) */
|
---|
468 | #define BPB_SecPerTrk 24 /* Track size [sector] (2) */
|
---|
469 | #define BPB_NumHeads 26 /* Number of heads (2) */
|
---|
470 | #define BPB_HiddSec 28 /* Number of special hidden sectors (4) */
|
---|
471 | #define BPB_TotSec32 32 /* Volume size [sector] (4) */
|
---|
472 | #define BS_DrvNum 36 /* Physical drive number (1) */
|
---|
473 | #define BS_NTres 37 /* Error flag (1) */
|
---|
474 | #define BS_BootSig 38 /* Extended boot signature (1) */
|
---|
475 | #define BS_VolID 39 /* Volume serial number (4) */
|
---|
476 | #define BS_VolLab 43 /* Volume label (8) */
|
---|
477 | #define BS_FilSysType 54 /* File system type (1) */
|
---|
478 | #define BPB_FATSz32 36 /* FAT size [sector] (4) */
|
---|
479 | #define BPB_ExtFlags 40 /* Extended flags (2) */
|
---|
480 | #define BPB_FSVer 42 /* File system version (2) */
|
---|
481 | #define BPB_RootClus 44 /* Root directory first cluster (4) */
|
---|
482 | #define BPB_FSInfo 48 /* Offset of FSINFO sector (2) */
|
---|
483 | #define BPB_BkBootSec 50 /* Offset of backup boot sector (2) */
|
---|
484 | #define BS_DrvNum32 64 /* Physical drive number (1) */
|
---|
485 | #define BS_NTres32 65 /* Error flag (1) */
|
---|
486 | #define BS_BootSig32 66 /* Extended boot signature (1) */
|
---|
487 | #define BS_VolID32 67 /* Volume serial number (4) */
|
---|
488 | #define BS_VolLab32 71 /* Volume label (8) */
|
---|
489 | #define BS_FilSysType32 82 /* File system type (1) */
|
---|
490 | #define FSI_LeadSig 0 /* FSI: Leading signature (4) */
|
---|
491 | #define FSI_StrucSig 484 /* FSI: Structure signature (4) */
|
---|
492 | #define FSI_Free_Count 488 /* FSI: Number of free clusters (4) */
|
---|
493 | #define FSI_Nxt_Free 492 /* FSI: Last allocated cluster (4) */
|
---|
494 | #define MBR_Table 446 /* MBR: Partition table offset (2) */
|
---|
495 | #define SZ_PTE 16 /* MBR: Size of a partition table entry */
|
---|
496 | #define BS_55AA 510 /* Signature word (2) */
|
---|
497 |
|
---|
498 | #define DIR_Name 0 /* Short file name (11) */
|
---|
499 | #define DIR_Attr 11 /* Attribute (1) */
|
---|
500 | #define DIR_NTres 12 /* Lower case flag (1) */
|
---|
501 | #define DIR_CrtTimeTenth 13 /* Created time sub-second (1) */
|
---|
502 | #define DIR_CrtTime 14 /* Created time (2) */
|
---|
503 | #define DIR_CrtDate 16 /* Created date (2) */
|
---|
504 | #define DIR_LstAccDate 18 /* Last accessed date (2) */
|
---|
505 | #define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (2) */
|
---|
506 | #define DIR_WrtTime 22 /* Modified time (2) */
|
---|
507 | #define DIR_WrtDate 24 /* Modified date (2) */
|
---|
508 | #define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (2) */
|
---|
509 | #define DIR_FileSize 28 /* File size (4) */
|
---|
510 | #define LDIR_Ord 0 /* LFN entry order and LLE flag (1) */
|
---|
511 | #define LDIR_Attr 11 /* LFN attribute (1) */
|
---|
512 | #define LDIR_Type 12 /* LFN type (1) */
|
---|
513 | #define LDIR_Chksum 13 /* Checksum of corresponding SFN entry */
|
---|
514 | #define LDIR_FstClusLO 26 /* Must be zero (0) */
|
---|
515 | #define SZ_DIRE 32 /* Size of a directory entry */
|
---|
516 | #define LLEF 0x40 /* Last long entry flag in LDIR_Ord */
|
---|
517 | #define DDEM 0xE5 /* Deleted directory entry mark at DIR_Name[0] */
|
---|
518 | #define RDDEM 0x05 /* Replacement of the character collides with DDEM */
|
---|
519 |
|
---|
520 |
|
---|
521 |
|
---|
522 |
|
---|
523 | /*--------------------------------------------------------------------------
|
---|
524 |
|
---|
525 | Module Private Work Area
|
---|
526 |
|
---|
527 | ---------------------------------------------------------------------------*/
|
---|
528 |
|
---|
529 | /* Remark: Uninitialized variables with static duration are guaranteed
|
---|
530 | / zero/null at start-up. If not, either the linker or start-up routine
|
---|
531 | / being used is not compliance with ANSI-C standard.
|
---|
532 | */
|
---|
533 |
|
---|
534 | #if FF_VOLUMES < 1 || FF_VOLUMES > 9
|
---|
535 | #error Wrong FF_VOLUMES setting
|
---|
536 | #endif
|
---|
537 | static FATFS *FatFs[FF_VOLUMES]; /* Pointer to the file system objects (logical drives) */
|
---|
538 | static WORD Fsid; /* File system mount ID */
|
---|
539 |
|
---|
540 | #if FF_FS_RPATH && FF_VOLUMES >= 2
|
---|
541 | static BYTE CurrVol; /* Current drive */
|
---|
542 | #endif
|
---|
543 |
|
---|
544 | #if FF_FS_LOCK != 0
|
---|
545 | static FILESEM Files[FF_FS_LOCK]; /* Open object lock semaphores */
|
---|
546 | #endif
|
---|
547 |
|
---|
548 | #if FF_USE_LFN == 0 /* Non LFN feature */
|
---|
549 | #define DEFINE_NAMEBUF BYTE sfn[12]
|
---|
550 | #define INIT_BUF(dobj) (dobj).fn = sfn
|
---|
551 | #define FREE_BUF()
|
---|
552 | #else
|
---|
553 | #if FF_MAX_LFN < 12 || FF_MAX_LFN > 255
|
---|
554 | #error Wrong FF_MAX_LFN setting
|
---|
555 | #endif
|
---|
556 | #if FF_USE_LFN == 1 /* LFN feature with static working buffer */
|
---|
557 | static WCHAR LfnBuf[FF_MAX_LFN + 1];
|
---|
558 | #define DEFINE_NAMEBUF BYTE sfn[12]
|
---|
559 | #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = LfnBuf; }
|
---|
560 | #define FREE_BUF()
|
---|
561 | #elif FF_USE_LFN == 2 /* LFN feature with dynamic working buffer on the stack */
|
---|
562 | #define DEFINE_NAMEBUF BYTE sfn[12]; WCHAR lbuf[FF_MAX_LFN + 1]
|
---|
563 | #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = lbuf; }
|
---|
564 | #define FREE_BUF()
|
---|
565 | #elif FF_USE_LFN == 3 /* LFN feature with dynamic working buffer on the heap */
|
---|
566 | #define DEFINE_NAMEBUF BYTE sfn[12]; WCHAR *lfn
|
---|
567 | #define INIT_BUF(dobj) { lfn = ff_memalloc((FF_MAX_LFN + 1) * 2); if (!lfn) LEAVE_FF((dobj).fs, FR_NOT_ENOUGH_CORE); (dobj).lfn = lfn; (dobj).fn = sfn; }
|
---|
568 | #define FREE_BUF() ff_memfree(lfn)
|
---|
569 | #else
|
---|
570 | #error Wrong FF_USE_LFN setting
|
---|
571 | #endif
|
---|
572 | #endif
|
---|
573 |
|
---|
574 | #ifdef _EXCVT
|
---|
575 | static const BYTE ExCvt[] = _EXCVT; /* Upper conversion table for SBCS extended characters */
|
---|
576 | #endif
|
---|
577 |
|
---|
578 |
|
---|
579 |
|
---|
580 |
|
---|
581 |
|
---|
582 |
|
---|
583 | /*--------------------------------------------------------------------------
|
---|
584 |
|
---|
585 | Module Private Functions
|
---|
586 |
|
---|
587 | ---------------------------------------------------------------------------*/
|
---|
588 |
|
---|
589 |
|
---|
590 | /*-----------------------------------------------------------------------*/
|
---|
591 | /* String functions */
|
---|
592 | /*-----------------------------------------------------------------------*/
|
---|
593 |
|
---|
594 | /* Copy memory to memory */
|
---|
595 | static
|
---|
596 | void mem_cpy (void* dst, const void* src, UINT cnt) {
|
---|
597 | BYTE *d = (BYTE*)dst;
|
---|
598 | const BYTE *s = (const BYTE*)src;
|
---|
599 |
|
---|
600 | #if FF_WORD_ACCESS == 1
|
---|
601 | while (cnt >= sizeof (int)) {
|
---|
602 | *(int*)d = *(int*)s;
|
---|
603 | d += sizeof (int); s += sizeof (int);
|
---|
604 | cnt -= sizeof (int);
|
---|
605 | }
|
---|
606 | #endif
|
---|
607 | while (cnt--)
|
---|
608 | *d++ = *s++;
|
---|
609 | }
|
---|
610 |
|
---|
611 | /* Fill memory */
|
---|
612 | static
|
---|
613 | void mem_set (void* dst, int val, UINT cnt) {
|
---|
614 | BYTE *d = (BYTE*)dst;
|
---|
615 |
|
---|
616 | while (cnt--)
|
---|
617 | *d++ = (BYTE)val;
|
---|
618 | }
|
---|
619 |
|
---|
620 | /* Compare memory to memory */
|
---|
621 | static
|
---|
622 | int mem_cmp (const void* dst, const void* src, UINT cnt) {
|
---|
623 | const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
|
---|
624 | int r = 0;
|
---|
625 |
|
---|
626 | while (cnt-- && (r = *d++ - *s++) == 0) ;
|
---|
627 | return r;
|
---|
628 | }
|
---|
629 |
|
---|
630 | /* Check if chr is contained in the string */
|
---|
631 | static
|
---|
632 | int chk_chr (const char* str, int chr) {
|
---|
633 | while (*str && *str != chr) str++;
|
---|
634 | return *str;
|
---|
635 | }
|
---|
636 |
|
---|
637 |
|
---|
638 |
|
---|
639 |
|
---|
640 | /*-----------------------------------------------------------------------*/
|
---|
641 | /* Request/Release grant to access the volume */
|
---|
642 | /*-----------------------------------------------------------------------*/
|
---|
643 | #if FF_FS_REENTRANT
|
---|
644 | static
|
---|
645 | int lock_fs (
|
---|
646 | FATFS* fs /* File system object */
|
---|
647 | )
|
---|
648 | {
|
---|
649 | return ff_req_grant(fs->sobj);
|
---|
650 | }
|
---|
651 |
|
---|
652 |
|
---|
653 | static
|
---|
654 | void unlock_fs (
|
---|
655 | FATFS* fs, /* File system object */
|
---|
656 | FRESULT res /* Result code to be returned */
|
---|
657 | )
|
---|
658 | {
|
---|
659 | if (fs &&
|
---|
660 | res != FR_NOT_ENABLED &&
|
---|
661 | res != FR_INVALID_DRIVE &&
|
---|
662 | res != FR_INVALID_OBJECT &&
|
---|
663 | res != FR_TIMEOUT) {
|
---|
664 | ff_rel_grant(fs->sobj);
|
---|
665 | }
|
---|
666 | }
|
---|
667 | #endif
|
---|
668 |
|
---|
669 |
|
---|
670 |
|
---|
671 |
|
---|
672 | /*-----------------------------------------------------------------------*/
|
---|
673 | /* File lock control functions */
|
---|
674 | /*-----------------------------------------------------------------------*/
|
---|
675 | #if FF_FS_LOCK != 0
|
---|
676 |
|
---|
677 | static
|
---|
678 | FRESULT chk_lock ( /* Check if the file can be accessed */
|
---|
679 | FATFS_DIR* dp, /* Directory object pointing the file to be checked */
|
---|
680 | int acc /* Desired access type (0:Read, 1:Write, 2:Delete/Rename) */
|
---|
681 | )
|
---|
682 | {
|
---|
683 | UINT i, be;
|
---|
684 |
|
---|
685 | /* Search file semaphore table */
|
---|
686 | for (i = be = 0; i < FF_FS_LOCK; i++) {
|
---|
687 | if (Files[i].fs) { /* Existing entry */
|
---|
688 | if (Files[i].fs == dp->fs && /* Check if the object matched with an open object */
|
---|
689 | Files[i].clu == dp->sclust &&
|
---|
690 | Files[i].idx == dp->index) break;
|
---|
691 | } else { /* Blank entry */
|
---|
692 | be = 1;
|
---|
693 | }
|
---|
694 | }
|
---|
695 | if (i == FF_FS_LOCK) /* The object is not opened */
|
---|
696 | return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES; /* Is there a blank entry for new object? */
|
---|
697 |
|
---|
698 | /* The object has been opened. Reject any open against writing file and all write mode open */
|
---|
699 | return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
|
---|
700 | }
|
---|
701 |
|
---|
702 |
|
---|
703 | static
|
---|
704 | int enq_lock (void) /* Check if an entry is available for a new object */
|
---|
705 | {
|
---|
706 | UINT i;
|
---|
707 |
|
---|
708 | for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ;
|
---|
709 | return (i == FF_FS_LOCK) ? 0 : 1;
|
---|
710 | }
|
---|
711 |
|
---|
712 |
|
---|
713 | static
|
---|
714 | UINT inc_lock ( /* Increment object open counter and returns its index (0:Internal error) */
|
---|
715 | FATFS_DIR* dp, /* Directory object pointing the file to register or increment */
|
---|
716 | int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */
|
---|
717 | )
|
---|
718 | {
|
---|
719 | UINT i;
|
---|
720 |
|
---|
721 |
|
---|
722 | for (i = 0; i < FF_FS_LOCK; i++) { /* Find the object */
|
---|
723 | if (Files[i].fs == dp->fs &&
|
---|
724 | Files[i].clu == dp->sclust &&
|
---|
725 | Files[i].idx == dp->index) break;
|
---|
726 | }
|
---|
727 |
|
---|
728 | if (i == FF_FS_LOCK) { /* Not opened. Register it as new. */
|
---|
729 | for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ;
|
---|
730 | if (i == FF_FS_LOCK) return 0; /* No free entry to register (int err) */
|
---|
731 | Files[i].fs = dp->fs;
|
---|
732 | Files[i].clu = dp->sclust;
|
---|
733 | Files[i].idx = dp->index;
|
---|
734 | Files[i].ctr = 0;
|
---|
735 | }
|
---|
736 |
|
---|
737 | if (acc && Files[i].ctr) return 0; /* Access violation (int err) */
|
---|
738 |
|
---|
739 | Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */
|
---|
740 |
|
---|
741 | return i + 1;
|
---|
742 | }
|
---|
743 |
|
---|
744 |
|
---|
745 | static
|
---|
746 | FRESULT dec_lock ( /* Decrement object open counter */
|
---|
747 | UINT i /* Semaphore index (1..) */
|
---|
748 | )
|
---|
749 | {
|
---|
750 | WORD n;
|
---|
751 | FRESULT res;
|
---|
752 |
|
---|
753 |
|
---|
754 | if (--i < FF_FS_LOCK) { /* Shift index number origin from 0 */
|
---|
755 | n = Files[i].ctr;
|
---|
756 | if (n == 0x100) n = 0; /* If write mode open, delete the entry */
|
---|
757 | if (n) n--; /* Decrement read mode open count */
|
---|
758 | Files[i].ctr = n;
|
---|
759 | if (!n) Files[i].fs = 0; /* Delete the entry if open count gets zero */
|
---|
760 | res = FR_OK;
|
---|
761 | } else {
|
---|
762 | res = FR_INT_ERR; /* Invalid index nunber */
|
---|
763 | }
|
---|
764 | return res;
|
---|
765 | }
|
---|
766 |
|
---|
767 |
|
---|
768 | static
|
---|
769 | void clear_lock ( /* Clear lock entries of the volume */
|
---|
770 | FATFS *fs
|
---|
771 | )
|
---|
772 | {
|
---|
773 | UINT i;
|
---|
774 |
|
---|
775 | for (i = 0; i < FF_FS_LOCK; i++) {
|
---|
776 | if (Files[i].fs == fs) Files[i].fs = 0;
|
---|
777 | }
|
---|
778 | }
|
---|
779 | #endif
|
---|
780 |
|
---|
781 |
|
---|
782 |
|
---|
783 |
|
---|
784 | /*-----------------------------------------------------------------------*/
|
---|
785 | /* Move/Flush disk access window in the file system object */
|
---|
786 | /*-----------------------------------------------------------------------*/
|
---|
787 | #if !FF_FS_READONLY
|
---|
788 | static
|
---|
789 | FRESULT sync_window ( /* FR_OK:succeeded, !=0:error */
|
---|
790 | FATFS* fs /* File system object */
|
---|
791 | )
|
---|
792 | {
|
---|
793 | DWORD wsect;
|
---|
794 | UINT nf;
|
---|
795 | FRESULT res = FR_OK;
|
---|
796 |
|
---|
797 |
|
---|
798 | if (fs->wflag) { /* Write back the sector if it is dirty */
|
---|
799 | wsect = fs->winsect; /* Current sector number */
|
---|
800 | if (disk_write(fs->drv, fs->win, wsect, 1) != RES_OK) {
|
---|
801 | res = FR_DISK_ERR;
|
---|
802 | } else {
|
---|
803 | fs->wflag = 0;
|
---|
804 | if (wsect - fs->fatbase < fs->fsize) { /* Is it in the FAT area? */
|
---|
805 | for (nf = fs->n_fats; nf >= 2; nf--) { /* Reflect the change to all FAT copies */
|
---|
806 | wsect += fs->fsize;
|
---|
807 | disk_write(fs->drv, fs->win, wsect, 1);
|
---|
808 | }
|
---|
809 | }
|
---|
810 | }
|
---|
811 | }
|
---|
812 | return res;
|
---|
813 | }
|
---|
814 | #endif
|
---|
815 |
|
---|
816 |
|
---|
817 | static
|
---|
818 | FRESULT move_window ( /* FR_OK(0):succeeded, !=0:error */
|
---|
819 | FATFS* fs, /* File system object */
|
---|
820 | DWORD sector /* Sector number to make appearance in the fs->win[] */
|
---|
821 | )
|
---|
822 | {
|
---|
823 | FRESULT res = FR_OK;
|
---|
824 |
|
---|
825 |
|
---|
826 | if (sector != fs->winsect) { /* Window offset changed? */
|
---|
827 | #if !FF_FS_READONLY
|
---|
828 | res = sync_window(fs); /* Write-back changes */
|
---|
829 | #endif
|
---|
830 | if (res == FR_OK) { /* Fill sector window with new data */
|
---|
831 | if (disk_read(fs->drv, fs->win, sector, 1) != RES_OK) {
|
---|
832 | sector = 0xFFFFFFFF; /* Invalidate window if data is not reliable */
|
---|
833 | res = FR_DISK_ERR;
|
---|
834 | }
|
---|
835 | fs->winsect = sector;
|
---|
836 | }
|
---|
837 | }
|
---|
838 | return res;
|
---|
839 | }
|
---|
840 |
|
---|
841 |
|
---|
842 |
|
---|
843 |
|
---|
844 | /*-----------------------------------------------------------------------*/
|
---|
845 | /* Synchronize file system and strage device */
|
---|
846 | /*-----------------------------------------------------------------------*/
|
---|
847 | #if !FF_FS_READONLY
|
---|
848 | static
|
---|
849 | FRESULT sync_fs ( /* FR_OK:succeeded, !=0:error */
|
---|
850 | FATFS* fs /* File system object */
|
---|
851 | )
|
---|
852 | {
|
---|
853 | FRESULT res;
|
---|
854 |
|
---|
855 |
|
---|
856 | res = sync_window(fs);
|
---|
857 | if (res == FR_OK) {
|
---|
858 | /* Update FSInfo sector if needed */
|
---|
859 | if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) {
|
---|
860 | /* Create FSInfo structure */
|
---|
861 | mem_set(fs->win, 0, SS(fs));
|
---|
862 | ST_WORD(fs->win + BS_55AA, 0xAA55);
|
---|
863 | ST_DWORD(fs->win + FSI_LeadSig, 0x41615252);
|
---|
864 | ST_DWORD(fs->win + FSI_StrucSig, 0x61417272);
|
---|
865 | ST_DWORD(fs->win + FSI_Free_Count, fs->free_clust);
|
---|
866 | ST_DWORD(fs->win + FSI_Nxt_Free, fs->last_clust);
|
---|
867 | /* Write it into the FSInfo sector */
|
---|
868 | fs->winsect = fs->volbase + 1;
|
---|
869 | disk_write(fs->drv, fs->win, fs->winsect, 1);
|
---|
870 | fs->fsi_flag = 0;
|
---|
871 | }
|
---|
872 | /* Make sure that no pending write process in the physical drive */
|
---|
873 | if (disk_ioctl(fs->drv, CTRL_SYNC, 0) != RES_OK)
|
---|
874 | res = FR_DISK_ERR;
|
---|
875 | }
|
---|
876 |
|
---|
877 | return res;
|
---|
878 | }
|
---|
879 | #endif
|
---|
880 |
|
---|
881 |
|
---|
882 |
|
---|
883 |
|
---|
884 | /*-----------------------------------------------------------------------*/
|
---|
885 | /* Get sector# from cluster# */
|
---|
886 | /*-----------------------------------------------------------------------*/
|
---|
887 | /* Hidden API for hacks and disk tools */
|
---|
888 |
|
---|
889 | DWORD clust2sect ( /* !=0:Sector number, 0:Failed (invalid cluster#) */
|
---|
890 | FATFS* fs, /* File system object */
|
---|
891 | DWORD clst /* Cluster# to be converted */
|
---|
892 | )
|
---|
893 | {
|
---|
894 | clst -= 2;
|
---|
895 | if (clst >= fs->n_fatent - 2) return 0; /* Invalid cluster# */
|
---|
896 | return clst * fs->csize + fs->database;
|
---|
897 | }
|
---|
898 |
|
---|
899 |
|
---|
900 |
|
---|
901 |
|
---|
902 | /*-----------------------------------------------------------------------*/
|
---|
903 | /* FAT access - Read value of a FAT entry */
|
---|
904 | /*-----------------------------------------------------------------------*/
|
---|
905 | /* Hidden API for hacks and disk tools */
|
---|
906 |
|
---|
907 | DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x0FFFFFFF:Cluster status */
|
---|
908 | FATFS* fs, /* File system object */
|
---|
909 | DWORD clst /* FAT index number (cluster number) to get the value */
|
---|
910 | )
|
---|
911 | {
|
---|
912 | UINT wc, bc;
|
---|
913 | BYTE *p;
|
---|
914 | DWORD val;
|
---|
915 |
|
---|
916 |
|
---|
917 | if (clst < 2 || clst >= fs->n_fatent) { /* Check if in valid range */
|
---|
918 | val = 1; /* Internal error */
|
---|
919 |
|
---|
920 | } else {
|
---|
921 | val = 0xFFFFFFFF; /* Default value falls on disk error */
|
---|
922 |
|
---|
923 | switch (fs->fs_type) {
|
---|
924 | case FS_FAT12 :
|
---|
925 | bc = (UINT)clst; bc += bc / 2;
|
---|
926 | if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;
|
---|
927 | wc = fs->win[bc++ % SS(fs)];
|
---|
928 | if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;
|
---|
929 | wc |= fs->win[bc % SS(fs)] << 8;
|
---|
930 | val = clst & 1 ? wc >> 4 : (wc & 0xFFF);
|
---|
931 | break;
|
---|
932 |
|
---|
933 | case FS_FAT16 :
|
---|
934 | if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))) != FR_OK) break;
|
---|
935 | p = &fs->win[clst * 2 % SS(fs)];
|
---|
936 | val = LD_WORD(p);
|
---|
937 | break;
|
---|
938 |
|
---|
939 | case FS_FAT32 :
|
---|
940 | if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;
|
---|
941 | p = &fs->win[clst * 4 % SS(fs)];
|
---|
942 | val = LD_DWORD(p) & 0x0FFFFFFF;
|
---|
943 | break;
|
---|
944 |
|
---|
945 | default:
|
---|
946 | val = 1; /* Internal error */
|
---|
947 | }
|
---|
948 | }
|
---|
949 |
|
---|
950 | return val;
|
---|
951 | }
|
---|
952 |
|
---|
953 |
|
---|
954 |
|
---|
955 |
|
---|
956 | /*-----------------------------------------------------------------------*/
|
---|
957 | /* FAT access - Change value of a FAT entry */
|
---|
958 | /*-----------------------------------------------------------------------*/
|
---|
959 | /* Hidden API for hacks and disk tools */
|
---|
960 |
|
---|
961 | #if !FF_FS_READONLY
|
---|
962 | FRESULT put_fat ( /* FR_OK(0):succeeded, !=0:error */
|
---|
963 | FATFS* fs, /* File system object */
|
---|
964 | DWORD clst, /* FAT index number (cluster number) to be changed */
|
---|
965 | DWORD val /* New value to be set to the entry */
|
---|
966 | )
|
---|
967 | {
|
---|
968 | UINT bc;
|
---|
969 | BYTE *p;
|
---|
970 | FRESULT res;
|
---|
971 |
|
---|
972 |
|
---|
973 | if (clst < 2 || clst >= fs->n_fatent) { /* Check if in valid range */
|
---|
974 | res = FR_INT_ERR;
|
---|
975 |
|
---|
976 | } else {
|
---|
977 | switch (fs->fs_type) {
|
---|
978 | case FS_FAT12 :
|
---|
979 | bc = (UINT)clst; bc += bc / 2;
|
---|
980 | res = move_window(fs, fs->fatbase + (bc / SS(fs)));
|
---|
981 | if (res != FR_OK) break;
|
---|
982 | p = &fs->win[bc++ % SS(fs)];
|
---|
983 | *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
|
---|
984 | fs->wflag = 1;
|
---|
985 | res = move_window(fs, fs->fatbase + (bc / SS(fs)));
|
---|
986 | if (res != FR_OK) break;
|
---|
987 | p = &fs->win[bc % SS(fs)];
|
---|
988 | *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
|
---|
989 | fs->wflag = 1;
|
---|
990 | break;
|
---|
991 |
|
---|
992 | case FS_FAT16 :
|
---|
993 | res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
|
---|
994 | if (res != FR_OK) break;
|
---|
995 | p = &fs->win[clst * 2 % SS(fs)];
|
---|
996 | ST_WORD(p, (WORD)val);
|
---|
997 | fs->wflag = 1;
|
---|
998 | break;
|
---|
999 |
|
---|
1000 | case FS_FAT32 :
|
---|
1001 | res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
|
---|
1002 | if (res != FR_OK) break;
|
---|
1003 | p = &fs->win[clst * 4 % SS(fs)];
|
---|
1004 | val |= LD_DWORD(p) & 0xF0000000;
|
---|
1005 | ST_DWORD(p, val);
|
---|
1006 | fs->wflag = 1;
|
---|
1007 | break;
|
---|
1008 |
|
---|
1009 | default :
|
---|
1010 | res = FR_INT_ERR;
|
---|
1011 | }
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | return res;
|
---|
1015 | }
|
---|
1016 | #endif /* !FF_FS_READONLY */
|
---|
1017 |
|
---|
1018 |
|
---|
1019 |
|
---|
1020 |
|
---|
1021 | /*-----------------------------------------------------------------------*/
|
---|
1022 | /* FAT handling - Remove a cluster chain */
|
---|
1023 | /*-----------------------------------------------------------------------*/
|
---|
1024 | #if !FF_FS_READONLY
|
---|
1025 | static
|
---|
1026 | FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */
|
---|
1027 | FATFS* fs, /* File system object */
|
---|
1028 | DWORD clst /* Cluster# to remove a chain from */
|
---|
1029 | )
|
---|
1030 | {
|
---|
1031 | FRESULT res;
|
---|
1032 | DWORD nxt;
|
---|
1033 | #if FF_USE_TRIM
|
---|
1034 | DWORD scl = clst, ecl = clst, rt[2];
|
---|
1035 | #endif
|
---|
1036 |
|
---|
1037 | if (clst < 2 || clst >= fs->n_fatent) { /* Check if in valid range */
|
---|
1038 | res = FR_INT_ERR;
|
---|
1039 |
|
---|
1040 | } else {
|
---|
1041 | res = FR_OK;
|
---|
1042 | while (clst < fs->n_fatent) { /* Not a last link? */
|
---|
1043 | nxt = get_fat(fs, clst); /* Get cluster status */
|
---|
1044 | if (nxt == 0) break; /* Empty cluster? */
|
---|
1045 | if (nxt == 1) { res = FR_INT_ERR; break; } /* Internal error? */
|
---|
1046 | if (nxt == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } /* Disk error? */
|
---|
1047 | res = put_fat(fs, clst, 0); /* Mark the cluster "empty" */
|
---|
1048 | if (res != FR_OK) break;
|
---|
1049 | if (fs->free_clust != 0xFFFFFFFF) { /* Update FSINFO */
|
---|
1050 | fs->free_clust++;
|
---|
1051 | fs->fsi_flag |= 1;
|
---|
1052 | }
|
---|
1053 | #if FF_USE_TRIM
|
---|
1054 | if (ecl + 1 == nxt) { /* Is next cluster contiguous? */
|
---|
1055 | ecl = nxt;
|
---|
1056 | } else { /* End of contiguous clusters */
|
---|
1057 | rt[0] = clust2sect(fs, scl); /* Start sector */
|
---|
1058 | rt[1] = clust2sect(fs, ecl) + fs->csize - 1; /* End sector */
|
---|
1059 | disk_ioctl(fs->drv, CTRL_TRIM, rt); /* Erase the block */
|
---|
1060 | scl = ecl = nxt;
|
---|
1061 | }
|
---|
1062 | #endif
|
---|
1063 | clst = nxt; /* Next cluster */
|
---|
1064 | }
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | return res;
|
---|
1068 | }
|
---|
1069 | #endif
|
---|
1070 |
|
---|
1071 |
|
---|
1072 |
|
---|
1073 |
|
---|
1074 | /*-----------------------------------------------------------------------*/
|
---|
1075 | /* FAT handling - Stretch or Create a cluster chain */
|
---|
1076 | /*-----------------------------------------------------------------------*/
|
---|
1077 | #if !FF_FS_READONLY
|
---|
1078 | static
|
---|
1079 | DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */
|
---|
1080 | FATFS* fs, /* File system object */
|
---|
1081 | DWORD clst /* Cluster# to stretch, 0:Create a new chain */
|
---|
1082 | )
|
---|
1083 | {
|
---|
1084 | DWORD cs, ncl, scl;
|
---|
1085 | FRESULT res;
|
---|
1086 |
|
---|
1087 |
|
---|
1088 | if (clst == 0) { /* Create a new chain */
|
---|
1089 | scl = fs->last_clust; /* Get suggested start point */
|
---|
1090 | if (!scl || scl >= fs->n_fatent) scl = 1;
|
---|
1091 | }
|
---|
1092 | else { /* Stretch the current chain */
|
---|
1093 | cs = get_fat(fs, clst); /* Check the cluster status */
|
---|
1094 | if (cs < 2) return 1; /* Invalid value */
|
---|
1095 | if (cs == 0xFFFFFFFF) return cs; /* A disk error occurred */
|
---|
1096 | if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */
|
---|
1097 | scl = clst;
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | ncl = scl; /* Start cluster */
|
---|
1101 | for (;;) {
|
---|
1102 | ncl++; /* Next cluster */
|
---|
1103 | if (ncl >= fs->n_fatent) { /* Check wrap around */
|
---|
1104 | ncl = 2;
|
---|
1105 | if (ncl > scl) return 0; /* No free cluster */
|
---|
1106 | }
|
---|
1107 | cs = get_fat(fs, ncl); /* Get the cluster status */
|
---|
1108 | if (cs == 0) break; /* Found a free cluster */
|
---|
1109 | if (cs == 0xFFFFFFFF || cs == 1)/* An error occurred */
|
---|
1110 | return cs;
|
---|
1111 | if (ncl == scl) return 0; /* No free cluster */
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | res = put_fat(fs, ncl, 0x0FFFFFFF); /* Mark the new cluster "last link" */
|
---|
1115 | if (res == FR_OK && clst != 0) {
|
---|
1116 | res = put_fat(fs, clst, ncl); /* Link it to the previous one if needed */
|
---|
1117 | }
|
---|
1118 | if (res == FR_OK) {
|
---|
1119 | fs->last_clust = ncl; /* Update FSINFO */
|
---|
1120 | if (fs->free_clust != 0xFFFFFFFF) {
|
---|
1121 | fs->free_clust--;
|
---|
1122 | fs->fsi_flag |= 1;
|
---|
1123 | }
|
---|
1124 | } else {
|
---|
1125 | ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1;
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | return ncl; /* Return new cluster number or error code */
|
---|
1129 | }
|
---|
1130 | #endif /* !FF_FS_READONLY */
|
---|
1131 |
|
---|
1132 |
|
---|
1133 |
|
---|
1134 |
|
---|
1135 | /*-----------------------------------------------------------------------*/
|
---|
1136 | /* FAT handling - Convert offset into cluster with link map table */
|
---|
1137 | /*-----------------------------------------------------------------------*/
|
---|
1138 |
|
---|
1139 | #if FF_USE_FASTSEEK
|
---|
1140 | static
|
---|
1141 | DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */
|
---|
1142 | FIL* fp, /* Pointer to the file object */
|
---|
1143 | DWORD ofs /* File offset to be converted to cluster# */
|
---|
1144 | )
|
---|
1145 | {
|
---|
1146 | DWORD cl, ncl, *tbl;
|
---|
1147 |
|
---|
1148 |
|
---|
1149 | tbl = fp->cltbl + 1; /* Top of CLMT */
|
---|
1150 | cl = ofs / SS(fp->fs) / fp->fs->csize; /* Cluster order from top of the file */
|
---|
1151 | for (;;) {
|
---|
1152 | ncl = *tbl++; /* Number of cluters in the fragment */
|
---|
1153 | if (!ncl) return 0; /* End of table? (error) */
|
---|
1154 | if (cl < ncl) break; /* In this fragment? */
|
---|
1155 | cl -= ncl; tbl++; /* Next fragment */
|
---|
1156 | }
|
---|
1157 | return cl + *tbl; /* Return the cluster number */
|
---|
1158 | }
|
---|
1159 | #endif /* FF_USE_FASTSEEK */
|
---|
1160 |
|
---|
1161 |
|
---|
1162 |
|
---|
1163 |
|
---|
1164 | /*-----------------------------------------------------------------------*/
|
---|
1165 | /* Directory handling - Set directory index */
|
---|
1166 | /*-----------------------------------------------------------------------*/
|
---|
1167 |
|
---|
1168 | static
|
---|
1169 | FRESULT dir_sdi ( /* FR_OK(0):succeeded, !=0:error */
|
---|
1170 | FATFS_DIR* dp, /* Pointer to directory object */
|
---|
1171 | UINT idx /* Index of directory table */
|
---|
1172 | )
|
---|
1173 | {
|
---|
1174 | DWORD clst, sect;
|
---|
1175 | UINT ic;
|
---|
1176 |
|
---|
1177 |
|
---|
1178 | dp->index = (WORD)idx; /* Current index */
|
---|
1179 | clst = dp->sclust; /* Table start cluster (0:root) */
|
---|
1180 | if (clst == 1 || clst >= dp->fs->n_fatent) /* Check start cluster range */
|
---|
1181 | return FR_INT_ERR;
|
---|
1182 | if (!clst && dp->fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */
|
---|
1183 | clst = dp->fs->dirbase;
|
---|
1184 |
|
---|
1185 | if (clst == 0) { /* Static table (root-directory in FAT12/16) */
|
---|
1186 | if (idx >= dp->fs->n_rootdir) /* Is index out of range? */
|
---|
1187 | return FR_INT_ERR;
|
---|
1188 | sect = dp->fs->dirbase;
|
---|
1189 | }
|
---|
1190 | else { /* Dynamic table (root-directory in FAT32 or sub-directory) */
|
---|
1191 | ic = SS(dp->fs) / SZ_DIRE * dp->fs->csize; /* Entries per cluster */
|
---|
1192 | while (idx >= ic) { /* Follow cluster chain */
|
---|
1193 | clst = get_fat(dp->fs, clst); /* Get next cluster */
|
---|
1194 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
|
---|
1195 | if (clst < 2 || clst >= dp->fs->n_fatent) /* Reached to end of table or internal error */
|
---|
1196 | return FR_INT_ERR;
|
---|
1197 | idx -= ic;
|
---|
1198 | }
|
---|
1199 | sect = clust2sect(dp->fs, clst);
|
---|
1200 | }
|
---|
1201 | dp->clust = clst; /* Current cluster# */
|
---|
1202 | if (!sect) return FR_INT_ERR;
|
---|
1203 | dp->sect = sect + idx / (SS(dp->fs) / SZ_DIRE); /* Sector# of the directory entry */
|
---|
1204 | dp->dir = dp->fs->win + (idx % (SS(dp->fs) / SZ_DIRE)) * SZ_DIRE; /* Ptr to the entry in the sector */
|
---|
1205 |
|
---|
1206 | return FR_OK;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 |
|
---|
1210 |
|
---|
1211 |
|
---|
1212 | /*-----------------------------------------------------------------------*/
|
---|
1213 | /* Directory handling - Move directory table index next */
|
---|
1214 | /*-----------------------------------------------------------------------*/
|
---|
1215 |
|
---|
1216 | static
|
---|
1217 | FRESULT dir_next ( /* FR_OK(0):succeeded, FR_NO_FILE:End of table, FR_DENIED:Could not stretch */
|
---|
1218 | FATFS_DIR* dp, /* Pointer to the directory object */
|
---|
1219 | int stretch /* 0: Do not stretch table, 1: Stretch table if needed */
|
---|
1220 | )
|
---|
1221 | {
|
---|
1222 | DWORD clst;
|
---|
1223 | UINT i;
|
---|
1224 | #if !FF_FS_READONLY
|
---|
1225 | UINT c;
|
---|
1226 | #endif
|
---|
1227 |
|
---|
1228 |
|
---|
1229 | i = dp->index + 1;
|
---|
1230 | if (!(i & 0xFFFF) || !dp->sect) /* Report EOT when index has reached 65535 */
|
---|
1231 | return FR_NO_FILE;
|
---|
1232 |
|
---|
1233 | if (!(i % (SS(dp->fs) / SZ_DIRE))) { /* Sector changed? */
|
---|
1234 | dp->sect++; /* Next sector */
|
---|
1235 |
|
---|
1236 | if (!dp->clust) { /* Static table */
|
---|
1237 | if (i >= dp->fs->n_rootdir) /* Report EOT if it reached end of static table */
|
---|
1238 | return FR_NO_FILE;
|
---|
1239 | }
|
---|
1240 | else { /* Dynamic table */
|
---|
1241 | if (((i / (SS(dp->fs) / SZ_DIRE)) & (dp->fs->csize - 1)) == 0) { /* Cluster changed? */
|
---|
1242 | clst = get_fat(dp->fs, dp->clust); /* Get next cluster */
|
---|
1243 | if (clst <= 1) return FR_INT_ERR;
|
---|
1244 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
|
---|
1245 | if (clst >= dp->fs->n_fatent) { /* If it reached end of dynamic table, */
|
---|
1246 | #if !FF_FS_READONLY
|
---|
1247 | if (!stretch) return FR_NO_FILE; /* If do not stretch, report EOT */
|
---|
1248 | clst = create_chain(dp->fs, dp->clust); /* Stretch cluster chain */
|
---|
1249 | if (clst == 0) return FR_DENIED; /* No free cluster */
|
---|
1250 | if (clst == 1) return FR_INT_ERR;
|
---|
1251 | if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
|
---|
1252 | /* Clean-up stretched table */
|
---|
1253 | if (sync_window(dp->fs)) return FR_DISK_ERR;/* Flush disk access window */
|
---|
1254 | mem_set(dp->fs->win, 0, SS(dp->fs)); /* Clear window buffer */
|
---|
1255 | dp->fs->winsect = clust2sect(dp->fs, clst); /* Cluster start sector */
|
---|
1256 | for (c = 0; c < dp->fs->csize; c++) { /* Fill the new cluster with 0 */
|
---|
1257 | dp->fs->wflag = 1;
|
---|
1258 | if (sync_window(dp->fs)) return FR_DISK_ERR;
|
---|
1259 | dp->fs->winsect++;
|
---|
1260 | }
|
---|
1261 | dp->fs->winsect -= c; /* Rewind window offset */
|
---|
1262 | #else
|
---|
1263 | if (!stretch) return FR_NO_FILE; /* If do not stretch, report EOT (this is to suppress warning) */
|
---|
1264 | return FR_NO_FILE; /* Report EOT */
|
---|
1265 | #endif
|
---|
1266 | }
|
---|
1267 | dp->clust = clst; /* Initialize data for new cluster */
|
---|
1268 | dp->sect = clust2sect(dp->fs, clst);
|
---|
1269 | }
|
---|
1270 | }
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | dp->index = (WORD)i; /* Current index */
|
---|
1274 | dp->dir = dp->fs->win + (i % (SS(dp->fs) / SZ_DIRE)) * SZ_DIRE; /* Current entry in the window */
|
---|
1275 |
|
---|
1276 | return FR_OK;
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 |
|
---|
1280 |
|
---|
1281 |
|
---|
1282 | /*-----------------------------------------------------------------------*/
|
---|
1283 | /* Directory handling - Reserve directory entry */
|
---|
1284 | /*-----------------------------------------------------------------------*/
|
---|
1285 |
|
---|
1286 | #if !FF_FS_READONLY
|
---|
1287 | static
|
---|
1288 | FRESULT dir_alloc ( /* FR_OK(0):succeeded, !=0:error */
|
---|
1289 | FATFS_DIR* dp, /* Pointer to the directory object */
|
---|
1290 | UINT nent /* Number of contiguous entries to allocate (1-21) */
|
---|
1291 | )
|
---|
1292 | {
|
---|
1293 | FRESULT res;
|
---|
1294 | UINT n;
|
---|
1295 |
|
---|
1296 |
|
---|
1297 | res = dir_sdi(dp, 0);
|
---|
1298 | if (res == FR_OK) {
|
---|
1299 | n = 0;
|
---|
1300 | do {
|
---|
1301 | res = move_window(dp->fs, dp->sect);
|
---|
1302 | if (res != FR_OK) break;
|
---|
1303 | if (dp->dir[0] == DDEM || dp->dir[0] == 0) { /* Is it a free entry? */
|
---|
1304 | if (++n == nent) break; /* A block of contiguous free entries is found */
|
---|
1305 | } else {
|
---|
1306 | n = 0; /* Not a blank entry. Restart to search */
|
---|
1307 | }
|
---|
1308 | res = dir_next(dp, 1); /* Next entry with table stretch enabled */
|
---|
1309 | } while (res == FR_OK);
|
---|
1310 | }
|
---|
1311 | if (res == FR_NO_FILE) res = FR_DENIED; /* No directory entry to allocate */
|
---|
1312 | return res;
|
---|
1313 | }
|
---|
1314 | #endif
|
---|
1315 |
|
---|
1316 |
|
---|
1317 |
|
---|
1318 |
|
---|
1319 | /*-----------------------------------------------------------------------*/
|
---|
1320 | /* Directory handling - Load/Store start cluster number */
|
---|
1321 | /*-----------------------------------------------------------------------*/
|
---|
1322 |
|
---|
1323 | static
|
---|
1324 | DWORD ld_clust ( /* Returns the top cluster value of the SFN entry */
|
---|
1325 | FATFS* fs, /* Pointer to the fs object */
|
---|
1326 | const BYTE* dir /* Pointer to the SFN entry */
|
---|
1327 | )
|
---|
1328 | {
|
---|
1329 | DWORD cl;
|
---|
1330 |
|
---|
1331 | cl = LD_WORD(dir + DIR_FstClusLO);
|
---|
1332 | if (fs->fs_type == FS_FAT32)
|
---|
1333 | cl |= (DWORD)LD_WORD(dir + DIR_FstClusHI) << 16;
|
---|
1334 |
|
---|
1335 | return cl;
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 |
|
---|
1339 | #if !FF_FS_READONLY
|
---|
1340 | static
|
---|
1341 | void st_clust (
|
---|
1342 | BYTE* dir, /* Pointer to the SFN entry */
|
---|
1343 | DWORD cl /* Value to be set */
|
---|
1344 | )
|
---|
1345 | {
|
---|
1346 | ST_WORD(dir + DIR_FstClusLO, cl);
|
---|
1347 | ST_WORD(dir + DIR_FstClusHI, cl >> 16);
|
---|
1348 | }
|
---|
1349 | #endif
|
---|
1350 |
|
---|
1351 |
|
---|
1352 |
|
---|
1353 |
|
---|
1354 | /*-----------------------------------------------------------------------*/
|
---|
1355 | /* LFN handling - Test/Pick/Fit an LFN segment from/to directory entry */
|
---|
1356 | /*-----------------------------------------------------------------------*/
|
---|
1357 | #if FF_USE_LFN
|
---|
1358 | static
|
---|
1359 | const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN characters in the directory entry */
|
---|
1360 |
|
---|
1361 |
|
---|
1362 | static
|
---|
1363 | int cmp_lfn ( /* 1:matched, 0:not matched */
|
---|
1364 | WCHAR* lfnbuf, /* Pointer to the LFN working buffer to be compared */
|
---|
1365 | BYTE* dir /* Pointer to the directory entry containing the part of LFN */
|
---|
1366 | )
|
---|
1367 | {
|
---|
1368 | UINT i, s;
|
---|
1369 | WCHAR wc, uc;
|
---|
1370 |
|
---|
1371 |
|
---|
1372 | if (LD_WORD(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO */
|
---|
1373 |
|
---|
1374 | i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
|
---|
1375 |
|
---|
1376 | for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */
|
---|
1377 | uc = LD_WORD(dir + LfnOfs[s]); /* Pick an LFN character */
|
---|
1378 | if (wc) {
|
---|
1379 | if (i >= FF_MAX_LFN || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) /* Compare it */
|
---|
1380 | return 0; /* Not matched */
|
---|
1381 | wc = uc;
|
---|
1382 | } else {
|
---|
1383 | if (uc != 0xFFFF) return 0; /* Check filler */
|
---|
1384 | }
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | if ((dir[LDIR_Ord] & LLEF) && wc && lfnbuf[i]) /* Last segment matched but different length */
|
---|
1388 | return 0;
|
---|
1389 |
|
---|
1390 | return 1; /* The part of LFN matched */
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 |
|
---|
1394 |
|
---|
1395 | static
|
---|
1396 | int pick_lfn ( /* 1:succeeded, 0:buffer overflow or invalid LFN entry */
|
---|
1397 | WCHAR* lfnbuf, /* Pointer to the LFN working buffer */
|
---|
1398 | BYTE* dir /* Pointer to the LFN entry */
|
---|
1399 | )
|
---|
1400 | {
|
---|
1401 | UINT i, s;
|
---|
1402 | WCHAR wc, uc;
|
---|
1403 |
|
---|
1404 |
|
---|
1405 | if (LD_WORD(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO */
|
---|
1406 |
|
---|
1407 | i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
|
---|
1408 |
|
---|
1409 | for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */
|
---|
1410 | uc = LD_WORD(dir + LfnOfs[s]); /* Pick an LFN character */
|
---|
1411 | if (wc) {
|
---|
1412 | if (i >= FF_MAX_LFN) return 0; /* Buffer overflow? */
|
---|
1413 | lfnbuf[i++] = wc = uc; /* Store it */
|
---|
1414 | } else {
|
---|
1415 | if (uc != 0xFFFF) return 0; /* Check filler */
|
---|
1416 | }
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | if (dir[LDIR_Ord] & LLEF) { /* Put terminator if it is the last LFN part */
|
---|
1420 | if (i >= FF_MAX_LFN) return 0; /* Buffer overflow? */
|
---|
1421 | lfnbuf[i] = 0;
|
---|
1422 | }
|
---|
1423 |
|
---|
1424 | return 1; /* The part of LFN is valid */
|
---|
1425 | }
|
---|
1426 |
|
---|
1427 |
|
---|
1428 | #if !FF_FS_READONLY
|
---|
1429 | static
|
---|
1430 | void fit_lfn (
|
---|
1431 | const WCHAR* lfnbuf, /* Pointer to the LFN working buffer */
|
---|
1432 | BYTE* dir, /* Pointer to the LFN entry to be processed */
|
---|
1433 | BYTE ord, /* LFN order (1-20) */
|
---|
1434 | BYTE sum /* Checksum of the corresponding SFN */
|
---|
1435 | )
|
---|
1436 | {
|
---|
1437 | UINT i, s;
|
---|
1438 | WCHAR wc;
|
---|
1439 |
|
---|
1440 |
|
---|
1441 | dir[LDIR_Chksum] = sum; /* Set checksum */
|
---|
1442 | dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */
|
---|
1443 | dir[LDIR_Type] = 0;
|
---|
1444 | ST_WORD(dir + LDIR_FstClusLO, 0);
|
---|
1445 |
|
---|
1446 | i = (ord - 1) * 13; /* Get offset in the LFN working buffer */
|
---|
1447 | s = wc = 0;
|
---|
1448 | do {
|
---|
1449 | if (wc != 0xFFFF) wc = lfnbuf[i++]; /* Get an effective character */
|
---|
1450 | ST_WORD(dir+LfnOfs[s], wc); /* Put it */
|
---|
1451 | if (!wc) wc = 0xFFFF; /* Padding characters following last character */
|
---|
1452 | } while (++s < 13);
|
---|
1453 | if (wc == 0xFFFF || !lfnbuf[i]) ord |= LLEF; /* Bottom LFN part is the start of LFN sequence */
|
---|
1454 | dir[LDIR_Ord] = ord; /* Set the LFN order */
|
---|
1455 | }
|
---|
1456 |
|
---|
1457 | #endif
|
---|
1458 | #endif
|
---|
1459 |
|
---|
1460 |
|
---|
1461 |
|
---|
1462 |
|
---|
1463 | /*-----------------------------------------------------------------------*/
|
---|
1464 | /* Create numbered name */
|
---|
1465 | /*-----------------------------------------------------------------------*/
|
---|
1466 | #if FF_USE_LFN
|
---|
1467 | static
|
---|
1468 | void gen_numname (
|
---|
1469 | BYTE* dst, /* Pointer to the buffer to store numbered SFN */
|
---|
1470 | const BYTE* src, /* Pointer to SFN */
|
---|
1471 | const WCHAR* lfn, /* Pointer to LFN */
|
---|
1472 | UINT seq /* Sequence number */
|
---|
1473 | )
|
---|
1474 | {
|
---|
1475 | BYTE ns[8], c;
|
---|
1476 | UINT i, j;
|
---|
1477 | WCHAR wc;
|
---|
1478 | DWORD sr;
|
---|
1479 |
|
---|
1480 |
|
---|
1481 | mem_cpy(dst, src, 11);
|
---|
1482 |
|
---|
1483 | if (seq > 5) { /* On many collisions, generate a hash number instead of sequential number */
|
---|
1484 | sr = seq;
|
---|
1485 | while (*lfn) { /* Create a CRC */
|
---|
1486 | wc = *lfn++;
|
---|
1487 | for (i = 0; i < 16; i++) {
|
---|
1488 | sr = (sr << 1) + (wc & 1);
|
---|
1489 | wc >>= 1;
|
---|
1490 | if (sr & 0x10000) sr ^= 0x11021;
|
---|
1491 | }
|
---|
1492 | }
|
---|
1493 | seq = (UINT)sr;
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | /* itoa (hexdecimal) */
|
---|
1497 | i = 7;
|
---|
1498 | do {
|
---|
1499 | c = (seq % 16) + '0';
|
---|
1500 | if (c > '9') c += 7;
|
---|
1501 | ns[i--] = c;
|
---|
1502 | seq /= 16;
|
---|
1503 | } while (seq);
|
---|
1504 | ns[i] = '~';
|
---|
1505 |
|
---|
1506 | /* Append the number */
|
---|
1507 | for (j = 0; j < i && dst[j] != ' '; j++) {
|
---|
1508 | if (IsDBCS1(dst[j])) {
|
---|
1509 | if (j == i - 1) break;
|
---|
1510 | j++;
|
---|
1511 | }
|
---|
1512 | }
|
---|
1513 | do {
|
---|
1514 | dst[j++] = (i < 8) ? ns[i++] : ' ';
|
---|
1515 | } while (j < 8);
|
---|
1516 | }
|
---|
1517 | #endif
|
---|
1518 |
|
---|
1519 |
|
---|
1520 |
|
---|
1521 |
|
---|
1522 | /*-----------------------------------------------------------------------*/
|
---|
1523 | /* Calculate checksum of an SFN entry */
|
---|
1524 | /*-----------------------------------------------------------------------*/
|
---|
1525 | #if FF_USE_LFN
|
---|
1526 | static
|
---|
1527 | BYTE sum_sfn (
|
---|
1528 | const BYTE* dir /* Pointer to the SFN entry */
|
---|
1529 | )
|
---|
1530 | {
|
---|
1531 | BYTE sum = 0;
|
---|
1532 | UINT n = 11;
|
---|
1533 |
|
---|
1534 | do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);
|
---|
1535 | return sum;
|
---|
1536 | }
|
---|
1537 | #endif
|
---|
1538 |
|
---|
1539 |
|
---|
1540 |
|
---|
1541 |
|
---|
1542 | /*-----------------------------------------------------------------------*/
|
---|
1543 | /* Directory handling - Find an object in the directory */
|
---|
1544 | /*-----------------------------------------------------------------------*/
|
---|
1545 |
|
---|
1546 | static
|
---|
1547 | FRESULT dir_find ( /* FR_OK(0):succeeded, !=0:error */
|
---|
1548 | FATFS_DIR* dp /* Pointer to the directory object linked to the file name */
|
---|
1549 | )
|
---|
1550 | {
|
---|
1551 | FRESULT res;
|
---|
1552 | BYTE c, *dir;
|
---|
1553 | #if FF_USE_LFN
|
---|
1554 | BYTE a, ord, sum;
|
---|
1555 | #endif
|
---|
1556 |
|
---|
1557 | res = dir_sdi(dp, 0); /* Rewind directory object */
|
---|
1558 | if (res != FR_OK) return res;
|
---|
1559 |
|
---|
1560 | #if FF_USE_LFN
|
---|
1561 | ord = sum = 0xFF; dp->lfn_idx = 0xFFFF; /* Reset LFN sequence */
|
---|
1562 | #endif
|
---|
1563 | do {
|
---|
1564 | res = move_window(dp->fs, dp->sect);
|
---|
1565 | if (res != FR_OK) break;
|
---|
1566 | dir = dp->dir; /* Ptr to the directory entry of current index */
|
---|
1567 | c = dir[DIR_Name];
|
---|
1568 | if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
|
---|
1569 | #if FF_USE_LFN /* LFN configuration */
|
---|
1570 | a = dir[DIR_Attr] & AM_MASK;
|
---|
1571 | if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
|
---|
1572 | ord = 0xFF; dp->lfn_idx = 0xFFFF; /* Reset LFN sequence */
|
---|
1573 | } else {
|
---|
1574 | if (a == AM_LFN) { /* An LFN entry is found */
|
---|
1575 | if (dp->lfn) {
|
---|
1576 | if (c & LLEF) { /* Is it start of LFN sequence? */
|
---|
1577 | sum = dir[LDIR_Chksum];
|
---|
1578 | c &= ~LLEF; ord = c; /* LFN start order */
|
---|
1579 | dp->lfn_idx = dp->index; /* Start index of LFN */
|
---|
1580 | }
|
---|
1581 | /* Check validity of the LFN entry and compare it with given name */
|
---|
1582 | ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dp->lfn, dir)) ? ord - 1 : 0xFF;
|
---|
1583 | }
|
---|
1584 | } else { /* An SFN entry is found */
|
---|
1585 | if (!ord && sum == sum_sfn(dir)) break; /* LFN matched? */
|
---|
1586 | if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dir, dp->fn, 11)) break; /* SFN matched? */
|
---|
1587 | ord = 0xFF; dp->lfn_idx = 0xFFFF; /* Reset LFN sequence */
|
---|
1588 | }
|
---|
1589 | }
|
---|
1590 | #else /* Non LFN configuration */
|
---|
1591 | if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dp->fn, 11)) /* Is it a valid entry? */
|
---|
1592 | break;
|
---|
1593 | #endif
|
---|
1594 | res = dir_next(dp, 0); /* Next entry */
|
---|
1595 | } while (res == FR_OK);
|
---|
1596 |
|
---|
1597 | return res;
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 |
|
---|
1601 |
|
---|
1602 |
|
---|
1603 | /*-----------------------------------------------------------------------*/
|
---|
1604 | /* Read an object from the directory */
|
---|
1605 | /*-----------------------------------------------------------------------*/
|
---|
1606 | #if FF_FS_MINIMIZE <= 1 || FF_USE_LABEL || FF_FS_RPATH >= 2
|
---|
1607 | static
|
---|
1608 | FRESULT dir_read (
|
---|
1609 | FATFS_DIR* dp, /* Pointer to the directory object */
|
---|
1610 | int vol /* Filtered by 0:file/directory or 1:volume label */
|
---|
1611 | )
|
---|
1612 | {
|
---|
1613 | FRESULT res;
|
---|
1614 | BYTE a, c, *dir;
|
---|
1615 | #if FF_USE_LFN
|
---|
1616 | BYTE ord = 0xFF, sum = 0xFF;
|
---|
1617 | #endif
|
---|
1618 |
|
---|
1619 | res = FR_NO_FILE;
|
---|
1620 | while (dp->sect) {
|
---|
1621 | res = move_window(dp->fs, dp->sect);
|
---|
1622 | if (res != FR_OK) break;
|
---|
1623 | dir = dp->dir; /* Ptr to the directory entry of current index */
|
---|
1624 | c = dir[DIR_Name];
|
---|
1625 | if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
|
---|
1626 | a = dir[DIR_Attr] & AM_MASK;
|
---|
1627 | #if FF_USE_LFN /* LFN configuration */
|
---|
1628 | if (c == DDEM || (!FF_FS_RPATH && c == '.') || (int)((a & ~AM_ARC) == AM_VOL) != vol) { /* An entry without valid data */
|
---|
1629 | ord = 0xFF;
|
---|
1630 | } else {
|
---|
1631 | if (a == AM_LFN) { /* An LFN entry is found */
|
---|
1632 | if (c & LLEF) { /* Is it start of LFN sequence? */
|
---|
1633 | sum = dir[LDIR_Chksum];
|
---|
1634 | c &= ~LLEF; ord = c;
|
---|
1635 | dp->lfn_idx = dp->index;
|
---|
1636 | }
|
---|
1637 | /* Check LFN validity and capture it */
|
---|
1638 | ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dp->lfn, dir)) ? ord - 1 : 0xFF;
|
---|
1639 | } else { /* An SFN entry is found */
|
---|
1640 | if (ord || sum != sum_sfn(dir)) /* Is there a valid LFN? */
|
---|
1641 | dp->lfn_idx = 0xFFFF; /* It has no LFN. */
|
---|
1642 | break;
|
---|
1643 | }
|
---|
1644 | }
|
---|
1645 | #else /* Non LFN configuration */
|
---|
1646 | if (c != DDEM && (FF_FS_RPATH || c != '.') && a != AM_LFN && (int)((a & ~AM_ARC) == AM_VOL) == vol) /* Is it a valid entry? */
|
---|
1647 | break;
|
---|
1648 | #endif
|
---|
1649 | res = dir_next(dp, 0); /* Next entry */
|
---|
1650 | if (res != FR_OK) break;
|
---|
1651 | }
|
---|
1652 |
|
---|
1653 | if (res != FR_OK) dp->sect = 0;
|
---|
1654 |
|
---|
1655 | return res;
|
---|
1656 | }
|
---|
1657 | #endif /* FF_FS_MINIMIZE <= 1 || FF_USE_LABEL || FF_FS_RPATH >= 2 */
|
---|
1658 |
|
---|
1659 |
|
---|
1660 |
|
---|
1661 |
|
---|
1662 | /*-----------------------------------------------------------------------*/
|
---|
1663 | /* Register an object to the directory */
|
---|
1664 | /*-----------------------------------------------------------------------*/
|
---|
1665 | #if !FF_FS_READONLY
|
---|
1666 | static
|
---|
1667 | FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too many SFN collision, FR_DISK_ERR:disk error */
|
---|
1668 | FATFS_DIR* dp /* Target directory with object name to be created */
|
---|
1669 | )
|
---|
1670 | {
|
---|
1671 | FRESULT res;
|
---|
1672 | #if FF_USE_LFN /* LFN configuration */
|
---|
1673 | UINT n, nent;
|
---|
1674 | BYTE sn[12], *fn, sum;
|
---|
1675 | WCHAR *lfn;
|
---|
1676 |
|
---|
1677 |
|
---|
1678 | fn = dp->fn; lfn = dp->lfn;
|
---|
1679 | mem_cpy(sn, fn, 12);
|
---|
1680 |
|
---|
1681 | if (FF_FS_RPATH && (sn[NSFLAG] & NS_DOT)) /* Cannot create dot entry */
|
---|
1682 | return FR_INVALID_NAME;
|
---|
1683 |
|
---|
1684 | if (sn[NSFLAG] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */
|
---|
1685 | fn[NSFLAG] = 0; dp->lfn = 0; /* Find only SFN */
|
---|
1686 | for (n = 1; n < 100; n++) {
|
---|
1687 | gen_numname(fn, sn, lfn, n); /* Generate a numbered name */
|
---|
1688 | res = dir_find(dp); /* Check if the name collides with existing SFN */
|
---|
1689 | if (res != FR_OK) break;
|
---|
1690 | }
|
---|
1691 | if (n == 100) return FR_DENIED; /* Abort if too many collisions */
|
---|
1692 | if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */
|
---|
1693 | fn[NSFLAG] = sn[NSFLAG]; dp->lfn = lfn;
|
---|
1694 | }
|
---|
1695 |
|
---|
1696 | if (sn[NSFLAG] & NS_LFN) { /* When LFN is to be created, allocate entries for an SFN + LFNs. */
|
---|
1697 | for (n = 0; lfn[n]; n++) ;
|
---|
1698 | nent = (n + 25) / 13;
|
---|
1699 | } else { /* Otherwise allocate an entry for an SFN */
|
---|
1700 | nent = 1;
|
---|
1701 | }
|
---|
1702 | res = dir_alloc(dp, nent); /* Allocate entries */
|
---|
1703 |
|
---|
1704 | if (res == FR_OK && --nent) { /* Set LFN entry if needed */
|
---|
1705 | res = dir_sdi(dp, dp->index - nent);
|
---|
1706 | if (res == FR_OK) {
|
---|
1707 | sum = sum_sfn(dp->fn); /* Checksum value of the SFN tied to the LFN */
|
---|
1708 | do { /* Store LFN entries in bottom first */
|
---|
1709 | res = move_window(dp->fs, dp->sect);
|
---|
1710 | if (res != FR_OK) break;
|
---|
1711 | fit_lfn(dp->lfn, dp->dir, (BYTE)nent, sum);
|
---|
1712 | dp->fs->wflag = 1;
|
---|
1713 | res = dir_next(dp, 0); /* Next entry */
|
---|
1714 | } while (res == FR_OK && --nent);
|
---|
1715 | }
|
---|
1716 | }
|
---|
1717 | #else /* Non LFN configuration */
|
---|
1718 | res = dir_alloc(dp, 1); /* Allocate an entry for SFN */
|
---|
1719 | #endif
|
---|
1720 |
|
---|
1721 | if (res == FR_OK) { /* Set SFN entry */
|
---|
1722 | res = move_window(dp->fs, dp->sect);
|
---|
1723 | if (res == FR_OK) {
|
---|
1724 | mem_set(dp->dir, 0, SZ_DIRE); /* Clean the entry */
|
---|
1725 | mem_cpy(dp->dir, dp->fn, 11); /* Put SFN */
|
---|
1726 | #if FF_USE_LFN
|
---|
1727 | dp->dir[DIR_NTres] = dp->fn[NSFLAG] & (NS_BODY | NS_EXT); /* Put NT flag */
|
---|
1728 | #endif
|
---|
1729 | dp->fs->wflag = 1;
|
---|
1730 | }
|
---|
1731 | }
|
---|
1732 |
|
---|
1733 | return res;
|
---|
1734 | }
|
---|
1735 | #endif /* !FF_FS_READONLY */
|
---|
1736 |
|
---|
1737 |
|
---|
1738 |
|
---|
1739 |
|
---|
1740 | /*-----------------------------------------------------------------------*/
|
---|
1741 | /* Remove an object from the directory */
|
---|
1742 | /*-----------------------------------------------------------------------*/
|
---|
1743 | #if !FF_FS_READONLY && !FF_FS_MINIMIZE
|
---|
1744 | static
|
---|
1745 | FRESULT dir_remove ( /* FR_OK:Succeeded, FR_DISK_ERR:A disk error */
|
---|
1746 | FATFS_DIR* dp /* Directory object pointing the entry to be removed */
|
---|
1747 | )
|
---|
1748 | {
|
---|
1749 | FRESULT res;
|
---|
1750 | #if FF_USE_LFN /* LFN configuration */
|
---|
1751 | UINT i;
|
---|
1752 |
|
---|
1753 | i = dp->index; /* SFN index */
|
---|
1754 | res = dir_sdi(dp, (dp->lfn_idx == 0xFFFF) ? i : dp->lfn_idx); /* Goto the SFN or top of the LFN entries */
|
---|
1755 | if (res == FR_OK) {
|
---|
1756 | do {
|
---|
1757 | res = move_window(dp->fs, dp->sect);
|
---|
1758 | if (res != FR_OK) break;
|
---|
1759 | mem_set(dp->dir, 0, SZ_DIRE); /* Clear and mark the entry "deleted" */
|
---|
1760 | *dp->dir = DDEM;
|
---|
1761 | dp->fs->wflag = 1;
|
---|
1762 | if (dp->index >= i) break; /* When reached SFN, all entries of the object has been deleted. */
|
---|
1763 | res = dir_next(dp, 0); /* Next entry */
|
---|
1764 | } while (res == FR_OK);
|
---|
1765 | if (res == FR_NO_FILE) res = FR_INT_ERR;
|
---|
1766 | }
|
---|
1767 |
|
---|
1768 | #else /* Non LFN configuration */
|
---|
1769 | res = dir_sdi(dp, dp->index);
|
---|
1770 | if (res == FR_OK) {
|
---|
1771 | res = move_window(dp->fs, dp->sect);
|
---|
1772 | if (res == FR_OK) {
|
---|
1773 | mem_set(dp->dir, 0, SZ_DIRE); /* Clear and mark the entry "deleted" */
|
---|
1774 | *dp->dir = DDEM;
|
---|
1775 | dp->fs->wflag = 1;
|
---|
1776 | }
|
---|
1777 | }
|
---|
1778 | #endif
|
---|
1779 |
|
---|
1780 | return res;
|
---|
1781 | }
|
---|
1782 | #endif /* !FF_FS_READONLY */
|
---|
1783 |
|
---|
1784 |
|
---|
1785 |
|
---|
1786 |
|
---|
1787 | /*-----------------------------------------------------------------------*/
|
---|
1788 | /* Get file information from directory entry */
|
---|
1789 | /*-----------------------------------------------------------------------*/
|
---|
1790 | #if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2
|
---|
1791 | static
|
---|
1792 | void get_fileinfo ( /* No return code */
|
---|
1793 | FATFS_DIR* dp, /* Pointer to the directory object */
|
---|
1794 | FILINFO* fno /* Pointer to the file information to be filled */
|
---|
1795 | )
|
---|
1796 | {
|
---|
1797 | UINT i;
|
---|
1798 | TCHAR *p, c;
|
---|
1799 | BYTE *dir;
|
---|
1800 | #if FF_USE_LFN
|
---|
1801 | WCHAR w, *lfn;
|
---|
1802 | #endif
|
---|
1803 | #if FF_CODE_PAGE == 65001
|
---|
1804 | unsigned char utf8_code[6];
|
---|
1805 | int code_size;
|
---|
1806 | #endif
|
---|
1807 |
|
---|
1808 | p = fno->fname;
|
---|
1809 | if (dp->sect) { /* Get SFN */
|
---|
1810 | dir = dp->dir;
|
---|
1811 | i = 0;
|
---|
1812 | while (i < 11) { /* Copy name body and extension */
|
---|
1813 | c = (TCHAR)dir[i++];
|
---|
1814 | if (c == ' ') continue; /* Skip padding spaces */
|
---|
1815 | if (c == RDDEM) c = (TCHAR)DDEM; /* Restore replaced DDEM character */
|
---|
1816 | if (i == 9) *p++ = '.'; /* Insert a . if extension is exist */
|
---|
1817 | #if FF_USE_LFN
|
---|
1818 | if (IsUpper(c) && (dir[DIR_NTres] & (i >= 9 ? NS_EXT : NS_BODY)))
|
---|
1819 | c += 0x20; /* To lower */
|
---|
1820 | #if FF_LFN_UNICODE
|
---|
1821 | if (IsDBCS1(c) && i != 8 && i != 11 && IsDBCS2(dir[i]))
|
---|
1822 | c = c << 8 | dir[i++];
|
---|
1823 | c = ff_convert(c, 1); /* OEM -> Unicode */
|
---|
1824 | if (!c) c = '?';
|
---|
1825 | #endif
|
---|
1826 | #endif
|
---|
1827 | *p++ = c;
|
---|
1828 | }
|
---|
1829 | fno->fattrib = dir[DIR_Attr]; /* Attribute */
|
---|
1830 | fno->fsize = LD_DWORD(dir + DIR_FileSize); /* Size */
|
---|
1831 | fno->fdate = LD_WORD(dir + DIR_WrtDate); /* Date */
|
---|
1832 | fno->ftime = LD_WORD(dir + DIR_WrtTime); /* Time */
|
---|
1833 | }
|
---|
1834 | *p = 0; /* Terminate SFN string by a \0 */
|
---|
1835 |
|
---|
1836 | #if FF_USE_LFN
|
---|
1837 | if (fno->lfname) {
|
---|
1838 | i = 0; p = fno->lfname;
|
---|
1839 | if (dp->sect && fno->lfsize && dp->lfn_idx != 0xFFFF) { /* Get LFN if available */
|
---|
1840 | lfn = dp->lfn;
|
---|
1841 | while ((w = *lfn++) != 0) { /* Get an LFN character */
|
---|
1842 | #if !FF_LFN_UNICODE
|
---|
1843 | #if FF_CODE_PAGE == 65001
|
---|
1844 | Utf16_to_Utf8(utf8_code, &code_size, w);
|
---|
1845 | for (int j = 0; j < code_size - 1; j++) {
|
---|
1846 | p[i++] = utf8_code[j];
|
---|
1847 | }
|
---|
1848 | w = utf8_code[code_size - 1];
|
---|
1849 | #else
|
---|
1850 | w = ff_convert(w, 0); /* Unicode -> OEM */
|
---|
1851 | if (!w) { i = 0; break; } /* No LFN if it could not be converted */
|
---|
1852 | if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC (always false on SBCS cfg) */
|
---|
1853 | p[i++] = (TCHAR)(w >> 8);
|
---|
1854 | #endif
|
---|
1855 | #endif
|
---|
1856 | if (i >= fno->lfsize - 1) { i = 0; break; } /* No LFN if buffer overflow */
|
---|
1857 | p[i++] = (TCHAR)w;
|
---|
1858 | }
|
---|
1859 | }
|
---|
1860 | p[i] = 0; /* Terminate LFN string by a \0 */
|
---|
1861 | }
|
---|
1862 | #endif
|
---|
1863 | }
|
---|
1864 | #endif /* FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 */
|
---|
1865 |
|
---|
1866 |
|
---|
1867 |
|
---|
1868 |
|
---|
1869 | /*-----------------------------------------------------------------------*/
|
---|
1870 | /* Pattern matching */
|
---|
1871 | /*-----------------------------------------------------------------------*/
|
---|
1872 | #if FF_USE_FIND && FF_FS_MINIMIZE <= 1
|
---|
1873 | static
|
---|
1874 | WCHAR get_achar ( /* Get a character and advances ptr 1 or 2 */
|
---|
1875 | const TCHAR** ptr /* Pointer to pointer to the SBCS/DBCS/Unicode string */
|
---|
1876 | )
|
---|
1877 | {
|
---|
1878 | WCHAR chr;
|
---|
1879 |
|
---|
1880 | #if !FF_LFN_UNICODE
|
---|
1881 | #if FF_CODE_PAGE == 65001
|
---|
1882 | int code_size;
|
---|
1883 | chr = Utf8_to_Utf16(*ptr, &code_size);
|
---|
1884 | (*ptr) += code_size;
|
---|
1885 | #else
|
---|
1886 | chr = (BYTE)*(*ptr)++; /* Get a byte */
|
---|
1887 | if (IsLower(chr)) chr -= 0x20; /* To upper ASCII char */
|
---|
1888 | if (IsDBCS1(chr) && IsDBCS2(**ptr)) /* Get DBC 2nd byte if needed */
|
---|
1889 | chr = chr << 8 | (BYTE)*(*ptr)++;
|
---|
1890 | #endif
|
---|
1891 | #ifdef _EXCVT
|
---|
1892 | if (chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper SBCS extended char */
|
---|
1893 | #endif
|
---|
1894 | #else
|
---|
1895 | chr = ff_wtoupper(*(*ptr)++); /* Get a word and to upper */
|
---|
1896 | #endif
|
---|
1897 | return chr;
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 |
|
---|
1901 | static
|
---|
1902 | int pattern_matching ( /* 0:mismatched, 1:matched */
|
---|
1903 | const TCHAR* pat, /* Matching pattern */
|
---|
1904 | const TCHAR* nam, /* String to be tested */
|
---|
1905 | int skip, /* Number of pre-skip chars (number of ?s) */
|
---|
1906 | int inf /* Infinite search (* specified) */
|
---|
1907 | )
|
---|
1908 | {
|
---|
1909 | const TCHAR *pp, *np;
|
---|
1910 | WCHAR pc, nc;
|
---|
1911 | int nm, nx;
|
---|
1912 |
|
---|
1913 |
|
---|
1914 | while (skip--) { /* Pre-skip name chars */
|
---|
1915 | if (!get_achar(&nam)) return 0; /* Branch mismatched if less name chars */
|
---|
1916 | }
|
---|
1917 | if (!*pat && inf) return 1; /* (short circuit) */
|
---|
1918 |
|
---|
1919 | do {
|
---|
1920 | pp = pat; np = nam; /* Top of pattern and name to match */
|
---|
1921 | for (;;) {
|
---|
1922 | if (*pp == '?' || *pp == '*') { /* Wildcard? */
|
---|
1923 | nm = nx = 0;
|
---|
1924 | do { /* Analyze the wildcard chars */
|
---|
1925 | if (*pp++ == '?') nm++; else nx = 1;
|
---|
1926 | } while (*pp == '?' || *pp == '*');
|
---|
1927 | if (pattern_matching(pp, np, nm, nx)) return 1; /* Test new branch (recurs upto number of wildcard blocks in the pattern) */
|
---|
1928 | nc = *np; break; /* Branch mismatched */
|
---|
1929 | }
|
---|
1930 | pc = get_achar(&pp); /* Get a pattern char */
|
---|
1931 | nc = get_achar(&np); /* Get a name char */
|
---|
1932 | if (pc != nc) break; /* Branch mismatched? */
|
---|
1933 | if (!pc) return 1; /* Branch matched? (matched at end of both strings) */
|
---|
1934 | }
|
---|
1935 | get_achar(&nam); /* nam++ */
|
---|
1936 | } while (inf && nc); /* Retry until end of name if infinite search is specified */
|
---|
1937 |
|
---|
1938 | return 0;
|
---|
1939 | }
|
---|
1940 | #endif /* FF_USE_FIND && FF_FS_MINIMIZE <= 1 */
|
---|
1941 |
|
---|
1942 |
|
---|
1943 |
|
---|
1944 |
|
---|
1945 | /*-----------------------------------------------------------------------*/
|
---|
1946 | /* Pick a top segment and create the object name in directory form */
|
---|
1947 | /*-----------------------------------------------------------------------*/
|
---|
1948 |
|
---|
1949 | static
|
---|
1950 | FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not create */
|
---|
1951 | FATFS_DIR* dp, /* Pointer to the directory object */
|
---|
1952 | const TCHAR** path /* Pointer to pointer to the segment in the path string */
|
---|
1953 | )
|
---|
1954 | {
|
---|
1955 | #if FF_USE_LFN /* LFN configuration */
|
---|
1956 | BYTE b, cf;
|
---|
1957 | WCHAR w, *lfn;
|
---|
1958 | UINT i, ni, si, di;
|
---|
1959 | const TCHAR *p;
|
---|
1960 | #if FF_CODE_PAGE == 65001
|
---|
1961 | unsigned char utf8_code[6];
|
---|
1962 | int code_size;
|
---|
1963 | #endif
|
---|
1964 | /* Create LFN in Unicode */
|
---|
1965 | for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
|
---|
1966 | lfn = dp->lfn;
|
---|
1967 | si = di = 0;
|
---|
1968 | for (;;) {
|
---|
1969 | w = p[si++]; /* Get a character */
|
---|
1970 | if (w < ' ' || w == '/' || w == '\\') break; /* Break on end of segment */
|
---|
1971 | if (di >= FF_MAX_LFN) /* Reject too long name */
|
---|
1972 | return FR_INVALID_NAME;
|
---|
1973 | #if !FF_LFN_UNICODE
|
---|
1974 | #if FF_CODE_PAGE == 65001
|
---|
1975 | w = Utf8_to_Utf16(&p[si - 1], &code_size);
|
---|
1976 | si += code_size - 1;
|
---|
1977 | #else
|
---|
1978 | w &= 0xFF;
|
---|
1979 | if (IsDBCS1(w)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
|
---|
1980 | b = (BYTE)p[si++]; /* Get 2nd byte */
|
---|
1981 | w = (w << 8) + b; /* Create a DBC */
|
---|
1982 | if (!IsDBCS2(b))
|
---|
1983 | return FR_INVALID_NAME; /* Reject invalid sequence */
|
---|
1984 | }
|
---|
1985 | w = ff_convert(w, 1); /* Convert ANSI/OEM to Unicode */
|
---|
1986 | if (!w) return FR_INVALID_NAME; /* Reject invalid code */
|
---|
1987 | #endif
|
---|
1988 | #endif
|
---|
1989 | if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) /* Reject illegal characters for LFN */
|
---|
1990 | return FR_INVALID_NAME;
|
---|
1991 | lfn[di++] = w; /* Store the Unicode character */
|
---|
1992 | }
|
---|
1993 | *path = &p[si]; /* Return pointer to the next segment */
|
---|
1994 | cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
|
---|
1995 | #if FF_FS_RPATH
|
---|
1996 | if ((di == 1 && lfn[di - 1] == '.') ||
|
---|
1997 | (di == 2 && lfn[di - 1] == '.' && lfn[di - 2] == '.')) { /* Is this segment a dot entry? */
|
---|
1998 | lfn[di] = 0;
|
---|
1999 | for (i = 0; i < 11; i++) /* Create dot name for SFN entry */
|
---|
2000 | dp->fn[i] = (i < di) ? '.' : ' ';
|
---|
2001 | dp->fn[i] = cf | NS_DOT; /* This is a dot entry */
|
---|
2002 | return FR_OK;
|
---|
2003 | }
|
---|
2004 | #endif
|
---|
2005 | while (di) { /* Snip off trailing spaces and dots if exist */
|
---|
2006 | w = lfn[di - 1];
|
---|
2007 | if (w != ' ' && w != '.') break;
|
---|
2008 | di--;
|
---|
2009 | }
|
---|
2010 | if (!di) return FR_INVALID_NAME; /* Reject nul string */
|
---|
2011 | lfn[di] = 0; /* LFN is created */
|
---|
2012 |
|
---|
2013 | /* Create SFN in directory form */
|
---|
2014 | mem_set(dp->fn, ' ', 11);
|
---|
2015 | for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */
|
---|
2016 | if (si) cf |= NS_LOSS | NS_LFN;
|
---|
2017 | while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */
|
---|
2018 |
|
---|
2019 | b = i = 0; ni = 8;
|
---|
2020 | for (;;) {
|
---|
2021 | w = lfn[si++]; /* Get an LFN character */
|
---|
2022 | if (!w) break; /* Break on end of the LFN */
|
---|
2023 | if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */
|
---|
2024 | cf |= NS_LOSS | NS_LFN; continue;
|
---|
2025 | }
|
---|
2026 |
|
---|
2027 | if (i >= ni || si == di) { /* Extension or end of SFN */
|
---|
2028 | if (ni == 11) { /* Long extension */
|
---|
2029 | cf |= NS_LOSS | NS_LFN; break;
|
---|
2030 | }
|
---|
2031 | if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */
|
---|
2032 | if (si > di) break; /* No extension */
|
---|
2033 | si = di; i = 8; ni = 11; /* Enter extension section */
|
---|
2034 | b <<= 2; continue;
|
---|
2035 | }
|
---|
2036 |
|
---|
2037 | if (w >= 0x80) { /* Non ASCII character */
|
---|
2038 | #ifdef _EXCVT
|
---|
2039 | w = ff_convert(w, 0); /* Unicode -> OEM code */
|
---|
2040 | if (w) w = ExCvt[w - 0x80]; /* Convert extended character to upper (SBCS) */
|
---|
2041 | #else
|
---|
2042 | #if FF_CODE_PAGE == 65001
|
---|
2043 | Utf16_to_Utf8(utf8_code, &code_size, ff_wtoupper(w));
|
---|
2044 | #else
|
---|
2045 | w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */
|
---|
2046 | #endif
|
---|
2047 | #endif
|
---|
2048 | cf |= NS_LFN; /* Force create LFN entry */
|
---|
2049 | }
|
---|
2050 | #if FF_CODE_PAGE == 65001
|
---|
2051 | else
|
---|
2052 | code_size = 1;
|
---|
2053 | #endif
|
---|
2054 |
|
---|
2055 | #if FF_CODE_PAGE == 65001
|
---|
2056 | if (code_size > 1) { /* Is this DBC? (always false at SBCS cfg) */
|
---|
2057 | if (i >= ni - 1) {
|
---|
2058 | cf |= NS_LOSS | NS_LFN; i = ni; continue;
|
---|
2059 | }
|
---|
2060 | for (int j = 0; j < code_size; j++) {
|
---|
2061 | dp->fn[i++] = (BYTE)utf8_code[j];
|
---|
2062 | }
|
---|
2063 | }
|
---|
2064 | else { /* SBC */
|
---|
2065 | if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal characters for SFN */
|
---|
2066 | w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */
|
---|
2067 | }
|
---|
2068 | else {
|
---|
2069 | if (IsUpper(w)) { /* ASCII large capital */
|
---|
2070 | b |= 2;
|
---|
2071 | }
|
---|
2072 | else {
|
---|
2073 | if (IsLower(w)) { /* ASCII small capital */
|
---|
2074 | b |= 1; w -= 0x20;
|
---|
2075 | }
|
---|
2076 | }
|
---|
2077 | }
|
---|
2078 | dp->fn[i++] = (BYTE)w;
|
---|
2079 | }
|
---|
2080 | #else
|
---|
2081 | if (_DF1S && w >= 0x100) { /* Is this DBC? (always false at SBCS cfg) */
|
---|
2082 | if (i >= ni - 1) {
|
---|
2083 | cf |= NS_LOSS | NS_LFN; i = ni; continue;
|
---|
2084 | }
|
---|
2085 | dp->fn[i++] = (BYTE)(w >> 8);
|
---|
2086 | } else { /* SBC */
|
---|
2087 | if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal characters for SFN */
|
---|
2088 | w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */
|
---|
2089 | } else {
|
---|
2090 | if (IsUpper(w)) { /* ASCII large capital */
|
---|
2091 | b |= 2;
|
---|
2092 | } else {
|
---|
2093 | if (IsLower(w)) { /* ASCII small capital */
|
---|
2094 | b |= 1; w -= 0x20;
|
---|
2095 | }
|
---|
2096 | }
|
---|
2097 | }
|
---|
2098 | }
|
---|
2099 | dp->fn[i++] = (BYTE)w;
|
---|
2100 | #endif
|
---|
2101 | }
|
---|
2102 |
|
---|
2103 | if (dp->fn[0] == DDEM) dp->fn[0] = RDDEM; /* If the first character collides with DDEM, replace it with RDDEM */
|
---|
2104 |
|
---|
2105 | if (ni == 8) b <<= 2;
|
---|
2106 | if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) /* Create LFN entry when there are composite capitals */
|
---|
2107 | cf |= NS_LFN;
|
---|
2108 | if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended character, NT flags are created */
|
---|
2109 | if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */
|
---|
2110 | if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */
|
---|
2111 | }
|
---|
2112 |
|
---|
2113 | dp->fn[NSFLAG] = cf; /* SFN is created */
|
---|
2114 |
|
---|
2115 | return FR_OK;
|
---|
2116 |
|
---|
2117 |
|
---|
2118 | #else /* Non-LFN configuration */
|
---|
2119 | BYTE b, c, d, *sfn;
|
---|
2120 | UINT ni, si, i;
|
---|
2121 | const char *p;
|
---|
2122 |
|
---|
2123 | /* Create file name in directory form */
|
---|
2124 | for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Skip duplicated separator */
|
---|
2125 | sfn = dp->fn;
|
---|
2126 | mem_set(sfn, ' ', 11);
|
---|
2127 | si = i = b = 0; ni = 8;
|
---|
2128 | #if FF_FS_RPATH
|
---|
2129 | if (p[si] == '.') { /* Is this a dot entry? */
|
---|
2130 | for (;;) {
|
---|
2131 | c = (BYTE)p[si++];
|
---|
2132 | if (c != '.' || si >= 3) break;
|
---|
2133 | sfn[i++] = c;
|
---|
2134 | }
|
---|
2135 | if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;
|
---|
2136 | *path = &p[si]; /* Return pointer to the next segment */
|
---|
2137 | sfn[NSFLAG] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of path */
|
---|
2138 | return FR_OK;
|
---|
2139 | }
|
---|
2140 | #endif
|
---|
2141 | for (;;) {
|
---|
2142 | c = (BYTE)p[si++];
|
---|
2143 | if (c <= ' ' || c == '/' || c == '\\') break; /* Break on end of segment */
|
---|
2144 | if (c == '.' || i >= ni) {
|
---|
2145 | if (ni != 8 || c != '.') return FR_INVALID_NAME;
|
---|
2146 | i = 8; ni = 11;
|
---|
2147 | b <<= 2; continue;
|
---|
2148 | }
|
---|
2149 | if (c >= 0x80) { /* Extended character? */
|
---|
2150 | b |= 3; /* Eliminate NT flag */
|
---|
2151 | #ifdef _EXCVT
|
---|
2152 | c = ExCvt[c - 0x80]; /* To upper extended characters (SBCS cfg) */
|
---|
2153 | #else
|
---|
2154 | #if !_DF1S
|
---|
2155 | return FR_INVALID_NAME; /* Reject extended characters (ASCII cfg) */
|
---|
2156 | #endif
|
---|
2157 | #endif
|
---|
2158 | }
|
---|
2159 | if (IsDBCS1(c)) { /* Check if it is a DBC 1st byte (always false at SBCS cfg.) */
|
---|
2160 | d = (BYTE)p[si++]; /* Get 2nd byte */
|
---|
2161 | if (!IsDBCS2(d) || i >= ni - 1) /* Reject invalid DBC */
|
---|
2162 | return FR_INVALID_NAME;
|
---|
2163 | sfn[i++] = c;
|
---|
2164 | sfn[i++] = d;
|
---|
2165 | } else { /* SBC */
|
---|
2166 | if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) /* Reject illegal chrs for SFN */
|
---|
2167 | return FR_INVALID_NAME;
|
---|
2168 | if (IsUpper(c)) { /* ASCII large capital? */
|
---|
2169 | b |= 2;
|
---|
2170 | } else {
|
---|
2171 | if (IsLower(c)) { /* ASCII small capital? */
|
---|
2172 | b |= 1; c -= 0x20;
|
---|
2173 | }
|
---|
2174 | }
|
---|
2175 | sfn[i++] = c;
|
---|
2176 | }
|
---|
2177 | }
|
---|
2178 | *path = &p[si]; /* Return pointer to the next segment */
|
---|
2179 | c = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
|
---|
2180 |
|
---|
2181 | if (!i) return FR_INVALID_NAME; /* Reject nul string */
|
---|
2182 | if (sfn[0] == DDEM) sfn[0] = RDDEM; /* When first character collides with DDEM, replace it with RDDEM */
|
---|
2183 |
|
---|
2184 | if (ni == 8) b <<= 2;
|
---|
2185 | if ((b & 0x03) == 0x01) c |= NS_EXT; /* NT flag (Name extension has only small capital) */
|
---|
2186 | if ((b & 0x0C) == 0x04) c |= NS_BODY; /* NT flag (Name body has only small capital) */
|
---|
2187 |
|
---|
2188 | sfn[NSFLAG] = c; /* Store NT flag, File name is created */
|
---|
2189 |
|
---|
2190 | return FR_OK;
|
---|
2191 | #endif
|
---|
2192 | }
|
---|
2193 |
|
---|
2194 |
|
---|
2195 |
|
---|
2196 |
|
---|
2197 | /*-----------------------------------------------------------------------*/
|
---|
2198 | /* Follow a file path */
|
---|
2199 | /*-----------------------------------------------------------------------*/
|
---|
2200 |
|
---|
2201 | static
|
---|
2202 | FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
|
---|
2203 | FATFS_DIR* dp, /* Directory object to return last directory and found object */
|
---|
2204 | const TCHAR* path /* Full-path string to find a file or directory */
|
---|
2205 | )
|
---|
2206 | {
|
---|
2207 | FRESULT res;
|
---|
2208 | BYTE *dir, ns;
|
---|
2209 |
|
---|
2210 |
|
---|
2211 | #if FF_FS_RPATH
|
---|
2212 | if (*path == '/' || *path == '\\') { /* There is a heading separator */
|
---|
2213 | path++; dp->sclust = 0; /* Strip it and start from the root directory */
|
---|
2214 | } else { /* No heading separator */
|
---|
2215 | dp->sclust = dp->fs->cdir; /* Start from the current directory */
|
---|
2216 | }
|
---|
2217 | #else
|
---|
2218 | if (*path == '/' || *path == '\\') /* Strip heading separator if exist */
|
---|
2219 | path++;
|
---|
2220 | dp->sclust = 0; /* Always start from the root directory */
|
---|
2221 | #endif
|
---|
2222 |
|
---|
2223 | if ((UINT)*path < ' ') { /* Null path name is the origin directory itself */
|
---|
2224 | res = dir_sdi(dp, 0);
|
---|
2225 | dp->dir = 0;
|
---|
2226 | } else { /* Follow path */
|
---|
2227 | for (;;) {
|
---|
2228 | res = create_name(dp, &path); /* Get a segment name of the path */
|
---|
2229 | if (res != FR_OK) break;
|
---|
2230 | res = dir_find(dp); /* Find an object with the sagment name */
|
---|
2231 | ns = dp->fn[NSFLAG];
|
---|
2232 | if (res != FR_OK) { /* Failed to find the object */
|
---|
2233 | if (res == FR_NO_FILE) { /* Object is not found */
|
---|
2234 | if (FF_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exist, */
|
---|
2235 | dp->sclust = 0; dp->dir = 0; /* it is the root directory and stay there */
|
---|
2236 | if (!(ns & NS_LAST)) continue; /* Continue to follow if not last segment */
|
---|
2237 | res = FR_OK; /* Ended at the root directroy. Function completed. */
|
---|
2238 | } else { /* Could not find the object */
|
---|
2239 | if (!(ns & NS_LAST)) res = FR_NO_PATH; /* Adjust error code if not last segment */
|
---|
2240 | }
|
---|
2241 | }
|
---|
2242 | break;
|
---|
2243 | }
|
---|
2244 | if (ns & NS_LAST) break; /* Last segment matched. Function completed. */
|
---|
2245 | dir = dp->dir; /* Follow the sub-directory */
|
---|
2246 | if (!(dir[DIR_Attr] & AM_DIR)) { /* It is not a sub-directory and cannot follow */
|
---|
2247 | res = FR_NO_PATH; break;
|
---|
2248 | }
|
---|
2249 | dp->sclust = ld_clust(dp->fs, dir);
|
---|
2250 | }
|
---|
2251 | }
|
---|
2252 |
|
---|
2253 | return res;
|
---|
2254 | }
|
---|
2255 |
|
---|
2256 |
|
---|
2257 |
|
---|
2258 |
|
---|
2259 | /*-----------------------------------------------------------------------*/
|
---|
2260 | /* Get logical drive number from path name */
|
---|
2261 | /*-----------------------------------------------------------------------*/
|
---|
2262 |
|
---|
2263 | static
|
---|
2264 | int get_ldnumber ( /* Returns logical drive number (-1:invalid drive) */
|
---|
2265 | const TCHAR** path /* Pointer to pointer to the path name */
|
---|
2266 | )
|
---|
2267 | {
|
---|
2268 | const TCHAR *tp, *tt;
|
---|
2269 | UINT i;
|
---|
2270 | int vol = -1;
|
---|
2271 | #if FF_STR_VOLUME_ID /* Find string drive id */
|
---|
2272 | static const char* const str[] = {FF_VOLUME_STRS};
|
---|
2273 | const char *sp;
|
---|
2274 | char c;
|
---|
2275 | TCHAR tc;
|
---|
2276 | #endif
|
---|
2277 |
|
---|
2278 |
|
---|
2279 | if (*path) { /* If the pointer is not a null */
|
---|
2280 | for (tt = *path; (UINT)*tt >= (FF_USE_LFN ? ' ' : '!') && *tt != ':'; tt++) ; /* Find ':' in the path */
|
---|
2281 | if (*tt == ':') { /* If a ':' is exist in the path name */
|
---|
2282 | tp = *path;
|
---|
2283 | i = *tp++ - '0';
|
---|
2284 | if (i < 10 && tp == tt) { /* Is there a numeric drive id? */
|
---|
2285 | if (i < FF_VOLUMES) { /* If a drive id is found, get the value and strip it */
|
---|
2286 | vol = (int)i;
|
---|
2287 | *path = ++tt;
|
---|
2288 | }
|
---|
2289 | }
|
---|
2290 | #if FF_STR_VOLUME_ID
|
---|
2291 | else { /* No numeric drive number, find string drive id */
|
---|
2292 | i = 0; tt++;
|
---|
2293 | do {
|
---|
2294 | sp = str[i]; tp = *path;
|
---|
2295 | do { /* Compare a string drive id with path name */
|
---|
2296 | c = *sp++; tc = *tp++;
|
---|
2297 | if (IsLower(tc)) tc -= 0x20;
|
---|
2298 | } while (c && (TCHAR)c == tc);
|
---|
2299 | } while ((c || tp != tt) && ++i < FF_VOLUMES); /* Repeat for each id until pattern match */
|
---|
2300 | if (i < FF_VOLUMES) { /* If a drive id is found, get the value and strip it */
|
---|
2301 | vol = (int)i;
|
---|
2302 | *path = tt;
|
---|
2303 | }
|
---|
2304 | }
|
---|
2305 | #endif
|
---|
2306 | return vol;
|
---|
2307 | }
|
---|
2308 | #if FF_FS_RPATH && FF_VOLUMES >= 2
|
---|
2309 | vol = CurrVol; /* Current drive */
|
---|
2310 | #else
|
---|
2311 | vol = 0; /* Drive 0 */
|
---|
2312 | #endif
|
---|
2313 | }
|
---|
2314 | return vol;
|
---|
2315 | }
|
---|
2316 |
|
---|
2317 |
|
---|
2318 |
|
---|
2319 |
|
---|
2320 | /*-----------------------------------------------------------------------*/
|
---|
2321 | /* Load a sector and check if it is an FAT boot sector */
|
---|
2322 | /*-----------------------------------------------------------------------*/
|
---|
2323 |
|
---|
2324 | static
|
---|
2325 | BYTE check_fs ( /* 0:Valid FAT-BS, 1:Valid BS but not FAT, 2:Not a BS, 3:Disk error */
|
---|
2326 | FATFS* fs, /* File system object */
|
---|
2327 | DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
|
---|
2328 | )
|
---|
2329 | {
|
---|
2330 | fs->wflag = 0; fs->winsect = 0xFFFFFFFF; /* Invaidate window */
|
---|
2331 | if (move_window(fs, sect) != FR_OK) /* Load boot record */
|
---|
2332 | return 3;
|
---|
2333 |
|
---|
2334 | if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check boot record signature (always placed at offset 510 even if the sector size is >512) */
|
---|
2335 | return 2;
|
---|
2336 |
|
---|
2337 | if ((LD_DWORD(&fs->win[BS_FilSysType]) & 0xFFFFFF) == 0x544146) /* Check "FAT" string */
|
---|
2338 | return 0;
|
---|
2339 | if ((LD_DWORD(&fs->win[BS_FilSysType32]) & 0xFFFFFF) == 0x544146) /* Check "FAT" string */
|
---|
2340 | return 0;
|
---|
2341 |
|
---|
2342 | return 1;
|
---|
2343 | }
|
---|
2344 |
|
---|
2345 |
|
---|
2346 |
|
---|
2347 |
|
---|
2348 | /*-----------------------------------------------------------------------*/
|
---|
2349 | /* Find logical drive and check if the volume is mounted */
|
---|
2350 | /*-----------------------------------------------------------------------*/
|
---|
2351 |
|
---|
2352 | static
|
---|
2353 | FRESULT find_volume ( /* FR_OK(0): successful, !=0: any error occurred */
|
---|
2354 | FATFS** rfs, /* Pointer to pointer to the found file system object */
|
---|
2355 | const TCHAR** path, /* Pointer to pointer to the path name (drive number) */
|
---|
2356 | BYTE wmode /* !=0: Check write protection for write access */
|
---|
2357 | )
|
---|
2358 | {
|
---|
2359 | BYTE fmt, *pt;
|
---|
2360 | int vol;
|
---|
2361 | DSTATUS stat;
|
---|
2362 | DWORD bsect, fasize, tsect, sysect, nclst, szbfat, br[4];
|
---|
2363 | WORD nrsv;
|
---|
2364 | FATFS *fs;
|
---|
2365 | UINT i;
|
---|
2366 |
|
---|
2367 |
|
---|
2368 | /* Get logical drive number from the path name */
|
---|
2369 | *rfs = 0;
|
---|
2370 | vol = get_ldnumber(path);
|
---|
2371 | if (vol < 0) return FR_INVALID_DRIVE;
|
---|
2372 |
|
---|
2373 | /* Check if the file system object is valid or not */
|
---|
2374 | fs = FatFs[vol]; /* Get pointer to the file system object */
|
---|
2375 | if (!fs) return FR_NOT_ENABLED; /* Is the file system object available? */
|
---|
2376 |
|
---|
2377 | ENTER_FF(fs); /* Lock the volume */
|
---|
2378 | *rfs = fs; /* Return pointer to the file system object */
|
---|
2379 |
|
---|
2380 | if (fs->fs_type) { /* If the volume has been mounted */
|
---|
2381 | stat = disk_status(fs->drv);
|
---|
2382 | if (!(stat & STA_NOINIT)) { /* and the physical drive is kept initialized */
|
---|
2383 | if (!FF_FS_READONLY && wmode && (stat & STA_PROTECT)) /* Check write protection if needed */
|
---|
2384 | return FR_WRITE_PROTECTED;
|
---|
2385 | return FR_OK; /* The file system object is valid */
|
---|
2386 | }
|
---|
2387 | }
|
---|
2388 |
|
---|
2389 | /* The file system object is not valid. */
|
---|
2390 | /* Following code attempts to mount the volume. (analyze BPB and initialize the fs object) */
|
---|
2391 |
|
---|
2392 | fs->fs_type = 0; /* Clear the file system object */
|
---|
2393 | fs->drv = LD2PD(vol); /* Bind the logical drive and a physical drive */
|
---|
2394 | stat = disk_initialize(fs->drv); /* Initialize the physical drive */
|
---|
2395 | if (stat & STA_NOINIT) /* Check if the initialization succeeded */
|
---|
2396 | return FR_NOT_READY; /* Failed to initialize due to no medium or hard error */
|
---|
2397 | if (!FF_FS_READONLY && wmode && (stat & STA_PROTECT)) /* Check disk write protection if needed */
|
---|
2398 | return FR_WRITE_PROTECTED;
|
---|
2399 | #if FF_MAX_SS != FF_MIN_SS /* Get sector size (multiple sector size cfg only) */
|
---|
2400 | if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
|
---|
2401 | || SS(fs) < FF_MIN_SS || SS(fs) > FF_MAX_SS) return FR_DISK_ERR;
|
---|
2402 | #endif
|
---|
2403 | /* Find an FAT partition on the drive. Supports only generic partitioning, FDISK and SFD. */
|
---|
2404 | bsect = 0;
|
---|
2405 | fmt = check_fs(fs, bsect); /* Load sector 0 and check if it is an FAT boot sector as SFD */
|
---|
2406 | if (fmt == 1 || (!fmt && (LD2PT(vol)))) { /* Not an FAT boot sector or forced partition number */
|
---|
2407 | for (i = 0; i < 4; i++) { /* Get partition offset */
|
---|
2408 | pt = fs->win + MBR_Table + i * SZ_PTE;
|
---|
2409 | br[i] = pt[4] ? LD_DWORD(&pt[8]) : 0;
|
---|
2410 | }
|
---|
2411 | i = LD2PT(vol); /* Partition number: 0:auto, 1-4:forced */
|
---|
2412 | if (i) i--;
|
---|
2413 | do { /* Find an FAT volume */
|
---|
2414 | bsect = br[i];
|
---|
2415 | fmt = bsect ? check_fs(fs, bsect) : 2; /* Check the partition */
|
---|
2416 | } while (!LD2PT(vol) && fmt && ++i < 4);
|
---|
2417 | }
|
---|
2418 | if (fmt == 3) return FR_DISK_ERR; /* An error occured in the disk I/O layer */
|
---|
2419 | if (fmt) return FR_NO_FILESYSTEM; /* No FAT volume is found */
|
---|
2420 |
|
---|
2421 | /* An FAT volume is found. Following code initializes the file system object */
|
---|
2422 |
|
---|
2423 | if (LD_WORD(fs->win + BPB_BytsPerSec) != SS(fs)) /* (BPB_BytsPerSec must be equal to the physical sector size) */
|
---|
2424 | return FR_NO_FILESYSTEM;
|
---|
2425 |
|
---|
2426 | fasize = LD_WORD(fs->win + BPB_FATSz16); /* Number of sectors per FAT */
|
---|
2427 | if (!fasize) fasize = LD_DWORD(fs->win + BPB_FATSz32);
|
---|
2428 | fs->fsize = fasize;
|
---|
2429 |
|
---|
2430 | fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FAT copies */
|
---|
2431 | if (fs->n_fats != 1 && fs->n_fats != 2) /* (Must be 1 or 2) */
|
---|
2432 | return FR_NO_FILESYSTEM;
|
---|
2433 | fasize *= fs->n_fats; /* Number of sectors for FAT area */
|
---|
2434 |
|
---|
2435 | fs->csize = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
|
---|
2436 | if (!fs->csize || (fs->csize & (fs->csize - 1))) /* (Must be power of 2) */
|
---|
2437 | return FR_NO_FILESYSTEM;
|
---|
2438 |
|
---|
2439 | fs->n_rootdir = LD_WORD(fs->win + BPB_RootEntCnt); /* Number of root directory entries */
|
---|
2440 | if (fs->n_rootdir % (SS(fs) / SZ_DIRE)) /* (Must be sector aligned) */
|
---|
2441 | return FR_NO_FILESYSTEM;
|
---|
2442 |
|
---|
2443 | tsect = LD_WORD(fs->win + BPB_TotSec16); /* Number of sectors on the volume */
|
---|
2444 | if (!tsect) tsect = LD_DWORD(fs->win + BPB_TotSec32);
|
---|
2445 |
|
---|
2446 | nrsv = LD_WORD(fs->win + BPB_RsvdSecCnt); /* Number of reserved sectors */
|
---|
2447 | if (!nrsv) return FR_NO_FILESYSTEM; /* (Must not be 0) */
|
---|
2448 |
|
---|
2449 | /* Determine the FAT sub type */
|
---|
2450 | sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZ_DIRE); /* RSV + FAT + FATFS_DIR */
|
---|
2451 | if (tsect < sysect) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
|
---|
2452 | nclst = (tsect - sysect) / fs->csize; /* Number of clusters */
|
---|
2453 | if (!nclst) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
|
---|
2454 | fmt = FS_FAT12;
|
---|
2455 | if (nclst >= MIN_FAT16) fmt = FS_FAT16;
|
---|
2456 | if (nclst >= MIN_FAT32) fmt = FS_FAT32;
|
---|
2457 |
|
---|
2458 | /* Boundaries and Limits */
|
---|
2459 | fs->n_fatent = nclst + 2; /* Number of FAT entries */
|
---|
2460 | fs->volbase = bsect; /* Volume start sector */
|
---|
2461 | fs->fatbase = bsect + nrsv; /* FAT start sector */
|
---|
2462 | fs->database = bsect + sysect; /* Data start sector */
|
---|
2463 | if (fmt == FS_FAT32) {
|
---|
2464 | if (fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */
|
---|
2465 | fs->dirbase = LD_DWORD(fs->win + BPB_RootClus); /* Root directory start cluster */
|
---|
2466 | szbfat = fs->n_fatent * 4; /* (Needed FAT size) */
|
---|
2467 | } else {
|
---|
2468 | if (!fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must not be 0) */
|
---|
2469 | fs->dirbase = fs->fatbase + fasize; /* Root directory start sector */
|
---|
2470 | szbfat = (fmt == FS_FAT16) ? /* (Needed FAT size) */
|
---|
2471 | fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
|
---|
2472 | }
|
---|
2473 | if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs)) /* (BPB_FATSz must not be less than the size needed) */
|
---|
2474 | return FR_NO_FILESYSTEM;
|
---|
2475 |
|
---|
2476 | #if !FF_FS_READONLY
|
---|
2477 | /* Initialize cluster allocation information */
|
---|
2478 | fs->last_clust = fs->free_clust = 0xFFFFFFFF;
|
---|
2479 |
|
---|
2480 | /* Get fsinfo if available */
|
---|
2481 | fs->fsi_flag = 0x80;
|
---|
2482 | #if (FF_FS_NOFSINFO & 3) != 3
|
---|
2483 | if (fmt == FS_FAT32 /* Enable FSINFO only if FAT32 and BPB_FSInfo == 1 */
|
---|
2484 | && LD_WORD(fs->win + BPB_FSInfo) == 1
|
---|
2485 | && move_window(fs, bsect + 1) == FR_OK)
|
---|
2486 | {
|
---|
2487 | fs->fsi_flag = 0;
|
---|
2488 | if (LD_WORD(fs->win + BS_55AA) == 0xAA55 /* Load FSINFO data if available */
|
---|
2489 | && LD_DWORD(fs->win + FSI_LeadSig) == 0x41615252
|
---|
2490 | && LD_DWORD(fs->win + FSI_StrucSig) == 0x61417272)
|
---|
2491 | {
|
---|
2492 | #if (FF_FS_NOFSINFO & 1) == 0
|
---|
2493 | fs->free_clust = LD_DWORD(fs->win + FSI_Free_Count);
|
---|
2494 | #endif
|
---|
2495 | #if (FF_FS_NOFSINFO & 2) == 0
|
---|
2496 | fs->last_clust = LD_DWORD(fs->win + FSI_Nxt_Free);
|
---|
2497 | #endif
|
---|
2498 | }
|
---|
2499 | }
|
---|
2500 | #endif
|
---|
2501 | #endif
|
---|
2502 | fs->fs_type = fmt; /* FAT sub-type */
|
---|
2503 | fs->id = ++Fsid; /* File system mount ID */
|
---|
2504 | #if FF_FS_RPATH
|
---|
2505 | fs->cdir = 0; /* Set current directory to root */
|
---|
2506 | #endif
|
---|
2507 | #if FF_FS_LOCK != 0 /* Clear file lock semaphores */
|
---|
2508 | clear_lock(fs);
|
---|
2509 | #endif
|
---|
2510 |
|
---|
2511 | return FR_OK;
|
---|
2512 | }
|
---|
2513 |
|
---|
2514 |
|
---|
2515 |
|
---|
2516 |
|
---|
2517 | /*-----------------------------------------------------------------------*/
|
---|
2518 | /* Check if the file/directory object is valid or not */
|
---|
2519 | /*-----------------------------------------------------------------------*/
|
---|
2520 |
|
---|
2521 | static
|
---|
2522 | FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */
|
---|
2523 | void* obj /* Pointer to the object FIL/FATFS_DIR to check validity */
|
---|
2524 | )
|
---|
2525 | {
|
---|
2526 | FIL *fil = (FIL*)obj; /* Assuming offset of .fs and .id in the FIL/FATFS_DIR structure is identical */
|
---|
2527 |
|
---|
2528 |
|
---|
2529 | if (!fil || !fil->fs || !fil->fs->fs_type || fil->fs->id != fil->id || (disk_status(fil->fs->drv) & STA_NOINIT))
|
---|
2530 | return FR_INVALID_OBJECT;
|
---|
2531 |
|
---|
2532 | ENTER_FF(fil->fs); /* Lock file system */
|
---|
2533 |
|
---|
2534 | return FR_OK;
|
---|
2535 | }
|
---|
2536 |
|
---|
2537 |
|
---|
2538 |
|
---|
2539 |
|
---|
2540 | /*--------------------------------------------------------------------------
|
---|
2541 |
|
---|
2542 | Public Functions
|
---|
2543 |
|
---|
2544 | ---------------------------------------------------------------------------*/
|
---|
2545 |
|
---|
2546 |
|
---|
2547 |
|
---|
2548 | /*-----------------------------------------------------------------------*/
|
---|
2549 | /* Mount/Unmount a Logical Drive */
|
---|
2550 | /*-----------------------------------------------------------------------*/
|
---|
2551 |
|
---|
2552 | FRESULT f_mount (
|
---|
2553 | FATFS* fs, /* Pointer to the file system object (NULL:unmount)*/
|
---|
2554 | const TCHAR* path, /* Logical drive number to be mounted/unmounted */
|
---|
2555 | BYTE opt /* 0:Do not mount (delayed mount), 1:Mount immediately */
|
---|
2556 | )
|
---|
2557 | {
|
---|
2558 | FATFS *cfs;
|
---|
2559 | int vol;
|
---|
2560 | FRESULT res;
|
---|
2561 | const TCHAR *rp = path;
|
---|
2562 |
|
---|
2563 |
|
---|
2564 | vol = get_ldnumber(&rp);
|
---|
2565 | if (vol < 0) return FR_INVALID_DRIVE;
|
---|
2566 | cfs = FatFs[vol]; /* Pointer to fs object */
|
---|
2567 |
|
---|
2568 | if (cfs) {
|
---|
2569 | #if FF_FS_LOCK != 0
|
---|
2570 | clear_lock(cfs);
|
---|
2571 | #endif
|
---|
2572 | #if FF_FS_REENTRANT /* Discard sync object of the current volume */
|
---|
2573 | if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR;
|
---|
2574 | #endif
|
---|
2575 | cfs->fs_type = 0; /* Clear old fs object */
|
---|
2576 | }
|
---|
2577 |
|
---|
2578 | if (fs) {
|
---|
2579 | fs->fs_type = 0; /* Clear new fs object */
|
---|
2580 | #if FF_FS_REENTRANT /* Create sync object for the new volume */
|
---|
2581 | if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR;
|
---|
2582 | #endif
|
---|
2583 | }
|
---|
2584 | FatFs[vol] = fs; /* Register new fs object */
|
---|
2585 |
|
---|
2586 | if (!fs || opt != 1) return FR_OK; /* Do not mount now, it will be mounted later */
|
---|
2587 |
|
---|
2588 | res = find_volume(&fs, &path, 0); /* Force mounted the volume */
|
---|
2589 | LEAVE_FF(fs, res);
|
---|
2590 | }
|
---|
2591 |
|
---|
2592 |
|
---|
2593 |
|
---|
2594 |
|
---|
2595 | /*-----------------------------------------------------------------------*/
|
---|
2596 | /* Open or Create a File */
|
---|
2597 | /*-----------------------------------------------------------------------*/
|
---|
2598 |
|
---|
2599 | FRESULT f_open (
|
---|
2600 | FIL* fp, /* Pointer to the blank file object */
|
---|
2601 | const TCHAR* path, /* Pointer to the file name */
|
---|
2602 | BYTE mode /* Access mode and file open mode flags */
|
---|
2603 | )
|
---|
2604 | {
|
---|
2605 | FRESULT res;
|
---|
2606 | FATFS_DIR dj;
|
---|
2607 | BYTE *dir;
|
---|
2608 | DEFINE_NAMEBUF;
|
---|
2609 | #if !FF_FS_READONLY
|
---|
2610 | DWORD dw, cl;
|
---|
2611 | #endif
|
---|
2612 |
|
---|
2613 |
|
---|
2614 | if (!fp) return FR_INVALID_OBJECT;
|
---|
2615 | fp->fs = 0; /* Clear file object */
|
---|
2616 |
|
---|
2617 | /* Get logical drive number */
|
---|
2618 | #if !FF_FS_READONLY
|
---|
2619 | mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW | FA_OPEN_APPEND;
|
---|
2620 | res = find_volume(&dj.fs, &path, (BYTE)(mode & ~FA_READ));
|
---|
2621 | #else
|
---|
2622 | mode &= FA_READ;
|
---|
2623 | res = find_volume(&dj.fs, &path, 0);
|
---|
2624 | #endif
|
---|
2625 | if (res == FR_OK) {
|
---|
2626 | INIT_BUF(dj);
|
---|
2627 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
2628 | dir = dj.dir;
|
---|
2629 | #if !FF_FS_READONLY /* R/W configuration */
|
---|
2630 | if (res == FR_OK) {
|
---|
2631 | if (!dir) /* Default directory itself */
|
---|
2632 | res = FR_INVALID_NAME;
|
---|
2633 | #if FF_FS_LOCK != 0
|
---|
2634 | else
|
---|
2635 | res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
|
---|
2636 | #endif
|
---|
2637 | }
|
---|
2638 | /* Create or Open a file */
|
---|
2639 | if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
|
---|
2640 | if (res != FR_OK) { /* No file, create new */
|
---|
2641 | if (res == FR_NO_FILE) /* There is no file to open, create a new entry */
|
---|
2642 | #if FF_FS_LOCK != 0
|
---|
2643 | res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
|
---|
2644 | #else
|
---|
2645 | res = dir_register(&dj);
|
---|
2646 | #endif
|
---|
2647 | mode |= FA_CREATE_ALWAYS; /* File is created */
|
---|
2648 | dir = dj.dir; /* New entry */
|
---|
2649 | }
|
---|
2650 | else { /* Any object is already existing */
|
---|
2651 | if (dir[DIR_Attr] & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or FATFS_DIR) */
|
---|
2652 | res = FR_DENIED;
|
---|
2653 | } else {
|
---|
2654 | if (mode & FA_CREATE_NEW) /* Cannot create as new file */
|
---|
2655 | res = FR_EXIST;
|
---|
2656 | }
|
---|
2657 | }
|
---|
2658 | if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate it if overwrite mode */
|
---|
2659 | dw = GET_FATTIME();
|
---|
2660 | ST_DWORD(dir + DIR_CrtTime, dw);/* Set created time */
|
---|
2661 | ST_DWORD(dir + DIR_WrtTime, dw);/* Set modified time */
|
---|
2662 | dir[DIR_Attr] = 0; /* Reset attribute */
|
---|
2663 | ST_DWORD(dir + DIR_FileSize, 0);/* Reset file size */
|
---|
2664 | cl = ld_clust(dj.fs, dir); /* Get cluster chain */
|
---|
2665 | st_clust(dir, 0); /* Reset cluster */
|
---|
2666 | dj.fs->wflag = 1;
|
---|
2667 | if (cl) { /* Remove the cluster chain if exist */
|
---|
2668 | dw = dj.fs->winsect;
|
---|
2669 | res = remove_chain(dj.fs, cl);
|
---|
2670 | if (res == FR_OK) {
|
---|
2671 | dj.fs->last_clust = cl - 1; /* Reuse the cluster hole */
|
---|
2672 | res = move_window(dj.fs, dw);
|
---|
2673 | }
|
---|
2674 | }
|
---|
2675 | }
|
---|
2676 | }
|
---|
2677 | else { /* Open an existing file */
|
---|
2678 | if (res == FR_OK) { /* Following succeeded */
|
---|
2679 | if (dir[DIR_Attr] & AM_DIR) { /* It is a directory */
|
---|
2680 | res = FR_NO_FILE;
|
---|
2681 | } else {
|
---|
2682 | if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
|
---|
2683 | res = FR_DENIED;
|
---|
2684 | }
|
---|
2685 | }
|
---|
2686 | }
|
---|
2687 | if (res == FR_OK) {
|
---|
2688 | if (mode & FA_CREATE_ALWAYS) /* Set file change flag if created or overwritten */
|
---|
2689 | mode |= FA_MODIFIED;
|
---|
2690 | fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
|
---|
2691 | fp->dir_ptr = dir;
|
---|
2692 | #if FF_FS_LOCK != 0
|
---|
2693 | fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
|
---|
2694 | if (!fp->lockid) res = FR_INT_ERR;
|
---|
2695 | #endif
|
---|
2696 | }
|
---|
2697 |
|
---|
2698 | #else /* R/O configuration */
|
---|
2699 | if (res == FR_OK) { /* Follow succeeded */
|
---|
2700 | dir = dj.dir;
|
---|
2701 | if (!dir) { /* Current directory itself */
|
---|
2702 | res = FR_INVALID_NAME;
|
---|
2703 | } else {
|
---|
2704 | if (dir[DIR_Attr] & AM_DIR) /* It is a directory */
|
---|
2705 | res = FR_NO_FILE;
|
---|
2706 | }
|
---|
2707 | }
|
---|
2708 | #endif
|
---|
2709 | FREE_BUF();
|
---|
2710 |
|
---|
2711 | if (res == FR_OK) {
|
---|
2712 | fp->flag = mode; /* File access mode */
|
---|
2713 | fp->err = 0; /* Clear error flag */
|
---|
2714 | fp->sclust = ld_clust(dj.fs, dir); /* File start cluster */
|
---|
2715 | fp->fsize = LD_DWORD(dir + DIR_FileSize); /* File size */
|
---|
2716 | fp->fptr = 0; /* File pointer */
|
---|
2717 | fp->dsect = 0;
|
---|
2718 | #if FF_USE_FASTSEEK
|
---|
2719 | fp->cltbl = 0; /* Normal seek mode */
|
---|
2720 | #endif
|
---|
2721 | fp->fs = dj.fs; /* Validate file object */
|
---|
2722 | fp->id = fp->fs->id;
|
---|
2723 | }
|
---|
2724 | }
|
---|
2725 |
|
---|
2726 | LEAVE_FF(dj.fs, res);
|
---|
2727 | }
|
---|
2728 |
|
---|
2729 |
|
---|
2730 |
|
---|
2731 |
|
---|
2732 | /*-----------------------------------------------------------------------*/
|
---|
2733 | /* Read File */
|
---|
2734 | /*-----------------------------------------------------------------------*/
|
---|
2735 |
|
---|
2736 | FRESULT f_read (
|
---|
2737 | FIL* fp, /* Pointer to the file object */
|
---|
2738 | void* buff, /* Pointer to data buffer */
|
---|
2739 | UINT btr, /* Number of bytes to read */
|
---|
2740 | UINT* br /* Pointer to number of bytes read */
|
---|
2741 | )
|
---|
2742 | {
|
---|
2743 | FRESULT res;
|
---|
2744 | DWORD clst, sect, remain;
|
---|
2745 | UINT rcnt, cc;
|
---|
2746 | BYTE csect, *rbuff = (BYTE*)buff;
|
---|
2747 |
|
---|
2748 |
|
---|
2749 | *br = 0; /* Clear read byte counter */
|
---|
2750 |
|
---|
2751 | res = validate(fp); /* Check validity */
|
---|
2752 | if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
---|
2753 | if (fp->err) /* Check error */
|
---|
2754 | LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
---|
2755 | if (!(fp->flag & FA_READ)) /* Check access mode */
|
---|
2756 | LEAVE_FF(fp->fs, FR_DENIED);
|
---|
2757 | remain = fp->fsize - fp->fptr;
|
---|
2758 | if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
|
---|
2759 |
|
---|
2760 | for ( ; btr; /* Repeat until all data read */
|
---|
2761 | rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
|
---|
2762 | if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
|
---|
2763 | csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
|
---|
2764 | if (!csect) { /* On the cluster boundary? */
|
---|
2765 | if (fp->fptr == 0) { /* On the top of the file? */
|
---|
2766 | clst = fp->sclust; /* Follow from the origin */
|
---|
2767 | } else { /* Middle or end of the file */
|
---|
2768 | #if FF_USE_FASTSEEK
|
---|
2769 | if (fp->cltbl)
|
---|
2770 | clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
|
---|
2771 | else
|
---|
2772 | #endif
|
---|
2773 | clst = get_fat(fp->fs, fp->clust); /* Follow cluster chain on the FAT */
|
---|
2774 | }
|
---|
2775 | if (clst < 2) ABORT(fp->fs, FR_INT_ERR);
|
---|
2776 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
2777 | fp->clust = clst; /* Update current cluster */
|
---|
2778 | }
|
---|
2779 | sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
|
---|
2780 | if (!sect) ABORT(fp->fs, FR_INT_ERR);
|
---|
2781 | sect += csect;
|
---|
2782 | cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */
|
---|
2783 | if (cc) { /* Read maximum contiguous sectors directly */
|
---|
2784 | if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
|
---|
2785 | cc = fp->fs->csize - csect;
|
---|
2786 | if (disk_read(fp->fs->drv, rbuff, sect, cc) != RES_OK)
|
---|
2787 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
2788 | #if !FF_FS_READONLY && FF_FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */
|
---|
2789 | #if FF_FS_TINY
|
---|
2790 | if (fp->fs->wflag && fp->fs->winsect - sect < cc)
|
---|
2791 | mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));
|
---|
2792 | #else
|
---|
2793 | if ((fp->flag & FA_DIRTY) && fp->dsect - sect < cc)
|
---|
2794 | mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));
|
---|
2795 | #endif
|
---|
2796 | #endif
|
---|
2797 | rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
|
---|
2798 | continue;
|
---|
2799 | }
|
---|
2800 | #if !FF_FS_TINY
|
---|
2801 | if (fp->dsect != sect) { /* Load data sector if not in cache */
|
---|
2802 | #if !FF_FS_READONLY
|
---|
2803 | if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */
|
---|
2804 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
2805 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
2806 | fp->flag &= ~FA_DIRTY;
|
---|
2807 | }
|
---|
2808 | #endif
|
---|
2809 | if (disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK) /* Fill sector cache */
|
---|
2810 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
2811 | }
|
---|
2812 | #endif
|
---|
2813 | fp->dsect = sect;
|
---|
2814 | }
|
---|
2815 | rcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs)); /* Get partial sector data from sector buffer */
|
---|
2816 | if (rcnt > btr) rcnt = btr;
|
---|
2817 | #if FF_FS_TINY
|
---|
2818 | if (move_window(fp->fs, fp->dsect) != FR_OK) /* Move sector window */
|
---|
2819 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
2820 | mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
|
---|
2821 | #else
|
---|
2822 | mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
|
---|
2823 | #endif
|
---|
2824 | }
|
---|
2825 |
|
---|
2826 | LEAVE_FF(fp->fs, FR_OK);
|
---|
2827 | }
|
---|
2828 |
|
---|
2829 |
|
---|
2830 |
|
---|
2831 |
|
---|
2832 | #if !FF_FS_READONLY
|
---|
2833 | /*-----------------------------------------------------------------------*/
|
---|
2834 | /* Write File */
|
---|
2835 | /*-----------------------------------------------------------------------*/
|
---|
2836 |
|
---|
2837 | FRESULT f_write (
|
---|
2838 | FIL* fp, /* Pointer to the file object */
|
---|
2839 | const void *buff, /* Pointer to the data to be written */
|
---|
2840 | UINT btw, /* Number of bytes to write */
|
---|
2841 | UINT* bw /* Pointer to number of bytes written */
|
---|
2842 | )
|
---|
2843 | {
|
---|
2844 | FRESULT res;
|
---|
2845 | DWORD clst, sect;
|
---|
2846 | UINT wcnt, cc;
|
---|
2847 | const BYTE *wbuff = (const BYTE*)buff;
|
---|
2848 | BYTE csect;
|
---|
2849 |
|
---|
2850 |
|
---|
2851 | *bw = 0; /* Clear write byte counter */
|
---|
2852 |
|
---|
2853 | res = validate(fp); /* Check validity */
|
---|
2854 | if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
---|
2855 | if (fp->err) /* Check error */
|
---|
2856 | LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
---|
2857 | if (!(fp->flag & FA_WRITE)) /* Check access mode */
|
---|
2858 | LEAVE_FF(fp->fs, FR_DENIED);
|
---|
2859 | if (fp->fptr + btw < fp->fptr) btw = 0; /* File size cannot reach 4GB */
|
---|
2860 |
|
---|
2861 | for ( ; btw; /* Repeat until all data written */
|
---|
2862 | wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
|
---|
2863 | if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
|
---|
2864 | csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
|
---|
2865 | if (!csect) { /* On the cluster boundary? */
|
---|
2866 | if (fp->fptr == 0) { /* On the top of the file? */
|
---|
2867 | clst = fp->sclust; /* Follow from the origin */
|
---|
2868 | if (clst == 0) /* When no cluster is allocated, */
|
---|
2869 | clst = create_chain(fp->fs, 0); /* Create a new cluster chain */
|
---|
2870 | } else { /* Middle or end of the file */
|
---|
2871 | #if FF_USE_FASTSEEK
|
---|
2872 | if (fp->cltbl)
|
---|
2873 | clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
|
---|
2874 | else
|
---|
2875 | #endif
|
---|
2876 | clst = create_chain(fp->fs, fp->clust); /* Follow or stretch cluster chain on the FAT */
|
---|
2877 | }
|
---|
2878 | if (clst == 0) break; /* Could not allocate a new cluster (disk full) */
|
---|
2879 | if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
|
---|
2880 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
2881 | fp->clust = clst; /* Update current cluster */
|
---|
2882 | if (fp->sclust == 0) fp->sclust = clst; /* Set start cluster if the first write */
|
---|
2883 | }
|
---|
2884 | #if FF_FS_TINY
|
---|
2885 | if (fp->fs->winsect == fp->dsect && sync_window(fp->fs)) /* Write-back sector cache */
|
---|
2886 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
2887 | #else
|
---|
2888 | if (fp->flag & FA_DIRTY) { /* Write-back sector cache */
|
---|
2889 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
2890 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
2891 | fp->flag &= ~FA_DIRTY;
|
---|
2892 | }
|
---|
2893 | #endif
|
---|
2894 | sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
|
---|
2895 | if (!sect) ABORT(fp->fs, FR_INT_ERR);
|
---|
2896 | sect += csect;
|
---|
2897 | cc = btw / SS(fp->fs); /* When remaining bytes >= sector size, */
|
---|
2898 | if (cc) { /* Write maximum contiguous sectors directly */
|
---|
2899 | if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
|
---|
2900 | cc = fp->fs->csize - csect;
|
---|
2901 | if (disk_write(fp->fs->drv, wbuff, sect, cc) != RES_OK)
|
---|
2902 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
2903 | #if FF_FS_MINIMIZE <= 2
|
---|
2904 | #if FF_FS_TINY
|
---|
2905 | if (fp->fs->winsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
|
---|
2906 | mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
|
---|
2907 | fp->fs->wflag = 0;
|
---|
2908 | }
|
---|
2909 | #else
|
---|
2910 | if (fp->dsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
|
---|
2911 | mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
|
---|
2912 | fp->flag &= ~FA_DIRTY;
|
---|
2913 | }
|
---|
2914 | #endif
|
---|
2915 | #endif
|
---|
2916 | wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
|
---|
2917 | continue;
|
---|
2918 | }
|
---|
2919 | #if FF_FS_TINY
|
---|
2920 | if (fp->fptr >= fp->fsize) { /* Avoid silly cache filling at growing edge */
|
---|
2921 | if (sync_window(fp->fs)) ABORT(fp->fs, FR_DISK_ERR);
|
---|
2922 | fp->fs->winsect = sect;
|
---|
2923 | }
|
---|
2924 | #else
|
---|
2925 | if (fp->dsect != sect) { /* Fill sector cache with file data */
|
---|
2926 | if (fp->fptr < fp->fsize &&
|
---|
2927 | disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK)
|
---|
2928 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
2929 | }
|
---|
2930 | #endif
|
---|
2931 | fp->dsect = sect;
|
---|
2932 | }
|
---|
2933 | wcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs));/* Put partial sector into file I/O buffer */
|
---|
2934 | if (wcnt > btw) wcnt = btw;
|
---|
2935 | #if FF_FS_TINY
|
---|
2936 | if (move_window(fp->fs, fp->dsect) != FR_OK) /* Move sector window */
|
---|
2937 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
2938 | mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
|
---|
2939 | fp->fs->wflag = 1;
|
---|
2940 | #else
|
---|
2941 | mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
|
---|
2942 | fp->flag |= FA_DIRTY;
|
---|
2943 | #endif
|
---|
2944 | }
|
---|
2945 |
|
---|
2946 | if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
|
---|
2947 | fp->flag |= FA_MODIFIED; /* Set file change flag */
|
---|
2948 |
|
---|
2949 | LEAVE_FF(fp->fs, FR_OK);
|
---|
2950 | }
|
---|
2951 |
|
---|
2952 |
|
---|
2953 |
|
---|
2954 |
|
---|
2955 | /*-----------------------------------------------------------------------*/
|
---|
2956 | /* Synchronize the File */
|
---|
2957 | /*-----------------------------------------------------------------------*/
|
---|
2958 |
|
---|
2959 | FRESULT f_sync (
|
---|
2960 | FIL* fp /* Pointer to the file object */
|
---|
2961 | )
|
---|
2962 | {
|
---|
2963 | FRESULT res;
|
---|
2964 | DWORD tm;
|
---|
2965 | BYTE *dir;
|
---|
2966 |
|
---|
2967 |
|
---|
2968 | res = validate(fp); /* Check validity of the object */
|
---|
2969 | if (res == FR_OK) {
|
---|
2970 | if (fp->flag & FA_MODIFIED) { /* Is there any change to the file? */
|
---|
2971 | #if !FF_FS_TINY
|
---|
2972 | if (fp->flag & FA_DIRTY) { /* Write-back cached data if needed */
|
---|
2973 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
2974 | LEAVE_FF(fp->fs, FR_DISK_ERR);
|
---|
2975 | fp->flag &= ~FA_DIRTY;
|
---|
2976 | }
|
---|
2977 | #endif
|
---|
2978 | /* Update the directory entry */
|
---|
2979 | res = move_window(fp->fs, fp->dir_sect);
|
---|
2980 | if (res == FR_OK) {
|
---|
2981 | dir = fp->dir_ptr;
|
---|
2982 | dir[DIR_Attr] |= AM_ARC; /* Set archive bit */
|
---|
2983 | ST_DWORD(dir + DIR_FileSize, fp->fsize); /* Update file size */
|
---|
2984 | st_clust(dir, fp->sclust); /* Update start cluster */
|
---|
2985 | tm = GET_FATTIME(); /* Update modified time */
|
---|
2986 | ST_DWORD(dir + DIR_WrtTime, tm);
|
---|
2987 | ST_WORD(dir + DIR_LstAccDate, 0);
|
---|
2988 | fp->flag &= ~FA_MODIFIED;
|
---|
2989 | fp->fs->wflag = 1;
|
---|
2990 | res = sync_fs(fp->fs);
|
---|
2991 | }
|
---|
2992 | }
|
---|
2993 | }
|
---|
2994 |
|
---|
2995 | LEAVE_FF(fp->fs, res);
|
---|
2996 | }
|
---|
2997 |
|
---|
2998 | #endif /* !FF_FS_READONLY */
|
---|
2999 |
|
---|
3000 |
|
---|
3001 |
|
---|
3002 |
|
---|
3003 | /*-----------------------------------------------------------------------*/
|
---|
3004 | /* Close File */
|
---|
3005 | /*-----------------------------------------------------------------------*/
|
---|
3006 |
|
---|
3007 | FRESULT f_close (
|
---|
3008 | FIL *fp /* Pointer to the file object to be closed */
|
---|
3009 | )
|
---|
3010 | {
|
---|
3011 | FRESULT res;
|
---|
3012 |
|
---|
3013 |
|
---|
3014 | #if !FF_FS_READONLY
|
---|
3015 | res = f_sync(fp); /* Flush cached data */
|
---|
3016 | if (res == FR_OK)
|
---|
3017 | #endif
|
---|
3018 | {
|
---|
3019 | res = validate(fp); /* Lock volume */
|
---|
3020 | if (res == FR_OK) {
|
---|
3021 | #if FF_FS_REENTRANT
|
---|
3022 | FATFS *fs = fp->fs;
|
---|
3023 | #endif
|
---|
3024 | #if FF_FS_LOCK != 0
|
---|
3025 | res = dec_lock(fp->lockid); /* Decrement file open counter */
|
---|
3026 | if (res == FR_OK)
|
---|
3027 | #endif
|
---|
3028 | fp->fs = 0; /* Invalidate file object */
|
---|
3029 | #if FF_FS_REENTRANT
|
---|
3030 | unlock_fs(fs, FR_OK); /* Unlock volume */
|
---|
3031 | #endif
|
---|
3032 | }
|
---|
3033 | }
|
---|
3034 | return res;
|
---|
3035 | }
|
---|
3036 |
|
---|
3037 |
|
---|
3038 |
|
---|
3039 |
|
---|
3040 | /*-----------------------------------------------------------------------*/
|
---|
3041 | /* Change Current Directory or Current Drive, Get Current Directory */
|
---|
3042 | /*-----------------------------------------------------------------------*/
|
---|
3043 |
|
---|
3044 | #if FF_FS_RPATH >= 1
|
---|
3045 | #if FF_VOLUMES >= 2
|
---|
3046 | FRESULT f_chdrive (
|
---|
3047 | const TCHAR* path /* Drive number */
|
---|
3048 | )
|
---|
3049 | {
|
---|
3050 | int vol;
|
---|
3051 |
|
---|
3052 |
|
---|
3053 | vol = get_ldnumber(&path);
|
---|
3054 | if (vol < 0) return FR_INVALID_DRIVE;
|
---|
3055 |
|
---|
3056 | CurrVol = (BYTE)vol;
|
---|
3057 |
|
---|
3058 | return FR_OK;
|
---|
3059 | }
|
---|
3060 | #endif
|
---|
3061 |
|
---|
3062 |
|
---|
3063 | FRESULT f_chdir (
|
---|
3064 | const TCHAR* path /* Pointer to the directory path */
|
---|
3065 | )
|
---|
3066 | {
|
---|
3067 | FRESULT res;
|
---|
3068 | FATFS_DIR dj;
|
---|
3069 | DEFINE_NAMEBUF;
|
---|
3070 |
|
---|
3071 |
|
---|
3072 | /* Get logical drive number */
|
---|
3073 | res = find_volume(&dj.fs, &path, 0);
|
---|
3074 | if (res == FR_OK) {
|
---|
3075 | INIT_BUF(dj);
|
---|
3076 | res = follow_path(&dj, path); /* Follow the path */
|
---|
3077 | FREE_BUF();
|
---|
3078 | if (res == FR_OK) { /* Follow completed */
|
---|
3079 | if (!dj.dir) {
|
---|
3080 | dj.fs->cdir = dj.sclust; /* Start directory itself */
|
---|
3081 | } else {
|
---|
3082 | if (dj.dir[DIR_Attr] & AM_DIR) /* Reached to the directory */
|
---|
3083 | dj.fs->cdir = ld_clust(dj.fs, dj.dir);
|
---|
3084 | else
|
---|
3085 | res = FR_NO_PATH; /* Reached but a file */
|
---|
3086 | }
|
---|
3087 | }
|
---|
3088 | if (res == FR_NO_FILE) res = FR_NO_PATH;
|
---|
3089 | }
|
---|
3090 |
|
---|
3091 | LEAVE_FF(dj.fs, res);
|
---|
3092 | }
|
---|
3093 |
|
---|
3094 |
|
---|
3095 | #if FF_FS_RPATH >= 2
|
---|
3096 | FRESULT f_getcwd (
|
---|
3097 | TCHAR* buff, /* Pointer to the directory path */
|
---|
3098 | UINT len /* Size of path */
|
---|
3099 | )
|
---|
3100 | {
|
---|
3101 | FRESULT res;
|
---|
3102 | FATFS_DIR dj;
|
---|
3103 | UINT i, n;
|
---|
3104 | DWORD ccl;
|
---|
3105 | TCHAR *tp;
|
---|
3106 | FILINFO fno;
|
---|
3107 | DEFINE_NAMEBUF;
|
---|
3108 |
|
---|
3109 |
|
---|
3110 | *buff = 0;
|
---|
3111 | /* Get logical drive number */
|
---|
3112 | res = find_volume(&dj.fs, (const TCHAR**)&buff, 0); /* Get current volume */
|
---|
3113 | if (res == FR_OK) {
|
---|
3114 | INIT_BUF(dj);
|
---|
3115 | i = len; /* Bottom of buffer (directory stack base) */
|
---|
3116 | dj.sclust = dj.fs->cdir; /* Start to follow upper directory from current directory */
|
---|
3117 | while ((ccl = dj.sclust) != 0) { /* Repeat while current directory is a sub-directory */
|
---|
3118 | res = dir_sdi(&dj, 1); /* Get parent directory */
|
---|
3119 | if (res != FR_OK) break;
|
---|
3120 | res = dir_read(&dj, 0);
|
---|
3121 | if (res != FR_OK) break;
|
---|
3122 | dj.sclust = ld_clust(dj.fs, dj.dir); /* Goto parent directory */
|
---|
3123 | res = dir_sdi(&dj, 0);
|
---|
3124 | if (res != FR_OK) break;
|
---|
3125 | do { /* Find the entry links to the child directory */
|
---|
3126 | res = dir_read(&dj, 0);
|
---|
3127 | if (res != FR_OK) break;
|
---|
3128 | if (ccl == ld_clust(dj.fs, dj.dir)) break; /* Found the entry */
|
---|
3129 | res = dir_next(&dj, 0);
|
---|
3130 | } while (res == FR_OK);
|
---|
3131 | if (res == FR_NO_FILE) res = FR_INT_ERR;/* It cannot be 'not found'. */
|
---|
3132 | if (res != FR_OK) break;
|
---|
3133 | #if FF_USE_LFN
|
---|
3134 | fno.lfname = buff;
|
---|
3135 | fno.lfsize = i;
|
---|
3136 | #endif
|
---|
3137 | get_fileinfo(&dj, &fno); /* Get the directory name and push it to the buffer */
|
---|
3138 | tp = fno.fname;
|
---|
3139 | #if FF_USE_LFN
|
---|
3140 | if (*buff) tp = buff;
|
---|
3141 | #endif
|
---|
3142 | for (n = 0; tp[n]; n++) ;
|
---|
3143 | if (i < n + 3) {
|
---|
3144 | res = FR_NOT_ENOUGH_CORE; break;
|
---|
3145 | }
|
---|
3146 | while (n) buff[--i] = tp[--n];
|
---|
3147 | buff[--i] = '/';
|
---|
3148 | }
|
---|
3149 | tp = buff;
|
---|
3150 | if (res == FR_OK) {
|
---|
3151 | #if FF_VOLUMES >= 2
|
---|
3152 | *tp++ = '0' + CurrVol; /* Put drive number */
|
---|
3153 | *tp++ = ':';
|
---|
3154 | #endif
|
---|
3155 | if (i == len) { /* Root-directory */
|
---|
3156 | *tp++ = '/';
|
---|
3157 | } else { /* Sub-directroy */
|
---|
3158 | do /* Add stacked path str */
|
---|
3159 | *tp++ = buff[i++];
|
---|
3160 | while (i < len);
|
---|
3161 | }
|
---|
3162 | }
|
---|
3163 | *tp = 0;
|
---|
3164 | FREE_BUF();
|
---|
3165 | }
|
---|
3166 |
|
---|
3167 | LEAVE_FF(dj.fs, res);
|
---|
3168 | }
|
---|
3169 | #endif /* FF_FS_RPATH >= 2 */
|
---|
3170 | #endif /* FF_FS_RPATH >= 1 */
|
---|
3171 |
|
---|
3172 |
|
---|
3173 |
|
---|
3174 | #if FF_FS_MINIMIZE <= 2
|
---|
3175 | /*-----------------------------------------------------------------------*/
|
---|
3176 | /* Seek File R/W Pointer */
|
---|
3177 | /*-----------------------------------------------------------------------*/
|
---|
3178 |
|
---|
3179 | FRESULT f_lseek (
|
---|
3180 | FIL* fp, /* Pointer to the file object */
|
---|
3181 | DWORD ofs /* File pointer from top of file */
|
---|
3182 | )
|
---|
3183 | {
|
---|
3184 | FRESULT res;
|
---|
3185 | DWORD clst, bcs, nsect, ifptr;
|
---|
3186 | #if FF_USE_FASTSEEK
|
---|
3187 | DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;
|
---|
3188 | #endif
|
---|
3189 |
|
---|
3190 |
|
---|
3191 | res = validate(fp); /* Check validity of the object */
|
---|
3192 | if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
---|
3193 | if (fp->err) /* Check error */
|
---|
3194 | LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
---|
3195 |
|
---|
3196 | #if FF_USE_FASTSEEK
|
---|
3197 | if (fp->cltbl) { /* Fast seek */
|
---|
3198 | if (ofs == CREATE_LINKMAP) { /* Create CLMT */
|
---|
3199 | tbl = fp->cltbl;
|
---|
3200 | tlen = *tbl++; ulen = 2; /* Given table size and required table size */
|
---|
3201 | cl = fp->sclust; /* Top of the chain */
|
---|
3202 | if (cl) {
|
---|
3203 | do {
|
---|
3204 | /* Get a fragment */
|
---|
3205 | tcl = cl; ncl = 0; ulen += 2; /* Top, length and used items */
|
---|
3206 | do {
|
---|
3207 | pcl = cl; ncl++;
|
---|
3208 | cl = get_fat(fp->fs, cl);
|
---|
3209 | if (cl <= 1) ABORT(fp->fs, FR_INT_ERR);
|
---|
3210 | if (cl == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
3211 | } while (cl == pcl + 1);
|
---|
3212 | if (ulen <= tlen) { /* Store the length and top of the fragment */
|
---|
3213 | *tbl++ = ncl; *tbl++ = tcl;
|
---|
3214 | }
|
---|
3215 | } while (cl < fp->fs->n_fatent); /* Repeat until end of chain */
|
---|
3216 | }
|
---|
3217 | *fp->cltbl = ulen; /* Number of items used */
|
---|
3218 | if (ulen <= tlen)
|
---|
3219 | *tbl = 0; /* Terminate table */
|
---|
3220 | else
|
---|
3221 | res = FR_NOT_ENOUGH_CORE; /* Given table size is smaller than required */
|
---|
3222 |
|
---|
3223 | } else { /* Fast seek */
|
---|
3224 | if (ofs > fp->fsize) /* Clip offset at the file size */
|
---|
3225 | ofs = fp->fsize;
|
---|
3226 | fp->fptr = ofs; /* Set file pointer */
|
---|
3227 | if (ofs) {
|
---|
3228 | fp->clust = clmt_clust(fp, ofs - 1);
|
---|
3229 | dsc = clust2sect(fp->fs, fp->clust);
|
---|
3230 | if (!dsc) ABORT(fp->fs, FR_INT_ERR);
|
---|
3231 | dsc += (ofs - 1) / SS(fp->fs) & (fp->fs->csize - 1);
|
---|
3232 | if (fp->fptr % SS(fp->fs) && dsc != fp->dsect) { /* Refill sector cache if needed */
|
---|
3233 | #if !FF_FS_TINY
|
---|
3234 | #if !FF_FS_READONLY
|
---|
3235 | if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */
|
---|
3236 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
3237 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
3238 | fp->flag &= ~FA_DIRTY;
|
---|
3239 | }
|
---|
3240 | #endif
|
---|
3241 | if (disk_read(fp->fs->drv, fp->buf, dsc, 1) != RES_OK) /* Load current sector */
|
---|
3242 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
3243 | #endif
|
---|
3244 | fp->dsect = dsc;
|
---|
3245 | }
|
---|
3246 | }
|
---|
3247 | }
|
---|
3248 | } else
|
---|
3249 | #endif
|
---|
3250 |
|
---|
3251 | /* Normal Seek */
|
---|
3252 | {
|
---|
3253 | if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */
|
---|
3254 | #if !FF_FS_READONLY
|
---|
3255 | && !(fp->flag & FA_WRITE)
|
---|
3256 | #endif
|
---|
3257 | ) ofs = fp->fsize;
|
---|
3258 |
|
---|
3259 | ifptr = fp->fptr;
|
---|
3260 | fp->fptr = nsect = 0;
|
---|
3261 | if (ofs) {
|
---|
3262 | bcs = (DWORD)fp->fs->csize * SS(fp->fs); /* Cluster size (byte) */
|
---|
3263 | if (ifptr > 0 &&
|
---|
3264 | (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */
|
---|
3265 | fp->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */
|
---|
3266 | ofs -= fp->fptr;
|
---|
3267 | clst = fp->clust;
|
---|
3268 | } else { /* When seek to back cluster, */
|
---|
3269 | clst = fp->sclust; /* start from the first cluster */
|
---|
3270 | #if !FF_FS_READONLY
|
---|
3271 | if (clst == 0) { /* If no cluster chain, create a new chain */
|
---|
3272 | clst = create_chain(fp->fs, 0);
|
---|
3273 | if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
|
---|
3274 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
3275 | fp->sclust = clst;
|
---|
3276 | }
|
---|
3277 | #endif
|
---|
3278 | fp->clust = clst;
|
---|
3279 | }
|
---|
3280 | if (clst != 0) {
|
---|
3281 | while (ofs > bcs) { /* Cluster following loop */
|
---|
3282 | #if !FF_FS_READONLY
|
---|
3283 | if (fp->flag & FA_WRITE) { /* Check if in write mode or not */
|
---|
3284 | clst = create_chain(fp->fs, clst); /* Force stretch if in write mode */
|
---|
3285 | if (clst == 0) { /* When disk gets full, clip file size */
|
---|
3286 | ofs = bcs; break;
|
---|
3287 | }
|
---|
3288 | } else
|
---|
3289 | #endif
|
---|
3290 | clst = get_fat(fp->fs, clst); /* Follow cluster chain if not in write mode */
|
---|
3291 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
3292 | if (clst <= 1 || clst >= fp->fs->n_fatent) ABORT(fp->fs, FR_INT_ERR);
|
---|
3293 | fp->clust = clst;
|
---|
3294 | fp->fptr += bcs;
|
---|
3295 | ofs -= bcs;
|
---|
3296 | }
|
---|
3297 | fp->fptr += ofs;
|
---|
3298 | if (ofs % SS(fp->fs)) {
|
---|
3299 | nsect = clust2sect(fp->fs, clst); /* Current sector */
|
---|
3300 | if (!nsect) ABORT(fp->fs, FR_INT_ERR);
|
---|
3301 | nsect += ofs / SS(fp->fs);
|
---|
3302 | }
|
---|
3303 | }
|
---|
3304 | }
|
---|
3305 | if (fp->fptr % SS(fp->fs) && nsect != fp->dsect) { /* Fill sector cache if needed */
|
---|
3306 | #if !FF_FS_TINY
|
---|
3307 | #if !FF_FS_READONLY
|
---|
3308 | if (fp->flag & FA_DIRTY) { /* Write-back dirty sector cache */
|
---|
3309 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
3310 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
3311 | fp->flag &= ~FA_DIRTY;
|
---|
3312 | }
|
---|
3313 | #endif
|
---|
3314 | if (disk_read(fp->fs->drv, fp->buf, nsect, 1) != RES_OK) /* Fill sector cache */
|
---|
3315 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
3316 | #endif
|
---|
3317 | fp->dsect = nsect;
|
---|
3318 | }
|
---|
3319 | #if !FF_FS_READONLY
|
---|
3320 | if (fp->fptr > fp->fsize) { /* Set file change flag if the file size is extended */
|
---|
3321 | fp->fsize = fp->fptr;
|
---|
3322 | fp->flag |= FA_MODIFIED;
|
---|
3323 | }
|
---|
3324 | #endif
|
---|
3325 | }
|
---|
3326 |
|
---|
3327 | LEAVE_FF(fp->fs, res);
|
---|
3328 | }
|
---|
3329 |
|
---|
3330 | FRESULT f_seek(FIL* fp, DWORD ofs, BYTE mode)
|
---|
3331 | {
|
---|
3332 | switch (mode) {
|
---|
3333 | case F_SEEK_SET:
|
---|
3334 | return f_lseek((fp), ofs);
|
---|
3335 | case F_SEEK_CUR:
|
---|
3336 | return f_lseek((fp), (fp)->fptr + ofs);
|
---|
3337 | case F_SEEK_END:
|
---|
3338 | return f_lseek((fp), (fp)->fsize - ofs);
|
---|
3339 | default:
|
---|
3340 | return FR_INVALID_PARAMETER;
|
---|
3341 | }
|
---|
3342 | }
|
---|
3343 |
|
---|
3344 | #if FF_FS_MINIMIZE <= 1
|
---|
3345 | /*-----------------------------------------------------------------------*/
|
---|
3346 | /* Create a Directory Object */
|
---|
3347 | /*-----------------------------------------------------------------------*/
|
---|
3348 |
|
---|
3349 | FRESULT f_opendir (
|
---|
3350 | FATFS_DIR* dp, /* Pointer to directory object to create */
|
---|
3351 | const TCHAR* path /* Pointer to the directory path */
|
---|
3352 | )
|
---|
3353 | {
|
---|
3354 | FRESULT res;
|
---|
3355 | FATFS* fs;
|
---|
3356 | DEFINE_NAMEBUF;
|
---|
3357 |
|
---|
3358 |
|
---|
3359 | if (!dp) return FR_INVALID_OBJECT;
|
---|
3360 |
|
---|
3361 | /* Get logical drive number */
|
---|
3362 | res = find_volume(&fs, &path, 0);
|
---|
3363 | if (res == FR_OK) {
|
---|
3364 | dp->fs = fs;
|
---|
3365 | INIT_BUF(*dp);
|
---|
3366 | res = follow_path(dp, path); /* Follow the path to the directory */
|
---|
3367 | FREE_BUF();
|
---|
3368 | if (res == FR_OK) { /* Follow completed */
|
---|
3369 | if (dp->dir) { /* It is not the origin directory itself */
|
---|
3370 | if (dp->dir[DIR_Attr] & AM_DIR) /* The object is a sub directory */
|
---|
3371 | dp->sclust = ld_clust(fs, dp->dir);
|
---|
3372 | else /* The object is a file */
|
---|
3373 | res = FR_NO_PATH;
|
---|
3374 | }
|
---|
3375 | if (res == FR_OK) {
|
---|
3376 | dp->id = fs->id;
|
---|
3377 | res = dir_sdi(dp, 0); /* Rewind directory */
|
---|
3378 | #if FF_FS_LOCK != 0
|
---|
3379 | if (res == FR_OK) {
|
---|
3380 | if (dp->sclust) {
|
---|
3381 | dp->lockid = inc_lock(dp, 0); /* Lock the sub directory */
|
---|
3382 | if (!dp->lockid)
|
---|
3383 | res = FR_TOO_MANY_OPEN_FILES;
|
---|
3384 | } else {
|
---|
3385 | dp->lockid = 0; /* Root directory need not to be locked */
|
---|
3386 | }
|
---|
3387 | }
|
---|
3388 | #endif
|
---|
3389 | }
|
---|
3390 | }
|
---|
3391 | if (res == FR_NO_FILE) res = FR_NO_PATH;
|
---|
3392 | }
|
---|
3393 | if (res != FR_OK) dp->fs = 0; /* Invalidate the directory object if function faild */
|
---|
3394 |
|
---|
3395 | LEAVE_FF(fs, res);
|
---|
3396 | }
|
---|
3397 |
|
---|
3398 |
|
---|
3399 |
|
---|
3400 |
|
---|
3401 | /*-----------------------------------------------------------------------*/
|
---|
3402 | /* Close Directory */
|
---|
3403 | /*-----------------------------------------------------------------------*/
|
---|
3404 |
|
---|
3405 | FRESULT f_closedir (
|
---|
3406 | FATFS_DIR *dp /* Pointer to the directory object to be closed */
|
---|
3407 | )
|
---|
3408 | {
|
---|
3409 | FRESULT res;
|
---|
3410 |
|
---|
3411 |
|
---|
3412 | res = validate(dp);
|
---|
3413 | if (res == FR_OK) {
|
---|
3414 | #if FF_FS_REENTRANT
|
---|
3415 | FATFS *fs = dp->fs;
|
---|
3416 | #endif
|
---|
3417 | #if FF_FS_LOCK != 0
|
---|
3418 | if (dp->lockid) /* Decrement sub-directory open counter */
|
---|
3419 | res = dec_lock(dp->lockid);
|
---|
3420 | if (res == FR_OK)
|
---|
3421 | #endif
|
---|
3422 | dp->fs = 0; /* Invalidate directory object */
|
---|
3423 | #if FF_FS_REENTRANT
|
---|
3424 | unlock_fs(fs, FR_OK); /* Unlock volume */
|
---|
3425 | #endif
|
---|
3426 | }
|
---|
3427 | return res;
|
---|
3428 | }
|
---|
3429 |
|
---|
3430 |
|
---|
3431 |
|
---|
3432 |
|
---|
3433 | /*-----------------------------------------------------------------------*/
|
---|
3434 | /* Read Directory Entries in Sequence */
|
---|
3435 | /*-----------------------------------------------------------------------*/
|
---|
3436 |
|
---|
3437 | FRESULT f_readdir (
|
---|
3438 | FATFS_DIR* dp, /* Pointer to the open directory object */
|
---|
3439 | FILINFO* fno /* Pointer to file information to return */
|
---|
3440 | )
|
---|
3441 | {
|
---|
3442 | FRESULT res;
|
---|
3443 | DEFINE_NAMEBUF;
|
---|
3444 |
|
---|
3445 |
|
---|
3446 | res = validate(dp); /* Check validity of the object */
|
---|
3447 | if (res == FR_OK) {
|
---|
3448 | if (!fno) {
|
---|
3449 | res = dir_sdi(dp, 0); /* Rewind the directory object */
|
---|
3450 | } else {
|
---|
3451 | INIT_BUF(*dp);
|
---|
3452 | res = dir_read(dp, 0); /* Read an item */
|
---|
3453 | if (res == FR_NO_FILE) { /* Reached end of directory */
|
---|
3454 | dp->sect = 0;
|
---|
3455 | res = FR_OK;
|
---|
3456 | }
|
---|
3457 | if (res == FR_OK) { /* A valid entry is found */
|
---|
3458 | get_fileinfo(dp, fno); /* Get the object information */
|
---|
3459 | res = dir_next(dp, 0); /* Increment index for next */
|
---|
3460 | if (res == FR_NO_FILE) {
|
---|
3461 | dp->sect = 0;
|
---|
3462 | res = FR_OK;
|
---|
3463 | }
|
---|
3464 | }
|
---|
3465 | FREE_BUF();
|
---|
3466 | }
|
---|
3467 | }
|
---|
3468 |
|
---|
3469 | LEAVE_FF(dp->fs, res);
|
---|
3470 | }
|
---|
3471 |
|
---|
3472 |
|
---|
3473 |
|
---|
3474 | #if FF_USE_FIND
|
---|
3475 | /*-----------------------------------------------------------------------*/
|
---|
3476 | /* Find next file */
|
---|
3477 | /*-----------------------------------------------------------------------*/
|
---|
3478 |
|
---|
3479 | FRESULT f_findnext (
|
---|
3480 | FATFS_DIR* dp, /* Pointer to the open directory object */
|
---|
3481 | FILINFO* fno /* Pointer to the file information structure */
|
---|
3482 | )
|
---|
3483 | {
|
---|
3484 | FRESULT res;
|
---|
3485 |
|
---|
3486 |
|
---|
3487 | for (;;) {
|
---|
3488 | res = f_readdir(dp, fno); /* Get a directory item */
|
---|
3489 | if (res != FR_OK || !fno || !fno->fname[0]) break; /* Terminate if any error or end of directory */
|
---|
3490 | #if FF_USE_LFN
|
---|
3491 | if (fno->lfname && pattern_matching(dp->pat, fno->lfname, 0, 0)) break; /* Test for LFN if exist */
|
---|
3492 | #endif
|
---|
3493 | if (pattern_matching(dp->pat, fno->fname, 0, 0)) break; /* Test for SFN */
|
---|
3494 | }
|
---|
3495 | return res;
|
---|
3496 |
|
---|
3497 | }
|
---|
3498 |
|
---|
3499 |
|
---|
3500 |
|
---|
3501 | /*-----------------------------------------------------------------------*/
|
---|
3502 | /* Find first file */
|
---|
3503 | /*-----------------------------------------------------------------------*/
|
---|
3504 |
|
---|
3505 | FRESULT f_findfirst (
|
---|
3506 | FATFS_DIR* dp, /* Pointer to the blank directory object */
|
---|
3507 | FILINFO* fno, /* Pointer to the file information structure */
|
---|
3508 | const TCHAR* path, /* Pointer to the directory to open */
|
---|
3509 | const TCHAR* pattern /* Pointer to the matching pattern */
|
---|
3510 | )
|
---|
3511 | {
|
---|
3512 | FRESULT res;
|
---|
3513 |
|
---|
3514 |
|
---|
3515 | dp->pat = pattern; /* Save pointer to pattern string */
|
---|
3516 | res = f_opendir(dp, path); /* Open the target directory */
|
---|
3517 | if (res == FR_OK)
|
---|
3518 | res = f_findnext(dp, fno); /* Find the first item */
|
---|
3519 | return res;
|
---|
3520 | }
|
---|
3521 |
|
---|
3522 | #endif /* FF_USE_FIND */
|
---|
3523 |
|
---|
3524 |
|
---|
3525 |
|
---|
3526 | #if FF_FS_MINIMIZE == 0
|
---|
3527 | /*-----------------------------------------------------------------------*/
|
---|
3528 | /* Get File Status */
|
---|
3529 | /*-----------------------------------------------------------------------*/
|
---|
3530 |
|
---|
3531 | FRESULT f_stat (
|
---|
3532 | const TCHAR* path, /* Pointer to the file path */
|
---|
3533 | FILINFO* fno /* Pointer to file information to return */
|
---|
3534 | )
|
---|
3535 | {
|
---|
3536 | FRESULT res;
|
---|
3537 | FATFS_DIR dj;
|
---|
3538 | DEFINE_NAMEBUF;
|
---|
3539 |
|
---|
3540 |
|
---|
3541 | /* Get logical drive number */
|
---|
3542 | res = find_volume(&dj.fs, &path, 0);
|
---|
3543 | if (res == FR_OK) {
|
---|
3544 | INIT_BUF(dj);
|
---|
3545 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
3546 | if (res == FR_OK) { /* Follow completed */
|
---|
3547 | if (dj.dir) { /* Found an object */
|
---|
3548 | if (fno) get_fileinfo(&dj, fno);
|
---|
3549 | } else { /* It is root directory */
|
---|
3550 | res = FR_INVALID_NAME;
|
---|
3551 | }
|
---|
3552 | }
|
---|
3553 | FREE_BUF();
|
---|
3554 | }
|
---|
3555 |
|
---|
3556 | LEAVE_FF(dj.fs, res);
|
---|
3557 | }
|
---|
3558 |
|
---|
3559 |
|
---|
3560 |
|
---|
3561 | #if !FF_FS_READONLY
|
---|
3562 | /*-----------------------------------------------------------------------*/
|
---|
3563 | /* Get Number of Free Clusters */
|
---|
3564 | /*-----------------------------------------------------------------------*/
|
---|
3565 |
|
---|
3566 | FRESULT f_getfree (
|
---|
3567 | const TCHAR* path, /* Path name of the logical drive number */
|
---|
3568 | DWORD* nclst, /* Pointer to a variable to return number of free clusters */
|
---|
3569 | FATFS** fatfs /* Pointer to return pointer to corresponding file system object */
|
---|
3570 | )
|
---|
3571 | {
|
---|
3572 | FRESULT res;
|
---|
3573 | FATFS *fs;
|
---|
3574 | DWORD nfree, clst, sect, stat;
|
---|
3575 | UINT i;
|
---|
3576 | BYTE fat, *p;
|
---|
3577 |
|
---|
3578 |
|
---|
3579 | /* Get logical drive number */
|
---|
3580 | res = find_volume(fatfs, &path, 0);
|
---|
3581 | fs = *fatfs;
|
---|
3582 | if (res == FR_OK) {
|
---|
3583 | /* If free_clust is valid, return it without full cluster scan */
|
---|
3584 | if (fs->free_clust <= fs->n_fatent - 2) {
|
---|
3585 | *nclst = fs->free_clust;
|
---|
3586 | } else {
|
---|
3587 | /* Get number of free clusters */
|
---|
3588 | fat = fs->fs_type;
|
---|
3589 | nfree = 0;
|
---|
3590 | if (fat == FS_FAT12) { /* Sector unalighed entries: Search FAT via regular routine. */
|
---|
3591 | clst = 2;
|
---|
3592 | do {
|
---|
3593 | stat = get_fat(fs, clst);
|
---|
3594 | if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
|
---|
3595 | if (stat == 1) { res = FR_INT_ERR; break; }
|
---|
3596 | if (stat == 0) nfree++;
|
---|
3597 | } while (++clst < fs->n_fatent);
|
---|
3598 | } else { /* Sector alighed entries: Accelerate the FAT search. */
|
---|
3599 | clst = fs->n_fatent; sect = fs->fatbase;
|
---|
3600 | i = 0; p = 0;
|
---|
3601 | do {
|
---|
3602 | if (!i) {
|
---|
3603 | res = move_window(fs, sect++);
|
---|
3604 | if (res != FR_OK) break;
|
---|
3605 | p = fs->win;
|
---|
3606 | i = SS(fs);
|
---|
3607 | }
|
---|
3608 | if (fat == FS_FAT16) {
|
---|
3609 | if (LD_WORD(p) == 0) nfree++;
|
---|
3610 | p += 2; i -= 2;
|
---|
3611 | } else {
|
---|
3612 | if ((LD_DWORD(p) & 0x0FFFFFFF) == 0) nfree++;
|
---|
3613 | p += 4; i -= 4;
|
---|
3614 | }
|
---|
3615 | } while (--clst);
|
---|
3616 | }
|
---|
3617 | fs->free_clust = nfree; /* free_clust is valid */
|
---|
3618 | fs->fsi_flag |= 1; /* FSInfo is to be updated */
|
---|
3619 | *nclst = nfree; /* Return the free clusters */
|
---|
3620 | }
|
---|
3621 | }
|
---|
3622 | LEAVE_FF(fs, res);
|
---|
3623 | }
|
---|
3624 |
|
---|
3625 |
|
---|
3626 |
|
---|
3627 |
|
---|
3628 | /*-----------------------------------------------------------------------*/
|
---|
3629 | /* Truncate File */
|
---|
3630 | /*-----------------------------------------------------------------------*/
|
---|
3631 |
|
---|
3632 | FRESULT f_truncate (
|
---|
3633 | FIL* fp /* Pointer to the file object */
|
---|
3634 | )
|
---|
3635 | {
|
---|
3636 | FRESULT res;
|
---|
3637 | DWORD ncl;
|
---|
3638 |
|
---|
3639 |
|
---|
3640 | res = validate(fp); /* Check validity of the object */
|
---|
3641 | if (res == FR_OK) {
|
---|
3642 | if (fp->err) { /* Check error */
|
---|
3643 | res = (FRESULT)fp->err;
|
---|
3644 | } else {
|
---|
3645 | if (!(fp->flag & FA_WRITE)) /* Check access mode */
|
---|
3646 | res = FR_DENIED;
|
---|
3647 | }
|
---|
3648 | }
|
---|
3649 | if (res == FR_OK) {
|
---|
3650 | if (fp->fsize > fp->fptr) {
|
---|
3651 | fp->fsize = fp->fptr; /* Set file size to current R/W point */
|
---|
3652 | fp->flag |= FA_MODIFIED;
|
---|
3653 | if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */
|
---|
3654 | res = remove_chain(fp->fs, fp->sclust);
|
---|
3655 | fp->sclust = 0;
|
---|
3656 | } else { /* When truncate a part of the file, remove remaining clusters */
|
---|
3657 | ncl = get_fat(fp->fs, fp->clust);
|
---|
3658 | res = FR_OK;
|
---|
3659 | if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;
|
---|
3660 | if (ncl == 1) res = FR_INT_ERR;
|
---|
3661 | if (res == FR_OK && ncl < fp->fs->n_fatent) {
|
---|
3662 | res = put_fat(fp->fs, fp->clust, 0x0FFFFFFF);
|
---|
3663 | if (res == FR_OK) res = remove_chain(fp->fs, ncl);
|
---|
3664 | }
|
---|
3665 | }
|
---|
3666 | #if !FF_FS_TINY
|
---|
3667 | if (res == FR_OK && (fp->flag & FA_DIRTY)) {
|
---|
3668 | if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
|
---|
3669 | res = FR_DISK_ERR;
|
---|
3670 | else
|
---|
3671 | fp->flag &= ~FA_DIRTY;
|
---|
3672 | }
|
---|
3673 | #endif
|
---|
3674 | }
|
---|
3675 | if (res != FR_OK) fp->err = (FRESULT)res;
|
---|
3676 | }
|
---|
3677 |
|
---|
3678 | LEAVE_FF(fp->fs, res);
|
---|
3679 | }
|
---|
3680 |
|
---|
3681 |
|
---|
3682 |
|
---|
3683 |
|
---|
3684 | /*-----------------------------------------------------------------------*/
|
---|
3685 | /* Delete a File or Directory */
|
---|
3686 | /*-----------------------------------------------------------------------*/
|
---|
3687 |
|
---|
3688 | FRESULT f_unlink (
|
---|
3689 | const TCHAR* path /* Pointer to the file or directory path */
|
---|
3690 | )
|
---|
3691 | {
|
---|
3692 | FRESULT res;
|
---|
3693 | FATFS_DIR dj, sdj;
|
---|
3694 | BYTE *dir;
|
---|
3695 | DWORD dclst = 0;
|
---|
3696 | DEFINE_NAMEBUF;
|
---|
3697 |
|
---|
3698 |
|
---|
3699 | /* Get logical drive number */
|
---|
3700 | res = find_volume(&dj.fs, &path, 1);
|
---|
3701 | if (res == FR_OK) {
|
---|
3702 | INIT_BUF(dj);
|
---|
3703 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
3704 | if (FF_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT))
|
---|
3705 | res = FR_INVALID_NAME; /* Cannot remove dot entry */
|
---|
3706 | #if FF_FS_LOCK != 0
|
---|
3707 | if (res == FR_OK) res = chk_lock(&dj, 2); /* Cannot remove open object */
|
---|
3708 | #endif
|
---|
3709 | if (res == FR_OK) { /* The object is accessible */
|
---|
3710 | dir = dj.dir;
|
---|
3711 | if (!dir) {
|
---|
3712 | res = FR_INVALID_NAME; /* Cannot remove the origin directory */
|
---|
3713 | } else {
|
---|
3714 | if (dir[DIR_Attr] & AM_RDO)
|
---|
3715 | res = FR_DENIED; /* Cannot remove R/O object */
|
---|
3716 | }
|
---|
3717 | if (res == FR_OK) {
|
---|
3718 | dclst = ld_clust(dj.fs, dir);
|
---|
3719 | if (dclst && (dir[DIR_Attr] & AM_DIR)) { /* Is it a sub-directory ? */
|
---|
3720 | #if FF_FS_RPATH
|
---|
3721 | if (dclst == dj.fs->cdir) { /* Is it the current directory? */
|
---|
3722 | res = FR_DENIED;
|
---|
3723 | } else
|
---|
3724 | #endif
|
---|
3725 | {
|
---|
3726 | mem_cpy(&sdj, &dj, sizeof (FATFS_DIR)); /* Open the sub-directory */
|
---|
3727 | sdj.sclust = dclst;
|
---|
3728 | res = dir_sdi(&sdj, 2);
|
---|
3729 | if (res == FR_OK) {
|
---|
3730 | res = dir_read(&sdj, 0); /* Read an item (excluding dot entries) */
|
---|
3731 | if (res == FR_OK) res = FR_DENIED; /* Not empty? (cannot remove) */
|
---|
3732 | if (res == FR_NO_FILE) res = FR_OK; /* Empty? (can remove) */
|
---|
3733 | }
|
---|
3734 | }
|
---|
3735 | }
|
---|
3736 | }
|
---|
3737 | if (res == FR_OK) {
|
---|
3738 | res = dir_remove(&dj); /* Remove the directory entry */
|
---|
3739 | if (res == FR_OK && dclst) /* Remove the cluster chain if exist */
|
---|
3740 | res = remove_chain(dj.fs, dclst);
|
---|
3741 | if (res == FR_OK) res = sync_fs(dj.fs);
|
---|
3742 | }
|
---|
3743 | }
|
---|
3744 | FREE_BUF();
|
---|
3745 | }
|
---|
3746 |
|
---|
3747 | LEAVE_FF(dj.fs, res);
|
---|
3748 | }
|
---|
3749 |
|
---|
3750 |
|
---|
3751 |
|
---|
3752 |
|
---|
3753 | /*-----------------------------------------------------------------------*/
|
---|
3754 | /* Create a Directory */
|
---|
3755 | /*-----------------------------------------------------------------------*/
|
---|
3756 |
|
---|
3757 | FRESULT f_mkdir (
|
---|
3758 | const TCHAR* path /* Pointer to the directory path */
|
---|
3759 | )
|
---|
3760 | {
|
---|
3761 | FRESULT res;
|
---|
3762 | FATFS_DIR dj;
|
---|
3763 | BYTE *dir, n;
|
---|
3764 | DWORD dsc, dcl, pcl, tm = GET_FATTIME();
|
---|
3765 | DEFINE_NAMEBUF;
|
---|
3766 |
|
---|
3767 |
|
---|
3768 | /* Get logical drive number */
|
---|
3769 | res = find_volume(&dj.fs, &path, 1);
|
---|
3770 | if (res == FR_OK) {
|
---|
3771 | INIT_BUF(dj);
|
---|
3772 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
3773 | if (res == FR_OK) res = FR_EXIST; /* Any object with same name is already existing */
|
---|
3774 | if (FF_FS_RPATH && res == FR_NO_FILE && (dj.fn[NSFLAG] & NS_DOT))
|
---|
3775 | res = FR_INVALID_NAME;
|
---|
3776 | if (res == FR_NO_FILE) { /* Can create a new directory */
|
---|
3777 | dcl = create_chain(dj.fs, 0); /* Allocate a cluster for the new directory table */
|
---|
3778 | res = FR_OK;
|
---|
3779 | if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster */
|
---|
3780 | if (dcl == 1) res = FR_INT_ERR;
|
---|
3781 | if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
|
---|
3782 | if (res == FR_OK) /* Flush FAT */
|
---|
3783 | res = sync_window(dj.fs);
|
---|
3784 | if (res == FR_OK) { /* Initialize the new directory table */
|
---|
3785 | dsc = clust2sect(dj.fs, dcl);
|
---|
3786 | dir = dj.fs->win;
|
---|
3787 | mem_set(dir, 0, SS(dj.fs));
|
---|
3788 | mem_set(dir + DIR_Name, ' ', 11); /* Create "." entry */
|
---|
3789 | dir[DIR_Name] = '.';
|
---|
3790 | dir[DIR_Attr] = AM_DIR;
|
---|
3791 | ST_DWORD(dir + DIR_WrtTime, tm);
|
---|
3792 | st_clust(dir, dcl);
|
---|
3793 | mem_cpy(dir + SZ_DIRE, dir, SZ_DIRE); /* Create ".." entry */
|
---|
3794 | dir[SZ_DIRE + 1] = '.'; pcl = dj.sclust;
|
---|
3795 | if (dj.fs->fs_type == FS_FAT32 && pcl == dj.fs->dirbase)
|
---|
3796 | pcl = 0;
|
---|
3797 | st_clust(dir + SZ_DIRE, pcl);
|
---|
3798 | for (n = dj.fs->csize; n; n--) { /* Write dot entries and clear following sectors */
|
---|
3799 | dj.fs->winsect = dsc++;
|
---|
3800 | dj.fs->wflag = 1;
|
---|
3801 | res = sync_window(dj.fs);
|
---|
3802 | if (res != FR_OK) break;
|
---|
3803 | mem_set(dir, 0, SS(dj.fs));
|
---|
3804 | }
|
---|
3805 | }
|
---|
3806 | if (res == FR_OK) res = dir_register(&dj); /* Register the object to the directoy */
|
---|
3807 | if (res != FR_OK) {
|
---|
3808 | remove_chain(dj.fs, dcl); /* Could not register, remove cluster chain */
|
---|
3809 | } else {
|
---|
3810 | dir = dj.dir;
|
---|
3811 | dir[DIR_Attr] = AM_DIR; /* Attribute */
|
---|
3812 | ST_DWORD(dir + DIR_WrtTime, tm); /* Created time */
|
---|
3813 | st_clust(dir, dcl); /* Table start cluster */
|
---|
3814 | dj.fs->wflag = 1;
|
---|
3815 | res = sync_fs(dj.fs);
|
---|
3816 | }
|
---|
3817 | }
|
---|
3818 | FREE_BUF();
|
---|
3819 | }
|
---|
3820 |
|
---|
3821 | LEAVE_FF(dj.fs, res);
|
---|
3822 | }
|
---|
3823 |
|
---|
3824 |
|
---|
3825 |
|
---|
3826 |
|
---|
3827 | /*-----------------------------------------------------------------------*/
|
---|
3828 | /* Change Attribute */
|
---|
3829 | /*-----------------------------------------------------------------------*/
|
---|
3830 |
|
---|
3831 | FRESULT f_chmod (
|
---|
3832 | const TCHAR* path, /* Pointer to the file path */
|
---|
3833 | BYTE attr, /* Attribute bits */
|
---|
3834 | BYTE mask /* Attribute mask to change */
|
---|
3835 | )
|
---|
3836 | {
|
---|
3837 | FRESULT res;
|
---|
3838 | FATFS_DIR dj;
|
---|
3839 | BYTE *dir;
|
---|
3840 | DEFINE_NAMEBUF;
|
---|
3841 |
|
---|
3842 |
|
---|
3843 | res = find_volume(&dj.fs, &path, 1); /* Get logical drive number */
|
---|
3844 | if (res == FR_OK) {
|
---|
3845 | INIT_BUF(dj);
|
---|
3846 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
3847 | FREE_BUF();
|
---|
3848 | if (FF_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT))
|
---|
3849 | res = FR_INVALID_NAME;
|
---|
3850 | if (res == FR_OK) {
|
---|
3851 | dir = dj.dir;
|
---|
3852 | if (!dir) { /* Is it a root directory? */
|
---|
3853 | res = FR_INVALID_NAME;
|
---|
3854 | } else { /* File or sub directory */
|
---|
3855 | mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */
|
---|
3856 | dir[DIR_Attr] = (attr & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */
|
---|
3857 | dj.fs->wflag = 1;
|
---|
3858 | res = sync_fs(dj.fs);
|
---|
3859 | }
|
---|
3860 | }
|
---|
3861 | }
|
---|
3862 |
|
---|
3863 | LEAVE_FF(dj.fs, res);
|
---|
3864 | }
|
---|
3865 |
|
---|
3866 |
|
---|
3867 |
|
---|
3868 |
|
---|
3869 | /*-----------------------------------------------------------------------*/
|
---|
3870 | /* Rename File/Directory */
|
---|
3871 | /*-----------------------------------------------------------------------*/
|
---|
3872 |
|
---|
3873 | FRESULT f_rename (
|
---|
3874 | const TCHAR* path_old, /* Pointer to the object to be renamed */
|
---|
3875 | const TCHAR* path_new /* Pointer to the new name */
|
---|
3876 | )
|
---|
3877 | {
|
---|
3878 | FRESULT res;
|
---|
3879 | FATFS_DIR djo, djn;
|
---|
3880 | BYTE buf[21], *dir;
|
---|
3881 | DWORD dw;
|
---|
3882 | DEFINE_NAMEBUF;
|
---|
3883 | TCHAR *temp_new_path = 0;
|
---|
3884 |
|
---|
3885 |
|
---|
3886 | /* Get logical drive number of the source object */
|
---|
3887 | res = find_volume(&djo.fs, &path_old, 1);
|
---|
3888 | if (res == FR_OK) {
|
---|
3889 | djn.fs = djo.fs;
|
---|
3890 | INIT_BUF(djo);
|
---|
3891 | res = follow_path(&djo, path_old); /* Check old object */
|
---|
3892 | if (FF_FS_RPATH && res == FR_OK && (djo.fn[NSFLAG] & NS_DOT))
|
---|
3893 | res = FR_INVALID_NAME;
|
---|
3894 | #if FF_FS_LOCK != 0
|
---|
3895 | if (res == FR_OK) res = chk_lock(&djo, 2);
|
---|
3896 | #endif
|
---|
3897 | if (res == FR_OK) { /* Old object is found */
|
---|
3898 | if (!djo.dir) { /* Is root dir? */
|
---|
3899 | res = FR_NO_FILE;
|
---|
3900 | } else {
|
---|
3901 | mem_cpy(buf, djo.dir + DIR_Attr, 21); /* Save information about object except name */
|
---|
3902 | mem_cpy(&djn, &djo, sizeof (FATFS_DIR)); /* Duplicate the directory object */
|
---|
3903 | if (get_ldnumber(&path_new) >= 0) /* Snip drive number off and ignore it */
|
---|
3904 | res = follow_path(&djn, path_new); /* and make sure if new object name is not conflicting */
|
---|
3905 | else
|
---|
3906 | res = FR_INVALID_DRIVE;
|
---|
3907 | if (res == FR_OK) {
|
---|
3908 | res = FR_EXIST; /* The new object name is already existing */
|
---|
3909 | dir = djn.dir;
|
---|
3910 | if (dir[DIR_Attr] & AM_DIR) { /* The new object is a directory */
|
---|
3911 | temp_new_path = (TCHAR *)ff_memalloc((FF_MAX_LFN + 1) * sizeof(WCHAR));
|
---|
3912 | ntstdio_snprintf(temp_new_path, FF_MAX_LFN, "%s/%s", path_new, basename((char *)path_old));
|
---|
3913 | res = follow_path(&djn, temp_new_path);
|
---|
3914 | }
|
---|
3915 | }
|
---|
3916 | if (res == FR_NO_FILE) { /* It is a valid path and no name collision */
|
---|
3917 | res = dir_register(&djn); /* Register the new entry */
|
---|
3918 | if (res == FR_OK) {
|
---|
3919 | /* Start of critical section where any interruption can cause a cross-link */
|
---|
3920 | dir = djn.dir; /* Copy information about object except name */
|
---|
3921 | mem_cpy(dir + 13, buf + 2, 19);
|
---|
3922 | dir[DIR_Attr] = buf[0] | AM_ARC;
|
---|
3923 | djo.fs->wflag = 1;
|
---|
3924 | if ((dir[DIR_Attr] & AM_DIR) && djo.sclust != djn.sclust) { /* Update .. entry in the sub-directory if needed */
|
---|
3925 | dw = clust2sect(djo.fs, ld_clust(djo.fs, dir));
|
---|
3926 | if (!dw) {
|
---|
3927 | res = FR_INT_ERR;
|
---|
3928 | } else {
|
---|
3929 | res = move_window(djo.fs, dw);
|
---|
3930 | dir = djo.fs->win + SZ_DIRE * 1; /* Ptr to .. entry */
|
---|
3931 | if (res == FR_OK && dir[1] == '.') {
|
---|
3932 | st_clust(dir, djn.sclust);
|
---|
3933 | djo.fs->wflag = 1;
|
---|
3934 | }
|
---|
3935 | }
|
---|
3936 | }
|
---|
3937 | if (res == FR_OK) {
|
---|
3938 | res = dir_remove(&djo); /* Remove old entry */
|
---|
3939 | if (res == FR_OK)
|
---|
3940 | res = sync_fs(djo.fs);
|
---|
3941 | }
|
---|
3942 | /* End of critical section */
|
---|
3943 | }
|
---|
3944 | }
|
---|
3945 | }
|
---|
3946 | }
|
---|
3947 | FREE_BUF();
|
---|
3948 | }
|
---|
3949 |
|
---|
3950 | if (temp_new_path != 0) ff_memfree(temp_new_path);
|
---|
3951 |
|
---|
3952 | LEAVE_FF(djo.fs, res);
|
---|
3953 | }
|
---|
3954 |
|
---|
3955 |
|
---|
3956 |
|
---|
3957 |
|
---|
3958 | /*-----------------------------------------------------------------------*/
|
---|
3959 | /* Change Timestamp */
|
---|
3960 | /*-----------------------------------------------------------------------*/
|
---|
3961 |
|
---|
3962 | FRESULT f_utime (
|
---|
3963 | const TCHAR* path, /* Pointer to the file/directory name */
|
---|
3964 | const FILINFO* fno /* Pointer to the time stamp to be set */
|
---|
3965 | )
|
---|
3966 | {
|
---|
3967 | FRESULT res;
|
---|
3968 | FATFS_DIR dj;
|
---|
3969 | BYTE *dir;
|
---|
3970 | DEFINE_NAMEBUF;
|
---|
3971 |
|
---|
3972 |
|
---|
3973 | /* Get logical drive number */
|
---|
3974 | res = find_volume(&dj.fs, &path, 1);
|
---|
3975 | if (res == FR_OK) {
|
---|
3976 | INIT_BUF(dj);
|
---|
3977 | res = follow_path(&dj, path); /* Follow the file path */
|
---|
3978 | FREE_BUF();
|
---|
3979 | if (FF_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT))
|
---|
3980 | res = FR_INVALID_NAME;
|
---|
3981 | if (res == FR_OK) {
|
---|
3982 | dir = dj.dir;
|
---|
3983 | if (!dir) { /* Root directory */
|
---|
3984 | res = FR_INVALID_NAME;
|
---|
3985 | } else { /* File or sub-directory */
|
---|
3986 | ST_WORD(dir + DIR_WrtTime, fno->ftime);
|
---|
3987 | ST_WORD(dir + DIR_WrtDate, fno->fdate);
|
---|
3988 | dj.fs->wflag = 1;
|
---|
3989 | res = sync_fs(dj.fs);
|
---|
3990 | }
|
---|
3991 | }
|
---|
3992 | }
|
---|
3993 |
|
---|
3994 | LEAVE_FF(dj.fs, res);
|
---|
3995 | }
|
---|
3996 |
|
---|
3997 | #endif /* !FF_FS_READONLY */
|
---|
3998 | #endif /* FF_FS_MINIMIZE == 0 */
|
---|
3999 | #endif /* FF_FS_MINIMIZE <= 1 */
|
---|
4000 | #endif /* FF_FS_MINIMIZE <= 2 */
|
---|
4001 |
|
---|
4002 |
|
---|
4003 |
|
---|
4004 |
|
---|
4005 | #if FF_USE_LABEL
|
---|
4006 | /*-----------------------------------------------------------------------*/
|
---|
4007 | /* Get volume label */
|
---|
4008 | /*-----------------------------------------------------------------------*/
|
---|
4009 |
|
---|
4010 | FRESULT f_getlabel (
|
---|
4011 | const TCHAR* path, /* Path name of the logical drive number */
|
---|
4012 | TCHAR* label, /* Pointer to a buffer to return the volume label */
|
---|
4013 | DWORD* vsn /* Pointer to a variable to return the volume serial number */
|
---|
4014 | )
|
---|
4015 | {
|
---|
4016 | FRESULT res;
|
---|
4017 | FATFS_DIR dj;
|
---|
4018 | UINT i, j;
|
---|
4019 | #if FF_USE_LFN && FF_LFN_UNICODE
|
---|
4020 | WCHAR w;
|
---|
4021 | #endif
|
---|
4022 |
|
---|
4023 |
|
---|
4024 | /* Get logical drive number */
|
---|
4025 | res = find_volume(&dj.fs, &path, 0);
|
---|
4026 |
|
---|
4027 | /* Get volume label */
|
---|
4028 | if (res == FR_OK && label) {
|
---|
4029 | dj.sclust = 0; /* Open root directory */
|
---|
4030 | res = dir_sdi(&dj, 0);
|
---|
4031 | if (res == FR_OK) {
|
---|
4032 | res = dir_read(&dj, 1); /* Get an entry with AM_VOL */
|
---|
4033 | if (res == FR_OK) { /* A volume label is exist */
|
---|
4034 | #if FF_USE_LFN && FF_LFN_UNICODE
|
---|
4035 | i = j = 0;
|
---|
4036 | do {
|
---|
4037 | w = (i < 11) ? dj.dir[i++] : ' ';
|
---|
4038 | if (IsDBCS1(w) && i < 11 && IsDBCS2(dj.dir[i]))
|
---|
4039 | w = w << 8 | dj.dir[i++];
|
---|
4040 | label[j++] = ff_convert(w, 1); /* OEM -> Unicode */
|
---|
4041 | } while (j < 11);
|
---|
4042 | #else
|
---|
4043 | mem_cpy(label, dj.dir, 11);
|
---|
4044 | #endif
|
---|
4045 | j = 11;
|
---|
4046 | do {
|
---|
4047 | label[j] = 0;
|
---|
4048 | if (!j) break;
|
---|
4049 | } while (label[--j] == ' ');
|
---|
4050 | }
|
---|
4051 | if (res == FR_NO_FILE) { /* No label, return nul string */
|
---|
4052 | label[0] = 0;
|
---|
4053 | res = FR_OK;
|
---|
4054 | }
|
---|
4055 | }
|
---|
4056 | }
|
---|
4057 |
|
---|
4058 | /* Get volume serial number */
|
---|
4059 | if (res == FR_OK && vsn) {
|
---|
4060 | res = move_window(dj.fs, dj.fs->volbase);
|
---|
4061 | if (res == FR_OK) {
|
---|
4062 | i = dj.fs->fs_type == FS_FAT32 ? BS_VolID32 : BS_VolID;
|
---|
4063 | *vsn = LD_DWORD(&dj.fs->win[i]);
|
---|
4064 | }
|
---|
4065 | }
|
---|
4066 |
|
---|
4067 | LEAVE_FF(dj.fs, res);
|
---|
4068 | }
|
---|
4069 |
|
---|
4070 |
|
---|
4071 |
|
---|
4072 | #if !FF_FS_READONLY
|
---|
4073 | /*-----------------------------------------------------------------------*/
|
---|
4074 | /* Set volume label */
|
---|
4075 | /*-----------------------------------------------------------------------*/
|
---|
4076 |
|
---|
4077 | FRESULT f_setlabel (
|
---|
4078 | const TCHAR* label /* Pointer to the volume label to set */
|
---|
4079 | )
|
---|
4080 | {
|
---|
4081 | FRESULT res;
|
---|
4082 | FATFS_DIR dj;
|
---|
4083 | BYTE vn[11];
|
---|
4084 | UINT i, j, sl;
|
---|
4085 | WCHAR w;
|
---|
4086 | DWORD tm;
|
---|
4087 | #if FF_CODE_PAGE == 65001
|
---|
4088 | int code_size;
|
---|
4089 | unsigned char utf8_code[6];
|
---|
4090 | #endif
|
---|
4091 |
|
---|
4092 | /* Get logical drive number */
|
---|
4093 | res = find_volume(&dj.fs, &label, 1);
|
---|
4094 | if (res) LEAVE_FF(dj.fs, res);
|
---|
4095 |
|
---|
4096 | /* Create a volume label in directory form */
|
---|
4097 | vn[0] = 0;
|
---|
4098 | for (sl = 0; label[sl]; sl++) ; /* Get name length */
|
---|
4099 | for ( ; sl && label[sl - 1] == ' '; sl--) ; /* Remove trailing spaces */
|
---|
4100 | if (sl) { /* Create volume label in directory form */
|
---|
4101 | i = j = 0;
|
---|
4102 | do {
|
---|
4103 | #if FF_CODE_PAGE == 65001
|
---|
4104 | w = ff_wtoupper(Utf8_to_Utf16(&label[i], &code_size));
|
---|
4105 | i += code_size;
|
---|
4106 | Utf16_to_Utf8(utf8_code, &code_size, w);
|
---|
4107 | if (!w || chk_chr("\"*+,.:;<=>\?[]|\x7F", w) || j >= sizeof(vn) - code_size) /* Reject invalid characters for volume label */
|
---|
4108 | LEAVE_FF(dj.fs, FR_INVALID_NAME);
|
---|
4109 | for (int k = 0; k < code_size; k++)
|
---|
4110 | vn[j++] = utf8_code[k];
|
---|
4111 | #else
|
---|
4112 | #if FF_USE_LFN && FF_LFN_UNICODE
|
---|
4113 | w = ff_convert(ff_wtoupper(label[i++]), 0);
|
---|
4114 | #else
|
---|
4115 | w = (BYTE)label[i++];
|
---|
4116 | if (IsDBCS1(w))
|
---|
4117 | w = (j < 10 && i < sl && IsDBCS2(label[i])) ? w << 8 | (BYTE)label[i++] : 0;
|
---|
4118 | #if FF_USE_LFN
|
---|
4119 | w = ff_convert(ff_wtoupper(ff_convert(w, 1)), 0);
|
---|
4120 | #else
|
---|
4121 | if (IsLower(w)) w -= 0x20; /* To upper ASCII characters */
|
---|
4122 | #ifdef _EXCVT
|
---|
4123 | if (w >= 0x80) w = ExCvt[w - 0x80]; /* To upper extended characters (SBCS cfg) */
|
---|
4124 | #else
|
---|
4125 | if (!_DF1S && w >= 0x80) w = 0; /* Reject extended characters (ASCII cfg) */
|
---|
4126 | #endif
|
---|
4127 | #endif
|
---|
4128 | #endif
|
---|
4129 | if (!w || chk_chr("\"*+,.:;<=>\?[]|\x7F", w) || j >= (UINT)((w >= 0x100) ? 10 : 11)) /* Reject invalid characters for volume label */
|
---|
4130 | LEAVE_FF(dj.fs, FR_INVALID_NAME);
|
---|
4131 | if (w >= 0x100) vn[j++] = (BYTE)(w >> 8);
|
---|
4132 | vn[j++] = (BYTE)w;
|
---|
4133 | #endif
|
---|
4134 | } while (i < sl);
|
---|
4135 | while (j < 11) vn[j++] = ' '; /* Fill remaining name field */
|
---|
4136 | if (vn[0] == DDEM) LEAVE_FF(dj.fs, FR_INVALID_NAME); /* Reject illegal name (heading DDEM) */
|
---|
4137 | }
|
---|
4138 |
|
---|
4139 | /* Set volume label */
|
---|
4140 | dj.sclust = 0; /* Open root directory */
|
---|
4141 | res = dir_sdi(&dj, 0);
|
---|
4142 | if (res == FR_OK) {
|
---|
4143 | res = dir_read(&dj, 1); /* Get an entry with AM_VOL */
|
---|
4144 | if (res == FR_OK) { /* A volume label is found */
|
---|
4145 | if (vn[0]) {
|
---|
4146 | mem_cpy(dj.dir, vn, 11); /* Change the volume label name */
|
---|
4147 | tm = GET_FATTIME();
|
---|
4148 | ST_DWORD(dj.dir + DIR_WrtTime, tm);
|
---|
4149 | } else {
|
---|
4150 | dj.dir[0] = DDEM; /* Remove the volume label */
|
---|
4151 | }
|
---|
4152 | dj.fs->wflag = 1;
|
---|
4153 | res = sync_fs(dj.fs);
|
---|
4154 | } else { /* No volume label is found or error */
|
---|
4155 | if (res == FR_NO_FILE) {
|
---|
4156 | res = FR_OK;
|
---|
4157 | if (vn[0]) { /* Create volume label as new */
|
---|
4158 | res = dir_alloc(&dj, 1); /* Allocate an entry for volume label */
|
---|
4159 | if (res == FR_OK) {
|
---|
4160 | mem_set(dj.dir, 0, SZ_DIRE); /* Set volume label */
|
---|
4161 | mem_cpy(dj.dir, vn, 11);
|
---|
4162 | dj.dir[DIR_Attr] = AM_VOL;
|
---|
4163 | tm = GET_FATTIME();
|
---|
4164 | ST_DWORD(dj.dir + DIR_WrtTime, tm);
|
---|
4165 | dj.fs->wflag = 1;
|
---|
4166 | res = sync_fs(dj.fs);
|
---|
4167 | }
|
---|
4168 | }
|
---|
4169 | }
|
---|
4170 | }
|
---|
4171 | }
|
---|
4172 |
|
---|
4173 | LEAVE_FF(dj.fs, res);
|
---|
4174 | }
|
---|
4175 |
|
---|
4176 | #endif /* !FF_FS_READONLY */
|
---|
4177 | #endif /* FF_USE_LABEL */
|
---|
4178 |
|
---|
4179 |
|
---|
4180 |
|
---|
4181 | /*-----------------------------------------------------------------------*/
|
---|
4182 | /* Forward data to the stream directly (available on only tiny cfg) */
|
---|
4183 | /*-----------------------------------------------------------------------*/
|
---|
4184 | #if FF_USE_FORWARD && FF_FS_TINY
|
---|
4185 |
|
---|
4186 | FRESULT f_forward (
|
---|
4187 | FIL* fp, /* Pointer to the file object */
|
---|
4188 | UINT (*func)(const BYTE*,UINT), /* Pointer to the streaming function */
|
---|
4189 | UINT btf, /* Number of bytes to forward */
|
---|
4190 | UINT* bf /* Pointer to number of bytes forwarded */
|
---|
4191 | )
|
---|
4192 | {
|
---|
4193 | FRESULT res;
|
---|
4194 | DWORD remain, clst, sect;
|
---|
4195 | UINT rcnt;
|
---|
4196 | BYTE csect;
|
---|
4197 |
|
---|
4198 |
|
---|
4199 | *bf = 0; /* Clear transfer byte counter */
|
---|
4200 |
|
---|
4201 | res = validate(fp); /* Check validity of the object */
|
---|
4202 | if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
---|
4203 | if (fp->err) /* Check error */
|
---|
4204 | LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
---|
4205 | if (!(fp->flag & FA_READ)) /* Check access mode */
|
---|
4206 | LEAVE_FF(fp->fs, FR_DENIED);
|
---|
4207 |
|
---|
4208 | remain = fp->fsize - fp->fptr;
|
---|
4209 | if (btf > remain) btf = (UINT)remain; /* Truncate btf by remaining bytes */
|
---|
4210 |
|
---|
4211 | for ( ; btf && (*func)(0, 0); /* Repeat until all data transferred or stream becomes busy */
|
---|
4212 | fp->fptr += rcnt, *bf += rcnt, btf -= rcnt) {
|
---|
4213 | csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
|
---|
4214 | if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
|
---|
4215 | if (!csect) { /* On the cluster boundary? */
|
---|
4216 | clst = (fp->fptr == 0) ? /* On the top of the file? */
|
---|
4217 | fp->sclust : get_fat(fp->fs, fp->clust);
|
---|
4218 | if (clst <= 1) ABORT(fp->fs, FR_INT_ERR);
|
---|
4219 | if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
---|
4220 | fp->clust = clst; /* Update current cluster */
|
---|
4221 | }
|
---|
4222 | }
|
---|
4223 | sect = clust2sect(fp->fs, fp->clust); /* Get current data sector */
|
---|
4224 | if (!sect) ABORT(fp->fs, FR_INT_ERR);
|
---|
4225 | sect += csect;
|
---|
4226 | if (move_window(fp->fs, sect) != FR_OK) /* Move sector window */
|
---|
4227 | ABORT(fp->fs, FR_DISK_ERR);
|
---|
4228 | fp->dsect = sect;
|
---|
4229 | rcnt = SS(fp->fs) - (WORD)(fp->fptr % SS(fp->fs)); /* Forward data from sector window */
|
---|
4230 | if (rcnt > btf) rcnt = btf;
|
---|
4231 | rcnt = (*func)(&fp->fs->win[(WORD)fp->fptr % SS(fp->fs)], rcnt);
|
---|
4232 | if (!rcnt) ABORT(fp->fs, FR_INT_ERR);
|
---|
4233 | }
|
---|
4234 |
|
---|
4235 | LEAVE_FF(fp->fs, FR_OK);
|
---|
4236 | }
|
---|
4237 | #endif /* FF_USE_FORWARD */
|
---|
4238 |
|
---|
4239 |
|
---|
4240 |
|
---|
4241 | #if FF_USE_MKFS && !FF_FS_READONLY
|
---|
4242 | /*-----------------------------------------------------------------------*/
|
---|
4243 | /* Create file system on the logical drive */
|
---|
4244 | /*-----------------------------------------------------------------------*/
|
---|
4245 | #define N_ROOTDIR 512 /* Number of root directory entries for FAT12/16 */
|
---|
4246 | #define N_FATS 1 /* Number of FATs (1 or 2) */
|
---|
4247 |
|
---|
4248 |
|
---|
4249 | FRESULT f_mkfs (
|
---|
4250 | const TCHAR* path, /* Logical drive number */
|
---|
4251 | BYTE sfd, /* Partitioning rule 0:FDISK, 1:SFD */
|
---|
4252 | UINT au /* Size of allocation unit in unit of byte or sector */
|
---|
4253 | )
|
---|
4254 | {
|
---|
4255 | static const WORD vst[] = { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0};
|
---|
4256 | static const WORD cst[] = {32768, 16384, 8192, 4096, 2048, 16384, 8192, 4096, 2048, 1024, 512};
|
---|
4257 | int vol;
|
---|
4258 | BYTE fmt, md, sys, *tbl, pdrv, part;
|
---|
4259 | DWORD n_clst, vs, n, wsect;
|
---|
4260 | UINT i;
|
---|
4261 | DWORD b_vol, b_fat, b_dir, b_data; /* LBA */
|
---|
4262 | DWORD n_vol, n_rsv, n_fat, n_dir; /* Size */
|
---|
4263 | FATFS *fs;
|
---|
4264 | DSTATUS stat;
|
---|
4265 | #if FF_USE_TRIM
|
---|
4266 | DWORD eb[2];
|
---|
4267 | #endif
|
---|
4268 |
|
---|
4269 |
|
---|
4270 | /* Check mounted drive and clear work area */
|
---|
4271 | if (sfd > 1) return FR_INVALID_PARAMETER;
|
---|
4272 | vol = get_ldnumber(&path);
|
---|
4273 | if (vol < 0) return FR_INVALID_DRIVE;
|
---|
4274 | fs = FatFs[vol];
|
---|
4275 | if (!fs) return FR_NOT_ENABLED;
|
---|
4276 | fs->fs_type = 0;
|
---|
4277 | pdrv = LD2PD(vol); /* Physical drive */
|
---|
4278 | part = LD2PT(vol); /* Partition (0:auto detect, 1-4:get from partition table)*/
|
---|
4279 |
|
---|
4280 | /* Get disk statics */
|
---|
4281 | stat = disk_initialize(pdrv);
|
---|
4282 | if (stat & STA_NOINIT) return FR_NOT_READY;
|
---|
4283 | if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
|
---|
4284 | #if FF_MAX_SS != FF_MIN_SS /* Get disk sector size */
|
---|
4285 | if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > FF_MAX_SS || SS(fs) < FF_MIN_SS)
|
---|
4286 | return FR_DISK_ERR;
|
---|
4287 | #endif
|
---|
4288 | if (FF_MULTI_PARTITION && part) {
|
---|
4289 | /* Get partition information from partition table in the MBR */
|
---|
4290 | if (disk_read(pdrv, fs->win, 0, 1) != RES_OK) return FR_DISK_ERR;
|
---|
4291 | if (LD_WORD(fs->win + BS_55AA) != 0xAA55) return FR_MKFS_ABORTED;
|
---|
4292 | tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
|
---|
4293 | if (!tbl[4]) return FR_MKFS_ABORTED; /* No partition? */
|
---|
4294 | b_vol = LD_DWORD(tbl + 8); /* Volume start sector */
|
---|
4295 | n_vol = LD_DWORD(tbl + 12); /* Volume size */
|
---|
4296 | } else {
|
---|
4297 | /* Create a partition in this function */
|
---|
4298 | if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &n_vol) != RES_OK || n_vol < 128)
|
---|
4299 | return FR_DISK_ERR;
|
---|
4300 | b_vol = (sfd) ? 0 : 63; /* Volume start sector */
|
---|
4301 | n_vol -= b_vol; /* Volume size */
|
---|
4302 | }
|
---|
4303 |
|
---|
4304 | if (au & (au - 1)) au = 0;
|
---|
4305 | if (!au) { /* AU auto selection */
|
---|
4306 | vs = n_vol / (2000 / (SS(fs) / 512));
|
---|
4307 | for (i = 0; vs < vst[i]; i++) ;
|
---|
4308 | au = cst[i];
|
---|
4309 | }
|
---|
4310 | if (au >= FF_MIN_SS) au /= SS(fs); /* Number of sectors per cluster */
|
---|
4311 | if (!au) au = 1;
|
---|
4312 | if (au > 128) au = 128;
|
---|
4313 |
|
---|
4314 | /* Pre-compute number of clusters and FAT sub-type */
|
---|
4315 | n_clst = n_vol / au;
|
---|
4316 | fmt = FS_FAT12;
|
---|
4317 | if (n_clst >= MIN_FAT16) fmt = FS_FAT16;
|
---|
4318 | if (n_clst >= MIN_FAT32) fmt = FS_FAT32;
|
---|
4319 |
|
---|
4320 | /* Determine offset and size of FAT structure */
|
---|
4321 | if (fmt == FS_FAT32) {
|
---|
4322 | n_fat = ((n_clst * 4) + 8 + SS(fs) - 1) / SS(fs);
|
---|
4323 | n_rsv = 32;
|
---|
4324 | n_dir = 0;
|
---|
4325 | } else {
|
---|
4326 | n_fat = (fmt == FS_FAT12) ? (n_clst * 3 + 1) / 2 + 3 : (n_clst * 2) + 4;
|
---|
4327 | n_fat = (n_fat + SS(fs) - 1) / SS(fs);
|
---|
4328 | n_rsv = 1;
|
---|
4329 | n_dir = (DWORD)N_ROOTDIR * SZ_DIRE / SS(fs);
|
---|
4330 | }
|
---|
4331 | b_fat = b_vol + n_rsv; /* FAT area start sector */
|
---|
4332 | b_dir = b_fat + n_fat * N_FATS; /* Directory area start sector */
|
---|
4333 | b_data = b_dir + n_dir; /* Data area start sector */
|
---|
4334 | if (n_vol < b_data + au - b_vol) return FR_MKFS_ABORTED; /* Too small volume */
|
---|
4335 |
|
---|
4336 | /* Align data start sector to erase block boundary (for flash memory media) */
|
---|
4337 | if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &n) != RES_OK || !n || n > 32768) n = 1;
|
---|
4338 | n = (b_data + n - 1) & ~(n - 1); /* Next nearest erase block from current data start */
|
---|
4339 | n = (n - b_data) / N_FATS;
|
---|
4340 | if (fmt == FS_FAT32) { /* FAT32: Move FAT offset */
|
---|
4341 | n_rsv += n;
|
---|
4342 | b_fat += n;
|
---|
4343 | } else { /* FAT12/16: Expand FAT size */
|
---|
4344 | n_fat += n;
|
---|
4345 | }
|
---|
4346 |
|
---|
4347 | /* Determine number of clusters and final check of validity of the FAT sub-type */
|
---|
4348 | n_clst = (n_vol - n_rsv - n_fat * N_FATS - n_dir) / au;
|
---|
4349 | if ( (fmt == FS_FAT16 && n_clst < MIN_FAT16)
|
---|
4350 | || (fmt == FS_FAT32 && n_clst < MIN_FAT32))
|
---|
4351 | return FR_MKFS_ABORTED;
|
---|
4352 |
|
---|
4353 | /* Determine system ID in the partition table */
|
---|
4354 | if (fmt == FS_FAT32) {
|
---|
4355 | sys = 0x0C; /* FAT32X */
|
---|
4356 | } else {
|
---|
4357 | if (fmt == FS_FAT12 && n_vol < 0x10000) {
|
---|
4358 | sys = 0x01; /* FAT12(<65536) */
|
---|
4359 | } else {
|
---|
4360 | sys = (n_vol < 0x10000) ? 0x04 : 0x06; /* FAT16(<65536) : FAT12/16(>=65536) */
|
---|
4361 | }
|
---|
4362 | }
|
---|
4363 |
|
---|
4364 | if (FF_MULTI_PARTITION && part) {
|
---|
4365 | /* Update system ID in the partition table */
|
---|
4366 | tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
|
---|
4367 | tbl[4] = sys;
|
---|
4368 | if (disk_write(pdrv, fs->win, 0, 1) != RES_OK) /* Write it to teh MBR */
|
---|
4369 | return FR_DISK_ERR;
|
---|
4370 | md = 0xF8;
|
---|
4371 | } else {
|
---|
4372 | if (sfd) { /* No partition table (SFD) */
|
---|
4373 | md = 0xF0;
|
---|
4374 | } else { /* Create partition table (FDISK) */
|
---|
4375 | mem_set(fs->win, 0, SS(fs));
|
---|
4376 | tbl = fs->win + MBR_Table; /* Create partition table for single partition in the drive */
|
---|
4377 | tbl[1] = 1; /* Partition start head */
|
---|
4378 | tbl[2] = 1; /* Partition start sector */
|
---|
4379 | tbl[3] = 0; /* Partition start cylinder */
|
---|
4380 | tbl[4] = sys; /* System type */
|
---|
4381 | tbl[5] = 254; /* Partition end head */
|
---|
4382 | n = (b_vol + n_vol) / 63 / 255;
|
---|
4383 | tbl[6] = (BYTE)(n >> 2 | 63); /* Partition end sector */
|
---|
4384 | tbl[7] = (BYTE)n; /* End cylinder */
|
---|
4385 | ST_DWORD(tbl + 8, 63); /* Partition start in LBA */
|
---|
4386 | ST_DWORD(tbl + 12, n_vol); /* Partition size in LBA */
|
---|
4387 | ST_WORD(fs->win + BS_55AA, 0xAA55); /* MBR signature */
|
---|
4388 | if (disk_write(pdrv, fs->win, 0, 1) != RES_OK) /* Write it to the MBR */
|
---|
4389 | return FR_DISK_ERR;
|
---|
4390 | md = 0xF8;
|
---|
4391 | }
|
---|
4392 | }
|
---|
4393 |
|
---|
4394 | /* Create BPB in the VBR */
|
---|
4395 | tbl = fs->win; /* Clear sector */
|
---|
4396 | mem_set(tbl, 0, SS(fs));
|
---|
4397 | mem_cpy(tbl, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code, OEM name */
|
---|
4398 | i = SS(fs); /* Sector size */
|
---|
4399 | ST_WORD(tbl + BPB_BytsPerSec, i);
|
---|
4400 | tbl[BPB_SecPerClus] = (BYTE)au; /* Sectors per cluster */
|
---|
4401 | ST_WORD(tbl + BPB_RsvdSecCnt, n_rsv); /* Reserved sectors */
|
---|
4402 | tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */
|
---|
4403 | i = (fmt == FS_FAT32) ? 0 : N_ROOTDIR; /* Number of root directory entries */
|
---|
4404 | ST_WORD(tbl + BPB_RootEntCnt, i);
|
---|
4405 | if (n_vol < 0x10000) { /* Number of total sectors */
|
---|
4406 | ST_WORD(tbl + BPB_TotSec16, n_vol);
|
---|
4407 | } else {
|
---|
4408 | ST_DWORD(tbl + BPB_TotSec32, n_vol);
|
---|
4409 | }
|
---|
4410 | tbl[BPB_Media] = md; /* Media descriptor */
|
---|
4411 | ST_WORD(tbl + BPB_SecPerTrk, 63); /* Number of sectors per track */
|
---|
4412 | ST_WORD(tbl + BPB_NumHeads, 255); /* Number of heads */
|
---|
4413 | ST_DWORD(tbl + BPB_HiddSec, b_vol); /* Hidden sectors */
|
---|
4414 | n = GET_FATTIME(); /* Use current time as VSN */
|
---|
4415 | if (fmt == FS_FAT32) {
|
---|
4416 | ST_DWORD(tbl + BS_VolID32, n); /* VSN */
|
---|
4417 | ST_DWORD(tbl + BPB_FATSz32, n_fat); /* Number of sectors per FAT */
|
---|
4418 | ST_DWORD(tbl + BPB_RootClus, 2); /* Root directory start cluster (2) */
|
---|
4419 | ST_WORD(tbl + BPB_FSInfo, 1); /* FSINFO record offset (VBR + 1) */
|
---|
4420 | ST_WORD(tbl + BPB_BkBootSec, 6); /* Backup boot record offset (VBR + 6) */
|
---|
4421 | tbl[BS_DrvNum32] = 0x80; /* Drive number */
|
---|
4422 | tbl[BS_BootSig32] = 0x29; /* Extended boot signature */
|
---|
4423 | mem_cpy(tbl + BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */
|
---|
4424 | } else {
|
---|
4425 | ST_DWORD(tbl + BS_VolID, n); /* VSN */
|
---|
4426 | ST_WORD(tbl + BPB_FATSz16, n_fat); /* Number of sectors per FAT */
|
---|
4427 | tbl[BS_DrvNum] = 0x80; /* Drive number */
|
---|
4428 | tbl[BS_BootSig] = 0x29; /* Extended boot signature */
|
---|
4429 | mem_cpy(tbl + BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */
|
---|
4430 | }
|
---|
4431 | ST_WORD(tbl + BS_55AA, 0xAA55); /* Signature (Offset is fixed here regardless of sector size) */
|
---|
4432 | if (disk_write(pdrv, tbl, b_vol, 1) != RES_OK) /* Write it to the VBR sector */
|
---|
4433 | return FR_DISK_ERR;
|
---|
4434 | if (fmt == FS_FAT32) /* Write it to the backup VBR if needed (VBR + 6) */
|
---|
4435 | disk_write(pdrv, tbl, b_vol + 6, 1);
|
---|
4436 |
|
---|
4437 | /* Initialize FAT area */
|
---|
4438 | wsect = b_fat;
|
---|
4439 | for (i = 0; i < N_FATS; i++) { /* Initialize each FAT copy */
|
---|
4440 | mem_set(tbl, 0, SS(fs)); /* 1st sector of the FAT */
|
---|
4441 | n = md; /* Media descriptor byte */
|
---|
4442 | if (fmt != FS_FAT32) {
|
---|
4443 | n |= (fmt == FS_FAT12) ? 0x00FFFF00 : 0xFFFFFF00;
|
---|
4444 | ST_DWORD(tbl + 0, n); /* Reserve cluster #0-1 (FAT12/16) */
|
---|
4445 | } else {
|
---|
4446 | n |= 0xFFFFFF00;
|
---|
4447 | ST_DWORD(tbl + 0, n); /* Reserve cluster #0-1 (FAT32) */
|
---|
4448 | ST_DWORD(tbl + 4, 0xFFFFFFFF);
|
---|
4449 | ST_DWORD(tbl + 8, 0x0FFFFFFF); /* Reserve cluster #2 for root directory */
|
---|
4450 | }
|
---|
4451 | if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
|
---|
4452 | return FR_DISK_ERR;
|
---|
4453 | mem_set(tbl, 0, SS(fs)); /* Fill following FAT entries with zero */
|
---|
4454 | for (n = 1; n < n_fat; n++) { /* This loop may take a time on FAT32 volume due to many single sector writes */
|
---|
4455 | if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
|
---|
4456 | return FR_DISK_ERR;
|
---|
4457 | }
|
---|
4458 | }
|
---|
4459 |
|
---|
4460 | /* Initialize root directory */
|
---|
4461 | i = (fmt == FS_FAT32) ? au : (UINT)n_dir;
|
---|
4462 | do {
|
---|
4463 | if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
|
---|
4464 | return FR_DISK_ERR;
|
---|
4465 | } while (--i);
|
---|
4466 |
|
---|
4467 | #if FF_USE_TRIM /* Erase data area if needed */
|
---|
4468 | {
|
---|
4469 | eb[0] = wsect; eb[1] = wsect + (n_clst - ((fmt == FS_FAT32) ? 1 : 0)) * au - 1;
|
---|
4470 | disk_ioctl(pdrv, CTRL_TRIM, eb);
|
---|
4471 | }
|
---|
4472 | #endif
|
---|
4473 |
|
---|
4474 | /* Create FSINFO if needed */
|
---|
4475 | if (fmt == FS_FAT32) {
|
---|
4476 | ST_DWORD(tbl + FSI_LeadSig, 0x41615252);
|
---|
4477 | ST_DWORD(tbl + FSI_StrucSig, 0x61417272);
|
---|
4478 | ST_DWORD(tbl + FSI_Free_Count, n_clst - 1); /* Number of free clusters */
|
---|
4479 | ST_DWORD(tbl + FSI_Nxt_Free, 2); /* Last allocated cluster# */
|
---|
4480 | ST_WORD(tbl + BS_55AA, 0xAA55);
|
---|
4481 | disk_write(pdrv, tbl, b_vol + 1, 1); /* Write original (VBR + 1) */
|
---|
4482 | disk_write(pdrv, tbl, b_vol + 7, 1); /* Write backup (VBR + 7) */
|
---|
4483 | }
|
---|
4484 |
|
---|
4485 | return (disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR;
|
---|
4486 | }
|
---|
4487 |
|
---|
4488 |
|
---|
4489 |
|
---|
4490 | #if FF_MULTI_PARTITION
|
---|
4491 | /*-----------------------------------------------------------------------*/
|
---|
4492 | /* Create partition table on the physical drive */
|
---|
4493 | /*-----------------------------------------------------------------------*/
|
---|
4494 |
|
---|
4495 | FRESULT f_fdisk (
|
---|
4496 | BYTE pdrv, /* Physical drive number */
|
---|
4497 | const DWORD szt[], /* Pointer to the size table for each partitions */
|
---|
4498 | void* work /* Pointer to the working buffer */
|
---|
4499 | )
|
---|
4500 | {
|
---|
4501 | UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl;
|
---|
4502 | BYTE s_hd, e_hd, *p, *buf = (BYTE*)work;
|
---|
4503 | DSTATUS stat;
|
---|
4504 | DWORD sz_disk, sz_part, s_part;
|
---|
4505 |
|
---|
4506 |
|
---|
4507 | stat = disk_initialize(pdrv);
|
---|
4508 | if (stat & STA_NOINIT) return FR_NOT_READY;
|
---|
4509 | if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
|
---|
4510 | if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk)) return FR_DISK_ERR;
|
---|
4511 |
|
---|
4512 | /* Determine CHS in the table regardless of the drive geometry */
|
---|
4513 | for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ;
|
---|
4514 | if (n == 256) n--;
|
---|
4515 | e_hd = n - 1;
|
---|
4516 | sz_cyl = 63 * n;
|
---|
4517 | tot_cyl = sz_disk / sz_cyl;
|
---|
4518 |
|
---|
4519 | /* Create partition table */
|
---|
4520 | mem_set(buf, 0, FF_MAX_SS);
|
---|
4521 | p = buf + MBR_Table; b_cyl = 0;
|
---|
4522 | for (i = 0; i < 4; i++, p += SZ_PTE) {
|
---|
4523 | p_cyl = (szt[i] <= 100U) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl;
|
---|
4524 | if (!p_cyl) continue;
|
---|
4525 | s_part = (DWORD)sz_cyl * b_cyl;
|
---|
4526 | sz_part = (DWORD)sz_cyl * p_cyl;
|
---|
4527 | if (i == 0) { /* Exclude first track of cylinder 0 */
|
---|
4528 | s_hd = 1;
|
---|
4529 | s_part += 63; sz_part -= 63;
|
---|
4530 | } else {
|
---|
4531 | s_hd = 0;
|
---|
4532 | }
|
---|
4533 | e_cyl = b_cyl + p_cyl - 1;
|
---|
4534 | if (e_cyl >= tot_cyl) return FR_INVALID_PARAMETER;
|
---|
4535 |
|
---|
4536 | /* Set partition table */
|
---|
4537 | p[1] = s_hd; /* Start head */
|
---|
4538 | p[2] = (BYTE)((b_cyl >> 2) + 1); /* Start sector */
|
---|
4539 | p[3] = (BYTE)b_cyl; /* Start cylinder */
|
---|
4540 | p[4] = 0x06; /* System type (temporary setting) */
|
---|
4541 | p[5] = e_hd; /* End head */
|
---|
4542 | p[6] = (BYTE)((e_cyl >> 2) + 63); /* End sector */
|
---|
4543 | p[7] = (BYTE)e_cyl; /* End cylinder */
|
---|
4544 | ST_DWORD(p + 8, s_part); /* Start sector in LBA */
|
---|
4545 | ST_DWORD(p + 12, sz_part); /* Partition size */
|
---|
4546 |
|
---|
4547 | /* Next partition */
|
---|
4548 | b_cyl += p_cyl;
|
---|
4549 | }
|
---|
4550 | ST_WORD(p, 0xAA55);
|
---|
4551 |
|
---|
4552 | /* Write it to the MBR */
|
---|
4553 | return (disk_write(pdrv, buf, 0, 1) != RES_OK || disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) ? FR_DISK_ERR : FR_OK;
|
---|
4554 | }
|
---|
4555 |
|
---|
4556 |
|
---|
4557 | #endif /* FF_MULTI_PARTITION */
|
---|
4558 | #endif /* FF_USE_MKFS && !FF_FS_READONLY */
|
---|
4559 |
|
---|
4560 |
|
---|
4561 |
|
---|
4562 |
|
---|
4563 | #if FF_USE_STRFUNC
|
---|
4564 | /*-----------------------------------------------------------------------*/
|
---|
4565 | /* Get a string from the file */
|
---|
4566 | /*-----------------------------------------------------------------------*/
|
---|
4567 |
|
---|
4568 | TCHAR* f_gets (
|
---|
4569 | TCHAR* buff, /* Pointer to the string buffer to read */
|
---|
4570 | int len, /* Size of string buffer (characters) */
|
---|
4571 | FIL* fp /* Pointer to the file object */
|
---|
4572 | )
|
---|
4573 | {
|
---|
4574 | int n = 0;
|
---|
4575 | TCHAR c, *p = buff;
|
---|
4576 | BYTE s[2];
|
---|
4577 | UINT rc;
|
---|
4578 |
|
---|
4579 |
|
---|
4580 | while (n < len - 1) { /* Read characters until buffer gets filled */
|
---|
4581 | #if FF_USE_LFN && FF_LFN_UNICODE
|
---|
4582 | #if FF_STRF_ENCODE == 3 /* Read a character in UTF-8 */
|
---|
4583 | f_read(fp, s, 1, &rc);
|
---|
4584 | if (rc != 1) break;
|
---|
4585 | c = s[0];
|
---|
4586 | if (c >= 0x80) {
|
---|
4587 | if (c < 0xC0) continue; /* Skip stray trailer */
|
---|
4588 | if (c < 0xE0) { /* Two-byte sequence */
|
---|
4589 | f_read(fp, s, 1, &rc);
|
---|
4590 | if (rc != 1) break;
|
---|
4591 | c = (c & 0x1F) << 6 | (s[0] & 0x3F);
|
---|
4592 | if (c < 0x80) c = '?';
|
---|
4593 | } else {
|
---|
4594 | if (c < 0xF0) { /* Three-byte sequence */
|
---|
4595 | f_read(fp, s, 2, &rc);
|
---|
4596 | if (rc != 2) break;
|
---|
4597 | c = c << 12 | (s[0] & 0x3F) << 6 | (s[1] & 0x3F);
|
---|
4598 | if (c < 0x800) c = '?';
|
---|
4599 | } else { /* Reject four-byte sequence */
|
---|
4600 | c = '?';
|
---|
4601 | }
|
---|
4602 | }
|
---|
4603 | }
|
---|
4604 | #elif FF_STRF_ENCODE == 2 /* Read a character in UTF-16BE */
|
---|
4605 | f_read(fp, s, 2, &rc);
|
---|
4606 | if (rc != 2) break;
|
---|
4607 | c = s[1] + (s[0] << 8);
|
---|
4608 | #elif FF_STRF_ENCODE == 1 /* Read a character in UTF-16LE */
|
---|
4609 | f_read(fp, s, 2, &rc);
|
---|
4610 | if (rc != 2) break;
|
---|
4611 | c = s[0] + (s[1] << 8);
|
---|
4612 | #else /* Read a character in ANSI/OEM */
|
---|
4613 | f_read(fp, s, 1, &rc);
|
---|
4614 | if (rc != 1) break;
|
---|
4615 | c = s[0];
|
---|
4616 | if (IsDBCS1(c)) {
|
---|
4617 | f_read(fp, s, 1, &rc);
|
---|
4618 | if (rc != 1) break;
|
---|
4619 | c = (c << 8) + s[0];
|
---|
4620 | }
|
---|
4621 | c = ff_convert(c, 1); /* OEM -> Unicode */
|
---|
4622 | if (!c) c = '?';
|
---|
4623 | #endif
|
---|
4624 | #else /* Read a character without conversion */
|
---|
4625 | f_read(fp, s, 1, &rc);
|
---|
4626 | if (rc != 1) break;
|
---|
4627 | c = s[0];
|
---|
4628 | #endif
|
---|
4629 | if (FF_USE_STRFUNC == 2 && c == '\r') continue; /* Strip '\r' */
|
---|
4630 | *p++ = c;
|
---|
4631 | n++;
|
---|
4632 | if (c == '\n') break; /* Break on EOL */
|
---|
4633 | }
|
---|
4634 | *p = 0;
|
---|
|
---|