test

Fork of FATFileSystem by mbed official

Committer:
mbed_official
Date:
Thu Aug 13 10:15:39 2015 +0100
Revision:
5:b3b3370574cf
Parent:
4:3ff2606d5713
Child:
6:a5fcdbf92056
Synchronized with git revision ab6a668ec18ad1530649c4cf1067a42816c6a7f8

Full URL: https://github.com/mbedmicro/mbed/commit/ab6a668ec18ad1530649c4cf1067a42816c6a7f8/

FATFileSystem - Updated FatFs to R0.11

Who changed what in which revision?

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