fat32.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. #include <common/errno.h>
  8. struct vfs_super_block_operations_t fat32_sb_ops;
  9. struct vfs_dir_entry_operations_t fat32_dEntry_ops;
  10. struct vfs_file_operations_t fat32_file_ops;
  11. struct vfs_inode_operations_t fat32_inode_ops;
  12. /**
  13. * @brief 注册指定磁盘上的指定分区的fat32文件系统
  14. *
  15. * @param ahci_ctrl_num ahci控制器编号
  16. * @param ahci_port_num ahci控制器端口编号
  17. * @param part_num 磁盘分区编号
  18. *
  19. * @return struct vfs_super_block_t * 文件系统的超级块
  20. */
  21. struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t ahci_port_num, uint8_t part_num)
  22. {
  23. struct MBR_disk_partition_table_t *DPT = MBR_read_partition_table(ahci_ctrl_num, ahci_port_num);
  24. // for(i = 0 ;i < 512 ; i++)
  25. // color_printk(PURPLE,WHITE,"%02x",buf[i]);
  26. printk_color(ORANGE, BLACK, "DPTE[0] start_LBA:%#018lx\ttype:%#018lx\n", DPT->DPTE[part_num].starting_LBA, DPT->DPTE[part_num].type);
  27. uint8_t buf[512] = {0};
  28. // 读取文件系统的boot扇区
  29. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, DPT->DPTE[part_num].starting_LBA, 1, (uint64_t)&buf, ahci_ctrl_num, ahci_port_num);
  30. // 挂载文件系统到vfs
  31. return vfs_mount_fs("FAT32", (void *)(&DPT->DPTE[part_num]), VFS_DPT_MBR, buf, ahci_ctrl_num, ahci_port_num, part_num);
  32. }
  33. /**
  34. * @brief 读取指定簇的FAT表项
  35. *
  36. * @param fsbi fat32超级块私有信息结构体
  37. * @param cluster 指定簇
  38. * @return uint32_t 下一个簇的簇号
  39. */
  40. uint32_t fat32_read_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster)
  41. {
  42. // 计算每个扇区内含有的FAT表项数
  43. // FAT每项4bytes
  44. uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂
  45. uint32_t buf[256];
  46. memset(buf, 0, fsbi->bytes_per_sec);
  47. // 读取一个sector的数据,
  48. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
  49. (uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  50. // 返回下一个fat表项的值(也就是下一个cluster)
  51. return buf[cluster & (fat_ent_per_sec - 1)] & 0x0fffffff;
  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(AHCI_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(AHCI_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(AHCI_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(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  103. // ahci_operation.transfer(AHCI_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 创建fat32文件系统的超级块
  306. *
  307. * @param DPTE 磁盘分区表entry
  308. * @param DPT_type 磁盘分区表类型
  309. * @param buf fat32文件系统的引导扇区
  310. * @return struct vfs_superblock_t* 创建好的超级块
  311. */
  312. 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)
  313. {
  314. if (DPT_type != VFS_DPT_MBR) // 暂时只支持MBR分区表
  315. {
  316. kerror("fat32_read_superblock(): Unsupported DPT!");
  317. return NULL;
  318. }
  319. // 分配超级块的空间
  320. struct vfs_superblock_t *sb_ptr = (struct vfs_superblock_t *)kmalloc(sizeof(struct vfs_superblock_t), 0);
  321. memset(sb_ptr, 0, sizeof(struct vfs_superblock_t));
  322. sb_ptr->sb_ops = &fat32_sb_ops;
  323. sb_ptr->private_sb_info = kmalloc(sizeof(fat32_sb_info_t), 0);
  324. memset(sb_ptr->private_sb_info, 0, sizeof(fat32_sb_info_t));
  325. struct fat32_BootSector_t *fbs = (struct fat32_BootSector_t *)buf;
  326. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(sb_ptr->private_sb_info);
  327. // MBR分区表entry
  328. struct MBR_disk_partition_table_entry_t *MBR_DPTE = (struct MBR_disk_partition_table_entry_t *)DPTE;
  329. fsbi->ahci_ctrl_num = ahci_ctrl_num;
  330. fsbi->ahci_port_num = ahci_port_num;
  331. fsbi->part_num = part_num;
  332. fsbi->starting_sector = MBR_DPTE->starting_LBA;
  333. fsbi->sector_count = MBR_DPTE->total_sectors;
  334. fsbi->sec_per_clus = fbs->BPB_SecPerClus;
  335. fsbi->bytes_per_clus = fbs->BPB_SecPerClus * fbs->BPB_BytesPerSec;
  336. fsbi->bytes_per_sec = fbs->BPB_BytesPerSec;
  337. fsbi->first_data_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt + fbs->BPB_FATSz32 * fbs->BPB_NumFATs;
  338. fsbi->FAT1_base_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt;
  339. fsbi->FAT2_base_sector = fsbi->FAT1_base_sector + fbs->BPB_FATSz32;
  340. fsbi->sec_per_FAT = fbs->BPB_FATSz32;
  341. fsbi->NumFATs = fbs->BPB_NumFATs;
  342. fsbi->fsinfo_sector_addr_infat = fbs->BPB_FSInfo;
  343. fsbi->bootsector_bak_sector_addr_infat = fbs->BPB_BkBootSec;
  344. 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);
  345. // fsinfo扇区的信息
  346. memset(&fsbi->fsinfo, 0, sizeof(struct fat32_FSInfo_t));
  347. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, MBR_DPTE->starting_LBA + fbs->BPB_FSInfo, 1, (uint64_t)&fsbi->fsinfo, ahci_ctrl_num, ahci_port_num);
  348. 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);
  349. // 初始化超级块的dir entry
  350. sb_ptr->root = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
  351. memset(sb_ptr->root, 0, sizeof(struct vfs_dir_entry_t));
  352. list_init(&sb_ptr->root->child_node_list);
  353. list_init(&sb_ptr->root->subdirs_list);
  354. sb_ptr->root->parent = sb_ptr->root;
  355. sb_ptr->root->dir_ops = &fat32_dEntry_ops;
  356. // 分配2个字节的name
  357. sb_ptr->root->name = (char *)(kmalloc(2, 0));
  358. sb_ptr->root->name[0] = '/';
  359. sb_ptr->root->name_length = 1;
  360. // 为root目录项分配index node
  361. sb_ptr->root->dir_inode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
  362. memset(sb_ptr->root->dir_inode, 0, sizeof(struct vfs_index_node_t));
  363. sb_ptr->root->dir_inode->inode_ops = &fat32_inode_ops;
  364. sb_ptr->root->dir_inode->file_ops = &fat32_file_ops;
  365. sb_ptr->root->dir_inode->file_size = 0;
  366. // 计算文件占用的扇区数, 由于最小存储单位是簇,因此需要按照簇的大小来对齐扇区
  367. sb_ptr->root->dir_inode->blocks = (sb_ptr->root->dir_inode->file_size + fsbi->bytes_per_clus - 1) / fsbi->bytes_per_sec;
  368. sb_ptr->root->dir_inode->attribute = VFS_ATTR_DIR;
  369. sb_ptr->root->dir_inode->sb = sb_ptr; // 反向绑定对应的超级块
  370. // 初始化inode信息
  371. sb_ptr->root->dir_inode->private_inode_info = kmalloc(sizeof(struct fat32_inode_info_t), 0);
  372. memset(sb_ptr->root->dir_inode->private_inode_info, 0, sizeof(struct fat32_inode_info_t));
  373. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)sb_ptr->root->dir_inode->private_inode_info;
  374. finode->first_clus = fbs->BPB_RootClus;
  375. finode->dEntry_location_clus = 0;
  376. finode->dEntry_location_clus_offset = 0;
  377. finode->create_time = 0;
  378. finode->create_date = 0;
  379. finode->write_date = 0;
  380. finode->write_time;
  381. return sb_ptr;
  382. }
  383. /**
  384. * @brief todo: 写入superblock
  385. *
  386. * @param sb
  387. */
  388. void fat32_write_superblock(struct vfs_superblock_t *sb)
  389. {
  390. }
  391. /**
  392. * @brief 释放superblock的内存空间
  393. *
  394. * @param sb 要被释放的superblock
  395. */
  396. void fat32_put_superblock(struct vfs_superblock_t *sb)
  397. {
  398. kfree(sb->private_sb_info);
  399. kfree(sb->root->dir_inode->private_inode_info);
  400. kfree(sb->root->dir_inode);
  401. kfree(sb->root);
  402. kfree(sb);
  403. }
  404. /**
  405. * @brief 写入inode到硬盘上
  406. *
  407. * @param inode
  408. */
  409. void fat32_write_inode(struct vfs_index_node_t *inode)
  410. {
  411. fat32_inode_info_t *finode = inode->private_inode_info;
  412. if (finode->dEntry_location_clus == 0)
  413. {
  414. kerror("FAT32 error: Attempt to write the root inode");
  415. return;
  416. }
  417. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)inode->sb->private_sb_info;
  418. // 计算目标inode对应数据区的LBA地址
  419. uint64_t fLBA = fsbi->first_data_sector + (finode->dEntry_location_clus - 2) * fsbi->sec_per_clus;
  420. uint8_t *buf = (uint8_t *)kmalloc(fsbi->bytes_per_clus, 0);
  421. memset(buf, 0, sizeof(fsbi->bytes_per_clus));
  422. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  423. // 计算目标dEntry所在的位置
  424. struct fat32_Directory_t *fdEntry = (struct fat32_Directory_t *)((uint64_t)buf + finode->dEntry_location_clus_offset);
  425. // 写入fat32文件系统的dir_entry
  426. fdEntry->DIR_FileSize = inode->file_size;
  427. fdEntry->DIR_FstClusLO = finode->first_clus & 0xffff;
  428. fdEntry->DIR_FstClusHI = (finode->first_clus >> 16) | (fdEntry->DIR_FstClusHI & 0xf000);
  429. // 将dir entry写回磁盘
  430. ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  431. kfree(buf);
  432. }
  433. struct vfs_super_block_operations_t fat32_sb_ops =
  434. {
  435. .write_superblock = fat32_write_superblock,
  436. .put_superblock = fat32_put_superblock,
  437. .write_inode = fat32_write_inode,
  438. };
  439. // todo: compare
  440. long fat32_compare(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename)
  441. {
  442. }
  443. // todo: hash
  444. long fat32_hash(struct vfs_dir_entry_t *dEntry, char *filename)
  445. {
  446. }
  447. // todo: release
  448. long fat32_release(struct vfs_dir_entry_t *dEntry)
  449. {
  450. }
  451. // todo: iput
  452. long fat32_iput(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode)
  453. {
  454. }
  455. /**
  456. * @brief fat32文件系统对于dEntry的操作
  457. *
  458. */
  459. struct vfs_dir_entry_operations_t fat32_dEntry_ops =
  460. {
  461. .compare = fat32_compare,
  462. .hash = fat32_hash,
  463. .release = fat32_release,
  464. .iput = fat32_iput,
  465. };
  466. // todo: open
  467. long fat32_open(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  468. {
  469. return VFS_SUCCESS;
  470. }
  471. // todo: close
  472. long fat32_close(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  473. {
  474. return VFS_SUCCESS;
  475. }
  476. /**
  477. * @brief 从fat32文件系统读取数据
  478. *
  479. * @param file_ptr 文件描述符
  480. * @param buf 输出缓冲区
  481. * @param count 要读取的字节数
  482. * @param position 文件指针位置
  483. * @return long 执行成功:传输的字节数量 执行失败:错误码(小于0)
  484. */
  485. long fat32_read(struct vfs_file_t *file_ptr, char *buf, uint64_t count, long *position)
  486. {
  487. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)(file_ptr->dEntry->dir_inode->private_inode_info);
  488. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info);
  489. // First cluster num of the file
  490. uint64_t cluster = finode->first_clus;
  491. // kdebug("fsbi->bytes_per_clus=%d", fsbi->bytes_per_clus);
  492. // clus offset in file
  493. uint64_t total_clus_of_file = (*position) / fsbi->bytes_per_clus;
  494. // bytes offset in clus
  495. uint64_t bytes_offset = (*position) % fsbi->bytes_per_clus;
  496. if (!cluster)
  497. return -EFAULT;
  498. // find the actual cluster on disk of the specified position
  499. for (int i = 0; i < total_clus_of_file; ++i)
  500. cluster = fat32_read_FAT_entry(fsbi, cluster);
  501. // 如果需要读取的数据边界大于文件大小
  502. if (*position + count > file_ptr->dEntry->dir_inode->file_size)
  503. count = file_ptr->dEntry->dir_inode->file_size - *position;
  504. // 剩余还需要传输的字节数量
  505. uint64_t bytes_remain = count;
  506. // alloc buffer memory space for ahci transfer
  507. void *tmp_buffer = kmalloc(fsbi->bytes_per_clus, 0);
  508. int64_t retval = 0;
  509. do
  510. {
  511. memset(tmp_buffer, 0, fsbi->bytes_per_clus);
  512. uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  513. // 读取一个簇的数据
  514. int errno = ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  515. if (errno != AHCI_SUCCESS)
  516. {
  517. kerror("FAT32 FS(read) error!");
  518. retval = -EIO;
  519. break;
  520. }
  521. int64_t step_trans_len = 0; // 当前循环传输的字节数
  522. if (bytes_remain > (fsbi->bytes_per_clus - bytes_offset))
  523. step_trans_len = (fsbi->bytes_per_clus - bytes_offset);
  524. else
  525. step_trans_len = bytes_remain;
  526. if (((uint64_t)buf) < USER_MAX_LINEAR_ADDR)
  527. copy_to_user(buf, tmp_buffer + bytes_offset, step_trans_len);
  528. else
  529. memcpy(buf, tmp_buffer, step_trans_len);
  530. bytes_remain -= step_trans_len;
  531. buf += step_trans_len;
  532. bytes_offset -= bytes_offset;
  533. *position += step_trans_len; // 更新文件指针
  534. cluster = fat32_read_FAT_entry(fsbi, cluster);
  535. } while (bytes_remain && (cluster < 0x0ffffff8) && cluster != 0);
  536. kfree(tmp_buffer);
  537. if(!bytes_remain)
  538. retval = count;
  539. return retval;
  540. }
  541. // todo: write
  542. long fat32_write(struct vfs_file_t *file_ptr, char *buf, uint64_t count, long *position)
  543. {
  544. }
  545. // todo: lseek
  546. long fat32_lseek(struct vfs_file_t *file_ptr, long offset, long origin)
  547. {
  548. }
  549. // todo: ioctl
  550. long fat32_ioctl(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr, uint64_t cmd, uint64_t arg)
  551. {
  552. }
  553. /**
  554. * @brief fat32文件系统,关于文件的操作
  555. *
  556. */
  557. struct vfs_file_operations_t fat32_file_ops =
  558. {
  559. .open = fat32_open,
  560. .close = fat32_close,
  561. .read = fat32_read,
  562. .write = fat32_write,
  563. .lseek = fat32_lseek,
  564. .ioctl = fat32_ioctl,
  565. };
  566. // todo: create
  567. long fat32_create(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dentry, int mode)
  568. {
  569. }
  570. // todo: mkdir
  571. int64_t fat32_mkdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry, int mode)
  572. {
  573. }
  574. // todo: rmdir
  575. int64_t fat32_rmdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry)
  576. {
  577. }
  578. // todo: rename
  579. 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)
  580. {
  581. }
  582. // todo: getAttr
  583. int64_t fat32_getAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
  584. {
  585. }
  586. // todo: setAttr
  587. int64_t fat32_setAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
  588. {
  589. }
  590. struct vfs_inode_operations_t fat32_inode_ops =
  591. {
  592. .create = fat32_create,
  593. .mkdir = fat32_mkdir,
  594. .rmdir = fat32_rmdir,
  595. .lookup = fat32_lookup,
  596. .rename = fat32_rename,
  597. .getAttr = fat32_getAttr,
  598. .setAttr = fat32_setAttr,
  599. };
  600. struct vfs_filesystem_type_t fat32_fs_type =
  601. {
  602. .name = "FAT32",
  603. .fs_flags = 0,
  604. .read_superblock = fat32_read_superblock,
  605. .next = NULL,
  606. };
  607. void fat32_init()
  608. {
  609. kinfo("Initializing FAT32...");
  610. // 在VFS中注册fat32文件系统
  611. vfs_register_filesystem(&fat32_fs_type);
  612. // 挂载根文件系统
  613. vfs_root_sb = fat32_register_partition(0, 0, 0);
  614. kinfo("FAT32 initialized.");
  615. }