|
@@ -1,52 +1,62 @@
|
|
|
-/* FAT12 filesystem with subdirectory support.
|
|
|
|
|
- * A "directory" is identified by its first data cluster; 0 means the root dir.
|
|
|
|
|
- * Error conventions: 0 = success, -1 = not found / not ready / full, -2 = disk I/O error. */
|
|
|
|
|
|
|
+/* FAT12/16/32 filesystem with subdirectory support, device-parameterized.
|
|
|
|
|
+ * A "directory" is identified by a cluster: 0 = root for FAT12/16,
|
|
|
|
|
+ * root_cluster (2) = root for FAT32, else a subdirectory's first cluster.
|
|
|
|
|
+ * Error conventions: 0 = success, -1 = not found / not ready / full / invalid,
|
|
|
|
|
+ * -2 = disk I/O error. */
|
|
|
|
|
|
|
|
#ifndef FAT_H
|
|
#ifndef FAT_H
|
|
|
#define FAT_H
|
|
#define FAT_H
|
|
|
|
|
|
|
|
-typedef void (*fat_root_callback_t)(const char *name, int is_dir, unsigned int size);
|
|
|
|
|
-
|
|
|
|
|
-/* Parse BPB from sector 0. Call once before list/read/write.
|
|
|
|
|
- * Returns 0 on success, -1 if no valid FAT filesystem, -2 on disk I/O error. */
|
|
|
|
|
-int fat_init(void);
|
|
|
|
|
-
|
|
|
|
|
-/* Iterate root directory, call callback for each file/dir. */
|
|
|
|
|
-int fat_list_root(fat_root_callback_t callback);
|
|
|
|
|
-
|
|
|
|
|
-/* Iterate a directory (0 = root, else first cluster). */
|
|
|
|
|
-int fat_list_dir(unsigned int dir_cluster, fat_root_callback_t callback);
|
|
|
|
|
-
|
|
|
|
|
-/* Read file by 8.3 name into buf, max max_len bytes (root only). */
|
|
|
|
|
-int fat_read_file(const char *name_8_3, void *buf, unsigned int max_len);
|
|
|
|
|
-
|
|
|
|
|
-/* Read a file in a directory (0 = root). */
|
|
|
|
|
-int fat_read_file_in(unsigned int dir_cluster, const char *name_8_3, void *buf, unsigned int max_len);
|
|
|
|
|
|
|
+#include "blkdev.h"
|
|
|
|
|
|
|
|
-/* Create or overwrite a file in the root directory. */
|
|
|
|
|
-int fat_write_file(const char *name_8_3, const void *buf, unsigned int size);
|
|
|
|
|
|
|
+typedef enum { FAT_NONE, FAT12, FAT16, FAT32 } fat_type_t;
|
|
|
|
|
|
|
|
-/* Create or overwrite a file in a directory (0 = root). */
|
|
|
|
|
-int fat_write_file_in(unsigned int dir_cluster, const char *name_8_3, const void *buf, unsigned int size);
|
|
|
|
|
-
|
|
|
|
|
-/* Delete a file from the root directory. */
|
|
|
|
|
-int fat_delete_file(const char *name_8_3);
|
|
|
|
|
-
|
|
|
|
|
-/* Delete a file from a directory (0 = root). */
|
|
|
|
|
-int fat_delete_file_in(unsigned int dir_cluster, const char *name_8_3);
|
|
|
|
|
-
|
|
|
|
|
-/* Create a subdirectory in `parent_cluster` (0 = root). */
|
|
|
|
|
-int fat_mkdir(unsigned int parent_cluster, const char *name_8_3);
|
|
|
|
|
-
|
|
|
|
|
-/* Remove an empty subdirectory from `parent_cluster` (0 = root). */
|
|
|
|
|
-int fat_rmdir(unsigned int parent_cluster, const char *name_8_3);
|
|
|
|
|
|
|
+typedef void (*fat_root_callback_t)(const char *name, int is_dir, unsigned int size);
|
|
|
|
|
|
|
|
-/* Resolve a path "a/b/c" into (parent dir cluster, final 8.3 name).
|
|
|
|
|
- * The returned name_8_3 is uppercase and space-padded (11 bytes + NUL). */
|
|
|
|
|
-int fat_resolve_path(const char *path, unsigned int *dir_cluster, char name_8_3[12]);
|
|
|
|
|
|
|
+typedef struct {
|
|
|
|
|
+ blk_dev_t *dev;
|
|
|
|
|
+ unsigned int part_lba; /* LBA of the filesystem (partition) start */
|
|
|
|
|
+ fat_type_t type;
|
|
|
|
|
+ unsigned int bytes_per_sector;
|
|
|
|
|
+ unsigned int sectors_per_cluster;
|
|
|
|
|
+ unsigned int reserved;
|
|
|
|
|
+ unsigned int num_fats;
|
|
|
|
|
+ unsigned int sec_per_fat;
|
|
|
|
|
+ unsigned int root_entries; /* 0 for FAT32 */
|
|
|
|
|
+ unsigned int root_dir_sectors;
|
|
|
|
|
+ unsigned int root_cluster; /* FAT32 root (2); 0 otherwise */
|
|
|
|
|
+ unsigned int root_start; /* LBA of root dir (FAT12/16) */
|
|
|
|
|
+ unsigned int data_start; /* LBA of first data cluster */
|
|
|
|
|
+ unsigned int total_sectors;
|
|
|
|
|
+ unsigned int max_cluster;
|
|
|
|
|
+ char label[12]; /* trimmed 8.3 volume label + NUL */
|
|
|
|
|
+ int mounted;
|
|
|
|
|
+} fat_fs_t;
|
|
|
|
|
+
|
|
|
|
|
+/* Mount (parse BPB + validate FAT media byte) a filesystem at part_lba on dev.
|
|
|
|
|
+ * Returns 0 on success, -1 if no valid FAT, -2 on I/O error. */
|
|
|
|
|
+int fat_mount(blk_dev_t *dev, unsigned int part_lba, fat_fs_t *fs);
|
|
|
|
|
+
|
|
|
|
|
+/* Iterate a directory (root or subdir), call callback for each entry. */
|
|
|
|
|
+int fat_list_dir(fat_fs_t *fs, unsigned int dir_cluster, fat_root_callback_t callback);
|
|
|
|
|
+
|
|
|
|
|
+/* Read a file by 8.3 name in a directory. */
|
|
|
|
|
+int fat_read_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
|
|
|
|
|
+ void *buf, unsigned int max_len);
|
|
|
|
|
+
|
|
|
|
|
+/* Write / delete / mkdir / rmdir. Return -1 if FAT32 (read-only). */
|
|
|
|
|
+int fat_write_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
|
|
|
|
|
+ const void *buf, unsigned int size);
|
|
|
|
|
+int fat_delete_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3);
|
|
|
|
|
+int fat_mkdir(fat_fs_t *fs, unsigned int parent_cluster, const char *name_8_3);
|
|
|
|
|
+int fat_rmdir(fat_fs_t *fs, unsigned int parent_cluster, const char *name_8_3);
|
|
|
|
|
+
|
|
|
|
|
+/* Resolve a path "a/b/c" into (parent dir cluster, final 8.3 name). */
|
|
|
|
|
+int fat_resolve_path(fat_fs_t *fs, const char *path, unsigned int *dir_cluster,
|
|
|
|
|
+ char name_8_3[12]);
|
|
|
|
|
|
|
|
/* Resolve a full path (all components directories) to a directory cluster.
|
|
/* Resolve a full path (all components directories) to a directory cluster.
|
|
|
- * An empty path resolves to the root (cluster 0). */
|
|
|
|
|
-int fat_resolve_dir(const char *path, unsigned int *dir_cluster);
|
|
|
|
|
|
|
+ * An empty path resolves to the root. */
|
|
|
|
|
+int fat_resolve_dir(fat_fs_t *fs, const char *path, unsigned int *dir_cluster);
|
|
|
|
|
|
|
|
#endif
|
|
#endif
|