3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 /////////////////////////////////////////////////////////////////////////////////////////////
35 /////////////////////////////////////////////////////////////////////////////////////////////
37 U32 FileSystemType
= 0; // Type of filesystem on boot device, set by FsOpenVolume()
39 /////////////////////////////////////////////////////////////////////////////////////////////
41 /////////////////////////////////////////////////////////////////////////////////////////////
43 VOID
FileSystemError(PUCHAR ErrorString
)
45 DbgPrint((DPRINT_FILESYSTEM
, "%s\n", ErrorString
));
47 UiMessageBox(ErrorString
);
52 * BOOL FsOpenVolume(U32 DriveNumber, U32 PartitionNumber);
54 * This function is called to open a disk volume for file access.
55 * It must be called before any of the file functions will work.
56 * It takes two parameters:
58 * Drive: The BIOS drive number of the disk to open
59 * Partition: This is zero for floppy drives.
60 * If the disk is a hard disk then this specifies
61 * The partition number to open (1 - 4)
62 * If it is zero then it opens the active (bootable) partition
65 BOOL
FsOpenVolume(U32 DriveNumber
, U32 PartitionNumber
)
67 PARTITION_TABLE_ENTRY PartitionTableEntry
;
71 DbgPrint((DPRINT_FILESYSTEM
, "FsOpenVolume() DriveNumber: 0x%x PartitionNumber: 0x%x\n", DriveNumber
, PartitionNumber
));
73 // Check and see if it is a floppy drive
74 // If so then just assume FAT12 file system type
75 if (DiskIsDriveRemovable(DriveNumber
))
77 DbgPrint((DPRINT_FILESYSTEM
, "Drive is a floppy diskette drive. Assuming FAT12 file system.\n"));
79 FileSystemType
= FS_FAT
;
80 return FatOpenVolume(DriveNumber
, 0);
83 // Check for ISO9660 file system type
84 if (DriveNumber
> 0x80 && FsRecIsIso9660(DriveNumber
))
86 DbgPrint((DPRINT_FILESYSTEM
, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n"));
88 FileSystemType
= FS_ISO9660
;
89 return IsoOpenVolume(DriveNumber
);
92 // Set the boot partition
93 BootPartition
= PartitionNumber
;
95 // Get the requested partition entry
96 if (PartitionNumber
== 0)
98 // Partition requested was zero which means the boot partition
99 if (DiskGetActivePartitionEntry(DriveNumber
, &PartitionTableEntry
) == FALSE
)
101 FileSystemError("No active partition.");
107 // Get requested partition
108 if (DiskGetPartitionEntry(DriveNumber
, PartitionNumber
, &PartitionTableEntry
) == FALSE
)
110 FileSystemError("Partition not found.");
115 // Check for valid partition
116 if (PartitionTableEntry
.SystemIndicator
== PARTITION_ENTRY_UNUSED
)
118 FileSystemError("Invalid partition.");
122 // Try to recognize the file system
123 if (!FsRecognizeVolume(DriveNumber
, PartitionTableEntry
.SectorCountBeforePartition
, &VolumeType
))
125 FileSystemError("Unrecognized file system.");
129 //switch (PartitionTableEntry.SystemIndicator)
132 case PARTITION_FAT_12
:
133 case PARTITION_FAT_16
:
135 case PARTITION_XINT13
:
136 case PARTITION_FAT32
:
137 case PARTITION_FAT32_XINT13
:
138 FileSystemType
= FS_FAT
;
139 return FatOpenVolume(DriveNumber
, PartitionTableEntry
.SectorCountBeforePartition
);
141 FileSystemType
= FS_EXT2
;
142 return Ext2OpenVolume(DriveNumber
, PartitionTableEntry
.SectorCountBeforePartition
);
145 sprintf(ErrorText
, "Unsupported file system. Type: 0x%x", VolumeType
);
146 FileSystemError(ErrorText
);
153 PFILE
FsOpenFile(PUCHAR FileName
)
155 PFILE FileHandle
= NULL
;
158 // Print status message
160 DbgPrint((DPRINT_FILESYSTEM
, "Opening file '%s'...\n", FileName
));
163 // Check and see if the first character is '\' or '/' and remove it if so
165 while ((*FileName
== '\\') || (*FileName
== '/'))
171 // Check file system type and pass off to appropriate handler
173 switch (FileSystemType
)
176 FileHandle
= FatOpenFile(FileName
);
179 FileHandle
= IsoOpenFile(FileName
);
182 FileHandle
= Ext2OpenFile(FileName
);
185 FileSystemError("Error: Unknown filesystem.");
191 // Check return value
193 if (FileHandle
!= NULL
)
195 DbgPrint((DPRINT_FILESYSTEM
, "FsOpenFile() succeeded. FileHandle: 0x%x\n", FileHandle
));
199 DbgPrint((DPRINT_FILESYSTEM
, "FsOpenFile() failed.\n"));
201 #endif // defined DEBUG
206 VOID
FsCloseFile(PFILE FileHandle
)
212 * returns number of bytes read or EOF
214 BOOL
FsReadFile(PFILE FileHandle
, U32 BytesToRead
, U32
* BytesRead
, PVOID Buffer
)
220 // Set the number of bytes read equal to zero
222 if (BytesRead
!= NULL
)
227 switch (FileSystemType
)
231 return FatReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
235 return IsoReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
239 //return Ext2ReadFile(FileHandle, BytesToRead, BytesRead, Buffer);
240 Success
= Ext2ReadFile(FileHandle
, BytesToRead
, &BytesReadBig
, Buffer
);
241 *BytesRead
= (U32
)BytesReadBig
;
246 FileSystemError("Unknown file system.");
253 U32
FsGetFileSize(PFILE FileHandle
)
255 switch (FileSystemType
)
259 return FatGetFileSize(FileHandle
);
263 return IsoGetFileSize(FileHandle
);
267 return Ext2GetFileSize(FileHandle
);
270 FileSystemError("Unknown file system.");
277 VOID
FsSetFilePointer(PFILE FileHandle
, U32 NewFilePointer
)
279 switch (FileSystemType
)
283 FatSetFilePointer(FileHandle
, NewFilePointer
);
288 IsoSetFilePointer(FileHandle
, NewFilePointer
);
293 Ext2SetFilePointer(FileHandle
, NewFilePointer
);
297 FileSystemError("Unknown file system.");
302 U32
FsGetFilePointer(PFILE FileHandle
)
304 switch (FileSystemType
)
308 return FatGetFilePointer(FileHandle
);
313 return IsoGetFilePointer(FileHandle
);
318 return Ext2GetFilePointer(FileHandle
);
322 FileSystemError("Unknown file system.");
329 BOOL
FsIsEndOfFile(PFILE FileHandle
)
331 if (FsGetFilePointer(FileHandle
) >= FsGetFileSize(FileHandle
))
342 * FsGetNumPathParts()
343 * This function parses a path in the form of dir1\dir2\file1.ext
344 * and returns the number of parts it has (i.e. 3 - dir1,dir2,file1.ext)
346 U32
FsGetNumPathParts(PUCHAR Path
)
351 for (i
=0,num
=0; i
<(int)strlen(Path
); i
++)
353 if ((Path
[i
] == '\\') || (Path
[i
] == '/'))
360 DbgPrint((DPRINT_FILESYSTEM
, "FatGetNumPathParts() Path = %s NumPathParts = %d\n", Path
, num
));
366 * FsGetFirstNameFromPath()
367 * This function parses a path in the form of dir1\dir2\file1.ext
368 * and puts the first name of the path (e.g. "dir1") in buffer
369 * compatible with the MSDOS directory structure
371 VOID
FsGetFirstNameFromPath(PUCHAR Buffer
, PUCHAR Path
)
375 // Copy all the characters up to the end of the
376 // string or until we hit a '\' character
377 // and put them in Buffer
378 for (i
=0; i
<(int)strlen(Path
); i
++)
380 if ((Path
[i
] == '\\') || (Path
[i
] == '/'))
392 DbgPrint((DPRINT_FILESYSTEM
, "FatGetFirstNameFromPath() Path = %s FirstName = %s\n", Path
, Buffer
));