507 |
kaklik |
1 |
/*----------------------------------------------------------------------------/
|
|
|
2 |
/ FatFs - Tiny FAT file system module R0.06 (C)ChaN, 2008
|
|
|
3 |
/-----------------------------------------------------------------------------/
|
|
|
4 |
/ The FatFs module is an experimenal project to implement FAT file system to
|
|
|
5 |
/ cheap microcontrollers. This is a free software and is opened for education,
|
|
|
6 |
/ research and development under license policy of following trems.
|
|
|
7 |
/
|
|
|
8 |
/ Copyright (C) 2008, ChaN, all right reserved.
|
|
|
9 |
/
|
|
|
10 |
/ * The FatFs module is a free software and there is no warranty.
|
|
|
11 |
/ * You can use, modify and/or redistribute it for personal, non-profit or
|
|
|
12 |
/ commercial use without any restriction under your responsibility.
|
|
|
13 |
/ * Redistributions of source code must retain the above copyright notice.
|
|
|
14 |
/
|
|
|
15 |
/-----------------------------------------------------------------------------/
|
|
|
16 |
/ Feb 26,'06 R0.00 Prototype.
|
|
|
17 |
/
|
|
|
18 |
/ Apr 29,'06 R0.01 First stable version.
|
|
|
19 |
/
|
|
|
20 |
/ Jun 01,'06 R0.02 Added FAT12 support.
|
|
|
21 |
/ Removed unbuffered mode.
|
|
|
22 |
/ Fixed a problem on small (<32M) patition.
|
|
|
23 |
/ Jun 10,'06 R0.02a Added a configuration option (_FS_MINIMUM).
|
|
|
24 |
/
|
|
|
25 |
/ Sep 22,'06 R0.03 Added f_rename().
|
|
|
26 |
/ Changed option _FS_MINIMUM to _FS_MINIMIZE.
|
|
|
27 |
/ Dec 09,'06 R0.03a Improved cluster scan algolithm to write files fast.
|
|
|
28 |
/
|
|
|
29 |
/ Feb 04,'07 R0.04 Added FAT32 supprt.
|
|
|
30 |
/ Changed some interfaces incidental to FatFs.
|
|
|
31 |
/ Changed f_mountdrv() to f_mount().
|
|
|
32 |
/ Apr 01,'07 R0.04a Added a capability of extending file size to f_lseek().
|
|
|
33 |
/ Added minimization level 3.
|
|
|
34 |
/ Fixed a problem in FAT32 support.
|
|
|
35 |
/ May 05,'07 R0.04b Added a configuration option _USE_NTFLAG.
|
|
|
36 |
/ Added FSInfo support.
|
|
|
37 |
/ Fixed some problems corresponds to FAT32 support.
|
|
|
38 |
/ Fixed DBCS name can result FR_INVALID_NAME.
|
|
|
39 |
/ Fixed short seek (<= csize) collapses the file object.
|
|
|
40 |
/
|
|
|
41 |
/ Aug 25,'07 R0.05 Changed arguments of f_read() and f_write().
|
|
|
42 |
/ Feb 03,'08 R0.05a Added f_truncate() and f_utime().
|
|
|
43 |
/ Fixed off by one error at FAT sub-type determination.
|
|
|
44 |
/ Fixed btr in f_read() can be mistruncated.
|
|
|
45 |
/ Fixed cached sector is not flushed when create and close
|
|
|
46 |
/ without write.
|
|
|
47 |
/
|
|
|
48 |
/ Apr 01,'08 R0.06 Added f_forward(), fputc(), fputs(), fprintf() and fgets().
|
|
|
49 |
/ Improved performance of f_lseek() on moving to the same
|
|
|
50 |
/ or following cluster.
|
|
|
51 |
/----------------------------------------------------------------------------*/
|
|
|
52 |
|
|
|
53 |
#include <string.h>
|
|
|
54 |
#include "tff.h" /* Tiny-FatFs declarations */
|
|
|
55 |
#include "diskio.h" /* Include file for user provided disk functions */
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
static
|
|
|
59 |
FATFS *FatFs; /* Pointer to the file system objects (logical drive) */
|
|
|
60 |
static
|
|
|
61 |
WORD fsid; /* File system mount ID */
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
/*-------------------------------------------------------------------------
|
|
|
65 |
|
|
|
66 |
Module Private Functions
|
|
|
67 |
|
|
|
68 |
-------------------------------------------------------------------------*/
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
/*-----------------------------------------------------------------------*/
|
|
|
72 |
/* Change window offset */
|
|
|
73 |
/*-----------------------------------------------------------------------*/
|
|
|
74 |
|
|
|
75 |
static
|
|
|
76 |
BOOL move_window ( /* TRUE: successful, FALSE: failed */
|
|
|
77 |
DWORD sector /* Sector number to make apperance in the FatFs->win */
|
|
|
78 |
) /* Move to zero only writes back dirty window */
|
|
|
79 |
{
|
|
|
80 |
DWORD wsect;
|
|
|
81 |
FATFS *fs = FatFs;
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
wsect = fs->winsect;
|
|
|
85 |
if (wsect != sector) { /* Changed current window */
|
|
|
86 |
#if !_FS_READONLY
|
|
|
87 |
BYTE n;
|
|
|
88 |
if (fs->winflag) { /* Write back dirty window if needed */
|
|
|
89 |
if (disk_write(0, fs->win, wsect, 1) != RES_OK)
|
|
|
90 |
return FALSE;
|
|
|
91 |
fs->winflag = 0;
|
|
|
92 |
if (wsect < (fs->fatbase + fs->sects_fat)) { /* In FAT area */
|
|
|
93 |
for (n = fs->n_fats; n >= 2; n--) { /* Refrect the change to all FAT copies */
|
|
|
94 |
wsect += fs->sects_fat;
|
|
|
95 |
disk_write(0, fs->win, wsect, 1);
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
#endif
|
|
|
100 |
if (sector) {
|
|
|
101 |
if (disk_read(0, fs->win, sector, 1) != RES_OK)
|
|
|
102 |
return FALSE;
|
|
|
103 |
fs->winsect = sector;
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
return TRUE;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
/*-----------------------------------------------------------------------*/
|
|
|
113 |
/* Clean-up cached data */
|
|
|
114 |
/*-----------------------------------------------------------------------*/
|
|
|
115 |
|
|
|
116 |
#if !_FS_READONLY
|
|
|
117 |
static
|
|
|
118 |
FRESULT sync (void) /* FR_OK: successful, FR_RW_ERROR: failed */
|
|
|
119 |
{
|
|
|
120 |
FATFS *fs = FatFs;
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
fs->winflag = 1;
|
|
|
124 |
if (!move_window(0)) return FR_RW_ERROR;
|
|
|
125 |
#if _USE_FSINFO
|
|
|
126 |
/* Update FSInfo sector if needed */
|
|
|
127 |
if (fs->fs_type == FS_FAT32 && fs->fsi_flag) {
|
|
|
128 |
fs->winsect = 0;
|
|
|
129 |
memset(fs->win, 0, 512U);
|
|
|
130 |
ST_WORD(&fs->win[BS_55AA], 0xAA55);
|
|
|
131 |
ST_DWORD(&fs->win[FSI_LeadSig], 0x41615252);
|
|
|
132 |
ST_DWORD(&fs->win[FSI_StrucSig], 0x61417272);
|
|
|
133 |
ST_DWORD(&fs->win[FSI_Free_Count], fs->free_clust);
|
|
|
134 |
ST_DWORD(&fs->win[FSI_Nxt_Free], fs->last_clust);
|
|
|
135 |
disk_write(0, fs->win, fs->fsi_sector, 1);
|
|
|
136 |
fs->fsi_flag = 0;
|
|
|
137 |
}
|
|
|
138 |
#endif
|
|
|
139 |
/* Make sure that no pending write process in the physical drive */
|
|
|
140 |
if (disk_ioctl(0, CTRL_SYNC, NULL) != RES_OK)
|
|
|
141 |
return FR_RW_ERROR;
|
|
|
142 |
return FR_OK;
|
|
|
143 |
}
|
|
|
144 |
#endif
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
/*-----------------------------------------------------------------------*/
|
|
|
150 |
/* Get a cluster status */
|
|
|
151 |
/*-----------------------------------------------------------------------*/
|
|
|
152 |
|
|
|
153 |
static
|
|
|
154 |
CLUST get_cluster ( /* 0,>=2: successful, 1: failed */
|
|
|
155 |
CLUST clust /* Cluster# to get the link information */
|
|
|
156 |
)
|
|
|
157 |
{
|
|
|
158 |
WORD wc, bc;
|
|
|
159 |
DWORD fatsect;
|
|
|
160 |
FATFS *fs = FatFs;
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
if (clust >= 2 && clust < fs->max_clust) { /* Valid cluster# */
|
|
|
164 |
fatsect = fs->fatbase;
|
|
|
165 |
switch (fs->fs_type) {
|
|
|
166 |
case FS_FAT12 :
|
|
|
167 |
bc = (WORD)clust * 3 / 2;
|
|
|
168 |
if (!move_window(fatsect + bc / 512U)) break;
|
|
|
169 |
wc = fs->win[bc % 512U]; bc++;
|
|
|
170 |
if (!move_window(fatsect + bc / 512U)) break;
|
|
|
171 |
wc |= (WORD)fs->win[bc % 512U] << 8;
|
|
|
172 |
return (clust & 1) ? (wc >> 4) : (wc & 0xFFF);
|
|
|
173 |
|
|
|
174 |
case FS_FAT16 :
|
|
|
175 |
if (!move_window(fatsect + clust / 256)) break;
|
|
|
176 |
return LD_WORD(&fs->win[((WORD)clust * 2) % 512U]);
|
|
|
177 |
#if _FAT32
|
|
|
178 |
case FS_FAT32 :
|
|
|
179 |
if (!move_window(fatsect + clust / 128)) break;
|
|
|
180 |
return LD_DWORD(&fs->win[((WORD)clust * 4) % 512U]) & 0x0FFFFFFF;
|
|
|
181 |
#endif
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
return 1; /* Out of cluster range, or an error occured */
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
/*-----------------------------------------------------------------------*/
|
|
|
192 |
/* Change a cluster status */
|
|
|
193 |
/*-----------------------------------------------------------------------*/
|
|
|
194 |
|
|
|
195 |
#if !_FS_READONLY
|
|
|
196 |
static
|
|
|
197 |
BOOL put_cluster ( /* TRUE: successful, FALSE: failed */
|
|
|
198 |
CLUST clust, /* Cluster# to change (must be 2 to fs->max_clust-1) */
|
|
|
199 |
CLUST val /* New value to mark the cluster */
|
|
|
200 |
)
|
|
|
201 |
{
|
|
|
202 |
WORD bc;
|
|
|
203 |
BYTE *p;
|
|
|
204 |
DWORD fatsect;
|
|
|
205 |
FATFS *fs = FatFs;
|
|
|
206 |
|
|
|
207 |
|
|
|
208 |
fatsect = fs->fatbase;
|
|
|
209 |
switch (fs->fs_type) {
|
|
|
210 |
case FS_FAT12 :
|
|
|
211 |
bc = (WORD)clust * 3 / 2;
|
|
|
212 |
if (!move_window(fatsect + bc / 512U)) return FALSE;
|
|
|
213 |
p = &fs->win[bc % 512U];
|
|
|
214 |
*p = (clust & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
|
|
|
215 |
bc++;
|
|
|
216 |
fs->winflag = 1;
|
|
|
217 |
if (!move_window(fatsect + bc / 512U)) return FALSE;
|
|
|
218 |
p = &fs->win[bc % 512U];
|
|
|
219 |
*p = (clust & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
|
|
|
220 |
break;
|
|
|
221 |
|
|
|
222 |
case FS_FAT16 :
|
|
|
223 |
if (!move_window(fatsect + clust / 256)) return FALSE;
|
|
|
224 |
ST_WORD(&fs->win[((WORD)clust * 2) % 512U], (WORD)val);
|
|
|
225 |
break;
|
|
|
226 |
#if _FAT32
|
|
|
227 |
case FS_FAT32 :
|
|
|
228 |
if (!move_window(fatsect + clust / 128)) return FALSE;
|
|
|
229 |
ST_DWORD(&fs->win[((WORD)clust * 4) % 512U], val);
|
|
|
230 |
break;
|
|
|
231 |
#endif
|
|
|
232 |
default :
|
|
|
233 |
return FALSE;
|
|
|
234 |
}
|
|
|
235 |
fs->winflag = 1;
|
|
|
236 |
return TRUE;
|
|
|
237 |
}
|
|
|
238 |
#endif /* !_FS_READONLY */
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
/*-----------------------------------------------------------------------*/
|
|
|
244 |
/* Remove a cluster chain */
|
|
|
245 |
/*-----------------------------------------------------------------------*/
|
|
|
246 |
|
|
|
247 |
#if !_FS_READONLY
|
|
|
248 |
static
|
|
|
249 |
BOOL remove_chain ( /* TRUE: successful, FALSE: failed */
|
|
|
250 |
CLUST clust /* Cluster# to remove chain from */
|
|
|
251 |
)
|
|
|
252 |
{
|
|
|
253 |
CLUST nxt;
|
|
|
254 |
FATFS *fs = FatFs;
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
while (clust >= 2 && clust < fs->max_clust) {
|
|
|
258 |
nxt = get_cluster(clust);
|
|
|
259 |
if (nxt == 1) return FALSE;
|
|
|
260 |
if (!put_cluster(clust, 0)) return FALSE;
|
|
|
261 |
if (fs->free_clust != (CLUST)0xFFFFFFFF) {
|
|
|
262 |
fs->free_clust++;
|
|
|
263 |
#if _USE_FSINFO
|
|
|
264 |
fs->fsi_flag = 1;
|
|
|
265 |
#endif
|
|
|
266 |
}
|
|
|
267 |
clust = nxt;
|
|
|
268 |
}
|
|
|
269 |
return TRUE;
|
|
|
270 |
}
|
|
|
271 |
#endif
|
|
|
272 |
|
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
/*-----------------------------------------------------------------------*/
|
|
|
277 |
/* Stretch or create a cluster chain */
|
|
|
278 |
/*-----------------------------------------------------------------------*/
|
|
|
279 |
|
|
|
280 |
#if !_FS_READONLY
|
|
|
281 |
static
|
|
|
282 |
CLUST create_chain ( /* 0: No free cluster, 1: Error, >=2: New cluster number */
|
|
|
283 |
CLUST clust /* Cluster# to stretch, 0 means create new */
|
|
|
284 |
)
|
|
|
285 |
{
|
|
|
286 |
CLUST cstat, ncl, scl, mcl;
|
|
|
287 |
FATFS *fs = FatFs;
|
|
|
288 |
|
|
|
289 |
|
|
|
290 |
mcl = fs->max_clust;
|
|
|
291 |
if (clust == 0) { /* Create new chain */
|
|
|
292 |
scl = fs->last_clust; /* Get last allocated cluster */
|
|
|
293 |
if (scl < 2 || scl >= mcl) scl = 1;
|
|
|
294 |
}
|
|
|
295 |
else { /* Stretch existing chain */
|
|
|
296 |
cstat = get_cluster(clust); /* Check the cluster status */
|
|
|
297 |
if (cstat < 2) return 1; /* It is an invalid cluster */
|
|
|
298 |
if (cstat < mcl) return cstat; /* It is already followed by next cluster */
|
|
|
299 |
scl = clust;
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
ncl = scl; /* Start cluster */
|
|
|
303 |
for (;;) {
|
|
|
304 |
ncl++; /* Next cluster */
|
|
|
305 |
if (ncl >= mcl) { /* Wrap around */
|
|
|
306 |
ncl = 2;
|
|
|
307 |
if (ncl > scl) return 0; /* No free custer */
|
|
|
308 |
}
|
|
|
309 |
cstat = get_cluster(ncl); /* Get the cluster status */
|
|
|
310 |
if (cstat == 0) break; /* Found a free cluster */
|
|
|
311 |
if (cstat == 1) return 1; /* Any error occured */
|
|
|
312 |
if (ncl == scl) return 0; /* No free custer */
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
if (!put_cluster(ncl, (CLUST)0x0FFFFFFF)) return 1; /* Mark the new cluster "in use" */
|
|
|
316 |
if (clust != 0 && !put_cluster(clust, ncl)) return 1; /* Link it to previous one if needed */
|
|
|
317 |
|
|
|
318 |
fs->last_clust = ncl; /* Update fsinfo */
|
|
|
319 |
if (fs->free_clust != (CLUST)0xFFFFFFFF) {
|
|
|
320 |
fs->free_clust--;
|
|
|
321 |
#if _USE_FSINFO
|
|
|
322 |
fs->fsi_flag = 1;
|
|
|
323 |
#endif
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
return ncl; /* Return new cluster number */
|
|
|
327 |
}
|
|
|
328 |
#endif /* !_FS_READONLY */
|
|
|
329 |
|
|
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
|
333 |
/*-----------------------------------------------------------------------*/
|
|
|
334 |
/* Get sector# from cluster# */
|
|
|
335 |
/*-----------------------------------------------------------------------*/
|
|
|
336 |
|
|
|
337 |
static
|
|
|
338 |
DWORD clust2sect ( /* !=0: sector number, 0: failed - invalid cluster# */
|
|
|
339 |
CLUST clust /* Cluster# to be converted */
|
|
|
340 |
)
|
|
|
341 |
{
|
|
|
342 |
FATFS *fs = FatFs;
|
|
|
343 |
|
|
|
344 |
|
|
|
345 |
clust -= 2;
|
|
|
346 |
if (clust >= (fs->max_clust - 2)) return 0; /* Invalid cluster# */
|
|
|
347 |
return (DWORD)clust * fs->csize + fs->database;
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
|
|
|
351 |
|
|
|
352 |
|
|
|
353 |
/*-----------------------------------------------------------------------*/
|
|
|
354 |
/* Move directory pointer to next */
|
|
|
355 |
/*-----------------------------------------------------------------------*/
|
|
|
356 |
|
|
|
357 |
static
|
|
|
358 |
BOOL next_dir_entry ( /* TRUE: successful, FALSE: could not move next */
|
|
|
359 |
DIR *dj /* Pointer to directory object */
|
|
|
360 |
)
|
|
|
361 |
{
|
|
|
362 |
CLUST clust;
|
|
|
363 |
WORD idx;
|
|
|
364 |
|
|
|
365 |
|
|
|
366 |
idx = dj->index + 1;
|
|
|
367 |
if ((idx & 15) == 0) { /* Table sector changed? */
|
|
|
368 |
dj->sect++; /* Next sector */
|
|
|
369 |
if (dj->clust == 0) { /* In static table */
|
|
|
370 |
if (idx >= dj->fs->n_rootdir) return FALSE; /* Reached to end of table */
|
|
|
371 |
} else { /* In dynamic table */
|
|
|
372 |
if (((idx / 16) & (dj->fs->csize - 1)) == 0) { /* Cluster changed? */
|
|
|
373 |
clust = get_cluster(dj->clust); /* Get next cluster */
|
|
|
374 |
if (clust < 2 || clust >= dj->fs->max_clust) /* Reached to end of table */
|
|
|
375 |
return FALSE;
|
|
|
376 |
dj->clust = clust; /* Initialize for new cluster */
|
|
|
377 |
dj->sect = clust2sect(clust);
|
|
|
378 |
}
|
|
|
379 |
}
|
|
|
380 |
}
|
|
|
381 |
dj->index = idx; /* Lower 4 bit of dj->index indicates offset in dj->sect */
|
|
|
382 |
return TRUE;
|
|
|
383 |
}
|
|
|
384 |
|
|
|
385 |
|
|
|
386 |
|
|
|
387 |
|
|
|
388 |
/*-----------------------------------------------------------------------*/
|
|
|
389 |
/* Get file status from directory entry */
|
|
|
390 |
/*-----------------------------------------------------------------------*/
|
|
|
391 |
|
|
|
392 |
#if _FS_MINIMIZE <= 1
|
|
|
393 |
static
|
|
|
394 |
void get_fileinfo ( /* No return code */
|
|
|
395 |
FILINFO *finfo, /* Ptr to store the File Information */
|
|
|
396 |
const BYTE *dir /* Ptr to the directory entry */
|
|
|
397 |
)
|
|
|
398 |
{
|
|
|
399 |
BYTE n, c, a;
|
|
|
400 |
char *p;
|
|
|
401 |
|
|
|
402 |
|
|
|
403 |
p = &finfo->fname[0];
|
|
|
404 |
a = _USE_NTFLAG ? dir[DIR_NTres] : 0; /* NT flag */
|
|
|
405 |
for (n = 0; n < 8; n++) { /* Convert file name (body) */
|
|
|
406 |
c = dir[n];
|
|
|
407 |
if (c == ' ') break;
|
|
|
408 |
if (c == 0x05) c = 0xE5;
|
|
|
409 |
if (a & 0x08 && c >= 'A' && c <= 'Z') c += 0x20;
|
|
|
410 |
*p++ = c;
|
|
|
411 |
}
|
|
|
412 |
if (dir[8] != ' ') { /* Convert file name (extension) */
|
|
|
413 |
*p++ = '.';
|
|
|
414 |
for (n = 8; n < 11; n++) {
|
|
|
415 |
c = dir[n];
|
|
|
416 |
if (c == ' ') break;
|
|
|
417 |
if (a & 0x10 && c >= 'A' && c <= 'Z') c += 0x20;
|
|
|
418 |
*p++ = c;
|
|
|
419 |
}
|
|
|
420 |
}
|
|
|
421 |
*p = '\0';
|
|
|
422 |
|
|
|
423 |
finfo->fattrib = dir[DIR_Attr]; /* Attribute */
|
|
|
424 |
finfo->fsize = LD_DWORD(&dir[DIR_FileSize]); /* Size */
|
|
|
425 |
finfo->fdate = LD_WORD(&dir[DIR_WrtDate]); /* Date */
|
|
|
426 |
finfo->ftime = LD_WORD(&dir[DIR_WrtTime]); /* Time */
|
|
|
427 |
}
|
|
|
428 |
#endif /* _FS_MINIMIZE <= 1 */
|
|
|
429 |
|
|
|
430 |
|
|
|
431 |
|
|
|
432 |
|
|
|
433 |
/*-----------------------------------------------------------------------*/
|
|
|
434 |
/* Pick a paragraph and create the name in format of directory entry */
|
|
|
435 |
/*-----------------------------------------------------------------------*/
|
|
|
436 |
|
|
|
437 |
static
|
|
|
438 |
char make_dirfile ( /* 1: error - detected an invalid format, '\0'or'/': next character */
|
|
|
439 |
const char **path, /* Pointer to the file path pointer */
|
|
|
440 |
char *dirname /* Pointer to directory name buffer {Name(8), Ext(3), NT flag(1)} */
|
|
|
441 |
)
|
|
|
442 |
{
|
|
|
443 |
BYTE n, t, c, a, b;
|
|
|
444 |
|
|
|
445 |
|
|
|
446 |
memset(dirname, ' ', 8+3); /* Fill buffer with spaces */
|
|
|
447 |
a = 0; b = 0x18; /* NT flag */
|
|
|
448 |
n = 0; t = 8;
|
|
|
449 |
for (;;) {
|
|
|
450 |
c = *(*path)++;
|
|
|
451 |
if (c == '\0' || c == '/') { /* Reached to end of str or directory separator */
|
|
|
452 |
if (n == 0) break;
|
|
|
453 |
dirname[11] = _USE_NTFLAG ? (a & b) : 0;
|
|
|
454 |
return c;
|
|
|
455 |
}
|
|
|
456 |
if (c <= ' ' || c == 0x7F) break; /* Reject invisible chars */
|
|
|
457 |
if (c == '.') {
|
|
|
458 |
if (!(a & 1) && n >= 1 && n <= 8) { /* Enter extension part */
|
|
|
459 |
n = 8; t = 11; continue;
|
|
|
460 |
}
|
|
|
461 |
break;
|
|
|
462 |
}
|
|
|
463 |
if (_USE_SJIS &&
|
|
|
464 |
((c >= 0x81 && c <= 0x9F) || /* Accept S-JIS code */
|
|
|
465 |
(c >= 0xE0 && c <= 0xFC))) {
|
|
|
466 |
if (n == 0 && c == 0xE5) /* Change heading \xE5 to \x05 */
|
|
|
467 |
c = 0x05;
|
|
|
468 |
a ^= 1; goto md_l2;
|
|
|
469 |
}
|
|
|
470 |
if (c == '"') break; /* Reject " */
|
|
|
471 |
if (c <= ')') goto md_l1; /* Accept ! # $ % & ' ( ) */
|
|
|
472 |
if (c <= ',') break; /* Reject * + , */
|
|
|
473 |
if (c <= '9') goto md_l1; /* Accept - 0-9 */
|
|
|
474 |
if (c <= '?') break; /* Reject : ; < = > ? */
|
|
|
475 |
if (!(a & 1)) { /* These checks are not applied to S-JIS 2nd byte */
|
|
|
476 |
if (c == '|') break; /* Reject | */
|
|
|
477 |
if (c >= '[' && c <= ']') break;/* Reject [ \ ] */
|
|
|
478 |
if (_USE_NTFLAG && c >= 'A' && c <= 'Z')
|
|
|
479 |
(t == 8) ? (b &= 0xF7) : (b &= 0xEF);
|
|
|
480 |
if (c >= 'a' && c <= 'z') { /* Convert to upper case */
|
|
|
481 |
c -= 0x20;
|
|
|
482 |
if (_USE_NTFLAG) (t == 8) ? (a |= 0x08) : (a |= 0x10);
|
|
|
483 |
}
|
|
|
484 |
}
|
|
|
485 |
md_l1:
|
|
|
486 |
a &= 0xFE;
|
|
|
487 |
md_l2:
|
|
|
488 |
if (n >= t) break;
|
|
|
489 |
dirname[n++] = c;
|
|
|
490 |
}
|
|
|
491 |
return 1;
|
|
|
492 |
}
|
|
|
493 |
|
|
|
494 |
|
|
|
495 |
|
|
|
496 |
/*-----------------------------------------------------------------------*/
|
|
|
497 |
/* Trace a file path */
|
|
|
498 |
/*-----------------------------------------------------------------------*/
|
|
|
499 |
|
|
|
500 |
static
|
|
|
501 |
FRESULT trace_path ( /* FR_OK(0): successful, !=0: error code */
|
|
|
502 |
DIR *dj, /* Pointer to directory object to return last directory */
|
|
|
503 |
char *fn, /* Pointer to last segment name to return */
|
|
|
504 |
const char *path, /* Full-path string to trace a file or directory */
|
|
|
505 |
BYTE **dir /* Pointer to pointer to found entry to retutn */
|
|
|
506 |
)
|
|
|
507 |
{
|
|
|
508 |
CLUST clust;
|
|
|
509 |
char ds;
|
|
|
510 |
BYTE *dptr = NULL;
|
|
|
511 |
FATFS *fs = FatFs;
|
|
|
512 |
|
|
|
513 |
/* Initialize directory object */
|
|
|
514 |
dj->fs = fs;
|
|
|
515 |
clust = fs->dirbase;
|
|
|
516 |
#if _FAT32
|
|
|
517 |
if (fs->fs_type == FS_FAT32) {
|
|
|
518 |
dj->clust = dj->sclust = clust;
|
|
|
519 |
dj->sect = clust2sect(clust);
|
|
|
520 |
} else
|
|
|
521 |
#endif
|
|
|
522 |
{
|
|
|
523 |
dj->clust = dj->sclust = 0;
|
|
|
524 |
dj->sect = clust;
|
|
|
525 |
}
|
|
|
526 |
dj->index = 0;
|
|
|
527 |
|
|
|
528 |
if (*path == '\0') { /* Null path means the root directory */
|
|
|
529 |
*dir = NULL; return FR_OK;
|
|
|
530 |
}
|
|
|
531 |
|
|
|
532 |
for (;;) {
|
|
|
533 |
ds = make_dirfile(&path, fn); /* Get a paragraph into fn[] */
|
|
|
534 |
if (ds == 1) return FR_INVALID_NAME;
|
|
|
535 |
for (;;) {
|
|
|
536 |
if (!move_window(dj->sect)) return FR_RW_ERROR;
|
|
|
537 |
dptr = &fs->win[(dj->index & 15) * 32]; /* Pointer to the directory entry */
|
|
|
538 |
if (dptr[DIR_Name] == 0) /* Has it reached to end of dir? */
|
|
|
539 |
return !ds ? FR_NO_FILE : FR_NO_PATH;
|
|
|
540 |
if (dptr[DIR_Name] != 0xE5 /* Matched? */
|
|
|
541 |
&& !(dptr[DIR_Attr] & AM_VOL)
|
|
|
542 |
&& !memcmp(&dptr[DIR_Name], fn, 8+3) ) break;
|
|
|
543 |
if (!next_dir_entry(dj)) /* Next directory pointer */
|
|
|
544 |
return !ds ? FR_NO_FILE : FR_NO_PATH;
|
|
|
545 |
}
|
|
|
546 |
if (!ds) { *dir = dptr; return FR_OK; } /* Matched with end of path */
|
|
|
547 |
if (!(dptr[DIR_Attr] & AM_DIR)) return FR_NO_PATH; /* Cannot trace because it is a file */
|
|
|
548 |
clust = /* Get cluster# of the directory */
|
|
|
549 |
#if _FAT32
|
|
|
550 |
((DWORD)LD_WORD(&dptr[DIR_FstClusHI]) << 16) |
|
|
|
551 |
#endif
|
|
|
552 |
LD_WORD(&dptr[DIR_FstClusLO]);
|
|
|
553 |
dj->clust = dj->sclust = clust; /* Restart scannig with the new directory */
|
|
|
554 |
dj->sect = clust2sect(clust);
|
|
|
555 |
dj->index = 2;
|
|
|
556 |
}
|
|
|
557 |
}
|
|
|
558 |
|
|
|
559 |
|
|
|
560 |
|
|
|
561 |
/*-----------------------------------------------------------------------*/
|
|
|
562 |
/* Reserve a directory entry */
|
|
|
563 |
/*-----------------------------------------------------------------------*/
|
|
|
564 |
|
|
|
565 |
#if !_FS_READONLY
|
|
|
566 |
static
|
|
|
567 |
FRESULT reserve_direntry ( /* FR_OK: successful, FR_DENIED: no free entry, FR_RW_ERROR: a disk error occured */
|
|
|
568 |
DIR *dj, /* Target directory to create new entry */
|
|
|
569 |
BYTE **dir /* Pointer to pointer to created entry to retutn */
|
|
|
570 |
)
|
|
|
571 |
{
|
|
|
572 |
CLUST clust;
|
|
|
573 |
DWORD sector;
|
|
|
574 |
BYTE c, n, *dptr;
|
|
|
575 |
FATFS *fs = dj->fs;
|
|
|
576 |
|
|
|
577 |
|
|
|
578 |
/* Re-initialize directory object */
|
|
|
579 |
clust = dj->sclust;
|
|
|
580 |
if (clust != 0) { /* Dyanmic directory table */
|
|
|
581 |
dj->clust = clust;
|
|
|
582 |
dj->sect = clust2sect(clust);
|
|
|
583 |
} else { /* Static directory table */
|
|
|
584 |
dj->sect = fs->dirbase;
|
|
|
585 |
}
|
|
|
586 |
dj->index = 0;
|
|
|
587 |
|
|
|
588 |
do {
|
|
|
589 |
if (!move_window(dj->sect)) return FR_RW_ERROR;
|
|
|
590 |
dptr = &fs->win[(dj->index & 15) * 32]; /* Pointer to the directory entry */
|
|
|
591 |
c = dptr[DIR_Name];
|
|
|
592 |
if (c == 0 || c == 0xE5) { /* Found an empty entry! */
|
|
|
593 |
*dir = dptr; return FR_OK;
|
|
|
594 |
}
|
|
|
595 |
} while (next_dir_entry(dj)); /* Next directory pointer */
|
|
|
596 |
/* Reached to end of the directory table */
|
|
|
597 |
|
|
|
598 |
/* Abort when static table or could not stretch dynamic table */
|
|
|
599 |
if (clust == 0 || !(clust = create_chain(dj->clust))) return FR_DENIED;
|
|
|
600 |
if (clust == 1 || !move_window(0)) return FR_RW_ERROR;
|
|
|
601 |
|
|
|
602 |
fs->winsect = sector = clust2sect(clust); /* Cleanup the expanded table */
|
|
|
603 |
memset(fs->win, 0, 512U);
|
|
|
604 |
for (n = fs->csize; n; n--) {
|
|
|
605 |
if (disk_write(0, fs->win, sector, 1) != RES_OK)
|
|
|
606 |
return FR_RW_ERROR;
|
|
|
607 |
sector++;
|
|
|
608 |
}
|
|
|
609 |
fs->winflag = 1;
|
|
|
610 |
*dir = fs->win;
|
|
|
611 |
return FR_OK;
|
|
|
612 |
}
|
|
|
613 |
#endif /* !_FS_READONLY */
|
|
|
614 |
|
|
|
615 |
|
|
|
616 |
|
|
|
617 |
|
|
|
618 |
/*-----------------------------------------------------------------------*/
|
|
|
619 |
/* Load boot record and check if it is an FAT boot record */
|
|
|
620 |
/*-----------------------------------------------------------------------*/
|
|
|
621 |
|
|
|
622 |
static
|
|
|
623 |
BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record or error */
|
|
|
624 |
DWORD sect /* Sector# to check if it is an FAT boot record or not */
|
|
|
625 |
)
|
|
|
626 |
{
|
|
|
627 |
FATFS *fs = FatFs;
|
|
|
628 |
|
|
|
629 |
if (disk_read(0, fs->win, sect, 1) != RES_OK) /* Load boot record */
|
|
|
630 |
return 2;
|
|
|
631 |
if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature */
|
|
|
632 |
return 2;
|
|
|
633 |
|
|
|
634 |
if (!memcmp(&fs->win[BS_FilSysType], "FAT", 3)) /* Check FAT signature */
|
|
|
635 |
return 0;
|
|
|
636 |
#if _FAT32
|
|
|
637 |
if (!memcmp(&fs->win[BS_FilSysType32], "FAT32", 5) && !(fs->win[BPB_ExtFlags] & 0x80))
|
|
|
638 |
return 0;
|
|
|
639 |
#endif
|
|
|
640 |
return 1;
|
|
|
641 |
}
|
|
|
642 |
|
|
|
643 |
|
|
|
644 |
|
|
|
645 |
|
|
|
646 |
/*-----------------------------------------------------------------------*/
|
|
|
647 |
/* Make sure that the file system is valid */
|
|
|
648 |
/*-----------------------------------------------------------------------*/
|
|
|
649 |
|
|
|
650 |
static
|
|
|
651 |
FRESULT auto_mount ( /* FR_OK(0): successful, !=0: any error occured */
|
|
|
652 |
const char **path, /* Pointer to pointer to the path name (drive number) */
|
|
|
653 |
BYTE chk_wp /* !=0: Check media write protection for write access */
|
|
|
654 |
)
|
|
|
655 |
{
|
|
|
656 |
BYTE fmt;
|
|
|
657 |
DSTATUS stat;
|
|
|
658 |
DWORD bootsect, fatsize, totalsect, maxclust;
|
|
|
659 |
const char *p = *path;
|
|
|
660 |
FATFS *fs;
|
|
|
661 |
|
|
|
662 |
|
|
|
663 |
while (*p == ' ') p++; /* Strip leading spaces */
|
|
|
664 |
if (*p == '/') p++; /* Strip heading slash */
|
|
|
665 |
*path = p; /* Return pointer to the path name */
|
|
|
666 |
|
|
|
667 |
/* Is the file system object registered? */
|
|
|
668 |
fs = FatFs;
|
|
|
669 |
if (!fs) return FR_NOT_ENABLED;
|
|
|
670 |
|
|
|
671 |
if (fs->fs_type) { /* If the logical drive has been mounted */
|
|
|
672 |
stat = disk_status(0);
|
|
|
673 |
if (!(stat & STA_NOINIT)) { /* and physical drive is kept initialized (has not been changed), */
|
|
|
674 |
#if !_FS_READONLY
|
|
|
675 |
if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
|
|
|
676 |
return FR_WRITE_PROTECTED;
|
|
|
677 |
#endif
|
|
|
678 |
return FR_OK; /* The file system object is valid */
|
|
|
679 |
}
|
|
|
680 |
}
|
|
|
681 |
|
|
|
682 |
/* The logical drive must be re-mounted. Following code attempts to mount the logical drive */
|
|
|
683 |
|
|
|
684 |
memset(fs, 0, sizeof(FATFS)); /* Clean-up the file system object */
|
|
|
685 |
stat = disk_initialize(0); /* Initialize low level disk I/O layer */
|
|
|
686 |
if (stat & STA_NOINIT) /* Check if the drive is ready */
|
|
|
687 |
return FR_NOT_READY;
|
|
|
688 |
#if !_FS_READONLY
|
|
|
689 |
if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
|
|
|
690 |
return FR_WRITE_PROTECTED;
|
|
|
691 |
#endif
|
|
|
692 |
|
|
|
693 |
/* Search FAT partition on the drive */
|
|
|
694 |
fmt = check_fs(bootsect = 0); /* Check sector 0 as an SFD format */
|
|
|
695 |
if (fmt == 1) { /* Not an FAT boot record, it may be patitioned */
|
|
|
696 |
/* Check a partition listed in top of the partition table */
|
|
|
697 |
if (fs->win[MBR_Table+4]) { /* Is the 1st partition existing? */
|
|
|
698 |
bootsect = LD_DWORD(&fs->win[MBR_Table+8]); /* Partition offset in LBA */
|
|
|
699 |
fmt = check_fs(bootsect); /* Check the partition */
|
|
|
700 |
}
|
|
|
701 |
}
|
|
|
702 |
if (fmt || LD_WORD(&fs->win[BPB_BytsPerSec]) != 512U) /* No valid FAT patition is found */
|
|
|
703 |
return FR_NO_FILESYSTEM;
|
|
|
704 |
|
|
|
705 |
/* Initialize the file system object */
|
|
|
706 |
fatsize = LD_WORD(&fs->win[BPB_FATSz16]); /* Number of sectors per FAT */
|
|
|
707 |
if (!fatsize) fatsize = LD_DWORD(&fs->win[BPB_FATSz32]);
|
|
|
708 |
fs->sects_fat = (CLUST)fatsize;
|
|
|
709 |
fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FAT copies */
|
|
|
710 |
fatsize *= fs->n_fats; /* (Number of sectors in FAT area) */
|
|
|
711 |
fs->fatbase = bootsect + LD_WORD(&fs->win[BPB_RsvdSecCnt]); /* FAT start sector (lba) */
|
|
|
712 |
fs->csize = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
|
|
|
713 |
fs->n_rootdir = LD_WORD(&fs->win[BPB_RootEntCnt]); /* Nmuber of root directory entries */
|
|
|
714 |
totalsect = LD_WORD(&fs->win[BPB_TotSec16]); /* Number of sectors on the file system */
|
|
|
715 |
if (!totalsect) totalsect = LD_DWORD(&fs->win[BPB_TotSec32]);
|
|
|
716 |
fs->max_clust = maxclust = (totalsect /* max_clust = Last cluster# + 1 */
|
|
|
717 |
- LD_WORD(&fs->win[BPB_RsvdSecCnt]) - fatsize - fs->n_rootdir / 16
|
|
|
718 |
) / fs->csize + 2;
|
|
|
719 |
|
|
|
720 |
fmt = FS_FAT12; /* Determine the FAT sub type */
|
|
|
721 |
if (maxclust >= 0xFF7) fmt = FS_FAT16;
|
|
|
722 |
if (maxclust >= 0xFFF7)
|
|
|
723 |
#if !_FAT32
|
|
|
724 |
return FR_NO_FILESYSTEM;
|
|
|
725 |
#else
|
|
|
726 |
fmt = FS_FAT32;
|
|
|
727 |
if (fmt == FS_FAT32)
|
|
|
728 |
fs->dirbase = LD_DWORD(&fs->win[BPB_RootClus]); /* Root directory start cluster */
|
|
|
729 |
else
|
|
|
730 |
#endif
|
|
|
731 |
fs->dirbase = fs->fatbase + fatsize; /* Root directory start sector (lba) */
|
|
|
732 |
fs->database = fs->fatbase + fatsize + fs->n_rootdir / 16; /* Data start sector (lba) */
|
|
|
733 |
|
|
|
734 |
#if !_FS_READONLY
|
|
|
735 |
/* Initialize allocation information */
|
|
|
736 |
fs->free_clust = (CLUST)0xFFFFFFFF;
|
|
|
737 |
#if _USE_FSINFO
|
|
|
738 |
/* Get fsinfo if needed */
|
|
|
739 |
if (fmt == FS_FAT32) {
|
|
|
740 |
fs->fsi_sector = bootsect + LD_WORD(&fs->win[BPB_FSInfo]);
|
|
|
741 |
if (disk_read(0, fs->win, fs->fsi_sector, 1) == RES_OK &&
|
|
|
742 |
LD_WORD(&fs->win[BS_55AA]) == 0xAA55 &&
|
|
|
743 |
LD_DWORD(&fs->win[FSI_LeadSig]) == 0x41615252 &&
|
|
|
744 |
LD_DWORD(&fs->win[FSI_StrucSig]) == 0x61417272) {
|
|
|
745 |
fs->last_clust = LD_DWORD(&fs->win[FSI_Nxt_Free]);
|
|
|
746 |
fs->free_clust = LD_DWORD(&fs->win[FSI_Free_Count]);
|
|
|
747 |
}
|
|
|
748 |
}
|
|
|
749 |
#endif
|
|
|
750 |
#endif
|
|
|
751 |
|
|
|
752 |
fs->fs_type = fmt; /* FAT syb-type */
|
|
|
753 |
fs->id = ++fsid; /* File system mount ID */
|
|
|
754 |
return FR_OK;
|
|
|
755 |
}
|
|
|
756 |
|
|
|
757 |
|
|
|
758 |
|
|
|
759 |
|
|
|
760 |
/*-----------------------------------------------------------------------*/
|
|
|
761 |
/* Check if the file/dir object is valid or not */
|
|
|
762 |
/*-----------------------------------------------------------------------*/
|
|
|
763 |
|
|
|
764 |
static
|
|
|
765 |
FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */
|
|
|
766 |
const FATFS *fs, /* Pointer to the file system object */
|
|
|
767 |
WORD id /* Member id of the target object to be checked */
|
|
|
768 |
)
|
|
|
769 |
{
|
|
|
770 |
if (!fs || !fs->fs_type || fs->id != id)
|
|
|
771 |
return FR_INVALID_OBJECT;
|
|
|
772 |
if (disk_status(0) & STA_NOINIT)
|
|
|
773 |
return FR_NOT_READY;
|
|
|
774 |
|
|
|
775 |
return FR_OK;
|
|
|
776 |
}
|
|
|
777 |
|
|
|
778 |
|
|
|
779 |
|
|
|
780 |
|
|
|
781 |
/*--------------------------------------------------------------------------
|
|
|
782 |
|
|
|
783 |
Public Functions
|
|
|
784 |
|
|
|
785 |
--------------------------------------------------------------------------*/
|
|
|
786 |
|
|
|
787 |
|
|
|
788 |
/*-----------------------------------------------------------------------*/
|
|
|
789 |
/* Mount/Unmount a Locical Drive */
|
|
|
790 |
/*-----------------------------------------------------------------------*/
|
|
|
791 |
|
|
|
792 |
FRESULT f_mount (
|
|
|
793 |
BYTE drv, /* Logical drive number to be mounted/unmounted */
|
|
|
794 |
FATFS *fs /* Pointer to new file system object (NULL for unmount)*/
|
|
|
795 |
)
|
|
|
796 |
{
|
|
|
797 |
if (drv) return FR_INVALID_DRIVE;
|
|
|
798 |
|
|
|
799 |
if (FatFs) FatFs->fs_type = 0; /* Clear old object */
|
|
|
800 |
|
|
|
801 |
FatFs = fs; /* Register and clear new object */
|
|
|
802 |
if (fs) fs->fs_type = 0;
|
|
|
803 |
|
|
|
804 |
return FR_OK;
|
|
|
805 |
}
|
|
|
806 |
|
|
|
807 |
|
|
|
808 |
|
|
|
809 |
|
|
|
810 |
/*-----------------------------------------------------------------------*/
|
|
|
811 |
/* Open or Create a File */
|
|
|
812 |
/*-----------------------------------------------------------------------*/
|
|
|
813 |
|
|
|
814 |
FRESULT f_open (
|
|
|
815 |
FIL *fp, /* Pointer to the blank file object */
|
|
|
816 |
const char *path, /* Pointer to the file name */
|
|
|
817 |
BYTE mode /* Access mode and file open mode flags */
|
|
|
818 |
)
|
|
|
819 |
{
|
|
|
820 |
FRESULT res;
|
|
|
821 |
DIR dj;
|
|
|
822 |
BYTE *dir;
|
|
|
823 |
char fn[8+3+1];
|
|
|
824 |
|
|
|
825 |
|
|
|
826 |
fp->fs = NULL; /* Clear file object */
|
|
|
827 |
#if !_FS_READONLY
|
|
|
828 |
mode &= (FA_READ|FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW);
|
|
|
829 |
res = auto_mount(&path, (BYTE)(mode & (FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)));
|
|
|
830 |
#else
|
|
|
831 |
mode &= FA_READ;
|
|
|
832 |
res = auto_mount(&path, 0);
|
|
|
833 |
#endif
|
|
|
834 |
if (res != FR_OK) return res;
|
|
|
835 |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
|
|
|
836 |
|
|
|
837 |
#if !_FS_READONLY
|
|
|
838 |
/* Create or Open a File */
|
|
|
839 |
if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)) {
|
|
|
840 |
CLUST rs;
|
|
|
841 |
DWORD dw;
|
|
|
842 |
if (res != FR_OK) { /* No file, create new */
|
|
|
843 |
if (res != FR_NO_FILE) return res;
|
|
|
844 |
res = reserve_direntry(&dj, &dir);
|
|
|
845 |
if (res != FR_OK) return res;
|
|
|
846 |
memset(dir, 0, 32); /* Initialize the new entry with open name */
|
|
|
847 |
memcpy(&dir[DIR_Name], fn, 8+3);
|
|
|
848 |
dir[DIR_NTres] = fn[11];
|
|
|
849 |
mode |= FA_CREATE_ALWAYS;
|
|
|
850 |
}
|
|
|
851 |
else { /* Any object is already existing */
|
|
|
852 |
if (mode & FA_CREATE_NEW) /* Cannot create new */
|
|
|
853 |
return FR_EXIST;
|
|
|
854 |
if (!dir || (dir[DIR_Attr] & (AM_RDO|AM_DIR))) /* Cannot overwrite (R/O or DIR) */
|
|
|
855 |
return FR_DENIED;
|
|
|
856 |
if (mode & FA_CREATE_ALWAYS) { /* Resize it to zero */
|
|
|
857 |
#if _FAT32
|
|
|
858 |
rs = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
|
|
|
859 |
ST_WORD(&dir[DIR_FstClusHI], 0);
|
|
|
860 |
#else
|
|
|
861 |
rs = LD_WORD(&dir[DIR_FstClusLO]);
|
|
|
862 |
#endif
|
|
|
863 |
ST_WORD(&dir[DIR_FstClusLO], 0); /* cluster = 0 */
|
|
|
864 |
ST_DWORD(&dir[DIR_FileSize], 0); /* size = 0 */
|
|
|
865 |
dj.fs->winflag = 1;
|
|
|
866 |
dw = dj.fs->winsect; /* Remove the cluster chain */
|
|
|
867 |
if (!remove_chain(rs) || !move_window(dw))
|
|
|
868 |
return FR_RW_ERROR;
|
|
|
869 |
dj.fs->last_clust = rs - 1; /* Reuse the cluster hole */
|
|
|
870 |
}
|
|
|
871 |
}
|
|
|
872 |
if (mode & FA_CREATE_ALWAYS) {
|
|
|
873 |
dir[DIR_Attr] = 0; /* Reset attribute */
|
|
|
874 |
dw = get_fattime();
|
|
|
875 |
ST_DWORD(&dir[DIR_CrtTime], dw); /* Created time */
|
|
|
876 |
dj.fs->winflag = 1;
|
|
|
877 |
mode |= FA__WRITTEN; /* Set file changed flag */
|
|
|
878 |
}
|
|
|
879 |
}
|
|
|
880 |
/* Open an existing file */
|
|
|
881 |
else {
|
|
|
882 |
#endif /* !_FS_READONLY */
|
|
|
883 |
if (res != FR_OK) return res; /* Trace failed */
|
|
|
884 |
if (!dir || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */
|
|
|
885 |
return FR_NO_FILE;
|
|
|
886 |
#if !_FS_READONLY
|
|
|
887 |
if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
|
|
|
888 |
return FR_DENIED;
|
|
|
889 |
}
|
|
|
890 |
fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
|
|
|
891 |
fp->dir_ptr = dir;
|
|
|
892 |
#endif
|
|
|
893 |
fp->flag = mode; /* File access mode */
|
|
|
894 |
fp->org_clust = /* File start cluster */
|
|
|
895 |
#if _FAT32
|
|
|
896 |
((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) |
|
|
|
897 |
#endif
|
|
|
898 |
LD_WORD(&dir[DIR_FstClusLO]);
|
|
|
899 |
fp->fsize = LD_DWORD(&dir[DIR_FileSize]); /* File size */
|
|
|
900 |
fp->fptr = 0; fp->csect = 255; /* File pointer */
|
|
|
901 |
fp->fs = dj.fs; fp->id = dj.fs->id; /* Owner file system object of the file */
|
|
|
902 |
|
|
|
903 |
return FR_OK;
|
|
|
904 |
}
|
|
|
905 |
|
|
|
906 |
|
|
|
907 |
|
|
|
908 |
|
|
|
909 |
/*-----------------------------------------------------------------------*/
|
|
|
910 |
/* Read File */
|
|
|
911 |
/*-----------------------------------------------------------------------*/
|
|
|
912 |
|
|
|
913 |
FRESULT f_read (
|
|
|
914 |
FIL *fp, /* Pointer to the file object */
|
|
|
915 |
void *buff, /* Pointer to data buffer */
|
|
|
916 |
UINT btr, /* Number of bytes to read */
|
|
|
917 |
UINT *br /* Pointer to number of bytes read */
|
|
|
918 |
)
|
|
|
919 |
{
|
|
|
920 |
FRESULT res;
|
|
|
921 |
DWORD sect, remain;
|
|
|
922 |
UINT rcnt, cc;
|
|
|
923 |
CLUST clust;
|
|
|
924 |
BYTE *rbuff = buff;
|
|
|
925 |
|
|
|
926 |
|
|
|
927 |
*br = 0;
|
|
|
928 |
res = validate(fp->fs, fp->id); /* Check validity of the object */
|
|
|
929 |
if (res != FR_OK) return res;
|
|
|
930 |
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
|
|
|
931 |
if (!(fp->flag & FA_READ)) return FR_DENIED; /* Check access mode */
|
|
|
932 |
remain = fp->fsize - fp->fptr;
|
|
|
933 |
if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
|
|
|
934 |
|
|
|
935 |
for ( ; btr; /* Repeat until all data transferred */
|
|
|
936 |
rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
|
|
|
937 |
if ((fp->fptr % 512U) == 0) { /* On the sector boundary? */
|
|
|
938 |
if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
|
|
|
939 |
clust = (fp->fptr == 0) ? /* On the top of the file? */
|
|
|
940 |
fp->org_clust : get_cluster(fp->curr_clust);
|
|
|
941 |
if (clust < 2 || clust >= fp->fs->max_clust) goto fr_error;
|
|
|
942 |
fp->curr_clust = clust; /* Update current cluster */
|
|
|
943 |
fp->csect = 0; /* Reset sector address in the cluster */
|
|
|
944 |
}
|
|
|
945 |
sect = clust2sect(fp->curr_clust) + fp->csect; /* Get current sector */
|
|
|
946 |
cc = btr / 512U; /* When remaining bytes >= sector size, */
|
|
|
947 |
if (cc) { /* Read maximum contiguous sectors directly */
|
|
|
948 |
if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
|
|
|
949 |
cc = fp->fs->csize - fp->csect;
|
|
|
950 |
if (disk_read(0, rbuff, sect, (BYTE)cc) != RES_OK)
|
|
|
951 |
goto fr_error;
|
|
|
952 |
fp->csect += (BYTE)cc; /* Next sector address in the cluster */
|
|
|
953 |
rcnt = 512U * cc; /* Number of bytes transferred */
|
|
|
954 |
continue;
|
|
|
955 |
}
|
|
|
956 |
fp->csect++; /* Next sector address in the cluster */
|
|
|
957 |
}
|
|
|
958 |
sect = clust2sect(fp->curr_clust) + fp->csect - 1; /* Get current sector */
|
|
|
959 |
if (!move_window(sect)) goto fr_error; /* Move sector window */
|
|
|
960 |
rcnt = 512U - (fp->fptr % 512U); /* Get partial sector from sector window */
|
|
|
961 |
if (rcnt > btr) rcnt = btr;
|
|
|
962 |
memcpy(rbuff, &fp->fs->win[fp->fptr % 512U], rcnt);
|
|
|
963 |
}
|
|
|
964 |
|
|
|
965 |
return FR_OK;
|
|
|
966 |
|
|
|
967 |
fr_error: /* Abort this file due to an unrecoverable error */
|
|
|
968 |
fp->flag |= FA__ERROR;
|
|
|
969 |
return FR_RW_ERROR;
|
|
|
970 |
}
|
|
|
971 |
|
|
|
972 |
|
|
|
973 |
|
|
|
974 |
|
|
|
975 |
#if !_FS_READONLY
|
|
|
976 |
/*-----------------------------------------------------------------------*/
|
|
|
977 |
/* Write File */
|
|
|
978 |
/*-----------------------------------------------------------------------*/
|
|
|
979 |
|
|
|
980 |
FRESULT f_write (
|
|
|
981 |
FIL *fp, /* Pointer to the file object */
|
|
|
982 |
const void *buff, /* Pointer to the data to be written */
|
|
|
983 |
UINT btw, /* Number of bytes to write */
|
|
|
984 |
UINT *bw /* Pointer to number of bytes written */
|
|
|
985 |
)
|
|
|
986 |
{
|
|
|
987 |
FRESULT res;
|
|
|
988 |
DWORD sect;
|
|
|
989 |
UINT wcnt, cc;
|
|
|
990 |
CLUST clust;
|
|
|
991 |
const BYTE *wbuff = buff;
|
|
|
992 |
|
|
|
993 |
|
|
|
994 |
*bw = 0;
|
|
|
995 |
res = validate(fp->fs, fp->id); /* Check validity of the object */
|
|
|
996 |
if (res != FR_OK) return res;
|
|
|
997 |
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
|
|
|
998 |
if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */
|
|
|
999 |
if (fp->fsize + btw < fp->fsize) return FR_OK; /* File size cannot reach 4GB */
|
|
|
1000 |
|
|
|
1001 |
for ( ; btw; /* Repeat until all data transferred */
|
|
|
1002 |
wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
|
|
|
1003 |
if ((fp->fptr % 512U) == 0) { /* On the sector boundary? */
|
|
|
1004 |
if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
|
|
|
1005 |
if (fp->fptr == 0) { /* On the top of the file? */
|
|
|
1006 |
clust = fp->org_clust; /* Follow from the origin */
|
|
|
1007 |
if (clust == 0) /* When there is no cluster chain, */
|
|
|
1008 |
fp->org_clust = clust = create_chain(0); /* Create a new cluster chain */
|
|
|
1009 |
} else { /* Middle or end of the file */
|
|
|
1010 |
clust = create_chain(fp->curr_clust); /* Trace or streach cluster chain */
|
|
|
1011 |
}
|
|
|
1012 |
if (clust == 0) break; /* Could not allocate a new cluster (disk full) */
|
|
|
1013 |
if (clust == 1 || clust >= fp->fs->max_clust) goto fw_error;
|
|
|
1014 |
fp->curr_clust = clust; /* Update current cluster */
|
|
|
1015 |
fp->csect = 0; /* Reset sector address in the cluster */
|
|
|
1016 |
}
|
|
|
1017 |
sect = clust2sect(fp->curr_clust) + fp->csect; /* Get current sector */
|
|
|
1018 |
cc = btw / 512U; /* When remaining bytes >= sector size, */
|
|
|
1019 |
if (cc) { /* Write maximum contiguous sectors directly */
|
|
|
1020 |
if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
|
|
|
1021 |
cc = fp->fs->csize - fp->csect;
|
|
|
1022 |
if (disk_write(0, wbuff, sect, (BYTE)cc) != RES_OK)
|
|
|
1023 |
goto fw_error;
|
|
|
1024 |
fp->csect += (BYTE)cc; /* Next sector address in the cluster */
|
|
|
1025 |
wcnt = 512U * cc; /* Number of bytes transferred */
|
|
|
1026 |
continue;
|
|
|
1027 |
}
|
|
|
1028 |
if (fp->fptr >= fp->fsize) { /* Flush R/W window without reading if needed */
|
|
|
1029 |
if (!move_window(0)) goto fw_error;
|
|
|
1030 |
fp->fs->winsect = sect;
|
|
|
1031 |
}
|
|
|
1032 |
fp->csect++; /* Next sector address in the cluster */
|
|
|
1033 |
}
|
|
|
1034 |
sect = clust2sect(fp->curr_clust) + fp->csect - 1; /* Get current sector */
|
|
|
1035 |
if (!move_window(sect)) goto fw_error; /* Move sector window */
|
|
|
1036 |
wcnt = 512U - (fp->fptr % 512U); /* Put partial sector into sector window */
|
|
|
1037 |
if (wcnt > btw) wcnt = btw;
|
|
|
1038 |
memcpy(&fp->fs->win[fp->fptr % 512U], wbuff, wcnt);
|
|
|
1039 |
fp->fs->winflag = 1;
|
|
|
1040 |
}
|
|
|
1041 |
|
|
|
1042 |
if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
|
|
|
1043 |
fp->flag |= FA__WRITTEN; /* Set file changed flag */
|
|
|
1044 |
return res;
|
|
|
1045 |
|
|
|
1046 |
fw_error: /* Abort this file due to an unrecoverable error */
|
|
|
1047 |
fp->flag |= FA__ERROR;
|
|
|
1048 |
return FR_RW_ERROR;
|
|
|
1049 |
}
|
|
|
1050 |
|
|
|
1051 |
|
|
|
1052 |
|
|
|
1053 |
|
|
|
1054 |
/*-----------------------------------------------------------------------*/
|
|
|
1055 |
/* Synchronize the file object */
|
|
|
1056 |
/*-----------------------------------------------------------------------*/
|
|
|
1057 |
|
|
|
1058 |
FRESULT f_sync (
|
|
|
1059 |
FIL *fp /* Pointer to the file object */
|
|
|
1060 |
)
|
|
|
1061 |
{
|
|
|
1062 |
FRESULT res;
|
|
|
1063 |
DWORD tim;
|
|
|
1064 |
BYTE *dir;
|
|
|
1065 |
|
|
|
1066 |
|
|
|
1067 |
res = validate(fp->fs, fp->id); /* Check validity of the object */
|
|
|
1068 |
if (res == FR_OK) {
|
|
|
1069 |
if (fp->flag & FA__WRITTEN) { /* Has the file been written? */
|
|
|
1070 |
/* Update the directory entry */
|
|
|
1071 |
if (!move_window(fp->dir_sect))
|
|
|
1072 |
return FR_RW_ERROR;
|
|
|
1073 |
dir = fp->dir_ptr;
|
|
|
1074 |
dir[DIR_Attr] |= AM_ARC; /* Set archive bit */
|
|
|
1075 |
ST_DWORD(&dir[DIR_FileSize], fp->fsize); /* Update file size */
|
|
|
1076 |
ST_WORD(&dir[DIR_FstClusLO], fp->org_clust); /* Update start cluster */
|
|
|
1077 |
#if _FAT32
|
|
|
1078 |
ST_WORD(&dir[DIR_FstClusHI], fp->org_clust >> 16);
|
|
|
1079 |
#endif
|
|
|
1080 |
tim = get_fattime(); /* Updated time */
|
|
|
1081 |
ST_DWORD(&dir[DIR_WrtTime], tim);
|
|
|
1082 |
fp->flag &= (BYTE)~FA__WRITTEN;
|
|
|
1083 |
res = sync();
|
|
|
1084 |
}
|
|
|
1085 |
}
|
|
|
1086 |
return res;
|
|
|
1087 |
}
|
|
|
1088 |
|
|
|
1089 |
#endif /* !_FS_READONLY */
|
|
|
1090 |
|
|
|
1091 |
|
|
|
1092 |
|
|
|
1093 |
/*-----------------------------------------------------------------------*/
|
|
|
1094 |
/* Close File */
|
|
|
1095 |
/*-----------------------------------------------------------------------*/
|
|
|
1096 |
|
|
|
1097 |
FRESULT f_close (
|
|
|
1098 |
FIL *fp /* Pointer to the file object to be closed */
|
|
|
1099 |
)
|
|
|
1100 |
{
|
|
|
1101 |
FRESULT res;
|
|
|
1102 |
|
|
|
1103 |
|
|
|
1104 |
#if !_FS_READONLY
|
|
|
1105 |
res = f_sync(fp);
|
|
|
1106 |
#else
|
|
|
1107 |
res = validate(fp->fs, fp->id);
|
|
|
1108 |
#endif
|
|
|
1109 |
if (res == FR_OK) fp->fs = NULL;
|
|
|
1110 |
return res;
|
|
|
1111 |
}
|
|
|
1112 |
|
|
|
1113 |
|
|
|
1114 |
|
|
|
1115 |
|
|
|
1116 |
#if _FS_MINIMIZE <= 2
|
|
|
1117 |
/*-----------------------------------------------------------------------*/
|
|
|
1118 |
/* Seek File R/W Pointer */
|
|
|
1119 |
/*-----------------------------------------------------------------------*/
|
|
|
1120 |
|
|
|
1121 |
FRESULT f_lseek (
|
|
|
1122 |
FIL *fp, /* Pointer to the file object */
|
|
|
1123 |
DWORD ofs /* File pointer from top of file */
|
|
|
1124 |
)
|
|
|
1125 |
{
|
|
|
1126 |
FRESULT res;
|
|
|
1127 |
CLUST clust;
|
|
|
1128 |
DWORD csize, ifptr;
|
|
|
1129 |
|
|
|
1130 |
|
|
|
1131 |
res = validate(fp->fs, fp->id); /* Check validity of the object */
|
|
|
1132 |
if (res != FR_OK) return res;
|
|
|
1133 |
if (fp->flag & FA__ERROR) return FR_RW_ERROR;
|
|
|
1134 |
if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */
|
|
|
1135 |
#if !_FS_READONLY
|
|
|
1136 |
&& !(fp->flag & FA_WRITE)
|
|
|
1137 |
#endif
|
|
|
1138 |
) ofs = fp->fsize;
|
|
|
1139 |
|
|
|
1140 |
ifptr = fp->fptr;
|
|
|
1141 |
fp->fptr = 0; fp->csect = 255;
|
|
|
1142 |
if (ofs > 0) {
|
|
|
1143 |
csize = (DWORD)fp->fs->csize * 512U; /* Cluster size (byte) */
|
|
|
1144 |
if (ifptr > 0 &&
|
|
|
1145 |
(ofs - 1) / csize >= (ifptr - 1) / csize) {/* When seek to same or following cluster, */
|
|
|
1146 |
fp->fptr = (ifptr - 1) & ~(csize - 1); /* start from the current cluster */
|
|
|
1147 |
ofs -= fp->fptr;
|
|
|
1148 |
clust = fp->curr_clust;
|
|
|
1149 |
} else { /* When seek to back cluster, */
|
|
|
1150 |
clust = fp->org_clust; /* start from the first cluster */
|
|
|
1151 |
#if !_FS_READONLY
|
|
|
1152 |
if (clust == 0) { /* If no cluster chain, create a new chain */
|
|
|
1153 |
clust = create_chain(0);
|
|
|
1154 |
if (clust == 1) goto fk_error;
|
|
|
1155 |
fp->org_clust = clust;
|
|
|
1156 |
}
|
|
|
1157 |
#endif
|
|
|
1158 |
fp->curr_clust = clust;
|
|
|
1159 |
}
|
|
|
1160 |
if (clust != 0) {
|
|
|
1161 |
while (ofs > csize) { /* Cluster following loop */
|
|
|
1162 |
#if !_FS_READONLY
|
|
|
1163 |
if (fp->flag & FA_WRITE) { /* Check if in write mode or not */
|
|
|
1164 |
clust = create_chain(clust); /* Force streached if in write mode */
|
|
|
1165 |
if (clust == 0) { /* When disk gets full, clip file size */
|
|
|
1166 |
ofs = csize; break;
|
|
|
1167 |
}
|
|
|
1168 |
} else
|
|
|
1169 |
#endif
|
|
|
1170 |
clust = get_cluster(clust); /* Follow cluster chain if not in write mode */
|
|
|
1171 |
if (clust < 2 || clust >= fp->fs->max_clust) goto fk_error;
|
|
|
1172 |
fp->curr_clust = clust;
|
|
|
1173 |
fp->fptr += csize;
|
|
|
1174 |
ofs -= csize;
|
|
|
1175 |
}
|
|
|
1176 |
fp->fptr += ofs;
|
|
|
1177 |
fp->csect = (BYTE)(ofs / 512U); /* Sector offset in the cluster */
|
|
|
1178 |
if (ofs % 512U) fp->csect++;
|
|
|
1179 |
}
|
|
|
1180 |
}
|
|
|
1181 |
|
|
|
1182 |
#if !_FS_READONLY
|
|
|
1183 |
if (fp->fptr > fp->fsize) { /* Set changed flag if the file was extended */
|
|
|
1184 |
fp->fsize = fp->fptr;
|
|
|
1185 |
fp->flag |= FA__WRITTEN;
|
|
|
1186 |
}
|
|
|
1187 |
#endif
|
|
|
1188 |
|
|
|
1189 |
return FR_OK;
|
|
|
1190 |
|
|
|
1191 |
fk_error: /* Abort this file due to an unrecoverable error */
|
|
|
1192 |
fp->flag |= FA__ERROR;
|
|
|
1193 |
return FR_RW_ERROR;
|
|
|
1194 |
}
|
|
|
1195 |
|
|
|
1196 |
|
|
|
1197 |
|
|
|
1198 |
|
|
|
1199 |
#if _FS_MINIMIZE <= 1
|
|
|
1200 |
/*-----------------------------------------------------------------------*/
|
|
|
1201 |
/* Create a directroy object */
|
|
|
1202 |
/*-----------------------------------------------------------------------*/
|
|
|
1203 |
|
|
|
1204 |
FRESULT f_opendir (
|
|
|
1205 |
DIR *dj, /* Pointer to directory object to create */
|
|
|
1206 |
const char *path /* Pointer to the directory path */
|
|
|
1207 |
)
|
|
|
1208 |
{
|
|
|
1209 |
FRESULT res;
|
|
|
1210 |
BYTE *dir;
|
|
|
1211 |
char fn[8+3+1];
|
|
|
1212 |
|
|
|
1213 |
|
|
|
1214 |
res = auto_mount(&path, 0);
|
|
|
1215 |
if (res == FR_OK) {
|
|
|
1216 |
res = trace_path(dj, fn, path, &dir); /* Trace the directory path */
|
|
|
1217 |
if (res == FR_OK) { /* Trace completed */
|
|
|
1218 |
if (dir) { /* It is not the root dir */
|
|
|
1219 |
if (dir[DIR_Attr] & AM_DIR) { /* The entry is a directory */
|
|
|
1220 |
dj->clust =
|
|
|
1221 |
#if _FAT32
|
|
|
1222 |
((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) |
|
|
|
1223 |
#endif
|
|
|
1224 |
LD_WORD(&dir[DIR_FstClusLO]);
|
|
|
1225 |
dj->sect = clust2sect(dj->clust);
|
|
|
1226 |
dj->index = 2;
|
|
|
1227 |
} else { /* The entry is not a directory */
|
|
|
1228 |
res = FR_NO_FILE;
|
|
|
1229 |
}
|
|
|
1230 |
}
|
|
|
1231 |
dj->id = dj->fs->id;
|
|
|
1232 |
}
|
|
|
1233 |
}
|
|
|
1234 |
|
|
|
1235 |
return res;
|
|
|
1236 |
}
|
|
|
1237 |
|
|
|
1238 |
|
|
|
1239 |
|
|
|
1240 |
|
|
|
1241 |
/*-----------------------------------------------------------------------*/
|
|
|
1242 |
/* Read Directory Entry in Sequense */
|
|
|
1243 |
/*-----------------------------------------------------------------------*/
|
|
|
1244 |
|
|
|
1245 |
FRESULT f_readdir (
|
|
|
1246 |
DIR *dj, /* Pointer to the directory object */
|
|
|
1247 |
FILINFO *finfo /* Pointer to file information to return */
|
|
|
1248 |
)
|
|
|
1249 |
{
|
|
|
1250 |
FRESULT res;
|
|
|
1251 |
BYTE *dir, c;
|
|
|
1252 |
|
|
|
1253 |
|
|
|
1254 |
res = validate(dj->fs, dj->id); /* Check validity of the object */
|
|
|
1255 |
if (res != FR_OK) return res;
|
|
|
1256 |
|
|
|
1257 |
finfo->fname[0] = 0;
|
|
|
1258 |
while (dj->sect) {
|
|
|
1259 |
if (!move_window(dj->sect))
|
|
|
1260 |
return FR_RW_ERROR;
|
|
|
1261 |
dir = &dj->fs->win[(dj->index & 15) * 32]; /* pointer to the directory entry */
|
|
|
1262 |
c = dir[DIR_Name];
|
|
|
1263 |
if (c == 0) break; /* Has it reached to end of dir? */
|
|
|
1264 |
if (c != 0xE5 && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */
|
|
|
1265 |
get_fileinfo(finfo, dir);
|
|
|
1266 |
if (!next_dir_entry(dj)) dj->sect = 0; /* Next entry */
|
|
|
1267 |
if (finfo->fname[0]) break; /* Found valid entry */
|
|
|
1268 |
}
|
|
|
1269 |
|
|
|
1270 |
return FR_OK;
|
|
|
1271 |
}
|
|
|
1272 |
|
|
|
1273 |
|
|
|
1274 |
|
|
|
1275 |
|
|
|
1276 |
#if _FS_MINIMIZE == 0
|
|
|
1277 |
/*-----------------------------------------------------------------------*/
|
|
|
1278 |
/* Get File Status */
|
|
|
1279 |
/*-----------------------------------------------------------------------*/
|
|
|
1280 |
|
|
|
1281 |
FRESULT f_stat (
|
|
|
1282 |
const char *path, /* Pointer to the file path */
|
|
|
1283 |
FILINFO *finfo /* Pointer to file information to return */
|
|
|
1284 |
)
|
|
|
1285 |
{
|
|
|
1286 |
FRESULT res;
|
|
|
1287 |
DIR dj;
|
|
|
1288 |
BYTE *dir;
|
|
|
1289 |
char fn[8+3+1];
|
|
|
1290 |
|
|
|
1291 |
|
|
|
1292 |
res = auto_mount(&path, 0);
|
|
|
1293 |
if (res == FR_OK) {
|
|
|
1294 |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
|
|
|
1295 |
if (res == FR_OK) { /* Trace completed */
|
|
|
1296 |
if (dir) /* Found an object */
|
|
|
1297 |
get_fileinfo(finfo, dir);
|
|
|
1298 |
else /* It is root dir */
|
|
|
1299 |
res = FR_INVALID_NAME;
|
|
|
1300 |
}
|
|
|
1301 |
}
|
|
|
1302 |
|
|
|
1303 |
return res;
|
|
|
1304 |
}
|
|
|
1305 |
|
|
|
1306 |
|
|
|
1307 |
|
|
|
1308 |
|
|
|
1309 |
#if !_FS_READONLY
|
|
|
1310 |
/*-----------------------------------------------------------------------*/
|
|
|
1311 |
/* Truncate File */
|
|
|
1312 |
/*-----------------------------------------------------------------------*/
|
|
|
1313 |
|
|
|
1314 |
FRESULT f_truncate (
|
|
|
1315 |
FIL *fp /* Pointer to the file object */
|
|
|
1316 |
)
|
|
|
1317 |
{
|
|
|
1318 |
FRESULT res;
|
|
|
1319 |
CLUST ncl;
|
|
|
1320 |
|
|
|
1321 |
|
|
|
1322 |
res = validate(fp->fs, fp->id); /* Check validity of the object */
|
|
|
1323 |
if (res != FR_OK) return res;
|
|
|
1324 |
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
|
|
|
1325 |
if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */
|
|
|
1326 |
|
|
|
1327 |
if (fp->fsize > fp->fptr) {
|
|
|
1328 |
fp->fsize = fp->fptr; /* Set file size to current R/W point */
|
|
|
1329 |
fp->flag |= FA__WRITTEN;
|
|
|
1330 |
if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */
|
|
|
1331 |
if (!remove_chain(fp->org_clust)) goto ft_error;
|
|
|
1332 |
fp->org_clust = 0;
|
|
|
1333 |
} else { /* When truncate a part of the file, remove remaining clusters */
|
|
|
1334 |
ncl = get_cluster(fp->curr_clust);
|
|
|
1335 |
if (ncl < 2) goto ft_error;
|
|
|
1336 |
if (ncl < fp->fs->max_clust) {
|
|
|
1337 |
if (!put_cluster(fp->curr_clust, (CLUST)0x0FFFFFFF)) goto ft_error;
|
|
|
1338 |
if (!remove_chain(ncl)) goto ft_error;
|
|
|
1339 |
}
|
|
|
1340 |
}
|
|
|
1341 |
}
|
|
|
1342 |
|
|
|
1343 |
return FR_OK;
|
|
|
1344 |
|
|
|
1345 |
ft_error: /* Abort this file due to an unrecoverable error */
|
|
|
1346 |
fp->flag |= FA__ERROR;
|
|
|
1347 |
return FR_RW_ERROR;
|
|
|
1348 |
}
|
|
|
1349 |
|
|
|
1350 |
|
|
|
1351 |
|
|
|
1352 |
|
|
|
1353 |
/*-----------------------------------------------------------------------*/
|
|
|
1354 |
/* Get Number of Free Clusters */
|
|
|
1355 |
/*-----------------------------------------------------------------------*/
|
|
|
1356 |
|
|
|
1357 |
FRESULT f_getfree (
|
|
|
1358 |
const char *drv, /* Pointer to the logical drive number (root dir) */
|
|
|
1359 |
DWORD *nclust, /* Pointer to the variable to return number of free clusters */
|
|
|
1360 |
FATFS **fatfs /* Pointer to pointer to corresponding file system object to return */
|
|
|
1361 |
)
|
|
|
1362 |
{
|
|
|
1363 |
FRESULT res;
|
|
|
1364 |
FATFS *fs;
|
|
|
1365 |
DWORD n, sect;
|
|
|
1366 |
CLUST clust;
|
|
|
1367 |
BYTE fat, f, *p;
|
|
|
1368 |
|
|
|
1369 |
|
|
|
1370 |
/* Get drive number */
|
|
|
1371 |
res = auto_mount(&drv, 0);
|
|
|
1372 |
if (res != FR_OK) return res;
|
|
|
1373 |
*fatfs = fs = FatFs;
|
|
|
1374 |
|
|
|
1375 |
/* If number of free cluster is valid, return it without cluster scan. */
|
|
|
1376 |
if (fs->free_clust <= fs->max_clust - 2) {
|
|
|
1377 |
*nclust = fs->free_clust;
|
|
|
1378 |
return FR_OK;
|
|
|
1379 |
}
|
|
|
1380 |
|
|
|
1381 |
/* Get number of free clusters */
|
|
|
1382 |
fat = fs->fs_type;
|
|
|
1383 |
n = 0;
|
|
|
1384 |
if (fat == FS_FAT12) {
|
|
|
1385 |
clust = 2;
|
|
|
1386 |
do {
|
|
|
1387 |
if ((WORD)get_cluster(clust) == 0) n++;
|
|
|
1388 |
} while (++clust < fs->max_clust);
|
|
|
1389 |
} else {
|
|
|
1390 |
clust = fs->max_clust;
|
|
|
1391 |
sect = fs->fatbase;
|
|
|
1392 |
f = 0; p = 0;
|
|
|
1393 |
do {
|
|
|
1394 |
if (!f) {
|
|
|
1395 |
if (!move_window(sect++)) return FR_RW_ERROR;
|
|
|
1396 |
p = fs->win;
|
|
|
1397 |
}
|
|
|
1398 |
if (!_FAT32 || fat == FS_FAT16) {
|
|
|
1399 |
if (LD_WORD(p) == 0) n++;
|
|
|
1400 |
p += 2; f += 1;
|
|
|
1401 |
} else {
|
|
|
1402 |
if (LD_DWORD(p) == 0) n++;
|
|
|
1403 |
p += 4; f += 2;
|
|
|
1404 |
}
|
|
|
1405 |
} while (--clust);
|
|
|
1406 |
}
|
|
|
1407 |
fs->free_clust = n;
|
|
|
1408 |
#if _USE_FSINFO
|
|
|
1409 |
if (fat == FS_FAT32) fs->fsi_flag = 1;
|
|
|
1410 |
#endif
|
|
|
1411 |
|
|
|
1412 |
*nclust = n;
|
|
|
1413 |
return FR_OK;
|
|
|
1414 |
}
|
|
|
1415 |
|
|
|
1416 |
|
|
|
1417 |
|
|
|
1418 |
|
|
|
1419 |
/*-----------------------------------------------------------------------*/
|
|
|
1420 |
/* Delete a File or Directory */
|
|
|
1421 |
/*-----------------------------------------------------------------------*/
|
|
|
1422 |
|
|
|
1423 |
FRESULT f_unlink (
|
|
|
1424 |
const char *path /* Pointer to the file or directory path */
|
|
|
1425 |
)
|
|
|
1426 |
{
|
|
|
1427 |
FRESULT res;
|
|
|
1428 |
DIR dj;
|
|
|
1429 |
BYTE *dir, *sdir;
|
|
|
1430 |
DWORD dsect;
|
|
|
1431 |
char fn[8+3+1];
|
|
|
1432 |
CLUST dclust;
|
|
|
1433 |
|
|
|
1434 |
|
|
|
1435 |
res = auto_mount(&path, 1);
|
|
|
1436 |
if (res != FR_OK) return res;
|
|
|
1437 |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
|
|
|
1438 |
if (res != FR_OK) return res; /* Trace failed */
|
|
|
1439 |
if (!dir) return FR_INVALID_NAME; /* It is the root directory */
|
|
|
1440 |
if (dir[DIR_Attr] & AM_RDO) return FR_DENIED; /* It is a R/O object */
|
|
|
1441 |
dsect = dj.fs->winsect;
|
|
|
1442 |
dclust =
|
|
|
1443 |
#if _FAT32
|
|
|
1444 |
((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) |
|
|
|
1445 |
#endif
|
|
|
1446 |
LD_WORD(&dir[DIR_FstClusLO]);
|
|
|
1447 |
if (dir[DIR_Attr] & AM_DIR) { /* It is a sub-directory */
|
|
|
1448 |
dj.clust = dclust; /* Check if the sub-dir is empty or not */
|
|
|
1449 |
dj.sect = clust2sect(dclust);
|
|
|
1450 |
dj.index = 2;
|
|
|
1451 |
do {
|
|
|
1452 |
if (!move_window(dj.sect)) return FR_RW_ERROR;
|
|
|
1453 |
sdir = &dj.fs->win[(dj.index & 15) * 32];
|
|
|
1454 |
if (sdir[DIR_Name] == 0) break;
|
|
|
1455 |
if (sdir[DIR_Name] != 0xE5 && !(sdir[DIR_Attr] & AM_VOL))
|
|
|
1456 |
return FR_DENIED; /* The directory is not empty */
|
|
|
1457 |
} while (next_dir_entry(&dj));
|
|
|
1458 |
}
|
|
|
1459 |
|
|
|
1460 |
if (!move_window(dsect)) return FR_RW_ERROR; /* Mark the directory entry 'deleted' */
|
|
|
1461 |
dir[DIR_Name] = 0xE5;
|
|
|
1462 |
dj.fs->winflag = 1;
|
|
|
1463 |
if (!remove_chain(dclust)) return FR_RW_ERROR; /* Remove the cluster chain */
|
|
|
1464 |
|
|
|
1465 |
return sync();
|
|
|
1466 |
}
|
|
|
1467 |
|
|
|
1468 |
|
|
|
1469 |
|
|
|
1470 |
|
|
|
1471 |
/*-----------------------------------------------------------------------*/
|
|
|
1472 |
/* Create a Directory */
|
|
|
1473 |
/*-----------------------------------------------------------------------*/
|
|
|
1474 |
|
|
|
1475 |
FRESULT f_mkdir (
|
|
|
1476 |
const char *path /* Pointer to the directory path */
|
|
|
1477 |
)
|
|
|
1478 |
{
|
|
|
1479 |
FRESULT res;
|
|
|
1480 |
DIR dj;
|
|
|
1481 |
BYTE *dir, *fw, n;
|
|
|
1482 |
char fn[8+3+1];
|
|
|
1483 |
DWORD sect, dsect, tim;
|
|
|
1484 |
CLUST dclust, pclust;
|
|
|
1485 |
|
|
|
1486 |
|
|
|
1487 |
res = auto_mount(&path, 1);
|
|
|
1488 |
if (res != FR_OK) return res;
|
|
|
1489 |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
|
|
|
1490 |
if (res == FR_OK) return FR_EXIST; /* Any file or directory is already existing */
|
|
|
1491 |
if (res != FR_NO_FILE) return res;
|
|
|
1492 |
|
|
|
1493 |
res = reserve_direntry(&dj, &dir); /* Reserve a directory entry */
|
|
|
1494 |
if (res != FR_OK) return res;
|
|
|
1495 |
sect = dj.fs->winsect;
|
|
|
1496 |
dclust = create_chain(0); /* Allocate a cluster for new directory table */
|
|
|
1497 |
if (dclust == 1) return FR_RW_ERROR;
|
|
|
1498 |
dsect = clust2sect(dclust);
|
|
|
1499 |
if (!dsect) return FR_DENIED;
|
|
|
1500 |
if (!move_window(dsect)) return FR_RW_ERROR;
|
|
|
1501 |
|
|
|
1502 |
fw = dj.fs->win;
|
|
|
1503 |
memset(fw, 0, 512U); /* Clear the directory table */
|
|
|
1504 |
for (n = 1; n < dj.fs->csize; n++) {
|
|
|
1505 |
if (disk_write(0, fw, ++dsect, 1) != RES_OK)
|
|
|
1506 |
return FR_RW_ERROR;
|
|
|
1507 |
}
|
|
|
1508 |
|
|
|
1509 |
memset(&fw[DIR_Name], ' ', 8+3); /* Create "." entry */
|
|
|
1510 |
fw[DIR_Name] = '.';
|
|
|
1511 |
fw[DIR_Attr] = AM_DIR;
|
|
|
1512 |
tim = get_fattime();
|
|
|
1513 |
ST_DWORD(&fw[DIR_WrtTime], tim);
|
|
|
1514 |
memcpy(&fw[32], &fw[0], 32); fw[33] = '.'; /* Create ".." entry */
|
|
|
1515 |
pclust = dj.sclust;
|
|
|
1516 |
#if _FAT32
|
|
|
1517 |
ST_WORD(&fw[ DIR_FstClusHI], dclust >> 16);
|
|
|
1518 |
if (dj.fs->fs_type == FS_FAT32 && pclust == dj.fs->dirbase) pclust = 0;
|
|
|
1519 |
ST_WORD(&fw[32+DIR_FstClusHI], pclust >> 16);
|
|
|
1520 |
#endif
|
|
|
1521 |
ST_WORD(&fw[ DIR_FstClusLO], dclust);
|
|
|
1522 |
ST_WORD(&fw[32+DIR_FstClusLO], pclust);
|
|
|
1523 |
dj.fs->winflag = 1;
|
|
|
1524 |
|
|
|
1525 |
if (!move_window(sect)) return FR_RW_ERROR;
|
|
|
1526 |
memset(&dir[0], 0, 32); /* Clean-up the new entry */
|
|
|
1527 |
memcpy(&dir[DIR_Name], fn, 8+3); /* Name */
|
|
|
1528 |
dir[DIR_NTres] = fn[11];
|
|
|
1529 |
dir[DIR_Attr] = AM_DIR; /* Attribute */
|
|
|
1530 |
ST_DWORD(&dir[DIR_WrtTime], tim); /* Crated time */
|
|
|
1531 |
ST_WORD(&dir[DIR_FstClusLO], dclust); /* Table start cluster */
|
|
|
1532 |
#if _FAT32
|
|
|
1533 |
ST_WORD(&dir[DIR_FstClusHI], dclust >> 16);
|
|
|
1534 |
#endif
|
|
|
1535 |
|
|
|
1536 |
return sync();
|
|
|
1537 |
}
|
|
|
1538 |
|
|
|
1539 |
|
|
|
1540 |
|
|
|
1541 |
|
|
|
1542 |
/*-----------------------------------------------------------------------*/
|
|
|
1543 |
/* Change File Attribute */
|
|
|
1544 |
/*-----------------------------------------------------------------------*/
|
|
|
1545 |
|
|
|
1546 |
FRESULT f_chmod (
|
|
|
1547 |
const char *path, /* Pointer to the file path */
|
|
|
1548 |
BYTE value, /* Attribute bits */
|
|
|
1549 |
BYTE mask /* Attribute mask to change */
|
|
|
1550 |
)
|
|
|
1551 |
{
|
|
|
1552 |
FRESULT res;
|
|
|
1553 |
DIR dj;
|
|
|
1554 |
BYTE *dir;
|
|
|
1555 |
char fn[8+3+1];
|
|
|
1556 |
|
|
|
1557 |
|
|
|
1558 |
res = auto_mount(&path, 1);
|
|
|
1559 |
if (res == FR_OK) {
|
|
|
1560 |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
|
|
|
1561 |
if (res == FR_OK) { /* Trace completed */
|
|
|
1562 |
if (!dir) {
|
|
|
1563 |
res = FR_INVALID_NAME; /* Root directory */
|
|
|
1564 |
} else {
|
|
|
1565 |
mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */
|
|
|
1566 |
dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */
|
|
|
1567 |
res = sync();
|
|
|
1568 |
}
|
|
|
1569 |
}
|
|
|
1570 |
}
|
|
|
1571 |
return res;
|
|
|
1572 |
}
|
|
|
1573 |
|
|
|
1574 |
|
|
|
1575 |
|
|
|
1576 |
|
|
|
1577 |
/*-----------------------------------------------------------------------*/
|
|
|
1578 |
/* Change Timestamp */
|
|
|
1579 |
/*-----------------------------------------------------------------------*/
|
|
|
1580 |
|
|
|
1581 |
FRESULT f_utime (
|
|
|
1582 |
const char *path, /* Pointer to the file/directory name */
|
|
|
1583 |
const FILINFO *finfo /* Pointer to the timestamp to be set */
|
|
|
1584 |
)
|
|
|
1585 |
{
|
|
|
1586 |
FRESULT res;
|
|
|
1587 |
DIR dj;
|
|
|
1588 |
BYTE *dir;
|
|
|
1589 |
char fn[8+3+1];
|
|
|
1590 |
|
|
|
1591 |
|
|
|
1592 |
res = auto_mount(&path, 1);
|
|
|
1593 |
if (res == FR_OK) {
|
|
|
1594 |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
|
|
|
1595 |
if (res == FR_OK) { /* Trace completed */
|
|
|
1596 |
if (!dir) {
|
|
|
1597 |
res = FR_INVALID_NAME; /* Root directory */
|
|
|
1598 |
} else {
|
|
|
1599 |
ST_WORD(&dir[DIR_WrtTime], finfo->ftime);
|
|
|
1600 |
ST_WORD(&dir[DIR_WrtDate], finfo->fdate);
|
|
|
1601 |
res = sync();
|
|
|
1602 |
}
|
|
|
1603 |
}
|
|
|
1604 |
}
|
|
|
1605 |
return res;
|
|
|
1606 |
}
|
|
|
1607 |
|
|
|
1608 |
|
|
|
1609 |
|
|
|
1610 |
|
|
|
1611 |
/*-----------------------------------------------------------------------*/
|
|
|
1612 |
/* Rename File/Directory */
|
|
|
1613 |
/*-----------------------------------------------------------------------*/
|
|
|
1614 |
|
|
|
1615 |
FRESULT f_rename (
|
|
|
1616 |
const char *path_old, /* Pointer to the old name */
|
|
|
1617 |
const char *path_new /* Pointer to the new name */
|
|
|
1618 |
)
|
|
|
1619 |
{
|
|
|
1620 |
FRESULT res;
|
|
|
1621 |
DWORD sect_old;
|
|
|
1622 |
BYTE *dir_old, *dir_new, direntry[32-11];
|
|
|
1623 |
DIR dj;
|
|
|
1624 |
char fn[8+3+1];
|
|
|
1625 |
|
|
|
1626 |
|
|
|
1627 |
res = auto_mount(&path_old, 1);
|
|
|
1628 |
if (res != FR_OK) return res;
|
|
|
1629 |
|
|
|
1630 |
res = trace_path(&dj, fn, path_old, &dir_old); /* Check old object */
|
|
|
1631 |
if (res != FR_OK) return res; /* The old object is not found */
|
|
|
1632 |
if (!dir_old) return FR_NO_FILE;
|
|
|
1633 |
sect_old = dj.fs->winsect; /* Save the object information */
|
|
|
1634 |
memcpy(direntry, &dir_old[DIR_Attr], 32-11);
|
|
|
1635 |
|
|
|
1636 |
res = trace_path(&dj, fn, path_new, &dir_new); /* Check new object */
|
|
|
1637 |
if (res == FR_OK) return FR_EXIST; /* The new object name is already existing */
|
|
|
1638 |
if (res != FR_NO_FILE) return res; /* Is there no old name? */
|
|
|
1639 |
res = reserve_direntry(&dj, &dir_new); /* Reserve a directory entry */
|
|
|
1640 |
if (res != FR_OK) return res;
|
|
|
1641 |
|
|
|
1642 |
memcpy(&dir_new[DIR_Attr], direntry, 32-11); /* Create new entry */
|
|
|
1643 |
memcpy(&dir_new[DIR_Name], fn, 8+3);
|
|
|
1644 |
dir_new[DIR_NTres] = fn[11];
|
|
|
1645 |
dj.fs->winflag = 1;
|
|
|
1646 |
|
|
|
1647 |
if (!move_window(sect_old)) return FR_RW_ERROR; /* Delete old entry */
|
|
|
1648 |
dir_old[DIR_Name] = 0xE5;
|
|
|
1649 |
|
|
|
1650 |
return sync();
|
|
|
1651 |
}
|
|
|
1652 |
|
|
|
1653 |
#endif /* !_FS_READONLY */
|
|
|
1654 |
#endif /* _FS_MINIMIZE == 0 */
|
|
|
1655 |
#endif /* _FS_MINIMIZE <= 1 */
|
|
|
1656 |
#endif /* _FS_MINIMIZE <= 2 */
|
|
|
1657 |
|
|
|
1658 |
|
|
|
1659 |
#if _USE_FORWARD
|
|
|
1660 |
/*-----------------------------------------------------------------------*/
|
|
|
1661 |
/* Forward data to the stream directly */
|
|
|
1662 |
/*-----------------------------------------------------------------------*/
|
|
|
1663 |
|
|
|
1664 |
FRESULT f_forward (
|
|
|
1665 |
FIL *fp, /* Pointer to the file object */
|
|
|
1666 |
UINT (*func)(const BYTE*,UINT), /* Pointer to the streaming function */
|
|
|
1667 |
UINT btr, /* Number of bytes to forward */
|
|
|
1668 |
UINT *br /* Pointer to number of bytes forwarded */
|
|
|
1669 |
)
|
|
|
1670 |
{
|
|
|
1671 |
FRESULT res;
|
|
|
1672 |
DWORD remain;
|
|
|
1673 |
UINT rcnt;
|
|
|
1674 |
CLUST clust;
|
|
|
1675 |
|
|
|
1676 |
|
|
|
1677 |
*br = 0;
|
|
|
1678 |
res = validate(fp->fs, fp->id); /* Check validity of the object */
|
|
|
1679 |
if (res != FR_OK) return res;
|
|
|
1680 |
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
|
|
|
1681 |
if (!(fp->flag & FA_READ)) return FR_DENIED; /* Check access mode */
|
|
|
1682 |
remain = fp->fsize - fp->fptr;
|
|
|
1683 |
if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
|
|
|
1684 |
|
|
|
1685 |
for ( ; btr && (*func)(NULL, 0); /* Repeat until all data transferred */
|
|
|
1686 |
fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
|
|
|
1687 |
if ((fp->fptr % 512U) == 0) { /* On the sector boundary? */
|
|
|
1688 |
if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
|
|
|
1689 |
clust = (fp->fptr == 0) ? /* On the top of the file? */
|
|
|
1690 |
fp->org_clust : get_cluster(fp->curr_clust);
|
|
|
1691 |
if (clust < 2 || clust >= fp->fs->max_clust) goto ff_error;
|
|
|
1692 |
fp->curr_clust = clust; /* Update current cluster */
|
|
|
1693 |
fp->csect = 0; /* Reset sector address in the cluster */
|
|
|
1694 |
}
|
|
|
1695 |
fp->csect++; /* Next sector address in the cluster */
|
|
|
1696 |
}
|
|
|
1697 |
if (!move_window(clust2sect(fp->curr_clust) + fp->csect - 1)) /* Move sector window */
|
|
|
1698 |
goto ff_error;
|
|
|
1699 |
rcnt = 512U - (WORD)(fp->fptr % 512U); /* Forward data from sector window */
|
|
|
1700 |
if (rcnt > btr) rcnt = btr;
|
|
|
1701 |
rcnt = (*func)(&fp->fs->win[(WORD)fp->fptr % 512U], rcnt);
|
|
|
1702 |
if (rcnt == 0) goto ff_error;
|
|
|
1703 |
}
|
|
|
1704 |
|
|
|
1705 |
return FR_OK;
|
|
|
1706 |
|
|
|
1707 |
ff_error: /* Abort this function due to an unrecoverable error */
|
|
|
1708 |
fp->flag |= FA__ERROR;
|
|
|
1709 |
return FR_RW_ERROR;
|
|
|
1710 |
}
|
|
|
1711 |
#endif /* _USE_FORWARD */
|
|
|
1712 |
|
|
|
1713 |
|
|
|
1714 |
|
|
|
1715 |
#if _USE_STRFUNC >= 1
|
|
|
1716 |
/*-----------------------------------------------------------------------*/
|
|
|
1717 |
/* Get a string from the file */
|
|
|
1718 |
/*-----------------------------------------------------------------------*/
|
|
|
1719 |
char* fgets (
|
|
|
1720 |
char* buff, /* Pointer to the string buffer to read */
|
|
|
1721 |
int len, /* Size of string buffer */
|
|
|
1722 |
FIL* fil /* Pointer to the file object */
|
|
|
1723 |
)
|
|
|
1724 |
{
|
|
|
1725 |
int i = 0;
|
|
|
1726 |
char *p = buff;
|
|
|
1727 |
UINT rc;
|
|
|
1728 |
|
|
|
1729 |
|
|
|
1730 |
while (i < len - 1) { /* Read bytes until buffer gets filled */
|
|
|
1731 |
f_read(fil, p, 1, &rc);
|
|
|
1732 |
if (rc != 1) break; /* Break when no data to read */
|
|
|
1733 |
#if _USE_STRFUNC >= 2
|
|
|
1734 |
if (*p == '\r') continue; /* Strip '\r' */
|
|
|
1735 |
#endif
|
|
|
1736 |
i++;
|
|
|
1737 |
if (*p++ == '\n') break; /* Break when reached end of line */
|
|
|
1738 |
}
|
|
|
1739 |
*p = 0;
|
|
|
1740 |
return i ? buff : 0; /* When no data read (eof or error), return with error. */
|
|
|
1741 |
}
|
|
|
1742 |
|
|
|
1743 |
|
|
|
1744 |
|
|
|
1745 |
#if !_FS_READONLY
|
|
|
1746 |
#include <stdarg.h>
|
|
|
1747 |
/*-----------------------------------------------------------------------*/
|
|
|
1748 |
/* Put a character to the file */
|
|
|
1749 |
/*-----------------------------------------------------------------------*/
|
|
|
1750 |
int fputc (
|
|
|
1751 |
int chr, /* A character to be output */
|
|
|
1752 |
FIL* fil /* Ponter to the file object */
|
|
|
1753 |
)
|
|
|
1754 |
{
|
|
|
1755 |
UINT bw;
|
|
|
1756 |
char c;
|
|
|
1757 |
|
|
|
1758 |
|
|
|
1759 |
#if _USE_STRFUNC >= 2
|
|
|
1760 |
if (chr == '\n') fputc ('\r', fil); /* LF -> CRLF conversion */
|
|
|
1761 |
#endif
|
|
|
1762 |
if (!fil) { /* Special value may be used to switch the destination to any other device */
|
|
|
1763 |
/* put_console(chr); */
|
|
|
1764 |
return chr;
|
|
|
1765 |
}
|
|
|
1766 |
c = (char)chr;
|
|
|
1767 |
f_write(fil, &c, 1, &bw); /* Write a byte to the file */
|
|
|
1768 |
return bw ? chr : EOF; /* Return the resulut */
|
|
|
1769 |
}
|
|
|
1770 |
|
|
|
1771 |
|
|
|
1772 |
|
|
|
1773 |
|
|
|
1774 |
/*-----------------------------------------------------------------------*/
|
|
|
1775 |
/* Put a string to the file */
|
|
|
1776 |
/*-----------------------------------------------------------------------*/
|
|
|
1777 |
int fputs (
|
|
|
1778 |
const char* str, /* Pointer to the string to be output */
|
|
|
1779 |
FIL* fil /* Pointer to the file object */
|
|
|
1780 |
)
|
|
|
1781 |
{
|
|
|
1782 |
int n;
|
|
|
1783 |
|
|
|
1784 |
|
|
|
1785 |
for (n = 0; *str; str++, n++) {
|
|
|
1786 |
if (fputc(*str, fil) == EOF) return EOF;
|
|
|
1787 |
}
|
|
|
1788 |
return n;
|
|
|
1789 |
}
|
|
|
1790 |
|
|
|
1791 |
|
|
|
1792 |
|
|
|
1793 |
|
|
|
1794 |
/*-----------------------------------------------------------------------*/
|
|
|
1795 |
/* Put a formatted string to the file */
|
|
|
1796 |
/*-----------------------------------------------------------------------*/
|
|
|
1797 |
int fprintf (
|
|
|
1798 |
FIL* fil, /* Pointer to the file object */
|
|
|
1799 |
const char* str, /* Pointer to the format string */
|
|
|
1800 |
... /* Optional arguments... */
|
|
|
1801 |
)
|
|
|
1802 |
{
|
|
|
1803 |
va_list arp;
|
|
|
1804 |
UCHAR c, f, r;
|
|
|
1805 |
ULONG val;
|
|
|
1806 |
char s[16];
|
|
|
1807 |
int i, w, res, cc;
|
|
|
1808 |
|
|
|
1809 |
|
|
|
1810 |
va_start(arp, str);
|
|
|
1811 |
|
|
|
1812 |
for (cc = res = 0; cc != EOF; res += cc) {
|
|
|
1813 |
c = *str++;
|
|
|
1814 |
if (c == 0) break; /* End of string */
|
|
|
1815 |
if (c != '%') { /* Non escape cahracter */
|
|
|
1816 |
cc = fputc(c, fil);
|
|
|
1817 |
if (cc != EOF) cc = 1;
|
|
|
1818 |
continue;
|
|
|
1819 |
}
|
|
|
1820 |
w = f = 0;
|
|
|
1821 |
c = *str++;
|
|
|
1822 |
if (c == '0') { /* Flag: '0' padding */
|
|
|
1823 |
f = 1; c = *str++;
|
|
|
1824 |
}
|
|
|
1825 |
while (c >= '0' && c <= '9') { /* Precision */
|
|
|
1826 |
w = w * 10 + (c - '0');
|
|
|
1827 |
c = *str++;
|
|
|
1828 |
}
|
|
|
1829 |
if (c == 'l') { /* Prefix: Size is long int */
|
|
|
1830 |
f |= 2; c = *str++;
|
|
|
1831 |
}
|
|
|
1832 |
if (c == 's') { /* Type is string */
|
|
|
1833 |
cc = fputs(va_arg(arp, char*), fil);
|
|
|
1834 |
continue;
|
|
|
1835 |
}
|
|
|
1836 |
if (c == 'c') { /* Type is character */
|
|
|
1837 |
cc = fputc(va_arg(arp, char), fil);
|
|
|
1838 |
if (cc != EOF) cc = 1;
|
|
|
1839 |
continue;
|
|
|
1840 |
}
|
|
|
1841 |
r = 0;
|
|
|
1842 |
if (c == 'd') r = 10; /* Type is signed decimal */
|
|
|
1843 |
if (c == 'u') r = 10; /* Type is unsigned decimal */
|
|
|
1844 |
if (c == 'X') r = 16; /* Type is unsigned hexdecimal */
|
|
|
1845 |
if (r == 0) break; /* Unknown type */
|
|
|
1846 |
if (f & 2) { /* Get the value */
|
|
|
1847 |
val = (ULONG)va_arg(arp, long);
|
|
|
1848 |
} else {
|
|
|
1849 |
val = (c == 'd') ? (ULONG)(long)va_arg(arp, int) : (ULONG)va_arg(arp, unsigned int);
|
|
|
1850 |
}
|
|
|
1851 |
/* Put numeral string */
|
|
|
1852 |
if (c == 'd') {
|
|
|
1853 |
if (val >= 0x80000000) {
|
|
|
1854 |
val = 0 - val;
|
|
|
1855 |
f |= 4;
|
|
|
1856 |
}
|
|
|
1857 |
}
|
|
|
1858 |
i = sizeof(s) - 1; s[i] = 0;
|
|
|
1859 |
do {
|
|
|
1860 |
c = (UCHAR)(val % r + '0');
|
|
|
1861 |
if (c > '9') c += 7;
|
|
|
1862 |
s[--i] = c;
|
|
|
1863 |
val /= r;
|
|
|
1864 |
} while (i && val);
|
|
|
1865 |
if (i && (f & 4)) s[--i] = '-';
|
|
|
1866 |
w = sizeof(s) - 1 - w;
|
|
|
1867 |
while (i && i > w) s[--i] = (f & 1) ? '0' : ' ';
|
|
|
1868 |
cc = fputs(&s[i], fil);
|
|
|
1869 |
}
|
|
|
1870 |
|
|
|
1871 |
va_end(arp);
|
|
|
1872 |
return (cc == EOF) ? cc : res;
|
|
|
1873 |
}
|
|
|
1874 |
|
|
|
1875 |
#endif /* !_FS_READONLY */
|
|
|
1876 |
#endif /* _USE_STRFUNC >= 1*/
|