hattori&ide

Dependencies:   mbed

Committer:
hattori_atsushi
Date:
Sun Dec 18 08:16:01 2022 +0000
Revision:
0:f77369cabd75
hattori

Who changed what in which revision?

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