うおーるぼっとをWiiリモコンでコントロールする新しいプログラムです。 以前のものより、Wiiリモコンが早く繋がる様になりました。 It is a program which controls A with the Wii remote. ※ A Bluetooth dongle and a Wii remote control are needed.

Dependencies:   USBHost mbed FATFileSystem mbed-rtos

Committer:
jksoft
Date:
Mon Jun 10 16:01:50 2013 +0000
Revision:
0:fccb789424fc
1.0

Who changed what in which revision?

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