X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fvfs%2Ffs%2Ffat.c;h=fab8965195621445a76d8af32d8cb5eb505f0b28;hb=47e9dfd89189fc6b150bd6b20229cb047c7e0858;hp=8cfeca448502fb2ba1de0b6b530556629f20d06d;hpb=00f24d0160b5ada315a6338c2ac6062deb2880ed;p=tpg%2Facess2.git diff --git a/Kernel/vfs/fs/fat.c b/Kernel/vfs/fs/fat.c index 8cfeca44..fab89651 100644 --- a/Kernel/vfs/fs/fat.c +++ b/Kernel/vfs/fs/fat.c @@ -1,31 +1,19 @@ /* - * Acess2 + * Acess 2 * FAT12/16/32 Driver Version (Incl LFN) */ -//INCLUDES -#include -#include -#include -#include "fs_fat.h" - #define DEBUG 0 #define VERBOSE 1 -#if DEBUG -# define DEBUGS(v...) Log(v) -#else -# define DEBUGS(v...) -# undef ENTER -# undef LOG -# undef LEAVE -# define ENTER(...) -# define LOG(...) -# define LEAVE(...) -#endif - #define CACHE_FAT 1 //!< Caches the FAT in memory #define USE_LFN 1 //!< Enables the use of Long File Names +#include +#include +#include +#include "fs_fat.h" + + // === TYPES === #if USE_LFN typedef struct s_lfncache { @@ -38,7 +26,7 @@ typedef struct s_lfncache { // === PROTOTYPES === int FAT_Install(char **Arguments); -tVFS_Node *FAT_InitDevice(char *device, char *options); +tVFS_Node *FAT_InitDevice(char *device, char **options); void FAT_Unmount(tVFS_Node *Node); Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer); Uint64 FAT_Write(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer); @@ -49,12 +37,9 @@ tVFS_Node *FAT_FindDir(tVFS_Node *dirNode, char *file); void FAT_CloseFile(tVFS_Node *node); // === SEMI-GLOBALS === -MODULE_DEFINE(0, 0x5B /*v0.90*/, FAT32, FAT_Install, NULL); +MODULE_DEFINE(0, 0x51 /*v0.80*/, FAT32, FAT_Install, NULL, NULL); tFAT_VolInfo gFAT_Disks[8]; int giFAT_PartCount = 0; -#if CACHE_FAT -Uint32 *fat_cache[8]; -#endif #if USE_LFN t_lfncache *fat_lfncache; #endif @@ -73,9 +58,11 @@ int FAT_Install(char **Arguments) return 0; } -/* Reads the boot sector of a disk and prepares the structures for it +/** + * \fn tVFS_Node *FAT_InitDevice(char *Device, char **options) + * \brief Reads the boot sector of a disk and prepares the structures for it */ -tVFS_Node *FAT_InitDevice(char *Device, char *options) +tVFS_Node *FAT_InitDevice(char *Device, char **options) { fat_bootsect *bs; int i; @@ -146,7 +133,6 @@ tVFS_Node *FAT_InitDevice(char *Device, char *options) #endif //Get Name - //puts(" Name: "); if(diskInfo->type == FAT32) { for(i=0;i<11;i++) diskInfo->name[i] = (bs->spec.fat32.label[i] == ' ' ? '\0' : bs->spec.fat32.label[i]); @@ -156,7 +142,6 @@ tVFS_Node *FAT_InitDevice(char *Device, char *options) diskInfo->name[i] = (bs->spec.fat16.label[i] == ' ' ? '\0' : bs->spec.fat16.label[i]); } diskInfo->name[11] = '\0'; - //puts(diskInfo->name); putch('\n'); //Compute Root directory offset if(diskInfo->type == FAT32) @@ -172,8 +157,8 @@ tVFS_Node *FAT_InitDevice(char *Device, char *options) #if CACHE_FAT { Uint32 Ofs; - fat_cache[ giFAT_PartCount ] = (Uint32*)malloc(sizeof(Uint32)*CountofClusters); - if(fat_cache[giFAT_PartCount] == NULL) { + diskInfo->FATCache = (Uint32*)malloc(sizeof(Uint32)*CountofClusters); + if(diskInfo->FATCache == NULL) { Warning("FAT_InitDisk - Heap Exhausted\n"); return NULL; } @@ -189,8 +174,8 @@ tVFS_Node *FAT_InitDevice(char *Device, char *options) Ofs += 3*512; } val = *((int*)(buf+j*3)); - fat_cache[giFAT_PartCount][i*2] = val & 0xFFF; - fat_cache[giFAT_PartCount][i*2+1] = (val>>12) & 0xFFF; + diskInfo->FATCache[i*2] = val & 0xFFF; + diskInfo->FATCache[i*2+1] = (val>>12) & 0xFFF; } } if(diskInfo->type == FAT16) { @@ -200,7 +185,7 @@ tVFS_Node *FAT_InitDevice(char *Device, char *options) VFS_ReadAt(diskInfo->fileHandle, Ofs, 512, buf); Ofs += 512; } - fat_cache[giFAT_PartCount][i] = buf[i&255]; + diskInfo->FATCache[i] = buf[i&255]; } } if(diskInfo->type == FAT32) { @@ -210,30 +195,26 @@ tVFS_Node *FAT_InitDevice(char *Device, char *options) VFS_ReadAt(diskInfo->fileHandle, Ofs, 512, buf); Ofs += 512; } - fat_cache[giFAT_PartCount][i] = buf[i&127]; + diskInfo->FATCache[i] = buf[i&127]; } } - DEBUGS(" FAT_InitDisk: FAT Fully Cached\n"); + LOG("FAT Fully Cached"); } #endif /*CACHE_FAT*/ //Initalise inode cache for FAT - gFAT_Disks[giFAT_PartCount].inodeHandle = Inode_GetHandle(); + diskInfo->inodeHandle = Inode_GetHandle(); + LOG("Inode Cache handle is %i", diskInfo->inodeHandle); - #if DEBUG - Log(" FAT_InitDisk: Inode Cache handle is %i\n", gFAT_Disks[giFAT_PartCount].inodeHandle); - #endif - - //== VFS Interface - node = &gFAT_Disks[giFAT_PartCount].rootNode; - //node->Name = gFAT_Disks[giFAT_PartCount].name; + // == VFS Interface + node = &diskInfo->rootNode; node->Inode = diskInfo->rootOffset; - node->Size = bs->files_in_root; //Unknown - To be set on readdir + node->Size = bs->files_in_root; // Unknown - To be set on readdir node->ImplInt = giFAT_PartCount; node->ReferenceCount = 1; - node->UID = 0; node->GID= 0; + node->UID = 0; node->GID = 0; node->NumACLs = 1; node->ACLs = &gVFS_ACL_EveryoneRWX; node->Flags = VFS_FFLAG_DIRECTORY; @@ -266,23 +247,26 @@ void FAT_Unmount(tVFS_Node *Node) } /** - * \fn static Uint32 FAT_int_GetFatValue(int handle, Uint32 cluster) + * \fn static Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster) * \brief Fetches a value from the FAT */ -static Uint32 FAT_int_GetFatValue(int handle, Uint32 cluster) +static Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster) { - Uint32 val; + Uint32 val = 0; + #if !CACHE_FAT + Uint32 ofs = Disk->bootsect.resvSectCount*512; + #endif ENTER("iHandle xCluster", handle, cluster); #if CACHE_FAT - val = fat_cache[handle][cluster]; + val = Disk->FATCache[cluster]; #else - if(gFAT_Disks[handle].type == FAT12) { - VFS_ReadAt(gFAT_Disks[handle].fileHandle, 512+(cluster&~1)*3, 3, &val); + if(Disk->type == FAT12) { + VFS_ReadAt(Disk->fileHandle, ofs+(cluster>>1)*3, 3, &val); val = (cluster&1 ? val&0xFFF : val>>12); - } else if(gFAT_Disks[handle].type == FAT16) { - VFS_ReadAt(gFAT_Disks[handle].fileHandle, 512+cluster*2, 2, &val); + } else if(Disk->type == FAT16) { + VFS_ReadAt(Disk->fileHandle, ofs+cluster*2, 2, &val); } else { - VFS_ReadAt(gFAT_Disks[handle].fileHandle, 512+cluster*4, 4, &val); + VFS_ReadAt(Disk->fileHandle, ofs+cluster*4, 4, &val); } #endif /*CACHE_FAT*/ LEAVE('x', val); @@ -293,9 +277,7 @@ static Uint32 FAT_int_GetFatValue(int handle, Uint32 cluster) */ static void FAT_int_ReadCluster(int Handle, Uint32 Cluster, int Length, void *Buffer) { - #if DEBUG ENTER("iHandle xCluster iLength pBuffer", Handle, Cluster, Length, Buffer); - #endif VFS_ReadAt( gFAT_Disks[Handle].fileHandle, (gFAT_Disks[Handle].firstDataSect + (Cluster-2)*gFAT_Disks[Handle].bootsect.spc ) @@ -303,9 +285,7 @@ static void FAT_int_ReadCluster(int Handle, Uint32 Cluster, int Length, void *Bu Length, Buffer ); - #if DEBUG LEAVE('-'); - #endif } /** @@ -343,6 +323,19 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer) return 0; } + // Sanity Check offset + if(offset > node->Size) { + //LOG("Reading past EOF (%i > %i)", offset, node->Size); + LEAVE('i', 0); + return 0; + } + // Clamp Size + if(offset + length > node->Size) { + //LOG("Reading past EOF (%lli + %lli > %lli), clamped to %lli", + // offset, length, node->Size, node->Size - offset); + length = node->Size - offset; + } + // Single Cluster including offset if(length + offset < bpc) { @@ -350,14 +343,14 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer) memcpy( buffer, (void*)( tmpBuf + offset%bpc ), length ); free(tmpBuf); LEAVE('i', 1); - return 1; + return length; } preSkip = offset / bpc; //Skip previous clusters for(i=preSkip;i--;) { - cluster = FAT_int_GetFatValue(handle, cluster); + cluster = FAT_int_GetFatValue(disk, cluster); if(cluster == eocMarker) { Warning("FAT_Read - Offset is past end of cluster chain mark"); } @@ -380,10 +373,10 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer) if (count == 1) { free(tmpBuf); LEAVE('i', 1); - return 1; + return length; } - cluster = FAT_int_GetFatValue(handle, cluster); + cluster = FAT_int_GetFatValue(disk, cluster); #if DEBUG LOG("pos=%i\n", pos); @@ -397,7 +390,7 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer) FAT_int_ReadCluster(handle, cluster, bpc, tmpBuf); memcpy((void*)(buffer+pos), tmpBuf, bpc); pos += bpc; - cluster = FAT_int_GetFatValue(handle, cluster); + cluster = FAT_int_GetFatValue(disk, cluster); if(cluster == eocMarker) { Warning("FAT_Read - Read past End of Cluster Chain"); free(tmpBuf); @@ -490,8 +483,6 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *parent, fat_filetable *ft, char *LongFi ENTER("pParent pFT sLongFileName", parent, ft, LongFileName); - // Get Name - //node.Name = FAT_int_CreateName(parent, ft, LongFileName); // Set Other Data node.Inode = ft->cluster | (ft->clusterHi<<16); node.Size = ft->size; @@ -530,6 +521,7 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *parent, fat_filetable *ft, char *LongFi node.ReadDir = FAT_ReadDir; node.FindDir = FAT_FindDir; node.MkNod = FAT_Mknod; + node.Size = -1; } else { node.Read = FAT_Read; node.Write = FAT_Write; @@ -629,15 +621,17 @@ char *FAT_ReadDir(tVFS_Node *dirNode, int dirpos) { //Skip previous clusters for(a=preSkip;a--;) { - cluster = FAT_int_GetFatValue(dirNode->ImplInt, cluster); + cluster = FAT_int_GetFatValue(disk, cluster); } } // Check for end of cluster chain if((disk->type == FAT12 && cluster == EOC_FAT12) || (disk->type == FAT16 && cluster == EOC_FAT16) - || (disk->type == FAT32 && cluster == EOC_FAT32)) + || (disk->type == FAT32 && cluster == EOC_FAT32)) { + LEAVE('n'); return NULL; + } // Bounds Checking (Used to spot heap overflows) if(cluster > disk->clusterCount + 2) @@ -667,16 +661,16 @@ char *FAT_ReadDir(tVFS_Node *dirNode, int dirpos) // Offset in sector a = dirpos & 0xF; - LOG("offset=%i, a=%i\n", (Uint)offset, a); + LOG("offset=%i, a=%i", (Uint)offset, a); // Read Sector VFS_ReadAt(disk->fileHandle, offset*512, 512, fileinfo); // Read Dir Data - LOG("name[0] = 0x%x\n", (Uint8)fileinfo[a].name[0]); + LOG("name[0] = 0x%x", (Uint8)fileinfo[a].name[0]); //Check if this is the last entry if(fileinfo[a].name[0] == '\0') { dirNode->Size = dirpos; - LOG("End of list\n"); + LOG("End of list"); LEAVE('n'); return NULL; // break } @@ -701,7 +695,7 @@ char *FAT_ReadDir(tVFS_Node *dirNode, int dirpos) // Get the current length len = strlen(lfn); - // Sanity Check (FAT implementations do not allow >255 bytes) + // Sanity Check (FAT implementations should not allow >255 bytes) if(len + 13 > 255) return VFS_SKIP; // Rebase all bytes for(a=len+1;a--;) lfn[a+13] = lfn[a]; @@ -862,7 +856,7 @@ tVFS_Node *FAT_FindDir(tVFS_Node *node, char *name) { if( dirCluster == disk->rootOffset && disk->type != FAT32 ) continue; - dirCluster = FAT_int_GetFatValue(node->ImplInt, dirCluster); + dirCluster = FAT_int_GetFatValue(disk, dirCluster); diskOffset = (disk->firstDataSect+(dirCluster-2)*disk->bootsect.spc)*512; } }