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