Xbee CountUp

Dependencies:   mbed

Fork of HeptaXbee_CountUp by 智也 大野

Committer:
tomoya123
Date:
Fri Dec 09 04:58:00 2016 +0000
Revision:
0:0a7fa0911e6c
Xbee CountUP

Who changed what in which revision?

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