Updated get_fattime to use rtc and provide a date/time. This has been an annoying missing feature.

Dependents:   IoTGateway_Basic y_XBeeTest_5_read CameraC1098_picture LifeCam ... more

Committer:
SomeRandomBloke
Date:
Mon Apr 02 22:06:35 2012 +0000
Revision:
1:5baba5d5b728
Parent:
0:93acdd9f65f4

        

Who changed what in which revision?

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