fat32.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. #include <common/stdio.h>
  9. struct vfs_super_block_operations_t fat32_sb_ops;
  10. struct vfs_dir_entry_operations_t fat32_dEntry_ops;
  11. struct vfs_file_operations_t fat32_file_ops;
  12. struct vfs_inode_operations_t fat32_inode_ops;
  13. /**
  14. * @brief 注册指定磁盘上的指定分区的fat32文件系统
  15. *
  16. * @param ahci_ctrl_num ahci控制器编号
  17. * @param ahci_port_num ahci控制器端口编号
  18. * @param part_num 磁盘分区编号
  19. *
  20. * @return struct vfs_super_block_t * 文件系统的超级块
  21. */
  22. struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t ahci_port_num, uint8_t part_num)
  23. {
  24. struct MBR_disk_partition_table_t *DPT = MBR_read_partition_table(ahci_ctrl_num, ahci_port_num);
  25. // for(i = 0 ;i < 512 ; i++)
  26. // color_printk(PURPLE,WHITE,"%02x",buf[i]);
  27. printk_color(ORANGE, BLACK, "DPTE[0] start_LBA:%#018lx\ttype:%#018lx\n", DPT->DPTE[part_num].starting_LBA, DPT->DPTE[part_num].type);
  28. uint8_t buf[512] = {0};
  29. // 读取文件系统的boot扇区
  30. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, DPT->DPTE[part_num].starting_LBA, 1, (uint64_t)&buf, ahci_ctrl_num, ahci_port_num);
  31. // 挂载文件系统到vfs
  32. return vfs_mount_fs("FAT32", (void *)(&DPT->DPTE[part_num]), VFS_DPT_MBR, buf, ahci_ctrl_num, ahci_port_num, part_num);
  33. }
  34. /**
  35. * @brief 读取指定簇的FAT表项
  36. *
  37. * @param fsbi fat32超级块私有信息结构体
  38. * @param cluster 指定簇
  39. * @return uint32_t 下一个簇的簇号
  40. */
  41. uint32_t fat32_read_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster)
  42. {
  43. // 计算每个扇区内含有的FAT表项数
  44. // FAT每项4bytes
  45. uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂
  46. uint32_t buf[256];
  47. memset(buf, 0, fsbi->bytes_per_sec);
  48. // 读取一个sector的数据,
  49. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
  50. (uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  51. // 返回下一个fat表项的值(也就是下一个cluster)
  52. return buf[cluster & (fat_ent_per_sec - 1)] & 0x0fffffff;
  53. }
  54. /**
  55. * @brief 写入指定簇的FAT表项
  56. *
  57. * @param fsbi fat32超级块私有信息结构体
  58. * @param cluster 指定簇
  59. * @param value 要写入该fat表项的值
  60. * @return uint32_t errcode
  61. */
  62. uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t value)
  63. {
  64. // 计算每个扇区内含有的FAT表项数
  65. // FAT每项4bytes
  66. uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂
  67. uint32_t buf[256];
  68. memset(buf, 0, fsbi->bytes_per_sec);
  69. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
  70. (uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  71. buf[cluster & (fat_ent_per_sec - 1)] = (buf[cluster & (fat_ent_per_sec - 1)] & 0xf0000000) | (value & 0x0fffffff);
  72. // 向FAT1和FAT2写入数据
  73. ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
  74. (uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  75. ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fsbi->FAT2_base_sector + (cluster / fat_ent_per_sec), 1,
  76. (uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  77. return 0;
  78. }
  79. /**
  80. * @brief 在父目录中寻找指定的目录项
  81. *
  82. * @param parent_inode 父目录项的inode
  83. * @param dest_inode 搜索目标目录项的inode
  84. * @return struct vfs_dir_entry_t* 目标目录项
  85. */
  86. struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dentry)
  87. {
  88. int errcode = 0;
  89. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
  90. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
  91. uint8_t *buf = kmalloc(fsbi->bytes_per_clus, 0);
  92. memset(buf, 0, fsbi->bytes_per_clus);
  93. // 计算父目录项的起始簇号
  94. uint32_t cluster = finode->first_clus;
  95. struct fat32_Directory_t *tmp_dEntry = NULL;
  96. while (true)
  97. {
  98. // 计算父目录项的起始LBA扇区号
  99. uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  100. // kdebug("fat32_part_info[part_id].bootsector.BPB_SecPerClus=%d",fat32_part_info[part_id].bootsector.BPB_SecPerClus);
  101. // kdebug("sector=%d",sector);
  102. // 读取父目录项的起始簇数据
  103. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  104. // 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);
  105. tmp_dEntry = (struct fat32_Directory_t *)buf;
  106. // 查找短目录项
  107. for (int i = 0; i < fsbi->bytes_per_clus; i += 32, ++tmp_dEntry)
  108. {
  109. // 跳过长目录项
  110. if (tmp_dEntry->DIR_Attr == ATTR_LONG_NAME)
  111. continue;
  112. // 跳过无效页表项、空闲页表项
  113. if (tmp_dEntry->DIR_Name[0] == 0xe5 || tmp_dEntry->DIR_Name[0] == 0x00 || tmp_dEntry->DIR_Name[0] == 0x05)
  114. continue;
  115. // 找到长目录项,位于短目录项之前
  116. struct fat32_LongDirectory_t *tmp_ldEntry = (struct fat32_LongDirectory_t *)tmp_dEntry - 1;
  117. int js = 0;
  118. // 遍历每个长目录项
  119. while (tmp_ldEntry->LDIR_Attr == ATTR_LONG_NAME && tmp_ldEntry->LDIR_Ord != 0xe5)
  120. {
  121. // 比较name1
  122. for (int x = 0; x < 5; ++x)
  123. {
  124. if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name1[x] == 0xffff)
  125. continue;
  126. else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name1[x] != (uint16_t)(dest_dentry->name[js++])) // 文件名不匹配,检索下一个短目录项
  127. goto continue_cmp_fail;
  128. }
  129. // 比较name2
  130. for (int x = 0; x < 6; ++x)
  131. {
  132. if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name2[x] == 0xffff)
  133. continue;
  134. else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name2[x] != (uint16_t)(dest_dentry->name[js++])) // 文件名不匹配,检索下一个短目录项
  135. goto continue_cmp_fail;
  136. }
  137. // 比较name3
  138. for (int x = 0; x < 2; ++x)
  139. {
  140. if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name3[x] == 0xffff)
  141. continue;
  142. else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name3[x] != (uint16_t)(dest_dentry->name[js++])) // 文件名不匹配,检索下一个短目录项
  143. goto continue_cmp_fail;
  144. }
  145. if (js >= dest_dentry->name_length) // 找到需要的目录项,返回
  146. {
  147. goto find_lookup_success;
  148. }
  149. --tmp_ldEntry; // 检索下一个长目录项
  150. }
  151. // 不存在长目录项,匹配短目录项的基础名
  152. js = 0;
  153. for (int x = 0; x < 8; ++x)
  154. {
  155. switch (tmp_dEntry->DIR_Name[x])
  156. {
  157. case ' ':
  158. if (!(tmp_dEntry->DIR_Attr & ATTR_DIRECTORY)) // 不是文件夹(是文件)
  159. {
  160. if (dest_dentry->name[js] == '.')
  161. continue;
  162. else if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  163. {
  164. ++js;
  165. break;
  166. }
  167. else
  168. goto continue_cmp_fail;
  169. }
  170. else // 是文件夹
  171. {
  172. if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js]) // 当前位正确匹配
  173. {
  174. ++js;
  175. break; // 进行下一位的匹配
  176. }
  177. else if (js == dest_dentry->name_length)
  178. continue;
  179. else
  180. goto continue_cmp_fail;
  181. }
  182. break;
  183. // 当前位是字母
  184. case 'A' ... 'Z':
  185. case 'a' ... 'z':
  186. if (tmp_dEntry->DIR_NTRes & LOWERCASE_BASE) // 为兼容windows系统,检测DIR_NTRes字段
  187. {
  188. if (js < dest_dentry->name_length && (tmp_dEntry->DIR_Name[x] + 32 == dest_dentry->name[js]))
  189. {
  190. ++js;
  191. break;
  192. }
  193. else
  194. goto continue_cmp_fail;
  195. }
  196. else
  197. {
  198. if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  199. {
  200. ++js;
  201. break;
  202. }
  203. else
  204. goto continue_cmp_fail;
  205. }
  206. break;
  207. case '0' ... '9':
  208. if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  209. {
  210. ++js;
  211. break;
  212. }
  213. else
  214. goto continue_cmp_fail;
  215. break;
  216. default:
  217. ++js;
  218. break;
  219. }
  220. }
  221. // 若短目录项为文件,则匹配扩展名
  222. if (!(tmp_dEntry->DIR_Attr & ATTR_DIRECTORY))
  223. {
  224. ++js;
  225. for (int x = 8; x < 11; ++x)
  226. {
  227. switch (tmp_dEntry->DIR_Name[x])
  228. {
  229. // 当前位是字母
  230. case 'A' ... 'Z':
  231. case 'a' ... 'z':
  232. if (tmp_dEntry->DIR_NTRes & LOWERCASE_EXT) // 为兼容windows系统,检测DIR_NTRes字段
  233. {
  234. if ((tmp_dEntry->DIR_Name[x] + 32 == dest_dentry->name[js]))
  235. {
  236. ++js;
  237. break;
  238. }
  239. else
  240. goto continue_cmp_fail;
  241. }
  242. else
  243. {
  244. if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  245. {
  246. ++js;
  247. break;
  248. }
  249. else
  250. goto continue_cmp_fail;
  251. }
  252. break;
  253. case '0' ... '9':
  254. case ' ':
  255. if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
  256. {
  257. ++js;
  258. break;
  259. }
  260. else
  261. goto continue_cmp_fail;
  262. break;
  263. default:
  264. goto continue_cmp_fail;
  265. break;
  266. }
  267. }
  268. }
  269. goto find_lookup_success;
  270. continue_cmp_fail:;
  271. }
  272. // 当前簇没有发现目标文件名,寻找下一个簇
  273. cluster = fat32_read_FAT_entry(fsbi, cluster);
  274. if (cluster >= 0x0ffffff7) // 寻找完父目录的所有簇,都没有找到目标文件名
  275. {
  276. kfree(buf);
  277. return NULL;
  278. }
  279. }
  280. find_lookup_success:; // 找到目标dentry
  281. struct vfs_index_node_t *p = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
  282. memset(p, 0, sizeof(struct vfs_index_node_t));
  283. p->file_size = tmp_dEntry->DIR_FileSize;
  284. // 计算文件占用的扇区数, 由于最小存储单位是簇,因此需要按照簇的大小来对齐扇区
  285. p->blocks = (p->file_size + fsbi->bytes_per_clus - 1) / fsbi->bytes_per_sec;
  286. p->attribute = (tmp_dEntry->DIR_Attr & ATTR_DIRECTORY) ? VFS_ATTR_DIR : VFS_ATTR_FILE;
  287. p->sb = parent_inode->sb;
  288. p->file_ops = &fat32_file_ops;
  289. p->inode_ops = &fat32_inode_ops;
  290. // 为inode的与文件系统相关的信息结构体分配空间
  291. p->private_inode_info = (void *)kmalloc(sizeof(fat32_inode_info_t), 0);
  292. memset(p->private_inode_info, 0, sizeof(fat32_inode_info_t));
  293. finode = (fat32_inode_info_t *)p->private_inode_info;
  294. finode->first_clus = ((tmp_dEntry->DIR_FstClusHI << 16) | tmp_dEntry->DIR_FstClusLO) & 0x0fffffff;
  295. finode->dEntry_location_clus = cluster;
  296. finode->dEntry_location_clus_offset = tmp_dEntry - (struct fat32_Directory_t *)buf; //计算dentry的偏移量
  297. kdebug("finode->dEntry_location_clus=%#018lx", finode->dEntry_location_clus);
  298. kdebug("finode->dEntry_location_clus_offset=%#018lx", finode->dEntry_location_clus_offset);
  299. finode->create_date = tmp_dEntry->DIR_CrtDate;
  300. finode->create_time = tmp_dEntry->DIR_CrtTime;
  301. finode->write_date = tmp_dEntry->DIR_WrtDate;
  302. finode->write_time = tmp_dEntry->DIR_WrtTime;
  303. // 暂时使用fat32的高4bit来标志设备文件
  304. // todo: 引入devfs后删除这段代码
  305. if ((tmp_dEntry->DIR_FstClusHI >> 12) && (p->attribute & VFS_ATTR_FILE))
  306. p->attribute |= VFS_ATTR_DEVICE;
  307. dest_dentry->dir_inode = p;
  308. kfree(buf);
  309. return dest_dentry;
  310. }
  311. /**
  312. * @brief 创建fat32文件系统的超级块
  313. *
  314. * @param DPTE 磁盘分区表entry
  315. * @param DPT_type 磁盘分区表类型
  316. * @param buf fat32文件系统的引导扇区
  317. * @return struct vfs_superblock_t* 创建好的超级块
  318. */
  319. 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)
  320. {
  321. if (DPT_type != VFS_DPT_MBR) // 暂时只支持MBR分区表
  322. {
  323. kerror("fat32_read_superblock(): Unsupported DPT!");
  324. return NULL;
  325. }
  326. // 分配超级块的空间
  327. struct vfs_superblock_t *sb_ptr = (struct vfs_superblock_t *)kmalloc(sizeof(struct vfs_superblock_t), 0);
  328. memset(sb_ptr, 0, sizeof(struct vfs_superblock_t));
  329. sb_ptr->sb_ops = &fat32_sb_ops;
  330. sb_ptr->private_sb_info = kmalloc(sizeof(fat32_sb_info_t), 0);
  331. memset(sb_ptr->private_sb_info, 0, sizeof(fat32_sb_info_t));
  332. struct fat32_BootSector_t *fbs = (struct fat32_BootSector_t *)buf;
  333. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(sb_ptr->private_sb_info);
  334. // MBR分区表entry
  335. struct MBR_disk_partition_table_entry_t *MBR_DPTE = (struct MBR_disk_partition_table_entry_t *)DPTE;
  336. fsbi->ahci_ctrl_num = ahci_ctrl_num;
  337. fsbi->ahci_port_num = ahci_port_num;
  338. fsbi->part_num = part_num;
  339. fsbi->starting_sector = MBR_DPTE->starting_LBA;
  340. fsbi->sector_count = MBR_DPTE->total_sectors;
  341. fsbi->sec_per_clus = fbs->BPB_SecPerClus;
  342. fsbi->bytes_per_clus = fbs->BPB_SecPerClus * fbs->BPB_BytesPerSec;
  343. fsbi->bytes_per_sec = fbs->BPB_BytesPerSec;
  344. fsbi->first_data_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt + fbs->BPB_FATSz32 * fbs->BPB_NumFATs;
  345. fsbi->FAT1_base_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt;
  346. fsbi->FAT2_base_sector = fsbi->FAT1_base_sector + fbs->BPB_FATSz32;
  347. fsbi->sec_per_FAT = fbs->BPB_FATSz32;
  348. fsbi->NumFATs = fbs->BPB_NumFATs;
  349. fsbi->fsinfo_sector_addr_infat = fbs->BPB_FSInfo;
  350. fsbi->bootsector_bak_sector_addr_infat = fbs->BPB_BkBootSec;
  351. 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);
  352. // fsinfo扇区的信息
  353. memset(&fsbi->fsinfo, 0, sizeof(struct fat32_FSInfo_t));
  354. 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);
  355. 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);
  356. // 初始化超级块的dir entry
  357. sb_ptr->root = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
  358. memset(sb_ptr->root, 0, sizeof(struct vfs_dir_entry_t));
  359. list_init(&sb_ptr->root->child_node_list);
  360. list_init(&sb_ptr->root->subdirs_list);
  361. sb_ptr->root->parent = sb_ptr->root;
  362. sb_ptr->root->dir_ops = &fat32_dEntry_ops;
  363. // 分配2个字节的name
  364. sb_ptr->root->name = (char *)(kmalloc(2, 0));
  365. sb_ptr->root->name[0] = '/';
  366. sb_ptr->root->name_length = 1;
  367. // 为root目录项分配index node
  368. sb_ptr->root->dir_inode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
  369. memset(sb_ptr->root->dir_inode, 0, sizeof(struct vfs_index_node_t));
  370. sb_ptr->root->dir_inode->inode_ops = &fat32_inode_ops;
  371. sb_ptr->root->dir_inode->file_ops = &fat32_file_ops;
  372. sb_ptr->root->dir_inode->file_size = 0;
  373. // 计算文件占用的扇区数, 由于最小存储单位是簇,因此需要按照簇的大小来对齐扇区
  374. sb_ptr->root->dir_inode->blocks = (sb_ptr->root->dir_inode->file_size + fsbi->bytes_per_clus - 1) / fsbi->bytes_per_sec;
  375. sb_ptr->root->dir_inode->attribute = VFS_ATTR_DIR;
  376. sb_ptr->root->dir_inode->sb = sb_ptr; // 反向绑定对应的超级块
  377. // 初始化inode信息
  378. sb_ptr->root->dir_inode->private_inode_info = kmalloc(sizeof(struct fat32_inode_info_t), 0);
  379. memset(sb_ptr->root->dir_inode->private_inode_info, 0, sizeof(struct fat32_inode_info_t));
  380. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)sb_ptr->root->dir_inode->private_inode_info;
  381. finode->first_clus = fbs->BPB_RootClus;
  382. finode->dEntry_location_clus = 0;
  383. finode->dEntry_location_clus_offset = 0;
  384. finode->create_time = 0;
  385. finode->create_date = 0;
  386. finode->write_date = 0;
  387. finode->write_time;
  388. return sb_ptr;
  389. }
  390. /**
  391. * @brief todo: 写入superblock
  392. *
  393. * @param sb
  394. */
  395. void fat32_write_superblock(struct vfs_superblock_t *sb)
  396. {
  397. }
  398. /**
  399. * @brief 释放superblock的内存空间
  400. *
  401. * @param sb 要被释放的superblock
  402. */
  403. void fat32_put_superblock(struct vfs_superblock_t *sb)
  404. {
  405. kfree(sb->private_sb_info);
  406. kfree(sb->root->dir_inode->private_inode_info);
  407. kfree(sb->root->dir_inode);
  408. kfree(sb->root);
  409. kfree(sb);
  410. }
  411. /**
  412. * @brief 写入inode到硬盘上
  413. *
  414. * @param inode
  415. */
  416. void fat32_write_inode(struct vfs_index_node_t *inode)
  417. {
  418. fat32_inode_info_t *finode = inode->private_inode_info;
  419. if (finode->dEntry_location_clus == 0)
  420. {
  421. kerror("FAT32 error: Attempt to write the root inode");
  422. return;
  423. }
  424. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)inode->sb->private_sb_info;
  425. // 计算目标inode对应数据区的LBA地址
  426. uint64_t fLBA = fsbi->first_data_sector + (finode->dEntry_location_clus - 2) * fsbi->sec_per_clus;
  427. struct fat32_Directory_t *buf = (struct fat32_Directory_t *)kmalloc(fsbi->bytes_per_clus, 0);
  428. memset(buf, 0, fsbi->bytes_per_clus);
  429. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  430. // 计算目标dEntry所在的位置
  431. struct fat32_Directory_t *fdEntry = buf + finode->dEntry_location_clus_offset;
  432. // 写入fat32文件系统的dir_entry
  433. fdEntry->DIR_FileSize = inode->file_size;
  434. fdEntry->DIR_FstClusLO = finode->first_clus & 0xffff;
  435. fdEntry->DIR_FstClusHI = (finode->first_clus >> 16) | (fdEntry->DIR_FstClusHI & 0xf000);
  436. // 将dir entry写回磁盘
  437. ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  438. kfree(buf);
  439. }
  440. struct vfs_super_block_operations_t fat32_sb_ops =
  441. {
  442. .write_superblock = fat32_write_superblock,
  443. .put_superblock = fat32_put_superblock,
  444. .write_inode = fat32_write_inode,
  445. };
  446. // todo: compare
  447. long fat32_compare(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename)
  448. {
  449. }
  450. // todo: hash
  451. long fat32_hash(struct vfs_dir_entry_t *dEntry, char *filename)
  452. {
  453. }
  454. // todo: release
  455. long fat32_release(struct vfs_dir_entry_t *dEntry)
  456. {
  457. }
  458. // todo: iput
  459. long fat32_iput(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode)
  460. {
  461. }
  462. /**
  463. * @brief fat32文件系统对于dEntry的操作
  464. *
  465. */
  466. struct vfs_dir_entry_operations_t fat32_dEntry_ops =
  467. {
  468. .compare = fat32_compare,
  469. .hash = fat32_hash,
  470. .release = fat32_release,
  471. .iput = fat32_iput,
  472. };
  473. // todo: open
  474. long fat32_open(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  475. {
  476. return VFS_SUCCESS;
  477. }
  478. // todo: close
  479. long fat32_close(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  480. {
  481. return VFS_SUCCESS;
  482. }
  483. /**
  484. * @brief 从fat32文件系统读取数据
  485. *
  486. * @param file_ptr 文件描述符
  487. * @param buf 输出缓冲区
  488. * @param count 要读取的字节数
  489. * @param position 文件指针位置
  490. * @return long 执行成功:传输的字节数量 执行失败:错误码(小于0)
  491. */
  492. long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position)
  493. {
  494. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)(file_ptr->dEntry->dir_inode->private_inode_info);
  495. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info);
  496. // First cluster num of the file
  497. uint64_t cluster = finode->first_clus;
  498. // kdebug("fsbi->bytes_per_clus=%d fsbi->sec_per_clus=%d finode->first_clus=%d cluster=%d", fsbi->bytes_per_clus, fsbi->sec_per_clus, finode->first_clus, cluster);
  499. // kdebug("fsbi->bytes_per_clus=%d", fsbi->bytes_per_clus);
  500. // clus offset in file
  501. uint64_t clus_offset_in_file = (*position) / fsbi->bytes_per_clus;
  502. // bytes offset in clus
  503. uint64_t bytes_offset = (*position) % fsbi->bytes_per_clus;
  504. if (!cluster)
  505. return -EFAULT;
  506. // find the actual cluster on disk of the specified position
  507. for (int i = 0; i < clus_offset_in_file; ++i)
  508. cluster = fat32_read_FAT_entry(fsbi, cluster);
  509. // 如果需要读取的数据边界大于文件大小
  510. if (*position + count > file_ptr->dEntry->dir_inode->file_size)
  511. count = file_ptr->dEntry->dir_inode->file_size - *position;
  512. // 剩余还需要传输的字节数量
  513. int64_t bytes_remain = count;
  514. // alloc buffer memory space for ahci transfer
  515. void *tmp_buffer = kmalloc(fsbi->bytes_per_clus, 0);
  516. int64_t retval = 0;
  517. do
  518. {
  519. memset(tmp_buffer, 0, fsbi->bytes_per_clus);
  520. uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
  521. // 读取一个簇的数据
  522. 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);
  523. if (errno != AHCI_SUCCESS)
  524. {
  525. kerror("FAT32 FS(read) error!");
  526. retval = -EIO;
  527. break;
  528. }
  529. int64_t step_trans_len = 0; // 当前循环传输的字节数
  530. if (bytes_remain > (fsbi->bytes_per_clus - bytes_offset))
  531. step_trans_len = (fsbi->bytes_per_clus - bytes_offset);
  532. else
  533. step_trans_len = bytes_remain;
  534. if (((uint64_t)buf) < USER_MAX_LINEAR_ADDR)
  535. copy_to_user(buf, tmp_buffer + bytes_offset, step_trans_len);
  536. else
  537. memcpy(buf, tmp_buffer + bytes_offset, step_trans_len);
  538. bytes_remain -= step_trans_len;
  539. buf += step_trans_len;
  540. bytes_offset -= bytes_offset;
  541. *position += step_trans_len; // 更新文件指针
  542. cluster = fat32_read_FAT_entry(fsbi, cluster);
  543. } while (bytes_remain && (cluster < 0x0ffffff8) && cluster != 0);
  544. kfree(tmp_buffer);
  545. if (!bytes_remain)
  546. retval = count;
  547. return retval;
  548. }
  549. /**
  550. * @brief 在磁盘中寻找一个空闲的簇
  551. *
  552. * @param fsbi fat32超级块信息结构体
  553. * @return uint64_t 空闲簇号(找不到则返回0)
  554. */
  555. uint64_t fat32_find_available_cluster(fat32_sb_info_t *fsbi)
  556. {
  557. uint64_t sec_per_fat = fsbi->sec_per_FAT;
  558. // 申请1扇区的缓冲区
  559. uint32_t *buf = (uint32_t *)kmalloc(fsbi->bytes_per_sec, 0);
  560. int ent_per_sec = (fsbi->bytes_per_sec >> 2);
  561. for (int i = 0; i < sec_per_fat; ++i)
  562. {
  563. memset(buf, 0, fsbi->bytes_per_sec);
  564. ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + i, 1, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  565. // 依次检查簇是否空闲
  566. for (int j = 0; j < ent_per_sec; ++j)
  567. {
  568. // 找到空闲簇
  569. if ((buf[j] & 0x0fffffff) == 0)
  570. return i * ent_per_sec + j;
  571. }
  572. }
  573. return 0;
  574. }
  575. /**
  576. * @brief 向fat32文件系统写入数据
  577. *
  578. * @param file_ptr 文件描述符
  579. * @param buf 输入写入的字节数
  580. * @param position 文件指针位置
  581. * @return long 执行成功:传输的字节数量 执行失败:错误码(小于0)
  582. */
  583. long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position)
  584. {
  585. struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)file_ptr->dEntry->dir_inode->private_inode_info;
  586. fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info);
  587. // First cluster num of the file
  588. uint64_t cluster = finode->first_clus;
  589. int64_t flags = 0;
  590. // kdebug("fsbi->bytes_per_clus=%d fsbi->sec_per_clus=%d finode->first_clus=%d *position=%d", fsbi->bytes_per_clus, fsbi->sec_per_clus, finode->first_clus, *position);
  591. // kdebug("buf=%s", buf);
  592. // clus offset in file
  593. uint64_t clus_offset_in_file = (*position) / fsbi->bytes_per_clus;
  594. // bytes offset in clus
  595. uint64_t bytes_offset = (*position) % fsbi->bytes_per_clus;
  596. if (!cluster) // 起始簇号为0,说明是空文件
  597. {
  598. // 找一个可用的簇
  599. cluster = fat32_find_available_cluster(fsbi);
  600. flags = 1;
  601. }
  602. else
  603. {
  604. // 跳转到position所在的簇
  605. for (uint64_t i = 0; i < clus_offset_in_file; ++i)
  606. cluster = fat32_read_FAT_entry(fsbi, cluster);
  607. }
  608. // kdebug("cluster(start)=%d", cluster);
  609. // 没有可用的磁盘空间
  610. if (!cluster)
  611. return -ENOSPC;
  612. if (flags) // 空文件
  613. {
  614. // kdebug("empty file");
  615. finode->first_clus = cluster;
  616. // 写入目录项
  617. file_ptr->dEntry->dir_inode->sb->sb_ops->write_inode(file_ptr->dEntry->dir_inode);
  618. fat32_write_FAT_entry(fsbi, cluster, 0x0ffffff8); // 写入fat表项
  619. }
  620. int64_t bytes_remain = count;
  621. if (count < 0) // 要写入的字节数小于0
  622. return -EINVAL;
  623. uint64_t sector;
  624. int64_t retval = 0;
  625. void *tmp_buffer = kmalloc(fsbi->bytes_per_clus, 0);
  626. do
  627. {
  628. memset(tmp_buffer, 0, fsbi->bytes_per_clus);
  629. sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus; // 计算对应的扇区
  630. if (!flags) // 当前簇已分配
  631. {
  632. // kdebug("read existed sec=%ld", sector);
  633. // 读取一个簇的数据
  634. 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);
  635. if (errno != AHCI_SUCCESS)
  636. {
  637. // kerror("FAT32 FS(write) read disk error!");
  638. retval = -EIO;
  639. break;
  640. }
  641. }
  642. int64_t step_trans_len = 0; // 当前循环传输的字节数
  643. if (bytes_remain > (fsbi->bytes_per_clus - bytes_offset))
  644. step_trans_len = (fsbi->bytes_per_clus - bytes_offset);
  645. else
  646. step_trans_len = bytes_remain;
  647. // kdebug("step_trans_len=%d, bytes_offset=%d", step_trans_len, bytes_offset);
  648. if (((uint64_t)buf) < USER_MAX_LINEAR_ADDR)
  649. copy_from_user(tmp_buffer + bytes_offset, buf, step_trans_len);
  650. else
  651. memcpy(tmp_buffer + bytes_offset, buf, step_trans_len);
  652. // 写入数据到对应的簇
  653. int errno = ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
  654. if (errno != AHCI_SUCCESS)
  655. {
  656. kerror("FAT32 FS(write) write disk error!");
  657. retval = -EIO;
  658. break;
  659. }
  660. bytes_remain -= step_trans_len;
  661. buf += step_trans_len;
  662. bytes_offset -= bytes_offset;
  663. *position += step_trans_len; // 更新文件指针
  664. // kdebug("step_trans_len=%d", step_trans_len);
  665. int next_clus = 0;
  666. if (bytes_remain)
  667. next_clus = fat32_read_FAT_entry(fsbi, cluster);
  668. else
  669. break;
  670. if (next_clus >= 0x0ffffff8) // 已经到达了最后一个簇,需要分配新簇
  671. {
  672. next_clus = fat32_find_available_cluster(fsbi);
  673. if (!next_clus) // 没有空闲簇
  674. {
  675. kfree(tmp_buffer);
  676. return -ENOSPC;
  677. }
  678. // 将簇加入到文件末尾
  679. fat32_write_FAT_entry(fsbi, cluster, next_clus);
  680. fat32_write_FAT_entry(fsbi, next_clus, 0x0ffffff8);
  681. cluster = next_clus; // 切换当前簇
  682. flags = 1; // 标记当前簇是新分配的簇
  683. }
  684. } while (bytes_remain);
  685. // 文件大小有增长
  686. if (*position > (file_ptr->dEntry->dir_inode->file_size))
  687. {
  688. file_ptr->dEntry->dir_inode->file_size = *position;
  689. file_ptr->dEntry->dir_inode->sb->sb_ops->write_inode(file_ptr->dEntry->dir_inode);
  690. kdebug("new file size=%ld", *position);
  691. }
  692. kfree(tmp_buffer);
  693. if (!bytes_remain)
  694. retval = count;
  695. // kdebug("retval=%lld", retval);
  696. return retval;
  697. }
  698. /**
  699. * @brief 调整文件的当前访问位置
  700. *
  701. * @param file_ptr vfs文件指针
  702. * @param offset 调整的偏移量
  703. * @param whence 调整方法
  704. * @return long 更新后的指针位置
  705. */
  706. long fat32_lseek(struct vfs_file_t *file_ptr, long offset, long whence)
  707. {
  708. struct vfs_index_node_t *inode = file_ptr->dEntry->dir_inode;
  709. long pos = 0;
  710. switch (whence)
  711. {
  712. case SEEK_SET: // 相对于文件头
  713. pos = offset;
  714. break;
  715. case SEEK_CUR: // 相对于当前位置
  716. pos = file_ptr->position + offset;
  717. break;
  718. case SEEK_END: // 相对于文件末尾
  719. pos = file_ptr->dEntry->dir_inode->file_size + offset;
  720. break;
  721. default:
  722. return -EINVAL;
  723. break;
  724. }
  725. if (pos < 0 || pos > file_ptr->dEntry->dir_inode->file_size)
  726. return -EOVERFLOW;
  727. file_ptr->position = pos;
  728. // kdebug("fat32 lseek -> position=%d", file_ptr->position);
  729. return pos;
  730. }
  731. // todo: ioctl
  732. long fat32_ioctl(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr, uint64_t cmd, uint64_t arg)
  733. {
  734. }
  735. /**
  736. * @brief fat32文件系统,关于文件的操作
  737. *
  738. */
  739. struct vfs_file_operations_t fat32_file_ops =
  740. {
  741. .open = fat32_open,
  742. .close = fat32_close,
  743. .read = fat32_read,
  744. .write = fat32_write,
  745. .lseek = fat32_lseek,
  746. .ioctl = fat32_ioctl,
  747. };
  748. // todo: create
  749. long fat32_create(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dentry, int mode)
  750. {
  751. }
  752. // todo: mkdir
  753. int64_t fat32_mkdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry, int mode)
  754. {
  755. }
  756. // todo: rmdir
  757. int64_t fat32_rmdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry)
  758. {
  759. }
  760. // todo: rename
  761. 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)
  762. {
  763. }
  764. // todo: getAttr
  765. int64_t fat32_getAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
  766. {
  767. }
  768. // todo: setAttr
  769. int64_t fat32_setAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
  770. {
  771. }
  772. struct vfs_inode_operations_t fat32_inode_ops =
  773. {
  774. .create = fat32_create,
  775. .mkdir = fat32_mkdir,
  776. .rmdir = fat32_rmdir,
  777. .lookup = fat32_lookup,
  778. .rename = fat32_rename,
  779. .getAttr = fat32_getAttr,
  780. .setAttr = fat32_setAttr,
  781. };
  782. struct vfs_filesystem_type_t fat32_fs_type =
  783. {
  784. .name = "FAT32",
  785. .fs_flags = 0,
  786. .read_superblock = fat32_read_superblock,
  787. .next = NULL,
  788. };
  789. void fat32_init()
  790. {
  791. kinfo("Initializing FAT32...");
  792. // 在VFS中注册fat32文件系统
  793. vfs_register_filesystem(&fat32_fs_type);
  794. // 挂载根文件系统
  795. vfs_root_sb = fat32_register_partition(0, 0, 0);
  796. kinfo("FAT32 initialized.");
  797. }