summaryrefslogtreecommitdiff
path: root/fatfs/source/ff.c
diff options
context:
space:
mode:
Diffstat (limited to 'fatfs/source/ff.c')
-rw-r--r--fatfs/source/ff.c2696
1 files changed, 1623 insertions, 1073 deletions
diff --git a/fatfs/source/ff.c b/fatfs/source/ff.c
index c57a320..07412bb 100644
--- a/fatfs/source/ff.c
+++ b/fatfs/source/ff.c
@@ -1,8 +1,8 @@
/*----------------------------------------------------------------------------/
-/ FatFs - Generic FAT Filesystem Module R0.13b /
+/ FatFs - Generic FAT Filesystem Module R0.15 w/patch1 /
/-----------------------------------------------------------------------------/
/
-/ Copyright (C) 2018, ChaN, all right reserved.
+/ Copyright (C) 2022, ChaN, all right reserved.
/
/ FatFs module is an open source software. Redistribution and use of FatFs in
/ source and binary forms, with or without modification, are permitted provided
@@ -19,6 +19,7 @@
/----------------------------------------------------------------------------*/
+#include <string.h>
#include "ff.h" /* Declarations of FatFs API */
#include "diskio.h" /* Declarations of device I/O functions */
@@ -29,32 +30,44 @@
---------------------------------------------------------------------------*/
-#if FF_DEFINED != 63463 /* Revision ID */
+#if FF_DEFINED != 80286 /* Revision ID */
#error Wrong include file (ff.h).
#endif
+/* Limits and boundaries */
+#define MAX_DIR 0x200000 /* Max size of FAT directory */
+#define MAX_DIR_EX 0x10000000 /* Max size of exFAT directory */
+#define MAX_FAT12 0xFF5 /* Max FAT12 clusters (differs from specs, but right for real DOS/Windows behavior) */
+#define MAX_FAT16 0xFFF5 /* Max FAT16 clusters (differs from specs, but right for real DOS/Windows behavior) */
+#define MAX_FAT32 0x0FFFFFF5 /* Max FAT32 clusters (not specified, practical limit) */
+#define MAX_EXFAT 0x7FFFFFFD /* Max exFAT clusters (differs from specs, implementation limit) */
+
+
/* 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')
+#define IsSeparator(c) ((c) == '/' || (c) == '\\')
+#define IsTerminator(c) ((UINT)(c) < (FF_USE_LFN ? ' ' : '!'))
#define IsSurrogate(c) ((c) >= 0xD800 && (c) <= 0xDFFF)
#define IsSurrogateH(c) ((c) >= 0xD800 && (c) <= 0xDBFF)
#define IsSurrogateL(c) ((c) >= 0xDC00 && (c) <= 0xDFFF)
-/* Additional file attribute bits for internal use */
-#define AM_VOL 0x08 /* Volume label */
-#define AM_LFN 0x0F /* LFN entry */
-#define AM_MASK 0x3F /* Mask of defined bits */
-
-
/* Additional file access control and file status flags for internal use */
#define FA_SEEKEND 0x20 /* Seek to end of the file on file open */
#define FA_MODIFIED 0x40 /* File has been modified */
#define FA_DIRTY 0x80 /* FIL.buf[] needs to be written-back */
+/* Additional file attribute bits for internal use */
+#define AM_VOL 0x08 /* Volume label */
+#define AM_LFN 0x0F /* LFN entry */
+#define AM_MASK 0x3F /* Mask of defined bits in FAT */
+#define AM_MASKX 0x37 /* Mask of defined bits in exFAT */
+
+
/* Name status flags in fn[11] */
#define NSFLAG 11 /* Index of the name status byte */
#define NS_LOSS 0x01 /* Out of 8.3 format */
@@ -67,13 +80,13 @@
#define NS_NONAME 0x80 /* Not followed */
-/* Limits and boundaries */
-#define MAX_DIR 0x200000 /* Max size of FAT directory */
-#define MAX_DIR_EX 0x10000000 /* Max size of exFAT directory */
-#define MAX_FAT12 0xFF5 /* Max FAT12 clusters (differs from specs, but right for real DOS/Windows behavior) */
-#define MAX_FAT16 0xFFF5 /* Max FAT16 clusters (differs from specs, but right for real DOS/Windows behavior) */
-#define MAX_FAT32 0x0FFFFFF5 /* Max FAT32 clusters (not specified, practical limit) */
-#define MAX_EXFAT 0x7FFFFFFD /* Max exFAT clusters (differs from specs, implementation limit) */
+/* exFAT directory entry types */
+#define ET_BITMAP 0x81 /* Allocation bitmap */
+#define ET_UPCASE 0x82 /* Up-case table */
+#define ET_VLABEL 0x83 /* Volume label */
+#define ET_FILEDIR 0x85 /* File and directory */
+#define ET_STREAM 0xC0 /* Stream extension */
+#define ET_FILENAME 0xC1 /* Name extension */
/* FatFs refers the FAT structure as simple byte array instead of structure member
@@ -195,6 +208,27 @@
#define PTE_StLba 8 /* MBR PTE: Start in LBA */
#define PTE_SizLba 12 /* MBR PTE: Size in LBA */
+#define GPTH_Sign 0 /* GPT HDR: Signature (8-byte) */
+#define GPTH_Rev 8 /* GPT HDR: Revision (DWORD) */
+#define GPTH_Size 12 /* GPT HDR: Header size (DWORD) */
+#define GPTH_Bcc 16 /* GPT HDR: Header BCC (DWORD) */
+#define GPTH_CurLba 24 /* GPT HDR: This header LBA (QWORD) */
+#define GPTH_BakLba 32 /* GPT HDR: Another header LBA (QWORD) */
+#define GPTH_FstLba 40 /* GPT HDR: First LBA for partition data (QWORD) */
+#define GPTH_LstLba 48 /* GPT HDR: Last LBA for partition data (QWORD) */
+#define GPTH_DskGuid 56 /* GPT HDR: Disk GUID (16-byte) */
+#define GPTH_PtOfs 72 /* GPT HDR: Partition table LBA (QWORD) */
+#define GPTH_PtNum 80 /* GPT HDR: Number of table entries (DWORD) */
+#define GPTH_PteSize 84 /* GPT HDR: Size of table entry (DWORD) */
+#define GPTH_PtBcc 88 /* GPT HDR: Partition table BCC (DWORD) */
+#define SZ_GPTE 128 /* GPT PTE: Size of partition table entry */
+#define GPTE_PtGuid 0 /* GPT PTE: Partition type GUID (16-byte) */
+#define GPTE_UpGuid 16 /* GPT PTE: Partition unique GUID (16-byte) */
+#define GPTE_FstLba 32 /* GPT PTE: First LBA of partition (QWORD) */
+#define GPTE_LstLba 40 /* GPT PTE: Last LBA of partition (QWORD) */
+#define GPTE_Flags 48 /* GPT PTE: Partition flags (QWORD) */
+#define GPTE_Name 56 /* GPT PTE: Partition name */
+
/* Post process on fatal error in the file operations */
#define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); }
@@ -203,21 +237,21 @@
/* Re-entrancy related */
#if FF_FS_REENTRANT
#if FF_USE_LFN == 1
-#error Static LFN work area cannot be used at thread-safe configuration
+#error Static LFN work area cannot be used in thread-safe configuration
#endif
-#define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }
+#define LEAVE_FF(fs, res) { unlock_volume(fs, res); return res; }
#else
#define LEAVE_FF(fs, res) return res
#endif
-/* Definitions of volume - physical location conversion */
+/* Definitions of logical drive - physical location conversion */
#if FF_MULTI_PARTITION
#define LD2PD(vol) VolToPart[vol].pd /* Get physical drive number */
-#define LD2PT(vol) VolToPart[vol].pt /* Get partition index */
+#define LD2PT(vol) VolToPart[vol].pt /* Get partition number (0:auto search, 1..:forced partition number) */
#else
-#define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */
-#define LD2PT(vol) 0 /* Find first valid partition or in SFD */
+#define LD2PD(vol) (BYTE)(vol) /* Each logical drive is associated with the same physical drive number */
+#define LD2PT(vol) 0 /* Auto partition search */
#endif
@@ -244,15 +278,15 @@
/* File lock controls */
-#if FF_FS_LOCK != 0
+#if FF_FS_LOCK
#if FF_FS_READONLY
#error FF_FS_LOCK must be 0 at read-only configuration
#endif
typedef struct {
- FATFS *fs; /* Object ID 1, volume (NULL:blank entry) */
+ FATFS* fs; /* Object ID 1, volume (NULL:blank entry) */
DWORD clu; /* Object ID 2, containing directory (0:root) */
DWORD ofs; /* Object ID 3, offset in the directory */
- WORD ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */
+ UINT ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */
} FILESEM;
#endif
@@ -397,6 +431,7 @@ typedef struct {
/* DBCS code range |----- 1st byte -----| |----------- 2nd byte -----------| */
+/* <------> <------> <------> <------> <------> */
#define TBL_DC932 {0x81, 0x9F, 0xE0, 0xFC, 0x40, 0x7E, 0x80, 0xFC, 0x00, 0x00}
#define TBL_DC936 {0x81, 0xFE, 0x00, 0x00, 0x40, 0x7E, 0x80, 0xFE, 0x00, 0x00}
#define TBL_DC949 {0x81, 0xFE, 0x00, 0x00, 0x41, 0x5A, 0x61, 0x7A, 0x81, 0xFE}
@@ -426,24 +461,35 @@ typedef struct {
#if FF_VOLUMES < 1 || FF_VOLUMES > 10
#error Wrong FF_VOLUMES setting
#endif
-static FATFS* FatFs[FF_VOLUMES]; /* Pointer to the filesystem objects (logical drives) */
+static FATFS *FatFs[FF_VOLUMES]; /* Pointer to the filesystem objects (logical drives) */
static WORD Fsid; /* Filesystem mount ID */
#if FF_FS_RPATH != 0
-static BYTE CurrVol; /* Current drive */
+static BYTE CurrVol; /* Current drive set by f_chdrive() */
#endif
#if FF_FS_LOCK != 0
static FILESEM Files[FF_FS_LOCK]; /* Open object lock semaphores */
+#if FF_FS_REENTRANT
+static BYTE SysLock; /* System lock flag (0:no mutex, 1:unlocked, 2:locked) */
+#endif
#endif
#if FF_STR_VOLUME_ID
#ifdef FF_VOLUME_STRS
-static const char* const VolumeStr[FF_VOLUMES] = {FF_VOLUME_STRS}; /* Pre-defined volume ID */
+static const char *const VolumeStr[FF_VOLUMES] = {FF_VOLUME_STRS}; /* Pre-defined volume ID */
+#endif
+#endif
+
+#if FF_LBA64
+#if FF_MIN_GPT > 0x100000000
+#error Wrong FF_MIN_GPT setting
#endif
+static const BYTE GUID_MS_Basic[16] = {0xA2,0xA0,0xD0,0xEB,0xE5,0xB9,0x33,0x44,0x87,0xC0,0x68,0xB6,0xB7,0x26,0x99,0xC7};
#endif
+
/*--------------------------------*/
/* LFN/Directory working buffer */
/*--------------------------------*/
@@ -517,10 +563,12 @@ static WCHAR LfnBuf[FF_MAX_LFN + 1]; /* LFN working buffer */
/* Code conversion tables */
/*--------------------------------*/
-#if FF_CODE_PAGE == 0 /* Run-time code page configuration */
+#if FF_CODE_PAGE == 0 /* Run-time code page configuration */
#define CODEPAGE CodePage
static WORD CodePage; /* Current code page */
-static const BYTE *ExCvt, *DbcTbl; /* Pointer to current SBCS up-case table and DBCS code range table below */
+static const BYTE* ExCvt; /* Ptr to SBCS up-case table Ct???[] (null:not used) */
+static const BYTE* DbcTbl; /* Ptr to DBCS code range table Dc???[] (null:not used) */
+
static const BYTE Ct437[] = TBL_CT437;
static const BYTE Ct720[] = TBL_CT720;
static const BYTE Ct737[] = TBL_CT737;
@@ -640,54 +688,7 @@ static void st_qword (BYTE* ptr, QWORD val) /* Store an 8-byte word in little-en
/* String functions */
/*-----------------------------------------------------------------------*/
-/* Copy memory to memory */
-static void mem_cpy (void* dst, const void* src, UINT cnt)
-{
- BYTE *d = (BYTE*)dst;
- const BYTE *s = (const BYTE*)src;
-
- if (cnt != 0) {
- do {
- *d++ = *s++;
- } while (--cnt);
- }
-}
-
-
-/* Fill memory block */
-static void mem_set (void* dst, int val, UINT cnt)
-{
- BYTE *d = (BYTE*)dst;
-
- do {
- *d++ = (BYTE)val;
- } while (--cnt);
-}
-
-
-/* Compare memory block */
-static int mem_cmp (const void* dst, const void* src, UINT cnt) /* ZR:same, NZ:different */
-{
- const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
- int r = 0;
-
- do {
- r = *d++ - *s++;
- } while (--cnt && r == 0);
-
- return r;
-}
-
-
-/* Check if chr is contained in the string */
-static int chk_chr (const char* str, int chr) /* NZ:contained, ZR:not contained */
-{
- while (*str && *str != chr) str++;
- return *str;
-}
-
-
-/* Test if the character is DBC 1st byte */
+/* Test if the byte is DBC 1st byte */
static int dbc_1st (BYTE c)
{
#if FF_CODE_PAGE == 0 /* Variable code page */
@@ -707,7 +708,7 @@ static int dbc_1st (BYTE c)
}
-/* Test if the character is DBC 2nd byte */
+/* Test if the byte is DBC 2nd byte */
static int dbc_2nd (BYTE c)
{
#if FF_CODE_PAGE == 0 /* Variable code page */
@@ -731,8 +732,8 @@ static int dbc_2nd (BYTE c)
#if FF_USE_LFN
-/* Get a character from TCHAR string in defined API encodeing */
-static DWORD tchar2uni ( /* Returns character in UTF-16 encoding (>=0x10000 on double encoding unit, 0xFFFFFFFF on decode error) */
+/* Get a Unicode code point from the TCHAR string in defined API encodeing */
+static DWORD tchar2uni ( /* Returns a character in UTF-16 encoding (>=0x10000 on surrogate pair, 0xFFFFFFFF on decode error) */
const TCHAR** str /* Pointer to pointer to TCHAR string in configured encoding */
)
{
@@ -753,20 +754,16 @@ static DWORD tchar2uni ( /* Returns character in UTF-16 encoding (>=0x10000 on d
BYTE b;
int nf;
- uc = (BYTE)*p++; /* Get a unit */
+ uc = (BYTE)*p++; /* Get an encoding unit */
if (uc & 0x80) { /* Multiple byte code? */
- if ((uc & 0xE0) == 0xC0) { /* 2-byte sequence? */
+ if ((uc & 0xE0) == 0xC0) { /* 2-byte sequence? */
uc &= 0x1F; nf = 1;
- } else {
- if ((uc & 0xF0) == 0xE0) { /* 3-byte sequence? */
- uc &= 0x0F; nf = 2;
- } else {
- if ((uc & 0xF8) == 0xF0) { /* 4-byte sequence? */
- uc &= 0x07; nf = 3;
- } else { /* Wrong sequence */
- return 0xFFFFFFFF;
- }
- }
+ } else if ((uc & 0xF0) == 0xE0) { /* 3-byte sequence? */
+ uc &= 0x0F; nf = 2;
+ } else if ((uc & 0xF8) == 0xF0) { /* 4-byte sequence? */
+ uc &= 0x07; nf = 3;
+ } else { /* Wrong sequence */
+ return 0xFFFFFFFF;
}
do { /* Get trailing bytes */
b = (BYTE)*p++;
@@ -779,7 +776,7 @@ static DWORD tchar2uni ( /* Returns character in UTF-16 encoding (>=0x10000 on d
#elif FF_LFN_UNICODE == 3 /* UTF-32 input */
uc = (TCHAR)*p++; /* Get a unit */
- if (uc >= 0x110000) return 0xFFFFFFFF; /* Wrong code? */
+ if (uc >= 0x110000 || IsSurrogate(uc)) return 0xFFFFFFFF; /* Wrong code? */
if (uc >= 0x010000) uc = 0xD800DC00 | ((uc - 0x10000) << 6 & 0x3FF0000) | (uc & 0x3FF); /* Make a surrogate pair if needed */
#else /* ANSI/OEM input */
@@ -804,9 +801,9 @@ static DWORD tchar2uni ( /* Returns character in UTF-16 encoding (>=0x10000 on d
}
-/* Output a TCHAR string in defined API encoding */
-static BYTE put_utf ( /* Returns number of encoding units written (0:buffer overflow or wrong encoding) */
- DWORD chr, /* UTF-16 encoded character (Double encoding unit char if >=0x10000) */
+/* Store a Unicode char in defined API encoding */
+static UINT put_utf ( /* Returns number of encoding units written (0:buffer overflow or wrong encoding) */
+ DWORD chr, /* UTF-16 encoded character (Surrogate pair if >=0x10000) */
TCHAR* buf, /* Output buffer */
UINT szb /* Size of the buffer */
)
@@ -894,21 +891,45 @@ static BYTE put_utf ( /* Returns number of encoding units written (0:buffer over
/*-----------------------------------------------------------------------*/
/* Request/Release grant to access the volume */
/*-----------------------------------------------------------------------*/
-static int lock_fs ( /* 1:Ok, 0:timeout */
- FATFS* fs /* Filesystem object */
+
+static int lock_volume ( /* 1:Ok, 0:timeout */
+ FATFS* fs, /* Filesystem object to lock */
+ int syslock /* System lock required */
)
{
- return ff_req_grant(fs->sobj);
+ int rv;
+
+
+#if FF_FS_LOCK
+ rv = ff_mutex_take(fs->ldrv); /* Lock the volume */
+ if (rv && syslock) { /* System lock reqiered? */
+ rv = ff_mutex_take(FF_VOLUMES); /* Lock the system */
+ if (rv) {
+ SysLock = 2; /* System lock succeeded */
+ } else {
+ ff_mutex_give(fs->ldrv); /* Failed system lock */
+ }
+ }
+#else
+ rv = syslock ? ff_mutex_take(fs->ldrv) : ff_mutex_take(fs->ldrv); /* Lock the volume (this is to prevent compiler warning) */
+#endif
+ return rv;
}
-static void unlock_fs (
+static void unlock_volume (
FATFS* fs, /* Filesystem object */
FRESULT res /* Result code to be returned */
)
{
if (fs && res != FR_NOT_ENABLED && res != FR_INVALID_DRIVE && res != FR_TIMEOUT) {
- ff_rel_grant(fs->sobj);
+#if FF_FS_LOCK
+ if (SysLock == 2) { /* Is the system locked? */
+ SysLock = 1;
+ ff_mutex_give(FF_VOLUMES);
+ }
+#endif
+ ff_mutex_give(fs->ldrv); /* Unlock the volume */
}
}
@@ -916,12 +937,12 @@ static void unlock_fs (
-#if FF_FS_LOCK != 0
+#if FF_FS_LOCK
/*-----------------------------------------------------------------------*/
-/* File lock control functions */
+/* File shareing control functions */
/*-----------------------------------------------------------------------*/
-static FRESULT chk_lock ( /* Check if the file can be accessed */
+static FRESULT chk_share ( /* Check if the file can be accessed */
DIR* dp, /* Directory object pointing the file to be checked */
int acc /* Desired access type (0:Read mode open, 1:Write mode open, 2:Delete or rename) */
)
@@ -948,16 +969,16 @@ static FRESULT chk_lock ( /* Check if the file can be accessed */
}
-static int enq_lock (void) /* Check if an entry is available for a new object */
+static int enq_share (void) /* Check if an entry is available for a new object */
{
UINT i;
- for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ;
+ for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ; /* Find a free entry */
return (i == FF_FS_LOCK) ? 0 : 1;
}
-static UINT inc_lock ( /* Increment object open counter and returns its index (0:Internal error) */
+static UINT inc_share ( /* Increment object open counter and returns its index (0:Internal error) */
DIR* dp, /* Directory object pointing the file to register or increment */
int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */
)
@@ -966,13 +987,13 @@ static UINT inc_lock ( /* Increment object open counter and returns its index (0
for (i = 0; i < FF_FS_LOCK; i++) { /* Find the object */
- if (Files[i].fs == dp->obj.fs &&
- Files[i].clu == dp->obj.sclust &&
- Files[i].ofs == dp->dptr) break;
+ if (Files[i].fs == dp->obj.fs
+ && Files[i].clu == dp->obj.sclust
+ && Files[i].ofs == dp->dptr) break;
}
- if (i == FF_FS_LOCK) { /* Not opened. Register it as new. */
- for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ;
+ if (i == FF_FS_LOCK) { /* Not opened. Register it as new. */
+ for (i = 0; i < FF_FS_LOCK && Files[i].fs; i++) ; /* Find a free entry */
if (i == FF_FS_LOCK) return 0; /* No free entry to register (int err) */
Files[i].fs = dp->obj.fs;
Files[i].clu = dp->obj.sclust;
@@ -988,30 +1009,32 @@ static UINT inc_lock ( /* Increment object open counter and returns its index (0
}
-static FRESULT dec_lock ( /* Decrement object open counter */
+static FRESULT dec_share ( /* Decrement object open counter */
UINT i /* Semaphore index (1..) */
)
{
- WORD n;
+ UINT n;
FRESULT res;
if (--i < FF_FS_LOCK) { /* Index number origin from 0 */
n = Files[i].ctr;
- if (n == 0x100) n = 0; /* If write mode open, delete the entry */
- if (n > 0) n--; /* Decrement read mode open count */
+ if (n == 0x100) n = 0; /* If write mode open, delete the object semaphore */
+ if (n > 0) n--; /* Decrement read mode open count */
Files[i].ctr = n;
- if (n == 0) Files[i].fs = 0; /* Delete the entry if open count gets zero */
+ if (n == 0) { /* Delete the object semaphore if open count becomes zero */
+ Files[i].fs = 0; /* Free the entry <<<If this memory write operation is not in atomic, FF_FS_REENTRANT == 1 and FF_VOLUMES > 1, there is a potential error in this process >>> */
+ }
res = FR_OK;
} else {
- res = FR_INT_ERR; /* Invalid index nunber */
+ res = FR_INT_ERR; /* Invalid index number */
}
return res;
}
-static void clear_lock ( /* Clear lock entries of the volume */
- FATFS *fs
+static void clear_share ( /* Clear all lock entries of the volume */
+ FATFS* fs
)
{
UINT i;
@@ -1021,7 +1044,7 @@ static void clear_lock ( /* Clear lock entries of the volume */
}
}
-#endif /* FF_FS_LOCK != 0 */
+#endif /* FF_FS_LOCK */
@@ -1036,8 +1059,8 @@ static FRESULT sync_window ( /* Returns FR_OK or FR_DISK_ERR */
FRESULT res = FR_OK;
- if (fs->wflag) { /* Is the disk access window dirty */
- if (disk_write(fs->pdrv, fs->win, fs->winsect, 1) == RES_OK) { /* Write back the window */
+ if (fs->wflag) { /* Is the disk access window dirty? */
+ if (disk_write(fs->pdrv, fs->win, fs->winsect, 1) == RES_OK) { /* Write it back into the volume */
fs->wflag = 0; /* Clear window dirty flag */
if (fs->winsect - fs->fatbase < fs->fsize) { /* Is it in the 1st FAT? */
if (fs->n_fats == 2) disk_write(fs->pdrv, fs->win, fs->winsect + fs->fsize, 1); /* Reflect it to 2nd FAT if needed */
@@ -1052,23 +1075,23 @@ static FRESULT sync_window ( /* Returns FR_OK or FR_DISK_ERR */
static FRESULT move_window ( /* Returns FR_OK or FR_DISK_ERR */
- FATFS* fs, /* Filesystem object */
- DWORD sector /* Sector number to make appearance in the fs->win[] */
+ FATFS* fs, /* Filesystem object */
+ LBA_t sect /* Sector LBA to make appearance in the fs->win[] */
)
{
FRESULT res = FR_OK;
- if (sector != fs->winsect) { /* Window offset changed? */
+ if (sect != fs->winsect) { /* Window offset changed? */
#if !FF_FS_READONLY
- res = sync_window(fs); /* Write-back changes */
+ res = sync_window(fs); /* Flush the window */
#endif
if (res == FR_OK) { /* Fill sector window with new data */
- if (disk_read(fs->pdrv, fs->win, sector, 1) != RES_OK) {
- sector = 0xFFFFFFFF; /* Invalidate window if read data is not valid */
+ if (disk_read(fs->pdrv, fs->win, sect, 1) != RES_OK) {
+ sect = (LBA_t)0 - 1; /* Invalidate window if read data is not valid */
res = FR_DISK_ERR;
}
- fs->winsect = sector;
+ fs->winsect = sect;
}
}
return res;
@@ -1093,14 +1116,13 @@ static FRESULT sync_fs ( /* Returns FR_OK or FR_DISK_ERR */
if (res == FR_OK) {
if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) { /* FAT32: Update FSInfo sector if needed */
/* Create FSInfo structure */
- 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_clst);
- st_dword(fs->win + FSI_Nxt_Free, fs->last_clst);
- /* Write it into the FSInfo sector */
- fs->winsect = fs->volbase + 1;
+ memset(fs->win, 0, sizeof fs->win);
+ st_word(fs->win + BS_55AA, 0xAA55); /* Boot signature */
+ st_dword(fs->win + FSI_LeadSig, 0x41615252); /* Leading signature */
+ st_dword(fs->win + FSI_StrucSig, 0x61417272); /* Structure signature */
+ st_dword(fs->win + FSI_Free_Count, fs->free_clst); /* Number of free clusters */
+ st_dword(fs->win + FSI_Nxt_Free, fs->last_clst); /* Last allocated culuster */
+ fs->winsect = fs->volbase + 1; /* Write it into the FSInfo sector (Next to VBR) */
disk_write(fs->pdrv, fs->win, fs->winsect, 1);
fs->fsi_flag = 0;
}
@@ -1119,21 +1141,21 @@ static FRESULT sync_fs ( /* Returns FR_OK or FR_DISK_ERR */
/* Get physical sector number from cluster number */
/*-----------------------------------------------------------------------*/
-static DWORD clst2sect ( /* !=0:Sector number, 0:Failed (invalid cluster#) */
+static LBA_t clst2sect ( /* !=0:Sector number, 0:Failed (invalid cluster#) */
FATFS* fs, /* Filesystem object */
DWORD clst /* Cluster# to be converted */
)
{
clst -= 2; /* Cluster number is origin from 2 */
if (clst >= fs->n_fatent - 2) return 0; /* Is it invalid cluster number? */
- return fs->database + fs->csize * clst; /* Start sector number of the cluster */
+ return fs->database + (LBA_t)fs->csize * clst; /* Start sector number of the cluster */
}
/*-----------------------------------------------------------------------*/
-/* FAT access - Read value of a FAT entry */
+/* FAT access - Read value of an FAT entry */
/*-----------------------------------------------------------------------*/
static DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFFF:Cluster status */
@@ -1175,7 +1197,7 @@ static DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFF
case FS_EXFAT :
if ((obj->objsize != 0 && obj->sclust != 0) || obj->stat == 0) { /* Object except root dir must have valid data length */
DWORD cofs = clst - obj->sclust; /* Offset from start cluster */
- DWORD clen = (DWORD)((obj->objsize - 1) / SS(fs)) / fs->csize; /* Number of clusters - 1 */
+ DWORD clen = (DWORD)((LBA_t)((obj->objsize - 1) / SS(fs)) / fs->csize); /* Number of clusters - 1 */
if (obj->stat == 2 && cofs <= clen) { /* Is it a contiguous chain? */
val = (cofs == clen) ? 0x7FFFFFFF : clst + 1; /* No data on the FAT, generate the value */
@@ -1195,7 +1217,8 @@ static DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFF
break;
}
}
- /* go to default */
+ val = 1; /* Internal error */
+ break;
#endif
default:
val = 1; /* Internal error */
@@ -1210,7 +1233,7 @@ static DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFF
#if !FF_FS_READONLY
/*-----------------------------------------------------------------------*/
-/* FAT access - Change value of a FAT entry */
+/* FAT access - Change value of an FAT entry */
/*-----------------------------------------------------------------------*/
static FRESULT put_fat ( /* FR_OK(0):succeeded, !=0:error */
@@ -1226,30 +1249,30 @@ static FRESULT put_fat ( /* FR_OK(0):succeeded, !=0:error */
if (clst >= 2 && clst < fs->n_fatent) { /* Check if in valid range */
switch (fs->fs_type) {
- case FS_FAT12 :
+ case FS_FAT12:
bc = (UINT)clst; bc += bc / 2; /* bc: byte offset of the entry */
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; /* Put 1st byte */
+ *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val; /* Update 1st byte */
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)); /* Put 2nd byte */
+ *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F)); /* Update 2nd byte */
fs->wflag = 1;
break;
- case FS_FAT16 :
+ case FS_FAT16:
res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
if (res != FR_OK) break;
st_word(fs->win + clst * 2 % SS(fs), (WORD)val); /* Simple WORD array */
fs->wflag = 1;
break;
- case FS_FAT32 :
+ case FS_FAT32:
#if FF_FS_EXFAT
- case FS_EXFAT :
+ case FS_EXFAT:
#endif
res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
if (res != FR_OK) break;
@@ -1293,7 +1316,7 @@ static DWORD find_bitmap ( /* 0:Not found, 2..:Cluster block found, 0xFFFFFFFF:D
if (clst >= fs->n_fatent - 2) clst = 0;
scl = val = clst; ctr = 0;
for (;;) {
- if (move_window(fs, fs->database + val / 8 / SS(fs)) != FR_OK) return 0xFFFFFFFF; /* (assuming bitmap is located top of the cluster heap) */
+ if (move_window(fs, fs->bitbase + val / 8 / SS(fs)) != FR_OK) return 0xFFFFFFFF;
i = val / 8 % SS(fs); bm = 1 << (val % 8);
do {
do {
@@ -1327,13 +1350,13 @@ static FRESULT change_bitmap (
{
BYTE bm;
UINT i;
- DWORD sect;
+ LBA_t sect;
clst -= 2; /* The first bit corresponds to cluster #2 */
- sect = fs->database + clst / 8 / SS(fs); /* Sector address (assuming bitmap is located top of the cluster heap) */
- i = clst / 8 % SS(fs); /* Byte offset in the sector */
- bm = 1 << (clst % 8); /* Bit mask in the byte */
+ sect = fs->bitbase + clst / 8 / SS(fs); /* Sector address */
+ i = clst / 8 % SS(fs); /* Byte offset in the sector */
+ bm = 1 << (clst % 8); /* Bit mask in the byte */
for (;;) {
if (move_window(fs, sect++) != FR_OK) return FR_DISK_ERR;
do {
@@ -1406,7 +1429,7 @@ static FRESULT fill_last_frag (
static FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */
FFOBJID* obj, /* Corresponding object */
DWORD clst, /* Cluster to remove a chain from */
- DWORD pclst /* Previous cluster of clst (0:entire chain) */
+ DWORD pclst /* Previous cluster of clst (0 if entire chain) */
)
{
FRESULT res = FR_OK;
@@ -1416,7 +1439,7 @@ static FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */
DWORD scl = clst, ecl = clst;
#endif
#if FF_USE_TRIM
- DWORD rt[2];
+ LBA_t rt[2];
#endif
if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Check if in valid range */
@@ -1452,9 +1475,9 @@ static FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */
}
#endif
#if FF_USE_TRIM
- rt[0] = clst2sect(fs, scl); /* Start of data area freed */
- rt[1] = clst2sect(fs, ecl) + fs->csize - 1; /* End of data area freed */
- disk_ioctl(fs->pdrv, CTRL_TRIM, rt); /* Inform device the data in the block is no longer needed */
+ rt[0] = clst2sect(fs, scl); /* Start of data area to be freed */
+ rt[1] = clst2sect(fs, ecl) + fs->csize - 1; /* End of data area to be freed */
+ disk_ioctl(fs->pdrv, CTRL_TRIM, rt); /* Inform storage device that the data in the block may be erased */
#endif
scl = ecl = nxt;
}
@@ -1606,7 +1629,8 @@ static DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */
FSIZE_t ofs /* File offset to be converted to cluster# */
)
{
- DWORD cl, ncl, *tbl;
+ DWORD cl, ncl;
+ DWORD *tbl;
FATFS *fs = fp->obj.fs;
@@ -1636,7 +1660,7 @@ static FRESULT dir_clear ( /* Returns FR_OK or FR_DISK_ERR */
DWORD clst /* Directory table to clear */
)
{
- DWORD sect;
+ LBA_t sect;
UINT n, szb;
BYTE *ibuf;
@@ -1644,12 +1668,12 @@ static FRESULT dir_clear ( /* Returns FR_OK or FR_DISK_ERR */
if (sync_window(fs) != FR_OK) return FR_DISK_ERR; /* Flush disk access window */
sect = clst2sect(fs, clst); /* Top of the cluster */
fs->winsect = sect; /* Set window to top of the cluster */
- mem_set(fs->win, 0, SS(fs)); /* Clear window buffer */
+ memset(fs->win, 0, sizeof fs->win); /* Clear window buffer */
#if FF_USE_LFN == 3 /* Quick table clear by using multi-secter write */
/* Allocate a temporary buffer */
for (szb = ((DWORD)fs->csize * SS(fs) >= MAX_MALLOC) ? MAX_MALLOC : fs->csize * SS(fs), ibuf = 0; szb > SS(fs) && (ibuf = ff_memalloc(szb)) == 0; szb /= 2) ;
if (szb > SS(fs)) { /* Buffer allocated? */
- mem_set(ibuf, 0, szb);
+ memset(ibuf, 0, szb);
szb /= SS(fs); /* Bytes -> Sectors */
for (n = 0; n < fs->csize && disk_write(fs->pdrv, ibuf, sect + n, szb) == RES_OK; n += szb) ; /* Fill the cluster with 0 */
ff_memfree(ibuf);
@@ -1685,7 +1709,7 @@ static FRESULT dir_sdi ( /* FR_OK(0):succeeded, !=0:error */
dp->dptr = ofs; /* Set current offset */
clst = dp->obj.sclust; /* Table start cluster (0:root) */
if (clst == 0 && fs->fs_type >= FS_FAT32) { /* Replace cluster# 0 with root cluster# */
- clst = fs->dirbase;
+ clst = (DWORD)fs->dirbase;
if (FF_FS_EXFAT) dp->obj.stat = 0; /* exFAT: Root dir has an FAT chain */
}
@@ -1781,7 +1805,7 @@ static FRESULT dir_next ( /* FR_OK(0):succeeded, FR_NO_FILE:End of table, FR_DEN
static FRESULT dir_alloc ( /* FR_OK(0):succeeded, !=0:error */
DIR* dp, /* Pointer to the directory object */
- UINT nent /* Number of contiguous entries to allocate */
+ UINT n_ent /* Number of contiguous entries to allocate */
)
{
FRESULT res;
@@ -1796,16 +1820,16 @@ static FRESULT dir_alloc ( /* FR_OK(0):succeeded, !=0:error */
res = move_window(fs, dp->sect);
if (res != FR_OK) break;
#if FF_FS_EXFAT
- if ((fs->fs_type == FS_EXFAT) ? (int)((dp->dir[XDIR_Type] & 0x80) == 0) : (int)(dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0)) {
+ if ((fs->fs_type == FS_EXFAT) ? (int)((dp->dir[XDIR_Type] & 0x80) == 0) : (int)(dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0)) { /* Is the entry free? */
#else
- if (dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0) {
+ if (dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0) { /* Is the entry free? */
#endif
- if (++n == nent) break; /* A block of contiguous free entries is found */
+ if (++n == n_ent) break; /* Is a block of contiguous free entries found? */
} else {
- n = 0; /* Not a blank entry. Restart to search */
+ n = 0; /* Not a free entry, restart to search */
}
- res = dir_next(dp, 1);
- } while (res == FR_OK); /* Next entry with table stretch enabled */
+ res = dir_next(dp, 1); /* Next entry with table stretch enabled */
+ } while (res == FR_OK);
}
if (res == FR_NO_FILE) res = FR_DENIED; /* No directory entry to allocate */
@@ -1874,7 +1898,7 @@ static int cmp_lfn ( /* 1:matched, 0:not matched */
for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */
uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */
if (wc != 0) {
- if (i >= FF_MAX_LFN || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) { /* Compare it */
+ if (i >= FF_MAX_LFN + 1 || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) { /* Compare it */
return 0; /* Not matched */
}
wc = uc;
@@ -1910,15 +1934,15 @@ static int pick_lfn ( /* 1:succeeded, 0:buffer overflow or invalid LFN entry */
for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */
uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */
if (wc != 0) {
- if (i >= FF_MAX_LFN) return 0; /* Buffer overflow? */
+ if (i >= FF_MAX_LFN + 1) return 0; /* Buffer overflow? */
lfnbuf[i++] = wc = uc; /* Store it */
} else {
if (uc != 0xFFFF) return 0; /* Check filler */
}
}
- if (dir[LDIR_Ord] & LLEF) { /* Put terminator if it is the last LFN part */
- if (i >= FF_MAX_LFN) return 0; /* Buffer overflow? */
+ if (dir[LDIR_Ord] & LLEF && wc != 0) { /* Put terminator if it is the last LFN part and not terminated */
+ if (i >= FF_MAX_LFN + 1) return 0; /* Buffer overflow? */
lfnbuf[i] = 0;
}
@@ -1953,7 +1977,7 @@ static void put_lfn (
do {
if (wc != 0xFFFF) wc = lfn[i++]; /* Get an effective character */
st_word(dir + LfnOfs[s], wc); /* Put it */
- if (wc == 0) wc = 0xFFFF; /* Padding characters for left locations */
+ if (wc == 0) wc = 0xFFFF; /* Padding characters for following items */
} while (++s < 13);
if (wc == 0xFFFF || !lfn[i]) ord |= LLEF; /* Last LFN part is the start of LFN sequence */
dir[LDIR_Ord] = ord; /* Set the LFN order */
@@ -1971,7 +1995,7 @@ static void put_lfn (
static void gen_numname (
BYTE* dst, /* Pointer to the buffer to store numbered SFN */
- const BYTE* src, /* Pointer to SFN */
+ const BYTE* src, /* Pointer to SFN in directory form */
const WCHAR* lfn, /* Pointer to LFN */
UINT seq /* Sequence number */
)
@@ -1979,42 +2003,41 @@ static void gen_numname (
BYTE ns[8], c;
UINT i, j;
WCHAR wc;
- DWORD sr;
+ DWORD sreg;
- mem_cpy(dst, src, 11);
+ memcpy(dst, src, 11); /* Prepare the SFN to be modified */
if (seq > 5) { /* In case of many collisions, generate a hash number instead of sequential number */
- sr = seq;
+ sreg = seq;
while (*lfn) { /* Create a CRC as hash value */
wc = *lfn++;
for (i = 0; i < 16; i++) {
- sr = (sr << 1) + (wc & 1);
+ sreg = (sreg << 1) + (wc & 1);
wc >>= 1;
- if (sr & 0x10000) sr ^= 0x11021;
+ if (sreg & 0x10000) sreg ^= 0x11021;
}
}
- seq = (UINT)sr;
+ seq = (UINT)sreg;
}
- /* itoa (hexdecimal) */
+ /* Make suffix (~ + hexadecimal) */
i = 7;
do {
- c = (BYTE)((seq % 16) + '0');
+ c = (BYTE)((seq % 16) + '0'); seq /= 16;
if (c > '9') c += 7;
ns[i--] = c;
- seq /= 16;
- } while (seq);
+ } while (i && seq);
ns[i] = '~';
- /* Append the number to the SFN body */
- for (j = 0; j < i && dst[j] != ' '; j++) {
- if (dbc_1st(dst[j])) {
+ /* Append the suffix to the SFN body */
+ for (j = 0; j < i && dst[j] != ' '; j++) { /* Find the offset to append */
+ if (dbc_1st(dst[j])) { /* To avoid DBC break up */
if (j == i - 1) break;
j++;
}
}
- do {
+ do { /* Append the suffix */
dst[j++] = (i < 8) ? ns[i++] : ' ';
} while (j < 8);
}
@@ -2099,89 +2122,48 @@ static DWORD xsum32 ( /* Returns 32-bit checksum */
#endif
-#if FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2
-/*------------------------------------------------------*/
-/* exFAT: Get object information from a directory block */
-/*------------------------------------------------------*/
-static void get_xfileinfo (
- BYTE* dirb, /* Pointer to the direcotry entry block 85+C0+C1s */
- FILINFO* fno /* Buffer to store the extracted file information */
-)
-{
- WCHAR wc, hs;
- UINT di, si, nc;
-
- /* Get file name from the entry block */
- si = SZDIRE * 2; /* 1st C1 entry */
- nc = 0; hs = 0; di = 0;
- while (nc < dirb[XDIR_NumName]) {
- if (si >= MAXDIRB(FF_MAX_LFN)) { di = 0; break; } /* Truncated directory block? */
- if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */
- wc = ld_word(dirb + si); si += 2; nc++; /* Get a character */
- if (hs == 0 && IsSurrogate(wc)) { /* Is it a surrogate? */
- hs = wc; continue; /* Get low surrogate */
- }
- wc = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di); /* Store it in API encoding */
- if (wc == 0) { di = 0; break; } /* Buffer overflow or wrong encoding? */
- di += wc;
- hs = 0;
- }
- if (hs != 0) di = 0; /* Broken surrogate pair? */
- if (di == 0) fno->fname[di++] = '?'; /* Inaccessible object name? */
- fno->fname[di] = 0; /* Terminate the name */
- fno->altname[0] = 0; /* exFAT does not support SFN */
-
- fno->fattrib = dirb[XDIR_Attr]; /* Attribute */
- fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_qword(dirb + XDIR_FileSize); /* Size */
- fno->ftime = ld_word(dirb + XDIR_ModTime + 0); /* Time */
- fno->fdate = ld_word(dirb + XDIR_ModTime + 2); /* Date */
-}
-
-#endif /* FF_FS_MINIMIZE <= 1 || FF_FS_RPATH >= 2 */
-
-
-/*-----------------------------------*/
-/* exFAT: Get a directry entry block */
-/*-----------------------------------*/
+/*------------------------------------*/
+/* exFAT: Get a directory entry block */
+/*------------------------------------*/
static FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */
- DIR* dp /* Reading direcotry object pointing top of the entry block to load */
+ DIR* dp /* Reading directory object pointing top of the entry block to load */
)
{
FRESULT res;
UINT i, sz_ent;
- BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the on-memory direcotry entry block 85+C0+C1s */
+ BYTE *dirb = dp->obj.fs->dirbuf; /* Pointer to the on-memory directory entry block 85+C0+C1s */
- /* Load 85 entry */
+ /* Load file directory entry */
res = move_window(dp->obj.fs, dp->sect);
if (res != FR_OK) return res;
- if (dp->dir[XDIR_Type] != 0x85) return FR_INT_ERR; /* Invalid order */
- mem_cpy(dirb + 0 * SZDIRE, dp->dir, SZDIRE);
+ if (dp->dir[XDIR_Type] != ET_FILEDIR) return FR_INT_ERR; /* Invalid order */
+ memcpy(dirb + 0 * SZDIRE, dp->dir, SZDIRE);
sz_ent = (dirb[XDIR_NumSec] + 1) * SZDIRE;
if (sz_ent < 3 * SZDIRE || sz_ent > 19 * SZDIRE) return FR_INT_ERR;
- /* Load C0 entry */
+ /* Load stream extension entry */
res = dir_next(dp, 0);
if (res == FR_NO_FILE) res = FR_INT_ERR; /* It cannot be */
if (res != FR_OK) return res;
res = move_window(dp->obj.fs, dp->sect);
if (res != FR_OK) return res;
- if (dp->dir[XDIR_Type] != 0xC0) return FR_INT_ERR; /* Invalid order */
- mem_cpy(dirb + 1 * SZDIRE, dp->dir, SZDIRE);
+ if (dp->dir[XDIR_Type] != ET_STREAM) return FR_INT_ERR; /* Invalid order */
+ memcpy(dirb + 1 * SZDIRE, dp->dir, SZDIRE);
if (MAXDIRB(dirb[XDIR_NumName]) > sz_ent) return FR_INT_ERR;
- /* Load C1 entries */
- i = 2 * SZDIRE; /* C1 offset to load */
+ /* Load file name entries */
+ i = 2 * SZDIRE; /* Name offset to load */
do {
res = dir_next(dp, 0);
if (res == FR_NO_FILE) res = FR_INT_ERR; /* It cannot be */
if (res != FR_OK) return res;
res = move_window(dp->obj.fs, dp->sect);
if (res != FR_OK) return res;
- if (dp->dir[XDIR_Type] != 0xC1) return FR_INT_ERR; /* Invalid order */
- if (i < MAXDIRB(FF_MAX_LFN)) mem_cpy(dirb + i, dp->dir, SZDIRE);
+ if (dp->dir[XDIR_Type] != ET_FILENAME) return FR_INT_ERR; /* Invalid order */
+ if (i < MAXDIRB(FF_MAX_LFN)) memcpy(dirb + i, dp->dir, SZDIRE);
} while ((i += SZDIRE) < sz_ent);
/* Sanity check (do it for only accessible object) */
@@ -2215,7 +2197,7 @@ static void init_alloc_info (
/*------------------------------------------------*/
static FRESULT load_obj_xdir (
- DIR* dp, /* Blank directory object to be used to access containing direcotry */
+ DIR* dp, /* Blank directory object to be used to access containing directory */
const FFOBJID* obj /* Object with its containing directory information */
)
{
@@ -2244,23 +2226,23 @@ static FRESULT load_obj_xdir (
/*----------------------------------------*/
static FRESULT store_xdir (
- DIR* dp /* Pointer to the direcotry object */
+ DIR* dp /* Pointer to the directory object */
)
{
FRESULT res;
UINT nent;
- BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the direcotry entry block 85+C0+C1s */
+ BYTE *dirb = dp->obj.fs->dirbuf; /* Pointer to the directory entry block 85+C0+C1s */
/* Create set sum */
st_word(dirb + XDIR_SetSum, xdir_sum(dirb));
nent = dirb[XDIR_NumSec] + 1;
- /* Store the direcotry entry block to the directory */
+ /* Store the directory entry block to the directory */
res = dir_sdi(dp, dp->blk_ofs);
while (res == FR_OK) {
res = move_window(dp->obj.fs, dp->sect);
if (res != FR_OK) break;
- mem_cpy(dp->dir, dirb, SZDIRE);
+ memcpy(dp->dir, dirb, SZDIRE);
dp->obj.fs->wflag = 1;
if (--nent == 0) break;
dirb += SZDIRE;
@@ -2272,11 +2254,11 @@ static FRESULT store_xdir (
/*-------------------------------------------*/
-/* exFAT: Create a new directory enrty block */
+/* exFAT: Create a new directory entry block */
/*-------------------------------------------*/
static void create_xdir (
- BYTE* dirb, /* Pointer to the direcotry entry block buffer */
+ BYTE* dirb, /* Pointer to the directory entry block buffer */
const WCHAR* lfn /* Pointer to the object name */
)
{
@@ -2285,19 +2267,19 @@ static void create_xdir (
WCHAR wc;
- /* Create 85,C0 entry */
- mem_set(dirb, 0, 2 * SZDIRE);
- dirb[0 * SZDIRE + XDIR_Type] = 0x85; /* 85 entry */
- dirb[1 * SZDIRE + XDIR_Type] = 0xC0; /* C0 entry */
+ /* Create file-directory and stream-extension entry */
+ memset(dirb, 0, 2 * SZDIRE);
+ dirb[0 * SZDIRE + XDIR_Type] = ET_FILEDIR;
+ dirb[1 * SZDIRE + XDIR_Type] = ET_STREAM;
- /* Create C1 entries */
- i = SZDIRE * 2; /* Top of C1 entries */
+ /* Create file-name entries */
+ i = SZDIRE * 2; /* Top of file_name entries */
nlen = nc1 = 0; wc = 1;
do {
- dirb[i++] = 0xC1; dirb[i++] = 0; /* Entry type C1 */
+ dirb[i++] = ET_FILENAME; dirb[i++] = 0;
do { /* Fill name field */
if (wc != 0 && (wc = lfn[nlen]) != 0) nlen++; /* Get a character if exist */
- st_word(dirb + i, wc); /* Store it */
+ st_word(dirb + i, wc); /* Store it */
i += 2;
} while (i % SZDIRE != 0);
nc1++;
@@ -2318,8 +2300,8 @@ static void create_xdir (
/* Read an object from the directory */
/*-----------------------------------------------------------------------*/
-#define dir_read_file(dp) dir_read(dp, 0)
-#define dir_read_label(dp) dir_read(dp, 1)
+#define DIR_READ_FILE(dp) dir_read(dp, 0)
+#define DIR_READ_LABEL(dp) dir_read(dp, 1)
static FRESULT dir_read (
DIR* dp, /* Pointer to the directory object */
@@ -2328,7 +2310,7 @@ static FRESULT dir_read (
{
FRESULT res = FR_NO_FILE;
FATFS *fs = dp->obj.fs;
- BYTE a, c;
+ BYTE attr, b;
#if FF_USE_LFN
BYTE ord = 0xFF, sum = 0xFF;
#endif
@@ -2336,16 +2318,16 @@ static FRESULT dir_read (
while (dp->sect) {
res = move_window(fs, dp->sect);
if (res != FR_OK) break;
- c = dp->dir[DIR_Name]; /* Test for the entry type */
- if (c == 0) {
+ b = dp->dir[DIR_Name]; /* Test for the entry type */
+ if (b == 0) {
res = FR_NO_FILE; break; /* Reached to end of the directory */
}
#if FF_FS_EXFAT
if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */
if (FF_USE_LABEL && vol) {
- if (c == 0x83) break; /* Volume label entry? */
+ if (b == ET_VLABEL) break; /* Volume label entry? */
} else {
- if (c == 0x85) { /* Start of the file entry block? */
+ if (b == ET_FILEDIR) { /* Start of the file entry block? */
dp->blk_ofs = dp->dptr; /* Get location of the block */
res = load_xdir(dp); /* Load the entry block */
if (res == FR_OK) {
@@ -2357,28 +2339,28 @@ static FRESULT dir_read (
} else
#endif
{ /* On the FAT/FAT32 volume */
- dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK; /* Get attribute */
+ dp->obj.attr = attr = dp->dir[DIR_Attr] & AM_MASK; /* Get attribute */
#if FF_USE_LFN /* LFN configuration */
- if (c == DDEM || c == '.' || (int)((a & ~AM_ARC) == AM_VOL) != vol) { /* An entry without valid data */
+ if (b == DDEM || b == '.' || (int)((attr & ~AM_ARC) == AM_VOL) != vol) { /* An entry without valid data */
ord = 0xFF;
} else {
- if (a == AM_LFN) { /* An LFN entry is found */
- if (c & LLEF) { /* Is it start of an LFN sequence? */
+ if (attr == AM_LFN) { /* An LFN entry is found */
+ if (b & LLEF) { /* Is it start of an LFN sequence? */
sum = dp->dir[LDIR_Chksum];
- c &= (BYTE)~LLEF; ord = c;
+ b &= (BYTE)~LLEF; ord = b;
dp->blk_ofs = dp->dptr;
}
/* Check LFN validity and capture it */
- ord = (c == ord && sum == dp->dir[LDIR_Chksum] && pick_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;
- } else { /* An SFN entry is found */
+ ord = (b == ord && sum == dp->dir[LDIR_Chksum] && pick_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;
+ } else { /* An SFN entry is found */
if (ord != 0 || sum != sum_sfn(dp->dir)) { /* Is there a valid LFN? */
- dp->blk_ofs = 0xFFFFFFFF; /* It has no LFN. */
+ dp->blk_ofs = 0xFFFFFFFF; /* It has no LFN. */
}
break;
}
}
#else /* Non LFN configuration */
- if (c != DDEM && c != '.' && a != AM_LFN && (int)((a & ~AM_ARC) == AM_VOL) == vol) { /* Is it a valid entry? */
+ if (b != DDEM && b != '.' && attr != AM_LFN && (int)((attr & ~AM_ARC) == AM_VOL) == vol) { /* Is it a valid entry? */
break;
}
#endif
@@ -2418,9 +2400,9 @@ static FRESULT dir_find ( /* FR_OK(0):succeeded, !=0:error */
UINT di, ni;
WORD hash = xname_sum(fs->lfnbuf); /* Hash value of the name to find */
- while ((res = dir_read_file(dp)) == FR_OK) { /* Read an item */
+ while ((res = DIR_READ_FILE(dp)) == FR_OK) { /* Read an item */
#if FF_MAX_LFN < 255
- if (fs->dirbuf[XDIR_NumName] > FF_MAX_LFN) continue; /* Skip comparison if inaccessible object name */
+ if (fs->dirbuf[XDIR_NumName] > FF_MAX_LFN) continue; /* Skip comparison if inaccessible object name */
#endif
if (ld_word(fs->dirbuf + XDIR_NameHash) != hash) continue; /* Skip comparison if hash mismatched */
for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) { /* Compare the name */
@@ -2458,13 +2440,13 @@ static FRESULT dir_find ( /* FR_OK(0):succeeded, !=0:error */
}
} else { /* An SFN entry is found */
if (ord == 0 && sum == sum_sfn(dp->dir)) break; /* LFN matched? */
- if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* SFN matched? */
+ if (!(dp->fn[NSFLAG] & NS_LOSS) && !memcmp(dp->dir, dp->fn, 11)) break; /* SFN matched? */
ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */
}
}
#else /* Non LFN configuration */
dp->obj.attr = dp->dir[DIR_Attr] & AM_MASK;
- if (!(dp->dir[DIR_Attr] & AM_VOL) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* Is it a valid entry? */
+ if (!(dp->dir[DIR_Attr] & AM_VOL) && !memcmp(dp->dir, dp->fn, 11)) break; /* Is it a valid entry? */
#endif
res = dir_next(dp, 0); /* Next entry */
} while (res == FR_OK);
@@ -2487,35 +2469,35 @@ static FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too
FRESULT res;
FATFS *fs = dp->obj.fs;
#if FF_USE_LFN /* LFN configuration */
- UINT n, nlen, nent;
+ UINT n, len, n_ent;
BYTE sn[12], sum;
if (dp->fn[NSFLAG] & (NS_DOT | NS_NONAME)) return FR_INVALID_NAME; /* Check name validity */
- for (nlen = 0; fs->lfnbuf[nlen]; nlen++) ; /* Get lfn length */
+ for (len = 0; fs->lfnbuf[len]; len++) ; /* Get lfn length */
#if FF_FS_EXFAT
if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */
- nent = (nlen + 14) / 15 + 2; /* Number of entries to allocate (85+C0+C1s) */
- res = dir_alloc(dp, nent); /* Allocate entries */
+ n_ent = (len + 14) / 15 + 2; /* Number of entries to allocate (85+C0+C1s) */
+ res = dir_alloc(dp, n_ent); /* Allocate directory entries */
if (res != FR_OK) return res;
- dp->blk_ofs = dp->dptr - SZDIRE * (nent - 1); /* Set the allocated entry block offset */
+ dp->blk_ofs = dp->dptr - SZDIRE * (n_ent - 1); /* Set the allocated entry block offset */
- if (dp->obj.stat & 4) { /* Has the directory been stretched? */
+ if (dp->obj.stat & 4) { /* Has the directory been stretched by new allocation? */
dp->obj.stat &= ~4;
res = fill_first_frag(&dp->obj); /* Fill the first fragment on the FAT if needed */
if (res != FR_OK) return res;
res = fill_last_frag(&dp->obj, dp->clust, 0xFFFFFFFF); /* Fill the last fragment on the FAT if needed */
if (res != FR_OK) return res;
- if (dp->obj.sclust != 0) { /* Is it a sub directory? */
+ if (dp->obj.sclust != 0) { /* Is it a sub-directory? */
DIR dj;
res = load_obj_xdir(&dj, &dp->obj); /* Load the object status */
if (res != FR_OK) return res;
- dp->obj.objsize += (DWORD)fs->csize * SS(fs); /* Increase the directory size by cluster size */
- st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize); /* Update the allocation status */
+ dp->obj.objsize += (DWORD)fs->csize * SS(fs); /* Increase the directory size by cluster size */
+ st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize);
st_qword(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize);
- fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1;
+ fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1; /* Update the allocation status */
res = store_xdir(&dj); /* Store the object status */
if (res != FR_OK) return res;
}
@@ -2526,7 +2508,7 @@ static FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too
}
#endif
/* On the FAT/FAT32 volume */
- mem_cpy(sn, dp->fn, 12);
+ memcpy(sn, dp->fn, 12);
if (sn[NSFLAG] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */
dp->fn[NSFLAG] = NS_NOLFN; /* Find only SFN */
for (n = 1; n < 100; n++) {
@@ -2540,19 +2522,19 @@ static FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too
}
/* Create an SFN with/without LFNs. */
- nent = (sn[NSFLAG] & NS_LFN) ? (nlen + 12) / 13 + 1 : 1; /* Number of entries to allocate */
- res = dir_alloc(dp, nent); /* Allocate entries */
- if (res == FR_OK && --nent) { /* Set LFN entry if needed */
- res = dir_sdi(dp, dp->dptr - nent * SZDIRE);
+ n_ent = (sn[NSFLAG] & NS_LFN) ? (len + 12) / 13 + 1 : 1; /* Number of entries to allocate */
+ res = dir_alloc(dp, n_ent); /* Allocate entries */
+ if (res == FR_OK && --n_ent) { /* Set LFN entry if needed */
+ res = dir_sdi(dp, dp->dptr - n_ent * SZDIRE);
if (res == FR_OK) {
sum = sum_sfn(dp->fn); /* Checksum value of the SFN tied to the LFN */
do { /* Store LFN entries in bottom first */
res = move_window(fs, dp->sect);
if (res != FR_OK) break;
- put_lfn(fs->lfnbuf, dp->dir, (BYTE)nent, sum);
+ put_lfn(fs->lfnbuf, dp->dir, (BYTE)n_ent, sum);
fs->wflag = 1;
res = dir_next(dp, 0); /* Next entry */
- } while (res == FR_OK && --nent);
+ } while (res == FR_OK && --n_ent);
}
}
@@ -2565,8 +2547,8 @@ static FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too
if (res == FR_OK) {
res = move_window(fs, dp->sect);
if (res == FR_OK) {
- mem_set(dp->dir, 0, SZDIRE); /* Clean the entry */
- mem_cpy(dp->dir + DIR_Name, dp->fn, 11); /* Put SFN */
+ memset(dp->dir, 0, SZDIRE); /* Clean the entry */
+ memcpy(dp->dir + DIR_Name, dp->fn, 11); /* Put SFN */
#if FF_USE_LFN
dp->dir[DIR_NTres] = dp->fn[NSFLAG] & (NS_BODY | NS_EXT); /* Put NT flag */
#endif
@@ -2602,7 +2584,7 @@ static FRESULT dir_remove ( /* FR_OK:Succeeded, FR_DISK_ERR:A disk error */
if (res != FR_OK) break;
if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) { /* On the exFAT volume */
dp->dir[XDIR_Type] &= 0x7F; /* Clear the entry InUse flag. */
- } else { /* On the FAT/FAT32 volume */
+ } else { /* On the FAT/FAT32 volume */
dp->dir[DIR_Name] = DDEM; /* Mark the entry 'deleted'. */
}
fs->wflag = 1;
@@ -2639,8 +2621,10 @@ static void get_fileinfo (
{
UINT si, di;
#if FF_USE_LFN
+ BYTE lcf;
WCHAR wc, hs;
FATFS *fs = dp->obj.fs;
+ UINT nw;
#else
TCHAR c;
#endif
@@ -2651,22 +2635,53 @@ static void get_fileinfo (
#if FF_USE_LFN /* LFN configuration */
#if FF_FS_EXFAT
- if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */
- get_xfileinfo(fs->dirbuf, fno);
+ if (fs->fs_type == FS_EXFAT) { /* exFAT volume */
+ UINT nc = 0;
+
+ si = SZDIRE * 2; di = 0; /* 1st C1 entry in the entry block */
+ hs = 0;
+ while (nc < fs->dirbuf[XDIR_NumName]) {
+ if (si >= MAXDIRB(FF_MAX_LFN)) { /* Truncated directory block? */
+ di = 0; break;
+ }
+ if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */
+ wc = ld_word(fs->dirbuf + si); si += 2; nc++; /* Get a character */
+ if (hs == 0 && IsSurrogate(wc)) { /* Is it a surrogate? */
+ hs = wc; continue; /* Get low surrogate */
+ }
+ nw = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di); /* Store it in API encoding */
+ if (nw == 0) { /* Buffer overflow or wrong char? */
+ di = 0; break;
+ }
+ di += nw;
+ hs = 0;
+ }
+ if (hs != 0) di = 0; /* Broken surrogate pair? */
+ if (di == 0) fno->fname[di++] = '\?'; /* Inaccessible object name? */
+ fno->fname[di] = 0; /* Terminate the name */
+ fno->altname[0] = 0; /* exFAT does not support SFN */
+
+ fno->fattrib = fs->dirbuf[XDIR_Attr] & AM_MASKX; /* Attribute */
+ fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_qword(fs->dirbuf + XDIR_FileSize); /* Size */
+ fno->ftime = ld_word(fs->dirbuf + XDIR_ModTime + 0); /* Time */
+ fno->fdate = ld_word(fs->dirbuf + XDIR_ModTime + 2); /* Date */
return;
} else
#endif
- { /* On the FAT/FAT32 volume */
+ { /* FAT/FAT32 volume */
if (dp->blk_ofs != 0xFFFFFFFF) { /* Get LFN if available */
- si = di = hs = 0;
+ si = di = 0;
+ hs = 0;
while (fs->lfnbuf[si] != 0) {
wc = fs->lfnbuf[si++]; /* Get an LFN character (UTF-16) */
if (hs == 0 && IsSurrogate(wc)) { /* Is it a surrogate? */
hs = wc; continue; /* Get low surrogate */
}
- wc = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di); /* Store it in UTF-16 or UTF-8 encoding */
- if (wc == 0) { di = 0; break; } /* Invalid char or buffer overflow? */
- di += wc;
+ nw = put_utf((DWORD)hs << 16 | wc, &fno->fname[di], FF_LFN_BUF - di); /* Store it in API encoding */
+ if (nw == 0) { /* Buffer overflow or wrong char? */
+ di = 0; break;
+ }
+ di += nw;
hs = 0;
}
if (hs != 0) di = 0; /* Broken surrogate pair? */
@@ -2685,10 +2700,14 @@ static void get_fileinfo (
wc = wc << 8 | dp->dir[si++];
}
wc = ff_oem2uni(wc, CODEPAGE); /* ANSI/OEM -> Unicode */
- if (wc == 0) { di = 0; break; } /* Wrong char in the current code page? */
- wc = put_utf(wc, &fno->altname[di], FF_SFN_BUF - di); /* Store it in Unicode */
- if (wc == 0) { di = 0; break; } /* Buffer overflow? */
- di += wc;
+ if (wc == 0) { /* Wrong char in the current code page? */
+ di = 0; break;
+ }
+ nw = put_utf(wc, &fno->altname[di], FF_SFN_BUF - di); /* Store it in API encoding */
+ if (nw == 0) { /* Buffer overflow? */
+ di = 0; break;
+ }
+ di += nw;
#else /* ANSI/OEM output */
fno->altname[di++] = (TCHAR)wc; /* Store it without any conversion */
#endif
@@ -2696,12 +2715,13 @@ static void get_fileinfo (
fno->altname[di] = 0; /* Terminate the SFN (null string means SFN is invalid) */
if (fno->fname[0] == 0) { /* If LFN is invalid, altname[] needs to be copied to fname[] */
- if (di == 0) { /* If LFN and SFN both are invalid, this object is inaccesible */
- fno->fname[di++] = '?';
+ if (di == 0) { /* If LFN and SFN both are invalid, this object is inaccessible */
+ fno->fname[di++] = '\?';
} else {
- for (si = di = 0; fno->altname[si]; si++, di++) { /* Copy altname[] to fname[] with case information */
+ for (si = di = 0, lcf = NS_BODY; fno->altname[si]; si++, di++) { /* Copy altname[] to fname[] with case information */
wc = (WCHAR)fno->altname[si];
- if (IsUpper(wc) && (dp->dir[DIR_NTres] & ((si >= 9) ? NS_EXT : NS_BODY))) wc += 0x20;
+ if (wc == '.') lcf = NS_EXT;
+ if (IsUpper(wc) && (dp->dir[DIR_NTres] & lcf)) wc += 0x20;
fno->fname[di] = (TCHAR)wc;
}
}
@@ -2718,10 +2738,10 @@ static void get_fileinfo (
if (si == 9) fno->fname[di++] = '.';/* Insert a . if extension is exist */
fno->fname[di++] = c;
}
- fno->fname[di] = 0;
+ fno->fname[di] = 0; /* Terminate the SFN */
#endif
- fno->fattrib = dp->dir[DIR_Attr]; /* Attribute */
+ fno->fattrib = dp->dir[DIR_Attr] & AM_MASK; /* Attribute */
fno->fsize = ld_dword(dp->dir + DIR_FileSize); /* Size */
fno->ftime = ld_word(dp->dir + DIR_ModTime + 0); /* Time */
fno->fdate = ld_word(dp->dir + DIR_ModTime + 2); /* Date */
@@ -2736,7 +2756,10 @@ static void get_fileinfo (
/* Pattern matching */
/*-----------------------------------------------------------------------*/
-static DWORD get_achar ( /* Get a character and advances ptr */
+#define FIND_RECURS 4 /* Maximum number of wildcard terms in the pattern to limit recursion */
+
+
+static DWORD get_achar ( /* Get a character and advance ptr */
const TCHAR** ptr /* Pointer to pointer to the ANSI/OEM or Unicode string */
)
{
@@ -2767,41 +2790,48 @@ static DWORD get_achar ( /* Get a character and advances ptr */
}
-static int pattern_matching ( /* 0:not matched, 1:matched */
+static int pattern_match ( /* 0:mismatched, 1:matched */
const TCHAR* pat, /* Matching pattern */
const TCHAR* nam, /* String to be tested */
- int skip, /* Number of pre-skip chars (number of ?s) */
- int inf /* Infinite search (* specified) */
+ UINT skip, /* Number of pre-skip chars (number of ?s, b8:infinite (* specified)) */
+ UINT recur /* Recursion count */
)
{
- const TCHAR *pp, *np;
- DWORD pc, nc;
- int nm, nx;
+ const TCHAR *pptr;
+ const TCHAR *nptr;
+ DWORD pchr, nchr;
+ UINT sk;
- while (skip--) { /* Pre-skip name chars */
+ while ((skip & 0xFF) != 0) { /* Pre-skip name chars */
if (!get_achar(&nam)) return 0; /* Branch mismatched if less name chars */
+ skip--;
}
- if (*pat == 0 && inf) return 1; /* (short circuit) */
+ if (*pat == 0 && skip) return 1; /* Matched? (short circuit) */
do {
- pp = pat; np = nam; /* Top of pattern and name to match */
+ pptr = pat; nptr = nam; /* Top of pattern and name to match */
for (;;) {
- if (*pp == '?' || *pp == '*') { /* Wildcard? */
- nm = nx = 0;
- do { /* Analyze the wildcard block */
- if (*pp++ == '?') nm++; else nx = 1;
- } while (*pp == '?' || *pp == '*');
- if (pattern_matching(pp, np, nm, nx)) return 1; /* Test new branch (recurs upto number of wildcard blocks in the pattern) */
- nc = *np; break; /* Branch mismatched */
- }
- pc = get_achar(&pp); /* Get a pattern char */
- nc = get_achar(&np); /* Get a name char */
- if (pc != nc) break; /* Branch mismatched? */
- if (pc == 0) return 1; /* Branch matched? (matched at end of both strings) */
+ if (*pptr == '\?' || *pptr == '*') { /* Wildcard term? */
+ if (recur == 0) return 0; /* Too many wildcard terms? */
+ sk = 0;
+ do { /* Analyze the wildcard term */
+ if (*pptr++ == '\?') {
+ sk++;
+ } else {
+ sk |= 0x100;
+ }
+ } while (*pptr == '\?' || *pptr == '*');
+ if (pattern_match(pptr, nptr, sk, recur - 1)) return 1; /* Test new branch (recursive call) */
+ nchr = *nptr; break; /* Branch mismatched */
+ }
+ pchr = get_achar(&pptr); /* Get a pattern char */
+ nchr = get_achar(&nptr); /* Get a name char */
+ if (pchr != nchr) break; /* Branch mismatched? */
+ if (pchr == 0) return 1; /* Branch matched? (matched at end of both strings) */
}
get_achar(&nam); /* nam++ */
- } while (inf && nc); /* Retry until end of name if infinite search is specified */
+ } while (skip && nchr); /* Retry until end of name if infinite search is specified */
return 0;
}
@@ -2821,10 +2851,11 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr
{
#if FF_USE_LFN /* LFN configuration */
BYTE b, cf;
- WCHAR wc, *lfn;
+ WCHAR wc;
+ WCHAR *lfn;
+ const TCHAR* p;
DWORD uc;
UINT i, ni, si, di;
- const TCHAR *p;
/* Create LFN into LFN working buffer */
@@ -2834,27 +2865,32 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr
if (uc == 0xFFFFFFFF) return FR_INVALID_NAME; /* Invalid code or UTF decode error */
if (uc >= 0x10000) lfn[di++] = (WCHAR)(uc >> 16); /* Store high surrogate if needed */
wc = (WCHAR)uc;
- if (wc < ' ' || wc == '/' || wc == '\\') break; /* Break if end of the path or a separator is found */
- if (wc < 0x80 && chk_chr("\"*:<>\?|\x7F", wc)) return FR_INVALID_NAME; /* Reject illegal characters for LFN */
+ if (wc < ' ' || IsSeparator(wc)) break; /* Break if end of the path or a separator is found */
+ if (wc < 0x80 && strchr("*:<>|\"\?\x7F", (int)wc)) return FR_INVALID_NAME; /* Reject illegal characters for LFN */
if (di >= FF_MAX_LFN) return FR_INVALID_NAME; /* Reject too long name */
- lfn[di++] = wc; /* Store the Unicode character */
+ lfn[di++] = wc; /* Store the Unicode character */
}
- while (*p == '/' || *p == '\\') p++; /* Skip duplicated separators if exist */
- *path = p; /* Return pointer to the next segment */
- cf = (wc < ' ') ? NS_LAST : 0; /* Set last segment flag if end of the path */
+ if (wc < ' ') { /* Stopped at end of the path? */
+ cf = NS_LAST; /* Last segment */
+ } else { /* Stopped at a separator */
+ while (IsSeparator(*p)) p++; /* Skip duplicated separators if exist */
+ cf = 0; /* Next segment may follow */
+ if (IsTerminator(*p)) cf = NS_LAST; /* Ignore terminating separator */
+ }
+ *path = p; /* Return pointer to the next segment */
#if FF_FS_RPATH != 0
if ((di == 1 && lfn[di - 1] == '.') ||
(di == 2 && lfn[di - 1] == '.' && lfn[di - 2] == '.')) { /* Is this segment a dot name? */
lfn[di] = 0;
- for (i = 0; i < 11; i++) { /* Create dot name for SFN entry */
+ for (i = 0; i < 11; i++) { /* Create dot name for SFN entry */
dp->fn[i] = (i < di) ? '.' : ' ';
}
- dp->fn[i] = cf | NS_DOT; /* This is a dot entry */
+ dp->fn[i] = cf | NS_DOT; /* This is a dot entry */
return FR_OK;
}
#endif
- while (di) { /* Snip off trailing spaces and dots if exist */
+ while (di) { /* Snip off trailing spaces and dots if exist */
wc = lfn[di - 1];
if (wc != ' ' && wc != '.') break;
di--;
@@ -2867,7 +2903,7 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr
if (si > 0 || lfn[si] == '.') cf |= NS_LOSS | NS_LFN; /* Is there any leading space or dot? */
while (di > 0 && lfn[di - 1] != '.') di--; /* Find last dot (di<=si: no extension) */
- mem_set(dp->fn, ' ', 11);
+ memset(dp->fn, ' ', 11);
i = b = 0; ni = 8;
for (;;) {
wc = lfn[si++]; /* Get an LFN character */
@@ -2888,20 +2924,20 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr
continue;
}
- if (wc >= 0x80) { /* Is this a non-ASCII character? */
+ if (wc >= 0x80) { /* Is this an extended character? */
cf |= NS_LFN; /* LFN entry needs to be created */
#if FF_CODE_PAGE == 0
- if (ExCvt) { /* At SBCS */
+ if (ExCvt) { /* In SBCS cfg */
wc = ff_uni2oem(wc, CODEPAGE); /* Unicode ==> ANSI/OEM code */
if (wc & 0x80) wc = ExCvt[wc & 0x7F]; /* Convert extended character to upper (SBCS) */
- } else { /* At DBCS */
- wc = ff_uni2oem(ff_wtoupper(wc), CODEPAGE); /* Unicode ==> Upper convert ==> ANSI/OEM code */
+ } else { /* In DBCS cfg */
+ wc = ff_uni2oem(ff_wtoupper(wc), CODEPAGE); /* Unicode ==> Up-convert ==> ANSI/OEM code */
}
-#elif FF_CODE_PAGE < 900 /* SBCS cfg */
+#elif FF_CODE_PAGE < 900 /* In SBCS cfg */
wc = ff_uni2oem(wc, CODEPAGE); /* Unicode ==> ANSI/OEM code */
if (wc & 0x80) wc = ExCvt[wc & 0x7F]; /* Convert extended character to upper (SBCS) */
-#else /* DBCS cfg */
- wc = ff_uni2oem(ff_wtoupper(wc), CODEPAGE); /* Unicode ==> Upper convert ==> ANSI/OEM code */
+#else /* In DBCS cfg */
+ wc = ff_uni2oem(ff_wtoupper(wc), CODEPAGE); /* Unicode ==> Up-convert ==> ANSI/OEM code */
#endif
}
@@ -2912,7 +2948,7 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr
}
dp->fn[i++] = (BYTE)(wc >> 8); /* Put 1st byte */
} else { /* SBC */
- if (wc == 0 || chk_chr("+,;=[]", wc)) { /* Replace illegal characters for SFN if needed */
+ if (wc == 0 || strchr("+,;=[]", (int)wc)) { /* Replace illegal characters for SFN */
wc = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */
} else {
if (IsUpper(wc)) { /* ASCII upper case? */
@@ -2941,13 +2977,14 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr
#else /* FF_USE_LFN : Non-LFN configuration */
- BYTE c, d, *sfn;
+ BYTE c, d;
+ BYTE *sfn;
UINT ni, si, i;
const char *p;
/* Create file name in directory form */
p = *path; sfn = dp->fn;
- mem_set(sfn, ' ', 11);
+ memset(sfn, ' ', 11);
si = i = 0; ni = 8;
#if FF_FS_RPATH != 0
if (p[si] == '.') { /* Is this a dot entry? */
@@ -2956,8 +2993,8 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr
if (c != '.' || si >= 3) break;
sfn[i++] = c;
}
- if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;
- *path = p + si; /* Return pointer to the next segment */
+ if (!IsSeparator(c) && c > ' ') return FR_INVALID_NAME;
+ *path = p + si; /* Return pointer to the next segment */
sfn[NSFLAG] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of the path */
return FR_OK;
}
@@ -2965,8 +3002,8 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr
for (;;) {
c = (BYTE)p[si++]; /* Get a byte */
if (c <= ' ') break; /* Break if end of the path name */
- if (c == '/' || c == '\\') { /* Break if a separator is found */
- while (p[si] == '/' || p[si] == '\\') si++; /* Skip duplicated separator if exist */
+ if (IsSeparator(c)) { /* Break if a separator is found */
+ while (IsSeparator(p[si])) si++; /* Skip duplicated separator if exist */
break;
}
if (c == '.' || i >= ni) { /* End of body or field overflow? */
@@ -2989,16 +3026,16 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr
sfn[i++] = c;
sfn[i++] = d;
} else { /* SBC */
- if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) return FR_INVALID_NAME; /* Reject illegal chrs for SFN */
+ if (strchr("*+,:;<=>[]|\"\?\x7F", (int)c)) return FR_INVALID_NAME; /* Reject illegal chrs for SFN */
if (IsLower(c)) c -= 0x20; /* To upper */
sfn[i++] = c;
}
}
- *path = p + si; /* Return pointer to the next segment */
+ *path = &p[si]; /* Return pointer to the next segment */
if (i == 0) return FR_INVALID_NAME; /* Reject nul string */
if (sfn[0] == DDEM) sfn[0] = RDDEM; /* If the first character collides with DDEM, replace it with RDDEM */
- sfn[NSFLAG] = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of the path */
+ sfn[NSFLAG] = (c <= ' ' || p[si] <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of the path */
return FR_OK;
#endif /* FF_USE_LFN */
@@ -3022,13 +3059,13 @@ static FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
#if FF_FS_RPATH != 0
- if (*path != '/' && *path != '\\') { /* Without heading separator */
- dp->obj.sclust = fs->cdir; /* Start from current directory */
+ if (!IsSeparator(*path) && (FF_STR_VOLUME_ID != 2 || !IsTerminator(*path))) { /* Without heading separator */
+ dp->obj.sclust = fs->cdir; /* Start at the current directory */
} else
#endif
{ /* With heading separator */
- while (*path == '/' || *path == '\\') path++; /* Strip heading separator */
- dp->obj.sclust = 0; /* Start from root directory */
+ while (IsSeparator(*path)) path++; /* Strip separators */
+ dp->obj.sclust = 0; /* Start from the root directory */
}
#if FF_FS_EXFAT
dp->obj.n_frag = 0; /* Invalidate last fragment counter of the object */
@@ -3069,13 +3106,13 @@ static FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
}
break;
}
- if (ns & NS_LAST) break; /* Last segment matched. Function completed. */
+ if (ns & NS_LAST) break; /* Last segment matched. Function completed. */
/* Get into the sub-directory */
- if (!(dp->obj.attr & AM_DIR)) { /* It is not a sub-directory and cannot follow */
+ if (!(dp->obj.attr & AM_DIR)) { /* It is not a sub-directory and cannot follow */
res = FR_NO_PATH; break;
}
#if FF_FS_EXFAT
- if (fs->fs_type == FS_EXFAT) { /* Save containing directory information for next dir */
+ if (fs->fs_type == FS_EXFAT) { /* Save containing directory information for next dir */
dp->obj.c_scl = dp->obj.sclust;
dp->obj.c_size = ((DWORD)dp->obj.objsize & 0xFFFFFF00) | dp->obj.stat;
dp->obj.c_ofs = dp->blk_ofs;
@@ -3102,9 +3139,11 @@ static int get_ldnumber ( /* Returns logical drive number (-1:invalid drive numb
const TCHAR** path /* Pointer to pointer to the path name */
)
{
- const TCHAR *tp, *tt;
+ const TCHAR *tp;
+ const TCHAR *tt;
TCHAR tc;
- int i, vol = -1;
+ int i;
+ int vol = -1;
#if FF_STR_VOLUME_ID /* Find string volume ID */
const char *sp;
char c;
@@ -3112,7 +3151,9 @@ static int get_ldnumber ( /* Returns logical drive number (-1:invalid drive numb
tt = tp = *path;
if (!tp) return vol; /* Invalid path name? */
- do tc = *tt++; while ((UINT)tc >= (FF_USE_LFN ? ' ' : '!') && tc != ':'); /* Find a colon in the path */
+ do { /* Find a colon in the path */
+ tc = *tt++;
+ } while (!IsTerminator(tc) && tc != ':');
if (tc == ':') { /* DOS/Windows style volume ID? */
i = FF_VOLUMES;
@@ -3139,21 +3180,22 @@ static int get_ldnumber ( /* Returns logical drive number (-1:invalid drive numb
return vol;
}
#if FF_STR_VOLUME_ID == 2 /* Unix style volume ID is enabled */
- if (*tp == '/') {
+ if (*tp == '/') { /* Is there a volume ID? */
+ while (*(tp + 1) == '/') tp++; /* Skip duplicated separator */
i = 0;
do {
- sp = VolumeStr[i]; tp = *path; /* This string volume ID and path name */
+ tt = tp; sp = VolumeStr[i]; /* Path name and this string volume ID */
do { /* Compare the volume ID with path name */
- c = *sp++; tc = *(++tp);
+ c = *sp++; tc = *(++tt);
if (IsLower(c)) c -= 0x20;
if (IsLower(tc)) tc -= 0x20;
} while (c && (TCHAR)c == tc);
- } while ((c || (tc != '/' && (UINT)tc >= (FF_USE_LFN ? ' ' : '!'))) && ++i < FF_VOLUMES); /* Repeat for each ID until pattern match */
+ } while ((c || (tc != '/' && !IsTerminator(tc))) && ++i < FF_VOLUMES); /* Repeat for each ID until pattern match */
if (i < FF_VOLUMES) { /* If a volume ID is found, get the drive number and strip it */
vol = i; /* Drive number */
- *path = tp; /* Snip the drive prefix off */
- return vol;
+ *path = tt; /* Snip the drive prefix off */
}
+ return vol;
}
#endif
/* No drive prefix is found */
@@ -3169,27 +3211,168 @@ static int get_ldnumber ( /* Returns logical drive number (-1:invalid drive numb
/*-----------------------------------------------------------------------*/
+/* GPT support functions */
+/*-----------------------------------------------------------------------*/
+
+#if FF_LBA64
+
+/* Calculate CRC32 in byte-by-byte */
+
+static DWORD crc32 ( /* Returns next CRC value */
+ DWORD crc, /* Current CRC value */
+ BYTE d /* A byte to be processed */
+)
+{
+ BYTE b;
+
+
+ for (b = 1; b; b <<= 1) {
+ crc ^= (d & b) ? 1 : 0;
+ crc = (crc & 1) ? crc >> 1 ^ 0xEDB88320 : crc >> 1;
+ }
+ return crc;
+}
+
+
+/* Check validity of GPT header */
+
+static int test_gpt_header ( /* 0:Invalid, 1:Valid */
+ const BYTE* gpth /* Pointer to the GPT header */
+)
+{
+ UINT i;
+ DWORD bcc, hlen;
+
+
+ if (memcmp(gpth + GPTH_Sign, "EFI PART" "\0\0\1", 12)) return 0; /* Check signature and version (1.0) */
+ hlen = ld_dword(gpth + GPTH_Size); /* Check header size */
+ if (hlen < 92 || hlen > FF_MIN_SS) return 0;
+ for (i = 0, bcc = 0xFFFFFFFF; i < hlen; i++) { /* Check header BCC */
+ bcc = crc32(bcc, i - GPTH_Bcc < 4 ? 0 : gpth[i]);
+ }
+ if (~bcc != ld_dword(gpth + GPTH_Bcc)) return 0;
+ if (ld_dword(gpth + GPTH_PteSize) != SZ_GPTE) return 0; /* Table entry size (must be SZ_GPTE bytes) */
+ if (ld_dword(gpth + GPTH_PtNum) > 128) return 0; /* Table size (must be 128 entries or less) */
+
+ return 1;
+}
+
+#if !FF_FS_READONLY && FF_USE_MKFS
+
+/* Generate random value */
+static DWORD make_rand (
+ DWORD seed, /* Seed value */
+ BYTE *buff, /* Output buffer */
+ UINT n /* Data length */
+)
+{
+ UINT r;
+
+
+ if (seed == 0) seed = 1;
+ do {
+ for (r = 0; r < 8; r++) seed = seed & 1 ? seed >> 1 ^ 0xA3000000 : seed >> 1; /* Shift 8 bits the 32-bit LFSR */
+ *buff++ = (BYTE)seed;
+ } while (--n);
+ return seed;
+}
+
+#endif
+#endif
+
+
+
+/*-----------------------------------------------------------------------*/
/* Load a sector and check if it is an FAT VBR */
/*-----------------------------------------------------------------------*/
-static BYTE check_fs ( /* 0:FAT, 1:exFAT, 2:Valid BS but not FAT, 3:Not a BS, 4:Disk error */
+/* Check what the sector is */
+
+static UINT check_fs ( /* 0:FAT/FAT32 VBR, 1:exFAT VBR, 2:Not FAT and valid BS, 3:Not FAT and invalid BS, 4:Disk error */
FATFS* fs, /* Filesystem object */
- DWORD sect /* Sector# (lba) to load and check if it is an FAT-VBR or not */
+ LBA_t sect /* Sector to load and check if it is an FAT-VBR or not */
)
{
- fs->wflag = 0; fs->winsect = 0xFFFFFFFF; /* Invaidate window */
- if (move_window(fs, sect) != FR_OK) return 4; /* Load boot record */
+ WORD w, sign;
+ BYTE b;
- if (ld_word(fs->win + BS_55AA) != 0xAA55) return 3; /* Check boot record signature (always here regardless of the sector size) */
+ fs->wflag = 0; fs->winsect = (LBA_t)0 - 1; /* Invaidate window */
+ if (move_window(fs, sect) != FR_OK) return 4; /* Load the boot sector */
+ sign = ld_word(fs->win + BS_55AA);
#if FF_FS_EXFAT
- if (!mem_cmp(fs->win + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11)) return 1; /* Check if exFAT VBR */
+ if (sign == 0xAA55 && !memcmp(fs->win + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11)) return 1; /* It is an exFAT VBR */
+#endif
+ b = fs->win[BS_JmpBoot];
+ if (b == 0xEB || b == 0xE9 || b == 0xE8) { /* Valid JumpBoot code? (short jump, near jump or near call) */
+ if (sign == 0xAA55 && !memcmp(fs->win + BS_FilSysType32, "FAT32 ", 8)) {
+ return 0; /* It is an FAT32 VBR */
+ }
+ /* FAT volumes formatted with early MS-DOS lack BS_55AA and BS_FilSysType, so FAT VBR needs to be identified without them. */
+ w = ld_word(fs->win + BPB_BytsPerSec);
+ b = fs->win[BPB_SecPerClus];
+ if ((w & (w - 1)) == 0 && w >= FF_MIN_SS && w <= FF_MAX_SS /* Properness of sector size (512-4096 and 2^n) */
+ && b != 0 && (b & (b - 1)) == 0 /* Properness of cluster size (2^n) */
+ && ld_word(fs->win + BPB_RsvdSecCnt) != 0 /* Properness of reserved sectors (MNBZ) */
+ && (UINT)fs->win[BPB_NumFATs] - 1 <= 1 /* Properness of FATs (1 or 2) */
+ && ld_word(fs->win + BPB_RootEntCnt) != 0 /* Properness of root dir entries (MNBZ) */
+ && (ld_word(fs->win + BPB_TotSec16) >= 128 || ld_dword(fs->win + BPB_TotSec32) >= 0x10000) /* Properness of volume sectors (>=128) */
+ && ld_word(fs->win + BPB_FATSz16) != 0) { /* Properness of FAT size (MNBZ) */
+ return 0; /* It can be presumed an FAT VBR */
+ }
+ }
+ return sign == 0xAA55 ? 2 : 3; /* Not an FAT VBR (valid or invalid BS) */
+}
+
+
+/* Find an FAT volume */
+/* (It supports only generic partitioning rules, MBR, GPT and SFD) */
+
+static UINT find_volume ( /* Returns BS status found in the hosting drive */
+ FATFS* fs, /* Filesystem object */
+ UINT part /* Partition to fined = 0:find as SFD and partitions, >0:forced partition number */
+)
+{
+ UINT fmt, i;
+ DWORD mbr_pt[4];
+
+
+ fmt = check_fs(fs, 0); /* Load sector 0 and check if it is an FAT VBR as SFD format */
+ if (fmt != 2 && (fmt >= 3 || part == 0)) return fmt; /* Returns if it is an FAT VBR as auto scan, not a BS or disk error */
+
+ /* Sector 0 is not an FAT VBR or forced partition number wants a partition */
+
+#if FF_LBA64
+ if (fs->win[MBR_Table + PTE_System] == 0xEE) { /* GPT protective MBR? */
+ DWORD n_ent, v_ent, ofs;
+ QWORD pt_lba;
+
+ if (move_window(fs, 1) != FR_OK) return 4; /* Load GPT header sector (next to MBR) */
+ if (!test_gpt_header(fs->win)) return 3; /* Check if GPT header is valid */
+ n_ent = ld_dword(fs->win + GPTH_PtNum); /* Number of entries */
+ pt_lba = ld_qword(fs->win + GPTH_PtOfs); /* Table location */
+ for (v_ent = i = 0; i < n_ent; i++) { /* Find FAT partition */
+ if (move_window(fs, pt_lba + i * SZ_GPTE / SS(fs)) != FR_OK) return 4; /* PT sector */
+ ofs = i * SZ_GPTE % SS(fs); /* Offset in the sector */
+ if (!memcmp(fs->win + ofs + GPTE_PtGuid, GUID_MS_Basic, 16)) { /* MS basic data partition? */
+ v_ent++;
+ fmt = check_fs(fs, ld_qword(fs->win + ofs + GPTE_FstLba)); /* Load VBR and check status */
+ if (part == 0 && fmt <= 1) return fmt; /* Auto search (valid FAT volume found first) */
+ if (part != 0 && v_ent == part) return fmt; /* Forced partition order (regardless of it is valid or not) */
+ }
+ }
+ return 3; /* Not found */
+ }
#endif
- if (fs->win[BS_JmpBoot] == 0xE9 || fs->win[BS_JmpBoot] == 0xEB || fs->win[BS_JmpBoot] == 0xE8) { /* Valid JumpBoot code? */
- if (!mem_cmp(fs->win + BS_FilSysType, "FAT", 3)) return 0; /* Is it an FAT VBR? */
- if (!mem_cmp(fs->win + BS_FilSysType32, "FAT32", 5)) return 0; /* Is it an FAT32 VBR? */
+ if (FF_MULTI_PARTITION && part > 4) return 3; /* MBR has 4 partitions max */
+ for (i = 0; i < 4; i++) { /* Load partition offset in the MBR */
+ mbr_pt[i] = ld_dword(fs->win + MBR_Table + i * SZ_PTE + PTE_StLba);
}
- return 2; /* Valid BS but not FAT */
+ i = part ? part - 1 : 0; /* Table index to find first */
+ do { /* Find an FAT volume */
+ fmt = mbr_pt[i] ? check_fs(fs, mbr_pt[i]) : 3; /* Check if the partition is FAT */
+ } while (part == 0 && fmt >= 2 && ++i < 4);
+ return fmt;
}
@@ -3199,19 +3382,19 @@ static BYTE check_fs ( /* 0:FAT, 1:exFAT, 2:Valid BS but not FAT, 3:Not a BS, 4:
/* Determine logical drive number and mount the volume if needed */
/*-----------------------------------------------------------------------*/
-static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */
+static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */
const TCHAR** path, /* Pointer to pointer to the path name (drive number) */
FATFS** rfs, /* Pointer to pointer to the found filesystem object */
- BYTE mode /* !=0: Check write protection for write access */
+ BYTE mode /* Desiered access mode to check write protection */
)
{
- BYTE fmt, *pt;
int vol;
+ FATFS *fs;
DSTATUS stat;
- DWORD bsect, fasize, tsect, sysect, nclst, szbfat, br[4];
+ LBA_t bsect;
+ DWORD tsect, sysect, fasize, nclst, szbfat;
WORD nrsv;
- FATFS *fs;
- UINT i;
+ UINT fmt;
/* Get logical drive number */
@@ -3223,7 +3406,7 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */
fs = FatFs[vol]; /* Get pointer to the filesystem object */
if (!fs) return FR_NOT_ENABLED; /* Is the filesystem object available? */
#if FF_FS_REENTRANT
- if (!lock_fs(fs)) return FR_TIMEOUT; /* Lock the volume */
+ if (!lock_volume(fs, 1)) return FR_TIMEOUT; /* Lock the volume, and system if needed */
#endif
*rfs = fs; /* Return pointer to the filesystem object */
@@ -3234,16 +3417,15 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */
if (!FF_FS_READONLY && mode && (stat & STA_PROTECT)) { /* Check write protection if needed */
return FR_WRITE_PROTECTED;
}
- return FR_OK; /* The filesystem object is valid */
+ return FR_OK; /* The filesystem object is already valid */
}
}
/* The filesystem object is not valid. */
- /* Following code attempts to mount the volume. (analyze BPB and initialize the filesystem object) */
+ /* Following code attempts to mount the volume. (find an FAT volume, analyze the BPB and initialize the filesystem object) */
- fs->fs_type = 0; /* Clear the filesystem object */
- fs->pdrv = LD2PD(vol); /* Bind the logical drive and a physical drive */
- stat = disk_initialize(fs->pdrv); /* Initialize the physical drive */
+ fs->fs_type = 0; /* Invalidate the filesystem object */
+ stat = disk_initialize(fs->pdrv); /* Initialize the volume hosting physical drive */
if (stat & STA_NOINIT) { /* Check if the initialization succeeded */
return FR_NOT_READY; /* Failed to initialize due to no medium or hard error */
}
@@ -3255,29 +3437,18 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */
if (SS(fs) > FF_MAX_SS || SS(fs) < FF_MIN_SS || (SS(fs) & (SS(fs) - 1))) return FR_DISK_ERR;
#endif
- /* Find an FAT partition on the drive. Supports only generic partitioning rules, FDISK and SFD. */
- bsect = 0;
- fmt = check_fs(fs, bsect); /* Load sector 0 and check if it is an FAT-VBR as SFD */
- if (fmt == 2 || (fmt < 2 && LD2PT(vol) != 0)) { /* Not an FAT-VBR or forced partition number */
- for (i = 0; i < 4; i++) { /* Get partition offset */
- pt = fs->win + (MBR_Table + i * SZ_PTE);
- br[i] = pt[PTE_System] ? ld_dword(pt + PTE_StLba) : 0;
- }
- i = LD2PT(vol); /* Partition number: 0:auto, 1-4:forced */
- if (i != 0) i--;
- do { /* Find an FAT volume */
- bsect = br[i];
- fmt = bsect ? check_fs(fs, bsect) : 3; /* Check the partition */
- } while (LD2PT(vol) == 0 && fmt >= 2 && ++i < 4);
- }
- if (fmt == 4) return FR_DISK_ERR; /* An error occured in the disk I/O layer */
+ /* Find an FAT volume on the hosting drive */
+ fmt = find_volume(fs, LD2PT(vol));
+ if (fmt == 4) return FR_DISK_ERR; /* An error occurred in the disk I/O layer */
if (fmt >= 2) return FR_NO_FILESYSTEM; /* No FAT volume is found */
+ bsect = fs->winsect; /* Volume offset in the hosting physical drive */
/* An FAT volume is found (bsect). Following code initializes the filesystem object */
#if FF_FS_EXFAT
if (fmt == 1) {
QWORD maxlba;
+ DWORD so, cv, bcl, i;
for (i = BPB_ZeroedEx; i < BPB_ZeroedEx + 53 && fs->win[i] == 0; i++) ; /* Check zero filler */
if (i < BPB_ZeroedEx + 53) return FR_NO_FILESYSTEM;
@@ -3288,8 +3459,8 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */
return FR_NO_FILESYSTEM;
}
- maxlba = ld_qword(fs->win + BPB_TotSecEx) + bsect; /* Last LBA + 1 of the volume */
- if (maxlba >= 0x100000000) return FR_NO_FILESYSTEM; /* (It cannot be handled in 32-bit LBA) */
+ maxlba = ld_qword(fs->win + BPB_TotSecEx) + bsect; /* Last LBA of the volume + 1 */
+ if (!FF_LBA64 && maxlba >= 0x100000000) return FR_NO_FILESYSTEM; /* (It cannot be accessed in 32-bit LBA) */
fs->fsize = ld_dword(fs->win + BPB_FatSzEx); /* Number of sectors per FAT */
@@ -3297,7 +3468,7 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */
if (fs->n_fats != 1) return FR_NO_FILESYSTEM; /* (Supports only 1 FAT) */
fs->csize = 1 << fs->win[BPB_SecPerClusEx]; /* Cluster size */
- if (fs->csize == 0) return FR_NO_FILESYSTEM; /* (Must be 1..32768) */
+ if (fs->csize == 0) return FR_NO_FILESYSTEM; /* (Must be 1..32768 sectors) */
nclst = ld_dword(fs->win + BPB_NumClusEx); /* Number of clusters */
if (nclst > MAX_EXFAT) return FR_NO_FILESYSTEM; /* (Too many clusters) */
@@ -3307,15 +3478,30 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */
fs->volbase = bsect;
fs->database = bsect + ld_dword(fs->win + BPB_DataOfsEx);
fs->fatbase = bsect + ld_dword(fs->win + BPB_FatOfsEx);
- if (maxlba < (QWORD)fs->database + nclst * fs->csize) return FR_NO_FILESYSTEM; /* (Volume size must not be smaller than the size requiered) */
+ if (maxlba < (QWORD)fs->database + nclst * fs->csize) return FR_NO_FILESYSTEM; /* (Volume size must not be smaller than the size required) */
fs->dirbase = ld_dword(fs->win + BPB_RootClusEx);
- /* Check if bitmap location is in assumption (at the first cluster) */
- if (move_window(fs, clst2sect(fs, fs->dirbase)) != FR_OK) return FR_DISK_ERR;
- for (i = 0; i < SS(fs); i += SZDIRE) {
- if (fs->win[i] == 0x81 && ld_dword(fs->win + i + 20) == 2) break; /* 81 entry with cluster #2? */
+ /* Get bitmap location and check if it is contiguous (implementation assumption) */
+ so = i = 0;
+ for (;;) { /* Find the bitmap entry in the root directory (in only first cluster) */
+ if (i == 0) {
+ if (so >= fs->csize) return FR_NO_FILESYSTEM; /* Not found? */
+ if (move_window(fs, clst2sect(fs, (DWORD)fs->dirbase) + so) != FR_OK) return FR_DISK_ERR;
+ so++;
+ }
+ if (fs->win[i] == ET_BITMAP) break; /* Is it a bitmap entry? */
+ i = (i + SZDIRE) % SS(fs); /* Next entry */
+ }
+ bcl = ld_dword(fs->win + i + 20); /* Bitmap cluster */
+ if (bcl < 2 || bcl >= fs->n_fatent) return FR_NO_FILESYSTEM; /* (Wrong cluster#) */
+ fs->bitbase = fs->database + fs->csize * (bcl - 2); /* Bitmap sector */
+ for (;;) { /* Check if bitmap is contiguous */
+ if (move_window(fs, fs->fatbase + bcl / (SS(fs) / 4)) != FR_OK) return FR_DISK_ERR;
+ cv = ld_dword(fs->win + bcl % (SS(fs) / 4) * 4);
+ if (cv == 0xFFFFFFFF) break; /* Last link? */
+ if (cv != ++bcl) return FR_NO_FILESYSTEM; /* Fragmented bitmap? */
}
- if (i == SS(fs)) return FR_NO_FILESYSTEM;
+
#if !FF_FS_READONLY
fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Initialize cluster allocation information */
#endif
@@ -3400,7 +3586,7 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */
#endif /* !FF_FS_READONLY */
}
- fs->fs_type = fmt; /* FAT sub-type */
+ fs->fs_type = (BYTE)fmt;/* FAT sub-type (the filesystem object gets valid) */
fs->id = ++Fsid; /* Volume mount ID */
#if FF_USE_LFN == 1
fs->lfnbuf = LfnBuf; /* Static LFN working buffer */
@@ -3411,8 +3597,8 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */
#if FF_FS_RPATH != 0
fs->cdir = 0; /* Initialize current directory */
#endif
-#if FF_FS_LOCK != 0 /* Clear file lock semaphores */
- clear_lock(fs);
+#if FF_FS_LOCK /* Clear file lock semaphores */
+ clear_share(fs);
#endif
return FR_OK;
}
@@ -3425,7 +3611,7 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */
/*-----------------------------------------------------------------------*/
static FRESULT validate ( /* Returns FR_OK or FR_INVALID_OBJECT */
- FFOBJID* obj, /* Pointer to the FFOBJID, the 1st member in the FIL/DIR object, to check validity */
+ FFOBJID* obj, /* Pointer to the FFOBJID, the 1st member in the FIL/DIR structure, to check validity */
FATFS** rfs /* Pointer to pointer to the owner filesystem object to return */
)
{
@@ -3434,22 +3620,22 @@ static FRESULT validate ( /* Returns FR_OK or FR_INVALID_OBJECT */
if (obj && obj->fs && obj->fs->fs_type && obj->id == obj->fs->id) { /* Test if the object is valid */
#if FF_FS_REENTRANT
- if (lock_fs(obj->fs)) { /* Obtain the filesystem object */
- if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the phsical drive is kept initialized */
+ if (lock_volume(obj->fs, 0)) { /* Take a grant to access the volume */
+ if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the hosting phsical drive is kept initialized */
res = FR_OK;
} else {
- unlock_fs(obj->fs, FR_OK);
+ unlock_volume(obj->fs, FR_OK); /* Invalidated volume, abort to access */
}
- } else {
+ } else { /* Could not take */
res = FR_TIMEOUT;
}
#else
- if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the phsical drive is kept initialized */
+ if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the hosting phsical drive is kept initialized */
res = FR_OK;
}
#endif
}
- *rfs = (res == FR_OK) ? obj->fs : 0; /* Corresponding filesystem object */
+ *rfs = (res == FR_OK) ? obj->fs : 0; /* Return corresponding filesystem object if it is valid */
return res;
}
@@ -3469,9 +3655,9 @@ static FRESULT validate ( /* Returns FR_OK or FR_INVALID_OBJECT */
/*-----------------------------------------------------------------------*/
FRESULT f_mount (
- FATFS* fs, /* Pointer to the filesystem object (NULL:unmount)*/
+ FATFS* fs, /* Pointer to the filesystem object to be registered (NULL:unmount)*/
const TCHAR* path, /* Logical drive number to be mounted/unmounted */
- BYTE opt /* Mode option 0:Do not mount (delayed mount), 1:Mount immediately */
+ BYTE opt /* Mount option: 0=Do not mount (delayed mount), 1=Mount immediately */
)
{
FATFS *cfs;
@@ -3480,32 +3666,44 @@ FRESULT f_mount (
const TCHAR *rp = path;
- /* Get logical drive number */
+ /* Get volume ID (logical drive number) */
vol = get_ldnumber(&rp);
if (vol < 0) return FR_INVALID_DRIVE;
- cfs = FatFs[vol]; /* Pointer to fs object */
+ cfs = FatFs[vol]; /* Pointer to the filesystem object of the volume */
- if (cfs) {
-#if FF_FS_LOCK != 0
- clear_lock(cfs);
+ if (cfs) { /* Unregister current filesystem object if regsitered */
+ FatFs[vol] = 0;
+#if FF_FS_LOCK
+ clear_share(cfs);
#endif
-#if FF_FS_REENTRANT /* Discard sync object of the current volume */
- if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR;
+#if FF_FS_REENTRANT /* Discard mutex of the current volume */
+ ff_mutex_delete(vol);
#endif
- cfs->fs_type = 0; /* Clear old fs object */
+ cfs->fs_type = 0; /* Invalidate the filesystem object to be unregistered */
}
- if (fs) {
- fs->fs_type = 0; /* Clear new fs object */
-#if FF_FS_REENTRANT /* Create sync object for the new volume */
- if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR;
+ if (fs) { /* Register new filesystem object */
+ fs->pdrv = LD2PD(vol); /* Volume hosting physical drive */
+#if FF_FS_REENTRANT /* Create a volume mutex */
+ fs->ldrv = (BYTE)vol; /* Owner volume ID */
+ if (!ff_mutex_create(vol)) return FR_INT_ERR;
+#if FF_FS_LOCK
+ if (SysLock == 0) { /* Create a system mutex if needed */
+ if (!ff_mutex_create(FF_VOLUMES)) {
+ ff_mutex_delete(vol);
+ return FR_INT_ERR;
+ }
+ SysLock = 1; /* System mutex is ready */
+ }
+#endif
#endif
+ fs->fs_type = 0; /* Invalidate the new filesystem object */
+ FatFs[vol] = fs; /* Register new fs object */
}
- FatFs[vol] = fs; /* Register new fs object */
- if (opt == 0) return FR_OK; /* Do not mount now, it will be mounted later */
+ if (opt == 0) return FR_OK; /* Do not mount now, it will be mounted in subsequent file functions */
- res = find_volume(&path, &fs, 0); /* Force mounted the volume */
+ res = mount_volume(&path, &fs, 0); /* Force mounted the volume */
LEAVE_FF(fs, res);
}
@@ -3519,14 +3717,15 @@ FRESULT f_mount (
FRESULT f_open (
FIL* fp, /* Pointer to the blank file object */
const TCHAR* path, /* Pointer to the file name */
- BYTE mode /* Access mode and file open mode flags */
+ BYTE mode /* Access mode and open mode flags */
)
{
FRESULT res;
DIR dj;
FATFS *fs;
#if !FF_FS_READONLY
- DWORD dw, cl, bcs, clst, sc;
+ DWORD cl, bcs, clst, tm;
+ LBA_t sc;
FSIZE_t ofs;
#endif
DEF_NAMBUF
@@ -3536,7 +3735,7 @@ FRESULT f_open (
/* Get logical drive number */
mode &= FF_FS_READONLY ? FA_READ : FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_CREATE_NEW | FA_OPEN_ALWAYS | FA_OPEN_APPEND;
- res = find_volume(&path, &fs, mode);
+ res = mount_volume(&path, &fs, mode);
if (res == FR_OK) {
dj.obj.fs = fs;
INIT_NAMBUF(fs);
@@ -3546,9 +3745,9 @@ FRESULT f_open (
if (dj.fn[NSFLAG] & NS_NONAME) { /* Origin directory itself? */
res = FR_INVALID_NAME;
}
-#if FF_FS_LOCK != 0
+#if FF_FS_LOCK
else {
- res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0); /* Check if the file can be used */
+ res = chk_share(&dj, (mode & ~FA_READ) ? 1 : 0); /* Check if the file can be used */
}
#endif
}
@@ -3556,8 +3755,8 @@ FRESULT f_open (
if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
if (res != FR_OK) { /* No file, create new */
if (res == FR_NO_FILE) { /* There is no file to open, create a new entry */
-#if FF_FS_LOCK != 0
- res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
+#if FF_FS_LOCK
+ res = enq_share() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
#else
res = dir_register(&dj);
#endif
@@ -3578,8 +3777,8 @@ FRESULT f_open (
fp->obj.fs = fs;
init_alloc_info(fs, &fp->obj);
/* Set directory entry block initial state */
- mem_set(fs->dirbuf + 2, 0, 30); /* Clear 85 entry except for NumSec */
- mem_set(fs->dirbuf + 38, 0, 26); /* Clear C0 entry except for NumName and NameHash */
+ memset(fs->dirbuf + 2, 0, 30); /* Clear 85 entry except for NumSec */
+ memset(fs->dirbuf + 38, 0, 26); /* Clear C0 entry except for NumName and NameHash */
fs->dirbuf[XDIR_Attr] = AM_ARC;
st_dword(fs->dirbuf + XDIR_CrtTime, GET_FATTIME());
fs->dirbuf[XDIR_GenFlags] = 1;
@@ -3592,17 +3791,19 @@ FRESULT f_open (
#endif
{
/* Set directory entry initial state */
+ tm = GET_FATTIME(); /* Set created time */
+ st_dword(dj.dir + DIR_CrtTime, tm);
+ st_dword(dj.dir + DIR_ModTime, tm);
cl = ld_clust(fs, dj.dir); /* Get current cluster chain */
- st_dword(dj.dir + DIR_CrtTime, GET_FATTIME()); /* Set created time */
dj.dir[DIR_Attr] = AM_ARC; /* Reset attribute */
st_clust(fs, dj.dir, 0); /* Reset file allocation info */
st_dword(dj.dir + DIR_FileSize, 0);
fs->wflag = 1;
if (cl != 0) { /* Remove the cluster chain if exist */
- dw = fs->winsect;
+ sc = fs->winsect;
res = remove_chain(&dj.obj, cl, 0);
if (res == FR_OK) {
- res = move_window(fs, dw);
+ res = move_window(fs, sc);
fs->last_clst = cl - 1; /* Reuse the cluster hole */
}
}
@@ -3624,8 +3825,8 @@ FRESULT f_open (
if (mode & FA_CREATE_ALWAYS) mode |= FA_MODIFIED; /* Set file change flag if created or overwritten */
fp->dir_sect = fs->winsect; /* Pointer to the directory entry */
fp->dir_ptr = dj.dir;
-#if FF_FS_LOCK != 0
- fp->obj.lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0); /* Lock the file for this session */
+#if FF_FS_LOCK
+ fp->obj.lockid = inc_share(&dj, (mode & ~FA_READ) ? 1 : 0); /* Lock the file for this session */
if (fp->obj.lockid == 0) res = FR_INT_ERR;
#endif
}
@@ -3655,17 +3856,17 @@ FRESULT f_open (
fp->obj.objsize = ld_dword(dj.dir + DIR_FileSize);
}
#if FF_USE_FASTSEEK
- fp->cltbl = 0; /* Disable fast seek mode */
+ fp->cltbl = 0; /* Disable fast seek mode */
#endif
- fp->obj.fs = fs; /* Validate the file object */
+ fp->obj.fs = fs; /* Validate the file object */
fp->obj.id = fs->id;
- fp->flag = mode; /* Set file access mode */
- fp->err = 0; /* Clear error flag */
- fp->sect = 0; /* Invalidate current data sector */
- fp->fptr = 0; /* Set file pointer top of the file */
+ fp->flag = mode; /* Set file access mode */
+ fp->err = 0; /* Clear error flag */
+ fp->sect = 0; /* Invalidate current data sector */
+ fp->fptr = 0; /* Set file pointer top of the file */
#if !FF_FS_READONLY
#if !FF_FS_TINY
- mem_set(fp->buf, 0, FF_MAX_SS); /* Clear sector buffer */
+ memset(fp->buf, 0, sizeof fp->buf); /* Clear sector buffer */
#endif
if ((mode & FA_SEEKEND) && fp->obj.objsize > 0) { /* Seek to end of file if FA_OPEN_APPEND is specified */
fp->fptr = fp->obj.objsize; /* Offset to seek */
@@ -3678,7 +3879,8 @@ FRESULT f_open (
}
fp->clust = clst;
if (res == FR_OK && ofs % SS(fs)) { /* Fill sector buffer if not on the sector boundary */
- if ((sc = clst2sect(fs, clst)) == 0) {
+ sc = clst2sect(fs, clst);
+ if (sc == 0) {
res = FR_INT_ERR;
} else {
fp->sect = sc + (DWORD)(ofs / SS(fs));
@@ -3687,6 +3889,9 @@ FRESULT f_open (
#endif
}
}
+#if FF_FS_LOCK
+ if (res != FR_OK) dec_share(fp->obj.lockid); /* Decrement file open counter if seek failed */
+#endif
}
#endif
}
@@ -3707,15 +3912,16 @@ FRESULT f_open (
/*-----------------------------------------------------------------------*/
FRESULT f_read (
- FIL* fp, /* Pointer to the file object */
- void* buff, /* Pointer to data buffer */
+ FIL* fp, /* Open file to be read */
+ void* buff, /* Data buffer to store the read data */
UINT btr, /* Number of bytes to read */
- UINT* br /* Pointer to number of bytes read */
+ UINT* br /* Number of bytes read */
)
{
FRESULT res;
FATFS *fs;
- DWORD clst, sect;
+ DWORD clst;
+ LBA_t sect;
FSIZE_t remain;
UINT rcnt, cc, csect;
BYTE *rbuff = (BYTE*)buff;
@@ -3728,8 +3934,7 @@ FRESULT f_read (
remain = fp->obj.objsize - fp->fptr;
if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
- for ( ; btr; /* Repeat until btr bytes read */
- btr -= rcnt, *br += rcnt, rbuff += rcnt, fp->fptr += rcnt) {
+ for ( ; btr > 0; btr -= rcnt, *br += rcnt, rbuff += rcnt, fp->fptr += rcnt) { /* Repeat until btr bytes read */
if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */
csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1)); /* Sector offset in the cluster */
if (csect == 0) { /* On the cluster boundary? */
@@ -3761,11 +3966,11 @@ FRESULT f_read (
#if !FF_FS_READONLY && FF_FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */
#if FF_FS_TINY
if (fs->wflag && fs->winsect - sect < cc) {
- mem_cpy(rbuff + ((fs->winsect - sect) * SS(fs)), fs->win, SS(fs));
+ memcpy(rbuff + ((fs->winsect - sect) * SS(fs)), fs->win, SS(fs));
}
#else
if ((fp->flag & FA_DIRTY) && fp->sect - sect < cc) {
- mem_cpy(rbuff + ((fp->sect - sect) * SS(fs)), fp->buf, SS(fs));
+ memcpy(rbuff + ((fp->sect - sect) * SS(fs)), fp->buf, SS(fs));
}
#endif
#endif
@@ -3780,18 +3985,18 @@ FRESULT f_read (
fp->flag &= (BYTE)~FA_DIRTY;
}
#endif
- if (disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Fill sector cache */
+ if (disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR); /* Fill sector cache */
}
#endif
fp->sect = sect;
}
- rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */
+ rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes remains in the sector */
if (rcnt > btr) rcnt = btr; /* Clip it by btr if needed */
#if FF_FS_TINY
if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window */
- mem_cpy(rbuff, fs->win + fp->fptr % SS(fs), rcnt); /* Extract partial sector */
+ memcpy(rbuff, fs->win + fp->fptr % SS(fs), rcnt); /* Extract partial sector */
#else
- mem_cpy(rbuff, fp->buf + fp->fptr % SS(fs), rcnt); /* Extract partial sector */
+ memcpy(rbuff, fp->buf + fp->fptr % SS(fs), rcnt); /* Extract partial sector */
#endif
}
@@ -3807,15 +4012,16 @@ FRESULT f_read (
/*-----------------------------------------------------------------------*/
FRESULT f_write (
- FIL* fp, /* Pointer to the file object */
- const void* buff, /* Pointer to the data to be written */
+ FIL* fp, /* Open file to be written */
+ const void* buff, /* Data to be written */
UINT btw, /* Number of bytes to write */
- UINT* bw /* Pointer to number of bytes written */
+ UINT* bw /* Number of bytes written */
)
{
FRESULT res;
FATFS *fs;
- DWORD clst, sect;
+ DWORD clst;
+ LBA_t sect;
UINT wcnt, cc, csect;
const BYTE *wbuff = (const BYTE*)buff;
@@ -3830,8 +4036,7 @@ FRESULT f_write (
btw = (UINT)(0xFFFFFFFF - (DWORD)fp->fptr);
}
- for ( ; btw; /* Repeat until all data written */
- btw -= wcnt, *bw += wcnt, wbuff += wcnt, fp->fptr += wcnt, fp->obj.objsize = (fp->fptr > fp->obj.objsize) ? fp->fptr : fp->obj.objsize) {
+ for ( ; btw > 0; btw -= wcnt, *bw += wcnt, wbuff += wcnt, fp->fptr += wcnt, fp->obj.objsize = (fp->fptr > fp->obj.objsize) ? fp->fptr : fp->obj.objsize) { /* Repeat until all data written */
if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */
csect = (UINT)(fp->fptr / SS(fs)) & (fs->csize - 1); /* Sector offset in the cluster */
if (csect == 0) { /* On the cluster boundary? */
@@ -3876,12 +4081,12 @@ FRESULT f_write (
#if FF_FS_MINIMIZE <= 2
#if FF_FS_TINY
if (fs->winsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
- mem_cpy(fs->win, wbuff + ((fs->winsect - sect) * SS(fs)), SS(fs));
+ memcpy(fs->win, wbuff + ((fs->winsect - sect) * SS(fs)), SS(fs));
fs->wflag = 0;
}
#else
if (fp->sect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
- mem_cpy(fp->buf, wbuff + ((fp->sect - sect) * SS(fs)), SS(fs));
+ memcpy(fp->buf, wbuff + ((fp->sect - sect) * SS(fs)), SS(fs));
fp->flag &= (BYTE)~FA_DIRTY;
}
#endif
@@ -3903,14 +4108,14 @@ FRESULT f_write (
#endif
fp->sect = sect;
}
- wcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */
+ wcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes remains in the sector */
if (wcnt > btw) wcnt = btw; /* Clip it by btw if needed */
#if FF_FS_TINY
if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window */
- mem_cpy(fs->win + fp->fptr % SS(fs), wbuff, wcnt); /* Fit data to the sector */
+ memcpy(fs->win + fp->fptr % SS(fs), wbuff, wcnt); /* Fit data to the sector */
fs->wflag = 1;
#else
- mem_cpy(fp->buf + fp->fptr % SS(fs), wbuff, wcnt); /* Fit data to the sector */
+ memcpy(fp->buf + fp->fptr % SS(fs), wbuff, wcnt); /* Fit data to the sector */
fp->flag |= FA_DIRTY;
#endif
}
@@ -3928,7 +4133,7 @@ FRESULT f_write (
/*-----------------------------------------------------------------------*/
FRESULT f_sync (
- FIL* fp /* Pointer to the file object */
+ FIL* fp /* Open file to be synced */
)
{
FRESULT res;
@@ -3963,9 +4168,9 @@ FRESULT f_sync (
if (res == FR_OK) {
fs->dirbuf[XDIR_Attr] |= AM_ARC; /* Set archive attribute to indicate that the file has been changed */
fs->dirbuf[XDIR_GenFlags] = fp->obj.stat | 1; /* Update file allocation information */
- st_dword(fs->dirbuf + XDIR_FstClus, fp->obj.sclust);
- st_qword(fs->dirbuf + XDIR_FileSize, fp->obj.objsize);
- st_qword(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize);
+ st_dword(fs->dirbuf + XDIR_FstClus, fp->obj.sclust); /* Update start cluster */
+ st_qword(fs->dirbuf + XDIR_FileSize, fp->obj.objsize); /* Update file size */
+ st_qword(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize); /* (FatFs does not support Valid File Size feature) */
st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Update modified time */
fs->dirbuf[XDIR_ModTime10] = 0;
st_dword(fs->dirbuf + XDIR_AccTime, 0);
@@ -4009,7 +4214,7 @@ FRESULT f_sync (
/*-----------------------------------------------------------------------*/
FRESULT f_close (
- FIL* fp /* Pointer to the file object to be closed */
+ FIL* fp /* Open file to be closed */
)
{
FRESULT res;
@@ -4022,14 +4227,14 @@ FRESULT f_close (
{
res = validate(&fp->obj, &fs); /* Lock volume */
if (res == FR_OK) {
-#if FF_FS_LOCK != 0
- res = dec_lock(fp->obj.lockid); /* Decrement file open counter */
+#if FF_FS_LOCK
+ res = dec_share(fp->obj.lockid); /* Decrement file open counter */
if (res == FR_OK) fp->obj.fs = 0; /* Invalidate file object */
#else
fp->obj.fs = 0; /* Invalidate file object */
#endif
#if FF_FS_REENTRANT
- unlock_fs(fs, FR_OK); /* Unlock volume */
+ unlock_volume(fs, FR_OK); /* Unlock volume */
#endif
}
}
@@ -4075,7 +4280,7 @@ FRESULT f_chdir (
/* Get logical drive */
- res = find_volume(&path, &fs, 0);
+ res = mount_volume(&path, &fs, 0);
if (res == FR_OK) {
dj.obj.fs = fs;
INIT_NAMBUF(fs);
@@ -4110,7 +4315,7 @@ FRESULT f_chdir (
}
FREE_NAMBUF();
if (res == FR_NO_FILE) res = FR_NO_PATH;
-#if FF_STR_VOLUME_ID == 2 /* Also current drive is changed at Unix style volume ID */
+#if FF_STR_VOLUME_ID == 2 /* Also current drive is changed if in Unix style volume ID */
if (res == FR_OK) {
for (i = FF_VOLUMES - 1; i && fs != FatFs[i]; i--) ; /* Set current drive */
CurrVol = (BYTE)i;
@@ -4136,16 +4341,17 @@ FRESULT f_getcwd (
TCHAR *tp = buff;
#if FF_VOLUMES >= 2
UINT vl;
-#endif
#if FF_STR_VOLUME_ID
const char *vp;
#endif
+#endif
FILINFO fno;
DEF_NAMBUF
/* Get logical drive */
- res = find_volume((const TCHAR**)&buff, &fs, 0); /* Get current volume */
+ buff[0] = 0; /* Set null string to get current volume */
+ res = mount_volume((const TCHAR**)&buff, &fs, 0); /* Get current volume */
if (res == FR_OK) {
dj.obj.fs = fs;
INIT_NAMBUF(fs);
@@ -4163,7 +4369,7 @@ FRESULT f_getcwd (
res = dir_sdi(&dj, 0);
if (res != FR_OK) break;
do { /* Find the entry links to the child directory */
- res = dir_read_file(&dj);
+ res = DIR_READ_FILE(&dj);
if (res != FR_OK) break;
if (ccl == ld_clust(fs, dj.dir)) break; /* Found the entry */
res = dir_next(&dj, 0);
@@ -4202,7 +4408,9 @@ FRESULT f_getcwd (
#endif
/* Add current directory path */
if (res == FR_OK) {
- do *tp++ = buff[i++]; while (i < len); /* Copy stacked path string */
+ do { /* Copy stacked path string */
+ *tp++ = buff[i++];
+ } while (i < len);
}
}
FREE_NAMBUF();
@@ -4229,10 +4437,13 @@ FRESULT f_lseek (
{
FRESULT res;
FATFS *fs;
- DWORD clst, bcs, nsect;
+ DWORD clst, bcs;
+ LBA_t nsect;
FSIZE_t ifptr;
#if FF_USE_FASTSEEK
- DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;
+ DWORD cl, pcl, ncl, tcl, tlen, ulen;
+ DWORD *tbl;
+ LBA_t dsc;
#endif
res = validate(&fp->obj, &fs); /* Check validity of the file object */
@@ -4396,7 +4607,7 @@ FRESULT f_opendir (
if (!dp) return FR_INVALID_OBJECT;
/* Get logical drive */
- res = find_volume(&path, &fs, 0);
+ res = mount_volume(&path, &fs, 0);
if (res == FR_OK) {
dp->obj.fs = fs;
INIT_NAMBUF(fs);
@@ -4406,7 +4617,7 @@ FRESULT f_opendir (
if (dp->obj.attr & AM_DIR) { /* This object is a sub-directory */
#if FF_FS_EXFAT
if (fs->fs_type == FS_EXFAT) {
- dp->obj.c_scl = dp->obj.sclust; /* Get containing directory inforamation */
+ dp->obj.c_scl = dp->obj.sclust; /* Get containing directory inforamation */
dp->obj.c_size = ((DWORD)dp->obj.objsize & 0xFFFFFF00) | dp->obj.stat;
dp->obj.c_ofs = dp->blk_ofs;
init_alloc_info(fs, &dp->obj); /* Get object allocation info */
@@ -4422,10 +4633,10 @@ FRESULT f_opendir (
if (res == FR_OK) {
dp->obj.id = fs->id;
res = dir_sdi(dp, 0); /* Rewind directory */
-#if FF_FS_LOCK != 0
+#if FF_FS_LOCK
if (res == FR_OK) {
if (dp->obj.sclust != 0) {
- dp->obj.lockid = inc_lock(dp, 0); /* Lock the sub directory */
+ dp->obj.lockid = inc_share(dp, 0); /* Lock the sub directory */
if (!dp->obj.lockid) res = FR_TOO_MANY_OPEN_FILES;
} else {
dp->obj.lockid = 0; /* Root directory need not to be locked */
@@ -4437,7 +4648,7 @@ FRESULT f_opendir (
FREE_NAMBUF();
if (res == FR_NO_FILE) res = FR_NO_PATH;
}
- if (res != FR_OK) dp->obj.fs = 0; /* Invalidate the directory object if function faild */
+ if (res != FR_OK) dp->obj.fs = 0; /* Invalidate the directory object if function failed */
LEAVE_FF(fs, res);
}
@@ -4459,14 +4670,14 @@ FRESULT f_closedir (
res = validate(&dp->obj, &fs); /* Check validity of the file object */
if (res == FR_OK) {
-#if FF_FS_LOCK != 0
- if (dp->obj.lockid) res = dec_lock(dp->obj.lockid); /* Decrement sub-directory open counter */
+#if FF_FS_LOCK
+ if (dp->obj.lockid) res = dec_share(dp->obj.lockid); /* Decrement sub-directory open counter */
if (res == FR_OK) dp->obj.fs = 0; /* Invalidate directory object */
#else
dp->obj.fs = 0; /* Invalidate directory object */
#endif
#if FF_FS_REENTRANT
- unlock_fs(fs, FR_OK); /* Unlock volume */
+ unlock_volume(fs, FR_OK); /* Unlock volume */
#endif
}
return res;
@@ -4492,10 +4703,10 @@ FRESULT f_readdir (
res = validate(&dp->obj, &fs); /* Check validity of the directory object */
if (res == FR_OK) {
if (!fno) {
- res = dir_sdi(dp, 0); /* Rewind the directory object */
+ res = dir_sdi(dp, 0); /* Rewind the directory object */
} else {
INIT_NAMBUF(fs);
- res = dir_read_file(dp); /* Read an item */
+ res = DIR_READ_FILE(dp); /* Read an item */
if (res == FR_NO_FILE) res = FR_OK; /* Ignore end of directory */
if (res == FR_OK) { /* A valid entry is found */
get_fileinfo(dp, fno); /* Get the object information */
@@ -4526,9 +4737,9 @@ FRESULT f_findnext (
for (;;) {
res = f_readdir(dp, fno); /* Get a directory item */
if (res != FR_OK || !fno || !fno->fname[0]) break; /* Terminate if any error or end of directory */
- if (pattern_matching(dp->pat, fno->fname, 0, 0)) break; /* Test for the file name */
+ if (pattern_match(dp->pat, fno->fname, 0, FIND_RECURS)) break; /* Test for the file name */
#if FF_USE_LFN && FF_USE_FIND == 2
- if (pattern_matching(dp->pat, fno->altname, 0, 0)) break; /* Test for alternative name if exist */
+ if (pattern_match(dp->pat, fno->altname, 0, FIND_RECURS)) break; /* Test for alternative name if exist */
#endif
}
return res;
@@ -4578,7 +4789,7 @@ FRESULT f_stat (
/* Get logical drive */
- res = find_volume(&path, &dj.obj.fs, 0);
+ res = mount_volume(&path, &dj.obj.fs, 0);
if (res == FR_OK) {
INIT_NAMBUF(dj.obj.fs);
res = follow_path(&dj, path); /* Follow the file path */
@@ -4610,13 +4821,14 @@ FRESULT f_getfree (
{
FRESULT res;
FATFS *fs;
- DWORD nfree, clst, sect, stat;
+ DWORD nfree, clst, stat;
+ LBA_t sect;
UINT i;
FFOBJID obj;
/* Get logical drive */
- res = find_volume(&path, &fs, 0);
+ res = mount_volume(&path, &fs, 0);
if (res == FR_OK) {
*fatfs = fs; /* Return ptr to the fs object */
/* If free_clst is valid, return it without full FAT scan */
@@ -4629,8 +4841,12 @@ FRESULT f_getfree (
clst = 2; obj.fs = fs;
do {
stat = get_fat(&obj, clst);
- if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
- if (stat == 1) { res = FR_INT_ERR; break; }
+ if (stat == 0xFFFFFFFF) {
+ res = FR_DISK_ERR; break;
+ }
+ if (stat == 1) {
+ res = FR_INT_ERR; break;
+ }
if (stat == 0) nfree++;
} while (++clst < fs->n_fatent);
} else {
@@ -4640,15 +4856,15 @@ FRESULT f_getfree (
UINT b;
clst = fs->n_fatent - 2; /* Number of clusters */
- sect = fs->database; /* Assuming bitmap starts at cluster 2 */
+ sect = fs->bitbase; /* Bitmap sector */
i = 0; /* Offset in the sector */
do { /* Counts numbuer of bits with zero in the bitmap */
- if (i == 0) {
+ if (i == 0) { /* New sector? */
res = move_window(fs, sect++);
if (res != FR_OK) break;
}
- for (b = 8, bm = fs->win[i]; b && clst; b--, clst--) {
- if (!(bm & 1)) nfree++;
+ for (b = 8, bm = ~fs->win[i]; b && clst; b--, clst--) {
+ nfree += bm & 1;
bm >>= 1;
}
i = (i + 1) % SS(fs);
@@ -4660,7 +4876,7 @@ FRESULT f_getfree (
sect = fs->fatbase; /* Top of the FAT */
i = 0; /* Offset in the sector */
do { /* Counts numbuer of entries with zero in the FAT */
- if (i == 0) {
+ if (i == 0) { /* New sector? */
res = move_window(fs, sect++);
if (res != FR_OK) break;
}
@@ -4675,9 +4891,11 @@ FRESULT f_getfree (
} while (--clst);
}
}
- *nclst = nfree; /* Return the free clusters */
- fs->free_clst = nfree; /* Now free_clst is valid */
- fs->fsi_flag |= 1; /* FAT32: FSInfo is to be updated */
+ if (res == FR_OK) { /* Update parameters if succeeded */
+ *nclst = nfree; /* Return the free clusters */
+ fs->free_clst = nfree; /* Now free_clst is valid */
+ fs->fsi_flag |= 1; /* FAT32: FSInfo is to be updated */
+ }
}
}
@@ -4746,9 +4964,9 @@ FRESULT f_unlink (
)
{
FRESULT res;
+ FATFS *fs;
DIR dj, sdj;
DWORD dclst = 0;
- FATFS *fs;
#if FF_FS_EXFAT
FFOBJID obj;
#endif
@@ -4756,7 +4974,7 @@ FRESULT f_unlink (
/* Get logical drive */
- res = find_volume(&path, &fs, FA_WRITE);
+ res = mount_volume(&path, &fs, FA_WRITE);
if (res == FR_OK) {
dj.obj.fs = fs;
INIT_NAMBUF(fs);
@@ -4764,8 +4982,8 @@ FRESULT f_unlink (
if (FF_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT)) {
res = FR_INVALID_NAME; /* Cannot remove dot entry */
}
-#if FF_FS_LOCK != 0
- if (res == FR_OK) res = chk_lock(&dj, 2); /* Check if it is an open object */
+#if FF_FS_LOCK
+ if (res == FR_OK) res = chk_share(&dj, 2); /* Check if it is an open object */
#endif
if (res == FR_OK) { /* The object is accessible */
if (dj.fn[NSFLAG] & NS_NONAME) {
@@ -4788,12 +5006,12 @@ FRESULT f_unlink (
}
if (dj.obj.attr & AM_DIR) { /* Is it a sub-directory? */
#if FF_FS_RPATH != 0
- if (dclst == fs->cdir) { /* Is it the current directory? */
+ if (dclst == fs->cdir) { /* Is it the current directory? */
res = FR_DENIED;
} else
#endif
{
- sdj.obj.fs = fs; /* Open the sub-directory */
+ sdj.obj.fs = fs; /* Open the sub-directory */
sdj.obj.sclust = dclst;
#if FF_FS_EXFAT
if (fs->fs_type == FS_EXFAT) {
@@ -4803,7 +5021,7 @@ FRESULT f_unlink (
#endif
res = dir_sdi(&sdj, 0);
if (res == FR_OK) {
- res = dir_read_file(&sdj); /* Test if the directory is empty */
+ res = DIR_READ_FILE(&sdj); /* Test if the directory is empty */
if (res == FR_OK) res = FR_DENIED; /* Not empty? */
if (res == FR_NO_FILE) res = FR_OK; /* Empty? */
}
@@ -4840,74 +5058,70 @@ FRESULT f_mkdir (
)
{
FRESULT res;
- DIR dj;
FATFS *fs;
- BYTE *dir;
+ DIR dj;
+ FFOBJID sobj;
DWORD dcl, pcl, tm;
DEF_NAMBUF
- /* Get logical drive */
- res = find_volume(&path, &fs, FA_WRITE);
+ res = mount_volume(&path, &fs, FA_WRITE); /* Get logical drive */
if (res == FR_OK) {
dj.obj.fs = fs;
INIT_NAMBUF(fs);
res = follow_path(&dj, path); /* Follow the file path */
- if (res == FR_OK) res = FR_EXIST; /* Any object with same name is already existing */
- if (FF_FS_RPATH && res == FR_NO_FILE && (dj.fn[NSFLAG] & NS_DOT)) {
+ if (res == FR_OK) res = FR_EXIST; /* Name collision? */
+ if (FF_FS_RPATH && res == FR_NO_FILE && (dj.fn[NSFLAG] & NS_DOT)) { /* Invalid name? */
res = FR_INVALID_NAME;
}
- if (res == FR_NO_FILE) { /* Can create a new directory */
- dcl = create_chain(&dj.obj, 0); /* Allocate a cluster for the new directory table */
- dj.obj.objsize = (DWORD)fs->csize * SS(fs);
+ if (res == FR_NO_FILE) { /* It is clear to create a new directory */
+ sobj.fs = fs; /* New object id to create a new chain */
+ dcl = create_chain(&sobj, 0); /* Allocate a cluster for the new directory */
res = FR_OK;
- if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster */
- if (dcl == 1) res = FR_INT_ERR;
- if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
- if (res == FR_OK) res = sync_window(fs); /* Flush FAT */
+ if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster? */
+ if (dcl == 1) res = FR_INT_ERR; /* Any insanity? */
+ if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR; /* Disk error? */
tm = GET_FATTIME();
- if (res == FR_OK) { /* Initialize the new directory table */
+ if (res == FR_OK) {
res = dir_clear(fs, dcl); /* Clean up the new table */
- if (res == FR_OK && (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT)) { /* Create dot entries (FAT only) */
- dir = fs->win;
- mem_set(dir + DIR_Name, ' ', 11); /* Create "." entry */
- dir[DIR_Name] = '.';
- dir[DIR_Attr] = AM_DIR;
- st_dword(dir + DIR_ModTime, tm);
- st_clust(fs, dir, dcl);
- mem_cpy(dir + SZDIRE, dir, SZDIRE); /* Create ".." entry */
- dir[SZDIRE + 1] = '.'; pcl = dj.obj.sclust;
- st_clust(fs, dir + SZDIRE, pcl);
- fs->wflag = 1;
+ if (res == FR_OK) {
+ if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) { /* Create dot entries (FAT only) */
+ memset(fs->win + DIR_Name, ' ', 11); /* Create "." entry */
+ fs->win[DIR_Name] = '.';
+ fs->win[DIR_Attr] = AM_DIR;
+ st_dword(fs->win + DIR_ModTime, tm);
+ st_clust(fs, fs->win, dcl);
+ memcpy(fs->win + SZDIRE, fs->win, SZDIRE); /* Create ".." entry */
+ fs->win[SZDIRE + 1] = '.'; pcl = dj.obj.sclust;
+ st_clust(fs, fs->win + SZDIRE, pcl);
+ fs->wflag = 1;
+ }
+ res = dir_register(&dj); /* Register the object to the parent directoy */
}
}
if (res == FR_OK) {
- res = dir_register(&dj); /* Register the object to the directoy */
- }
- if (res == FR_OK) {
#if FF_FS_EXFAT
if (fs->fs_type == FS_EXFAT) { /* Initialize directory entry block */
st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Created time */
st_dword(fs->dirbuf + XDIR_FstClus, dcl); /* Table start cluster */
- st_dword(fs->dirbuf + XDIR_FileSize, (DWORD)dj.obj.objsize); /* File size needs to be valid */
- st_dword(fs->dirbuf + XDIR_ValidFileSize, (DWORD)dj.obj.objsize);
+ st_dword(fs->dirbuf + XDIR_FileSize, (DWORD)fs->csize * SS(fs)); /* Directory size needs to be valid */
+ st_dword(fs->dirbuf + XDIR_ValidFileSize, (DWORD)fs->csize * SS(fs));
fs->dirbuf[XDIR_GenFlags] = 3; /* Initialize the object flag */
fs->dirbuf[XDIR_Attr] = AM_DIR; /* Attribute */
res = store_xdir(&dj);
} else
#endif
{
- dir = dj.dir;
- st_dword(dir + DIR_ModTime, tm); /* Created time */
- st_clust(fs, dir, dcl); /* Table start cluster */
- dir[DIR_Attr] = AM_DIR; /* Attribute */
+ st_dword(dj.dir + DIR_ModTime, tm); /* Created time */
+ st_clust(fs, dj.dir, dcl); /* Table start cluster */
+ dj.dir[DIR_Attr] = AM_DIR; /* Attribute */
fs->wflag = 1;
}
if (res == FR_OK) {
res = sync_fs(fs);
}
} else {
- remove_chain(&dj.obj, dcl, 0); /* Could not register, remove cluster chain */
+ remove_chain(&sobj, dcl, 0); /* Could not register, remove the allocated cluster */
}
}
FREE_NAMBUF();
@@ -4929,33 +5143,33 @@ FRESULT f_rename (
)
{
FRESULT res;
- DIR djo, djn;
FATFS *fs;
+ DIR djo, djn;
BYTE buf[FF_FS_EXFAT ? SZDIRE * 2 : SZDIRE], *dir;
- DWORD dw;
+ LBA_t sect;
DEF_NAMBUF
get_ldnumber(&path_new); /* Snip the drive number of new name off */
- res = find_volume(&path_old, &fs, FA_WRITE); /* Get logical drive of the old object */
+ res = mount_volume(&path_old, &fs, FA_WRITE); /* Get logical drive of the old object */
if (res == FR_OK) {
djo.obj.fs = fs;
INIT_NAMBUF(fs);
- res = follow_path(&djo, path_old); /* Check old object */
+ res = follow_path(&djo, path_old); /* Check old object */
if (res == FR_OK && (djo.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME; /* Check validity of name */
-#if FF_FS_LOCK != 0
+#if FF_FS_LOCK
if (res == FR_OK) {
- res = chk_lock(&djo, 2);
+ res = chk_share(&djo, 2);
}
#endif
- if (res == FR_OK) { /* Object to be renamed is found */
+ if (res == FR_OK) { /* Object to be renamed is found */
#if FF_FS_EXFAT
if (fs->fs_type == FS_EXFAT) { /* At exFAT volume */
BYTE nf, nn;
WORD nh;
- mem_cpy(buf, fs->dirbuf, SZDIRE * 2); /* Save 85+C0 entry of old object */
- mem_cpy(&djn, &djo, sizeof djo);
+ memcpy(buf, fs->dirbuf, SZDIRE * 2); /* Save 85+C0 entry of old object */
+ memcpy(&djn, &djo, sizeof djo);
res = follow_path(&djn, path_new); /* Make sure if new object name is not in use */
if (res == FR_OK) { /* Is new name already in use by any other object? */
res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;
@@ -4965,7 +5179,7 @@ FRESULT f_rename (
if (res == FR_OK) {
nf = fs->dirbuf[XDIR_NumSec]; nn = fs->dirbuf[XDIR_NumName];
nh = ld_word(fs->dirbuf + XDIR_NameHash);
- mem_cpy(fs->dirbuf, buf, SZDIRE * 2); /* Restore 85+C0 entry */
+ memcpy(fs->dirbuf, buf, SZDIRE * 2); /* Restore 85+C0 entry */
fs->dirbuf[XDIR_NumSec] = nf; fs->dirbuf[XDIR_NumName] = nn;
st_word(fs->dirbuf + XDIR_NameHash, nh);
if (!(fs->dirbuf[XDIR_Attr] & AM_DIR)) fs->dirbuf[XDIR_Attr] |= AM_ARC; /* Set archive attribute if it is a file */
@@ -4976,8 +5190,8 @@ FRESULT f_rename (
} else
#endif
{ /* At FAT/FAT32 volume */
- mem_cpy(buf, djo.dir, SZDIRE); /* Save directory entry of the object */
- mem_cpy(&djn, &djo, sizeof (DIR)); /* Duplicate the directory object */
+ memcpy(buf, djo.dir, SZDIRE); /* Save directory entry of the object */
+ memcpy(&djn, &djo, sizeof (DIR)); /* Duplicate the directory object */
res = follow_path(&djn, path_new); /* Make sure if new object name is not in use */
if (res == FR_OK) { /* Is new name already in use by any other object? */
res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;
@@ -4986,17 +5200,17 @@ FRESULT f_rename (
res = dir_register(&djn); /* Register the new entry */
if (res == FR_OK) {
dir = djn.dir; /* Copy directory entry of the object except name */
- mem_cpy(dir + 13, buf + 13, SZDIRE - 13);
+ memcpy(dir + 13, buf + 13, SZDIRE - 13);
dir[DIR_Attr] = buf[DIR_Attr];
if (!(dir[DIR_Attr] & AM_DIR)) dir[DIR_Attr] |= AM_ARC; /* Set archive attribute if it is a file */
fs->wflag = 1;
if ((dir[DIR_Attr] & AM_DIR) && djo.obj.sclust != djn.obj.sclust) { /* Update .. entry in the sub-directory if needed */
- dw = clst2sect(fs, ld_clust(fs, dir));
- if (dw == 0) {
+ sect = clst2sect(fs, ld_clust(fs, dir));
+ if (sect == 0) {
res = FR_INT_ERR;
} else {
/* Start of critical section where an interruption can cause a cross-link */
- res = move_window(fs, dw);
+ res = move_window(fs, sect);
dir = fs->win + SZDIRE * 1; /* Ptr to .. entry */
if (res == FR_OK && dir[1] == '.') {
st_clust(fs, dir, djn.obj.sclust);
@@ -5040,12 +5254,12 @@ FRESULT f_chmod (
)
{
FRESULT res;
- DIR dj;
FATFS *fs;
+ DIR dj;
DEF_NAMBUF
- res = find_volume(&path, &fs, FA_WRITE); /* Get logical drive */
+ res = mount_volume(&path, &fs, FA_WRITE); /* Get logical drive */
if (res == FR_OK) {
dj.obj.fs = fs;
INIT_NAMBUF(fs);
@@ -5086,12 +5300,12 @@ FRESULT f_utime (
)
{
FRESULT res;
- DIR dj;
FATFS *fs;
+ DIR dj;
DEF_NAMBUF
- res = find_volume(&path, &fs, FA_WRITE); /* Get logical drive */
+ res = mount_volume(&path, &fs, FA_WRITE); /* Get logical drive */
if (res == FR_OK) {
dj.obj.fs = fs;
INIT_NAMBUF(fs);
@@ -5134,33 +5348,36 @@ FRESULT f_getlabel (
)
{
FRESULT res;
- DIR dj;
FATFS *fs;
+ DIR dj;
UINT si, di;
WCHAR wc;
/* Get logical drive */
- res = find_volume(&path, &fs, 0);
+ res = mount_volume(&path, &fs, 0);
/* Get volume label */
if (res == FR_OK && label) {
dj.obj.fs = fs; dj.obj.sclust = 0; /* Open root directory */
res = dir_sdi(&dj, 0);
if (res == FR_OK) {
- res = dir_read_label(&dj); /* Find a volume label entry */
+ res = DIR_READ_LABEL(&dj); /* Find a volume label entry */
if (res == FR_OK) {
#if FF_FS_EXFAT
if (fs->fs_type == FS_EXFAT) {
WCHAR hs;
+ UINT nw;
for (si = di = hs = 0; si < dj.dir[XDIR_NumLabel]; si++) { /* Extract volume label from 83 entry */
wc = ld_word(dj.dir + XDIR_Label + si * 2);
if (hs == 0 && IsSurrogate(wc)) { /* Is the code a surrogate? */
hs = wc; continue;
}
- wc = put_utf((DWORD)hs << 16 | wc, &label[di], 4);
- if (wc == 0) { di = 0; break; }
- di += wc;
+ nw = put_utf((DWORD)hs << 16 | wc, &label[di], 4); /* Store it in API encoding */
+ if (nw == 0) { /* Encode error? */
+ di = 0; break;
+ }
+ di += nw;
hs = 0;
}
if (hs != 0) di = 0; /* Broken surrogate pair? */
@@ -5173,10 +5390,11 @@ FRESULT f_getlabel (
wc = dj.dir[si++];
#if FF_USE_LFN && FF_LFN_UNICODE >= 1 /* Unicode output */
if (dbc_1st((BYTE)wc) && si < 11) wc = wc << 8 | dj.dir[si++]; /* Is it a DBC? */
- wc = ff_oem2uni(wc, CODEPAGE); /* Convert it into Unicode */
- if (wc != 0) wc = put_utf(wc, &label[di], 4); /* Put it in Unicode */
- if (wc == 0) { di = 0; break; }
- di += wc;
+ wc = ff_oem2uni(wc, CODEPAGE); /* Convert it into Unicode */
+ if (wc == 0) { /* Invalid char in current code page? */
+ di = 0; break;
+ }
+ di += put_utf(wc, &label[di], 4); /* Store it in Unicode */
#else /* ANSI/OEM output */
label[di++] = (TCHAR)wc;
#endif
@@ -5200,10 +5418,12 @@ FRESULT f_getlabel (
if (res == FR_OK) {
switch (fs->fs_type) {
case FS_EXFAT:
- di = BPB_VolIDEx; break;
+ di = BPB_VolIDEx;
+ break;
case FS_FAT32:
- di = BS_VolID32; break;
+ di = BS_VolID32;
+ break;
default:
di = BS_VolID;
@@ -5227,23 +5447,23 @@ FRESULT f_setlabel (
)
{
FRESULT res;
- DIR dj;
FATFS *fs;
+ DIR dj;
BYTE dirvn[22];
UINT di;
WCHAR wc;
- static const char badchr[] = "+.,;=[]/\\\"*:<>\?|\x7F"; /* [0..] for FAT, [7..] for exFAT */
+ static const char badchr[18] = "+.,;=[]" "/*:<>|\\\"\?\x7F"; /* [0..16] for FAT, [7..16] for exFAT */
#if FF_USE_LFN
DWORD dc;
#endif
/* Get logical drive */
- res = find_volume(&label, &fs, FA_WRITE);
+ res = mount_volume(&label, &fs, FA_WRITE);
if (res != FR_OK) LEAVE_FF(fs, res);
#if FF_FS_EXFAT
if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */
- mem_set(dirvn, 0, 22);
+ memset(dirvn, 0, 22);
di = 0;
while ((UINT)*label >= ' ') { /* Create volume label */
dc = tchar2uni(&label); /* Get a Unicode character */
@@ -5254,7 +5474,7 @@ FRESULT f_setlabel (
st_word(dirvn + di * 2, (WCHAR)(dc >> 16)); di++;
}
}
- if (dc == 0 || chk_chr(badchr + 7, (int)dc) || di >= 11) { /* Check validity of the volume label */
+ if (dc == 0 || strchr(&badchr[7], (int)dc) || di >= 11) { /* Check validity of the volume label */
LEAVE_FF(fs, FR_INVALID_NAME);
}
st_word(dirvn + di * 2, (WCHAR)dc); di++;
@@ -5262,7 +5482,7 @@ FRESULT f_setlabel (
} else
#endif
{ /* On the FAT/FAT32 volume */
- mem_set(dirvn, ' ', 11);
+ memset(dirvn, ' ', 11);
di = 0;
while ((UINT)*label >= ' ') { /* Create volume label */
#if FF_USE_LFN
@@ -5278,7 +5498,7 @@ FRESULT f_setlabel (
if (wc >= 0x80) wc = ExCvt[wc - 0x80]; /* To upper extended characters (SBCS cfg) */
#endif
#endif
- if (wc == 0 || chk_chr(badchr + 0, (int)wc) || di >= (UINT)((wc >= 0x100) ? 10 : 11)) { /* Reject invalid characters for volume label */
+ if (wc == 0 || strchr(&badchr[0], (int)wc) || di >= (UINT)((wc >= 0x100) ? 10 : 11)) { /* Reject invalid characters for volume label */
LEAVE_FF(fs, FR_INVALID_NAME);
}
if (wc >= 0x100) dirvn[di++] = (BYTE)(wc >> 8);
@@ -5292,14 +5512,14 @@ FRESULT f_setlabel (
dj.obj.fs = fs; dj.obj.sclust = 0; /* Open root directory */
res = dir_sdi(&dj, 0);
if (res == FR_OK) {
- res = dir_read_label(&dj); /* Get volume label entry */
+ res = DIR_READ_LABEL(&dj); /* Get volume label entry */
if (res == FR_OK) {
if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) {
dj.dir[XDIR_NumLabel] = (BYTE)di; /* Change the volume label */
- mem_cpy(dj.dir + XDIR_Label, dirvn, 22);
+ memcpy(dj.dir + XDIR_Label, dirvn, 22);
} else {
if (di != 0) {
- mem_cpy(dj.dir, dirvn, 11); /* Change the volume label */
+ memcpy(dj.dir, dirvn, 11); /* Change the volume label */
} else {
dj.dir[DIR_Name] = DDEM; /* Remove the volume label */
}
@@ -5312,14 +5532,14 @@ FRESULT f_setlabel (
if (di != 0) { /* Create a volume label entry */
res = dir_alloc(&dj, 1); /* Allocate an entry */
if (res == FR_OK) {
- mem_set(dj.dir, 0, SZDIRE); /* Clean the entry */
+ memset(dj.dir, 0, SZDIRE); /* Clean the entry */
if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) {
- dj.dir[XDIR_Type] = 0x83; /* Create 83 entry */
+ dj.dir[XDIR_Type] = ET_VLABEL; /* Create volume label entry */
dj.dir[XDIR_NumLabel] = (BYTE)di;
- mem_cpy(dj.dir + XDIR_Label, dirvn, 22);
+ memcpy(dj.dir + XDIR_Label, dirvn, 22);
} else {
dj.dir[DIR_Attr] = AM_VOL; /* Create volume label entry */
- mem_cpy(dj.dir, dirvn, 11);
+ memcpy(dj.dir, dirvn, 11);
}
fs->wflag = 1;
res = sync_fs(fs);
@@ -5384,14 +5604,20 @@ FRESULT f_expand (
for (;;) { /* Find a contiguous cluster block */
n = get_fat(&fp->obj, clst);
if (++clst >= fs->n_fatent) clst = 2;
- if (n == 1) { res = FR_INT_ERR; break; }
- if (n == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
+ if (n == 1) {
+ res = FR_INT_ERR; break;
+ }
+ if (n == 0xFFFFFFFF) {
+ res = FR_DISK_ERR; break;
+ }
if (n == 0) { /* Is it a free cluster? */
if (++ncl == tcl) break; /* Break if a contiguous cluster block is found */
} else {
scl = clst; ncl = 0; /* Not a free cluster */
}
- if (clst == stcl) { res = FR_DENIED; break; } /* No contiguous cluster? */
+ if (clst == stcl) { /* No contiguous cluster? */
+ res = FR_DENIED; break;
+ }
}
if (res == FR_OK) { /* A contiguous free area is found */
if (opt) { /* Allocate it now */
@@ -5441,7 +5667,8 @@ FRESULT f_forward (
{
FRESULT res;
FATFS *fs;
- DWORD clst, sect;
+ DWORD clst;
+ LBA_t sect;
FSIZE_t remain;
UINT rcnt, csect;
BYTE *dbuf;
@@ -5455,8 +5682,7 @@ FRESULT f_forward (
remain = fp->obj.objsize - fp->fptr;
if (btf > remain) btf = (UINT)remain; /* Truncate btf by remaining bytes */
- for ( ; btf && (*func)(0, 0); /* Repeat until all data transferred or stream goes busy */
- fp->fptr += rcnt, *bf += rcnt, btf -= rcnt) {
+ for ( ; btf > 0 && (*func)(0, 0); fp->fptr += rcnt, *bf += rcnt, btf -= rcnt) { /* Repeat until all data transferred or stream goes busy */
csect = (UINT)(fp->fptr / SS(fs) & (fs->csize - 1)); /* Sector offset in the cluster */
if (fp->fptr % SS(fs) == 0) { /* On the sector boundary? */
if (csect == 0) { /* On the cluster boundary? */
@@ -5486,7 +5712,7 @@ FRESULT f_forward (
dbuf = fp->buf;
#endif
fp->sect = sect;
- rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */
+ rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes remains in the sector */
if (rcnt > btf) rcnt = btf; /* Clip it by btr if needed */
rcnt = (*func)(dbuf + ((UINT)fp->fptr % SS(fs)), rcnt); /* Forward the file data */
if (rcnt == 0) ABORT(fs, FR_INT_ERR);
@@ -5498,138 +5724,334 @@ FRESULT f_forward (
-#if FF_USE_MKFS && !FF_FS_READONLY
+#if !FF_FS_READONLY && FF_USE_MKFS
/*-----------------------------------------------------------------------*/
-/* Create an FAT/exFAT volume */
+/* Create FAT/exFAT volume (with sub-functions) */
/*-----------------------------------------------------------------------*/
+#define N_SEC_TRACK 63 /* Sectors per track for determination of drive CHS */
+#define GPT_ALIGN 0x100000 /* Alignment of partitions in GPT [byte] (>=128KB) */
+#define GPT_ITEMS 128 /* Number of GPT table size (>=128, sector aligned) */
+
+
+/* Create partitions on the physical drive in format of MBR or GPT */
+
+static FRESULT create_partition (
+ BYTE drv, /* Physical drive number */
+ const LBA_t plst[], /* Partition list */
+ BYTE sys, /* System ID for each partition (for only MBR) */
+ BYTE *buf /* Working buffer for a sector */
+)
+{
+ UINT i, cy;
+ LBA_t sz_drv;
+ DWORD sz_drv32, nxt_alloc32, sz_part32;
+ BYTE *pte;
+ BYTE hd, n_hd, sc, n_sc;
+
+ /* Get physical drive size */
+ if (disk_ioctl(drv, GET_SECTOR_COUNT, &sz_drv) != RES_OK) return FR_DISK_ERR;
+
+#if FF_LBA64
+ if (sz_drv >= FF_MIN_GPT) { /* Create partitions in GPT format */
+ WORD ss;
+ UINT sz_ptbl, pi, si, ofs;
+ DWORD bcc, rnd, align;
+ QWORD nxt_alloc, sz_part, sz_pool, top_bpt;
+ static const BYTE gpt_mbr[16] = {0x00, 0x00, 0x02, 0x00, 0xEE, 0xFE, 0xFF, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
+
+#if FF_MAX_SS != FF_MIN_SS
+ if (disk_ioctl(drv, GET_SECTOR_SIZE, &ss) != RES_OK) return FR_DISK_ERR; /* Get sector size */
+ if (ss > FF_MAX_SS || ss < FF_MIN_SS || (ss & (ss - 1))) return FR_DISK_ERR;
+#else
+ ss = FF_MAX_SS;
+#endif
+ rnd = (DWORD)sz_drv + GET_FATTIME(); /* Random seed */
+ align = GPT_ALIGN / ss; /* Partition alignment for GPT [sector] */
+ sz_ptbl = GPT_ITEMS * SZ_GPTE / ss; /* Size of partition table [sector] */
+ top_bpt = sz_drv - sz_ptbl - 1; /* Backup partition table start sector */
+ nxt_alloc = 2 + sz_ptbl; /* First allocatable sector */
+ sz_pool = top_bpt - nxt_alloc; /* Size of allocatable area */
+ bcc = 0xFFFFFFFF; sz_part = 1;
+ pi = si = 0; /* partition table index, size table index */
+ do {
+ if (pi * SZ_GPTE % ss == 0) memset(buf, 0, ss); /* Clean the buffer if needed */
+ if (sz_part != 0) { /* Is the size table not termintated? */
+ nxt_alloc = (nxt_alloc + align - 1) & ((QWORD)0 - align); /* Align partition start */
+ sz_part = plst[si++]; /* Get a partition size */
+ if (sz_part <= 100) { /* Is the size in percentage? */
+ sz_part = sz_pool * sz_part / 100;
+ sz_part = (sz_part + align - 1) & ((QWORD)0 - align); /* Align partition end (only if in percentage) */
+ }
+ if (nxt_alloc + sz_part > top_bpt) { /* Clip the size at end of the pool */
+ sz_part = (nxt_alloc < top_bpt) ? top_bpt - nxt_alloc : 0;
+ }
+ }
+ if (sz_part != 0) { /* Add a partition? */
+ ofs = pi * SZ_GPTE % ss;
+ memcpy(buf + ofs + GPTE_PtGuid, GUID_MS_Basic, 16); /* Set partition GUID (Microsoft Basic Data) */
+ rnd = make_rand(rnd, buf + ofs + GPTE_UpGuid, 16); /* Set unique partition GUID */
+ st_qword(buf + ofs + GPTE_FstLba, nxt_alloc); /* Set partition start sector */
+ st_qword(buf + ofs + GPTE_LstLba, nxt_alloc + sz_part - 1); /* Set partition end sector */
+ nxt_alloc += sz_part; /* Next allocatable sector */
+ }
+ if ((pi + 1) * SZ_GPTE % ss == 0) { /* Write the buffer if it is filled up */
+ for (i = 0; i < ss; bcc = crc32(bcc, buf[i++])) ; /* Calculate table check sum */
+ if (disk_write(drv, buf, 2 + pi * SZ_GPTE / ss, 1) != RES_OK) return FR_DISK_ERR; /* Write to primary table */
+ if (disk_write(drv, buf, top_bpt + pi * SZ_GPTE / ss, 1) != RES_OK) return FR_DISK_ERR; /* Write to secondary table */
+ }
+ } while (++pi < GPT_ITEMS);
+
+ /* Create primary GPT header */
+ memset(buf, 0, ss);
+ memcpy(buf + GPTH_Sign, "EFI PART" "\0\0\1\0" "\x5C\0\0", 16); /* Signature, version (1.0) and size (92) */
+ st_dword(buf + GPTH_PtBcc, ~bcc); /* Table check sum */
+ st_qword(buf + GPTH_CurLba, 1); /* LBA of this header */
+ st_qword(buf + GPTH_BakLba, sz_drv - 1); /* LBA of secondary header */
+ st_qword(buf + GPTH_FstLba, 2 + sz_ptbl); /* LBA of first allocatable sector */
+ st_qword(buf + GPTH_LstLba, top_bpt - 1); /* LBA of last allocatable sector */
+ st_dword(buf + GPTH_PteSize, SZ_GPTE); /* Size of a table entry */
+ st_dword(buf + GPTH_PtNum, GPT_ITEMS); /* Number of table entries */
+ st_dword(buf + GPTH_PtOfs, 2); /* LBA of this table */
+ rnd = make_rand(rnd, buf + GPTH_DskGuid, 16); /* Disk GUID */
+ for (i = 0, bcc= 0xFFFFFFFF; i < 92; bcc = crc32(bcc, buf[i++])) ; /* Calculate header check sum */
+ st_dword(buf + GPTH_Bcc, ~bcc); /* Header check sum */
+ if (disk_write(drv, buf, 1, 1) != RES_OK) return FR_DISK_ERR;
+
+ /* Create secondary GPT header */
+ st_qword(buf + GPTH_CurLba, sz_drv - 1); /* LBA of this header */
+ st_qword(buf + GPTH_BakLba, 1); /* LBA of primary header */
+ st_qword(buf + GPTH_PtOfs, top_bpt); /* LBA of this table */
+ st_dword(buf + GPTH_Bcc, 0);
+ for (i = 0, bcc= 0xFFFFFFFF; i < 92; bcc = crc32(bcc, buf[i++])) ; /* Calculate header check sum */
+ st_dword(buf + GPTH_Bcc, ~bcc); /* Header check sum */
+ if (disk_write(drv, buf, sz_drv - 1, 1) != RES_OK) return FR_DISK_ERR;
+
+ /* Create protective MBR */
+ memset(buf, 0, ss);
+ memcpy(buf + MBR_Table, gpt_mbr, 16); /* Create a GPT partition */
+ st_word(buf + BS_55AA, 0xAA55);
+ if (disk_write(drv, buf, 0, 1) != RES_OK) return FR_DISK_ERR;
+
+ } else
+#endif
+ { /* Create partitions in MBR format */
+ sz_drv32 = (DWORD)sz_drv;
+ n_sc = N_SEC_TRACK; /* Determine drive CHS without any consideration of the drive geometry */
+ for (n_hd = 8; n_hd != 0 && sz_drv32 / n_hd / n_sc > 1024; n_hd *= 2) ;
+ if (n_hd == 0) n_hd = 255; /* Number of heads needs to be <256 */
+
+ memset(buf, 0, FF_MAX_SS); /* Clear MBR */
+ pte = buf + MBR_Table; /* Partition table in the MBR */
+ for (i = 0, nxt_alloc32 = n_sc; i < 4 && nxt_alloc32 != 0 && nxt_alloc32 < sz_drv32; i++, nxt_alloc32 += sz_part32) {
+ sz_part32 = (DWORD)plst[i]; /* Get partition size */
+ if (sz_part32 <= 100) sz_part32 = (sz_part32 == 100) ? sz_drv32 : sz_drv32 / 100 * sz_part32; /* Size in percentage? */
+ if (nxt_alloc32 + sz_part32 > sz_drv32 || nxt_alloc32 + sz_part32 < nxt_alloc32) sz_part32 = sz_drv32 - nxt_alloc32; /* Clip at drive size */
+ if (sz_part32 == 0) break; /* End of table or no sector to allocate? */
+
+ st_dword(pte + PTE_StLba, nxt_alloc32); /* Start LBA */
+ st_dword(pte + PTE_SizLba, sz_part32); /* Number of sectors */
+ pte[PTE_System] = sys; /* System type */
+
+ cy = (UINT)(nxt_alloc32 / n_sc / n_hd); /* Start cylinder */
+ hd = (BYTE)(nxt_alloc32 / n_sc % n_hd); /* Start head */
+ sc = (BYTE)(nxt_alloc32 % n_sc + 1); /* Start sector */
+ pte[PTE_StHead] = hd;
+ pte[PTE_StSec] = (BYTE)((cy >> 2 & 0xC0) | sc);
+ pte[PTE_StCyl] = (BYTE)cy;
+
+ cy = (UINT)((nxt_alloc32 + sz_part32 - 1) / n_sc / n_hd); /* End cylinder */
+ hd = (BYTE)((nxt_alloc32 + sz_part32 - 1) / n_sc % n_hd); /* End head */
+ sc = (BYTE)((nxt_alloc32 + sz_part32 - 1) % n_sc + 1); /* End sector */
+ pte[PTE_EdHead] = hd;
+ pte[PTE_EdSec] = (BYTE)((cy >> 2 & 0xC0) | sc);
+ pte[PTE_EdCyl] = (BYTE)cy;
+
+ pte += SZ_PTE; /* Next entry */
+ }
+
+ st_word(buf + BS_55AA, 0xAA55); /* MBR signature */
+ if (disk_write(drv, buf, 0, 1) != RES_OK) return FR_DISK_ERR; /* Write it to the MBR */
+ }
+
+ return FR_OK;
+}
+
+
+
FRESULT f_mkfs (
- const TCHAR* path, /* Logical drive number */
- BYTE opt, /* Format option */
- DWORD au, /* Size of allocation unit (cluster) [byte] */
- void* work, /* Pointer to working buffer (null: use heap memory) */
- UINT len /* Size of working buffer [byte] */
+ const TCHAR* path, /* Logical drive number */
+ const MKFS_PARM* opt, /* Format options */
+ void* work, /* Pointer to working buffer (null: use len bytes of heap memory) */
+ UINT len /* Size of working buffer [byte] */
)
{
- const UINT n_fats = 1; /* Number of FATs for FAT/FAT32 volume (1 or 2) */
- const UINT n_rootdir = 512; /* Number of root directory entries for FAT volume */
static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0}; /* Cluster size boundary for FAT volume (4Ks unit) */
static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0}; /* Cluster size boundary for FAT32 volume (128Ks unit) */
- BYTE fmt, sys, *buf, *pte, pdrv, part;
+ static const MKFS_PARM defopt = {FM_ANY, 0, 0, 0, 0}; /* Default parameter */
+ BYTE fsopt, fsty, sys, pdrv, ipart;
+ BYTE *buf;
+ BYTE *pte;
WORD ss; /* Sector size */
- DWORD szb_buf, sz_buf, sz_blk, n_clst, pau, sect, nsect, n;
- DWORD b_vol, b_fat, b_data; /* Base LBA for volume, fat, data */
- DWORD sz_vol, sz_rsv, sz_fat, sz_dir; /* Size for volume, fat, dir, data */
- UINT i;
+ DWORD sz_buf, sz_blk, n_clst, pau, nsect, n, vsn;
+ LBA_t sz_vol, b_vol, b_fat, b_data; /* Size of volume, Base LBA of volume, fat, data */
+ LBA_t sect, lba[2];
+ DWORD sz_rsv, sz_fat, sz_dir, sz_au; /* Size of reserved, fat, dir, data, cluster */
+ UINT n_fat, n_root, i; /* Index, Number of FATs and Number of roor dir entries */
int vol;
- DSTATUS stat;
-#if FF_USE_TRIM || FF_FS_EXFAT
- DWORD tbl[3];
-#endif
+ DSTATUS ds;
+ FRESULT res;
/* Check mounted drive and clear work area */
vol = get_ldnumber(&path); /* Get target logical drive */
if (vol < 0) return FR_INVALID_DRIVE;
- if (FatFs[vol]) FatFs[vol]->fs_type = 0; /* Clear the volume if mounted */
- pdrv = LD2PD(vol); /* Physical drive */
- part = LD2PT(vol); /* Partition (0:create as new, 1-4:get from partition table) */
-
- /* Check physical drive status */
- 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_BLOCK_SIZE, &sz_blk) != RES_OK || !sz_blk || sz_blk > 32768 || (sz_blk & (sz_blk - 1))) sz_blk = 1; /* Erase block to align data area */
-#if FF_MAX_SS != FF_MIN_SS /* Get sector size of the medium if variable sector size cfg. */
+ if (FatFs[vol]) FatFs[vol]->fs_type = 0; /* Clear the fs object if mounted */
+ pdrv = LD2PD(vol); /* Hosting physical drive */
+ ipart = LD2PT(vol); /* Hosting partition (0:create as new, 1..:existing partition) */
+
+ /* Initialize the hosting physical drive */
+ ds = disk_initialize(pdrv);
+ if (ds & STA_NOINIT) return FR_NOT_READY;
+ if (ds & STA_PROTECT) return FR_WRITE_PROTECTED;
+
+ /* Get physical drive parameters (sz_drv, sz_blk and ss) */
+ if (!opt) opt = &defopt; /* Use default parameter if it is not given */
+ sz_blk = opt->align;
+ if (sz_blk == 0) disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk); /* Block size from the paramter or lower layer */
+ if (sz_blk == 0 || sz_blk > 0x8000 || (sz_blk & (sz_blk - 1))) sz_blk = 1; /* Use default if the block size is invalid */
+#if FF_MAX_SS != FF_MIN_SS
if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) return FR_DISK_ERR;
if (ss > FF_MAX_SS || ss < FF_MIN_SS || (ss & (ss - 1))) return FR_DISK_ERR;
#else
ss = FF_MAX_SS;
#endif
- if ((au != 0 && au < ss) || au > 0x1000000 || (au & (au - 1))) return FR_INVALID_PARAMETER; /* Check if au is valid */
- au /= ss; /* Cluster size in unit of sector */
+
+ /* Options for FAT sub-type and FAT parameters */
+ fsopt = opt->fmt & (FM_ANY | FM_SFD);
+ n_fat = (opt->n_fat >= 1 && opt->n_fat <= 2) ? opt->n_fat : 1;
+ n_root = (opt->n_root >= 1 && opt->n_root <= 32768 && (opt->n_root % (ss / SZDIRE)) == 0) ? opt->n_root : 512;
+ sz_au = (opt->au_size <= 0x1000000 && (opt->au_size & (opt->au_size - 1)) == 0) ? opt->au_size : 0;
+ sz_au /= ss; /* Byte --> Sector */
/* Get working buffer */
+ sz_buf = len / ss; /* Size of working buffer [sector] */
+ if (sz_buf == 0) return FR_NOT_ENOUGH_CORE;
+ buf = (BYTE*)work; /* Working buffer */
#if FF_USE_LFN == 3
- if (!work) { /* Use heap memory for working buffer */
- for (szb_buf = MAX_MALLOC, buf = 0; szb_buf >= ss && (buf = ff_memalloc(szb_buf)) == 0; szb_buf /= 2) ;
- sz_buf = szb_buf / ss; /* Size of working buffer (sector) */
- } else
+ if (!buf) buf = ff_memalloc(sz_buf * ss); /* Use heap memory for working buffer */
#endif
- {
- buf = (BYTE*)work; /* Working buffer */
- sz_buf = len / ss; /* Size of working buffer (sector) */
- szb_buf = sz_buf * ss; /* Size of working buffer (byte) */
- }
- if (!buf || sz_buf == 0) return FR_NOT_ENOUGH_CORE;
+ if (!buf) return FR_NOT_ENOUGH_CORE;
/* Determine where the volume to be located (b_vol, sz_vol) */
- if (FF_MULTI_PARTITION && part != 0) {
- /* Get partition information from partition table in the MBR */
+ b_vol = sz_vol = 0;
+ if (FF_MULTI_PARTITION && ipart != 0) { /* Is the volume associated with any specific partition? */
+ /* Get partition location from the existing partition table */
if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Load MBR */
if (ld_word(buf + BS_55AA) != 0xAA55) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if MBR is valid */
- pte = buf + (MBR_Table + (part - 1) * SZ_PTE);
- if (pte[PTE_System] == 0) LEAVE_MKFS(FR_MKFS_ABORTED); /* No partition? */
- b_vol = ld_dword(pte + PTE_StLba); /* Get volume start sector */
- sz_vol = ld_dword(pte + PTE_SizLba); /* Get volume size */
- } else {
- /* Create a single-partition in this function */
+#if FF_LBA64
+ if (buf[MBR_Table + PTE_System] == 0xEE) { /* GPT protective MBR? */
+ DWORD n_ent, ofs;
+ QWORD pt_lba;
+
+ /* Get the partition location from GPT */
+ if (disk_read(pdrv, buf, 1, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Load GPT header sector (next to MBR) */
+ if (!test_gpt_header(buf)) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if GPT header is valid */
+ n_ent = ld_dword(buf + GPTH_PtNum); /* Number of entries */
+ pt_lba = ld_qword(buf + GPTH_PtOfs); /* Table start sector */
+ ofs = i = 0;
+ while (n_ent) { /* Find MS Basic partition with order of ipart */
+ if (ofs == 0 && disk_read(pdrv, buf, pt_lba++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Get PT sector */
+ if (!memcmp(buf + ofs + GPTE_PtGuid, GUID_MS_Basic, 16) && ++i == ipart) { /* MS basic data partition? */
+ b_vol = ld_qword(buf + ofs + GPTE_FstLba);
+ sz_vol = ld_qword(buf + ofs + GPTE_LstLba) - b_vol + 1;
+ break;
+ }
+ n_ent--; ofs = (ofs + SZ_GPTE) % ss; /* Next entry */
+ }
+ if (n_ent == 0) LEAVE_MKFS(FR_MKFS_ABORTED); /* Partition not found */
+ fsopt |= 0x80; /* Partitioning is in GPT */
+ } else
+#endif
+ { /* Get the partition location from MBR partition table */
+ pte = buf + (MBR_Table + (ipart - 1) * SZ_PTE);
+ if (ipart > 4 || pte[PTE_System] == 0) LEAVE_MKFS(FR_MKFS_ABORTED); /* No partition? */
+ b_vol = ld_dword(pte + PTE_StLba); /* Get volume start sector */
+ sz_vol = ld_dword(pte + PTE_SizLba); /* Get volume size */
+ }
+ } else { /* The volume is associated with a physical drive */
if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_vol) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
- b_vol = (opt & FM_SFD) ? 0 : 63; /* Volume start sector */
- if (sz_vol < b_vol) LEAVE_MKFS(FR_MKFS_ABORTED);
- sz_vol -= b_vol; /* Volume size */
+ if (!(fsopt & FM_SFD)) { /* To be partitioned? */
+ /* Create a single-partition on the drive in this function */
+#if FF_LBA64
+ if (sz_vol >= FF_MIN_GPT) { /* Which partition type to create, MBR or GPT? */
+ fsopt |= 0x80; /* Partitioning is in GPT */
+ b_vol = GPT_ALIGN / ss; sz_vol -= b_vol + GPT_ITEMS * SZ_GPTE / ss + 1; /* Estimated partition offset and size */
+ } else
+#endif
+ { /* Partitioning is in MBR */
+ if (sz_vol > N_SEC_TRACK) {
+ b_vol = N_SEC_TRACK; sz_vol -= b_vol; /* Estimated partition offset and size */
+ }
+ }
+ }
}
if (sz_vol < 128) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if volume size is >=128s */
- /* Pre-determine the FAT type */
- do {
- if (FF_FS_EXFAT && (opt & FM_EXFAT)) { /* exFAT possible? */
- if ((opt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || au > 128) { /* exFAT only, vol >= 64Ms or au > 128s ? */
- fmt = FS_EXFAT; break;
+ /* Now start to create an FAT volume at b_vol and sz_vol */
+
+ do { /* Pre-determine the FAT type */
+ if (FF_FS_EXFAT && (fsopt & FM_EXFAT)) { /* exFAT possible? */
+ if ((fsopt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || sz_au > 128) { /* exFAT only, vol >= 64MS or sz_au > 128S ? */
+ fsty = FS_EXFAT; break;
}
}
- if (au > 128) LEAVE_MKFS(FR_INVALID_PARAMETER); /* Too large au for FAT/FAT32 */
- if (opt & FM_FAT32) { /* FAT32 possible? */
- if ((opt & FM_ANY) == FM_FAT32 || !(opt & FM_FAT)) { /* FAT32 only or no-FAT? */
- fmt = FS_FAT32; break;
+#if FF_LBA64
+ if (sz_vol >= 0x100000000) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too large volume for FAT/FAT32 */
+#endif
+ if (sz_au > 128) sz_au = 128; /* Invalid AU for FAT/FAT32? */
+ if (fsopt & FM_FAT32) { /* FAT32 possible? */
+ if (!(fsopt & FM_FAT)) { /* no-FAT? */
+ fsty = FS_FAT32; break;
}
}
- if (!(opt & FM_FAT)) LEAVE_MKFS(FR_INVALID_PARAMETER); /* no-FAT? */
- fmt = FS_FAT16;
+ if (!(fsopt & FM_FAT)) LEAVE_MKFS(FR_INVALID_PARAMETER); /* no-FAT? */
+ fsty = FS_FAT16;
} while (0);
+ vsn = (DWORD)sz_vol + GET_FATTIME(); /* VSN generated from current time and partitiion size */
+
#if FF_FS_EXFAT
- if (fmt == FS_EXFAT) { /* Create an exFAT volume */
- DWORD szb_bit, szb_case, sum, nb, cl;
+ if (fsty == FS_EXFAT) { /* Create an exFAT volume */
+ DWORD szb_bit, szb_case, sum, nbit, clu, clen[3];
WCHAR ch, si;
UINT j, st;
- BYTE b;
- if (sz_vol < 0x1000) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */
+ if (sz_vol < 0x1000) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume for exFAT? */
#if FF_USE_TRIM
- tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1; /* Inform the device the volume area may be erased */
- disk_ioctl(pdrv, CTRL_TRIM, tbl);
+ lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1; /* Inform storage device that the volume area may be erased */
+ disk_ioctl(pdrv, CTRL_TRIM, lba);
#endif
/* Determine FAT location, data location and number of clusters */
- if (au == 0) { /* au auto-selection */
- au = 8;
- if (sz_vol >= 0x80000) au = 64; /* >= 512Ks */
- if (sz_vol >= 0x4000000) au = 256; /* >= 64Ms */
+ if (sz_au == 0) { /* AU auto-selection */
+ sz_au = 8;
+ if (sz_vol >= 0x80000) sz_au = 64; /* >= 512Ks */
+ if (sz_vol >= 0x4000000) sz_au = 256; /* >= 64Ms */
}
b_fat = b_vol + 32; /* FAT start at offset 32 */
- sz_fat = ((sz_vol / au + 2) * 4 + ss - 1) / ss; /* Number of FAT sectors */
- b_data = (b_fat + sz_fat + sz_blk - 1) & ~(sz_blk - 1); /* Align data area to the erase block boundary */
- if (b_data >= sz_vol / 2) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */
- n_clst = (sz_vol - (b_data - b_vol)) / au; /* Number of clusters */
+ sz_fat = (DWORD)((sz_vol / sz_au + 2) * 4 + ss - 1) / ss; /* Number of FAT sectors */
+ b_data = (b_fat + sz_fat + sz_blk - 1) & ~((LBA_t)sz_blk - 1); /* Align data area to the erase block boundary */
+ if (b_data - b_vol >= sz_vol / 2) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */
+ n_clst = (DWORD)((sz_vol - (b_data - b_vol)) / sz_au); /* Number of clusters */
if (n_clst <16) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too few clusters? */
if (n_clst > MAX_EXFAT) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too many clusters? */
- szb_bit = (n_clst + 7) / 8; /* Size of allocation bitmap */
- tbl[0] = (szb_bit + au * ss - 1) / (au * ss); /* Number of allocation bitmap clusters */
+ szb_bit = (n_clst + 7) / 8; /* Size of allocation bitmap */
+ clen[0] = (szb_bit + sz_au * ss - 1) / (sz_au * ss); /* Number of allocation bitmap clusters */
/* Create a compressed up-case table */
- sect = b_data + au * tbl[0]; /* Table start sector */
- sum = 0; /* Table checksum to be stored in the 82 entry */
+ sect = b_data + sz_au * clen[0]; /* Table start sector */
+ sum = 0; /* Table checksum to be stored in the 82 entry */
st = 0; si = 0; i = 0; j = 0; szb_case = 0;
do {
switch (st) {
@@ -5640,10 +6062,10 @@ FRESULT f_mkfs (
}
for (j = 1; (WCHAR)(si + j) && (WCHAR)(si + j) == ff_wtoupper((WCHAR)(si + j)); j++) ; /* Get run length of no-case block */
if (j >= 128) {
- ch = 0xFFFF; st = 2; break; /* Compress the no-case block if run is >= 128 */
+ ch = 0xFFFF; st = 2; break; /* Compress the no-case block if run is >= 128 chars */
}
st = 1; /* Do not compress short run */
- /* go to next case */
+ /* FALLTHROUGH */
case 1:
ch = si++; /* Fill the short run */
if (--j == 0) st = 0;
@@ -5653,25 +6075,24 @@ FRESULT f_mkfs (
ch = (WCHAR)j; si += (WCHAR)j; /* Number of chars to skip */
st = 0;
}
- sum = xsum32(buf[i + 0] = (BYTE)ch, sum); /* Put it into the write buffer */
+ sum = xsum32(buf[i + 0] = (BYTE)ch, sum); /* Put it into the write buffer */
sum = xsum32(buf[i + 1] = (BYTE)(ch >> 8), sum);
i += 2; szb_case += 2;
- if (si == 0 || i == szb_buf) { /* Write buffered data when buffer full or end of process */
+ if (si == 0 || i == sz_buf * ss) { /* Write buffered data when buffer full or end of process */
n = (i + ss - 1) / ss;
if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
sect += n; i = 0;
}
} while (si);
- tbl[1] = (szb_case + au * ss - 1) / (au * ss); /* Number of up-case table clusters */
- tbl[2] = 1; /* Number of root dir clusters */
+ clen[1] = (szb_case + sz_au * ss - 1) / (sz_au * ss); /* Number of up-case table clusters */
+ clen[2] = 1; /* Number of root dir clusters */
/* Initialize the allocation bitmap */
- sect = b_data; nsect = (szb_bit + ss - 1) / ss; /* Start of bitmap and number of sectors */
- nb = tbl[0] + tbl[1] + tbl[2]; /* Number of clusters in-use by system */
+ sect = b_data; nsect = (szb_bit + ss - 1) / ss; /* Start of bitmap and number of bitmap sectors */
+ nbit = clen[0] + clen[1] + clen[2]; /* Number of clusters in-use by system (bitmap, up-case and root-dir) */
do {
- mem_set(buf, 0, szb_buf);
- for (i = 0; nb >= 8 && i < szb_buf; buf[i++] = 0xFF, nb -= 8) ;
- for (b = 1; nb != 0 && i < szb_buf; buf[i] |= b, b <<= 1, nb--) ;
+ memset(buf, 0, sz_buf * ss); /* Initialize bitmap buffer */
+ for (i = 0; nbit != 0 && i / 8 < sz_buf * ss; buf[i / 8] |= 1 << (i % 8), i++, nbit--) ; /* Mark used clusters */
n = (nsect > sz_buf) ? sz_buf : nsect; /* Write the buffered data */
if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
sect += n; nsect -= n;
@@ -5679,40 +6100,40 @@ FRESULT f_mkfs (
/* Initialize the FAT */
sect = b_fat; nsect = sz_fat; /* Start of FAT and number of FAT sectors */
- j = nb = cl = 0;
+ j = nbit = clu = 0;
do {
- mem_set(buf, 0, szb_buf); i = 0; /* Clear work area and reset write index */
- if (cl == 0) { /* Set entry 0 and 1 */
- st_dword(buf + i, 0xFFFFFFF8); i += 4; cl++;
- st_dword(buf + i, 0xFFFFFFFF); i += 4; cl++;
+ memset(buf, 0, sz_buf * ss); i = 0; /* Clear work area and reset write offset */
+ if (clu == 0) { /* Initialize FAT [0] and FAT[1] */
+ st_dword(buf + i, 0xFFFFFFF8); i += 4; clu++;
+ st_dword(buf + i, 0xFFFFFFFF); i += 4; clu++;
}
do { /* Create chains of bitmap, up-case and root dir */
- while (nb != 0 && i < szb_buf) { /* Create a chain */
- st_dword(buf + i, (nb > 1) ? cl + 1 : 0xFFFFFFFF);
- i += 4; cl++; nb--;
+ while (nbit != 0 && i < sz_buf * ss) { /* Create a chain */
+ st_dword(buf + i, (nbit > 1) ? clu + 1 : 0xFFFFFFFF);
+ i += 4; clu++; nbit--;
}
- if (nb == 0 && j < 3) nb = tbl[j++]; /* Next chain */
- } while (nb != 0 && i < szb_buf);
+ if (nbit == 0 && j < 3) nbit = clen[j++]; /* Get next chain length */
+ } while (nbit != 0 && i < sz_buf * ss);
n = (nsect > sz_buf) ? sz_buf : nsect; /* Write the buffered data */
if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
sect += n; nsect -= n;
} while (nsect);
/* Initialize the root directory */
- mem_set(buf, 0, szb_buf);
- buf[SZDIRE * 0 + 0] = 0x83; /* 83 entry (volume label) */
- buf[SZDIRE * 1 + 0] = 0x81; /* 81 entry (allocation bitmap) */
- st_dword(buf + SZDIRE * 1 + 20, 2); /* cluster */
- st_dword(buf + SZDIRE * 1 + 24, szb_bit); /* size */
- buf[SZDIRE * 2 + 0] = 0x82; /* 82 entry (up-case table) */
- st_dword(buf + SZDIRE * 2 + 4, sum); /* sum */
- st_dword(buf + SZDIRE * 2 + 20, 2 + tbl[0]); /* cluster */
- st_dword(buf + SZDIRE * 2 + 24, szb_case); /* size */
- sect = b_data + au * (tbl[0] + tbl[1]); nsect = au; /* Start of the root directory and number of sectors */
+ memset(buf, 0, sz_buf * ss);
+ buf[SZDIRE * 0 + 0] = ET_VLABEL; /* Volume label entry (no label) */
+ buf[SZDIRE * 1 + 0] = ET_BITMAP; /* Bitmap entry */
+ st_dword(buf + SZDIRE * 1 + 20, 2); /* cluster */
+ st_dword(buf + SZDIRE * 1 + 24, szb_bit); /* size */
+ buf[SZDIRE * 2 + 0] = ET_UPCASE; /* Up-case table entry */
+ st_dword(buf + SZDIRE * 2 + 4, sum); /* sum */
+ st_dword(buf + SZDIRE * 2 + 20, 2 + clen[0]); /* cluster */
+ st_dword(buf + SZDIRE * 2 + 24, szb_case); /* size */
+ sect = b_data + sz_au * (clen[0] + clen[1]); nsect = sz_au; /* Start of the root directory and number of sectors */
do { /* Fill root directory sectors */
n = (nsect > sz_buf) ? sz_buf : nsect;
if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
- mem_set(buf, 0, ss);
+ memset(buf, 0, ss); /* Rest of entries are filled with zero */
sect += n; nsect -= n;
} while (nsect);
@@ -5720,19 +6141,19 @@ FRESULT f_mkfs (
sect = b_vol;
for (n = 0; n < 2; n++) {
/* Main record (+0) */
- mem_set(buf, 0, ss);
- mem_cpy(buf + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11); /* Boot jump code (x86), OEM name */
- st_dword(buf + BPB_VolOfsEx, b_vol); /* Volume offset in the physical drive [sector] */
- st_dword(buf + BPB_TotSecEx, sz_vol); /* Volume size [sector] */
- st_dword(buf + BPB_FatOfsEx, b_fat - b_vol); /* FAT offset [sector] */
+ memset(buf, 0, ss);
+ memcpy(buf + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11); /* Boot jump code (x86), OEM name */
+ st_qword(buf + BPB_VolOfsEx, b_vol); /* Volume offset in the physical drive [sector] */
+ st_qword(buf + BPB_TotSecEx, sz_vol); /* Volume size [sector] */
+ st_dword(buf + BPB_FatOfsEx, (DWORD)(b_fat - b_vol)); /* FAT offset [sector] */
st_dword(buf + BPB_FatSzEx, sz_fat); /* FAT size [sector] */
- st_dword(buf + BPB_DataOfsEx, b_data - b_vol); /* Data offset [sector] */
+ st_dword(buf + BPB_DataOfsEx, (DWORD)(b_data - b_vol)); /* Data offset [sector] */
st_dword(buf + BPB_NumClusEx, n_clst); /* Number of clusters */
- st_dword(buf + BPB_RootClusEx, 2 + tbl[0] + tbl[1]); /* Root dir cluster # */
- st_dword(buf + BPB_VolIDEx, GET_FATTIME()); /* VSN */
+ st_dword(buf + BPB_RootClusEx, 2 + clen[0] + clen[1]); /* Root dir cluster # */
+ st_dword(buf + BPB_VolIDEx, vsn); /* VSN */
st_word(buf + BPB_FSVerEx, 0x100); /* Filesystem version (1.00) */
for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ; /* Log2 of sector size [byte] */
- for (buf[BPB_SecPerClusEx] = 0, i = au; i >>= 1; buf[BPB_SecPerClusEx]++) ; /* Log2 of cluster size [sector] */
+ for (buf[BPB_SecPerClusEx] = 0, i = sz_au; i >>= 1; buf[BPB_SecPerClusEx]++) ; /* Log2 of cluster size [sector] */
buf[BPB_NumFATsEx] = 1; /* Number of FATs */
buf[BPB_DrvNumEx] = 0x80; /* Drive number (for int13) */
st_word(buf + BS_BootCodeEx, 0xFEEB); /* Boot code (x86) */
@@ -5742,14 +6163,14 @@ FRESULT f_mkfs (
}
if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
/* Extended bootstrap record (+1..+8) */
- mem_set(buf, 0, ss);
+ memset(buf, 0, ss);
st_word(buf + ss - 2, 0xAA55); /* Signature (placed at end of sector) */
for (j = 1; j < 9; j++) {
for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ; /* VBR checksum */
if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
}
/* OEM/Reserved record (+9..+10) */
- mem_set(buf, 0, ss);
+ memset(buf, 0, ss);
for ( ; j < 11; j++) {
for (i = 0; i < ss; sum = xsum32(buf[i++], sum)) ; /* VBR checksum */
if (disk_write(pdrv, buf, sect++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
@@ -5763,120 +6184,123 @@ FRESULT f_mkfs (
#endif /* FF_FS_EXFAT */
{ /* Create an FAT/FAT32 volume */
do {
- pau = au;
+ pau = sz_au;
/* Pre-determine number of clusters and FAT sub-type */
- if (fmt == FS_FAT32) { /* FAT32 volume */
- if (pau == 0) { /* au auto-selection */
- n = sz_vol / 0x20000; /* Volume size in unit of 128KS */
+ if (fsty == FS_FAT32) { /* FAT32 volume */
+ if (pau == 0) { /* AU auto-selection */
+ n = (DWORD)sz_vol / 0x20000; /* Volume size in unit of 128KS */
for (i = 0, pau = 1; cst32[i] && cst32[i] <= n; i++, pau <<= 1) ; /* Get from table */
}
- n_clst = sz_vol / pau; /* Number of clusters */
+ n_clst = (DWORD)sz_vol / pau; /* Number of clusters */
sz_fat = (n_clst * 4 + 8 + ss - 1) / ss; /* FAT size [sector] */
sz_rsv = 32; /* Number of reserved sectors */
sz_dir = 0; /* No static directory */
if (n_clst <= MAX_FAT16 || n_clst > MAX_FAT32) LEAVE_MKFS(FR_MKFS_ABORTED);
} else { /* FAT volume */
if (pau == 0) { /* au auto-selection */
- n = sz_vol / 0x1000; /* Volume size in unit of 4KS */
+ n = (DWORD)sz_vol / 0x1000; /* Volume size in unit of 4KS */
for (i = 0, pau = 1; cst[i] && cst[i] <= n; i++, pau <<= 1) ; /* Get from table */
}
- n_clst = sz_vol / pau;
+ n_clst = (DWORD)sz_vol / pau;
if (n_clst > MAX_FAT12) {
n = n_clst * 2 + 4; /* FAT size [byte] */
} else {
- fmt = FS_FAT12;
+ fsty = FS_FAT12;
n = (n_clst * 3 + 1) / 2 + 3; /* FAT size [byte] */
}
sz_fat = (n + ss - 1) / ss; /* FAT size [sector] */
sz_rsv = 1; /* Number of reserved sectors */
- sz_dir = (DWORD)n_rootdir * SZDIRE / ss; /* Rootdir size [sector] */
+ sz_dir = (DWORD)n_root * SZDIRE / ss; /* Root dir size [sector] */
}
b_fat = b_vol + sz_rsv; /* FAT base */
- b_data = b_fat + sz_fat * n_fats + sz_dir; /* Data base */
+ b_data = b_fat + sz_fat * n_fat + sz_dir; /* Data base */
- /* Align data base to erase block boundary (for flash memory media) */
- n = ((b_data + sz_blk - 1) & ~(sz_blk - 1)) - b_data; /* Next nearest erase block from current data base */
- if (fmt == FS_FAT32) { /* FAT32: Move FAT base */
+ /* Align data area to erase block boundary (for flash memory media) */
+ n = (DWORD)(((b_data + sz_blk - 1) & ~(sz_blk - 1)) - b_data); /* Sectors to next nearest from current data base */
+ if (fsty == FS_FAT32) { /* FAT32: Move FAT */
sz_rsv += n; b_fat += n;
- } else { /* FAT: Expand FAT size */
- sz_fat += n / n_fats;
+ } else { /* FAT: Expand FAT */
+ if (n % n_fat) { /* Adjust fractional error if needed */
+ n--; sz_rsv++; b_fat++;
+ }
+ sz_fat += n / n_fat;
}
/* Determine number of clusters and final check of validity of the FAT sub-type */
- if (sz_vol < b_data + pau * 16 - b_vol) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume */
- n_clst = (sz_vol - sz_rsv - sz_fat * n_fats - sz_dir) / pau;
- if (fmt == FS_FAT32) {
- if (n_clst <= MAX_FAT16) { /* Too few clusters for FAT32 */
- if (au == 0 && (au = pau / 2) != 0) continue; /* Adjust cluster size and retry */
+ if (sz_vol < b_data + pau * 16 - b_vol) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */
+ n_clst = ((DWORD)sz_vol - sz_rsv - sz_fat * n_fat - sz_dir) / pau;
+ if (fsty == FS_FAT32) {
+ if (n_clst <= MAX_FAT16) { /* Too few clusters for FAT32? */
+ if (sz_au == 0 && (sz_au = pau / 2) != 0) continue; /* Adjust cluster size and retry */
LEAVE_MKFS(FR_MKFS_ABORTED);
}
}
- if (fmt == FS_FAT16) {
+ if (fsty == FS_FAT16) {
if (n_clst > MAX_FAT16) { /* Too many clusters for FAT16 */
- if (au == 0 && (pau * 2) <= 64) {
- au = pau * 2; continue; /* Adjust cluster size and retry */
+ if (sz_au == 0 && (pau * 2) <= 64) {
+ sz_au = pau * 2; continue; /* Adjust cluster size and retry */
}
- if ((opt & FM_FAT32)) {
- fmt = FS_FAT32; continue; /* Switch type to FAT32 and retry */
+ if ((fsopt & FM_FAT32)) {
+ fsty = FS_FAT32; continue; /* Switch type to FAT32 and retry */
}
- if (au == 0 && (au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */
+ if (sz_au == 0 && (sz_au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */
LEAVE_MKFS(FR_MKFS_ABORTED);
}
if (n_clst <= MAX_FAT12) { /* Too few clusters for FAT16 */
- if (au == 0 && (au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */
+ if (sz_au == 0 && (sz_au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */
LEAVE_MKFS(FR_MKFS_ABORTED);
}
}
- if (fmt == FS_FAT12 && n_clst > MAX_FAT12) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too many clusters for FAT12 */
+ if (fsty == FS_FAT12 && n_clst > MAX_FAT12) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too many clusters for FAT12 */
/* Ok, it is the valid cluster configuration */
break;
} while (1);
#if FF_USE_TRIM
- tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1; /* Inform the device the volume area can be erased */
- disk_ioctl(pdrv, CTRL_TRIM, tbl);
+ lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1; /* Inform storage device that the volume area may be erased */
+ disk_ioctl(pdrv, CTRL_TRIM, lba);
#endif
/* Create FAT VBR */
- mem_set(buf, 0, ss);
- mem_cpy(buf + BS_JmpBoot, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code (x86), OEM name */
+ memset(buf, 0, ss);
+ memcpy(buf + BS_JmpBoot, "\xEB\xFE\x90" "MSDOS5.0", 11); /* Boot jump code (x86), OEM name */
st_word(buf + BPB_BytsPerSec, ss); /* Sector size [byte] */
buf[BPB_SecPerClus] = (BYTE)pau; /* Cluster size [sector] */
st_word(buf + BPB_RsvdSecCnt, (WORD)sz_rsv); /* Size of reserved area */
- buf[BPB_NumFATs] = (BYTE)n_fats; /* Number of FATs */
- st_word(buf + BPB_RootEntCnt, (WORD)((fmt == FS_FAT32) ? 0 : n_rootdir)); /* Number of root directory entries */
+ buf[BPB_NumFATs] = (BYTE)n_fat; /* Number of FATs */
+ st_word(buf + BPB_RootEntCnt, (WORD)((fsty == FS_FAT32) ? 0 : n_root)); /* Number of root directory entries */
if (sz_vol < 0x10000) {
st_word(buf + BPB_TotSec16, (WORD)sz_vol); /* Volume size in 16-bit LBA */
} else {
- st_dword(buf + BPB_TotSec32, sz_vol); /* Volume size in 32-bit LBA */
+ st_dword(buf + BPB_TotSec32, (DWORD)sz_vol); /* Volume size in 32-bit LBA */
}
buf[BPB_Media] = 0xF8; /* Media descriptor byte */
st_word(buf + BPB_SecPerTrk, 63); /* Number of sectors per track (for int13) */
st_word(buf + BPB_NumHeads, 255); /* Number of heads (for int13) */
- st_dword(buf + BPB_HiddSec, b_vol); /* Volume offset in the physical drive [sector] */
- if (fmt == FS_FAT32) {
- st_dword(buf + BS_VolID32, GET_FATTIME()); /* VSN */
+ st_dword(buf + BPB_HiddSec, (DWORD)b_vol); /* Volume offset in the physical drive [sector] */
+ if (fsty == FS_FAT32) {
+ st_dword(buf + BS_VolID32, vsn); /* VSN */
st_dword(buf + BPB_FATSz32, sz_fat); /* FAT size [sector] */
st_dword(buf + BPB_RootClus32, 2); /* Root directory cluster # (2) */
st_word(buf + BPB_FSInfo32, 1); /* Offset of FSINFO sector (VBR + 1) */
st_word(buf + BPB_BkBootSec32, 6); /* Offset of backup VBR (VBR + 6) */
buf[BS_DrvNum32] = 0x80; /* Drive number (for int13) */
buf[BS_BootSig32] = 0x29; /* Extended boot signature */
- mem_cpy(buf + BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */
+ memcpy(buf + BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */
} else {
- st_dword(buf + BS_VolID, GET_FATTIME()); /* VSN */
+ st_dword(buf + BS_VolID, vsn); /* VSN */
st_word(buf + BPB_FATSz16, (WORD)sz_fat); /* FAT size [sector] */
buf[BS_DrvNum] = 0x80; /* Drive number (for int13) */
buf[BS_BootSig] = 0x29; /* Extended boot signature */
- mem_cpy(buf + BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */
+ memcpy(buf + BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */
}
st_word(buf + BS_55AA, 0xAA55); /* Signature (offset is fixed here regardless of sector size) */
if (disk_write(pdrv, buf, b_vol, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it to the VBR sector */
/* Create FSINFO record if needed */
- if (fmt == FS_FAT32) {
+ if (fsty == FS_FAT32) {
disk_write(pdrv, buf, b_vol + 6, 1); /* Write backup VBR (VBR + 6) */
- mem_set(buf, 0, ss);
+ memset(buf, 0, ss);
st_dword(buf + FSI_LeadSig, 0x41615252);
st_dword(buf + FSI_StrucSig, 0x61417272);
st_dword(buf + FSI_Free_Count, n_clst - 1); /* Number of free clusters */
@@ -5887,27 +6311,27 @@ FRESULT f_mkfs (
}
/* Initialize FAT area */
- mem_set(buf, 0, (UINT)szb_buf);
+ memset(buf, 0, sz_buf * ss);
sect = b_fat; /* FAT start sector */
- for (i = 0; i < n_fats; i++) { /* Initialize FATs each */
- if (fmt == FS_FAT32) {
- st_dword(buf + 0, 0xFFFFFFF8); /* Entry 0 */
- st_dword(buf + 4, 0xFFFFFFFF); /* Entry 1 */
- st_dword(buf + 8, 0x0FFFFFFF); /* Entry 2 (root directory) */
+ for (i = 0; i < n_fat; i++) { /* Initialize FATs each */
+ if (fsty == FS_FAT32) {
+ st_dword(buf + 0, 0xFFFFFFF8); /* FAT[0] */
+ st_dword(buf + 4, 0xFFFFFFFF); /* FAT[1] */
+ st_dword(buf + 8, 0x0FFFFFFF); /* FAT[2] (root directory) */
} else {
- st_dword(buf + 0, (fmt == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* Entry 0 and 1 */
+ st_dword(buf + 0, (fsty == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* FAT[0] and FAT[1] */
}
nsect = sz_fat; /* Number of FAT sectors */
do { /* Fill FAT sectors */
n = (nsect > sz_buf) ? sz_buf : nsect;
if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
- mem_set(buf, 0, ss);
+ memset(buf, 0, ss); /* Rest of FAT all are cleared */
sect += n; nsect -= n;
} while (nsect);
}
/* Initialize root directory (fill with zero) */
- nsect = (fmt == FS_FAT32) ? pau : sz_dir; /* Number of root directory sectors */
+ nsect = (fsty == FS_FAT32) ? pau : sz_dir; /* Number of root directory sectors */
do {
n = (nsect > sz_buf) ? sz_buf : nsect;
if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
@@ -5915,44 +6339,34 @@ FRESULT f_mkfs (
} while (nsect);
}
- /* Determine system ID in the partition table */
- if (FF_FS_EXFAT && fmt == FS_EXFAT) {
- sys = 0x07; /* HPFS/NTFS/exFAT */
+ /* A FAT volume has been created here */
+
+ /* Determine system ID in the MBR partition table */
+ if (FF_FS_EXFAT && fsty == FS_EXFAT) {
+ sys = 0x07; /* exFAT */
+ } else if (fsty == FS_FAT32) {
+ sys = 0x0C; /* FAT32X */
+ } else if (sz_vol >= 0x10000) {
+ sys = 0x06; /* FAT12/16 (large) */
+ } else if (fsty == FS_FAT16) {
+ sys = 0x04; /* FAT16 */
} else {
- if (fmt == FS_FAT32) {
- sys = 0x0C; /* FAT32X */
- } else {
- if (sz_vol >= 0x10000) {
- sys = 0x06; /* FAT12/16 (large) */
- } else {
- sys = (fmt == FS_FAT16) ? 0x04 : 0x01; /* FAT16 : FAT12 */
- }
- }
+ sys = 0x01; /* FAT12 */
}
/* Update partition information */
- if (FF_MULTI_PARTITION && part != 0) { /* Created in the existing partition */
- /* Update system ID in the partition table */
- if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Read the MBR */
- buf[MBR_Table + (part - 1) * SZ_PTE + PTE_System] = sys; /* Set system ID */
- if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it back to the MBR */
- } else { /* Created as a new single partition */
- if (!(opt & FM_SFD)) { /* Create partition table if in FDISK format */
- mem_set(buf, 0, ss);
- st_word(buf + BS_55AA, 0xAA55); /* MBR signature */
- pte = buf + MBR_Table; /* Create partition table for single partition in the drive */
- pte[PTE_Boot] = 0; /* Boot indicator */
- pte[PTE_StHead] = 1; /* Start head */
- pte[PTE_StSec] = 1; /* Start sector */
- pte[PTE_StCyl] = 0; /* Start cylinder */
- pte[PTE_System] = sys; /* System type */
- n = (b_vol + sz_vol) / (63 * 255); /* (End CHS may be invalid) */
- pte[PTE_EdHead] = 254; /* End head */
- pte[PTE_EdSec] = (BYTE)(((n >> 2) & 0xC0) | 63); /* End sector */
- pte[PTE_EdCyl] = (BYTE)n; /* End cylinder */
- st_dword(pte + PTE_StLba, b_vol); /* Start offset in LBA */
- st_dword(pte + PTE_SizLba, sz_vol); /* Size in sectors */
- if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it to the MBR */
+ if (FF_MULTI_PARTITION && ipart != 0) { /* Volume is in the existing partition */
+ if (!FF_LBA64 || !(fsopt & 0x80)) { /* Is the partition in MBR? */
+ /* Update system ID in the partition table */
+ if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Read the MBR */
+ buf[MBR_Table + (ipart - 1) * SZ_PTE + PTE_System] = sys; /* Set system ID */
+ if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it back to the MBR */
+ }
+ } else { /* Volume as a new single partition */
+ if (!(fsopt & FM_SFD)) { /* Create partition table if not in SFD format */
+ lba[0] = sz_vol; lba[1] = 0;
+ res = create_partition(pdrv, lba, sys, buf);
+ if (res != FR_OK) LEAVE_MKFS(res);
}
}
@@ -5963,6 +6377,7 @@ FRESULT f_mkfs (
+
#if FF_MULTI_PARTITION
/*-----------------------------------------------------------------------*/
/* Create Partition Table on the Physical Drive */
@@ -5970,75 +6385,32 @@ FRESULT f_mkfs (
FRESULT f_fdisk (
BYTE pdrv, /* Physical drive number */
- const DWORD* szt, /* Pointer to the size table for each partitions */
+ const LBA_t ptbl[], /* Pointer to the size table for each partitions */
void* work /* Pointer to the working buffer (null: use heap memory) */
)
{
- UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl;
- BYTE s_hd, e_hd, *p, *buf = (BYTE*)work;
+ BYTE *buf = (BYTE*)work;
DSTATUS stat;
- DWORD sz_disk, sz_part, s_part;
FRESULT res;
+ /* Initialize the physical drive */
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;
- buf = (BYTE*)work;
#if FF_USE_LFN == 3
if (!buf) buf = ff_memalloc(FF_MAX_SS); /* Use heap memory for working buffer */
#endif
if (!buf) return FR_NOT_ENOUGH_CORE;
- /* Determine the CHS without any consideration of the drive geometry */
- for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ;
- if (n == 256) n--;
- e_hd = (BYTE)(n - 1);
- sz_cyl = 63 * n;
- tot_cyl = sz_disk / sz_cyl;
-
- /* Create partition table */
- mem_set(buf, 0, FF_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; /* Number of cylinders */
- if (p_cyl == 0) continue;
- s_part = (DWORD)sz_cyl * b_cyl;
- sz_part = (DWORD)sz_cyl * p_cyl;
- if (i == 0) { /* Exclude first track of cylinder 0 */
- s_hd = 1;
- s_part += 63; sz_part -= 63;
- } else {
- s_hd = 0;
- }
- e_cyl = b_cyl + p_cyl - 1; /* End cylinder */
- if (e_cyl >= tot_cyl) LEAVE_MKFS(FR_INVALID_PARAMETER);
-
- /* Set partition table */
- p[1] = s_hd; /* Start head */
- p[2] = (BYTE)(((b_cyl >> 2) & 0xC0) | 1); /* Start sector */
- p[3] = (BYTE)b_cyl; /* Start cylinder */
- p[4] = 0x07; /* System type (temporary setting) */
- p[5] = e_hd; /* End head */
- p[6] = (BYTE)(((e_cyl >> 2) & 0xC0) | 63); /* End sector */
- p[7] = (BYTE)e_cyl; /* End cylinder */
- st_dword(p + 8, s_part); /* Start sector in LBA */
- st_dword(p + 12, sz_part); /* Number of sectors */
+ res = create_partition(pdrv, ptbl, 0x07, buf); /* Create partitions (system ID is temporary setting and determined by f_mkfs) */
- /* Next partition */
- b_cyl += p_cyl;
- }
- st_word(p, 0xAA55); /* MBR signature (always at offset 510) */
-
- /* Write it to the MBR */
- res = (disk_write(pdrv, buf, 0, 1) == RES_OK && disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR;
LEAVE_MKFS(res);
}
#endif /* FF_MULTI_PARTITION */
-#endif /* FF_USE_MKFS && !FF_FS_READONLY */
+#endif /* !FF_FS_READONLY && FF_USE_MKFS */
@@ -6052,7 +6424,7 @@ FRESULT f_fdisk (
/*-----------------------------------------------------------------------*/
TCHAR* f_gets (
- TCHAR* buff, /* Pointer to the string buffer to read */
+ TCHAR* buff, /* Pointer to the buffer to store read string */
int len, /* Size of string buffer (items) */
FIL* fp /* Pointer to the file object */
)
@@ -6075,49 +6447,57 @@ TCHAR* f_gets (
if (FF_LFN_UNICODE == 2) len -= (FF_STRF_ENCODE == 0) ? 3 : 4;
if (FF_LFN_UNICODE == 3) len -= 1;
while (nc < len) {
-#if FF_STRF_ENCODE == 0 /* Read a character in ANSI/OEM */
- f_read(fp, s, 1, &rc);
- if (rc != 1) break;
+#if FF_STRF_ENCODE == 0 /* Read a character in ANSI/OEM */
+ f_read(fp, s, 1, &rc); /* Get a code unit */
+ if (rc != 1) break; /* EOF? */
wc = s[0];
- if (dbc_1st((BYTE)wc)) {
- f_read(fp, s, 1, &rc);
- if (rc != 1 || !dbc_2nd(s[0])) continue;
+ if (dbc_1st((BYTE)wc)) { /* DBC 1st byte? */
+ f_read(fp, s, 1, &rc); /* Get 2nd byte */
+ if (rc != 1 || !dbc_2nd(s[0])) continue; /* Wrong code? */
wc = wc << 8 | s[0];
}
- dc = ff_oem2uni(wc, CODEPAGE);
- if (dc == 0) continue;
+ dc = ff_oem2uni(wc, CODEPAGE); /* Convert ANSI/OEM into Unicode */
+ if (dc == 0) continue; /* Conversion error? */
#elif FF_STRF_ENCODE == 1 || FF_STRF_ENCODE == 2 /* Read a character in UTF-16LE/BE */
- f_read(fp, s, 2, &rc);
- if (rc != 2) break;
+ f_read(fp, s, 2, &rc); /* Get a code unit */
+ if (rc != 2) break; /* EOF? */
dc = (FF_STRF_ENCODE == 1) ? ld_word(s) : s[0] << 8 | s[1];
- if (IsSurrogateL(dc)) continue;
- if (IsSurrogateH(dc)) {
- f_read(fp, s, 2, &rc);
- if (rc != 2) break;
+ if (IsSurrogateL(dc)) continue; /* Broken surrogate pair? */
+ if (IsSurrogateH(dc)) { /* High surrogate? */
+ f_read(fp, s, 2, &rc); /* Get low surrogate */
+ if (rc != 2) break; /* EOF? */
wc = (FF_STRF_ENCODE == 1) ? ld_word(s) : s[0] << 8 | s[1];
- if (!IsSurrogateL(wc)) continue;
- dc = ((dc & 0x3FF) + 0x40) << 10 | (wc & 0x3FF);
+ if (!IsSurrogateL(wc)) continue; /* Broken surrogate pair? */
+ dc = ((dc & 0x3FF) + 0x40) << 10 | (wc & 0x3FF); /* Merge surrogate pair */
}
#else /* Read a character in UTF-8 */
- f_read(fp, s, 1, &rc);
- if (rc != 1) break;
+ f_read(fp, s, 1, &rc); /* Get a code unit */
+ if (rc != 1) break; /* EOF? */
dc = s[0];
- if (dc >= 0x80) { /* Multi-byte character? */
+ if (dc >= 0x80) { /* Multi-byte sequence? */
ct = 0;
- if ((dc & 0xE0) == 0xC0) { dc &= 0x1F; ct = 1; } /* 2-byte? */
- if ((dc & 0xF0) == 0xE0) { dc &= 0x0F; ct = 2; } /* 3-byte? */
- if ((dc & 0xF8) == 0xF0) { dc &= 0x07; ct = 3; } /* 4-byte? */
+ if ((dc & 0xE0) == 0xC0) { /* 2-byte sequence? */
+ dc &= 0x1F; ct = 1;
+ }
+ if ((dc & 0xF0) == 0xE0) { /* 3-byte sequence? */
+ dc &= 0x0F; ct = 2;
+ }
+ if ((dc & 0xF8) == 0xF0) { /* 4-byte sequence? */
+ dc &= 0x07; ct = 3;
+ }
if (ct == 0) continue;
- f_read(fp, s, ct, &rc); /* Get trailing bytes */
+ f_read(fp, s, ct, &rc); /* Get trailing bytes */
if (rc != ct) break;
rc = 0;
- do { /* Merge trailing bytes */
+ do { /* Merge the byte sequence */
if ((s[rc] & 0xC0) != 0x80) break;
dc = dc << 6 | (s[rc] & 0x3F);
} while (++rc < ct);
if (rc != ct || dc < 0x80 || IsSurrogate(dc) || dc >= 0x110000) continue; /* Wrong encoding? */
}
#endif
+ /* A code point is avaialble in dc to be output */
+
if (FF_USE_STRFUNC == 2 && dc == '\r') continue; /* Strip \r off if needed */
#if FF_LFN_UNICODE == 1 || FF_LFN_UNICODE == 3 /* Output it in UTF-16/32 encoding */
if (FF_LFN_UNICODE == 1 && dc >= 0x10000) { /* Out of BMP at UTF-16? */
@@ -6127,38 +6507,34 @@ TCHAR* f_gets (
*p++ = (TCHAR)dc; nc++;
if (dc == '\n') break; /* End of line? */
#elif FF_LFN_UNICODE == 2 /* Output it in UTF-8 encoding */
- if (dc < 0x80) { /* 1-byte */
+ if (dc < 0x80) { /* Single byte? */
*p++ = (TCHAR)dc;
nc++;
if (dc == '\n') break; /* End of line? */
- } else {
- if (dc < 0x800) { /* 2-byte */
- *p++ = (TCHAR)(0xC0 | (dc >> 6 & 0x1F));
- *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));
- nc += 2;
- } else {
- if (dc < 0x10000) { /* 3-byte */
- *p++ = (TCHAR)(0xE0 | (dc >> 12 & 0x0F));
- *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F));
- *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));
- nc += 3;
- } else { /* 4-byte */
- *p++ = (TCHAR)(0xF0 | (dc >> 18 & 0x07));
- *p++ = (TCHAR)(0x80 | (dc >> 12 & 0x3F));
- *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F));
- *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));
- nc += 4;
- }
- }
- }
-#endif
- }
-
-#else /* Byte-by-byte without any conversion (ANSI/OEM API) */
+ } else if (dc < 0x800) { /* 2-byte sequence? */
+ *p++ = (TCHAR)(0xC0 | (dc >> 6 & 0x1F));
+ *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));
+ nc += 2;
+ } else if (dc < 0x10000) { /* 3-byte sequence? */
+ *p++ = (TCHAR)(0xE0 | (dc >> 12 & 0x0F));
+ *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F));
+ *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));
+ nc += 3;
+ } else { /* 4-byte sequence */
+ *p++ = (TCHAR)(0xF0 | (dc >> 18 & 0x07));
+ *p++ = (TCHAR)(0x80 | (dc >> 12 & 0x3F));
+ *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F));
+ *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F));
+ nc += 4;
+ }
+#endif
+ }
+
+#else /* Byte-by-byte read without any conversion (ANSI/OEM API) */
len -= 1; /* Make a room for the terminator */
while (nc < len) {
- f_read(fp, s, 1, &rc);
- if (rc != 1) break;
+ f_read(fp, s, 1, &rc); /* Get a byte */
+ if (rc != 1) break; /* EOF? */
dc = s[0];
if (FF_USE_STRFUNC == 2 && dc == '\r') continue;
*p++ = (TCHAR)dc; nc++;
@@ -6175,11 +6551,16 @@ TCHAR* f_gets (
#if !FF_FS_READONLY
#include <stdarg.h>
+#define SZ_PUTC_BUF 64
+#define SZ_NUM_BUF 32
+
/*-----------------------------------------------------------------------*/
-/* Put a Character to the File */
+/* Put a Character to the File (with sub-functions) */
/*-----------------------------------------------------------------------*/
-typedef struct { /* Putchar output buffer and work area */
+/* Output buffer and work area */
+
+typedef struct {
FIL *fp; /* Ptr to the writing file */
int idx, nchr; /* Write index of buf[] (-1:error), number of encoding units written */
#if FF_USE_LFN && FF_LFN_UNICODE == 1
@@ -6188,15 +6569,13 @@ typedef struct { /* Putchar output buffer and work area */
BYTE bs[4];
UINT wi, ct;
#endif
- BYTE buf[64]; /* Write buffer */
+ BYTE buf[SZ_PUTC_BUF]; /* Write buffer */
} putbuff;
-static
-void putc_bfd ( /* Buffered write with code conversion */
- putbuff* pb,
- TCHAR c
-)
+/* Buffered file write with code conversion */
+
+static void putc_bfd (putbuff* pb, TCHAR c)
{
UINT n;
int i, nc;
@@ -6204,7 +6583,7 @@ void putc_bfd ( /* Buffered write with code conversion */
WCHAR hs, wc;
#if FF_LFN_UNICODE == 2
DWORD dc;
- TCHAR *tp;
+ const TCHAR* tp;
#endif
#endif
@@ -6213,47 +6592,47 @@ void putc_bfd ( /* Buffered write with code conversion */
}
i = pb->idx; /* Write index of pb->buf[] */
- if (i < 0) return;
+ if (i < 0) return; /* In write error? */
nc = pb->nchr; /* Write unit counter */
#if FF_USE_LFN && FF_LFN_UNICODE
#if FF_LFN_UNICODE == 1 /* UTF-16 input */
- if (IsSurrogateH(c)) {
- pb->hs = c; return;
+ if (IsSurrogateH(c)) { /* Is this a high-surrogate? */
+ pb->hs = c; return; /* Save it for next */
}
hs = pb->hs; pb->hs = 0;
- if (hs != 0) {
- if (!IsSurrogateL(c)) hs = 0;
+ if (hs != 0) { /* Is there a leading high-surrogate? */
+ if (!IsSurrogateL(c)) hs = 0; /* Discard high-surrogate if not a surrogate pair */
} else {
- if (IsSurrogateL(c)) return;
+ if (IsSurrogateL(c)) return; /* Discard stray low-surrogate */
}
wc = c;
#elif FF_LFN_UNICODE == 2 /* UTF-8 input */
for (;;) {
if (pb->ct == 0) { /* Out of multi-byte sequence? */
pb->bs[pb->wi = 0] = (BYTE)c; /* Save 1st byte */
- if ((BYTE)c < 0x80) break; /* 1-byte? */
- if (((BYTE)c & 0xE0) == 0xC0) pb->ct = 1; /* 2-byte? */
- if (((BYTE)c & 0xF0) == 0xE0) pb->ct = 2; /* 3-byte? */
- if (((BYTE)c & 0xF1) == 0xF0) pb->ct = 3; /* 4-byte? */
- return;
+ if ((BYTE)c < 0x80) break; /* Single byte code? */
+ if (((BYTE)c & 0xE0) == 0xC0) pb->ct = 1; /* 2-byte sequence? */
+ if (((BYTE)c & 0xF0) == 0xE0) pb->ct = 2; /* 3-byte sequence? */
+ if (((BYTE)c & 0xF8) == 0xF0) pb->ct = 3; /* 4-byte sequence? */
+ return; /* Wrong leading byte (discard it) */
} else { /* In the multi-byte sequence */
if (((BYTE)c & 0xC0) != 0x80) { /* Broken sequence? */
- pb->ct = 0; continue;
+ pb->ct = 0; continue; /* Discard the sequense */
}
pb->bs[++pb->wi] = (BYTE)c; /* Save the trailing byte */
- if (--pb->ct == 0) break; /* End of multi-byte sequence? */
+ if (--pb->ct == 0) break; /* End of the sequence? */
return;
}
}
- tp = (TCHAR*)pb->bs;
- dc = tchar2uni(&tp); /* UTF-8 ==> UTF-16 */
- if (dc == 0xFFFFFFFF) return;
- wc = (WCHAR)dc;
+ tp = (const TCHAR*)pb->bs;
+ dc = tchar2uni(&tp); /* UTF-8 ==> UTF-16 */
+ if (dc == 0xFFFFFFFF) return; /* Wrong code? */
hs = (WCHAR)(dc >> 16);
+ wc = (WCHAR)dc;
#elif FF_LFN_UNICODE == 3 /* UTF-32 input */
- if (IsSurrogate(c) || c >= 0x110000) return;
- if (c >= 0x10000) {
+ if (IsSurrogate(c) || c >= 0x110000) return; /* Discard invalid code */
+ if (c >= 0x10000) { /* Out of BMP? */
hs = (WCHAR)(0xD800 | ((c >> 10) - 0x40)); /* Make high surrogate */
wc = 0xDC00 | (c & 0x3FF); /* Make low surrogate */
} else {
@@ -6261,25 +6640,26 @@ void putc_bfd ( /* Buffered write with code conversion */
wc = (WCHAR)c;
}
#endif
+ /* A code point in UTF-16 is available in hs and wc */
-#if FF_STRF_ENCODE == 1 /* Write a character in UTF-16LE */
- if (hs != 0) {
+#if FF_STRF_ENCODE == 1 /* Write a code point in UTF-16LE */
+ if (hs != 0) { /* Surrogate pair? */
st_word(&pb->buf[i], hs);
i += 2;
nc++;
}
st_word(&pb->buf[i], wc);
i += 2;
-#elif FF_STRF_ENCODE == 2 /* Write a character in UTF-16BE */
- if (hs != 0) {
+#elif FF_STRF_ENCODE == 2 /* Write a code point in UTF-16BE */
+ if (hs != 0) { /* Surrogate pair? */
pb->buf[i++] = (BYTE)(hs >> 8);
pb->buf[i++] = (BYTE)hs;
nc++;
}
pb->buf[i++] = (BYTE)(wc >> 8);
pb->buf[i++] = (BYTE)wc;
-#elif FF_STRF_ENCODE == 3 /* Write it in UTF-8 */
- if (hs != 0) { /* 4-byte */
+#elif FF_STRF_ENCODE == 3 /* Write a code point in UTF-8 */
+ if (hs != 0) { /* 4-byte sequence? */
nc += 3;
hs = (hs & 0x3FF) + 0x40;
pb->buf[i++] = (BYTE)(0xF0 | hs >> 8);
@@ -6287,13 +6667,13 @@ void putc_bfd ( /* Buffered write with code conversion */
pb->buf[i++] = (BYTE)(0x80 | (hs & 3) << 4 | (wc >> 6 & 0x0F));
pb->buf[i++] = (BYTE)(0x80 | (wc & 0x3F));
} else {
- if (wc < 0x80) { /* 1-byte */
+ if (wc < 0x80) { /* Single byte? */
pb->buf[i++] = (BYTE)wc;
} else {
- if (wc < 0x800) { /* 2-byte */
+ if (wc < 0x800) { /* 2-byte sequence? */
nc += 1;
pb->buf[i++] = (BYTE)(0xC0 | wc >> 6);
- } else { /* 3-byte */
+ } else { /* 3-byte sequence */
nc += 2;
pb->buf[i++] = (BYTE)(0xE0 | wc >> 12);
pb->buf[i++] = (BYTE)(0x80 | (wc >> 6 & 0x3F));
@@ -6301,17 +6681,17 @@ void putc_bfd ( /* Buffered write with code conversion */
pb->buf[i++] = (BYTE)(0x80 | (wc & 0x3F));
}
}
-#else /* Write it in ANSI/OEM */
+#else /* Write a code point in ANSI/OEM */
if (hs != 0) return;
wc = ff_uni2oem(wc, CODEPAGE); /* UTF-16 ==> ANSI/OEM */
- if (wc == 0) return;;
+ if (wc == 0) return;
if (wc >= 0x100) {
pb->buf[i++] = (BYTE)(wc >> 8); nc++;
}
pb->buf[i++] = (BYTE)wc;
#endif
-#else /* ANSI/OEM input (without re-encode) */
+#else /* ANSI/OEM input (without re-encoding) */
pb->buf[i++] = (BYTE)c;
#endif
@@ -6324,27 +6704,24 @@ void putc_bfd ( /* Buffered write with code conversion */
}
-static
-int putc_flush ( /* Flush left characters in the buffer */
- putbuff* pb
-)
+/* Flush remaining characters in the buffer */
+
+static int putc_flush (putbuff* pb)
{
UINT nw;
if ( pb->idx >= 0 /* Flush buffered characters to the file */
&& f_write(pb->fp, pb->buf, (UINT)pb->idx, &nw) == FR_OK
&& (UINT)pb->idx == nw) return pb->nchr;
- return EOF;
+ return -1;
}
-static
-void putc_init ( /* Initialize write buffer */
- putbuff* pb,
- FIL* fp
-)
+/* Initialize write buffer */
+
+static void putc_init (putbuff* pb, FIL* fp)
{
- mem_set(pb, 0, sizeof (putbuff));
+ memset(pb, 0, sizeof (putbuff));
pb->fp = fp;
}
@@ -6387,8 +6764,131 @@ int f_puts (
/*-----------------------------------------------------------------------*/
-/* Put a Formatted String to the File */
+/* Put a Formatted String to the File (with sub-functions) */
/*-----------------------------------------------------------------------*/
+#if FF_PRINT_FLOAT && FF_INTDEF == 2
+#include <math.h>
+
+static int ilog10 (double n) /* Calculate log10(n) in integer output */
+{
+ int rv = 0;
+
+ while (n >= 10) { /* Decimate digit in right shift */
+ if (n >= 100000) {
+ n /= 100000; rv += 5;
+ } else {
+ n /= 10; rv++;
+ }
+ }
+ while (n < 1) { /* Decimate digit in left shift */
+ if (n < 0.00001) {
+ n *= 100000; rv -= 5;
+ } else {
+ n *= 10; rv--;
+ }
+ }
+ return rv;
+}
+
+
+static double i10x (int n) /* Calculate 10^n in integer input */
+{
+ double rv = 1;
+
+ while (n > 0) { /* Left shift */
+ if (n >= 5) {
+ rv *= 100000; n -= 5;
+ } else {
+ rv *= 10; n--;
+ }
+ }
+ while (n < 0) { /* Right shift */
+ if (n <= -5) {
+ rv /= 100000; n += 5;
+ } else {
+ rv /= 10; n++;
+ }
+ }
+ return rv;
+}
+
+
+static void ftoa (
+ char* buf, /* Buffer to output the floating point string */
+ double val, /* Value to output */
+ int prec, /* Number of fractional digits */
+ TCHAR fmt /* Notation */
+)
+{
+ int d;
+ int e = 0, m = 0;
+ char sign = 0;
+ double w;
+ const char *er = 0;
+ const char ds = FF_PRINT_FLOAT == 2 ? ',' : '.';
+
+
+ if (isnan(val)) { /* Not a number? */
+ er = "NaN";
+ } else {
+ if (prec < 0) prec = 6; /* Default precision? (6 fractional digits) */
+ if (val < 0) { /* Negative? */
+ val = 0 - val; sign = '-';
+ } else {
+ sign = '+';
+ }
+ if (isinf(val)) { /* Infinite? */
+ er = "INF";
+ } else {
+ if (fmt == 'f') { /* Decimal notation? */
+ val += i10x(0 - prec) / 2; /* Round (nearest) */
+ m = ilog10(val);
+ if (m < 0) m = 0;
+ if (m + prec + 3 >= SZ_NUM_BUF) er = "OV"; /* Buffer overflow? */
+ } else { /* E notation */
+ if (val != 0) { /* Not a true zero? */
+ val += i10x(ilog10(val) - prec) / 2; /* Round (nearest) */
+ e = ilog10(val);
+ if (e > 99 || prec + 7 >= SZ_NUM_BUF) { /* Buffer overflow or E > +99? */
+ er = "OV";
+ } else {
+ if (e < -99) e = -99;
+ val /= i10x(e); /* Normalize */
+ }
+ }
+ }
+ }
+ if (!er) { /* Not error condition */
+ if (sign == '-') *buf++ = sign; /* Add a - if negative value */
+ do { /* Put decimal number */
+ if (m == -1) *buf++ = ds; /* Insert a decimal separator when get into fractional part */
+ w = i10x(m); /* Snip the highest digit d */
+ d = (int)(val / w); val -= d * w;
+ *buf++ = (char)('0' + d); /* Put the digit */
+ } while (--m >= -prec); /* Output all digits specified by prec */
+ if (fmt != 'f') { /* Put exponent if needed */
+ *buf++ = (char)fmt;
+ if (e < 0) {
+ e = 0 - e; *buf++ = '-';
+ } else {
+ *buf++ = '+';
+ }
+ *buf++ = (char)('0' + e / 10);
+ *buf++ = (char)('0' + e % 10);
+ }
+ }
+ }
+ if (er) { /* Error condition */
+ if (sign) *buf++ = sign; /* Add sign if needed */
+ do { /* Put error symbol */
+ *buf++ = *er++;
+ } while (*er);
+ }
+ *buf = 0; /* Term */
+}
+#endif /* FF_PRINT_FLOAT && FF_INTDEF == 2 */
+
+
int f_printf (
FIL* fp, /* Pointer to the file object */
@@ -6398,10 +6898,17 @@ int f_printf (
{
va_list arp;
putbuff pb;
- BYTE f, r;
- UINT i, j, w;
+ UINT i, j, w, f, r;
+ int prec;
+#if FF_PRINT_LLI && FF_INTDEF == 2
+ QWORD v;
+#else
DWORD v;
- TCHAR c, d, str[32], *p;
+#endif
+ TCHAR *tp;
+ TCHAR tc, pad;
+ TCHAR nul = 0;
+ char d, str[SZ_NUM_BUF];
putc_init(&pb, fp);
@@ -6409,88 +6916,131 @@ int f_printf (
va_start(arp, fmt);
for (;;) {
- c = *fmt++;
- if (c == 0) break; /* End of string */
- if (c != '%') { /* Non escape character */
- putc_bfd(&pb, c);
+ tc = *fmt++;
+ if (tc == 0) break; /* End of format string */
+ if (tc != '%') { /* Not an escape character (pass-through) */
+ putc_bfd(&pb, tc);
continue;
}
- w = f = 0;
- c = *fmt++;
- if (c == '0') { /* Flag: '0' padding */
- f = 1; c = *fmt++;
- } else {
- if (c == '-') { /* Flag: left justified */
- f = 2; c = *fmt++;
- }
+ f = w = 0; pad = ' '; prec = -1; /* Initialize parms */
+ tc = *fmt++;
+ if (tc == '0') { /* Flag: '0' padded */
+ pad = '0'; tc = *fmt++;
+ } else if (tc == '-') { /* Flag: Left aligned */
+ f = 2; tc = *fmt++;
}
- if (c == '*') { /* Minimum width by argument */
+ if (tc == '*') { /* Minimum width from an argument */
w = va_arg(arp, int);
- c = *fmt++;
+ tc = *fmt++;
} else {
- while (IsDigit(c)) { /* Minimum width */
- w = w * 10 + c - '0';
- c = *fmt++;
- }
- }
- if (c == 'l' || c == 'L') { /* Type prefix: Size is long int */
- f |= 4; c = *fmt++;
- }
- if (c == 0) break;
- d = c;
- if (IsLower(d)) d -= 0x20;
- switch (d) { /* Atgument type is... */
- case 'S' : /* String */
- p = va_arg(arp, TCHAR*);
- for (j = 0; p[j]; j++) ;
- if (!(f & 2)) { /* Right padded */
- while (j++ < w) putc_bfd(&pb, ' ') ;
- }
- while (*p) putc_bfd(&pb, *p++) ; /* String body */
- while (j++ < w) putc_bfd(&pb, ' ') ; /* Left padded */
- continue;
-
- case 'C' : /* Character */
- putc_bfd(&pb, (TCHAR)va_arg(arp, int)); continue;
-
- case 'B' : /* Unsigned binary */
+ while (IsDigit(tc)) { /* Minimum width */
+ w = w * 10 + tc - '0';
+ tc = *fmt++;
+ }
+ }
+ if (tc == '.') { /* Precision */
+ tc = *fmt++;
+ if (tc == '*') { /* Precision from an argument */
+ prec = va_arg(arp, int);
+ tc = *fmt++;
+ } else {
+ prec = 0;
+ while (IsDigit(tc)) { /* Precision */
+ prec = prec * 10 + tc - '0';
+ tc = *fmt++;
+ }
+ }
+ }
+ if (tc == 'l') { /* Size: long int */
+ f |= 4; tc = *fmt++;
+#if FF_PRINT_LLI && FF_INTDEF == 2
+ if (tc == 'l') { /* Size: long long int */
+ f |= 8; tc = *fmt++;
+ }
+#endif
+ }
+ if (tc == 0) break; /* End of format string */
+ switch (tc) { /* Atgument type is... */
+ case 'b': /* Unsigned binary */
r = 2; break;
- case 'O' : /* Unsigned octal */
+ case 'o': /* Unsigned octal */
r = 8; break;
- case 'D' : /* Signed decimal */
- case 'U' : /* Unsigned decimal */
+ case 'd': /* Signed decimal */
+ case 'u': /* Unsigned decimal */
r = 10; break;
- case 'X' : /* Unsigned hexdecimal */
+ case 'x': /* Unsigned hexadecimal (lower case) */
+ case 'X': /* Unsigned hexadecimal (upper case) */
r = 16; break;
+ case 'c': /* Character */
+ putc_bfd(&pb, (TCHAR)va_arg(arp, int));
+ continue;
+
+ case 's': /* String */
+ tp = va_arg(arp, TCHAR*); /* Get a pointer argument */
+ if (!tp) tp = &nul; /* Null ptr generates a null string */
+ for (j = 0; tp[j]; j++) ; /* j = tcslen(tp) */
+ if (prec >= 0 && j > (UINT)prec) j = prec; /* Limited length of string body */
+ for ( ; !(f & 2) && j < w; j++) putc_bfd(&pb, pad); /* Left pads */
+ while (*tp && prec--) putc_bfd(&pb, *tp++); /* Body */
+ while (j++ < w) putc_bfd(&pb, ' '); /* Right pads */
+ continue;
+#if FF_PRINT_FLOAT && FF_INTDEF == 2
+ case 'f': /* Floating point (decimal) */
+ case 'e': /* Floating point (e) */
+ case 'E': /* Floating point (E) */
+ ftoa(str, va_arg(arp, double), prec, tc); /* Make a floating point string */
+ for (j = strlen(str); !(f & 2) && j < w; j++) putc_bfd(&pb, pad); /* Left pads */
+ for (i = 0; str[i]; putc_bfd(&pb, str[i++])) ; /* Body */
+ while (j++ < w) putc_bfd(&pb, ' '); /* Right pads */
+ continue;
+#endif
default: /* Unknown type (pass-through) */
- putc_bfd(&pb, c); continue;
+ putc_bfd(&pb, tc); continue;
}
- /* Get an argument and put it in numeral */
- 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;
+ /* Get an integer argument and put it in numeral */
+#if FF_PRINT_LLI && FF_INTDEF == 2
+ if (f & 8) { /* long long argument? */
+ v = (QWORD)va_arg(arp, long long);
+ } else if (f & 4) { /* long argument? */
+ v = (tc == 'd') ? (QWORD)(long long)va_arg(arp, long) : (QWORD)va_arg(arp, unsigned long);
+ } else { /* int/short/char argument */
+ v = (tc == 'd') ? (QWORD)(long long)va_arg(arp, int) : (QWORD)va_arg(arp, unsigned int);
+ }
+ if (tc == 'd' && (v & 0x8000000000000000)) { /* Negative value? */
+ v = 0 - v; f |= 1;
+ }
+#else
+ if (f & 4) { /* long argument? */
+ v = (DWORD)va_arg(arp, long);
+ } else { /* int/short/char argument */
+ v = (tc == 'd') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int);
}
+ if (tc == 'd' && (v & 0x80000000)) { /* Negative value? */
+ v = 0 - v; f |= 1;
+ }
+#endif
i = 0;
- do {
- d = (TCHAR)(v % r); v /= r;
- if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
+ do { /* Make an integer number string */
+ d = (char)(v % r); v /= r;
+ if (d > 9) d += (tc == 'x') ? 0x27 : 0x07;
str[i++] = d + '0';
- } while (v && i < sizeof str / sizeof *str);
- if (f & 8) str[i++] = '-';
- j = i; d = (f & 1) ? '0' : ' ';
- if (!(f & 2)) {
- while (j++ < w) putc_bfd(&pb, d); /* Right pad */
- }
- do {
- putc_bfd(&pb, str[--i]); /* Number body */
+ } while (v && i < SZ_NUM_BUF);
+ if (f & 1) str[i++] = '-'; /* Sign */
+ /* Write it */
+ for (j = i; !(f & 2) && j < w; j++) { /* Left pads */
+ putc_bfd(&pb, pad);
+ }
+ do { /* Body */
+ putc_bfd(&pb, (TCHAR)str[--i]);
} while (i);
- while (j++ < w) putc_bfd(&pb, d); /* Left pad */
+ while (j++ < w) { /* Right pads */
+ putc_bfd(&pb, ' ');
+ }
}
va_end(arp);
@@ -6512,13 +7062,13 @@ FRESULT f_setcp (
WORD cp /* Value to be set as active code page */
)
{
- static const WORD validcp[] = { 437, 720, 737, 771, 775, 850, 852, 857, 860, 861, 862, 863, 864, 865, 866, 869, 932, 936, 949, 950, 0};
- static const BYTE* const tables[] = {Ct437, Ct720, Ct737, Ct771, Ct775, Ct850, Ct852, Ct857, Ct860, Ct861, Ct862, Ct863, Ct864, Ct865, Ct866, Ct869, Dc932, Dc936, Dc949, Dc950, 0};
+ static const WORD validcp[22] = { 437, 720, 737, 771, 775, 850, 852, 855, 857, 860, 861, 862, 863, 864, 865, 866, 869, 932, 936, 949, 950, 0};
+ static const BYTE *const tables[22] = {Ct437, Ct720, Ct737, Ct771, Ct775, Ct850, Ct852, Ct855, Ct857, Ct860, Ct861, Ct862, Ct863, Ct864, Ct865, Ct866, Ct869, Dc932, Dc936, Dc949, Dc950, 0};
UINT i;
for (i = 0; validcp[i] != 0 && validcp[i] != cp; i++) ; /* Find the code page */
- if (validcp[i] != cp) return FR_INVALID_PARAMETER; /* Not found? */
+ if (validcp[i] != cp) return FR_INVALID_PARAMETER; /* Not found? */
CodePage = cp;
if (cp >= 900) { /* DBCS */