fat32.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 fat32_partition_info_t fat32_part_info[FAT32_MAX_PARTITION_NUM] = {0};
  8. static int total_fat32_parts = 0;
  9. static int max_fat32_parts_id = -1;
  10. static uint64_t fat32_part_info_bmp[FAT32_MAX_PARTITION_NUM / 64 + 1] = {0};
  11. static spinlock_t fat32_part_reg_lock;
  12. /**
  13. * @brief 注册指定磁盘上的指定分区的fat32文件系统
  14. *
  15. * @param ahci_ctrl_num ahci控制器编号
  16. * @param ahci_port_num ahci控制器端口编号
  17. * @param part_num 磁盘分区编号
  18. *
  19. * @return int 全局fat32分区id
  20. */
  21. int fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t ahci_port_num, uint8_t part_num)
  22. {
  23. for (int i = 0; i <= max_fat32_parts_id; ++i)
  24. {
  25. if (fat32_part_info_bmp[i / 64] & (1 << (i % 64)))
  26. {
  27. // 已经注册
  28. if (ahci_ctrl_num == fat32_part_info[i].ahci_ctrl_num && ahci_port_num == fat32_part_info[i].ahci_port_num && part_num == fat32_part_info[i].part_num)
  29. return i;
  30. }
  31. }
  32. // 注册分区
  33. spin_lock(&fat32_part_reg_lock);
  34. int current_part_id;
  35. for (int i = 0; i <= max_fat32_parts_id; ++i)
  36. {
  37. if ((fat32_part_info_bmp[i / 64] & (1 << (i % 64))) == 0)
  38. {
  39. current_part_id = i;
  40. break;
  41. }
  42. }
  43. ++max_fat32_parts_id;
  44. current_part_id = max_fat32_parts_id;
  45. fat32_part_info_bmp[current_part_id / 64] |= (1 << (current_part_id % 64));
  46. spin_unlock(&fat32_part_reg_lock);
  47. fat32_part_info[current_part_id].ahci_ctrl_num = ahci_ctrl_num;
  48. fat32_part_info[current_part_id].ahci_port_num = ahci_port_num;
  49. fat32_part_info[current_part_id].part_num = part_num;
  50. fat32_part_info[current_part_id].partition_id = current_part_id;
  51. struct MBR_disk_partition_table_t *DPT = MBR_read_partition_table(ahci_ctrl_num, ahci_port_num);
  52. // for(i = 0 ;i < 512 ; i++)
  53. // color_printk(PURPLE,WHITE,"%02x",buf[i]);
  54. printk_color(ORANGE, BLACK, "DPTE[0] start_LBA:%#018lx\ttype:%#018lx\n", DPT->DPTE[part_num].starting_LBA, DPT->DPTE[part_num].type);
  55. memset(buf, 0, 512);
  56. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, DPT->DPTE[part_num].starting_LBA, 1, (uint64_t)&buf, ahci_ctrl_num, ahci_port_num);
  57. fat32_part_info[current_part_id].bootsector = *(struct fat32_BootSector_t *)buf;
  58. // 计算数据区起始扇区号
  59. fat32_part_info[current_part_id].first_data_sector = DPT->DPTE[part_num].starting_LBA + fat32_part_info[current_part_id].bootsector.BPB_RsvdSecCnt +
  60. fat32_part_info[current_part_id].bootsector.BPB_FATSz32 * fat32_part_info[current_part_id].bootsector.BPB_NumFATs;
  61. // 计算FAT1的起始扇区号
  62. fat32_part_info[current_part_id].FAT1_base_sector = DPT->DPTE[part_num].starting_LBA + fat32_part_info[current_part_id].bootsector.BPB_RsvdSecCnt;
  63. // 计算FAT2的起始扇区号
  64. fat32_part_info[current_part_id].FAT2_base_sector = fat32_part_info[current_part_id].FAT1_base_sector + fat32_part_info[current_part_id].bootsector.BPB_FATSz32;
  65. // 计算每个簇的大小
  66. fat32_part_info[current_part_id].bytes_per_clus = fat32_part_info[current_part_id].bootsector.BPB_BytesPerSec * fat32_part_info[current_part_id].bootsector.BPB_SecPerClus;
  67. kdebug("fat32_part_info[current_part_id].FAT1_base_sector=%#018lx", fat32_part_info[current_part_id].FAT1_base_sector);
  68. printk_color(ORANGE, BLACK, "FAT32 Boot Sector\n\tBPB_FSInfo:%#018lx\n\tBPB_BkBootSec:%#018lx\n\tBPB_TotSec32:%#018lx\n", fat32_part_info[current_part_id].bootsector.BPB_FSInfo, fat32_part_info[current_part_id].bootsector.BPB_BkBootSec, fat32_part_info[current_part_id].bootsector.BPB_TotSec32);
  69. memset(buf, 0, 512);
  70. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, DPT->DPTE[part_num].starting_LBA + fat32_part_info[current_part_id].bootsector.BPB_FSInfo, 1, (uint64_t)&buf, ahci_ctrl_num, ahci_port_num);
  71. fat32_part_info[current_part_id].fsinfo = *(struct fat32_FSInfo_t *)buf;
  72. // for(i = 0 ;i < 512 ; i++)
  73. // printk_color(PURPLE,WHITE,"%02x",buf[i]);
  74. printk_color(ORANGE, BLACK, "FAT32 FSInfo\n\tFSI_LeadSig:%#018lx\n\tFSI_StrucSig:%#018lx\n\tFSI_Free_Count:%#018lx\n", fat32_part_info[current_part_id].fsinfo.FSI_LeadSig, fat32_part_info[current_part_id].fsinfo.FSI_StrucSig, fat32_part_info[current_part_id].fsinfo.FSI_Free_Count);
  75. kdebug("fat32_part_info[part_id].bootsector.BPB_RootClus = %#018lx", fat32_part_info[current_part_id].bootsector.BPB_RootClus);
  76. return current_part_id;
  77. }
  78. /**
  79. * @brief 读取指定簇的FAT表项
  80. *
  81. * @param part_id 分区id
  82. * @param cluster
  83. * @return uint32_t 下一个簇的簇号
  84. */
  85. uint32_t fat32_read_FAT_entry(uint32_t part_id, uint32_t cluster)
  86. {
  87. uint32_t fat_ent_per_sec = (fat32_part_info[part_id].bootsector.BPB_BytesPerSec >> 2); // 该值应为2的n次幂
  88. uint32_t buf[256];
  89. memset(buf, 0, fat32_part_info[part_id].bootsector.BPB_BytesPerSec);
  90. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, fat32_part_info[part_id].FAT1_base_sector + (cluster / fat_ent_per_sec), 1, (uint64_t)&buf, fat32_part_info[part_id].ahci_ctrl_num, fat32_part_info[part_id].ahci_port_num);
  91. uint32_t ret = buf[cluster & (fat_ent_per_sec - 1)] & 0x0fffffff;
  92. return ret;
  93. }
  94. /**
  95. * @brief 写入指定簇的FAT表项
  96. *
  97. * @param part_id 分区id
  98. * @param cluster
  99. * @param value 要写入该fat表项的值
  100. * @return uint32_t 下一个簇的簇号
  101. */
  102. uint32_t fat32_write_FAT_entry(uint32_t part_id, uint32_t cluster, uint32_t value)
  103. {
  104. uint32_t fat_ent_per_sec = (fat32_part_info[part_id].bootsector.BPB_BytesPerSec >> 2); // 该值应为2的n次幂
  105. uint32_t buf[256];
  106. memset(buf, 0, fat32_part_info[part_id].bootsector.BPB_BytesPerSec);
  107. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, fat32_part_info[part_id].FAT1_base_sector + (cluster / fat_ent_per_sec), 1, (uint64_t)&buf, fat32_part_info[part_id].ahci_ctrl_num, fat32_part_info[part_id].ahci_port_num);
  108. buf[cluster & (fat_ent_per_sec - 1)] = (buf[cluster & (fat_ent_per_sec - 1)] & 0xf0000000) | (value & 0x0fffffff);
  109. // 向FAT1和FAT2写入数据
  110. ahci_operation.transfer(ATA_CMD_WRITE_DMA_EXT, fat32_part_info[part_id].FAT1_base_sector + (cluster / fat_ent_per_sec), 1, (uint64_t)&buf, fat32_part_info[part_id].ahci_ctrl_num, fat32_part_info[part_id].ahci_port_num);
  111. ahci_operation.transfer(ATA_CMD_WRITE_DMA_EXT, fat32_part_info[part_id].FAT2_base_sector + (cluster / fat_ent_per_sec), 1, (uint64_t)&buf, fat32_part_info[part_id].ahci_ctrl_num, fat32_part_info[part_id].ahci_port_num);
  112. return 0;
  113. }
  114. /**
  115. * @brief 在父目录中寻找指定的目录项
  116. *
  117. * @param part_id 分区id
  118. * @param name 目录项名字
  119. * @param name_len 目录项名字长度
  120. * @param dentry 父目录
  121. * @param flags
  122. * @return struct fat32_Directory_t* 目标目录项
  123. */
  124. struct fat32_Directory_t *fat32_lookup(uint32_t part_id, char *name, int name_len, struct fat32_Directory_t *dentry, int flags)
  125. {
  126. int errcode = 0;
  127. uint8_t *buf = kmalloc(fat32_part_info[part_id].bytes_per_clus, 0);
  128. memset(buf, 0, fat32_part_info[part_id].bytes_per_clus);
  129. // 计算父目录项的起始簇号
  130. uint32_t cluster = ((dentry->DIR_FstClusHI << 16) | (dentry->DIR_FstClusLO)) & 0x0fffffff;
  131. /*
  132. kdebug("dentry->DIR_FstClusHI=%#010lx", dentry->DIR_FstClusHI);
  133. kdebug("dentry->DIR_FstClusLo=%#010lx", dentry->DIR_FstClusLO);
  134. kdebug("cluster=%#010lx", cluster);
  135. */
  136. while (true)
  137. {
  138. // 计算父目录项的起始LBA扇区号
  139. uint64_t sector = fat32_part_info[part_id].first_data_sector + (cluster - 2) * fat32_part_info[part_id].bootsector.BPB_SecPerClus;
  140. //kdebug("fat32_part_info[part_id].bootsector.BPB_SecPerClus=%d",fat32_part_info[part_id].bootsector.BPB_SecPerClus);
  141. //kdebug("sector=%d",sector);
  142. // 读取父目录项的起始簇数据
  143. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, sector, fat32_part_info[part_id].bootsector.BPB_SecPerClus, (uint64_t)buf, 0, 0);
  144. //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);
  145. struct fat32_Directory_t *tmp_dEntry = (struct fat32_Directory_t *)buf;
  146. // 查找短目录项
  147. for (int i = 0; i < fat32_part_info[part_id].bytes_per_clus; i += 32, ++tmp_dEntry)
  148. {
  149. // 跳过长目录项
  150. if (tmp_dEntry->DIR_Attr == ATTR_LONG_NAME)
  151. continue;
  152. // 跳过无效页表项、空闲页表项
  153. if (tmp_dEntry->DIR_Name[0] == 0xe5 || tmp_dEntry->DIR_Name[0] == 0x00 || tmp_dEntry->DIR_Name[0] == 0x05)
  154. continue;
  155. // 找到长目录项,位于短目录项之前
  156. struct fat32_LongDirectory_t *tmp_ldEntry = (struct fat32_LongDirectory_t *)tmp_dEntry - 1;
  157. int js = 0;
  158. // 遍历每个长目录项
  159. while (tmp_ldEntry->LDIR_Attr == ATTR_LONG_NAME && tmp_ldEntry->LDIR_Ord != 0xe5)
  160. {
  161. // 比较name1
  162. for (int x = 0; x < 5; ++x)
  163. {
  164. if (js > name_len && tmp_ldEntry->LDIR_Name1[x] == 0xffff)
  165. continue;
  166. else if (js > name_len || tmp_ldEntry->LDIR_Name1[x] != (uint16_t)(name[js++])) // 文件名不匹配,检索下一个短目录项
  167. goto continue_cmp_fail;
  168. }
  169. // 比较name2
  170. for (int x = 0; x < 6; ++x)
  171. {
  172. if (js > name_len && tmp_ldEntry->LDIR_Name2[x] == 0xffff)
  173. continue;
  174. else if (js > name_len || tmp_ldEntry->LDIR_Name2[x] != (uint16_t)(name[js++])) // 文件名不匹配,检索下一个短目录项
  175. goto continue_cmp_fail;
  176. }
  177. // 比较name3
  178. for (int x = 0; x < 2; ++x)
  179. {
  180. if (js > name_len && tmp_ldEntry->LDIR_Name3[x] == 0xffff)
  181. continue;
  182. else if (js > name_len || tmp_ldEntry->LDIR_Name3[x] != (uint16_t)(name[js++])) // 文件名不匹配,检索下一个短目录项
  183. goto continue_cmp_fail;
  184. }
  185. if (js >= name_len) // 找到需要的目录项,返回
  186. {
  187. struct fat32_Directory_t *p = (struct fat32_Directory_t *)kmalloc(sizeof(struct fat32_Directory_t), 0);
  188. *p = *tmp_dEntry;
  189. kfree(buf);
  190. return p;
  191. }
  192. --tmp_ldEntry; // 检索下一个长目录项
  193. }
  194. // 不存在长目录项,匹配短目录项的基础名
  195. js = 0;
  196. for (int x = 0; x < 8; ++x)
  197. {
  198. switch (tmp_dEntry->DIR_Name[x])
  199. {
  200. case ' ':
  201. if (!(tmp_dEntry->DIR_Attr & ATTR_DIRECTORY)) // 不是文件夹(是文件)
  202. {
  203. if (name[js] == '.')
  204. continue;
  205. else if (tmp_dEntry->DIR_Name[x] == name[js])
  206. {
  207. ++js;
  208. break;
  209. }
  210. else
  211. goto continue_cmp_fail;
  212. }
  213. else // 是文件夹
  214. {
  215. if (js < name_len && tmp_dEntry->DIR_Name[x] == name[js]) // 当前位正确匹配
  216. {
  217. ++js;
  218. break; // 进行下一位的匹配
  219. }
  220. else if (js == name_len)
  221. continue;
  222. else
  223. goto continue_cmp_fail;
  224. }
  225. break;
  226. // 当前位是字母
  227. case 'A' ... 'Z':
  228. case 'a' ... 'z':
  229. if (tmp_dEntry->DIR_NTRes & LOWERCASE_BASE) // 为兼容windows系统,检测DIR_NTRes字段
  230. {
  231. if (js < name_len && (tmp_dEntry->DIR_Name[x] + 32 == name[js]))
  232. {
  233. ++js;
  234. break;
  235. }
  236. else
  237. goto continue_cmp_fail;
  238. }
  239. else
  240. {
  241. if (js < name_len && tmp_dEntry->DIR_Name[x] == name[js])
  242. {
  243. ++js;
  244. break;
  245. }
  246. else
  247. goto continue_cmp_fail;
  248. }
  249. break;
  250. case '0' ... '9':
  251. if (js < name_len && tmp_dEntry->DIR_Name[x] == name[js])
  252. {
  253. ++js;
  254. break;
  255. }
  256. else
  257. goto continue_cmp_fail;
  258. break;
  259. default:
  260. ++js;
  261. break;
  262. }
  263. }
  264. // 若短目录项为文件,则匹配扩展名
  265. if (!(tmp_dEntry->DIR_Attr & ATTR_DIRECTORY))
  266. {
  267. ++js;
  268. for (int x = 8; x < 11; ++x)
  269. {
  270. switch (tmp_dEntry->DIR_Name[x])
  271. {
  272. // 当前位是字母
  273. case 'A' ... 'Z':
  274. case 'a' ... 'z':
  275. if (tmp_dEntry->DIR_NTRes & LOWERCASE_EXT) // 为兼容windows系统,检测DIR_NTRes字段
  276. {
  277. if ((tmp_dEntry->DIR_Name[x] + 32 == name[js]))
  278. {
  279. ++js;
  280. break;
  281. }
  282. else
  283. goto continue_cmp_fail;
  284. }
  285. else
  286. {
  287. if (tmp_dEntry->DIR_Name[x] == name[js])
  288. {
  289. ++js;
  290. break;
  291. }
  292. else
  293. goto continue_cmp_fail;
  294. }
  295. break;
  296. case '0' ... '9':
  297. case ' ':
  298. if (tmp_dEntry->DIR_Name[x] == name[js])
  299. {
  300. ++js;
  301. break;
  302. }
  303. else
  304. goto continue_cmp_fail;
  305. break;
  306. default:
  307. goto continue_cmp_fail;
  308. break;
  309. }
  310. }
  311. }
  312. struct fat32_Directory_t *p = (struct fat32_Directory_t *)kmalloc(sizeof(struct fat32_Directory_t), 0);
  313. *p = *tmp_dEntry;
  314. kfree(buf);
  315. return p;
  316. continue_cmp_fail:;
  317. }
  318. // 当前簇没有发现目标文件名,寻找下一个簇
  319. cluster = fat32_read_FAT_entry(part_id, cluster);
  320. if (cluster >= 0x0ffffff7) // 寻找完父目录的所有簇,都没有找到目标文件名
  321. {
  322. kfree(buf);
  323. return NULL;
  324. }
  325. }
  326. }
  327. /**
  328. * @brief 按照路径查找文件
  329. *
  330. * @param part_id fat32分区id
  331. * @param path
  332. * @param flags
  333. * @return struct fat32_Directory_t*
  334. */
  335. struct fat32_Directory_t *fat32_path_walk(uint32_t part_id, char *path, uint64_t flags)
  336. {
  337. // 去除路径前的斜杠
  338. while (*path == '/')
  339. ++path;
  340. if ((!*path) || (*path == '\0'))
  341. return NULL;
  342. struct fat32_Directory_t *parent = (struct fat32_Directory_t *)kmalloc(sizeof(struct fat32_Directory_t), 0);
  343. char *dEntry_name = kmalloc(PAGE_4K_SIZE, 0);
  344. memset(parent, 0, sizeof(struct fat32_Directory_t));
  345. memset(dEntry_name, 0, PAGE_4K_SIZE);
  346. parent->DIR_FstClusLO = fat32_part_info[part_id].bootsector.BPB_RootClus & 0xffff;
  347. parent->DIR_FstClusHI = (fat32_part_info[part_id].bootsector.BPB_RootClus >> 16) & 0xffff;
  348. while (true)
  349. {
  350. // 提取出下一级待搜索的目录名或文件名,并保存在dEntry_name中
  351. char *tmp_path = path;
  352. while ((*path && *path != '\0') && (*path != '/'))
  353. ++path;
  354. int tmp_path_len = path - tmp_path;
  355. memcpy(dEntry_name, tmp_path, tmp_path_len);
  356. dEntry_name[tmp_path_len] = '\0';
  357. //kdebug("dEntry_name=%s", dEntry_name);
  358. struct fat32_Directory_t *next_dir = fat32_lookup(part_id, dEntry_name, tmp_path_len, parent, flags);
  359. if (next_dir == NULL)
  360. {
  361. // 搜索失败
  362. kerror("cannot find the file/dir : %s", dEntry_name);
  363. kfree(dEntry_name);
  364. kfree(parent);
  365. return NULL;
  366. }
  367. while (*path == '/')
  368. ++path;
  369. if ((!*path) || (*path == '\0')) // 已经到达末尾
  370. {
  371. if (flags & 1) // 返回父目录
  372. {
  373. kfree(dEntry_name);
  374. kfree(next_dir);
  375. return parent;
  376. }
  377. kfree(dEntry_name);
  378. kfree(parent);
  379. return next_dir;
  380. }
  381. *parent = *next_dir;
  382. kfree(next_dir);
  383. }
  384. }
  385. void fat32_init()
  386. {
  387. spin_init(&fat32_part_reg_lock);
  388. }