fat32.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. #include "fat32.h"
  2. #include <common/kprint.h>
  3. #include <driver/disk/ahci/ahci.h>
  4. #include <filesystem/MBR.h>
  5. #include <process/spinlock.h>
  6. #include <mm/slab.h>
  7. struct vfs_super_block_operations_t fat32_sb_ops;
  8. struct vfs_dir_entry_operations_t fat32_dEntry_ops;
  9. struct vfs_file_operations_t fat32_file_ops;
  10. struct vfs_inode_operations_t fat32_inode_ops;
  11. /**
  12. * @brief 注册指定磁盘上的指定分区的fat32文件系统
  13. *
  14. * @param ahci_ctrl_num ahci控制器编号
  15. * @param ahci_port_num ahci控制器端口编号
  16. * @param part_num 磁盘分区编号
  17. *
  18. * @return struct vfs_super_block_t * 文件系统的超级块
  19. */
  20. struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t ahci_port_num, uint8_t part_num)
  21. {
  22. struct MBR_disk_partition_table_t *DPT = MBR_read_partition_table(ahci_ctrl_num, ahci_port_num);
  23. // for(i = 0 ;i < 512 ; i++)
  24. // color_printk(PURPLE,WHITE,"%02x",buf[i]);
  25. printk_color(ORANGE, BLACK, "DPTE[0] start_LBA:%#018lx\ttype:%#018lx\n", DPT->DPTE[part_num].starting_LBA, DPT->DPTE[part_num].type);
  26. uint8_t buf[512] = {0};
  27. // 读取文件系统的boot扇区
  28. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, DPT->DPTE[part_num].starting_LBA, 1, (uint64_t)&buf, ahci_ctrl_num, ahci_port_num);
  29. // 挂载文件系统到vfs
  30. return vfs_mount_fs("FAT32", (void *)(&DPT->DPTE[part_num]), VFS_DPT_MBR, buf, ahci_ctrl_num, ahci_port_num, part_num);
  31. }
  32. /**
  33. * @brief 读取指定簇的FAT表项
  34. *
  35. * @param fsbi fat32超级块私有信息结构体
  36. * @param cluster 指定簇
  37. * @return uint32_t 下一个簇的簇号
  38. */
  39. uint32_t fat32_read_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster)
  40. {
  41. // 计算每个扇区内含有的FAT表项数
  42. // FAT每项4bytes
  43. uint32_t fat_ent_per_sec = (fsbi->bootsector.BPB_BytesPerSec >> 2); // 该值应为2的n次幂
  44. uint32_t buf[256];
  45. memset(buf, 0, fsbi->bootsector.BPB_BytesPerSec);
  46. // 读取一个sector的数据,
  47. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
  48. (uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  49. // 返回下一个fat表项的值(也就是下一个cluster)
  50. return buf[cluster & (fat_ent_per_sec - 1)] & 0x0fffffff;
  51. ;
  52. }
  53. /**
  54. * @brief 写入指定簇的FAT表项
  55. *
  56. * @param fsbi fat32超级块私有信息结构体
  57. * @param cluster 指定簇
  58. * @param value 要写入该fat表项的值
  59. * @return uint32_t errcode
  60. */
  61. uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t value)
  62. {
  63. // 计算每个扇区内含有的FAT表项数
  64. // FAT每项4bytes
  65. uint32_t fat_ent_per_sec = (fsbi->bootsector.BPB_BytesPerSec >> 2); // 该值应为2的n次幂
  66. uint32_t buf[256];
  67. memset(buf, 0, fsbi->bootsector.BPB_BytesPerSec);
  68. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
  69. (uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  70. buf[cluster & (fat_ent_per_sec - 1)] = (buf[cluster & (fat_ent_per_sec - 1)] & 0xf0000000) | (value & 0x0fffffff);
  71. // 向FAT1和FAT2写入数据
  72. ahci_operation.transfer(ATA_CMD_WRITE_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
  73. (uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  74. ahci_operation.transfer(ATA_CMD_WRITE_DMA_EXT, fsbi->FAT2_base_sector + (cluster / fat_ent_per_sec), 1,
  75. (uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  76. return 0;
  77. }
  78. /**
  79. * @brief 在父目录中寻找指定的目录项
  80. *
  81. * @param parent_inode 父目录项的inode
  82. * @param dest_inode 搜索目标目录项的inode
  83. * @return struct vfs_dir_entry_t* 目标目录项
  84. */
  85. struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dentry)
  86. {
  87. int errcode = 0;
  88. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
  89. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
  90. uint8_t *buf = kmalloc(fsbi->bytes_per_clus, 0);
  91. memset(buf, 0, fsbi->bytes_per_clus);
  92. // 计算父目录项的起始簇号
  93. uint32_t cluster = finode->first_clus;
  94. struct fat32_Directory_t *tmp_dEntry = NULL;
  95. while (true)
  96. {
  97. // 计算父目录项的起始LBA扇区号
  98. uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  99. // kdebug("fat32_part_info[part_id].bootsector.BPB_SecPerClus=%d",fat32_part_info[part_id].bootsector.BPB_SecPerClus);
  100. // kdebug("sector=%d",sector);
  101. // 读取父目录项的起始簇数据
  102. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  103. // ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, sector, fat32_part_info[part_id].bootsector.BPB_SecPerClus, (uint64_t)buf, fat32_part_info[part_id].ahci_ctrl_num, fat32_part_info[part_id].ahci_port_num);
  104. tmp_dEntry = (struct fat32_Directory_t *)buf;
  105. // 查找短目录项
  106. for (int i = 0; i < fsbi->bytes_per_clus; i += 32, ++tmp_dEntry)
  107. {
  108. // 跳过长目录项
  109. if (tmp_dEntry->DIR_Attr == ATTR_LONG_NAME)
  110. continue;
  111. // 跳过无效页表项、空闲页表项
  112. if (tmp_dEntry->DIR_Name[0] == 0xe5 || tmp_dEntry->DIR_Name[0] == 0x00 || tmp_dEntry->DIR_Name[0] == 0x05)
  113. continue;
  114. // 找到长目录项,位于短目录项之前
  115. struct fat32_LongDirectory_t *tmp_ldEntry = (struct fat32_LongDirectory_t *)tmp_dEntry - 1;
  116. int js = 0;
  117. // 遍历每个长目录项
  118. while (tmp_ldEntry->LDIR_Attr == ATTR_LONG_NAME && tmp_ldEntry->LDIR_Ord != 0xe5)
  119. {
  120. // 比较name1
  121. for (int x = 0; x < 5; ++x)
  122. {
  123. if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name1[x] == 0xffff)
  124. continue;
  125. else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name1[x] != (uint16_t)(dest_dentry->name[js++])) // 文件名不匹配,检索下一个短目录项
  126. goto continue_cmp_fail;
  127. }
  128. // 比较name2
  129. for (int x = 0; x < 6; ++x)
  130. {
  131. if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name2[x] == 0xffff)
  132. continue;
  133. else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name2[x] != (uint16_t)(dest_dentry->name[js++])) // 文件名不匹配,检索下一个短目录项
  134. goto continue_cmp_fail;
  135. }
  136. // 比较name3
  137. for (int x = 0; x < 2; ++x)
  138. {
  139. if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name3[x] == 0xffff)
  140. continue;
  141. else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name3[x] != (uint16_t)(dest_dentry->name[js++])) // 文件名不匹配,检索下一个短目录项
  142. goto continue_cmp_fail;
  143. }
  144. if (js >= dest_dentry->name_length) // 找到需要的目录项,返回
  145. {
  146. goto find_lookup_success;
  147. }
  148. --tmp_ldEntry; // 检索下一个长目录项
  149. }
  150. // 不存在长目录项,匹配短目录项的基础名
  151. js = 0;
  152. for (int x = 0; x < 8; ++x)
  153. {
  154. switch (tmp_dEntry->DIR_Name[x])
  155. {
  156. case ' ':
  157. if (!(tmp_dEntry->DIR_Attr & ATTR_DIRECTORY)) // 不是文件夹(是文件)
  158. {
  159. if (dest_dentry->name[js] == '.')
  160. continue;
  161. else if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  162. {
  163. ++js;
  164. break;
  165. }
  166. else
  167. goto continue_cmp_fail;
  168. }
  169. else // 是文件夹
  170. {
  171. if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js]) // 当前位正确匹配
  172. {
  173. ++js;
  174. break; // 进行下一位的匹配
  175. }
  176. else if (js == dest_dentry->name_length)
  177. continue;
  178. else
  179. goto continue_cmp_fail;
  180. }
  181. break;
  182. // 当前位是字母
  183. case 'A' ... 'Z':
  184. case 'a' ... 'z':
  185. if (tmp_dEntry->DIR_NTRes & LOWERCASE_BASE) // 为兼容windows系统,检测DIR_NTRes字段
  186. {
  187. if (js < dest_dentry->name_length && (tmp_dEntry->DIR_Name[x] + 32 == dest_dentry->name[js]))
  188. {
  189. ++js;
  190. break;
  191. }
  192. else
  193. goto continue_cmp_fail;
  194. }
  195. else
  196. {
  197. if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  198. {
  199. ++js;
  200. break;
  201. }
  202. else
  203. goto continue_cmp_fail;
  204. }
  205. break;
  206. case '0' ... '9':
  207. if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  208. {
  209. ++js;
  210. break;
  211. }
  212. else
  213. goto continue_cmp_fail;
  214. break;
  215. default:
  216. ++js;
  217. break;
  218. }
  219. }
  220. // 若短目录项为文件,则匹配扩展名
  221. if (!(tmp_dEntry->DIR_Attr & ATTR_DIRECTORY))
  222. {
  223. ++js;
  224. for (int x = 8; x < 11; ++x)
  225. {
  226. switch (tmp_dEntry->DIR_Name[x])
  227. {
  228. // 当前位是字母
  229. case 'A' ... 'Z':
  230. case 'a' ... 'z':
  231. if (tmp_dEntry->DIR_NTRes & LOWERCASE_EXT) // 为兼容windows系统,检测DIR_NTRes字段
  232. {
  233. if ((tmp_dEntry->DIR_Name[x] + 32 == dest_dentry->name[js]))
  234. {
  235. ++js;
  236. break;
  237. }
  238. else
  239. goto continue_cmp_fail;
  240. }
  241. else
  242. {
  243. if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  244. {
  245. ++js;
  246. break;
  247. }
  248. else
  249. goto continue_cmp_fail;
  250. }
  251. break;
  252. case '0' ... '9':
  253. case ' ':
  254. if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  255. {
  256. ++js;
  257. break;
  258. }
  259. else
  260. goto continue_cmp_fail;
  261. break;
  262. default:
  263. goto continue_cmp_fail;
  264. break;
  265. }
  266. }
  267. }
  268. goto find_lookup_success;
  269. continue_cmp_fail:;
  270. }
  271. // 当前簇没有发现目标文件名,寻找下一个簇
  272. cluster = fat32_read_FAT_entry(fsbi, cluster);
  273. if (cluster >= 0x0ffffff7) // 寻找完父目录的所有簇,都没有找到目标文件名
  274. {
  275. kfree(buf);
  276. return NULL;
  277. }
  278. }
  279. find_lookup_success:; // 找到目标dentry
  280. struct vfs_index_node_t *p = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
  281. memset(p, 0, sizeof(struct vfs_index_node_t));
  282. p->file_size = tmp_dEntry->DIR_FileSize;
  283. // 计算文件占用的扇区数, 由于最小存储单位是簇,因此需要按照簇的大小来对齐扇区
  284. p->blocks = (p->file_size + fsbi->bytes_per_clus - 1) / fsbi->bytes_per_sec;
  285. p->attribute = (tmp_dEntry->DIR_Attr & ATTR_DIRECTORY) ? VFS_ATTR_DIR : VFS_ATTR_FILE;
  286. p->sb = parent_inode->sb;
  287. p->file_ops = &fat32_file_ops;
  288. p->inode_ops = &fat32_inode_ops;
  289. // 为inode的与文件系统相关的信息结构体分配空间
  290. p->private_inode_info = (void *)kmalloc(sizeof(fat32_inode_info_t), 0);
  291. memset(p->private_inode_info, 0, sizeof(fat32_inode_info_t));
  292. finode = (fat32_inode_info_t *)p->private_inode_info;
  293. finode->first_clus = ((tmp_dEntry->DIR_FstClusHI << 16) | tmp_dEntry->DIR_FstClusLO) & 0x0fffffff;
  294. finode->dEntry_location_clus = cluster;
  295. finode->dEntry_location_clus_offset = tmp_dEntry - (struct fat32_Directory_t *)buf; //计算dentry的偏移量
  296. finode->create_date = tmp_dEntry->DIR_CrtDate;
  297. finode->create_time = tmp_dEntry->DIR_CrtTime;
  298. finode->write_date = tmp_dEntry->DIR_WrtDate;
  299. finode->write_time = tmp_dEntry->DIR_WrtTime;
  300. dest_dentry->dir_inode = p;
  301. kfree(buf);
  302. return dest_dentry;
  303. }
  304. /**
  305. * @brief 按照路径查找文件
  306. *
  307. * @param part_id fat32分区id
  308. * @param path
  309. * @param flags 1:返回父目录项, 0:返回结果目录项
  310. * @return struct vfs_dir_entry_t* 目录项
  311. */
  312. struct vfs_dir_entry_t *fat32_path_walk(char *path, uint64_t flags)
  313. {
  314. struct vfs_dir_entry_t *parent = vfs_root_sb->root;
  315. // 去除路径前的斜杠
  316. while (*path == '/')
  317. ++path;
  318. if ((!*path) || (*path == '\0'))
  319. return parent;
  320. struct vfs_dir_entry_t *dentry;
  321. while (true)
  322. {
  323. // 提取出下一级待搜索的目录名或文件名,并保存在dEntry_name中
  324. char *tmp_path = path;
  325. while ((*path && *path != '\0') && (*path != '/'))
  326. ++path;
  327. int tmp_path_len = path - tmp_path;
  328. dentry = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
  329. memset(dentry, 0, sizeof(struct vfs_dir_entry_t));
  330. // 为目录项的名称分配内存
  331. dentry->name = (char *)kmalloc(tmp_path_len + 1, 0);
  332. // 貌似这里不需要memset,因为空间会被覆盖
  333. // memset(dentry->name, 0, tmp_path_len+1);
  334. memcpy(dentry->name, tmp_path, tmp_path_len);
  335. dentry->name[tmp_path_len] = '\0';
  336. dentry->name_length = tmp_path_len;
  337. if (parent->dir_inode->inode_ops->lookup(parent->dir_inode, dentry) == NULL)
  338. {
  339. // 搜索失败
  340. kerror("cannot find the file/dir : %s", dentry->name);
  341. kfree(dentry->name);
  342. kfree(dentry);
  343. return NULL;
  344. }
  345. // 找到子目录项
  346. // 初始化子目录项的entry
  347. list_init(&dentry->child_node_list);
  348. list_init(&dentry->subdirs_list);
  349. dentry->parent = parent;
  350. while (*path == '/')
  351. ++path;
  352. if ((!*path) || (*path == '\0')) // 已经到达末尾
  353. {
  354. if (flags & 1) // 返回父目录
  355. {
  356. return parent;
  357. }
  358. return dentry;
  359. }
  360. parent = dentry;
  361. }
  362. }
  363. /**
  364. * @brief 创建fat32文件系统的超级块
  365. *
  366. * @param DPTE 磁盘分区表entry
  367. * @param DPT_type 磁盘分区表类型
  368. * @param buf fat32文件系统的引导扇区
  369. * @return struct vfs_superblock_t* 创建好的超级块
  370. */
  371. 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)
  372. {
  373. if (DPT_type != VFS_DPT_MBR) // 暂时只支持MBR分区表
  374. {
  375. kerror("fat32_read_superblock(): Unsupported DPT!");
  376. return NULL;
  377. }
  378. // 分配超级块的空间
  379. struct vfs_superblock_t *sb_ptr = (struct vfs_superblock_t *)kmalloc(sizeof(struct vfs_superblock_t), 0);
  380. memset(sb_ptr, 0, sizeof(struct vfs_superblock_t));
  381. sb_ptr->sb_ops = &fat32_sb_ops;
  382. sb_ptr->private_sb_info = kmalloc(sizeof(fat32_sb_info_t), 0);
  383. memset(sb_ptr->private_sb_info, 0, sizeof(fat32_sb_info_t));
  384. struct fat32_BootSector_t *fbs = (struct fat32_BootSector_t *)buf;
  385. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(sb_ptr->private_sb_info);
  386. // MBR分区表entry
  387. struct MBR_disk_partition_table_entry_t *MBR_DPTE = (struct MBR_disk_partition_table_entry_t *)DPTE;
  388. fsbi->ahci_ctrl_num = ahci_ctrl_num;
  389. fsbi->ahci_port_num = ahci_port_num;
  390. fsbi->part_num = part_num;
  391. fsbi->starting_sector = MBR_DPTE->starting_LBA;
  392. fsbi->sector_count = MBR_DPTE->total_sectors;
  393. fsbi->sec_per_clus = fbs->BPB_SecPerClus;
  394. fsbi->bytes_per_clus = fbs->BPB_SecPerClus * fbs->BPB_BytesPerSec;
  395. fsbi->bytes_per_sec = fbs->BPB_BytesPerSec;
  396. fsbi->first_data_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt + fbs->BPB_FATSz32 * fbs->BPB_NumFATs;
  397. fsbi->FAT1_base_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt;
  398. fsbi->FAT2_base_sector = fsbi->FAT1_base_sector + fbs->BPB_FATSz32;
  399. fsbi->sec_per_FAT = fbs->BPB_FATSz32;
  400. fsbi->NumFATs = fbs->BPB_NumFATs;
  401. fsbi->fsinfo_sector_addr_infat = fbs->BPB_FSInfo;
  402. fsbi->bootsector_bak_sector_addr_infat = fbs->BPB_BkBootSec;
  403. printk_color(ORANGE, BLACK, "FAT32 Boot Sector\n\tBPB_FSInfo:%#018lx\n\tBPB_BkBootSec:%#018lx\n\tBPB_TotSec32:%#018lx\n", fbs->BPB_FSInfo, fbs->BPB_BkBootSec, fbs->BPB_TotSec32);
  404. // fsinfo扇区的信息
  405. memset(&fsbi->fsinfo, 0, sizeof(struct fat32_FSInfo_t));
  406. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, MBR_DPTE->starting_LBA + fbs->BPB_FSInfo, 1, (uint64_t)&fsbi->fsinfo, ahci_ctrl_num, ahci_port_num);
  407. printk_color(BLUE, BLACK, "FAT32 FSInfo\n\tFSI_LeadSig:%#018lx\n\tFSI_StrucSig:%#018lx\n\tFSI_Free_Count:%#018lx\n", fsbi->fsinfo.FSI_LeadSig, fsbi->fsinfo.FSI_StrucSig, fsbi->fsinfo.FSI_Free_Count);
  408. // 初始化超级块的dir entry
  409. sb_ptr->root = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
  410. memset(sb_ptr->root, 0, sizeof(struct vfs_dir_entry_t));
  411. list_init(&sb_ptr->root->child_node_list);
  412. list_init(&sb_ptr->root->subdirs_list);
  413. sb_ptr->root->parent = sb_ptr->root;
  414. sb_ptr->root->dir_ops = &fat32_dEntry_ops;
  415. // 分配2个字节的name
  416. sb_ptr->root->name = (char *)(kmalloc(2, 0));
  417. sb_ptr->root->name[0] = '/';
  418. sb_ptr->root->name_length = 1;
  419. // 为root目录项分配index node
  420. sb_ptr->root->dir_inode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
  421. memset(sb_ptr->root->dir_inode, 0, sizeof(struct vfs_index_node_t));
  422. sb_ptr->root->dir_inode->inode_ops = &fat32_inode_ops;
  423. sb_ptr->root->dir_inode->file_ops = &fat32_file_ops;
  424. sb_ptr->root->dir_inode->file_size = 0;
  425. // 计算文件占用的扇区数, 由于最小存储单位是簇,因此需要按照簇的大小来对齐扇区
  426. sb_ptr->root->dir_inode->blocks = (sb_ptr->root->dir_inode->file_size + fsbi->bytes_per_clus - 1) / fsbi->bytes_per_sec;
  427. sb_ptr->root->dir_inode->attribute = VFS_ATTR_DIR;
  428. sb_ptr->root->dir_inode->sb = sb_ptr; // 反向绑定对应的超级块
  429. // 初始化inode信息
  430. sb_ptr->root->dir_inode->private_inode_info = kmalloc(sizeof(struct fat32_inode_info_t), 0);
  431. memset(sb_ptr->root->dir_inode->private_inode_info, 0, sizeof(struct fat32_inode_info_t));
  432. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)sb_ptr->root->dir_inode->private_inode_info;
  433. finode->first_clus = fbs->BPB_RootClus;
  434. finode->dEntry_location_clus = 0;
  435. finode->dEntry_location_clus_offset = 0;
  436. finode->create_time = 0;
  437. finode->create_date = 0;
  438. finode->write_date = 0;
  439. finode->write_time;
  440. return sb_ptr;
  441. }
  442. /**
  443. * @brief todo: 写入superblock
  444. *
  445. * @param sb
  446. */
  447. void fat32_write_superblock(struct vfs_superblock_t *sb)
  448. {
  449. }
  450. /**
  451. * @brief 释放superblock的内存空间
  452. *
  453. * @param sb 要被释放的superblock
  454. */
  455. void fat32_put_superblock(struct vfs_superblock_t *sb)
  456. {
  457. kfree(sb->private_sb_info);
  458. kfree(sb->root->dir_inode->private_inode_info);
  459. kfree(sb->root->dir_inode);
  460. kfree(sb->root);
  461. kfree(sb);
  462. }
  463. /**
  464. * @brief 写入inode到硬盘上
  465. *
  466. * @param inode
  467. */
  468. void fat32_write_inode(struct vfs_index_node_t *inode)
  469. {
  470. fat32_inode_info_t *finode = inode->private_inode_info;
  471. if (finode->dEntry_location_clus == 0)
  472. {
  473. kerror("FAT32 error: Attempt to write the root inode");
  474. return;
  475. }
  476. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)inode->sb->private_sb_info;
  477. // 计算目标inode对应数据区的LBA地址
  478. uint64_t fLBA = fsbi->first_data_sector + (finode->dEntry_location_clus - 2) * fsbi->sec_per_clus;
  479. uint8_t *buf = (uint8_t *)kmalloc(fsbi->bytes_per_clus, 0);
  480. memset(buf, 0, sizeof(fsbi->bytes_per_clus));
  481. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  482. // 计算目标dEntry所在的位置
  483. struct fat32_Directory_t *fdEntry = (struct fat32_Directory_t *)((uint64_t)buf + finode->dEntry_location_clus_offset);
  484. // 写入fat32文件系统的dir_entry
  485. fdEntry->DIR_FileSize = inode->file_size;
  486. fdEntry->DIR_FstClusLO = finode->first_clus & 0xffff;
  487. fdEntry->DIR_FstClusHI = (finode->first_clus >> 16) | (fdEntry->DIR_FstClusHI & 0xf000);
  488. // 将dir entry写回磁盘
  489. ahci_operation.transfer(ATA_CMD_WRITE_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  490. kfree(buf);
  491. }
  492. struct vfs_super_block_operations_t fat32_sb_ops =
  493. {
  494. .write_superblock = fat32_write_superblock,
  495. .put_superblock = fat32_put_superblock,
  496. .write_inode = fat32_write_inode,
  497. };
  498. // todo: compare
  499. long fat32_compare(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename)
  500. {
  501. }
  502. // todo: hash
  503. long fat32_hash(struct vfs_dir_entry_t *dEntry, char *filename)
  504. {
  505. }
  506. // todo: release
  507. long fat32_release(struct vfs_dir_entry_t *dEntry)
  508. {
  509. }
  510. // todo: iput
  511. long fat32_iput(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode)
  512. {
  513. }
  514. /**
  515. * @brief fat32文件系统对于dEntry的操作
  516. *
  517. */
  518. struct vfs_dir_entry_operations_t fat32_dEntry_ops =
  519. {
  520. .compare = fat32_compare,
  521. .hash = fat32_hash,
  522. .release = fat32_release,
  523. .iput = fat32_iput,
  524. };
  525. // todo: open
  526. long fat32_open(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  527. {
  528. }
  529. // todo: close
  530. long fat32_close(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  531. {
  532. }
  533. // todo: read
  534. long fat32_read(struct vfs_file_t *file_ptr, char *buf, uint64_t buf_size, long *position)
  535. {
  536. }
  537. // todo: write
  538. long fat32_write(struct vfs_file_t *file_ptr, char *buf, uint64_t buf_size, long *position)
  539. {
  540. }
  541. // todo: lseek
  542. long fat32_lseek(struct vfs_file_t *file_ptr, long offset, long origin)
  543. {
  544. }
  545. // todo: ioctl
  546. long fat32_ioctl(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr, uint64_t cmd, uint64_t arg)
  547. {
  548. }
  549. /**
  550. * @brief fat32文件系统,关于文件的操作
  551. *
  552. */
  553. struct vfs_file_operations_t fat32_file_ops =
  554. {
  555. .open = fat32_open,
  556. .close = fat32_close,
  557. .read = fat32_read,
  558. .write = fat32_write,
  559. .lseek = fat32_lseek,
  560. .ioctl = fat32_ioctl,
  561. };
  562. // todo: create
  563. long fat32_create(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dentry, int mode)
  564. {
  565. }
  566. // todo: mkdir
  567. int64_t fat32_mkdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry, int mode)
  568. {
  569. }
  570. // todo: rmdir
  571. int64_t fat32_rmdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry)
  572. {
  573. }
  574. // todo: rename
  575. int64_t fat32_rename(struct vfs_index_node_t *old_inode, struct vfs_dir_entry_t *old_dEntry, struct vfs_index_node_t *new_inode, struct vfs_dir_entry_t *new_dEntry)
  576. {
  577. }
  578. // todo: getAttr
  579. int64_t fat32_getAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
  580. {
  581. }
  582. // todo: setAttr
  583. int64_t fat32_setAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
  584. {
  585. }
  586. struct vfs_inode_operations_t fat32_inode_ops =
  587. {
  588. .create = fat32_create,
  589. .mkdir = fat32_mkdir,
  590. .rmdir = fat32_rmdir,
  591. .lookup = fat32_lookup,
  592. .rename = fat32_rename,
  593. .getAttr = fat32_getAttr,
  594. .setAttr = fat32_setAttr,
  595. };
  596. struct vfs_filesystem_type_t fat32_fs_type =
  597. {
  598. .name = "FAT32",
  599. .fs_flags = 0,
  600. .read_superblock = fat32_read_superblock,
  601. .next = NULL,
  602. };
  603. void fat32_init()
  604. {
  605. // kinfo("Initializing FAT32...");
  606. // 在VFS中注册fat32文件系统
  607. vfs_register_filesystem(&fat32_fs_type);
  608. // 挂载根文件系统
  609. vfs_root_sb = fat32_register_partition(0, 0, 0);
  610. }