Library for SD card

Dependents:   SDFileSystem_HelloWorld Sharp_ce140f_emul

Committer:
ffxx68
Date:
Tue Jul 19 13:49:28 2022 +0000
Revision:
2:02f003d025a7
Parent:
0:3bdfc1556537
SD Card handling added

Who changed what in which revision?

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