3 * Copyright (C) 1998-2002 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.
30 /////////////////////////////////////////////////////////////////////////////////////////////
32 /////////////////////////////////////////////////////////////////////////////////////////////
34 ULONG FileSystemType
= 0; // Type of filesystem on boot device, set by OpenDiskDrive()
36 /////////////////////////////////////////////////////////////////////////////////////////////
38 /////////////////////////////////////////////////////////////////////////////////////////////
40 VOID
FileSystemError(PUCHAR ErrorString
)
42 DbgPrint((DPRINT_FILESYSTEM
, "%s\n", ErrorString
));
46 MessageBox(ErrorString
);
50 printf("%s", ErrorString
);
51 printf("\nPress any key\n");
58 * BOOL OpenDiskDrive(ULONG DriveNumber, ULONG PartitionNumber);
60 * This function is called to open a disk drive for file access.
61 * It must be called before any of the file functions will work.
62 * It takes two parameters:
64 * Drive: The BIOS drive number of the disk to open
65 * Partition: This is zero for floppy drives.
66 * If the disk is a hard disk then this specifies
67 * The partition number to open (1 - 4)
68 * If it is zero then it opens the active (bootable) partition
71 BOOL
OpenDiskDrive(ULONG DriveNumber
, ULONG PartitionNumber
)
73 PARTITION_TABLE_ENTRY PartitionTableEntry
;
75 DbgPrint((DPRINT_FILESYSTEM
, "OpenDiskDrive() DriveNumber: 0x%x PartitionNumber: 0x%x\n", DriveNumber
, PartitionNumber
));
77 // Check and see if it is a floppy drive
78 // If so then just assume FAT12 file system type
79 if (DiskIsDriveRemovable(DriveNumber
))
81 DbgPrint((DPRINT_FILESYSTEM
, "Drive is a floppy diskette drive. Assuming FAT12 file system.\n"));
83 FileSystemType
= FS_FAT
;
84 return FatOpenVolume(DriveNumber
, 0);
87 // Set the boot partition
88 BootPartition
= PartitionNumber
;
90 // Get the requested partition entry
91 if (PartitionNumber
== 0)
93 // Partition requested was zero which means the boot partition
94 if (DiskGetActivePartitionEntry(DriveNumber
, &PartitionTableEntry
) == FALSE
)
101 // Get requested partition
102 if (DiskGetPartitionEntry(DriveNumber
, PartitionNumber
, &PartitionTableEntry
) == FALSE
)
108 // Check for valid partition
109 if (PartitionTableEntry
.SystemIndicator
== PARTITION_ENTRY_UNUSED
)
111 FileSystemError("Invalid partition.");
115 switch (PartitionTableEntry
.SystemIndicator
)
117 case PARTITION_FAT_12
:
118 case PARTITION_FAT_16
:
120 case PARTITION_XINT13
:
121 case PARTITION_FAT32
:
122 case PARTITION_FAT32_XINT13
:
123 FileSystemType
= FS_FAT
;
124 return FatOpenVolume(DriveNumber
, PartitionTableEntry
.SectorCountBeforePartition
);
127 FileSystemError("Unsupported file system.");
134 PFILE
OpenFile(PUCHAR FileName
)
136 PFILE FileHandle
= NULL
;
139 // Print status message
141 DbgPrint((DPRINT_FILESYSTEM
, "Opening file '%s'...\n", FileName
));
144 // Check file system type and pass off to appropriate handler
146 if (FileSystemType
== FS_FAT
)
148 FileHandle
= FatOpenFile(FileName
);
152 FileSystemError("Error: Unknown filesystem.");
157 // Check return value
159 if (FileHandle
!= NULL
)
161 DbgPrint((DPRINT_FILESYSTEM
, "OpenFile() succeeded. FileHandle: 0x%x\n", FileHandle
));
165 DbgPrint((DPRINT_FILESYSTEM
, "OpenFile() failed.\n"));
167 #endif // defined DEBUG
172 VOID
CloseFile(PFILE FileHandle
)
178 * returns number of bytes read or EOF
180 BOOL
ReadFile(PFILE FileHandle
, ULONG BytesToRead
, PULONG BytesRead
, PVOID Buffer
)
183 // Set the number of bytes read equal to zero
185 if (BytesRead
!=NULL
)
190 switch (FileSystemType
)
194 return FatReadFile(FileHandle
, BytesToRead
, BytesRead
, Buffer
);
198 FileSystemError("Unknown file system.");
205 ULONG
GetFileSize(PFILE FileHandle
)
207 switch (FileSystemType
)
211 return FatGetFileSize(FileHandle
);
214 FileSystemError("Unknown file system.");
221 VOID
SetFilePointer(PFILE FileHandle
, ULONG NewFilePointer
)
223 switch (FileSystemType
)
227 FatSetFilePointer(FileHandle
, NewFilePointer
);
231 FileSystemError("Unknown file system.");
236 ULONG
GetFilePointer(PFILE FileHandle
)
238 switch (FileSystemType
)
242 return FatGetFilePointer(FileHandle
);
246 FileSystemError("Unknown file system.");
253 BOOL
IsEndOfFile(PFILE FileHandle
)
255 if (GetFilePointer(FileHandle
) >= GetFileSize(FileHandle
))