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.
35 /////////////////////////////////////////////////////////////////////////////////////////////
37 /////////////////////////////////////////////////////////////////////////////////////////////
39 ULONG FsType
= 0; // Type of filesystem on boot device, set by FsOpenVolume()
41 /////////////////////////////////////////////////////////////////////////////////////////////
43 /////////////////////////////////////////////////////////////////////////////////////////////
45 VOID
FileSystemError(PUCHAR ErrorString
)
47 DbgPrint((DPRINT_FILESYSTEM
, "%s\n", ErrorString
));
49 UiMessageBox(ErrorString
);
54 * BOOL FsOpenVolume(ULONG DriveNumber, ULONG PartitionNumber);
56 * This function is called to open a disk volume for file access.
57 * It must be called before any of the file functions will work.
58 * It takes two parameters:
60 * Drive: The BIOS drive number of the disk to open
61 * Partition: This is zero for floppy drives.
62 * If the disk is a hard disk then this specifies
63 * The partition number to open (1 - 4)
64 * If it is zero then it opens the active (bootable) partition
67 BOOL
FsOpenVolume(ULONG DriveNumber
, ULONG PartitionNumber
)
69 PARTITION_TABLE_ENTRY PartitionTableEntry
;
73 DbgPrint((DPRINT_FILESYSTEM
, "FsOpenVolume() DriveNumber: 0x%x PartitionNumber: 0x%x\n", DriveNumber
, PartitionNumber
));
75 // Check and see if it is a floppy drive
76 // If so then just assume FAT12 file system type
77 if (DiskIsDriveRemovable(DriveNumber
))
79 DbgPrint((DPRINT_FILESYSTEM
, "Drive is a floppy diskette drive. Assuming FAT12 file system.\n"));
82 return FatOpenVolume(DriveNumber
, 0, 0);
85 // Check for ISO9660 file system type
86 if (DriveNumber
>= 0x80 && FsRecIsIso9660(DriveNumber
))
88 DbgPrint((DPRINT_FILESYSTEM
, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n"));
91 return IsoOpenVolume(DriveNumber
);
94 // Set the boot partition
95 BootPartition
= PartitionNumber
;
97 // Get the requested partition entry
98 if (PartitionNumber
== 0)
100 // Partition requested was zero which means the boot partition
101 if (DiskGetActivePartitionEntry(DriveNumber
, &PartitionTableEntry
) == FALSE
)
103 FileSystemError("No active partition.");
109 // Get requested partition
110 if (MachDiskGetPartitionEntry(DriveNumber
, PartitionNumber
, &PartitionTableEntry
) == FALSE
)
112 FileSystemError("Partition not found.");
117 // Check for valid partition
118 if (PartitionTableEntry
.SystemIndicator
== PARTITION_ENTRY_UNUSED
)
120 FileSystemError("Invalid partition.");
124 // Try to recognize the file system
125 if (!FsRecognizeVolume(DriveNumber
, PartitionTableEntry
.SectorCountBeforePartition
, &VolumeType
))
127 FileSystemError("Unrecognized file system.");
131 //switch (PartitionTableEntry.SystemIndicator)
134 case PARTITION_FAT_12
:
135 case PARTITION_FAT_16
:
137 case PARTITION_XINT13
:
138 case PARTITION_FAT32
:
139 case PARTITION_FAT32_XINT13
:
141 return FatOpenVolume(DriveNumber
, PartitionTableEntry
.SectorCountBeforePartition
, PartitionTableEntry
.PartitionSectorCount
);
144 return Ext2OpenVolume(DriveNumber
, PartitionTableEntry
.SectorCountBeforePartition
);
147 return NtfsOpenVolume(DriveNumber
, PartitionTableEntry
.SectorCountBeforePartition
);
150 sprintf(ErrorText
, "Unsupported file system. Type: 0x%x", VolumeType
);
151 FileSystemError(ErrorText
);
158 PFILE
FsOpenFile(PUCHAR FileName
)
160 PFILE FileHandle
= NULL
;
163 // Print status message
165 DbgPrint((DPRINT_FILESYSTEM
, "Opening file '%s'...\n", FileName
));
168 // Check and see if the first character is '\' or '/' and remove it if so
170 while ((*FileName
== '\\') || (*FileName
== '/'))
176 // Check file system type and pass off to appropriate handler
181 FileHandle
= FatOpenFile(FileName
);
184 FileHandle
= IsoOpenFile(FileName
);
187 FileHandle
= Ext2OpenFile(FileName
);
190 FileHandle
= NtfsOpenFile(FileName
);
193 FileSystemError("Error: Unknown filesystem.");
199 // Check return value
201 if (FileHandle
!= NULL
)
203 DbgPrint((DPRINT_FILESYSTEM
, "FsOpenFile() succeeded. FileHandle: 0x%x\n", FileHandle
));
207 DbgPrint((DPRINT_FILESYSTEM
, "FsOpenFile() failed.\n"));
209 #endif // defined DEBUG
214 VOID
FsCloseFile(PFILE FileHandle
)
220 * returns number of bytes read or EOF
222 BOOL
FsReadFile(PFILE FileHandle
, ULONG BytesToRead
, ULONG
* BytesRead
, PVOID Buffer
)
224 ULONGLONG BytesReadBig
;
228 // Set the number of bytes read equal to zero
230 if (BytesRead
!= NULL
)
239 return FatReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
243 return IsoReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
247 //return Ext2ReadFile(FileHandle, BytesToRead, BytesRead, Buffer);
248 Success
= Ext2ReadFile(FileHandle
, BytesToRead
, &BytesReadBig
, Buffer
);
249 *BytesRead
= (ULONG
)BytesReadBig
;
254 return NtfsReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
258 FileSystemError("Unknown file system.");
265 ULONG
FsGetFileSize(PFILE FileHandle
)
271 return FatGetFileSize(FileHandle
);
275 return IsoGetFileSize(FileHandle
);
279 return Ext2GetFileSize(FileHandle
);
283 return NtfsGetFileSize(FileHandle
);
286 FileSystemError("Unknown file system.");
293 VOID
FsSetFilePointer(PFILE FileHandle
, ULONG NewFilePointer
)
299 FatSetFilePointer(FileHandle
, NewFilePointer
);
304 IsoSetFilePointer(FileHandle
, NewFilePointer
);
309 Ext2SetFilePointer(FileHandle
, NewFilePointer
);
314 NtfsSetFilePointer(FileHandle
, NewFilePointer
);
318 FileSystemError("Unknown file system.");
323 ULONG
FsGetFilePointer(PFILE FileHandle
)
329 return FatGetFilePointer(FileHandle
);
334 return IsoGetFilePointer(FileHandle
);
339 return Ext2GetFilePointer(FileHandle
);
344 return NtfsGetFilePointer(FileHandle
);
348 FileSystemError("Unknown file system.");
355 BOOL
FsIsEndOfFile(PFILE FileHandle
)
357 if (FsGetFilePointer(FileHandle
) >= FsGetFileSize(FileHandle
))
368 * FsGetNumPathParts()
369 * This function parses a path in the form of dir1\dir2\file1.ext
370 * and returns the number of parts it has (i.e. 3 - dir1,dir2,file1.ext)
372 ULONG
FsGetNumPathParts(PUCHAR Path
)
377 for (i
=0,num
=0; i
<(int)strlen(Path
); i
++)
379 if ((Path
[i
] == '\\') || (Path
[i
] == '/'))
386 DbgPrint((DPRINT_FILESYSTEM
, "FatGetNumPathParts() Path = %s NumPathParts = %d\n", Path
, num
));
392 * FsGetFirstNameFromPath()
393 * This function parses a path in the form of dir1\dir2\file1.ext
394 * and puts the first name of the path (e.g. "dir1") in buffer
395 * compatible with the MSDOS directory structure
397 VOID
FsGetFirstNameFromPath(PUCHAR Buffer
, PUCHAR Path
)
401 // Copy all the characters up to the end of the
402 // string or until we hit a '\' character
403 // and put them in Buffer
404 for (i
=0; i
<(int)strlen(Path
); i
++)
406 if ((Path
[i
] == '\\') || (Path
[i
] == '/'))
418 DbgPrint((DPRINT_FILESYSTEM
, "FatGetFirstNameFromPath() Path = %s FirstName = %s\n", Path
, Buffer
));