this library has few changes from the original library, for effects of this work.

Dependents:   OBC3_1_h

Committer:
FannyCalle
Date:
Mon May 21 15:10:37 2018 +0000
Revision:
0:8214896432e0
el sd hay que revisar;

Who changed what in which revision?

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