fatfs: update to 0.07a
authorBernhard Urban <lewurm@gmx.net>
Mon, 4 Jan 2010 03:25:16 +0000 (04:25 +0100)
committerBernhard Urban <lewurm@gmx.net>
Mon, 4 Jan 2010 03:31:27 +0000 (04:31 +0100)
ff.c
ff.h

diff --git a/ff.c b/ff.c
index 6cf415837be4404bd661166e8bddb665c439d97a..7bbedbadf0680b1480a8c5a44ff1aa1c13396052 100644 (file)
--- a/ff.c
+++ b/ff.c
@@ -1,9 +1,9 @@
 /*----------------------------------------------------------------------------/
-/  FatFs - FAT file system module  R0.07                     (C)ChaN, 2009
+/  FatFs - FAT file system module  R0.07a                    (C)ChaN, 2009
 /-----------------------------------------------------------------------------/
 / FatFs module is an open source software to implement FAT file system to
 / small embedded systems. This is a free software and is opened for education,
-/ research and commecial developments under license policy of following trems.
+/ research and commercial developments under license policy of following trems.
 /
 /  Copyright (C) 2009, ChaN, all right reserved.
 /
@@ -11,8 +11,7 @@
 / * No restriction on use. You can use, modify and redistribute it for
 /   personal, non-profit or commercial use UNDER YOUR RESPONSIBILITY.
 / * Redistributions of source code must retain the above copyright notice.
-/
-/-----------------------------------------------------------------------------/
+//-----------------------------------------------------------------------------/
 / Feb 26,'06 R0.00  Prototype.
 /
 / Apr 29,'06 R0.01  First stable version.
@@ -61,6 +60,8 @@
 /                   Added rewind option to f_readdir().
 /                   Changed result code of critical errors.
 /                   Renamed string functions to avoid name collision.
+/ Apr 14,'09 R0.07a Separated out OS dependent code on reentrant cfg.
+/                   Added multiple sector size support.
 /---------------------------------------------------------------------------*/
 
 #include "ff.h"                        /* FatFs configurations and declarations */
 
 ---------------------------------------------------------------------------*/
 
-#if _EXCLUDE_LIB
-static
-void MemCpy (void* dst, const void* src, int cnt) {
-       char *d = (char*)dst;
-       const char *s = (const char *)src;
-       while (cnt--) *d++ = *s++;
-}
-
-static
-void MemSet (void* dst, int val, int cnt) {
-       char *d = (char*)dst;
-       while (cnt--) *d++ = val;
-}
-
-static
-int MemCmp (const void* dst, const void* src, int cnt) {
-       const char *d = (const char *)dst, *s = (const char *)src;
-       int r = 0;
-       while (cnt-- && !(r = *d++ - *s++));
-       return r;
-}
-
-static
-char *StrChr (const char* str, int chr) {
-       while (*str && *str != chr) str++;
-       return (*str == chr) ? (char*)str : 0;
-}
-
-#else
-#include "string.h"
-#define MemCpy(x,y,z)  memcpy(x,y,z)
-#define MemCmp(x,y,z)  memcmp(x,y,z)
-#define MemSet(x,y,z)  memset(x,y,z)
-#define StrChr(x,y)            strchr(x,y)
-
-#endif
-
-#ifndef NULL
-#define        NULL    0
-#endif
-
-
 #if _FS_REENTRANT
 #if _USE_LFN == 1
 #error Static LFN work area must not be used in re-entrant configuration.
@@ -128,15 +87,16 @@ char *StrChr (const char* str, int chr) {
 
 #endif
 
-
 #define        ABORT(fs, res)          { fp->flag |= FA__ERROR; LEAVE_FF(fs, res); }
 
-
+#ifndef NULL
+#define        NULL    0
+#endif
 
 
 /*--------------------------------------------------------------------------
 
-   Module Private Work Area
+   Private Work Area
 
 ---------------------------------------------------------------------------*/
 
@@ -167,20 +127,59 @@ WORD LfnBuf[_MAX_LFN + 1];
 
 /*--------------------------------------------------------------------------
 
-   Module Private Functions
+   Private Functions
 
 ---------------------------------------------------------------------------*/
 
+
+/*-----------------------------------------------------------------------*/
+/* String functions                                                      */
+/*-----------------------------------------------------------------------*/
+
+/* Copy memory to memory */
+static
+void mem_cpy (void* dst, const void* src, int cnt) {
+       char *d = (char*)dst;
+       const char *s = (const char *)src;
+       while (cnt--) *d++ = *s++;
+}
+
+/* Fill memory */
+static
+void mem_set (void* dst, int val, int cnt) {
+       char *d = (char*)dst;
+       while (cnt--) *d++ = (char)val;
+}
+
+/* Compare memory to memory */
+static
+int mem_cmp (const void* dst, const void* src, int cnt) {
+       const char *d = (const char *)dst, *s = (const char *)src;
+       int r = 0;
+       while (cnt-- && (r = *d++ - *s++) == 0) ;
+       return r;
+}
+
+/* Check if chr is contained in the string */
+static
+int chk_chr (const char* str, int chr) {
+       while (*str && *str != chr) str++;
+       return *str;
+}
+
+
+
 /*-----------------------------------------------------------------------*/
-/* Request/Release grant to access the fs object (Platform dependent)    */
+/* Request/Release grant to access the volume                            */
 /*-----------------------------------------------------------------------*/
 #if _FS_REENTRANT
+
 static
 BOOL lock_fs (
        FATFS *fs               /* File system object */
 )
 {
-       return (WaitForSingleObject(fs->h_mutex, _TIMEOUT) == WAIT_OBJECT_0) ? TRUE : FALSE;
+       return ff_req_grant(fs->sobj);
 }
 
 
@@ -192,9 +191,9 @@ void unlock_fs (
 {
        if (res != FR_NOT_ENABLED &&
                res != FR_INVALID_DRIVE &&
-               ree != FR_INVALID_OBJECT &&
+               res != FR_INVALID_OBJECT &&
                res != FR_TIMEOUT) {
-               ReleaseMutex(fs->h_mutex);
+               ff_rel_grant(fs->sobj);
        }
 }
 #endif
@@ -260,7 +259,7 @@ FRESULT sync (      /* FR_OK: successful, FR_DISK_ERR: failed */
                /* Update FSInfo sector if needed */
                if (fs->fs_type == FS_FAT32 && fs->fsi_flag) {
                        fs->winsect = 0;
-                       MemSet(fs->win, 0, 512);
+                       mem_set(fs->win, 0, 512);
                        ST_WORD(fs->win+BS_55AA, 0xAA55);
                        ST_DWORD(fs->win+FSI_LeadSig, 0x41615252);
                        ST_DWORD(fs->win+FSI_StrucSig, 0x61417272);
@@ -489,7 +488,7 @@ DWORD create_chain (        /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk err
 /* Get sector# from cluster#                                             */
 /*-----------------------------------------------------------------------*/
 
-/*static*/
+static
 DWORD clust2sect (     /* !=0: sector number, 0: failed - invalid cluster# */
        FATFS *fs,              /* File system object */
        DWORD clst              /* Cluster# to be converted */
@@ -500,21 +499,6 @@ DWORD clust2sect ( /* !=0: sector number, 0: failed - invalid cluster# */
        return clst * fs->csize + fs->database;
 }
 
-DWORD f_check_contig(FIL *fp)
-{
-       DWORD nxt;
-       DWORD clust = fp->org_clust;
-       DWORD retval = 1;
-       while (clust >= 2 && clust < fp->fs->max_clust) {
-               nxt = get_cluster(fp->fs, clust);
-               if (nxt == 1) return retval;
-               if (nxt != (clust + 1)) return retval;
-               clust = nxt;
-               retval++;
-       }
-       return retval;
-}
-
 
 
 
@@ -602,7 +586,7 @@ FRESULT dir_next (  /* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:EOT an
                                        if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
                                        /* Clean-up streached table */
                                        if (move_window(dj->fs, 0)) return FR_DISK_ERR; /* Flush active window */
-                                       MemSet(dj->fs->win, 0, SS(fs));                         /* Clear window buffer */
+                                       mem_set(dj->fs->win, 0, SS(dj->fs));                    /* Clear window buffer */
                                        dj->fs->winsect = clust2sect(dj->fs, clst);     /* Cluster start sector */
                                        for (c = 0; c < dj->fs->csize; c++) {           /* Fill the new cluster with 0 */
                                                dj->fs->wflag = 1;
@@ -736,7 +720,7 @@ void gen_numname (
        int i, j;
 
 
-       MemCpy(dst, src, 11);
+       mem_cpy(dst, src, 11);
 
        if (num > 5) {  /* On many collisions, generate a hash number instead of sequencial number */
                do num = (num >> 1) + (num << 15) + (WORD)*lfn++; while (*lfn);
@@ -796,9 +780,13 @@ FRESULT dir_find (
 )
 {
        FRESULT res;
-       BYTE a, c, stat, ord, sum, *dir;
+       BYTE a, c, lfen, ord, sum, *dir;
+
 
-       ord = sum = 0xFF; stat = *(dj->fn+11);
+       res = dir_seek(dj, 0);                  /* Rewind directory object */
+       if (res != FR_OK) return res;
+
+       ord = sum = 0xFF; lfen = *(dj->fn+11) & 1;
        do {
                res = move_window(dj->fs, dj->sect);
                if (res != FR_OK) break;
@@ -818,22 +806,22 @@ FRESULT dir_find (
                                                dj->lfn_idx = dj->index;
                                        }
                                        /* Check LFN validity. Compare LFN if it is out of 8.3 format */
-                                       ord = (c == ord && sum == dir[LDIR_Chksum] && (!(stat & 1) || test_lfn(dj->lfn, dir))) ? ord - 1 : 0xFF;
+                                       ord = (c == ord && sum == dir[LDIR_Chksum] && (!lfen || test_lfn(dj->lfn, dir))) ? ord - 1 : 0xFF;
                                }
                        } else {                                        /* An SFN entry is found */
                                if (ord || sum != sum_sfn(dir)) {       /* Did not LFN match? */
                                        dj->lfn_idx = 0xFFFF;
                                        ord = 0xFF;
                                }
-                               if (stat & 1) {                 /* Match LFN if it is out of 8.3 format */
+                               if (lfen) {                             /* Match LFN if it is out of 8.3 format */
                                        if (ord == 0) break;
                                } else {                                /* Match SFN if LFN is in 8.3 format */
-                                       if (!MemCmp(dir, dj->fn, 11)) break;
+                                       if (!mem_cmp(dir, dj->fn, 11)) break;
                                }
                        }
                }
 #else  /* Non LFN configuration */
-               if (c != 0xE5 && c != '.' && !(a & AM_VOL) && !MemCmp(dir, dj->fn, 11)) /* Is it a valid entry? */
+               if (c != 0xE5 && c != '.' && !(a & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */
                        break;
 #endif
                res = dir_next(dj, FALSE);                              /* Next entry */
@@ -919,13 +907,11 @@ FRESULT dir_register (    /* FR_OK:Successful, FR_DENIED:No free entry or too many
        WCHAR *lfn;
 
        fn = dj->fn; lfn = dj->lfn;
-       MemCpy(sn, fn, 12);
+       mem_cpy(sn, fn, 12);
        if (sn[11] & 1) {               /* When LFN is out of 8.3 format, generate a numbered name */
                fn[11] = 0; dj->lfn = NULL;                     /* Find only SFN */
                for (n = 1; n < 100; n++) {
                        gen_numname(fn, sn, lfn, n);    /* Generate a numbered name */
-                       res = dir_seek(dj, 0);
-                       if (res != FR_OK) break;
                        res = dir_find(dj);                             /* Check if the name collides with existing SFN */
                        if (res != FR_OK) break;
                }
@@ -989,8 +975,8 @@ FRESULT dir_register (      /* FR_OK:Successful, FR_DENIED:No free entry or too many
                res = move_window(dj->fs, dj->sect);
                if (res == FR_OK) {
                        dir = dj->dir;
-                       MemSet(dir, 0, 32);                     /* Clean the entry */
-                       MemCpy(dir, dj->fn, 11);        /* Put SFN */
+                       mem_set(dir, 0, 32);                    /* Clean the entry */
+                       mem_cpy(dir, dj->fn, 11);       /* Put SFN */
                        dir[DIR_NTres] = *(dj->fn+11) & 0x18;   /* Put NT flag */
                        dj->fs->wflag = 1;
                }
@@ -1078,7 +1064,7 @@ FRESULT create_name (
                                return FR_INVALID_NAME;
                        w = (w << 8) + c;
                } else {
-                       if (StrChr("\"*:<>?|\x7F", w))  /* Reject unallowable chars for LFN */
+                       if (chk_chr("\"*:<>\?|\x7F", w))        /* Reject unallowable chars for LFN */
                                return FR_INVALID_NAME;
                }
                w = ff_convert(w, 1);                   /* Convert OEM to Unicode, store it */
@@ -1100,7 +1086,7 @@ FRESULT create_name (
 
        /* Create SFN in directory form */
        sfn = dj->fn;
-       MemSet(sfn, ' ', 11);
+       mem_set(sfn, ' ', 11);
        for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ;  /* Strip leading spaces and dots */
        if (si) cf |= 1;
        while (di && lfn[di - 1] != '.') di--;  /* Find extension (di<=si: no extension) */
@@ -1129,7 +1115,7 @@ FRESULT create_name (
                        }
                        sfn[i++] = (BYTE)(w >> 8);
                } else {                                                /* Single byte char */
-                       if (StrChr("+,;[=]", w)) {      /* Replace unallowable chars for SFN */
+                       if (chk_chr("+,;[=]", w)) {     /* Replace unallowable chars for SFN */
                                w = '_'; cf |= 1;
                        } else {
                                if (IsUpper(w)) {               /* Large capital */
@@ -1161,7 +1147,7 @@ FRESULT create_name (
 
        /* Create file name in directory form */
        sfn = dj->fn;
-       MemSet(sfn, ' ', 11);
+       mem_set(sfn, ' ', 11);
        si = i = b = 0; ni = 8;
        p = *path;
        for (;;) {
@@ -1180,7 +1166,7 @@ FRESULT create_name (
                        sfn[i++] = c;
                        sfn[i++] = d;
                } else {
-                       if (StrChr(" +,;[=]\"*:<>?|\x7F", c))   /* Reject unallowable chrs for SFN */
+                       if (chk_chr(" +,;[=]\"*:<>\?|\x7F", c)) /* Reject unallowable chrs for SFN */
                                return FR_INVALID_NAME;
                        if (IsUpper(c)) {
                                b |= 2;
@@ -1290,7 +1276,7 @@ FRESULT follow_path (     /* FR_OK(0): successful, !=0: error code */
 )
 {
        FRESULT res;
-       BYTE *dir, stat;
+       BYTE *dir, last;
 
 
        if (*path == '/' || *path == '\\' ) path++;     /* Strip heading separator */
@@ -1304,18 +1290,16 @@ FRESULT follow_path (   /* FR_OK(0): successful, !=0: error code */
 
        } else {                                                        /* Follow path */
                for (;;) {
-                       res = dir_seek(dj, 0);                  /* Rewind directory object */
-                       if (res != FR_OK) break;
                        res = create_name(dj, &path);   /* Get a segment */
                        if (res != FR_OK) break;
                        res = dir_find(dj);                             /* Find it */
-                       stat = *(dj->fn+11);
+                       last = *(dj->fn+11) & 4;
                        if (res != FR_OK) {                             /* Could not find the object */
-                               if (res == FR_NO_FILE && !(stat & 4))
+                               if (res == FR_NO_FILE && !last)
                                        res = FR_NO_PATH;
                                break;
                        }
-                       if (stat & 4) break;                    /* Last segment match. Function completed. */
+                       if (last) break;                                /* Last segment match. Function completed. */
                        dir = dj->dir;                                  /* There is next segment. Follow the sub directory */
                        if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow because it is a file */
                                res = FR_NO_PATH; break;
@@ -1345,9 +1329,9 @@ BYTE check_fs (   /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:
        if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55)                               /* Check record signature (always placed at offset 510 even if the sector size is >512) */
                return 2;
 
-       if (!MemCmp(&fs->win[BS_FilSysType], "FAT", 3))         /* Check FAT signature */
+       if (!mem_cmp(&fs->win[BS_FilSysType], "FAT", 3))                /* Check FAT signature */
                return 0;
-       if (!MemCmp(&fs->win[BS_FilSysType32], "FAT32", 5) && !(fs->win[BPB_ExtFlags] & 0x80))
+       if (!mem_cmp(&fs->win[BS_FilSysType32], "FAT32", 5) && !(fs->win[BPB_ExtFlags] & 0x80))
                return 0;
 
        return 1;
@@ -1368,25 +1352,25 @@ FRESULT auto_mount (    /* FR_OK(0): successful, !=0: any error occured */
 )
 {
        FRESULT res;
-       BYTE drv, fmt, *tbl;
+       BYTE vol, fmt, *tbl;
        DSTATUS stat;
        DWORD bsect, fsize, tsect, mclst;
        const char *p = *path;
        FATFS *fs;
 
 
-       /* Get drive number from the path name */
-       drv = p[0] - '0';                       /* Is there a drive number? */
-       if (drv <= 9 && p[1] == ':') {
+       /* Get logical drive number from the path name */
+       vol = p[0] - '0';                       /* Is there a drive number? */
+       if (vol <= 9 && p[1] == ':') {
                p += 2;                                 /* Found a drive number, get and strip it */
                *path = p;                              /* Return pointer to the path name */
        } else {
-               drv = 0;                                /* No drive number is given, use drive number 0 as default */
+               vol = 0;                                /* No drive number is given, use drive number 0 as default */
        }
 
-       /* Check if the drive number is valid or not */
-       if (drv >= _DRIVES) return FR_INVALID_DRIVE;    /* Is the drive number valid? */
-       *rfs = fs = FatFs[drv];                                 /* Returen pointer to the corresponding file system object */
+       /* Check if the logical drive number is valid or not */
+       if (vol >= _DRIVES) return FR_INVALID_DRIVE;    /* Is the drive number valid? */
+       *rfs = fs = FatFs[vol];                                 /* Returen pointer to the corresponding file system object */
        if (!fs) return FR_NOT_ENABLED;                 /* Is the file system object registered? */
 
        ENTER_FF(fs);                           /* Lock file system */
@@ -1402,15 +1386,15 @@ FRESULT auto_mount (    /* FR_OK(0): successful, !=0: any error occured */
                }
        }
 
-       /* The logical drive must be re-mounted. Following code attempts to mount the logical drive */
+       /* The logical drive must be re-mounted. Following code attempts to mount the volume */
 
        fs->fs_type = 0;                                        /* Clear the file system object */
-       fs->drive = LD2PD(drv);                         /* Bind the logical drive and a physical drive */
+       fs->drive = LD2PD(vol);                         /* Bind the logical drive and a physical drive */
        stat = disk_initialize(fs->drive);      /* Initialize low level disk I/O layer */
        if (stat & STA_NOINIT)                          /* Check if the drive is ready */
                return FR_NOT_READY;
-#if S_MAX_SIZ > 512                                            /* Get disk sector size if needed */
-       if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > S_MAX_SIZ)
+#if _MAX_SS != 512                                             /* Get disk sector size if needed */
+       if (disk_ioctl(fs->drive, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS)
                return FR_NO_FILESYSTEM;
 #endif
 #if !_FS_READONLY
@@ -1421,7 +1405,7 @@ FRESULT auto_mount (      /* FR_OK(0): successful, !=0: any error occured */
        fmt = check_fs(fs, bsect = 0);          /* Check sector 0 as an SFD format */
        if (fmt == 1) {                                         /* Not an FAT boot record, it may be patitioned */
                /* Check a partition listed in top of the partition table */
-               tbl = &fs->win[MBR_Table + LD2PT(drv) * 16];    /* Partition table */
+               tbl = &fs->win[MBR_Table + LD2PT(vol) * 16];    /* Partition table */
                if (tbl[4]) {                                                                   /* Is the partition existing? */
                        bsect = LD_DWORD(&tbl[8]);                                      /* Partition offset in LBA */
                        fmt = check_fs(fs, bsect);                                      /* Check the partition */
@@ -1521,32 +1505,31 @@ FRESULT validate (      /* FR_OK(0): The object is valid, !=0: Invalid */
 /*-----------------------------------------------------------------------*/
 
 FRESULT f_mount (
-       BYTE drv,               /* Logical drive number to be mounted/unmounted */
+       BYTE vol,               /* Logical drive number to be mounted/unmounted */
        FATFS *fs               /* Pointer to new file system object (NULL for unmount)*/
 )
 {
        FATFS *rfs;
 
 
-       if (drv >= _DRIVES)
+       if (vol >= _DRIVES)                             /* Check if the drive number is valid */
                return FR_INVALID_DRIVE;
-
-       rfs = FatFs[drv];
+       rfs = FatFs[vol];                               /* Get current state */
 
        if (rfs) {
-#if _FS_REENTRANT                                      /* Discard mutex of the current fs. (Platform dependent) */
-               CloseHandle(rfs->h_mutex);      /* Discard mutex */
+#if _FS_REENTRANT                                      /* Discard sync object of the current volume */
+               if (!ff_del_syncobj(fs->sobj)) return FR_INT_ERR;
 #endif
                rfs->fs_type = 0;                       /* Clear old fs object */
        }
 
        if (fs) {
-               fs->fs_type = 0;        /* Clear new fs object */
-#if _FS_REENTRANT                              /* Create mutex for the new fs. (Platform dependent) */
-               fs->h_mutex = CreateMutex(NULL, FALSE, NULL);
+               fs->fs_type = 0;                        /* Clear new fs object */
+#if _FS_REENTRANT                                      /* Create sync object for the new volume */
+               if (!ff_cre_syncobj(vol, &fs->sobj)) return FR_INT_ERR;
 #endif
        }
-       FatFs[drv] = fs;                /* Register new fs object */
+       FatFs[vol] = fs;                                /* Register new fs object */
 
        return FR_OK;
 }
@@ -1725,9 +1708,9 @@ FRESULT f_read (
 #if _FS_TINY
                if (move_window(fp->fs, fp->dsect))                     /* Move sector window */
                        ABORT(fp->fs, FR_DISK_ERR);
-               MemCpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt);       /* Pick partial sector */
+               mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt);      /* Pick partial sector */
 #else
-               MemCpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt);   /* Pick partial sector */
+               mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt);  /* Pick partial sector */
 #endif
        }
 
@@ -1802,6 +1785,17 @@ FRESULT f_write (
                                        cc = fp->fs->csize - fp->csect;
                                if (disk_write(fp->fs->drive, wbuff, sect, (BYTE)cc) != RES_OK)
                                        ABORT(fp->fs, FR_DISK_ERR);
+#if _FS_TINY
+                               if (fp->fs->winsect - sect < cc) {  /* Refill sector cache if it gets dirty by the direct write */
+                                       mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
+                                       fp->fs->wflag = 0;
+                               }
+#else
+                               if (fp->dsect - sect < cc) {  /* Refill sector cache if it gets dirty by the direct write */
+                                       mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
+                                       fp->flag &= ~FA__DIRTY;
+                               }
+#endif
                                fp->csect += (BYTE)cc;                          /* Next sector address in the cluster */
                                wcnt = SS(fp->fs) * cc;                         /* Number of bytes transferred */
                                continue;
@@ -1826,10 +1820,10 @@ FRESULT f_write (
 #if _FS_TINY
                if (move_window(fp->fs, fp->dsect))                     /* Move sector window */
                        ABORT(fp->fs, FR_DISK_ERR);
-               MemCpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt);       /* Fit partial sector */
+               mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt);      /* Fit partial sector */
                fp->fs->wflag = 1;
 #else
-               MemCpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt);   /* Fit partial sector */
+               mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt);  /* Fit partial sector */
                fp->flag |= FA__DIRTY;
 #endif
        }
@@ -2270,7 +2264,7 @@ FRESULT f_unlink (
 
        if (dir[DIR_Attr] & AM_DIR) {                   /* It is a sub-directory */
                if (dclst < 2) LEAVE_FF(dj.fs, FR_INT_ERR);
-               MemCpy(&sdj, &dj, sizeof(DIR));         /* Check if the sub-dir is empty or not */
+               mem_cpy(&sdj, &dj, sizeof(DIR));                /* Check if the sub-dir is empty or not */
                sdj.sclust = dclst;
                res = dir_seek(&sdj, 0);
                if (res != FR_OK) LEAVE_FF(dj.fs, res);
@@ -2286,7 +2280,7 @@ FRESULT f_unlink (
                if (res == FR_OK) res = sync(dj.fs);
        }
 
-       LEAVE_FF(dj.fs, FR_OK);
+       LEAVE_FF(dj.fs, res);
 }
 
 
@@ -2327,15 +2321,15 @@ FRESULT f_mkdir (
        dsect = clust2sect(dj.fs, dclst);
 
        dir = dj.fs->win;                                               /* Initialize the new directory table */
-       MemSet(dir, 0, SS(dj.fs));
-       MemSet(dir+DIR_Name, ' ', 8+3);         /* Create "." entry */
+       mem_set(dir, 0, SS(dj.fs));
+       mem_set(dir+DIR_Name, ' ', 8+3);                /* Create "." entry */
        dir[DIR_Name] = '.';
        dir[DIR_Attr] = AM_DIR;
        tim = get_fattime();
        ST_DWORD(dir+DIR_WrtTime, tim);
        ST_WORD(dir+DIR_FstClusLO, dclst);
        ST_WORD(dir+DIR_FstClusHI, dclst >> 16);
-       MemCpy(dir+32, dir, 32);                        /* Create ".." entry */
+       mem_cpy(dir+32, dir, 32);                       /* Create ".." entry */
        dir[33] = '.';
        pclst = dj.sclust;
        if (dj.fs->fs_type == FS_FAT32 && pclst == dj.fs->dirbase)
@@ -2347,7 +2341,7 @@ FRESULT f_mkdir (
                dj.fs->wflag = 1;
                res = move_window(dj.fs, 0);
                if (res) LEAVE_FF(dj.fs, res);
-               MemSet(dir, 0, SS(dj.fs));
+               mem_set(dir, 0, SS(dj.fs));
        }
 
        res = dir_register(&dj);
@@ -2471,16 +2465,16 @@ FRESULT f_rename (
        if (res != FR_OK) LEAVE_FF(dj_old.fs, res);     /* The old object is not found */
 
        if (!dj_old.dir) LEAVE_FF(dj_old.fs, FR_NO_FILE);       /* Is root dir? */
-       MemCpy(buf, dj_old.dir+DIR_Attr, 21);           /* Save the object information */
+       mem_cpy(buf, dj_old.dir+DIR_Attr, 21);          /* Save the object information */
 
-       MemCpy(&dj_new, &dj_old, sizeof(DIR));
+       mem_cpy(&dj_new, &dj_old, sizeof(DIR));
        res = follow_path(&dj_new, path_new);           /* Check new object */
        if (res == FR_OK) res = FR_EXIST;                       /* The new object name is already existing */
        if (res == FR_NO_FILE) {                                        /* Is it a valid path and no name collision? */
                res = dir_register(&dj_new);                    /* Register the new object */
                if (res == FR_OK) {
                        dir = dj_new.dir;                                       /* Copy object information into new entry */
-                       MemCpy(dir+13, buf+2, 19);
+                       mem_cpy(dir+13, buf+2, 19);
                        dir[DIR_Attr] = buf[0];
                        dj_old.fs->wflag = 1;
                        if (dir[DIR_Attr] & AM_DIR) {           /* Update .. entry in the directory if needed */
@@ -2622,7 +2616,7 @@ FRESULT f_mkfs (
        if (n_part > MAX_SECTOR) n_part = MAX_SECTOR;
        b_part = (!partition) ? 63 : 0;         /* Boot sector */
        n_part -= b_part;
-#if MAX_SS == 512
+#if _MAX_SS == 512
        if (!allocsize) {                                       /* Auto selection of cluster size */
                for (n = 0; n_part < sstbl[n]; n++) ;
                allocsize = cstbl[n];
@@ -2630,9 +2624,9 @@ FRESULT f_mkfs (
 #endif
        for (as = 512; as <= 32768U && as != allocsize; as <<= 1);
        if (as != allocsize) return FR_MKFS_ABORTED;
-#if MAX_SS > 512                                               /* Check disk sector size */
+#if _MAX_SS != 512                                             /* Check disk sector size */
        if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
-               || SS(fs) > S_MAX_SIZ
+               || SS(fs) > _MAX_SS
                || SS(fs) > allocsize)
                return FR_MKFS_ABORTED;
 #endif
@@ -2704,9 +2698,8 @@ FRESULT f_mkfs (
 
        /* Create boot record */
        tbl = fs->win;                                                          /* Clear buffer */
-       MemSet(tbl, 0, SS(fs));
+       mem_set(tbl, 0, SS(fs));
        ST_DWORD(tbl+BS_jmpBoot, 0x90FEEB);                     /* Boot code (jmp $, nop) */
-       memcpy(&tbl[BS_OEMName], "bootmii", 8);
        ST_WORD(tbl+BPB_BytsPerSec, SS(fs));            /* Sector size */
        tbl[BPB_SecPerClus] = (BYTE)allocsize;          /* Sectors per cluster */
        ST_WORD(tbl+BPB_RsvdSecCnt, n_rsv);                     /* Reserved sectors */
@@ -2727,7 +2720,7 @@ FRESULT f_mkfs (
                ST_WORD(tbl+BPB_FATSz16, n_fat);                /* Number of secters per FAT */
                tbl[BS_DrvNum] = 0x80;                                  /* Drive number */
                tbl[BS_BootSig] = 0x29;                                 /* Extended boot signature */
-               MemCpy(tbl+BS_VolLab, "backupmii  FAT     ", 19);       /* Volume lavel, FAT signature */
+               mem_cpy(tbl+BS_VolLab, "NO NAME    FAT     ", 19);      /* Volume lavel, FAT signature */
        } else {
                ST_DWORD(tbl+BS_VolID32, n);                    /* Volume serial number */
                ST_DWORD(tbl+BPB_FATSz32, n_fat);               /* Number of secters per FAT */
@@ -2736,7 +2729,7 @@ FRESULT f_mkfs (
                ST_WORD(tbl+BPB_BkBootSec, 6);                  /* Backup boot record offset (bs+6) */
                tbl[BS_DrvNum32] = 0x80;                                /* Drive number */
                tbl[BS_BootSig32] = 0x29;                               /* Extended boot signature */
-               MemCpy(tbl+BS_VolLab32, "backupmii  FAT32   ", 19);     /* Volume lavel, FAT signature */
+               mem_cpy(tbl+BS_VolLab32, "NO NAME    FAT32   ", 19);    /* Volume lavel, FAT signature */
        }
        ST_WORD(tbl+BS_55AA, 0xAA55);                           /* Signature */
        if (disk_write(drv, tbl, b_part+0, 1) != RES_OK)
@@ -2746,7 +2739,7 @@ FRESULT f_mkfs (
 
        /* Initialize FAT area */
        for (m = 0; m < N_FATS; m++) {
-               MemSet(tbl, 0, SS(fs));         /* 1st sector of the FAT  */
+               mem_set(tbl, 0, SS(fs));                /* 1st sector of the FAT  */
                if (fmt != FS_FAT32) {
                        n = (fmt == FS_FAT12) ? 0x00FFFFF8 : 0xFFFFFFF8;
                        ST_DWORD(tbl, n);                               /* Reserve cluster #0-1 (FAT12/16) */
@@ -2757,7 +2750,7 @@ FRESULT f_mkfs (
                }
                if (disk_write(drv, tbl, b_fat++, 1) != RES_OK)
                        return FR_DISK_ERR;
-               MemSet(tbl, 0, SS(fs));         /* Following FAT entries are filled by zero */
+               mem_set(tbl, 0, SS(fs));                /* Following FAT entries are filled by zero */
                for (n = 1; n < n_fat; n++) {
                        if (disk_write(drv, tbl, b_fat++, 1) != RES_OK)
                                return FR_DISK_ERR;
diff --git a/ff.h b/ff.h
index ea30078c8251fae5fca4b596e3baea4006146cfb..81de3c5b6a15c7f24357e44b34c12daa4089cb5d 100644 (file)
--- a/ff.h
+++ b/ff.h
@@ -1,17 +1,16 @@
 /*---------------------------------------------------------------------------/
-/  FatFs - FAT file system module include file  R0.07        (C)ChaN, 2009
+/  FatFs - FAT file system module include file  R0.07a       (C)ChaN, 2009
 /----------------------------------------------------------------------------/
-/ FatFs module is an open source project to implement FAT file system to small
-/ embedded systems. It is opened for education, research and development under
-/ license policy of following trems.
+/ FatFs module is an open source software to implement FAT file system to
+/ small embedded systems. This is a free software and is opened for education,
+/ research and commercial developments under license policy of following trems.
 /
 /  Copyright (C) 2009, ChaN, all right reserved.
 /
-/ * The FatFs module is a free software and there is no warranty.
-/ * You can use, modify and/or redistribute it for personal, non-profit or
-/   commercial use without any restriction under your responsibility.
+/ * The FatFs module is a free software and there is NO WARRANTY.
+/ * No restriction on use. You can use, modify and redistribute it for
+/   personal, non-profit or commercial use UNDER YOUR RESPONSIBILITY.
 / * Redistributions of source code must retain the above copyright notice.
-/
 /----------------------------------------------------------------------------*/
 
 #include "types.h"
 /  performance and code efficiency. */
 
 
-#define _FS_READONLY   0
+#define _FS_READONLY   1
 /* Setting _FS_READONLY to 1 defines read only configuration. This removes
 /  writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename,
 /  f_truncate and useless f_getfree. */
 
 
-#define _FS_MINIMIZE   0
+#define _FS_MINIMIZE   2
 /* The _FS_MINIMIZE option defines minimization level to remove some functions.
 /
 /   0: Full function.
 /   3: f_lseek is removed in addition to level 2. */
 
 
-#define        _FS_TINY        0
+#define        _FS_TINY        1
 /* When _FS_TINY is set to 1, FatFs uses the sector buffer in the file system
 /  object instead of the sector buffer in the individual file object for file
 /  data transfer. This reduces memory consumption 512 bytes each file object. */
 
 
-#define _DRIVES                1
-/* Number of volumes (logical drives) to be used. */
-
-
 #define        _USE_STRFUNC    0
 /* To enable string functions, set _USE_STRFUNC to 1 or 2. */
 
 
-#define        _USE_MKFS       1
+#define        _USE_MKFS       0
 /* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */
 
 
 /* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */
 
 
-#define        _USE_LFN        0
-#define        _MAX_LFN        255             /* Maximum LFN length to handle (max:255) */
-/* The _USE_LFN option switches the LFN support.
-/
-/   0: Disable LFN.
-/   1: Enable LFN with static working buffer on the bss. Not re-entrant.
-/   2: Enable LFN with dynamic working buffer on the caller's 'stack'.
-/
-/  The working buffer occupies (_MAX_LFN + 1) * 2 bytes. When enable LFN,
-/  a Unicode - OEM code conversion function ff_convert() must be linked. */
+#define _DRIVES                1
+/* Number of volumes (logical drives) to be used. */
+
 
+#define        _MAX_SS 512
+/* Maximum sector size to be handled. (512/1024/2048/4096) */
+/* 512 for memroy card and hard disk, 1024 for floppy disk, 2048 for MO disk */
 
-#define _CODE_PAGE     437
+
+#define        _MULTI_PARTITION        0
+/* When _MULTI_PARTITION is set to 0, each volume is bound to the same physical
+/ drive number and can mount only first primaly partition. When it is set to 1,
+/ each volume is tied to the partitions listed in Drives[]. */
+
+
+#define _CODE_PAGE     932
 /* The _CODE_PAGE specifies the OEM code page to be used on the target system.
 /  When it is non LFN configuration, there is no difference between SBCS code
 /  pages. When LFN is enabled, the code page must always be set correctly.
 */
 
 
-#define        _MULTI_PARTITION        0
-/* When _MULTI_PARTITION is set to 0, each volume is bound to same physical
-/ drive number and can mount only 1st primaly partition. When it is set to 1,
-/ each volume is tied to the partition listed in Drives[]. */
+#define        _USE_LFN        0
+#define        _MAX_LFN        255             /* Maximum LFN length to handle (max:255) */
+/* The _USE_LFN option switches the LFN support.
+/
+/   0: Disable LFN.
+/   1: Enable LFN with static working buffer on the bss. NOT REENTRANT.
+/   2: Enable LFN with dynamic working buffer on the caller's STACK.
+/
+/  The working buffer occupies (_MAX_LFN + 1) * 2 bytes. When enable LFN,
+/  a Unicode - OEM code conversion function ff_convert() must be added to
+/  the project. */
 
 
 #define _FS_REENTRANT  0
-#define _TIMEOUT               1000
-/* To make the FatFs module re-entrant, set 1 and re-write platform dependent
-/  lock out code that defined arownd _FS_REENTRANT. The _TIMEOUT defines the
-/  time out value in unit of milliseconds on the multi access exclusion. */
-
-
-#define _EXCLUDE_LIB   0
-/* When _EXCLUDE_LIB is set to 1, FatFs module does not use standard library. */
+#define _TIMEOUT               1000    /* Timeout period in unit of time ticks */
+#define        _SYNC_t                 HANDLE  /* Type of sync object used on the OS. */
+                                                               /* e.g. HANDLE, OS_EVENT*, ID and etc.. */
+/* To make the FatFs module re-entrant, set _FS_REENTRANT to 1 and add user
+/  provided synchronization handlers, ff_req_grant, ff_rel_grant,
+/  ff_del_syncobj and ff_cre_syncobj function to the project. */
 
 
 
 
 
 
-/* Definitions corresponds to multiple sector size (Not tested) */
+/* Definitions corresponds to multiple sector size */
 
-#define        MAX_SS  512U    /* Do not change */
-#if MAX_SS > 512U
+#if _MAX_SS == 512
+#define        SS(fs)  512
+#else
+#if _MAX_SS == 1024 || _MAX_SS == 2048 || _MAX_SS == 4096
 #define        SS(fs)  ((fs)->s_size)
 #else
-#define        SS(fs)  512U
+#error Sector size must be 512, 1024, 2048 or 4096.
+#endif
 #endif
 
 
@@ -159,9 +165,9 @@ typedef struct _FATFS {
        WORD    id;                     /* File system mount ID */
        WORD    n_rootdir;      /* Number of root directory entries (0 on FAT32) */
 #if _FS_REENTRANT
-       HANDLE  h_mutex;        /* Handle to the mutex (Platform dependent) */
+       _SYNC_t sobj;           /* Identifier of sync object */
 #endif
-#if MAX_SS > 512U
+#if _MAX_SS != 512U
        WORD    s_size;         /* Sector size */
 #endif
 #if !_FS_READONLY
@@ -177,7 +183,7 @@ typedef struct _FATFS {
        DWORD   dirbase;        /* Root directory start sector (Cluster# on FAT32) */
        DWORD   database;       /* Data start sector */
        DWORD   winsect;        /* Current sector appearing in the win[] */
-       BYTE    win[MAX_SS];/* Disk access window for Directory/FAT */
+       BYTE    win[_MAX_SS];/* Disk access window for Directory/FAT */
 } FATFS;
 
 
@@ -213,12 +219,12 @@ typedef struct _FIL {
        DWORD   org_clust;      /* File start cluster */
        DWORD   curr_clust;     /* Current cluster */
        DWORD   dsect;          /* Current data sector */
-#if _FS_READONLY == 0
+#if !_FS_READONLY
        DWORD   dir_sect;       /* Sector containing the directory entry */
        BYTE*   dir_ptr;        /* Ponter to the directory entry in the window */
 #endif
 #if !_FS_TINY
-       BYTE    buf[MAX_SS];/* File R/W buffer */
+       BYTE    buf[_MAX_SS];/* File R/W buffer */
 #endif
 } FIL;
 
@@ -396,21 +402,28 @@ char* f_gets (char*, int, FIL*);                                  /* Get a string from the file */
 #endif
 
 
+
 /*--------------------------------------------------------------*/
 /* User defined functions                                       */
 
-
 /* Real time clock */
+#if !_FS_READONLY
 DWORD get_fattime (void);      /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
                                                        /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
+#endif
 
 /* Unicode - OEM code conversion */
 #if _USE_LFN
 WCHAR ff_convert (WCHAR, UINT);
 #endif
 
-DWORD clust2sect(FATFS *fs, DWORD clust);
-DWORD f_check_contig(FIL *fp);
+/* Sync functions */
+#if _FS_REENTRANT
+BOOL ff_cre_syncobj(BYTE, _SYNC_t*);
+BOOL ff_del_syncobj(_SYNC_t);
+BOOL ff_req_grant(_SYNC_t);
+void ff_rel_grant(_SYNC_t);
+#endif
 
 
 
@@ -524,10 +537,10 @@ DWORD f_check_contig(FIL *fp);
 #define        ST_WORD(ptr,val)        *(WORD*)(BYTE*)(ptr)=(WORD)(val)
 #define        ST_DWORD(ptr,val)       *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
 #else                                  /* Use byte-by-byte access to the FAT structure */
-#define        LD_WORD(ptr)            (WORD)(((WORD)*(volatile BYTE*)((ptr)+1)<<8)|(WORD)*(volatile BYTE*)(ptr))
-#define        LD_DWORD(ptr)           (DWORD)(((DWORD)*(volatile BYTE*)((ptr)+3)<<24)|((DWORD)*(volatile BYTE*)((ptr)+2)<<16)|((WORD)*(volatile BYTE*)((ptr)+1)<<8)|*(volatile BYTE*)(ptr))
-#define        ST_WORD(ptr,val)        *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8)
-#define        ST_DWORD(ptr,val)       *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8); *(volatile BYTE*)((ptr)+2)=(BYTE)((DWORD)(val)>>16); *(volatile BYTE*)((ptr)+3)=(BYTE)((DWORD)(val)>>24)
+#define        LD_WORD(ptr)            (WORD)(((WORD)*(BYTE*)((ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
+#define        LD_DWORD(ptr)           (DWORD)(((DWORD)*(BYTE*)((ptr)+3)<<24)|((DWORD)*(BYTE*)((ptr)+2)<<16)|((WORD)*(BYTE*)((ptr)+1)<<8)|*(BYTE*)(ptr))
+#define        ST_WORD(ptr,val)        *(BYTE*)(ptr)=(BYTE)(val); *(BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8)
+#define        ST_DWORD(ptr,val)       *(BYTE*)(ptr)=(BYTE)(val); *(BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8); *(BYTE*)((ptr)+2)=(BYTE)((DWORD)(val)>>16); *(BYTE*)((ptr)+3)=(BYTE)((DWORD)(val)>>24)
 #endif