fat.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. unsigned int next_free; /* allocation hint (FAT32: from FSInfo) */
  28. char label[12]; /* trimmed 8.3 volume label + NUL */
  29. int mounted;
  30. } fat_fs_t;
  31. /* Mount (parse BPB + validate FAT media byte) a filesystem at part_lba on dev.
  32. * Returns 0 on success, -1 if no valid FAT, -2 on I/O error. */
  33. int fat_mount(blk_dev_t *dev, unsigned int part_lba, fat_fs_t *fs);
  34. /* Iterate a directory (root or subdir), call callback for each entry. */
  35. int fat_list_dir(fat_fs_t *fs, unsigned int dir_cluster, fat_root_callback_t callback);
  36. /* Read a file by 8.3 name in a directory. */
  37. int fat_read_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
  38. void *buf, unsigned int max_len);
  39. /* Write / delete / mkdir / rmdir (FAT12/16/32). */
  40. int fat_write_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
  41. const void *buf, unsigned int size);
  42. int fat_delete_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3);
  43. int fat_mkdir(fat_fs_t *fs, unsigned int parent_cluster, const char *name_8_3);
  44. int fat_rmdir(fat_fs_t *fs, unsigned int parent_cluster, const char *name_8_3);
  45. /* Resolve a path "a/b/c" into (parent dir cluster, final 8.3 name). */
  46. int fat_resolve_path(fat_fs_t *fs, const char *path, unsigned int *dir_cluster,
  47. char name_8_3[12]);
  48. /* Resolve a full path (all components directories) to a directory cluster.
  49. * An empty path resolves to the root. */
  50. int fat_resolve_dir(fat_fs_t *fs, const char *path, unsigned int *dir_cluster);
  51. /* Stat a path: sets *is_dir and *size (file size; 0 for directories).
  52. * Returns 0, -1 not found, -2 on I/O error. */
  53. int fat_stat(fat_fs_t *fs, const char *path, unsigned int *size, int *is_dir);
  54. #endif