old repo - SD card read example

Dependents:   SDFileSystem

Fork of FATFileSystem by mbed official

Committer:
emilmont
Date:
Tue Nov 27 17:31:18 2012 +0000
Revision:
1:46ce1e16c870
Child:
3:e960e2b81a3c
Commit FATFileSystem code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:46ce1e16c870 1 /*----------------------------------------------------------------------------/
emilmont 1:46ce1e16c870 2 / FatFs - FAT file system module R0.09a (C)ChaN, 2012
emilmont 1:46ce1e16c870 3 /-----------------------------------------------------------------------------/
emilmont 1:46ce1e16c870 4 / FatFs module is a generic FAT file system module for small embedded systems.
emilmont 1:46ce1e16c870 5 / This is a free software that opened for education, research and commercial
emilmont 1:46ce1e16c870 6 / developments under license policy of following terms.
emilmont 1:46ce1e16c870 7 /
emilmont 1:46ce1e16c870 8 / Copyright (C) 2012, ChaN, all right reserved.
emilmont 1:46ce1e16c870 9 /
emilmont 1:46ce1e16c870 10 / * The FatFs module is a free software and there is NO WARRANTY.
emilmont 1:46ce1e16c870 11 / * No restriction on use. You can use, modify and redistribute it for
emilmont 1:46ce1e16c870 12 / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
emilmont 1:46ce1e16c870 13 / * Redistributions of source code must retain the above copyright notice.
emilmont 1:46ce1e16c870 14 /
emilmont 1:46ce1e16c870 15 /-----------------------------------------------------------------------------/
emilmont 1:46ce1e16c870 16 / Feb 26,'06 R0.00 Prototype.
emilmont 1:46ce1e16c870 17 /
emilmont 1:46ce1e16c870 18 / Apr 29,'06 R0.01 First stable version.
emilmont 1:46ce1e16c870 19 /
emilmont 1:46ce1e16c870 20 / Jun 01,'06 R0.02 Added FAT12 support.
emilmont 1:46ce1e16c870 21 / Removed unbuffered mode.
emilmont 1:46ce1e16c870 22 / Fixed a problem on small (<32M) partition.
emilmont 1:46ce1e16c870 23 / Jun 10,'06 R0.02a Added a configuration option (_FS_MINIMUM).
emilmont 1:46ce1e16c870 24 /
emilmont 1:46ce1e16c870 25 / Sep 22,'06 R0.03 Added f_rename().
emilmont 1:46ce1e16c870 26 / Changed option _FS_MINIMUM to _FS_MINIMIZE.
emilmont 1:46ce1e16c870 27 / Dec 11,'06 R0.03a Improved cluster scan algorithm to write files fast.
emilmont 1:46ce1e16c870 28 / Fixed f_mkdir() creates incorrect directory on FAT32.
emilmont 1:46ce1e16c870 29 /
emilmont 1:46ce1e16c870 30 / Feb 04,'07 R0.04 Supported multiple drive system.
emilmont 1:46ce1e16c870 31 / Changed some interfaces for multiple drive system.
emilmont 1:46ce1e16c870 32 / Changed f_mountdrv() to f_mount().
emilmont 1:46ce1e16c870 33 / Added f_mkfs().
emilmont 1:46ce1e16c870 34 / Apr 01,'07 R0.04a Supported multiple partitions on a physical drive.
emilmont 1:46ce1e16c870 35 / Added a capability of extending file size to f_lseek().
emilmont 1:46ce1e16c870 36 / Added minimization level 3.
emilmont 1:46ce1e16c870 37 / Fixed an endian sensitive code in f_mkfs().
emilmont 1:46ce1e16c870 38 / May 05,'07 R0.04b Added a configuration option _USE_NTFLAG.
emilmont 1:46ce1e16c870 39 / Added FSInfo support.
emilmont 1:46ce1e16c870 40 / Fixed DBCS name can result FR_INVALID_NAME.
emilmont 1:46ce1e16c870 41 / Fixed short seek (<= csize) collapses the file object.
emilmont 1:46ce1e16c870 42 /
emilmont 1:46ce1e16c870 43 / Aug 25,'07 R0.05 Changed arguments of f_read(), f_write() and f_mkfs().
emilmont 1:46ce1e16c870 44 / Fixed f_mkfs() on FAT32 creates incorrect FSInfo.
emilmont 1:46ce1e16c870 45 / Fixed f_mkdir() on FAT32 creates incorrect directory.
emilmont 1:46ce1e16c870 46 / Feb 03,'08 R0.05a Added f_truncate() and f_utime().
emilmont 1:46ce1e16c870 47 / Fixed off by one error at FAT sub-type determination.
emilmont 1:46ce1e16c870 48 / Fixed btr in f_read() can be mistruncated.
emilmont 1:46ce1e16c870 49 / Fixed cached sector is not flushed when create and close without write.
emilmont 1:46ce1e16c870 50 /
emilmont 1:46ce1e16c870 51 / Apr 01,'08 R0.06 Added fputc(), fputs(), fprintf() and fgets().
emilmont 1:46ce1e16c870 52 / Improved performance of f_lseek() on moving to the same or following cluster.
emilmont 1:46ce1e16c870 53 /
emilmont 1:46ce1e16c870 54 / Apr 01,'09 R0.07 Merged Tiny-FatFs as a configuration option. (_FS_TINY)
emilmont 1:46ce1e16c870 55 / Added long file name feature.
emilmont 1:46ce1e16c870 56 / Added multiple code page feature.
emilmont 1:46ce1e16c870 57 / Added re-entrancy for multitask operation.
emilmont 1:46ce1e16c870 58 / Added auto cluster size selection to f_mkfs().
emilmont 1:46ce1e16c870 59 / Added rewind option to f_readdir().
emilmont 1:46ce1e16c870 60 / Changed result code of critical errors.
emilmont 1:46ce1e16c870 61 / Renamed string functions to avoid name collision.
emilmont 1:46ce1e16c870 62 / Apr 14,'09 R0.07a Separated out OS dependent code on reentrant cfg.
emilmont 1:46ce1e16c870 63 / Added multiple sector size feature.
emilmont 1:46ce1e16c870 64 / Jun 21,'09 R0.07c Fixed f_unlink() can return FR_OK on error.
emilmont 1:46ce1e16c870 65 / Fixed wrong cache control in f_lseek().
emilmont 1:46ce1e16c870 66 / Added relative path feature.
emilmont 1:46ce1e16c870 67 / Added f_chdir() and f_chdrive().
emilmont 1:46ce1e16c870 68 / Added proper case conversion to extended char.
emilmont 1:46ce1e16c870 69 / Nov 03,'09 R0.07e Separated out configuration options from ff.h to ffconf.h.
emilmont 1:46ce1e16c870 70 / Fixed f_unlink() fails to remove a sub-dir on _FS_RPATH.
emilmont 1:46ce1e16c870 71 / Fixed name matching error on the 13 char boundary.
emilmont 1:46ce1e16c870 72 / Added a configuration option, _LFN_UNICODE.
emilmont 1:46ce1e16c870 73 / Changed f_readdir() to return the SFN with always upper case on non-LFN cfg.
emilmont 1:46ce1e16c870 74 /
emilmont 1:46ce1e16c870 75 / May 15,'10 R0.08 Added a memory configuration option. (_USE_LFN = 3)
emilmont 1:46ce1e16c870 76 / Added file lock feature. (_FS_SHARE)
emilmont 1:46ce1e16c870 77 / Added fast seek feature. (_USE_FASTSEEK)
emilmont 1:46ce1e16c870 78 / Changed some types on the API, XCHAR->TCHAR.
emilmont 1:46ce1e16c870 79 / Changed fname member in the FILINFO structure on Unicode cfg.
emilmont 1:46ce1e16c870 80 / String functions support UTF-8 encoding files on Unicode cfg.
emilmont 1:46ce1e16c870 81 / Aug 16,'10 R0.08a Added f_getcwd(). (_FS_RPATH = 2)
emilmont 1:46ce1e16c870 82 / Added sector erase feature. (_USE_ERASE)
emilmont 1:46ce1e16c870 83 / Moved file lock semaphore table from fs object to the bss.
emilmont 1:46ce1e16c870 84 / Fixed a wrong directory entry is created on non-LFN cfg when the given name contains ';'.
emilmont 1:46ce1e16c870 85 / Fixed f_mkfs() creates wrong FAT32 volume.
emilmont 1:46ce1e16c870 86 / Jan 15,'11 R0.08b Fast seek feature is also applied to f_read() and f_write().
emilmont 1:46ce1e16c870 87 / f_lseek() reports required table size on creating CLMP.
emilmont 1:46ce1e16c870 88 / Extended format syntax of f_printf function.
emilmont 1:46ce1e16c870 89 / Ignores duplicated directory separators in given path name.
emilmont 1:46ce1e16c870 90 /
emilmont 1:46ce1e16c870 91 / Sep 06,'11 R0.09 f_mkfs() supports multiple partition to finish the multiple partition feature.
emilmont 1:46ce1e16c870 92 / Added f_fdisk(). (_MULTI_PARTITION = 2)
emilmont 1:46ce1e16c870 93 / Aug 27,'12 R0.09a Fixed assertion failure due to OS/2 EA on FAT12/16 volume.
emilmont 1:46ce1e16c870 94 / Changed f_open() and f_opendir reject null object pointer to avoid crash.
emilmont 1:46ce1e16c870 95 / Changed option name _FS_SHARE to _FS_LOCK.
emilmont 1:46ce1e16c870 96 /---------------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 97
emilmont 1:46ce1e16c870 98 #include "ff.h" /* FatFs configurations and declarations */
emilmont 1:46ce1e16c870 99 #include "diskio.h" /* Declarations of low level disk I/O functions */
emilmont 1:46ce1e16c870 100
emilmont 1:46ce1e16c870 101
emilmont 1:46ce1e16c870 102 /*--------------------------------------------------------------------------
emilmont 1:46ce1e16c870 103
emilmont 1:46ce1e16c870 104 Module Private Definitions
emilmont 1:46ce1e16c870 105
emilmont 1:46ce1e16c870 106 ---------------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 107
emilmont 1:46ce1e16c870 108 #if _FATFS != 4004 /* Revision ID */
emilmont 1:46ce1e16c870 109 #error Wrong include file (ff.h).
emilmont 1:46ce1e16c870 110 #endif
emilmont 1:46ce1e16c870 111
emilmont 1:46ce1e16c870 112
emilmont 1:46ce1e16c870 113 /* Definitions on sector size */
emilmont 1:46ce1e16c870 114 #if _MAX_SS != 512 && _MAX_SS != 1024 && _MAX_SS != 2048 && _MAX_SS != 4096
emilmont 1:46ce1e16c870 115 #error Wrong sector size.
emilmont 1:46ce1e16c870 116 #endif
emilmont 1:46ce1e16c870 117 #if _MAX_SS != 512
emilmont 1:46ce1e16c870 118 #define SS(fs) ((fs)->ssize) /* Variable sector size */
emilmont 1:46ce1e16c870 119 #else
emilmont 1:46ce1e16c870 120 #define SS(fs) 512U /* Fixed sector size */
emilmont 1:46ce1e16c870 121 #endif
emilmont 1:46ce1e16c870 122
emilmont 1:46ce1e16c870 123
emilmont 1:46ce1e16c870 124 /* Reentrancy related */
emilmont 1:46ce1e16c870 125 #if _FS_REENTRANT
emilmont 1:46ce1e16c870 126 #if _USE_LFN == 1
emilmont 1:46ce1e16c870 127 #error Static LFN work area must not be used in re-entrant configuration.
emilmont 1:46ce1e16c870 128 #endif
emilmont 1:46ce1e16c870 129 #define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; }
emilmont 1:46ce1e16c870 130 #define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }
emilmont 1:46ce1e16c870 131 #else
emilmont 1:46ce1e16c870 132 #define ENTER_FF(fs)
emilmont 1:46ce1e16c870 133 #define LEAVE_FF(fs, res) return res
emilmont 1:46ce1e16c870 134 #endif
emilmont 1:46ce1e16c870 135
emilmont 1:46ce1e16c870 136 #define ABORT(fs, res) { fp->flag |= FA__ERROR; LEAVE_FF(fs, res); }
emilmont 1:46ce1e16c870 137
emilmont 1:46ce1e16c870 138
emilmont 1:46ce1e16c870 139 /* File access control feature */
emilmont 1:46ce1e16c870 140 #if _FS_LOCK
emilmont 1:46ce1e16c870 141 #if _FS_READONLY
emilmont 1:46ce1e16c870 142 #error _FS_LOCK must be 0 on read-only cfg.
emilmont 1:46ce1e16c870 143 #endif
emilmont 1:46ce1e16c870 144 typedef struct {
emilmont 1:46ce1e16c870 145 FATFS *fs; /* File ID 1, volume (NULL:blank entry) */
emilmont 1:46ce1e16c870 146 DWORD clu; /* File ID 2, directory */
emilmont 1:46ce1e16c870 147 WORD idx; /* File ID 3, directory index */
emilmont 1:46ce1e16c870 148 WORD ctr; /* File open counter, 0:none, 0x01..0xFF:read open count, 0x100:write mode */
emilmont 1:46ce1e16c870 149 } FILESEM;
emilmont 1:46ce1e16c870 150 #endif
emilmont 1:46ce1e16c870 151
emilmont 1:46ce1e16c870 152
emilmont 1:46ce1e16c870 153
emilmont 1:46ce1e16c870 154 /* DBCS code ranges and SBCS extend char conversion table */
emilmont 1:46ce1e16c870 155
emilmont 1:46ce1e16c870 156 #if _CODE_PAGE == 932 /* Japanese Shift-JIS */
emilmont 1:46ce1e16c870 157 #define _DF1S 0x81 /* DBC 1st byte range 1 start */
emilmont 1:46ce1e16c870 158 #define _DF1E 0x9F /* DBC 1st byte range 1 end */
emilmont 1:46ce1e16c870 159 #define _DF2S 0xE0 /* DBC 1st byte range 2 start */
emilmont 1:46ce1e16c870 160 #define _DF2E 0xFC /* DBC 1st byte range 2 end */
emilmont 1:46ce1e16c870 161 #define _DS1S 0x40 /* DBC 2nd byte range 1 start */
emilmont 1:46ce1e16c870 162 #define _DS1E 0x7E /* DBC 2nd byte range 1 end */
emilmont 1:46ce1e16c870 163 #define _DS2S 0x80 /* DBC 2nd byte range 2 start */
emilmont 1:46ce1e16c870 164 #define _DS2E 0xFC /* DBC 2nd byte range 2 end */
emilmont 1:46ce1e16c870 165
emilmont 1:46ce1e16c870 166 #elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
emilmont 1:46ce1e16c870 167 #define _DF1S 0x81
emilmont 1:46ce1e16c870 168 #define _DF1E 0xFE
emilmont 1:46ce1e16c870 169 #define _DS1S 0x40
emilmont 1:46ce1e16c870 170 #define _DS1E 0x7E
emilmont 1:46ce1e16c870 171 #define _DS2S 0x80
emilmont 1:46ce1e16c870 172 #define _DS2E 0xFE
emilmont 1:46ce1e16c870 173
emilmont 1:46ce1e16c870 174 #elif _CODE_PAGE == 949 /* Korean */
emilmont 1:46ce1e16c870 175 #define _DF1S 0x81
emilmont 1:46ce1e16c870 176 #define _DF1E 0xFE
emilmont 1:46ce1e16c870 177 #define _DS1S 0x41
emilmont 1:46ce1e16c870 178 #define _DS1E 0x5A
emilmont 1:46ce1e16c870 179 #define _DS2S 0x61
emilmont 1:46ce1e16c870 180 #define _DS2E 0x7A
emilmont 1:46ce1e16c870 181 #define _DS3S 0x81
emilmont 1:46ce1e16c870 182 #define _DS3E 0xFE
emilmont 1:46ce1e16c870 183
emilmont 1:46ce1e16c870 184 #elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */
emilmont 1:46ce1e16c870 185 #define _DF1S 0x81
emilmont 1:46ce1e16c870 186 #define _DF1E 0xFE
emilmont 1:46ce1e16c870 187 #define _DS1S 0x40
emilmont 1:46ce1e16c870 188 #define _DS1E 0x7E
emilmont 1:46ce1e16c870 189 #define _DS2S 0xA1
emilmont 1:46ce1e16c870 190 #define _DS2E 0xFE
emilmont 1:46ce1e16c870 191
emilmont 1:46ce1e16c870 192 #elif _CODE_PAGE == 437 /* U.S. (OEM) */
emilmont 1:46ce1e16c870 193 #define _DF1S 0
emilmont 1:46ce1e16c870 194 #define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F,0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 195 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 196 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 197 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 198
emilmont 1:46ce1e16c870 199 #elif _CODE_PAGE == 720 /* Arabic (OEM) */
emilmont 1:46ce1e16c870 200 #define _DF1S 0
emilmont 1:46ce1e16c870 201 #define _EXCVT {0x80,0x81,0x45,0x41,0x84,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x8E,0x8F,0x90,0x92,0x92,0x93,0x94,0x95,0x49,0x49,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 202 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 203 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 204 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 205
emilmont 1:46ce1e16c870 206 #elif _CODE_PAGE == 737 /* Greek (OEM) */
emilmont 1:46ce1e16c870 207 #define _DF1S 0
emilmont 1:46ce1e16c870 208 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \
emilmont 1:46ce1e16c870 209 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 210 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 211 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xE7,0xE8,0xF1,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 212
emilmont 1:46ce1e16c870 213 #elif _CODE_PAGE == 775 /* Baltic (OEM) */
emilmont 1:46ce1e16c870 214 #define _DF1S 0
emilmont 1:46ce1e16c870 215 #define _EXCVT {0x80,0x9A,0x91,0xA0,0x8E,0x95,0x8F,0x80,0xAD,0xED,0x8A,0x8A,0xA1,0x8D,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0x95,0x96,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 216 0xA0,0xA1,0xE0,0xA3,0xA3,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 217 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 218 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE3,0xE8,0xE8,0xEA,0xEA,0xEE,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 219
emilmont 1:46ce1e16c870 220 #elif _CODE_PAGE == 850 /* Multilingual Latin 1 (OEM) */
emilmont 1:46ce1e16c870 221 #define _DF1S 0
emilmont 1:46ce1e16c870 222 #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0xDE,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x59,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 223 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 224 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 225 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE7,0xE9,0xEA,0xEB,0xED,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 226
emilmont 1:46ce1e16c870 227 #elif _CODE_PAGE == 852 /* Latin 2 (OEM) */
emilmont 1:46ce1e16c870 228 #define _DF1S 0
emilmont 1:46ce1e16c870 229 #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F,0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 230 0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA,0x8D,0xAC,0xB8,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBF, \
emilmont 1:46ce1e16c870 231 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC6,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD2,0xD3,0xD2,0xD5,0xD6,0xD7,0xB7,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 232 0xE0,0xE1,0xE2,0xE3,0xE3,0xD5,0xE6,0xE6,0xE8,0xE9,0xE8,0xEB,0xED,0xED,0xDD,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xEB,0xFC,0xFC,0xFE,0xFF}
emilmont 1:46ce1e16c870 233
emilmont 1:46ce1e16c870 234 #elif _CODE_PAGE == 855 /* Cyrillic (OEM) */
emilmont 1:46ce1e16c870 235 #define _DF1S 0
emilmont 1:46ce1e16c870 236 #define _EXCVT {0x81,0x81,0x83,0x83,0x85,0x85,0x87,0x87,0x89,0x89,0x8B,0x8B,0x8D,0x8D,0x8F,0x8F,0x91,0x91,0x93,0x93,0x95,0x95,0x97,0x97,0x99,0x99,0x9B,0x9B,0x9D,0x9D,0x9F,0x9F, \
emilmont 1:46ce1e16c870 237 0xA1,0xA1,0xA3,0xA3,0xA5,0xA5,0xA7,0xA7,0xA9,0xA9,0xAB,0xAB,0xAD,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB6,0xB6,0xB8,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBE,0xBF, \
emilmont 1:46ce1e16c870 238 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD3,0xD3,0xD5,0xD5,0xD7,0xD7,0xDD,0xD9,0xDA,0xDB,0xDC,0xDD,0xE0,0xDF, \
emilmont 1:46ce1e16c870 239 0xE0,0xE2,0xE2,0xE4,0xE4,0xE6,0xE6,0xE8,0xE8,0xEA,0xEA,0xEC,0xEC,0xEE,0xEE,0xEF,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 240
emilmont 1:46ce1e16c870 241 #elif _CODE_PAGE == 857 /* Turkish (OEM) */
emilmont 1:46ce1e16c870 242 #define _DF1S 0
emilmont 1:46ce1e16c870 243 #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0x98,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x98,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9E, \
emilmont 1:46ce1e16c870 244 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 245 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 246 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xDE,0x59,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 247
emilmont 1:46ce1e16c870 248 #elif _CODE_PAGE == 858 /* Multilingual Latin 1 + Euro (OEM) */
emilmont 1:46ce1e16c870 249 #define _DF1S 0
emilmont 1:46ce1e16c870 250 #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0xDE,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x59,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 251 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 252 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 253 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE7,0xE9,0xEA,0xEB,0xED,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 254
emilmont 1:46ce1e16c870 255 #elif _CODE_PAGE == 862 /* Hebrew (OEM) */
emilmont 1:46ce1e16c870 256 #define _DF1S 0
emilmont 1:46ce1e16c870 257 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 258 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 259 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 260 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 261
emilmont 1:46ce1e16c870 262 #elif _CODE_PAGE == 866 /* Russian (OEM) */
emilmont 1:46ce1e16c870 263 #define _DF1S 0
emilmont 1:46ce1e16c870 264 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 265 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 266 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 267 0x90,0x91,0x92,0x93,0x9d,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 268
emilmont 1:46ce1e16c870 269 #elif _CODE_PAGE == 874 /* Thai (OEM, Windows) */
emilmont 1:46ce1e16c870 270 #define _DF1S 0
emilmont 1:46ce1e16c870 271 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 272 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 273 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 274 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 275
emilmont 1:46ce1e16c870 276 #elif _CODE_PAGE == 1250 /* Central Europe (Windows) */
emilmont 1:46ce1e16c870 277 #define _DF1S 0
emilmont 1:46ce1e16c870 278 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x8D,0x8E,0x8F, \
emilmont 1:46ce1e16c870 279 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xA3,0xB4,0xB5,0xB6,0xB7,0xB8,0xA5,0xAA,0xBB,0xBC,0xBD,0xBC,0xAF, \
emilmont 1:46ce1e16c870 280 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 281 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF}
emilmont 1:46ce1e16c870 282
emilmont 1:46ce1e16c870 283 #elif _CODE_PAGE == 1251 /* Cyrillic (Windows) */
emilmont 1:46ce1e16c870 284 #define _DF1S 0
emilmont 1:46ce1e16c870 285 #define _EXCVT {0x80,0x81,0x82,0x82,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x80,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x8D,0x8E,0x8F, \
emilmont 1:46ce1e16c870 286 0xA0,0xA2,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB2,0xA5,0xB5,0xB6,0xB7,0xA8,0xB9,0xAA,0xBB,0xA3,0xBD,0xBD,0xAF, \
emilmont 1:46ce1e16c870 287 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 288 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF}
emilmont 1:46ce1e16c870 289
emilmont 1:46ce1e16c870 290 #elif _CODE_PAGE == 1252 /* Latin 1 (Windows) */
emilmont 1:46ce1e16c870 291 #define _DF1S 0
emilmont 1:46ce1e16c870 292 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0xAd,0x9B,0x8C,0x9D,0xAE,0x9F, \
emilmont 1:46ce1e16c870 293 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 294 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 295 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F}
emilmont 1:46ce1e16c870 296
emilmont 1:46ce1e16c870 297 #elif _CODE_PAGE == 1253 /* Greek (Windows) */
emilmont 1:46ce1e16c870 298 #define _DF1S 0
emilmont 1:46ce1e16c870 299 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 300 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 301 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xA2,0xB8,0xB9,0xBA, \
emilmont 1:46ce1e16c870 302 0xE0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xFB,0xBC,0xFD,0xBF,0xFF}
emilmont 1:46ce1e16c870 303
emilmont 1:46ce1e16c870 304 #elif _CODE_PAGE == 1254 /* Turkish (Windows) */
emilmont 1:46ce1e16c870 305 #define _DF1S 0
emilmont 1:46ce1e16c870 306 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 307 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 308 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 309 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F}
emilmont 1:46ce1e16c870 310
emilmont 1:46ce1e16c870 311 #elif _CODE_PAGE == 1255 /* Hebrew (Windows) */
emilmont 1:46ce1e16c870 312 #define _DF1S 0
emilmont 1:46ce1e16c870 313 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 314 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 315 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 316 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 317
emilmont 1:46ce1e16c870 318 #elif _CODE_PAGE == 1256 /* Arabic (Windows) */
emilmont 1:46ce1e16c870 319 #define _DF1S 0
emilmont 1:46ce1e16c870 320 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x8C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 321 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 322 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 323 0x41,0xE1,0x41,0xE3,0xE4,0xE5,0xE6,0x43,0x45,0x45,0x45,0x45,0xEC,0xED,0x49,0x49,0xF0,0xF1,0xF2,0xF3,0x4F,0xF5,0xF6,0xF7,0xF8,0x55,0xFA,0x55,0x55,0xFD,0xFE,0xFF}
emilmont 1:46ce1e16c870 324
emilmont 1:46ce1e16c870 325 #elif _CODE_PAGE == 1257 /* Baltic (Windows) */
emilmont 1:46ce1e16c870 326 #define _DF1S 0
emilmont 1:46ce1e16c870 327 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 328 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xA8,0xB9,0xAA,0xBB,0xBC,0xBD,0xBE,0xAF, \
emilmont 1:46ce1e16c870 329 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 330 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF}
emilmont 1:46ce1e16c870 331
emilmont 1:46ce1e16c870 332 #elif _CODE_PAGE == 1258 /* Vietnam (OEM, Windows) */
emilmont 1:46ce1e16c870 333 #define _DF1S 0
emilmont 1:46ce1e16c870 334 #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0xAC,0x9D,0x9E,0x9F, \
emilmont 1:46ce1e16c870 335 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
emilmont 1:46ce1e16c870 336 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
emilmont 1:46ce1e16c870 337 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xEC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xFE,0x9F}
emilmont 1:46ce1e16c870 338
emilmont 1:46ce1e16c870 339 #elif _CODE_PAGE == 1 /* ASCII (for only non-LFN cfg) */
emilmont 1:46ce1e16c870 340 #if _USE_LFN
emilmont 1:46ce1e16c870 341 #error Cannot use LFN feature without valid code page.
emilmont 1:46ce1e16c870 342 #endif
emilmont 1:46ce1e16c870 343 #define _DF1S 0
emilmont 1:46ce1e16c870 344
emilmont 1:46ce1e16c870 345 #else
emilmont 1:46ce1e16c870 346 #error Unknown code page
emilmont 1:46ce1e16c870 347
emilmont 1:46ce1e16c870 348 #endif
emilmont 1:46ce1e16c870 349
emilmont 1:46ce1e16c870 350
emilmont 1:46ce1e16c870 351 /* Character code support macros */
emilmont 1:46ce1e16c870 352 #define IsUpper(c) (((c)>='A')&&((c)<='Z'))
emilmont 1:46ce1e16c870 353 #define IsLower(c) (((c)>='a')&&((c)<='z'))
emilmont 1:46ce1e16c870 354 #define IsDigit(c) (((c)>='0')&&((c)<='9'))
emilmont 1:46ce1e16c870 355
emilmont 1:46ce1e16c870 356 #if _DF1S /* Code page is DBCS */
emilmont 1:46ce1e16c870 357
emilmont 1:46ce1e16c870 358 #ifdef _DF2S /* Two 1st byte areas */
emilmont 1:46ce1e16c870 359 #define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
emilmont 1:46ce1e16c870 360 #else /* One 1st byte area */
emilmont 1:46ce1e16c870 361 #define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
emilmont 1:46ce1e16c870 362 #endif
emilmont 1:46ce1e16c870 363
emilmont 1:46ce1e16c870 364 #ifdef _DS3S /* Three 2nd byte areas */
emilmont 1:46ce1e16c870 365 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
emilmont 1:46ce1e16c870 366 #else /* Two 2nd byte areas */
emilmont 1:46ce1e16c870 367 #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
emilmont 1:46ce1e16c870 368 #endif
emilmont 1:46ce1e16c870 369
emilmont 1:46ce1e16c870 370 #else /* Code page is SBCS */
emilmont 1:46ce1e16c870 371
emilmont 1:46ce1e16c870 372 #define IsDBCS1(c) 0
emilmont 1:46ce1e16c870 373 #define IsDBCS2(c) 0
emilmont 1:46ce1e16c870 374
emilmont 1:46ce1e16c870 375 #endif /* _DF1S */
emilmont 1:46ce1e16c870 376
emilmont 1:46ce1e16c870 377
emilmont 1:46ce1e16c870 378 /* Name status flags */
emilmont 1:46ce1e16c870 379 #define NS 11 /* Index of name status byte in fn[] */
emilmont 1:46ce1e16c870 380 #define NS_LOSS 0x01 /* Out of 8.3 format */
emilmont 1:46ce1e16c870 381 #define NS_LFN 0x02 /* Force to create LFN entry */
emilmont 1:46ce1e16c870 382 #define NS_LAST 0x04 /* Last segment */
emilmont 1:46ce1e16c870 383 #define NS_BODY 0x08 /* Lower case flag (body) */
emilmont 1:46ce1e16c870 384 #define NS_EXT 0x10 /* Lower case flag (ext) */
emilmont 1:46ce1e16c870 385 #define NS_DOT 0x20 /* Dot entry */
emilmont 1:46ce1e16c870 386
emilmont 1:46ce1e16c870 387
emilmont 1:46ce1e16c870 388 /* FAT sub-type boundaries */
emilmont 1:46ce1e16c870 389 /* Note that the FAT spec by Microsoft says 4085 but Windows works with 4087! */
emilmont 1:46ce1e16c870 390 #define MIN_FAT16 4086 /* Minimum number of clusters for FAT16 */
emilmont 1:46ce1e16c870 391 #define MIN_FAT32 65526 /* Minimum number of clusters for FAT32 */
emilmont 1:46ce1e16c870 392
emilmont 1:46ce1e16c870 393
emilmont 1:46ce1e16c870 394 /* FatFs refers the members in the FAT structures as byte array instead of
emilmont 1:46ce1e16c870 395 / structure member because the structure is not binary compatible between
emilmont 1:46ce1e16c870 396 / different platforms */
emilmont 1:46ce1e16c870 397
emilmont 1:46ce1e16c870 398 #define BS_jmpBoot 0 /* Jump instruction (3) */
emilmont 1:46ce1e16c870 399 #define BS_OEMName 3 /* OEM name (8) */
emilmont 1:46ce1e16c870 400 #define BPB_BytsPerSec 11 /* Sector size [byte] (2) */
emilmont 1:46ce1e16c870 401 #define BPB_SecPerClus 13 /* Cluster size [sector] (1) */
emilmont 1:46ce1e16c870 402 #define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (2) */
emilmont 1:46ce1e16c870 403 #define BPB_NumFATs 16 /* Number of FAT copies (1) */
emilmont 1:46ce1e16c870 404 #define BPB_RootEntCnt 17 /* Number of root dir entries for FAT12/16 (2) */
emilmont 1:46ce1e16c870 405 #define BPB_TotSec16 19 /* Volume size [sector] (2) */
emilmont 1:46ce1e16c870 406 #define BPB_Media 21 /* Media descriptor (1) */
emilmont 1:46ce1e16c870 407 #define BPB_FATSz16 22 /* FAT size [sector] (2) */
emilmont 1:46ce1e16c870 408 #define BPB_SecPerTrk 24 /* Track size [sector] (2) */
emilmont 1:46ce1e16c870 409 #define BPB_NumHeads 26 /* Number of heads (2) */
emilmont 1:46ce1e16c870 410 #define BPB_HiddSec 28 /* Number of special hidden sectors (4) */
emilmont 1:46ce1e16c870 411 #define BPB_TotSec32 32 /* Volume size [sector] (4) */
emilmont 1:46ce1e16c870 412 #define BS_DrvNum 36 /* Physical drive number (2) */
emilmont 1:46ce1e16c870 413 #define BS_BootSig 38 /* Extended boot signature (1) */
emilmont 1:46ce1e16c870 414 #define BS_VolID 39 /* Volume serial number (4) */
emilmont 1:46ce1e16c870 415 #define BS_VolLab 43 /* Volume label (8) */
emilmont 1:46ce1e16c870 416 #define BS_FilSysType 54 /* File system type (1) */
emilmont 1:46ce1e16c870 417 #define BPB_FATSz32 36 /* FAT size [sector] (4) */
emilmont 1:46ce1e16c870 418 #define BPB_ExtFlags 40 /* Extended flags (2) */
emilmont 1:46ce1e16c870 419 #define BPB_FSVer 42 /* File system version (2) */
emilmont 1:46ce1e16c870 420 #define BPB_RootClus 44 /* Root dir first cluster (4) */
emilmont 1:46ce1e16c870 421 #define BPB_FSInfo 48 /* Offset of FSInfo sector (2) */
emilmont 1:46ce1e16c870 422 #define BPB_BkBootSec 50 /* Offset of backup boot sector (2) */
emilmont 1:46ce1e16c870 423 #define BS_DrvNum32 64 /* Physical drive number (2) */
emilmont 1:46ce1e16c870 424 #define BS_BootSig32 66 /* Extended boot signature (1) */
emilmont 1:46ce1e16c870 425 #define BS_VolID32 67 /* Volume serial number (4) */
emilmont 1:46ce1e16c870 426 #define BS_VolLab32 71 /* Volume label (8) */
emilmont 1:46ce1e16c870 427 #define BS_FilSysType32 82 /* File system type (1) */
emilmont 1:46ce1e16c870 428 #define FSI_LeadSig 0 /* FSI: Leading signature (4) */
emilmont 1:46ce1e16c870 429 #define FSI_StrucSig 484 /* FSI: Structure signature (4) */
emilmont 1:46ce1e16c870 430 #define FSI_Free_Count 488 /* FSI: Number of free clusters (4) */
emilmont 1:46ce1e16c870 431 #define FSI_Nxt_Free 492 /* FSI: Last allocated cluster (4) */
emilmont 1:46ce1e16c870 432 #define MBR_Table 446 /* MBR: Partition table offset (2) */
emilmont 1:46ce1e16c870 433 #define SZ_PTE 16 /* MBR: Size of a partition table entry */
emilmont 1:46ce1e16c870 434 #define BS_55AA 510 /* Boot sector signature (2) */
emilmont 1:46ce1e16c870 435
emilmont 1:46ce1e16c870 436 #define DIR_Name 0 /* Short file name (11) */
emilmont 1:46ce1e16c870 437 #define DIR_Attr 11 /* Attribute (1) */
emilmont 1:46ce1e16c870 438 #define DIR_NTres 12 /* NT flag (1) */
emilmont 1:46ce1e16c870 439 #define DIR_CrtTimeTenth 13 /* Created time sub-second (1) */
emilmont 1:46ce1e16c870 440 #define DIR_CrtTime 14 /* Created time (2) */
emilmont 1:46ce1e16c870 441 #define DIR_CrtDate 16 /* Created date (2) */
emilmont 1:46ce1e16c870 442 #define DIR_LstAccDate 18 /* Last accessed date (2) */
emilmont 1:46ce1e16c870 443 #define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (2) */
emilmont 1:46ce1e16c870 444 #define DIR_WrtTime 22 /* Modified time (2) */
emilmont 1:46ce1e16c870 445 #define DIR_WrtDate 24 /* Modified date (2) */
emilmont 1:46ce1e16c870 446 #define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (2) */
emilmont 1:46ce1e16c870 447 #define DIR_FileSize 28 /* File size (4) */
emilmont 1:46ce1e16c870 448 #define LDIR_Ord 0 /* LFN entry order and LLE flag (1) */
emilmont 1:46ce1e16c870 449 #define LDIR_Attr 11 /* LFN attribute (1) */
emilmont 1:46ce1e16c870 450 #define LDIR_Type 12 /* LFN type (1) */
emilmont 1:46ce1e16c870 451 #define LDIR_Chksum 13 /* Sum of corresponding SFN entry */
emilmont 1:46ce1e16c870 452 #define LDIR_FstClusLO 26 /* Filled by zero (0) */
emilmont 1:46ce1e16c870 453 #define SZ_DIR 32 /* Size of a directory entry */
emilmont 1:46ce1e16c870 454 #define LLE 0x40 /* Last long entry flag in LDIR_Ord */
emilmont 1:46ce1e16c870 455 #define DDE 0xE5 /* Deleted directory entry mark in DIR_Name[0] */
emilmont 1:46ce1e16c870 456 #define NDDE 0x05 /* Replacement of the character collides with DDE */
emilmont 1:46ce1e16c870 457
emilmont 1:46ce1e16c870 458
emilmont 1:46ce1e16c870 459 /*------------------------------------------------------------*/
emilmont 1:46ce1e16c870 460 /* Module private work area */
emilmont 1:46ce1e16c870 461 /*------------------------------------------------------------*/
emilmont 1:46ce1e16c870 462 /* Note that uninitialized variables with static duration are
emilmont 1:46ce1e16c870 463 / zeroed/nulled at start-up. If not, the compiler or start-up
emilmont 1:46ce1e16c870 464 / routine is out of ANSI-C standard.
emilmont 1:46ce1e16c870 465 */
emilmont 1:46ce1e16c870 466
emilmont 1:46ce1e16c870 467 #if _VOLUMES
emilmont 1:46ce1e16c870 468 static
emilmont 1:46ce1e16c870 469 FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical drives) */
emilmont 1:46ce1e16c870 470 #else
emilmont 1:46ce1e16c870 471 #error Number of volumes must not be 0.
emilmont 1:46ce1e16c870 472 #endif
emilmont 1:46ce1e16c870 473
emilmont 1:46ce1e16c870 474 static
emilmont 1:46ce1e16c870 475 WORD Fsid; /* File system mount ID */
emilmont 1:46ce1e16c870 476
emilmont 1:46ce1e16c870 477 #if _FS_RPATH
emilmont 1:46ce1e16c870 478 static
emilmont 1:46ce1e16c870 479 BYTE CurrVol; /* Current drive */
emilmont 1:46ce1e16c870 480 #endif
emilmont 1:46ce1e16c870 481
emilmont 1:46ce1e16c870 482 #if _FS_LOCK
emilmont 1:46ce1e16c870 483 static
emilmont 1:46ce1e16c870 484 FILESEM Files[_FS_LOCK]; /* File lock semaphores */
emilmont 1:46ce1e16c870 485 #endif
emilmont 1:46ce1e16c870 486
emilmont 1:46ce1e16c870 487 #if _USE_LFN == 0 /* No LFN feature */
emilmont 1:46ce1e16c870 488 #define DEF_NAMEBUF BYTE sfn[12]
emilmont 1:46ce1e16c870 489 #define INIT_BUF(dobj) (dobj).fn = sfn
emilmont 1:46ce1e16c870 490 #define FREE_BUF()
emilmont 1:46ce1e16c870 491
emilmont 1:46ce1e16c870 492 #elif _USE_LFN == 1 /* LFN feature with static working buffer */
emilmont 1:46ce1e16c870 493 static WCHAR LfnBuf[_MAX_LFN+1];
emilmont 1:46ce1e16c870 494 #define DEF_NAMEBUF BYTE sfn[12]
emilmont 1:46ce1e16c870 495 #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = LfnBuf; }
emilmont 1:46ce1e16c870 496 #define FREE_BUF()
emilmont 1:46ce1e16c870 497
emilmont 1:46ce1e16c870 498 #elif _USE_LFN == 2 /* LFN feature with dynamic working buffer on the stack */
emilmont 1:46ce1e16c870 499 #define DEF_NAMEBUF BYTE sfn[12]; WCHAR lbuf[_MAX_LFN+1]
emilmont 1:46ce1e16c870 500 #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = lbuf; }
emilmont 1:46ce1e16c870 501 #define FREE_BUF()
emilmont 1:46ce1e16c870 502
emilmont 1:46ce1e16c870 503 #elif _USE_LFN == 3 /* LFN feature with dynamic working buffer on the heap */
emilmont 1:46ce1e16c870 504 #define DEF_NAMEBUF BYTE sfn[12]; WCHAR *lfn
emilmont 1:46ce1e16c870 505 #define INIT_BUF(dobj) { lfn = ff_memalloc((_MAX_LFN + 1) * 2); \
emilmont 1:46ce1e16c870 506 if (!lfn) LEAVE_FF((dobj).fs, FR_NOT_ENOUGH_CORE); \
emilmont 1:46ce1e16c870 507 (dobj).lfn = lfn; (dobj).fn = sfn; }
emilmont 1:46ce1e16c870 508 #define FREE_BUF() ff_memfree(lfn)
emilmont 1:46ce1e16c870 509
emilmont 1:46ce1e16c870 510 #else
emilmont 1:46ce1e16c870 511 #error Wrong LFN configuration.
emilmont 1:46ce1e16c870 512 #endif
emilmont 1:46ce1e16c870 513
emilmont 1:46ce1e16c870 514
emilmont 1:46ce1e16c870 515
emilmont 1:46ce1e16c870 516
emilmont 1:46ce1e16c870 517 /*--------------------------------------------------------------------------
emilmont 1:46ce1e16c870 518
emilmont 1:46ce1e16c870 519 Module Private Functions
emilmont 1:46ce1e16c870 520
emilmont 1:46ce1e16c870 521 ---------------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 522
emilmont 1:46ce1e16c870 523
emilmont 1:46ce1e16c870 524 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 525 /* String functions */
emilmont 1:46ce1e16c870 526 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 527
emilmont 1:46ce1e16c870 528 /* Copy memory to memory */
emilmont 1:46ce1e16c870 529 static
emilmont 1:46ce1e16c870 530 void mem_cpy (void* dst, const void* src, UINT cnt) {
emilmont 1:46ce1e16c870 531 BYTE *d = (BYTE*)dst;
emilmont 1:46ce1e16c870 532 const BYTE *s = (const BYTE*)src;
emilmont 1:46ce1e16c870 533
emilmont 1:46ce1e16c870 534 #if _WORD_ACCESS == 1
emilmont 1:46ce1e16c870 535 while (cnt >= sizeof (int)) {
emilmont 1:46ce1e16c870 536 *(int*)d = *(int*)s;
emilmont 1:46ce1e16c870 537 d += sizeof (int); s += sizeof (int);
emilmont 1:46ce1e16c870 538 cnt -= sizeof (int);
emilmont 1:46ce1e16c870 539 }
emilmont 1:46ce1e16c870 540 #endif
emilmont 1:46ce1e16c870 541 while (cnt--)
emilmont 1:46ce1e16c870 542 *d++ = *s++;
emilmont 1:46ce1e16c870 543 }
emilmont 1:46ce1e16c870 544
emilmont 1:46ce1e16c870 545 /* Fill memory */
emilmont 1:46ce1e16c870 546 static
emilmont 1:46ce1e16c870 547 void mem_set (void* dst, int val, UINT cnt) {
emilmont 1:46ce1e16c870 548 BYTE *d = (BYTE*)dst;
emilmont 1:46ce1e16c870 549
emilmont 1:46ce1e16c870 550 while (cnt--)
emilmont 1:46ce1e16c870 551 *d++ = (BYTE)val;
emilmont 1:46ce1e16c870 552 }
emilmont 1:46ce1e16c870 553
emilmont 1:46ce1e16c870 554 /* Compare memory to memory */
emilmont 1:46ce1e16c870 555 static
emilmont 1:46ce1e16c870 556 int mem_cmp (const void* dst, const void* src, UINT cnt) {
emilmont 1:46ce1e16c870 557 const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
emilmont 1:46ce1e16c870 558 int r = 0;
emilmont 1:46ce1e16c870 559
emilmont 1:46ce1e16c870 560 while (cnt-- && (r = *d++ - *s++) == 0) ;
emilmont 1:46ce1e16c870 561 return r;
emilmont 1:46ce1e16c870 562 }
emilmont 1:46ce1e16c870 563
emilmont 1:46ce1e16c870 564 /* Check if chr is contained in the string */
emilmont 1:46ce1e16c870 565 static
emilmont 1:46ce1e16c870 566 int chk_chr (const char* str, int chr) {
emilmont 1:46ce1e16c870 567 while (*str && *str != chr) str++;
emilmont 1:46ce1e16c870 568 return *str;
emilmont 1:46ce1e16c870 569 }
emilmont 1:46ce1e16c870 570
emilmont 1:46ce1e16c870 571
emilmont 1:46ce1e16c870 572
emilmont 1:46ce1e16c870 573 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 574 /* Request/Release grant to access the volume */
emilmont 1:46ce1e16c870 575 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 576 #if _FS_REENTRANT
emilmont 1:46ce1e16c870 577
emilmont 1:46ce1e16c870 578 static
emilmont 1:46ce1e16c870 579 int lock_fs (
emilmont 1:46ce1e16c870 580 FATFS *fs /* File system object */
emilmont 1:46ce1e16c870 581 )
emilmont 1:46ce1e16c870 582 {
emilmont 1:46ce1e16c870 583 return ff_req_grant(fs->sobj);
emilmont 1:46ce1e16c870 584 }
emilmont 1:46ce1e16c870 585
emilmont 1:46ce1e16c870 586
emilmont 1:46ce1e16c870 587 static
emilmont 1:46ce1e16c870 588 void unlock_fs (
emilmont 1:46ce1e16c870 589 FATFS *fs, /* File system object */
emilmont 1:46ce1e16c870 590 FRESULT res /* Result code to be returned */
emilmont 1:46ce1e16c870 591 )
emilmont 1:46ce1e16c870 592 {
emilmont 1:46ce1e16c870 593 if (fs &&
emilmont 1:46ce1e16c870 594 res != FR_NOT_ENABLED &&
emilmont 1:46ce1e16c870 595 res != FR_INVALID_DRIVE &&
emilmont 1:46ce1e16c870 596 res != FR_INVALID_OBJECT &&
emilmont 1:46ce1e16c870 597 res != FR_TIMEOUT) {
emilmont 1:46ce1e16c870 598 ff_rel_grant(fs->sobj);
emilmont 1:46ce1e16c870 599 }
emilmont 1:46ce1e16c870 600 }
emilmont 1:46ce1e16c870 601 #endif
emilmont 1:46ce1e16c870 602
emilmont 1:46ce1e16c870 603
emilmont 1:46ce1e16c870 604
emilmont 1:46ce1e16c870 605 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 606 /* File lock control functions */
emilmont 1:46ce1e16c870 607 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 608 #if _FS_LOCK
emilmont 1:46ce1e16c870 609
emilmont 1:46ce1e16c870 610 static
emilmont 1:46ce1e16c870 611 FRESULT chk_lock ( /* Check if the file can be accessed */
emilmont 1:46ce1e16c870 612 DIR* dj, /* Directory object pointing the file to be checked */
emilmont 1:46ce1e16c870 613 int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */
emilmont 1:46ce1e16c870 614 )
emilmont 1:46ce1e16c870 615 {
emilmont 1:46ce1e16c870 616 UINT i, be;
emilmont 1:46ce1e16c870 617
emilmont 1:46ce1e16c870 618 /* Search file semaphore table */
emilmont 1:46ce1e16c870 619 for (i = be = 0; i < _FS_LOCK; i++) {
emilmont 1:46ce1e16c870 620 if (Files[i].fs) { /* Existing entry */
emilmont 1:46ce1e16c870 621 if (Files[i].fs == dj->fs && /* Check if the file matched with an open file */
emilmont 1:46ce1e16c870 622 Files[i].clu == dj->sclust &&
emilmont 1:46ce1e16c870 623 Files[i].idx == dj->index) break;
emilmont 1:46ce1e16c870 624 } else { /* Blank entry */
emilmont 1:46ce1e16c870 625 be++;
emilmont 1:46ce1e16c870 626 }
emilmont 1:46ce1e16c870 627 }
emilmont 1:46ce1e16c870 628 if (i == _FS_LOCK) /* The file is not opened */
emilmont 1:46ce1e16c870 629 return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES; /* Is there a blank entry for new file? */
emilmont 1:46ce1e16c870 630
emilmont 1:46ce1e16c870 631 /* The file has been opened. Reject any open against writing file and all write mode open */
emilmont 1:46ce1e16c870 632 return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
emilmont 1:46ce1e16c870 633 }
emilmont 1:46ce1e16c870 634
emilmont 1:46ce1e16c870 635
emilmont 1:46ce1e16c870 636 static
emilmont 1:46ce1e16c870 637 int enq_lock (void) /* Check if an entry is available for a new file */
emilmont 1:46ce1e16c870 638 {
emilmont 1:46ce1e16c870 639 UINT i;
emilmont 1:46ce1e16c870 640
emilmont 1:46ce1e16c870 641 for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
emilmont 1:46ce1e16c870 642 return (i == _FS_LOCK) ? 0 : 1;
emilmont 1:46ce1e16c870 643 }
emilmont 1:46ce1e16c870 644
emilmont 1:46ce1e16c870 645
emilmont 1:46ce1e16c870 646 static
emilmont 1:46ce1e16c870 647 UINT inc_lock ( /* Increment file open counter and returns its index (0:int error) */
emilmont 1:46ce1e16c870 648 DIR* dj, /* Directory object pointing the file to register or increment */
emilmont 1:46ce1e16c870 649 int acc /* Desired access mode (0:Read, !0:Write) */
emilmont 1:46ce1e16c870 650 )
emilmont 1:46ce1e16c870 651 {
emilmont 1:46ce1e16c870 652 UINT i;
emilmont 1:46ce1e16c870 653
emilmont 1:46ce1e16c870 654
emilmont 1:46ce1e16c870 655 for (i = 0; i < _FS_LOCK; i++) { /* Find the file */
emilmont 1:46ce1e16c870 656 if (Files[i].fs == dj->fs &&
emilmont 1:46ce1e16c870 657 Files[i].clu == dj->sclust &&
emilmont 1:46ce1e16c870 658 Files[i].idx == dj->index) break;
emilmont 1:46ce1e16c870 659 }
emilmont 1:46ce1e16c870 660
emilmont 1:46ce1e16c870 661 if (i == _FS_LOCK) { /* Not opened. Register it as new. */
emilmont 1:46ce1e16c870 662 for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
emilmont 1:46ce1e16c870 663 if (i == _FS_LOCK) return 0; /* No space to register (int err) */
emilmont 1:46ce1e16c870 664 Files[i].fs = dj->fs;
emilmont 1:46ce1e16c870 665 Files[i].clu = dj->sclust;
emilmont 1:46ce1e16c870 666 Files[i].idx = dj->index;
emilmont 1:46ce1e16c870 667 Files[i].ctr = 0;
emilmont 1:46ce1e16c870 668 }
emilmont 1:46ce1e16c870 669
emilmont 1:46ce1e16c870 670 if (acc && Files[i].ctr) return 0; /* Access violation (int err) */
emilmont 1:46ce1e16c870 671
emilmont 1:46ce1e16c870 672 Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */
emilmont 1:46ce1e16c870 673
emilmont 1:46ce1e16c870 674 return i + 1;
emilmont 1:46ce1e16c870 675 }
emilmont 1:46ce1e16c870 676
emilmont 1:46ce1e16c870 677
emilmont 1:46ce1e16c870 678 static
emilmont 1:46ce1e16c870 679 FRESULT dec_lock ( /* Decrement file open counter */
emilmont 1:46ce1e16c870 680 UINT i /* Semaphore index */
emilmont 1:46ce1e16c870 681 )
emilmont 1:46ce1e16c870 682 {
emilmont 1:46ce1e16c870 683 WORD n;
emilmont 1:46ce1e16c870 684 FRESULT res;
emilmont 1:46ce1e16c870 685
emilmont 1:46ce1e16c870 686
emilmont 1:46ce1e16c870 687 if (--i < _FS_LOCK) {
emilmont 1:46ce1e16c870 688 n = Files[i].ctr;
emilmont 1:46ce1e16c870 689 if (n == 0x100) n = 0;
emilmont 1:46ce1e16c870 690 if (n) n--;
emilmont 1:46ce1e16c870 691 Files[i].ctr = n;
emilmont 1:46ce1e16c870 692 if (!n) Files[i].fs = 0;
emilmont 1:46ce1e16c870 693 res = FR_OK;
emilmont 1:46ce1e16c870 694 } else {
emilmont 1:46ce1e16c870 695 res = FR_INT_ERR;
emilmont 1:46ce1e16c870 696 }
emilmont 1:46ce1e16c870 697 return res;
emilmont 1:46ce1e16c870 698 }
emilmont 1:46ce1e16c870 699
emilmont 1:46ce1e16c870 700
emilmont 1:46ce1e16c870 701 static
emilmont 1:46ce1e16c870 702 void clear_lock ( /* Clear lock entries of the volume */
emilmont 1:46ce1e16c870 703 FATFS *fs
emilmont 1:46ce1e16c870 704 )
emilmont 1:46ce1e16c870 705 {
emilmont 1:46ce1e16c870 706 UINT i;
emilmont 1:46ce1e16c870 707
emilmont 1:46ce1e16c870 708 for (i = 0; i < _FS_LOCK; i++) {
emilmont 1:46ce1e16c870 709 if (Files[i].fs == fs) Files[i].fs = 0;
emilmont 1:46ce1e16c870 710 }
emilmont 1:46ce1e16c870 711 }
emilmont 1:46ce1e16c870 712 #endif
emilmont 1:46ce1e16c870 713
emilmont 1:46ce1e16c870 714
emilmont 1:46ce1e16c870 715
emilmont 1:46ce1e16c870 716 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 717 /* Change window offset */
emilmont 1:46ce1e16c870 718 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 719
emilmont 1:46ce1e16c870 720 static
emilmont 1:46ce1e16c870 721 FRESULT move_window (
emilmont 1:46ce1e16c870 722 FATFS *fs, /* File system object */
emilmont 1:46ce1e16c870 723 DWORD sector /* Sector number to make appearance in the fs->win[] */
emilmont 1:46ce1e16c870 724 ) /* Move to zero only writes back dirty window */
emilmont 1:46ce1e16c870 725 {
emilmont 1:46ce1e16c870 726 DWORD wsect;
emilmont 1:46ce1e16c870 727
emilmont 1:46ce1e16c870 728
emilmont 1:46ce1e16c870 729 wsect = fs->winsect;
emilmont 1:46ce1e16c870 730 if (wsect != sector) { /* Changed current window */
emilmont 1:46ce1e16c870 731 #if !_FS_READONLY
emilmont 1:46ce1e16c870 732 if (fs->wflag) { /* Write back dirty window if needed */
emilmont 1:46ce1e16c870 733 if (disk_write(fs->drv, fs->win, wsect, 1) != RES_OK)
emilmont 1:46ce1e16c870 734 return FR_DISK_ERR;
emilmont 1:46ce1e16c870 735 fs->wflag = 0;
emilmont 1:46ce1e16c870 736 if (wsect < (fs->fatbase + fs->fsize)) { /* In FAT area */
emilmont 1:46ce1e16c870 737 BYTE nf;
emilmont 1:46ce1e16c870 738 for (nf = fs->n_fats; nf > 1; nf--) { /* Reflect the change to all FAT copies */
emilmont 1:46ce1e16c870 739 wsect += fs->fsize;
emilmont 1:46ce1e16c870 740 disk_write(fs->drv, fs->win, wsect, 1);
emilmont 1:46ce1e16c870 741 }
emilmont 1:46ce1e16c870 742 }
emilmont 1:46ce1e16c870 743 }
emilmont 1:46ce1e16c870 744 #endif
emilmont 1:46ce1e16c870 745 if (sector) {
emilmont 1:46ce1e16c870 746 if (disk_read(fs->drv, fs->win, sector, 1) != RES_OK)
emilmont 1:46ce1e16c870 747 return FR_DISK_ERR;
emilmont 1:46ce1e16c870 748 fs->winsect = sector;
emilmont 1:46ce1e16c870 749 }
emilmont 1:46ce1e16c870 750 }
emilmont 1:46ce1e16c870 751
emilmont 1:46ce1e16c870 752 return FR_OK;
emilmont 1:46ce1e16c870 753 }
emilmont 1:46ce1e16c870 754
emilmont 1:46ce1e16c870 755
emilmont 1:46ce1e16c870 756
emilmont 1:46ce1e16c870 757
emilmont 1:46ce1e16c870 758 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 759 /* Clean-up cached data */
emilmont 1:46ce1e16c870 760 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 761 #if !_FS_READONLY
emilmont 1:46ce1e16c870 762 static
emilmont 1:46ce1e16c870 763 FRESULT sync ( /* FR_OK: successful, FR_DISK_ERR: failed */
emilmont 1:46ce1e16c870 764 FATFS *fs /* File system object */
emilmont 1:46ce1e16c870 765 )
emilmont 1:46ce1e16c870 766 {
emilmont 1:46ce1e16c870 767 FRESULT res;
emilmont 1:46ce1e16c870 768
emilmont 1:46ce1e16c870 769
emilmont 1:46ce1e16c870 770 res = move_window(fs, 0);
emilmont 1:46ce1e16c870 771 if (res == FR_OK) {
emilmont 1:46ce1e16c870 772 /* Update FSInfo sector if needed */
emilmont 1:46ce1e16c870 773 if (fs->fs_type == FS_FAT32 && fs->fsi_flag) {
emilmont 1:46ce1e16c870 774 fs->winsect = 0;
emilmont 1:46ce1e16c870 775 /* Create FSInfo structure */
emilmont 1:46ce1e16c870 776 mem_set(fs->win, 0, 512);
emilmont 1:46ce1e16c870 777 ST_WORD(fs->win+BS_55AA, 0xAA55);
emilmont 1:46ce1e16c870 778 ST_DWORD(fs->win+FSI_LeadSig, 0x41615252);
emilmont 1:46ce1e16c870 779 ST_DWORD(fs->win+FSI_StrucSig, 0x61417272);
emilmont 1:46ce1e16c870 780 ST_DWORD(fs->win+FSI_Free_Count, fs->free_clust);
emilmont 1:46ce1e16c870 781 ST_DWORD(fs->win+FSI_Nxt_Free, fs->last_clust);
emilmont 1:46ce1e16c870 782 /* Write it into the FSInfo sector */
emilmont 1:46ce1e16c870 783 disk_write(fs->drv, fs->win, fs->fsi_sector, 1);
emilmont 1:46ce1e16c870 784 fs->fsi_flag = 0;
emilmont 1:46ce1e16c870 785 }
emilmont 1:46ce1e16c870 786 /* Make sure that no pending write process in the physical drive */
emilmont 1:46ce1e16c870 787 if (disk_ioctl(fs->drv, CTRL_SYNC, 0) != RES_OK)
emilmont 1:46ce1e16c870 788 res = FR_DISK_ERR;
emilmont 1:46ce1e16c870 789 }
emilmont 1:46ce1e16c870 790
emilmont 1:46ce1e16c870 791 return res;
emilmont 1:46ce1e16c870 792 }
emilmont 1:46ce1e16c870 793 #endif
emilmont 1:46ce1e16c870 794
emilmont 1:46ce1e16c870 795
emilmont 1:46ce1e16c870 796
emilmont 1:46ce1e16c870 797
emilmont 1:46ce1e16c870 798 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 799 /* Get sector# from cluster# */
emilmont 1:46ce1e16c870 800 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 801
emilmont 1:46ce1e16c870 802
emilmont 1:46ce1e16c870 803 DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */
emilmont 1:46ce1e16c870 804 FATFS *fs, /* File system object */
emilmont 1:46ce1e16c870 805 DWORD clst /* Cluster# to be converted */
emilmont 1:46ce1e16c870 806 )
emilmont 1:46ce1e16c870 807 {
emilmont 1:46ce1e16c870 808 clst -= 2;
emilmont 1:46ce1e16c870 809 if (clst >= (fs->n_fatent - 2)) return 0; /* Invalid cluster# */
emilmont 1:46ce1e16c870 810 return clst * fs->csize + fs->database;
emilmont 1:46ce1e16c870 811 }
emilmont 1:46ce1e16c870 812
emilmont 1:46ce1e16c870 813
emilmont 1:46ce1e16c870 814
emilmont 1:46ce1e16c870 815
emilmont 1:46ce1e16c870 816 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 817 /* FAT access - Read value of a FAT entry */
emilmont 1:46ce1e16c870 818 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 819
emilmont 1:46ce1e16c870 820
emilmont 1:46ce1e16c870 821 DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, Else:Cluster status */
emilmont 1:46ce1e16c870 822 FATFS *fs, /* File system object */
emilmont 1:46ce1e16c870 823 DWORD clst /* Cluster# to get the link information */
emilmont 1:46ce1e16c870 824 )
emilmont 1:46ce1e16c870 825 {
emilmont 1:46ce1e16c870 826 UINT wc, bc;
emilmont 1:46ce1e16c870 827 BYTE *p;
emilmont 1:46ce1e16c870 828
emilmont 1:46ce1e16c870 829
emilmont 1:46ce1e16c870 830 if (clst < 2 || clst >= fs->n_fatent) /* Check range */
emilmont 1:46ce1e16c870 831 return 1;
emilmont 1:46ce1e16c870 832
emilmont 1:46ce1e16c870 833 switch (fs->fs_type) {
emilmont 1:46ce1e16c870 834 case FS_FAT12 :
emilmont 1:46ce1e16c870 835 bc = (UINT)clst; bc += bc / 2;
emilmont 1:46ce1e16c870 836 if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;
emilmont 1:46ce1e16c870 837 wc = fs->win[bc % SS(fs)]; bc++;
emilmont 1:46ce1e16c870 838 if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;
emilmont 1:46ce1e16c870 839 wc |= fs->win[bc % SS(fs)] << 8;
emilmont 1:46ce1e16c870 840 return (clst & 1) ? (wc >> 4) : (wc & 0xFFF);
emilmont 1:46ce1e16c870 841
emilmont 1:46ce1e16c870 842 case FS_FAT16 :
emilmont 1:46ce1e16c870 843 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)))) break;
emilmont 1:46ce1e16c870 844 p = &fs->win[clst * 2 % SS(fs)];
emilmont 1:46ce1e16c870 845 return LD_WORD(p);
emilmont 1:46ce1e16c870 846
emilmont 1:46ce1e16c870 847 case FS_FAT32 :
emilmont 1:46ce1e16c870 848 if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)))) break;
emilmont 1:46ce1e16c870 849 p = &fs->win[clst * 4 % SS(fs)];
emilmont 1:46ce1e16c870 850 return LD_DWORD(p) & 0x0FFFFFFF;
emilmont 1:46ce1e16c870 851 }
emilmont 1:46ce1e16c870 852
emilmont 1:46ce1e16c870 853 return 0xFFFFFFFF; /* An error occurred at the disk I/O layer */
emilmont 1:46ce1e16c870 854 }
emilmont 1:46ce1e16c870 855
emilmont 1:46ce1e16c870 856
emilmont 1:46ce1e16c870 857
emilmont 1:46ce1e16c870 858
emilmont 1:46ce1e16c870 859 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 860 /* FAT access - Change value of a FAT entry */
emilmont 1:46ce1e16c870 861 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 862 #if !_FS_READONLY
emilmont 1:46ce1e16c870 863
emilmont 1:46ce1e16c870 864 FRESULT put_fat (
emilmont 1:46ce1e16c870 865 FATFS *fs, /* File system object */
emilmont 1:46ce1e16c870 866 DWORD clst, /* Cluster# to be changed in range of 2 to fs->n_fatent - 1 */
emilmont 1:46ce1e16c870 867 DWORD val /* New value to mark the cluster */
emilmont 1:46ce1e16c870 868 )
emilmont 1:46ce1e16c870 869 {
emilmont 1:46ce1e16c870 870 UINT bc;
emilmont 1:46ce1e16c870 871 BYTE *p;
emilmont 1:46ce1e16c870 872 FRESULT res;
emilmont 1:46ce1e16c870 873
emilmont 1:46ce1e16c870 874
emilmont 1:46ce1e16c870 875 if (clst < 2 || clst >= fs->n_fatent) { /* Check range */
emilmont 1:46ce1e16c870 876 res = FR_INT_ERR;
emilmont 1:46ce1e16c870 877
emilmont 1:46ce1e16c870 878 } else {
emilmont 1:46ce1e16c870 879 switch (fs->fs_type) {
emilmont 1:46ce1e16c870 880 case FS_FAT12 :
emilmont 1:46ce1e16c870 881 bc = (UINT)clst; bc += bc / 2;
emilmont 1:46ce1e16c870 882 res = move_window(fs, fs->fatbase + (bc / SS(fs)));
emilmont 1:46ce1e16c870 883 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 884 p = &fs->win[bc % SS(fs)];
emilmont 1:46ce1e16c870 885 *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
emilmont 1:46ce1e16c870 886 bc++;
emilmont 1:46ce1e16c870 887 fs->wflag = 1;
emilmont 1:46ce1e16c870 888 res = move_window(fs, fs->fatbase + (bc / SS(fs)));
emilmont 1:46ce1e16c870 889 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 890 p = &fs->win[bc % SS(fs)];
emilmont 1:46ce1e16c870 891 *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
emilmont 1:46ce1e16c870 892 break;
emilmont 1:46ce1e16c870 893
emilmont 1:46ce1e16c870 894 case FS_FAT16 :
emilmont 1:46ce1e16c870 895 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
emilmont 1:46ce1e16c870 896 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 897 p = &fs->win[clst * 2 % SS(fs)];
emilmont 1:46ce1e16c870 898 ST_WORD(p, (WORD)val);
emilmont 1:46ce1e16c870 899 break;
emilmont 1:46ce1e16c870 900
emilmont 1:46ce1e16c870 901 case FS_FAT32 :
emilmont 1:46ce1e16c870 902 res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
emilmont 1:46ce1e16c870 903 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 904 p = &fs->win[clst * 4 % SS(fs)];
emilmont 1:46ce1e16c870 905 val |= LD_DWORD(p) & 0xF0000000;
emilmont 1:46ce1e16c870 906 ST_DWORD(p, val);
emilmont 1:46ce1e16c870 907 break;
emilmont 1:46ce1e16c870 908
emilmont 1:46ce1e16c870 909 default :
emilmont 1:46ce1e16c870 910 res = FR_INT_ERR;
emilmont 1:46ce1e16c870 911 }
emilmont 1:46ce1e16c870 912 fs->wflag = 1;
emilmont 1:46ce1e16c870 913 }
emilmont 1:46ce1e16c870 914
emilmont 1:46ce1e16c870 915 return res;
emilmont 1:46ce1e16c870 916 }
emilmont 1:46ce1e16c870 917 #endif /* !_FS_READONLY */
emilmont 1:46ce1e16c870 918
emilmont 1:46ce1e16c870 919
emilmont 1:46ce1e16c870 920
emilmont 1:46ce1e16c870 921
emilmont 1:46ce1e16c870 922 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 923 /* FAT handling - Remove a cluster chain */
emilmont 1:46ce1e16c870 924 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 925 #if !_FS_READONLY
emilmont 1:46ce1e16c870 926 static
emilmont 1:46ce1e16c870 927 FRESULT remove_chain (
emilmont 1:46ce1e16c870 928 FATFS *fs, /* File system object */
emilmont 1:46ce1e16c870 929 DWORD clst /* Cluster# to remove a chain from */
emilmont 1:46ce1e16c870 930 )
emilmont 1:46ce1e16c870 931 {
emilmont 1:46ce1e16c870 932 FRESULT res;
emilmont 1:46ce1e16c870 933 DWORD nxt;
emilmont 1:46ce1e16c870 934 #if _USE_ERASE
emilmont 1:46ce1e16c870 935 DWORD scl = clst, ecl = clst, rt[2];
emilmont 1:46ce1e16c870 936 #endif
emilmont 1:46ce1e16c870 937
emilmont 1:46ce1e16c870 938 if (clst < 2 || clst >= fs->n_fatent) { /* Check range */
emilmont 1:46ce1e16c870 939 res = FR_INT_ERR;
emilmont 1:46ce1e16c870 940
emilmont 1:46ce1e16c870 941 } else {
emilmont 1:46ce1e16c870 942 res = FR_OK;
emilmont 1:46ce1e16c870 943 while (clst < fs->n_fatent) { /* Not a last link? */
emilmont 1:46ce1e16c870 944 nxt = get_fat(fs, clst); /* Get cluster status */
emilmont 1:46ce1e16c870 945 if (nxt == 0) break; /* Empty cluster? */
emilmont 1:46ce1e16c870 946 if (nxt == 1) { res = FR_INT_ERR; break; } /* Internal error? */
emilmont 1:46ce1e16c870 947 if (nxt == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } /* Disk error? */
emilmont 1:46ce1e16c870 948 res = put_fat(fs, clst, 0); /* Mark the cluster "empty" */
emilmont 1:46ce1e16c870 949 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 950 if (fs->free_clust != 0xFFFFFFFF) { /* Update FSInfo */
emilmont 1:46ce1e16c870 951 fs->free_clust++;
emilmont 1:46ce1e16c870 952 fs->fsi_flag = 1;
emilmont 1:46ce1e16c870 953 }
emilmont 1:46ce1e16c870 954 #if _USE_ERASE
emilmont 1:46ce1e16c870 955 if (ecl + 1 == nxt) { /* Is next cluster contiguous? */
emilmont 1:46ce1e16c870 956 ecl = nxt;
emilmont 1:46ce1e16c870 957 } else { /* End of contiguous clusters */
emilmont 1:46ce1e16c870 958 rt[0] = clust2sect(fs, scl); /* Start sector */
emilmont 1:46ce1e16c870 959 rt[1] = clust2sect(fs, ecl) + fs->csize - 1; /* End sector */
emilmont 1:46ce1e16c870 960 disk_ioctl(fs->drv, CTRL_ERASE_SECTOR, rt); /* Erase the block */
emilmont 1:46ce1e16c870 961 scl = ecl = nxt;
emilmont 1:46ce1e16c870 962 }
emilmont 1:46ce1e16c870 963 #endif
emilmont 1:46ce1e16c870 964 clst = nxt; /* Next cluster */
emilmont 1:46ce1e16c870 965 }
emilmont 1:46ce1e16c870 966 }
emilmont 1:46ce1e16c870 967
emilmont 1:46ce1e16c870 968 return res;
emilmont 1:46ce1e16c870 969 }
emilmont 1:46ce1e16c870 970 #endif
emilmont 1:46ce1e16c870 971
emilmont 1:46ce1e16c870 972
emilmont 1:46ce1e16c870 973
emilmont 1:46ce1e16c870 974
emilmont 1:46ce1e16c870 975 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 976 /* FAT handling - Stretch or Create a cluster chain */
emilmont 1:46ce1e16c870 977 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 978 #if !_FS_READONLY
emilmont 1:46ce1e16c870 979 static
emilmont 1:46ce1e16c870 980 DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */
emilmont 1:46ce1e16c870 981 FATFS *fs, /* File system object */
emilmont 1:46ce1e16c870 982 DWORD clst /* Cluster# to stretch. 0 means create a new chain. */
emilmont 1:46ce1e16c870 983 )
emilmont 1:46ce1e16c870 984 {
emilmont 1:46ce1e16c870 985 DWORD cs, ncl, scl;
emilmont 1:46ce1e16c870 986 FRESULT res;
emilmont 1:46ce1e16c870 987
emilmont 1:46ce1e16c870 988
emilmont 1:46ce1e16c870 989 if (clst == 0) { /* Create a new chain */
emilmont 1:46ce1e16c870 990 scl = fs->last_clust; /* Get suggested start point */
emilmont 1:46ce1e16c870 991 if (!scl || scl >= fs->n_fatent) scl = 1;
emilmont 1:46ce1e16c870 992 }
emilmont 1:46ce1e16c870 993 else { /* Stretch the current chain */
emilmont 1:46ce1e16c870 994 cs = get_fat(fs, clst); /* Check the cluster status */
emilmont 1:46ce1e16c870 995 if (cs < 2) return 1; /* It is an invalid cluster */
emilmont 1:46ce1e16c870 996 if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */
emilmont 1:46ce1e16c870 997 scl = clst;
emilmont 1:46ce1e16c870 998 }
emilmont 1:46ce1e16c870 999
emilmont 1:46ce1e16c870 1000 ncl = scl; /* Start cluster */
emilmont 1:46ce1e16c870 1001 for (;;) {
emilmont 1:46ce1e16c870 1002 ncl++; /* Next cluster */
emilmont 1:46ce1e16c870 1003 if (ncl >= fs->n_fatent) { /* Wrap around */
emilmont 1:46ce1e16c870 1004 ncl = 2;
emilmont 1:46ce1e16c870 1005 if (ncl > scl) return 0; /* No free cluster */
emilmont 1:46ce1e16c870 1006 }
emilmont 1:46ce1e16c870 1007 cs = get_fat(fs, ncl); /* Get the cluster status */
emilmont 1:46ce1e16c870 1008 if (cs == 0) break; /* Found a free cluster */
emilmont 1:46ce1e16c870 1009 if (cs == 0xFFFFFFFF || cs == 1)/* An error occurred */
emilmont 1:46ce1e16c870 1010 return cs;
emilmont 1:46ce1e16c870 1011 if (ncl == scl) return 0; /* No free cluster */
emilmont 1:46ce1e16c870 1012 }
emilmont 1:46ce1e16c870 1013
emilmont 1:46ce1e16c870 1014 res = put_fat(fs, ncl, 0x0FFFFFFF); /* Mark the new cluster "last link" */
emilmont 1:46ce1e16c870 1015 if (res == FR_OK && clst != 0) {
emilmont 1:46ce1e16c870 1016 res = put_fat(fs, clst, ncl); /* Link it to the previous one if needed */
emilmont 1:46ce1e16c870 1017 }
emilmont 1:46ce1e16c870 1018 if (res == FR_OK) {
emilmont 1:46ce1e16c870 1019 fs->last_clust = ncl; /* Update FSINFO */
emilmont 1:46ce1e16c870 1020 if (fs->free_clust != 0xFFFFFFFF) {
emilmont 1:46ce1e16c870 1021 fs->free_clust--;
emilmont 1:46ce1e16c870 1022 fs->fsi_flag = 1;
emilmont 1:46ce1e16c870 1023 }
emilmont 1:46ce1e16c870 1024 } else {
emilmont 1:46ce1e16c870 1025 ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1;
emilmont 1:46ce1e16c870 1026 }
emilmont 1:46ce1e16c870 1027
emilmont 1:46ce1e16c870 1028 return ncl; /* Return new cluster number or error code */
emilmont 1:46ce1e16c870 1029 }
emilmont 1:46ce1e16c870 1030 #endif /* !_FS_READONLY */
emilmont 1:46ce1e16c870 1031
emilmont 1:46ce1e16c870 1032
emilmont 1:46ce1e16c870 1033
emilmont 1:46ce1e16c870 1034 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1035 /* FAT handling - Convert offset into cluster with link map table */
emilmont 1:46ce1e16c870 1036 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1037
emilmont 1:46ce1e16c870 1038 #if _USE_FASTSEEK
emilmont 1:46ce1e16c870 1039 static
emilmont 1:46ce1e16c870 1040 DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */
emilmont 1:46ce1e16c870 1041 FIL* fp, /* Pointer to the file object */
emilmont 1:46ce1e16c870 1042 DWORD ofs /* File offset to be converted to cluster# */
emilmont 1:46ce1e16c870 1043 )
emilmont 1:46ce1e16c870 1044 {
emilmont 1:46ce1e16c870 1045 DWORD cl, ncl, *tbl;
emilmont 1:46ce1e16c870 1046
emilmont 1:46ce1e16c870 1047
emilmont 1:46ce1e16c870 1048 tbl = fp->cltbl + 1; /* Top of CLMT */
emilmont 1:46ce1e16c870 1049 cl = ofs / SS(fp->fs) / fp->fs->csize; /* Cluster order from top of the file */
emilmont 1:46ce1e16c870 1050 for (;;) {
emilmont 1:46ce1e16c870 1051 ncl = *tbl++; /* Number of cluters in the fragment */
emilmont 1:46ce1e16c870 1052 if (!ncl) return 0; /* End of table? (error) */
emilmont 1:46ce1e16c870 1053 if (cl < ncl) break; /* In this fragment? */
emilmont 1:46ce1e16c870 1054 cl -= ncl; tbl++; /* Next fragment */
emilmont 1:46ce1e16c870 1055 }
emilmont 1:46ce1e16c870 1056 return cl + *tbl; /* Return the cluster number */
emilmont 1:46ce1e16c870 1057 }
emilmont 1:46ce1e16c870 1058 #endif /* _USE_FASTSEEK */
emilmont 1:46ce1e16c870 1059
emilmont 1:46ce1e16c870 1060
emilmont 1:46ce1e16c870 1061
emilmont 1:46ce1e16c870 1062 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1063 /* Directory handling - Set directory index */
emilmont 1:46ce1e16c870 1064 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1065
emilmont 1:46ce1e16c870 1066 static
emilmont 1:46ce1e16c870 1067 FRESULT dir_sdi (
emilmont 1:46ce1e16c870 1068 FATFS_DIR *dj, /* Pointer to directory object */
emilmont 1:46ce1e16c870 1069 WORD idx /* Index of directory table */
emilmont 1:46ce1e16c870 1070 )
emilmont 1:46ce1e16c870 1071 {
emilmont 1:46ce1e16c870 1072 DWORD clst;
emilmont 1:46ce1e16c870 1073 WORD ic;
emilmont 1:46ce1e16c870 1074
emilmont 1:46ce1e16c870 1075
emilmont 1:46ce1e16c870 1076 dj->index = idx;
emilmont 1:46ce1e16c870 1077 clst = dj->sclust;
emilmont 1:46ce1e16c870 1078 if (clst == 1 || clst >= dj->fs->n_fatent) /* Check start cluster range */
emilmont 1:46ce1e16c870 1079 return FR_INT_ERR;
emilmont 1:46ce1e16c870 1080 if (!clst && dj->fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */
emilmont 1:46ce1e16c870 1081 clst = dj->fs->dirbase;
emilmont 1:46ce1e16c870 1082
emilmont 1:46ce1e16c870 1083 if (clst == 0) { /* Static table (root-dir in FAT12/16) */
emilmont 1:46ce1e16c870 1084 dj->clust = clst;
emilmont 1:46ce1e16c870 1085 if (idx >= dj->fs->n_rootdir) /* Index is out of range */
emilmont 1:46ce1e16c870 1086 return FR_INT_ERR;
emilmont 1:46ce1e16c870 1087 dj->sect = dj->fs->dirbase + idx / (SS(dj->fs) / SZ_DIR); /* Sector# */
emilmont 1:46ce1e16c870 1088 }
emilmont 1:46ce1e16c870 1089 else { /* Dynamic table (sub-dirs or root-dir in FAT32) */
emilmont 1:46ce1e16c870 1090 ic = SS(dj->fs) / SZ_DIR * dj->fs->csize; /* Entries per cluster */
emilmont 1:46ce1e16c870 1091 while (idx >= ic) { /* Follow cluster chain */
emilmont 1:46ce1e16c870 1092 clst = get_fat(dj->fs, clst); /* Get next cluster */
emilmont 1:46ce1e16c870 1093 if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
emilmont 1:46ce1e16c870 1094 if (clst < 2 || clst >= dj->fs->n_fatent) /* Reached to end of table or int error */
emilmont 1:46ce1e16c870 1095 return FR_INT_ERR;
emilmont 1:46ce1e16c870 1096 idx -= ic;
emilmont 1:46ce1e16c870 1097 }
emilmont 1:46ce1e16c870 1098 dj->clust = clst;
emilmont 1:46ce1e16c870 1099 dj->sect = clust2sect(dj->fs, clst) + idx / (SS(dj->fs) / SZ_DIR); /* Sector# */
emilmont 1:46ce1e16c870 1100 }
emilmont 1:46ce1e16c870 1101
emilmont 1:46ce1e16c870 1102 dj->dir = dj->fs->win + (idx % (SS(dj->fs) / SZ_DIR)) * SZ_DIR; /* Ptr to the entry in the sector */
emilmont 1:46ce1e16c870 1103
emilmont 1:46ce1e16c870 1104 return FR_OK; /* Seek succeeded */
emilmont 1:46ce1e16c870 1105 }
emilmont 1:46ce1e16c870 1106
emilmont 1:46ce1e16c870 1107
emilmont 1:46ce1e16c870 1108
emilmont 1:46ce1e16c870 1109
emilmont 1:46ce1e16c870 1110 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1111 /* Directory handling - Move directory table index next */
emilmont 1:46ce1e16c870 1112 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1113
emilmont 1:46ce1e16c870 1114 static
emilmont 1:46ce1e16c870 1115 FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:EOT and could not stretch */
emilmont 1:46ce1e16c870 1116 FATFS_DIR *dj, /* Pointer to directory object */
emilmont 1:46ce1e16c870 1117 int stretch /* 0: Do not stretch table, 1: Stretch table if needed */
emilmont 1:46ce1e16c870 1118 )
emilmont 1:46ce1e16c870 1119 {
emilmont 1:46ce1e16c870 1120 DWORD clst;
emilmont 1:46ce1e16c870 1121 WORD i;
emilmont 1:46ce1e16c870 1122
emilmont 1:46ce1e16c870 1123
emilmont 1:46ce1e16c870 1124 stretch = stretch; /* To suppress warning on read-only cfg. */
emilmont 1:46ce1e16c870 1125 i = dj->index + 1;
emilmont 1:46ce1e16c870 1126 if (!i || !dj->sect) /* Report EOT when index has reached 65535 */
emilmont 1:46ce1e16c870 1127 return FR_NO_FILE;
emilmont 1:46ce1e16c870 1128
emilmont 1:46ce1e16c870 1129 if (!(i % (SS(dj->fs) / SZ_DIR))) { /* Sector changed? */
emilmont 1:46ce1e16c870 1130 dj->sect++; /* Next sector */
emilmont 1:46ce1e16c870 1131
emilmont 1:46ce1e16c870 1132 if (dj->clust == 0) { /* Static table */
emilmont 1:46ce1e16c870 1133 if (i >= dj->fs->n_rootdir) /* Report EOT when end of table */
emilmont 1:46ce1e16c870 1134 return FR_NO_FILE;
emilmont 1:46ce1e16c870 1135 }
emilmont 1:46ce1e16c870 1136 else { /* Dynamic table */
emilmont 1:46ce1e16c870 1137 if (((i / (SS(dj->fs) / SZ_DIR)) & (dj->fs->csize - 1)) == 0) { /* Cluster changed? */
emilmont 1:46ce1e16c870 1138 clst = get_fat(dj->fs, dj->clust); /* Get next cluster */
emilmont 1:46ce1e16c870 1139 if (clst <= 1) return FR_INT_ERR;
emilmont 1:46ce1e16c870 1140 if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
emilmont 1:46ce1e16c870 1141 if (clst >= dj->fs->n_fatent) { /* When it reached end of dynamic table */
emilmont 1:46ce1e16c870 1142 #if !_FS_READONLY
emilmont 1:46ce1e16c870 1143 BYTE c;
emilmont 1:46ce1e16c870 1144 if (!stretch) return FR_NO_FILE; /* When do not stretch, report EOT */
emilmont 1:46ce1e16c870 1145 clst = create_chain(dj->fs, dj->clust); /* Stretch cluster chain */
emilmont 1:46ce1e16c870 1146 if (clst == 0) return FR_DENIED; /* No free cluster */
emilmont 1:46ce1e16c870 1147 if (clst == 1) return FR_INT_ERR;
emilmont 1:46ce1e16c870 1148 if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
emilmont 1:46ce1e16c870 1149 /* Clean-up stretched table */
emilmont 1:46ce1e16c870 1150 if (move_window(dj->fs, 0)) return FR_DISK_ERR; /* Flush active window */
emilmont 1:46ce1e16c870 1151 mem_set(dj->fs->win, 0, SS(dj->fs)); /* Clear window buffer */
emilmont 1:46ce1e16c870 1152 dj->fs->winsect = clust2sect(dj->fs, clst); /* Cluster start sector */
emilmont 1:46ce1e16c870 1153 for (c = 0; c < dj->fs->csize; c++) { /* Fill the new cluster with 0 */
emilmont 1:46ce1e16c870 1154 dj->fs->wflag = 1;
emilmont 1:46ce1e16c870 1155 if (move_window(dj->fs, 0)) return FR_DISK_ERR;
emilmont 1:46ce1e16c870 1156 dj->fs->winsect++;
emilmont 1:46ce1e16c870 1157 }
emilmont 1:46ce1e16c870 1158 dj->fs->winsect -= c; /* Rewind window address */
emilmont 1:46ce1e16c870 1159 #else
emilmont 1:46ce1e16c870 1160 return FR_NO_FILE; /* Report EOT */
emilmont 1:46ce1e16c870 1161 #endif
emilmont 1:46ce1e16c870 1162 }
emilmont 1:46ce1e16c870 1163 dj->clust = clst; /* Initialize data for new cluster */
emilmont 1:46ce1e16c870 1164 dj->sect = clust2sect(dj->fs, clst);
emilmont 1:46ce1e16c870 1165 }
emilmont 1:46ce1e16c870 1166 }
emilmont 1:46ce1e16c870 1167 }
emilmont 1:46ce1e16c870 1168
emilmont 1:46ce1e16c870 1169 dj->index = i;
emilmont 1:46ce1e16c870 1170 dj->dir = dj->fs->win + (i % (SS(dj->fs) / SZ_DIR)) * SZ_DIR;
emilmont 1:46ce1e16c870 1171
emilmont 1:46ce1e16c870 1172 return FR_OK;
emilmont 1:46ce1e16c870 1173 }
emilmont 1:46ce1e16c870 1174
emilmont 1:46ce1e16c870 1175
emilmont 1:46ce1e16c870 1176
emilmont 1:46ce1e16c870 1177
emilmont 1:46ce1e16c870 1178 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1179 /* Directory handling - Load/Store start cluster number */
emilmont 1:46ce1e16c870 1180 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1181
emilmont 1:46ce1e16c870 1182 static
emilmont 1:46ce1e16c870 1183 DWORD ld_clust (
emilmont 1:46ce1e16c870 1184 FATFS *fs, /* Pointer to the fs object */
emilmont 1:46ce1e16c870 1185 BYTE *dir /* Pointer to the directory entry */
emilmont 1:46ce1e16c870 1186 )
emilmont 1:46ce1e16c870 1187 {
emilmont 1:46ce1e16c870 1188 DWORD cl;
emilmont 1:46ce1e16c870 1189
emilmont 1:46ce1e16c870 1190 cl = LD_WORD(dir+DIR_FstClusLO);
emilmont 1:46ce1e16c870 1191 if (fs->fs_type == FS_FAT32)
emilmont 1:46ce1e16c870 1192 cl |= (DWORD)LD_WORD(dir+DIR_FstClusHI) << 16;
emilmont 1:46ce1e16c870 1193
emilmont 1:46ce1e16c870 1194 return cl;
emilmont 1:46ce1e16c870 1195 }
emilmont 1:46ce1e16c870 1196
emilmont 1:46ce1e16c870 1197
emilmont 1:46ce1e16c870 1198 #if !_FS_READONLY
emilmont 1:46ce1e16c870 1199 static
emilmont 1:46ce1e16c870 1200 void st_clust (
emilmont 1:46ce1e16c870 1201 BYTE *dir, /* Pointer to the directory entry */
emilmont 1:46ce1e16c870 1202 DWORD cl /* Value to be set */
emilmont 1:46ce1e16c870 1203 )
emilmont 1:46ce1e16c870 1204 {
emilmont 1:46ce1e16c870 1205 ST_WORD(dir+DIR_FstClusLO, cl);
emilmont 1:46ce1e16c870 1206 ST_WORD(dir+DIR_FstClusHI, cl >> 16);
emilmont 1:46ce1e16c870 1207 }
emilmont 1:46ce1e16c870 1208 #endif
emilmont 1:46ce1e16c870 1209
emilmont 1:46ce1e16c870 1210
emilmont 1:46ce1e16c870 1211
emilmont 1:46ce1e16c870 1212 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1213 /* LFN handling - Test/Pick/Fit an LFN segment from/to directory entry */
emilmont 1:46ce1e16c870 1214 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1215 #if _USE_LFN
emilmont 1:46ce1e16c870 1216 static
emilmont 1:46ce1e16c870 1217 const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN chars in the directory entry */
emilmont 1:46ce1e16c870 1218
emilmont 1:46ce1e16c870 1219
emilmont 1:46ce1e16c870 1220 static
emilmont 1:46ce1e16c870 1221 int cmp_lfn ( /* 1:Matched, 0:Not matched */
emilmont 1:46ce1e16c870 1222 WCHAR *lfnbuf, /* Pointer to the LFN to be compared */
emilmont 1:46ce1e16c870 1223 BYTE *dir /* Pointer to the directory entry containing a part of LFN */
emilmont 1:46ce1e16c870 1224 )
emilmont 1:46ce1e16c870 1225 {
emilmont 1:46ce1e16c870 1226 UINT i, s;
emilmont 1:46ce1e16c870 1227 WCHAR wc, uc;
emilmont 1:46ce1e16c870 1228
emilmont 1:46ce1e16c870 1229
emilmont 1:46ce1e16c870 1230 i = ((dir[LDIR_Ord] & ~LLE) - 1) * 13; /* Get offset in the LFN buffer */
emilmont 1:46ce1e16c870 1231 s = 0; wc = 1;
emilmont 1:46ce1e16c870 1232 do {
emilmont 1:46ce1e16c870 1233 uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
emilmont 1:46ce1e16c870 1234 if (wc) { /* Last char has not been processed */
emilmont 1:46ce1e16c870 1235 wc = ff_wtoupper(uc); /* Convert it to upper case */
emilmont 1:46ce1e16c870 1236 if (i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++])) /* Compare it */
emilmont 1:46ce1e16c870 1237 return 0; /* Not matched */
emilmont 1:46ce1e16c870 1238 } else {
emilmont 1:46ce1e16c870 1239 if (uc != 0xFFFF) return 0; /* Check filler */
emilmont 1:46ce1e16c870 1240 }
emilmont 1:46ce1e16c870 1241 } while (++s < 13); /* Repeat until all chars in the entry are checked */
emilmont 1:46ce1e16c870 1242
emilmont 1:46ce1e16c870 1243 if ((dir[LDIR_Ord] & LLE) && wc && lfnbuf[i]) /* Last segment matched but different length */
emilmont 1:46ce1e16c870 1244 return 0;
emilmont 1:46ce1e16c870 1245
emilmont 1:46ce1e16c870 1246 return 1; /* The part of LFN matched */
emilmont 1:46ce1e16c870 1247 }
emilmont 1:46ce1e16c870 1248
emilmont 1:46ce1e16c870 1249
emilmont 1:46ce1e16c870 1250
emilmont 1:46ce1e16c870 1251 static
emilmont 1:46ce1e16c870 1252 int pick_lfn ( /* 1:Succeeded, 0:Buffer overflow */
emilmont 1:46ce1e16c870 1253 WCHAR *lfnbuf, /* Pointer to the Unicode-LFN buffer */
emilmont 1:46ce1e16c870 1254 BYTE *dir /* Pointer to the directory entry */
emilmont 1:46ce1e16c870 1255 )
emilmont 1:46ce1e16c870 1256 {
emilmont 1:46ce1e16c870 1257 UINT i, s;
emilmont 1:46ce1e16c870 1258 WCHAR wc, uc;
emilmont 1:46ce1e16c870 1259
emilmont 1:46ce1e16c870 1260
emilmont 1:46ce1e16c870 1261 i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
emilmont 1:46ce1e16c870 1262
emilmont 1:46ce1e16c870 1263 s = 0; wc = 1;
emilmont 1:46ce1e16c870 1264 do {
emilmont 1:46ce1e16c870 1265 uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
emilmont 1:46ce1e16c870 1266 if (wc) { /* Last char has not been processed */
emilmont 1:46ce1e16c870 1267 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
emilmont 1:46ce1e16c870 1268 lfnbuf[i++] = wc = uc; /* Store it */
emilmont 1:46ce1e16c870 1269 } else {
emilmont 1:46ce1e16c870 1270 if (uc != 0xFFFF) return 0; /* Check filler */
emilmont 1:46ce1e16c870 1271 }
emilmont 1:46ce1e16c870 1272 } while (++s < 13); /* Read all character in the entry */
emilmont 1:46ce1e16c870 1273
emilmont 1:46ce1e16c870 1274 if (dir[LDIR_Ord] & LLE) { /* Put terminator if it is the last LFN part */
emilmont 1:46ce1e16c870 1275 if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
emilmont 1:46ce1e16c870 1276 lfnbuf[i] = 0;
emilmont 1:46ce1e16c870 1277 }
emilmont 1:46ce1e16c870 1278
emilmont 1:46ce1e16c870 1279 return 1;
emilmont 1:46ce1e16c870 1280 }
emilmont 1:46ce1e16c870 1281
emilmont 1:46ce1e16c870 1282
emilmont 1:46ce1e16c870 1283 #if !_FS_READONLY
emilmont 1:46ce1e16c870 1284 static
emilmont 1:46ce1e16c870 1285 void fit_lfn (
emilmont 1:46ce1e16c870 1286 const WCHAR *lfnbuf, /* Pointer to the LFN buffer */
emilmont 1:46ce1e16c870 1287 BYTE *dir, /* Pointer to the directory entry */
emilmont 1:46ce1e16c870 1288 BYTE ord, /* LFN order (1-20) */
emilmont 1:46ce1e16c870 1289 BYTE sum /* SFN sum */
emilmont 1:46ce1e16c870 1290 )
emilmont 1:46ce1e16c870 1291 {
emilmont 1:46ce1e16c870 1292 UINT i, s;
emilmont 1:46ce1e16c870 1293 WCHAR wc;
emilmont 1:46ce1e16c870 1294
emilmont 1:46ce1e16c870 1295
emilmont 1:46ce1e16c870 1296 dir[LDIR_Chksum] = sum; /* Set check sum */
emilmont 1:46ce1e16c870 1297 dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */
emilmont 1:46ce1e16c870 1298 dir[LDIR_Type] = 0;
emilmont 1:46ce1e16c870 1299 ST_WORD(dir+LDIR_FstClusLO, 0);
emilmont 1:46ce1e16c870 1300
emilmont 1:46ce1e16c870 1301 i = (ord - 1) * 13; /* Get offset in the LFN buffer */
emilmont 1:46ce1e16c870 1302 s = wc = 0;
emilmont 1:46ce1e16c870 1303 do {
emilmont 1:46ce1e16c870 1304 if (wc != 0xFFFF) wc = lfnbuf[i++]; /* Get an effective char */
emilmont 1:46ce1e16c870 1305 ST_WORD(dir+LfnOfs[s], wc); /* Put it */
emilmont 1:46ce1e16c870 1306 if (!wc) wc = 0xFFFF; /* Padding chars following last char */
emilmont 1:46ce1e16c870 1307 } while (++s < 13);
emilmont 1:46ce1e16c870 1308 if (wc == 0xFFFF || !lfnbuf[i]) ord |= LLE; /* Bottom LFN part is the start of LFN sequence */
emilmont 1:46ce1e16c870 1309 dir[LDIR_Ord] = ord; /* Set the LFN order */
emilmont 1:46ce1e16c870 1310 }
emilmont 1:46ce1e16c870 1311
emilmont 1:46ce1e16c870 1312 #endif
emilmont 1:46ce1e16c870 1313 #endif
emilmont 1:46ce1e16c870 1314
emilmont 1:46ce1e16c870 1315
emilmont 1:46ce1e16c870 1316
emilmont 1:46ce1e16c870 1317 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1318 /* Create numbered name */
emilmont 1:46ce1e16c870 1319 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1320 #if _USE_LFN
emilmont 1:46ce1e16c870 1321 void gen_numname (
emilmont 1:46ce1e16c870 1322 BYTE *dst, /* Pointer to generated SFN */
emilmont 1:46ce1e16c870 1323 const BYTE *src, /* Pointer to source SFN to be modified */
emilmont 1:46ce1e16c870 1324 const WCHAR *lfn, /* Pointer to LFN */
emilmont 1:46ce1e16c870 1325 WORD seq /* Sequence number */
emilmont 1:46ce1e16c870 1326 )
emilmont 1:46ce1e16c870 1327 {
emilmont 1:46ce1e16c870 1328 BYTE ns[8], c;
emilmont 1:46ce1e16c870 1329 UINT i, j;
emilmont 1:46ce1e16c870 1330
emilmont 1:46ce1e16c870 1331
emilmont 1:46ce1e16c870 1332 mem_cpy(dst, src, 11);
emilmont 1:46ce1e16c870 1333
emilmont 1:46ce1e16c870 1334 if (seq > 5) { /* On many collisions, generate a hash number instead of sequential number */
emilmont 1:46ce1e16c870 1335 do seq = (seq >> 1) + (seq << 15) + (WORD)*lfn++; while (*lfn);
emilmont 1:46ce1e16c870 1336 }
emilmont 1:46ce1e16c870 1337
emilmont 1:46ce1e16c870 1338 /* itoa (hexdecimal) */
emilmont 1:46ce1e16c870 1339 i = 7;
emilmont 1:46ce1e16c870 1340 do {
emilmont 1:46ce1e16c870 1341 c = (seq % 16) + '0';
emilmont 1:46ce1e16c870 1342 if (c > '9') c += 7;
emilmont 1:46ce1e16c870 1343 ns[i--] = c;
emilmont 1:46ce1e16c870 1344 seq /= 16;
emilmont 1:46ce1e16c870 1345 } while (seq);
emilmont 1:46ce1e16c870 1346 ns[i] = '~';
emilmont 1:46ce1e16c870 1347
emilmont 1:46ce1e16c870 1348 /* Append the number */
emilmont 1:46ce1e16c870 1349 for (j = 0; j < i && dst[j] != ' '; j++) {
emilmont 1:46ce1e16c870 1350 if (IsDBCS1(dst[j])) {
emilmont 1:46ce1e16c870 1351 if (j == i - 1) break;
emilmont 1:46ce1e16c870 1352 j++;
emilmont 1:46ce1e16c870 1353 }
emilmont 1:46ce1e16c870 1354 }
emilmont 1:46ce1e16c870 1355 do {
emilmont 1:46ce1e16c870 1356 dst[j++] = (i < 8) ? ns[i++] : ' ';
emilmont 1:46ce1e16c870 1357 } while (j < 8);
emilmont 1:46ce1e16c870 1358 }
emilmont 1:46ce1e16c870 1359 #endif
emilmont 1:46ce1e16c870 1360
emilmont 1:46ce1e16c870 1361
emilmont 1:46ce1e16c870 1362
emilmont 1:46ce1e16c870 1363
emilmont 1:46ce1e16c870 1364 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1365 /* Calculate sum of an SFN */
emilmont 1:46ce1e16c870 1366 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1367 #if _USE_LFN
emilmont 1:46ce1e16c870 1368 static
emilmont 1:46ce1e16c870 1369 BYTE sum_sfn (
emilmont 1:46ce1e16c870 1370 const BYTE *dir /* Ptr to directory entry */
emilmont 1:46ce1e16c870 1371 )
emilmont 1:46ce1e16c870 1372 {
emilmont 1:46ce1e16c870 1373 BYTE sum = 0;
emilmont 1:46ce1e16c870 1374 UINT n = 11;
emilmont 1:46ce1e16c870 1375
emilmont 1:46ce1e16c870 1376 do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);
emilmont 1:46ce1e16c870 1377 return sum;
emilmont 1:46ce1e16c870 1378 }
emilmont 1:46ce1e16c870 1379 #endif
emilmont 1:46ce1e16c870 1380
emilmont 1:46ce1e16c870 1381
emilmont 1:46ce1e16c870 1382
emilmont 1:46ce1e16c870 1383
emilmont 1:46ce1e16c870 1384 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1385 /* Directory handling - Find an object in the directory */
emilmont 1:46ce1e16c870 1386 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1387
emilmont 1:46ce1e16c870 1388 static
emilmont 1:46ce1e16c870 1389 FRESULT dir_find (
emilmont 1:46ce1e16c870 1390 FATFS_DIR *dj /* Pointer to the directory object linked to the file name */
emilmont 1:46ce1e16c870 1391 )
emilmont 1:46ce1e16c870 1392 {
emilmont 1:46ce1e16c870 1393 FRESULT res;
emilmont 1:46ce1e16c870 1394 BYTE c, *dir;
emilmont 1:46ce1e16c870 1395 #if _USE_LFN
emilmont 1:46ce1e16c870 1396 BYTE a, ord, sum;
emilmont 1:46ce1e16c870 1397 #endif
emilmont 1:46ce1e16c870 1398
emilmont 1:46ce1e16c870 1399 res = dir_sdi(dj, 0); /* Rewind directory object */
emilmont 1:46ce1e16c870 1400 if (res != FR_OK) return res;
emilmont 1:46ce1e16c870 1401
emilmont 1:46ce1e16c870 1402 #if _USE_LFN
emilmont 1:46ce1e16c870 1403 ord = sum = 0xFF;
emilmont 1:46ce1e16c870 1404 #endif
emilmont 1:46ce1e16c870 1405 do {
emilmont 1:46ce1e16c870 1406 res = move_window(dj->fs, dj->sect);
emilmont 1:46ce1e16c870 1407 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 1408 dir = dj->dir; /* Ptr to the directory entry of current index */
emilmont 1:46ce1e16c870 1409 c = dir[DIR_Name];
emilmont 1:46ce1e16c870 1410 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
emilmont 1:46ce1e16c870 1411 #if _USE_LFN /* LFN configuration */
emilmont 1:46ce1e16c870 1412 a = dir[DIR_Attr] & AM_MASK;
emilmont 1:46ce1e16c870 1413 if (c == DDE || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
emilmont 1:46ce1e16c870 1414 ord = 0xFF;
emilmont 1:46ce1e16c870 1415 } else {
emilmont 1:46ce1e16c870 1416 if (a == AM_LFN) { /* An LFN entry is found */
emilmont 1:46ce1e16c870 1417 if (dj->lfn) {
emilmont 1:46ce1e16c870 1418 if (c & LLE) { /* Is it start of LFN sequence? */
emilmont 1:46ce1e16c870 1419 sum = dir[LDIR_Chksum];
emilmont 1:46ce1e16c870 1420 c &= ~LLE; ord = c; /* LFN start order */
emilmont 1:46ce1e16c870 1421 dj->lfn_idx = dj->index;
emilmont 1:46ce1e16c870 1422 }
emilmont 1:46ce1e16c870 1423 /* Check validity of the LFN entry and compare it with given name */
emilmont 1:46ce1e16c870 1424 ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
emilmont 1:46ce1e16c870 1425 }
emilmont 1:46ce1e16c870 1426 } else { /* An SFN entry is found */
emilmont 1:46ce1e16c870 1427 if (!ord && sum == sum_sfn(dir)) break; /* LFN matched? */
emilmont 1:46ce1e16c870 1428 ord = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */
emilmont 1:46ce1e16c870 1429 if (!(dj->fn[NS] & NS_LOSS) && !mem_cmp(dir, dj->fn, 11)) break; /* SFN matched? */
emilmont 1:46ce1e16c870 1430 }
emilmont 1:46ce1e16c870 1431 }
emilmont 1:46ce1e16c870 1432 #else /* Non LFN configuration */
emilmont 1:46ce1e16c870 1433 if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */
emilmont 1:46ce1e16c870 1434 break;
emilmont 1:46ce1e16c870 1435 #endif
emilmont 1:46ce1e16c870 1436 res = dir_next(dj, 0); /* Next entry */
emilmont 1:46ce1e16c870 1437 } while (res == FR_OK);
emilmont 1:46ce1e16c870 1438
emilmont 1:46ce1e16c870 1439 return res;
emilmont 1:46ce1e16c870 1440 }
emilmont 1:46ce1e16c870 1441
emilmont 1:46ce1e16c870 1442
emilmont 1:46ce1e16c870 1443
emilmont 1:46ce1e16c870 1444
emilmont 1:46ce1e16c870 1445 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1446 /* Read an object from the directory */
emilmont 1:46ce1e16c870 1447 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1448 #if _FS_MINIMIZE <= 1
emilmont 1:46ce1e16c870 1449 static
emilmont 1:46ce1e16c870 1450 FRESULT dir_read (
emilmont 1:46ce1e16c870 1451 FATFS_DIR *dj /* Pointer to the directory object that pointing the entry to be read */
emilmont 1:46ce1e16c870 1452 )
emilmont 1:46ce1e16c870 1453 {
emilmont 1:46ce1e16c870 1454 FRESULT res;
emilmont 1:46ce1e16c870 1455 BYTE c, *dir;
emilmont 1:46ce1e16c870 1456 #if _USE_LFN
emilmont 1:46ce1e16c870 1457 BYTE a, ord = 0xFF, sum = 0xFF;
emilmont 1:46ce1e16c870 1458 #endif
emilmont 1:46ce1e16c870 1459
emilmont 1:46ce1e16c870 1460 res = FR_NO_FILE;
emilmont 1:46ce1e16c870 1461 while (dj->sect) {
emilmont 1:46ce1e16c870 1462 res = move_window(dj->fs, dj->sect);
emilmont 1:46ce1e16c870 1463 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 1464 dir = dj->dir; /* Ptr to the directory entry of current index */
emilmont 1:46ce1e16c870 1465 c = dir[DIR_Name];
emilmont 1:46ce1e16c870 1466 if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
emilmont 1:46ce1e16c870 1467 #if _USE_LFN /* LFN configuration */
emilmont 1:46ce1e16c870 1468 a = dir[DIR_Attr] & AM_MASK;
emilmont 1:46ce1e16c870 1469 if (c == DDE || (!_FS_RPATH && c == '.') || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
emilmont 1:46ce1e16c870 1470 ord = 0xFF;
emilmont 1:46ce1e16c870 1471 } else {
emilmont 1:46ce1e16c870 1472 if (a == AM_LFN) { /* An LFN entry is found */
emilmont 1:46ce1e16c870 1473 if (c & LLE) { /* Is it start of LFN sequence? */
emilmont 1:46ce1e16c870 1474 sum = dir[LDIR_Chksum];
emilmont 1:46ce1e16c870 1475 c &= ~LLE; ord = c;
emilmont 1:46ce1e16c870 1476 dj->lfn_idx = dj->index;
emilmont 1:46ce1e16c870 1477 }
emilmont 1:46ce1e16c870 1478 /* Check LFN validity and capture it */
emilmont 1:46ce1e16c870 1479 ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
emilmont 1:46ce1e16c870 1480 } else { /* An SFN entry is found */
emilmont 1:46ce1e16c870 1481 if (ord || sum != sum_sfn(dir)) /* Is there a valid LFN? */
emilmont 1:46ce1e16c870 1482 dj->lfn_idx = 0xFFFF; /* It has no LFN. */
emilmont 1:46ce1e16c870 1483 break;
emilmont 1:46ce1e16c870 1484 }
emilmont 1:46ce1e16c870 1485 }
emilmont 1:46ce1e16c870 1486 #else /* Non LFN configuration */
emilmont 1:46ce1e16c870 1487 if (c != DDE && (_FS_RPATH || c != '.') && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */
emilmont 1:46ce1e16c870 1488 break;
emilmont 1:46ce1e16c870 1489 #endif
emilmont 1:46ce1e16c870 1490 res = dir_next(dj, 0); /* Next entry */
emilmont 1:46ce1e16c870 1491 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 1492 }
emilmont 1:46ce1e16c870 1493
emilmont 1:46ce1e16c870 1494 if (res != FR_OK) dj->sect = 0;
emilmont 1:46ce1e16c870 1495
emilmont 1:46ce1e16c870 1496 return res;
emilmont 1:46ce1e16c870 1497 }
emilmont 1:46ce1e16c870 1498 #endif
emilmont 1:46ce1e16c870 1499
emilmont 1:46ce1e16c870 1500
emilmont 1:46ce1e16c870 1501
emilmont 1:46ce1e16c870 1502 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1503 /* Register an object to the directory */
emilmont 1:46ce1e16c870 1504 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1505 #if !_FS_READONLY
emilmont 1:46ce1e16c870 1506 static
emilmont 1:46ce1e16c870 1507 FRESULT dir_register ( /* FR_OK:Successful, FR_DENIED:No free entry or too many SFN collision, FR_DISK_ERR:Disk error */
emilmont 1:46ce1e16c870 1508 FATFS_DIR *dj /* Target directory with object name to be created */
emilmont 1:46ce1e16c870 1509 )
emilmont 1:46ce1e16c870 1510 {
emilmont 1:46ce1e16c870 1511 FRESULT res;
emilmont 1:46ce1e16c870 1512 BYTE c, *dir;
emilmont 1:46ce1e16c870 1513 #if _USE_LFN /* LFN configuration */
emilmont 1:46ce1e16c870 1514 WORD n, ne, is;
emilmont 1:46ce1e16c870 1515 BYTE sn[12], *fn, sum;
emilmont 1:46ce1e16c870 1516 WCHAR *lfn;
emilmont 1:46ce1e16c870 1517
emilmont 1:46ce1e16c870 1518
emilmont 1:46ce1e16c870 1519 fn = dj->fn; lfn = dj->lfn;
emilmont 1:46ce1e16c870 1520 mem_cpy(sn, fn, 12);
emilmont 1:46ce1e16c870 1521
emilmont 1:46ce1e16c870 1522 if (_FS_RPATH && (sn[NS] & NS_DOT)) /* Cannot create dot entry */
emilmont 1:46ce1e16c870 1523 return FR_INVALID_NAME;
emilmont 1:46ce1e16c870 1524
emilmont 1:46ce1e16c870 1525 if (sn[NS] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */
emilmont 1:46ce1e16c870 1526 fn[NS] = 0; dj->lfn = 0; /* Find only SFN */
emilmont 1:46ce1e16c870 1527 for (n = 1; n < 100; n++) {
emilmont 1:46ce1e16c870 1528 gen_numname(fn, sn, lfn, n); /* Generate a numbered name */
emilmont 1:46ce1e16c870 1529 res = dir_find(dj); /* Check if the name collides with existing SFN */
emilmont 1:46ce1e16c870 1530 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 1531 }
emilmont 1:46ce1e16c870 1532 if (n == 100) return FR_DENIED; /* Abort if too many collisions */
emilmont 1:46ce1e16c870 1533 if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */
emilmont 1:46ce1e16c870 1534 fn[NS] = sn[NS]; dj->lfn = lfn;
emilmont 1:46ce1e16c870 1535 }
emilmont 1:46ce1e16c870 1536
emilmont 1:46ce1e16c870 1537 if (sn[NS] & NS_LFN) { /* When LFN is to be created, reserve an SFN + LFN entries. */
emilmont 1:46ce1e16c870 1538 for (ne = 0; lfn[ne]; ne++) ;
emilmont 1:46ce1e16c870 1539 ne = (ne + 25) / 13;
emilmont 1:46ce1e16c870 1540 } else { /* Otherwise reserve only an SFN entry. */
emilmont 1:46ce1e16c870 1541 ne = 1;
emilmont 1:46ce1e16c870 1542 }
emilmont 1:46ce1e16c870 1543
emilmont 1:46ce1e16c870 1544 /* Reserve contiguous entries */
emilmont 1:46ce1e16c870 1545 res = dir_sdi(dj, 0);
emilmont 1:46ce1e16c870 1546 if (res != FR_OK) return res;
emilmont 1:46ce1e16c870 1547 n = is = 0;
emilmont 1:46ce1e16c870 1548 do {
emilmont 1:46ce1e16c870 1549 res = move_window(dj->fs, dj->sect);
emilmont 1:46ce1e16c870 1550 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 1551 c = *dj->dir; /* Check the entry status */
emilmont 1:46ce1e16c870 1552 if (c == DDE || c == 0) { /* Is it a blank entry? */
emilmont 1:46ce1e16c870 1553 if (n == 0) is = dj->index; /* First index of the contiguous entry */
emilmont 1:46ce1e16c870 1554 if (++n == ne) break; /* A contiguous entry that required count is found */
emilmont 1:46ce1e16c870 1555 } else {
emilmont 1:46ce1e16c870 1556 n = 0; /* Not a blank entry. Restart to search */
emilmont 1:46ce1e16c870 1557 }
emilmont 1:46ce1e16c870 1558 res = dir_next(dj, 1); /* Next entry with table stretch */
emilmont 1:46ce1e16c870 1559 } while (res == FR_OK);
emilmont 1:46ce1e16c870 1560
emilmont 1:46ce1e16c870 1561 if (res == FR_OK && ne > 1) { /* Initialize LFN entry if needed */
emilmont 1:46ce1e16c870 1562 res = dir_sdi(dj, is);
emilmont 1:46ce1e16c870 1563 if (res == FR_OK) {
emilmont 1:46ce1e16c870 1564 sum = sum_sfn(dj->fn); /* Sum of the SFN tied to the LFN */
emilmont 1:46ce1e16c870 1565 ne--;
emilmont 1:46ce1e16c870 1566 do { /* Store LFN entries in bottom first */
emilmont 1:46ce1e16c870 1567 res = move_window(dj->fs, dj->sect);
emilmont 1:46ce1e16c870 1568 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 1569 fit_lfn(dj->lfn, dj->dir, (BYTE)ne, sum);
emilmont 1:46ce1e16c870 1570 dj->fs->wflag = 1;
emilmont 1:46ce1e16c870 1571 res = dir_next(dj, 0); /* Next entry */
emilmont 1:46ce1e16c870 1572 } while (res == FR_OK && --ne);
emilmont 1:46ce1e16c870 1573 }
emilmont 1:46ce1e16c870 1574 }
emilmont 1:46ce1e16c870 1575
emilmont 1:46ce1e16c870 1576 #else /* Non LFN configuration */
emilmont 1:46ce1e16c870 1577 res = dir_sdi(dj, 0);
emilmont 1:46ce1e16c870 1578 if (res == FR_OK) {
emilmont 1:46ce1e16c870 1579 do { /* Find a blank entry for the SFN */
emilmont 1:46ce1e16c870 1580 res = move_window(dj->fs, dj->sect);
emilmont 1:46ce1e16c870 1581 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 1582 c = *dj->dir;
emilmont 1:46ce1e16c870 1583 if (c == DDE || c == 0) break; /* Is it a blank entry? */
emilmont 1:46ce1e16c870 1584 res = dir_next(dj, 1); /* Next entry with table stretch */
emilmont 1:46ce1e16c870 1585 } while (res == FR_OK);
emilmont 1:46ce1e16c870 1586 }
emilmont 1:46ce1e16c870 1587 #endif
emilmont 1:46ce1e16c870 1588
emilmont 1:46ce1e16c870 1589 if (res == FR_OK) { /* Initialize the SFN entry */
emilmont 1:46ce1e16c870 1590 res = move_window(dj->fs, dj->sect);
emilmont 1:46ce1e16c870 1591 if (res == FR_OK) {
emilmont 1:46ce1e16c870 1592 dir = dj->dir;
emilmont 1:46ce1e16c870 1593 mem_set(dir, 0, SZ_DIR); /* Clean the entry */
emilmont 1:46ce1e16c870 1594 mem_cpy(dir, dj->fn, 11); /* Put SFN */
emilmont 1:46ce1e16c870 1595 #if _USE_LFN
emilmont 1:46ce1e16c870 1596 dir[DIR_NTres] = *(dj->fn+NS) & (NS_BODY | NS_EXT); /* Put NT flag */
emilmont 1:46ce1e16c870 1597 #endif
emilmont 1:46ce1e16c870 1598 dj->fs->wflag = 1;
emilmont 1:46ce1e16c870 1599 }
emilmont 1:46ce1e16c870 1600 }
emilmont 1:46ce1e16c870 1601
emilmont 1:46ce1e16c870 1602 return res;
emilmont 1:46ce1e16c870 1603 }
emilmont 1:46ce1e16c870 1604 #endif /* !_FS_READONLY */
emilmont 1:46ce1e16c870 1605
emilmont 1:46ce1e16c870 1606
emilmont 1:46ce1e16c870 1607
emilmont 1:46ce1e16c870 1608
emilmont 1:46ce1e16c870 1609 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1610 /* Remove an object from the directory */
emilmont 1:46ce1e16c870 1611 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1612 #if !_FS_READONLY && !_FS_MINIMIZE
emilmont 1:46ce1e16c870 1613 static
emilmont 1:46ce1e16c870 1614 FRESULT dir_remove ( /* FR_OK: Successful, FR_DISK_ERR: A disk error */
emilmont 1:46ce1e16c870 1615 FATFS_DIR *dj /* Directory object pointing the entry to be removed */
emilmont 1:46ce1e16c870 1616 )
emilmont 1:46ce1e16c870 1617 {
emilmont 1:46ce1e16c870 1618 FRESULT res;
emilmont 1:46ce1e16c870 1619 #if _USE_LFN /* LFN configuration */
emilmont 1:46ce1e16c870 1620 WORD i;
emilmont 1:46ce1e16c870 1621
emilmont 1:46ce1e16c870 1622 i = dj->index; /* SFN index */
emilmont 1:46ce1e16c870 1623 res = dir_sdi(dj, (WORD)((dj->lfn_idx == 0xFFFF) ? i : dj->lfn_idx)); /* Goto the SFN or top of the LFN entries */
emilmont 1:46ce1e16c870 1624 if (res == FR_OK) {
emilmont 1:46ce1e16c870 1625 do {
emilmont 1:46ce1e16c870 1626 res = move_window(dj->fs, dj->sect);
emilmont 1:46ce1e16c870 1627 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 1628 *dj->dir = DDE; /* Mark the entry "deleted" */
emilmont 1:46ce1e16c870 1629 dj->fs->wflag = 1;
emilmont 1:46ce1e16c870 1630 if (dj->index >= i) break; /* When reached SFN, all entries of the object has been deleted. */
emilmont 1:46ce1e16c870 1631 res = dir_next(dj, 0); /* Next entry */
emilmont 1:46ce1e16c870 1632 } while (res == FR_OK);
emilmont 1:46ce1e16c870 1633 if (res == FR_NO_FILE) res = FR_INT_ERR;
emilmont 1:46ce1e16c870 1634 }
emilmont 1:46ce1e16c870 1635
emilmont 1:46ce1e16c870 1636 #else /* Non LFN configuration */
emilmont 1:46ce1e16c870 1637 res = dir_sdi(dj, dj->index);
emilmont 1:46ce1e16c870 1638 if (res == FR_OK) {
emilmont 1:46ce1e16c870 1639 res = move_window(dj->fs, dj->sect);
emilmont 1:46ce1e16c870 1640 if (res == FR_OK) {
emilmont 1:46ce1e16c870 1641 *dj->dir = DDE; /* Mark the entry "deleted" */
emilmont 1:46ce1e16c870 1642 dj->fs->wflag = 1;
emilmont 1:46ce1e16c870 1643 }
emilmont 1:46ce1e16c870 1644 }
emilmont 1:46ce1e16c870 1645 #endif
emilmont 1:46ce1e16c870 1646
emilmont 1:46ce1e16c870 1647 return res;
emilmont 1:46ce1e16c870 1648 }
emilmont 1:46ce1e16c870 1649 #endif /* !_FS_READONLY */
emilmont 1:46ce1e16c870 1650
emilmont 1:46ce1e16c870 1651
emilmont 1:46ce1e16c870 1652
emilmont 1:46ce1e16c870 1653
emilmont 1:46ce1e16c870 1654 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1655 /* Pick a segment and create the object name in directory form */
emilmont 1:46ce1e16c870 1656 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1657
emilmont 1:46ce1e16c870 1658 static
emilmont 1:46ce1e16c870 1659 FRESULT create_name (
emilmont 1:46ce1e16c870 1660 FATFS_DIR *dj, /* Pointer to the directory object */
emilmont 1:46ce1e16c870 1661 const TCHAR **path /* Pointer to pointer to the segment in the path string */
emilmont 1:46ce1e16c870 1662 )
emilmont 1:46ce1e16c870 1663 {
emilmont 1:46ce1e16c870 1664 #ifdef _EXCVT
emilmont 1:46ce1e16c870 1665 static const BYTE excvt[] = _EXCVT; /* Upper conversion table for extended chars */
emilmont 1:46ce1e16c870 1666 #endif
emilmont 1:46ce1e16c870 1667
emilmont 1:46ce1e16c870 1668 #if _USE_LFN /* LFN configuration */
emilmont 1:46ce1e16c870 1669 BYTE b, cf;
emilmont 1:46ce1e16c870 1670 WCHAR w, *lfn;
emilmont 1:46ce1e16c870 1671 UINT i, ni, si, di;
emilmont 1:46ce1e16c870 1672 const TCHAR *p;
emilmont 1:46ce1e16c870 1673
emilmont 1:46ce1e16c870 1674 /* Create LFN in Unicode */
emilmont 1:46ce1e16c870 1675 for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
emilmont 1:46ce1e16c870 1676 lfn = dj->lfn;
emilmont 1:46ce1e16c870 1677 si = di = 0;
emilmont 1:46ce1e16c870 1678 for (;;) {
emilmont 1:46ce1e16c870 1679 w = p[si++]; /* Get a character */
emilmont 1:46ce1e16c870 1680 if (w < ' ' || w == '/' || w == '\\') break; /* Break on end of segment */
emilmont 1:46ce1e16c870 1681 if (di >= _MAX_LFN) /* Reject too long name */
emilmont 1:46ce1e16c870 1682 return FR_INVALID_NAME;
emilmont 1:46ce1e16c870 1683 #if !_LFN_UNICODE
emilmont 1:46ce1e16c870 1684 w &= 0xFF;
emilmont 1:46ce1e16c870 1685 if (IsDBCS1(w)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
emilmont 1:46ce1e16c870 1686 b = (BYTE)p[si++]; /* Get 2nd byte */
emilmont 1:46ce1e16c870 1687 if (!IsDBCS2(b))
emilmont 1:46ce1e16c870 1688 return FR_INVALID_NAME; /* Reject invalid sequence */
emilmont 1:46ce1e16c870 1689 w = (w << 8) + b; /* Create a DBC */
emilmont 1:46ce1e16c870 1690 }
emilmont 1:46ce1e16c870 1691 w = ff_convert(w, 1); /* Convert ANSI/OEM to Unicode */
emilmont 1:46ce1e16c870 1692 if (!w) return FR_INVALID_NAME; /* Reject invalid code */
emilmont 1:46ce1e16c870 1693 #endif
emilmont 1:46ce1e16c870 1694 if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) /* Reject illegal chars for LFN */
emilmont 1:46ce1e16c870 1695 return FR_INVALID_NAME;
emilmont 1:46ce1e16c870 1696 lfn[di++] = w; /* Store the Unicode char */
emilmont 1:46ce1e16c870 1697 }
emilmont 1:46ce1e16c870 1698 *path = &p[si]; /* Return pointer to the next segment */
emilmont 1:46ce1e16c870 1699 cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
emilmont 1:46ce1e16c870 1700 #if _FS_RPATH
emilmont 1:46ce1e16c870 1701 if ((di == 1 && lfn[di-1] == '.') || /* Is this a dot entry? */
emilmont 1:46ce1e16c870 1702 (di == 2 && lfn[di-1] == '.' && lfn[di-2] == '.')) {
emilmont 1:46ce1e16c870 1703 lfn[di] = 0;
emilmont 1:46ce1e16c870 1704 for (i = 0; i < 11; i++)
emilmont 1:46ce1e16c870 1705 dj->fn[i] = (i < di) ? '.' : ' ';
emilmont 1:46ce1e16c870 1706 dj->fn[i] = cf | NS_DOT; /* This is a dot entry */
emilmont 1:46ce1e16c870 1707 return FR_OK;
emilmont 1:46ce1e16c870 1708 }
emilmont 1:46ce1e16c870 1709 #endif
emilmont 1:46ce1e16c870 1710 while (di) { /* Strip trailing spaces and dots */
emilmont 1:46ce1e16c870 1711 w = lfn[di-1];
emilmont 1:46ce1e16c870 1712 if (w != ' ' && w != '.') break;
emilmont 1:46ce1e16c870 1713 di--;
emilmont 1:46ce1e16c870 1714 }
emilmont 1:46ce1e16c870 1715 if (!di) return FR_INVALID_NAME; /* Reject nul string */
emilmont 1:46ce1e16c870 1716
emilmont 1:46ce1e16c870 1717 lfn[di] = 0; /* LFN is created */
emilmont 1:46ce1e16c870 1718
emilmont 1:46ce1e16c870 1719 /* Create SFN in directory form */
emilmont 1:46ce1e16c870 1720 mem_set(dj->fn, ' ', 11);
emilmont 1:46ce1e16c870 1721 for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */
emilmont 1:46ce1e16c870 1722 if (si) cf |= NS_LOSS | NS_LFN;
emilmont 1:46ce1e16c870 1723 while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */
emilmont 1:46ce1e16c870 1724
emilmont 1:46ce1e16c870 1725 b = i = 0; ni = 8;
emilmont 1:46ce1e16c870 1726 for (;;) {
emilmont 1:46ce1e16c870 1727 w = lfn[si++]; /* Get an LFN char */
emilmont 1:46ce1e16c870 1728 if (!w) break; /* Break on end of the LFN */
emilmont 1:46ce1e16c870 1729 if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */
emilmont 1:46ce1e16c870 1730 cf |= NS_LOSS | NS_LFN; continue;
emilmont 1:46ce1e16c870 1731 }
emilmont 1:46ce1e16c870 1732
emilmont 1:46ce1e16c870 1733 if (i >= ni || si == di) { /* Extension or end of SFN */
emilmont 1:46ce1e16c870 1734 if (ni == 11) { /* Long extension */
emilmont 1:46ce1e16c870 1735 cf |= NS_LOSS | NS_LFN; break;
emilmont 1:46ce1e16c870 1736 }
emilmont 1:46ce1e16c870 1737 if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */
emilmont 1:46ce1e16c870 1738 if (si > di) break; /* No extension */
emilmont 1:46ce1e16c870 1739 si = di; i = 8; ni = 11; /* Enter extension section */
emilmont 1:46ce1e16c870 1740 b <<= 2; continue;
emilmont 1:46ce1e16c870 1741 }
emilmont 1:46ce1e16c870 1742
emilmont 1:46ce1e16c870 1743 if (w >= 0x80) { /* Non ASCII char */
emilmont 1:46ce1e16c870 1744 #ifdef _EXCVT
emilmont 1:46ce1e16c870 1745 w = ff_convert(w, 0); /* Unicode -> OEM code */
emilmont 1:46ce1e16c870 1746 if (w) w = excvt[w - 0x80]; /* Convert extended char to upper (SBCS) */
emilmont 1:46ce1e16c870 1747 #else
emilmont 1:46ce1e16c870 1748 w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */
emilmont 1:46ce1e16c870 1749 #endif
emilmont 1:46ce1e16c870 1750 cf |= NS_LFN; /* Force create LFN entry */
emilmont 1:46ce1e16c870 1751 }
emilmont 1:46ce1e16c870 1752
emilmont 1:46ce1e16c870 1753 if (_DF1S && w >= 0x100) { /* Double byte char (always false on SBCS cfg) */
emilmont 1:46ce1e16c870 1754 if (i >= ni - 1) {
emilmont 1:46ce1e16c870 1755 cf |= NS_LOSS | NS_LFN; i = ni; continue;
emilmont 1:46ce1e16c870 1756 }
emilmont 1:46ce1e16c870 1757 dj->fn[i++] = (BYTE)(w >> 8);
emilmont 1:46ce1e16c870 1758 } else { /* Single byte char */
emilmont 1:46ce1e16c870 1759 if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal chars for SFN */
emilmont 1:46ce1e16c870 1760 w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */
emilmont 1:46ce1e16c870 1761 } else {
emilmont 1:46ce1e16c870 1762 if (IsUpper(w)) { /* ASCII large capital */
emilmont 1:46ce1e16c870 1763 b |= 2;
emilmont 1:46ce1e16c870 1764 } else {
emilmont 1:46ce1e16c870 1765 if (IsLower(w)) { /* ASCII small capital */
emilmont 1:46ce1e16c870 1766 b |= 1; w -= 0x20;
emilmont 1:46ce1e16c870 1767 }
emilmont 1:46ce1e16c870 1768 }
emilmont 1:46ce1e16c870 1769 }
emilmont 1:46ce1e16c870 1770 }
emilmont 1:46ce1e16c870 1771 dj->fn[i++] = (BYTE)w;
emilmont 1:46ce1e16c870 1772 }
emilmont 1:46ce1e16c870 1773
emilmont 1:46ce1e16c870 1774 if (dj->fn[0] == DDE) dj->fn[0] = NDDE; /* If the first char collides with deleted mark, replace it with 0x05 */
emilmont 1:46ce1e16c870 1775
emilmont 1:46ce1e16c870 1776 if (ni == 8) b <<= 2;
emilmont 1:46ce1e16c870 1777 if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) /* Create LFN entry when there are composite capitals */
emilmont 1:46ce1e16c870 1778 cf |= NS_LFN;
emilmont 1:46ce1e16c870 1779 if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended char, NT flags are created */
emilmont 1:46ce1e16c870 1780 if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */
emilmont 1:46ce1e16c870 1781 if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */
emilmont 1:46ce1e16c870 1782 }
emilmont 1:46ce1e16c870 1783
emilmont 1:46ce1e16c870 1784 dj->fn[NS] = cf; /* SFN is created */
emilmont 1:46ce1e16c870 1785
emilmont 1:46ce1e16c870 1786 return FR_OK;
emilmont 1:46ce1e16c870 1787
emilmont 1:46ce1e16c870 1788
emilmont 1:46ce1e16c870 1789 #else /* Non-LFN configuration */
emilmont 1:46ce1e16c870 1790 BYTE b, c, d, *sfn;
emilmont 1:46ce1e16c870 1791 UINT ni, si, i;
emilmont 1:46ce1e16c870 1792 const char *p;
emilmont 1:46ce1e16c870 1793
emilmont 1:46ce1e16c870 1794 /* Create file name in directory form */
emilmont 1:46ce1e16c870 1795 for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
emilmont 1:46ce1e16c870 1796 sfn = dj->fn;
emilmont 1:46ce1e16c870 1797 mem_set(sfn, ' ', 11);
emilmont 1:46ce1e16c870 1798 si = i = b = 0; ni = 8;
emilmont 1:46ce1e16c870 1799 #if _FS_RPATH
emilmont 1:46ce1e16c870 1800 if (p[si] == '.') { /* Is this a dot entry? */
emilmont 1:46ce1e16c870 1801 for (;;) {
emilmont 1:46ce1e16c870 1802 c = (BYTE)p[si++];
emilmont 1:46ce1e16c870 1803 if (c != '.' || si >= 3) break;
emilmont 1:46ce1e16c870 1804 sfn[i++] = c;
emilmont 1:46ce1e16c870 1805 }
emilmont 1:46ce1e16c870 1806 if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;
emilmont 1:46ce1e16c870 1807 *path = &p[si]; /* Return pointer to the next segment */
emilmont 1:46ce1e16c870 1808 sfn[NS] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of path */
emilmont 1:46ce1e16c870 1809 return FR_OK;
emilmont 1:46ce1e16c870 1810 }
emilmont 1:46ce1e16c870 1811 #endif
emilmont 1:46ce1e16c870 1812 for (;;) {
emilmont 1:46ce1e16c870 1813 c = (BYTE)p[si++];
emilmont 1:46ce1e16c870 1814 if (c <= ' ' || c == '/' || c == '\\') break; /* Break on end of segment */
emilmont 1:46ce1e16c870 1815 if (c == '.' || i >= ni) {
emilmont 1:46ce1e16c870 1816 if (ni != 8 || c != '.') return FR_INVALID_NAME;
emilmont 1:46ce1e16c870 1817 i = 8; ni = 11;
emilmont 1:46ce1e16c870 1818 b <<= 2; continue;
emilmont 1:46ce1e16c870 1819 }
emilmont 1:46ce1e16c870 1820 if (c >= 0x80) { /* Extended char? */
emilmont 1:46ce1e16c870 1821 b |= 3; /* Eliminate NT flag */
emilmont 1:46ce1e16c870 1822 #ifdef _EXCVT
emilmont 1:46ce1e16c870 1823 c = excvt[c - 0x80]; /* Upper conversion (SBCS) */
emilmont 1:46ce1e16c870 1824 #else
emilmont 1:46ce1e16c870 1825 #if !_DF1S /* ASCII only cfg */
emilmont 1:46ce1e16c870 1826 return FR_INVALID_NAME;
emilmont 1:46ce1e16c870 1827 #endif
emilmont 1:46ce1e16c870 1828 #endif
emilmont 1:46ce1e16c870 1829 }
emilmont 1:46ce1e16c870 1830 if (IsDBCS1(c)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
emilmont 1:46ce1e16c870 1831 d = (BYTE)p[si++]; /* Get 2nd byte */
emilmont 1:46ce1e16c870 1832 if (!IsDBCS2(d) || i >= ni - 1) /* Reject invalid DBC */
emilmont 1:46ce1e16c870 1833 return FR_INVALID_NAME;
emilmont 1:46ce1e16c870 1834 sfn[i++] = c;
emilmont 1:46ce1e16c870 1835 sfn[i++] = d;
emilmont 1:46ce1e16c870 1836 } else { /* Single byte code */
emilmont 1:46ce1e16c870 1837 if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) /* Reject illegal chrs for SFN */
emilmont 1:46ce1e16c870 1838 return FR_INVALID_NAME;
emilmont 1:46ce1e16c870 1839 if (IsUpper(c)) { /* ASCII large capital? */
emilmont 1:46ce1e16c870 1840 b |= 2;
emilmont 1:46ce1e16c870 1841 } else {
emilmont 1:46ce1e16c870 1842 if (IsLower(c)) { /* ASCII small capital? */
emilmont 1:46ce1e16c870 1843 b |= 1; c -= 0x20;
emilmont 1:46ce1e16c870 1844 }
emilmont 1:46ce1e16c870 1845 }
emilmont 1:46ce1e16c870 1846 sfn[i++] = c;
emilmont 1:46ce1e16c870 1847 }
emilmont 1:46ce1e16c870 1848 }
emilmont 1:46ce1e16c870 1849 *path = &p[si]; /* Return pointer to the next segment */
emilmont 1:46ce1e16c870 1850 c = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
emilmont 1:46ce1e16c870 1851
emilmont 1:46ce1e16c870 1852 if (!i) return FR_INVALID_NAME; /* Reject nul string */
emilmont 1:46ce1e16c870 1853 if (sfn[0] == DDE) sfn[0] = NDDE; /* When first char collides with DDE, replace it with 0x05 */
emilmont 1:46ce1e16c870 1854
emilmont 1:46ce1e16c870 1855 if (ni == 8) b <<= 2;
emilmont 1:46ce1e16c870 1856 if ((b & 0x03) == 0x01) c |= NS_EXT; /* NT flag (Name extension has only small capital) */
emilmont 1:46ce1e16c870 1857 if ((b & 0x0C) == 0x04) c |= NS_BODY; /* NT flag (Name body has only small capital) */
emilmont 1:46ce1e16c870 1858
emilmont 1:46ce1e16c870 1859 sfn[NS] = c; /* Store NT flag, File name is created */
emilmont 1:46ce1e16c870 1860
emilmont 1:46ce1e16c870 1861 return FR_OK;
emilmont 1:46ce1e16c870 1862 #endif
emilmont 1:46ce1e16c870 1863 }
emilmont 1:46ce1e16c870 1864
emilmont 1:46ce1e16c870 1865
emilmont 1:46ce1e16c870 1866
emilmont 1:46ce1e16c870 1867
emilmont 1:46ce1e16c870 1868 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1869 /* Get file information from directory entry */
emilmont 1:46ce1e16c870 1870 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1871 #if _FS_MINIMIZE <= 1
emilmont 1:46ce1e16c870 1872 static
emilmont 1:46ce1e16c870 1873 void get_fileinfo ( /* No return code */
emilmont 1:46ce1e16c870 1874 FATFS_DIR *dj, /* Pointer to the directory object */
emilmont 1:46ce1e16c870 1875 FILINFO *fno /* Pointer to the file information to be filled */
emilmont 1:46ce1e16c870 1876 )
emilmont 1:46ce1e16c870 1877 {
emilmont 1:46ce1e16c870 1878 UINT i;
emilmont 1:46ce1e16c870 1879 BYTE nt, *dir;
emilmont 1:46ce1e16c870 1880 TCHAR *p, c;
emilmont 1:46ce1e16c870 1881
emilmont 1:46ce1e16c870 1882
emilmont 1:46ce1e16c870 1883 p = fno->fname;
emilmont 1:46ce1e16c870 1884 if (dj->sect) {
emilmont 1:46ce1e16c870 1885 dir = dj->dir;
emilmont 1:46ce1e16c870 1886 nt = dir[DIR_NTres]; /* NT flag */
emilmont 1:46ce1e16c870 1887 for (i = 0; i < 8; i++) { /* Copy name body */
emilmont 1:46ce1e16c870 1888 c = dir[i];
emilmont 1:46ce1e16c870 1889 if (c == ' ') break;
emilmont 1:46ce1e16c870 1890 if (c == NDDE) c = (TCHAR)DDE;
emilmont 1:46ce1e16c870 1891 if (_USE_LFN && (nt & NS_BODY) && IsUpper(c)) c += 0x20;
emilmont 1:46ce1e16c870 1892 #if _LFN_UNICODE
emilmont 1:46ce1e16c870 1893 if (IsDBCS1(c) && i < 7 && IsDBCS2(dir[i+1]))
emilmont 1:46ce1e16c870 1894 c = (c << 8) | dir[++i];
emilmont 1:46ce1e16c870 1895 c = ff_convert(c, 1);
emilmont 1:46ce1e16c870 1896 if (!c) c = '?';
emilmont 1:46ce1e16c870 1897 #endif
emilmont 1:46ce1e16c870 1898 *p++ = c;
emilmont 1:46ce1e16c870 1899 }
emilmont 1:46ce1e16c870 1900 if (dir[8] != ' ') { /* Copy name extension */
emilmont 1:46ce1e16c870 1901 *p++ = '.';
emilmont 1:46ce1e16c870 1902 for (i = 8; i < 11; i++) {
emilmont 1:46ce1e16c870 1903 c = dir[i];
emilmont 1:46ce1e16c870 1904 if (c == ' ') break;
emilmont 1:46ce1e16c870 1905 if (_USE_LFN && (nt & NS_EXT) && IsUpper(c)) c += 0x20;
emilmont 1:46ce1e16c870 1906 #if _LFN_UNICODE
emilmont 1:46ce1e16c870 1907 if (IsDBCS1(c) && i < 10 && IsDBCS2(dir[i+1]))
emilmont 1:46ce1e16c870 1908 c = (c << 8) | dir[++i];
emilmont 1:46ce1e16c870 1909 c = ff_convert(c, 1);
emilmont 1:46ce1e16c870 1910 if (!c) c = '?';
emilmont 1:46ce1e16c870 1911 #endif
emilmont 1:46ce1e16c870 1912 *p++ = c;
emilmont 1:46ce1e16c870 1913 }
emilmont 1:46ce1e16c870 1914 }
emilmont 1:46ce1e16c870 1915 fno->fattrib = dir[DIR_Attr]; /* Attribute */
emilmont 1:46ce1e16c870 1916 fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */
emilmont 1:46ce1e16c870 1917 fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */
emilmont 1:46ce1e16c870 1918 fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */
emilmont 1:46ce1e16c870 1919 }
emilmont 1:46ce1e16c870 1920 *p = 0; /* Terminate SFN str by a \0 */
emilmont 1:46ce1e16c870 1921
emilmont 1:46ce1e16c870 1922 #if _USE_LFN
emilmont 1:46ce1e16c870 1923 if (fno->lfname && fno->lfsize) {
emilmont 1:46ce1e16c870 1924 TCHAR *tp = fno->lfname;
emilmont 1:46ce1e16c870 1925 WCHAR w, *lfn;
emilmont 1:46ce1e16c870 1926
emilmont 1:46ce1e16c870 1927 i = 0;
emilmont 1:46ce1e16c870 1928 if (dj->sect && dj->lfn_idx != 0xFFFF) {/* Get LFN if available */
emilmont 1:46ce1e16c870 1929 lfn = dj->lfn;
emilmont 1:46ce1e16c870 1930 while ((w = *lfn++) != 0) { /* Get an LFN char */
emilmont 1:46ce1e16c870 1931 #if !_LFN_UNICODE
emilmont 1:46ce1e16c870 1932 w = ff_convert(w, 0); /* Unicode -> OEM conversion */
emilmont 1:46ce1e16c870 1933 if (!w) { i = 0; break; } /* Could not convert, no LFN */
emilmont 1:46ce1e16c870 1934 if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC (always false on SBCS cfg) */
emilmont 1:46ce1e16c870 1935 tp[i++] = (TCHAR)(w >> 8);
emilmont 1:46ce1e16c870 1936 #endif
emilmont 1:46ce1e16c870 1937 if (i >= fno->lfsize - 1) { i = 0; break; } /* Buffer overflow, no LFN */
emilmont 1:46ce1e16c870 1938 tp[i++] = (TCHAR)w;
emilmont 1:46ce1e16c870 1939 }
emilmont 1:46ce1e16c870 1940 }
emilmont 1:46ce1e16c870 1941 tp[i] = 0; /* Terminate the LFN str by a \0 */
emilmont 1:46ce1e16c870 1942 }
emilmont 1:46ce1e16c870 1943 #endif
emilmont 1:46ce1e16c870 1944 }
emilmont 1:46ce1e16c870 1945 #endif /* _FS_MINIMIZE <= 1 */
emilmont 1:46ce1e16c870 1946
emilmont 1:46ce1e16c870 1947
emilmont 1:46ce1e16c870 1948
emilmont 1:46ce1e16c870 1949
emilmont 1:46ce1e16c870 1950 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1951 /* Follow a file path */
emilmont 1:46ce1e16c870 1952 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 1953
emilmont 1:46ce1e16c870 1954 static
emilmont 1:46ce1e16c870 1955 FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
emilmont 1:46ce1e16c870 1956 FATFS_DIR *dj, /* Directory object to return last directory and found object */
emilmont 1:46ce1e16c870 1957 const TCHAR *path /* Full-path string to find a file or directory */
emilmont 1:46ce1e16c870 1958 )
emilmont 1:46ce1e16c870 1959 {
emilmont 1:46ce1e16c870 1960 FRESULT res;
emilmont 1:46ce1e16c870 1961 BYTE *dir, ns;
emilmont 1:46ce1e16c870 1962
emilmont 1:46ce1e16c870 1963
emilmont 1:46ce1e16c870 1964 #if _FS_RPATH
emilmont 1:46ce1e16c870 1965 if (*path == '/' || *path == '\\') { /* There is a heading separator */
emilmont 1:46ce1e16c870 1966 path++; dj->sclust = 0; /* Strip it and start from the root dir */
emilmont 1:46ce1e16c870 1967 } else { /* No heading separator */
emilmont 1:46ce1e16c870 1968 dj->sclust = dj->fs->cdir; /* Start from the current dir */
emilmont 1:46ce1e16c870 1969 }
emilmont 1:46ce1e16c870 1970 #else
emilmont 1:46ce1e16c870 1971 if (*path == '/' || *path == '\\') /* Strip heading separator if exist */
emilmont 1:46ce1e16c870 1972 path++;
emilmont 1:46ce1e16c870 1973 dj->sclust = 0; /* Start from the root dir */
emilmont 1:46ce1e16c870 1974 #endif
emilmont 1:46ce1e16c870 1975
emilmont 1:46ce1e16c870 1976 if ((UINT)*path < ' ') { /* Nul path means the start directory itself */
emilmont 1:46ce1e16c870 1977 res = dir_sdi(dj, 0);
emilmont 1:46ce1e16c870 1978 dj->dir = 0;
emilmont 1:46ce1e16c870 1979 } else { /* Follow path */
emilmont 1:46ce1e16c870 1980 for (;;) {
emilmont 1:46ce1e16c870 1981 res = create_name(dj, &path); /* Get a segment */
emilmont 1:46ce1e16c870 1982 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 1983 res = dir_find(dj); /* Find it */
emilmont 1:46ce1e16c870 1984 ns = *(dj->fn+NS);
emilmont 1:46ce1e16c870 1985 if (res != FR_OK) { /* Failed to find the object */
emilmont 1:46ce1e16c870 1986 if (res != FR_NO_FILE) break; /* Abort if any hard error occurred */
emilmont 1:46ce1e16c870 1987 /* Object not found */
emilmont 1:46ce1e16c870 1988 if (_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exit */
emilmont 1:46ce1e16c870 1989 dj->sclust = 0; dj->dir = 0; /* It is the root dir */
emilmont 1:46ce1e16c870 1990 res = FR_OK;
emilmont 1:46ce1e16c870 1991 if (!(ns & NS_LAST)) continue;
emilmont 1:46ce1e16c870 1992 } else { /* Could not find the object */
emilmont 1:46ce1e16c870 1993 if (!(ns & NS_LAST)) res = FR_NO_PATH;
emilmont 1:46ce1e16c870 1994 }
emilmont 1:46ce1e16c870 1995 break;
emilmont 1:46ce1e16c870 1996 }
emilmont 1:46ce1e16c870 1997 if (ns & NS_LAST) break; /* Last segment match. Function completed. */
emilmont 1:46ce1e16c870 1998 dir = dj->dir; /* There is next segment. Follow the sub directory */
emilmont 1:46ce1e16c870 1999 if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow because it is a file */
emilmont 1:46ce1e16c870 2000 res = FR_NO_PATH; break;
emilmont 1:46ce1e16c870 2001 }
emilmont 1:46ce1e16c870 2002 dj->sclust = ld_clust(dj->fs, dir);
emilmont 1:46ce1e16c870 2003 }
emilmont 1:46ce1e16c870 2004 }
emilmont 1:46ce1e16c870 2005
emilmont 1:46ce1e16c870 2006 return res;
emilmont 1:46ce1e16c870 2007 }
emilmont 1:46ce1e16c870 2008
emilmont 1:46ce1e16c870 2009
emilmont 1:46ce1e16c870 2010
emilmont 1:46ce1e16c870 2011
emilmont 1:46ce1e16c870 2012 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2013 /* Load a sector and check if it is an FAT Volume Boot Record */
emilmont 1:46ce1e16c870 2014 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2015
emilmont 1:46ce1e16c870 2016 static
emilmont 1:46ce1e16c870 2017 BYTE check_fs ( /* 0:FAT-VBR, 1:Any BR but not FAT, 2:Not a BR, 3:Disk error */
emilmont 1:46ce1e16c870 2018 FATFS *fs, /* File system object */
emilmont 1:46ce1e16c870 2019 DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
emilmont 1:46ce1e16c870 2020 )
emilmont 1:46ce1e16c870 2021 {
emilmont 1:46ce1e16c870 2022 if (disk_read(fs->drv, fs->win, sect, 1) != RES_OK) /* Load boot record */
emilmont 1:46ce1e16c870 2023 return 3;
emilmont 1:46ce1e16c870 2024 if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature (always placed at offset 510 even if the sector size is >512) */
emilmont 1:46ce1e16c870 2025 return 2;
emilmont 1:46ce1e16c870 2026
emilmont 1:46ce1e16c870 2027 if ((LD_DWORD(&fs->win[BS_FilSysType]) & 0xFFFFFF) == 0x544146) /* Check "FAT" string */
emilmont 1:46ce1e16c870 2028 return 0;
emilmont 1:46ce1e16c870 2029 if ((LD_DWORD(&fs->win[BS_FilSysType32]) & 0xFFFFFF) == 0x544146)
emilmont 1:46ce1e16c870 2030 return 0;
emilmont 1:46ce1e16c870 2031
emilmont 1:46ce1e16c870 2032 return 1;
emilmont 1:46ce1e16c870 2033 }
emilmont 1:46ce1e16c870 2034
emilmont 1:46ce1e16c870 2035
emilmont 1:46ce1e16c870 2036
emilmont 1:46ce1e16c870 2037
emilmont 1:46ce1e16c870 2038 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2039 /* Check if the file system object is valid or not */
emilmont 1:46ce1e16c870 2040 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2041
emilmont 1:46ce1e16c870 2042 static
emilmont 1:46ce1e16c870 2043 FRESULT chk_mounted ( /* FR_OK(0): successful, !=0: any error occurred */
emilmont 1:46ce1e16c870 2044 const TCHAR **path, /* Pointer to pointer to the path name (drive number) */
emilmont 1:46ce1e16c870 2045 FATFS **rfs, /* Pointer to pointer to the found file system object */
emilmont 1:46ce1e16c870 2046 BYTE wmode /* !=0: Check write protection for write access */
emilmont 1:46ce1e16c870 2047 )
emilmont 1:46ce1e16c870 2048 {
emilmont 1:46ce1e16c870 2049 BYTE fmt, b, pi, *tbl;
emilmont 1:46ce1e16c870 2050 UINT vol;
emilmont 1:46ce1e16c870 2051 DSTATUS stat;
emilmont 1:46ce1e16c870 2052 DWORD bsect, fasize, tsect, sysect, nclst, szbfat;
emilmont 1:46ce1e16c870 2053 WORD nrsv;
emilmont 1:46ce1e16c870 2054 const TCHAR *p = *path;
emilmont 1:46ce1e16c870 2055 FATFS *fs;
emilmont 1:46ce1e16c870 2056
emilmont 1:46ce1e16c870 2057
emilmont 1:46ce1e16c870 2058 /* Get logical drive number from the path name */
emilmont 1:46ce1e16c870 2059 vol = p[0] - '0'; /* Is there a drive number? */
emilmont 1:46ce1e16c870 2060 if (vol <= 9 && p[1] == ':') { /* Found a drive number, get and strip it */
emilmont 1:46ce1e16c870 2061 p += 2; *path = p; /* Return pointer to the path name */
emilmont 1:46ce1e16c870 2062 } else { /* No drive number is given */
emilmont 1:46ce1e16c870 2063 #if _FS_RPATH
emilmont 1:46ce1e16c870 2064 vol = CurrVol; /* Use current drive */
emilmont 1:46ce1e16c870 2065 #else
emilmont 1:46ce1e16c870 2066 vol = 0; /* Use drive 0 */
emilmont 1:46ce1e16c870 2067 #endif
emilmont 1:46ce1e16c870 2068 }
emilmont 1:46ce1e16c870 2069
emilmont 1:46ce1e16c870 2070 /* Check if the file system object is valid or not */
emilmont 1:46ce1e16c870 2071 *rfs = 0;
emilmont 1:46ce1e16c870 2072 if (vol >= _VOLUMES) /* Is the drive number valid? */
emilmont 1:46ce1e16c870 2073 return FR_INVALID_DRIVE;
emilmont 1:46ce1e16c870 2074 fs = FatFs[vol]; /* Get corresponding file system object */
emilmont 1:46ce1e16c870 2075 if (!fs) return FR_NOT_ENABLED; /* Is the file system object available? */
emilmont 1:46ce1e16c870 2076
emilmont 1:46ce1e16c870 2077 ENTER_FF(fs); /* Lock file system */
emilmont 1:46ce1e16c870 2078
emilmont 1:46ce1e16c870 2079 *rfs = fs; /* Return pointer to the corresponding file system object */
emilmont 1:46ce1e16c870 2080 if (fs->fs_type) { /* If the volume has been mounted */
emilmont 1:46ce1e16c870 2081 stat = disk_status(fs->drv);
emilmont 1:46ce1e16c870 2082 if (!(stat & STA_NOINIT)) { /* and the physical drive is kept initialized (has not been changed), */
emilmont 1:46ce1e16c870 2083 if (!_FS_READONLY && wmode && (stat & STA_PROTECT)) /* Check write protection if needed */
emilmont 1:46ce1e16c870 2084 return FR_WRITE_PROTECTED;
emilmont 1:46ce1e16c870 2085 return FR_OK; /* The file system object is valid */
emilmont 1:46ce1e16c870 2086 }
emilmont 1:46ce1e16c870 2087 }
emilmont 1:46ce1e16c870 2088
emilmont 1:46ce1e16c870 2089 /* The file system object is not valid. */
emilmont 1:46ce1e16c870 2090 /* Following code attempts to mount the volume. (analyze BPB and initialize the fs object) */
emilmont 1:46ce1e16c870 2091
emilmont 1:46ce1e16c870 2092 fs->fs_type = 0; /* Clear the file system object */
emilmont 1:46ce1e16c870 2093 fs->drv = LD2PD(vol); /* Bind the logical drive and a physical drive */
emilmont 1:46ce1e16c870 2094 stat = disk_initialize(fs->drv); /* Initialize the physical drive */
emilmont 1:46ce1e16c870 2095 if (stat & STA_NOINIT) /* Check if the initialization succeeded */
emilmont 1:46ce1e16c870 2096 return FR_NOT_READY; /* Failed to initialize due to no medium or hard error */
emilmont 1:46ce1e16c870 2097 if (!_FS_READONLY && wmode && (stat & STA_PROTECT)) /* Check disk write protection if needed */
emilmont 1:46ce1e16c870 2098 return FR_WRITE_PROTECTED;
emilmont 1:46ce1e16c870 2099 #if _MAX_SS != 512 /* Get disk sector size (variable sector size cfg only) */
emilmont 1:46ce1e16c870 2100 if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &fs->ssize) != RES_OK)
emilmont 1:46ce1e16c870 2101 return FR_DISK_ERR;
emilmont 1:46ce1e16c870 2102 #endif
emilmont 1:46ce1e16c870 2103 /* Search FAT partition on the drive. Supports only generic partitions, FDISK and SFD. */
emilmont 1:46ce1e16c870 2104 fmt = check_fs(fs, bsect = 0); /* Load sector 0 and check if it is an FAT-VBR (in SFD) */
emilmont 1:46ce1e16c870 2105 if (LD2PT(vol) && !fmt) fmt = 1; /* Force non-SFD if the volume is forced partition */
emilmont 1:46ce1e16c870 2106 if (fmt == 1) { /* Not an FAT-VBR, the physical drive can be partitioned */
emilmont 1:46ce1e16c870 2107 /* Check the partition listed in the partition table */
emilmont 1:46ce1e16c870 2108 pi = LD2PT(vol);
emilmont 1:46ce1e16c870 2109 if (pi) pi--;
emilmont 1:46ce1e16c870 2110 tbl = &fs->win[MBR_Table + pi * SZ_PTE];/* Partition table */
emilmont 1:46ce1e16c870 2111 if (tbl[4]) { /* Is the partition existing? */
emilmont 1:46ce1e16c870 2112 bsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */
emilmont 1:46ce1e16c870 2113 fmt = check_fs(fs, bsect); /* Check the partition */
emilmont 1:46ce1e16c870 2114 }
emilmont 1:46ce1e16c870 2115 }
emilmont 1:46ce1e16c870 2116 if (fmt == 3) return FR_DISK_ERR;
emilmont 1:46ce1e16c870 2117 if (fmt) return FR_NO_FILESYSTEM; /* No FAT volume is found */
emilmont 1:46ce1e16c870 2118
emilmont 1:46ce1e16c870 2119 /* An FAT volume is found. Following code initializes the file system object */
emilmont 1:46ce1e16c870 2120
emilmont 1:46ce1e16c870 2121 if (LD_WORD(fs->win+BPB_BytsPerSec) != SS(fs)) /* (BPB_BytsPerSec must be equal to the physical sector size) */
emilmont 1:46ce1e16c870 2122 return FR_NO_FILESYSTEM;
emilmont 1:46ce1e16c870 2123
emilmont 1:46ce1e16c870 2124 fasize = LD_WORD(fs->win+BPB_FATSz16); /* Number of sectors per FAT */
emilmont 1:46ce1e16c870 2125 if (!fasize) fasize = LD_DWORD(fs->win+BPB_FATSz32);
emilmont 1:46ce1e16c870 2126 fs->fsize = fasize;
emilmont 1:46ce1e16c870 2127
emilmont 1:46ce1e16c870 2128 fs->n_fats = b = fs->win[BPB_NumFATs]; /* Number of FAT copies */
emilmont 1:46ce1e16c870 2129 if (b != 1 && b != 2) return FR_NO_FILESYSTEM; /* (Must be 1 or 2) */
emilmont 1:46ce1e16c870 2130 fasize *= b; /* Number of sectors for FAT area */
emilmont 1:46ce1e16c870 2131
emilmont 1:46ce1e16c870 2132 fs->csize = b = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
emilmont 1:46ce1e16c870 2133 if (!b || (b & (b - 1))) return FR_NO_FILESYSTEM; /* (Must be power of 2) */
emilmont 1:46ce1e16c870 2134
emilmont 1:46ce1e16c870 2135 fs->n_rootdir = LD_WORD(fs->win+BPB_RootEntCnt); /* Number of root directory entries */
emilmont 1:46ce1e16c870 2136 if (fs->n_rootdir % (SS(fs) / SZ_DIR)) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be sector aligned) */
emilmont 1:46ce1e16c870 2137
emilmont 1:46ce1e16c870 2138 tsect = LD_WORD(fs->win+BPB_TotSec16); /* Number of sectors on the volume */
emilmont 1:46ce1e16c870 2139 if (!tsect) tsect = LD_DWORD(fs->win+BPB_TotSec32);
emilmont 1:46ce1e16c870 2140
emilmont 1:46ce1e16c870 2141 nrsv = LD_WORD(fs->win+BPB_RsvdSecCnt); /* Number of reserved sectors */
emilmont 1:46ce1e16c870 2142 if (!nrsv) return FR_NO_FILESYSTEM; /* (BPB_RsvdSecCnt must not be 0) */
emilmont 1:46ce1e16c870 2143
emilmont 1:46ce1e16c870 2144 /* Determine the FAT sub type */
emilmont 1:46ce1e16c870 2145 sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZ_DIR); /* RSV+FAT+DIR */
emilmont 1:46ce1e16c870 2146 if (tsect < sysect) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
emilmont 1:46ce1e16c870 2147 nclst = (tsect - sysect) / fs->csize; /* Number of clusters */
emilmont 1:46ce1e16c870 2148 if (!nclst) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
emilmont 1:46ce1e16c870 2149 fmt = FS_FAT12;
emilmont 1:46ce1e16c870 2150 if (nclst >= MIN_FAT16) fmt = FS_FAT16;
emilmont 1:46ce1e16c870 2151 if (nclst >= MIN_FAT32) fmt = FS_FAT32;
emilmont 1:46ce1e16c870 2152
emilmont 1:46ce1e16c870 2153 /* Boundaries and Limits */
emilmont 1:46ce1e16c870 2154 fs->n_fatent = nclst + 2; /* Number of FAT entries */
emilmont 1:46ce1e16c870 2155 fs->database = bsect + sysect; /* Data start sector */
emilmont 1:46ce1e16c870 2156 fs->fatbase = bsect + nrsv; /* FAT start sector */
emilmont 1:46ce1e16c870 2157 if (fmt == FS_FAT32) {
emilmont 1:46ce1e16c870 2158 if (fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */
emilmont 1:46ce1e16c870 2159 fs->dirbase = LD_DWORD(fs->win+BPB_RootClus); /* Root directory start cluster */
emilmont 1:46ce1e16c870 2160 szbfat = fs->n_fatent * 4; /* (Required FAT size) */
emilmont 1:46ce1e16c870 2161 } else {
emilmont 1:46ce1e16c870 2162 if (!fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must not be 0) */
emilmont 1:46ce1e16c870 2163 fs->dirbase = fs->fatbase + fasize; /* Root directory start sector */
emilmont 1:46ce1e16c870 2164 szbfat = (fmt == FS_FAT16) ? /* (Required FAT size) */
emilmont 1:46ce1e16c870 2165 fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
emilmont 1:46ce1e16c870 2166 }
emilmont 1:46ce1e16c870 2167 if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs)) /* (BPB_FATSz must not be less than required) */
emilmont 1:46ce1e16c870 2168 return FR_NO_FILESYSTEM;
emilmont 1:46ce1e16c870 2169
emilmont 1:46ce1e16c870 2170 #if !_FS_READONLY
emilmont 1:46ce1e16c870 2171 /* Initialize cluster allocation information */
emilmont 1:46ce1e16c870 2172 fs->free_clust = 0xFFFFFFFF;
emilmont 1:46ce1e16c870 2173 fs->last_clust = 0;
emilmont 1:46ce1e16c870 2174
emilmont 1:46ce1e16c870 2175 /* Get fsinfo if available */
emilmont 1:46ce1e16c870 2176 if (fmt == FS_FAT32) {
emilmont 1:46ce1e16c870 2177 fs->fsi_flag = 0;
emilmont 1:46ce1e16c870 2178 fs->fsi_sector = bsect + LD_WORD(fs->win+BPB_FSInfo);
emilmont 1:46ce1e16c870 2179 if (disk_read(fs->drv, fs->win, fs->fsi_sector, 1) == RES_OK &&
emilmont 1:46ce1e16c870 2180 LD_WORD(fs->win+BS_55AA) == 0xAA55 &&
emilmont 1:46ce1e16c870 2181 LD_DWORD(fs->win+FSI_LeadSig) == 0x41615252 &&
emilmont 1:46ce1e16c870 2182 LD_DWORD(fs->win+FSI_StrucSig) == 0x61417272) {
emilmont 1:46ce1e16c870 2183 fs->last_clust = LD_DWORD(fs->win+FSI_Nxt_Free);
emilmont 1:46ce1e16c870 2184 fs->free_clust = LD_DWORD(fs->win+FSI_Free_Count);
emilmont 1:46ce1e16c870 2185 }
emilmont 1:46ce1e16c870 2186 }
emilmont 1:46ce1e16c870 2187 #endif
emilmont 1:46ce1e16c870 2188 fs->fs_type = fmt; /* FAT sub-type */
emilmont 1:46ce1e16c870 2189 fs->id = ++Fsid; /* File system mount ID */
emilmont 1:46ce1e16c870 2190 fs->winsect = 0; /* Invalidate sector cache */
emilmont 1:46ce1e16c870 2191 fs->wflag = 0;
emilmont 1:46ce1e16c870 2192 #if _FS_RPATH
emilmont 1:46ce1e16c870 2193 fs->cdir = 0; /* Current directory (root dir) */
emilmont 1:46ce1e16c870 2194 #endif
emilmont 1:46ce1e16c870 2195 #if _FS_LOCK /* Clear file lock semaphores */
emilmont 1:46ce1e16c870 2196 clear_lock(fs);
emilmont 1:46ce1e16c870 2197 #endif
emilmont 1:46ce1e16c870 2198
emilmont 1:46ce1e16c870 2199 return FR_OK;
emilmont 1:46ce1e16c870 2200 }
emilmont 1:46ce1e16c870 2201
emilmont 1:46ce1e16c870 2202
emilmont 1:46ce1e16c870 2203
emilmont 1:46ce1e16c870 2204
emilmont 1:46ce1e16c870 2205 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2206 /* Check if the file/dir object is valid or not */
emilmont 1:46ce1e16c870 2207 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2208
emilmont 1:46ce1e16c870 2209 static
emilmont 1:46ce1e16c870 2210 FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */
emilmont 1:46ce1e16c870 2211 void* obj /* Pointer to the object FIL/DIR to check validity */
emilmont 1:46ce1e16c870 2212 )
emilmont 1:46ce1e16c870 2213 {
emilmont 1:46ce1e16c870 2214 FIL *fil;
emilmont 1:46ce1e16c870 2215
emilmont 1:46ce1e16c870 2216
emilmont 1:46ce1e16c870 2217 fil = (FIL*)obj; /* Assuming offset of fs and id in the FIL/DIR is identical */
emilmont 1:46ce1e16c870 2218 if (!fil->fs || !fil->fs->fs_type || fil->fs->id != fil->id)
emilmont 1:46ce1e16c870 2219 return FR_INVALID_OBJECT;
emilmont 1:46ce1e16c870 2220
emilmont 1:46ce1e16c870 2221 ENTER_FF(fil->fs); /* Lock file system */
emilmont 1:46ce1e16c870 2222
emilmont 1:46ce1e16c870 2223 if (disk_status(fil->fs->drv) & STA_NOINIT)
emilmont 1:46ce1e16c870 2224 return FR_NOT_READY;
emilmont 1:46ce1e16c870 2225
emilmont 1:46ce1e16c870 2226 return FR_OK;
emilmont 1:46ce1e16c870 2227 }
emilmont 1:46ce1e16c870 2228
emilmont 1:46ce1e16c870 2229
emilmont 1:46ce1e16c870 2230
emilmont 1:46ce1e16c870 2231
emilmont 1:46ce1e16c870 2232 /*--------------------------------------------------------------------------
emilmont 1:46ce1e16c870 2233
emilmont 1:46ce1e16c870 2234 Public Functions
emilmont 1:46ce1e16c870 2235
emilmont 1:46ce1e16c870 2236 --------------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2237
emilmont 1:46ce1e16c870 2238
emilmont 1:46ce1e16c870 2239
emilmont 1:46ce1e16c870 2240 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2241 /* Mount/Unmount a Logical Drive */
emilmont 1:46ce1e16c870 2242 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2243
emilmont 1:46ce1e16c870 2244 FRESULT f_mount (
emilmont 1:46ce1e16c870 2245 BYTE vol, /* Logical drive number to be mounted/unmounted */
emilmont 1:46ce1e16c870 2246 FATFS *fs /* Pointer to new file system object (NULL for unmount)*/
emilmont 1:46ce1e16c870 2247 )
emilmont 1:46ce1e16c870 2248 {
emilmont 1:46ce1e16c870 2249 FATFS *rfs;
emilmont 1:46ce1e16c870 2250
emilmont 1:46ce1e16c870 2251
emilmont 1:46ce1e16c870 2252 if (vol >= _VOLUMES) /* Check if the drive number is valid */
emilmont 1:46ce1e16c870 2253 return FR_INVALID_DRIVE;
emilmont 1:46ce1e16c870 2254 rfs = FatFs[vol]; /* Get current fs object */
emilmont 1:46ce1e16c870 2255
emilmont 1:46ce1e16c870 2256 if (rfs) {
emilmont 1:46ce1e16c870 2257 #if _FS_LOCK
emilmont 1:46ce1e16c870 2258 clear_lock(rfs);
emilmont 1:46ce1e16c870 2259 #endif
emilmont 1:46ce1e16c870 2260 #if _FS_REENTRANT /* Discard sync object of the current volume */
emilmont 1:46ce1e16c870 2261 if (!ff_del_syncobj(rfs->sobj)) return FR_INT_ERR;
emilmont 1:46ce1e16c870 2262 #endif
emilmont 1:46ce1e16c870 2263 rfs->fs_type = 0; /* Clear old fs object */
emilmont 1:46ce1e16c870 2264 }
emilmont 1:46ce1e16c870 2265
emilmont 1:46ce1e16c870 2266 if (fs) {
emilmont 1:46ce1e16c870 2267 fs->fs_type = 0; /* Clear new fs object */
emilmont 1:46ce1e16c870 2268 #if _FS_REENTRANT /* Create sync object for the new volume */
emilmont 1:46ce1e16c870 2269 if (!ff_cre_syncobj(vol, &fs->sobj)) return FR_INT_ERR;
emilmont 1:46ce1e16c870 2270 #endif
emilmont 1:46ce1e16c870 2271 }
emilmont 1:46ce1e16c870 2272 FatFs[vol] = fs; /* Register new fs object */
emilmont 1:46ce1e16c870 2273
emilmont 1:46ce1e16c870 2274 return FR_OK;
emilmont 1:46ce1e16c870 2275 }
emilmont 1:46ce1e16c870 2276
emilmont 1:46ce1e16c870 2277
emilmont 1:46ce1e16c870 2278
emilmont 1:46ce1e16c870 2279
emilmont 1:46ce1e16c870 2280 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2281 /* Open or Create a File */
emilmont 1:46ce1e16c870 2282 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2283
emilmont 1:46ce1e16c870 2284 FRESULT f_open (
emilmont 1:46ce1e16c870 2285 FIL *fp, /* Pointer to the blank file object */
emilmont 1:46ce1e16c870 2286 const TCHAR *path, /* Pointer to the file name */
emilmont 1:46ce1e16c870 2287 BYTE mode /* Access mode and file open mode flags */
emilmont 1:46ce1e16c870 2288 )
emilmont 1:46ce1e16c870 2289 {
emilmont 1:46ce1e16c870 2290 FRESULT res;
emilmont 1:46ce1e16c870 2291 FATFS_DIR dj;
emilmont 1:46ce1e16c870 2292 BYTE *dir;
emilmont 1:46ce1e16c870 2293 DEF_NAMEBUF;
emilmont 1:46ce1e16c870 2294
emilmont 1:46ce1e16c870 2295
emilmont 1:46ce1e16c870 2296 if (!fp) return FR_INVALID_OBJECT;
emilmont 1:46ce1e16c870 2297 fp->fs = 0; /* Clear file object */
emilmont 1:46ce1e16c870 2298
emilmont 1:46ce1e16c870 2299 #if !_FS_READONLY
emilmont 1:46ce1e16c870 2300 mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW;
emilmont 1:46ce1e16c870 2301 res = chk_mounted(&path, &dj.fs, (BYTE)(mode & ~FA_READ));
emilmont 1:46ce1e16c870 2302 #else
emilmont 1:46ce1e16c870 2303 mode &= FA_READ;
emilmont 1:46ce1e16c870 2304 res = chk_mounted(&path, &dj.fs, 0);
emilmont 1:46ce1e16c870 2305 #endif
emilmont 1:46ce1e16c870 2306 if (res == FR_OK) {
emilmont 1:46ce1e16c870 2307 INIT_BUF(dj);
emilmont 1:46ce1e16c870 2308 res = follow_path(&dj, path); /* Follow the file path */
emilmont 1:46ce1e16c870 2309 dir = dj.dir;
emilmont 1:46ce1e16c870 2310 #if !_FS_READONLY /* R/W configuration */
emilmont 1:46ce1e16c870 2311 if (res == FR_OK) {
emilmont 1:46ce1e16c870 2312 if (!dir) /* Current dir itself */
emilmont 1:46ce1e16c870 2313 res = FR_INVALID_NAME;
emilmont 1:46ce1e16c870 2314 #if _FS_LOCK
emilmont 1:46ce1e16c870 2315 else
emilmont 1:46ce1e16c870 2316 res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
emilmont 1:46ce1e16c870 2317 #endif
emilmont 1:46ce1e16c870 2318 }
emilmont 1:46ce1e16c870 2319 /* Create or Open a file */
emilmont 1:46ce1e16c870 2320 if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
emilmont 1:46ce1e16c870 2321 DWORD dw, cl;
emilmont 1:46ce1e16c870 2322
emilmont 1:46ce1e16c870 2323 if (res != FR_OK) { /* No file, create new */
emilmont 1:46ce1e16c870 2324 if (res == FR_NO_FILE) /* There is no file to open, create a new entry */
emilmont 1:46ce1e16c870 2325 #if _FS_LOCK
emilmont 1:46ce1e16c870 2326 res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
emilmont 1:46ce1e16c870 2327 #else
emilmont 1:46ce1e16c870 2328 res = dir_register(&dj);
emilmont 1:46ce1e16c870 2329 #endif
emilmont 1:46ce1e16c870 2330 mode |= FA_CREATE_ALWAYS; /* File is created */
emilmont 1:46ce1e16c870 2331 dir = dj.dir; /* New entry */
emilmont 1:46ce1e16c870 2332 }
emilmont 1:46ce1e16c870 2333 else { /* Any object is already existing */
emilmont 1:46ce1e16c870 2334 if (dir[DIR_Attr] & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or DIR) */
emilmont 1:46ce1e16c870 2335 res = FR_DENIED;
emilmont 1:46ce1e16c870 2336 } else {
emilmont 1:46ce1e16c870 2337 if (mode & FA_CREATE_NEW) /* Cannot create as new file */
emilmont 1:46ce1e16c870 2338 res = FR_EXIST;
emilmont 1:46ce1e16c870 2339 }
emilmont 1:46ce1e16c870 2340 }
emilmont 1:46ce1e16c870 2341 if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate it if overwrite mode */
emilmont 1:46ce1e16c870 2342 dw = get_fattime(); /* Created time */
emilmont 1:46ce1e16c870 2343 ST_DWORD(dir+DIR_CrtTime, dw);
emilmont 1:46ce1e16c870 2344 dir[DIR_Attr] = 0; /* Reset attribute */
emilmont 1:46ce1e16c870 2345 ST_DWORD(dir+DIR_FileSize, 0); /* size = 0 */
emilmont 1:46ce1e16c870 2346 cl = ld_clust(dj.fs, dir); /* Get start cluster */
emilmont 1:46ce1e16c870 2347 st_clust(dir, 0); /* cluster = 0 */
emilmont 1:46ce1e16c870 2348 dj.fs->wflag = 1;
emilmont 1:46ce1e16c870 2349 if (cl) { /* Remove the cluster chain if exist */
emilmont 1:46ce1e16c870 2350 dw = dj.fs->winsect;
emilmont 1:46ce1e16c870 2351 res = remove_chain(dj.fs, cl);
emilmont 1:46ce1e16c870 2352 if (res == FR_OK) {
emilmont 1:46ce1e16c870 2353 dj.fs->last_clust = cl - 1; /* Reuse the cluster hole */
emilmont 1:46ce1e16c870 2354 res = move_window(dj.fs, dw);
emilmont 1:46ce1e16c870 2355 }
emilmont 1:46ce1e16c870 2356 }
emilmont 1:46ce1e16c870 2357 }
emilmont 1:46ce1e16c870 2358 }
emilmont 1:46ce1e16c870 2359 else { /* Open an existing file */
emilmont 1:46ce1e16c870 2360 if (res == FR_OK) { /* Follow succeeded */
emilmont 1:46ce1e16c870 2361 if (dir[DIR_Attr] & AM_DIR) { /* It is a directory */
emilmont 1:46ce1e16c870 2362 res = FR_NO_FILE;
emilmont 1:46ce1e16c870 2363 } else {
emilmont 1:46ce1e16c870 2364 if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
emilmont 1:46ce1e16c870 2365 res = FR_DENIED;
emilmont 1:46ce1e16c870 2366 }
emilmont 1:46ce1e16c870 2367 }
emilmont 1:46ce1e16c870 2368 }
emilmont 1:46ce1e16c870 2369 if (res == FR_OK) {
emilmont 1:46ce1e16c870 2370 if (mode & FA_CREATE_ALWAYS) /* Set file change flag if created or overwritten */
emilmont 1:46ce1e16c870 2371 mode |= FA__WRITTEN;
emilmont 1:46ce1e16c870 2372 fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
emilmont 1:46ce1e16c870 2373 fp->dir_ptr = dir;
emilmont 1:46ce1e16c870 2374 #if _FS_LOCK
emilmont 1:46ce1e16c870 2375 fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
emilmont 1:46ce1e16c870 2376 if (!fp->lockid) res = FR_INT_ERR;
emilmont 1:46ce1e16c870 2377 #endif
emilmont 1:46ce1e16c870 2378 }
emilmont 1:46ce1e16c870 2379
emilmont 1:46ce1e16c870 2380 #else /* R/O configuration */
emilmont 1:46ce1e16c870 2381 if (res == FR_OK) { /* Follow succeeded */
emilmont 1:46ce1e16c870 2382 dir = dj.dir;
emilmont 1:46ce1e16c870 2383 if (!dir) { /* Current dir itself */
emilmont 1:46ce1e16c870 2384 res = FR_INVALID_NAME;
emilmont 1:46ce1e16c870 2385 } else {
emilmont 1:46ce1e16c870 2386 if (dir[DIR_Attr] & AM_DIR) /* It is a directory */
emilmont 1:46ce1e16c870 2387 res = FR_NO_FILE;
emilmont 1:46ce1e16c870 2388 }
emilmont 1:46ce1e16c870 2389 }
emilmont 1:46ce1e16c870 2390 #endif
emilmont 1:46ce1e16c870 2391 FREE_BUF();
emilmont 1:46ce1e16c870 2392
emilmont 1:46ce1e16c870 2393 if (res == FR_OK) {
emilmont 1:46ce1e16c870 2394 fp->flag = mode; /* File access mode */
emilmont 1:46ce1e16c870 2395 fp->sclust = ld_clust(dj.fs, dir); /* File start cluster */
emilmont 1:46ce1e16c870 2396 fp->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */
emilmont 1:46ce1e16c870 2397 fp->fptr = 0; /* File pointer */
emilmont 1:46ce1e16c870 2398 fp->dsect = 0;
emilmont 1:46ce1e16c870 2399 #if _USE_FASTSEEK
emilmont 1:46ce1e16c870 2400 fp->cltbl = 0; /* Normal seek mode */
emilmont 1:46ce1e16c870 2401 #endif
emilmont 1:46ce1e16c870 2402 fp->fs = dj.fs; fp->id = dj.fs->id; /* Validate file object */
emilmont 1:46ce1e16c870 2403 }
emilmont 1:46ce1e16c870 2404 }
emilmont 1:46ce1e16c870 2405
emilmont 1:46ce1e16c870 2406 LEAVE_FF(dj.fs, res);
emilmont 1:46ce1e16c870 2407 }
emilmont 1:46ce1e16c870 2408
emilmont 1:46ce1e16c870 2409
emilmont 1:46ce1e16c870 2410
emilmont 1:46ce1e16c870 2411
emilmont 1:46ce1e16c870 2412 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2413 /* Read File */
emilmont 1:46ce1e16c870 2414 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2415
emilmont 1:46ce1e16c870 2416 FRESULT f_read (
emilmont 1:46ce1e16c870 2417 FIL *fp, /* Pointer to the file object */
emilmont 1:46ce1e16c870 2418 void *buff, /* Pointer to data buffer */
emilmont 1:46ce1e16c870 2419 UINT btr, /* Number of bytes to read */
emilmont 1:46ce1e16c870 2420 UINT *br /* Pointer to number of bytes read */
emilmont 1:46ce1e16c870 2421 )
emilmont 1:46ce1e16c870 2422 {
emilmont 1:46ce1e16c870 2423 FRESULT res;
emilmont 1:46ce1e16c870 2424 DWORD clst, sect, remain;
emilmont 1:46ce1e16c870 2425 UINT rcnt, cc;
emilmont 1:46ce1e16c870 2426 BYTE csect, *rbuff = (BYTE *)buff;
emilmont 1:46ce1e16c870 2427
emilmont 1:46ce1e16c870 2428
emilmont 1:46ce1e16c870 2429 *br = 0; /* Clear read byte counter */
emilmont 1:46ce1e16c870 2430
emilmont 1:46ce1e16c870 2431 res = validate(fp); /* Check validity */
emilmont 1:46ce1e16c870 2432 if (res != FR_OK) LEAVE_FF(fp->fs, res);
emilmont 1:46ce1e16c870 2433 if (fp->flag & FA__ERROR) /* Aborted file? */
emilmont 1:46ce1e16c870 2434 LEAVE_FF(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2435 if (!(fp->flag & FA_READ)) /* Check access mode */
emilmont 1:46ce1e16c870 2436 LEAVE_FF(fp->fs, FR_DENIED);
emilmont 1:46ce1e16c870 2437 remain = fp->fsize - fp->fptr;
emilmont 1:46ce1e16c870 2438 if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
emilmont 1:46ce1e16c870 2439
emilmont 1:46ce1e16c870 2440 for ( ; btr; /* Repeat until all data read */
emilmont 1:46ce1e16c870 2441 rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
emilmont 1:46ce1e16c870 2442 if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
emilmont 1:46ce1e16c870 2443 csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
emilmont 1:46ce1e16c870 2444 if (!csect) { /* On the cluster boundary? */
emilmont 1:46ce1e16c870 2445 if (fp->fptr == 0) { /* On the top of the file? */
emilmont 1:46ce1e16c870 2446 clst = fp->sclust; /* Follow from the origin */
emilmont 1:46ce1e16c870 2447 } else { /* Middle or end of the file */
emilmont 1:46ce1e16c870 2448 #if _USE_FASTSEEK
emilmont 1:46ce1e16c870 2449 if (fp->cltbl)
emilmont 1:46ce1e16c870 2450 clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
emilmont 1:46ce1e16c870 2451 else
emilmont 1:46ce1e16c870 2452 #endif
emilmont 1:46ce1e16c870 2453 clst = get_fat(fp->fs, fp->clust); /* Follow cluster chain on the FAT */
emilmont 1:46ce1e16c870 2454 }
emilmont 1:46ce1e16c870 2455 if (clst < 2) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2456 if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2457 fp->clust = clst; /* Update current cluster */
emilmont 1:46ce1e16c870 2458 }
emilmont 1:46ce1e16c870 2459 sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
emilmont 1:46ce1e16c870 2460 if (!sect) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2461 sect += csect;
emilmont 1:46ce1e16c870 2462 cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */
emilmont 1:46ce1e16c870 2463 if (cc) { /* Read maximum contiguous sectors directly */
emilmont 1:46ce1e16c870 2464 if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
emilmont 1:46ce1e16c870 2465 cc = fp->fs->csize - csect;
emilmont 1:46ce1e16c870 2466 if (disk_read(fp->fs->drv, rbuff, sect, (BYTE)cc) != RES_OK)
emilmont 1:46ce1e16c870 2467 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2468 #if !_FS_READONLY && _FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */
emilmont 1:46ce1e16c870 2469 #if _FS_TINY
emilmont 1:46ce1e16c870 2470 if (fp->fs->wflag && fp->fs->winsect - sect < cc)
emilmont 1:46ce1e16c870 2471 mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));
emilmont 1:46ce1e16c870 2472 #else
emilmont 1:46ce1e16c870 2473 if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc)
emilmont 1:46ce1e16c870 2474 mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));
emilmont 1:46ce1e16c870 2475 #endif
emilmont 1:46ce1e16c870 2476 #endif
emilmont 1:46ce1e16c870 2477 rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
emilmont 1:46ce1e16c870 2478 continue;
emilmont 1:46ce1e16c870 2479 }
emilmont 1:46ce1e16c870 2480 #if !_FS_TINY
emilmont 1:46ce1e16c870 2481 if (fp->dsect != sect) { /* Load data sector if not in cache */
emilmont 1:46ce1e16c870 2482 #if !_FS_READONLY
emilmont 1:46ce1e16c870 2483 if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
emilmont 1:46ce1e16c870 2484 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
emilmont 1:46ce1e16c870 2485 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2486 fp->flag &= ~FA__DIRTY;
emilmont 1:46ce1e16c870 2487 }
emilmont 1:46ce1e16c870 2488 #endif
emilmont 1:46ce1e16c870 2489 if (disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK) /* Fill sector cache */
emilmont 1:46ce1e16c870 2490 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2491 }
emilmont 1:46ce1e16c870 2492 #endif
emilmont 1:46ce1e16c870 2493 fp->dsect = sect;
emilmont 1:46ce1e16c870 2494 }
emilmont 1:46ce1e16c870 2495 rcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs)); /* Get partial sector data from sector buffer */
emilmont 1:46ce1e16c870 2496 if (rcnt > btr) rcnt = btr;
emilmont 1:46ce1e16c870 2497 #if _FS_TINY
emilmont 1:46ce1e16c870 2498 if (move_window(fp->fs, fp->dsect)) /* Move sector window */
emilmont 1:46ce1e16c870 2499 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2500 mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
emilmont 1:46ce1e16c870 2501 #else
emilmont 1:46ce1e16c870 2502 mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
emilmont 1:46ce1e16c870 2503 #endif
emilmont 1:46ce1e16c870 2504 }
emilmont 1:46ce1e16c870 2505
emilmont 1:46ce1e16c870 2506 LEAVE_FF(fp->fs, FR_OK);
emilmont 1:46ce1e16c870 2507 }
emilmont 1:46ce1e16c870 2508
emilmont 1:46ce1e16c870 2509
emilmont 1:46ce1e16c870 2510
emilmont 1:46ce1e16c870 2511
emilmont 1:46ce1e16c870 2512 #if !_FS_READONLY
emilmont 1:46ce1e16c870 2513 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2514 /* Write File */
emilmont 1:46ce1e16c870 2515 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2516
emilmont 1:46ce1e16c870 2517 FRESULT f_write (
emilmont 1:46ce1e16c870 2518 FIL *fp, /* Pointer to the file object */
emilmont 1:46ce1e16c870 2519 const void *buff, /* Pointer to the data to be written */
emilmont 1:46ce1e16c870 2520 UINT btw, /* Number of bytes to write */
emilmont 1:46ce1e16c870 2521 UINT *bw /* Pointer to number of bytes written */
emilmont 1:46ce1e16c870 2522 )
emilmont 1:46ce1e16c870 2523 {
emilmont 1:46ce1e16c870 2524 FRESULT res;
emilmont 1:46ce1e16c870 2525 DWORD clst, sect;
emilmont 1:46ce1e16c870 2526 UINT wcnt, cc;
emilmont 1:46ce1e16c870 2527 const BYTE *wbuff = (const BYTE *)buff;
emilmont 1:46ce1e16c870 2528 BYTE csect;
emilmont 1:46ce1e16c870 2529
emilmont 1:46ce1e16c870 2530
emilmont 1:46ce1e16c870 2531 *bw = 0; /* Clear write byte counter */
emilmont 1:46ce1e16c870 2532
emilmont 1:46ce1e16c870 2533 res = validate(fp); /* Check validity */
emilmont 1:46ce1e16c870 2534 if (res != FR_OK) LEAVE_FF(fp->fs, res);
emilmont 1:46ce1e16c870 2535 if (fp->flag & FA__ERROR) /* Aborted file? */
emilmont 1:46ce1e16c870 2536 LEAVE_FF(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2537 if (!(fp->flag & FA_WRITE)) /* Check access mode */
emilmont 1:46ce1e16c870 2538 LEAVE_FF(fp->fs, FR_DENIED);
emilmont 1:46ce1e16c870 2539 if ((DWORD)(fp->fsize + btw) < fp->fsize) btw = 0; /* File size cannot reach 4GB */
emilmont 1:46ce1e16c870 2540
emilmont 1:46ce1e16c870 2541 for ( ; btw; /* Repeat until all data written */
emilmont 1:46ce1e16c870 2542 wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
emilmont 1:46ce1e16c870 2543 if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
emilmont 1:46ce1e16c870 2544 csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
emilmont 1:46ce1e16c870 2545 if (!csect) { /* On the cluster boundary? */
emilmont 1:46ce1e16c870 2546 if (fp->fptr == 0) { /* On the top of the file? */
emilmont 1:46ce1e16c870 2547 clst = fp->sclust; /* Follow from the origin */
emilmont 1:46ce1e16c870 2548 if (clst == 0) /* When no cluster is allocated, */
emilmont 1:46ce1e16c870 2549 fp->sclust = clst = create_chain(fp->fs, 0); /* Create a new cluster chain */
emilmont 1:46ce1e16c870 2550 } else { /* Middle or end of the file */
emilmont 1:46ce1e16c870 2551 #if _USE_FASTSEEK
emilmont 1:46ce1e16c870 2552 if (fp->cltbl)
emilmont 1:46ce1e16c870 2553 clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
emilmont 1:46ce1e16c870 2554 else
emilmont 1:46ce1e16c870 2555 #endif
emilmont 1:46ce1e16c870 2556 clst = create_chain(fp->fs, fp->clust); /* Follow or stretch cluster chain on the FAT */
emilmont 1:46ce1e16c870 2557 }
emilmont 1:46ce1e16c870 2558 if (clst == 0) break; /* Could not allocate a new cluster (disk full) */
emilmont 1:46ce1e16c870 2559 if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2560 if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2561 fp->clust = clst; /* Update current cluster */
emilmont 1:46ce1e16c870 2562 }
emilmont 1:46ce1e16c870 2563 #if _FS_TINY
emilmont 1:46ce1e16c870 2564 if (fp->fs->winsect == fp->dsect && move_window(fp->fs, 0)) /* Write-back sector cache */
emilmont 1:46ce1e16c870 2565 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2566 #else
emilmont 1:46ce1e16c870 2567 if (fp->flag & FA__DIRTY) { /* Write-back sector cache */
emilmont 1:46ce1e16c870 2568 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
emilmont 1:46ce1e16c870 2569 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2570 fp->flag &= ~FA__DIRTY;
emilmont 1:46ce1e16c870 2571 }
emilmont 1:46ce1e16c870 2572 #endif
emilmont 1:46ce1e16c870 2573 sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
emilmont 1:46ce1e16c870 2574 if (!sect) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2575 sect += csect;
emilmont 1:46ce1e16c870 2576 cc = btw / SS(fp->fs); /* When remaining bytes >= sector size, */
emilmont 1:46ce1e16c870 2577 if (cc) { /* Write maximum contiguous sectors directly */
emilmont 1:46ce1e16c870 2578 if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
emilmont 1:46ce1e16c870 2579 cc = fp->fs->csize - csect;
emilmont 1:46ce1e16c870 2580 if (disk_write(fp->fs->drv, wbuff, sect, (BYTE)cc) != RES_OK)
emilmont 1:46ce1e16c870 2581 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2582 #if _FS_TINY
emilmont 1:46ce1e16c870 2583 if (fp->fs->winsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
emilmont 1:46ce1e16c870 2584 mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
emilmont 1:46ce1e16c870 2585 fp->fs->wflag = 0;
emilmont 1:46ce1e16c870 2586 }
emilmont 1:46ce1e16c870 2587 #else
emilmont 1:46ce1e16c870 2588 if (fp->dsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
emilmont 1:46ce1e16c870 2589 mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
emilmont 1:46ce1e16c870 2590 fp->flag &= ~FA__DIRTY;
emilmont 1:46ce1e16c870 2591 }
emilmont 1:46ce1e16c870 2592 #endif
emilmont 1:46ce1e16c870 2593 wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
emilmont 1:46ce1e16c870 2594 continue;
emilmont 1:46ce1e16c870 2595 }
emilmont 1:46ce1e16c870 2596 #if _FS_TINY
emilmont 1:46ce1e16c870 2597 if (fp->fptr >= fp->fsize) { /* Avoid silly cache filling at growing edge */
emilmont 1:46ce1e16c870 2598 if (move_window(fp->fs, 0)) ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2599 fp->fs->winsect = sect;
emilmont 1:46ce1e16c870 2600 }
emilmont 1:46ce1e16c870 2601 #else
emilmont 1:46ce1e16c870 2602 if (fp->dsect != sect) { /* Fill sector cache with file data */
emilmont 1:46ce1e16c870 2603 if (fp->fptr < fp->fsize &&
emilmont 1:46ce1e16c870 2604 disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK)
emilmont 1:46ce1e16c870 2605 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2606 }
emilmont 1:46ce1e16c870 2607 #endif
emilmont 1:46ce1e16c870 2608 fp->dsect = sect;
emilmont 1:46ce1e16c870 2609 }
emilmont 1:46ce1e16c870 2610 wcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs));/* Put partial sector into file I/O buffer */
emilmont 1:46ce1e16c870 2611 if (wcnt > btw) wcnt = btw;
emilmont 1:46ce1e16c870 2612 #if _FS_TINY
emilmont 1:46ce1e16c870 2613 if (move_window(fp->fs, fp->dsect)) /* Move sector window */
emilmont 1:46ce1e16c870 2614 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2615 mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
emilmont 1:46ce1e16c870 2616 fp->fs->wflag = 1;
emilmont 1:46ce1e16c870 2617 #else
emilmont 1:46ce1e16c870 2618 mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
emilmont 1:46ce1e16c870 2619 fp->flag |= FA__DIRTY;
emilmont 1:46ce1e16c870 2620 #endif
emilmont 1:46ce1e16c870 2621 }
emilmont 1:46ce1e16c870 2622
emilmont 1:46ce1e16c870 2623 if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
emilmont 1:46ce1e16c870 2624 fp->flag |= FA__WRITTEN; /* Set file change flag */
emilmont 1:46ce1e16c870 2625
emilmont 1:46ce1e16c870 2626 LEAVE_FF(fp->fs, FR_OK);
emilmont 1:46ce1e16c870 2627 }
emilmont 1:46ce1e16c870 2628
emilmont 1:46ce1e16c870 2629
emilmont 1:46ce1e16c870 2630
emilmont 1:46ce1e16c870 2631
emilmont 1:46ce1e16c870 2632 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2633 /* Synchronize the File Object */
emilmont 1:46ce1e16c870 2634 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2635
emilmont 1:46ce1e16c870 2636 FRESULT f_sync (
emilmont 1:46ce1e16c870 2637 FIL *fp /* Pointer to the file object */
emilmont 1:46ce1e16c870 2638 )
emilmont 1:46ce1e16c870 2639 {
emilmont 1:46ce1e16c870 2640 FRESULT res;
emilmont 1:46ce1e16c870 2641 DWORD tim;
emilmont 1:46ce1e16c870 2642 BYTE *dir;
emilmont 1:46ce1e16c870 2643
emilmont 1:46ce1e16c870 2644
emilmont 1:46ce1e16c870 2645 res = validate(fp); /* Check validity of the object */
emilmont 1:46ce1e16c870 2646 if (res == FR_OK) {
emilmont 1:46ce1e16c870 2647 if (fp->flag & FA__WRITTEN) { /* Has the file been written? */
emilmont 1:46ce1e16c870 2648 #if !_FS_TINY /* Write-back dirty buffer */
emilmont 1:46ce1e16c870 2649 if (fp->flag & FA__DIRTY) {
emilmont 1:46ce1e16c870 2650 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
emilmont 1:46ce1e16c870 2651 LEAVE_FF(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2652 fp->flag &= ~FA__DIRTY;
emilmont 1:46ce1e16c870 2653 }
emilmont 1:46ce1e16c870 2654 #endif
emilmont 1:46ce1e16c870 2655 /* Update the directory entry */
emilmont 1:46ce1e16c870 2656 res = move_window(fp->fs, fp->dir_sect);
emilmont 1:46ce1e16c870 2657 if (res == FR_OK) {
emilmont 1:46ce1e16c870 2658 dir = fp->dir_ptr;
emilmont 1:46ce1e16c870 2659 dir[DIR_Attr] |= AM_ARC; /* Set archive bit */
emilmont 1:46ce1e16c870 2660 ST_DWORD(dir+DIR_FileSize, fp->fsize); /* Update file size */
emilmont 1:46ce1e16c870 2661 st_clust(dir, fp->sclust); /* Update start cluster */
emilmont 1:46ce1e16c870 2662 tim = get_fattime(); /* Update updated time */
emilmont 1:46ce1e16c870 2663 ST_DWORD(dir+DIR_WrtTime, tim);
emilmont 1:46ce1e16c870 2664 ST_WORD(dir+DIR_LstAccDate, 0);
emilmont 1:46ce1e16c870 2665 fp->flag &= ~FA__WRITTEN;
emilmont 1:46ce1e16c870 2666 fp->fs->wflag = 1;
emilmont 1:46ce1e16c870 2667 res = sync(fp->fs);
emilmont 1:46ce1e16c870 2668 }
emilmont 1:46ce1e16c870 2669 }
emilmont 1:46ce1e16c870 2670 }
emilmont 1:46ce1e16c870 2671
emilmont 1:46ce1e16c870 2672 LEAVE_FF(fp->fs, res);
emilmont 1:46ce1e16c870 2673 }
emilmont 1:46ce1e16c870 2674
emilmont 1:46ce1e16c870 2675 #endif /* !_FS_READONLY */
emilmont 1:46ce1e16c870 2676
emilmont 1:46ce1e16c870 2677
emilmont 1:46ce1e16c870 2678
emilmont 1:46ce1e16c870 2679
emilmont 1:46ce1e16c870 2680 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2681 /* Close File */
emilmont 1:46ce1e16c870 2682 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2683
emilmont 1:46ce1e16c870 2684 FRESULT f_close (
emilmont 1:46ce1e16c870 2685 FIL *fp /* Pointer to the file object to be closed */
emilmont 1:46ce1e16c870 2686 )
emilmont 1:46ce1e16c870 2687 {
emilmont 1:46ce1e16c870 2688 FRESULT res;
emilmont 1:46ce1e16c870 2689
emilmont 1:46ce1e16c870 2690
emilmont 1:46ce1e16c870 2691 #if _FS_READONLY
emilmont 1:46ce1e16c870 2692 res = validate(fp);
emilmont 1:46ce1e16c870 2693 {
emilmont 1:46ce1e16c870 2694 #if _FS_REENTRANT
emilmont 1:46ce1e16c870 2695 FATFS *fs = fp->fs;
emilmont 1:46ce1e16c870 2696 #endif
emilmont 1:46ce1e16c870 2697 if (res == FR_OK) fp->fs = 0; /* Discard file object */
emilmont 1:46ce1e16c870 2698 LEAVE_FF(fs, res);
emilmont 1:46ce1e16c870 2699 }
emilmont 1:46ce1e16c870 2700 #else
emilmont 1:46ce1e16c870 2701 res = f_sync(fp); /* Flush cached data */
emilmont 1:46ce1e16c870 2702 #if _FS_LOCK
emilmont 1:46ce1e16c870 2703 if (res == FR_OK) { /* Decrement open counter */
emilmont 1:46ce1e16c870 2704 #if _FS_REENTRANT
emilmont 1:46ce1e16c870 2705 FATFS *fs = fp->fs;;
emilmont 1:46ce1e16c870 2706 res = validate(fp);
emilmont 1:46ce1e16c870 2707 if (res == FR_OK) {
emilmont 1:46ce1e16c870 2708 res = dec_lock(fp->lockid);
emilmont 1:46ce1e16c870 2709 unlock_fs(fs, FR_OK);
emilmont 1:46ce1e16c870 2710 }
emilmont 1:46ce1e16c870 2711 #else
emilmont 1:46ce1e16c870 2712 res = dec_lock(fp->lockid);
emilmont 1:46ce1e16c870 2713 #endif
emilmont 1:46ce1e16c870 2714 }
emilmont 1:46ce1e16c870 2715 #endif
emilmont 1:46ce1e16c870 2716 if (res == FR_OK) fp->fs = 0; /* Discard file object */
emilmont 1:46ce1e16c870 2717 return res;
emilmont 1:46ce1e16c870 2718 #endif
emilmont 1:46ce1e16c870 2719 }
emilmont 1:46ce1e16c870 2720
emilmont 1:46ce1e16c870 2721
emilmont 1:46ce1e16c870 2722
emilmont 1:46ce1e16c870 2723
emilmont 1:46ce1e16c870 2724 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2725 /* Current Drive/Directory Handlings */
emilmont 1:46ce1e16c870 2726 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2727
emilmont 1:46ce1e16c870 2728 #if _FS_RPATH >= 1
emilmont 1:46ce1e16c870 2729
emilmont 1:46ce1e16c870 2730 FRESULT f_chdrive (
emilmont 1:46ce1e16c870 2731 BYTE drv /* Drive number */
emilmont 1:46ce1e16c870 2732 )
emilmont 1:46ce1e16c870 2733 {
emilmont 1:46ce1e16c870 2734 if (drv >= _VOLUMES) return FR_INVALID_DRIVE;
emilmont 1:46ce1e16c870 2735
emilmont 1:46ce1e16c870 2736 CurrVol = drv;
emilmont 1:46ce1e16c870 2737
emilmont 1:46ce1e16c870 2738 return FR_OK;
emilmont 1:46ce1e16c870 2739 }
emilmont 1:46ce1e16c870 2740
emilmont 1:46ce1e16c870 2741
emilmont 1:46ce1e16c870 2742
emilmont 1:46ce1e16c870 2743 FRESULT f_chdir (
emilmont 1:46ce1e16c870 2744 const TCHAR *path /* Pointer to the directory path */
emilmont 1:46ce1e16c870 2745 )
emilmont 1:46ce1e16c870 2746 {
emilmont 1:46ce1e16c870 2747 FRESULT res;
emilmont 1:46ce1e16c870 2748 DIR dj;
emilmont 1:46ce1e16c870 2749 DEF_NAMEBUF;
emilmont 1:46ce1e16c870 2750
emilmont 1:46ce1e16c870 2751
emilmont 1:46ce1e16c870 2752 res = chk_mounted(&path, &dj.fs, 0);
emilmont 1:46ce1e16c870 2753 if (res == FR_OK) {
emilmont 1:46ce1e16c870 2754 INIT_BUF(dj);
emilmont 1:46ce1e16c870 2755 res = follow_path(&dj, path); /* Follow the path */
emilmont 1:46ce1e16c870 2756 FREE_BUF();
emilmont 1:46ce1e16c870 2757 if (res == FR_OK) { /* Follow completed */
emilmont 1:46ce1e16c870 2758 if (!dj.dir) {
emilmont 1:46ce1e16c870 2759 dj.fs->cdir = dj.sclust; /* Start directory itself */
emilmont 1:46ce1e16c870 2760 } else {
emilmont 1:46ce1e16c870 2761 if (dj.dir[DIR_Attr] & AM_DIR) /* Reached to the directory */
emilmont 1:46ce1e16c870 2762 dj.fs->cdir = ld_clust(dj.fs, dj.dir);
emilmont 1:46ce1e16c870 2763 else
emilmont 1:46ce1e16c870 2764 res = FR_NO_PATH; /* Reached but a file */
emilmont 1:46ce1e16c870 2765 }
emilmont 1:46ce1e16c870 2766 }
emilmont 1:46ce1e16c870 2767 if (res == FR_NO_FILE) res = FR_NO_PATH;
emilmont 1:46ce1e16c870 2768 }
emilmont 1:46ce1e16c870 2769
emilmont 1:46ce1e16c870 2770 LEAVE_FF(dj.fs, res);
emilmont 1:46ce1e16c870 2771 }
emilmont 1:46ce1e16c870 2772
emilmont 1:46ce1e16c870 2773
emilmont 1:46ce1e16c870 2774 #if _FS_RPATH >= 2
emilmont 1:46ce1e16c870 2775 FRESULT f_getcwd (
emilmont 1:46ce1e16c870 2776 TCHAR *path, /* Pointer to the directory path */
emilmont 1:46ce1e16c870 2777 UINT sz_path /* Size of path */
emilmont 1:46ce1e16c870 2778 )
emilmont 1:46ce1e16c870 2779 {
emilmont 1:46ce1e16c870 2780 FRESULT res;
emilmont 1:46ce1e16c870 2781 DIR dj;
emilmont 1:46ce1e16c870 2782 UINT i, n;
emilmont 1:46ce1e16c870 2783 DWORD ccl;
emilmont 1:46ce1e16c870 2784 TCHAR *tp;
emilmont 1:46ce1e16c870 2785 FILINFO fno;
emilmont 1:46ce1e16c870 2786 DEF_NAMEBUF;
emilmont 1:46ce1e16c870 2787
emilmont 1:46ce1e16c870 2788
emilmont 1:46ce1e16c870 2789 *path = 0;
emilmont 1:46ce1e16c870 2790 res = chk_mounted((const TCHAR**)&path, &dj.fs, 0); /* Get current volume */
emilmont 1:46ce1e16c870 2791 if (res == FR_OK) {
emilmont 1:46ce1e16c870 2792 INIT_BUF(dj);
emilmont 1:46ce1e16c870 2793 i = sz_path; /* Bottom of buffer (dir stack base) */
emilmont 1:46ce1e16c870 2794 dj.sclust = dj.fs->cdir; /* Start to follow upper dir from current dir */
emilmont 1:46ce1e16c870 2795 while ((ccl = dj.sclust) != 0) { /* Repeat while current dir is a sub-dir */
emilmont 1:46ce1e16c870 2796 res = dir_sdi(&dj, 1); /* Get parent dir */
emilmont 1:46ce1e16c870 2797 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 2798 res = dir_read(&dj);
emilmont 1:46ce1e16c870 2799 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 2800 dj.sclust = ld_clust(dj.fs, dj.dir); /* Goto parent dir */
emilmont 1:46ce1e16c870 2801 res = dir_sdi(&dj, 0);
emilmont 1:46ce1e16c870 2802 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 2803 do { /* Find the entry links to the child dir */
emilmont 1:46ce1e16c870 2804 res = dir_read(&dj);
emilmont 1:46ce1e16c870 2805 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 2806 if (ccl == ld_clust(dj.fs, dj.dir)) break; /* Found the entry */
emilmont 1:46ce1e16c870 2807 res = dir_next(&dj, 0);
emilmont 1:46ce1e16c870 2808 } while (res == FR_OK);
emilmont 1:46ce1e16c870 2809 if (res == FR_NO_FILE) res = FR_INT_ERR;/* It cannot be 'not found'. */
emilmont 1:46ce1e16c870 2810 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 2811 #if _USE_LFN
emilmont 1:46ce1e16c870 2812 fno.lfname = path;
emilmont 1:46ce1e16c870 2813 fno.lfsize = i;
emilmont 1:46ce1e16c870 2814 #endif
emilmont 1:46ce1e16c870 2815 get_fileinfo(&dj, &fno); /* Get the dir name and push it to the buffer */
emilmont 1:46ce1e16c870 2816 tp = fno.fname;
emilmont 1:46ce1e16c870 2817 if (_USE_LFN && *path) tp = path;
emilmont 1:46ce1e16c870 2818 for (n = 0; tp[n]; n++) ;
emilmont 1:46ce1e16c870 2819 if (i < n + 3) {
emilmont 1:46ce1e16c870 2820 res = FR_NOT_ENOUGH_CORE; break;
emilmont 1:46ce1e16c870 2821 }
emilmont 1:46ce1e16c870 2822 while (n) path[--i] = tp[--n];
emilmont 1:46ce1e16c870 2823 path[--i] = '/';
emilmont 1:46ce1e16c870 2824 }
emilmont 1:46ce1e16c870 2825 tp = path;
emilmont 1:46ce1e16c870 2826 if (res == FR_OK) {
emilmont 1:46ce1e16c870 2827 *tp++ = '0' + CurrVol; /* Put drive number */
emilmont 1:46ce1e16c870 2828 *tp++ = ':';
emilmont 1:46ce1e16c870 2829 if (i == sz_path) { /* Root-dir */
emilmont 1:46ce1e16c870 2830 *tp++ = '/';
emilmont 1:46ce1e16c870 2831 } else { /* Sub-dir */
emilmont 1:46ce1e16c870 2832 do /* Add stacked path str */
emilmont 1:46ce1e16c870 2833 *tp++ = path[i++];
emilmont 1:46ce1e16c870 2834 while (i < sz_path);
emilmont 1:46ce1e16c870 2835 }
emilmont 1:46ce1e16c870 2836 }
emilmont 1:46ce1e16c870 2837 *tp = 0;
emilmont 1:46ce1e16c870 2838 FREE_BUF();
emilmont 1:46ce1e16c870 2839 }
emilmont 1:46ce1e16c870 2840
emilmont 1:46ce1e16c870 2841 LEAVE_FF(dj.fs, res);
emilmont 1:46ce1e16c870 2842 }
emilmont 1:46ce1e16c870 2843 #endif /* _FS_RPATH >= 2 */
emilmont 1:46ce1e16c870 2844 #endif /* _FS_RPATH >= 1 */
emilmont 1:46ce1e16c870 2845
emilmont 1:46ce1e16c870 2846
emilmont 1:46ce1e16c870 2847
emilmont 1:46ce1e16c870 2848 #if _FS_MINIMIZE <= 2
emilmont 1:46ce1e16c870 2849 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2850 /* Seek File R/W Pointer */
emilmont 1:46ce1e16c870 2851 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 2852
emilmont 1:46ce1e16c870 2853 FRESULT f_lseek (
emilmont 1:46ce1e16c870 2854 FIL *fp, /* Pointer to the file object */
emilmont 1:46ce1e16c870 2855 DWORD ofs /* File pointer from top of file */
emilmont 1:46ce1e16c870 2856 )
emilmont 1:46ce1e16c870 2857 {
emilmont 1:46ce1e16c870 2858 FRESULT res;
emilmont 1:46ce1e16c870 2859
emilmont 1:46ce1e16c870 2860
emilmont 1:46ce1e16c870 2861 res = validate(fp); /* Check validity of the object */
emilmont 1:46ce1e16c870 2862 if (res != FR_OK) LEAVE_FF(fp->fs, res);
emilmont 1:46ce1e16c870 2863 if (fp->flag & FA__ERROR) /* Check abort flag */
emilmont 1:46ce1e16c870 2864 LEAVE_FF(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2865
emilmont 1:46ce1e16c870 2866 #if _USE_FASTSEEK
emilmont 1:46ce1e16c870 2867 if (fp->cltbl) { /* Fast seek */
emilmont 1:46ce1e16c870 2868 DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;
emilmont 1:46ce1e16c870 2869
emilmont 1:46ce1e16c870 2870 if (ofs == CREATE_LINKMAP) { /* Create CLMT */
emilmont 1:46ce1e16c870 2871 tbl = fp->cltbl;
emilmont 1:46ce1e16c870 2872 tlen = *tbl++; ulen = 2; /* Given table size and required table size */
emilmont 1:46ce1e16c870 2873 cl = fp->sclust; /* Top of the chain */
emilmont 1:46ce1e16c870 2874 if (cl) {
emilmont 1:46ce1e16c870 2875 do {
emilmont 1:46ce1e16c870 2876 /* Get a fragment */
emilmont 1:46ce1e16c870 2877 tcl = cl; ncl = 0; ulen += 2; /* Top, length and used items */
emilmont 1:46ce1e16c870 2878 do {
emilmont 1:46ce1e16c870 2879 pcl = cl; ncl++;
emilmont 1:46ce1e16c870 2880 cl = get_fat(fp->fs, cl);
emilmont 1:46ce1e16c870 2881 if (cl <= 1) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2882 if (cl == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2883 } while (cl == pcl + 1);
emilmont 1:46ce1e16c870 2884 if (ulen <= tlen) { /* Store the length and top of the fragment */
emilmont 1:46ce1e16c870 2885 *tbl++ = ncl; *tbl++ = tcl;
emilmont 1:46ce1e16c870 2886 }
emilmont 1:46ce1e16c870 2887 } while (cl < fp->fs->n_fatent); /* Repeat until end of chain */
emilmont 1:46ce1e16c870 2888 }
emilmont 1:46ce1e16c870 2889 *fp->cltbl = ulen; /* Number of items used */
emilmont 1:46ce1e16c870 2890 if (ulen <= tlen)
emilmont 1:46ce1e16c870 2891 *tbl = 0; /* Terminate table */
emilmont 1:46ce1e16c870 2892 else
emilmont 1:46ce1e16c870 2893 res = FR_NOT_ENOUGH_CORE; /* Given table size is smaller than required */
emilmont 1:46ce1e16c870 2894
emilmont 1:46ce1e16c870 2895 } else { /* Fast seek */
emilmont 1:46ce1e16c870 2896 if (ofs > fp->fsize) /* Clip offset at the file size */
emilmont 1:46ce1e16c870 2897 ofs = fp->fsize;
emilmont 1:46ce1e16c870 2898 fp->fptr = ofs; /* Set file pointer */
emilmont 1:46ce1e16c870 2899 if (ofs) {
emilmont 1:46ce1e16c870 2900 fp->clust = clmt_clust(fp, ofs - 1);
emilmont 1:46ce1e16c870 2901 dsc = clust2sect(fp->fs, fp->clust);
emilmont 1:46ce1e16c870 2902 if (!dsc) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2903 dsc += (ofs - 1) / SS(fp->fs) & (fp->fs->csize - 1);
emilmont 1:46ce1e16c870 2904 if (fp->fptr % SS(fp->fs) && dsc != fp->dsect) { /* Refill sector cache if needed */
emilmont 1:46ce1e16c870 2905 #if !_FS_TINY
emilmont 1:46ce1e16c870 2906 #if !_FS_READONLY
emilmont 1:46ce1e16c870 2907 if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
emilmont 1:46ce1e16c870 2908 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
emilmont 1:46ce1e16c870 2909 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2910 fp->flag &= ~FA__DIRTY;
emilmont 1:46ce1e16c870 2911 }
emilmont 1:46ce1e16c870 2912 #endif
emilmont 1:46ce1e16c870 2913 if (disk_read(fp->fs->drv, fp->buf, dsc, 1) != RES_OK) /* Load current sector */
emilmont 1:46ce1e16c870 2914 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2915 #endif
emilmont 1:46ce1e16c870 2916 fp->dsect = dsc;
emilmont 1:46ce1e16c870 2917 }
emilmont 1:46ce1e16c870 2918 }
emilmont 1:46ce1e16c870 2919 }
emilmont 1:46ce1e16c870 2920 } else
emilmont 1:46ce1e16c870 2921 #endif
emilmont 1:46ce1e16c870 2922
emilmont 1:46ce1e16c870 2923 /* Normal Seek */
emilmont 1:46ce1e16c870 2924 {
emilmont 1:46ce1e16c870 2925 DWORD clst, bcs, nsect, ifptr;
emilmont 1:46ce1e16c870 2926
emilmont 1:46ce1e16c870 2927 if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */
emilmont 1:46ce1e16c870 2928 #if !_FS_READONLY
emilmont 1:46ce1e16c870 2929 && !(fp->flag & FA_WRITE)
emilmont 1:46ce1e16c870 2930 #endif
emilmont 1:46ce1e16c870 2931 ) ofs = fp->fsize;
emilmont 1:46ce1e16c870 2932
emilmont 1:46ce1e16c870 2933 ifptr = fp->fptr;
emilmont 1:46ce1e16c870 2934 fp->fptr = nsect = 0;
emilmont 1:46ce1e16c870 2935 if (ofs) {
emilmont 1:46ce1e16c870 2936 bcs = (DWORD)fp->fs->csize * SS(fp->fs); /* Cluster size (byte) */
emilmont 1:46ce1e16c870 2937 if (ifptr > 0 &&
emilmont 1:46ce1e16c870 2938 (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */
emilmont 1:46ce1e16c870 2939 fp->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */
emilmont 1:46ce1e16c870 2940 ofs -= fp->fptr;
emilmont 1:46ce1e16c870 2941 clst = fp->clust;
emilmont 1:46ce1e16c870 2942 } else { /* When seek to back cluster, */
emilmont 1:46ce1e16c870 2943 clst = fp->sclust; /* start from the first cluster */
emilmont 1:46ce1e16c870 2944 #if !_FS_READONLY
emilmont 1:46ce1e16c870 2945 if (clst == 0) { /* If no cluster chain, create a new chain */
emilmont 1:46ce1e16c870 2946 clst = create_chain(fp->fs, 0);
emilmont 1:46ce1e16c870 2947 if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2948 if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2949 fp->sclust = clst;
emilmont 1:46ce1e16c870 2950 }
emilmont 1:46ce1e16c870 2951 #endif
emilmont 1:46ce1e16c870 2952 fp->clust = clst;
emilmont 1:46ce1e16c870 2953 }
emilmont 1:46ce1e16c870 2954 if (clst != 0) {
emilmont 1:46ce1e16c870 2955 while (ofs > bcs) { /* Cluster following loop */
emilmont 1:46ce1e16c870 2956 #if !_FS_READONLY
emilmont 1:46ce1e16c870 2957 if (fp->flag & FA_WRITE) { /* Check if in write mode or not */
emilmont 1:46ce1e16c870 2958 clst = create_chain(fp->fs, clst); /* Force stretch if in write mode */
emilmont 1:46ce1e16c870 2959 if (clst == 0) { /* When disk gets full, clip file size */
emilmont 1:46ce1e16c870 2960 ofs = bcs; break;
emilmont 1:46ce1e16c870 2961 }
emilmont 1:46ce1e16c870 2962 } else
emilmont 1:46ce1e16c870 2963 #endif
emilmont 1:46ce1e16c870 2964 clst = get_fat(fp->fs, clst); /* Follow cluster chain if not in write mode */
emilmont 1:46ce1e16c870 2965 if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2966 if (clst <= 1 || clst >= fp->fs->n_fatent) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2967 fp->clust = clst;
emilmont 1:46ce1e16c870 2968 fp->fptr += bcs;
emilmont 1:46ce1e16c870 2969 ofs -= bcs;
emilmont 1:46ce1e16c870 2970 }
emilmont 1:46ce1e16c870 2971 fp->fptr += ofs;
emilmont 1:46ce1e16c870 2972 if (ofs % SS(fp->fs)) {
emilmont 1:46ce1e16c870 2973 nsect = clust2sect(fp->fs, clst); /* Current sector */
emilmont 1:46ce1e16c870 2974 if (!nsect) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 2975 nsect += ofs / SS(fp->fs);
emilmont 1:46ce1e16c870 2976 }
emilmont 1:46ce1e16c870 2977 }
emilmont 1:46ce1e16c870 2978 }
emilmont 1:46ce1e16c870 2979 if (fp->fptr % SS(fp->fs) && nsect != fp->dsect) { /* Fill sector cache if needed */
emilmont 1:46ce1e16c870 2980 #if !_FS_TINY
emilmont 1:46ce1e16c870 2981 #if !_FS_READONLY
emilmont 1:46ce1e16c870 2982 if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
emilmont 1:46ce1e16c870 2983 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
emilmont 1:46ce1e16c870 2984 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2985 fp->flag &= ~FA__DIRTY;
emilmont 1:46ce1e16c870 2986 }
emilmont 1:46ce1e16c870 2987 #endif
emilmont 1:46ce1e16c870 2988 if (disk_read(fp->fs->drv, fp->buf, nsect, 1) != RES_OK) /* Fill sector cache */
emilmont 1:46ce1e16c870 2989 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 2990 #endif
emilmont 1:46ce1e16c870 2991 fp->dsect = nsect;
emilmont 1:46ce1e16c870 2992 }
emilmont 1:46ce1e16c870 2993 #if !_FS_READONLY
emilmont 1:46ce1e16c870 2994 if (fp->fptr > fp->fsize) { /* Set file change flag if the file size is extended */
emilmont 1:46ce1e16c870 2995 fp->fsize = fp->fptr;
emilmont 1:46ce1e16c870 2996 fp->flag |= FA__WRITTEN;
emilmont 1:46ce1e16c870 2997 }
emilmont 1:46ce1e16c870 2998 #endif
emilmont 1:46ce1e16c870 2999 }
emilmont 1:46ce1e16c870 3000
emilmont 1:46ce1e16c870 3001 LEAVE_FF(fp->fs, res);
emilmont 1:46ce1e16c870 3002 }
emilmont 1:46ce1e16c870 3003
emilmont 1:46ce1e16c870 3004
emilmont 1:46ce1e16c870 3005
emilmont 1:46ce1e16c870 3006 #if _FS_MINIMIZE <= 1
emilmont 1:46ce1e16c870 3007 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3008 /* Create a Directory Object */
emilmont 1:46ce1e16c870 3009 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3010
emilmont 1:46ce1e16c870 3011 FRESULT f_opendir (
emilmont 1:46ce1e16c870 3012 FATFS_DIR *dj, /* Pointer to directory object to create */
emilmont 1:46ce1e16c870 3013 const TCHAR *path /* Pointer to the directory path */
emilmont 1:46ce1e16c870 3014 )
emilmont 1:46ce1e16c870 3015 {
emilmont 1:46ce1e16c870 3016 FRESULT res;
emilmont 1:46ce1e16c870 3017 FATFS *fs;
emilmont 1:46ce1e16c870 3018 DEF_NAMEBUF;
emilmont 1:46ce1e16c870 3019
emilmont 1:46ce1e16c870 3020
emilmont 1:46ce1e16c870 3021 if (!dj) return FR_INVALID_OBJECT;
emilmont 1:46ce1e16c870 3022
emilmont 1:46ce1e16c870 3023 res = chk_mounted(&path, &dj->fs, 0);
emilmont 1:46ce1e16c870 3024 fs = dj->fs;
emilmont 1:46ce1e16c870 3025 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3026 INIT_BUF(*dj);
emilmont 1:46ce1e16c870 3027 res = follow_path(dj, path); /* Follow the path to the directory */
emilmont 1:46ce1e16c870 3028 FREE_BUF();
emilmont 1:46ce1e16c870 3029 if (res == FR_OK) { /* Follow completed */
emilmont 1:46ce1e16c870 3030 if (dj->dir) { /* It is not the root dir */
emilmont 1:46ce1e16c870 3031 if (dj->dir[DIR_Attr] & AM_DIR) { /* The object is a directory */
emilmont 1:46ce1e16c870 3032 dj->sclust = ld_clust(fs, dj->dir);
emilmont 1:46ce1e16c870 3033 } else { /* The object is not a directory */
emilmont 1:46ce1e16c870 3034 res = FR_NO_PATH;
emilmont 1:46ce1e16c870 3035 }
emilmont 1:46ce1e16c870 3036 }
emilmont 1:46ce1e16c870 3037 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3038 dj->id = fs->id;
emilmont 1:46ce1e16c870 3039 res = dir_sdi(dj, 0); /* Rewind dir */
emilmont 1:46ce1e16c870 3040 }
emilmont 1:46ce1e16c870 3041 }
emilmont 1:46ce1e16c870 3042 if (res == FR_NO_FILE) res = FR_NO_PATH;
emilmont 1:46ce1e16c870 3043 if (res != FR_OK) dj->fs = 0; /* Invalidate the dir object if function faild */
emilmont 1:46ce1e16c870 3044 } else {
emilmont 1:46ce1e16c870 3045 dj->fs = 0;
emilmont 1:46ce1e16c870 3046 }
emilmont 1:46ce1e16c870 3047
emilmont 1:46ce1e16c870 3048 LEAVE_FF(fs, res);
emilmont 1:46ce1e16c870 3049 }
emilmont 1:46ce1e16c870 3050
emilmont 1:46ce1e16c870 3051
emilmont 1:46ce1e16c870 3052
emilmont 1:46ce1e16c870 3053
emilmont 1:46ce1e16c870 3054 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3055 /* Read Directory Entry in Sequence */
emilmont 1:46ce1e16c870 3056 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3057
emilmont 1:46ce1e16c870 3058 FRESULT f_readdir (
emilmont 1:46ce1e16c870 3059 FATFS_DIR *dj, /* Pointer to the open directory object */
emilmont 1:46ce1e16c870 3060 FILINFO *fno /* Pointer to file information to return */
emilmont 1:46ce1e16c870 3061 )
emilmont 1:46ce1e16c870 3062 {
emilmont 1:46ce1e16c870 3063 FRESULT res;
emilmont 1:46ce1e16c870 3064 DEF_NAMEBUF;
emilmont 1:46ce1e16c870 3065
emilmont 1:46ce1e16c870 3066
emilmont 1:46ce1e16c870 3067 res = validate(dj); /* Check validity of the object */
emilmont 1:46ce1e16c870 3068 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3069 if (!fno) {
emilmont 1:46ce1e16c870 3070 res = dir_sdi(dj, 0); /* Rewind the directory object */
emilmont 1:46ce1e16c870 3071 } else {
emilmont 1:46ce1e16c870 3072 INIT_BUF(*dj);
emilmont 1:46ce1e16c870 3073 res = dir_read(dj); /* Read an directory item */
emilmont 1:46ce1e16c870 3074 if (res == FR_NO_FILE) { /* Reached end of dir */
emilmont 1:46ce1e16c870 3075 dj->sect = 0;
emilmont 1:46ce1e16c870 3076 res = FR_OK;
emilmont 1:46ce1e16c870 3077 }
emilmont 1:46ce1e16c870 3078 if (res == FR_OK) { /* A valid entry is found */
emilmont 1:46ce1e16c870 3079 get_fileinfo(dj, fno); /* Get the object information */
emilmont 1:46ce1e16c870 3080 res = dir_next(dj, 0); /* Increment index for next */
emilmont 1:46ce1e16c870 3081 if (res == FR_NO_FILE) {
emilmont 1:46ce1e16c870 3082 dj->sect = 0;
emilmont 1:46ce1e16c870 3083 res = FR_OK;
emilmont 1:46ce1e16c870 3084 }
emilmont 1:46ce1e16c870 3085 }
emilmont 1:46ce1e16c870 3086 FREE_BUF();
emilmont 1:46ce1e16c870 3087 }
emilmont 1:46ce1e16c870 3088 }
emilmont 1:46ce1e16c870 3089
emilmont 1:46ce1e16c870 3090 LEAVE_FF(dj->fs, res);
emilmont 1:46ce1e16c870 3091 }
emilmont 1:46ce1e16c870 3092
emilmont 1:46ce1e16c870 3093
emilmont 1:46ce1e16c870 3094
emilmont 1:46ce1e16c870 3095 #if _FS_MINIMIZE == 0
emilmont 1:46ce1e16c870 3096 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3097 /* Get File Status */
emilmont 1:46ce1e16c870 3098 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3099
emilmont 1:46ce1e16c870 3100 FRESULT f_stat (
emilmont 1:46ce1e16c870 3101 const TCHAR *path, /* Pointer to the file path */
emilmont 1:46ce1e16c870 3102 FILINFO *fno /* Pointer to file information to return */
emilmont 1:46ce1e16c870 3103 )
emilmont 1:46ce1e16c870 3104 {
emilmont 1:46ce1e16c870 3105 FRESULT res;
emilmont 1:46ce1e16c870 3106 FATFS_DIR dj;
emilmont 1:46ce1e16c870 3107 DEF_NAMEBUF;
emilmont 1:46ce1e16c870 3108
emilmont 1:46ce1e16c870 3109
emilmont 1:46ce1e16c870 3110 res = chk_mounted(&path, &dj.fs, 0);
emilmont 1:46ce1e16c870 3111 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3112 INIT_BUF(dj);
emilmont 1:46ce1e16c870 3113 res = follow_path(&dj, path); /* Follow the file path */
emilmont 1:46ce1e16c870 3114 if (res == FR_OK) { /* Follow completed */
emilmont 1:46ce1e16c870 3115 if (dj.dir) /* Found an object */
emilmont 1:46ce1e16c870 3116 get_fileinfo(&dj, fno);
emilmont 1:46ce1e16c870 3117 else /* It is root dir */
emilmont 1:46ce1e16c870 3118 res = FR_INVALID_NAME;
emilmont 1:46ce1e16c870 3119 }
emilmont 1:46ce1e16c870 3120 FREE_BUF();
emilmont 1:46ce1e16c870 3121 }
emilmont 1:46ce1e16c870 3122
emilmont 1:46ce1e16c870 3123 LEAVE_FF(dj.fs, res);
emilmont 1:46ce1e16c870 3124 }
emilmont 1:46ce1e16c870 3125
emilmont 1:46ce1e16c870 3126
emilmont 1:46ce1e16c870 3127
emilmont 1:46ce1e16c870 3128 #if !_FS_READONLY
emilmont 1:46ce1e16c870 3129 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3130 /* Get Number of Free Clusters */
emilmont 1:46ce1e16c870 3131 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3132
emilmont 1:46ce1e16c870 3133 FRESULT f_getfree (
emilmont 1:46ce1e16c870 3134 const TCHAR *path, /* Pointer to the logical drive number (root dir) */
emilmont 1:46ce1e16c870 3135 DWORD *nclst, /* Pointer to the variable to return number of free clusters */
emilmont 1:46ce1e16c870 3136 FATFS **fatfs /* Pointer to pointer to corresponding file system object to return */
emilmont 1:46ce1e16c870 3137 )
emilmont 1:46ce1e16c870 3138 {
emilmont 1:46ce1e16c870 3139 FRESULT res;
emilmont 1:46ce1e16c870 3140 FATFS *fs;
emilmont 1:46ce1e16c870 3141 DWORD n, clst, sect, stat;
emilmont 1:46ce1e16c870 3142 UINT i;
emilmont 1:46ce1e16c870 3143 BYTE fat, *p;
emilmont 1:46ce1e16c870 3144
emilmont 1:46ce1e16c870 3145
emilmont 1:46ce1e16c870 3146 /* Get drive number */
emilmont 1:46ce1e16c870 3147 res = chk_mounted(&path, fatfs, 0);
emilmont 1:46ce1e16c870 3148 fs = *fatfs;
emilmont 1:46ce1e16c870 3149 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3150 /* If free_clust is valid, return it without full cluster scan */
emilmont 1:46ce1e16c870 3151 if (fs->free_clust <= fs->n_fatent - 2) {
emilmont 1:46ce1e16c870 3152 *nclst = fs->free_clust;
emilmont 1:46ce1e16c870 3153 } else {
emilmont 1:46ce1e16c870 3154 /* Get number of free clusters */
emilmont 1:46ce1e16c870 3155 fat = fs->fs_type;
emilmont 1:46ce1e16c870 3156 n = 0;
emilmont 1:46ce1e16c870 3157 if (fat == FS_FAT12) {
emilmont 1:46ce1e16c870 3158 clst = 2;
emilmont 1:46ce1e16c870 3159 do {
emilmont 1:46ce1e16c870 3160 stat = get_fat(fs, clst);
emilmont 1:46ce1e16c870 3161 if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
emilmont 1:46ce1e16c870 3162 if (stat == 1) { res = FR_INT_ERR; break; }
emilmont 1:46ce1e16c870 3163 if (stat == 0) n++;
emilmont 1:46ce1e16c870 3164 } while (++clst < fs->n_fatent);
emilmont 1:46ce1e16c870 3165 } else {
emilmont 1:46ce1e16c870 3166 clst = fs->n_fatent;
emilmont 1:46ce1e16c870 3167 sect = fs->fatbase;
emilmont 1:46ce1e16c870 3168 i = 0; p = 0;
emilmont 1:46ce1e16c870 3169 do {
emilmont 1:46ce1e16c870 3170 if (!i) {
emilmont 1:46ce1e16c870 3171 res = move_window(fs, sect++);
emilmont 1:46ce1e16c870 3172 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 3173 p = fs->win;
emilmont 1:46ce1e16c870 3174 i = SS(fs);
emilmont 1:46ce1e16c870 3175 }
emilmont 1:46ce1e16c870 3176 if (fat == FS_FAT16) {
emilmont 1:46ce1e16c870 3177 if (LD_WORD(p) == 0) n++;
emilmont 1:46ce1e16c870 3178 p += 2; i -= 2;
emilmont 1:46ce1e16c870 3179 } else {
emilmont 1:46ce1e16c870 3180 if ((LD_DWORD(p) & 0x0FFFFFFF) == 0) n++;
emilmont 1:46ce1e16c870 3181 p += 4; i -= 4;
emilmont 1:46ce1e16c870 3182 }
emilmont 1:46ce1e16c870 3183 } while (--clst);
emilmont 1:46ce1e16c870 3184 }
emilmont 1:46ce1e16c870 3185 fs->free_clust = n;
emilmont 1:46ce1e16c870 3186 if (fat == FS_FAT32) fs->fsi_flag = 1;
emilmont 1:46ce1e16c870 3187 *nclst = n;
emilmont 1:46ce1e16c870 3188 }
emilmont 1:46ce1e16c870 3189 }
emilmont 1:46ce1e16c870 3190 LEAVE_FF(fs, res);
emilmont 1:46ce1e16c870 3191 }
emilmont 1:46ce1e16c870 3192
emilmont 1:46ce1e16c870 3193
emilmont 1:46ce1e16c870 3194
emilmont 1:46ce1e16c870 3195
emilmont 1:46ce1e16c870 3196 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3197 /* Truncate File */
emilmont 1:46ce1e16c870 3198 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3199
emilmont 1:46ce1e16c870 3200 FRESULT f_truncate (
emilmont 1:46ce1e16c870 3201 FIL *fp /* Pointer to the file object */
emilmont 1:46ce1e16c870 3202 )
emilmont 1:46ce1e16c870 3203 {
emilmont 1:46ce1e16c870 3204 FRESULT res;
emilmont 1:46ce1e16c870 3205 DWORD ncl;
emilmont 1:46ce1e16c870 3206
emilmont 1:46ce1e16c870 3207
emilmont 1:46ce1e16c870 3208 if (!fp) return FR_INVALID_OBJECT;
emilmont 1:46ce1e16c870 3209
emilmont 1:46ce1e16c870 3210 res = validate(fp); /* Check validity of the object */
emilmont 1:46ce1e16c870 3211 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3212 if (fp->flag & FA__ERROR) { /* Check abort flag */
emilmont 1:46ce1e16c870 3213 res = FR_INT_ERR;
emilmont 1:46ce1e16c870 3214 } else {
emilmont 1:46ce1e16c870 3215 if (!(fp->flag & FA_WRITE)) /* Check access mode */
emilmont 1:46ce1e16c870 3216 res = FR_DENIED;
emilmont 1:46ce1e16c870 3217 }
emilmont 1:46ce1e16c870 3218 }
emilmont 1:46ce1e16c870 3219 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3220 if (fp->fsize > fp->fptr) {
emilmont 1:46ce1e16c870 3221 fp->fsize = fp->fptr; /* Set file size to current R/W point */
emilmont 1:46ce1e16c870 3222 fp->flag |= FA__WRITTEN;
emilmont 1:46ce1e16c870 3223 if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */
emilmont 1:46ce1e16c870 3224 res = remove_chain(fp->fs, fp->sclust);
emilmont 1:46ce1e16c870 3225 fp->sclust = 0;
emilmont 1:46ce1e16c870 3226 } else { /* When truncate a part of the file, remove remaining clusters */
emilmont 1:46ce1e16c870 3227 ncl = get_fat(fp->fs, fp->clust);
emilmont 1:46ce1e16c870 3228 res = FR_OK;
emilmont 1:46ce1e16c870 3229 if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;
emilmont 1:46ce1e16c870 3230 if (ncl == 1) res = FR_INT_ERR;
emilmont 1:46ce1e16c870 3231 if (res == FR_OK && ncl < fp->fs->n_fatent) {
emilmont 1:46ce1e16c870 3232 res = put_fat(fp->fs, fp->clust, 0x0FFFFFFF);
emilmont 1:46ce1e16c870 3233 if (res == FR_OK) res = remove_chain(fp->fs, ncl);
emilmont 1:46ce1e16c870 3234 }
emilmont 1:46ce1e16c870 3235 }
emilmont 1:46ce1e16c870 3236 }
emilmont 1:46ce1e16c870 3237 if (res != FR_OK) fp->flag |= FA__ERROR;
emilmont 1:46ce1e16c870 3238 }
emilmont 1:46ce1e16c870 3239
emilmont 1:46ce1e16c870 3240 LEAVE_FF(fp->fs, res);
emilmont 1:46ce1e16c870 3241 }
emilmont 1:46ce1e16c870 3242
emilmont 1:46ce1e16c870 3243
emilmont 1:46ce1e16c870 3244
emilmont 1:46ce1e16c870 3245
emilmont 1:46ce1e16c870 3246 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3247 /* Delete a File or Directory */
emilmont 1:46ce1e16c870 3248 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3249
emilmont 1:46ce1e16c870 3250 FRESULT f_unlink (
emilmont 1:46ce1e16c870 3251 const TCHAR *path /* Pointer to the file or directory path */
emilmont 1:46ce1e16c870 3252 )
emilmont 1:46ce1e16c870 3253 {
emilmont 1:46ce1e16c870 3254 FRESULT res;
emilmont 1:46ce1e16c870 3255 FATFS_DIR dj, sdj;
emilmont 1:46ce1e16c870 3256 BYTE *dir;
emilmont 1:46ce1e16c870 3257 DWORD dclst;
emilmont 1:46ce1e16c870 3258 DEF_NAMEBUF;
emilmont 1:46ce1e16c870 3259
emilmont 1:46ce1e16c870 3260
emilmont 1:46ce1e16c870 3261 res = chk_mounted(&path, &dj.fs, 1);
emilmont 1:46ce1e16c870 3262 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3263 INIT_BUF(dj);
emilmont 1:46ce1e16c870 3264 res = follow_path(&dj, path); /* Follow the file path */
emilmont 1:46ce1e16c870 3265 if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
emilmont 1:46ce1e16c870 3266 res = FR_INVALID_NAME; /* Cannot remove dot entry */
emilmont 1:46ce1e16c870 3267 #if _FS_LOCK
emilmont 1:46ce1e16c870 3268 if (res == FR_OK) res = chk_lock(&dj, 2); /* Cannot remove open file */
emilmont 1:46ce1e16c870 3269 #endif
emilmont 1:46ce1e16c870 3270 if (res == FR_OK) { /* The object is accessible */
emilmont 1:46ce1e16c870 3271 dir = dj.dir;
emilmont 1:46ce1e16c870 3272 if (!dir) {
emilmont 1:46ce1e16c870 3273 res = FR_INVALID_NAME; /* Cannot remove the start directory */
emilmont 1:46ce1e16c870 3274 } else {
emilmont 1:46ce1e16c870 3275 if (dir[DIR_Attr] & AM_RDO)
emilmont 1:46ce1e16c870 3276 res = FR_DENIED; /* Cannot remove R/O object */
emilmont 1:46ce1e16c870 3277 }
emilmont 1:46ce1e16c870 3278 dclst = ld_clust(dj.fs, dir);
emilmont 1:46ce1e16c870 3279 if (res == FR_OK && (dir[DIR_Attr] & AM_DIR)) { /* Is it a sub-dir? */
emilmont 1:46ce1e16c870 3280 if (dclst < 2) {
emilmont 1:46ce1e16c870 3281 res = FR_INT_ERR;
emilmont 1:46ce1e16c870 3282 } else {
emilmont 1:46ce1e16c870 3283 mem_cpy(&sdj, &dj, sizeof (FATFS_DIR)); /* Check if the sub-dir is empty or not */
emilmont 1:46ce1e16c870 3284 sdj.sclust = dclst;
emilmont 1:46ce1e16c870 3285 res = dir_sdi(&sdj, 2); /* Exclude dot entries */
emilmont 1:46ce1e16c870 3286 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3287 res = dir_read(&sdj);
emilmont 1:46ce1e16c870 3288 if (res == FR_OK /* Not empty dir */
emilmont 1:46ce1e16c870 3289 #if _FS_RPATH
emilmont 1:46ce1e16c870 3290 || dclst == dj.fs->cdir /* Current dir */
emilmont 1:46ce1e16c870 3291 #endif
emilmont 1:46ce1e16c870 3292 ) res = FR_DENIED;
emilmont 1:46ce1e16c870 3293 if (res == FR_NO_FILE) res = FR_OK; /* Empty */
emilmont 1:46ce1e16c870 3294 }
emilmont 1:46ce1e16c870 3295 }
emilmont 1:46ce1e16c870 3296 }
emilmont 1:46ce1e16c870 3297 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3298 res = dir_remove(&dj); /* Remove the directory entry */
emilmont 1:46ce1e16c870 3299 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3300 if (dclst) /* Remove the cluster chain if exist */
emilmont 1:46ce1e16c870 3301 res = remove_chain(dj.fs, dclst);
emilmont 1:46ce1e16c870 3302 if (res == FR_OK) res = sync(dj.fs);
emilmont 1:46ce1e16c870 3303 }
emilmont 1:46ce1e16c870 3304 }
emilmont 1:46ce1e16c870 3305 }
emilmont 1:46ce1e16c870 3306 FREE_BUF();
emilmont 1:46ce1e16c870 3307 }
emilmont 1:46ce1e16c870 3308 LEAVE_FF(dj.fs, res);
emilmont 1:46ce1e16c870 3309 }
emilmont 1:46ce1e16c870 3310
emilmont 1:46ce1e16c870 3311
emilmont 1:46ce1e16c870 3312
emilmont 1:46ce1e16c870 3313
emilmont 1:46ce1e16c870 3314 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3315 /* Create a Directory */
emilmont 1:46ce1e16c870 3316 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3317
emilmont 1:46ce1e16c870 3318 FRESULT f_mkdir (
emilmont 1:46ce1e16c870 3319 const TCHAR *path /* Pointer to the directory path */
emilmont 1:46ce1e16c870 3320 )
emilmont 1:46ce1e16c870 3321 {
emilmont 1:46ce1e16c870 3322 FRESULT res;
emilmont 1:46ce1e16c870 3323 FATFS_DIR dj;
emilmont 1:46ce1e16c870 3324 BYTE *dir, n;
emilmont 1:46ce1e16c870 3325 DWORD dsc, dcl, pcl, tim = get_fattime();
emilmont 1:46ce1e16c870 3326 DEF_NAMEBUF;
emilmont 1:46ce1e16c870 3327
emilmont 1:46ce1e16c870 3328
emilmont 1:46ce1e16c870 3329 res = chk_mounted(&path, &dj.fs, 1);
emilmont 1:46ce1e16c870 3330 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3331 INIT_BUF(dj);
emilmont 1:46ce1e16c870 3332 res = follow_path(&dj, path); /* Follow the file path */
emilmont 1:46ce1e16c870 3333 if (res == FR_OK) res = FR_EXIST; /* Any object with same name is already existing */
emilmont 1:46ce1e16c870 3334 if (_FS_RPATH && res == FR_NO_FILE && (dj.fn[NS] & NS_DOT))
emilmont 1:46ce1e16c870 3335 res = FR_INVALID_NAME;
emilmont 1:46ce1e16c870 3336 if (res == FR_NO_FILE) { /* Can create a new directory */
emilmont 1:46ce1e16c870 3337 dcl = create_chain(dj.fs, 0); /* Allocate a cluster for the new directory table */
emilmont 1:46ce1e16c870 3338 res = FR_OK;
emilmont 1:46ce1e16c870 3339 if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster */
emilmont 1:46ce1e16c870 3340 if (dcl == 1) res = FR_INT_ERR;
emilmont 1:46ce1e16c870 3341 if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
emilmont 1:46ce1e16c870 3342 if (res == FR_OK) /* Flush FAT */
emilmont 1:46ce1e16c870 3343 res = move_window(dj.fs, 0);
emilmont 1:46ce1e16c870 3344 if (res == FR_OK) { /* Initialize the new directory table */
emilmont 1:46ce1e16c870 3345 dsc = clust2sect(dj.fs, dcl);
emilmont 1:46ce1e16c870 3346 dir = dj.fs->win;
emilmont 1:46ce1e16c870 3347 mem_set(dir, 0, SS(dj.fs));
emilmont 1:46ce1e16c870 3348 mem_set(dir+DIR_Name, ' ', 8+3); /* Create "." entry */
emilmont 1:46ce1e16c870 3349 dir[DIR_Name] = '.';
emilmont 1:46ce1e16c870 3350 dir[DIR_Attr] = AM_DIR;
emilmont 1:46ce1e16c870 3351 ST_DWORD(dir+DIR_WrtTime, tim);
emilmont 1:46ce1e16c870 3352 st_clust(dir, dcl);
emilmont 1:46ce1e16c870 3353 mem_cpy(dir+SZ_DIR, dir, SZ_DIR); /* Create ".." entry */
emilmont 1:46ce1e16c870 3354 dir[33] = '.'; pcl = dj.sclust;
emilmont 1:46ce1e16c870 3355 if (dj.fs->fs_type == FS_FAT32 && pcl == dj.fs->dirbase)
emilmont 1:46ce1e16c870 3356 pcl = 0;
emilmont 1:46ce1e16c870 3357 st_clust(dir+SZ_DIR, pcl);
emilmont 1:46ce1e16c870 3358 for (n = dj.fs->csize; n; n--) { /* Write dot entries and clear following sectors */
emilmont 1:46ce1e16c870 3359 dj.fs->winsect = dsc++;
emilmont 1:46ce1e16c870 3360 dj.fs->wflag = 1;
emilmont 1:46ce1e16c870 3361 res = move_window(dj.fs, 0);
emilmont 1:46ce1e16c870 3362 if (res != FR_OK) break;
emilmont 1:46ce1e16c870 3363 mem_set(dir, 0, SS(dj.fs));
emilmont 1:46ce1e16c870 3364 }
emilmont 1:46ce1e16c870 3365 }
emilmont 1:46ce1e16c870 3366 if (res == FR_OK) res = dir_register(&dj); /* Register the object to the directoy */
emilmont 1:46ce1e16c870 3367 if (res != FR_OK) {
emilmont 1:46ce1e16c870 3368 remove_chain(dj.fs, dcl); /* Could not register, remove cluster chain */
emilmont 1:46ce1e16c870 3369 } else {
emilmont 1:46ce1e16c870 3370 dir = dj.dir;
emilmont 1:46ce1e16c870 3371 dir[DIR_Attr] = AM_DIR; /* Attribute */
emilmont 1:46ce1e16c870 3372 ST_DWORD(dir+DIR_WrtTime, tim); /* Created time */
emilmont 1:46ce1e16c870 3373 st_clust(dir, dcl); /* Table start cluster */
emilmont 1:46ce1e16c870 3374 dj.fs->wflag = 1;
emilmont 1:46ce1e16c870 3375 res = sync(dj.fs);
emilmont 1:46ce1e16c870 3376 }
emilmont 1:46ce1e16c870 3377 }
emilmont 1:46ce1e16c870 3378 FREE_BUF();
emilmont 1:46ce1e16c870 3379 }
emilmont 1:46ce1e16c870 3380
emilmont 1:46ce1e16c870 3381 LEAVE_FF(dj.fs, res);
emilmont 1:46ce1e16c870 3382 }
emilmont 1:46ce1e16c870 3383
emilmont 1:46ce1e16c870 3384
emilmont 1:46ce1e16c870 3385
emilmont 1:46ce1e16c870 3386
emilmont 1:46ce1e16c870 3387 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3388 /* Change Attribute */
emilmont 1:46ce1e16c870 3389 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3390
emilmont 1:46ce1e16c870 3391 FRESULT f_chmod (
emilmont 1:46ce1e16c870 3392 const TCHAR *path, /* Pointer to the file path */
emilmont 1:46ce1e16c870 3393 BYTE value, /* Attribute bits */
emilmont 1:46ce1e16c870 3394 BYTE mask /* Attribute mask to change */
emilmont 1:46ce1e16c870 3395 )
emilmont 1:46ce1e16c870 3396 {
emilmont 1:46ce1e16c870 3397 FRESULT res;
emilmont 1:46ce1e16c870 3398 FATFS_DIR dj;
emilmont 1:46ce1e16c870 3399 BYTE *dir;
emilmont 1:46ce1e16c870 3400 DEF_NAMEBUF;
emilmont 1:46ce1e16c870 3401
emilmont 1:46ce1e16c870 3402
emilmont 1:46ce1e16c870 3403 res = chk_mounted(&path, &dj.fs, 1);
emilmont 1:46ce1e16c870 3404 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3405 INIT_BUF(dj);
emilmont 1:46ce1e16c870 3406 res = follow_path(&dj, path); /* Follow the file path */
emilmont 1:46ce1e16c870 3407 FREE_BUF();
emilmont 1:46ce1e16c870 3408 if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
emilmont 1:46ce1e16c870 3409 res = FR_INVALID_NAME;
emilmont 1:46ce1e16c870 3410 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3411 dir = dj.dir;
emilmont 1:46ce1e16c870 3412 if (!dir) { /* Is it a root directory? */
emilmont 1:46ce1e16c870 3413 res = FR_INVALID_NAME;
emilmont 1:46ce1e16c870 3414 } else { /* File or sub directory */
emilmont 1:46ce1e16c870 3415 mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */
emilmont 1:46ce1e16c870 3416 dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */
emilmont 1:46ce1e16c870 3417 dj.fs->wflag = 1;
emilmont 1:46ce1e16c870 3418 res = sync(dj.fs);
emilmont 1:46ce1e16c870 3419 }
emilmont 1:46ce1e16c870 3420 }
emilmont 1:46ce1e16c870 3421 }
emilmont 1:46ce1e16c870 3422
emilmont 1:46ce1e16c870 3423 LEAVE_FF(dj.fs, res);
emilmont 1:46ce1e16c870 3424 }
emilmont 1:46ce1e16c870 3425
emilmont 1:46ce1e16c870 3426
emilmont 1:46ce1e16c870 3427
emilmont 1:46ce1e16c870 3428
emilmont 1:46ce1e16c870 3429 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3430 /* Change Timestamp */
emilmont 1:46ce1e16c870 3431 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3432
emilmont 1:46ce1e16c870 3433 FRESULT f_utime (
emilmont 1:46ce1e16c870 3434 const TCHAR *path, /* Pointer to the file/directory name */
emilmont 1:46ce1e16c870 3435 const FILINFO *fno /* Pointer to the time stamp to be set */
emilmont 1:46ce1e16c870 3436 )
emilmont 1:46ce1e16c870 3437 {
emilmont 1:46ce1e16c870 3438 FRESULT res;
emilmont 1:46ce1e16c870 3439 FATFS_DIR dj;
emilmont 1:46ce1e16c870 3440 BYTE *dir;
emilmont 1:46ce1e16c870 3441 DEF_NAMEBUF;
emilmont 1:46ce1e16c870 3442
emilmont 1:46ce1e16c870 3443
emilmont 1:46ce1e16c870 3444 res = chk_mounted(&path, &dj.fs, 1);
emilmont 1:46ce1e16c870 3445 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3446 INIT_BUF(dj);
emilmont 1:46ce1e16c870 3447 res = follow_path(&dj, path); /* Follow the file path */
emilmont 1:46ce1e16c870 3448 FREE_BUF();
emilmont 1:46ce1e16c870 3449 if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
emilmont 1:46ce1e16c870 3450 res = FR_INVALID_NAME;
emilmont 1:46ce1e16c870 3451 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3452 dir = dj.dir;
emilmont 1:46ce1e16c870 3453 if (!dir) { /* Root directory */
emilmont 1:46ce1e16c870 3454 res = FR_INVALID_NAME;
emilmont 1:46ce1e16c870 3455 } else { /* File or sub-directory */
emilmont 1:46ce1e16c870 3456 ST_WORD(dir+DIR_WrtTime, fno->ftime);
emilmont 1:46ce1e16c870 3457 ST_WORD(dir+DIR_WrtDate, fno->fdate);
emilmont 1:46ce1e16c870 3458 dj.fs->wflag = 1;
emilmont 1:46ce1e16c870 3459 res = sync(dj.fs);
emilmont 1:46ce1e16c870 3460 }
emilmont 1:46ce1e16c870 3461 }
emilmont 1:46ce1e16c870 3462 }
emilmont 1:46ce1e16c870 3463
emilmont 1:46ce1e16c870 3464 LEAVE_FF(dj.fs, res);
emilmont 1:46ce1e16c870 3465 }
emilmont 1:46ce1e16c870 3466
emilmont 1:46ce1e16c870 3467
emilmont 1:46ce1e16c870 3468
emilmont 1:46ce1e16c870 3469
emilmont 1:46ce1e16c870 3470 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3471 /* Rename File/Directory */
emilmont 1:46ce1e16c870 3472 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3473
emilmont 1:46ce1e16c870 3474 FRESULT f_rename (
emilmont 1:46ce1e16c870 3475 const TCHAR *path_old, /* Pointer to the old name */
emilmont 1:46ce1e16c870 3476 const TCHAR *path_new /* Pointer to the new name */
emilmont 1:46ce1e16c870 3477 )
emilmont 1:46ce1e16c870 3478 {
emilmont 1:46ce1e16c870 3479 FRESULT res;
emilmont 1:46ce1e16c870 3480 FATFS_DIR djo, djn;
emilmont 1:46ce1e16c870 3481 BYTE buf[21], *dir;
emilmont 1:46ce1e16c870 3482 DWORD dw;
emilmont 1:46ce1e16c870 3483 DEF_NAMEBUF;
emilmont 1:46ce1e16c870 3484
emilmont 1:46ce1e16c870 3485
emilmont 1:46ce1e16c870 3486 res = chk_mounted(&path_old, &djo.fs, 1);
emilmont 1:46ce1e16c870 3487 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3488 djn.fs = djo.fs;
emilmont 1:46ce1e16c870 3489 INIT_BUF(djo);
emilmont 1:46ce1e16c870 3490 res = follow_path(&djo, path_old); /* Check old object */
emilmont 1:46ce1e16c870 3491 if (_FS_RPATH && res == FR_OK && (djo.fn[NS] & NS_DOT))
emilmont 1:46ce1e16c870 3492 res = FR_INVALID_NAME;
emilmont 1:46ce1e16c870 3493 #if _FS_LOCK
emilmont 1:46ce1e16c870 3494 if (res == FR_OK) res = chk_lock(&djo, 2);
emilmont 1:46ce1e16c870 3495 #endif
emilmont 1:46ce1e16c870 3496 if (res == FR_OK) { /* Old object is found */
emilmont 1:46ce1e16c870 3497 if (!djo.dir) { /* Is root dir? */
emilmont 1:46ce1e16c870 3498 res = FR_NO_FILE;
emilmont 1:46ce1e16c870 3499 } else {
emilmont 1:46ce1e16c870 3500 mem_cpy(buf, djo.dir+DIR_Attr, 21); /* Save the object information except for name */
emilmont 1:46ce1e16c870 3501 mem_cpy(&djn, &djo, sizeof (FATFS_DIR)); /* Check new object */
emilmont 1:46ce1e16c870 3502 res = follow_path(&djn, path_new);
emilmont 1:46ce1e16c870 3503 if (res == FR_OK) res = FR_EXIST; /* The new object name is already existing */
emilmont 1:46ce1e16c870 3504 if (res == FR_NO_FILE) { /* Is it a valid path and no name collision? */
emilmont 1:46ce1e16c870 3505 /* Start critical section that an interruption or error can cause cross-link */
emilmont 1:46ce1e16c870 3506 res = dir_register(&djn); /* Register the new entry */
emilmont 1:46ce1e16c870 3507 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3508 dir = djn.dir; /* Copy object information except for name */
emilmont 1:46ce1e16c870 3509 mem_cpy(dir+13, buf+2, 19);
emilmont 1:46ce1e16c870 3510 dir[DIR_Attr] = buf[0] | AM_ARC;
emilmont 1:46ce1e16c870 3511 djo.fs->wflag = 1;
emilmont 1:46ce1e16c870 3512 if (djo.sclust != djn.sclust && (dir[DIR_Attr] & AM_DIR)) { /* Update .. entry in the directory if needed */
emilmont 1:46ce1e16c870 3513 dw = clust2sect(djo.fs, ld_clust(djo.fs, dir));
emilmont 1:46ce1e16c870 3514 if (!dw) {
emilmont 1:46ce1e16c870 3515 res = FR_INT_ERR;
emilmont 1:46ce1e16c870 3516 } else {
emilmont 1:46ce1e16c870 3517 res = move_window(djo.fs, dw);
emilmont 1:46ce1e16c870 3518 dir = djo.fs->win+SZ_DIR; /* .. entry */
emilmont 1:46ce1e16c870 3519 if (res == FR_OK && dir[1] == '.') {
emilmont 1:46ce1e16c870 3520 dw = (djo.fs->fs_type == FS_FAT32 && djn.sclust == djo.fs->dirbase) ? 0 : djn.sclust;
emilmont 1:46ce1e16c870 3521 st_clust(dir, dw);
emilmont 1:46ce1e16c870 3522 djo.fs->wflag = 1;
emilmont 1:46ce1e16c870 3523 }
emilmont 1:46ce1e16c870 3524 }
emilmont 1:46ce1e16c870 3525 }
emilmont 1:46ce1e16c870 3526 if (res == FR_OK) {
emilmont 1:46ce1e16c870 3527 res = dir_remove(&djo); /* Remove old entry */
emilmont 1:46ce1e16c870 3528 if (res == FR_OK)
emilmont 1:46ce1e16c870 3529 res = sync(djo.fs);
emilmont 1:46ce1e16c870 3530 }
emilmont 1:46ce1e16c870 3531 }
emilmont 1:46ce1e16c870 3532 /* End critical section */
emilmont 1:46ce1e16c870 3533 }
emilmont 1:46ce1e16c870 3534 }
emilmont 1:46ce1e16c870 3535 }
emilmont 1:46ce1e16c870 3536 FREE_BUF();
emilmont 1:46ce1e16c870 3537 }
emilmont 1:46ce1e16c870 3538 LEAVE_FF(djo.fs, res);
emilmont 1:46ce1e16c870 3539 }
emilmont 1:46ce1e16c870 3540
emilmont 1:46ce1e16c870 3541 #endif /* !_FS_READONLY */
emilmont 1:46ce1e16c870 3542 #endif /* _FS_MINIMIZE == 0 */
emilmont 1:46ce1e16c870 3543 #endif /* _FS_MINIMIZE <= 1 */
emilmont 1:46ce1e16c870 3544 #endif /* _FS_MINIMIZE <= 2 */
emilmont 1:46ce1e16c870 3545
emilmont 1:46ce1e16c870 3546
emilmont 1:46ce1e16c870 3547
emilmont 1:46ce1e16c870 3548 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3549 /* Forward data to the stream directly (available on only tiny cfg) */
emilmont 1:46ce1e16c870 3550 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3551 #if _USE_FORWARD && _FS_TINY
emilmont 1:46ce1e16c870 3552
emilmont 1:46ce1e16c870 3553 FRESULT f_forward (
emilmont 1:46ce1e16c870 3554 FIL *fp, /* Pointer to the file object */
emilmont 1:46ce1e16c870 3555 UINT (*func)(const BYTE*,UINT), /* Pointer to the streaming function */
emilmont 1:46ce1e16c870 3556 UINT btr, /* Number of bytes to forward */
emilmont 1:46ce1e16c870 3557 UINT *bf /* Pointer to number of bytes forwarded */
emilmont 1:46ce1e16c870 3558 )
emilmont 1:46ce1e16c870 3559 {
emilmont 1:46ce1e16c870 3560 FRESULT res;
emilmont 1:46ce1e16c870 3561 DWORD remain, clst, sect;
emilmont 1:46ce1e16c870 3562 UINT rcnt;
emilmont 1:46ce1e16c870 3563 BYTE csect;
emilmont 1:46ce1e16c870 3564
emilmont 1:46ce1e16c870 3565
emilmont 1:46ce1e16c870 3566 *bf = 0; /* Clear transfer byte counter */
emilmont 1:46ce1e16c870 3567
emilmont 1:46ce1e16c870 3568 if (!fp) return FR_INVALID_OBJECT;
emilmont 1:46ce1e16c870 3569
emilmont 1:46ce1e16c870 3570 res = validate(fp); /* Check validity of the object */
emilmont 1:46ce1e16c870 3571 if (res != FR_OK) LEAVE_FF(fp->fs, res);
emilmont 1:46ce1e16c870 3572 if (fp->flag & FA__ERROR) /* Check error flag */
emilmont 1:46ce1e16c870 3573 LEAVE_FF(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 3574 if (!(fp->flag & FA_READ)) /* Check access mode */
emilmont 1:46ce1e16c870 3575 LEAVE_FF(fp->fs, FR_DENIED);
emilmont 1:46ce1e16c870 3576
emilmont 1:46ce1e16c870 3577 remain = fp->fsize - fp->fptr;
emilmont 1:46ce1e16c870 3578 if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
emilmont 1:46ce1e16c870 3579
emilmont 1:46ce1e16c870 3580 for ( ; btr && (*func)(0, 0); /* Repeat until all data transferred or stream becomes busy */
emilmont 1:46ce1e16c870 3581 fp->fptr += rcnt, *bf += rcnt, btr -= rcnt) {
emilmont 1:46ce1e16c870 3582 csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
emilmont 1:46ce1e16c870 3583 if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
emilmont 1:46ce1e16c870 3584 if (!csect) { /* On the cluster boundary? */
emilmont 1:46ce1e16c870 3585 clst = (fp->fptr == 0) ? /* On the top of the file? */
emilmont 1:46ce1e16c870 3586 fp->sclust : get_fat(fp->fs, fp->clust);
emilmont 1:46ce1e16c870 3587 if (clst <= 1) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 3588 if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 3589 fp->clust = clst; /* Update current cluster */
emilmont 1:46ce1e16c870 3590 }
emilmont 1:46ce1e16c870 3591 }
emilmont 1:46ce1e16c870 3592 sect = clust2sect(fp->fs, fp->clust); /* Get current data sector */
emilmont 1:46ce1e16c870 3593 if (!sect) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 3594 sect += csect;
emilmont 1:46ce1e16c870 3595 if (move_window(fp->fs, sect)) /* Move sector window */
emilmont 1:46ce1e16c870 3596 ABORT(fp->fs, FR_DISK_ERR);
emilmont 1:46ce1e16c870 3597 fp->dsect = sect;
emilmont 1:46ce1e16c870 3598 rcnt = SS(fp->fs) - (WORD)(fp->fptr % SS(fp->fs)); /* Forward data from sector window */
emilmont 1:46ce1e16c870 3599 if (rcnt > btr) rcnt = btr;
emilmont 1:46ce1e16c870 3600 rcnt = (*func)(&fp->fs->win[(WORD)fp->fptr % SS(fp->fs)], rcnt);
emilmont 1:46ce1e16c870 3601 if (!rcnt) ABORT(fp->fs, FR_INT_ERR);
emilmont 1:46ce1e16c870 3602 }
emilmont 1:46ce1e16c870 3603
emilmont 1:46ce1e16c870 3604 LEAVE_FF(fp->fs, FR_OK);
emilmont 1:46ce1e16c870 3605 }
emilmont 1:46ce1e16c870 3606 #endif /* _USE_FORWARD */
emilmont 1:46ce1e16c870 3607
emilmont 1:46ce1e16c870 3608
emilmont 1:46ce1e16c870 3609
emilmont 1:46ce1e16c870 3610 #if _USE_MKFS && !_FS_READONLY
emilmont 1:46ce1e16c870 3611 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3612 /* Create File System on the Drive */
emilmont 1:46ce1e16c870 3613 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3614 #define N_ROOTDIR 512 /* Number of root dir entries for FAT12/16 */
emilmont 1:46ce1e16c870 3615 #define N_FATS 1 /* Number of FAT copies (1 or 2) */
emilmont 1:46ce1e16c870 3616
emilmont 1:46ce1e16c870 3617
emilmont 1:46ce1e16c870 3618 FRESULT f_mkfs (
emilmont 1:46ce1e16c870 3619 BYTE drv, /* Logical drive number */
emilmont 1:46ce1e16c870 3620 BYTE sfd, /* Partitioning rule 0:FDISK, 1:SFD */
emilmont 1:46ce1e16c870 3621 UINT au /* Allocation unit size [bytes] */
emilmont 1:46ce1e16c870 3622 )
emilmont 1:46ce1e16c870 3623 {
emilmont 1:46ce1e16c870 3624 static const WORD vst[] = { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0};
emilmont 1:46ce1e16c870 3625 static const WORD cst[] = {32768, 16384, 8192, 4096, 2048, 16384, 8192, 4096, 2048, 1024, 512};
emilmont 1:46ce1e16c870 3626 BYTE fmt, md, sys, *tbl, pdrv, part;
emilmont 1:46ce1e16c870 3627 DWORD n_clst, vs, n, wsect;
emilmont 1:46ce1e16c870 3628 UINT i;
emilmont 1:46ce1e16c870 3629 DWORD b_vol, b_fat, b_dir, b_data; /* LBA */
emilmont 1:46ce1e16c870 3630 DWORD n_vol, n_rsv, n_fat, n_dir; /* Size */
emilmont 1:46ce1e16c870 3631 FATFS *fs;
emilmont 1:46ce1e16c870 3632 DSTATUS stat;
emilmont 1:46ce1e16c870 3633
emilmont 1:46ce1e16c870 3634
emilmont 1:46ce1e16c870 3635 /* Check mounted drive and clear work area */
emilmont 1:46ce1e16c870 3636 if (drv >= _VOLUMES) return FR_INVALID_DRIVE;
emilmont 1:46ce1e16c870 3637 if (sfd > 1) return FR_INVALID_PARAMETER;
emilmont 1:46ce1e16c870 3638 if (au & (au - 1)) return FR_INVALID_PARAMETER;
emilmont 1:46ce1e16c870 3639 fs = FatFs[drv];
emilmont 1:46ce1e16c870 3640 if (!fs) return FR_NOT_ENABLED;
emilmont 1:46ce1e16c870 3641 fs->fs_type = 0;
emilmont 1:46ce1e16c870 3642 pdrv = LD2PD(drv); /* Physical drive */
emilmont 1:46ce1e16c870 3643 part = LD2PT(drv); /* Partition (0:auto detect, 1-4:get from partition table)*/
emilmont 1:46ce1e16c870 3644
emilmont 1:46ce1e16c870 3645 /* Get disk statics */
emilmont 1:46ce1e16c870 3646 stat = disk_initialize(pdrv);
emilmont 1:46ce1e16c870 3647 if (stat & STA_NOINIT) return FR_NOT_READY;
emilmont 1:46ce1e16c870 3648 if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
emilmont 1:46ce1e16c870 3649 #if _MAX_SS != 512 /* Get disk sector size */
emilmont 1:46ce1e16c870 3650 if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS)
emilmont 1:46ce1e16c870 3651 return FR_DISK_ERR;
emilmont 1:46ce1e16c870 3652 #endif
emilmont 1:46ce1e16c870 3653 if (_MULTI_PARTITION && part) {
emilmont 1:46ce1e16c870 3654 /* Get partition information from partition table in the MBR */
emilmont 1:46ce1e16c870 3655 if (disk_read(pdrv, fs->win, 0, 1) != RES_OK) return FR_DISK_ERR;
emilmont 1:46ce1e16c870 3656 if (LD_WORD(fs->win+BS_55AA) != 0xAA55) return FR_MKFS_ABORTED;
emilmont 1:46ce1e16c870 3657 tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
emilmont 1:46ce1e16c870 3658 if (!tbl[4]) return FR_MKFS_ABORTED; /* No partition? */
emilmont 1:46ce1e16c870 3659 b_vol = LD_DWORD(tbl+8); /* Volume start sector */
emilmont 1:46ce1e16c870 3660 n_vol = LD_DWORD(tbl+12); /* Volume size */
emilmont 1:46ce1e16c870 3661 } else {
emilmont 1:46ce1e16c870 3662 /* Create a partition in this function */
emilmont 1:46ce1e16c870 3663 if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &n_vol) != RES_OK || n_vol < 128)
emilmont 1:46ce1e16c870 3664 return FR_DISK_ERR;
emilmont 1:46ce1e16c870 3665 b_vol = (sfd) ? 0 : 63; /* Volume start sector */
emilmont 1:46ce1e16c870 3666 n_vol -= b_vol; /* Volume size */
emilmont 1:46ce1e16c870 3667 }
emilmont 1:46ce1e16c870 3668
emilmont 1:46ce1e16c870 3669 if (!au) { /* AU auto selection */
emilmont 1:46ce1e16c870 3670 vs = n_vol / (2000 / (SS(fs) / 512));
emilmont 1:46ce1e16c870 3671 for (i = 0; vs < vst[i]; i++) ;
emilmont 1:46ce1e16c870 3672 au = cst[i];
emilmont 1:46ce1e16c870 3673 }
emilmont 1:46ce1e16c870 3674 au /= SS(fs); /* Number of sectors per cluster */
emilmont 1:46ce1e16c870 3675 if (au == 0) au = 1;
emilmont 1:46ce1e16c870 3676 if (au > 128) au = 128;
emilmont 1:46ce1e16c870 3677
emilmont 1:46ce1e16c870 3678 /* Pre-compute number of clusters and FAT sub-type */
emilmont 1:46ce1e16c870 3679 n_clst = n_vol / au;
emilmont 1:46ce1e16c870 3680 fmt = FS_FAT12;
emilmont 1:46ce1e16c870 3681 if (n_clst >= MIN_FAT16) fmt = FS_FAT16;
emilmont 1:46ce1e16c870 3682 if (n_clst >= MIN_FAT32) fmt = FS_FAT32;
emilmont 1:46ce1e16c870 3683
emilmont 1:46ce1e16c870 3684 /* Determine offset and size of FAT structure */
emilmont 1:46ce1e16c870 3685 if (fmt == FS_FAT32) {
emilmont 1:46ce1e16c870 3686 n_fat = ((n_clst * 4) + 8 + SS(fs) - 1) / SS(fs);
emilmont 1:46ce1e16c870 3687 n_rsv = 32;
emilmont 1:46ce1e16c870 3688 n_dir = 0;
emilmont 1:46ce1e16c870 3689 } else {
emilmont 1:46ce1e16c870 3690 n_fat = (fmt == FS_FAT12) ? (n_clst * 3 + 1) / 2 + 3 : (n_clst * 2) + 4;
emilmont 1:46ce1e16c870 3691 n_fat = (n_fat + SS(fs) - 1) / SS(fs);
emilmont 1:46ce1e16c870 3692 n_rsv = 1;
emilmont 1:46ce1e16c870 3693 n_dir = (DWORD)N_ROOTDIR * SZ_DIR / SS(fs);
emilmont 1:46ce1e16c870 3694 }
emilmont 1:46ce1e16c870 3695 b_fat = b_vol + n_rsv; /* FAT area start sector */
emilmont 1:46ce1e16c870 3696 b_dir = b_fat + n_fat * N_FATS; /* Directory area start sector */
emilmont 1:46ce1e16c870 3697 b_data = b_dir + n_dir; /* Data area start sector */
emilmont 1:46ce1e16c870 3698 if (n_vol < b_data + au - b_vol) return FR_MKFS_ABORTED; /* Too small volume */
emilmont 1:46ce1e16c870 3699
emilmont 1:46ce1e16c870 3700 /* Align data start sector to erase block boundary (for flash memory media) */
emilmont 1:46ce1e16c870 3701 if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &n) != RES_OK || !n || n > 32768) n = 1;
emilmont 1:46ce1e16c870 3702 n = (b_data + n - 1) & ~(n - 1); /* Next nearest erase block from current data start */
emilmont 1:46ce1e16c870 3703 n = (n - b_data) / N_FATS;
emilmont 1:46ce1e16c870 3704 if (fmt == FS_FAT32) { /* FAT32: Move FAT offset */
emilmont 1:46ce1e16c870 3705 n_rsv += n;
emilmont 1:46ce1e16c870 3706 b_fat += n;
emilmont 1:46ce1e16c870 3707 } else { /* FAT12/16: Expand FAT size */
emilmont 1:46ce1e16c870 3708 n_fat += n;
emilmont 1:46ce1e16c870 3709 }
emilmont 1:46ce1e16c870 3710
emilmont 1:46ce1e16c870 3711 /* Determine number of clusters and final check of validity of the FAT sub-type */
emilmont 1:46ce1e16c870 3712 n_clst = (n_vol - n_rsv - n_fat * N_FATS - n_dir) / au;
emilmont 1:46ce1e16c870 3713 if ( (fmt == FS_FAT16 && n_clst < MIN_FAT16)
emilmont 1:46ce1e16c870 3714 || (fmt == FS_FAT32 && n_clst < MIN_FAT32))
emilmont 1:46ce1e16c870 3715 return FR_MKFS_ABORTED;
emilmont 1:46ce1e16c870 3716
emilmont 1:46ce1e16c870 3717 switch (fmt) { /* Determine system ID for partition table */
emilmont 1:46ce1e16c870 3718 case FS_FAT12: sys = 0x01; break;
emilmont 1:46ce1e16c870 3719 case FS_FAT16: sys = (n_vol < 0x10000) ? 0x04 : 0x06; break;
emilmont 1:46ce1e16c870 3720 default: sys = 0x0C;
emilmont 1:46ce1e16c870 3721 }
emilmont 1:46ce1e16c870 3722
emilmont 1:46ce1e16c870 3723 if (_MULTI_PARTITION && part) {
emilmont 1:46ce1e16c870 3724 /* Update system ID in the partition table */
emilmont 1:46ce1e16c870 3725 tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
emilmont 1:46ce1e16c870 3726 tbl[4] = sys;
emilmont 1:46ce1e16c870 3727 if (disk_write(pdrv, fs->win, 0, 1) != RES_OK) return FR_DISK_ERR;
emilmont 1:46ce1e16c870 3728 md = 0xF8;
emilmont 1:46ce1e16c870 3729 } else {
emilmont 1:46ce1e16c870 3730 if (sfd) { /* No partition table (SFD) */
emilmont 1:46ce1e16c870 3731 md = 0xF0;
emilmont 1:46ce1e16c870 3732 } else { /* Create partition table (FDISK) */
emilmont 1:46ce1e16c870 3733 mem_set(fs->win, 0, SS(fs));
emilmont 1:46ce1e16c870 3734 tbl = fs->win+MBR_Table; /* Create partition table for single partition in the drive */
emilmont 1:46ce1e16c870 3735 tbl[1] = 1; /* Partition start head */
emilmont 1:46ce1e16c870 3736 tbl[2] = 1; /* Partition start sector */
emilmont 1:46ce1e16c870 3737 tbl[3] = 0; /* Partition start cylinder */
emilmont 1:46ce1e16c870 3738 tbl[4] = sys; /* System type */
emilmont 1:46ce1e16c870 3739 tbl[5] = 254; /* Partition end head */
emilmont 1:46ce1e16c870 3740 n = (b_vol + n_vol) / 63 / 255;
emilmont 1:46ce1e16c870 3741 tbl[6] = (BYTE)((n >> 2) | 63); /* Partition end sector */
emilmont 1:46ce1e16c870 3742 tbl[7] = (BYTE)n; /* End cylinder */
emilmont 1:46ce1e16c870 3743 ST_DWORD(tbl+8, 63); /* Partition start in LBA */
emilmont 1:46ce1e16c870 3744 ST_DWORD(tbl+12, n_vol); /* Partition size in LBA */
emilmont 1:46ce1e16c870 3745 ST_WORD(fs->win+BS_55AA, 0xAA55); /* MBR signature */
emilmont 1:46ce1e16c870 3746 if (disk_write(pdrv, fs->win, 0, 1) != RES_OK) /* Write it to the MBR sector */
emilmont 1:46ce1e16c870 3747 return FR_DISK_ERR;
emilmont 1:46ce1e16c870 3748 md = 0xF8;
emilmont 1:46ce1e16c870 3749 }
emilmont 1:46ce1e16c870 3750 }
emilmont 1:46ce1e16c870 3751
emilmont 1:46ce1e16c870 3752 /* Create BPB in the VBR */
emilmont 1:46ce1e16c870 3753 tbl = fs->win; /* Clear sector */
emilmont 1:46ce1e16c870 3754 mem_set(tbl, 0, SS(fs));
emilmont 1:46ce1e16c870 3755 mem_cpy(tbl, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code, OEM name */
emilmont 1:46ce1e16c870 3756 i = SS(fs); /* Sector size */
emilmont 1:46ce1e16c870 3757 ST_WORD(tbl+BPB_BytsPerSec, i);
emilmont 1:46ce1e16c870 3758 tbl[BPB_SecPerClus] = (BYTE)au; /* Sectors per cluster */
emilmont 1:46ce1e16c870 3759 ST_WORD(tbl+BPB_RsvdSecCnt, n_rsv); /* Reserved sectors */
emilmont 1:46ce1e16c870 3760 tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */
emilmont 1:46ce1e16c870 3761 i = (fmt == FS_FAT32) ? 0 : N_ROOTDIR; /* Number of rootdir entries */
emilmont 1:46ce1e16c870 3762 ST_WORD(tbl+BPB_RootEntCnt, i);
emilmont 1:46ce1e16c870 3763 if (n_vol < 0x10000) { /* Number of total sectors */
emilmont 1:46ce1e16c870 3764 ST_WORD(tbl+BPB_TotSec16, n_vol);
emilmont 1:46ce1e16c870 3765 } else {
emilmont 1:46ce1e16c870 3766 ST_DWORD(tbl+BPB_TotSec32, n_vol);
emilmont 1:46ce1e16c870 3767 }
emilmont 1:46ce1e16c870 3768 tbl[BPB_Media] = md; /* Media descriptor */
emilmont 1:46ce1e16c870 3769 ST_WORD(tbl+BPB_SecPerTrk, 63); /* Number of sectors per track */
emilmont 1:46ce1e16c870 3770 ST_WORD(tbl+BPB_NumHeads, 255); /* Number of heads */
emilmont 1:46ce1e16c870 3771 ST_DWORD(tbl+BPB_HiddSec, b_vol); /* Hidden sectors */
emilmont 1:46ce1e16c870 3772 n = get_fattime(); /* Use current time as VSN */
emilmont 1:46ce1e16c870 3773 if (fmt == FS_FAT32) {
emilmont 1:46ce1e16c870 3774 ST_DWORD(tbl+BS_VolID32, n); /* VSN */
emilmont 1:46ce1e16c870 3775 ST_DWORD(tbl+BPB_FATSz32, n_fat); /* Number of sectors per FAT */
emilmont 1:46ce1e16c870 3776 ST_DWORD(tbl+BPB_RootClus, 2); /* Root directory start cluster (2) */
emilmont 1:46ce1e16c870 3777 ST_WORD(tbl+BPB_FSInfo, 1); /* FSInfo record offset (VBR+1) */
emilmont 1:46ce1e16c870 3778 ST_WORD(tbl+BPB_BkBootSec, 6); /* Backup boot record offset (VBR+6) */
emilmont 1:46ce1e16c870 3779 tbl[BS_DrvNum32] = 0x80; /* Drive number */
emilmont 1:46ce1e16c870 3780 tbl[BS_BootSig32] = 0x29; /* Extended boot signature */
emilmont 1:46ce1e16c870 3781 mem_cpy(tbl+BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */
emilmont 1:46ce1e16c870 3782 } else {
emilmont 1:46ce1e16c870 3783 ST_DWORD(tbl+BS_VolID, n); /* VSN */
emilmont 1:46ce1e16c870 3784 ST_WORD(tbl+BPB_FATSz16, n_fat); /* Number of sectors per FAT */
emilmont 1:46ce1e16c870 3785 tbl[BS_DrvNum] = 0x80; /* Drive number */
emilmont 1:46ce1e16c870 3786 tbl[BS_BootSig] = 0x29; /* Extended boot signature */
emilmont 1:46ce1e16c870 3787 mem_cpy(tbl+BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */
emilmont 1:46ce1e16c870 3788 }
emilmont 1:46ce1e16c870 3789 ST_WORD(tbl+BS_55AA, 0xAA55); /* Signature (Offset is fixed here regardless of sector size) */
emilmont 1:46ce1e16c870 3790 if (disk_write(pdrv, tbl, b_vol, 1) != RES_OK) /* Write it to the VBR sector */
emilmont 1:46ce1e16c870 3791 return FR_DISK_ERR;
emilmont 1:46ce1e16c870 3792 if (fmt == FS_FAT32) /* Write backup VBR if needed (VBR+6) */
emilmont 1:46ce1e16c870 3793 disk_write(pdrv, tbl, b_vol + 6, 1);
emilmont 1:46ce1e16c870 3794
emilmont 1:46ce1e16c870 3795 /* Initialize FAT area */
emilmont 1:46ce1e16c870 3796 wsect = b_fat;
emilmont 1:46ce1e16c870 3797 for (i = 0; i < N_FATS; i++) { /* Initialize each FAT copy */
emilmont 1:46ce1e16c870 3798 mem_set(tbl, 0, SS(fs)); /* 1st sector of the FAT */
emilmont 1:46ce1e16c870 3799 n = md; /* Media descriptor byte */
emilmont 1:46ce1e16c870 3800 if (fmt != FS_FAT32) {
emilmont 1:46ce1e16c870 3801 n |= (fmt == FS_FAT12) ? 0x00FFFF00 : 0xFFFFFF00;
emilmont 1:46ce1e16c870 3802 ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT12/16) */
emilmont 1:46ce1e16c870 3803 } else {
emilmont 1:46ce1e16c870 3804 n |= 0xFFFFFF00;
emilmont 1:46ce1e16c870 3805 ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT32) */
emilmont 1:46ce1e16c870 3806 ST_DWORD(tbl+4, 0xFFFFFFFF);
emilmont 1:46ce1e16c870 3807 ST_DWORD(tbl+8, 0x0FFFFFFF); /* Reserve cluster #2 for root dir */
emilmont 1:46ce1e16c870 3808 }
emilmont 1:46ce1e16c870 3809 if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
emilmont 1:46ce1e16c870 3810 return FR_DISK_ERR;
emilmont 1:46ce1e16c870 3811 mem_set(tbl, 0, SS(fs)); /* Fill following FAT entries with zero */
emilmont 1:46ce1e16c870 3812 for (n = 1; n < n_fat; n++) { /* This loop may take a time on FAT32 volume due to many single sector writes */
emilmont 1:46ce1e16c870 3813 if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
emilmont 1:46ce1e16c870 3814 return FR_DISK_ERR;
emilmont 1:46ce1e16c870 3815 }
emilmont 1:46ce1e16c870 3816 }
emilmont 1:46ce1e16c870 3817
emilmont 1:46ce1e16c870 3818 /* Initialize root directory */
emilmont 1:46ce1e16c870 3819 i = (fmt == FS_FAT32) ? au : n_dir;
emilmont 1:46ce1e16c870 3820 do {
emilmont 1:46ce1e16c870 3821 if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
emilmont 1:46ce1e16c870 3822 return FR_DISK_ERR;
emilmont 1:46ce1e16c870 3823 } while (--i);
emilmont 1:46ce1e16c870 3824
emilmont 1:46ce1e16c870 3825 #if _USE_ERASE /* Erase data area if needed */
emilmont 1:46ce1e16c870 3826 {
emilmont 1:46ce1e16c870 3827 DWORD eb[2];
emilmont 1:46ce1e16c870 3828
emilmont 1:46ce1e16c870 3829 eb[0] = wsect; eb[1] = wsect + (n_clst - ((fmt == FS_FAT32) ? 1 : 0)) * au - 1;
emilmont 1:46ce1e16c870 3830 disk_ioctl(pdrv, CTRL_ERASE_SECTOR, eb);
emilmont 1:46ce1e16c870 3831 }
emilmont 1:46ce1e16c870 3832 #endif
emilmont 1:46ce1e16c870 3833
emilmont 1:46ce1e16c870 3834 /* Create FSInfo if needed */
emilmont 1:46ce1e16c870 3835 if (fmt == FS_FAT32) {
emilmont 1:46ce1e16c870 3836 ST_DWORD(tbl+FSI_LeadSig, 0x41615252);
emilmont 1:46ce1e16c870 3837 ST_DWORD(tbl+FSI_StrucSig, 0x61417272);
emilmont 1:46ce1e16c870 3838 ST_DWORD(tbl+FSI_Free_Count, n_clst - 1); /* Number of free clusters */
emilmont 1:46ce1e16c870 3839 ST_DWORD(tbl+FSI_Nxt_Free, 2); /* Last allocated cluster# */
emilmont 1:46ce1e16c870 3840 ST_WORD(tbl+BS_55AA, 0xAA55);
emilmont 1:46ce1e16c870 3841 disk_write(pdrv, tbl, b_vol + 1, 1); /* Write original (VBR+1) */
emilmont 1:46ce1e16c870 3842 disk_write(pdrv, tbl, b_vol + 7, 1); /* Write backup (VBR+7) */
emilmont 1:46ce1e16c870 3843 }
emilmont 1:46ce1e16c870 3844
emilmont 1:46ce1e16c870 3845 return (disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR;
emilmont 1:46ce1e16c870 3846 }
emilmont 1:46ce1e16c870 3847
emilmont 1:46ce1e16c870 3848
emilmont 1:46ce1e16c870 3849 #if _MULTI_PARTITION == 2
emilmont 1:46ce1e16c870 3850 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3851 /* Divide Physical Drive */
emilmont 1:46ce1e16c870 3852 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3853
emilmont 1:46ce1e16c870 3854 FRESULT f_fdisk (
emilmont 1:46ce1e16c870 3855 BYTE pdrv, /* Physical drive number */
emilmont 1:46ce1e16c870 3856 const DWORD szt[], /* Pointer to the size table for each partitions */
emilmont 1:46ce1e16c870 3857 void* work /* Pointer to the working buffer */
emilmont 1:46ce1e16c870 3858 )
emilmont 1:46ce1e16c870 3859 {
emilmont 1:46ce1e16c870 3860 UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl;
emilmont 1:46ce1e16c870 3861 BYTE s_hd, e_hd, *p, *buf = (BYTE*)work;
emilmont 1:46ce1e16c870 3862 DSTATUS stat;
emilmont 1:46ce1e16c870 3863 DWORD sz_disk, sz_part, s_part;
emilmont 1:46ce1e16c870 3864
emilmont 1:46ce1e16c870 3865
emilmont 1:46ce1e16c870 3866 stat = disk_initialize(pdrv);
emilmont 1:46ce1e16c870 3867 if (stat & STA_NOINIT) return FR_NOT_READY;
emilmont 1:46ce1e16c870 3868 if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
emilmont 1:46ce1e16c870 3869 if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk)) return FR_DISK_ERR;
emilmont 1:46ce1e16c870 3870
emilmont 1:46ce1e16c870 3871 /* Determine CHS in the table regardless of the drive geometry */
emilmont 1:46ce1e16c870 3872 for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ;
emilmont 1:46ce1e16c870 3873 if (n == 256) n--;
emilmont 1:46ce1e16c870 3874 e_hd = n - 1;
emilmont 1:46ce1e16c870 3875 sz_cyl = 63 * n;
emilmont 1:46ce1e16c870 3876 tot_cyl = sz_disk / sz_cyl;
emilmont 1:46ce1e16c870 3877
emilmont 1:46ce1e16c870 3878 /* Create partition table */
emilmont 1:46ce1e16c870 3879 mem_set(buf, 0, _MAX_SS);
emilmont 1:46ce1e16c870 3880 p = buf + MBR_Table; b_cyl = 0;
emilmont 1:46ce1e16c870 3881 for (i = 0; i < 4; i++, p += SZ_PTE) {
emilmont 1:46ce1e16c870 3882 p_cyl = (szt[i] <= 100) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl;
emilmont 1:46ce1e16c870 3883 if (!p_cyl) continue;
emilmont 1:46ce1e16c870 3884 s_part = (DWORD)sz_cyl * b_cyl;
emilmont 1:46ce1e16c870 3885 sz_part = (DWORD)sz_cyl * p_cyl;
emilmont 1:46ce1e16c870 3886 if (i == 0) { /* Exclude first track of cylinder 0 */
emilmont 1:46ce1e16c870 3887 s_hd = 1;
emilmont 1:46ce1e16c870 3888 s_part += 63; sz_part -= 63;
emilmont 1:46ce1e16c870 3889 } else {
emilmont 1:46ce1e16c870 3890 s_hd = 0;
emilmont 1:46ce1e16c870 3891 }
emilmont 1:46ce1e16c870 3892 e_cyl = b_cyl + p_cyl - 1;
emilmont 1:46ce1e16c870 3893 if (e_cyl >= tot_cyl) return FR_INVALID_PARAMETER;
emilmont 1:46ce1e16c870 3894
emilmont 1:46ce1e16c870 3895 /* Set partition table */
emilmont 1:46ce1e16c870 3896 p[1] = s_hd; /* Start head */
emilmont 1:46ce1e16c870 3897 p[2] = (BYTE)((b_cyl >> 2) + 1); /* Start sector */
emilmont 1:46ce1e16c870 3898 p[3] = (BYTE)b_cyl; /* Start cylinder */
emilmont 1:46ce1e16c870 3899 p[4] = 0x06; /* System type (temporary setting) */
emilmont 1:46ce1e16c870 3900 p[5] = e_hd; /* End head */
emilmont 1:46ce1e16c870 3901 p[6] = (BYTE)((e_cyl >> 2) + 63); /* End sector */
emilmont 1:46ce1e16c870 3902 p[7] = (BYTE)e_cyl; /* End cylinder */
emilmont 1:46ce1e16c870 3903 ST_DWORD(p + 8, s_part); /* Start sector in LBA */
emilmont 1:46ce1e16c870 3904 ST_DWORD(p + 12, sz_part); /* Partition size */
emilmont 1:46ce1e16c870 3905
emilmont 1:46ce1e16c870 3906 /* Next partition */
emilmont 1:46ce1e16c870 3907 b_cyl += p_cyl;
emilmont 1:46ce1e16c870 3908 }
emilmont 1:46ce1e16c870 3909 ST_WORD(p, 0xAA55);
emilmont 1:46ce1e16c870 3910
emilmont 1:46ce1e16c870 3911 /* Write it to the MBR */
emilmont 1:46ce1e16c870 3912 return (disk_write(pdrv, buf, 0, 1) || disk_ioctl(pdrv, CTRL_SYNC, 0)) ? FR_DISK_ERR : FR_OK;
emilmont 1:46ce1e16c870 3913 }
emilmont 1:46ce1e16c870 3914
emilmont 1:46ce1e16c870 3915
emilmont 1:46ce1e16c870 3916 #endif /* _MULTI_PARTITION == 2 */
emilmont 1:46ce1e16c870 3917 #endif /* _USE_MKFS && !_FS_READONLY */
emilmont 1:46ce1e16c870 3918
emilmont 1:46ce1e16c870 3919
emilmont 1:46ce1e16c870 3920
emilmont 1:46ce1e16c870 3921
emilmont 1:46ce1e16c870 3922 #if _USE_STRFUNC
emilmont 1:46ce1e16c870 3923 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3924 /* Get a string from the file */
emilmont 1:46ce1e16c870 3925 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3926 TCHAR* f_gets (
emilmont 1:46ce1e16c870 3927 TCHAR* buff, /* Pointer to the string buffer to read */
emilmont 1:46ce1e16c870 3928 int len, /* Size of string buffer (characters) */
emilmont 1:46ce1e16c870 3929 FIL* fil /* Pointer to the file object */
emilmont 1:46ce1e16c870 3930 )
emilmont 1:46ce1e16c870 3931 {
emilmont 1:46ce1e16c870 3932 int n = 0;
emilmont 1:46ce1e16c870 3933 TCHAR c, *p = buff;
emilmont 1:46ce1e16c870 3934 BYTE s[2];
emilmont 1:46ce1e16c870 3935 UINT rc;
emilmont 1:46ce1e16c870 3936
emilmont 1:46ce1e16c870 3937
emilmont 1:46ce1e16c870 3938 while (n < len - 1) { /* Read bytes until buffer gets filled */
emilmont 1:46ce1e16c870 3939 f_read(fil, s, 1, &rc);
emilmont 1:46ce1e16c870 3940 if (rc != 1) break; /* Break on EOF or error */
emilmont 1:46ce1e16c870 3941 c = s[0];
emilmont 1:46ce1e16c870 3942 #if _LFN_UNICODE /* Read a character in UTF-8 encoding */
emilmont 1:46ce1e16c870 3943 if (c >= 0x80) {
emilmont 1:46ce1e16c870 3944 if (c < 0xC0) continue; /* Skip stray trailer */
emilmont 1:46ce1e16c870 3945 if (c < 0xE0) { /* Two-byte sequence */
emilmont 1:46ce1e16c870 3946 f_read(fil, s, 1, &rc);
emilmont 1:46ce1e16c870 3947 if (rc != 1) break;
emilmont 1:46ce1e16c870 3948 c = ((c & 0x1F) << 6) | (s[0] & 0x3F);
emilmont 1:46ce1e16c870 3949 if (c < 0x80) c = '?';
emilmont 1:46ce1e16c870 3950 } else {
emilmont 1:46ce1e16c870 3951 if (c < 0xF0) { /* Three-byte sequence */
emilmont 1:46ce1e16c870 3952 f_read(fil, s, 2, &rc);
emilmont 1:46ce1e16c870 3953 if (rc != 2) break;
emilmont 1:46ce1e16c870 3954 c = (c << 12) | ((s[0] & 0x3F) << 6) | (s[1] & 0x3F);
emilmont 1:46ce1e16c870 3955 if (c < 0x800) c = '?';
emilmont 1:46ce1e16c870 3956 } else { /* Reject four-byte sequence */
emilmont 1:46ce1e16c870 3957 c = '?';
emilmont 1:46ce1e16c870 3958 }
emilmont 1:46ce1e16c870 3959 }
emilmont 1:46ce1e16c870 3960 }
emilmont 1:46ce1e16c870 3961 #endif
emilmont 1:46ce1e16c870 3962 #if _USE_STRFUNC >= 2
emilmont 1:46ce1e16c870 3963 if (c == '\r') continue; /* Strip '\r' */
emilmont 1:46ce1e16c870 3964 #endif
emilmont 1:46ce1e16c870 3965 *p++ = c;
emilmont 1:46ce1e16c870 3966 n++;
emilmont 1:46ce1e16c870 3967 if (c == '\n') break; /* Break on EOL */
emilmont 1:46ce1e16c870 3968 }
emilmont 1:46ce1e16c870 3969 *p = 0;
emilmont 1:46ce1e16c870 3970 return n ? buff : 0; /* When no data read (eof or error), return with error. */
emilmont 1:46ce1e16c870 3971 }
emilmont 1:46ce1e16c870 3972
emilmont 1:46ce1e16c870 3973
emilmont 1:46ce1e16c870 3974
emilmont 1:46ce1e16c870 3975 #if !_FS_READONLY
emilmont 1:46ce1e16c870 3976 #include <stdarg.h>
emilmont 1:46ce1e16c870 3977 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3978 /* Put a character to the file */
emilmont 1:46ce1e16c870 3979 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 3980 int f_putc (
emilmont 1:46ce1e16c870 3981 TCHAR c, /* A character to be output */
emilmont 1:46ce1e16c870 3982 FIL* fil /* Pointer to the file object */
emilmont 1:46ce1e16c870 3983 )
emilmont 1:46ce1e16c870 3984 {
emilmont 1:46ce1e16c870 3985 UINT bw, btw;
emilmont 1:46ce1e16c870 3986 BYTE s[3];
emilmont 1:46ce1e16c870 3987
emilmont 1:46ce1e16c870 3988
emilmont 1:46ce1e16c870 3989 #if _USE_STRFUNC >= 2
emilmont 1:46ce1e16c870 3990 if (c == '\n') f_putc ('\r', fil); /* LF -> CRLF conversion */
emilmont 1:46ce1e16c870 3991 #endif
emilmont 1:46ce1e16c870 3992
emilmont 1:46ce1e16c870 3993 #if _LFN_UNICODE /* Write the character in UTF-8 encoding */
emilmont 1:46ce1e16c870 3994 if (c < 0x80) { /* 7-bit */
emilmont 1:46ce1e16c870 3995 s[0] = (BYTE)c;
emilmont 1:46ce1e16c870 3996 btw = 1;
emilmont 1:46ce1e16c870 3997 } else {
emilmont 1:46ce1e16c870 3998 if (c < 0x800) { /* 11-bit */
emilmont 1:46ce1e16c870 3999 s[0] = (BYTE)(0xC0 | (c >> 6));
emilmont 1:46ce1e16c870 4000 s[1] = (BYTE)(0x80 | (c & 0x3F));
emilmont 1:46ce1e16c870 4001 btw = 2;
emilmont 1:46ce1e16c870 4002 } else { /* 16-bit */
emilmont 1:46ce1e16c870 4003 s[0] = (BYTE)(0xE0 | (c >> 12));
emilmont 1:46ce1e16c870 4004 s[1] = (BYTE)(0x80 | ((c >> 6) & 0x3F));
emilmont 1:46ce1e16c870 4005 s[2] = (BYTE)(0x80 | (c & 0x3F));
emilmont 1:46ce1e16c870 4006 btw = 3;
emilmont 1:46ce1e16c870 4007 }
emilmont 1:46ce1e16c870 4008 }
emilmont 1:46ce1e16c870 4009 #else /* Write the character without conversion */
emilmont 1:46ce1e16c870 4010 s[0] = (BYTE)c;
emilmont 1:46ce1e16c870 4011 btw = 1;
emilmont 1:46ce1e16c870 4012 #endif
emilmont 1:46ce1e16c870 4013 f_write(fil, s, btw, &bw); /* Write the char to the file */
emilmont 1:46ce1e16c870 4014 return (bw == btw) ? 1 : EOF; /* Return the result */
emilmont 1:46ce1e16c870 4015 }
emilmont 1:46ce1e16c870 4016
emilmont 1:46ce1e16c870 4017
emilmont 1:46ce1e16c870 4018
emilmont 1:46ce1e16c870 4019
emilmont 1:46ce1e16c870 4020 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 4021 /* Put a string to the file */
emilmont 1:46ce1e16c870 4022 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 4023 int f_puts (
emilmont 1:46ce1e16c870 4024 const TCHAR* str, /* Pointer to the string to be output */
emilmont 1:46ce1e16c870 4025 FIL* fil /* Pointer to the file object */
emilmont 1:46ce1e16c870 4026 )
emilmont 1:46ce1e16c870 4027 {
emilmont 1:46ce1e16c870 4028 int n;
emilmont 1:46ce1e16c870 4029
emilmont 1:46ce1e16c870 4030
emilmont 1:46ce1e16c870 4031 for (n = 0; *str; str++, n++) {
emilmont 1:46ce1e16c870 4032 if (f_putc(*str, fil) == EOF) return EOF;
emilmont 1:46ce1e16c870 4033 }
emilmont 1:46ce1e16c870 4034 return n;
emilmont 1:46ce1e16c870 4035 }
emilmont 1:46ce1e16c870 4036
emilmont 1:46ce1e16c870 4037
emilmont 1:46ce1e16c870 4038
emilmont 1:46ce1e16c870 4039
emilmont 1:46ce1e16c870 4040 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 4041 /* Put a formatted string to the file */
emilmont 1:46ce1e16c870 4042 /*-----------------------------------------------------------------------*/
emilmont 1:46ce1e16c870 4043 int f_printf (
emilmont 1:46ce1e16c870 4044 FIL* fil, /* Pointer to the file object */
emilmont 1:46ce1e16c870 4045 const TCHAR* str, /* Pointer to the format string */
emilmont 1:46ce1e16c870 4046 ... /* Optional arguments... */
emilmont 1:46ce1e16c870 4047 )
emilmont 1:46ce1e16c870 4048 {
emilmont 1:46ce1e16c870 4049 va_list arp;
emilmont 1:46ce1e16c870 4050 BYTE f, r;
emilmont 1:46ce1e16c870 4051 UINT i, j, w;
emilmont 1:46ce1e16c870 4052 ULONG v;
emilmont 1:46ce1e16c870 4053 TCHAR c, d, s[16], *p;
emilmont 1:46ce1e16c870 4054 int res, chc, cc;
emilmont 1:46ce1e16c870 4055
emilmont 1:46ce1e16c870 4056
emilmont 1:46ce1e16c870 4057 va_start(arp, str);
emilmont 1:46ce1e16c870 4058
emilmont 1:46ce1e16c870 4059 for (cc = res = 0; cc != EOF; res += cc) {
emilmont 1:46ce1e16c870 4060 c = *str++;
emilmont 1:46ce1e16c870 4061 if (c == 0) break; /* End of string */
emilmont 1:46ce1e16c870 4062 if (c != '%') { /* Non escape character */
emilmont 1:46ce1e16c870 4063 cc = f_putc(c, fil);
emilmont 1:46ce1e16c870 4064 if (cc != EOF) cc = 1;
emilmont 1:46ce1e16c870 4065 continue;
emilmont 1:46ce1e16c870 4066 }
emilmont 1:46ce1e16c870 4067 w = f = 0;
emilmont 1:46ce1e16c870 4068 c = *str++;
emilmont 1:46ce1e16c870 4069 if (c == '0') { /* Flag: '0' padding */
emilmont 1:46ce1e16c870 4070 f = 1; c = *str++;
emilmont 1:46ce1e16c870 4071 } else {
emilmont 1:46ce1e16c870 4072 if (c == '-') { /* Flag: left justified */
emilmont 1:46ce1e16c870 4073 f = 2; c = *str++;
emilmont 1:46ce1e16c870 4074 }
emilmont 1:46ce1e16c870 4075 }
emilmont 1:46ce1e16c870 4076 while (IsDigit(c)) { /* Precision */
emilmont 1:46ce1e16c870 4077 w = w * 10 + c - '0';
emilmont 1:46ce1e16c870 4078 c = *str++;
emilmont 1:46ce1e16c870 4079 }
emilmont 1:46ce1e16c870 4080 if (c == 'l' || c == 'L') { /* Prefix: Size is long int */
emilmont 1:46ce1e16c870 4081 f |= 4; c = *str++;
emilmont 1:46ce1e16c870 4082 }
emilmont 1:46ce1e16c870 4083 if (!c) break;
emilmont 1:46ce1e16c870 4084 d = c;
emilmont 1:46ce1e16c870 4085 if (IsLower(d)) d -= 0x20;
emilmont 1:46ce1e16c870 4086 switch (d) { /* Type is... */
emilmont 1:46ce1e16c870 4087 case 'S' : /* String */
emilmont 1:46ce1e16c870 4088 p = va_arg(arp, TCHAR*);
emilmont 1:46ce1e16c870 4089 for (j = 0; p[j]; j++) ;
emilmont 1:46ce1e16c870 4090 chc = 0;
emilmont 1:46ce1e16c870 4091 if (!(f & 2)) {
emilmont 1:46ce1e16c870 4092 while (j++ < w) chc += (cc = f_putc(' ', fil));
emilmont 1:46ce1e16c870 4093 }
emilmont 1:46ce1e16c870 4094 chc += (cc = f_puts(p, fil));
emilmont 1:46ce1e16c870 4095 while (j++ < w) chc += (cc = f_putc(' ', fil));
emilmont 1:46ce1e16c870 4096 if (cc != EOF) cc = chc;
emilmont 1:46ce1e16c870 4097 continue;
emilmont 1:46ce1e16c870 4098 case 'C' : /* Character */
emilmont 1:46ce1e16c870 4099 cc = f_putc((TCHAR)va_arg(arp, int), fil); continue;
emilmont 1:46ce1e16c870 4100 case 'B' : /* Binary */
emilmont 1:46ce1e16c870 4101 r = 2; break;
emilmont 1:46ce1e16c870 4102 case 'O' : /* Octal */
emilmont 1:46ce1e16c870 4103 r = 8; break;
emilmont 1:46ce1e16c870 4104 case 'D' : /* Signed decimal */
emilmont 1:46ce1e16c870 4105 case 'U' : /* Unsigned decimal */
emilmont 1:46ce1e16c870 4106 r = 10; break;
emilmont 1:46ce1e16c870 4107 case 'X' : /* Hexdecimal */
emilmont 1:46ce1e16c870 4108 r = 16; break;
emilmont 1:46ce1e16c870 4109 default: /* Unknown type (pass-through) */
emilmont 1:46ce1e16c870 4110 cc = f_putc(c, fil); continue;
emilmont 1:46ce1e16c870 4111 }
emilmont 1:46ce1e16c870 4112
emilmont 1:46ce1e16c870 4113 /* Get an argument and put it in numeral */
emilmont 1:46ce1e16c870 4114 v = (f & 4) ? (ULONG)va_arg(arp, long) : ((d == 'D') ? (ULONG)(long)va_arg(arp, int) : (ULONG)va_arg(arp, unsigned int));
emilmont 1:46ce1e16c870 4115 if (d == 'D' && (v & 0x80000000)) {
emilmont 1:46ce1e16c870 4116 v = 0 - v;
emilmont 1:46ce1e16c870 4117 f |= 8;
emilmont 1:46ce1e16c870 4118 }
emilmont 1:46ce1e16c870 4119 i = 0;
emilmont 1:46ce1e16c870 4120 do {
emilmont 1:46ce1e16c870 4121 d = (TCHAR)(v % r); v /= r;
emilmont 1:46ce1e16c870 4122 if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
emilmont 1:46ce1e16c870 4123 s[i++] = d + '0';
emilmont 1:46ce1e16c870 4124 } while (v && i < sizeof s / sizeof s[0]);
emilmont 1:46ce1e16c870 4125 if (f & 8) s[i++] = '-';
emilmont 1:46ce1e16c870 4126 j = i; d = (f & 1) ? '0' : ' ';
emilmont 1:46ce1e16c870 4127 res = 0;
emilmont 1:46ce1e16c870 4128 while (!(f & 2) && j++ < w) res += (cc = f_putc(d, fil));
emilmont 1:46ce1e16c870 4129 do res += (cc = f_putc(s[--i], fil)); while(i);
emilmont 1:46ce1e16c870 4130 while (j++ < w) res += (cc = f_putc(' ', fil));
emilmont 1:46ce1e16c870 4131 if (cc != EOF) cc = res;
emilmont 1:46ce1e16c870 4132 }
emilmont 1:46ce1e16c870 4133
emilmont 1:46ce1e16c870 4134 va_end(arp);
emilmont 1:46ce1e16c870 4135 return (cc == EOF) ? cc : res;
emilmont 1:46ce1e16c870 4136 }
emilmont 1:46ce1e16c870 4137
emilmont 1:46ce1e16c870 4138 #endif /* !_FS_READONLY */
emilmont 1:46ce1e16c870 4139 #endif /* _USE_STRFUNC */