This is code is part of a Technion course project in advanced IoT, implementing a device to receive and present sensors data from a Formula racing car built by students at Technion - Israel Institute of Technology.

Dependencies:   mbed Buffer

Fork of DISCO-L072CZ-LRWAN1_LoRa_PingPong by ST

This is code is part of a Technion course project in advanced IoT, implementing a device to receive sensors data from another L072CZ-LRWAN1 installed on a Formula racing car (built by students at Technion - Israel Institute of Technology), and sends it to a GUI presenting the data (GUI project: github.com/ward-mattar/TechnionFormulaGUI).

How to install

  • Create an account on Mbed: https://os.mbed.com/account/signup/
  • Import project into Compiler
  • In the Program Workspace select "Formula_Nucleo_Receiver"
  • Select a Platform like so:
  1. Click button at top-left
  2. Add Board
  3. Search "NUCLEO F103RB" and then "Add to your Mbed Compiler"
  • Finally click "Compile", if the build was successful, the binary would download automatically
  • To install it on device simply plug it in to a PC, open device drive and drag then drop binary file in it
Committer:
wardm
Date:
Sat May 19 15:42:38 2018 +0000
Revision:
12:046346a16ff4
V1.0.0

Who changed what in which revision?

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