X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=KernelLand%2FModules%2FFilesystems%2FFAT%2Ffat.c;h=be0a51c7f20e9a0b77e37b76a43eb7b6788ed89b;hb=f0b5018caef356cda6afa147ddb6917068c62dd7;hp=bb90c3cfd49a7b1fbc8d1792f776e28685cf912f;hpb=97159caf60a26cff3cc8f52e050a44d2492430f8;p=tpg%2Facess2.git diff --git a/KernelLand/Modules/Filesystems/FAT/fat.c b/KernelLand/Modules/Filesystems/FAT/fat.c index bb90c3cf..be0a51c7 100644 --- a/KernelLand/Modules/Filesystems/FAT/fat.c +++ b/KernelLand/Modules/Filesystems/FAT/fat.c @@ -17,7 +17,7 @@ * \todo Implement changing of the parent directory when a file is written to * \todo Implement file creation / deletion */ -#define DEBUG 1 +#define DEBUG 0 #define VERBOSE 1 #include @@ -27,6 +27,7 @@ // === PROTOTYPES === // --- Driver Core int FAT_Install(char **Arguments); + int FAT_Detect(int FD); tVFS_Node *FAT_InitDevice(const char *device, const char **options); void FAT_Unmount(tVFS_Node *Node); // --- Helpers @@ -46,7 +47,13 @@ void FAT_CloseFile(tVFS_Node *node); MODULE_DEFINE(0, VER2(0,80) /*v0.80*/, VFAT, FAT_Install, NULL, NULL); tFAT_VolInfo gFAT_Disks[8]; int giFAT_PartCount = 0; -tVFS_Driver gFAT_FSInfo = {"fat", 0, FAT_InitDevice, FAT_Unmount, FAT_GetNodeFromINode, NULL}; +tVFS_Driver gFAT_FSInfo = { + .Name = "fat", + .Detect = FAT_Detect, + .InitDevice = FAT_InitDevice, + .Unmount = FAT_Unmount, + .GetNodeFromINode = FAT_GetNodeFromINode +}; tVFS_NodeType gFAT_DirType = { .TypeName = "FAT-Dir", .ReadDir = FAT_ReadDir, @@ -78,6 +85,26 @@ int FAT_Install(char **Arguments) return MODULE_ERR_OK; } +/** + * \brief Detect if a file is a FAT device + */ +int FAT_Detect(int FD) +{ + fat_bootsect bs; + + if( VFS_ReadAt(FD, 0, 512, &bs) != 512) { + return 0; + } + + if(bs.bps == 0 || bs.spc == 0) + return 0; + + Log_Debug("FAT", "_Detect: Media type = %02x", bs.mediaDesc); + if( bs.mediaDesc < 0xF0 ) + return 0; + + return 1; +} /** * \brief Reads the boot sector of a disk and prepares the structures for it */ @@ -105,6 +132,7 @@ tVFS_Node *FAT_InitDevice(const char *Device, const char **Options) if(bs->bps == 0 || bs->spc == 0) { Log_Notice("FAT", "Error in FAT Boot Sector (zero BPS/SPC)"); + VFS_Close(diskInfo->fileHandle); return NULL; } @@ -185,6 +213,7 @@ tVFS_Node *FAT_InitDevice(const char *Device, const char **Options) diskInfo->FATCache = (Uint32*)malloc(sizeof(Uint32)*diskInfo->ClusterCount); if(diskInfo->FATCache == NULL) { Log_Warning("FAT", "Heap Exhausted"); + VFS_Cose(diskInfo->fileHandle); return NULL; } Ofs = bs->resvSectCount*512; @@ -309,7 +338,7 @@ int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Clu if(Cluster) *Cluster = cluster; cluster = FAT_int_GetFatValue(disk, cluster); // Check for end of cluster chain - if(cluster == 0xFFFFFFFF) { LEAVE('i', 1); return 1;} + if(cluster == GETFATVALUE_EOC) { LEAVE('i', 1); return 1; } } if(Cluster) *Cluster = cluster; } @@ -392,7 +421,7 @@ size_t FAT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer) for(i = preSkip; i--; ) { cluster = FAT_int_GetFatValue(disk, cluster); - if(cluster == -1) { + if(cluster == GETFATVALUE_EOC) { Log_Warning("FAT", "Offset is past end of cluster chain mark"); LEAVE('i', 0); return 0; @@ -418,7 +447,7 @@ size_t FAT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer) LOG("pos = %i, Reading the rest of the clusters"); // Get next cluster in the chain cluster = FAT_int_GetFatValue(disk, cluster); - if(cluster == -1) { + if(cluster == GETFATVALUE_EOC) { Log_Warning("FAT", "Read past End of Cluster Chain (Align)"); LEAVE('X', pos); return pos; @@ -436,7 +465,7 @@ size_t FAT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer) // Read the rest of the cluster data for( ; count; count -- ) { - if(cluster == -1) { + if(cluster == GETFATVALUE_EOC) { Log_Warning("FAT", "Read past End of Cluster Chain (Bulk)"); LEAVE('X', pos); return pos; @@ -494,7 +523,7 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe while( Offset > disk->BytesPerCluster ) { cluster = FAT_int_GetFatValue( disk, cluster ); - if(cluster == -1) { + if(cluster == GETFATVALUE_EOC) { Log_Warning("FAT", "EOC Unexpectedly Reached"); LEAVE('i', 0); return 0; @@ -540,7 +569,7 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe // Get next cluster (allocating if needed) tmpCluster = FAT_int_GetFatValue(disk, cluster); - if(tmpCluster == -1) { + if(tmpCluster == GETFATVALUE_EOC) { tmpCluster = FAT_int_AllocateCluster(disk, cluster); if( tmpCluster == 0 ) goto ret_incomplete; @@ -556,7 +585,7 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe // Get next cluster (allocating if needed) tmpCluster = FAT_int_GetFatValue(disk, cluster); - if(tmpCluster == -1) { + if(tmpCluster == GETFATVALUE_EOC) { bNewCluster = 1; tmpCluster = FAT_int_AllocateCluster(disk, cluster); if( tmpCluster == 0 )