/*\r
* Acess 2\r
* FAT12/16/32 Driver Version (Incl LFN)\r
+ * \r
+ * NOTE: This driver will only support _reading_ long file names, not\r
+ * writing. I don't even know why i'm adding write-support. FAT sucks.\r
+ * \r
+ * Known Bugs:\r
+ * - LFN Is buggy in FAT_ReadDir\r
*/\r
/**\r
* \todo Implement changing of the parent directory when a file is written to\r
* \todo Implement file creation / deletion\r
*/\r
-#define DEBUG 0\r
+#define DEBUG 1\r
#define VERBOSE 1\r
\r
#define CACHE_FAT 1 //!< Caches the FAT in memory\r
#define USE_LFN 1 //!< Enables the use of Long File Names\r
+#define SUPPORT_WRITE 0\r
\r
#include <acess.h>\r
#include <modules.h>\r
Uint32 FAT_int_AllocateCluster(tFAT_VolInfo *Disk, Uint32 Previous);\r
\r
void FAT_int_ReadCluster(tFAT_VolInfo *Disk, Uint32 Cluster, int Length, void *Buffer);\r
-void FAT_int_WriteCluster(tFAT_VolInfo *Disk, Uint32 Cluster, void *Buffer);\r
-\r
Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);\r
+#if SUPPORT_WRITE\r
+void FAT_int_WriteCluster(tFAT_VolInfo *Disk, Uint32 Cluster, void *Buffer);\r
Uint64 FAT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);\r
+#endif\r
+\r
char *FAT_ReadDir(tVFS_Node *Node, int ID);\r
tVFS_Node *FAT_FindDir(tVFS_Node *Node, char *Name);\r
int FAT_Mknod(tVFS_Node *Node, char *Name, Uint Flags);\r
node->Read = node->Write = NULL;\r
node->ReadDir = FAT_ReadDir;\r
node->FindDir = FAT_FindDir;\r
+ #if SUPPORT_WRITE\r
node->Relink = FAT_Relink;\r
node->MkNod = FAT_Mknod;\r
+ #else\r
+ node->Relink = NULL;\r
+ node->MkNod = NULL;\r
+ #endif\r
//node->Close = FAT_Unmount;\r
\r
giFAT_PartCount ++;\r
ENTER("pNode XOffset", Node, Offset);\r
\r
cluster = Node->Inode & 0xFFFFFFFF; // Cluster ID\r
+ LOG("cluster = %08x", cluster);\r
\r
// Do Cluster Skip\r
// - Pre FAT32 had a reserved area for the root.\r
if( disk->type == FAT32 || cluster != disk->rootOffset )\r
{\r
skip = Offset / disk->BytesPerCluster;\r
+ LOG("skip = %i", skip);\r
// Skip previous clusters\r
for(; skip-- ; )\r
{\r
addr += cluster * disk->BytesPerCluster;\r
}\r
else {\r
- addr = disk->firstDataSect;\r
+ addr = disk->firstDataSect * disk->bootsect.bps;\r
addr += (cluster - 2) * disk->BytesPerCluster;\r
}\r
addr += Offset % disk->BytesPerCluster;\r
\r
+ LOG("addr = 0x%08x", addr);\r
*Addr = addr;\r
LEAVE('i', 0);\r
return 0;\r
return val;\r
}\r
\r
+#if SUPPORT_WRITE\r
/**\r
* \brief Allocate a new cluster\r
*/\r
if(Disk->type == FAT32 && ret == EOC_FAT32) ret = -1;\r
return ret;\r
}\r
+#endif\r
\r
/*\r
* ====================\r
LEAVE('-');\r
}\r
\r
-/**\r
- * \brief Write a cluster to disk\r
- */\r
-void FAT_int_WriteCluster(tFAT_VolInfo *Disk, Uint32 Cluster, void *Buffer)\r
-{\r
- ENTER("pDisk xCluster pBuffer", Disk, Cluster, Buffer);\r
- VFS_ReadAt(\r
- Disk->fileHandle,\r
- (Disk->firstDataSect + (Cluster-2)*Disk->bootsect.spc )\r
- * Disk->bootsect.bps,\r
- Disk->BytesPerCluster,\r
- Buffer\r
- );\r
- LEAVE('-');\r
-}\r
-\r
/* ====================\r
* File IO\r
* ====================\r
return length;\r
}\r
\r
+#if SUPPORT_WRITE\r
+/**\r
+ * \brief Write a cluster to disk\r
+ */\r
+void FAT_int_WriteCluster(tFAT_VolInfo *Disk, Uint32 Cluster, void *Buffer)\r
+{\r
+ ENTER("pDisk xCluster pBuffer", Disk, Cluster, Buffer);\r
+ VFS_ReadAt(\r
+ Disk->fileHandle,\r
+ (Disk->firstDataSect + (Cluster-2)*Disk->bootsect.spc )\r
+ * Disk->bootsect.bps,\r
+ Disk->BytesPerCluster,\r
+ Buffer\r
+ );\r
+ LEAVE('-');\r
+}\r
+\r
/**\r
* \brief Write to a file\r
* \param Node File Node\r
\r
return Length;\r
}\r
+#endif\r
\r
/* ====================\r
* File Names & Nodes\r
}\r
\r
/**\r
- * \fn char *FAT_int_CreateName(tVFS_Node *parent, fat_filetable *ft, char *LongFileName)\r
+ * \fn char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName)\r
* \brief Converts either a LFN or a 8.3 Name into a proper name\r
*/\r
-char *FAT_int_CreateName(tVFS_Node *parent, fat_filetable *ft, char *LongFileName)\r
+char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName)\r
{\r
char *ret;\r
int len;\r
+ ENTER("pft sLongFileName", ft, LongFileName);\r
#if USE_LFN\r
if(LongFileName && LongFileName[0] != '\0')\r
{ \r
#if USE_LFN\r
}\r
#endif\r
+ LEAVE('s', ret);\r
return ret;\r
}\r
\r
/**\r
- * \fn tVFS_Node *FAT_int_CreateNode(tVFS_Node *parent, fat_filetable *ft, char *LongFileName)\r
+ * \fn tVFS_Node *FAT_int_CreateNode(tVFS_Node *parent, fat_filetable *ft)\r
* \brief Creates a tVFS_Node structure for a given file entry\r
*/\r
-tVFS_Node *FAT_int_CreateNode(tVFS_Node *parent, fat_filetable *ft, char *LongFileName)\r
+tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry, int Pos)\r
{\r
tVFS_Node node = {0};\r
tVFS_Node *ret;\r
- tFAT_VolInfo *disk = parent->ImplPtr;\r
+ tFAT_VolInfo *disk = Parent->ImplPtr;\r
\r
- ENTER("pParent pFT sLongFileName", parent, ft, LongFileName);\r
+ ENTER("pParent pFT", Parent, Entry);\r
\r
// Set Other Data\r
- node.Inode = ft->cluster | (ft->clusterHi<<16);\r
- node.Size = ft->size;\r
- LOG("ft->size = %i", ft->size);\r
- node.ImplPtr = parent->ImplPtr;\r
+ node.Inode = Entry->cluster | (Entry->clusterHi<<16) | (Parent->Inode << 32);\r
+ LOG("node.Inode = %llx", node.Inode);\r
+ node.ImplInt = Pos & 0xFFFF;\r
+ node.ImplPtr = disk;\r
+ node.Size = Entry->size;\r
+ LOG("Entry->size = %i", Entry->size);\r
node.UID = 0; node.GID = 0;\r
node.NumACLs = 1;\r
\r
node.Flags = 0;\r
- if(ft->attrib & ATTR_DIRECTORY) node.Flags |= VFS_FFLAG_DIRECTORY;\r
- if(ft->attrib & ATTR_READONLY) {\r
+ if(Entry->attrib & ATTR_DIRECTORY) node.Flags |= VFS_FFLAG_DIRECTORY;\r
+ if(Entry->attrib & ATTR_READONLY) {\r
node.Flags |= VFS_FFLAG_READONLY;\r
node.ACLs = &gVFS_ACL_EveryoneRX; // R-XR-XR-X\r
}\r
}\r
\r
node.ATime = timestamp(0,0,0,\r
- ((ft->adate&0x1F)-1), //Days\r
- ((ft->adate&0x1E0)-1), //Months\r
- 1980+((ft->adate&0xFF00)>>8)); //Years\r
+ ((Entry->adate&0x1F) - 1), // Days\r
+ ((Entry->adate&0x1E0) - 1), // Months\r
+ 1980+((Entry->adate&0xFF00)>>8) // Years\r
+ );\r
\r
- node.CTime = ft->ctimems * 10; //Miliseconds\r
+ node.CTime = Entry->ctimems * 10; // Miliseconds\r
node.CTime += timestamp(\r
- (ft->ctime&0x1F)<<1, //Seconds\r
- ((ft->ctime&0x3F0)>>5), //Minutes\r
- ((ft->ctime&0xF800)>>11), //Hours\r
- ((ft->cdate&0x1F)-1), //Days\r
- ((ft->cdate&0x1E0)-1), //Months\r
- 1980+((ft->cdate&0xFF00)>>8)); //Years\r
+ ((Entry->ctime&0x1F)<<1), // Seconds\r
+ ((Entry->ctime&0x3F0)>>5), // Minutes\r
+ ((Entry->ctime&0xF800)>>11), // Hours\r
+ ((Entry->cdate&0x1F)-1), // Days\r
+ ((Entry->cdate&0x1E0)-1), // Months\r
+ 1980+((Entry->cdate&0xFF00)>>8) // Years\r
+ );\r
\r
node.MTime = timestamp(\r
- (ft->mtime&0x1F)<<1, //Seconds\r
- ((ft->mtime&0x3F0)>>5), //Minuites\r
- ((ft->mtime&0xF800)>>11), //Hours\r
- ((ft->mdate&0x1F)-1), //Days\r
- ((ft->mdate&0x1E0)-1), //Months\r
- 1980+((ft->mdate&0xFF00)>>8)); //Years\r
+ ((Entry->mtime&0x1F)<<1), // Seconds\r
+ ((Entry->mtime&0x3F0)>>5), // Minutes\r
+ ((Entry->mtime&0xF800)>>11), // Hours\r
+ ((Entry->mdate&0x1F)-1), // Days\r
+ ((Entry->mdate&0x1E0)-1), // Months\r
+ 1980+((Entry->mdate&0xFF00)>>8) // Years\r
+ );\r
\r
if(node.Flags & VFS_FFLAG_DIRECTORY) {\r
+ //Log_Debug("FAT", "Directory %08x has size 0x%x", node.Inode, node.Size);\r
node.ReadDir = FAT_ReadDir;\r
node.FindDir = FAT_FindDir;\r
+ #if SUPPORT_WRITE\r
node.MkNod = FAT_Mknod;\r
+ node.Relink = FAT_Relink;\r
+ #endif\r
node.Size = -1;\r
} else {\r
node.Read = FAT_Read;\r
+ #if SUPPORT_WRITE\r
node.Write = FAT_Write;\r
+ #endif\r
}\r
node.Close = FAT_CloseFile;\r
- node.Relink = FAT_Relink;\r
\r
ret = Inode_CacheNode(disk->inodeHandle, &node);\r
LEAVE('p', ret);\r
Uint64 addr;\r
tFAT_VolInfo *disk = Node->ImplPtr;\r
\r
- ENTER("pNode iSector pEntry", Node, Sector, Buffer)\r
+ ENTER("pNode iSector pEntry", Node, Sector, Buffer);\r
\r
if(FAT_int_GetAddress(Node, Sector * 512, &addr, NULL))\r
{\r
}\r
\r
// Read Sector\r
- VFS_ReadAt(disk->fileHandle, addr, 512, Buffer); // Read Dir Data\r
+ if(VFS_ReadAt(disk->fileHandle, addr, 512, Buffer) != 512)\r
+ {\r
+ LEAVE('i', 1);\r
+ return 1;\r
+ }\r
\r
LEAVE('i', 0);\r
return 0;\r
}\r
\r
+#if SUPPORT_WRITE\r
/**\r
* \brief Writes an entry to the disk\r
* \todo Support expanding a directory\r
LEAVE('i', 0);\r
return 0;\r
}\r
+#endif\r
\r
#if USE_LFN\r
// I should probably more tightly associate the LFN cache with the node\r
\r
ENTER("pNode iID", Node, ID);\r
\r
- if(FAT_int_ReadDirSector(Node, ID, fileinfo))\r
+ if(FAT_int_ReadDirSector(Node, ID/16, fileinfo))\r
{\r
LEAVE('n');\r
return NULL;\r
lfn[ 7] = lfnInfo->name2[2]; lfn[ 8] = lfnInfo->name2[3];\r
lfn[ 9] = lfnInfo->name2[4]; lfn[10] = lfnInfo->name2[5];\r
lfn[11] = lfnInfo->name3[0]; lfn[12] = lfnInfo->name3[1];\r
+ LOG("lfn = '%s'", lfn);\r
LEAVE('p', VFS_SKIP);\r
return VFS_SKIP;\r
}\r
return VFS_SKIP;\r
} \r
\r
- LOG("name='%c%c%c%c%c%c%c%c.%c%c%c'\n",\r
+ LOG("name='%c%c%c%c%c%c%c%c.%c%c%c'",\r
fileinfo[a].name[0], fileinfo[a].name[1], fileinfo[a].name[2], fileinfo[a].name[3],\r
fileinfo[a].name[4], fileinfo[a].name[5], fileinfo[a].name[6], fileinfo[a].name[7],\r
fileinfo[a].name[8], fileinfo[a].name[9], fileinfo[a].name[10] );\r
\r
#if USE_LFN\r
- ret = FAT_int_CreateName(Node, &fileinfo[a], lfn);\r
+ ret = FAT_int_CreateName(&fileinfo[a], lfn);\r
lfn[0] = '\0';\r
#else\r
- ret = FAT_int_CreateName(Node, &fileinfo[a], NULL);\r
+ ret = FAT_int_CreateName(&fileinfo[a], NULL);\r
#endif\r
\r
LEAVE('s', ret);\r
* \fn tVFS_Node *FAT_FindDir(tVFS_Node *node, char *name)\r
* \brief Finds an entry in the current directory\r
*/\r
-tVFS_Node *FAT_FindDir(tVFS_Node *Node, char *name)\r
+tVFS_Node *FAT_FindDir(tVFS_Node *Node, char *Name)\r
{\r
fat_filetable fileinfo[16];\r
- char tmpName[11];\r
+ char tmpName[13];\r
#if USE_LFN\r
fat_longfilename *lfnInfo;\r
char lfn[256];\r
int lfnPos=255, lfnId = -1;\r
#endif\r
- int i=0;\r
+ int i;\r
tVFS_Node *tmpNode;\r
tFAT_VolInfo *disk = Node->ImplPtr;\r
Uint32 cluster;\r
\r
- ENTER("pNode sname", Node, name);\r
+ ENTER("pNode sname", Node, Name);\r
\r
// Fast Returns\r
- if(!name || name[0] == '\0') {\r
+ if(!Name || Name[0] == '\0') {\r
LEAVE('n');\r
return NULL;\r
}\r
\r
- for(;;i++)\r
+ for( i = 0; ; i++ )\r
{\r
if((i & 0xF) == 0) {\r
- if(FAT_int_ReadDirSector(Node, i, fileinfo))\r
+ if(FAT_int_ReadDirSector(Node, i/16, fileinfo))\r
{\r
LEAVE('n');\r
return NULL;\r
}\r
\r
//Check if the files are free\r
- if(fileinfo[i&0xF].name[0] == '\0') break; // Free and last\r
- if(fileinfo[i&0xF].name[0] == '\xE5') continue; //Free\r
+ if(fileinfo[i&0xF].name[0] == '\0') break; // End of List marker\r
+ if(fileinfo[i&0xF].name[0] == '\xE5') continue; // Free entry\r
\r
\r
#if USE_LFN\r
#endif\r
// Get Real Filename\r
FAT_int_ProperFilename(tmpName, fileinfo[i&0xF].name);\r
- \r
LOG("tmpName = '%s'", tmpName);\r
\r
- //Only Long name is case sensitive, 8.3 is not\r
+ // Only the long name is case sensitive, 8.3 is not\r
#if USE_LFN\r
- if(strucmp(tmpName, name) == 0 || strcmp(lfn, name) == 0) {\r
+ if(strucmp(tmpName, Name) == 0 || strcmp(lfn, Name) == 0)\r
#else\r
- if(strucmp(tmpName, name) == 0) {\r
+ if(strucmp(tmpName, Name) == 0)\r
#endif\r
+ {\r
cluster = fileinfo[i&0xF].cluster | (fileinfo[i&0xF].clusterHi << 16);\r
tmpNode = Inode_GetCache(disk->inodeHandle, cluster);\r
if(tmpNode == NULL) // Node is not cached\r
{\r
- #if USE_LFN\r
- tmpNode = FAT_int_CreateNode(Node, &fileinfo[i&0xF], lfn);\r
- #else\r
- tmpNode = FAT_int_CreateNode(Node, &fileinfo[i&0xF], NULL);\r
- #endif\r
+ tmpNode = FAT_int_CreateNode(Node, &fileinfo[i&0xF], i);\r
}\r
- #if USE_LFN\r
- lfn[0] = '\0';\r
- #endif\r
LEAVE('p', tmpNode);\r
return tmpNode;\r
}\r
return NULL;\r
}\r
\r
+#if SUPPORT_WRITE\r
/**\r
* \fn int FAT_Mknod(tVFS_Node *Node, char *Name, Uint Flags)\r
* \brief Create a new node\r
child->Close( child );\r
return ret;\r
}\r
+#endif\r
\r
/**\r
* \fn void FAT_CloseFile(tVFS_Node *Node)\r
tFAT_VolInfo *disk = Node->ImplPtr;\r
if(Node == NULL) return ;\r
\r
+ #if SUPPORT_WRITE\r
// Update the node if it's dirty (don't bother if it's marked for\r
// deletion)\r
if( Node->ImplInt & FAT_FLAG_DIRTY && !(Node->ImplInt & FAT_FLAG_DELETE) )\r
\r
Node->ImplInt &= ~FAT_FLAG_DIRTY;\r
}\r
+ #endif\r
\r
// TODO: Make this more thread safe somehow, probably by moving the\r
// Inode_UncacheNode higher up and saving the cluster value somewhere\r
FAT_int_DelLFN(Node);\r
#endif\r
\r
+ #if SUPPORT_WRITE\r
// Delete File\r
if( Node->ImplInt & FAT_FLAG_DELETE ) {\r
// Since the node is marked, we only need to remove it's data\r
while( cluster != -1 )\r
cluster = FAT_int_FreeCluster(Node->ImplPtr, cluster);\r
}\r
+ #endif\r
}\r
\r
Inode_UncacheNode(disk->inodeHandle, Node->Inode);\r