FAT12 read only file system

Fork of FATFileSystem by mbed official

Revision:
7:f9f52d9c0c57
Parent:
6:3c5b3606e019
Child:
8:6c6acf81ff08
--- a/F12RFileSystem.cpp	Sat Oct 31 00:30:21 2015 +0000
+++ b/F12RFileSystem.cpp	Wed Nov 11 19:47:04 2015 +0900
@@ -1,154 +1,195 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2015 ARM Limited
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-#include "mbed.h"
-
-#include "ffconf.h"
-#include "mbed_debug.h"
-
-#include "F12RFileSystem.h"
-#include "F12RFileHandle.h"
-#include "F12RDirHandle.h"
-
-DWORD get_fattime(void) {
-    time_t rawtime;
-    time(&rawtime);
-    struct tm *ptm = localtime(&rawtime);
-    return (DWORD)(ptm->tm_year - 80) << 25
-         | (DWORD)(ptm->tm_mon + 1  ) << 21
-         | (DWORD)(ptm->tm_mday     ) << 16
-         | (DWORD)(ptm->tm_hour     ) << 11
-         | (DWORD)(ptm->tm_min      ) << 5
-         | (DWORD)(ptm->tm_sec/2    );
-}
-
-F12RFileSystem *F12RFileSystem::_ffs[_VOLUMES] = {0};
-
-F12RFileSystem::F12RFileSystem(const char* n) : FileSystemLike(n) {
-    debug_if(FFS_DBG, "F12RFileSystem(%s)\n", n);
-    for(int i=0; i<_VOLUMES; i++) {
-        if(_ffs[i] == 0) {
-            _ffs[i] = this;
-            _fsid[0] = '0' + i;
-            _fsid[1] = '\0';
-            debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", _name, _fsid);
-            f_mount(&_fs, _fsid, 0);
-            return;
-        }
-    }
-    error("Couldn't create %s in F12RFileSystem::F12RFileSystem\n", n);
-}
-
-F12RFileSystem::~F12RFileSystem() {
-    for (int i=0; i<_VOLUMES; i++) {
-        if (_ffs[i] == this) {
-            _ffs[i] = 0;
-            f_mount(NULL, _fsid, 0);
-        }
-    }
-}
-
-FileHandle *F12RFileSystem::open(const char* name, int flags) {
-    debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, _name, _fsid);
-    char n[64];
-    sprintf(n, "%s:/%s", _fsid, name);
-
-    /* POSIX flags -> FatFS open mode */
-    BYTE openmode;
-    if (flags & O_RDWR) {
-        openmode = FA_READ|FA_WRITE;
-    } else if(flags & O_WRONLY) {
-        openmode = FA_WRITE;
-    } else {
-        openmode = FA_READ;
-    }
-    if(flags & O_CREAT) {
-        if(flags & O_TRUNC) {
-            openmode |= FA_CREATE_ALWAYS;
-        } else {
-            openmode |= FA_OPEN_ALWAYS;
-        }
-    }
-
-    FIL fh;
-    FRESULT res = f_open(&fh, n, openmode);
-    if (res) {
-        debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
-        return NULL;
-    }
-    if (flags & O_APPEND) {
-        f_lseek(&fh, fh.fsize);
-    }
-    return new F12RFileHandle(fh);
-}
-
-int F12RFileSystem::remove(const char *filename) {
-    FRESULT res = f_unlink(filename);
-    if (res) {
-        debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
-        return -1;
-    }
-    return 0;
-}
-
-int F12RFileSystem::rename(const char *oldname, const char *newname) {
-    FRESULT res = f_rename(oldname, newname);
-    if (res) {
-        debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
-        return -1;
-    }
-    return 0;
-}
-
-int F12RFileSystem::format() {
-    FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
-    if (res) {
-        debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
-        return -1;
-    }
-    return 0;
-}
-
-DirHandle *F12RFileSystem::opendir(const char *name) {
-    FATFS_DIR dir;
-    FRESULT res = f_opendir(&dir, name);
-    if (res != 0) {
-        return NULL;
-    }
-    return new F12RDirHandle(dir);
-}
-
-int F12RFileSystem::mkdir(const char *name, mode_t mode) {
-    FRESULT res = f_mkdir(name);
-    return res == 0 ? 0 : -1;
-}
-
-int F12RFileSystem::mount() {
-    FRESULT res = f_mount(&_fs, _fsid, 1);
-    return res == 0 ? 0 : -1;
-}
-
-int F12RFileSystem::unmount() {
-    if (disk_sync())
-        return -1;
-    FRESULT res = f_mount(NULL, _fsid, 0);
-    return res == 0 ? 0 : -1;
-}
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2016 ARM Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include "F12RFileSystem.h"
+#include "F12RFileHandle.h"
+#include "F12RDirHandle.h"
+#include "fsdebug.h"
+
+F12RFileSystem::F12RFileSystem(StorageInterface* storage_, const char* n)
+    : FileSystemLike(n), storage(storage_), mounted(false) {
+}
+
+F12RFileSystem::~F12RFileSystem() {
+}
+
+static bool to_sfn(const char* name, uint8_t* buf) {
+    memset(buf, '\x20', 8+3);
+    for(int i = 0; i < 8; i++) {
+        char c = *name++;
+        if (c == '\0') {
+            return true;
+        } else if (c == '.') {
+            break;
+        } else {
+            buf[i] = toupper(c);
+        }
+    }
+    for(int i = 8; i < 11; i++) {
+        char c = *name++;
+        if (c == '\0') {
+            break;
+        } else {
+            buf[i] = toupper(c);
+        }
+    }
+    return true;
+}
+
+FileHandle *F12RFileSystem::open(const char* name, int flags) {
+    FS_DBG("name=[%s] flags=%d", name, flags);
+    if (mount() != 0) {
+        return NULL;
+    }
+
+#if 1
+    uint8_t fat12[16];
+    storage->storage_read(base_fat * 512, fat12, sizeof(fat12));
+    FS_DBG_HEX(fat12, sizeof(fat12));
+#endif
+
+    uint8_t sfn[8+3];
+    to_sfn(name, sfn);
+    for(int i = 0; i < max_root_dir_entries; i++) {
+        uint8_t buf[sizeof(sfn)];
+        storage->storage_read(base_dir * 512 + i * 32, buf, sizeof(buf));
+        FS_DBG_HEX(buf, sizeof(buf));
+        if (buf[0] == 0x00) {
+            return NULL;
+        } else if (memcmp(buf, sfn, sizeof(sfn)) == 0) {
+            return new F12RFileHandle(*this, base_dir * 512 + i * 32);
+        }
+    }
+    return NULL;
+}
+
+int F12RFileSystem::remove(const char *filename) {
+    return -1;
+}
+
+int F12RFileSystem::rename(const char *oldname, const char *newname) {
+    return -1;
+}
+
+int F12RFileSystem::format() {
+    return -1;
+}
+
+DirHandle *F12RFileSystem::opendir(const char *name) {
+    FS_DBG("name=[%s]", name);
+
+    if (mount() != 0) {
+        return NULL;
+    }
+    return new F12RDirHandle(*this);
+}
+
+int F12RFileSystem::mkdir(const char *name, mode_t mode) {
+    return -1;
+}
+
+int F12RFileSystem::mount() {
+#if 1
+    uint8_t buf[512];
+    storage->storage_read(0, buf, sizeof(buf));
+    FS_DBG_HEX(buf, sizeof(buf));
+#endif
+
+    if (storage_peek(0 * 512 + 510, 2) != 0xaa55) {
+        return -1;
+    }
+    uint32_t lba_offset = 0;
+    struct { // +446
+        uint8_t flag; // +0
+        uint8_t first_sector_chs[3]; // +1
+        uint8_t type; // +4
+        uint8_t last_sector_chs[3]; // +5
+        uint32_t sector_start; // +8
+        uint32_t sector_count; // +12
+    } partition_entry;
+    storage->storage_read(446, (uint8_t*)&partition_entry, sizeof(partition_entry));
+    if (partition_entry.type == 0x01) {
+        lba_offset = partition_entry.sector_start;
+        if (storage_peek(lba_offset * 512 + 510, 2) != 0xaa55) {
+            return -1;
+        }
+    }
+
+#if 1
+    FS_DBG("lba_offset=%d", lba_offset);
+    storage->storage_read(lba_offset * 512, buf, sizeof(buf));
+    FS_DBG_HEX(buf, sizeof(buf));
+#endif
+
+    if (storage_peek(lba_offset * 512 + 11, 2) != 512) {
+        return -1;
+    }
+    cluster_size = storage_peek(lba_offset * 512 + 13, 1) * 512;
+    sector_t number_of_reserved_sectors = storage_peek(lba_offset * 512 + 14, 2);
+    base_fat = lba_offset + number_of_reserved_sectors;
+    int number_of_fats = storage_peek(lba_offset * 512 + 16, 2);
+    max_root_dir_entries = storage_peek(lba_offset * 512 + 17, 2);
+    sector_t total_logical_sectors = storage_peek(lba_offset * 512 + 19, 2);
+    sector_t logical_sectors_per_fat = storage_peek(lba_offset * 512 + 22, 2);
+    base_dir = base_fat + logical_sectors_per_fat * number_of_fats;
+    base_data = base_dir + (max_root_dir_entries * 32 + 511) / 512;
+
+    FS_DBG("number_of_reserved_sectors=%d", number_of_reserved_sectors);
+    FS_DBG("number_of_fats=%d", number_of_fats);
+    FS_DBG("max_root_dir_entries=%d", max_root_dir_entries);
+    FS_DBG("total_logical_sectors=%d", total_logical_sectors);
+    FS_DBG("logical_sectors_per_fat=%d", logical_sectors_per_fat);
+
+    FS_DBG("cluster_size=%d", cluster_size);
+    FS_DBG("base_fat=%d", base_fat);
+    FS_DBG("base_dir=%d", base_dir);
+    FS_DBG("base_data=%d", base_data);
+
+    mounted = true;
+    return 0;
+}
+
+int F12RFileSystem::unmount() {
+    mounted = false;
+    return 0;
+}
+
+uint32_t F12RFileSystem::storage_peek(uint32_t offset, int n) {
+    uint8_t buf[n];
+    storage->storage_read(offset, buf, sizeof(buf));
+    uint32_t val = 0;
+    for(int i = 0; i < n; i++) {
+        val |= buf[i]<<(i*8);
+    }
+    return val;
+}
+
+cluster_t F12RFileSystem::fat_read(cluster_t index) {
+    int i = index / 2 * 3 + (index&1);
+    cluster_t next = storage_peek(base_fat*512 + i, 2);
+    if (index & 1) {
+        next >>= 4;
+    } else {
+        next &= 0xfff;
+    }
+    return next;
+}
+