Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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