old repo - SD card read example

Dependents:   SDFileSystem

Fork of FATFileSystem by mbed official

Committer:
mbed_official
Date:
Thu Nov 26 13:30:57 2015 +0000
Revision:
6:a5fcdbf92056
Parent:
5:b3b3370574cf
Synchronized with git revision 7fcda4012c43cb7904b7250986e326d5c007a32a

Full URL: https://github.com/mbedmicro/mbed/commit/7fcda4012c43cb7904b7250986e326d5c007a32a/

[FATFileSystem] Updated FatFs to R0.11a & fixed sync bug

Who changed what in which revision?

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