Modules/FAT - Fixed handling of <FAT32 root directory
authorJohn Hodge (sonata) <[email protected]>
Mon, 13 May 2013 07:11:51 +0000 (15:11 +0800)
committerJohn Hodge (sonata) <[email protected]>
Mon, 13 May 2013 07:11:51 +0000 (15:11 +0800)
KernelLand/Modules/Filesystems/FAT/common.h
KernelLand/Modules/Filesystems/FAT/dir.c
KernelLand/Modules/Filesystems/FAT/fat.c

index 3983b93..2e9a232 100644 (file)
@@ -43,6 +43,7 @@ struct sFAT_VolInfo
        char    name[12];       //!< Volume Name (With NULL Terminator)
        Uint32  firstDataSect;  //!< First data sector
        Uint32  rootOffset;     //!< Root Offset (clusters)
+       Uint32  RootSector;     //!< Root Offset (sectors)
        Uint32  ClusterCount;   //!< Total Cluster Count
        fat_bootsect    bootsect;       //!< Boot Sector
        tVFS_Node       rootNode;       //!< Root Node
index c7268d7..cb2c6f3 100644 (file)
@@ -83,6 +83,7 @@ int FAT_int_CreateName(fat_filetable *ft, const Uint16 *LongFileName, char *Dest
        {
                 int    len = FAT_int_ConvertUTF16_to_UTF8(NULL, LongFileName);
                if( len > FILENAME_MAX ) {
+                       LEAVE('i', -1);
                        return -1;
                }
                FAT_int_ConvertUTF16_to_UTF8((Uint8*)Dest, LongFileName);
@@ -94,6 +95,7 @@ int FAT_int_CreateName(fat_filetable *ft, const Uint16 *LongFileName, char *Dest
        #if USE_LFN
        }
        #endif
+       LEAVE('i', 0);
        return 0;
 }
 
index be0a51c..fc7d21e 100644 (file)
@@ -200,8 +200,10 @@ tVFS_Node *FAT_InitDevice(const char *Device, const char **Options)
        // Compute Root directory offset\r
        if(diskInfo->type == FAT32)\r
                diskInfo->rootOffset = bs->spec.fat32.rootClust;\r
-       else\r
+       else {\r
+               diskInfo->RootSector = FATSz * bs->fatCount;\r
                diskInfo->rootOffset = (FATSz * bs->fatCount) / bs->spc;\r
+       }\r
        \r
        diskInfo->firstDataSect = bs->resvSectCount + (bs->fatCount * FATSz) + RootDirSectors;\r
        \r
@@ -325,31 +327,43 @@ int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Clu
        \r
        cluster = base_cluster = Node->Inode & 0xFFFFFFF;       // Cluster ID\r
 //     LOG("base cluster = 0x%07x", cluster);\r
-       \r
-       // Do Cluster Skip\r
+\r
+       // Handle root directory\r
        // - Pre FAT32 had a reserved area for the root.\r
-       if( disk->type == FAT32 || cluster != disk->rootOffset )\r
+       if( disk->type != FAT32 && Node == &disk->rootNode )\r
        {\r
-               skip = Offset / disk->BytesPerCluster;\r
-               LOG("skip = %i", skip);\r
-               // Skip previous clusters\r
-               for(; skip-- ; )\r
-               {\r
-                       if(Cluster)     *Cluster = cluster;\r
-                       cluster = FAT_int_GetFatValue(disk, cluster);\r
-                       // Check for end of cluster chain\r
-                       if(cluster == GETFATVALUE_EOC) { LEAVE('i', 1); return 1; }\r
+               Uint32  root_byte_count = disk->bootsect.files_in_root * 32 ;\r
+               if( Offset >= root_byte_count ) {\r
+                       LOG("FAT12/16 root out of range (%i >= %i)", Offset, root_byte_count);\r
+                       LEAVE('i', 1);\r
+                       return 1;\r
                }\r
-               if(Cluster)     *Cluster = cluster;\r
+               LOG("FAT12/16 root");\r
+               \r
+               // Calculate address\r
+               addr = (disk->bootsect.resvSectCount + disk->RootSector) * disk->bootsect.bps;\r
+               addr += Offset;\r
+\r
+               LOG("addr = %llx", addr);\r
+               *Addr = addr;\r
+               LEAVE('i', 0);\r
+               return 0;\r
        }\r
-       else {\r
-               // TODO: Bounds checking on root\r
-//             LOG("Root cluster count %i", disk->bootsect.files_in_root*32/disk->BytesPerCluster);\r
-               // Increment by clusters in offset\r
-               cluster += Offset / disk->BytesPerCluster;\r
+\r
+       // Do Cluster Skip\r
+       skip = Offset / disk->BytesPerCluster;\r
+       LOG("skip = %i", skip);\r
+       // Skip previous clusters\r
+       for(; skip-- ; )\r
+       {\r
+               if(Cluster)     *Cluster = cluster;\r
+               cluster = FAT_int_GetFatValue(disk, cluster);\r
+               // Check for end of cluster chain\r
+               if(cluster == GETFATVALUE_EOC) { LEAVE('i', 1); return 1; }\r
        }\r
+       if(Cluster)     *Cluster = cluster;\r
        \r
-//     LOG("cluster = 0x%07x", cluster);\r
+       LOG("cluster = 0x%07x", cluster);\r
        \r
        // Bounds Checking (Used to spot corruption)\r
        if(cluster > disk->ClusterCount + 2)\r
@@ -361,15 +375,8 @@ int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Clu
        }\r
        \r
        // Compute Offsets\r
-       // - Pre FAT32 cluster base (in sectors)\r
-       if( base_cluster == disk->rootOffset && disk->type != FAT32 ) {\r
-               addr = disk->bootsect.resvSectCount * disk->bootsect.bps;\r
-               addr += cluster * disk->BytesPerCluster;\r
-       }\r
-       else {\r
-               addr = disk->firstDataSect * disk->bootsect.bps;\r
-               addr += (cluster - 2) * disk->BytesPerCluster;\r
-       }\r
+       addr = disk->firstDataSect * disk->bootsect.bps;\r
+       addr += (cluster - 2) * disk->BytesPerCluster;\r
        // In-cluster offset\r
        addr += Offset % disk->BytesPerCluster;\r
        \r

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