fat32.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * @file fat32.h
  3. * @author fslongjin ([email protected])
  4. * @brief fat32文件系统
  5. * @version 0.1
  6. * @date 2022-04-19
  7. *
  8. * @copyright Copyright (c) 2022
  9. *
  10. */
  11. #pragma once
  12. #include <filesystem/MBR.h>
  13. #include <filesystem/VFS/VFS.h>
  14. #define FAT32_MAX_PARTITION_NUM 128 // 系统支持的最大的fat32分区数量
  15. /**
  16. * @brief fat32文件系统引导扇区结构体
  17. *
  18. */
  19. struct fat32_BootSector_t
  20. {
  21. uint8_t BS_jmpBoot[3]; // 跳转指令
  22. uint8_t BS_OEMName[8]; // 生产厂商名
  23. uint16_t BPB_BytesPerSec; // 每扇区字节数
  24. uint8_t BPB_SecPerClus; // 每簇扇区数
  25. uint16_t BPB_RsvdSecCnt; // 保留扇区数
  26. uint8_t BPB_NumFATs; // FAT表数量
  27. uint16_t BPB_RootEntCnt; // 根目录文件数最大值
  28. uint16_t BPB_TotSec16; // 16位扇区总数
  29. uint8_t BPB_Media; // 介质描述符
  30. uint16_t BPB_FATSz16; // FAT12/16每FAT扇区数
  31. uint16_t BPB_SecPerTrk; // 每磁道扇区数
  32. uint16_t BPB_NumHeads; // 磁头数
  33. uint32_t BPB_HiddSec; // 隐藏扇区数
  34. uint32_t BPB_TotSec32; // 32位扇区总数
  35. uint32_t BPB_FATSz32; // FAT32每FAT扇区数
  36. uint16_t BPB_ExtFlags; // 扩展标志
  37. uint16_t BPB_FSVer; // 文件系统版本号
  38. uint32_t BPB_RootClus; // 根目录起始簇号
  39. uint16_t BPB_FSInfo; // FS info结构体的扇区号
  40. uint16_t BPB_BkBootSec; // 引导扇区的备份扇区号
  41. uint8_t BPB_Reserved0[12];
  42. uint8_t BS_DrvNum; // int0x13的驱动器号
  43. uint8_t BS_Reserved1;
  44. uint8_t BS_BootSig; // 扩展引导标记
  45. uint32_t BS_VolID; // 卷序列号
  46. uint8_t BS_VolLab[11]; // 卷标
  47. uint8_t BS_FilSysType[8]; // 文件系统类型
  48. uint8_t BootCode[420]; // 引导代码、数据
  49. uint16_t BS_TrailSig; // 结束标志0xAA55
  50. } __attribute__((packed));
  51. /**
  52. * @brief fat32文件系统的FSInfo扇区结构体
  53. *
  54. */
  55. struct fat32_FSInfo_t
  56. {
  57. uint32_t FSI_LeadSig; // FS info扇区标志符 数值为0x41615252
  58. uint8_t FSI_Reserved1[480]; // 保留使用,全部置为0
  59. uint32_t FSI_StrucSig; // 另一个标志符,数值为0x61417272
  60. uint32_t FSI_Free_Count; // 上一次记录的空闲簇数量,这是一个参考值
  61. uint32_t FSI_Nxt_Free; // 空闲簇的起始搜索位置,这是为驱动程序提供的参考值
  62. uint8_t FSI_Reserved2[12]; // 保留使用,全部置为0
  63. uint32_t FSI_TrailSig; // 结束标志,数值为0xaa550000
  64. } __attribute__((packed));
  65. #define ATTR_READ_ONLY (1 << 0)
  66. #define ATTR_HIDDEN (1 << 1)
  67. #define ATTR_SYSTEM (1 << 2)
  68. #define ATTR_VOLUME_ID (1 << 3)
  69. #define ATTR_DIRECTORY (1 << 4)
  70. #define ATTR_ARCHIVE (1 << 5)
  71. #define ATTR_LONG_NAME (ATTR_READ_ONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME_ID)
  72. /**
  73. * @brief fat32文件系统短目录项,大小为32bytes
  74. *
  75. */
  76. struct fat32_Directory_t
  77. {
  78. unsigned char DIR_Name[11];
  79. unsigned char DIR_Attr; // 文件属性
  80. unsigned char DIR_NTRes; // EXT|BASE => 8(BASE).3(EXT)
  81. // BASE:LowerCase(8),UpperCase(0)
  82. // EXT:LowerCase(16),UpperCase(0)
  83. unsigned char DIR_CrtTimeTenth; // 文件创建的毫秒级时间戳
  84. unsigned short DIR_CrtTime; // 文件创建时间
  85. unsigned short DIR_CrtDate; // 文件创建日期
  86. unsigned short DIR_LastAccDate; // 文件的最后访问日期
  87. unsigned short DIR_FstClusHI; // 起始簇号(高16bit)
  88. unsigned short DIR_WrtTime; // 最后写入时间
  89. unsigned short DIR_WrtDate; // 最后写入日期
  90. unsigned short DIR_FstClusLO; // 起始簇号(低16bit)
  91. unsigned int DIR_FileSize; // 文件大小
  92. } __attribute__((packed));
  93. #define LOWERCASE_BASE (8)
  94. #define LOWERCASE_EXT (16)
  95. /**
  96. * @brief fat32文件系统长目录项,大小为32bytes
  97. *
  98. */
  99. struct fat32_LongDirectory_t
  100. {
  101. unsigned char LDIR_Ord; // 长目录项的序号
  102. unsigned short LDIR_Name1[5]; // 长文件名的第1-5个字符,每个字符占2bytes
  103. unsigned char LDIR_Attr; // 目录项属性必须为ATTR_LONG_NAME
  104. unsigned char LDIR_Type; // 如果为0,则说明这是长目录项的子项
  105. unsigned char LDIR_Chksum; // 短文件名的校验和
  106. unsigned short LDIR_Name2[6]; // 长文件名的第6-11个字符,每个字符占2bytes
  107. unsigned short LDIR_FstClusLO; // 必须为0
  108. unsigned short LDIR_Name3[2]; // 长文件名的12-13个字符,每个字符占2bytes
  109. } __attribute__((packed));
  110. /**
  111. * @brief fat32文件系统的超级块信息结构体
  112. *
  113. */
  114. struct fat32_partition_info_t
  115. {
  116. uint16_t partition_id; // 全局fat32分区id
  117. uint8_t ahci_ctrl_num;
  118. uint8_t ahci_port_num;
  119. uint8_t part_num; // 硬盘中的分区号
  120. struct fat32_BootSector_t bootsector;
  121. struct fat32_FSInfo_t fsinfo;
  122. uint64_t fsinfo_sector_addr_infat;
  123. uint64_t bootsector_bak_sector_addr_infat;
  124. uint64_t starting_sector;
  125. uint64_t sector_count;
  126. uint64_t sec_per_clus; // 每簇扇区数
  127. uint64_t bytes_per_sec; // 每扇区字节数
  128. uint64_t bytes_per_clus; // 每簇字节数
  129. uint64_t first_data_sector; // 数据区起始扇区号
  130. uint64_t FAT1_base_sector; // FAT1表的起始簇号
  131. uint64_t FAT2_base_sector; // FAT2表的起始簇号
  132. uint64_t sec_per_FAT; // 每FAT表扇区数
  133. uint64_t NumFATs; // FAT表数
  134. };
  135. typedef struct fat32_partition_info_t fat32_sb_info_t;
  136. struct fat32_inode_info_t
  137. {
  138. uint64_t first_clus; // 文件的起始簇号
  139. uint64_t dEntry_location_clus; // fat entry的起始簇号 dEntry struct in cluster (0 is root, 1 is invalid)
  140. uint64_t dEntry_location_clus_offset; // fat entry在起始簇中的偏移量(是第几个entry) dEntry struct offset in cluster
  141. uint16_t create_date;
  142. uint16_t create_time;
  143. uint16_t write_time;
  144. uint16_t write_date;
  145. };
  146. typedef struct fat32_inode_info_t fat32_inode_info_t;
  147. /**
  148. * @brief 注册指定磁盘上的指定分区的fat32文件系统
  149. *
  150. * @param ahci_ctrl_num ahci控制器编号
  151. * @param ahci_port_num ahci控制器端口编号
  152. * @return struct vfs_super_block_t * 文件系统的超级块
  153. */
  154. struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t ahci_port_num, uint8_t part_num);
  155. /**
  156. * @brief 创建fat32文件系统的超级块
  157. *
  158. * @param DPTE 磁盘分区表entry
  159. * @param DPT_type 磁盘分区表类型
  160. * @param buf fat32文件系统的引导扇区
  161. * @return struct vfs_superblock_t* 创建好的超级块
  162. */
  163. struct vfs_superblock_t *fat32_read_superblock(void *DPTE, uint8_t DPT_type, void *buf, int8_t ahci_ctrl_num, int8_t ahci_port_num, int8_t part_num);
  164. long fat32_create(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dentry, int mode);
  165. void fat32_init();
  166. /**
  167. * @brief 读取文件夹(在指定目录中找出有效目录项)
  168. *
  169. * @param file_ptr 文件结构体指针
  170. * @param dirent 返回的dirent
  171. * @param filler 填充dirent的函数
  172. * @return int64_t
  173. */
  174. int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t filler);