📄 ff.c
字号:
/*----------------------------------------------------------------------------/
/ FatFs - FAT file system module R0.08 (C)ChaN, 2010
/-----------------------------------------------------------------------------/
/ 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) 2010, 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 buffer configuration option.
/ Added long file name support.
/ Added multiple code page support.
/ 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 support.
/ 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 char.
/ Nov 03,'09 R0.07e Separated out configuration options from ff.h to ffconf.h.
/ Fixed f_unlink() fails to remove a sub-dir on _FS_RPATH.
/ Fixed name matching error on the 13 char 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)
/ Added file lock feature. (_FS_SHARE)
/ Added fast seek feature. (_USE_FASTSEEK)
/ Changed some types on the API, XCHAR->TCHAR.
/ Changed fname member in the FILINFO structure on Unicode cfg.
/ String functions support UTF-8 encoding files on Unicode cfg.
/---------------------------------------------------------------------------*/
#include "ff.h" /* FatFs configurations and declarations */
#include "diskio.h" /* Declarations of low level disk I/O functions */
/*--------------------------------------------------------------------------
Module Private Definitions
---------------------------------------------------------------------------*/
#if _FATFS != 8085
#error Wrong include file (ff.h).
#endif
/* FAT sub-type boundaries */
/* Note that the FAT spec by Microsoft says 4085 but Windows works with 4087! */
#define MIN_FAT16 4086 /* Minimum number of clusters for FAT16 */
#define MIN_FAT32 65526 /* Minimum number of clusters for FAT32 */
/* Definitions corresponds to multiple sector size */
#if _MAX_SS == 512 /* Single sector size */
#define SS(fs) 512U
#elif _MAX_SS == 1024 || _MAX_SS == 2048 || _MAX_SS == 4096 /* Multiple sector size */
#define SS(fs) ((fs)->ssize)
#else
#error Wrong sector size.
#endif
/* Reentrancy related */
#if _FS_REENTRANT
#if _USE_LFN == 1
#error Static LFN work area must not be used in re-entrant 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->flag |= FA__ERROR; LEAVE_FF(fs, res); }
/* Character code support macros */
#define IsUpper(c) (((c)>='A')&&((c)<='Z'))
#define IsLower(c) (((c)>='a')&&((c)<='z'))
#define IsDigit(c) (((c)>='0')&&((c)<='9'))
#if _DF1S /* Code page is DBCS */
#ifdef _DF2S /* Two 1st byte areas */
#define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
#else /* One 1st byte area */
#define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
#endif
#ifdef _DS3S /* Three 2nd byte areas */
#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
#else /* Two 2nd byte areas */
#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
#endif
#else /* Code page is SBCS */
#define IsDBCS1(c) 0
#define IsDBCS2(c) 0
#endif /* _DF1S */
/* Name status flags */
#define NS 11 /* Offset of name status byte */
#define NS_LOSS 0x01 /* Out of 8.3 format */
#define NS_LFN 0x02 /* Force to create LFN entry */
#define NS_LAST 0x04 /* Last segment */
#define NS_BODY 0x08 /* Lower case flag (body) */
#define NS_EXT 0x10 /* Lower case flag (ext) */
#define NS_DOT 0x20 /* Dot entry */
/*------------------------------------------------------------*/
/* Work area */
#if !_DRIVES
#error Number of drives must not be 0.
#endif
static
WORD Fsid; /* File system mount ID */
static
FATFS *FatFs[_DRIVES]; /* Pointer to the file system objects (logical drives) */
#if _FS_RPATH
static
BYTE Drive; /* Current drive */
#endif
#if _USE_LFN == 0 /* No LFN */
#define DEF_NAMEBUF BYTE sfn[12]
#define INIT_BUF(dobj) (dobj).fn = sfn
#define FREE_BUF()
#elif _USE_LFN == 1 /* LFN with static LFN working buffer */
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 /* LFN with dynamic LFN working buffer on the stack */
#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 /* LFN with dynamic LFN working buffer on the heap */
#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
/*--------------------------------------------------------------------------
Module Private Functions
---------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
/* String functions */
/*-----------------------------------------------------------------------*/
/* Copy memory to memory */
static
void mem_cpy (void* dst, const void* src, int 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++;
}
/* Fill memory */
static
void mem_set (void* dst, int val, int cnt) {
BYTE *d = (BYTE*)dst;
while (cnt--)
*d++ = (BYTE)val;
}
/* Compare memory to memory */
static
int mem_cmp (const void* dst, const void* src, int cnt) {
const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
int r = 0;
while (cnt-- && (r = *d++ - *s++) == 0) ;
return r;
}
/* Check if chr is contained in the string */
static
int chk_chr (const char* str, int chr) {
while (*str && *str != chr) str++;
return *str;
}
/*-----------------------------------------------------------------------*/
/* Request/Release grant to access the volume */
/*-----------------------------------------------------------------------*/
#if _FS_REENTRANT
static
int lock_fs (
FATFS *fs /* File system object */
)
{
return ff_req_grant(fs->sobj);
}
static
void unlock_fs (
FATFS *fs, /* File system object */
FRESULT res /* Result code to be returned */
)
{
if (res != FR_NOT_ENABLED &&
res != FR_INVALID_DRIVE &&
res != FR_INVALID_OBJECT &&
res != FR_TIMEOUT) {
ff_rel_grant(fs->sobj);
}
}
#endif
/*-----------------------------------------------------------------------*/
/* File shareing control functions */
/*-----------------------------------------------------------------------*/
#if _FS_SHARE
static
FRESULT chk_lock ( /* Check if the file can be accessed */
DIR* dj, /* Directory object pointing the file to be checked */
int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */
)
{
UINT i, be;
/* Search file semaphore table */
for (i = be = 0; i < _FS_SHARE; i++) {
if (dj->fs->flsem[i].ctr) { /* Existing entry */
if (dj->fs->flsem[i].clu == dj->sclust && /* The file is found (identified with its location) */
dj->fs->flsem[i].idx == dj->index) break;
} else { /* Blank entry */
be++;
}
}
if (i == _FS_SHARE) /* The file has not been opened */
return (be || acc != 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES; /* Is there a blank entry for new file? */
/* The file has been opened. Reject any open against writing file and all write mode open */
return (acc || dj->fs->flsem[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
}
static
int enq_lock ( /* Check if an entry is available for a new file */
FATFS* fs /* File system object */
)
{
UINT i;
for (i = 0; i < _FS_SHARE && fs->flsem[i].ctr; i++) ;
return (i == _FS_SHARE) ? 0 : 1;
}
static
UINT inc_lock ( /* Increment file open counter and returns its index (0:int error) */
DIR* dj, /* Directory object pointing the file to register or increment */
int acc /* Desired access mode (0:Read, !0:Write) */
)
{
UINT i;
for (i = 0; i < _FS_SHARE; i++) { /* Find the file */
if (dj->fs->flsem[i].ctr &&
dj->fs->flsem[i].clu == dj->sclust &&
dj->fs->flsem[i].idx == dj->index) break;
}
if (i == _FS_SHARE) { /* Not opened. Register it as new. */
for (i = 0; i < _FS_SHARE && dj->fs->flsem[i].ctr; i++) ;
if (i == _FS_SHARE) return 0; /* No space to register (int err) */
dj->fs->flsem[i].clu = dj->sclust;
dj->fs->flsem[i].idx = dj->index;
}
if (acc && dj->fs->flsem[i].ctr) return 0; /* Access violation (int err) */
dj->fs->flsem[i].ctr = acc ? 0x100 : dj->fs->flsem[i].ctr + 1; /* Set semaphore value */
return i + 1;
}
static
FRESULT dec_lock ( /* Decrement file open counter */
FATFS* fs, /* File system object */
UINT i /* Semaphore index */
)
{
WORD n;
FRESULT res;
if (--i < _FS_SHARE) {
n = fs->flsem[i].ctr;
if (n >= 0x100) n = 0;
if (n) n--;
fs->flsem[i].ctr = n;
res = FR_OK;
} else {
res = FR_INT_ERR;
}
return res;
}
#endif
/*-----------------------------------------------------------------------*/
/* Change window offset */
/*-----------------------------------------------------------------------*/
static
FRESULT move_window (
FATFS *fs, /* File system object */
DWORD sector /* Sector number to make appearance in the fs->win[] */
) /* Move to zero only writes back dirty window */
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -