Various Changes
[tpg/acess2.git] / Kernel / vfs / fs / fat.c
index 87b5d87..d70fae0 100644 (file)
@@ -1,28 +1,15 @@
 /*\r
- * Acess2\r
+ * Acess 2\r
  * FAT12/16/32 Driver Version (Incl LFN)\r
  */\r
-//INCLUDES\r
+#define DEBUG  0\r
 #include <common.h>\r
 #include <modules.h>\r
 #include <vfs.h>\r
 #include "fs_fat.h"\r
 \r
-#define DEBUG  1\r
 #define VERBOSE        1\r
 \r
-#if DEBUG\r
-# define DEBUGS(v...)  Log(v)\r
-#else\r
-# define DEBUGS(v...)\r
-# undef ENTER\r
-# undef LOG\r
-# undef LEAVE\r
-# define ENTER(...)\r
-# define LOG(...)\r
-# define LEAVE(...)\r
-#endif\r
-\r
 #define CACHE_FAT      1       //!< Caches the FAT in memory\r
 #define USE_LFN                1       //!< Enables the use of Long File Names\r
 \r
@@ -38,7 +25,7 @@ typedef struct s_lfncache {
 \r
 // === PROTOTYPES ===\r
  int   FAT_Install(char **Arguments);\r
-tVFS_Node      *FAT_InitDevice(char *device, char *options);\r
+tVFS_Node      *FAT_InitDevice(char *device, char **options);\r
 void   FAT_Unmount(tVFS_Node *Node);\r
 Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer);\r
 Uint64 FAT_Write(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer);\r
@@ -49,7 +36,7 @@ tVFS_Node     *FAT_FindDir(tVFS_Node *dirNode, char *file);
 void   FAT_CloseFile(tVFS_Node *node);\r
 \r
 // === SEMI-GLOBALS ===\r
-MODULE_DEFINE(0, 0x5B /*v0.90*/, FAT32, FAT_Install, NULL);\r
+MODULE_DEFINE(0, 0x51 /*v0.80*/, FAT32, FAT_Install, NULL);\r
 tFAT_VolInfo   gFAT_Disks[8];\r
  int   giFAT_PartCount = 0;\r
 #if CACHE_FAT\r
@@ -74,10 +61,10 @@ int FAT_Install(char **Arguments)
 }\r
 \r
 /**\r
- * \fn tVFS_Node *FAT_InitDevice(char *Device, char *options)\r
+ * \fn tVFS_Node *FAT_InitDevice(char *Device, char **options)\r
  * \brief Reads the boot sector of a disk and prepares the structures for it\r
  */\r
-tVFS_Node *FAT_InitDevice(char *Device, char *options)\r
+tVFS_Node *FAT_InitDevice(char *Device, char **options)\r
 {\r
        fat_bootsect *bs;\r
         int    i;\r
@@ -213,16 +200,13 @@ tVFS_Node *FAT_InitDevice(char *Device, char *options)
                        fat_cache[giFAT_PartCount][i] = buf[i&127];\r
                }\r
        }\r
-       DEBUGS(" FAT_InitDisk: FAT Fully Cached\n");\r
+       LOG("FAT Fully Cached");\r
        }\r
        #endif /*CACHE_FAT*/\r
        \r
        //Initalise inode cache for FAT\r
        gFAT_Disks[giFAT_PartCount].inodeHandle = Inode_GetHandle();\r
-       \r
-       #if DEBUG\r
-               Log(" FAT_InitDisk: Inode Cache handle is %i\n", gFAT_Disks[giFAT_PartCount].inodeHandle);\r
-       #endif\r
+       LOG("Inode Cache handle is %i", gFAT_Disks[giFAT_PartCount].inodeHandle);\r
        \r
        // == VFS Interface\r
        node = &gFAT_Disks[giFAT_PartCount].rootNode;\r
@@ -292,9 +276,7 @@ static Uint32 FAT_int_GetFatValue(int handle, Uint32 cluster)
  */\r
 static void FAT_int_ReadCluster(int Handle, Uint32 Cluster, int Length, void *Buffer)\r
 {\r
-       #if DEBUG\r
        ENTER("iHandle xCluster iLength pBuffer", Handle, Cluster, Length, Buffer);\r
-       #endif\r
        VFS_ReadAt(\r
                gFAT_Disks[Handle].fileHandle,\r
                (gFAT_Disks[Handle].firstDataSect + (Cluster-2)*gFAT_Disks[Handle].bootsect.spc )\r
@@ -302,9 +284,7 @@ static void FAT_int_ReadCluster(int Handle, Uint32 Cluster, int Length, void *Bu
                Length,\r
                Buffer\r
                );\r
-       #if DEBUG\r
        LEAVE('-');\r
-       #endif\r
 }\r
 \r
 /**\r
@@ -342,6 +322,18 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
                return 0;\r
        }\r
        \r
+       // Sanity Check offset\r
+       if(offset > node->Size) {\r
+               //Log("FAT_Read: Reading past EOF (%i > %i)", offset, node->Size);\r
+               return 0;\r
+       }\r
+       // Clamp Size\r
+       if(offset + length > node->Size) {\r
+               //Log("FAT_Read: Reading past EOF (%lli + %lli > %lli), clamped to %lli",\r
+               //      offset, length, node->Size, node->Size - offset);\r
+               length = node->Size - offset;\r
+       }\r
+       \r
        // Single Cluster including offset\r
        if(length + offset < bpc)\r
        {\r
@@ -349,7 +341,7 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
                memcpy( buffer, (void*)( tmpBuf + offset%bpc ), length );\r
                free(tmpBuf);\r
                LEAVE('i', 1);\r
-               return 1;\r
+               return length;\r
        }\r
        \r
        preSkip = offset / bpc;\r
@@ -379,7 +371,7 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
        if (count == 1) {\r
                free(tmpBuf);\r
                LEAVE('i', 1);\r
-               return 1;\r
+               return length;\r
        }\r
        \r
        cluster = FAT_int_GetFatValue(handle, cluster);\r
@@ -665,16 +657,16 @@ char *FAT_ReadDir(tVFS_Node *dirNode, int dirpos)
        // Offset in sector\r
        a = dirpos & 0xF;\r
 \r
-       LOG("offset=%i, a=%i\n", (Uint)offset, a);\r
+       LOG("offset=%i, a=%i", (Uint)offset, a);\r
        \r
        // Read Sector\r
        VFS_ReadAt(disk->fileHandle, offset*512, 512, fileinfo);        // Read Dir Data\r
        \r
-       LOG("name[0] = 0x%x\n", (Uint8)fileinfo[a].name[0]);\r
+       LOG("name[0] = 0x%x", (Uint8)fileinfo[a].name[0]);\r
        //Check if this is the last entry\r
        if(fileinfo[a].name[0] == '\0') {\r
                dirNode->Size = dirpos;\r
-               LOG("End of list\n");\r
+               LOG("End of list");\r
                LEAVE('n');\r
                return NULL;    // break\r
        }\r
@@ -743,7 +735,7 @@ char *FAT_ReadDir(tVFS_Node *dirNode, int dirpos)
        #endif\r
        \r
        LEAVE('s', ret);\r
-       return VFS_FREEPLZ(ret);\r
+       return ret;\r
 }\r
 \r
 /**\r

UCC git Repository :: git.ucc.asn.au