Revised to support ability to have both SD and USB drives mounted.

Dependents:   Multi-FileSystem Multi-FileSystem

Fork of FATFileSystem by mbed official

Committer:
WiredHome
Date:
Sat Jul 29 12:39:03 2017 +0000
Revision:
12:d816f503fc6f
Parent:
10:e58f6254f8a2
Reduce the number of sectors definition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 9:4000fad3b21f 1 /* mbed Microcontroller Library
WiredHome 9:4000fad3b21f 2 * Copyright (c) 2006-2012 ARM Limited
WiredHome 9:4000fad3b21f 3 *
WiredHome 9:4000fad3b21f 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
WiredHome 9:4000fad3b21f 5 * of this software and associated documentation files (the "Software"), to deal
WiredHome 9:4000fad3b21f 6 * in the Software without restriction, including without limitation the rights
WiredHome 9:4000fad3b21f 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
WiredHome 9:4000fad3b21f 8 * copies of the Software, and to permit persons to whom the Software is
WiredHome 9:4000fad3b21f 9 * furnished to do so, subject to the following conditions:
WiredHome 9:4000fad3b21f 10 *
WiredHome 9:4000fad3b21f 11 * The above copyright notice and this permission notice shall be included in
WiredHome 9:4000fad3b21f 12 * all copies or substantial portions of the Software.
WiredHome 9:4000fad3b21f 13 *
WiredHome 9:4000fad3b21f 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
WiredHome 9:4000fad3b21f 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
WiredHome 9:4000fad3b21f 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
WiredHome 9:4000fad3b21f 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
WiredHome 9:4000fad3b21f 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
WiredHome 9:4000fad3b21f 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
WiredHome 9:4000fad3b21f 20 * SOFTWARE.
WiredHome 9:4000fad3b21f 21 */
WiredHome 9:4000fad3b21f 22 #include "mbed.h"
WiredHome 9:4000fad3b21f 23
WiredHome 9:4000fad3b21f 24 #include "ffconf.h"
WiredHome 9:4000fad3b21f 25 #include "mbed_debug.h"
WiredHome 9:4000fad3b21f 26
WiredHome 9:4000fad3b21f 27 #include "FATFileSystem.h"
WiredHome 9:4000fad3b21f 28 #include "FATFileHandle.h"
WiredHome 9:4000fad3b21f 29 #include "FATDirHandle.h"
WiredHome 9:4000fad3b21f 30
WiredHome 10:e58f6254f8a2 31 // Define the maximum length for a filename.
WiredHome 10:e58f6254f8a2 32 //
WiredHome 10:e58f6254f8a2 33 // Can accept the _MAX_LFN from ffconf, but this could be big (e.g. 255).
WiredHome 10:e58f6254f8a2 34 // Generally this only inflates the stack frame for these functions.
WiredHome 10:e58f6254f8a2 35 // Can also set a smaller number if you know the maximum is less.
WiredHome 10:e58f6254f8a2 36 // example: "0:/12345678.abc\0" would be 16.
WiredHome 10:e58f6254f8a2 37 // keep in mind that something
WiredHome 10:e58f6254f8a2 38 // like "/usb/12345678.abc" is translated to "0:/12345678.abc"
WiredHome 10:e58f6254f8a2 39 //
WiredHome 10:e58f6254f8a2 40 // Various APIs will return the failure code if you try to pass too long
WiredHome 10:e58f6254f8a2 41 // of a path/name in.
WiredHome 10:e58f6254f8a2 42 //
WiredHome 10:e58f6254f8a2 43 #define _MAX_FN_LEN _MAX_LFN
WiredHome 9:4000fad3b21f 44
WiredHome 9:4000fad3b21f 45 //#define DEBUG "FtFS"
WiredHome 9:4000fad3b21f 46 // ...
WiredHome 9:4000fad3b21f 47 // INFO("Stuff to show %d", var); // new-line is automatically appended
WiredHome 9:4000fad3b21f 48 //
WiredHome 9:4000fad3b21f 49 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
WiredHome 9:4000fad3b21f 50 #include "mbed.h"
WiredHome 9:4000fad3b21f 51 #define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 9:4000fad3b21f 52 #define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 9:4000fad3b21f 53 #define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 9:4000fad3b21f 54 static void HexDump(const char * title, const uint8_t * p, int count)
WiredHome 9:4000fad3b21f 55 {
WiredHome 9:4000fad3b21f 56 int i;
WiredHome 9:4000fad3b21f 57 char buf[100] = "0000: ";
WiredHome 9:4000fad3b21f 58
WiredHome 9:4000fad3b21f 59 if (*title)
WiredHome 9:4000fad3b21f 60 INFO("%s", title);
WiredHome 9:4000fad3b21f 61 for (i=0; i<count; ) {
WiredHome 9:4000fad3b21f 62 sprintf(buf + strlen(buf), "%02X ", *(p+i));
WiredHome 9:4000fad3b21f 63 if ((++i & 0x0F) == 0x00) {
WiredHome 9:4000fad3b21f 64 INFO("%s", buf);
WiredHome 9:4000fad3b21f 65 if (i < count)
WiredHome 9:4000fad3b21f 66 sprintf(buf, "%04X: ", i);
WiredHome 9:4000fad3b21f 67 else
WiredHome 9:4000fad3b21f 68 buf[0] = '\0';
WiredHome 9:4000fad3b21f 69 }
WiredHome 9:4000fad3b21f 70 }
WiredHome 9:4000fad3b21f 71 if (strlen(buf))
WiredHome 9:4000fad3b21f 72 INFO("%s", buf);
WiredHome 9:4000fad3b21f 73 }
WiredHome 9:4000fad3b21f 74 #else
WiredHome 9:4000fad3b21f 75 #define INFO(x, ...)
WiredHome 9:4000fad3b21f 76 #define WARN(x, ...)
WiredHome 9:4000fad3b21f 77 #define ERR(x, ...)
WiredHome 9:4000fad3b21f 78 #define HexDump(a, b, c)
WiredHome 9:4000fad3b21f 79 #endif
WiredHome 9:4000fad3b21f 80
WiredHome 9:4000fad3b21f 81 DWORD get_fattime(void) {
WiredHome 9:4000fad3b21f 82 time_t rawtime;
WiredHome 9:4000fad3b21f 83 time(&rawtime);
WiredHome 9:4000fad3b21f 84 struct tm *ptm = localtime(&rawtime);
WiredHome 9:4000fad3b21f 85 return (DWORD)(ptm->tm_year - 80) << 25
WiredHome 9:4000fad3b21f 86 | (DWORD)(ptm->tm_mon + 1 ) << 21
WiredHome 9:4000fad3b21f 87 | (DWORD)(ptm->tm_mday ) << 16
WiredHome 9:4000fad3b21f 88 | (DWORD)(ptm->tm_hour ) << 11
WiredHome 9:4000fad3b21f 89 | (DWORD)(ptm->tm_min ) << 5
WiredHome 9:4000fad3b21f 90 | (DWORD)(ptm->tm_sec/2 );
WiredHome 9:4000fad3b21f 91 }
WiredHome 9:4000fad3b21f 92
WiredHome 9:4000fad3b21f 93 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
WiredHome 9:4000fad3b21f 94
WiredHome 9:4000fad3b21f 95 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
WiredHome 9:4000fad3b21f 96 INFO("FATFileSystem(%s)", n);
WiredHome 9:4000fad3b21f 97 for(int i=0; i<_VOLUMES; i++) {
WiredHome 9:4000fad3b21f 98 if(_ffs[i] == this) // 2nd attempt to mount the same drive
WiredHome 9:4000fad3b21f 99 return;
WiredHome 9:4000fad3b21f 100 if(_ffs[i] == 0) {
WiredHome 9:4000fad3b21f 101 _ffs[i] = this;
WiredHome 9:4000fad3b21f 102 _fsid[0] = '0' + i;
WiredHome 9:4000fad3b21f 103 _fsid[1] = ':';
WiredHome 9:4000fad3b21f 104 _fsid[2] = '\0';
WiredHome 9:4000fad3b21f 105 INFO("Mounting [%s] on ffs drive [%s]", _name, _fsid);
WiredHome 9:4000fad3b21f 106 f_mount(&_fs, _fsid, 0);
WiredHome 9:4000fad3b21f 107 for (int d=0; d<_VOLUMES; d++) {
WiredHome 9:4000fad3b21f 108 INFO(" _ffs[%d] = %p", d, _ffs[d]);
WiredHome 9:4000fad3b21f 109 }
WiredHome 9:4000fad3b21f 110 return;
WiredHome 9:4000fad3b21f 111 }
WiredHome 9:4000fad3b21f 112 }
WiredHome 9:4000fad3b21f 113 error("Couldn't create %s in FATFileSystem::FATFileSystem\r\n", n);
WiredHome 9:4000fad3b21f 114 }
WiredHome 8:f08059355141 115
WiredHome 9:4000fad3b21f 116 FATFileSystem::~FATFileSystem() {
WiredHome 9:4000fad3b21f 117 for (int i=0; i<_VOLUMES; i++) {
WiredHome 9:4000fad3b21f 118 if (_ffs[i] == this) {
WiredHome 9:4000fad3b21f 119 _ffs[i] = 0;
WiredHome 9:4000fad3b21f 120 f_mount(NULL, _fsid, 0);
WiredHome 9:4000fad3b21f 121 }
WiredHome 9:4000fad3b21f 122 }
WiredHome 9:4000fad3b21f 123 }
WiredHome 9:4000fad3b21f 124
WiredHome 9:4000fad3b21f 125 FileHandle *FATFileSystem::open(const char* name, int flags) {
WiredHome 9:4000fad3b21f 126 INFO("open(%s) on filesystem [%s], drv [%s]", name, _name, _fsid);
WiredHome 10:e58f6254f8a2 127 char n[_MAX_FN_LEN];
WiredHome 10:e58f6254f8a2 128 int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name);
WiredHome 10:e58f6254f8a2 129 if (sn < 0)
WiredHome 10:e58f6254f8a2 130 return NULL;
WiredHome 9:4000fad3b21f 131 INFO(" :: open(%s)", n);
WiredHome 9:4000fad3b21f 132
WiredHome 9:4000fad3b21f 133 /* POSIX flags -> FatFS open mode */
WiredHome 9:4000fad3b21f 134 BYTE openmode;
WiredHome 9:4000fad3b21f 135 if (flags & O_RDWR) {
WiredHome 9:4000fad3b21f 136 openmode = FA_READ|FA_WRITE;
WiredHome 9:4000fad3b21f 137 } else if(flags & O_WRONLY) {
WiredHome 9:4000fad3b21f 138 openmode = FA_WRITE;
WiredHome 9:4000fad3b21f 139 } else {
WiredHome 9:4000fad3b21f 140 openmode = FA_READ;
WiredHome 9:4000fad3b21f 141 }
WiredHome 9:4000fad3b21f 142 if(flags & O_CREAT) {
WiredHome 9:4000fad3b21f 143 if(flags & O_TRUNC) {
WiredHome 9:4000fad3b21f 144 openmode |= FA_CREATE_ALWAYS;
WiredHome 9:4000fad3b21f 145 } else {
WiredHome 9:4000fad3b21f 146 openmode |= FA_OPEN_ALWAYS;
WiredHome 9:4000fad3b21f 147 }
WiredHome 9:4000fad3b21f 148 }
WiredHome 9:4000fad3b21f 149
WiredHome 9:4000fad3b21f 150 FIL fh;
WiredHome 9:4000fad3b21f 151 FRESULT res = f_open(&fh, n, openmode);
WiredHome 9:4000fad3b21f 152 if (res) {
WiredHome 9:4000fad3b21f 153 INFO("f_open('w') failed: %d", res);
WiredHome 9:4000fad3b21f 154 return NULL;
WiredHome 9:4000fad3b21f 155 }
WiredHome 9:4000fad3b21f 156 if (flags & O_APPEND) {
WiredHome 9:4000fad3b21f 157 f_lseek(&fh, fh.fsize);
WiredHome 9:4000fad3b21f 158 }
WiredHome 9:4000fad3b21f 159 return new FATFileHandle(fh);
WiredHome 9:4000fad3b21f 160 }
WiredHome 9:4000fad3b21f 161
WiredHome 9:4000fad3b21f 162 int FATFileSystem::remove(const char *name) {
WiredHome 9:4000fad3b21f 163 INFO("remove(%s) on filesystem [%s], drv [%s]", name, _name, _fsid);
WiredHome 10:e58f6254f8a2 164 char n[_MAX_FN_LEN];
WiredHome 10:e58f6254f8a2 165 int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name);
WiredHome 10:e58f6254f8a2 166 if (sn < 0)
WiredHome 10:e58f6254f8a2 167 return -1;
WiredHome 10:e58f6254f8a2 168
WiredHome 9:4000fad3b21f 169 INFO(" :: remove(%s)", n);
WiredHome 9:4000fad3b21f 170 FRESULT res = f_unlink(n);
WiredHome 9:4000fad3b21f 171 if (res) {
WiredHome 9:4000fad3b21f 172 INFO("f_unlink() failed: %d", res);
WiredHome 9:4000fad3b21f 173 return -1;
WiredHome 9:4000fad3b21f 174 }
WiredHome 9:4000fad3b21f 175 return 0;
WiredHome 9:4000fad3b21f 176 }
WiredHome 9:4000fad3b21f 177
WiredHome 9:4000fad3b21f 178 int FATFileSystem::rename(const char *oldname, const char *newname) {
WiredHome 10:e58f6254f8a2 179 char o[_MAX_FN_LEN], n[_MAX_FN_LEN];
WiredHome 10:e58f6254f8a2 180 int sn = snprintf(o, _MAX_FN_LEN, "%s/%s", _fsid, oldname);
WiredHome 10:e58f6254f8a2 181 if (sn < 0)
WiredHome 10:e58f6254f8a2 182 return -1;
WiredHome 10:e58f6254f8a2 183 sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, newname);
WiredHome 10:e58f6254f8a2 184 if (sn < 0)
WiredHome 10:e58f6254f8a2 185 return -1;
WiredHome 10:e58f6254f8a2 186 FRESULT res = f_rename(o, n);
WiredHome 9:4000fad3b21f 187 if (res) {
WiredHome 9:4000fad3b21f 188 INFO("f_rename() failed: %d", res);
WiredHome 10:e58f6254f8a2 189 return -res;
WiredHome 9:4000fad3b21f 190 }
WiredHome 9:4000fad3b21f 191 return 0;
WiredHome 9:4000fad3b21f 192 }
WiredHome 9:4000fad3b21f 193
WiredHome 9:4000fad3b21f 194 int FATFileSystem::format() {
WiredHome 9:4000fad3b21f 195 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
WiredHome 9:4000fad3b21f 196 if (res) {
WiredHome 9:4000fad3b21f 197 INFO("f_mkfs() failed: %d", res);
WiredHome 9:4000fad3b21f 198 return -1;
WiredHome 9:4000fad3b21f 199 }
WiredHome 9:4000fad3b21f 200 return 0;
WiredHome 9:4000fad3b21f 201 }
WiredHome 9:4000fad3b21f 202
WiredHome 9:4000fad3b21f 203 DirHandle *FATFileSystem::opendir(const char *name) {
WiredHome 9:4000fad3b21f 204 FATFS_DIR dir;
WiredHome 10:e58f6254f8a2 205 char n[_MAX_FN_LEN];
WiredHome 10:e58f6254f8a2 206 int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name);
WiredHome 10:e58f6254f8a2 207 if (sn < 0)
WiredHome 10:e58f6254f8a2 208 return NULL;
WiredHome 9:4000fad3b21f 209 FRESULT res = f_opendir(&dir, n);
WiredHome 9:4000fad3b21f 210 if (res != 0) {
WiredHome 9:4000fad3b21f 211 return NULL;
WiredHome 9:4000fad3b21f 212 }
WiredHome 9:4000fad3b21f 213 return new FATDirHandle(dir);
WiredHome 9:4000fad3b21f 214 }
WiredHome 9:4000fad3b21f 215
WiredHome 9:4000fad3b21f 216 int FATFileSystem::mkdir(const char *name, mode_t mode) {
WiredHome 10:e58f6254f8a2 217 char n[_MAX_FN_LEN];
WiredHome 10:e58f6254f8a2 218 int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, name);
WiredHome 10:e58f6254f8a2 219 if (sn < 0)
WiredHome 10:e58f6254f8a2 220 return -1;
WiredHome 9:4000fad3b21f 221 FRESULT res = f_mkdir(n);
WiredHome 9:4000fad3b21f 222 return res == 0 ? 0 : -1;
WiredHome 9:4000fad3b21f 223 }
WiredHome 9:4000fad3b21f 224
WiredHome 9:4000fad3b21f 225 int FATFileSystem::mount() {
WiredHome 9:4000fad3b21f 226 FRESULT res = f_mount(&_fs, _fsid, 1);
WiredHome 9:4000fad3b21f 227 return res == 0 ? 0 : -1;
WiredHome 9:4000fad3b21f 228 }
WiredHome 9:4000fad3b21f 229
WiredHome 9:4000fad3b21f 230 int FATFileSystem::unmount() {
WiredHome 9:4000fad3b21f 231 if (disk_sync())
WiredHome 9:4000fad3b21f 232 return -1;
WiredHome 9:4000fad3b21f 233 FRESULT res = f_mount(NULL, _fsid, 0);
WiredHome 9:4000fad3b21f 234 return res == 0 ? 0 : -1;
WiredHome 9:4000fad3b21f 235 }
WiredHome 9:4000fad3b21f 236
WiredHome 10:e58f6254f8a2 237 #if 0
WiredHome 10:e58f6254f8a2 238 // I think this cannot work unless the FileSystemLike and other interfaces
WiredHome 10:e58f6254f8a2 239 // are updated.
WiredHome 10:e58f6254f8a2 240 int FATFileSystem::fstat(const char *path, FILINFO *fileinfo) {
WiredHome 10:e58f6254f8a2 241 char n[_MAX_FN_LEN];
WiredHome 10:e58f6254f8a2 242 int sn = snprintf(n, _MAX_FN_LEN, "%s/%s", _fsid, path);
WiredHome 10:e58f6254f8a2 243 if (sn < 0)
WiredHome 10:e58f6254f8a2 244 return -1;
WiredHome 10:e58f6254f8a2 245 FRESULT res = f_stat(n, fileinfo);
WiredHome 10:e58f6254f8a2 246 printf("f_stat(%s) returned: %d\r\n", n, res);
WiredHome 10:e58f6254f8a2 247 return res == 0 ? 0 : -res;
WiredHome 10:e58f6254f8a2 248 }
WiredHome 10:e58f6254f8a2 249 #endif