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