1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2012 ARM Limited
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #include "mbed_debug.h"
27 #include "FATFileSystem.h"
28 #include "FATFileHandle.h"
29 #include "FATDirHandle.h"
31 DWORD get_fattime(void) {
34 struct tm *ptm = localtime(&rawtime);
35 return (DWORD)(ptm->tm_year - 80) << 25
36 | (DWORD)(ptm->tm_mon + 1 ) << 21
37 | (DWORD)(ptm->tm_mday ) << 16
38 | (DWORD)(ptm->tm_hour ) << 11
39 | (DWORD)(ptm->tm_min ) << 5
40 | (DWORD)(ptm->tm_sec/2 );
43 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
45 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
46 debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
47 for(int i=0; i<_VOLUMES; i++) {
51 debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
56 error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
59 FATFileSystem::~FATFileSystem() {
60 for (int i=0; i<_VOLUMES; i++) {
61 if (_ffs[i] == this) {
68 FileHandle *FATFileSystem::open(const char* name, int flags) {
69 debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
71 sprintf(n, "%d:/%s", _fsid, name);
73 /* POSIX flags -> FatFS open mode */
76 openmode = FA_READ|FA_WRITE;
77 } else if(flags & O_WRONLY) {
84 openmode |= FA_CREATE_ALWAYS;
86 openmode |= FA_OPEN_ALWAYS;
91 FRESULT res = f_open(&fh, n, openmode);
93 debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
96 if (flags & O_APPEND) {
97 f_lseek(&fh, fh.fsize);
99 return new FATFileHandle(fh);
102 int FATFileSystem::remove(const char *filename) {
103 FRESULT res = f_unlink(filename);
105 debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
111 int FATFileSystem::rename(const char *oldname, const char *newname) {
112 FRESULT res = f_rename(oldname, newname);
114 debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
120 int FATFileSystem::format() {
121 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
123 debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
129 DirHandle *FATFileSystem::opendir(const char *name) {
131 FRESULT res = f_opendir(&dir, name);
135 return new FATDirHandle(dir);
138 int FATFileSystem::mkdir(const char *name, mode_t mode) {
139 FRESULT res = f_mkdir(name);
140 return res == 0 ? 0 : -1;
143 int FATFileSystem::mount() {
144 FRESULT res = f_mount(_fsid, &_fs);
145 return res == 0 ? 0 : -1;
148 int FATFileSystem::unmount() {
151 FRESULT res = f_mount(_fsid, NULL);
152 return res == 0 ? 0 : -1;