Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of F12RFileSystem by
Diff: F32RFileSystem.cpp
- Revision:
- 9:e9843d731180
- Parent:
- 8:661b3ce0e9c0
- Child:
- 10:0f8c8845a40b
diff -r 661b3ce0e9c0 -r e9843d731180 F32RFileSystem.cpp
--- a/F32RFileSystem.cpp Sat Nov 14 01:40:04 2015 +0000
+++ b/F32RFileSystem.cpp Fri Apr 08 06:38:36 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') {
@@ -61,21 +64,30 @@
}
#if 1
- uint8_t fat12[16];
- storage->storage_read(base_fat * 512, fat12, sizeof(fat12));
- FS_DBG_HEX(fat12, sizeof(fat12));
+ uint8_t fat[32];
+ storage->Read(base_fat, fat, sizeof(fat));
+ FS_DBG_HEX(fat, sizeof(fat));
#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(sfn, sizeof(sfn));
+ cluster_t cluster = root_directory_first_cluster;
+ int pos = 0;
+ while(cluster < 0x0ffffff8) {
+ FS_TEST_ASSERT(cluster >= 2);
+ uint32_t dir_pos = base_data + (cluster-2)*cluster_size + cluster_head(pos);
+ uint8_t buf[32];
+ storage->Read(dir_pos, 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 new F32RFileHandle(*this, dir_pos);
+ }
+ pos += 32;
+ if (cluster_head(pos) == 0) {
+ cluster = fat_read(cluster);
}
}
return NULL;
@@ -99,7 +111,7 @@
if (mount() != 0) {
return NULL;
}
- return new F32RDirHandle(*this);
+ return new F32RDirHandle(*this, root_directory_first_cluster);
}
int F32RFileSystem::mkdir(const char *name, mode_t mode) {
@@ -107,12 +119,6 @@
}
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;
}
@@ -125,42 +131,49 @@
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) {
+ storage->Read(446, (uint8_t*)&partition_entry, sizeof(partition_entry));
+ fs_type = partition_entry.type;
+ FS_DBG_HEX((uint8_t*)&partition_entry, sizeof(partition_entry));
+ FS_DBG("fs_type=%02x", fs_type);
+ FS_TEST_ASSERT(is_fat32());
+ if (is_fat12() || is_fat16() || is_fat32()) {
+ 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));
+ uint8_t buf[512];
+ 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) { // verify sector size
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);
+ // fat32
+ sector_t number_of_per_fat = storage_peek(lba_offset + 0x24, 4);
+ root_directory_first_cluster = storage_peek(lba_offset + 0x2c, 4);
+ root_directory_size = fat32_count(root_directory_first_cluster) * cluster_size;
+
+ base_data = base_fat + number_of_per_fat * number_of_fats * 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("fat32: number_of_per_fat=%d", number_of_per_fat);
+ FS_DBG("fat32: root_directory_first_cluster=%d", root_directory_first_cluster);
+ FS_DBG("fat32: root_directory_size=%d", root_directory_size);
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;
@@ -174,7 +187,7 @@
uint32_t F32RFileSystem::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);
@@ -183,6 +196,27 @@
}
cluster_t F32RFileSystem::fat_read(cluster_t index) {
- cluster_t next = storage_peek(base_fat*512 + index*4, 4);
+ if (is_fat32()) {
+ return storage_peek(base_fat + index*4, 4);
+ } else if (is_fat16()) {
+ return storage_peek(base_fat + index*2, 2);
+ }
+ FS_TEST_ASSERT(is_fat12());
+ int i = index / 2 * 3 + (index&1);
+ cluster_t next = storage_peek(base_fat + i, 2);
+ if (index & 1) {
+ next >>= 4;
+ } else {
+ next &= 0xfff;
+ }
return next;
}
+
+int F32RFileSystem::fat32_count(cluster_t cluster) {
+ int count = 0;
+ for(; cluster >= 2 && cluster < 0x0ffffff8; count++) {
+ cluster = fat_read(cluster);
+ }
+ return count;
+}
+
