/*----------------------------------------------------------------------------/
/ FatFs - FAT file system module R0.07b (C)ChaN, 2009
/-----------------------------------------------------------------------------/
/ FatFs module is an open source software to implement FAT file system to
/ small embedded systems. This is a free software and is opened for education,
/ research and commercial developments under license policy of following trems.
/
/ Copyright (C) 2009, 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 use 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) patition.
/ 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 algolithm 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 plysical 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 17,'09 R0.07b Fixed f_unlink() may return FR_OK on error.
/ Fixed wrong cache control in f_lseek().
/ Added relative path feature.
/ Added f_chdir() and f_chdrive().
/---------------------------------------------------------------------------*/
#include "ff.h" /* FatFs configurations and declarations */
#include "diskio.h" /* Declarations of low level disk I/O functions */
/*--------------------------------------------------------------------------
Module Private Definitions
---------------------------------------------------------------------------*/
#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); }
#ifndef NULL
#define NULL 0
#endif
/* Name status flags */
#define NS_LOSSY 0x01 /* Lossy conversion */
#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 */
/*--------------------------------------------------------------------------
Private Work Area
---------------------------------------------------------------------------*/
static
FATFS *FatFs[_DRIVES]; /* Pointer to the file system objects (logical drives) */
static
WORD Fsid; /* File system mount ID */
#if _FS_RPATH
static
BYTE Drive; /* Current drive */
#endif
#if _USE_LFN == 1 /* LFN with static LFN working buffer */
static
WORD LfnBuf[_MAX_LFN + 1];
#define NAMEBUF(sp,lp) BYTE sp[12]; WCHAR *lp = LfnBuf
#define INITBUF(dj,sp,lp) dj.fn = sp; dj.lfn = lp
#elif _USE_LFN > 1 /* LFN with dynamic LFN working buffer */
#define NAMEBUF(sp,lp) BYTE sp[12]; WCHAR lbuf[_MAX_LFN + 1], *lp = lbuf
#define INITBUF(dj,sp,lp) dj.fn = sp; dj.lfn = lp
#else /* No LFN */
#define NAMEBUF(sp,lp) BYTE sp[12]
#define INITBUF(dj,sp,lp) dj.fn = sp
#endif
/*--------------------------------------------------------------------------
Private Functions
---------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
/* String functions */
/*-----------------------------------------------------------------------*/
/* Copy memory to memory */
static
void mem_cpy (void* dst, const void* src, int cnt) {
char *d = (char*)dst;
const char *s = (const char *)src;
while (cnt--) *d++ = *s++;
}
/* Fill memory */
static
void mem_set (void* dst, int val, int cnt) {
char *d = (char*)dst;
while (cnt--) *d++ = (char)val;
}
/* Compare memory to memory */
static
int mem_cmp (const void* dst, const void* src, int cnt) {
const char *d = (const char *)dst, *s = (const char *)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
BOOL 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
/*-----------------------------------------------------------------------*/
/* Change window offset */
/*-----------------------------------------------------------------------*/
static
FRESULT move_window (
FATFS *fs, /* File system object */
DWORD sector /* Sector number to make apperance in the fs->win[] */
)