aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTravis Geiselbrecht <geist@foobox.com>2016-03-25 12:18:13 -0700
committerTravis Geiselbrecht <geist@foobox.com>2016-03-25 21:36:15 -0700
commit87bbe81050834de1dd43ad64be07d37e8be1f4c2 (patch)
tree0ea6802ab3e115d9b02df26819a63e0bf207a1bf /lib
parent4daede91646847883a67c54516ea2c6c42e10e26 (diff)
downloadcommon-87bbe81050834de1dd43ad64be07d37e8be1f4c2.tar.gz
[fs][fat] run astyle over the newly imported code to match LK style
Diffstat (limited to 'lib')
-rw-r--r--lib/fs/fat32/fat.c244
-rw-r--r--lib/fs/fat32/fat_fs.h60
-rw-r--r--lib/fs/fat32/file.c470
3 files changed, 385 insertions, 389 deletions
diff --git a/lib/fs/fat32/fat.c b/lib/fs/fat32/fat.c
index e712c80a..7c1a3d9c 100644
--- a/lib/fs/fat32/fat.c
+++ b/lib/fs/fat32/fat.c
@@ -33,7 +33,8 @@
#include "fat32_priv.h"
#include "fat_fs.h"
-void fat32_dump(fat_fs_t *fat) {
+void fat32_dump(fat_fs_t *fat)
+{
printf("bytes_per_sector=%i\n", fat->bytes_per_sector);
printf("sectors_per_cluster=%i\n", fat->sectors_per_cluster);
printf("bytes_per_cluster=%i\n", fat->bytes_per_cluster);
@@ -50,134 +51,135 @@ void fat32_dump(fat_fs_t *fat) {
printf("root_start=%i\n", fat->root_start);
}
-status_t fat32_mount(bdev_t *dev, fscookie **cookie) {
- status_t result = NO_ERROR;
-
- if (!dev)
- return ERR_NOT_VALID;
-
- uint8_t *bs = malloc(512);
- int err = bio_read(dev, bs, 1024, 512);
- if (err < 0) {
- result = ERR_GENERIC;
- goto end;
- }
-
- if (((bs[0x1fe] != 0x55) || (bs[0x1ff] != 0xaa)) && (bs[0x15] == 0xf8)) {
- printf("missing boot signature\n");
- result = ERR_NOT_VALID;
- goto end;
- }
-
- fat_fs_t *fat = malloc(sizeof(fat_fs_t));
- fat->lba_start = 1024;
- fat->dev = dev;
-
- fat->bytes_per_sector = fat_read16(bs,0xb);
- if ((fat->bytes_per_sector != 0x200) && (fat->bytes_per_sector != 0x400) && (fat->bytes_per_sector != 0x800)) {
- printf("unsupported sector size (%x)\n", fat->bytes_per_sector);
- result = ERR_NOT_VALID;
- goto end;
- }
-
- fat->sectors_per_cluster = bs[0xd];
- switch (fat->sectors_per_cluster) {
- case 1:
- case 2:
- case 4:
- case 8:
- case 0x10:
- case 0x20:
- case 0x40:
- case 0x80:
- break;
- default:
- printf("unsupported sectors/cluster (%x)\n", fat->sectors_per_cluster);
- result = ERR_NOT_VALID;
- goto end;
- }
-
- fat->reserved_sectors = fat_read16(bs, 0xe);
- fat->fat_count = bs[0x10];
-
- if ((fat->fat_count == 0) || (fat->fat_count > 8)) {
- printf("unreasonable FAT count (%x)\n", fat->fat_count);
- result = ERR_NOT_VALID;
- goto end;
- }
-
- if (bs[0x15] != 0xf8) {
- printf("unsupported media descriptor byte (%x)\n", bs[0x15]);
- result = ERR_NOT_VALID;
- goto end;
- }
-
- fat->sectors_per_fat = fat_read16(bs, 0x16);
- if (fat->sectors_per_fat == 0) {
- fat->fat_bits = 32;
- fat->sectors_per_fat = fat_read32(bs,0x24);
- fat->total_sectors = fat_read32(bs,0x20);
- fat->active_fat = (bs[0x28] & 0x80) ? 0 : (bs[0x28] & 0xf);
- fat->data_start = fat->reserved_sectors + (fat->fat_count * fat->sectors_per_fat);
- fat->total_clusters = (fat->total_sectors - fat->data_start) / fat->sectors_per_cluster;
-
- // In FAT32, root directory appears in data area on given cluster and can be a cluster chain.
- fat->root_cluster = fat_read32(bs,0x2c);
- fat->root_start = 0;
- if (fat->root_cluster >= fat->total_clusters) {
- printf("root cluster too large (%x > %x)\n", fat->root_cluster, fat->total_clusters);
- result = ERR_NOT_VALID;
- goto end;
+status_t fat32_mount(bdev_t *dev, fscookie **cookie)
+{
+ status_t result = NO_ERROR;
+
+ if (!dev)
+ return ERR_NOT_VALID;
+
+ uint8_t *bs = malloc(512);
+ int err = bio_read(dev, bs, 1024, 512);
+ if (err < 0) {
+ result = ERR_GENERIC;
+ goto end;
}
- fat->root_entries = 0;
- }
- else {
- if (fat->fat_count != 2) {
- printf("illegal FAT count (%x)\n", fat->fat_count);
- result = ERR_NOT_VALID;
- goto end;
+
+ if (((bs[0x1fe] != 0x55) || (bs[0x1ff] != 0xaa)) && (bs[0x15] == 0xf8)) {
+ printf("missing boot signature\n");
+ result = ERR_NOT_VALID;
+ goto end;
+ }
+
+ fat_fs_t *fat = malloc(sizeof(fat_fs_t));
+ fat->lba_start = 1024;
+ fat->dev = dev;
+
+ fat->bytes_per_sector = fat_read16(bs,0xb);
+ if ((fat->bytes_per_sector != 0x200) && (fat->bytes_per_sector != 0x400) && (fat->bytes_per_sector != 0x800)) {
+ printf("unsupported sector size (%x)\n", fat->bytes_per_sector);
+ result = ERR_NOT_VALID;
+ goto end;
}
-
- // On a FAT 12 or FAT 16 volumes the root directory is at a fixed position immediately after the File Allocation Tables
- fat->root_cluster = 0;
- fat->root_entries = fat_read16(bs,0x11);
- if (fat->root_entries % (fat->bytes_per_sector / 0x20)) {
- printf("illegal number of root entries (%x)\n", fat->root_entries);
- result = ERR_NOT_VALID;
- goto end;
+
+ fat->sectors_per_cluster = bs[0xd];
+ switch (fat->sectors_per_cluster) {
+ case 1:
+ case 2:
+ case 4:
+ case 8:
+ case 0x10:
+ case 0x20:
+ case 0x40:
+ case 0x80:
+ break;
+ default:
+ printf("unsupported sectors/cluster (%x)\n", fat->sectors_per_cluster);
+ result = ERR_NOT_VALID;
+ goto end;
}
-
- fat->total_sectors = fat_read16(bs,0x13);
- if (fat->total_sectors == 0) {
- fat->total_sectors = fat_read32(bs,0x20);
+
+ fat->reserved_sectors = fat_read16(bs, 0xe);
+ fat->fat_count = bs[0x10];
+
+ if ((fat->fat_count == 0) || (fat->fat_count > 8)) {
+ printf("unreasonable FAT count (%x)\n", fat->fat_count);
+ result = ERR_NOT_VALID;
+ goto end;
}
-
- fat->root_start = fat->reserved_sectors + fat->fat_count * fat->sectors_per_fat;
- fat->data_start = fat->root_start + fat->root_entries * 0x20 / fat->bytes_per_sector;
- fat->total_clusters = (fat->total_sectors - fat->data_start) / fat->sectors_per_cluster;
-
- if (fat->total_clusters < 0xff2) {
- printf("small FAT12, not supported\n");
- result = ERR_NOT_VALID;
- goto end;
+
+ if (bs[0x15] != 0xf8) {
+ printf("unsupported media descriptor byte (%x)\n", bs[0x15]);
+ result = ERR_NOT_VALID;
+ goto end;
}
- fat->fat_bits = 16;
- }
-
- fat->bytes_per_cluster = fat->sectors_per_cluster * fat->bytes_per_sector;
- fat->cache = bcache_create(fat->dev, fat->bytes_per_sector, 4);
-
- *cookie = (fscookie *)fat;
+
+ fat->sectors_per_fat = fat_read16(bs, 0x16);
+ if (fat->sectors_per_fat == 0) {
+ fat->fat_bits = 32;
+ fat->sectors_per_fat = fat_read32(bs,0x24);
+ fat->total_sectors = fat_read32(bs,0x20);
+ fat->active_fat = (bs[0x28] & 0x80) ? 0 : (bs[0x28] & 0xf);
+ fat->data_start = fat->reserved_sectors + (fat->fat_count * fat->sectors_per_fat);
+ fat->total_clusters = (fat->total_sectors - fat->data_start) / fat->sectors_per_cluster;
+
+ // In FAT32, root directory appears in data area on given cluster and can be a cluster chain.
+ fat->root_cluster = fat_read32(bs,0x2c);
+ fat->root_start = 0;
+ if (fat->root_cluster >= fat->total_clusters) {
+ printf("root cluster too large (%x > %x)\n", fat->root_cluster, fat->total_clusters);
+ result = ERR_NOT_VALID;
+ goto end;
+ }
+ fat->root_entries = 0;
+ } else {
+ if (fat->fat_count != 2) {
+ printf("illegal FAT count (%x)\n", fat->fat_count);
+ result = ERR_NOT_VALID;
+ goto end;
+ }
+
+ // On a FAT 12 or FAT 16 volumes the root directory is at a fixed position immediately after the File Allocation Tables
+ fat->root_cluster = 0;
+ fat->root_entries = fat_read16(bs,0x11);
+ if (fat->root_entries % (fat->bytes_per_sector / 0x20)) {
+ printf("illegal number of root entries (%x)\n", fat->root_entries);
+ result = ERR_NOT_VALID;
+ goto end;
+ }
+
+ fat->total_sectors = fat_read16(bs,0x13);
+ if (fat->total_sectors == 0) {
+ fat->total_sectors = fat_read32(bs,0x20);
+ }
+
+ fat->root_start = fat->reserved_sectors + fat->fat_count * fat->sectors_per_fat;
+ fat->data_start = fat->root_start + fat->root_entries * 0x20 / fat->bytes_per_sector;
+ fat->total_clusters = (fat->total_sectors - fat->data_start) / fat->sectors_per_cluster;
+
+ if (fat->total_clusters < 0xff2) {
+ printf("small FAT12, not supported\n");
+ result = ERR_NOT_VALID;
+ goto end;
+ }
+ fat->fat_bits = 16;
+ }
+
+ fat->bytes_per_cluster = fat->sectors_per_cluster * fat->bytes_per_sector;
+ fat->cache = bcache_create(fat->dev, fat->bytes_per_sector, 4);
+
+ *cookie = (fscookie *)fat;
end:
- free(bs);
- return result;
+ free(bs);
+ return result;
}
-status_t fat32_unmount(fscookie *cookie) {
- fat_fs_t *fat = (fat_fs_t *)cookie;
- bcache_destroy(fat->cache);
- free(fat);
- return NO_ERROR;
+status_t fat32_unmount(fscookie *cookie)
+{
+ fat_fs_t *fat = (fat_fs_t *)cookie;
+ bcache_destroy(fat->cache);
+ free(fat);
+ return NO_ERROR;
}
static const struct fs_api fat32_api = {
diff --git a/lib/fs/fat32/fat_fs.h b/lib/fs/fat32/fat_fs.h
index 084d586d..d3e2608f 100644
--- a/lib/fs/fat32/fat_fs.h
+++ b/lib/fs/fat32/fat_fs.h
@@ -28,42 +28,42 @@
#include <lib/bcache.h>
typedef struct {
- bdev_t *dev;
- bcache_t cache;
-
- uint32_t lba_start;
-
- uint32_t bytes_per_sector;
- uint32_t sectors_per_cluster;
- uint32_t bytes_per_cluster;
- uint32_t reserved_sectors;
- uint32_t fat_bits;
- uint32_t fat_count;
- uint32_t sectors_per_fat;
- uint32_t total_sectors;
- uint32_t active_fat;
- uint32_t data_start;
- uint32_t total_clusters;
- uint32_t root_cluster;
- uint32_t root_entries;
- uint32_t root_start;
+ bdev_t *dev;
+ bcache_t cache;
+
+ uint32_t lba_start;
+
+ uint32_t bytes_per_sector;
+ uint32_t sectors_per_cluster;
+ uint32_t bytes_per_cluster;
+ uint32_t reserved_sectors;
+ uint32_t fat_bits;
+ uint32_t fat_count;
+ uint32_t sectors_per_fat;
+ uint32_t total_sectors;
+ uint32_t active_fat;
+ uint32_t data_start;
+ uint32_t total_clusters;
+ uint32_t root_cluster;
+ uint32_t root_entries;
+ uint32_t root_start;
} fat_fs_t;
typedef struct {
- fat_fs_t *fat_fs;
- uint32_t start_cluster;
- uint32_t length;
- uint8_t attributes;
+ fat_fs_t *fat_fs;
+ uint32_t start_cluster;
+ uint32_t length;
+ uint8_t attributes;
} fat_file_t;
typedef enum {
- fat_attribute_read_only = 0x01,
- fat_attribute_hidden = 0x02,
- fat_attribute_system = 0x04,
- fat_attribute_volume_id = 0x08,
- fat_attribute_directory = 0x10,
- fat_attribute_archive = 0x20,
- fat_attribute_lfn = fat_attribute_read_only | fat_attribute_hidden | fat_attribute_system | fat_attribute_volume_id,
+ fat_attribute_read_only = 0x01,
+ fat_attribute_hidden = 0x02,
+ fat_attribute_system = 0x04,
+ fat_attribute_volume_id = 0x08,
+ fat_attribute_directory = 0x10,
+ fat_attribute_archive = 0x20,
+ fat_attribute_lfn = fat_attribute_read_only | fat_attribute_hidden | fat_attribute_system | fat_attribute_volume_id,
} fat_attributes;
#define fat_read32(buffer,off) \
diff --git a/lib/fs/fat32/file.c b/lib/fs/fat32/file.c
index 98c55997..c73ae938 100644
--- a/lib/fs/fat32/file.c
+++ b/lib/fs/fat32/file.c
@@ -36,267 +36,261 @@
#define DIR_ENTRY_LENGTH 32
#define USE_CACHE 1
-uint32_t fat32_next_cluster_in_chain(fat_fs_t *fat, uint32_t cluster) {
- uint32_t fat_sector = (cluster) >> 7;
- uint32_t fat_index = (cluster ) & 127;
-
- uint32_t bnum = (fat->lba_start / fat->bytes_per_sector) + (fat->reserved_sectors + fat_sector);
- uint32_t next_cluster = 0x0fffffff;
-
+uint32_t fat32_next_cluster_in_chain(fat_fs_t *fat, uint32_t cluster)
+{
+ uint32_t fat_sector = (cluster) >> 7;
+ uint32_t fat_index = (cluster ) & 127;
+
+ uint32_t bnum = (fat->lba_start / fat->bytes_per_sector) + (fat->reserved_sectors + fat_sector);
+ uint32_t next_cluster = 0x0fffffff;
+
#if USE_CACHE
- void *cache_ptr;
- int err = bcache_get_block(fat->cache, &cache_ptr, bnum);
- if (err < 0) {
- printf("bcache_get_block returned: %i\n", err);
- }
- else {
- if (fat->fat_bits == 32) {
- uint32_t *table = (uint32_t *)cache_ptr;
- next_cluster = table[fat_index];
- LE32SWAP(next_cluster);
- }
- else if (fat->fat_bits == 16) {
- uint16_t *table = (uint16_t *)cache_ptr;
- next_cluster = table[fat_index];
- LE16SWAP(next_cluster);
- if (next_cluster > 0xfff0) {
- next_cluster |= 0x0fff0000;
- }
+ void *cache_ptr;
+ int err = bcache_get_block(fat->cache, &cache_ptr, bnum);
+ if (err < 0) {
+ printf("bcache_get_block returned: %i\n", err);
+ } else {
+ if (fat->fat_bits == 32) {
+ uint32_t *table = (uint32_t *)cache_ptr;
+ next_cluster = table[fat_index];
+ LE32SWAP(next_cluster);
+ } else if (fat->fat_bits == 16) {
+ uint16_t *table = (uint16_t *)cache_ptr;
+ next_cluster = table[fat_index];
+ LE16SWAP(next_cluster);
+ if (next_cluster > 0xfff0) {
+ next_cluster |= 0x0fff0000;
+ }
+ }
+
+ bcache_put_block(fat->cache, bnum);
}
-
- bcache_put_block(fat->cache, bnum);
- }
#else
- uint32_t offset = (bnum * fat->bytes_per_sector) + (fat_index * (fat->fat_bits / 8));
- bio_read(fat->dev, &next_cluster, offset, 4);
- LE32SWAP(next_cluster);
-#endif
- return next_cluster;
+ uint32_t offset = (bnum * fat->bytes_per_sector) + (fat_index * (fat->fat_bits / 8));
+ bio_read(fat->dev, &next_cluster, offset, 4);
+ LE32SWAP(next_cluster);
+#endif
+ return next_cluster;
}
-static inline off_t fat32_offset_for_cluster(fat_fs_t *fat, uint32_t cluster) {
- off_t cluster_begin_lba = fat->reserved_sectors + (fat->fat_count * fat->sectors_per_fat);
- return fat->lba_start + (cluster_begin_lba + (cluster - 2) * fat->sectors_per_cluster) * fat->bytes_per_sector;
+static inline off_t fat32_offset_for_cluster(fat_fs_t *fat, uint32_t cluster)
+{
+ off_t cluster_begin_lba = fat->reserved_sectors + (fat->fat_count * fat->sectors_per_fat);
+ return fat->lba_start + (cluster_begin_lba + (cluster - 2) * fat->sectors_per_cluster) * fat->bytes_per_sector;
}
-char *fat32_dir_get_filename(uint8_t *dir, off_t offset, int lfn_sequences) {
- int result_len = 1 + (lfn_sequences == 0 ? 12 : (lfn_sequences * 26));
- char *result = malloc(result_len);
- int j = 0;
- memset(result, 0x00, result_len);
-
- if (lfn_sequences == 0) {
- // Ignore trailing spaces in filename and/or extension
- int fn_len=8, ext_len=3;
- for (int i=7; i>=0; i--) {
- if (dir[offset + i] == 0x20) {
- fn_len--;
- }
- else {
- break;
- }
- }
- for (int i=10; i>=8; i--) {
- if (dir[offset + i] == 0x20) {
- ext_len--;
- }
- else {
- break;
- }
- }
-
- for (int i=0; i<fn_len; i++) {
- result[j++] = dir[offset + i];
- }
- if (ext_len > 0) {
- result[j++] = '.';
- for (int i=0; i<ext_len; i++) {
- result[j++] = dir[offset + 8 + i];
- }
- }
- }
- else {
- // XXX: not unicode aware.
- for (int sequence=1; sequence<=lfn_sequences; sequence++) {
- for (int i=1; i<DIR_ENTRY_LENGTH; i++) {
- int char_offset = (offset - (sequence * DIR_ENTRY_LENGTH)) + i;
- if (dir[char_offset] != 0x00 && dir[char_offset] != 0xff) {
- result[j++] = dir[char_offset];
+char *fat32_dir_get_filename(uint8_t *dir, off_t offset, int lfn_sequences)
+{
+ int result_len = 1 + (lfn_sequences == 0 ? 12 : (lfn_sequences * 26));
+ char *result = malloc(result_len);
+ int j = 0;
+ memset(result, 0x00, result_len);
+
+ if (lfn_sequences == 0) {
+ // Ignore trailing spaces in filename and/or extension
+ int fn_len=8, ext_len=3;
+ for (int i=7; i>=0; i--) {
+ if (dir[offset + i] == 0x20) {
+ fn_len--;
+ } else {
+ break;
+ }
+ }
+ for (int i=10; i>=8; i--) {
+ if (dir[offset + i] == 0x20) {
+ ext_len--;
+ } else {
+ break;
+ }
}
-
- if (i == 9) {
- i = 13;
+
+ for (int i=0; i<fn_len; i++) {
+ result[j++] = dir[offset + i];
+ }
+ if (ext_len > 0) {
+ result[j++] = '.';
+ for (int i=0; i<ext_len; i++) {
+ result[j++] = dir[offset + 8 + i];
+ }
}
- else if (i == 25) {
- i = 27;
+ } else {
+ // XXX: not unicode aware.
+ for (int sequence=1; sequence<=lfn_sequences; sequence++) {
+ for (int i=1; i<DIR_ENTRY_LENGTH; i++) {
+ int char_offset = (offset - (sequence * DIR_ENTRY_LENGTH)) + i;
+ if (dir[char_offset] != 0x00 && dir[char_offset] != 0xff) {
+ result[j++] = dir[char_offset];
+ }
+
+ if (i == 9) {
+ i = 13;
+ } else if (i == 25) {
+ i = 27;
+ }
+ }
}
- }
}
- }
- return result;
+ return result;
}
-status_t fat32_open_file(fscookie *cookie, const char *path, filecookie **fcookie) {
- fat_fs_t *fat = (fat_fs_t *)cookie;
- status_t result = ERR_GENERIC;
-
- uint8_t *dir = malloc(fat->bytes_per_cluster);
- uint32_t dir_cluster = fat->root_cluster;
- fat_file_t *file = NULL;
-
- const char *ptr;
- /* chew up leading slashes */
- ptr = &path[0];
- while (*ptr == '/') {
- ptr++;
- }
-
- bool done = false;
- do {
- // XXX: use the cache!
- bio_read(fat->dev, dir, fat32_offset_for_cluster(fat, dir_cluster), fat->bytes_per_cluster);
-
- char *next_sep = strchr(ptr, '/');
- if (next_sep) {
- /* terminate the next component, giving us a substring */
- *next_sep = 0;
- }
- else {
- /* this is the last component */
- done = true;
+status_t fat32_open_file(fscookie *cookie, const char *path, filecookie **fcookie)
+{
+ fat_fs_t *fat = (fat_fs_t *)cookie;
+ status_t result = ERR_GENERIC;
+
+ uint8_t *dir = malloc(fat->bytes_per_cluster);
+ uint32_t dir_cluster = fat->root_cluster;
+ fat_file_t *file = NULL;
+
+ const char *ptr;
+ /* chew up leading slashes */
+ ptr = &path[0];
+ while (*ptr == '/') {
+ ptr++;
}
-
- uint32_t offset = 0;
- uint32_t lfn_sequences = 0;
- bool matched = false;
- while (dir[offset] != 0x00 && offset < fat->bytes_per_cluster) {
- if ( dir[offset] == 0xE5 /*deleted*/) {
- offset += DIR_ENTRY_LENGTH;
- continue;
- }
- else if ((dir[offset + 0x0B] & 0x08)) {
- if (dir[offset + 0x0B] == 0x0f) {
- lfn_sequences++;
- }
-
- offset += DIR_ENTRY_LENGTH;
- continue;
- }
-
- char *filename = fat32_dir_get_filename(dir, offset, lfn_sequences);
- lfn_sequences = 0;
-
- matched = (strnicmp(ptr, filename, strlen(filename)) == 0);
- free(filename);
-
- if (matched) {
- uint16_t target_cluster = fat_read16(dir, offset + 0x1a);
- if (done == true) {
- file = malloc(sizeof(fat_file_t));
- file->fat_fs = fat;
- file->start_cluster = target_cluster;
- file->length = fat_read32(dir, offset + 0x1c);
- file->attributes = dir[0x0B + offset];
- result = NO_ERROR;
+
+ bool done = false;
+ do {
+ // XXX: use the cache!
+ bio_read(fat->dev, dir, fat32_offset_for_cluster(fat, dir_cluster), fat->bytes_per_cluster);
+
+ char *next_sep = strchr(ptr, '/');
+ if (next_sep) {
+ /* terminate the next component, giving us a substring */
+ *next_sep = 0;
+ } else {
+ /* this is the last component */
+ done = true;
}
- else {
- dir_cluster = target_cluster;
+
+ uint32_t offset = 0;
+ uint32_t lfn_sequences = 0;
+ bool matched = false;
+ while (dir[offset] != 0x00 && offset < fat->bytes_per_cluster) {
+ if ( dir[offset] == 0xE5 /*deleted*/) {
+ offset += DIR_ENTRY_LENGTH;
+ continue;
+ } else if ((dir[offset + 0x0B] & 0x08)) {
+ if (dir[offset + 0x0B] == 0x0f) {
+ lfn_sequences++;
+ }
+
+ offset += DIR_ENTRY_LENGTH;
+ continue;
+ }
+
+ char *filename = fat32_dir_get_filename(dir, offset, lfn_sequences);
+ lfn_sequences = 0;
+
+ matched = (strnicmp(ptr, filename, strlen(filename)) == 0);
+ free(filename);
+
+ if (matched) {
+ uint16_t target_cluster = fat_read16(dir, offset + 0x1a);
+ if (done == true) {
+ file = malloc(sizeof(fat_file_t));
+ file->fat_fs = fat;
+ file->start_cluster = target_cluster;
+ file->length = fat_read32(dir, offset + 0x1c);
+ file->attributes = dir[0x0B + offset];
+ result = NO_ERROR;
+ } else {
+ dir_cluster = target_cluster;
+ }
+ break;
+ }
+
+ offset += DIR_ENTRY_LENGTH;
}
- break;
- }
-
- offset += DIR_ENTRY_LENGTH;
- }
-
- if (matched == true) {
- if (done == true) {
- break;
- }
- else {
- /* move to the next seperator */
- ptr = next_sep + 1;
-
- /* consume multiple seperators */
- while (*ptr == '/') {
- ptr++;
+
+ if (matched == true) {
+ if (done == true) {
+ break;
+ } else {
+ /* move to the next seperator */
+ ptr = next_sep + 1;
+
+ /* consume multiple seperators */
+ while (*ptr == '/') {
+ ptr++;
+ }
+ }
+ } else {
+ // XXX: untested!!!
+ dir_cluster = fat32_next_cluster_in_chain(fat, dir_cluster);
+ if (dir_cluster == 0x0fffffff) {
+ // no more clusters in the chain
+ break;
+ }
}
- }
- }
- else {
- // XXX: untested!!!
- dir_cluster = fat32_next_cluster_in_chain(fat, dir_cluster);
- if (dir_cluster == 0x0fffffff) {
- // no more clusters in the chain
- break;
- }
- }
- } while(true);
-
+ } while (true);
+
out:
- *fcookie = (filecookie *)file;
- free(dir);
- return result;
+ *fcookie = (filecookie *)file;
+ free(dir);
+ return result;
}
-ssize_t fat32_read_file(filecookie *fcookie, void *buf, off_t offset, size_t len) {
- fat_file_t *file = (fat_file_t *)fcookie;
- fat_fs_t *fat = file->fat_fs;
- bdev_t *dev = fat->dev;
-
- uint32_t cluster = 0;
- if (offset <= fat->bytes_per_cluster) {
- cluster = file->start_cluster;
- }
- else {
- // XXX: support non-0 offsets
- TRACE;
- return -1;
- }
-
- uint32_t length = file->length;
- uint32_t amount_read = 0;
-
- do {
- off_t lba_addr = fat32_offset_for_cluster(fat, cluster);
-
- uint32_t to_read = fat->bytes_per_cluster;
- uint32_t next_cluster = 0;
- while ((next_cluster = fat32_next_cluster_in_chain(fat, cluster)) == cluster + 1) {
- cluster = next_cluster;
- to_read += fat->bytes_per_cluster;
- }
- cluster = next_cluster;
-
- to_read = MIN(length - amount_read, to_read);
- // XXX: support non-0 offsets
- int err = bio_read(dev, buf+amount_read, lba_addr, to_read);
- if (err < 0) {
- return err;
- }
-
- amount_read += to_read;
-
- if (amount_read < length) {
- if (cluster == 0x0fffffff) {
- printf("no more clusters, amount_read=%i, to_read=%i\n", amount_read, to_read);
- break;
- }
+ssize_t fat32_read_file(filecookie *fcookie, void *buf, off_t offset, size_t len)
+{
+ fat_file_t *file = (fat_file_t *)fcookie;
+ fat_fs_t *fat = file->fat_fs;
+ bdev_t *dev = fat->dev;
+
+ uint32_t cluster = 0;
+ if (offset <= fat->bytes_per_cluster) {
+ cluster = file->start_cluster;
+ } else {
+ // XXX: support non-0 offsets
+ TRACE;
+ return -1;
}
- }
- while(amount_read < length);
-
- return amount_read;
+
+ uint32_t length = file->length;
+ uint32_t amount_read = 0;
+
+ do {
+ off_t lba_addr = fat32_offset_for_cluster(fat, cluster);
+
+ uint32_t to_read = fat->bytes_per_cluster;
+ uint32_t next_cluster = 0;
+ while ((next_cluster = fat32_next_cluster_in_chain(fat, cluster)) == cluster + 1) {
+ cluster = next_cluster;
+ to_read += fat->bytes_per_cluster;
+ }
+ cluster = next_cluster;
+
+ to_read = MIN(length - amount_read, to_read);
+ // XXX: support non-0 offsets
+ int err = bio_read(dev, buf+amount_read, lba_addr, to_read);
+ if (err < 0) {
+ return err;
+ }
+
+ amount_read += to_read;
+
+ if (amount_read < length) {
+ if (cluster == 0x0fffffff) {
+ printf("no more clusters, amount_read=%i, to_read=%i\n", amount_read, to_read);
+ break;
+ }
+ }
+ } while (amount_read < length);
+
+ return amount_read;
}
-status_t fat32_close_file(filecookie *fcookie) {
- fat_file_t *file = (fat_file_t *)fcookie;
- free(file);
- return NO_ERROR;
+status_t fat32_close_file(filecookie *fcookie)
+{
+ fat_file_t *file = (fat_file_t *)fcookie;
+ free(file);
+ return NO_ERROR;
}
-status_t fat32_stat_file(filecookie *fcookie, struct file_stat *stat) {
- fat_file_t *file = (fat_file_t *)fcookie;
- stat->size = file->length;
- stat->is_dir = (file->attributes == fat_attribute_directory);
- return NO_ERROR;
+status_t fat32_stat_file(filecookie *fcookie, struct file_stat *stat)
+{
+ fat_file_t *file = (fat_file_t *)fcookie;
+ stat->size = file->length;
+ stat->is_dir = (file->attributes == fat_attribute_directory);
+ return NO_ERROR;
}