3 * FAT12/16/32 Driver Version (Incl LFN)
\r
15 # define DEBUGS(v...) Log(v)
\r
17 # define DEBUGS(v...)
\r
26 #define CACHE_FAT 1 //!< Caches the FAT in memory
\r
27 #define USE_LFN 1 //!< Enables the use of Long File Names
\r
31 typedef struct s_lfncache {
\r
35 struct s_lfncache *Next;
\r
39 // === PROTOTYPES ===
\r
40 int FAT_Install(char **Arguments);
\r
41 tVFS_Node *FAT_InitDevice(char *device, char *options);
\r
42 void FAT_Unmount(tVFS_Node *Node);
\r
43 Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer);
\r
44 Uint64 FAT_Write(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer);
\r
45 char *FAT_ReadDir(tVFS_Node *dirNode, int dirpos);
\r
46 tVFS_Node *FAT_FindDir(tVFS_Node *dirNode, char *file);
\r
47 int FAT_Mknod(tVFS_Node *Node, char *Name, Uint Flags);
\r
48 int FAT_Relink(tVFS_Node *node, char *OldName, char *NewName);
\r
49 void FAT_CloseFile(tVFS_Node *node);
\r
51 // === SEMI-GLOBALS ===
\r
52 MODULE_DEFINE(0, 0x5B /*v0.90*/, FAT32, FAT_Install, NULL);
\r
53 tFAT_VolInfo gFAT_Disks[8];
\r
54 int giFAT_PartCount = 0;
\r
56 Uint32 *fat_cache[8];
\r
59 t_lfncache *fat_lfncache;
\r
61 tVFS_Driver gFAT_FSInfo = {
\r
62 "fat", 0, FAT_InitDevice, FAT_Unmount, NULL
\r
67 * \fn int FAT_Install(char **Arguments)
\r
70 int FAT_Install(char **Arguments)
\r
72 VFS_AddDriver( &gFAT_FSInfo );
\r
76 /* Reads the boot sector of a disk and prepares the structures for it
\r
78 tVFS_Node *FAT_InitDevice(char *Device, char *options)
\r
82 Uint32 FATSz, RootDirSectors, TotSec, CountofClusters;
\r
83 tVFS_Node *node = NULL;
\r
84 tFAT_VolInfo *diskInfo = &gFAT_Disks[giFAT_PartCount];
\r
87 bs = &diskInfo->bootsect;
\r
89 //Open device and read boot sector
\r
90 diskInfo->fileHandle = VFS_Open(Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE);
\r
91 if(diskInfo->fileHandle == -1) {
\r
92 Warning("FAT_InitDisk - Unable to open device '%s'", Device);
\r
96 VFS_ReadAt(diskInfo->fileHandle, 0, 512, bs);
\r
98 if(bs->bps == 0 || bs->spc == 0) {
\r
99 Warning("FAT_InitDisk - Error in FAT Boot Sector\n");
\r
103 //FAT Type Determining
\r
104 // From Microsoft FAT Specifcation
\r
105 RootDirSectors = ((bs->files_in_root*32) + (bs->bps - 1)) / bs->bps;
\r
107 if(bs->fatSz16 != 0) FATSz = bs->fatSz16;
\r
108 else FATSz = bs->spec.fat32.fatSz32;
\r
110 if(bs->totalSect16 != 0) TotSec = bs->totalSect16;
\r
111 else TotSec = bs->totalSect32;
\r
113 CountofClusters = (TotSec - (bs->resvSectCount + (bs->fatCount * FATSz) + RootDirSectors)) / bs->spc;
\r
115 if(CountofClusters < 4085)
\r
116 diskInfo->type = FAT12;
\r
117 else if(CountofClusters < 65525)
\r
118 diskInfo->type = FAT16;
\r
120 diskInfo->type = FAT32;
\r
124 char *sFatType, *sSize;
\r
125 Uint iSize = CountofClusters * bs->spc / 2;
\r
127 switch(diskInfo->type)
\r
129 case FAT12: sFatType = "FAT12"; break;
\r
130 case FAT16: sFatType = "FAT16"; break;
\r
131 case FAT32: sFatType = "FAT32"; break;
\r
133 if(iSize <= 2*1024) {
\r
136 else if(iSize <= 2*1024*1024) {
\r
144 Log("[FAT ] '%s' %s, %i %s", Device, sFatType, iSize, sSize);
\r
150 if(diskInfo->type == FAT32) {
\r
152 diskInfo->name[i] = (bs->spec.fat32.label[i] == ' ' ? '\0' : bs->spec.fat32.label[i]);
\r
156 diskInfo->name[i] = (bs->spec.fat16.label[i] == ' ' ? '\0' : bs->spec.fat16.label[i]);
\r
158 diskInfo->name[11] = '\0';
\r
159 //puts(diskInfo->name); putch('\n');
\r
161 //Compute Root directory offset
\r
162 if(diskInfo->type == FAT32)
\r
163 diskInfo->rootOffset = bs->spec.fat32.rootClust;
\r
165 diskInfo->rootOffset = (FATSz * bs->fatCount) / bs->spc;
\r
167 diskInfo->clusterCount = CountofClusters;
\r
169 diskInfo->firstDataSect = bs->resvSectCount + (bs->fatCount * FATSz) + RootDirSectors;
\r
171 //Allow for Caching the FAT
\r
175 fat_cache[ giFAT_PartCount ] = (Uint32*)malloc(sizeof(Uint32)*CountofClusters);
\r
176 if(fat_cache[giFAT_PartCount] == NULL) {
\r
177 Warning("FAT_InitDisk - Heap Exhausted\n");
\r
180 Ofs = bs->resvSectCount*512;
\r
181 if(diskInfo->type == FAT12) {
\r
185 for(i=0;i<CountofClusters/2;i++) {
\r
186 j = i & 511; //%512
\r
188 VFS_ReadAt(diskInfo->fileHandle, Ofs, 3*512, buf);
\r
191 val = *((int*)(buf+j*3));
\r
192 fat_cache[giFAT_PartCount][i*2] = val & 0xFFF;
\r
193 fat_cache[giFAT_PartCount][i*2+1] = (val>>12) & 0xFFF;
\r
196 if(diskInfo->type == FAT16) {
\r
198 for(i=0;i<CountofClusters;i++) {
\r
199 if( (i & 255) == 0 ) {
\r
200 VFS_ReadAt(diskInfo->fileHandle, Ofs, 512, buf);
\r
203 fat_cache[giFAT_PartCount][i] = buf[i&255];
\r
206 if(diskInfo->type == FAT32) {
\r
208 for(i=0;i<CountofClusters;i++) {
\r
209 if( (i & 127) == 0 ) {
\r
210 VFS_ReadAt(diskInfo->fileHandle, Ofs, 512, buf);
\r
213 fat_cache[giFAT_PartCount][i] = buf[i&127];
\r
216 DEBUGS(" FAT_InitDisk: FAT Fully Cached\n");
\r
218 #endif /*CACHE_FAT*/
\r
220 //Initalise inode cache for FAT
\r
221 gFAT_Disks[giFAT_PartCount].inodeHandle = Inode_GetHandle();
\r
224 Log(" FAT_InitDisk: Inode Cache handle is %i\n", gFAT_Disks[giFAT_PartCount].inodeHandle);
\r
228 node = &gFAT_Disks[giFAT_PartCount].rootNode;
\r
229 //node->Name = gFAT_Disks[giFAT_PartCount].name;
\r
230 node->Inode = diskInfo->rootOffset;
\r
231 node->Size = bs->files_in_root; //Unknown - To be set on readdir
\r
232 node->ImplInt = giFAT_PartCount;
\r
234 node->ReferenceCount = 1;
\r
236 node->UID = 0; node->GID= 0;
\r
238 node->ACLs = &gVFS_ACL_EveryoneRWX;
\r
239 node->Flags = VFS_FFLAG_DIRECTORY;
\r
240 node->CTime = node->MTime = node->ATime = now();
\r
242 node->Read = node->Write = NULL;
\r
243 node->ReadDir = FAT_ReadDir;
\r
244 node->FindDir = FAT_FindDir;
\r
245 node->Relink = FAT_Relink;
\r
246 node->MkNod = FAT_Mknod;
\r
247 //node->Close = FAT_CloseDevice;
\r
249 giFAT_PartCount ++;
\r
254 * \fn void FAT_Unmount(tVFS_Node *Node)
\r
255 * \brief Closes a mount and marks it as free
\r
257 void FAT_Unmount(tVFS_Node *Node)
\r
259 // Close Disk Handle
\r
260 VFS_Close( gFAT_Disks[Node->ImplInt].fileHandle );
\r
261 // Clear Node Cache
\r
262 Inode_ClearCache(gFAT_Disks[Node->ImplInt].inodeHandle);
\r
264 gFAT_Disks[Node->ImplInt].fileHandle = -2;
\r
269 * \fn static Uint32 FAT_int_GetFatValue(int handle, Uint32 cluster)
\r
270 * \brief Fetches a value from the FAT
\r
272 static Uint32 FAT_int_GetFatValue(int handle, Uint32 cluster)
\r
275 ENTER("iHandle xCluster", handle, cluster);
\r
277 val = fat_cache[handle][cluster];
\r
279 if(gFAT_Disks[handle].type == FAT12) {
\r
280 VFS_ReadAt(gFAT_Disks[handle].fileHandle, 512+(cluster&~1)*3, 3, &val);
\r
281 val = (cluster&1 ? val&0xFFF : val>>12);
\r
282 } else if(gFAT_Disks[handle].type == FAT16) {
\r
283 VFS_ReadAt(gFAT_Disks[handle].fileHandle, 512+cluster*2, 2, &val);
\r
285 VFS_ReadAt(gFAT_Disks[handle].fileHandle, 512+cluster*4, 4, &val);
\r
287 #endif /*CACHE_FAT*/
\r
292 /* Reads a cluster's data
\r
294 static void FAT_int_ReadCluster(int Handle, Uint32 Cluster, int Length, void *Buffer)
\r
297 ENTER("iHandle xCluster iLength pBuffer", Handle, Cluster, Length, Buffer);
\r
300 gFAT_Disks[Handle].fileHandle,
\r
301 (gFAT_Disks[Handle].firstDataSect + (Cluster-2)*gFAT_Disks[Handle].bootsect.spc )
\r
302 * gFAT_Disks[Handle].bootsect.bps,
\r
312 * \fn Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
\r
313 * \brief Reads data from a specified file
\r
315 Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
\r
317 int preSkip, count;
\r
318 int handle = node->ImplInt;
\r
319 int i, cluster, pos;
\r
323 tFAT_VolInfo *disk = &gFAT_Disks[node->ImplInt];
\r
325 ENTER("Xoffset Xlength pbuffer", offset, length, buffer);
\r
327 // Calculate and Allocate Bytes Per Cluster
\r
328 bpc = disk->bootsect.spc * disk->bootsect.bps;
\r
329 tmpBuf = (void*) malloc(bpc);
\r
330 LOG("malloc'd %i bytes", bpc);
\r
332 // Cluster is stored in Inode Field
\r
333 cluster = node->Inode;
\r
336 if (disk->type == FAT12) eocMarker = EOC_FAT12;
\r
337 else if(disk->type == FAT16) eocMarker = EOC_FAT16;
\r
338 else if(disk->type == FAT32) eocMarker = EOC_FAT32;
\r
340 Log("ERROR: Unsupported FAT Variant.\n");
\r
346 // Single Cluster including offset
\r
347 if(length + offset < bpc)
\r
349 FAT_int_ReadCluster(handle, cluster, bpc, tmpBuf);
\r
350 memcpy( buffer, (void*)( tmpBuf + offset%bpc ), length );
\r
356 preSkip = offset / bpc;
\r
358 //Skip previous clusters
\r
359 for(i=preSkip;i--;) {
\r
360 cluster = FAT_int_GetFatValue(handle, cluster);
\r
361 if(cluster == eocMarker) {
\r
362 Warning("FAT_Read - Offset is past end of cluster chain mark");
\r
366 // Get Count of Clusters to read
\r
367 count = ((offset%bpc+length) / bpc) + 1;
\r
369 // Get buffer Position after 1st cluster
\r
370 pos = bpc - offset%bpc;
\r
372 // Read 1st Cluster
\r
373 FAT_int_ReadCluster(handle, cluster, bpc, tmpBuf);
\r
376 (void*)( tmpBuf + (bpc-pos) ),
\r
377 (pos < length ? pos : length)
\r
386 cluster = FAT_int_GetFatValue(handle, cluster);
\r
389 LOG("pos=%i\n", pos);
\r
390 LOG("Reading the rest of the clusters\n");
\r
394 //Read the rest of the cluster data
\r
395 for( i = 1; i < count-1; i++ )
\r
397 FAT_int_ReadCluster(handle, cluster, bpc, tmpBuf);
\r
398 memcpy((void*)(buffer+pos), tmpBuf, bpc);
\r
400 cluster = FAT_int_GetFatValue(handle, cluster);
\r
401 if(cluster == eocMarker) {
\r
402 Warning("FAT_Read - Read past End of Cluster Chain");
\r
409 FAT_int_ReadCluster(handle, cluster, bpc, tmpBuf);
\r
410 memcpy((void*)(buffer+pos), tmpBuf, length-pos);
\r
413 LOG("Free tmpBuf(0x%x) and Return\n", tmpBuf);
\r
417 LEAVE('X', length);
\r
422 * \fn Uint64 FAT_Write(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
\r
424 Uint64 FAT_Write(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
\r
430 * \fn static void FAT_int_ProperFilename(char *dest, char *src)
\r
431 * \brief Converts a FAT directory entry name into a proper filename
\r
433 static void FAT_int_ProperFilename(char *dest, char *src)
\r
437 for( a = 0; a < 8; a++) {
\r
438 if(src[a] == ' ') break;
\r
445 for( ; a < 11; a++, b++) {
\r
446 if(src[a] == ' ') break;
\r
451 //Log("FAT_int_ProperFilename: dest='%s'", dest);
\r
456 * \fn char *FAT_int_CreateName(tVFS_Node *parent, fat_filetable *ft, char *LongFileName)
\r
457 * \brief Converts either a LFN or a 8.3 Name into a proper name
\r
459 char *FAT_int_CreateName(tVFS_Node *parent, fat_filetable *ft, char *LongFileName)
\r
464 if(LongFileName && LongFileName[0] != '\0')
\r
466 len = strlen(LongFileName);
\r
467 ret = malloc(len+1);
\r
468 strcpy(ret, LongFileName);
\r
473 ret = (char*) malloc(13);
\r
474 memset(ret, 13, '\0');
\r
475 FAT_int_ProperFilename(ret, ft->name);
\r
483 * \fn tVFS_Node *FAT_int_CreateNode(tVFS_Node *parent, fat_filetable *ft, char *LongFileName)
\r
484 * \brief Creates a tVFS_Node structure for a given file entry
\r
486 tVFS_Node *FAT_int_CreateNode(tVFS_Node *parent, fat_filetable *ft, char *LongFileName)
\r
488 tVFS_Node node = {0};
\r
491 ENTER("pParent pFT sLongFileName", parent, ft, LongFileName);
\r
494 //node.Name = FAT_int_CreateName(parent, ft, LongFileName);
\r
496 node.Inode = ft->cluster | (ft->clusterHi<<16);
\r
497 node.Size = ft->size;
\r
498 node.ImplInt = parent->ImplInt;
\r
499 node.UID = 0; node.GID = 0;
\r
501 node.ACLs = &gVFS_ACL_EveryoneRWX; // RWXRWXRWX
\r
504 if(ft->attrib & ATTR_DIRECTORY) node.Flags |= VFS_FFLAG_DIRECTORY;
\r
505 if(ft->attrib & ATTR_READONLY) node.Flags |= VFS_FFLAG_READONLY;
\r
507 node.ATime = timestamp(0,0,0,
\r
508 ((ft->adate&0x1F)-1), //Days
\r
509 ((ft->adate&0x1E0)-1), //Months
\r
510 1980+((ft->adate&0xFF00)>>8)); //Years
\r
512 node.CTime = ft->ctimems * 10; //Miliseconds
\r
513 node.CTime += timestamp(
\r
514 (ft->ctime&0x1F)<<1, //Seconds
\r
515 ((ft->ctime&0x3F0)>>5), //Minutes
\r
516 ((ft->ctime&0xF800)>>11), //Hours
\r
517 ((ft->cdate&0x1F)-1), //Days
\r
518 ((ft->cdate&0x1E0)-1), //Months
\r
519 1980+((ft->cdate&0xFF00)>>8)); //Years
\r
521 node.MTime = timestamp(
\r
522 (ft->mtime&0x1F)<<1, //Seconds
\r
523 ((ft->mtime&0x3F0)>>5), //Minuites
\r
524 ((ft->mtime&0xF800)>>11), //Hours
\r
525 ((ft->mdate&0x1F)-1), //Days
\r
526 ((ft->mdate&0x1E0)-1), //Months
\r
527 1980+((ft->mdate&0xFF00)>>8)); //Years
\r
529 if(node.Flags & VFS_FFLAG_DIRECTORY) {
\r
530 node.ReadDir = FAT_ReadDir;
\r
531 node.FindDir = FAT_FindDir;
\r
532 node.MkNod = FAT_Mknod;
\r
534 node.Read = FAT_Read;
\r
535 node.Write = FAT_Write;
\r
537 node.Close = FAT_CloseFile;
\r
538 node.Relink = FAT_Relink;
\r
540 ret = Inode_CacheNode(gFAT_Disks[parent->ImplInt].inodeHandle, &node);
\r
547 \fn char *FAT_int_GetLFN(tVFS_Node *node)
\r
548 \brief Return pointer to LFN cache entry
\r
550 char *FAT_int_GetLFN(tVFS_Node *node)
\r
553 tmp = fat_lfncache;
\r
556 if(tmp->Inode == node->Inode && tmp->Impl == node->ImplInt)
\r
560 tmp = malloc(sizeof(t_lfncache));
\r
561 tmp->Inode = node->Inode;
\r
562 tmp->Impl = node->ImplInt;
\r
563 memset(tmp->Name, 0, 256);
\r
565 tmp->Next = fat_lfncache;
\r
566 fat_lfncache = tmp;
\r
572 \fn void FAT_int_DelLFN(tVFS_Node *node)
\r
573 \brief Delete a LFN cache entry
\r
575 void FAT_int_DelLFN(tVFS_Node *node)
\r
579 if(!fat_lfncache) return;
\r
581 if(!fat_lfncache->Next)
\r
583 tmp = fat_lfncache;
\r
584 fat_lfncache = tmp->Next;
\r
588 tmp = fat_lfncache;
\r
589 while(tmp && tmp->Next)
\r
591 if(tmp->Inode == node->Inode && tmp->Impl == node->ImplInt)
\r
594 tmp->Next = tmp->Next->Next;
\r
603 \fn char *FAT_ReadDir(tVFS_Node *dirNode, int dirPos)
\r
604 \param dirNode Node structure of directory
\r
605 \param dirPos Directory position
\r
607 char *FAT_ReadDir(tVFS_Node *dirNode, int dirpos)
\r
609 fat_filetable fileinfo[16]; //Sizeof=32, 16 per sector
\r
611 tFAT_VolInfo *disk = &gFAT_Disks[dirNode->ImplInt&7];
\r
612 Uint32 cluster, offset;
\r
619 ENTER("pDirNode iDirPos", dirNode, dirpos);
\r
621 // Get Byte Offset and skip
\r
622 offset = dirpos * sizeof(fat_filetable);
\r
623 preSkip = (offset >> 9) / disk->bootsect.spc; // >>9 == /512
\r
624 cluster = dirNode->Inode; // Cluster ID
\r
627 // - Pre FAT32 had a reserved area for the root.
\r
628 if( !(disk->type != FAT32 && cluster == disk->rootOffset) )
\r
630 //Skip previous clusters
\r
631 for(a=preSkip;a--;) {
\r
632 cluster = FAT_int_GetFatValue(dirNode->ImplInt, cluster);
\r
636 // Check for end of cluster chain
\r
637 if((disk->type == FAT12 && cluster == EOC_FAT12)
\r
638 || (disk->type == FAT16 && cluster == EOC_FAT16)
\r
639 || (disk->type == FAT32 && cluster == EOC_FAT32))
\r
642 // Bounds Checking (Used to spot heap overflows)
\r
643 if(cluster > disk->clusterCount + 2)
\r
645 Warning("FAT_ReadDir - Cluster ID is over cluster count (0x%x>0x%x)",
\r
646 cluster, disk->clusterCount+2);
\r
651 LOG("cluster=0x%x, dirpos=%i\n", cluster, dirpos);
\r
654 // - Pre FAT32 cluster base (in sectors)
\r
655 if( cluster == disk->rootOffset && disk->type != FAT32 )
\r
656 offset = disk->bootsect.resvSectCount + cluster*disk->bootsect.spc;
\r
658 { // FAT32 cluster base (in sectors)
\r
659 offset = disk->firstDataSect;
\r
660 offset += (cluster - 2) * disk->bootsect.spc;
\r
662 // Sector in cluster
\r
663 if(disk->bootsect.spc == 1)
\r
664 offset += (dirpos / 16);
\r
666 offset += (dirpos / 16) % disk->bootsect.spc;
\r
667 // Offset in sector
\r
670 LOG("offset=%i, a=%i\n", (Uint)offset, a);
\r
673 VFS_ReadAt(disk->fileHandle, offset*512, 512, fileinfo); // Read Dir Data
\r
675 LOG("name[0] = 0x%x\n", (Uint8)fileinfo[a].name[0]);
\r
676 //Check if this is the last entry
\r
677 if(fileinfo[a].name[0] == '\0') {
\r
678 dirNode->Size = dirpos;
\r
679 LOG("End of list\n");
\r
681 return NULL; // break
\r
684 // Check for empty entry
\r
685 if((Uint8)fileinfo[a].name[0] == 0xE5) {
\r
686 LOG("Empty Entry\n");
\r
687 LEAVE('p', VFS_SKIP);
\r
688 return VFS_SKIP; // Skip
\r
692 // Get Long File Name Cache
\r
693 lfn = FAT_int_GetLFN(dirNode);
\r
694 if(fileinfo[a].attrib == ATTR_LFN)
\r
696 fat_longfilename *lfnInfo;
\r
699 lfnInfo = (fat_longfilename *) &fileinfo[a];
\r
700 if(lfnInfo->id & 0x40) memset(lfn, 0, 256);
\r
701 // Get the current length
\r
704 // Sanity Check (FAT implementations do not allow >255 bytes)
\r
705 if(len + 13 > 255) return VFS_SKIP;
\r
706 // Rebase all bytes
\r
707 for(a=len+1;a--;) lfn[a+13] = lfn[a];
\r
709 // Append new bytes
\r
710 lfn[ 0] = lfnInfo->name1[0]; lfn[ 1] = lfnInfo->name1[1];
\r
711 lfn[ 2] = lfnInfo->name1[2]; lfn[ 3] = lfnInfo->name1[3];
\r
712 lfn[ 4] = lfnInfo->name1[4];
\r
713 lfn[ 5] = lfnInfo->name2[0]; lfn[ 6] = lfnInfo->name2[1];
\r
714 lfn[ 7] = lfnInfo->name2[2]; lfn[ 8] = lfnInfo->name2[3];
\r
715 lfn[ 9] = lfnInfo->name2[4]; lfn[10] = lfnInfo->name2[5];
\r
716 lfn[11] = lfnInfo->name3[0]; lfn[12] = lfnInfo->name3[1];
\r
717 LEAVE('p', VFS_SKIP);
\r
722 //Check if it is a volume entry
\r
723 if(fileinfo[a].attrib & 0x08) {
\r
724 LEAVE('p', VFS_SKIP);
\r
728 if(fileinfo[a].name[0] == '.') {
\r
729 LEAVE('p', VFS_SKIP);
\r
733 LOG("name='%c%c%c%c%c%c%c%c.%c%c%c'\n",
\r
734 fileinfo[a].name[0], fileinfo[a].name[1], fileinfo[a].name[2], fileinfo[a].name[3],
\r
735 fileinfo[a].name[4], fileinfo[a].name[5], fileinfo[a].name[6], fileinfo[a].name[7],
\r
736 fileinfo[a].name[8], fileinfo[a].name[9], fileinfo[a].name[10] );
\r
739 //node = FAT_int_CreateNode(dirNode, &fileinfo[a], lfn);
\r
740 ret = FAT_int_CreateName(dirNode, &fileinfo[a], lfn);
\r
743 //node = FAT_int_CreateNode(dirNode, &fileinfo[a], NULL);
\r
744 ret = FAT_int_CreateName(dirNode, &fileinfo[a], NULL);
\r
752 * \fn tVFS_Node *FAT_FindDir(tVFS_Node *node, char *name)
\r
753 * \brief Finds an entry in the current directory
\r
755 tVFS_Node *FAT_FindDir(tVFS_Node *node, char *name)
\r
757 fat_filetable fileinfo[16];
\r
760 fat_longfilename *lfnInfo;
\r
762 int lfnPos=255, lfnId = -1;
\r
765 tVFS_Node *tmpNode;
\r
767 tFAT_VolInfo *disk = &gFAT_Disks[node->ImplInt];
\r
771 ENTER("pnode sname", node, name);
\r
774 if(!name) return NULL;
\r
775 if(name[0] == '\0') return NULL;
\r
778 lfn = FAT_int_GetLFN(node);
\r
781 dirCluster = node->Inode;
\r
782 // Seek to Directory
\r
783 if( dirCluster == disk->rootOffset && disk->type != FAT32 )
\r
784 diskOffset = (disk->bootsect.resvSectCount+dirCluster*disk->bootsect.spc) << 9;
\r
786 diskOffset = (disk->firstDataSect+(dirCluster-2)*disk->bootsect.spc) << 9;
\r
791 if((i & 0xF) == 0) {
\r
792 VFS_ReadAt(disk->fileHandle, diskOffset, 512, fileinfo);
\r
796 //Check if the files are free
\r
797 if(fileinfo[i&0xF].name[0] == '\0') break; //Free and last
\r
798 if(fileinfo[i&0xF].name[0] == '\xE5') goto loadCluster; //Free
\r
802 // Long File Name Entry
\r
803 if(fileinfo[i&0xF].attrib == ATTR_LFN)
\r
805 lfnInfo = (fat_longfilename *) &fileinfo[i&0xF];
\r
806 if(lfnInfo->id & 0x40) {
\r
807 memset(lfn, 0, 256);
\r
810 lfn[lfnPos--] = lfnInfo->name3[1]; lfn[lfnPos--] = lfnInfo->name3[0];
\r
811 lfn[lfnPos--] = lfnInfo->name2[5]; lfn[lfnPos--] = lfnInfo->name2[4];
\r
812 lfn[lfnPos--] = lfnInfo->name2[3]; lfn[lfnPos--] = lfnInfo->name2[2];
\r
813 lfn[lfnPos--] = lfnInfo->name2[1]; lfn[lfnPos--] = lfnInfo->name2[0];
\r
814 lfn[lfnPos--] = lfnInfo->name1[4]; lfn[lfnPos--] = lfnInfo->name1[3];
\r
815 lfn[lfnPos--] = lfnInfo->name1[2]; lfn[lfnPos--] = lfnInfo->name1[1];
\r
816 lfn[lfnPos--] = lfnInfo->name1[0];
\r
817 if((lfnInfo->id&0x3F) == 1)
\r
819 memcpy(lfn, lfn+lfnPos+1, 256-lfnPos);
\r
825 // Remove LFN if it does not apply
\r
826 if(lfnId != i) lfn[0] = '\0';
\r
828 // Get Real Filename
\r
829 FAT_int_ProperFilename(tmpName, fileinfo[i&0xF].name);
\r
831 LOG("tmpName = '%s'\n", tmpName);
\r
833 //Only Long name is case sensitive, 8.3 is not
\r
835 if(strucmp(tmpName, name) == 0 || strcmp(lfn, name) == 0) {
\r
837 if(strucmp(tmpName, name) == 0) {
\r
839 cluster = fileinfo[i&0xF].cluster | (fileinfo[i&0xF].clusterHi << 16);
\r
840 tmpNode = Inode_GetCache(disk->inodeHandle, cluster);
\r
841 if(tmpNode == NULL) // Node is not cached
\r
844 tmpNode = FAT_int_CreateNode(node, &fileinfo[i&0xF], lfn);
\r
846 tmpNode = FAT_int_CreateNode(node, &fileinfo[i&0xF], NULL);
\r
852 LEAVE('p', tmpNode);
\r
860 //Load Next cluster?
\r
861 if( ((i+1) >> 4) % disk->bootsect.spc == 0 && ((i+1) & 0xF) == 0)
\r
863 if( dirCluster == disk->rootOffset && disk->type != FAT32 )
\r
865 dirCluster = FAT_int_GetFatValue(node->ImplInt, dirCluster);
\r
866 diskOffset = (disk->firstDataSect+(dirCluster-2)*disk->bootsect.spc)*512;
\r
875 * \fn int FAT_Mknod(tVFS_Node *Node, char *Name, Uint Flags)
\r
876 * \brief Create a new node
\r
878 int FAT_Mknod(tVFS_Node *Node, char *Name, Uint Flags)
\r
884 * \fn int FAT_Relink(tVFS_Node *Node, char *OldName, char *NewName)
\r
885 * \brief Rename / Delete a file
\r
887 int FAT_Relink(tVFS_Node *Node, char *OldName, char *NewName)
\r
893 * \fn void FAT_CloseFile(tVFS_Node *Node)
\r
894 * \brief Close an open file
\r
896 void FAT_CloseFile(tVFS_Node *Node)
\r
898 if(Node == NULL) return ;
\r
900 Inode_UncacheNode(gFAT_Disks[Node->ImplInt].inodeHandle, Node->Inode);
\r
902 if( !Inode_GetCache(gFAT_Disks[Node->ImplInt].inodeHandle, Node->Inode)
\r
903 && Node->Flags & VFS_FFLAG_DIRECTORY)
\r
904 FAT_int_DelLFN(Node);
\r
905 else // Get Cache references the node, so dereference it
\r
906 Inode_UncacheNode(gFAT_Disks[Node->ImplInt].inodeHandle, Node->Inode);
\r
912 * \fn void fat_install()
\r
913 * \brief Add the FAT Filesystem to the VFS
\r
917 VFS_AddDriver( &gFAT_FSInfo );
\r