|
@@ -0,0 +1,4606 @@
|
|
|
+
|
|
|
+/ FatFs - FAT file system module R0.10b (C)ChaN, 2014
|
|
|
+/-----------------------------------------------------------------------------/
|
|
|
+/ FatFs module is a generic FAT file system module for small embedded systems.
|
|
|
+/ This is a free software that opened for education, research and commercial
|
|
|
+/ developments under license policy of following terms.
|
|
|
+/
|
|
|
+/ Copyright (C) 2014, ChaN, all right reserved.
|
|
|
+/
|
|
|
+/ * The FatFs module is a free software and there is NO WARRANTY.
|
|
|
+/ * No restriction on use. You can use, modify and redistribute it for
|
|
|
+/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
|
|
|
+/ * Redistributions of source code must retain the above copyright notice.
|
|
|
+/
|
|
|
+/-----------------------------------------------------------------------------/
|
|
|
+/ Feb 26,'06 R0.00 Prototype.
|
|
|
+/
|
|
|
+/ Apr 29,'06 R0.01 First stable version.
|
|
|
+/
|
|
|
+/ Jun 01,'06 R0.02 Added FAT12 support.
|
|
|
+/ Removed unbuffered mode.
|
|
|
+/ Fixed a problem on small (<32M) partition.
|
|
|
+/ Jun 10,'06 R0.02a Added a configuration option (_FS_MINIMUM).
|
|
|
+/
|
|
|
+/ Sep 22,'06 R0.03 Added f_rename().
|
|
|
+/ Changed option _FS_MINIMUM to _FS_MINIMIZE.
|
|
|
+/ Dec 11,'06 R0.03a Improved cluster scan algorithm to write files fast.
|
|
|
+/ Fixed f_mkdir() creates incorrect directory on FAT32.
|
|
|
+/
|
|
|
+/ Feb 04,'07 R0.04 Supported multiple drive system.
|
|
|
+/ Changed some interfaces for multiple drive system.
|
|
|
+/ Changed f_mountdrv() to f_mount().
|
|
|
+/ Added f_mkfs().
|
|
|
+/ Apr 01,'07 R0.04a Supported multiple partitions on a physical drive.
|
|
|
+/ Added a capability of extending file size to f_lseek().
|
|
|
+/ Added minimization level 3.
|
|
|
+/ Fixed an endian sensitive code in f_mkfs().
|
|
|
+/ May 05,'07 R0.04b Added a configuration option _USE_NTFLAG.
|
|
|
+/ Added FSINFO support.
|
|
|
+/ Fixed DBCS name can result FR_INVALID_NAME.
|
|
|
+/ Fixed short seek (<= csize) collapses the file object.
|
|
|
+/
|
|
|
+/ Aug 25,'07 R0.05 Changed arguments of f_read(), f_write() and f_mkfs().
|
|
|
+/ Fixed f_mkfs() on FAT32 creates incorrect FSINFO.
|
|
|
+/ Fixed f_mkdir() on FAT32 creates incorrect directory.
|
|
|
+/ Feb 03,'08 R0.05a Added f_truncate() and f_utime().
|
|
|
+/ Fixed off by one error at FAT sub-type determination.
|
|
|
+/ Fixed btr in f_read() can be mistruncated.
|
|
|
+/ Fixed cached sector is not flushed when create and close without write.
|
|
|
+/
|
|
|
+/ Apr 01,'08 R0.06 Added fputc(), fputs(), fprintf() and fgets().
|
|
|
+/ Improved performance of f_lseek() on moving to the same or following cluster.
|
|
|
+/
|
|
|
+/ Apr 01,'09 R0.07 Merged Tiny-FatFs as a configuration option. (_FS_TINY)
|
|
|
+/ Added long file name feature.
|
|
|
+/ Added multiple code page feature.
|
|
|
+/ Added re-entrancy for multitask operation.
|
|
|
+/ Added auto cluster size selection to f_mkfs().
|
|
|
+/ Added rewind option to f_readdir().
|
|
|
+/ Changed result code of critical errors.
|
|
|
+/ Renamed string functions to avoid name collision.
|
|
|
+/ Apr 14,'09 R0.07a Separated out OS dependent code on reentrant cfg.
|
|
|
+/ Added multiple sector size feature.
|
|
|
+/ Jun 21,'09 R0.07c Fixed f_unlink() can return FR_OK on error.
|
|
|
+/ Fixed wrong cache control in f_lseek().
|
|
|
+/ Added relative path feature.
|
|
|
+/ Added f_chdir() and f_chdrive().
|
|
|
+/ Added proper case conversion to extended character.
|
|
|
+/ Nov 03,'09 R0.07e Separated out configuration options from ff.h to ffconf.h.
|
|
|
+/ Fixed f_unlink() fails to remove a sub-directory on _FS_RPATH.
|
|
|
+/ Fixed name matching error on the 13 character boundary.
|
|
|
+/ Added a configuration option, _LFN_UNICODE.
|
|
|
+/ Changed f_readdir() to return the SFN with always upper case on non-LFN cfg.
|
|
|
+/
|
|
|
+/ May 15,'10 R0.08 Added a memory configuration option. (_USE_LFN = 3)
|
|
|
+/ Added file lock feature. (_FS_SHARE)
|
|
|
+/ Added fast seek feature. (_USE_FASTSEEK)
|
|
|
+/ Changed some types on the API, XCHAR->TCHAR.
|
|
|
+/ Changed .fname in the FILINFO structure on Unicode cfg.
|
|
|
+/ String functions support UTF-8 encoding files on Unicode cfg.
|
|
|
+/ Aug 16,'10 R0.08a Added f_getcwd().
|
|
|
+/ Added sector erase feature. (_USE_ERASE)
|
|
|
+/ Moved file lock semaphore table from fs object to the bss.
|
|
|
+/ Fixed a wrong directory entry is created on non-LFN cfg when the given name contains ';'.
|
|
|
+/ Fixed f_mkfs() creates wrong FAT32 volume.
|
|
|
+/ Jan 15,'11 R0.08b Fast seek feature is also applied to f_read() and f_write().
|
|
|
+/ f_lseek() reports required table size on creating CLMP.
|
|
|
+/ Extended format syntax of f_printf().
|
|
|
+/ Ignores duplicated directory separators in given path name.
|
|
|
+/
|
|
|
+/ Sep 06,'11 R0.09 f_mkfs() supports multiple partition to complete the multiple partition feature.
|
|
|
+/ Added f_fdisk().
|
|
|
+/ Aug 27,'12 R0.09a Changed f_open() and f_opendir() reject null object pointer to avoid crash.
|
|
|
+/ Changed option name _FS_SHARE to _FS_LOCK.
|
|
|
+/ Fixed assertion failure due to OS/2 EA on FAT12/16 volume.
|
|
|
+/ Jan 24,'13 R0.09b Added f_setlabel() and f_getlabel().
|
|
|
+/
|
|
|
+/ Oct 02,'13 R0.10 Added selection of character encoding on the file. (_STRF_ENCODE)
|
|
|
+/ Added f_closedir().
|
|
|
+/ Added forced full FAT scan for f_getfree(). (_FS_NOFSINFO)
|
|
|
+/ Added forced mount feature with changes of f_mount().
|
|
|
+/ Improved behavior of volume auto detection.
|
|
|
+/ Improved write throughput of f_puts() and f_printf().
|
|
|
+/ Changed argument of f_chdrive(), f_mkfs(), disk_read() and disk_write().
|
|
|
+/ Fixed f_write() can be truncated when the file size is close to 4GB.
|
|
|
+/ Fixed f_open(), f_mkdir() and f_setlabel() can return incorrect error code.
|
|
|
+/ Jan 15,'14 R0.10a Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID)
|
|
|
+/ Added a configuration option of minimum sector size. (_MIN_SS)
|
|
|
+/ 2nd argument of f_rename() can have a drive number and it will be ignored.
|
|
|
+/ Fixed f_mount() with forced mount fails when drive number is >= 1.
|
|
|
+/ Fixed f_close() invalidates the file object without volume lock.
|
|
|
+/ Fixed f_closedir() returns but the volume lock is left acquired.
|
|
|
+/ Fixed creation of an entry with LFN fails on too many SFN collisions.
|
|
|
+/ May 19,'14 R0.10b Fixed a hard error in the disk I/O layer can collapse the directory entry.
|
|
|
+/ Fixed LFN entry is not deleted on delete/rename an object with lossy converted SFN.
|
|
|
+/---------------------------------------------------------------------------*/
|
|
|
+
|
|
|
+#include "ff.h"
|
|
|
+#include "diskio.h"
|
|
|
+
|
|
|
+#if _FS_REENTRANT
|
|
|
+#include "rtos.h"
|
|
|
+#include "semphr.h"
|
|
|
+extern volatile xSemaphoreHandle Mutex_fatfs;
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Module Private Definitions
|
|
|
+
|
|
|
+---------------------------------------------------------------------------*/
|
|
|
+
|
|
|
+#if _FATFS != 8051
|
|
|
+#error Wrong include file (ff.h).
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_REENTRANT
|
|
|
+#if _USE_LFN == 1
|
|
|
+#error Static LFN work area cannot be used at thread-safe configuration.
|
|
|
+#endif
|
|
|
+#define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; }
|
|
|
+#define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }
|
|
|
+#else
|
|
|
+#define ENTER_FF(fs)
|
|
|
+#define LEAVE_FF(fs, res) return res
|
|
|
+#endif
|
|
|
+
|
|
|
+#define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if (_MAX_SS < _MIN_SS) || (_MAX_SS != 512 && _MAX_SS != 1024 && _MAX_SS != 2048 && _MAX_SS != 4096) || (_MIN_SS != 512 && _MIN_SS != 1024 && _MIN_SS != 2048 && _MIN_SS != 4096)
|
|
|
+#error Wrong sector size configuration.
|
|
|
+#endif
|
|
|
+#if _MAX_SS == _MIN_SS
|
|
|
+#define SS(fs) ((UINT)_MAX_SS)
|
|
|
+#else
|
|
|
+#define SS(fs) ((fs)->ssize)
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_LOCK
|
|
|
+#if _FS_READONLY
|
|
|
+#error _FS_LOCK must be 0 at read-only cfg.
|
|
|
+#endif
|
|
|
+typedef struct {
|
|
|
+ FATFS *fs;
|
|
|
+ DWORD clu;
|
|
|
+ WORD idx;
|
|
|
+ WORD ctr;
|
|
|
+} FILESEM;
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _CODE_PAGE == 932
|
|
|
+#define _DF1S 0x81
|
|
|
+#define _DF1E 0x9F
|
|
|
+#define _DF2S 0xE0
|
|
|
+#define _DF2E 0xFC
|
|
|
+#define _DS1S 0x40
|
|
|
+#define _DS1E 0x7E
|
|
|
+#define _DS2S 0x80
|
|
|
+#define _DS2E 0xFC
|
|
|
+
|
|
|
+#elif _CODE_PAGE == 936
|
|
|
+#define _DF1S 0x81
|
|
|
+#define _DF1E 0xFE
|
|
|
+#define _DS1S 0x40
|
|
|
+#define _DS1E 0x7E
|
|
|
+#define _DS2S 0x80
|
|
|
+#define _DS2E 0xFE
|
|
|
+
|
|
|
+#elif _CODE_PAGE == 949
|
|
|
+#define _DF1S 0x81
|
|
|
+#define _DF1E 0xFE
|
|
|
+#define _DS1S 0x41
|
|
|
+#define _DS1E 0x5A
|
|
|
+#define _DS2S 0x61
|
|
|
+#define _DS2E 0x7A
|
|
|
+#define _DS3S 0x81
|
|
|
+#define _DS3E 0xFE
|
|
|
+
|
|
|
+#elif _CODE_PAGE == 950
|
|
|
+#define _DF1S 0x81
|
|
|
+#define _DF1E 0xFE
|
|
|
+#define _DS1S 0x40
|
|
|
+#define _DS1E 0x7E
|
|
|
+#define _DS2S 0xA1
|
|
|
+#define _DS2E 0xFE
|
|
|
+
|
|
|
+#elif _CODE_PAGE == 437
|
|
|
+#define _DF1S 0
|
|
|
+#define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F,0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
|
|
+ 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
|
|
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
|
|
+ 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
|
|
+
|
|
|
+#elif _CODE_PAGE == 720
|
|
|
+#define _DF1S 0
|
|
|
+#define _EXCVT {0x80,0x81,0x45,0x41,0x84,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x8E,0x8F,0x90,0x92,0x92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
|
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF}
|
|
|
+
|
|
|
+#elif _CODE_PAGE == 1258
|
|
|
+#define _DF1S 0
|
|
|
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0xAC,0x9D,0x9E,0x9F, \
|
|
|
+ 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
|
|
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
|
|
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xEC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xFE,0x9F}
|
|
|
+
|
|
|
+#elif _CODE_PAGE == 1
|
|
|
+#if _USE_LFN
|
|
|
+#error Cannot use LFN feature without valid code page.
|
|
|
+#endif
|
|
|
+#define _DF1S 0
|
|
|
+
|
|
|
+#else
|
|
|
+#error Unknown code page
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#define IsUpper(c) (((c)>='A')&&((c)<='Z'))
|
|
|
+#define IsLower(c) (((c)>='a')&&((c)<='z'))
|
|
|
+#define IsDigit(c) (((c)>='0')&&((c)<='9'))
|
|
|
+
|
|
|
+#if _DF1S
|
|
|
+
|
|
|
+#ifdef _DF2S
|
|
|
+#define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
|
|
|
+#else
|
|
|
+#define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef _DS3S
|
|
|
+#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
|
|
|
+#else
|
|
|
+#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
|
|
|
+#endif
|
|
|
+
|
|
|
+#else
|
|
|
+
|
|
|
+#define IsDBCS1(c) 0
|
|
|
+#define IsDBCS2(c) 0
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#define NS 11
|
|
|
+#define NS_LOSS 0x01
|
|
|
+#define NS_LFN 0x02
|
|
|
+#define NS_LAST 0x04
|
|
|
+#define NS_BODY 0x08
|
|
|
+#define NS_EXT 0x10
|
|
|
+#define NS_DOT 0x20
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#define MIN_FAT16 4086U
|
|
|
+#define MIN_FAT32 65526U
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/ structure member because the structure is not binary compatible between
|
|
|
+/ different platforms */
|
|
|
+
|
|
|
+#define BS_jmpBoot 0
|
|
|
+#define BS_OEMName 3
|
|
|
+#define BPB_BytsPerSec 11
|
|
|
+#define BPB_SecPerClus 13
|
|
|
+#define BPB_RsvdSecCnt 14
|
|
|
+#define BPB_NumFATs 16
|
|
|
+#define BPB_RootEntCnt 17
|
|
|
+#define BPB_TotSec16 19
|
|
|
+#define BPB_Media 21
|
|
|
+#define BPB_FATSz16 22
|
|
|
+#define BPB_SecPerTrk 24
|
|
|
+#define BPB_NumHeads 26
|
|
|
+#define BPB_HiddSec 28
|
|
|
+#define BPB_TotSec32 32
|
|
|
+#define BS_DrvNum 36
|
|
|
+#define BS_BootSig 38
|
|
|
+#define BS_VolID 39
|
|
|
+#define BS_VolLab 43
|
|
|
+#define BS_FilSysType 54
|
|
|
+#define BPB_FATSz32 36
|
|
|
+#define BPB_ExtFlags 40
|
|
|
+#define BPB_FSVer 42
|
|
|
+#define BPB_RootClus 44
|
|
|
+#define BPB_FSInfo 48
|
|
|
+#define BPB_BkBootSec 50
|
|
|
+#define BS_DrvNum32 64
|
|
|
+#define BS_BootSig32 66
|
|
|
+#define BS_VolID32 67
|
|
|
+#define BS_VolLab32 71
|
|
|
+#define BS_FilSysType32 82
|
|
|
+#define FSI_LeadSig 0
|
|
|
+#define FSI_StrucSig 484
|
|
|
+#define FSI_Free_Count 488
|
|
|
+#define FSI_Nxt_Free 492
|
|
|
+#define MBR_Table 446
|
|
|
+#define SZ_PTE 16
|
|
|
+#define BS_55AA 510
|
|
|
+
|
|
|
+#define DIR_Name 0
|
|
|
+#define DIR_Attr 11
|
|
|
+#define DIR_NTres 12
|
|
|
+#define DIR_CrtTimeTenth 13
|
|
|
+#define DIR_CrtTime 14
|
|
|
+#define DIR_CrtDate 16
|
|
|
+#define DIR_LstAccDate 18
|
|
|
+#define DIR_FstClusHI 20
|
|
|
+#define DIR_WrtTime 22
|
|
|
+#define DIR_WrtDate 24
|
|
|
+#define DIR_FstClusLO 26
|
|
|
+#define DIR_FileSize 28
|
|
|
+#define LDIR_Ord 0
|
|
|
+#define LDIR_Attr 11
|
|
|
+#define LDIR_Type 12
|
|
|
+#define LDIR_Chksum 13
|
|
|
+#define LDIR_FstClusLO 26
|
|
|
+#define SZ_DIR 32
|
|
|
+#define LLE 0x40
|
|
|
+#define DDE 0xE5
|
|
|
+#define NDDE 0x05
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/ guaranteed zero/null as initial value. If not, either the
|
|
|
+/ linker or start-up routine is out of ANSI-C standard.
|
|
|
+ */
|
|
|
+
|
|
|
+#if _VOLUMES >= 1 || _VOLUMES <= 10
|
|
|
+static
|
|
|
+FATFS *FatFs[_VOLUMES];
|
|
|
+#else
|
|
|
+#error Number of volumes must be 1 to 10.
|
|
|
+#endif
|
|
|
+
|
|
|
+static
|
|
|
+WORD Fsid;
|
|
|
+
|
|
|
+#if _FS_RPATH && _VOLUMES >= 2
|
|
|
+static
|
|
|
+BYTE CurrVol;
|
|
|
+#endif
|
|
|
+
|
|
|
+#if _FS_LOCK
|
|
|
+static
|
|
|
+FILESEM Files[_FS_LOCK];
|
|
|
+#endif
|
|
|
+
|
|
|
+#if _USE_LFN == 0
|
|
|
+#define DEF_NAMEBUF BYTE sfn[12]
|
|
|
+#define INIT_BUF(dobj) (dobj).fn = sfn
|
|
|
+#define FREE_BUF()
|
|
|
+
|
|
|
+#elif _USE_LFN == 1
|
|
|
+static
|
|
|
+WCHAR LfnBuf[_MAX_LFN+1];
|
|
|
+#define DEF_NAMEBUF BYTE sfn[12]
|
|
|
+#define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = LfnBuf; }
|
|
|
+#define FREE_BUF()
|
|
|
+
|
|
|
+#elif _USE_LFN == 2
|
|
|
+#define DEF_NAMEBUF BYTE sfn[12]; WCHAR lbuf[_MAX_LFN+1]
|
|
|
+#define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = lbuf; }
|
|
|
+#define FREE_BUF()
|
|
|
+
|
|
|
+#elif _USE_LFN == 3
|
|
|
+#define DEF_NAMEBUF BYTE sfn[12]; WCHAR *lfn
|
|
|
+#define INIT_BUF(dobj) { lfn = ff_memalloc((_MAX_LFN + 1) * 2); \
|
|
|
+ if (!lfn) LEAVE_FF((dobj).fs, FR_NOT_ENOUGH_CORE); \
|
|
|
+ (dobj).lfn = lfn; (dobj).fn = sfn; }
|
|
|
+#define FREE_BUF() ff_memfree(lfn)
|
|
|
+
|
|
|
+#else
|
|
|
+#error Wrong LFN configuration.
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+#ifdef _EXCVT
|
|
|
+static
|
|
|
+const BYTE ExCvt[] = _EXCVT;
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Module Private Functions
|
|
|
+
|
|
|
+---------------------------------------------------------------------------*/
|
|
|
+
|
|
|
+DWORD get_fattime ()
|
|
|
+{
|
|
|
+ return
|
|
|
+ | ((DWORD)DateTime.date.RTC_Month << 21)
|
|
|
+ | ((DWORD)DateTime.date.RTC_Date << 16)
|
|
|
+ | ((DWORD)DateTime.time.RTC_Hours << 11)
|
|
|
+ | ((DWORD)DateTime.time.RTC_Minutes << 5)
|
|
|
+ | ((DWORD)DateTime.time.RTC_Seconds >> 1);*/
|
|
|
+ ((DWORD)(2000 - 1980) << 25)
|
|
|
+ | ((DWORD)1 << 21)
|
|
|
+ | ((DWORD)1 << 16)
|
|
|
+ | ((DWORD)1 << 11)
|
|
|
+ | ((DWORD)1 << 5)
|
|
|
+ | ((DWORD)1 >> 1);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+void mem_cpy (void* dst, const void* src, UINT cnt) {
|
|
|
+ BYTE *d = (BYTE*)dst;
|
|
|
+ const BYTE *s = (const BYTE*)src;
|
|
|
+
|
|
|
+#if _WORD_ACCESS == 1
|
|
|
+ while (cnt >= sizeof (int)) {
|
|
|
+ *(int*)d = *(int*)s;
|
|
|
+ d += sizeof (int); s += sizeof (int);
|
|
|
+ cnt -= sizeof (int);
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ while (cnt--)
|
|
|
+ *d++ = *s++;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+void mem_set (void* dst, int val, UINT cnt) {
|
|
|
+ BYTE *d = (BYTE*)dst;
|
|
|
+
|
|
|
+ while (cnt--)
|
|
|
+ *d++ = (BYTE)val;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+int mem_cmp (const void* dst, const void* src, UINT cnt) {
|
|
|
+ const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
|
|
|
+ int r = 0;
|
|
|
+
|
|
|
+ while (cnt-- && (r = *d++ - *s++) == 0) ;
|
|
|
+ return r;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+int chk_chr (const char* str, int chr) {
|
|
|
+ while (*str && *str != chr) str++;
|
|
|
+ return *str;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_REENTRANT
|
|
|
+static
|
|
|
+int lock_fs (
|
|
|
+ FATFS* fs
|
|
|
+)
|
|
|
+{
|
|
|
+ return ff_req_grant(fs->sobj);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+void unlock_fs (
|
|
|
+ FATFS* fs,
|
|
|
+ FRESULT res
|
|
|
+)
|
|
|
+{
|
|
|
+ if (fs &&
|
|
|
+ res != FR_NOT_ENABLED &&
|
|
|
+ res != FR_INVALID_DRIVE &&
|
|
|
+ res != FR_INVALID_OBJECT &&
|
|
|
+ res != FR_TIMEOUT) {
|
|
|
+ ff_rel_grant(fs->sobj);
|
|
|
+ }
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_LOCK
|
|
|
+
|
|
|
+static
|
|
|
+FRESULT chk_lock (
|
|
|
+ DIR* dp,
|
|
|
+ int acc
|
|
|
+)
|
|
|
+{
|
|
|
+ UINT i, be;
|
|
|
+
|
|
|
+
|
|
|
+ for (i = be = 0; i < _FS_LOCK; i++) {
|
|
|
+ if (Files[i].fs) {
|
|
|
+ if (Files[i].fs == dp->fs &&
|
|
|
+ Files[i].clu == dp->sclust &&
|
|
|
+ Files[i].idx == dp->index) break;
|
|
|
+ } else {
|
|
|
+ be = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (i == _FS_LOCK)
|
|
|
+ return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES;
|
|
|
+
|
|
|
+
|
|
|
+ return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+int enq_lock (void)
|
|
|
+{
|
|
|
+ UINT i;
|
|
|
+
|
|
|
+ for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
|
|
|
+ return (i == _FS_LOCK) ? 0 : 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+UINT inc_lock (
|
|
|
+ DIR* dp,
|
|
|
+ int acc
|
|
|
+)
|
|
|
+{
|
|
|
+ UINT i;
|
|
|
+
|
|
|
+
|
|
|
+ for (i = 0; i < _FS_LOCK; i++) {
|
|
|
+ if (Files[i].fs == dp->fs &&
|
|
|
+ Files[i].clu == dp->sclust &&
|
|
|
+ Files[i].idx == dp->index) break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (i == _FS_LOCK) {
|
|
|
+ for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
|
|
|
+ if (i == _FS_LOCK) return 0;
|
|
|
+ Files[i].fs = dp->fs;
|
|
|
+ Files[i].clu = dp->sclust;
|
|
|
+ Files[i].idx = dp->index;
|
|
|
+ Files[i].ctr = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (acc && Files[i].ctr) return 0;
|
|
|
+
|
|
|
+ Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1;
|
|
|
+
|
|
|
+ return i + 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+FRESULT dec_lock (
|
|
|
+ UINT i
|
|
|
+)
|
|
|
+{
|
|
|
+ WORD n;
|
|
|
+ FRESULT res;
|
|
|
+
|
|
|
+
|
|
|
+ if (--i < _FS_LOCK) {
|
|
|
+ n = Files[i].ctr;
|
|
|
+ if (n == 0x100) n = 0;
|
|
|
+ if (n) n--;
|
|
|
+ Files[i].ctr = n;
|
|
|
+ if (!n) Files[i].fs = 0;
|
|
|
+ res = FR_OK;
|
|
|
+ } else {
|
|
|
+ res = FR_INT_ERR;
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+void clear_lock (
|
|
|
+ FATFS *fs
|
|
|
+)
|
|
|
+{
|
|
|
+ UINT i;
|
|
|
+
|
|
|
+ for (i = 0; i < _FS_LOCK; i++) {
|
|
|
+ if (Files[i].fs == fs) Files[i].fs = 0;
|
|
|
+ }
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+static
|
|
|
+FRESULT sync_window (
|
|
|
+ FATFS* fs
|
|
|
+)
|
|
|
+{
|
|
|
+ DWORD wsect;
|
|
|
+ UINT nf;
|
|
|
+
|
|
|
+
|
|
|
+ if (fs->wflag) {
|
|
|
+ wsect = fs->winsect;
|
|
|
+ if (disk_write(fs->drv, fs->win, wsect, 1))
|
|
|
+ return FR_DISK_ERR;
|
|
|
+ fs->wflag = 0;
|
|
|
+ if (wsect - fs->fatbase < fs->fsize) {
|
|
|
+ for (nf = fs->n_fats; nf >= 2; nf--) {
|
|
|
+ wsect += fs->fsize;
|
|
|
+ disk_write(fs->drv, fs->win, wsect, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return FR_OK;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+FRESULT move_window (
|
|
|
+ FATFS* fs,
|
|
|
+ DWORD sector
|
|
|
+)
|
|
|
+{
|
|
|
+ if (sector != fs->winsect) {
|
|
|
+#if !_FS_READONLY
|
|
|
+ if (sync_window(fs) != FR_OK)
|
|
|
+ return FR_DISK_ERR;
|
|
|
+#endif
|
|
|
+ if (disk_read(fs->drv, fs->win, sector, 1))
|
|
|
+ return FR_DISK_ERR;
|
|
|
+ fs->winsect = sector;
|
|
|
+ }
|
|
|
+
|
|
|
+ return FR_OK;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+static
|
|
|
+FRESULT sync_fs (
|
|
|
+ FATFS* fs
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+
|
|
|
+
|
|
|
+ res = sync_window(fs);
|
|
|
+ if (res == FR_OK) {
|
|
|
+
|
|
|
+ if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) {
|
|
|
+
|
|
|
+ mem_set(fs->win, 0, SS(fs));
|
|
|
+ ST_WORD(fs->win+BS_55AA, 0xAA55);
|
|
|
+ ST_DWORD(fs->win+FSI_LeadSig, 0x41615252);
|
|
|
+ ST_DWORD(fs->win+FSI_StrucSig, 0x61417272);
|
|
|
+ ST_DWORD(fs->win+FSI_Free_Count, fs->free_clust);
|
|
|
+ ST_DWORD(fs->win+FSI_Nxt_Free, fs->last_clust);
|
|
|
+
|
|
|
+ fs->winsect = fs->volbase + 1;
|
|
|
+ disk_write(fs->drv, fs->win, fs->winsect, 1);
|
|
|
+ fs->fsi_flag = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (disk_ioctl(fs->drv, CTRL_SYNC, 0) != RES_OK)
|
|
|
+ res = FR_DISK_ERR;
|
|
|
+ }
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+DWORD clust2sect (
|
|
|
+ FATFS* fs,
|
|
|
+ DWORD clst
|
|
|
+)
|
|
|
+{
|
|
|
+ clst -= 2;
|
|
|
+ if (clst >= (fs->n_fatent - 2)) return 0;
|
|
|
+ return clst * fs->csize + fs->database;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+DWORD get_fat (
|
|
|
+ FATFS* fs,
|
|
|
+ DWORD clst
|
|
|
+)
|
|
|
+{
|
|
|
+ UINT wc, bc;
|
|
|
+ BYTE *p;
|
|
|
+
|
|
|
+
|
|
|
+ if (clst < 2 || clst >= fs->n_fatent)
|
|
|
+ return 1;
|
|
|
+
|
|
|
+ switch (fs->fs_type) {
|
|
|
+ case FS_FAT12 :
|
|
|
+ bc = (UINT)clst; bc += bc / 2;
|
|
|
+ if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;
|
|
|
+ wc = fs->win[bc % SS(fs)]; bc++;
|
|
|
+ if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;
|
|
|
+ wc |= fs->win[bc % SS(fs)] << 8;
|
|
|
+ return clst & 1 ? wc >> 4 : (wc & 0xFFF);
|
|
|
+
|
|
|
+ case FS_FAT16 :
|
|
|
+ if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)))) break;
|
|
|
+ p = &fs->win[clst * 2 % SS(fs)];
|
|
|
+ return LD_WORD(p);
|
|
|
+
|
|
|
+ case FS_FAT32 :
|
|
|
+ if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)))) break;
|
|
|
+ p = &fs->win[clst * 4 % SS(fs)];
|
|
|
+ return LD_DWORD(p) & 0x0FFFFFFF;
|
|
|
+
|
|
|
+ default:
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0xFFFFFFFF;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+
|
|
|
+FRESULT put_fat (
|
|
|
+ FATFS* fs,
|
|
|
+ DWORD clst,
|
|
|
+ DWORD val
|
|
|
+)
|
|
|
+{
|
|
|
+ UINT bc;
|
|
|
+ BYTE *p;
|
|
|
+ FRESULT res;
|
|
|
+
|
|
|
+
|
|
|
+ if (clst < 2 || clst >= fs->n_fatent) {
|
|
|
+ res = FR_INT_ERR;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ switch (fs->fs_type) {
|
|
|
+ case FS_FAT12 :
|
|
|
+ bc = (UINT)clst; bc += bc / 2;
|
|
|
+ res = move_window(fs, fs->fatbase + (bc / SS(fs)));
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ p = &fs->win[bc % SS(fs)];
|
|
|
+ *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
|
|
|
+ bc++;
|
|
|
+ fs->wflag = 1;
|
|
|
+ res = move_window(fs, fs->fatbase + (bc / SS(fs)));
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ p = &fs->win[bc % SS(fs)];
|
|
|
+ *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
|
|
|
+ break;
|
|
|
+
|
|
|
+ case FS_FAT16 :
|
|
|
+ res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ p = &fs->win[clst * 2 % SS(fs)];
|
|
|
+ ST_WORD(p, (WORD)val);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case FS_FAT32 :
|
|
|
+ res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ p = &fs->win[clst * 4 % SS(fs)];
|
|
|
+ val |= LD_DWORD(p) & 0xF0000000;
|
|
|
+ ST_DWORD(p, val);
|
|
|
+ break;
|
|
|
+
|
|
|
+ default :
|
|
|
+ res = FR_INT_ERR;
|
|
|
+ }
|
|
|
+ fs->wflag = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+static
|
|
|
+FRESULT remove_chain (
|
|
|
+ FATFS* fs,
|
|
|
+ DWORD clst
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DWORD nxt;
|
|
|
+#if _USE_ERASE
|
|
|
+ DWORD scl = clst, ecl = clst, rt[2];
|
|
|
+#endif
|
|
|
+
|
|
|
+ if (clst < 2 || clst >= fs->n_fatent) {
|
|
|
+ res = FR_INT_ERR;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ res = FR_OK;
|
|
|
+ while (clst < fs->n_fatent) {
|
|
|
+ nxt = get_fat(fs, clst);
|
|
|
+ if (nxt == 0) break;
|
|
|
+ if (nxt == 1) { res = FR_INT_ERR; break; }
|
|
|
+ if (nxt == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
|
|
|
+ res = put_fat(fs, clst, 0);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ if (fs->free_clust != 0xFFFFFFFF) {
|
|
|
+ fs->free_clust++;
|
|
|
+ fs->fsi_flag |= 1;
|
|
|
+ }
|
|
|
+#if _USE_ERASE
|
|
|
+ if (ecl + 1 == nxt) {
|
|
|
+ ecl = nxt;
|
|
|
+ } else {
|
|
|
+ rt[0] = clust2sect(fs, scl);
|
|
|
+ rt[1] = clust2sect(fs, ecl) + fs->csize - 1;
|
|
|
+ disk_ioctl(fs->drv, CTRL_ERASE_SECTOR, rt);
|
|
|
+ scl = ecl = nxt;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ clst = nxt;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+static
|
|
|
+DWORD create_chain (
|
|
|
+ FATFS* fs,
|
|
|
+ DWORD clst
|
|
|
+)
|
|
|
+{
|
|
|
+ DWORD cs, ncl, scl;
|
|
|
+ FRESULT res;
|
|
|
+
|
|
|
+
|
|
|
+ if (clst == 0) {
|
|
|
+ scl = fs->last_clust;
|
|
|
+ if (!scl || scl >= fs->n_fatent) scl = 1;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ cs = get_fat(fs, clst);
|
|
|
+ if (cs < 2) return 1;
|
|
|
+ if (cs == 0xFFFFFFFF) return cs;
|
|
|
+ if (cs < fs->n_fatent) return cs;
|
|
|
+ scl = clst;
|
|
|
+ }
|
|
|
+
|
|
|
+ ncl = scl;
|
|
|
+ for (;;) {
|
|
|
+ ncl++;
|
|
|
+ if (ncl >= fs->n_fatent) {
|
|
|
+ ncl = 2;
|
|
|
+ if (ncl > scl) return 0;
|
|
|
+ }
|
|
|
+ cs = get_fat(fs, ncl);
|
|
|
+ if (cs == 0) break;
|
|
|
+ if (cs == 0xFFFFFFFF || cs == 1)
|
|
|
+ return cs;
|
|
|
+ if (ncl == scl) return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ res = put_fat(fs, ncl, 0x0FFFFFFF);
|
|
|
+ if (res == FR_OK && clst != 0) {
|
|
|
+ res = put_fat(fs, clst, ncl);
|
|
|
+ }
|
|
|
+ if (res == FR_OK) {
|
|
|
+ fs->last_clust = ncl;
|
|
|
+ if (fs->free_clust != 0xFFFFFFFF) {
|
|
|
+ fs->free_clust--;
|
|
|
+ fs->fsi_flag |= 1;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ncl;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _USE_FASTSEEK
|
|
|
+static
|
|
|
+DWORD clmt_clust (
|
|
|
+ FIL* fp,
|
|
|
+ DWORD ofs
|
|
|
+)
|
|
|
+{
|
|
|
+ DWORD cl, ncl, *tbl;
|
|
|
+
|
|
|
+
|
|
|
+ tbl = fp->cltbl + 1;
|
|
|
+ cl = ofs / SS(fp->fs) / fp->fs->csize;
|
|
|
+ for (;;) {
|
|
|
+ ncl = *tbl++;
|
|
|
+ if (!ncl) return 0;
|
|
|
+ if (cl < ncl) break;
|
|
|
+ cl -= ncl; tbl++;
|
|
|
+ }
|
|
|
+ return cl + *tbl;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+FRESULT dir_sdi (
|
|
|
+ DIR* dp,
|
|
|
+ UINT idx
|
|
|
+)
|
|
|
+{
|
|
|
+ DWORD clst, sect;
|
|
|
+ UINT ic;
|
|
|
+
|
|
|
+
|
|
|
+ dp->index = (WORD)idx;
|
|
|
+ clst = dp->sclust;
|
|
|
+ if (clst == 1 || clst >= dp->fs->n_fatent)
|
|
|
+ return FR_INT_ERR;
|
|
|
+ if (!clst && dp->fs->fs_type == FS_FAT32)
|
|
|
+ clst = dp->fs->dirbase;
|
|
|
+
|
|
|
+ if (clst == 0) {
|
|
|
+ if (idx >= dp->fs->n_rootdir)
|
|
|
+ return FR_INT_ERR;
|
|
|
+ sect = dp->fs->dirbase;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ ic = SS(dp->fs) / SZ_DIR * dp->fs->csize;
|
|
|
+ while (idx >= ic) {
|
|
|
+ clst = get_fat(dp->fs, clst);
|
|
|
+ if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
|
|
|
+ if (clst < 2 || clst >= dp->fs->n_fatent)
|
|
|
+ return FR_INT_ERR;
|
|
|
+ idx -= ic;
|
|
|
+ }
|
|
|
+ sect = clust2sect(dp->fs, clst);
|
|
|
+ }
|
|
|
+ dp->clust = clst;
|
|
|
+ if (!sect) return FR_INT_ERR;
|
|
|
+ dp->sect = sect + idx / (SS(dp->fs) / SZ_DIR);
|
|
|
+ dp->dir = dp->fs->win + (idx % (SS(dp->fs) / SZ_DIR)) * SZ_DIR;
|
|
|
+
|
|
|
+ return FR_OK;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+FRESULT dir_next (
|
|
|
+ DIR* dp,
|
|
|
+ int stretch
|
|
|
+)
|
|
|
+{
|
|
|
+ DWORD clst;
|
|
|
+ UINT i;
|
|
|
+
|
|
|
+
|
|
|
+ i = dp->index + 1;
|
|
|
+ if (!(i & 0xFFFF) || !dp->sect)
|
|
|
+ return FR_NO_FILE;
|
|
|
+
|
|
|
+ if (!(i % (SS(dp->fs) / SZ_DIR))) {
|
|
|
+ dp->sect++;
|
|
|
+
|
|
|
+ if (!dp->clust) {
|
|
|
+ if (i >= dp->fs->n_rootdir)
|
|
|
+ return FR_NO_FILE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (((i / (SS(dp->fs) / SZ_DIR)) & (dp->fs->csize - 1)) == 0) {
|
|
|
+ clst = get_fat(dp->fs, dp->clust);
|
|
|
+ if (clst <= 1) return FR_INT_ERR;
|
|
|
+ if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
|
|
|
+ if (clst >= dp->fs->n_fatent) {
|
|
|
+#if !_FS_READONLY
|
|
|
+ UINT c;
|
|
|
+ if (!stretch) return FR_NO_FILE;
|
|
|
+ clst = create_chain(dp->fs, dp->clust);
|
|
|
+ if (clst == 0) return FR_DENIED;
|
|
|
+ if (clst == 1) return FR_INT_ERR;
|
|
|
+ if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
|
|
|
+
|
|
|
+ if (sync_window(dp->fs)) return FR_DISK_ERR;
|
|
|
+ mem_set(dp->fs->win, 0, SS(dp->fs));
|
|
|
+ dp->fs->winsect = clust2sect(dp->fs, clst);
|
|
|
+ for (c = 0; c < dp->fs->csize; c++) {
|
|
|
+ dp->fs->wflag = 1;
|
|
|
+ if (sync_window(dp->fs)) return FR_DISK_ERR;
|
|
|
+ dp->fs->winsect++;
|
|
|
+ }
|
|
|
+ dp->fs->winsect -= c;
|
|
|
+#else
|
|
|
+ if (!stretch) return FR_NO_FILE;
|
|
|
+ return FR_NO_FILE;
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ dp->clust = clst;
|
|
|
+ dp->sect = clust2sect(dp->fs, clst);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ dp->index = (WORD)i;
|
|
|
+ dp->dir = dp->fs->win + (i % (SS(dp->fs) / SZ_DIR)) * SZ_DIR;
|
|
|
+
|
|
|
+ return FR_OK;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+static
|
|
|
+FRESULT dir_alloc (
|
|
|
+ DIR* dp,
|
|
|
+ UINT nent
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ UINT n;
|
|
|
+
|
|
|
+
|
|
|
+ res = dir_sdi(dp, 0);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ n = 0;
|
|
|
+ do {
|
|
|
+ res = move_window(dp->fs, dp->sect);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ if (dp->dir[0] == DDE || dp->dir[0] == 0) {
|
|
|
+ if (++n == nent) break;
|
|
|
+ } else {
|
|
|
+ n = 0;
|
|
|
+ }
|
|
|
+ res = dir_next(dp, 1);
|
|
|
+ } while (res == FR_OK);
|
|
|
+ }
|
|
|
+ if (res == FR_NO_FILE) res = FR_DENIED;
|
|
|
+ return res;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+DWORD ld_clust (
|
|
|
+ FATFS* fs,
|
|
|
+ BYTE* dir
|
|
|
+)
|
|
|
+{
|
|
|
+ DWORD cl;
|
|
|
+
|
|
|
+ cl = LD_WORD(dir+DIR_FstClusLO);
|
|
|
+ if (fs->fs_type == FS_FAT32)
|
|
|
+ cl |= (DWORD)LD_WORD(dir+DIR_FstClusHI) << 16;
|
|
|
+
|
|
|
+ return cl;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+static
|
|
|
+void st_clust (
|
|
|
+ BYTE* dir,
|
|
|
+ DWORD cl
|
|
|
+)
|
|
|
+{
|
|
|
+ ST_WORD(dir+DIR_FstClusLO, cl);
|
|
|
+ ST_WORD(dir+DIR_FstClusHI, cl >> 16);
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _USE_LFN
|
|
|
+static
|
|
|
+const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30};
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+int cmp_lfn (
|
|
|
+ WCHAR* lfnbuf,
|
|
|
+ BYTE* dir
|
|
|
+)
|
|
|
+{
|
|
|
+ UINT i, s;
|
|
|
+ WCHAR wc, uc;
|
|
|
+
|
|
|
+
|
|
|
+ i = ((dir[LDIR_Ord] & ~LLE) - 1) * 13;
|
|
|
+ s = 0; wc = 1;
|
|
|
+ do {
|
|
|
+ uc = LD_WORD(dir+LfnOfs[s]);
|
|
|
+ if (wc) {
|
|
|
+ wc = ff_wtoupper(uc);
|
|
|
+ if (i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++]))
|
|
|
+ return 0;
|
|
|
+ } else {
|
|
|
+ if (uc != 0xFFFF) return 0;
|
|
|
+ }
|
|
|
+ } while (++s < 13);
|
|
|
+
|
|
|
+ if ((dir[LDIR_Ord] & LLE) && wc && lfnbuf[i])
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+int pick_lfn (
|
|
|
+ WCHAR* lfnbuf,
|
|
|
+ BYTE* dir
|
|
|
+)
|
|
|
+{
|
|
|
+ UINT i, s;
|
|
|
+ WCHAR wc, uc;
|
|
|
+
|
|
|
+
|
|
|
+ i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13;
|
|
|
+
|
|
|
+ s = 0; wc = 1;
|
|
|
+ do {
|
|
|
+ uc = LD_WORD(dir+LfnOfs[s]);
|
|
|
+ if (wc) {
|
|
|
+ if (i >= _MAX_LFN) return 0;
|
|
|
+ lfnbuf[i++] = wc = uc;
|
|
|
+ } else {
|
|
|
+ if (uc != 0xFFFF) return 0;
|
|
|
+ }
|
|
|
+ } while (++s < 13);
|
|
|
+
|
|
|
+ if (dir[LDIR_Ord] & LLE) {
|
|
|
+ if (i >= _MAX_LFN) return 0;
|
|
|
+ lfnbuf[i] = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+static
|
|
|
+void fit_lfn (
|
|
|
+ const WCHAR* lfnbuf,
|
|
|
+ BYTE* dir,
|
|
|
+ BYTE ord,
|
|
|
+ BYTE sum
|
|
|
+)
|
|
|
+{
|
|
|
+ UINT i, s;
|
|
|
+ WCHAR wc;
|
|
|
+
|
|
|
+
|
|
|
+ dir[LDIR_Chksum] = sum;
|
|
|
+ dir[LDIR_Attr] = AM_LFN;
|
|
|
+ dir[LDIR_Type] = 0;
|
|
|
+ ST_WORD(dir+LDIR_FstClusLO, 0);
|
|
|
+
|
|
|
+ i = (ord - 1) * 13;
|
|
|
+ s = wc = 0;
|
|
|
+ do {
|
|
|
+ if (wc != 0xFFFF) wc = lfnbuf[i++];
|
|
|
+ ST_WORD(dir+LfnOfs[s], wc);
|
|
|
+ if (!wc) wc = 0xFFFF;
|
|
|
+ } while (++s < 13);
|
|
|
+ if (wc == 0xFFFF || !lfnbuf[i]) ord |= LLE;
|
|
|
+ dir[LDIR_Ord] = ord;
|
|
|
+}
|
|
|
+
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _USE_LFN
|
|
|
+static
|
|
|
+void gen_numname (
|
|
|
+ BYTE* dst,
|
|
|
+ const BYTE* src,
|
|
|
+ const WCHAR* lfn,
|
|
|
+ UINT seq
|
|
|
+)
|
|
|
+{
|
|
|
+ BYTE ns[8], c;
|
|
|
+ UINT i, j;
|
|
|
+
|
|
|
+
|
|
|
+ mem_cpy(dst, src, 11);
|
|
|
+
|
|
|
+ if (seq > 5) {
|
|
|
+ WCHAR wc;
|
|
|
+ DWORD sr = seq;
|
|
|
+
|
|
|
+ while (*lfn) {
|
|
|
+ wc = *lfn++;
|
|
|
+ for (i = 0; i < 16; i++) {
|
|
|
+ sr = (sr << 1) + (wc & 1);
|
|
|
+ wc >>= 1;
|
|
|
+ if (sr & 0x10000) sr ^= 0x11021;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ seq = (UINT)sr;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ i = 7;
|
|
|
+ do {
|
|
|
+ c = (seq % 16) + '0';
|
|
|
+ if (c > '9') c += 7;
|
|
|
+ ns[i--] = c;
|
|
|
+ seq /= 16;
|
|
|
+ } while (seq);
|
|
|
+ ns[i] = '~';
|
|
|
+
|
|
|
+
|
|
|
+ for (j = 0; j < i && dst[j] != ' '; j++) {
|
|
|
+ if (IsDBCS1(dst[j])) {
|
|
|
+ if (j == i - 1) break;
|
|
|
+ j++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ do {
|
|
|
+ dst[j++] = (i < 8) ? ns[i++] : ' ';
|
|
|
+ } while (j < 8);
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _USE_LFN
|
|
|
+static
|
|
|
+BYTE sum_sfn (
|
|
|
+ const BYTE* dir
|
|
|
+)
|
|
|
+{
|
|
|
+ BYTE sum = 0;
|
|
|
+ UINT n = 11;
|
|
|
+
|
|
|
+ do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);
|
|
|
+ return sum;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+FRESULT dir_find (
|
|
|
+ DIR* dp
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ BYTE c, *dir;
|
|
|
+#if _USE_LFN
|
|
|
+ BYTE a, ord, sum;
|
|
|
+#endif
|
|
|
+
|
|
|
+ res = dir_sdi(dp, 0);
|
|
|
+ if (res != FR_OK) return res;
|
|
|
+
|
|
|
+#if _USE_LFN
|
|
|
+ ord = sum = 0xFF; dp->lfn_idx = 0xFFFF;
|
|
|
+#endif
|
|
|
+ do {
|
|
|
+ res = move_window(dp->fs, dp->sect);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ dir = dp->dir;
|
|
|
+ c = dir[DIR_Name];
|
|
|
+ if (c == 0) { res = FR_NO_FILE; break; }
|
|
|
+#if _USE_LFN
|
|
|
+ a = dir[DIR_Attr] & AM_MASK;
|
|
|
+ if (c == DDE || ((a & AM_VOL) && a != AM_LFN)) {
|
|
|
+ ord = 0xFF; dp->lfn_idx = 0xFFFF;
|
|
|
+ } else {
|
|
|
+ if (a == AM_LFN) {
|
|
|
+ if (dp->lfn) {
|
|
|
+ if (c & LLE) {
|
|
|
+ sum = dir[LDIR_Chksum];
|
|
|
+ c &= ~LLE; ord = c;
|
|
|
+ dp->lfn_idx = dp->index;
|
|
|
+ }
|
|
|
+
|
|
|
+ ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dp->lfn, dir)) ? ord - 1 : 0xFF;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (!ord && sum == sum_sfn(dir)) break;
|
|
|
+ if (!(dp->fn[NS] & NS_LOSS) && !mem_cmp(dir, dp->fn, 11)) break;
|
|
|
+ ord = 0xFF; dp->lfn_idx = 0xFFFF;
|
|
|
+ }
|
|
|
+ }
|
|
|
+#else
|
|
|
+ if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dp->fn, 11))
|
|
|
+ break;
|
|
|
+#endif
|
|
|
+ res = dir_next(dp, 0);
|
|
|
+ } while (res == FR_OK);
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_MINIMIZE <= 1 || _USE_LABEL || _FS_RPATH >= 2
|
|
|
+static
|
|
|
+FRESULT dir_read (
|
|
|
+ DIR* dp,
|
|
|
+ int vol
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ BYTE a, c, *dir;
|
|
|
+#if _USE_LFN
|
|
|
+ BYTE ord = 0xFF, sum = 0xFF;
|
|
|
+#endif
|
|
|
+
|
|
|
+ res = FR_NO_FILE;
|
|
|
+ while (dp->sect) {
|
|
|
+ res = move_window(dp->fs, dp->sect);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ dir = dp->dir;
|
|
|
+ c = dir[DIR_Name];
|
|
|
+ if (c == 0) { res = FR_NO_FILE; break; }
|
|
|
+ a = dir[DIR_Attr] & AM_MASK;
|
|
|
+#if _USE_LFN
|
|
|
+ if (c == DDE || (!_FS_RPATH && c == '.') || (int)(a == AM_VOL) != vol) {
|
|
|
+ ord = 0xFF;
|
|
|
+ } else {
|
|
|
+ if (a == AM_LFN) {
|
|
|
+ if (c & LLE) {
|
|
|
+ sum = dir[LDIR_Chksum];
|
|
|
+ c &= ~LLE; ord = c;
|
|
|
+ dp->lfn_idx = dp->index;
|
|
|
+ }
|
|
|
+
|
|
|
+ ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dp->lfn, dir)) ? ord - 1 : 0xFF;
|
|
|
+ } else {
|
|
|
+ if (ord || sum != sum_sfn(dir))
|
|
|
+ dp->lfn_idx = 0xFFFF;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+#else
|
|
|
+ if (c != DDE && (_FS_RPATH || c != '.') && a != AM_LFN && (int)(a == AM_VOL) == vol)
|
|
|
+ break;
|
|
|
+#endif
|
|
|
+ res = dir_next(dp, 0);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (res != FR_OK) dp->sect = 0;
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+static
|
|
|
+FRESULT dir_register (
|
|
|
+ DIR* dp
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+#if _USE_LFN
|
|
|
+ UINT n, nent;
|
|
|
+ BYTE sn[12], *fn, sum;
|
|
|
+ WCHAR *lfn;
|
|
|
+
|
|
|
+
|
|
|
+ fn = dp->fn; lfn = dp->lfn;
|
|
|
+ mem_cpy(sn, fn, 12);
|
|
|
+
|
|
|
+ if (_FS_RPATH && (sn[NS] & NS_DOT))
|
|
|
+ return FR_INVALID_NAME;
|
|
|
+
|
|
|
+ if (sn[NS] & NS_LOSS) {
|
|
|
+ fn[NS] = 0; dp->lfn = 0;
|
|
|
+ for (n = 1; n < 100; n++) {
|
|
|
+ gen_numname(fn, sn, lfn, n);
|
|
|
+ res = dir_find(dp);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ }
|
|
|
+ if (n == 100) return FR_DENIED;
|
|
|
+ if (res != FR_NO_FILE) return res;
|
|
|
+ fn[NS] = sn[NS]; dp->lfn = lfn;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sn[NS] & NS_LFN) {
|
|
|
+ for (n = 0; lfn[n]; n++) ;
|
|
|
+ nent = (n + 25) / 13;
|
|
|
+ } else {
|
|
|
+ nent = 1;
|
|
|
+ }
|
|
|
+ res = dir_alloc(dp, nent);
|
|
|
+
|
|
|
+ if (res == FR_OK && --nent) {
|
|
|
+ res = dir_sdi(dp, dp->index - nent);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ sum = sum_sfn(dp->fn);
|
|
|
+ do {
|
|
|
+ res = move_window(dp->fs, dp->sect);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ fit_lfn(dp->lfn, dp->dir, (BYTE)nent, sum);
|
|
|
+ dp->fs->wflag = 1;
|
|
|
+ res = dir_next(dp, 0);
|
|
|
+ } while (res == FR_OK && --nent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+#else
|
|
|
+ res = dir_alloc(dp, 1);
|
|
|
+#endif
|
|
|
+
|
|
|
+ if (res == FR_OK) {
|
|
|
+ res = move_window(dp->fs, dp->sect);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ mem_set(dp->dir, 0, SZ_DIR);
|
|
|
+ mem_cpy(dp->dir, dp->fn, 11);
|
|
|
+#if _USE_LFN
|
|
|
+ dp->dir[DIR_NTres] = dp->fn[NS] & (NS_BODY | NS_EXT);
|
|
|
+#endif
|
|
|
+ dp->fs->wflag = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY && !_FS_MINIMIZE
|
|
|
+static
|
|
|
+FRESULT dir_remove (
|
|
|
+ DIR* dp
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+#if _USE_LFN
|
|
|
+ UINT i;
|
|
|
+
|
|
|
+ i = dp->index;
|
|
|
+ res = dir_sdi(dp, (dp->lfn_idx == 0xFFFF) ? i : dp->lfn_idx);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ do {
|
|
|
+ res = move_window(dp->fs, dp->sect);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ mem_set(dp->dir, 0, SZ_DIR);
|
|
|
+ *dp->dir = DDE;
|
|
|
+ dp->fs->wflag = 1;
|
|
|
+ if (dp->index >= i) break;
|
|
|
+ res = dir_next(dp, 0);
|
|
|
+ } while (res == FR_OK);
|
|
|
+ if (res == FR_NO_FILE) res = FR_INT_ERR;
|
|
|
+ }
|
|
|
+
|
|
|
+#else
|
|
|
+ res = dir_sdi(dp, dp->index);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ res = move_window(dp->fs, dp->sect);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ mem_set(dp->dir, 0, SZ_DIR);
|
|
|
+ *dp->dir = DDE;
|
|
|
+ dp->fs->wflag = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2
|
|
|
+static
|
|
|
+void get_fileinfo (
|
|
|
+ DIR* dp,
|
|
|
+ FILINFO* fno
|
|
|
+)
|
|
|
+{
|
|
|
+ UINT i;
|
|
|
+ TCHAR *p, c;
|
|
|
+
|
|
|
+
|
|
|
+ p = fno->fname;
|
|
|
+ if (dp->sect) {
|
|
|
+ BYTE *dir = dp->dir;
|
|
|
+
|
|
|
+ i = 0;
|
|
|
+ while (i < 11) {
|
|
|
+ c = (TCHAR)dir[i++];
|
|
|
+ if (c == ' ') continue;
|
|
|
+ if (c == NDDE) c = (TCHAR)DDE;
|
|
|
+ if (i == 9) *p++ = '.';
|
|
|
+#if _USE_LFN
|
|
|
+ if (IsUpper(c) && (dir[DIR_NTres] & (i >= 9 ? NS_EXT : NS_BODY)))
|
|
|
+ c += 0x20;
|
|
|
+#if _LFN_UNICODE
|
|
|
+ if (IsDBCS1(c) && i != 8 && i != 11 && IsDBCS2(dir[i]))
|
|
|
+ c = c << 8 | dir[i++];
|
|
|
+ c = ff_convert(c, 1);
|
|
|
+ if (!c) c = '?';
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+ *p++ = c;
|
|
|
+ }
|
|
|
+ fno->fattrib = dir[DIR_Attr];
|
|
|
+ fno->fsize = LD_DWORD(dir+DIR_FileSize);
|
|
|
+ fno->fdate = LD_WORD(dir+DIR_WrtDate);
|
|
|
+ fno->ftime = LD_WORD(dir+DIR_WrtTime);
|
|
|
+ }
|
|
|
+ *p = 0;
|
|
|
+
|
|
|
+#if _USE_LFN
|
|
|
+ if (fno->lfname) {
|
|
|
+ WCHAR w, *lfn;
|
|
|
+
|
|
|
+ i = 0; p = fno->lfname;
|
|
|
+ if (dp->sect && fno->lfsize && dp->lfn_idx != 0xFFFF) {
|
|
|
+ lfn = dp->lfn;
|
|
|
+ while ((w = *lfn++) != 0) {
|
|
|
+#if !_LFN_UNICODE
|
|
|
+ w = ff_convert(w, 0);
|
|
|
+ if (!w) { i = 0; break; }
|
|
|
+ if (_DF1S && w >= 0x100)
|
|
|
+ p[i++] = (TCHAR)(w >> 8);
|
|
|
+#endif
|
|
|
+ if (i >= fno->lfsize - 1) { i = 0; break; }
|
|
|
+ p[i++] = (TCHAR)w;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ p[i] = 0;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+FRESULT create_name (
|
|
|
+ DIR* dp,
|
|
|
+ const TCHAR** path
|
|
|
+)
|
|
|
+{
|
|
|
+#if _USE_LFN
|
|
|
+ BYTE b, cf;
|
|
|
+ WCHAR w, *lfn;
|
|
|
+ UINT i, ni, si, di;
|
|
|
+ const TCHAR *p;
|
|
|
+
|
|
|
+
|
|
|
+ for (p = *path; *p == '/' || *p == '\\'; p++) ;
|
|
|
+ lfn = dp->lfn;
|
|
|
+ si = di = 0;
|
|
|
+ for (;;) {
|
|
|
+ w = p[si++];
|
|
|
+ if (w < ' ' || w == '/' || w == '\\') break;
|
|
|
+ if (di >= _MAX_LFN)
|
|
|
+ return FR_INVALID_NAME;
|
|
|
+#if !_LFN_UNICODE
|
|
|
+ w &= 0xFF;
|
|
|
+ if (IsDBCS1(w)) {
|
|
|
+ b = (BYTE)p[si++];
|
|
|
+ if (!IsDBCS2(b))
|
|
|
+ return FR_INVALID_NAME;
|
|
|
+ w = (w << 8) + b;
|
|
|
+ }
|
|
|
+ w = ff_convert(w, 1);
|
|
|
+ if (!w) return FR_INVALID_NAME;
|
|
|
+#endif
|
|
|
+ if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w))
|
|
|
+ return FR_INVALID_NAME;
|
|
|
+ lfn[di++] = w;
|
|
|
+ }
|
|
|
+ *path = &p[si];
|
|
|
+ cf = (w < ' ') ? NS_LAST : 0;
|
|
|
+#if _FS_RPATH
|
|
|
+ if ((di == 1 && lfn[di-1] == '.') ||
|
|
|
+ (di == 2 && lfn[di-1] == '.' && lfn[di-2] == '.')) {
|
|
|
+ lfn[di] = 0;
|
|
|
+ for (i = 0; i < 11; i++)
|
|
|
+ dp->fn[i] = (i < di) ? '.' : ' ';
|
|
|
+ dp->fn[i] = cf | NS_DOT;
|
|
|
+ return FR_OK;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ while (di) {
|
|
|
+ w = lfn[di-1];
|
|
|
+ if (w != ' ' && w != '.') break;
|
|
|
+ di--;
|
|
|
+ }
|
|
|
+ if (!di) return FR_INVALID_NAME;
|
|
|
+
|
|
|
+ lfn[di] = 0;
|
|
|
+
|
|
|
+
|
|
|
+ mem_set(dp->fn, ' ', 11);
|
|
|
+ for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ;
|
|
|
+ if (si) cf |= NS_LOSS | NS_LFN;
|
|
|
+ while (di && lfn[di - 1] != '.') di--;
|
|
|
+
|
|
|
+ b = i = 0; ni = 8;
|
|
|
+ for (;;) {
|
|
|
+ w = lfn[si++];
|
|
|
+ if (!w) break;
|
|
|
+ if (w == ' ' || (w == '.' && si != di)) {
|
|
|
+ cf |= NS_LOSS | NS_LFN; continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (i >= ni || si == di) {
|
|
|
+ if (ni == 11) {
|
|
|
+ cf |= NS_LOSS | NS_LFN; break;
|
|
|
+ }
|
|
|
+ if (si != di) cf |= NS_LOSS | NS_LFN;
|
|
|
+ if (si > di) break;
|
|
|
+ si = di; i = 8; ni = 11;
|
|
|
+ b <<= 2; continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (w >= 0x80) {
|
|
|
+#ifdef _EXCVT
|
|
|
+ w = ff_convert(w, 0);
|
|
|
+ if (w) w = ExCvt[w - 0x80];
|
|
|
+#else
|
|
|
+ w = ff_convert(ff_wtoupper(w), 0);
|
|
|
+#endif
|
|
|
+ cf |= NS_LFN;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (_DF1S && w >= 0x100) {
|
|
|
+ if (i >= ni - 1) {
|
|
|
+ cf |= NS_LOSS | NS_LFN; i = ni; continue;
|
|
|
+ }
|
|
|
+ dp->fn[i++] = (BYTE)(w >> 8);
|
|
|
+ } else {
|
|
|
+ if (!w || chk_chr("+,;=[]", w)) {
|
|
|
+ w = '_'; cf |= NS_LOSS | NS_LFN;
|
|
|
+ } else {
|
|
|
+ if (IsUpper(w)) {
|
|
|
+ b |= 2;
|
|
|
+ } else {
|
|
|
+ if (IsLower(w)) {
|
|
|
+ b |= 1; w -= 0x20;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ dp->fn[i++] = (BYTE)w;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dp->fn[0] == DDE) dp->fn[0] = NDDE;
|
|
|
+
|
|
|
+ if (ni == 8) b <<= 2;
|
|
|
+ if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03)
|
|
|
+ cf |= NS_LFN;
|
|
|
+ if (!(cf & NS_LFN)) {
|
|
|
+ if ((b & 0x03) == 0x01) cf |= NS_EXT;
|
|
|
+ if ((b & 0x0C) == 0x04) cf |= NS_BODY;
|
|
|
+ }
|
|
|
+
|
|
|
+ dp->fn[NS] = cf;
|
|
|
+
|
|
|
+ return FR_OK;
|
|
|
+
|
|
|
+
|
|
|
+#else
|
|
|
+ BYTE b, c, d, *sfn;
|
|
|
+ UINT ni, si, i;
|
|
|
+ const char *p;
|
|
|
+
|
|
|
+
|
|
|
+ for (p = *path; *p == '/' || *p == '\\'; p++) ;
|
|
|
+ sfn = dp->fn;
|
|
|
+ mem_set(sfn, ' ', 11);
|
|
|
+ si = i = b = 0; ni = 8;
|
|
|
+#if _FS_RPATH
|
|
|
+ if (p[si] == '.') {
|
|
|
+ for (;;) {
|
|
|
+ c = (BYTE)p[si++];
|
|
|
+ if (c != '.' || si >= 3) break;
|
|
|
+ sfn[i++] = c;
|
|
|
+ }
|
|
|
+ if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;
|
|
|
+ *path = &p[si];
|
|
|
+ sfn[NS] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT;
|
|
|
+ return FR_OK;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ for (;;) {
|
|
|
+ c = (BYTE)p[si++];
|
|
|
+ if (c <= ' ' || c == '/' || c == '\\') break;
|
|
|
+ if (c == '.' || i >= ni) {
|
|
|
+ if (ni != 8 || c != '.') return FR_INVALID_NAME;
|
|
|
+ i = 8; ni = 11;
|
|
|
+ b <<= 2; continue;
|
|
|
+ }
|
|
|
+ if (c >= 0x80) {
|
|
|
+ b |= 3;
|
|
|
+#ifdef _EXCVT
|
|
|
+ c = ExCvt[c - 0x80];
|
|
|
+#else
|
|
|
+#if !_DF1S
|
|
|
+ return FR_INVALID_NAME;
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ if (IsDBCS1(c)) {
|
|
|
+ d = (BYTE)p[si++];
|
|
|
+ if (!IsDBCS2(d) || i >= ni - 1)
|
|
|
+ return FR_INVALID_NAME;
|
|
|
+ sfn[i++] = c;
|
|
|
+ sfn[i++] = d;
|
|
|
+ } else {
|
|
|
+ if (chk_chr("\"*+,:;<=>\?[]|\x7F", c))
|
|
|
+ return FR_INVALID_NAME;
|
|
|
+ if (IsUpper(c)) {
|
|
|
+ b |= 2;
|
|
|
+ } else {
|
|
|
+ if (IsLower(c)) {
|
|
|
+ b |= 1; c -= 0x20;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sfn[i++] = c;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ *path = &p[si];
|
|
|
+ c = (c <= ' ') ? NS_LAST : 0;
|
|
|
+
|
|
|
+ if (!i) return FR_INVALID_NAME;
|
|
|
+ if (sfn[0] == DDE) sfn[0] = NDDE;
|
|
|
+
|
|
|
+ if (ni == 8) b <<= 2;
|
|
|
+ if ((b & 0x03) == 0x01) c |= NS_EXT;
|
|
|
+ if ((b & 0x0C) == 0x04) c |= NS_BODY;
|
|
|
+
|
|
|
+ sfn[NS] = c;
|
|
|
+
|
|
|
+ return FR_OK;
|
|
|
+#endif
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+FRESULT follow_path (
|
|
|
+ DIR* dp,
|
|
|
+ const TCHAR* path
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ BYTE *dir, ns;
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_RPATH
|
|
|
+ if (*path == '/' || *path == '\\') {
|
|
|
+ path++; dp->sclust = 0;
|
|
|
+ } else {
|
|
|
+ dp->sclust = dp->fs->cdir;
|
|
|
+ }
|
|
|
+#else
|
|
|
+ if (*path == '/' || *path == '\\')
|
|
|
+ path++;
|
|
|
+ dp->sclust = 0;
|
|
|
+#endif
|
|
|
+
|
|
|
+ if ((UINT)*path < ' ') {
|
|
|
+ res = dir_sdi(dp, 0);
|
|
|
+ dp->dir = 0;
|
|
|
+ } else {
|
|
|
+ for (;;) {
|
|
|
+ res = create_name(dp, &path);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ res = dir_find(dp);
|
|
|
+ ns = dp->fn[NS];
|
|
|
+ if (res != FR_OK) {
|
|
|
+ if (res == FR_NO_FILE) {
|
|
|
+ if (_FS_RPATH && (ns & NS_DOT)) {
|
|
|
+ dp->sclust = 0; dp->dir = 0;
|
|
|
+ if (!(ns & NS_LAST)) continue;
|
|
|
+ res = FR_OK;
|
|
|
+ } else {
|
|
|
+ if (!(ns & NS_LAST)) res = FR_NO_PATH;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (ns & NS_LAST) break;
|
|
|
+ dir = dp->dir;
|
|
|
+ if (!(dir[DIR_Attr] & AM_DIR)) {
|
|
|
+ res = FR_NO_PATH; break;
|
|
|
+ }
|
|
|
+ dp->sclust = ld_clust(dp->fs, dir);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+int get_ldnumber (
|
|
|
+ const TCHAR** path
|
|
|
+)
|
|
|
+{
|
|
|
+ const TCHAR *tp, *tt;
|
|
|
+ UINT i;
|
|
|
+ int vol = -1;
|
|
|
+
|
|
|
+
|
|
|
+ if (*path) {
|
|
|
+ for (tt = *path; (UINT)*tt >= (_USE_LFN ? ' ' : '!') && *tt != ':'; tt++) ;
|
|
|
+ if (*tt == ':') {
|
|
|
+ tp = *path;
|
|
|
+ i = *tp++ - '0';
|
|
|
+ if (i < 10 && tp == tt) {
|
|
|
+ if (i < _VOLUMES) {
|
|
|
+ vol = (int)i;
|
|
|
+ *path = ++tt;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+#if _STR_VOLUME_ID
|
|
|
+ static const char* const str[] = {_VOLUME_STRS};
|
|
|
+ const char *sp;
|
|
|
+ char c;
|
|
|
+ TCHAR tc;
|
|
|
+
|
|
|
+ i = 0; tt++;
|
|
|
+ do {
|
|
|
+ sp = str[i]; tp = *path;
|
|
|
+ do {
|
|
|
+ c = *sp++; tc = *tp++;
|
|
|
+ if (IsLower(tc)) tc -= 0x20;
|
|
|
+ } while (c && (TCHAR)c == tc);
|
|
|
+ } while ((c || tp != tt) && ++i < _VOLUMES);
|
|
|
+ if (i < _VOLUMES) {
|
|
|
+ vol = (int)i;
|
|
|
+ *path = tt;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ return vol;
|
|
|
+ }
|
|
|
+#if _FS_RPATH && _VOLUMES >= 2
|
|
|
+ vol = CurrVol;
|
|
|
+#else
|
|
|
+ vol = 0;
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ return vol;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+BYTE check_fs (
|
|
|
+ FATFS* fs,
|
|
|
+ DWORD sect
|
|
|
+)
|
|
|
+{
|
|
|
+ fs->wflag = 0; fs->winsect = 0xFFFFFFFF;
|
|
|
+ if (move_window(fs, sect) != FR_OK)
|
|
|
+ return 3;
|
|
|
+
|
|
|
+ if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55)
|
|
|
+ return 2;
|
|
|
+
|
|
|
+ if ((LD_DWORD(&fs->win[BS_FilSysType]) & 0xFFFFFF) == 0x544146)
|
|
|
+ return 0;
|
|
|
+ if ((LD_DWORD(&fs->win[BS_FilSysType32]) & 0xFFFFFF) == 0x544146)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+FRESULT find_volume (
|
|
|
+ FATFS** rfs,
|
|
|
+ const TCHAR** path,
|
|
|
+ BYTE wmode
|
|
|
+)
|
|
|
+{
|
|
|
+ BYTE fmt;
|
|
|
+ int vol;
|
|
|
+ DSTATUS stat;
|
|
|
+ DWORD bsect, fasize, tsect, sysect, nclst, szbfat;
|
|
|
+ WORD nrsv;
|
|
|
+ FATFS *fs;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ *rfs = 0;
|
|
|
+ vol = get_ldnumber(path);
|
|
|
+ if (vol < 0) return FR_INVALID_DRIVE;
|
|
|
+
|
|
|
+
|
|
|
+ fs = FatFs[vol];
|
|
|
+ if (!fs) return FR_NOT_ENABLED;
|
|
|
+
|
|
|
+ ENTER_FF(fs);
|
|
|
+ *rfs = fs;
|
|
|
+
|
|
|
+ if (fs->fs_type) {
|
|
|
+ stat = disk_status(fs->drv);
|
|
|
+ if (!(stat & STA_NOINIT)) {
|
|
|
+ if (!_FS_READONLY && wmode && (stat & STA_PROTECT))
|
|
|
+ return FR_WRITE_PROTECTED;
|
|
|
+ return FR_OK;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ fs->fs_type = 0;
|
|
|
+ fs->drv = LD2PD(vol);
|
|
|
+ stat = disk_initialize(fs->drv);
|
|
|
+ if (stat & STA_NOINIT)
|
|
|
+ return FR_NOT_READY;
|
|
|
+ if (!_FS_READONLY && wmode && (stat & STA_PROTECT))
|
|
|
+ return FR_WRITE_PROTECTED;
|
|
|
+#if _MAX_SS != _MIN_SS
|
|
|
+ if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
|
|
|
+ || SS(fs) < _MIN_SS || SS(fs) > _MAX_SS) return FR_DISK_ERR;
|
|
|
+#endif
|
|
|
+
|
|
|
+ bsect = 0;
|
|
|
+ fmt = check_fs(fs, bsect);
|
|
|
+ if (fmt == 1 || (!fmt && (LD2PT(vol)))) {
|
|
|
+ UINT i;
|
|
|
+ DWORD br[4];
|
|
|
+
|
|
|
+ for (i = 0; i < 4; i++) {
|
|
|
+ BYTE *pt = fs->win+MBR_Table + i * SZ_PTE;
|
|
|
+ br[i] = pt[4] ? LD_DWORD(&pt[8]) : 0;
|
|
|
+ }
|
|
|
+ i = LD2PT(vol);
|
|
|
+ if (i) i--;
|
|
|
+ do {
|
|
|
+ bsect = br[i];
|
|
|
+ fmt = bsect ? check_fs(fs, bsect) : 2;
|
|
|
+ } while (!LD2PT(vol) && fmt && ++i < 4);
|
|
|
+ }
|
|
|
+ if (fmt == 3) return FR_DISK_ERR;
|
|
|
+ if (fmt) return FR_NO_FILESYSTEM;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (LD_WORD(fs->win+BPB_BytsPerSec) != SS(fs))
|
|
|
+ return FR_NO_FILESYSTEM;
|
|
|
+
|
|
|
+ fasize = LD_WORD(fs->win+BPB_FATSz16);
|
|
|
+ if (!fasize) fasize = LD_DWORD(fs->win+BPB_FATSz32);
|
|
|
+ fs->fsize = fasize;
|
|
|
+
|
|
|
+ fs->n_fats = fs->win[BPB_NumFATs];
|
|
|
+ if (fs->n_fats != 1 && fs->n_fats != 2)
|
|
|
+ return FR_NO_FILESYSTEM;
|
|
|
+ fasize *= fs->n_fats;
|
|
|
+
|
|
|
+ fs->csize = fs->win[BPB_SecPerClus];
|
|
|
+ if (!fs->csize || (fs->csize & (fs->csize - 1)))
|
|
|
+ return FR_NO_FILESYSTEM;
|
|
|
+
|
|
|
+ fs->n_rootdir = LD_WORD(fs->win+BPB_RootEntCnt);
|
|
|
+ if (fs->n_rootdir % (SS(fs) / SZ_DIR))
|
|
|
+ return FR_NO_FILESYSTEM;
|
|
|
+
|
|
|
+ tsect = LD_WORD(fs->win+BPB_TotSec16);
|
|
|
+ if (!tsect) tsect = LD_DWORD(fs->win+BPB_TotSec32);
|
|
|
+
|
|
|
+ nrsv = LD_WORD(fs->win+BPB_RsvdSecCnt);
|
|
|
+ if (!nrsv) return FR_NO_FILESYSTEM;
|
|
|
+
|
|
|
+
|
|
|
+ sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZ_DIR);
|
|
|
+ if (tsect < sysect) return FR_NO_FILESYSTEM;
|
|
|
+ nclst = (tsect - sysect) / fs->csize;
|
|
|
+ if (!nclst) return FR_NO_FILESYSTEM;
|
|
|
+ fmt = FS_FAT12;
|
|
|
+ if (nclst >= MIN_FAT16) fmt = FS_FAT16;
|
|
|
+ if (nclst >= MIN_FAT32) fmt = FS_FAT32;
|
|
|
+
|
|
|
+
|
|
|
+ fs->n_fatent = nclst + 2;
|
|
|
+ fs->volbase = bsect;
|
|
|
+ fs->fatbase = bsect + nrsv;
|
|
|
+ fs->database = bsect + sysect;
|
|
|
+ if (fmt == FS_FAT32) {
|
|
|
+ if (fs->n_rootdir) return FR_NO_FILESYSTEM;
|
|
|
+ fs->dirbase = LD_DWORD(fs->win+BPB_RootClus);
|
|
|
+ szbfat = fs->n_fatent * 4;
|
|
|
+ } else {
|
|
|
+ if (!fs->n_rootdir) return FR_NO_FILESYSTEM;
|
|
|
+ fs->dirbase = fs->fatbase + fasize;
|
|
|
+ szbfat = (fmt == FS_FAT16) ?
|
|
|
+ fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
|
|
|
+ }
|
|
|
+ if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs))
|
|
|
+ return FR_NO_FILESYSTEM;
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+
|
|
|
+ fs->last_clust = fs->free_clust = 0xFFFFFFFF;
|
|
|
+
|
|
|
+
|
|
|
+ fs->fsi_flag = 0x80;
|
|
|
+#if (_FS_NOFSINFO & 3) != 3
|
|
|
+ if (fmt == FS_FAT32
|
|
|
+ && LD_WORD(fs->win+BPB_FSInfo) == 1
|
|
|
+ && move_window(fs, bsect + 1) == FR_OK)
|
|
|
+ {
|
|
|
+ fs->fsi_flag = 0;
|
|
|
+ if (LD_WORD(fs->win+BS_55AA) == 0xAA55
|
|
|
+ && LD_DWORD(fs->win+FSI_LeadSig) == 0x41615252
|
|
|
+ && LD_DWORD(fs->win+FSI_StrucSig) == 0x61417272)
|
|
|
+ {
|
|
|
+#if (_FS_NOFSINFO & 1) == 0
|
|
|
+ fs->free_clust = LD_DWORD(fs->win+FSI_Free_Count);
|
|
|
+#endif
|
|
|
+#if (_FS_NOFSINFO & 2) == 0
|
|
|
+ fs->last_clust = LD_DWORD(fs->win+FSI_Nxt_Free);
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+ fs->fs_type = fmt;
|
|
|
+ fs->id = ++Fsid;
|
|
|
+#if _FS_RPATH
|
|
|
+ fs->cdir = 0;
|
|
|
+#endif
|
|
|
+#if _FS_LOCK
|
|
|
+ clear_lock(fs);
|
|
|
+#endif
|
|
|
+
|
|
|
+ return FR_OK;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+FRESULT validate (
|
|
|
+ void* obj
|
|
|
+)
|
|
|
+{
|
|
|
+ FIL *fil = (FIL*)obj;
|
|
|
+
|
|
|
+
|
|
|
+ if (!fil || !fil->fs || !fil->fs->fs_type || fil->fs->id != fil->id)
|
|
|
+ return FR_INVALID_OBJECT;
|
|
|
+
|
|
|
+ ENTER_FF(fil->fs);
|
|
|
+
|
|
|
+ if (disk_status(fil->fs->drv) & STA_NOINIT)
|
|
|
+ return FR_NOT_READY;
|
|
|
+
|
|
|
+ return FR_OK;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Public Functions
|
|
|
+
|
|
|
+--------------------------------------------------------------------------*/
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_mount (
|
|
|
+ FATFS* fs,
|
|
|
+ const TCHAR* path,
|
|
|
+ BYTE opt
|
|
|
+)
|
|
|
+{
|
|
|
+ FATFS *cfs;
|
|
|
+ int vol;
|
|
|
+ FRESULT res;
|
|
|
+ const TCHAR *rp = path;
|
|
|
+
|
|
|
+
|
|
|
+ vol = get_ldnumber(&rp);
|
|
|
+ if (vol < 0) return FR_INVALID_DRIVE;
|
|
|
+ cfs = FatFs[vol];
|
|
|
+
|
|
|
+ if (cfs) {
|
|
|
+#if _FS_LOCK
|
|
|
+ clear_lock(cfs);
|
|
|
+#endif
|
|
|
+#if _FS_REENTRANT
|
|
|
+ if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR;
|
|
|
+#endif
|
|
|
+ cfs->fs_type = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (fs) {
|
|
|
+ fs->fs_type = 0;
|
|
|
+#if _FS_REENTRANT
|
|
|
+ if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR;
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ FatFs[vol] = fs;
|
|
|
+
|
|
|
+ if (!fs || opt != 1) return FR_OK;
|
|
|
+
|
|
|
+ res = find_volume(&fs, &path, 0);
|
|
|
+ LEAVE_FF(fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_open (
|
|
|
+ FIL* fp,
|
|
|
+ const TCHAR* path,
|
|
|
+ BYTE mode
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DIR dj;
|
|
|
+ BYTE *dir;
|
|
|
+ DEF_NAMEBUF;
|
|
|
+
|
|
|
+
|
|
|
+ if (!fp) return FR_INVALID_OBJECT;
|
|
|
+ fp->fs = 0;
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+ mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW;
|
|
|
+ res = find_volume(&dj.fs, &path, (BYTE)(mode & ~FA_READ));
|
|
|
+#else
|
|
|
+ mode &= FA_READ;
|
|
|
+ res = find_volume(&dj.fs, &path, 0);
|
|
|
+#endif
|
|
|
+ if (res == FR_OK) {
|
|
|
+ INIT_BUF(dj);
|
|
|
+ res = follow_path(&dj, path);
|
|
|
+ dir = dj.dir;
|
|
|
+#if !_FS_READONLY
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (!dir)
|
|
|
+ res = FR_INVALID_NAME;
|
|
|
+#if _FS_LOCK
|
|
|
+ else
|
|
|
+ res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
|
|
|
+#endif
|
|
|
+ }
|
|
|
+
|
|
|
+ if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
|
|
|
+ DWORD dw, cl;
|
|
|
+
|
|
|
+ if (res != FR_OK) {
|
|
|
+ if (res == FR_NO_FILE)
|
|
|
+#if _FS_LOCK
|
|
|
+ res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
|
|
|
+#else
|
|
|
+ res = dir_register(&dj);
|
|
|
+#endif
|
|
|
+ mode |= FA_CREATE_ALWAYS;
|
|
|
+ dir = dj.dir;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (dir[DIR_Attr] & (AM_RDO | AM_DIR)) {
|
|
|
+ res = FR_DENIED;
|
|
|
+ } else {
|
|
|
+ if (mode & FA_CREATE_NEW)
|
|
|
+ res = FR_EXIST;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) {
|
|
|
+ dw = get_fattime();
|
|
|
+ ST_DWORD(dir+DIR_CrtTime, dw);
|
|
|
+ dir[DIR_Attr] = 0;
|
|
|
+ ST_DWORD(dir+DIR_FileSize, 0);
|
|
|
+ cl = ld_clust(dj.fs, dir);
|
|
|
+ st_clust(dir, 0);
|
|
|
+ dj.fs->wflag = 1;
|
|
|
+ if (cl) {
|
|
|
+ dw = dj.fs->winsect;
|
|
|
+ res = remove_chain(dj.fs, cl);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ dj.fs->last_clust = cl - 1;
|
|
|
+ res = move_window(dj.fs, dw);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (dir[DIR_Attr] & AM_DIR) {
|
|
|
+ res = FR_NO_FILE;
|
|
|
+ } else {
|
|
|
+ if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO))
|
|
|
+ res = FR_DENIED;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (mode & FA_CREATE_ALWAYS)
|
|
|
+ mode |= FA__WRITTEN;
|
|
|
+ fp->dir_sect = dj.fs->winsect;
|
|
|
+ fp->dir_ptr = dir;
|
|
|
+#if _FS_LOCK
|
|
|
+ fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
|
|
|
+ if (!fp->lockid) res = FR_INT_ERR;
|
|
|
+#endif
|
|
|
+ }
|
|
|
+
|
|
|
+#else
|
|
|
+ if (res == FR_OK) {
|
|
|
+ dir = dj.dir;
|
|
|
+ if (!dir) {
|
|
|
+ res = FR_INVALID_NAME;
|
|
|
+ } else {
|
|
|
+ if (dir[DIR_Attr] & AM_DIR)
|
|
|
+ res = FR_NO_FILE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ FREE_BUF();
|
|
|
+
|
|
|
+ if (res == FR_OK) {
|
|
|
+ fp->flag = mode;
|
|
|
+ fp->err = 0;
|
|
|
+ fp->sclust = ld_clust(dj.fs, dir);
|
|
|
+ fp->fsize = LD_DWORD(dir+DIR_FileSize);
|
|
|
+ fp->fptr = 0;
|
|
|
+ fp->dsect = 0;
|
|
|
+#if _USE_FASTSEEK
|
|
|
+ fp->cltbl = 0;
|
|
|
+#endif
|
|
|
+ fp->fs = dj.fs;
|
|
|
+ fp->id = fp->fs->id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(dj.fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_read (
|
|
|
+ FIL* fp,
|
|
|
+ void* buff,
|
|
|
+ UINT btr,
|
|
|
+ UINT* br
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DWORD clst, sect, remain;
|
|
|
+ UINT rcnt, cc;
|
|
|
+ BYTE csect, *rbuff = (BYTE*)buff;
|
|
|
+
|
|
|
+
|
|
|
+ *br = 0;
|
|
|
+
|
|
|
+ res = validate(fp);
|
|
|
+ if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
|
|
+ if (fp->err)
|
|
|
+ LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
|
|
+ if (!(fp->flag & FA_READ))
|
|
|
+ LEAVE_FF(fp->fs, FR_DENIED);
|
|
|
+ remain = fp->fsize - fp->fptr;
|
|
|
+ if (btr > remain) btr = (UINT)remain;
|
|
|
+
|
|
|
+ for ( ; btr;
|
|
|
+ rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
|
|
|
+ if ((fp->fptr % SS(fp->fs)) == 0) {
|
|
|
+ csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1));
|
|
|
+ if (!csect) {
|
|
|
+ if (fp->fptr == 0) {
|
|
|
+ clst = fp->sclust;
|
|
|
+ } else {
|
|
|
+#if _USE_FASTSEEK
|
|
|
+ if (fp->cltbl)
|
|
|
+ clst = clmt_clust(fp, fp->fptr);
|
|
|
+ else
|
|
|
+#endif
|
|
|
+ clst = get_fat(fp->fs, fp->clust);
|
|
|
+ }
|
|
|
+ if (clst < 2) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ fp->clust = clst;
|
|
|
+ }
|
|
|
+ sect = clust2sect(fp->fs, fp->clust);
|
|
|
+ if (!sect) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ sect += csect;
|
|
|
+ cc = btr / SS(fp->fs);
|
|
|
+ if (cc) {
|
|
|
+ if (csect + cc > fp->fs->csize)
|
|
|
+ cc = fp->fs->csize - csect;
|
|
|
+ if (disk_read(fp->fs->drv, rbuff, sect, cc))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+#if !_FS_READONLY && _FS_MINIMIZE <= 2
|
|
|
+#if _FS_TINY
|
|
|
+ if (fp->fs->wflag && fp->fs->winsect - sect < cc)
|
|
|
+ mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));
|
|
|
+#else
|
|
|
+ if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc)
|
|
|
+ mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+ rcnt = SS(fp->fs) * cc;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+#if !_FS_TINY
|
|
|
+ if (fp->dsect != sect) {
|
|
|
+#if !_FS_READONLY
|
|
|
+ if (fp->flag & FA__DIRTY) {
|
|
|
+ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ fp->flag &= ~FA__DIRTY;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ if (disk_read(fp->fs->drv, fp->buf, sect, 1))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ fp->dsect = sect;
|
|
|
+ }
|
|
|
+ rcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs));
|
|
|
+ if (rcnt > btr) rcnt = btr;
|
|
|
+#if _FS_TINY
|
|
|
+ if (move_window(fp->fs, fp->dsect))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt);
|
|
|
+#else
|
|
|
+ mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt);
|
|
|
+#endif
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(fp->fs, FR_OK);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_write (
|
|
|
+ FIL* fp,
|
|
|
+ const void *buff,
|
|
|
+ UINT btw,
|
|
|
+ UINT* bw
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DWORD clst, sect;
|
|
|
+ UINT wcnt, cc;
|
|
|
+ const BYTE *wbuff = (const BYTE*)buff;
|
|
|
+ BYTE csect;
|
|
|
+
|
|
|
+
|
|
|
+ *bw = 0;
|
|
|
+
|
|
|
+ res = validate(fp);
|
|
|
+ if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
|
|
+ if (fp->err)
|
|
|
+ LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
|
|
+ if (!(fp->flag & FA_WRITE))
|
|
|
+ LEAVE_FF(fp->fs, FR_DENIED);
|
|
|
+ if (fp->fptr + btw < fp->fptr) btw = 0;
|
|
|
+
|
|
|
+ for ( ; btw;
|
|
|
+ wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
|
|
|
+ if ((fp->fptr % SS(fp->fs)) == 0) {
|
|
|
+ csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1));
|
|
|
+ if (!csect) {
|
|
|
+ if (fp->fptr == 0) {
|
|
|
+ clst = fp->sclust;
|
|
|
+ if (clst == 0)
|
|
|
+ clst = create_chain(fp->fs, 0);
|
|
|
+ } else {
|
|
|
+#if _USE_FASTSEEK
|
|
|
+ if (fp->cltbl)
|
|
|
+ clst = clmt_clust(fp, fp->fptr);
|
|
|
+ else
|
|
|
+#endif
|
|
|
+ clst = create_chain(fp->fs, fp->clust);
|
|
|
+ }
|
|
|
+ if (clst == 0) break;
|
|
|
+ if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ fp->clust = clst;
|
|
|
+ if (fp->sclust == 0) fp->sclust = clst;
|
|
|
+ }
|
|
|
+#if _FS_TINY
|
|
|
+ if (fp->fs->winsect == fp->dsect && sync_window(fp->fs))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+#else
|
|
|
+ if (fp->flag & FA__DIRTY) {
|
|
|
+ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ fp->flag &= ~FA__DIRTY;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ sect = clust2sect(fp->fs, fp->clust);
|
|
|
+ if (!sect) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ sect += csect;
|
|
|
+ cc = btw / SS(fp->fs);
|
|
|
+ if (cc) {
|
|
|
+ if (csect + cc > fp->fs->csize)
|
|
|
+ cc = fp->fs->csize - csect;
|
|
|
+ if (disk_write(fp->fs->drv, wbuff, sect, cc))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+#if _FS_MINIMIZE <= 2
|
|
|
+#if _FS_TINY
|
|
|
+ if (fp->fs->winsect - sect < cc) {
|
|
|
+ mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
|
|
|
+ fp->fs->wflag = 0;
|
|
|
+ }
|
|
|
+#else
|
|
|
+ if (fp->dsect - sect < cc) {
|
|
|
+ mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
|
|
|
+ fp->flag &= ~FA__DIRTY;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+ wcnt = SS(fp->fs) * cc;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+#if _FS_TINY
|
|
|
+ if (fp->fptr >= fp->fsize) {
|
|
|
+ if (sync_window(fp->fs)) ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ fp->fs->winsect = sect;
|
|
|
+ }
|
|
|
+#else
|
|
|
+ if (fp->dsect != sect) {
|
|
|
+ if (fp->fptr < fp->fsize &&
|
|
|
+ disk_read(fp->fs->drv, fp->buf, sect, 1))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ fp->dsect = sect;
|
|
|
+ }
|
|
|
+ wcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs));
|
|
|
+ if (wcnt > btw) wcnt = btw;
|
|
|
+#if _FS_TINY
|
|
|
+ if (move_window(fp->fs, fp->dsect))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt);
|
|
|
+ fp->fs->wflag = 1;
|
|
|
+#else
|
|
|
+ mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt);
|
|
|
+ fp->flag |= FA__DIRTY;
|
|
|
+#endif
|
|
|
+ }
|
|
|
+
|
|
|
+ if (fp->fptr > fp->fsize) fp->fsize = fp->fptr;
|
|
|
+ fp->flag |= FA__WRITTEN;
|
|
|
+
|
|
|
+ LEAVE_FF(fp->fs, FR_OK);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_sync (
|
|
|
+ FIL* fp
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DWORD tm;
|
|
|
+ BYTE *dir;
|
|
|
+
|
|
|
+
|
|
|
+ res = validate(fp);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (fp->flag & FA__WRITTEN) {
|
|
|
+
|
|
|
+#if !_FS_TINY
|
|
|
+ if (fp->flag & FA__DIRTY) {
|
|
|
+ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
|
|
|
+ LEAVE_FF(fp->fs, FR_DISK_ERR);
|
|
|
+ fp->flag &= ~FA__DIRTY;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ res = move_window(fp->fs, fp->dir_sect);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ dir = fp->dir_ptr;
|
|
|
+ dir[DIR_Attr] |= AM_ARC;
|
|
|
+ ST_DWORD(dir+DIR_FileSize, fp->fsize);
|
|
|
+ st_clust(dir, fp->sclust);
|
|
|
+ tm = get_fattime();
|
|
|
+ ST_DWORD(dir+DIR_WrtTime, tm);
|
|
|
+ ST_WORD(dir+DIR_LstAccDate, 0);
|
|
|
+ fp->flag &= ~FA__WRITTEN;
|
|
|
+ fp->fs->wflag = 1;
|
|
|
+ res = sync_fs(fp->fs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(fp->fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_close (
|
|
|
+ FIL *fp
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+ res = f_sync(fp);
|
|
|
+ if (res == FR_OK)
|
|
|
+#endif
|
|
|
+ {
|
|
|
+ res = validate(fp);
|
|
|
+ if (res == FR_OK) {
|
|
|
+#if _FS_REENTRANT
|
|
|
+ FATFS *fs = fp->fs;
|
|
|
+#endif
|
|
|
+#if _FS_LOCK
|
|
|
+ res = dec_lock(fp->lockid);
|
|
|
+ if (res == FR_OK)
|
|
|
+#endif
|
|
|
+ fp->fs = 0;
|
|
|
+#if _FS_REENTRANT
|
|
|
+ unlock_fs(fs, FR_OK);
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_RPATH >= 1
|
|
|
+#if _VOLUMES >= 2
|
|
|
+FRESULT f_chdrive (
|
|
|
+ const TCHAR* path
|
|
|
+)
|
|
|
+{
|
|
|
+ int vol;
|
|
|
+
|
|
|
+
|
|
|
+ vol = get_ldnumber(&path);
|
|
|
+ if (vol < 0) return FR_INVALID_DRIVE;
|
|
|
+
|
|
|
+ CurrVol = (BYTE)vol;
|
|
|
+
|
|
|
+ return FR_OK;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_chdir (
|
|
|
+ const TCHAR* path
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DIR dj;
|
|
|
+ DEF_NAMEBUF;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ res = find_volume(&dj.fs, &path, 0);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ INIT_BUF(dj);
|
|
|
+ res = follow_path(&dj, path);
|
|
|
+ FREE_BUF();
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (!dj.dir) {
|
|
|
+ dj.fs->cdir = dj.sclust;
|
|
|
+ } else {
|
|
|
+ if (dj.dir[DIR_Attr] & AM_DIR)
|
|
|
+ dj.fs->cdir = ld_clust(dj.fs, dj.dir);
|
|
|
+ else
|
|
|
+ res = FR_NO_PATH;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (res == FR_NO_FILE) res = FR_NO_PATH;
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(dj.fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_RPATH >= 2
|
|
|
+FRESULT f_getcwd (
|
|
|
+ TCHAR* buff,
|
|
|
+ UINT len
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DIR dj;
|
|
|
+ UINT i, n;
|
|
|
+ DWORD ccl;
|
|
|
+ TCHAR *tp;
|
|
|
+ FILINFO fno;
|
|
|
+ DEF_NAMEBUF;
|
|
|
+
|
|
|
+
|
|
|
+ *buff = 0;
|
|
|
+
|
|
|
+ res = find_volume(&dj.fs, (const TCHAR**)&buff, 0);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ INIT_BUF(dj);
|
|
|
+ i = len;
|
|
|
+ dj.sclust = dj.fs->cdir;
|
|
|
+ while ((ccl = dj.sclust) != 0) {
|
|
|
+ res = dir_sdi(&dj, 1);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ res = dir_read(&dj, 0);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ dj.sclust = ld_clust(dj.fs, dj.dir);
|
|
|
+ res = dir_sdi(&dj, 0);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ do {
|
|
|
+ res = dir_read(&dj, 0);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ if (ccl == ld_clust(dj.fs, dj.dir)) break;
|
|
|
+ res = dir_next(&dj, 0);
|
|
|
+ } while (res == FR_OK);
|
|
|
+ if (res == FR_NO_FILE) res = FR_INT_ERR;
|
|
|
+ if (res != FR_OK) break;
|
|
|
+#if _USE_LFN
|
|
|
+ fno.lfname = buff;
|
|
|
+ fno.lfsize = i;
|
|
|
+#endif
|
|
|
+ get_fileinfo(&dj, &fno);
|
|
|
+ tp = fno.fname;
|
|
|
+#if _USE_LFN
|
|
|
+ if (*buff) tp = buff;
|
|
|
+#endif
|
|
|
+ for (n = 0; tp[n]; n++) ;
|
|
|
+ if (i < n + 3) {
|
|
|
+ res = FR_NOT_ENOUGH_CORE; break;
|
|
|
+ }
|
|
|
+ while (n) buff[--i] = tp[--n];
|
|
|
+ buff[--i] = '/';
|
|
|
+ }
|
|
|
+ tp = buff;
|
|
|
+ if (res == FR_OK) {
|
|
|
+#if _VOLUMES >= 2
|
|
|
+ *tp++ = '0' + CurrVol;
|
|
|
+ *tp++ = ':';
|
|
|
+#endif
|
|
|
+ if (i == len) {
|
|
|
+ *tp++ = '/';
|
|
|
+ } else {
|
|
|
+ do
|
|
|
+ *tp++ = buff[i++];
|
|
|
+ while (i < len);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ *tp = 0;
|
|
|
+ FREE_BUF();
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(dj.fs, res);
|
|
|
+}
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_MINIMIZE <= 2
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_lseek (
|
|
|
+ FIL* fp,
|
|
|
+ DWORD ofs
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+
|
|
|
+
|
|
|
+ res = validate(fp);
|
|
|
+ if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
|
|
+ if (fp->err)
|
|
|
+ LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
|
|
+
|
|
|
+#if _USE_FASTSEEK
|
|
|
+ if (fp->cltbl) {
|
|
|
+ DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;
|
|
|
+
|
|
|
+ if (ofs == CREATE_LINKMAP) {
|
|
|
+ tbl = fp->cltbl;
|
|
|
+ tlen = *tbl++; ulen = 2;
|
|
|
+ cl = fp->sclust;
|
|
|
+ if (cl) {
|
|
|
+ do {
|
|
|
+
|
|
|
+ tcl = cl; ncl = 0; ulen += 2;
|
|
|
+ do {
|
|
|
+ pcl = cl; ncl++;
|
|
|
+ cl = get_fat(fp->fs, cl);
|
|
|
+ if (cl <= 1) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ if (cl == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ } while (cl == pcl + 1);
|
|
|
+ if (ulen <= tlen) {
|
|
|
+ *tbl++ = ncl; *tbl++ = tcl;
|
|
|
+ }
|
|
|
+ } while (cl < fp->fs->n_fatent);
|
|
|
+ }
|
|
|
+ *fp->cltbl = ulen;
|
|
|
+ if (ulen <= tlen)
|
|
|
+ *tbl = 0;
|
|
|
+ else
|
|
|
+ res = FR_NOT_ENOUGH_CORE;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ if (ofs > fp->fsize)
|
|
|
+ ofs = fp->fsize;
|
|
|
+ fp->fptr = ofs;
|
|
|
+ if (ofs) {
|
|
|
+ fp->clust = clmt_clust(fp, ofs - 1);
|
|
|
+ dsc = clust2sect(fp->fs, fp->clust);
|
|
|
+ if (!dsc) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ dsc += (ofs - 1) / SS(fp->fs) & (fp->fs->csize - 1);
|
|
|
+ if (fp->fptr % SS(fp->fs) && dsc != fp->dsect) {
|
|
|
+#if !_FS_TINY
|
|
|
+#if !_FS_READONLY
|
|
|
+ if (fp->flag & FA__DIRTY) {
|
|
|
+ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ fp->flag &= ~FA__DIRTY;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ if (disk_read(fp->fs->drv, fp->buf, dsc, 1))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+#endif
|
|
|
+ fp->dsect = dsc;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ DWORD clst, bcs, nsect, ifptr;
|
|
|
+
|
|
|
+ if (ofs > fp->fsize
|
|
|
+#if !_FS_READONLY
|
|
|
+ && !(fp->flag & FA_WRITE)
|
|
|
+#endif
|
|
|
+ ) ofs = fp->fsize;
|
|
|
+
|
|
|
+ ifptr = fp->fptr;
|
|
|
+ fp->fptr = nsect = 0;
|
|
|
+ if (ofs) {
|
|
|
+ bcs = (DWORD)fp->fs->csize * SS(fp->fs);
|
|
|
+ if (ifptr > 0 &&
|
|
|
+ (ofs - 1) / bcs >= (ifptr - 1) / bcs) {
|
|
|
+ fp->fptr = (ifptr - 1) & ~(bcs - 1);
|
|
|
+ ofs -= fp->fptr;
|
|
|
+ clst = fp->clust;
|
|
|
+ } else {
|
|
|
+ clst = fp->sclust;
|
|
|
+#if !_FS_READONLY
|
|
|
+ if (clst == 0) {
|
|
|
+ clst = create_chain(fp->fs, 0);
|
|
|
+ if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ fp->sclust = clst;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ fp->clust = clst;
|
|
|
+ }
|
|
|
+ if (clst != 0) {
|
|
|
+ while (ofs > bcs) {
|
|
|
+#if !_FS_READONLY
|
|
|
+ if (fp->flag & FA_WRITE) {
|
|
|
+ clst = create_chain(fp->fs, clst);
|
|
|
+ if (clst == 0) {
|
|
|
+ ofs = bcs; break;
|
|
|
+ }
|
|
|
+ } else
|
|
|
+#endif
|
|
|
+ clst = get_fat(fp->fs, clst);
|
|
|
+ if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ if (clst <= 1 || clst >= fp->fs->n_fatent) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ fp->clust = clst;
|
|
|
+ fp->fptr += bcs;
|
|
|
+ ofs -= bcs;
|
|
|
+ }
|
|
|
+ fp->fptr += ofs;
|
|
|
+ if (ofs % SS(fp->fs)) {
|
|
|
+ nsect = clust2sect(fp->fs, clst);
|
|
|
+ if (!nsect) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ nsect += ofs / SS(fp->fs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fp->fptr % SS(fp->fs) && nsect != fp->dsect) {
|
|
|
+#if !_FS_TINY
|
|
|
+#if !_FS_READONLY
|
|
|
+ if (fp->flag & FA__DIRTY) {
|
|
|
+ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ fp->flag &= ~FA__DIRTY;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ if (disk_read(fp->fs->drv, fp->buf, nsect, 1))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+#endif
|
|
|
+ fp->dsect = nsect;
|
|
|
+ }
|
|
|
+#if !_FS_READONLY
|
|
|
+ if (fp->fptr > fp->fsize) {
|
|
|
+ fp->fsize = fp->fptr;
|
|
|
+ fp->flag |= FA__WRITTEN;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(fp->fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_MINIMIZE <= 1
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_opendir (
|
|
|
+ DIR* dp,
|
|
|
+ const TCHAR* path
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ FATFS* fs;
|
|
|
+ DEF_NAMEBUF;
|
|
|
+
|
|
|
+
|
|
|
+ if (!dp) return FR_INVALID_OBJECT;
|
|
|
+
|
|
|
+
|
|
|
+ res = find_volume(&fs, &path, 0);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ dp->fs = fs;
|
|
|
+ INIT_BUF(*dp);
|
|
|
+ res = follow_path(dp, path);
|
|
|
+ FREE_BUF();
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (dp->dir) {
|
|
|
+ if (dp->dir[DIR_Attr] & AM_DIR)
|
|
|
+ dp->sclust = ld_clust(fs, dp->dir);
|
|
|
+ else
|
|
|
+ res = FR_NO_PATH;
|
|
|
+ }
|
|
|
+ if (res == FR_OK) {
|
|
|
+ dp->id = fs->id;
|
|
|
+ res = dir_sdi(dp, 0);
|
|
|
+#if _FS_LOCK
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (dp->sclust) {
|
|
|
+ dp->lockid = inc_lock(dp, 0);
|
|
|
+ if (!dp->lockid)
|
|
|
+ res = FR_TOO_MANY_OPEN_FILES;
|
|
|
+ } else {
|
|
|
+ dp->lockid = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (res == FR_NO_FILE) res = FR_NO_PATH;
|
|
|
+ }
|
|
|
+ if (res != FR_OK) dp->fs = 0;
|
|
|
+
|
|
|
+ LEAVE_FF(fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_closedir (
|
|
|
+ DIR *dp
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+
|
|
|
+
|
|
|
+ res = validate(dp);
|
|
|
+ if (res == FR_OK) {
|
|
|
+#if _FS_REENTRANT
|
|
|
+ FATFS *fs = dp->fs;
|
|
|
+#endif
|
|
|
+#if _FS_LOCK
|
|
|
+ if (dp->lockid)
|
|
|
+ res = dec_lock(dp->lockid);
|
|
|
+ if (res == FR_OK)
|
|
|
+#endif
|
|
|
+ dp->fs = 0;
|
|
|
+#if _FS_REENTRANT
|
|
|
+ unlock_fs(fs, FR_OK);
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_readdir (
|
|
|
+ DIR* dp,
|
|
|
+ FILINFO* fno
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DEF_NAMEBUF;
|
|
|
+
|
|
|
+
|
|
|
+ res = validate(dp);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (!fno) {
|
|
|
+ res = dir_sdi(dp, 0);
|
|
|
+ } else {
|
|
|
+ INIT_BUF(*dp);
|
|
|
+ res = dir_read(dp, 0);
|
|
|
+ if (res == FR_NO_FILE) {
|
|
|
+ dp->sect = 0;
|
|
|
+ res = FR_OK;
|
|
|
+ }
|
|
|
+ if (res == FR_OK) {
|
|
|
+ get_fileinfo(dp, fno);
|
|
|
+ res = dir_next(dp, 0);
|
|
|
+ if (res == FR_NO_FILE) {
|
|
|
+ dp->sect = 0;
|
|
|
+ res = FR_OK;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FREE_BUF();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(dp->fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _FS_MINIMIZE == 0
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_stat (
|
|
|
+ const TCHAR* path,
|
|
|
+ FILINFO* fno
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DIR dj;
|
|
|
+ DEF_NAMEBUF;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ res = find_volume(&dj.fs, &path, 0);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ INIT_BUF(dj);
|
|
|
+ res = follow_path(&dj, path);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (dj.dir) {
|
|
|
+ if (fno) get_fileinfo(&dj, fno);
|
|
|
+ } else {
|
|
|
+ res = FR_INVALID_NAME;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FREE_BUF();
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(dj.fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_getfree (
|
|
|
+ const TCHAR* path,
|
|
|
+ DWORD* nclst,
|
|
|
+ FATFS** fatfs
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ FATFS *fs;
|
|
|
+ DWORD n, clst, sect, stat;
|
|
|
+ UINT i;
|
|
|
+ BYTE fat, *p;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ res = find_volume(fatfs, &path, 0);
|
|
|
+ fs = *fatfs;
|
|
|
+ if (res == FR_OK) {
|
|
|
+
|
|
|
+ if (fs->free_clust <= fs->n_fatent - 2) {
|
|
|
+ *nclst = fs->free_clust;
|
|
|
+ } else {
|
|
|
+
|
|
|
+ fat = fs->fs_type;
|
|
|
+ n = 0;
|
|
|
+ if (fat == FS_FAT12) {
|
|
|
+ clst = 2;
|
|
|
+ do {
|
|
|
+ stat = get_fat(fs, clst);
|
|
|
+ if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
|
|
|
+ if (stat == 1) { res = FR_INT_ERR; break; }
|
|
|
+ if (stat == 0) n++;
|
|
|
+ } while (++clst < fs->n_fatent);
|
|
|
+ } else {
|
|
|
+ clst = fs->n_fatent;
|
|
|
+ sect = fs->fatbase;
|
|
|
+ i = 0; p = 0;
|
|
|
+ do {
|
|
|
+ if (!i) {
|
|
|
+ res = move_window(fs, sect++);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ p = fs->win;
|
|
|
+ i = SS(fs);
|
|
|
+ }
|
|
|
+ if (fat == FS_FAT16) {
|
|
|
+ if (LD_WORD(p) == 0) n++;
|
|
|
+ p += 2; i -= 2;
|
|
|
+ } else {
|
|
|
+ if ((LD_DWORD(p) & 0x0FFFFFFF) == 0) n++;
|
|
|
+ p += 4; i -= 4;
|
|
|
+ }
|
|
|
+ } while (--clst);
|
|
|
+ }
|
|
|
+ fs->free_clust = n;
|
|
|
+ fs->fsi_flag |= 1;
|
|
|
+ *nclst = n;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ LEAVE_FF(fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_truncate (
|
|
|
+ FIL* fp
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DWORD ncl;
|
|
|
+
|
|
|
+
|
|
|
+ res = validate(fp);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (fp->err) {
|
|
|
+ res = (FRESULT)fp->err;
|
|
|
+ } else {
|
|
|
+ if (!(fp->flag & FA_WRITE))
|
|
|
+ res = FR_DENIED;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (fp->fsize > fp->fptr) {
|
|
|
+ fp->fsize = fp->fptr;
|
|
|
+ fp->flag |= FA__WRITTEN;
|
|
|
+ if (fp->fptr == 0) {
|
|
|
+ res = remove_chain(fp->fs, fp->sclust);
|
|
|
+ fp->sclust = 0;
|
|
|
+ } else {
|
|
|
+ ncl = get_fat(fp->fs, fp->clust);
|
|
|
+ res = FR_OK;
|
|
|
+ if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;
|
|
|
+ if (ncl == 1) res = FR_INT_ERR;
|
|
|
+ if (res == FR_OK && ncl < fp->fs->n_fatent) {
|
|
|
+ res = put_fat(fp->fs, fp->clust, 0x0FFFFFFF);
|
|
|
+ if (res == FR_OK) res = remove_chain(fp->fs, ncl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+#if !_FS_TINY
|
|
|
+ if (res == FR_OK && (fp->flag & FA__DIRTY)) {
|
|
|
+ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
|
|
|
+ res = FR_DISK_ERR;
|
|
|
+ else
|
|
|
+ fp->flag &= ~FA__DIRTY;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ if (res != FR_OK) fp->err = (FRESULT)res;
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(fp->fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_unlink (
|
|
|
+ const TCHAR* path
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DIR dj, sdj;
|
|
|
+ BYTE *dir;
|
|
|
+ DWORD dclst;
|
|
|
+ DEF_NAMEBUF;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ res = find_volume(&dj.fs, &path, 1);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ INIT_BUF(dj);
|
|
|
+ res = follow_path(&dj, path);
|
|
|
+ if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
|
|
|
+ res = FR_INVALID_NAME;
|
|
|
+#if _FS_LOCK
|
|
|
+ if (res == FR_OK) res = chk_lock(&dj, 2);
|
|
|
+#endif
|
|
|
+ if (res == FR_OK) {
|
|
|
+ dir = dj.dir;
|
|
|
+ if (!dir) {
|
|
|
+ res = FR_INVALID_NAME;
|
|
|
+ } else {
|
|
|
+ if (dir[DIR_Attr] & AM_RDO)
|
|
|
+ res = FR_DENIED;
|
|
|
+ }
|
|
|
+ dclst = ld_clust(dj.fs, dir);
|
|
|
+ if (res == FR_OK && (dir[DIR_Attr] & AM_DIR)) {
|
|
|
+ if (dclst < 2) {
|
|
|
+ res = FR_INT_ERR;
|
|
|
+ } else {
|
|
|
+ mem_cpy(&sdj, &dj, sizeof (DIR));
|
|
|
+ sdj.sclust = dclst;
|
|
|
+ res = dir_sdi(&sdj, 2);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ res = dir_read(&sdj, 0);
|
|
|
+ if (res == FR_OK
|
|
|
+#if _FS_RPATH
|
|
|
+ || dclst == dj.fs->cdir
|
|
|
+#endif
|
|
|
+ ) res = FR_DENIED;
|
|
|
+ if (res == FR_NO_FILE) res = FR_OK;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (res == FR_OK) {
|
|
|
+ res = dir_remove(&dj);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (dclst)
|
|
|
+ res = remove_chain(dj.fs, dclst);
|
|
|
+ if (res == FR_OK) res = sync_fs(dj.fs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FREE_BUF();
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(dj.fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_mkdir (
|
|
|
+ const TCHAR* path
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DIR dj;
|
|
|
+ BYTE *dir, n;
|
|
|
+ DWORD dsc, dcl, pcl, tm = get_fattime();
|
|
|
+ DEF_NAMEBUF;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ res = find_volume(&dj.fs, &path, 1);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ INIT_BUF(dj);
|
|
|
+ res = follow_path(&dj, path);
|
|
|
+ if (res == FR_OK) res = FR_EXIST;
|
|
|
+ if (_FS_RPATH && res == FR_NO_FILE && (dj.fn[NS] & NS_DOT))
|
|
|
+ res = FR_INVALID_NAME;
|
|
|
+ if (res == FR_NO_FILE) {
|
|
|
+ dcl = create_chain(dj.fs, 0);
|
|
|
+ res = FR_OK;
|
|
|
+ if (dcl == 0) res = FR_DENIED;
|
|
|
+ if (dcl == 1) res = FR_INT_ERR;
|
|
|
+ if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
|
|
|
+ if (res == FR_OK)
|
|
|
+ res = sync_window(dj.fs);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ dsc = clust2sect(dj.fs, dcl);
|
|
|
+ dir = dj.fs->win;
|
|
|
+ mem_set(dir, 0, SS(dj.fs));
|
|
|
+ mem_set(dir+DIR_Name, ' ', 11);
|
|
|
+ dir[DIR_Name] = '.';
|
|
|
+ dir[DIR_Attr] = AM_DIR;
|
|
|
+ ST_DWORD(dir+DIR_WrtTime, tm);
|
|
|
+ st_clust(dir, dcl);
|
|
|
+ mem_cpy(dir+SZ_DIR, dir, SZ_DIR);
|
|
|
+ dir[SZ_DIR+1] = '.'; pcl = dj.sclust;
|
|
|
+ if (dj.fs->fs_type == FS_FAT32 && pcl == dj.fs->dirbase)
|
|
|
+ pcl = 0;
|
|
|
+ st_clust(dir+SZ_DIR, pcl);
|
|
|
+ for (n = dj.fs->csize; n; n--) {
|
|
|
+ dj.fs->winsect = dsc++;
|
|
|
+ dj.fs->wflag = 1;
|
|
|
+ res = sync_window(dj.fs);
|
|
|
+ if (res != FR_OK) break;
|
|
|
+ mem_set(dir, 0, SS(dj.fs));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (res == FR_OK) res = dir_register(&dj);
|
|
|
+ if (res != FR_OK) {
|
|
|
+ remove_chain(dj.fs, dcl);
|
|
|
+ } else {
|
|
|
+ dir = dj.dir;
|
|
|
+ dir[DIR_Attr] = AM_DIR;
|
|
|
+ ST_DWORD(dir+DIR_WrtTime, tm);
|
|
|
+ st_clust(dir, dcl);
|
|
|
+ dj.fs->wflag = 1;
|
|
|
+ res = sync_fs(dj.fs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FREE_BUF();
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(dj.fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_chmod (
|
|
|
+ const TCHAR* path,
|
|
|
+ BYTE value,
|
|
|
+ BYTE mask
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DIR dj;
|
|
|
+ BYTE *dir;
|
|
|
+ DEF_NAMEBUF;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ res = find_volume(&dj.fs, &path, 1);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ INIT_BUF(dj);
|
|
|
+ res = follow_path(&dj, path);
|
|
|
+ FREE_BUF();
|
|
|
+ if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
|
|
|
+ res = FR_INVALID_NAME;
|
|
|
+ if (res == FR_OK) {
|
|
|
+ dir = dj.dir;
|
|
|
+ if (!dir) {
|
|
|
+ res = FR_INVALID_NAME;
|
|
|
+ } else {
|
|
|
+ mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC;
|
|
|
+ dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask);
|
|
|
+ dj.fs->wflag = 1;
|
|
|
+ res = sync_fs(dj.fs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(dj.fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_utime (
|
|
|
+ const TCHAR* path,
|
|
|
+ const FILINFO* fno
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DIR dj;
|
|
|
+ BYTE *dir;
|
|
|
+ DEF_NAMEBUF;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ res = find_volume(&dj.fs, &path, 1);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ INIT_BUF(dj);
|
|
|
+ res = follow_path(&dj, path);
|
|
|
+ FREE_BUF();
|
|
|
+ if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
|
|
|
+ res = FR_INVALID_NAME;
|
|
|
+ if (res == FR_OK) {
|
|
|
+ dir = dj.dir;
|
|
|
+ if (!dir) {
|
|
|
+ res = FR_INVALID_NAME;
|
|
|
+ } else {
|
|
|
+ ST_WORD(dir+DIR_WrtTime, fno->ftime);
|
|
|
+ ST_WORD(dir+DIR_WrtDate, fno->fdate);
|
|
|
+ dj.fs->wflag = 1;
|
|
|
+ res = sync_fs(dj.fs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(dj.fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_rename (
|
|
|
+ const TCHAR* path_old,
|
|
|
+ const TCHAR* path_new
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DIR djo, djn;
|
|
|
+ BYTE buf[21], *dir;
|
|
|
+ DWORD dw;
|
|
|
+ DEF_NAMEBUF;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ res = find_volume(&djo.fs, &path_old, 1);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ djn.fs = djo.fs;
|
|
|
+ INIT_BUF(djo);
|
|
|
+ res = follow_path(&djo, path_old);
|
|
|
+ if (_FS_RPATH && res == FR_OK && (djo.fn[NS] & NS_DOT))
|
|
|
+ res = FR_INVALID_NAME;
|
|
|
+#if _FS_LOCK
|
|
|
+ if (res == FR_OK) res = chk_lock(&djo, 2);
|
|
|
+#endif
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (!djo.dir) {
|
|
|
+ res = FR_NO_FILE;
|
|
|
+ } else {
|
|
|
+ mem_cpy(buf, djo.dir+DIR_Attr, 21);
|
|
|
+ mem_cpy(&djn, &djo, sizeof (DIR));
|
|
|
+ if (get_ldnumber(&path_new) >= 0)
|
|
|
+ res = follow_path(&djn, path_new);
|
|
|
+ else
|
|
|
+ res = FR_INVALID_DRIVE;
|
|
|
+ if (res == FR_OK) res = FR_EXIST;
|
|
|
+ if (res == FR_NO_FILE) {
|
|
|
+
|
|
|
+ res = dir_register(&djn);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ dir = djn.dir;
|
|
|
+ mem_cpy(dir+13, buf+2, 19);
|
|
|
+ dir[DIR_Attr] = buf[0] | AM_ARC;
|
|
|
+ djo.fs->wflag = 1;
|
|
|
+ if (djo.sclust != djn.sclust && (dir[DIR_Attr] & AM_DIR)) {
|
|
|
+ dw = clust2sect(djo.fs, ld_clust(djo.fs, dir));
|
|
|
+ if (!dw) {
|
|
|
+ res = FR_INT_ERR;
|
|
|
+ } else {
|
|
|
+ res = move_window(djo.fs, dw);
|
|
|
+ dir = djo.fs->win+SZ_DIR;
|
|
|
+ if (res == FR_OK && dir[1] == '.') {
|
|
|
+ dw = (djo.fs->fs_type == FS_FAT32 && djn.sclust == djo.fs->dirbase) ? 0 : djn.sclust;
|
|
|
+ st_clust(dir, dw);
|
|
|
+ djo.fs->wflag = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (res == FR_OK) {
|
|
|
+ res = dir_remove(&djo);
|
|
|
+ if (res == FR_OK)
|
|
|
+ res = sync_fs(djo.fs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FREE_BUF();
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(djo.fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _USE_LABEL
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_getlabel (
|
|
|
+ const TCHAR* path,
|
|
|
+ TCHAR* label,
|
|
|
+ DWORD* vsn
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DIR dj;
|
|
|
+ UINT i, j;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ res = find_volume(&dj.fs, &path, 0);
|
|
|
+
|
|
|
+
|
|
|
+ if (res == FR_OK && label) {
|
|
|
+ dj.sclust = 0;
|
|
|
+ res = dir_sdi(&dj, 0);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ res = dir_read(&dj, 1);
|
|
|
+ if (res == FR_OK) {
|
|
|
+#if _USE_LFN && _LFN_UNICODE
|
|
|
+ WCHAR w;
|
|
|
+ i = j = 0;
|
|
|
+ do {
|
|
|
+ w = (i < 11) ? dj.dir[i++] : ' ';
|
|
|
+ if (IsDBCS1(w) && i < 11 && IsDBCS2(dj.dir[i]))
|
|
|
+ w = w << 8 | dj.dir[i++];
|
|
|
+ label[j++] = ff_convert(w, 1);
|
|
|
+ } while (j < 11);
|
|
|
+#else
|
|
|
+ mem_cpy(label, dj.dir, 11);
|
|
|
+#endif
|
|
|
+ j = 11;
|
|
|
+ do {
|
|
|
+ label[j] = 0;
|
|
|
+ if (!j) break;
|
|
|
+ } while (label[--j] == ' ');
|
|
|
+ }
|
|
|
+ if (res == FR_NO_FILE) {
|
|
|
+ label[0] = 0;
|
|
|
+ res = FR_OK;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (res == FR_OK && vsn) {
|
|
|
+ res = move_window(dj.fs, dj.fs->volbase);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ i = dj.fs->fs_type == FS_FAT32 ? BS_VolID32 : BS_VolID;
|
|
|
+ *vsn = LD_DWORD(&dj.fs->win[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(dj.fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_setlabel (
|
|
|
+ const TCHAR* label
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DIR dj;
|
|
|
+ BYTE vn[11];
|
|
|
+ UINT i, j, sl;
|
|
|
+ WCHAR w;
|
|
|
+ DWORD tm;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ res = find_volume(&dj.fs, &label, 1);
|
|
|
+ if (res) LEAVE_FF(dj.fs, res);
|
|
|
+
|
|
|
+
|
|
|
+ vn[0] = 0;
|
|
|
+ for (sl = 0; label[sl]; sl++) ;
|
|
|
+ for ( ; sl && label[sl-1] == ' '; sl--) ;
|
|
|
+ if (sl) {
|
|
|
+ i = j = 0;
|
|
|
+ do {
|
|
|
+#if _USE_LFN && _LFN_UNICODE
|
|
|
+ w = ff_convert(ff_wtoupper(label[i++]), 0);
|
|
|
+#else
|
|
|
+ w = (BYTE)label[i++];
|
|
|
+ if (IsDBCS1(w))
|
|
|
+ w = (j < 10 && i < sl && IsDBCS2(label[i])) ? w << 8 | (BYTE)label[i++] : 0;
|
|
|
+#if _USE_LFN
|
|
|
+ w = ff_convert(ff_wtoupper(ff_convert(w, 1)), 0);
|
|
|
+#else
|
|
|
+ if (IsLower(w)) w -= 0x20;
|
|
|
+#ifdef _EXCVT
|
|
|
+ if (w >= 0x80) w = ExCvt[w - 0x80];
|
|
|
+#else
|
|
|
+ if (!_DF1S && w >= 0x80) w = 0;
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+ if (!w || chk_chr("\"*+,.:;<=>\?[]|\x7F", w) || j >= (UINT)((w >= 0x100) ? 10 : 11))
|
|
|
+ LEAVE_FF(dj.fs, FR_INVALID_NAME);
|
|
|
+ if (w >= 0x100) vn[j++] = (BYTE)(w >> 8);
|
|
|
+ vn[j++] = (BYTE)w;
|
|
|
+ } while (i < sl);
|
|
|
+ while (j < 11) vn[j++] = ' ';
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ dj.sclust = 0;
|
|
|
+ res = dir_sdi(&dj, 0);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ res = dir_read(&dj, 1);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ if (vn[0]) {
|
|
|
+ mem_cpy(dj.dir, vn, 11);
|
|
|
+ tm = get_fattime();
|
|
|
+ ST_DWORD(dj.dir+DIR_WrtTime, tm);
|
|
|
+ } else {
|
|
|
+ dj.dir[0] = DDE;
|
|
|
+ }
|
|
|
+ dj.fs->wflag = 1;
|
|
|
+ res = sync_fs(dj.fs);
|
|
|
+ } else {
|
|
|
+ if (res == FR_NO_FILE) {
|
|
|
+ res = FR_OK;
|
|
|
+ if (vn[0]) {
|
|
|
+ res = dir_alloc(&dj, 1);
|
|
|
+ if (res == FR_OK) {
|
|
|
+ mem_set(dj.dir, 0, SZ_DIR);
|
|
|
+ mem_cpy(dj.dir, vn, 11);
|
|
|
+ dj.dir[DIR_Attr] = AM_VOL;
|
|
|
+ tm = get_fattime();
|
|
|
+ ST_DWORD(dj.dir+DIR_WrtTime, tm);
|
|
|
+ dj.fs->wflag = 1;
|
|
|
+ res = sync_fs(dj.fs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(dj.fs, res);
|
|
|
+}
|
|
|
+
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _USE_FORWARD && _FS_TINY
|
|
|
+
|
|
|
+FRESULT f_forward (
|
|
|
+ FIL* fp,
|
|
|
+ UINT (*func)(const BYTE*,UINT),
|
|
|
+ UINT btf,
|
|
|
+ UINT* bf
|
|
|
+)
|
|
|
+{
|
|
|
+ FRESULT res;
|
|
|
+ DWORD remain, clst, sect;
|
|
|
+ UINT rcnt;
|
|
|
+ BYTE csect;
|
|
|
+
|
|
|
+
|
|
|
+ *bf = 0;
|
|
|
+
|
|
|
+ res = validate(fp);
|
|
|
+ if (res != FR_OK) LEAVE_FF(fp->fs, res);
|
|
|
+ if (fp->err)
|
|
|
+ LEAVE_FF(fp->fs, (FRESULT)fp->err);
|
|
|
+ if (!(fp->flag & FA_READ))
|
|
|
+ LEAVE_FF(fp->fs, FR_DENIED);
|
|
|
+
|
|
|
+ remain = fp->fsize - fp->fptr;
|
|
|
+ if (btf > remain) btf = (UINT)remain;
|
|
|
+
|
|
|
+ for ( ; btf && (*func)(0, 0);
|
|
|
+ fp->fptr += rcnt, *bf += rcnt, btf -= rcnt) {
|
|
|
+ csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1));
|
|
|
+ if ((fp->fptr % SS(fp->fs)) == 0) {
|
|
|
+ if (!csect) {
|
|
|
+ clst = (fp->fptr == 0) ?
|
|
|
+ fp->sclust : get_fat(fp->fs, fp->clust);
|
|
|
+ if (clst <= 1) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ fp->clust = clst;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sect = clust2sect(fp->fs, fp->clust);
|
|
|
+ if (!sect) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ sect += csect;
|
|
|
+ if (move_window(fp->fs, sect))
|
|
|
+ ABORT(fp->fs, FR_DISK_ERR);
|
|
|
+ fp->dsect = sect;
|
|
|
+ rcnt = SS(fp->fs) - (WORD)(fp->fptr % SS(fp->fs));
|
|
|
+ if (rcnt > btf) rcnt = btf;
|
|
|
+ rcnt = (*func)(&fp->fs->win[(WORD)fp->fptr % SS(fp->fs)], rcnt);
|
|
|
+ if (!rcnt) ABORT(fp->fs, FR_INT_ERR);
|
|
|
+ }
|
|
|
+
|
|
|
+ LEAVE_FF(fp->fs, FR_OK);
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _USE_MKFS && !_FS_READONLY
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#define N_ROOTDIR 512
|
|
|
+#define N_FATS 1
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_mkfs (
|
|
|
+ const TCHAR* path,
|
|
|
+ BYTE sfd,
|
|
|
+ UINT au
|
|
|
+)
|
|
|
+{
|
|
|
+ static const WORD vst[] = { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0};
|
|
|
+ static const WORD cst[] = {32768, 16384, 8192, 4096, 2048, 16384, 8192, 4096, 2048, 1024, 512};
|
|
|
+ int vol;
|
|
|
+ BYTE fmt, md, sys, *tbl, pdrv, part;
|
|
|
+ DWORD n_clst, vs, n, wsect;
|
|
|
+ UINT i;
|
|
|
+ DWORD b_vol, b_fat, b_dir, b_data;
|
|
|
+ DWORD n_vol, n_rsv, n_fat, n_dir;
|
|
|
+ FATFS *fs;
|
|
|
+ DSTATUS stat;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ vol = get_ldnumber(&path);
|
|
|
+ if (vol < 0) return FR_INVALID_DRIVE;
|
|
|
+ if (sfd > 1) return FR_INVALID_PARAMETER;
|
|
|
+ if (au & (au - 1)) return FR_INVALID_PARAMETER;
|
|
|
+ fs = FatFs[vol];
|
|
|
+ if (!fs) return FR_NOT_ENABLED;
|
|
|
+ fs->fs_type = 0;
|
|
|
+ pdrv = LD2PD(vol);
|
|
|
+ part = LD2PT(vol);
|
|
|
+
|
|
|
+
|
|
|
+ stat = disk_initialize(pdrv);
|
|
|
+ if (stat & STA_NOINIT) return FR_NOT_READY;
|
|
|
+ if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
|
|
|
+#if _MAX_SS != _MIN_SS
|
|
|
+ if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS || SS(fs) < _MIN_SS)
|
|
|
+ return FR_DISK_ERR;
|
|
|
+#endif
|
|
|
+ if (_MULTI_PARTITION && part) {
|
|
|
+
|
|
|
+ if (disk_read(pdrv, fs->win, 0, 1)) return FR_DISK_ERR;
|
|
|
+ if (LD_WORD(fs->win+BS_55AA) != 0xAA55) return FR_MKFS_ABORTED;
|
|
|
+ tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
|
|
|
+ if (!tbl[4]) return FR_MKFS_ABORTED;
|
|
|
+ b_vol = LD_DWORD(tbl+8);
|
|
|
+ n_vol = LD_DWORD(tbl+12);
|
|
|
+ } else {
|
|
|
+
|
|
|
+ if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &n_vol) != RES_OK || n_vol < 128)
|
|
|
+ return FR_DISK_ERR;
|
|
|
+ b_vol = (sfd) ? 0 : 63;
|
|
|
+ n_vol -= b_vol;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!au) {
|
|
|
+ vs = n_vol / (2000 / (SS(fs) / 512));
|
|
|
+ for (i = 0; vs < vst[i]; i++) ;
|
|
|
+ au = cst[i];
|
|
|
+ }
|
|
|
+ au /= SS(fs);
|
|
|
+ if (au == 0) au = 1;
|
|
|
+ if (au > 128) au = 128;
|
|
|
+
|
|
|
+
|
|
|
+ n_clst = n_vol / au;
|
|
|
+ fmt = FS_FAT12;
|
|
|
+ if (n_clst >= MIN_FAT16) fmt = FS_FAT16;
|
|
|
+ if (n_clst >= MIN_FAT32) fmt = FS_FAT32;
|
|
|
+
|
|
|
+
|
|
|
+ if (fmt == FS_FAT32) {
|
|
|
+ n_fat = ((n_clst * 4) + 8 + SS(fs) - 1) / SS(fs);
|
|
|
+ n_rsv = 32;
|
|
|
+ n_dir = 0;
|
|
|
+ } else {
|
|
|
+ n_fat = (fmt == FS_FAT12) ? (n_clst * 3 + 1) / 2 + 3 : (n_clst * 2) + 4;
|
|
|
+ n_fat = (n_fat + SS(fs) - 1) / SS(fs);
|
|
|
+ n_rsv = 1;
|
|
|
+ n_dir = (DWORD)N_ROOTDIR * SZ_DIR / SS(fs);
|
|
|
+ }
|
|
|
+ b_fat = b_vol + n_rsv;
|
|
|
+ b_dir = b_fat + n_fat * N_FATS;
|
|
|
+ b_data = b_dir + n_dir;
|
|
|
+ if (n_vol < b_data + au - b_vol) return FR_MKFS_ABORTED;
|
|
|
+
|
|
|
+
|
|
|
+ if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &n) != RES_OK || !n || n > 32768) n = 1;
|
|
|
+ n = (b_data + n - 1) & ~(n - 1);
|
|
|
+ n = (n - b_data) / N_FATS;
|
|
|
+ if (fmt == FS_FAT32) {
|
|
|
+ n_rsv += n;
|
|
|
+ b_fat += n;
|
|
|
+ } else {
|
|
|
+ n_fat += n;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ n_clst = (n_vol - n_rsv - n_fat * N_FATS - n_dir) / au;
|
|
|
+ if ( (fmt == FS_FAT16 && n_clst < MIN_FAT16)
|
|
|
+ || (fmt == FS_FAT32 && n_clst < MIN_FAT32))
|
|
|
+ return FR_MKFS_ABORTED;
|
|
|
+
|
|
|
+
|
|
|
+ if (fmt == FS_FAT32) {
|
|
|
+ sys = 0x0C;
|
|
|
+ } else {
|
|
|
+ if (fmt == FS_FAT12 && n_vol < 0x10000) {
|
|
|
+ sys = 0x01;
|
|
|
+ } else {
|
|
|
+ sys = (n_vol < 0x10000) ? 0x04 : 0x06;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (_MULTI_PARTITION && part) {
|
|
|
+
|
|
|
+ tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
|
|
|
+ tbl[4] = sys;
|
|
|
+ if (disk_write(pdrv, fs->win, 0, 1))
|
|
|
+ return FR_DISK_ERR;
|
|
|
+ md = 0xF8;
|
|
|
+ } else {
|
|
|
+ if (sfd) {
|
|
|
+ md = 0xF0;
|
|
|
+ } else {
|
|
|
+ mem_set(fs->win, 0, SS(fs));
|
|
|
+ tbl = fs->win+MBR_Table;
|
|
|
+ tbl[1] = 1;
|
|
|
+ tbl[2] = 1;
|
|
|
+ tbl[3] = 0;
|
|
|
+ tbl[4] = sys;
|
|
|
+ tbl[5] = 254;
|
|
|
+ n = (b_vol + n_vol) / 63 / 255;
|
|
|
+ tbl[6] = (BYTE)(n >> 2 | 63);
|
|
|
+ tbl[7] = (BYTE)n;
|
|
|
+ ST_DWORD(tbl+8, 63);
|
|
|
+ ST_DWORD(tbl+12, n_vol);
|
|
|
+ ST_WORD(fs->win+BS_55AA, 0xAA55);
|
|
|
+ if (disk_write(pdrv, fs->win, 0, 1))
|
|
|
+ return FR_DISK_ERR;
|
|
|
+ md = 0xF8;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ tbl = fs->win;
|
|
|
+ mem_set(tbl, 0, SS(fs));
|
|
|
+ mem_cpy(tbl, "\xEB\xFE\x90" "MSDOS5.0", 11);
|
|
|
+ i = SS(fs);
|
|
|
+ ST_WORD(tbl+BPB_BytsPerSec, i);
|
|
|
+ tbl[BPB_SecPerClus] = (BYTE)au;
|
|
|
+ ST_WORD(tbl+BPB_RsvdSecCnt, n_rsv);
|
|
|
+ tbl[BPB_NumFATs] = N_FATS;
|
|
|
+ i = (fmt == FS_FAT32) ? 0 : N_ROOTDIR;
|
|
|
+ ST_WORD(tbl+BPB_RootEntCnt, i);
|
|
|
+ if (n_vol < 0x10000) {
|
|
|
+ ST_WORD(tbl+BPB_TotSec16, n_vol);
|
|
|
+ } else {
|
|
|
+ ST_DWORD(tbl+BPB_TotSec32, n_vol);
|
|
|
+ }
|
|
|
+ tbl[BPB_Media] = md;
|
|
|
+ ST_WORD(tbl+BPB_SecPerTrk, 63);
|
|
|
+ ST_WORD(tbl+BPB_NumHeads, 255);
|
|
|
+ ST_DWORD(tbl+BPB_HiddSec, b_vol);
|
|
|
+ n = get_fattime();
|
|
|
+ if (fmt == FS_FAT32) {
|
|
|
+ ST_DWORD(tbl+BS_VolID32, n);
|
|
|
+ ST_DWORD(tbl+BPB_FATSz32, n_fat);
|
|
|
+ ST_DWORD(tbl+BPB_RootClus, 2);
|
|
|
+ ST_WORD(tbl+BPB_FSInfo, 1);
|
|
|
+ ST_WORD(tbl+BPB_BkBootSec, 6);
|
|
|
+ tbl[BS_DrvNum32] = 0x80;
|
|
|
+ tbl[BS_BootSig32] = 0x29;
|
|
|
+ mem_cpy(tbl+BS_VolLab32, "NO NAME " "FAT32 ", 19);
|
|
|
+ } else {
|
|
|
+ ST_DWORD(tbl+BS_VolID, n);
|
|
|
+ ST_WORD(tbl+BPB_FATSz16, n_fat);
|
|
|
+ tbl[BS_DrvNum] = 0x80;
|
|
|
+ tbl[BS_BootSig] = 0x29;
|
|
|
+ mem_cpy(tbl+BS_VolLab, "NO NAME " "FAT ", 19);
|
|
|
+ }
|
|
|
+ ST_WORD(tbl+BS_55AA, 0xAA55);
|
|
|
+ if (disk_write(pdrv, tbl, b_vol, 1))
|
|
|
+ return FR_DISK_ERR;
|
|
|
+ if (fmt == FS_FAT32)
|
|
|
+ disk_write(pdrv, tbl, b_vol + 6, 1);
|
|
|
+
|
|
|
+
|
|
|
+ wsect = b_fat;
|
|
|
+ for (i = 0; i < N_FATS; i++) {
|
|
|
+ mem_set(tbl, 0, SS(fs));
|
|
|
+ n = md;
|
|
|
+ if (fmt != FS_FAT32) {
|
|
|
+ n |= (fmt == FS_FAT12) ? 0x00FFFF00 : 0xFFFFFF00;
|
|
|
+ ST_DWORD(tbl+0, n);
|
|
|
+ } else {
|
|
|
+ n |= 0xFFFFFF00;
|
|
|
+ ST_DWORD(tbl+0, n);
|
|
|
+ ST_DWORD(tbl+4, 0xFFFFFFFF);
|
|
|
+ ST_DWORD(tbl+8, 0x0FFFFFFF);
|
|
|
+ }
|
|
|
+ if (disk_write(pdrv, tbl, wsect++, 1))
|
|
|
+ return FR_DISK_ERR;
|
|
|
+ mem_set(tbl, 0, SS(fs));
|
|
|
+ for (n = 1; n < n_fat; n++) {
|
|
|
+ if (disk_write(pdrv, tbl, wsect++, 1))
|
|
|
+ return FR_DISK_ERR;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ i = (fmt == FS_FAT32) ? au : (UINT)n_dir;
|
|
|
+ do {
|
|
|
+ if (disk_write(pdrv, tbl, wsect++, 1))
|
|
|
+ return FR_DISK_ERR;
|
|
|
+ } while (--i);
|
|
|
+
|
|
|
+#if _USE_ERASE
|
|
|
+ {
|
|
|
+ DWORD eb[2];
|
|
|
+
|
|
|
+ eb[0] = wsect; eb[1] = wsect + (n_clst - ((fmt == FS_FAT32) ? 1 : 0)) * au - 1;
|
|
|
+ disk_ioctl(pdrv, CTRL_ERASE_SECTOR, eb);
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+ if (fmt == FS_FAT32) {
|
|
|
+ ST_DWORD(tbl+FSI_LeadSig, 0x41615252);
|
|
|
+ ST_DWORD(tbl+FSI_StrucSig, 0x61417272);
|
|
|
+ ST_DWORD(tbl+FSI_Free_Count, n_clst - 1);
|
|
|
+ ST_DWORD(tbl+FSI_Nxt_Free, 2);
|
|
|
+ ST_WORD(tbl+BS_55AA, 0xAA55);
|
|
|
+ disk_write(pdrv, tbl, b_vol + 1, 1);
|
|
|
+ disk_write(pdrv, tbl, b_vol + 7, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ return (disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _MULTI_PARTITION
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+FRESULT f_fdisk (
|
|
|
+ BYTE pdrv,
|
|
|
+ const DWORD szt[],
|
|
|
+ void* work
|
|
|
+)
|
|
|
+{
|
|
|
+ UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl;
|
|
|
+ BYTE s_hd, e_hd, *p, *buf = (BYTE*)work;
|
|
|
+ DSTATUS stat;
|
|
|
+ DWORD sz_disk, sz_part, s_part;
|
|
|
+
|
|
|
+
|
|
|
+ stat = disk_initialize(pdrv);
|
|
|
+ if (stat & STA_NOINIT) return FR_NOT_READY;
|
|
|
+ if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
|
|
|
+ if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk)) return FR_DISK_ERR;
|
|
|
+
|
|
|
+
|
|
|
+ for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ;
|
|
|
+ if (n == 256) n--;
|
|
|
+ e_hd = n - 1;
|
|
|
+ sz_cyl = 63 * n;
|
|
|
+ tot_cyl = sz_disk / sz_cyl;
|
|
|
+
|
|
|
+
|
|
|
+ mem_set(buf, 0, _MAX_SS);
|
|
|
+ p = buf + MBR_Table; b_cyl = 0;
|
|
|
+ for (i = 0; i < 4; i++, p += SZ_PTE) {
|
|
|
+ p_cyl = (szt[i] <= 100U) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl;
|
|
|
+ if (!p_cyl) continue;
|
|
|
+ s_part = (DWORD)sz_cyl * b_cyl;
|
|
|
+ sz_part = (DWORD)sz_cyl * p_cyl;
|
|
|
+ if (i == 0) {
|
|
|
+ s_hd = 1;
|
|
|
+ s_part += 63; sz_part -= 63;
|
|
|
+ } else {
|
|
|
+ s_hd = 0;
|
|
|
+ }
|
|
|
+ e_cyl = b_cyl + p_cyl - 1;
|
|
|
+ if (e_cyl >= tot_cyl) return FR_INVALID_PARAMETER;
|
|
|
+
|
|
|
+
|
|
|
+ p[1] = s_hd;
|
|
|
+ p[2] = (BYTE)((b_cyl >> 2) + 1);
|
|
|
+ p[3] = (BYTE)b_cyl;
|
|
|
+ p[4] = 0x06;
|
|
|
+ p[5] = e_hd;
|
|
|
+ p[6] = (BYTE)((e_cyl >> 2) + 63);
|
|
|
+ p[7] = (BYTE)e_cyl;
|
|
|
+ ST_DWORD(p + 8, s_part);
|
|
|
+ ST_DWORD(p + 12, sz_part);
|
|
|
+
|
|
|
+
|
|
|
+ b_cyl += p_cyl;
|
|
|
+ }
|
|
|
+ ST_WORD(p, 0xAA55);
|
|
|
+
|
|
|
+
|
|
|
+ return (disk_write(pdrv, buf, 0, 1) || disk_ioctl(pdrv, CTRL_SYNC, 0)) ? FR_DISK_ERR : FR_OK;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if _USE_STRFUNC
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+TCHAR* f_gets (
|
|
|
+ TCHAR* buff,
|
|
|
+ int len,
|
|
|
+ FIL* fp
|
|
|
+)
|
|
|
+{
|
|
|
+ int n = 0;
|
|
|
+ TCHAR c, *p = buff;
|
|
|
+ BYTE s[2];
|
|
|
+ UINT rc;
|
|
|
+
|
|
|
+
|
|
|
+ while (n < len - 1) {
|
|
|
+#if _USE_LFN && _LFN_UNICODE
|
|
|
+#if _STRF_ENCODE == 3
|
|
|
+ f_read(fp, s, 1, &rc);
|
|
|
+ if (rc != 1) break;
|
|
|
+ c = s[0];
|
|
|
+ if (c >= 0x80) {
|
|
|
+ if (c < 0xC0) continue;
|
|
|
+ if (c < 0xE0) {
|
|
|
+ f_read(fp, s, 1, &rc);
|
|
|
+ if (rc != 1) break;
|
|
|
+ c = (c & 0x1F) << 6 | (s[0] & 0x3F);
|
|
|
+ if (c < 0x80) c = '?';
|
|
|
+ } else {
|
|
|
+ if (c < 0xF0) {
|
|
|
+ f_read(fp, s, 2, &rc);
|
|
|
+ if (rc != 2) break;
|
|
|
+ c = c << 12 | (s[0] & 0x3F) << 6 | (s[1] & 0x3F);
|
|
|
+ if (c < 0x800) c = '?';
|
|
|
+ } else {
|
|
|
+ c = '?';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+#elif _STRF_ENCODE == 2
|
|
|
+ f_read(fp, s, 2, &rc);
|
|
|
+ if (rc != 2) break;
|
|
|
+ c = s[1] + (s[0] << 8);
|
|
|
+#elif _STRF_ENCODE == 1
|
|
|
+ f_read(fp, s, 2, &rc);
|
|
|
+ if (rc != 2) break;
|
|
|
+ c = s[0] + (s[1] << 8);
|
|
|
+#else
|
|
|
+ f_read(fp, s, 1, &rc);
|
|
|
+ if (rc != 1) break;
|
|
|
+ c = s[0];
|
|
|
+ if (IsDBCS1(c)) {
|
|
|
+ f_read(fp, s, 1, &rc);
|
|
|
+ if (rc != 1) break;
|
|
|
+ c = (c << 8) + s[0];
|
|
|
+ }
|
|
|
+ c = ff_convert(c, 1);
|
|
|
+ if (!c) c = '?';
|
|
|
+#endif
|
|
|
+#else
|
|
|
+ f_read(fp, s, 1, &rc);
|
|
|
+ if (rc != 1) break;
|
|
|
+ c = s[0];
|
|
|
+#endif
|
|
|
+ if (_USE_STRFUNC == 2 && c == '\r') continue;
|
|
|
+ *p++ = c;
|
|
|
+ n++;
|
|
|
+ if (c == '\n') break;
|
|
|
+ }
|
|
|
+ *p = 0;
|
|
|
+ return n ? buff : 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if !_FS_READONLY
|
|
|
+#include <stdarg.h>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ FIL* fp;
|
|
|
+ int idx, nchr;
|
|
|
+ BYTE buf[64];
|
|
|
+} putbuff;
|
|
|
+
|
|
|
+
|
|
|
+static
|
|
|
+void putc_bfd (
|
|
|
+ putbuff* pb,
|
|
|
+ TCHAR c
|
|
|
+)
|
|
|
+{
|
|
|
+ UINT bw;
|
|
|
+ int i;
|
|
|
+
|
|
|
+
|
|
|
+ if (_USE_STRFUNC == 2 && c == '\n')
|
|
|
+ putc_bfd(pb, '\r');
|
|
|
+
|
|
|
+ i = pb->idx;
|
|
|
+ if (i < 0) return;
|
|
|
+
|
|
|
+#if _USE_LFN && _LFN_UNICODE
|
|
|
+#if _STRF_ENCODE == 3
|
|
|
+ if (c < 0x80) {
|
|
|
+ pb->buf[i++] = (BYTE)c;
|
|
|
+ } else {
|
|
|
+ if (c < 0x800) {
|
|
|
+ pb->buf[i++] = (BYTE)(0xC0 | c >> 6);
|
|
|
+ } else {
|
|
|
+ pb->buf[i++] = (BYTE)(0xE0 | c >> 12);
|
|
|
+ pb->buf[i++] = (BYTE)(0x80 | (c >> 6 & 0x3F));
|
|
|
+ }
|
|
|
+ pb->buf[i++] = (BYTE)(0x80 | (c & 0x3F));
|
|
|
+ }
|
|
|
+#elif _STRF_ENCODE == 2
|
|
|
+ pb->buf[i++] = (BYTE)(c >> 8);
|
|
|
+ pb->buf[i++] = (BYTE)c;
|
|
|
+#elif _STRF_ENCODE == 1
|
|
|
+ pb->buf[i++] = (BYTE)c;
|
|
|
+ pb->buf[i++] = (BYTE)(c >> 8);
|
|
|
+#else
|
|
|
+ c = ff_convert(c, 0);
|
|
|
+ if (!c) c = '?';
|
|
|
+ if (c >= 0x100)
|
|
|
+ pb->buf[i++] = (BYTE)(c >> 8);
|
|
|
+ pb->buf[i++] = (BYTE)c;
|
|
|
+#endif
|
|
|
+#else
|
|
|
+ pb->buf[i++] = (BYTE)c;
|
|
|
+#endif
|
|
|
+
|
|
|
+ if (i >= (int)(sizeof pb->buf) - 3) {
|
|
|
+ f_write(pb->fp, pb->buf, (UINT)i, &bw);
|
|
|
+ i = (bw == (UINT)i) ? 0 : -1;
|
|
|
+ }
|
|
|
+ pb->idx = i;
|
|
|
+ pb->nchr++;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+int f_putc (
|
|
|
+ TCHAR c,
|
|
|
+ FIL* fp
|
|
|
+)
|
|
|
+{
|
|
|
+ putbuff pb;
|
|
|
+ UINT nw;
|
|
|
+
|
|
|
+
|
|
|
+ pb.fp = fp;
|
|
|
+ pb.nchr = pb.idx = 0;
|
|
|
+
|
|
|
+ putc_bfd(&pb, c);
|
|
|
+
|
|
|
+ if ( pb.idx >= 0
|
|
|
+ && f_write(pb.fp, pb.buf, (UINT)pb.idx, &nw) == FR_OK
|
|
|
+ && (UINT)pb.idx == nw) return pb.nchr;
|
|
|
+ return EOF;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+int f_puts (
|
|
|
+ const TCHAR* str,
|
|
|
+ FIL* fp
|
|
|
+)
|
|
|
+{
|
|
|
+ putbuff pb;
|
|
|
+ UINT nw;
|
|
|
+
|
|
|
+
|
|
|
+ pb.fp = fp;
|
|
|
+ pb.nchr = pb.idx = 0;
|
|
|
+
|
|
|
+ while (*str)
|
|
|
+ putc_bfd(&pb, *str++);
|
|
|
+
|
|
|
+ if ( pb.idx >= 0
|
|
|
+ && f_write(pb.fp, pb.buf, (UINT)pb.idx, &nw) == FR_OK
|
|
|
+ && (UINT)pb.idx == nw) return pb.nchr;
|
|
|
+ return EOF;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+int f_printf (
|
|
|
+ FIL* fp,
|
|
|
+ const TCHAR* fmt,
|
|
|
+ ...
|
|
|
+)
|
|
|
+{
|
|
|
+ va_list arp;
|
|
|
+ BYTE f, r;
|
|
|
+ UINT nw, i, j, w;
|
|
|
+ DWORD v;
|
|
|
+ TCHAR c, d, s[16], *p;
|
|
|
+ putbuff pb;
|
|
|
+
|
|
|
+
|
|
|
+ pb.fp = fp;
|
|
|
+ pb.nchr = pb.idx = 0;
|
|
|
+
|
|
|
+ va_start(arp, fmt);
|
|
|
+
|
|
|
+ for (;;) {
|
|
|
+ c = *fmt++;
|
|
|
+ if (c == 0) break;
|
|
|
+ if (c != '%') {
|
|
|
+ putc_bfd(&pb, c);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ w = f = 0;
|
|
|
+ c = *fmt++;
|
|
|
+ if (c == '0') {
|
|
|
+ f = 1; c = *fmt++;
|
|
|
+ } else {
|
|
|
+ if (c == '-') {
|
|
|
+ f = 2; c = *fmt++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ while (IsDigit(c)) {
|
|
|
+ w = w * 10 + c - '0';
|
|
|
+ c = *fmt++;
|
|
|
+ }
|
|
|
+ if (c == 'l' || c == 'L') {
|
|
|
+ f |= 4; c = *fmt++;
|
|
|
+ }
|
|
|
+ if (!c) break;
|
|
|
+ d = c;
|
|
|
+ if (IsLower(d)) d -= 0x20;
|
|
|
+ switch (d) {
|
|
|
+ case 'S' :
|
|
|
+ p = va_arg(arp, TCHAR*);
|
|
|
+ for (j = 0; p[j]; j++) ;
|
|
|
+ if (!(f & 2)) {
|
|
|
+ while (j++ < w) putc_bfd(&pb, ' ');
|
|
|
+ }
|
|
|
+ while (*p) putc_bfd(&pb, *p++);
|
|
|
+ while (j++ < w) putc_bfd(&pb, ' ');
|
|
|
+ continue;
|
|
|
+ case 'C' :
|
|
|
+ putc_bfd(&pb, (TCHAR)va_arg(arp, int)); continue;
|
|
|
+ case 'B' :
|
|
|
+ r = 2; break;
|
|
|
+ case 'O' :
|
|
|
+ r = 8; break;
|
|
|
+ case 'D' :
|
|
|
+ case 'U' :
|
|
|
+ r = 10; break;
|
|
|
+ case 'X' :
|
|
|
+ r = 16; break;
|
|
|
+ default:
|
|
|
+ putc_bfd(&pb, c); continue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ v = (f & 4) ? (DWORD)va_arg(arp, long) : ((d == 'D') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int));
|
|
|
+ if (d == 'D' && (v & 0x80000000)) {
|
|
|
+ v = 0 - v;
|
|
|
+ f |= 8;
|
|
|
+ }
|
|
|
+ i = 0;
|
|
|
+ do {
|
|
|
+ d = (TCHAR)(v % r); v /= r;
|
|
|
+ if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
|
|
|
+ s[i++] = d + '0';
|
|
|
+ } while (v && i < sizeof s / sizeof s[0]);
|
|
|
+ if (f & 8) s[i++] = '-';
|
|
|
+ j = i; d = (f & 1) ? '0' : ' ';
|
|
|
+ while (!(f & 2) && j++ < w) putc_bfd(&pb, d);
|
|
|
+ do putc_bfd(&pb, s[--i]); while (i);
|
|
|
+ while (j++ < w) putc_bfd(&pb, d);
|
|
|
+ }
|
|
|
+
|
|
|
+ va_end(arp);
|
|
|
+
|
|
|
+ if ( pb.idx >= 0
|
|
|
+ && f_write(pb.fp, pb.buf, (UINT)pb.idx, &nw) == FR_OK
|
|
|
+ && (UINT)pb.idx == nw) return pb.nchr;
|
|
|
+ return EOF;
|
|
|
+}
|
|
|
+
|
|
|
+#endif
|
|
|
+#endif
|