IJFW - IchigoJamのBASICプログラムをメモリカード(MMCまたは互換カード)に保存したり読み出したりできるプログラム。メモリカードにファームウェアのファイルを置くだけで、電源ON時に自動的に書き換える機能も搭載(一応こちらがメイン)。LPC1114FN28専用。

Dependencies:   mbed

参考URL http://www.cyberchabudai.org/index.php/entry?tag=IJFW

Committer:
oks486
Date:
Sun Aug 21 07:51:01 2016 +0000
Revision:
2:daf6c4719496
Parent:
0:43cce7b453d0
Modified I2c2mem for "FILES" command

Who changed what in which revision?

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