1 /*----------------------------------------------------------------------------/
\r
2 / FatFs - FAT file system module R0.07e (C)ChaN, 2009
\r
3 /-----------------------------------------------------------------------------/
\r
4 / FatFs module is a generic FAT file system module for small embedded systems.
\r
5 / This is a free software that opened for education, research and commercial
\r
6 / developments under license policy of following trems.
\r
8 / Copyright (C) 2009, ChaN, all right reserved.
\r
10 / * The FatFs module is a free software and there is NO WARRANTY.
\r
11 / * No restriction on use. You can use, modify and redistribute it for
\r
12 / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
\r
13 / * Redistributions of source code must retain the above copyright notice.
\r
15 /-----------------------------------------------------------------------------/
\r
16 / Feb 26,'06 R0.00 Prototype.
\r
18 / Apr 29,'06 R0.01 First stable version.
\r
20 / Jun 01,'06 R0.02 Added FAT12 support.
\r
21 / Removed unbuffered mode.
\r
22 / Fixed a problem on small (<32M) patition.
\r
23 / Jun 10,'06 R0.02a Added a configuration option (_FS_MINIMUM).
\r
25 / Sep 22,'06 R0.03 Added f_rename().
\r
26 / Changed option _FS_MINIMUM to _FS_MINIMIZE.
\r
27 / Dec 11,'06 R0.03a Improved cluster scan algolithm to write files fast.
\r
28 / Fixed f_mkdir() creates incorrect directory on FAT32.
\r
30 / Feb 04,'07 R0.04 Supported multiple drive system.
\r
31 / Changed some interfaces for multiple drive system.
\r
32 / Changed f_mountdrv() to f_mount().
\r
34 / Apr 01,'07 R0.04a Supported multiple partitions on a plysical drive.
\r
35 / Added a capability of extending file size to f_lseek().
\r
36 / Added minimization level 3.
\r
37 / Fixed an endian sensitive code in f_mkfs().
\r
38 / May 05,'07 R0.04b Added a configuration option _USE_NTFLAG.
\r
39 / Added FSInfo support.
\r
40 / Fixed DBCS name can result FR_INVALID_NAME.
\r
41 / Fixed short seek (<= csize) collapses the file object.
\r
43 / Aug 25,'07 R0.05 Changed arguments of f_read(), f_write() and f_mkfs().
\r
44 / Fixed f_mkfs() on FAT32 creates incorrect FSInfo.
\r
45 / Fixed f_mkdir() on FAT32 creates incorrect directory.
\r
46 / Feb 03,'08 R0.05a Added f_truncate() and f_utime().
\r
47 / Fixed off by one error at FAT sub-type determination.
\r
48 / Fixed btr in f_read() can be mistruncated.
\r
49 / Fixed cached sector is not flushed when create and close
\r
52 / Apr 01,'08 R0.06 Added fputc(), fputs(), fprintf() and fgets().
\r
53 / Improved performance of f_lseek() on moving to the same
\r
54 / or following cluster.
\r
56 / Apr 01,'09 R0.07 Merged Tiny-FatFs as a buffer configuration option.
\r
57 / Added long file name support.
\r
58 / Added multiple code page support.
\r
59 / Added re-entrancy for multitask operation.
\r
60 / Added auto cluster size selection to f_mkfs().
\r
61 / Added rewind option to f_readdir().
\r
62 / Changed result code of critical errors.
\r
63 / Renamed string functions to avoid name collision.
\r
64 / Apr 14,'09 R0.07a Separated out OS dependent code on reentrant cfg.
\r
65 / Added multiple sector size support.
\r
66 / Jun 21,'09 R0.07c Fixed f_unlink() can return FR_OK on error.
\r
67 / Fixed wrong cache control in f_lseek().
\r
68 / Added relative path feature.
\r
69 / Added f_chdir() and f_chdrive().
\r
70 / Added proper case conversion to extended char.
\r
71 / Nov 03,'09 R0.07e Separated out configuration options from ff.h to ffconf.h.
\r
72 / Fixed f_unlink() fails to remove a sub-dir on _FS_RPATH.
\r
73 / Fixed name matching error on the 13 char boundary.
\r
74 / Added a configuration option, _LFN_UNICODE.
\r
75 / Changed f_readdir() to return the SFN with always upper
\r
76 / case on non-LFN cfg.
\r
77 /---------------------------------------------------------------------------*/
\r
78 #include "ff.h" /* FatFs configurations and declarations */
\r
79 #include "diskio.h" /* Declarations of low level disk I/O functions */
\r
81 /*--------------------------------------------------------------------------
\r
83 Module Private Definitions
\r
85 ---------------------------------------------------------------------------*/
\r
86 #if _FATFS != 0x007E
\r
87 #error Wrong include file (ff.h).
\r
91 #error Static LFN work area must not be used in re-entrant configuration.
\r
93 #define ENTER_FF( fs ) \
\r
95 if( !lock_fs(fs) ) \
\r
97 return FR_TIMEOUT; \
\r
101 #define LEAVE_FF( fs, res ) \
\r
103 unlock_fs( fs, res ); \
\r
108 #define ENTER_FF( fs )
\r
109 #define LEAVE_FF( fs, res ) return res
\r
111 #define ABORT( fs, res ) \
\r
113 fp->flag |= FA__ERROR; \
\r
114 LEAVE_FF( fs, res ); \
\r
121 /* Name status flags */
\r
122 #define NS 11 /* Offset of name status byte */
\r
123 #define NS_LOSS 0x01 /* Out of 8.3 format */
\r
124 #define NS_LFN 0x02 /* Force to create LFN entry */
\r
125 #define NS_LAST 0x04 /* Last segment */
\r
126 #define NS_BODY 0x08 /* Lower case flag (body) */
\r
127 #define NS_EXT 0x10 /* Lower case flag (ext) */
\r
128 #define NS_DOT 0x20 /* Dot entry */
\r
130 /*--------------------------------------------------------------------------
\r
134 ---------------------------------------------------------------------------*/
\r
135 #if _DRIVES < 1 || _DRIVES > 9
\r
136 #error Number of drives must be 1-9.
\r
138 static FATFS *FatFs[_DRIVES]; /* Pointer to the file system objects (logical drives) */
\r
140 static WORD Fsid; /* File system mount ID */
\r
143 static BYTE Drive; /* Current drive */
\r
145 #if _USE_LFN == 1 /* LFN with static LFN working buffer */
\r
146 static WCHAR LfnBuf[_MAX_LFN + 1];
\r
147 #define NAMEBUF( sp, lp ) \
\r
150 #define INITBUF( dj, sp, lp ) \
\r
154 #elif _USE_LFN > 1 /* LFN with dynamic LFN working buffer */
\r
155 #define NAMEBUF( sp, lp ) \
\r
157 WCHAR lbuf[_MAX_LFN + 1], *lp = lbuf
\r
158 #define INITBUF( dj, sp, lp ) \
\r
163 #define NAMEBUF( sp, lp ) BYTE sp[12]
\r
164 #define INITBUF( dj, sp, lp ) dj.fn = sp
\r
167 /*--------------------------------------------------------------------------
\r
169 Module Private Functions
\r
171 ---------------------------------------------------------------------------*/
\r
173 /*-----------------------------------------------------------------------*/
\r
175 /* String functions */
\r
177 /*-----------------------------------------------------------------------*/
\r
179 /* Copy memory to memory */
\r
180 static void mem_cpy ( void *dst, const void *src, int cnt )
\r
182 char *d = ( char * ) dst;
\r
183 const char *s = ( const char * ) src;
\r
191 static void mem_set ( void *dst, int val, int cnt )
\r
193 char *d = ( char * ) dst;
\r
196 *d++ = ( char ) val;
\r
200 /* Compare memory to memory */
\r
201 static int mem_cmp ( const void *dst, const void *src, int cnt )
\r
203 const char *d = ( const char * ) dst, *s = ( const char * ) src;
\r
205 while( cnt-- && (r = *d++ -*s++) == 0 );
\r
209 /* Check if chr is contained in the string */
\r
210 static int chk_chr ( const char *str, int chr )
\r
212 while( *str && *str != chr )
\r
220 /*-----------------------------------------------------------------------*/
\r
222 /* Request/Release grant to access the volume */
\r
224 /*-----------------------------------------------------------------------*/
\r
226 static BOOL lock_fs ( FATFS * fs /* File system object */ )
\r
228 return ff_req_grant( fs->sobj );
\r
231 static void unlock_fs ( FATFS * fs, /* File system object */ FRESULT res /* Result code to be returned */ )
\r
233 if( res != FR_NOT_ENABLED && res != FR_INVALID_DRIVE && res != FR_INVALID_OBJECT && res != FR_TIMEOUT )
\r
235 ff_rel_grant( fs->sobj );
\r
241 /*-----------------------------------------------------------------------*/
\r
243 /* Change window offset */
\r
245 /*-----------------------------------------------------------------------*/
\r
246 static FRESULT move_window ( FATFS * fs, /* File system object */ DWORD sector /* Sector number to make apperance in the fs->win[] */ ) /* Move to zero only writes back dirty window */
\r
250 wsect = fs->winsect;
\r
251 if( wsect != sector )
\r
252 { /* Changed current window */
\r
255 { /* Write back dirty window if needed */
\r
256 if( disk_write(fs->drive, fs->win, wsect, 1) != RES_OK )
\r
258 return FR_DISK_ERR;
\r
262 if( wsect < (fs->fatbase + fs->sects_fat) )
\r
263 { /* In FAT area */
\r
265 for( nf = fs->n_fats; nf > 1; nf-- )
\r
266 { /* Refrect the change to all FAT copies */
\r
267 wsect += fs->sects_fat;
\r
268 disk_write( fs->drive, fs->win, wsect, 1 );
\r
276 if( disk_read(fs->drive, fs->win, sector, 1) != RES_OK )
\r
278 return FR_DISK_ERR;
\r
281 fs->winsect = sector;
\r
288 /*-----------------------------------------------------------------------*/
\r
290 /* Clean-up cached data */
\r
292 /*-----------------------------------------------------------------------*/
\r
294 static FRESULT sync ( /* FR_OK: successful, FR_DISK_ERR: failed */ FATFS * fs /* File system object */ )
\r
298 res = move_window( fs, 0 );
\r
301 /* Update FSInfo sector if needed */
\r
302 if( fs->fs_type == FS_FAT32 && fs->fsi_flag )
\r
305 mem_set( fs->win, 0, 512 );
\r
306 ST_WORD( fs->win + BS_55AA, 0xAA55 );
\r
307 ST_DWORD( fs->win + FSI_LeadSig, 0x41615252 );
\r
308 ST_DWORD( fs->win + FSI_StrucSig, 0x61417272 );
\r
309 ST_DWORD( fs->win + FSI_Free_Count, fs->free_clust );
\r
310 ST_DWORD( fs->win + FSI_Nxt_Free, fs->last_clust );
\r
311 disk_write( fs->drive, fs->win, fs->fsi_sector, 1 );
\r
315 /* Make sure that no pending write process in the physical drive */
\r
316 if( disk_ioctl(fs->drive, CTRL_SYNC, ( void * ) NULL) != RES_OK )
\r
327 /*-----------------------------------------------------------------------*/
\r
329 /* FAT access - Read value of a FAT entry */
\r
331 /*-----------------------------------------------------------------------*/
\r
333 ( /* 0xFFFFFFFF:Disk error, 1:Interal error, Else:Cluster status */
\r
334 FATFS * fs, /* File system object */ DWORD clst /* Cluster# to get the link information */
\r
340 if( clst < 2 || clst >= fs->max_clust )
\r
341 { /* Range check */
\r
345 fsect = fs->fatbase;
\r
346 switch( fs->fs_type )
\r
351 if( move_window(fs, fsect + (bc / SS(fs))) )
\r
356 wc = fs->win[bc & ( SS(fs) - 1 )];
\r
358 if( move_window(fs, fsect + (bc / SS(fs))) )
\r
363 wc |= ( WORD ) fs->win[bc & ( SS(fs) - 1 )] << 8;
\r
364 return( clst & 1 ) ? ( wc >> 4 ) : ( wc & 0xFFF );
\r
367 if( move_window(fs, fsect + (clst / (SS(fs) / 2))) )
\r
372 return LD_WORD( &fs->win[((WORD) clst * 2) & (SS(fs) - 1)] );
\r
375 if( move_window(fs, fsect + (clst / (SS(fs) / 4))) )
\r
381 LD_DWORD( &fs->win[((WORD) clst * 4) & (SS(fs) - 1)] )
\r
385 return 0xFFFFFFFF; /* An error occured at the disk I/O layer */
\r
388 /*-----------------------------------------------------------------------*/
\r
390 /* FAT access - Change value of a FAT entry */
\r
392 /*-----------------------------------------------------------------------*/
\r
395 ( FATFS * fs, /* File system object */ DWORD clst, /* Cluster# to be changed in range of 2 to fs->max_clust - 1 */ DWORD val /* New value to mark the cluster */ )
\r
402 if( clst < 2 || clst >= fs->max_clust )
\r
403 { /* Range check */
\r
408 fsect = fs->fatbase;
\r
409 switch( fs->fs_type )
\r
414 res = move_window( fs, fsect + (bc / SS(fs)) );
\r
420 p = &fs->win[bc & ( SS(fs) - 1 )];
\r
421 *p = ( clst & 1 ) ? ( (*p & 0x0F) | ((BYTE) val << 4) ) : ( BYTE ) val;
\r
424 res = move_window( fs, fsect + (bc / SS(fs)) );
\r
430 p = &fs->win[bc & ( SS(fs) - 1 )];
\r
431 *p = ( clst & 1 ) ? ( BYTE ) ( val >> 4 ) : ( (*p & 0xF0) | ((BYTE) (val >> 8) & 0x0F) );
\r
435 res = move_window( fs, fsect + (clst / (SS(fs) / 2)) );
\r
441 ST_WORD( &fs->win[((WORD) clst * 2) & (SS(fs) - 1)], (WORD) val );
\r
445 res = move_window( fs, fsect + (clst / (SS(fs) / 4)) );
\r
451 ST_DWORD( &fs->win[((WORD) clst * 4) & (SS(fs) - 1)], val );
\r
464 #endif /* !_FS_READONLY */
\r
466 /*-----------------------------------------------------------------------*/
\r
468 /* FAT handling - Remove a cluster chain */
\r
470 /*-----------------------------------------------------------------------*/
\r
472 static FRESULT remove_chain ( FATFS * fs, /* File system object */ DWORD clst /* Cluster# to remove a chain from */ )
\r
477 if( clst < 2 || clst >= fs->max_clust )
\r
478 { /* Check the range of cluster# */
\r
484 while( clst < fs->max_clust )
\r
485 { /* Not a last link? */
\r
486 nxt = get_fat( fs, clst ); /* Get cluster status */
\r
489 break; /* Empty cluster? */
\r
496 } /* Internal error? */
\r
497 if( nxt == 0xFFFFFFFF )
\r
501 } /* Disk error? */
\r
502 res = put_fat( fs, clst, 0 ); /* Mark the cluster "empty" */
\r
508 if( fs->free_clust != 0xFFFFFFFF )
\r
509 { /* Update FSInfo */
\r
514 clst = nxt; /* Next cluster */
\r
523 /*-----------------------------------------------------------------------*/
\r
525 /* FAT handling - Stretch or Create a cluster chain */
\r
527 /*-----------------------------------------------------------------------*/
\r
529 static DWORD create_chain
\r
530 ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */
\r
531 FATFS * fs, /* File system object */ DWORD clst /* Cluster# to stretch. 0 means create a new chain. */
\r
534 DWORD cs, ncl, scl, mcl;
\r
536 mcl = fs->max_clust;
\r
538 { /* Create new chain */
\r
539 scl = fs->last_clust; /* Get suggested start point */
\r
540 if( scl == 0 || scl >= mcl )
\r
546 { /* Stretch existing chain */
\r
547 cs = get_fat( fs, clst ); /* Check the cluster status */
\r
550 return 1; /* It is an invalid cluster */
\r
555 return cs; /* It is already followed by next cluster */
\r
561 ncl = scl; /* Start cluster */
\r
564 ncl++; /* Next cluster */
\r
566 { /* Wrap around */
\r
570 return 0; /* No free custer */
\r
574 cs = get_fat( fs, ncl ); /* Get the cluster status */
\r
577 break; /* Found a free cluster */
\r
580 if( cs == 0xFFFFFFFF || cs == 1 )
\r
581 { /* An error occured */
\r
587 return 0; /* No free custer */
\r
591 if( put_fat(fs, ncl, 0x0FFFFFFF) )
\r
592 { /* Mark the new cluster "in use" */
\r
597 { /* Link it to the previous one if needed */
\r
598 if( put_fat(fs, clst, ncl) )
\r
604 fs->last_clust = ncl; /* Update FSINFO */
\r
605 if( fs->free_clust != 0xFFFFFFFF )
\r
611 return ncl; /* Return new cluster number */
\r
614 #endif /* !_FS_READONLY */
\r
616 /*-----------------------------------------------------------------------*/
\r
618 /* Get sector# from cluster# */
\r
620 /*-----------------------------------------------------------------------*/
\r
622 ( /* !=0: Sector number, 0: Failed - invalid cluster# */ FATFS * fs, /* File system object */ DWORD clst /* Cluster# to be converted */ )
\r
625 if( clst >= (fs->max_clust - 2) )
\r
627 return 0; /* Invalid cluster# */
\r
630 return clst * fs->csize + fs->database;
\r
633 /*-----------------------------------------------------------------------*/
\r
635 /* Directory handling - Seek directory index */
\r
637 /*-----------------------------------------------------------------------*/
\r
638 static FRESULT dir_seek ( DIR * dj, /* Pointer to directory object */ WORD idx /* Directory index number */ )
\r
645 if( clst == 1 || clst >= dj->fs->max_clust )
\r
646 { /* Check start cluster range */
\r
650 if( !clst && dj->fs->fs_type == FS_FAT32 )
\r
651 { /* Replace cluster# 0 with root cluster# if in FAT32 */
\r
652 clst = dj->fs->dirbase;
\r
656 { /* Static table */
\r
658 if( idx >= dj->fs->n_rootdir )
\r
659 { /* Index is out of range */
\r
663 dj->sect = dj->fs->dirbase + idx / ( SS(dj->fs) / 32 ); /* Sector# */
\r
666 { /* Dynamic table */
\r
669 / 32 * dj->fs->csize; /* Entries per cluster */
\r
671 { /* Follow cluster chain */
\r
672 clst = get_fat( dj->fs, clst ); /* Get next cluster */
\r
673 if( clst == 0xFFFFFFFF )
\r
675 return FR_DISK_ERR; /* Disk error */
\r
678 if( clst < 2 || clst >= dj->fs->max_clust )
\r
679 { /* Reached to end of table or int error */
\r
688 clust2sect( dj->fs, clst )
\r
689 + idx / ( SS(dj->fs) / 32 ); /* Sector# */
\r
692 dj->dir = dj->fs->win + ( idx % (SS(dj->fs) / 32) ) * 32; /* Ptr to the entry in the sector */
\r
694 return FR_OK; /* Seek succeeded */
\r
697 /*-----------------------------------------------------------------------*/
\r
699 /* Directory handling - Move directory index next */
\r
701 /*-----------------------------------------------------------------------*/
\r
702 static FRESULT dir_next
\r
703 ( /* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:EOT and could not streach */
\r
705 dj, /* Pointer to directory object */ BOOL streach /* FALSE: Do not streach table, TRUE: Streach table if needed */
\r
712 if( !i || !dj->sect )
\r
713 { /* Report EOT when index has reached 65535 */
\r
717 if( !(i % (SS(dj->fs) / 32)) )
\r
718 { /* Sector changed? */
\r
719 dj->sect++; /* Next sector */
\r
721 if( dj->clust == 0 )
\r
722 { /* Static table */
\r
723 if( i >= dj->fs->n_rootdir )
\r
724 { /* Report EOT when end of table */
\r
729 { /* Dynamic table */
\r
730 if( ((i / (SS(dj->fs) / 32)) & (dj->fs->csize - 1)) == 0 )
\r
731 { /* Cluster changed? */
\r
732 clst = get_fat( dj->fs, dj->clust ); /* Get next cluster */
\r
738 if( clst == 0xFFFFFFFF )
\r
740 return FR_DISK_ERR;
\r
743 if( clst >= dj->fs->max_clust )
\r
744 { /* When it reached end of dynamic table */
\r
749 return FR_NO_FILE; /* When do not streach, report EOT */
\r
752 clst = create_chain( dj->fs, dj->clust ); /* Streach cluster chain */
\r
755 return FR_DENIED; /* No free cluster */
\r
763 if( clst == 0xFFFFFFFF )
\r
765 return FR_DISK_ERR;
\r
768 /* Clean-up streached table */
\r
769 if( move_window(dj->fs, 0) )
\r
771 return FR_DISK_ERR; /* Flush active window */
\r
774 mem_set( dj->fs->win, 0, SS(dj->fs) ); /* Clear window buffer */
\r
775 dj->fs->winsect = clust2sect( dj->fs, clst ); /* Cluster start sector */
\r
776 for( c = 0; c < dj->fs->csize; c++ )
\r
777 { /* Fill the new cluster with 0 */
\r
779 if( move_window(dj->fs, 0) )
\r
781 return FR_DISK_ERR;
\r
787 dj->fs->winsect -= c; /* Rewind window address */
\r
789 return FR_NO_FILE; /* Report EOT */
\r
793 dj->clust = clst; /* Initialize data for new cluster */
\r
794 dj->sect = clust2sect( dj->fs, clst );
\r
800 dj->dir = dj->fs->win + ( i % (SS(dj->fs) / 32) ) * 32;
\r
805 /*-----------------------------------------------------------------------*/
\r
807 /* LFN handling - Test/Pick/Fit an LFN segment from/to directory entry */
\r
809 /*-----------------------------------------------------------------------*/
\r
811 static const BYTE LfnOfs[] = { 1, 3, 5, 7, 9, 14, 16, 18, 20, 22, 24, 28, 30 };
\r
813 /* Offset of LFN chars in the directory entry */
\r
815 static BOOL cmp_lfn
\r
816 ( /* TRUE:Matched, FALSE:Not matched */
\r
817 WCHAR *lfnbuf, /* Pointer to the LFN to be compared */
\r
818 BYTE *dir /* Pointer to the directory entry containing a part of LFN */
\r
824 i = ( (dir[LDIR_Ord] & 0xBF) - 1 ) * 13; /* Get offset in the LFN buffer */
\r
829 uc = LD_WORD( dir + LfnOfs[s] ); /* Pick an LFN character from the entry */
\r
831 { /* Last char has not been processed */
\r
832 wc = ff_wtoupper( uc ); /* Convert it to upper case */
\r
833 if( i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++]) )
\r
835 return FALSE; /* Not matched */
\r
842 return FALSE; /* Check filler */
\r
845 } while( ++s < 13 );
\r
847 /* Repeat until all chars in the entry are checked */
\r
849 if( (dir[LDIR_Ord] & 0x40) && wc && lfnbuf[i] )
\r
850 { /* Last segment matched but different length */
\r
854 return TRUE; /* The part of LFN matched */
\r
857 static BOOL pick_lfn
\r
858 ( /* TRUE:Succeeded, FALSE:Buffer overflow */
\r
859 WCHAR *lfnbuf, /* Pointer to the Unicode-LFN buffer */
\r
860 BYTE *dir /* Pointer to the directory entry */
\r
866 i = ( (dir[LDIR_Ord] & 0x3F) - 1 ) * 13; /* Offset in the LFN buffer */
\r
872 uc = LD_WORD( dir + LfnOfs[s] ); /* Pick an LFN character from the entry */
\r
874 { /* Last char has not been processed */
\r
875 if( i >= _MAX_LFN )
\r
877 return FALSE; /* Buffer overflow? */
\r
880 lfnbuf[i++] = wc = uc; /* Store it */
\r
886 return FALSE; /* Check filler */
\r
889 } while( ++s < 13 );
\r
891 /* Read all character in the entry */
\r
893 if( dir[LDIR_Ord] & 0x40 )
\r
894 { /* Put terminator if it is the last LFN part */
\r
895 if( i >= _MAX_LFN )
\r
897 return FALSE; /* Buffer overflow? */
\r
907 static void fit_lfn
\r
909 const WCHAR *lfnbuf, /* Pointer to the LFN buffer */
\r
910 BYTE *dir, /* Pointer to the directory entry */
\r
911 BYTE ord, /* LFN order (1-20) */
\r
912 BYTE sum /* SFN sum */
\r
918 dir[LDIR_Chksum] = sum; /* Set check sum */
\r
919 dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */
\r
920 dir[LDIR_Type] = 0;
\r
921 ST_WORD( dir + LDIR_FstClusLO, 0 );
\r
923 i = ( ord - 1 ) * 13; /* Get offset in the LFN buffer */
\r
929 wc = lfnbuf[i++]; /* Get an effective char */
\r
932 ST_WORD( dir + LfnOfs[s], wc ); /* Put it */
\r
935 wc = 0xFFFF; /* Padding chars following last char */
\r
937 } while( ++s < 13 );
\r
938 if( wc == 0xFFFF || !lfnbuf[i] )
\r
940 ord |= 0x40; /* Bottom LFN part is the start of LFN sequence */
\r
943 dir[LDIR_Ord] = ord; /* Set the LFN order */
\r
949 /*-----------------------------------------------------------------------*/
\r
951 /* Create numbered name */
\r
953 /*-----------------------------------------------------------------------*/
\r
957 BYTE *dst, /* Pointer to genartated SFN */
\r
958 const BYTE *src, /* Pointer to source SFN to be modified */
\r
959 const WCHAR *lfn, /* Pointer to LFN */
\r
960 WORD num /* Sequense number */
\r
966 mem_cpy( dst, src, 11 );
\r
969 { /* On many collisions, generate a hash number instead of sequencial number */
\r
972 num = ( num >> 1 ) + ( num << 15 ) + ( WORD ) * lfn++;
\r
980 ns[i--] = ( num % 10 ) + '0';
\r
985 /* Append the number */
\r
986 for( j = 0; j < i && dst[j] != ' '; j++ )
\r
988 if( IsDBCS1(dst[j]) )
\r
1001 dst[j++] = ( i < 8 ) ? ns[i++] : ' ';
\r
1007 /*-----------------------------------------------------------------------*/
\r
1009 /* Calculate sum of an SFN */
\r
1011 /*-----------------------------------------------------------------------*/
\r
1013 static BYTE sum_sfn( const BYTE *dir /* Ptr to directory entry */ )
\r
1020 sum = ( sum >> 1 ) + ( sum << 7 ) +*dir++;
\r
1027 /*-----------------------------------------------------------------------*/
\r
1029 /* Directory handling - Find an object in the directory */
\r
1031 /*-----------------------------------------------------------------------*/
\r
1032 static FRESULT dir_find( DIR *dj /* Pointer to the directory object linked to the file name */ )
\r
1039 res = dir_seek( dj, 0 ); /* Rewind directory object */
\r
1040 if( res != FR_OK )
\r
1050 res = move_window( dj->fs, dj->sect );
\r
1051 if( res != FR_OK )
\r
1056 dir = dj->dir; /* Ptr to the directory entry of current index */
\r
1057 c = dir[DIR_Name];
\r
1062 } /* Reached to end of table */
\r
1064 #if _USE_LFN /* LFN configuration */
\r
1065 a = dir[DIR_Attr] & AM_MASK;
\r
1066 if( c == 0xE5 || ((a & AM_VOL) && a != AM_LFN) )
\r
1067 { /* An entry without valid data */
\r
1073 { /* An LFN entry is found */
\r
1077 { /* Is it start of LFN sequence? */
\r
1078 sum = dir[LDIR_Chksum];
\r
1080 ord = c; /* LFN start order */
\r
1081 dj->lfn_idx = dj->index;
\r
1084 /* Check validity of the LFN entry and compare it with given name */
\r
1085 ord = ( c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dj->lfn, dir) ) ? ord - 1 : 0xFF;
\r
1089 { /* An SFN entry is found */
\r
1090 if( !ord && sum == sum_sfn(dir) )
\r
1092 break; /* LFN matched? */
\r
1096 dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */
\r
1097 if( !(dj->fn[NS] & NS_LOSS) && !mem_cmp(dir, dj->fn, 11) )
\r
1099 break; /* SFN matched? */
\r
1104 #else /* Non LFN configuration */
\r
1105 if( !(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11) )
\r
1106 { /* Is it a valid entry? */
\r
1111 res = dir_next( dj, FALSE ); /* Next entry */
\r
1112 } while( res == FR_OK );
\r
1117 /*-----------------------------------------------------------------------*/
\r
1119 /* Read an object from the directory */
\r
1121 /*-----------------------------------------------------------------------*/
\r
1122 #if _FS_MINIMIZE <= 1
\r
1123 static FRESULT dir_read( DIR *dj /* Pointer to the directory object that pointing the entry to be read */ )
\r
1128 BYTE a, ord = 0xFF, sum = 0xFF;
\r
1133 res = move_window( dj->fs, dj->sect );
\r
1134 if( res != FR_OK )
\r
1139 dir = dj->dir; /* Ptr to the directory entry of current index */
\r
1140 c = dir[DIR_Name];
\r
1145 } /* Reached to end of table */
\r
1147 #if _USE_LFN /* LFN configuration */
\r
1148 a = dir[DIR_Attr] & AM_MASK;
\r
1149 if( c == 0xE5 || (!_FS_RPATH && c == '.') || ((a & AM_VOL) && a != AM_LFN) )
\r
1150 { /* An entry without valid data */
\r
1156 { /* An LFN entry is found */
\r
1158 { /* Is it start of LFN sequence? */
\r
1159 sum = dir[LDIR_Chksum];
\r
1162 dj->lfn_idx = dj->index;
\r
1165 /* Check LFN validity and capture it */
\r
1166 ord = ( c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dj->lfn, dir) ) ? ord - 1 : 0xFF;
\r
1169 { /* An SFN entry is found */
\r
1170 if( ord || sum != sum_sfn(dir) )
\r
1171 { /* Is there a valid LFN? */
\r
1172 dj->lfn_idx = 0xFFFF; /* It has no LFN. */
\r
1179 #else /* Non LFN configuration */
\r
1180 if( c != 0xE5 && (_FS_RPATH || c != '.') && !(dir[DIR_Attr] & AM_VOL) )
\r
1181 { /* Is it a valid entry? */
\r
1186 res = dir_next( dj, FALSE ); /* Next entry */
\r
1187 if( res != FR_OK )
\r
1193 if( res != FR_OK )
\r
1203 /*-----------------------------------------------------------------------*/
\r
1205 /* Register an object to the directory */
\r
1207 /*-----------------------------------------------------------------------*/
\r
1209 static FRESULT dir_register
\r
1210 ( /* FR_OK:Successful, FR_DENIED:No free entry or too many SFN collision, FR_DISK_ERR:Disk error */
\r
1211 DIR *dj /* Target directory with object name to be created */
\r
1216 #if _USE_LFN /* LFN configuration */
\r
1218 BYTE sn[12], *fn, sum;
\r
1223 mem_cpy( sn, fn, 12 );
\r
1225 if( _FS_RPATH && (sn[NS] & NS_DOT) )
\r
1227 return FR_INVALID_NAME; /* Cannot create dot entry */
\r
1230 if( sn[NS] & NS_LOSS )
\r
1231 { /* When LFN is out of 8.3 format, generate a numbered name */
\r
1233 dj->lfn = NULL; /* Find only SFN */
\r
1234 for( n = 1; n < 100; n++ )
\r
1236 gen_numname( fn, sn, lfn, n ); /* Generate a numbered name */
\r
1237 res = dir_find( dj ); /* Check if the name collides with existing SFN */
\r
1238 if( res != FR_OK )
\r
1246 return FR_DENIED; /* Abort if too many collisions */
\r
1249 if( res != FR_NO_FILE )
\r
1251 return res; /* Abort if the result is other than 'not collided' */
\r
1258 if( sn[NS] & NS_LFN )
\r
1259 { /* When LFN is to be created, reserve reserve an SFN + LFN entries. */
\r
1260 for( ne = 0; lfn[ne]; ne++ );
\r
1261 ne = ( ne + 25 ) / 13;
\r
1264 { /* Otherwise reserve only an SFN entry. */
\r
1268 /* Reserve contiguous entries */
\r
1269 res = dir_seek( dj, 0 );
\r
1270 if( res != FR_OK )
\r
1278 res = move_window( dj->fs, dj->sect );
\r
1279 if( res != FR_OK )
\r
1284 c = *dj->dir; /* Check the entry status */
\r
1285 if( c == 0xE5 || c == 0 )
\r
1286 { /* Is it a blank entry? */
\r
1289 is = dj->index; /* First index of the contigulus entry */
\r
1294 break; /* A contiguous entry that requiered count is found */
\r
1299 n = 0; /* Not a blank entry. Restart to search */
\r
1302 res = dir_next( dj, TRUE ); /* Next entry with table streach */
\r
1303 } while( res == FR_OK );
\r
1305 if( res == FR_OK && ne > 1 )
\r
1306 { /* Initialize LFN entry if needed */
\r
1307 res = dir_seek( dj, is );
\r
1308 if( res == FR_OK )
\r
1310 sum = sum_sfn( dj->fn ); /* Sum of the SFN tied to the LFN */
\r
1313 { /* Store LFN entries in bottom first */
\r
1314 res = move_window( dj->fs, dj->sect );
\r
1315 if( res != FR_OK )
\r
1320 fit_lfn( dj->lfn, dj->dir, (BYTE) ne, sum );
\r
1321 dj->fs->wflag = 1;
\r
1322 res = dir_next( dj, FALSE ); /* Next entry */
\r
1323 } while( res == FR_OK && --ne );
\r
1327 #else /* Non LFN configuration */
\r
1328 res = dir_seek( dj, 0 );
\r
1329 if( res == FR_OK )
\r
1332 { /* Find a blank entry for the SFN */
\r
1333 res = move_window( dj->fs, dj->sect );
\r
1334 if( res != FR_OK )
\r
1340 if( c == 0xE5 || c == 0 )
\r
1342 break; /* Is it a blank entry? */
\r
1345 res = dir_next( dj, TRUE ); /* Next entry with table streach */
\r
1346 } while( res == FR_OK );
\r
1350 if( res == FR_OK )
\r
1351 { /* Initialize the SFN entry */
\r
1352 res = move_window( dj->fs, dj->sect );
\r
1353 if( res == FR_OK )
\r
1356 mem_set( dir, 0, 32 ); /* Clean the entry */
\r
1357 mem_cpy( dir, dj->fn, 11 ); /* Put SFN */
\r
1358 dir[DIR_NTres] = *( dj->fn + NS ) & ( NS_BODY | NS_EXT ); /* Put NT flag */
\r
1359 dj->fs->wflag = 1;
\r
1366 #endif /* !_FS_READONLY */
\r
1368 /*-----------------------------------------------------------------------*/
\r
1370 /* Remove an object from the directory */
\r
1372 /*-----------------------------------------------------------------------*/
\r
1373 #if !_FS_READONLY && !_FS_MINIMIZE
\r
1374 static FRESULT dir_remove
\r
1375 ( /* FR_OK: Successful, FR_DISK_ERR: A disk error */
\r
1376 DIR *dj /* Directory object pointing the entry to be removed */
\r
1380 #if _USE_LFN /* LFN configuration */
\r
1383 i = dj->index; /* SFN index */
\r
1384 res = dir_seek( dj, (WORD) ((dj->lfn_idx == 0xFFFF) ? i : dj->lfn_idx) ); /* Goto the SFN or top of the LFN entries */
\r
1385 if( res == FR_OK )
\r
1389 res = move_window( dj->fs, dj->sect );
\r
1390 if( res != FR_OK )
\r
1395 *dj->dir = 0xE5; /* Mark the entry "deleted" */
\r
1396 dj->fs->wflag = 1;
\r
1397 if( dj->index >= i )
\r
1399 break; /* When reached SFN, all entries of the object has been deleted. */
\r
1402 res = dir_next( dj, FALSE ); /* Next entry */
\r
1403 } while( res == FR_OK );
\r
1404 if( res == FR_NO_FILE )
\r
1410 #else /* Non LFN configuration */
\r
1411 res = dir_seek( dj, dj->index );
\r
1412 if( res == FR_OK )
\r
1414 res = move_window( dj->fs, dj->sect );
\r
1415 if( res == FR_OK )
\r
1417 *dj->dir = 0xE5; /* Mark the entry "deleted" */
\r
1418 dj->fs->wflag = 1;
\r
1426 #endif /* !_FS_READONLY */
\r
1428 /*-----------------------------------------------------------------------*/
\r
1430 /* Pick a segment and create the object name in directory form */
\r
1432 /*-----------------------------------------------------------------------*/
\r
1433 static FRESULT create_name
\r
1435 DIR *dj, /* Pointer to the directory object */
\r
1436 const XCHAR **path /* Pointer to pointer to the segment in the path string */
\r
1441 static const BYTE cvt[] = _EXCVT;
\r
1443 #if _USE_LFN /* LFN configuration */
\r
1446 int i, ni, si, di;
\r
1449 /* Create LFN in Unicode */
\r
1455 w = p[si++]; /* Get a character */
\r
1456 if( w < ' ' || w == '/' || w == '\\' )
\r
1458 break; /* Break on end of segment */
\r
1461 if( di >= _MAX_LFN )
\r
1462 { /* Reject too long name */
\r
1463 return FR_INVALID_NAME;
\r
1469 { /* If it is a DBC 1st byte */
\r
1470 b = p[si++]; /* Get 2nd byte */
\r
1472 { /* Reject invalid code for DBC */
\r
1473 return FR_INVALID_NAME;
\r
1476 w = ( w << 8 ) + b;
\r
1479 w = ff_convert( w, 1 ); /* Convert OEM to Unicode */
\r
1482 return FR_INVALID_NAME; /* Reject invalid code */
\r
1486 if( w < 0x80 && chk_chr("\"*:<>\?|\x7F", w) )
\r
1487 { /* Reject illegal chars for LFN */
\r
1488 return FR_INVALID_NAME;
\r
1491 lfn[di++] = w; /* Store the Unicode char */
\r
1494 *path = &p[si]; /* Rerurn pointer to the next segment */
\r
1495 cf = ( w < ' ' ) ? NS_LAST : 0; /* Set last segment flag if end of path */
\r
1497 if( (di == 1 && lfn[di - 1] == '.') || /* Is this a dot entry? */ (di == 2 && lfn[di - 1] == '.' && lfn[di - 2] == '.') )
\r
1500 for( i = 0; i < 11; i++ )
\r
1502 dj->fn[i] = ( i < di ) ? '.' : ' ';
\r
1505 dj->fn[i] = cf | NS_DOT; /* This is a dot entry */
\r
1511 { /* Strip trailing spaces and dots */
\r
1513 if( w != ' ' && w != '.' )
\r
1523 return FR_INVALID_NAME; /* Reject null string */
\r
1526 lfn[di] = 0; /* LFN is created */
\r
1528 /* Create SFN in directory form */
\r
1529 mem_set( dj->fn, ' ', 11 );
\r
1530 for( si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++ );
\r
1532 /* Strip leading spaces and dots */
\r
1535 cf |= NS_LOSS | NS_LFN;
\r
1538 while( di && lfn[di - 1] != '.' )
\r
1540 di--; /* Find extension (di<=si: no extension) */
\r
1547 w = lfn[si++]; /* Get an LFN char */
\r
1550 break; /* Break on enf of the LFN */
\r
1553 if( w == ' ' || (w == '.' && si != di) )
\r
1554 { /* Remove spaces and dots */
\r
1555 cf |= NS_LOSS | NS_LFN;
\r
1559 if( i >= ni || si == di )
\r
1560 { /* Extension or end of SFN */
\r
1562 { /* Long extension */
\r
1563 cf |= NS_LOSS | NS_LFN;
\r
1569 cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */
\r
1574 break; /* No extension */
\r
1579 ni = 11; /* Enter extension section */
\r
1585 { /* Non ASCII char */
\r
1587 w = ff_convert( w, 0 ); /* Unicode -> OEM code */
\r
1590 w = cvt[w - 0x80]; /* Convert extended char to upper (SBCS) */
\r
1594 w = ff_convert( ff_wtoupper(w), 0 ); /* Upper converted Unicode -> OEM code */
\r
1596 cf |= NS_LFN; /* Force create LFN entry */
\r
1599 if( _DF1S && w >= 0x100 )
\r
1600 { /* Double byte char */
\r
1603 cf |= NS_LOSS | NS_LFN;
\r
1608 dj->fn[i++] = ( BYTE ) ( w >> 8 );
\r
1611 { /* Single byte char */
\r
1612 if( !w || chk_chr("+,;[=]", w) )
\r
1613 { /* Replace illegal chars for SFN */
\r
1615 cf |= NS_LOSS | NS_LFN; /* Lossy conversion */
\r
1620 { /* ASCII large capital */
\r
1626 { /* ASCII small capital */
\r
1634 dj->fn[i++] = ( BYTE ) w;
\r
1637 if( dj->fn[0] == 0xE5 )
\r
1639 dj->fn[0] = 0x05; /* If the first char collides with deleted mark, replace it with 0x05 */
\r
1647 if( (b & 0x0C) == 0x0C || (b & 0x03) == 0x03 )
\r
1648 { /* Create LFN entry when there are composite capitals */
\r
1652 if( !(cf & NS_LFN) )
\r
1653 { /* When LFN is in 8.3 format without extended char, NT flags are created */
\r
1654 if( (b & 0x03) == 0x01 )
\r
1656 cf |= NS_EXT; /* NT flag (Extension has only small capital) */
\r
1659 if( (b & 0x0C) == 0x04 )
\r
1661 cf |= NS_BODY; /* NT flag (Filename has only small capital) */
\r
1665 dj->fn[NS] = cf; /* SFN is created */
\r
1669 #else /* Non-LFN configuration */
\r
1670 BYTE b, c, d, *sfn;
\r
1674 /* Create file name in directory form */
\r
1676 mem_set( sfn, ' ', 11 );
\r
1681 if( p[si] == '.' )
\r
1682 { /* Is this a dot entry? */
\r
1686 if( c != '.' || si >= 3 )
\r
1694 if( c != '/' && c != '\\' && c > ' ' )
\r
1696 return FR_INVALID_NAME;
\r
1699 *path = &p[si]; /* Rerurn pointer to the next segment */
\r
1700 sfn[NS] = ( c <= ' ' ) ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of path */
\r
1708 if( c <= ' ' || c == '/' || c == '\\' )
\r
1710 break; /* Break on end of segment */
\r
1713 if( c == '.' || i >= ni )
\r
1715 if( ni != 8 || c != '.' )
\r
1717 return FR_INVALID_NAME;
\r
1727 { /* Extended char */
\r
1729 c = cvt[c - 0x80]; /* Convert extend char (SBCS) */
\r
1731 b |= 3; /* Eliminate NT flag if ext char is exist */
\r
1732 #if !_DF1S /* ASCII only cfg */
\r
1733 return FR_INVALID_NAME;
\r
1739 { /* DBC 1st byte? */
\r
1740 d = p[si++]; /* Get 2nd byte */
\r
1741 if( !IsDBCS2(d) || i >= ni - 1 )
\r
1742 { /* Reject invalid DBC */
\r
1743 return FR_INVALID_NAME;
\r
1750 { /* Single byte code */
\r
1751 if( chk_chr(" \"*+,[=]|\x7F", c) )
\r
1752 { /* Reject illegal chrs for SFN */
\r
1753 return FR_INVALID_NAME;
\r
1757 { /* ASCII large capital? */
\r
1763 { /* ASCII small capital? */
\r
1773 *path = &p[si]; /* Return pointer to the next segment */
\r
1774 c = ( c <= ' ' ) ? NS_LAST : 0; /* Set last segment flag if end of path */
\r
1778 return FR_INVALID_NAME; /* Reject null string */
\r
1781 if( sfn[0] == 0xE5 )
\r
1783 sfn[0] = 0x05; /* When first char collides with 0xE5, replace it with 0x05 */
\r
1791 if( (b & 0x03) == 0x01 )
\r
1793 c |= NS_EXT; /* NT flag (Extension has only small capital) */
\r
1796 if( (b & 0x0C) == 0x04 )
\r
1798 c |= NS_BODY; /* NT flag (Filename has only small capital) */
\r
1801 sfn[NS] = c; /* Store NT flag, File name is created */
\r
1807 /*-----------------------------------------------------------------------*/
\r
1809 /* Get file information from directory entry */
\r
1811 /*-----------------------------------------------------------------------*/
\r
1812 #if _FS_MINIMIZE <= 1
\r
1813 static void get_fileinfo
\r
1814 ( /* No return code */
\r
1815 DIR *dj, /* Pointer to the directory object */
\r
1816 FILINFO *fno /* Pointer to the file information to be filled */
\r
1827 nt = dir[DIR_NTres]; /* NT flag */
\r
1828 for( i = 0; i < 8; i++ )
\r
1829 { /* Copy name body */
\r
1841 if( _USE_LFN && (nt & NS_BODY) && IsUpper(c) )
\r
1849 if( dir[8] != ' ' )
\r
1850 { /* Copy name extension */
\r
1852 for( i = 8; i < 11; i++ )
\r
1860 if( _USE_LFN && (nt & NS_EXT) && IsUpper(c) )
\r
1869 fno->fattrib = dir[DIR_Attr]; /* Attribute */
\r
1870 fno->fsize = LD_DWORD( dir + DIR_FileSize ); /* Size */
\r
1871 fno->fdate = LD_WORD( dir + DIR_WrtDate ); /* Date */
\r
1872 fno->ftime = LD_WORD( dir + DIR_WrtTime ); /* Time */
\r
1880 XCHAR *tp = fno->lfname;
\r
1884 if( dj->sect && dj->lfn_idx != 0xFFFF )
\r
1885 { /* Get LFN if available */
\r
1887 while( (w = *lfn++) != 0 )
\r
1888 { /* Get an LFN char */
\r
1890 w = ff_convert( w, 0 ); /* Unicode -> OEM conversion */
\r
1895 } /* Could not convert, no LFN */
\r
1897 if( _DF1S && w >= 0x100 )
\r
1898 { /* Put 1st byte if it is a DBC */
\r
1899 tp[i++] = ( XCHAR ) ( w >> 8 );
\r
1903 if( i >= fno->lfsize - 1 )
\r
1907 } /* Buffer overrun, no LFN */
\r
1909 tp[i++] = ( XCHAR ) w;
\r
1913 tp[i] = 0; /* Terminator */
\r
1919 #endif /* _FS_MINIMIZE <= 1 */
\r
1921 /*-----------------------------------------------------------------------*/
\r
1923 /* Follow a file path */
\r
1925 /*-----------------------------------------------------------------------*/
\r
1926 static FRESULT follow_path
\r
1927 ( /* FR_OK(0): successful, !=0: error code */
\r
1928 DIR *dj, /* Directory object to return last directory and found object */
\r
1929 const XCHAR *path /* Full-path string to find a file or directory */
\r
1935 while( !_USE_LFN && *path == ' ' )
\r
1937 path++; /* Skip leading spaces */
\r
1941 if( *path == '/' || *path == '\\' )
\r
1942 { /* There is a heading separator */
\r
1944 dj->sclust = 0; /* Strip it and start from the root dir */
\r
1947 { /* No heading saparator */
\r
1948 dj->sclust = dj->fs->cdir; /* Start from the current dir */
\r
1952 if( *path == '/' || *path == '\\' )
\r
1953 { /* Strip heading separator if exist */
\r
1957 dj->sclust = 0; /* Start from the root dir */
\r
1959 if( (UINT) * path < ' ' )
\r
1960 { /* Null path means the start directory itself */
\r
1961 res = dir_seek( dj, 0 );
\r
1965 { /* Follow path */
\r
1968 res = create_name( dj, &path ); /* Get a segment */
\r
1969 if( res != FR_OK )
\r
1974 res = dir_find( dj ); /* Find it */
\r
1975 last = *( dj->fn + NS ) & NS_LAST;
\r
1976 if( res != FR_OK )
\r
1977 { /* Could not find the object */
\r
1978 if( res == FR_NO_FILE && !last )
\r
1988 break; /* Last segment match. Function completed. */
\r
1991 dir = dj->dir; /* There is next segment. Follow the sub directory */
\r
1992 if( !(dir[DIR_Attr] & AM_DIR) )
\r
1993 { /* Cannot follow because it is a file */
\r
1998 dj->sclust = ( (DWORD) LD_WORD(dir + DIR_FstClusHI) << 16 ) | LD_WORD( dir + DIR_FstClusLO );
\r
2005 /*-----------------------------------------------------------------------*/
\r
2007 /* Load boot record and check if it is an FAT boot record */
\r
2009 /*-----------------------------------------------------------------------*/
\r
2010 static BYTE check_fs
\r
2011 ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record, 3:Error */
\r
2012 FATFS *fs, /* File system object */
\r
2013 DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
\r
2016 if( disk_read(fs->drive, fs->win, sect, 1) != RES_OK )
\r
2017 { /* Load boot record */
\r
2021 if( LD_WORD(&fs->win[BS_55AA]) != 0xAA55 )
\r
2022 { /* Check record signature (always placed at offset 510 even if the sector size is >512) */
\r
2026 if( (LD_DWORD(&fs->win[BS_FilSysType]) & 0xFFFFFF) == 0x544146 )
\r
2027 { /* Check "FAT" string */
\r
2031 if( (LD_DWORD(&fs->win[BS_FilSysType32]) & 0xFFFFFF) == 0x544146 )
\r
2039 /*-----------------------------------------------------------------------*/
\r
2041 /* Make sure that the file system is valid */
\r
2043 /*-----------------------------------------------------------------------*/
\r
2044 FRESULT chk_mounted
\r
2045 ( /* FR_OK(0): successful, !=0: any error occured */
\r
2046 const XCHAR **path, /* Pointer to pointer to the path name (drive number) */
\r
2047 FATFS **rfs, /* Pointer to pointer to the found file system object */
\r
2048 BYTE chk_wp /* !=0: Check media write protection for write access */
\r
2054 DWORD bsect, fsize, tsect, mclst;
\r
2055 const XCHAR *p = *path;
\r
2058 /* Get logical drive number from the path name */
\r
2059 vol = p[0] - '0'; /* Is there a drive number? */
\r
2060 if( vol <= 9 && p[1] == ':' )
\r
2061 { /* Found a drive number, get and strip it */
\r
2063 *path = p; /* Return pointer to the path name */
\r
2066 { /* No drive number is given */
\r
2068 vol = Drive; /* Use current drive */
\r
2070 vol = 0; /* Use drive 0 */
\r
2074 /* Check if the logical drive is valid or not */
\r
2075 if( vol >= _DRIVES )
\r
2076 { /* Is the drive number valid? */
\r
2077 return FR_INVALID_DRIVE;
\r
2080 *rfs = fs = FatFs[vol]; /* Returen pointer to the corresponding file system object */
\r
2083 return FR_NOT_ENABLED; /* Is the file system object available? */
\r
2086 ENTER_FF( fs ); /* Lock file system */
\r
2089 { /* If the logical drive has been mounted */
\r
2090 stat = disk_status( fs->drive );
\r
2091 if( !(stat & STA_NOINIT) )
\r
2092 { /* and the physical drive is kept initialized (has not been changed), */
\r
2094 if( chk_wp && (stat & STA_PROTECT) )
\r
2095 { /* Check write protection if needed */
\r
2096 return FR_WRITE_PROTECTED;
\r
2100 return FR_OK; /* The file system object is valid */
\r
2104 /* The logical drive must be mounted. Following code attempts to mount the volume */
\r
2105 fs->fs_type = 0; /* Clear the file system object */
\r
2106 fs->drive = ( BYTE ) LD2PD( vol ); /* Bind the logical drive and a physical drive */
\r
2107 stat = disk_initialize( fs->drive ); /* Initialize low level disk I/O layer */
\r
2108 if( stat & STA_NOINIT )
\r
2109 { /* Check if the drive is ready */
\r
2110 return FR_NOT_READY;
\r
2113 #if _MAX_SS != 512 /* Get disk sector size if needed */
\r
2114 if( disk_ioctl(fs->drive, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS )
\r
2116 return FR_NO_FILESYSTEM;
\r
2121 if( chk_wp && (stat & STA_PROTECT) )
\r
2122 { /* Check disk write protection if needed */
\r
2123 return FR_WRITE_PROTECTED;
\r
2128 /* Search FAT partition on the drive */
\r
2129 fmt = check_fs( fs, bsect = 0 ); /* Check sector 0 as an SFD format */
\r
2131 { /* Not an FAT boot record, it may be patitioned */
\r
2132 /* Check a partition listed in top of the partition table */
\r
2133 tbl = &fs->win[MBR_Table + LD2PT( vol ) * 16]; /* Partition table */
\r
2135 { /* Is the partition existing? */
\r
2136 bsect = LD_DWORD( &tbl[8] ); /* Partition offset in LBA */
\r
2137 fmt = check_fs( fs, bsect ); /* Check the partition */
\r
2143 return FR_DISK_ERR;
\r
2146 if( fmt || LD_WORD(fs->win + BPB_BytsPerSec) != SS(fs) )
\r
2147 { /* No valid FAT patition is found */
\r
2148 return FR_NO_FILESYSTEM;
\r
2151 /* Initialize the file system object */
\r
2152 fsize = LD_WORD( fs->win + BPB_FATSz16 ); /* Number of sectors per FAT */
\r
2155 fsize = LD_DWORD( fs->win + BPB_FATSz32 );
\r
2158 fs->sects_fat = fsize;
\r
2159 fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FAT copies */
\r
2160 fsize *= fs->n_fats; /* (Number of sectors in FAT area) */
\r
2161 fs->fatbase = bsect + LD_WORD( fs->win + BPB_RsvdSecCnt ); /* FAT start sector (lba) */
\r
2162 fs->csize = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
\r
2163 fs->n_rootdir = LD_WORD( fs->win + BPB_RootEntCnt ); /* Nmuber of root directory entries */
\r
2164 tsect = LD_WORD( fs->win + BPB_TotSec16 ); /* Number of sectors on the volume */
\r
2167 tsect = LD_DWORD( fs->win + BPB_TotSec32 );
\r
2170 fs->max_clust = mclst =
\r
2172 tsect /* Last cluster# + 1 (Number of clusters + 2) */ -
\r
2173 LD_WORD(fs->win + BPB_RsvdSecCnt) -
\r
2181 fmt = FS_FAT12; /* Determine the FAT sub type */
\r
2182 if( mclst >= 0xFF7 )
\r
2184 fmt = FS_FAT16; /* Number of clusters >= 0xFF5 */
\r
2187 if( mclst >= 0xFFF7 )
\r
2189 fmt = FS_FAT32; /* Number of clusters >= 0xFFF5 */
\r
2192 if( fmt == FS_FAT32 )
\r
2194 fs->dirbase = LD_DWORD( fs->win + BPB_RootClus ); /* Root directory start cluster */
\r
2198 fs->dirbase = fs->fatbase + fsize; /* Root directory start sector (lba) */
\r
2201 fs->database = fs->fatbase + fsize + fs->n_rootdir / ( SS(fs) / 32 ); /* Data start sector (lba) */
\r
2205 /* Initialize allocation information */
\r
2206 fs->free_clust = 0xFFFFFFFF;
\r
2209 /* Get fsinfo if needed */
\r
2210 if( fmt == FS_FAT32 )
\r
2213 fs->fsi_sector = bsect + LD_WORD( fs->win + BPB_FSInfo );
\r
2216 disk_read(fs->drive, fs->win, fs->fsi_sector, 1) == RES_OK &&
\r
2217 LD_WORD(fs->win + BS_55AA) == 0xAA55 &&
\r
2218 LD_DWORD(fs->win + FSI_LeadSig) == 0x41615252 &&
\r
2219 LD_DWORD(fs->win + FSI_StrucSig) == 0x61417272
\r
2222 fs->last_clust = LD_DWORD( fs->win + FSI_Nxt_Free );
\r
2223 fs->free_clust = LD_DWORD( fs->win + FSI_Free_Count );
\r
2228 fs->fs_type = fmt; /* FAT sub-type */
\r
2229 fs->winsect = 0; /* Invalidate sector cache */
\r
2231 fs->cdir = 0; /* Current directory (root dir) */
\r
2233 fs->id = ++Fsid; /* File system mount ID */
\r
2238 /*-----------------------------------------------------------------------*/
\r
2240 /* Check if the file/dir object is valid or not */
\r
2242 /*-----------------------------------------------------------------------*/
\r
2243 static FRESULT validate
\r
2244 ( /* FR_OK(0): The object is valid, !=0: Invalid */
\r
2245 FATFS *fs, /* Pointer to the file system object */
\r
2246 WORD id /* Member id of the target object to be checked */
\r
2249 if( !fs || !fs->fs_type || fs->id != id )
\r
2251 return FR_INVALID_OBJECT;
\r
2254 ENTER_FF( fs ); /* Lock file system */
\r
2256 if( disk_status(fs->drive) & STA_NOINIT )
\r
2258 return FR_NOT_READY;
\r
2264 /*--------------------------------------------------------------------------
\r
2268 --------------------------------------------------------------------------*/
\r
2270 /*-----------------------------------------------------------------------*/
\r
2272 /* Mount/Unmount a Locical Drive */
\r
2274 /*-----------------------------------------------------------------------*/
\r
2277 BYTE vol, /* Logical drive number to be mounted/unmounted */
\r
2278 FATFS *fs /* Pointer to new file system object (NULL for unmount)*/
\r
2283 if( vol >= _DRIVES )
\r
2284 { /* Check if the drive number is valid */
\r
2285 return FR_INVALID_DRIVE;
\r
2288 rfs = FatFs[vol]; /* Get current fs object */
\r
2292 #if _FS_REENTRANT /* Discard sync object of the current volume */
\r
2293 if( !ff_del_syncobj(rfs->sobj) )
\r
2295 return FR_INT_ERR;
\r
2299 rfs->fs_type = 0; /* Clear old fs object */
\r
2304 fs->fs_type = 0; /* Clear new fs object */
\r
2305 #if _FS_REENTRANT /* Create sync object for the new volume */
\r
2306 if( !ff_cre_syncobj(vol, &fs->sobj) )
\r
2308 return FR_INT_ERR;
\r
2314 FatFs[vol] = fs; /* Register new fs object */
\r
2319 /*-----------------------------------------------------------------------*/
\r
2321 /* Open or Create a File */
\r
2323 /*-----------------------------------------------------------------------*/
\r
2326 FIL *fp, /* Pointer to the blank file object */
\r
2327 const XCHAR *path, /* Pointer to the file name */
\r
2328 BYTE mode /* Access mode and file open mode flags */
\r
2333 NAMEBUF( sfn, lfn );
\r
2337 fp->fs = NULL; /* Clear file object */
\r
2339 mode &= ( FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW );
\r
2340 res = chk_mounted( &path, &dj.fs, (BYTE) (mode & (FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) );
\r
2343 res = chk_mounted( &path, &dj.fs, 0 );
\r
2345 if( res != FR_OK )
\r
2347 LEAVE_FF( dj.fs, res );
\r
2350 INITBUF( dj, sfn, lfn );
\r
2351 res = follow_path( &dj, path ); /* Follow the file path */
\r
2355 /* Create or Open a file */
\r
2356 if( mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW) )
\r
2360 if( res != FR_OK )
\r
2361 { /* No file, create new */
\r
2362 if( res == FR_NO_FILE )
\r
2363 { /* There is no file to open, create a new entry */
\r
2364 res = dir_register( &dj );
\r
2367 if( res != FR_OK )
\r
2369 LEAVE_FF( dj.fs, res );
\r
2372 mode |= FA_CREATE_ALWAYS;
\r
2373 dir = dj.dir; /* Created entry (SFN entry) */
\r
2376 { /* Any object is already existing */
\r
2377 if( mode & FA_CREATE_NEW )
\r
2378 { /* Cannot create new */
\r
2379 LEAVE_FF( dj.fs, FR_EXIST );
\r
2383 if( !dir || (dir[DIR_Attr] & (AM_RDO | AM_DIR)) )
\r
2384 { /* Cannot overwrite it (R/O or DIR) */
\r
2385 LEAVE_FF( dj.fs, FR_DENIED );
\r
2388 if( mode & FA_CREATE_ALWAYS )
\r
2389 { /* Resize it to zero on over write mode */
\r
2390 cl = ( (DWORD) LD_WORD(dir + DIR_FstClusHI) << 16 ) | LD_WORD( dir + DIR_FstClusLO ); /* Get start cluster */
\r
2391 ST_WORD( dir + DIR_FstClusHI, 0 ); /* cluster = 0 */
\r
2392 ST_WORD( dir + DIR_FstClusLO, 0 );
\r
2393 ST_DWORD( dir + DIR_FileSize, 0 ); /* size = 0 */
\r
2395 ps = dj.fs->winsect; /* Remove the cluster chain */
\r
2398 res = remove_chain( dj.fs, cl );
\r
2401 LEAVE_FF( dj.fs, res );
\r
2404 dj.fs->last_clust = cl - 1; /* Reuse the cluster hole */
\r
2407 res = move_window( dj.fs, ps );
\r
2408 if( res != FR_OK )
\r
2410 LEAVE_FF( dj.fs, res );
\r
2415 if( mode & FA_CREATE_ALWAYS )
\r
2417 dir[DIR_Attr] = 0; /* Reset attribute */
\r
2418 ps = get_fattime();
\r
2419 ST_DWORD( dir + DIR_CrtTime, ps ); /* Created time */
\r
2421 mode |= FA__WRITTEN; /* Set file changed flag */
\r
2425 /* Open an existing file */
\r
2428 #endif /* !_FS_READONLY */
\r
2429 if( res != FR_OK )
\r
2431 LEAVE_FF( dj.fs, res ); /* Follow failed */
\r
2435 if( !dir || (dir[DIR_Attr] & AM_DIR) )
\r
2436 { /* It is a directory */
\r
2437 LEAVE_FF( dj.fs, FR_NO_FILE );
\r
2441 if( (mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO) )
\r
2442 { /* R/O violation */
\r
2443 LEAVE_FF( dj.fs, FR_DENIED );
\r
2447 fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
\r
2448 fp->dir_ptr = dj.dir;
\r
2450 fp->flag = mode; /* File access mode */
\r
2451 fp->org_clust = /* File start cluster */
\r
2452 ( (DWORD) LD_WORD(dir + DIR_FstClusHI) << 16 ) | LD_WORD( dir + DIR_FstClusLO );
\r
2453 fp->fsize = LD_DWORD( dir + DIR_FileSize ); /* File size */
\r
2455 fp->csect = 255; /* File pointer */
\r
2458 fp->id = dj.fs->id; /* Owner file system object of the file */
\r
2460 LEAVE_FF( dj.fs, FR_OK );
\r
2463 /*-----------------------------------------------------------------------*/
\r
2467 /*-----------------------------------------------------------------------*/
\r
2470 FIL *fp, /* Pointer to the file object */
\r
2471 void *buff, /* Pointer to data buffer */
\r
2472 UINT btr, /* Number of bytes to read */
\r
2473 UINT *br /* Pointer to number of bytes read */
\r
2477 DWORD clst, sect, remain;
\r
2479 BYTE *rbuff = buff;
\r
2481 *br = 0; /* Initialize bytes read */
\r
2483 res = validate( fp->fs, fp->id ); /* Check validity of the object */
\r
2484 if( res != FR_OK )
\r
2486 LEAVE_FF( fp->fs, res );
\r
2489 if( fp->flag & FA__ERROR )
\r
2490 { /* Check abort flag */
\r
2491 LEAVE_FF( fp->fs, FR_INT_ERR );
\r
2494 if( !(fp->flag & FA_READ) )
\r
2495 { /* Check access mode */
\r
2496 LEAVE_FF( fp->fs, FR_DENIED );
\r
2499 remain = fp->fsize - fp->fptr;
\r
2500 if( btr > remain )
\r
2502 btr = ( UINT ) remain; /* Truncate btr by remaining bytes */
\r
2505 for( ; btr; /* Repeat until all data transferred */ rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt )
\r
2507 if( (fp->fptr % SS(fp->fs)) == 0 )
\r
2508 { /* On the sector boundary? */
\r
2509 if( fp->csect >= fp->fs->csize )
\r
2510 { /* On the cluster boundary? */
\r
2511 clst = ( fp->fptr == 0 ) ? /* On the top of the file? */
\r
2512 fp->org_clust : get_fat( fp->fs, fp->curr_clust );
\r
2515 ABORT( fp->fs, FR_INT_ERR );
\r
2518 if( clst == 0xFFFFFFFF )
\r
2520 ABORT( fp->fs, FR_DISK_ERR );
\r
2523 fp->curr_clust = clst; /* Update current cluster */
\r
2524 fp->csect = 0; /* Reset sector offset in the cluster */
\r
2527 sect = clust2sect( fp->fs, fp->curr_clust ); /* Get current sector */
\r
2530 ABORT( fp->fs, FR_INT_ERR );
\r
2533 sect += fp->csect;
\r
2534 cc = btr / SS( fp->fs ); /* When remaining bytes >= sector size, */
\r
2536 { /* Read maximum contiguous sectors directly */
\r
2537 if( fp->csect + cc > fp->fs->csize )
\r
2538 { /* Clip at cluster boundary */
\r
2539 cc = fp->fs->csize - fp->csect;
\r
2542 if( disk_read(fp->fs->drive, rbuff, sect, (BYTE) cc) != RES_OK )
\r
2544 ABORT( fp->fs, FR_DISK_ERR );
\r
2547 #if !_FS_READONLY && _FS_MINIMIZE <= 2
\r
2549 if( fp->fs->wflag && fp->fs->winsect - sect < cc )
\r
2550 { /* Replace one of the read sectors with cached data if it contains a dirty sector */
\r
2551 mem_cpy( rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs) );
\r
2555 if( (fp->flag & FA__DIRTY) && fp->dsect - sect < cc )
\r
2556 { /* Replace one of the read sectors with cached data if it contains a dirty sector */
\r
2557 mem_cpy( rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs) );
\r
2562 fp->csect += ( BYTE ) cc; /* Next sector address in the cluster */
\r
2563 rcnt = SS( fp->fs ) * cc; /* Number of bytes transferred */
\r
2569 if( fp->flag & FA__DIRTY )
\r
2570 { /* Write sector I/O buffer if needed */
\r
2571 if( disk_write(fp->fs->drive, fp->buf, fp->dsect, 1) != RES_OK )
\r
2573 ABORT( fp->fs, FR_DISK_ERR );
\r
2576 fp->flag &= ~FA__DIRTY;
\r
2580 if( fp->dsect != sect )
\r
2581 { /* Fill sector buffer with file data */
\r
2582 if( disk_read(fp->fs->drive, fp->buf, sect, 1) != RES_OK )
\r
2584 ABORT( fp->fs, FR_DISK_ERR );
\r
2590 fp->csect++; /* Next sector address in the cluster */
\r
2593 rcnt = SS( fp->fs ) - ( fp->fptr % SS(fp->fs) ); /* Get partial sector data from sector buffer */
\r
2600 if( move_window(fp->fs, fp->dsect) )
\r
2601 { /* Move sector window */
\r
2602 ABORT( fp->fs, FR_DISK_ERR );
\r
2605 mem_cpy( rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt ); /* Pick partial sector */
\r
2607 mem_cpy( rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt ); /* Pick partial sector */
\r
2611 LEAVE_FF( fp->fs, FR_OK );
\r
2616 /*-----------------------------------------------------------------------*/
\r
2620 /*-----------------------------------------------------------------------*/
\r
2623 FIL *fp, /* Pointer to the file object */
\r
2624 const void *buff, /* Pointer to the data to be written */
\r
2625 UINT btw, /* Number of bytes to write */
\r
2626 UINT *bw /* Pointer to number of bytes written */
\r
2632 const BYTE *wbuff = buff;
\r
2634 *bw = 0; /* Initialize bytes written */
\r
2636 res = validate( fp->fs, fp->id ); /* Check validity of the object */
\r
2637 if( res != FR_OK )
\r
2639 LEAVE_FF( fp->fs, res );
\r
2642 if( fp->flag & FA__ERROR )
\r
2643 { /* Check abort flag */
\r
2644 LEAVE_FF( fp->fs, FR_INT_ERR );
\r
2647 if( !(fp->flag & FA_WRITE) )
\r
2648 { /* Check access mode */
\r
2649 LEAVE_FF( fp->fs, FR_DENIED );
\r
2652 if( fp->fsize + btw < fp->fsize )
\r
2654 btw = 0; /* File size cannot reach 4GB */
\r
2657 for( ; btw; /* Repeat until all data transferred */ wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt )
\r
2659 if( (fp->fptr % SS(fp->fs)) == 0 )
\r
2660 { /* On the sector boundary? */
\r
2661 if( fp->csect >= fp->fs->csize )
\r
2662 { /* On the cluster boundary? */
\r
2663 if( fp->fptr == 0 )
\r
2664 { /* On the top of the file? */
\r
2665 clst = fp->org_clust; /* Follow from the origin */
\r
2667 { /* When there is no cluster chain, */
\r
2668 fp->org_clust = clst = create_chain( fp->fs, 0 ); /* Create a new cluster chain */
\r
2672 { /* Middle or end of the file */
\r
2673 clst = create_chain( fp->fs, fp->curr_clust ); /* Follow or streach cluster chain */
\r
2678 break; /* Could not allocate a new cluster (disk full) */
\r
2683 ABORT( fp->fs, FR_INT_ERR );
\r
2686 if( clst == 0xFFFFFFFF )
\r
2688 ABORT( fp->fs, FR_DISK_ERR );
\r
2691 fp->curr_clust = clst; /* Update current cluster */
\r
2692 fp->csect = 0; /* Reset sector address in the cluster */
\r
2696 if( fp->fs->winsect == fp->dsect && move_window(fp->fs, 0) )
\r
2697 { /* Write back data buffer prior to following direct transfer */
\r
2698 ABORT( fp->fs, FR_DISK_ERR );
\r
2702 if( fp->flag & FA__DIRTY )
\r
2703 { /* Write back data buffer prior to following direct transfer */
\r
2704 if( disk_write(fp->fs->drive, fp->buf, fp->dsect, 1) != RES_OK )
\r
2706 ABORT( fp->fs, FR_DISK_ERR );
\r
2709 fp->flag &= ~FA__DIRTY;
\r
2713 sect = clust2sect( fp->fs, fp->curr_clust ); /* Get current sector */
\r
2716 ABORT( fp->fs, FR_INT_ERR );
\r
2719 sect += fp->csect;
\r
2720 cc = btw / SS( fp->fs ); /* When remaining bytes >= sector size, */
\r
2722 { /* Write maximum contiguous sectors directly */
\r
2723 if( fp->csect + cc > fp->fs->csize )
\r
2724 { /* Clip at cluster boundary */
\r
2725 cc = fp->fs->csize - fp->csect;
\r
2728 if( disk_write(fp->fs->drive, wbuff, sect, (BYTE) cc) != RES_OK )
\r
2730 ABORT( fp->fs, FR_DISK_ERR );
\r
2734 if( fp->fs->winsect - sect < cc )
\r
2735 { /* Refill sector cache if it gets dirty by the direct write */
\r
2736 mem_cpy( fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs) );
\r
2737 fp->fs->wflag = 0;
\r
2741 if( fp->dsect - sect < cc )
\r
2742 { /* Refill sector cache if it gets dirty by the direct write */
\r
2743 mem_cpy( fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs) );
\r
2744 fp->flag &= ~FA__DIRTY;
\r
2748 fp->csect += ( BYTE ) cc; /* Next sector address in the cluster */
\r
2749 wcnt = SS( fp->fs ) * cc; /* Number of bytes transferred */
\r
2754 if( fp->fptr >= fp->fsize )
\r
2755 { /* Avoid silly buffer filling at growing edge */
\r
2756 if( move_window(fp->fs, 0) )
\r
2758 ABORT( fp->fs, FR_DISK_ERR );
\r
2761 fp->fs->winsect = sect;
\r
2765 if( fp->dsect != sect )
\r
2766 { /* Fill sector buffer with file data */
\r
2767 if( fp->fptr < fp->fsize && disk_read(fp->fs->drive, fp->buf, sect, 1) != RES_OK )
\r
2769 ABORT( fp->fs, FR_DISK_ERR );
\r
2775 fp->csect++; /* Next sector address in the cluster */
\r
2778 wcnt = SS( fp->fs ) - ( fp->fptr % SS(fp->fs) ); /* Put partial sector into file I/O buffer */
\r
2785 if( move_window(fp->fs, fp->dsect) )
\r
2786 { /* Move sector window */
\r
2787 ABORT( fp->fs, FR_DISK_ERR );
\r
2790 mem_cpy( &fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt ); /* Fit partial sector */
\r
2791 fp->fs->wflag = 1;
\r
2793 mem_cpy( &fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt ); /* Fit partial sector */
\r
2794 fp->flag |= FA__DIRTY;
\r
2798 if( fp->fptr > fp->fsize )
\r
2800 fp->fsize = fp->fptr; /* Update file size if needed */
\r
2803 fp->flag |= FA__WRITTEN; /* Set file changed flag */
\r
2805 LEAVE_FF( fp->fs, FR_OK );
\r
2808 /*-----------------------------------------------------------------------*/
\r
2810 /* Synchronize the File Object */
\r
2812 /*-----------------------------------------------------------------------*/
\r
2813 FRESULT f_sync( FIL *fp /* Pointer to the file object */ )
\r
2819 res = validate( fp->fs, fp->id ); /* Check validity of the object */
\r
2820 if( res == FR_OK )
\r
2822 if( fp->flag & FA__WRITTEN )
\r
2823 { /* Has the file been written? */
\r
2824 #if !_FS_TINY /* Write-back dirty buffer */
\r
2825 if( fp->flag & FA__DIRTY )
\r
2827 if( disk_write(fp->fs->drive, fp->buf, fp->dsect, 1) != RES_OK )
\r
2829 LEAVE_FF( fp->fs, FR_DISK_ERR );
\r
2832 fp->flag &= ~FA__DIRTY;
\r
2837 /* Update the directory entry */
\r
2838 res = move_window( fp->fs, fp->dir_sect );
\r
2839 if( res == FR_OK )
\r
2841 dir = fp->dir_ptr;
\r
2842 dir[DIR_Attr] |= AM_ARC; /* Set archive bit */
\r
2843 ST_DWORD( dir + DIR_FileSize, fp->fsize ); /* Update file size */
\r
2844 ST_WORD( dir + DIR_FstClusLO, fp->org_clust ); /* Update start cluster */
\r
2845 ST_WORD( dir + DIR_FstClusHI, fp->org_clust >> 16 );
\r
2846 tim = get_fattime(); /* Updated time */
\r
2847 ST_DWORD( dir + DIR_WrtTime, tim );
\r
2848 fp->flag &= ~FA__WRITTEN;
\r
2849 fp->fs->wflag = 1;
\r
2850 res = sync( fp->fs );
\r
2855 LEAVE_FF( fp->fs, res );
\r
2858 #endif /* !_FS_READONLY */
\r
2860 /*-----------------------------------------------------------------------*/
\r
2864 /*-----------------------------------------------------------------------*/
\r
2865 FRESULT f_close( FIL *fp /* Pointer to the file object to be closed */ )
\r
2870 res = validate( fp->fs, fp->id );
\r
2871 if( res == FR_OK )
\r
2876 LEAVE_FF( fp->fs, res );
\r
2878 res = f_sync( fp );
\r
2879 if( res == FR_OK )
\r
2888 /*-----------------------------------------------------------------------*/
\r
2890 /* Change Current Drive/Directory */
\r
2892 /*-----------------------------------------------------------------------*/
\r
2894 FRESULT f_chdrive( BYTE drv /* Drive number */ )
\r
2896 if( drv >= _DRIVES )
\r
2898 return FR_INVALID_DRIVE;
\r
2906 FRESULT f_chdir( const XCHAR *path /* Pointer to the directory path */ )
\r
2910 NAMEBUF( sfn, lfn );
\r
2914 res = chk_mounted( &path, &dj.fs, 0 );
\r
2915 if( res == FR_OK )
\r
2917 INITBUF( dj, sfn, lfn );
\r
2918 res = follow_path( &dj, path ); /* Follow the file path */
\r
2919 if( res == FR_OK )
\r
2920 { /* Follow completed */
\r
2921 dir = dj.dir; /* Pointer to the entry */
\r
2924 dj.fs->cdir = 0; /* No entry (root dir) */
\r
2928 if( dir[DIR_Attr] & AM_DIR )
\r
2929 { /* Reached to the dir */
\r
2930 dj.fs->cdir = ( (DWORD) LD_WORD(dir + DIR_FstClusHI) << 16 ) | LD_WORD( dir + DIR_FstClusLO );
\r
2934 res = FR_NO_PATH; /* Could not reach the dir (it is a file) */
\r
2939 if( res == FR_NO_FILE )
\r
2945 LEAVE_FF( dj.fs, res );
\r
2949 #if _FS_MINIMIZE <= 2
\r
2951 /*-----------------------------------------------------------------------*/
\r
2953 /* Seek File R/W Pointer */
\r
2955 /*-----------------------------------------------------------------------*/
\r
2956 FRESULT f_lseek( FIL *fp, /* Pointer to the file object */ DWORD ofs /* File pointer from top of file */ )
\r
2959 DWORD clst, bcs, nsect, ifptr;
\r
2961 res = validate( fp->fs, fp->id ); /* Check validity of the object */
\r
2962 if( res != FR_OK )
\r
2964 LEAVE_FF( fp->fs, res );
\r
2967 if( fp->flag & FA__ERROR )
\r
2968 { /* Check abort flag */
\r
2969 LEAVE_FF( fp->fs, FR_INT_ERR );
\r
2972 if( ofs > fp->fsize /* In read-only mode, clip offset with the file size */
\r
2974 && !(fp->flag & FA_WRITE)
\r
2982 fp->fptr = nsect = 0;
\r
2986 bcs = ( DWORD ) fp->fs->csize * SS( fp->fs ); /* Cluster size (byte) */
\r
2987 if( ifptr > 0 && (ofs - 1) / bcs >= (ifptr - 1) / bcs )
\r
2988 { /* When seek to same or following cluster, */
\r
2989 fp->fptr = ( ifptr - 1 ) &~( bcs - 1 ); /* start from the current cluster */
\r
2991 clst = fp->curr_clust;
\r
2994 { /* When seek to back cluster, */
\r
2995 clst = fp->org_clust; /* start from the first cluster */
\r
2998 { /* If no cluster chain, create a new chain */
\r
2999 clst = create_chain( fp->fs, 0 );
\r
3002 ABORT( fp->fs, FR_INT_ERR );
\r
3005 if( clst == 0xFFFFFFFF )
\r
3007 ABORT( fp->fs, FR_DISK_ERR );
\r
3010 fp->org_clust = clst;
\r
3014 fp->curr_clust = clst;
\r
3019 while( ofs > bcs )
\r
3020 { /* Cluster following loop */
\r
3022 if( fp->flag & FA_WRITE )
\r
3023 { /* Check if in write mode or not */
\r
3024 clst = create_chain( fp->fs, clst ); /* Force streached if in write mode */
\r
3026 { /* When disk gets full, clip file size */
\r
3033 clst = get_fat( fp->fs, clst ); /* Follow cluster chain if not in write mode */
\r
3034 if( clst == 0xFFFFFFFF )
\r
3036 ABORT( fp->fs, FR_DISK_ERR );
\r
3039 if( clst <= 1 || clst >= fp->fs->max_clust )
\r
3041 ABORT( fp->fs, FR_INT_ERR );
\r
3044 fp->curr_clust = clst;
\r
3050 fp->csect = ( BYTE ) ( ofs / SS(fp->fs) ); /* Sector offset in the cluster */
\r
3051 if( ofs % SS(fp->fs) )
\r
3053 nsect = clust2sect( fp->fs, clst ); /* Current sector */
\r
3056 ABORT( fp->fs, FR_INT_ERR );
\r
3059 nsect += fp->csect;
\r
3065 if( fp->fptr % SS(fp->fs) && nsect != fp->dsect )
\r
3069 if( fp->flag & FA__DIRTY )
\r
3070 { /* Write-back dirty buffer if needed */
\r
3071 if( disk_write(fp->fs->drive, fp->buf, fp->dsect, 1) != RES_OK )
\r
3073 ABORT( fp->fs, FR_DISK_ERR );
\r
3076 fp->flag &= ~FA__DIRTY;
\r
3080 if( disk_read(fp->fs->drive, fp->buf, nsect, 1) != RES_OK )
\r
3082 ABORT( fp->fs, FR_DISK_ERR );
\r
3086 fp->dsect = nsect;
\r
3090 if( fp->fptr > fp->fsize )
\r
3091 { /* Set changed flag if the file size is extended */
\r
3092 fp->fsize = fp->fptr;
\r
3093 fp->flag |= FA__WRITTEN;
\r
3097 LEAVE_FF( fp->fs, res );
\r
3100 #if _FS_MINIMIZE <= 1
\r
3102 /*-----------------------------------------------------------------------*/
\r
3104 /* Create a Directroy Object */
\r
3106 /*-----------------------------------------------------------------------*/
\r
3107 FRESULT f_opendir( DIR *dj, /* Pointer to directory object to create */ const XCHAR *path /* Pointer to the directory path */ )
\r
3110 NAMEBUF( sfn, lfn );
\r
3114 res = chk_mounted( &path, &dj->fs, 0 );
\r
3115 if( res == FR_OK )
\r
3117 INITBUF( (*dj), sfn, lfn );
\r
3118 res = follow_path( dj, path ); /* Follow the path to the directory */
\r
3119 if( res == FR_OK )
\r
3120 { /* Follow completed */
\r
3123 { /* It is not the root dir */
\r
3124 if( dir[DIR_Attr] & AM_DIR )
\r
3125 { /* The object is a directory */
\r
3126 dj->sclust = ( (DWORD) LD_WORD(dir + DIR_FstClusHI) << 16 ) | LD_WORD( dir + DIR_FstClusLO );
\r
3129 { /* The object is not a directory */
\r
3134 if( res == FR_OK )
\r
3136 dj->id = dj->fs->id;
\r
3137 res = dir_seek( dj, 0 ); /* Rewind dir */
\r
3141 if( res == FR_NO_FILE )
\r
3147 LEAVE_FF( dj->fs, res );
\r
3150 /*-----------------------------------------------------------------------*/
\r
3152 /* Read Directory Entry in Sequense */
\r
3154 /*-----------------------------------------------------------------------*/
\r
3155 FRESULT f_readdir( DIR *dj, /* Pointer to the open directory object */ FILINFO *fno /* Pointer to file information to return */ )
\r
3158 NAMEBUF( sfn, lfn );
\r
3160 res = validate( dj->fs, dj->id ); /* Check validity of the object */
\r
3161 if( res == FR_OK )
\r
3163 INITBUF( (*dj), sfn, lfn );
\r
3166 res = dir_seek( dj, 0 );
\r
3170 res = dir_read( dj );
\r
3171 if( res == FR_NO_FILE )
\r
3177 if( res == FR_OK )
\r
3178 { /* A valid entry is found */
\r
3179 get_fileinfo( dj, fno ); /* Get the object information */
\r
3180 res = dir_next( dj, FALSE ); /* Increment index for next */
\r
3181 if( res == FR_NO_FILE )
\r
3190 LEAVE_FF( dj->fs, res );
\r
3193 #if _FS_MINIMIZE == 0
\r
3195 /*-----------------------------------------------------------------------*/
\r
3197 /* Get File Status */
\r
3199 /*-----------------------------------------------------------------------*/
\r
3200 FRESULT f_stat( const XCHAR *path, /* Pointer to the file path */ FILINFO *fno /* Pointer to file information to return */ )
\r
3204 NAMEBUF( sfn, lfn );
\r
3206 res = chk_mounted( &path, &dj.fs, 0 );
\r
3207 if( res == FR_OK )
\r
3209 INITBUF( dj, sfn, lfn );
\r
3210 res = follow_path( &dj, path ); /* Follow the file path */
\r
3211 if( res == FR_OK )
\r
3212 { /* Follwo completed */
\r
3214 { /* Found an object */
\r
3215 get_fileinfo( &dj, fno );
\r
3218 { /* It is root dir */
\r
3219 res = FR_INVALID_NAME;
\r
3224 LEAVE_FF( dj.fs, res );
\r
3229 /*-----------------------------------------------------------------------*/
\r
3231 /* Get Number of Free Clusters */
\r
3233 /*-----------------------------------------------------------------------*/
\r
3236 const XCHAR *path, /* Pointer to the logical drive number (root dir) */
\r
3237 DWORD *nclst, /* Pointer to the variable to return number of free clusters */
\r
3238 FATFS **fatfs /* Pointer to pointer to corresponding file system object to return */
\r
3242 DWORD n, clst, sect, stat;
\r
3246 /* Get drive number */
\r
3247 res = chk_mounted( &path, fatfs, 0 );
\r
3248 if( res != FR_OK )
\r
3250 LEAVE_FF( *fatfs, res );
\r
3253 /* If number of free cluster is valid, return it without cluster scan. */
\r
3254 if( (*fatfs)->free_clust <= (*fatfs)->max_clust - 2 )
\r
3256 *nclst = ( *fatfs )->free_clust;
\r
3257 LEAVE_FF( *fatfs, FR_OK );
\r
3260 /* Get number of free clusters */
\r
3261 fat = ( *fatfs )->fs_type;
\r
3263 if( fat == FS_FAT12 )
\r
3268 stat = get_fat( *fatfs, clst );
\r
3269 if( stat == 0xFFFFFFFF )
\r
3271 LEAVE_FF( *fatfs, FR_DISK_ERR );
\r
3276 LEAVE_FF( *fatfs, FR_INT_ERR );
\r
3283 } while( ++clst < (*fatfs)->max_clust );
\r
3287 clst = ( *fatfs )->max_clust;
\r
3288 sect = ( *fatfs )->fatbase;
\r
3295 res = move_window( *fatfs, sect++ );
\r
3296 if( res != FR_OK )
\r
3298 LEAVE_FF( *fatfs, res );
\r
3301 p = ( *fatfs )->win;
\r
3305 if( fat == FS_FAT16 )
\r
3307 if( LD_WORD(p) == 0 )
\r
3317 if( (LD_DWORD(p) & 0x0FFFFFFF) == 0 )
\r
3325 } while( --clst );
\r
3328 ( *fatfs )->free_clust = n;
\r
3329 if( fat == FS_FAT32 )
\r
3331 ( *fatfs )->fsi_flag = 1;
\r
3336 LEAVE_FF( *fatfs, FR_OK );
\r
3339 /*-----------------------------------------------------------------------*/
\r
3341 /* Truncate File */
\r
3343 /*-----------------------------------------------------------------------*/
\r
3344 FRESULT f_truncate( FIL *fp /* Pointer to the file object */ )
\r
3349 res = validate( fp->fs, fp->id ); /* Check validity of the object */
\r
3350 if( res != FR_OK )
\r
3352 LEAVE_FF( fp->fs, res );
\r
3355 if( fp->flag & FA__ERROR )
\r
3356 { /* Check abort flag */
\r
3357 LEAVE_FF( fp->fs, FR_INT_ERR );
\r
3360 if( !(fp->flag & FA_WRITE) )
\r
3361 { /* Check access mode */
\r
3362 LEAVE_FF( fp->fs, FR_DENIED );
\r
3365 if( fp->fsize > fp->fptr )
\r
3367 fp->fsize = fp->fptr; /* Set file size to current R/W point */
\r
3368 fp->flag |= FA__WRITTEN;
\r
3369 if( fp->fptr == 0 )
\r
3370 { /* When set file size to zero, remove entire cluster chain */
\r
3371 res = remove_chain( fp->fs, fp->org_clust );
\r
3372 fp->org_clust = 0;
\r
3375 { /* When truncate a part of the file, remove remaining clusters */
\r
3376 ncl = get_fat( fp->fs, fp->curr_clust );
\r
3378 if( ncl == 0xFFFFFFFF )
\r
3380 res = FR_DISK_ERR;
\r
3388 if( res == FR_OK && ncl < fp->fs->max_clust )
\r
3390 res = put_fat( fp->fs, fp->curr_clust, 0x0FFFFFFF );
\r
3391 if( res == FR_OK )
\r
3393 res = remove_chain( fp->fs, ncl );
\r
3399 if( res != FR_OK )
\r
3401 fp->flag |= FA__ERROR;
\r
3404 LEAVE_FF( fp->fs, res );
\r
3407 /*-----------------------------------------------------------------------*/
\r
3409 /* Delete a File or Directory */
\r
3411 /*-----------------------------------------------------------------------*/
\r
3412 FRESULT f_unlink( const XCHAR *path /* Pointer to the file or directory path */ )
\r
3416 NAMEBUF( sfn, lfn );
\r
3421 res = chk_mounted( &path, &dj.fs, 1 );
\r
3422 if( res != FR_OK )
\r
3424 LEAVE_FF( dj.fs, res );
\r
3427 INITBUF( dj, sfn, lfn );
\r
3428 res = follow_path( &dj, path ); /* Follow the file path */
\r
3429 if( _FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT) )
\r
3431 res = FR_INVALID_NAME;
\r
3434 if( res != FR_OK )
\r
3436 LEAVE_FF( dj.fs, res ); /* Follow failed */
\r
3441 { /* Is it the root directory? */
\r
3442 LEAVE_FF( dj.fs, FR_INVALID_NAME );
\r
3445 if( dir[DIR_Attr] & AM_RDO )
\r
3446 { /* Is it a R/O object? */
\r
3447 LEAVE_FF( dj.fs, FR_DENIED );
\r
3450 dclst = ( (DWORD) LD_WORD(dir + DIR_FstClusHI) << 16 ) | LD_WORD( dir + DIR_FstClusLO );
\r
3452 if( dir[DIR_Attr] & AM_DIR )
\r
3453 { /* It is a sub-directory */
\r
3456 LEAVE_FF( dj.fs, FR_INT_ERR );
\r
3459 mem_cpy( &sdj, &dj, sizeof(DIR) ); /* Check if the sub-dir is empty or not */
\r
3460 sdj.sclust = dclst;
\r
3461 res = dir_seek( &sdj, 2 );
\r
3462 if( res != FR_OK )
\r
3464 LEAVE_FF( dj.fs, res );
\r
3467 res = dir_read( &sdj );
\r
3468 if( res == FR_OK )
\r
3470 res = FR_DENIED; /* Not empty sub-dir */
\r
3473 if( res != FR_NO_FILE )
\r
3475 LEAVE_FF( dj.fs, res );
\r
3479 res = dir_remove( &dj ); /* Remove directory entry */
\r
3480 if( res == FR_OK )
\r
3484 res = remove_chain( dj.fs, dclst ); /* Remove the cluster chain */
\r
3487 if( res == FR_OK )
\r
3489 res = sync( dj.fs );
\r
3493 LEAVE_FF( dj.fs, res );
\r
3496 /*-----------------------------------------------------------------------*/
\r
3498 /* Create a Directory */
\r
3500 /*-----------------------------------------------------------------------*/
\r
3501 FRESULT f_mkdir( const XCHAR *path /* Pointer to the directory path */ )
\r
3505 NAMEBUF( sfn, lfn );
\r
3508 DWORD dsect, dclst, pclst, tim;
\r
3510 res = chk_mounted( &path, &dj.fs, 1 );
\r
3511 if( res != FR_OK )
\r
3513 LEAVE_FF( dj.fs, res );
\r
3516 INITBUF( dj, sfn, lfn );
\r
3517 res = follow_path( &dj, path ); /* Follow the file path */
\r
3518 if( res == FR_OK )
\r
3520 res = FR_EXIST; /* Any file or directory is already existing */
\r
3523 if( _FS_RPATH && res == FR_NO_FILE && (dj.fn[NS] & NS_DOT) )
\r
3525 res = FR_INVALID_NAME;
\r
3528 if( res != FR_NO_FILE )
\r
3529 { /* Any error occured */
\r
3530 LEAVE_FF( dj.fs, res );
\r
3533 dclst = create_chain( dj.fs, 0 ); /* Allocate a new cluster for new directory table */
\r
3545 if( dclst == 0xFFFFFFFF )
\r
3547 res = FR_DISK_ERR;
\r
3550 if( res == FR_OK )
\r
3552 res = move_window( dj.fs, 0 );
\r
3555 if( res != FR_OK )
\r
3557 LEAVE_FF( dj.fs, res );
\r
3560 dsect = clust2sect( dj.fs, dclst );
\r
3562 dir = dj.fs->win; /* Initialize the new directory table */
\r
3563 mem_set( dir, 0, SS(dj.fs) );
\r
3564 mem_set( dir + DIR_Name, ' ', 8 + 3 ); /* Create "." entry */
\r
3565 dir[DIR_Name] = '.';
\r
3566 dir[DIR_Attr] = AM_DIR;
\r
3567 tim = get_fattime();
\r
3568 ST_DWORD( dir + DIR_WrtTime, tim );
\r
3569 ST_WORD( dir + DIR_FstClusLO, dclst );
\r
3570 ST_WORD( dir + DIR_FstClusHI, dclst >> 16 );
\r
3571 mem_cpy( dir + 32, dir, 32 ); /* Create ".." entry */
\r
3573 pclst = dj.sclust;
\r
3574 if( dj.fs->fs_type == FS_FAT32 && pclst == dj.fs->dirbase )
\r
3579 ST_WORD( dir + 32 + DIR_FstClusLO, pclst );
\r
3580 ST_WORD( dir + 32 + DIR_FstClusHI, pclst >> 16 );
\r
3581 for( n = 0; n < dj.fs->csize; n++ )
\r
3582 { /* Write dot entries and clear left sectors */
\r
3583 dj.fs->winsect = dsect++;
\r
3585 res = move_window( dj.fs, 0 );
\r
3588 LEAVE_FF( dj.fs, res );
\r
3591 mem_set( dir, 0, SS(dj.fs) );
\r
3594 res = dir_register( &dj );
\r
3595 if( res != FR_OK )
\r
3597 remove_chain( dj.fs, dclst );
\r
3602 dir[DIR_Attr] = AM_DIR; /* Attribute */
\r
3603 ST_DWORD( dir + DIR_WrtTime, tim ); /* Crated time */
\r
3604 ST_WORD( dir + DIR_FstClusLO, dclst ); /* Table start cluster */
\r
3605 ST_WORD( dir + DIR_FstClusHI, dclst >> 16 );
\r
3607 res = sync( dj.fs );
\r
3610 LEAVE_FF( dj.fs, res );
\r
3613 /*-----------------------------------------------------------------------*/
\r
3615 /* Change File Attribute */
\r
3617 /*-----------------------------------------------------------------------*/
\r
3618 FRESULT f_chmod( const XCHAR *path, /* Pointer to the file path */ BYTE value, /* Attribute bits */ BYTE mask /* Attribute mask to change */ )
\r
3622 NAMEBUF( sfn, lfn );
\r
3626 res = chk_mounted( &path, &dj.fs, 1 );
\r
3627 if( res == FR_OK )
\r
3629 INITBUF( dj, sfn, lfn );
\r
3630 res = follow_path( &dj, path ); /* Follow the file path */
\r
3631 if( _FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT) )
\r
3633 res = FR_INVALID_NAME;
\r
3636 if( res == FR_OK )
\r
3640 { /* Is it a root directory? */
\r
3641 res = FR_INVALID_NAME;
\r
3644 { /* File or sub directory */
\r
3645 mask &= AM_RDO | AM_HID | AM_SYS | AM_ARC; /* Valid attribute mask */
\r
3646 dir[DIR_Attr] = ( value & mask ) | ( dir[DIR_Attr] & (BYTE)~mask ); /* Apply attribute change */
\r
3648 res = sync( dj.fs );
\r
3653 LEAVE_FF( dj.fs, res );
\r
3656 /*-----------------------------------------------------------------------*/
\r
3658 /* Change Timestamp */
\r
3660 /*-----------------------------------------------------------------------*/
\r
3661 FRESULT f_utime( const XCHAR *path, /* Pointer to the file/directory name */ const FILINFO *fno /* Pointer to the timestamp to be set */ )
\r
3665 NAMEBUF( sfn, lfn );
\r
3669 res = chk_mounted( &path, &dj.fs, 1 );
\r
3670 if( res == FR_OK )
\r
3672 INITBUF( dj, sfn, lfn );
\r
3673 res = follow_path( &dj, path ); /* Follow the file path */
\r
3674 if( _FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT) )
\r
3676 res = FR_INVALID_NAME;
\r
3679 if( res == FR_OK )
\r
3683 { /* Root directory */
\r
3684 res = FR_INVALID_NAME;
\r
3687 { /* File or sub-directory */
\r
3688 ST_WORD( dir + DIR_WrtTime, fno->ftime );
\r
3689 ST_WORD( dir + DIR_WrtDate, fno->fdate );
\r
3691 res = sync( dj.fs );
\r
3696 LEAVE_FF( dj.fs, res );
\r
3699 /*-----------------------------------------------------------------------*/
\r
3701 /* Rename File/Directory */
\r
3703 /*-----------------------------------------------------------------------*/
\r
3704 FRESULT f_rename( const XCHAR *path_old, /* Pointer to the old name */ const XCHAR *path_new /* Pointer to the new name */ )
\r
3707 DIR dj_old, dj_new;
\r
3708 NAMEBUF( sfn, lfn );
\r
3710 BYTE buf[21], *dir;
\r
3713 INITBUF( dj_old, sfn, lfn );
\r
3714 res = chk_mounted( &path_old, &dj_old.fs, 1 );
\r
3715 if( res == FR_OK )
\r
3717 dj_new.fs = dj_old.fs;
\r
3718 res = follow_path( &dj_old, path_old ); /* Check old object */
\r
3719 if( _FS_RPATH && res == FR_OK && (dj_old.fn[NS] & NS_DOT) )
\r
3721 res = FR_INVALID_NAME;
\r
3725 if( res != FR_OK )
\r
3727 LEAVE_FF( dj_old.fs, res ); /* The old object is not found */
\r
3732 LEAVE_FF( dj_old.fs, FR_NO_FILE ); /* Is root dir? */
\r
3735 mem_cpy( buf, dj_old.dir + DIR_Attr, 21 ); /* Save the object information */
\r
3737 mem_cpy( &dj_new, &dj_old, sizeof(DIR) );
\r
3738 res = follow_path( &dj_new, path_new ); /* Check new object */
\r
3739 if( res == FR_OK )
\r
3741 res = FR_EXIST; /* The new object name is already existing */
\r
3744 if( res == FR_NO_FILE )
\r
3745 { /* Is it a valid path and no name collision? */
\r
3746 res = dir_register( &dj_new ); /* Register the new object */
\r
3747 if( res == FR_OK )
\r
3749 dir = dj_new.dir; /* Copy object information into new entry */
\r
3750 mem_cpy( dir + 13, buf + 2, 19 );
\r
3751 dir[DIR_Attr] = buf[0] | AM_ARC;
\r
3752 dj_old.fs->wflag = 1;
\r
3753 if( dir[DIR_Attr] & AM_DIR )
\r
3754 { /* Update .. entry in the directory if needed */
\r
3755 dw = clust2sect( dj_new.fs, (DWORD) LD_WORD(dir + DIR_FstClusHI) | LD_WORD(dir + DIR_FstClusLO) );
\r
3762 res = move_window( dj_new.fs, dw );
\r
3763 dir = dj_new.fs->win + 32;
\r
3764 if( res == FR_OK && dir[1] == '.' )
\r
3766 dw = ( dj_new.fs->fs_type == FS_FAT32 && dj_new.sclust == dj_new.fs->dirbase ) ? 0 : dj_new.sclust;
\r
3767 ST_WORD( dir + DIR_FstClusLO, dw );
\r
3768 ST_WORD( dir + DIR_FstClusHI, dw >> 16 );
\r
3769 dj_new.fs->wflag = 1;
\r
3774 if( res == FR_OK )
\r
3776 res = dir_remove( &dj_old ); /* Remove old entry */
\r
3777 if( res == FR_OK )
\r
3779 res = sync( dj_old.fs );
\r
3785 LEAVE_FF( dj_old.fs, res );
\r
3788 #endif /* !_FS_READONLY */
\r
3789 #endif /* _FS_MINIMIZE == 0 */
\r
3790 #endif /* _FS_MINIMIZE <= 1 */
\r
3791 #endif /* _FS_MINIMIZE <= 2 */
\r
3793 /*-----------------------------------------------------------------------*/
\r
3795 /* Forward data to the stream directly (Available on only _FS_TINY cfg) */
\r
3797 /*-----------------------------------------------------------------------*/
\r
3798 #if _USE_FORWARD && _FS_TINY
\r
3801 FIL *fp, /* Pointer to the file object */
\r
3802 UINT (*func) ( const BYTE *, UINT ), /* Pointer to the streaming function */
\r
3803 UINT btr, /* Number of bytes to forward */
\r
3804 UINT *bf /* Pointer to number of bytes forwarded */
\r
3808 DWORD remain, clst, sect;
\r
3813 res = validate( fp->fs, fp->id ); /* Check validity of the object */
\r
3814 if( res != FR_OK )
\r
3816 LEAVE_FF( fp->fs, res );
\r
3819 if( fp->flag & FA__ERROR )
\r
3820 { /* Check error flag */
\r
3821 LEAVE_FF( fp->fs, FR_INT_ERR );
\r
3824 if( !(fp->flag & FA_READ) )
\r
3825 { /* Check access mode */
\r
3826 LEAVE_FF( fp->fs, FR_DENIED );
\r
3829 remain = fp->fsize - fp->fptr;
\r
3830 if( btr > remain )
\r
3832 btr = ( UINT ) remain; /* Truncate btr by remaining bytes */
\r
3838 btr && (*func) (NULL, 0); /* Repeat until all data transferred or stream becomes busy */
\r
3839 fp->fptr += rcnt, *bf += rcnt, btr -= rcnt
\r
3842 if( (fp->fptr % SS(fp->fs)) == 0 )
\r
3843 { /* On the sector boundary? */
\r
3844 if( fp->csect >= fp->fs->csize )
\r
3845 { /* On the cluster boundary? */
\r
3846 clst = ( fp->fptr == 0 ) ? /* On the top of the file? */
\r
3847 fp->org_clust : get_fat( fp->fs, fp->curr_clust );
\r
3850 ABORT( fp->fs, FR_INT_ERR );
\r
3853 if( clst == 0xFFFFFFFF )
\r
3855 ABORT( fp->fs, FR_DISK_ERR );
\r
3858 fp->curr_clust = clst; /* Update current cluster */
\r
3859 fp->csect = 0; /* Reset sector address in the cluster */
\r
3862 fp->csect++; /* Next sector address in the cluster */
\r
3865 sect = clust2sect( fp->fs, fp->curr_clust ); /* Get current data sector */
\r
3868 ABORT( fp->fs, FR_INT_ERR );
\r
3871 sect += fp->csect - 1;
\r
3872 if( move_window(fp->fs, sect) )
\r
3873 { /* Move sector window */
\r
3874 ABORT( fp->fs, FR_DISK_ERR );
\r
3878 rcnt = SS( fp->fs ) - ( WORD ) ( fp->fptr % SS(fp->fs) ); /* Forward data from sector window */
\r
3884 rcnt = ( *func ) ( &fp->fs->win[(WORD) fp->fptr % SS(fp->fs)], rcnt );
\r
3887 ABORT( fp->fs, FR_INT_ERR );
\r
3891 LEAVE_FF( fp->fs, FR_OK );
\r
3894 #endif /* _USE_FORWARD */
\r
3896 #if _USE_MKFS && !_FS_READONLY
\r
3898 /*-----------------------------------------------------------------------*/
\r
3900 /* Create File System on the Drive */
\r
3902 /*-----------------------------------------------------------------------*/
\r
3903 #define N_ROOTDIR 512 /* Multiple of 32 and <= 2048 */
\r
3904 #define N_FATS 1 /* 1 or 2 */
\r
3905 #define MAX_SECTOR 131072000UL /* Maximum partition size */
\r
3906 #define MIN_SECTOR 2000UL /* Minimum partition size */
\r
3910 BYTE drv, /* Logical drive number */
\r
3911 BYTE partition, /* Partitioning rule 0:FDISK, 1:SFD */
\r
3912 WORD allocsize /* Allocation unit size [bytes] */
\r
3915 static const DWORD sstbl[] = { 2048000, 1024000, 512000, 256000, 128000, 64000, 32000, 16000, 8000, 4000, 0 };
\r
3916 static const WORD cstbl[] = { 32768, 16384, 8192, 4096, 2048, 16384, 8192, 4096, 2048, 1024, 512 };
\r
3917 BYTE fmt, m, *tbl;
\r
3918 DWORD b_part, b_fat, b_dir, b_data; /* Area offset (LBA) */
\r
3919 DWORD n_part, n_rsv, n_fat, n_dir; /* Area size */
\r
3920 DWORD n_clst, d, n;
\r
3925 /* Check validity of the parameters */
\r
3926 if( drv >= _DRIVES )
\r
3928 return FR_INVALID_DRIVE;
\r
3931 if( partition >= 2 )
\r
3933 return FR_MKFS_ABORTED;
\r
3936 /* Check mounted drive and clear work area */
\r
3940 return FR_NOT_ENABLED;
\r
3944 drv = LD2PD( drv );
\r
3946 /* Get disk statics */
\r
3947 stat = disk_initialize( drv );
\r
3948 if( stat & STA_NOINIT )
\r
3950 return FR_NOT_READY;
\r
3953 if( stat & STA_PROTECT )
\r
3955 return FR_WRITE_PROTECTED;
\r
3958 #if _MAX_SS != 512 /* Get disk sector size */
\r
3959 if( disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS )
\r
3961 return FR_MKFS_ABORTED;
\r
3965 if( disk_ioctl(drv, GET_SECTOR_COUNT, &n_part) != RES_OK || n_part < MIN_SECTOR )
\r
3967 return FR_MKFS_ABORTED;
\r
3970 if( n_part > MAX_SECTOR )
\r
3972 n_part = MAX_SECTOR;
\r
3975 b_part = ( !partition ) ? 63 : 0; /* Boot sector */
\r
3977 for( d = 512; d <= 32768U && d != allocsize; d <<= 1 );
\r
3979 /* Check validity of the allocation unit size */
\r
3980 if( d != allocsize )
\r
3986 { /* Auto selection of cluster size */
\r
3988 for( as = SS(fs); as > 512U; as >>= 1 )
\r
3993 for( n = 0; d < sstbl[n]; n++ );
\r
3994 allocsize = cstbl[n];
\r
3997 if( allocsize < SS(fs) )
\r
3999 allocsize = SS( fs );
\r
4002 allocsize /= SS( fs ); /* Number of sectors per cluster */
\r
4004 /* Pre-compute number of clusters and FAT type */
\r
4005 n_clst = n_part / allocsize;
\r
4007 if( n_clst >= 0xFF5 )
\r
4012 if( n_clst >= 0xFFF5 )
\r
4017 /* Determine offset and size of FAT structure */
\r
4021 n_fat = ( (n_clst * 3 + 1) / 2 + 3 + SS(fs) - 1 ) / SS( fs );
\r
4022 n_rsv = 1 + partition;
\r
4023 n_dir = N_ROOTDIR * 32 / SS( fs );
\r
4027 n_fat = ( (n_clst * 2) + 4 + SS(fs) - 1 ) / SS( fs );
\r
4028 n_rsv = 1 + partition;
\r
4029 n_dir = N_ROOTDIR * 32 / SS( fs );
\r
4033 n_fat = ( (n_clst * 4) + 8 + SS(fs) - 1 ) / SS( fs );
\r
4034 n_rsv = 33 - partition;
\r
4038 b_fat = b_part + n_rsv; /* FATs start sector */
\r
4039 b_dir = b_fat + n_fat * N_FATS; /* Directory start sector */
\r
4040 b_data = b_dir + n_dir; /* Data start sector */
\r
4042 /* Align data start sector to erase block boundary (for flash memory media) */
\r
4043 if( disk_ioctl(drv, GET_BLOCK_SIZE, &n) != RES_OK )
\r
4045 return FR_MKFS_ABORTED;
\r
4048 n = ( b_data + n - 1 ) &~( n - 1 );
\r
4049 n_fat += ( n - b_data ) / N_FATS;
\r
4051 /* b_dir and b_data are no longer used below */
\r
4053 /* Determine number of cluster and final check of validity of the FAT type */
\r
4054 n_clst = ( n_part - n_rsv - n_fat * N_FATS - n_dir ) / allocsize;
\r
4055 if( (fmt == FS_FAT16 && n_clst < 0xFF5) || (fmt == FS_FAT32 && n_clst < 0xFFF5) )
\r
4057 return FR_MKFS_ABORTED;
\r
4060 /* Create partition table if needed */
\r
4063 DWORD n_disk = b_part + n_part;
\r
4065 mem_set( fs->win, 0, SS(fs) );
\r
4066 tbl = fs->win + MBR_Table;
\r
4067 ST_DWORD( tbl, 0x00010180 ); /* Partition start in CHS */
\r
4068 if( n_disk < 63UL * 255 * 1024 )
\r
4069 { /* Partition end in CHS */
\r
4070 n_disk = n_disk / 63 / 255;
\r
4071 tbl[7] = ( BYTE ) n_disk;
\r
4072 tbl[6] = ( BYTE ) ( (n_disk >> 2) | 63 );
\r
4076 ST_WORD( &tbl[6], 0xFFFF );
\r
4080 if( fmt != FS_FAT32 )
\r
4082 tbl[4] = ( n_part < 0x10000 ) ? 0x04 : 0x06;
\r
4089 ST_DWORD( tbl + 8, 63 ); /* Partition start in LBA */
\r
4090 ST_DWORD( tbl + 12, n_part ); /* Partition size in LBA */
\r
4091 ST_WORD( tbl + 64, 0xAA55 ); /* Signature */
\r
4092 if( disk_write(drv, fs->win, 0, 1) != RES_OK )
\r
4094 return FR_DISK_ERR;
\r
4104 /* Create boot record */
\r
4105 tbl = fs->win; /* Clear buffer */
\r
4106 mem_set( tbl, 0, SS(fs) );
\r
4107 ST_DWORD( tbl + BS_jmpBoot, 0x90FEEB ); /* Boot code (jmp $, nop) */
\r
4108 ST_WORD( tbl + BPB_BytsPerSec, SS(fs) ); /* Sector size */
\r
4109 tbl[BPB_SecPerClus] = ( BYTE ) allocsize; /* Sectors per cluster */
\r
4110 ST_WORD( tbl + BPB_RsvdSecCnt, n_rsv ); /* Reserved sectors */
\r
4111 tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */
\r
4112 ST_WORD( tbl + BPB_RootEntCnt, SS(fs) / 32 * n_dir ); /* Number of rootdir entries */
\r
4113 if( n_part < 0x10000 )
\r
4114 { /* Number of total sectors */
\r
4115 ST_WORD( tbl + BPB_TotSec16, n_part );
\r
4119 ST_DWORD( tbl + BPB_TotSec32, n_part );
\r
4122 tbl[BPB_Media] = partition; /* Media descripter */
\r
4123 ST_WORD( tbl + BPB_SecPerTrk, 63 ); /* Number of sectors per track */
\r
4124 ST_WORD( tbl + BPB_NumHeads, 255 ); /* Number of heads */
\r
4125 ST_DWORD( tbl + BPB_HiddSec, b_part ); /* Hidden sectors */
\r
4126 n = get_fattime(); /* Use current time as a VSN */
\r
4127 if( fmt != FS_FAT32 )
\r
4129 ST_DWORD( tbl + BS_VolID, n ); /* Volume serial number */
\r
4130 ST_WORD( tbl + BPB_FATSz16, n_fat ); /* Number of secters per FAT */
\r
4131 tbl[BS_DrvNum] = 0x80; /* Drive number */
\r
4132 tbl[BS_BootSig] = 0x29; /* Extended boot signature */
\r
4133 mem_cpy( tbl + BS_VolLab, "NO NAME FAT ", 19 ); /* Volume lavel, FAT signature */
\r
4137 ST_DWORD( tbl + BS_VolID32, n ); /* Volume serial number */
\r
4138 ST_DWORD( tbl + BPB_FATSz32, n_fat ); /* Number of secters per FAT */
\r
4139 ST_DWORD( tbl + BPB_RootClus, 2 ); /* Root directory cluster (2) */
\r
4140 ST_WORD( tbl + BPB_FSInfo, 1 ); /* FSInfo record offset (bs+1) */
\r
4141 ST_WORD( tbl + BPB_BkBootSec, 6 ); /* Backup boot record offset (bs+6) */
\r
4142 tbl[BS_DrvNum32] = 0x80; /* Drive number */
\r
4143 tbl[BS_BootSig32] = 0x29; /* Extended boot signature */
\r
4144 mem_cpy( tbl + BS_VolLab32, "NO NAME FAT32 ", 19 ); /* Volume lavel, FAT signature */
\r
4147 ST_WORD( tbl + BS_55AA, 0xAA55 ); /* Signature */
\r
4148 if( SS(fs) > 512U )
\r
4150 ST_WORD( tbl + SS(fs) - 2, 0xAA55 );
\r
4153 if( disk_write(drv, tbl, b_part + 0, 1) != RES_OK )
\r
4155 return FR_DISK_ERR;
\r
4158 if( fmt == FS_FAT32 )
\r
4160 disk_write( drv, tbl, b_part + 6, 1 );
\r
4163 /* Initialize FAT area */
\r
4164 for( m = 0; m < N_FATS; m++ )
\r
4166 mem_set( tbl, 0, SS(fs) ); /* 1st sector of the FAT */
\r
4167 if( fmt != FS_FAT32 )
\r
4169 n = ( fmt == FS_FAT12 ) ? 0x00FFFF00 : 0xFFFFFF00;
\r
4171 ST_DWORD( tbl, n ); /* Reserve cluster #0-1 (FAT12/16) */
\r
4175 ST_DWORD( tbl + 0, 0xFFFFFFF8 ); /* Reserve cluster #0-1 (FAT32) */
\r
4176 ST_DWORD( tbl + 4, 0xFFFFFFFF );
\r
4177 ST_DWORD( tbl + 8, 0x0FFFFFFF ); /* Reserve cluster #2 for root dir */
\r
4180 if( disk_write(drv, tbl, b_fat++, 1) != RES_OK )
\r
4182 return FR_DISK_ERR;
\r
4185 mem_set( tbl, 0, SS(fs) ); /* Following FAT entries are filled by zero */
\r
4186 for( n = 1; n < n_fat; n++ )
\r
4188 if( disk_write(drv, tbl, b_fat++, 1) != RES_OK )
\r
4190 return FR_DISK_ERR;
\r
4195 /* Initialize Root directory */
\r
4196 m = ( BYTE ) ( (fmt == FS_FAT32) ? allocsize : n_dir );
\r
4199 if( disk_write(drv, tbl, b_fat++, 1) != RES_OK )
\r
4201 return FR_DISK_ERR;
\r
4205 /* Create FSInfo record if needed */
\r
4206 if( fmt == FS_FAT32 )
\r
4208 ST_WORD( tbl + BS_55AA, 0xAA55 );
\r
4209 ST_DWORD( tbl + FSI_LeadSig, 0x41615252 );
\r
4210 ST_DWORD( tbl + FSI_StrucSig, 0x61417272 );
\r
4211 ST_DWORD( tbl + FSI_Free_Count, n_clst - 1 );
\r
4212 ST_DWORD( tbl + FSI_Nxt_Free, 0xFFFFFFFF );
\r
4213 disk_write( drv, tbl, b_part + 1, 1 );
\r
4214 disk_write( drv, tbl, b_part + 7, 1 );
\r
4217 return( disk_ioctl(drv, CTRL_SYNC, ( void * ) NULL) == RES_OK ) ? FR_OK : FR_DISK_ERR;
\r
4220 #endif /* _USE_MKFS && !_FS_READONLY */
\r
4224 /*-----------------------------------------------------------------------*/
\r
4226 /* Get a string from the file */
\r
4228 /*-----------------------------------------------------------------------*/
\r
4231 char *buff, /* Pointer to the string buffer to read */
\r
4232 int len, /* Size of string buffer */
\r
4233 FIL *fil /* Pointer to the file object */
\r
4240 while( i < len - 1 )
\r
4241 { /* Read bytes until buffer gets filled */
\r
4242 f_read( fil, p, 1, &rc );
\r
4245 break; /* Break when no data to read */
\r
4248 #if _USE_STRFUNC >= 2
\r
4251 continue; /* Strip '\r' */
\r
4256 if( *p++ == '\n' )
\r
4258 break; /* Break when reached end of line */
\r
4263 return i ? buff : NULL; /* When no data read (eof or error), return with error. */
\r
4267 #include <stdarg.h>
\r
4269 /*-----------------------------------------------------------------------*/
\r
4271 /* Put a character to the file */
\r
4273 /*-----------------------------------------------------------------------*/
\r
4274 int f_putc( int chr, /* A character to be output */ FIL *fil /* Ponter to the file object */ )
\r
4279 #if _USE_STRFUNC >= 2
\r
4282 f_putc( '\r', fil ); /* LF -> CRLF conversion */
\r
4287 { /* Special value may be used to switch the destination to any other device */
\r
4288 /* put_console(chr); */
\r
4293 f_write( fil, &c, 1, &bw ); /* Write a byte to the file */
\r
4294 return bw ? chr : EOF; /* Return the result */
\r
4297 /*-----------------------------------------------------------------------*/
\r
4299 /* Put a string to the file */
\r
4301 /*-----------------------------------------------------------------------*/
\r
4302 int f_puts( const char *str, /* Pointer to the string to be output */ FIL *fil /* Pointer to the file object */ )
\r
4306 for( n = 0; *str; str++, n++ )
\r
4308 if( f_putc(*str, fil) == EOF )
\r
4317 /*-----------------------------------------------------------------------*/
\r
4319 /* Put a formatted string to the file */
\r
4321 /*-----------------------------------------------------------------------*/
\r
4322 int f_printf( FIL *fil, /* Pointer to the file object */ const char *str, /* Pointer to the format string */ ... /* Optional arguments... */ )
\r
4328 int i, w, res, cc;
\r
4330 va_start( arp, str );
\r
4332 for( cc = res = 0; cc != EOF; res += cc )
\r
4337 break; /* End of string */
\r
4341 { /* Non escape cahracter */
\r
4342 cc = f_putc( c, fil );
\r
4354 { /* Flag: '0' padding */
\r
4359 while( c >= '0' && c <= '9' )
\r
4361 w = w * 10 + ( c - '0' );
\r
4366 { /* Prefix: Size is long int */
\r
4372 { /* Type is string */
\r
4373 cc = f_puts( va_arg(arp, char *), fil );
\r
4378 { /* Type is character */
\r
4379 cc = f_putc( va_arg(arp, int), fil );
\r
4391 r = 10; /* Type is signed decimal */
\r
4396 r = 10; /* Type is unsigned decimal */
\r
4401 r = 16; /* Type is unsigned hexdecimal */
\r
4406 break; /* Unknown type */
\r
4410 { /* Get the value */
\r
4411 val = ( ULONG ) va_arg( arp, long );
\r
4415 val = ( c == 'd' ) ? ( ULONG ) ( long ) va_arg( arp, int ) : ( ULONG ) va_arg( arp, unsigned int );
\r
4418 /* Put numeral string */
\r
4421 if( val & 0x80000000 )
\r
4428 i = sizeof( s ) - 1;
\r
4432 c = ( UCHAR ) ( val % r + '0' );
\r
4440 } while( i && val );
\r
4441 if( i && (f & 4) )
\r
4446 w = sizeof( s ) - 1 - w;
\r
4447 while( i && i > w )
\r
4449 s[--i] = ( f & 1 ) ? '0' : ' ';
\r
4452 cc = f_puts( &s[i], fil );
\r
4456 return( cc == EOF ) ? cc : res;
\r
4459 #endif /* !_FS_READONLY */
\r
4460 #endif /* _USE_STRFUNC */
\r