FAT12 read only file system

Fork of FATFileSystem by mbed official

Revision:
8:6c6acf81ff08
Parent:
7:f9f52d9c0c57
Child:
9:d92e85b739a7
--- a/F12RFileSystem.cpp	Wed Nov 11 19:47:04 2015 +0900
+++ b/F12RFileSystem.cpp	Fri Apr 08 06:43:21 2016 +0900
@@ -1,5 +1,5 @@
 /* mbed Microcontroller Library
- * Copyright (c) 2006-2016 ARM Limited
+ * Copyright (c) 2006-2017 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
@@ -43,6 +43,9 @@
             buf[i] = toupper(c);
         }
     }
+    if (*name == '.') {
+        name++;
+    }
     for(int i = 8; i < 11; i++) {
         char c = *name++;
         if (c == '\0') {
@@ -62,7 +65,7 @@
 
 #if 1
     uint8_t fat12[16];
-    storage->storage_read(base_fat * 512, fat12, sizeof(fat12));
+    storage->Read(base_fat, fat12, sizeof(fat12));
     FS_DBG_HEX(fat12, sizeof(fat12));
 #endif
 
@@ -70,12 +73,12 @@
     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));
+        storage->Read(base_dir + 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 new F12RFileHandle(*this, base_dir + i * 32);
         }
     }
     return NULL;
@@ -109,7 +112,7 @@
 int F12RFileSystem::mount() {
 #if 1
     uint8_t buf[512];
-    storage->storage_read(0, buf, sizeof(buf));
+    storage->Read(0, buf, sizeof(buf));
     FS_DBG_HEX(buf, sizeof(buf));
 #endif
 
@@ -125,32 +128,33 @@
         uint32_t sector_start; // +8
         uint32_t sector_count; // +12
     } partition_entry;
-    storage->storage_read(446, (uint8_t*)&partition_entry, sizeof(partition_entry));
+    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) {
+        lba_offset = partition_entry.sector_start * 512;
+        if (storage_peek(lba_offset + 510, 2) != 0xaa55) {
             return -1;
         }
     }
 
 #if 1
     FS_DBG("lba_offset=%d", lba_offset);
-    storage->storage_read(lba_offset * 512, buf, sizeof(buf));
+    storage->Read(lba_offset, buf, sizeof(buf));
     FS_DBG_HEX(buf, sizeof(buf));
 #endif
 
-    if (storage_peek(lba_offset * 512 + 11, 2) != 512) {
+    if (storage_peek(lba_offset + 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;
+    cluster_size = storage_peek(lba_offset + 13, 1) * 512;
+    sector_t number_of_reserved_sectors = storage_peek(lba_offset + 14, 2);
+    base_fat = lba_offset + number_of_reserved_sectors * 512;
+    int number_of_fats = storage_peek(lba_offset + 16, 2);
+    FS_TEST_ASSERT(number_of_fats == 1 || number_of_fats == 2);
+    max_root_dir_entries = storage_peek(lba_offset + 17, 2);
+    sector_t total_logical_sectors = storage_peek(lba_offset + 19, 2);
+    sector_t logical_sectors_per_fat = storage_peek(lba_offset + 22, 2);
+    base_dir = base_fat + logical_sectors_per_fat * number_of_fats * 512;
+    base_data = base_dir + ((max_root_dir_entries * 32 + 511) / 512) * 512;
 
     FS_DBG("number_of_reserved_sectors=%d", number_of_reserved_sectors);
     FS_DBG("number_of_fats=%d", number_of_fats);
@@ -174,7 +178,7 @@
 
 uint32_t F12RFileSystem::storage_peek(uint32_t offset, int n) {
     uint8_t buf[n];
-    storage->storage_read(offset, buf, sizeof(buf));
+    storage->Read(offset, buf, sizeof(buf));
     uint32_t val = 0;
     for(int i = 0; i < n; i++) {
         val |= buf[i]<<(i*8);
@@ -184,7 +188,7 @@
 
 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);
+    cluster_t next = storage_peek(base_fat + i, 2);
     if (index & 1) {
         next >>= 4;
     } else {