1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285 |
- #include "fat32.h"
- #include <common/kprint.h>
- #include <driver/disk/ahci/ahci.h>
- #include <filesystem/MBR.h>
- #include <common/spinlock.h>
- #include <mm/slab.h>
- #include <common/errno.h>
- #include <common/stdio.h>
- #include "fat_ent.h"
- struct vfs_super_block_operations_t fat32_sb_ops;
- struct vfs_dir_entry_operations_t fat32_dEntry_ops;
- struct vfs_file_operations_t fat32_file_ops;
- struct vfs_inode_operations_t fat32_inode_ops;
- struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t ahci_port_num, uint8_t part_num)
- {
- struct MBR_disk_partition_table_t *DPT = MBR_read_partition_table(ahci_ctrl_num, ahci_port_num);
-
-
- printk_color(ORANGE, BLACK, "DPTE[0] start_LBA:%#018lx\ttype:%#018lx\n", DPT->DPTE[part_num].starting_LBA, DPT->DPTE[part_num].type);
- uint8_t buf[512] = {0};
-
- ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, DPT->DPTE[part_num].starting_LBA, 1, (uint64_t)&buf, ahci_ctrl_num, ahci_port_num);
-
- return vfs_mount_fs("FAT32", (void *)(&DPT->DPTE[part_num]), VFS_DPT_MBR, buf, ahci_ctrl_num, ahci_port_num, part_num);
- }
- static uint8_t fat32_ChkSum(uint8_t *name)
- {
- uint8_t chksum = 0;
- for (uint8_t i = 0; i < 11; ++i)
- {
- chksum = ((chksum & 1) ? 0x80 : 0) + (chksum >> 1) + *name;
- ++name;
- }
- return chksum;
- }
- struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dentry)
- {
- int errcode = 0;
- struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
- fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
- uint8_t *buf = kmalloc(fsbi->bytes_per_clus, 0);
- memset(buf, 0, fsbi->bytes_per_clus);
-
- uint32_t cluster = finode->first_clus;
- struct fat32_Directory_t *tmp_dEntry = NULL;
- while (true)
- {
-
- uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
-
-
-
- ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
-
- tmp_dEntry = (struct fat32_Directory_t *)buf;
-
- for (int i = 0; i < fsbi->bytes_per_clus; i += 32, ++tmp_dEntry)
- {
-
- if (tmp_dEntry->DIR_Attr == ATTR_LONG_NAME)
- continue;
-
- if (tmp_dEntry->DIR_Name[0] == 0xe5 || tmp_dEntry->DIR_Name[0] == 0x00 || tmp_dEntry->DIR_Name[0] == 0x05)
- continue;
-
-
- struct fat32_LongDirectory_t *tmp_ldEntry = (struct fat32_LongDirectory_t *)tmp_dEntry - 1;
- int js = 0;
-
- while (tmp_ldEntry->LDIR_Attr == ATTR_LONG_NAME && tmp_ldEntry->LDIR_Ord != 0xe5)
- {
-
- for (int x = 0; x < 5; ++x)
- {
- if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name1[x] == 0xffff)
- continue;
- else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name1[x] != (uint16_t)(dest_dentry->name[js++]))
- goto continue_cmp_fail;
- }
-
- for (int x = 0; x < 6; ++x)
- {
- if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name2[x] == 0xffff)
- continue;
- else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name2[x] != (uint16_t)(dest_dentry->name[js++]))
- goto continue_cmp_fail;
- }
-
- for (int x = 0; x < 2; ++x)
- {
- if (js > dest_dentry->name_length && tmp_ldEntry->LDIR_Name3[x] == 0xffff)
- continue;
- else if (js > dest_dentry->name_length || tmp_ldEntry->LDIR_Name3[x] != (uint16_t)(dest_dentry->name[js++]))
- goto continue_cmp_fail;
- }
- if (js >= dest_dentry->name_length)
- {
-
- goto find_lookup_success;
- }
- --tmp_ldEntry;
- }
-
- js = 0;
- for (int x = 0; x < 8; ++x)
- {
-
-
- switch (tmp_dEntry->DIR_Name[x])
- {
- case ' ':
- if (!(tmp_dEntry->DIR_Attr & ATTR_DIRECTORY))
- {
- if (dest_dentry->name[js] == '.')
- continue;
- else if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
- {
- ++js;
- break;
- }
- else
- goto continue_cmp_fail;
- }
- else
- {
- if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
- {
- ++js;
- break;
- }
- else if (js == dest_dentry->name_length)
- continue;
- else
- goto continue_cmp_fail;
- }
- break;
-
- case 'A' ... 'Z':
- case 'a' ... 'z':
- if (tmp_dEntry->DIR_NTRes & LOWERCASE_BASE)
- {
- if (js < dest_dentry->name_length && (tmp_dEntry->DIR_Name[x] + 32 == dest_dentry->name[js]))
- {
- ++js;
- break;
- }
- else
- goto continue_cmp_fail;
- }
- else
- {
- if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
- {
- ++js;
- break;
- }
- else
- goto continue_cmp_fail;
- }
- break;
- case '0' ... '9':
- if (js < dest_dentry->name_length && tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
- {
- ++js;
- break;
- }
- else
- goto continue_cmp_fail;
- break;
- default:
-
- goto continue_cmp_fail;
- break;
- }
- }
- if (js > dest_dentry->name_length)
- {
- kdebug("js > namelen");
- goto continue_cmp_fail;
- }
-
- if (!(tmp_dEntry->DIR_Attr & ATTR_DIRECTORY))
- {
- ++js;
- for (int x = 8; x < 11; ++x)
- {
- switch (tmp_dEntry->DIR_Name[x])
- {
-
- case 'A' ... 'Z':
- case 'a' ... 'z':
- if (tmp_dEntry->DIR_NTRes & LOWERCASE_EXT)
- {
- if ((tmp_dEntry->DIR_Name[x] + 32 == dest_dentry->name[js]))
- {
- ++js;
- break;
- }
- else
- goto continue_cmp_fail;
- }
- else
- {
- if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
- {
- ++js;
- break;
- }
- else
- goto continue_cmp_fail;
- }
- break;
- case '0' ... '9':
- case ' ':
- if (tmp_dEntry->DIR_Name[x] == dest_dentry->name[js])
- {
- ++js;
- break;
- }
- else
- goto continue_cmp_fail;
- break;
- default:
- goto continue_cmp_fail;
- break;
- }
- }
- }
- if (js > dest_dentry->name_length)
- {
- kdebug("js > namelen");
- goto continue_cmp_fail;
- }
- goto find_lookup_success;
- continue_cmp_fail:;
- }
-
- cluster = fat32_read_FAT_entry(fsbi, cluster);
- if (cluster >= 0x0ffffff7)
- {
- kfree(buf);
- return NULL;
- }
- }
- find_lookup_success:;
- struct vfs_index_node_t *p = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
- memset(p, 0, sizeof(struct vfs_index_node_t));
- p->file_size = tmp_dEntry->DIR_FileSize;
-
- p->blocks = (p->file_size + fsbi->bytes_per_clus - 1) / fsbi->bytes_per_sec;
- p->attribute = (tmp_dEntry->DIR_Attr & ATTR_DIRECTORY) ? VFS_ATTR_DIR : VFS_ATTR_FILE;
- p->sb = parent_inode->sb;
- p->file_ops = &fat32_file_ops;
- p->inode_ops = &fat32_inode_ops;
-
- p->private_inode_info = (void *)kmalloc(sizeof(fat32_inode_info_t), 0);
- memset(p->private_inode_info, 0, sizeof(fat32_inode_info_t));
- finode = (fat32_inode_info_t *)p->private_inode_info;
- finode->first_clus = ((tmp_dEntry->DIR_FstClusHI << 16) | tmp_dEntry->DIR_FstClusLO) & 0x0fffffff;
- finode->dEntry_location_clus = cluster;
- finode->dEntry_location_clus_offset = tmp_dEntry - (struct fat32_Directory_t *)buf;
-
-
- finode->create_date = tmp_dEntry->DIR_CrtDate;
- finode->create_time = tmp_dEntry->DIR_CrtTime;
- finode->write_date = tmp_dEntry->DIR_WrtDate;
- finode->write_time = tmp_dEntry->DIR_WrtTime;
-
-
- if ((tmp_dEntry->DIR_FstClusHI >> 12) && (p->attribute & VFS_ATTR_FILE))
- p->attribute |= VFS_ATTR_DEVICE;
- dest_dentry->dir_inode = p;
- dest_dentry->dir_ops = &fat32_dEntry_ops;
- list_init(&dest_dentry->child_node_list);
- list_init(&dest_dentry->subdirs_list);
- kfree(buf);
- return dest_dentry;
- }
- 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)
- {
- if (DPT_type != VFS_DPT_MBR)
- {
- kerror("fat32_read_superblock(): Unsupported DPT!");
- return NULL;
- }
-
- struct vfs_superblock_t *sb_ptr = (struct vfs_superblock_t *)kmalloc(sizeof(struct vfs_superblock_t), 0);
- memset(sb_ptr, 0, sizeof(struct vfs_superblock_t));
- sb_ptr->sb_ops = &fat32_sb_ops;
- sb_ptr->private_sb_info = kmalloc(sizeof(fat32_sb_info_t), 0);
- memset(sb_ptr->private_sb_info, 0, sizeof(fat32_sb_info_t));
- struct fat32_BootSector_t *fbs = (struct fat32_BootSector_t *)buf;
- fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(sb_ptr->private_sb_info);
-
- struct MBR_disk_partition_table_entry_t *MBR_DPTE = (struct MBR_disk_partition_table_entry_t *)DPTE;
- fsbi->ahci_ctrl_num = ahci_ctrl_num;
- fsbi->ahci_port_num = ahci_port_num;
- fsbi->part_num = part_num;
- fsbi->starting_sector = MBR_DPTE->starting_LBA;
- fsbi->sector_count = MBR_DPTE->total_sectors;
- fsbi->sec_per_clus = fbs->BPB_SecPerClus;
- fsbi->bytes_per_clus = fbs->BPB_SecPerClus * fbs->BPB_BytesPerSec;
- fsbi->bytes_per_sec = fbs->BPB_BytesPerSec;
- fsbi->first_data_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt + fbs->BPB_FATSz32 * fbs->BPB_NumFATs;
- fsbi->FAT1_base_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt;
- fsbi->FAT2_base_sector = fsbi->FAT1_base_sector + fbs->BPB_FATSz32;
- fsbi->sec_per_FAT = fbs->BPB_FATSz32;
- fsbi->NumFATs = fbs->BPB_NumFATs;
- fsbi->fsinfo_sector_addr_infat = fbs->BPB_FSInfo;
- fsbi->bootsector_bak_sector_addr_infat = fbs->BPB_BkBootSec;
- 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);
-
- memset(&fsbi->fsinfo, 0, sizeof(struct fat32_FSInfo_t));
- 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);
- 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);
-
- sb_ptr->root = (struct vfs_dir_entry_t *)kmalloc(sizeof(struct vfs_dir_entry_t), 0);
- memset(sb_ptr->root, 0, sizeof(struct vfs_dir_entry_t));
- list_init(&sb_ptr->root->child_node_list);
- list_init(&sb_ptr->root->subdirs_list);
- sb_ptr->root->parent = sb_ptr->root;
- sb_ptr->root->dir_ops = &fat32_dEntry_ops;
-
- sb_ptr->root->name = (char *)(kmalloc(2, 0));
- sb_ptr->root->name[0] = '/';
- sb_ptr->root->name_length = 1;
-
- sb_ptr->root->dir_inode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
- memset(sb_ptr->root->dir_inode, 0, sizeof(struct vfs_index_node_t));
- sb_ptr->root->dir_inode->inode_ops = &fat32_inode_ops;
- sb_ptr->root->dir_inode->file_ops = &fat32_file_ops;
- sb_ptr->root->dir_inode->file_size = 0;
-
- sb_ptr->root->dir_inode->blocks = (sb_ptr->root->dir_inode->file_size + fsbi->bytes_per_clus - 1) / fsbi->bytes_per_sec;
- sb_ptr->root->dir_inode->attribute = VFS_ATTR_DIR;
- sb_ptr->root->dir_inode->sb = sb_ptr;
-
- sb_ptr->root->dir_inode->private_inode_info = kmalloc(sizeof(struct fat32_inode_info_t), 0);
- memset(sb_ptr->root->dir_inode->private_inode_info, 0, sizeof(struct fat32_inode_info_t));
- struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)sb_ptr->root->dir_inode->private_inode_info;
- finode->first_clus = fbs->BPB_RootClus;
- finode->dEntry_location_clus = 0;
- finode->dEntry_location_clus_offset = 0;
- finode->create_time = 0;
- finode->create_date = 0;
- finode->write_date = 0;
- finode->write_time;
- return sb_ptr;
- }
- void fat32_write_superblock(struct vfs_superblock_t *sb)
- {
- }
- void fat32_put_superblock(struct vfs_superblock_t *sb)
- {
- kfree(sb->private_sb_info);
- kfree(sb->root->dir_inode->private_inode_info);
- kfree(sb->root->dir_inode);
- kfree(sb->root);
- kfree(sb);
- }
- void fat32_write_inode(struct vfs_index_node_t *inode)
- {
- fat32_inode_info_t *finode = inode->private_inode_info;
- if (finode->dEntry_location_clus == 0)
- {
- kerror("FAT32 error: Attempt to write the root inode");
- return;
- }
- fat32_sb_info_t *fsbi = (fat32_sb_info_t *)inode->sb->private_sb_info;
-
- uint64_t fLBA = fsbi->first_data_sector + (finode->dEntry_location_clus - 2) * fsbi->sec_per_clus;
- struct fat32_Directory_t *buf = (struct fat32_Directory_t *)kmalloc(fsbi->bytes_per_clus, 0);
- memset(buf, 0, fsbi->bytes_per_clus);
- ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
-
- struct fat32_Directory_t *fdEntry = buf + finode->dEntry_location_clus_offset;
-
- fdEntry->DIR_FileSize = inode->file_size;
- fdEntry->DIR_FstClusLO = finode->first_clus & 0xffff;
- fdEntry->DIR_FstClusHI = (finode->first_clus >> 16) | (fdEntry->DIR_FstClusHI & 0xf000);
-
- ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
- kfree(buf);
- }
- struct vfs_super_block_operations_t fat32_sb_ops =
- {
- .write_superblock = fat32_write_superblock,
- .put_superblock = fat32_put_superblock,
- .write_inode = fat32_write_inode,
- };
- long fat32_compare(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename)
- {
- }
- long fat32_hash(struct vfs_dir_entry_t *dEntry, char *filename)
- {
- }
- long fat32_release(struct vfs_dir_entry_t *dEntry)
- {
- }
- long fat32_iput(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode)
- {
- }
- struct vfs_dir_entry_operations_t fat32_dEntry_ops =
- {
- .compare = fat32_compare,
- .hash = fat32_hash,
- .release = fat32_release,
- .iput = fat32_iput,
- };
- long fat32_open(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
- {
- return VFS_SUCCESS;
- }
- long fat32_close(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
- {
- return VFS_SUCCESS;
- }
- long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position)
- {
- struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)(file_ptr->dEntry->dir_inode->private_inode_info);
- fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info);
-
- uint64_t cluster = finode->first_clus;
-
-
-
- uint64_t clus_offset_in_file = (*position) / fsbi->bytes_per_clus;
-
- uint64_t bytes_offset = (*position) % fsbi->bytes_per_clus;
- if (!cluster)
- return -EFAULT;
-
- for (int i = 0; i < clus_offset_in_file; ++i)
- cluster = fat32_read_FAT_entry(fsbi, cluster);
-
- if (*position + count > file_ptr->dEntry->dir_inode->file_size)
- count = file_ptr->dEntry->dir_inode->file_size - *position;
-
- int64_t bytes_remain = count;
-
- void *tmp_buffer = kmalloc(fsbi->bytes_per_clus, 0);
- int64_t retval = 0;
- do
- {
- memset(tmp_buffer, 0, fsbi->bytes_per_clus);
- uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
-
- 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);
- if (errno != AHCI_SUCCESS)
- {
- kerror("FAT32 FS(read) error!");
- retval = -EIO;
- break;
- }
- int64_t step_trans_len = 0;
- if (bytes_remain > (fsbi->bytes_per_clus - bytes_offset))
- step_trans_len = (fsbi->bytes_per_clus - bytes_offset);
- else
- step_trans_len = bytes_remain;
- if (((uint64_t)buf) < USER_MAX_LINEAR_ADDR)
- copy_to_user(buf, tmp_buffer + bytes_offset, step_trans_len);
- else
- memcpy(buf, tmp_buffer + bytes_offset, step_trans_len);
- bytes_remain -= step_trans_len;
- buf += step_trans_len;
- bytes_offset -= bytes_offset;
- *position += step_trans_len;
- cluster = fat32_read_FAT_entry(fsbi, cluster);
- } while (bytes_remain && (cluster < 0x0ffffff8) && cluster != 0);
- kfree(tmp_buffer);
- if (!bytes_remain)
- retval = count;
- return retval;
- }
- long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position)
- {
- struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)file_ptr->dEntry->dir_inode->private_inode_info;
- fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info);
-
- uint32_t cluster = finode->first_clus;
- int64_t flags = 0;
-
- uint64_t clus_offset_in_file = (*position) / fsbi->bytes_per_clus;
-
- uint64_t bytes_offset = (*position) % fsbi->bytes_per_clus;
- if (!cluster)
- {
-
- if (fat32_alloc_clusters(file_ptr->dEntry->dir_inode, &cluster, 1) != 0)
- return -ENOSPC;
- }
- else
- {
-
- for (uint64_t i = 0; i < clus_offset_in_file; ++i)
- cluster = fat32_read_FAT_entry(fsbi, cluster);
- }
-
-
- if (!cluster)
- return -ENOSPC;
-
- int64_t bytes_remain = count;
- if (count < 0)
- return -EINVAL;
- uint64_t sector;
- int64_t retval = 0;
- void *tmp_buffer = kmalloc(fsbi->bytes_per_clus, 0);
- do
- {
- memset(tmp_buffer, 0, fsbi->bytes_per_clus);
- sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
- if (!flags)
- {
-
-
- 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);
- if (errno != AHCI_SUCCESS)
- {
-
- retval = -EIO;
- break;
- }
- }
- int64_t step_trans_len = 0;
- if (bytes_remain > (fsbi->bytes_per_clus - bytes_offset))
- step_trans_len = (fsbi->bytes_per_clus - bytes_offset);
- else
- step_trans_len = bytes_remain;
-
- if (((uint64_t)buf) < USER_MAX_LINEAR_ADDR)
- copy_from_user(tmp_buffer + bytes_offset, buf, step_trans_len);
- else
- memcpy(tmp_buffer + bytes_offset, buf, step_trans_len);
-
- 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);
- if (errno != AHCI_SUCCESS)
- {
- kerror("FAT32 FS(write) write disk error!");
- retval = -EIO;
- break;
- }
- bytes_remain -= step_trans_len;
- buf += step_trans_len;
- bytes_offset -= bytes_offset;
- *position += step_trans_len;
-
- int next_clus = 0;
- if (bytes_remain)
- next_clus = fat32_read_FAT_entry(fsbi, cluster);
- else
- break;
- if (next_clus >= 0x0ffffff8)
- {
- if (fat32_alloc_clusters(file_ptr->dEntry->dir_inode, &next_clus, 1) != 0)
- {
-
- kfree(tmp_buffer);
- return -ENOSPC;
- }
- cluster = next_clus;
- flags = 1;
- }
- } while (bytes_remain);
-
- if (*position > (file_ptr->dEntry->dir_inode->file_size))
- {
- file_ptr->dEntry->dir_inode->file_size = *position;
- file_ptr->dEntry->dir_inode->sb->sb_ops->write_inode(file_ptr->dEntry->dir_inode);
-
- }
- kfree(tmp_buffer);
- if (!bytes_remain)
- retval = count;
-
- return retval;
- }
- long fat32_lseek(struct vfs_file_t *file_ptr, long offset, long whence)
- {
- struct vfs_index_node_t *inode = file_ptr->dEntry->dir_inode;
- long pos = 0;
- switch (whence)
- {
- case SEEK_SET:
- pos = offset;
- break;
- case SEEK_CUR:
- pos = file_ptr->position + offset;
- break;
- case SEEK_END:
- pos = file_ptr->dEntry->dir_inode->file_size + offset;
- break;
- default:
- return -EINVAL;
- break;
- }
- if (pos < 0 || pos > file_ptr->dEntry->dir_inode->file_size)
- return -EOVERFLOW;
- file_ptr->position = pos;
-
- return pos;
- }
- long fat32_ioctl(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr, uint64_t cmd, uint64_t arg)
- {
- }
- struct vfs_file_operations_t fat32_file_ops =
- {
- .open = fat32_open,
- .close = fat32_close,
- .read = fat32_read,
- .write = fat32_write,
- .lseek = fat32_lseek,
- .ioctl = fat32_ioctl,
- .readdir = fat32_readdir,
- };
- long fat32_create(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry, int mode)
- {
-
- fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
-
- struct fat32_inode_info_t *parent_inode_info = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
- int64_t retval = 0;
-
- retval = fat32_check_name_available(dest_dEntry->name, dest_dEntry->name_length, 0);
- if (retval != 0)
- return retval;
- if (dest_dEntry->dir_inode != NULL)
- return -EEXIST;
- struct vfs_index_node_t *inode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
- memset((void *)inode, 0, sizeof(struct vfs_index_node_t));
- dest_dEntry->dir_inode = inode;
- dest_dEntry->dir_ops = &fat32_dEntry_ops;
- struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)kmalloc(sizeof(struct fat32_inode_info_t), 0);
- memset((void *)finode, 0, sizeof(struct fat32_inode_info_t));
- inode->attribute = VFS_ATTR_FILE;
- inode->file_ops = &fat32_file_ops;
- inode->file_size = 0;
- inode->sb = parent_inode->sb;
- inode->inode_ops = &fat32_inode_ops;
- inode->private_inode_info = (void *)finode;
- inode->blocks = fsbi->sec_per_clus;
-
- uint32_t cnt_longname = (dest_dEntry->name_length + 25) / 26;
-
- if (cnt_longname == 0)
- cnt_longname = 1;
-
- uint32_t tmp_dentry_sector = 0;
-
- uint64_t tmp_dentry_clus_buf_addr = 0;
- uint64_t tmp_parent_dentry_clus = 0;
-
- struct fat32_Directory_t *empty_fat32_dentry = fat32_find_empty_dentry(parent_inode, cnt_longname + 1, 0, &tmp_dentry_sector, &tmp_parent_dentry_clus, &tmp_dentry_clus_buf_addr);
-
- finode->first_clus = 0;
- finode->dEntry_location_clus = tmp_parent_dentry_clus;
- finode->dEntry_location_clus_offset = empty_fat32_dentry - (struct fat32_Directory_t *)tmp_dentry_clus_buf_addr;
-
- uint32_t new_dir_clus;
- if (fat32_alloc_clusters(inode, &new_dir_clus, 1) != 0)
- {
- retval = -ENOSPC;
- goto fail;
- }
-
-
-
- fat32_fill_shortname(dest_dEntry, empty_fat32_dentry, new_dir_clus);
-
-
- uint8_t short_dentry_ChkSum = fat32_ChkSum(empty_fat32_dentry->DIR_Name);
-
-
- fat32_fill_longname(dest_dEntry, (struct fat32_LongDirectory_t *)(empty_fat32_dentry - 1), short_dentry_ChkSum, cnt_longname);
-
-
- ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, tmp_dentry_sector, fsbi->sec_per_clus, tmp_dentry_clus_buf_addr, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
-
-
- kfree((void *)tmp_dentry_clus_buf_addr);
- return 0;
- fail:;
-
- kfree((void *)tmp_dentry_clus_buf_addr);
- dest_dEntry->dir_inode = NULL;
- dest_dEntry->dir_ops = NULL;
- kfree(finode);
- kfree(inode);
- return retval;
- }
- int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dEntry, int mode)
- {
- int64_t retval = 0;
-
- fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
-
- struct fat32_inode_info_t *parent_inode_info = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
-
- retval = fat32_check_name_available(dEntry->name, dEntry->name_length, 0);
- if (retval != 0)
- return retval;
-
-
- uint32_t cnt_longname = (dEntry->name_length + 25) / 26;
-
- if (cnt_longname == 0)
- cnt_longname = 1;
-
- uint32_t tmp_dentry_sector = 0;
-
- uint64_t tmp_dentry_clus_buf_addr = 0;
- uint64_t tmp_parent_dentry_clus = 0;
-
- struct fat32_Directory_t *empty_fat32_dentry = fat32_find_empty_dentry(parent_inode, cnt_longname + 1, 0, &tmp_dentry_sector, &tmp_parent_dentry_clus, &tmp_dentry_clus_buf_addr);
-
- struct vfs_index_node_t *inode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
- memset(inode, 0, sizeof(struct vfs_index_node_t));
- inode->attribute = VFS_ATTR_DIR;
- inode->blocks = fsbi->sec_per_clus;
- inode->file_ops = &fat32_file_ops;
- inode->file_size = 0;
- inode->inode_ops = &fat32_inode_ops;
- inode->sb = parent_inode->sb;
-
- inode->private_inode_info = (fat32_inode_info_t *)kmalloc(sizeof(fat32_inode_info_t), 0);
- memset(inode->private_inode_info, 0, sizeof(fat32_inode_info_t));
- fat32_inode_info_t *p = (fat32_inode_info_t *)inode->private_inode_info;
- p->first_clus = 0;
- p->dEntry_location_clus = tmp_parent_dentry_clus;
- p->dEntry_location_clus_offset = empty_fat32_dentry - (struct fat32_Directory_t *)tmp_dentry_clus_buf_addr;
-
-
-
- list_init(&dEntry->child_node_list);
- list_init(&dEntry->subdirs_list);
- dEntry->dir_ops = &fat32_dEntry_ops;
- dEntry->dir_inode = inode;
-
- uint32_t new_dir_clus;
- if (fat32_alloc_clusters(inode, &new_dir_clus, 1) != 0)
- {
- retval = -ENOSPC;
- goto fail;
- }
-
-
- fat32_fill_shortname(dEntry, empty_fat32_dentry, new_dir_clus);
-
- uint8_t short_dentry_ChkSum = fat32_ChkSum(empty_fat32_dentry->DIR_Name);
-
- fat32_fill_longname(dEntry, (struct fat32_LongDirectory_t *)(empty_fat32_dentry - 1), short_dentry_ChkSum, cnt_longname);
-
-
- ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, tmp_dentry_sector, fsbi->sec_per_clus, tmp_dentry_clus_buf_addr, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
-
- {
-
- void *buf = kmalloc(fsbi->bytes_per_clus, 0);
- struct fat32_Directory_t *new_dir_dentries = (struct fat32_Directory_t *)buf;
- memset((void *)new_dir_dentries, 0, fsbi->bytes_per_clus);
-
- new_dir_dentries->DIR_Attr = ATTR_DIRECTORY;
- new_dir_dentries->DIR_FileSize = 0;
- new_dir_dentries->DIR_Name[0] = '.';
- for (int i = 1; i < 11; ++i)
- new_dir_dentries->DIR_Name[i] = 0x20;
- new_dir_dentries->DIR_FstClusHI = empty_fat32_dentry->DIR_FstClusHI;
- new_dir_dentries->DIR_FstClusLO = empty_fat32_dentry->DIR_FstClusLO;
-
- ++new_dir_dentries;
- new_dir_dentries->DIR_Attr = ATTR_DIRECTORY;
- new_dir_dentries->DIR_FileSize = 0;
- new_dir_dentries->DIR_Name[0] = '.';
- new_dir_dentries->DIR_Name[1] = '.';
- for (int i = 2; i < 11; ++i)
- new_dir_dentries->DIR_Name[i] = 0x20;
- new_dir_dentries->DIR_FstClusHI = (unsigned short)(parent_inode_info->first_clus >> 16) & 0x0fff;
- new_dir_dentries->DIR_FstClusLO = (unsigned short)(parent_inode_info->first_clus) & 0xffff;
-
- uint64_t sector = fsbi->first_data_sector + (new_dir_clus - 2) * fsbi->sec_per_clus;
-
- ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
- }
-
-
-
- kfree((void *)tmp_dentry_clus_buf_addr);
- return 0;
- fail:;
-
- kfree((void *)tmp_dentry_clus_buf_addr);
- return retval;
- }
- int64_t fat32_rmdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry)
- {
- }
- 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)
- {
- }
- int64_t fat32_getAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
- {
- }
- int64_t fat32_setAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr)
- {
- }
- int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t filler)
- {
- struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)file_ptr->dEntry->dir_inode->private_inode_info;
- fat32_sb_info_t *fsbi = (fat32_sb_info_t *)file_ptr->dEntry->dir_inode->sb->private_sb_info;
- unsigned char *buf = (unsigned char *)kmalloc(fsbi->bytes_per_clus, 0);
- uint32_t cluster = finode->first_clus;
-
- int clus_num = file_ptr->position / fsbi->bytes_per_clus;
-
- for (int i = 0; i < clus_num; ++i)
- {
- cluster = fat32_read_FAT_entry(fsbi, cluster);
- if (cluster > 0x0ffffff7)
- {
- kerror("file position out of range! (cluster not exists)");
- return NULL;
- }
- }
- uint64_t dentry_type = 0;
- char *dir_name = NULL;
- int name_len = 0;
-
- while (cluster <= 0x0ffffff7)
- {
-
- uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
-
- if (AHCI_SUCCESS != ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num))
- {
-
- kerror("Failed to read the file's first sector.");
- kfree(buf);
- return NULL;
- }
- struct fat32_Directory_t *dentry = NULL;
- struct fat32_LongDirectory_t *long_dentry = NULL;
-
- dentry = (struct fat32_Directory_t *)(buf + file_ptr->position % fsbi->bytes_per_clus);
- name_len = 0;
-
- for (int i = file_ptr->position % fsbi->bytes_per_clus; i < fsbi->bytes_per_clus; i += 32, file_ptr->position += 32, ++dentry)
- {
-
- if (dentry->DIR_Attr == ATTR_LONG_NAME)
- continue;
-
- if (dentry->DIR_Name[0] == 0xe5 || dentry->DIR_Name[0] == 0x00 || dentry->DIR_Name[0] == 0x05)
- continue;
-
-
- long_dentry = (struct fat32_LongDirectory_t *)(dentry - 1);
-
- if (long_dentry->LDIR_Attr == ATTR_LONG_NAME && long_dentry->LDIR_Ord != 0xe5 && long_dentry->LDIR_Ord != 0x00 && long_dentry->LDIR_Ord != 0x05)
- {
- int count_long_dentry = 0;
-
- while (long_dentry->LDIR_Attr == ATTR_LONG_NAME && long_dentry->LDIR_Ord != 0xe5 && long_dentry->LDIR_Ord != 0x00 && long_dentry->LDIR_Ord != 0x05)
- {
- ++count_long_dentry;
- if (long_dentry->LDIR_Ord & 0x40)
- break;
- --long_dentry;
- }
-
- dir_name = (char *)kmalloc(count_long_dentry * 26 + 1, 0);
- memset(dir_name, 0, count_long_dentry * 26 + 1);
-
- long_dentry = (struct fat32_LongDirectory_t *)(dentry - 1);
- name_len = 0;
-
- for (int j = 0; j < count_long_dentry; ++j, --long_dentry)
- {
-
- for (int k = 0; k < 5; ++k)
- {
- if (long_dentry->LDIR_Name1[k] != 0xffff && long_dentry->LDIR_Name1[k] != 0x0000)
- dir_name[name_len++] = (char)long_dentry->LDIR_Name1[k];
- }
-
- for (int k = 0; k < 6; ++k)
- {
- if (long_dentry->LDIR_Name2[k] != 0xffff && long_dentry->LDIR_Name2[k] != 0x0000)
- dir_name[name_len++] = (char)long_dentry->LDIR_Name2[k];
- }
-
- for (int k = 0; k < 2; ++k)
- {
- if (long_dentry->LDIR_Name3[k] != 0xffff && long_dentry->LDIR_Name3[k] != 0x0000)
- dir_name[name_len++] = (char)long_dentry->LDIR_Name3[k];
- }
- }
-
- dentry_type = dentry->DIR_Attr;
- goto find_dir_success;
- }
- else
- {
- dir_name = (char *)kmalloc(15, 0);
- memset(dir_name, 0, 15);
- name_len = 0;
- int total_len = 0;
-
- for (int j = 0; j < 8; ++j, ++total_len)
- {
- if (dentry->DIR_Name[j] == ' ')
- break;
- if (dentry->DIR_NTRes & LOWERCASE_BASE)
- dir_name[name_len++] = dentry->DIR_Name[j] + 32;
- else
- dir_name[name_len++] = dentry->DIR_Name[j];
- }
-
- if (dentry->DIR_Attr & ATTR_DIRECTORY)
- {
- dentry_type = dentry->DIR_Attr;
- goto find_dir_success;
- }
-
- dir_name[name_len++] = '.';
-
-
- for (int j = 0; j < 3; ++j, ++total_len)
- {
- if (dentry->DIR_Name[j] == ' ')
- break;
- if (dentry->DIR_NTRes & LOWERCASE_BASE)
- dir_name[name_len++] = dentry->DIR_Name[j] + 32;
- else
- dir_name[name_len++] = dentry->DIR_Name[j];
- }
- if (total_len == 8)
- dir_name[--name_len] = '\0';
- dentry_type = dentry->DIR_Attr;
- goto find_dir_success;
- }
- }
-
- cluster = fat32_read_FAT_entry(fsbi, cluster);
- }
- kfree(buf);
-
- return NULL;
- find_dir_success:;
-
- file_ptr->position += 32;
-
- if (dentry_type & ATTR_DIRECTORY)
- dentry_type = VFS_ATTR_DIR;
- else
- dentry_type = VFS_ATTR_FILE;
- return filler(dirent, 0, dir_name, name_len, dentry_type, 0);
- }
- struct vfs_inode_operations_t fat32_inode_ops =
- {
- .create = fat32_create,
- .mkdir = fat32_mkdir,
- .rmdir = fat32_rmdir,
- .lookup = fat32_lookup,
- .rename = fat32_rename,
- .getAttr = fat32_getAttr,
- .setAttr = fat32_setAttr,
- };
- struct vfs_filesystem_type_t fat32_fs_type =
- {
- .name = "FAT32",
- .fs_flags = 0,
- .read_superblock = fat32_read_superblock,
- .next = NULL,
- };
- void fat32_init()
- {
- kinfo("Initializing FAT32...");
-
- vfs_register_filesystem(&fat32_fs_type);
-
- vfs_root_sb = fat32_register_partition(0, 0, 0);
- kinfo("FAT32 initialized.");
- }
|