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