fat.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #pragma once
  2. #include <kernel/mem.h>
  3. #include <kernel/vfs.hpp>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <types/size.h>
  8. namespace fs::fat {
  9. using cluster_t = uint32_t;
  10. // for FAT32
  11. struct PACKED old_boot_sector {
  12. uint8_t jmp_instruction[3];
  13. char oem_name[8];
  14. // usually 512
  15. uint16_t bytes_per_sector;
  16. uint8_t sectors_per_cluster;
  17. // 32 for FAT32
  18. uint16_t reserved_sectors;
  19. // usually 2
  20. uint8_t fat_copies;
  21. // 0 for FAT32
  22. uint16_t root_directory_entries;
  23. // valid before FAT32
  24. uint16_t _sectors_cnt;
  25. // 0xf8 for hard disk
  26. uint8_t type;
  27. // valid before FAT32
  28. uint16_t _sectors_per_fat;
  29. // 12
  30. uint16_t sectors_per_track;
  31. // 2
  32. uint16_t heads;
  33. // 0
  34. uint16_t hidden_sectors;
  35. };
  36. // for FAT32
  37. struct PACKED ext_boot_sector {
  38. struct old_boot_sector old;
  39. // 0
  40. uint16_t hidden_sector_ext;
  41. uint32_t sectors_cnt;
  42. uint32_t sectors_per_fat;
  43. uint16_t mirror_flags;
  44. uint16_t fs_version;
  45. // 2
  46. cluster_t root_directory;
  47. // 1
  48. uint16_t fs_info_sector;
  49. // usually at 6, 0x0000 or 0xffff if none
  50. uint16_t backup_boot_sector;
  51. uint8_t _reserved[12];
  52. // for int $0x13
  53. uint8_t drive_number;
  54. uint8_t _reserved_for_current_head;
  55. // 0x29
  56. uint8_t ext_signature;
  57. uint32_t serial_number;
  58. char label[11];
  59. char fs_type[8];
  60. uint8_t _reserved_blank[420];
  61. // 0x55, 0xaa
  62. uint16_t magic;
  63. };
  64. struct PACKED fs_info_sector {
  65. // 0x41615252
  66. uint32_t signature_one;
  67. uint8_t _reserved[480];
  68. // 0x61417272
  69. uint32_t signature_two;
  70. // may be incorrect
  71. uint32_t free_clusters;
  72. // hint only
  73. uint32_t next_free_cluster;
  74. uint8_t _reserved_two[12];
  75. // 0xaa550000
  76. uint32_t sector_signature;
  77. };
  78. struct PACKED directory_entry {
  79. char filename[8];
  80. char extension[3];
  81. struct PACKED {
  82. uint8_t ro : 1;
  83. uint8_t hidden : 1;
  84. uint8_t system : 1;
  85. uint8_t volume_label : 1;
  86. uint8_t subdir : 1;
  87. uint8_t archive : 1;
  88. uint8_t _reserved : 2;
  89. } attributes;
  90. uint8_t _reserved;
  91. uint8_t c_time_date[5];
  92. uint16_t access_date;
  93. uint16_t cluster_hi;
  94. uint8_t m_time_date[4];
  95. uint16_t cluster_lo;
  96. uint32_t size;
  97. };
  98. // TODO: deallocate inodes when dentry is destroyed
  99. class fat32 : public virtual fs::vfs {
  100. private:
  101. constexpr static uint32_t SECTOR_SIZE = 512;
  102. constexpr static cluster_t EOC = 0xffffff8;
  103. private:
  104. uint32_t sector_cnt;
  105. uint32_t sectors_per_fat;
  106. uint32_t serial_number;
  107. uint32_t free_clusters;
  108. uint32_t next_free_cluster_hint;
  109. cluster_t root_dir;
  110. cluster_t data_region_offset;
  111. dev_t device;
  112. uint16_t reserved_sectors;
  113. uint8_t fat_copies;
  114. uint8_t sectors_per_cluster;
  115. char label[12];
  116. cluster_t* fat;
  117. struct buf_object {
  118. char* data;
  119. int ref;
  120. // bool dirty;
  121. };
  122. types::hash_map<cluster_t, buf_object> buf;
  123. // buf MUST be larger than 512 bytes
  124. inline void _raw_read_sector(void* buf, uint32_t sector_no);
  125. // buf MUST be larger than 4096 bytes
  126. inline void _raw_read_cluster(void* buf, cluster_t no);
  127. // buffered version, release_cluster(cluster_no) after used
  128. char* read_cluster(cluster_t no);
  129. void release_cluster(cluster_t no);
  130. static constexpr cluster_t cl(const inode* ind)
  131. {
  132. return ind->ino;
  133. }
  134. static inline cluster_t _rearrange(directory_entry* d)
  135. {
  136. return (((cluster_t)d->cluster_hi) << 16) + d->cluster_lo;
  137. }
  138. static inline size_t _write_buf_n(char* buf, size_t buf_size, const char* src, size_t n)
  139. {
  140. if (n <= buf_size) {
  141. memcpy(buf, src, n);
  142. return n;
  143. } else {
  144. memcpy(buf, src, buf_size);
  145. return buf_size;
  146. }
  147. }
  148. public:
  149. fat32(const fat32&) = delete;
  150. explicit fat32(dev_t device);
  151. ~fat32();
  152. virtual size_t read(inode* file, char* buf, size_t buf_size, size_t offset, size_t n) override;
  153. virtual int readdir(fs::inode* dir, size_t offset, const fs::vfs::filldir_func& callback) override;
  154. virtual int inode_statx(dentry* ent, statx* st, unsigned int mask) override;
  155. virtual int inode_stat(dentry* ent, struct stat* st) override;
  156. };
  157. }; // namespace fs::fat