fat.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* FAT12/16/32 filesystem with subdirectory support, device-parameterized.
  2. * A "directory" is identified by a cluster: 0 = root for FAT12/16,
  3. * root_cluster (2) = root for FAT32, else a subdirectory's first cluster.
  4. * Error conventions: 0 = success, -1 = not found / not ready / full / invalid,
  5. * -2 = disk I/O error. */
  6. #ifndef FAT_H
  7. #define FAT_H
  8. #include "blkdev.h"
  9. typedef enum { FAT_NONE, FAT12, FAT16, FAT32 } fat_type_t;
  10. typedef void (*fat_root_callback_t)(const char *name, int is_dir, unsigned int size);
  11. typedef struct {
  12. blk_dev_t *dev;
  13. unsigned int part_lba; /* LBA of the filesystem (partition) start */
  14. fat_type_t type;
  15. unsigned int bytes_per_sector;
  16. unsigned int sectors_per_cluster;
  17. unsigned int reserved;
  18. unsigned int num_fats;
  19. unsigned int sec_per_fat;
  20. unsigned int root_entries; /* 0 for FAT32 */
  21. unsigned int root_dir_sectors;
  22. unsigned int root_cluster; /* FAT32 root (2); 0 otherwise */
  23. unsigned int root_start; /* LBA of root dir (FAT12/16) */
  24. unsigned int data_start; /* LBA of first data cluster */
  25. unsigned int total_sectors;
  26. unsigned int max_cluster;
  27. char label[12]; /* trimmed 8.3 volume label + NUL */
  28. int mounted;
  29. } fat_fs_t;
  30. /* Mount (parse BPB + validate FAT media byte) a filesystem at part_lba on dev.
  31. * Returns 0 on success, -1 if no valid FAT, -2 on I/O error. */
  32. int fat_mount(blk_dev_t *dev, unsigned int part_lba, fat_fs_t *fs);
  33. /* Iterate a directory (root or subdir), call callback for each entry. */
  34. int fat_list_dir(fat_fs_t *fs, unsigned int dir_cluster, fat_root_callback_t callback);
  35. /* Read a file by 8.3 name in a directory. */
  36. int fat_read_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
  37. void *buf, unsigned int max_len);
  38. /* Write / delete / mkdir / rmdir. Return -1 if FAT32 (read-only). */
  39. int fat_write_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
  40. const void *buf, unsigned int size);
  41. int fat_delete_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3);
  42. int fat_mkdir(fat_fs_t *fs, unsigned int parent_cluster, const char *name_8_3);
  43. int fat_rmdir(fat_fs_t *fs, unsigned int parent_cluster, const char *name_8_3);
  44. /* Resolve a path "a/b/c" into (parent dir cluster, final 8.3 name). */
  45. int fat_resolve_path(fat_fs_t *fs, const char *path, unsigned int *dir_cluster,
  46. char name_8_3[12]);
  47. /* Resolve a full path (all components directories) to a directory cluster.
  48. * An empty path resolves to the root. */
  49. int fat_resolve_dir(fat_fs_t *fs, const char *path, unsigned int *dir_cluster);
  50. #endif