Norimasa Okamoto / F32RFileSystem

Fork of F12RFileSystem by Norimasa Okamoto

Revision:
8:661b3ce0e9c0
Parent:
7:f9f52d9c0c57
Child:
9:e9843d731180
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/F32RFileSystem.cpp	Sat Nov 14 01:40:04 2015 +0000
@@ -0,0 +1,188 @@
+/* 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 "F32RFileSystem.h"
+#include "F32RFileHandle.h"
+#include "F32RDirHandle.h"
+#include "fsdebug.h"
+
+F32RFileSystem::F32RFileSystem(StorageInterface* storage_, const char* n)
+    : FileSystemLike(n), storage(storage_), mounted(false) {
+}
+
+F32RFileSystem::~F32RFileSystem() {
+}
+
+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 *F32RFileSystem::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 F32RFileHandle(*this, base_dir * 512 + i * 32);
+        }
+    }
+    return NULL;
+}
+
+int F32RFileSystem::remove(const char *filename) {
+    return -1;
+}
+
+int F32RFileSystem::rename(const char *oldname, const char *newname) {
+    return -1;
+}
+
+int F32RFileSystem::format() {
+    return -1;
+}
+
+DirHandle *F32RFileSystem::opendir(const char *name) {
+    FS_DBG("name=[%s]", name);
+
+    if (mount() != 0) {
+        return NULL;
+    }
+    return new F32RDirHandle(*this);
+}
+
+int F32RFileSystem::mkdir(const char *name, mode_t mode) {
+    return -1;
+}
+
+int F32RFileSystem::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 F32RFileSystem::unmount() {
+    mounted = false;
+    return 0;
+}
+
+uint32_t F32RFileSystem::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 F32RFileSystem::fat_read(cluster_t index) {
+    cluster_t next = storage_peek(base_fat*512 + index*4, 4);
+    return next;
+}