Modules/FAT - Fixed handling of <FAT32 root directory
[tpg/acess2.git] / KernelLand / Modules / Filesystems / FAT / fat.c
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