Added full disk partition management functions
[reactos.git] / freeldr / freeldr / fs / fs.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #include <freeldr.h>
21 #include <fs.h>
22 #include "fat.h"
23 #include <disk.h>
24 #include <rtl.h>
25 #include <ui.h>
26 #include <arch.h>
27 #include <debug.h>
28
29
30 /////////////////////////////////////////////////////////////////////////////////////////////
31 // DATA
32 /////////////////////////////////////////////////////////////////////////////////////////////
33
34 ULONG FileSystemType = 0; // Type of filesystem on boot device, set by OpenDiskDrive()
35
36 /////////////////////////////////////////////////////////////////////////////////////////////
37 // FUNCTIONS
38 /////////////////////////////////////////////////////////////////////////////////////////////
39
40 VOID FileSystemError(PUCHAR ErrorString)
41 {
42 DbgPrint((DPRINT_FILESYSTEM, "%s\n", ErrorString));
43
44 if (UserInterfaceUp)
45 {
46 MessageBox(ErrorString);
47 }
48 else
49 {
50 printf("%s", ErrorString);
51 printf("\nPress any key\n");
52 getch();
53 }
54 }
55
56 /*
57 *
58 * BOOL OpenDiskDrive(ULONG DriveNumber, ULONG PartitionNumber);
59 *
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:
63 *
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
69 *
70 */
71 BOOL OpenDiskDrive(ULONG DriveNumber, ULONG PartitionNumber)
72 {
73 PARTITION_TABLE_ENTRY PartitionTableEntry;
74
75 DbgPrint((DPRINT_FILESYSTEM, "OpenDiskDrive() DriveNumber: 0x%x PartitionNumber: 0x%x\n", DriveNumber, PartitionNumber));
76
77 // Check and see if it is a floppy drive
78 // If so then just assume FAT12 file system type
79 if (DiskIsDriveRemovable(DriveNumber))
80 {
81 DbgPrint((DPRINT_FILESYSTEM, "Drive is a floppy diskette drive. Assuming FAT12 file system.\n"));
82
83 FileSystemType = FS_FAT;
84 return FatOpenVolume(DriveNumber, 0);
85 }
86
87 // Set the boot partition
88 BootPartition = PartitionNumber;
89
90 // Get the requested partition entry
91 if (PartitionNumber == 0)
92 {
93 // Partition requested was zero which means the boot partition
94 if (DiskGetActivePartitionEntry(DriveNumber, &PartitionTableEntry) == FALSE)
95 {
96 return FALSE;
97 }
98 }
99 else
100 {
101 // Get requested partition
102 if (DiskGetPartitionEntry(DriveNumber, PartitionNumber, &PartitionTableEntry) == FALSE)
103 {
104 return FALSE;
105 }
106 }
107
108 // Check for valid partition
109 if (PartitionTableEntry.SystemIndicator == PARTITION_ENTRY_UNUSED)
110 {
111 FileSystemError("Invalid partition.");
112 return FALSE;
113 }
114
115 switch (PartitionTableEntry.SystemIndicator)
116 {
117 case PARTITION_FAT_12:
118 case PARTITION_FAT_16:
119 case PARTITION_HUGE:
120 case PARTITION_XINT13:
121 case PARTITION_FAT32:
122 case PARTITION_FAT32_XINT13:
123 FileSystemType = FS_FAT;
124 return FatOpenVolume(DriveNumber, PartitionTableEntry.SectorCountBeforePartition);
125 default:
126 FileSystemType = 0;
127 FileSystemError("Unsupported file system.");
128 return FALSE;
129 }
130
131 return TRUE;
132 }
133
134 PFILE OpenFile(PUCHAR FileName)
135 {
136 PFILE FileHandle = NULL;
137
138 //
139 // Print status message
140 //
141 DbgPrint((DPRINT_FILESYSTEM, "Opening file '%s'...\n", FileName));
142
143 //
144 // Check file system type and pass off to appropriate handler
145 //
146 if (FileSystemType == FS_FAT)
147 {
148 FileHandle = FatOpenFile(FileName);
149 }
150 else
151 {
152 FileSystemError("Error: Unknown filesystem.");
153 }
154
155 #ifdef DEBUG
156 //
157 // Check return value
158 //
159 if (FileHandle != NULL)
160 {
161 DbgPrint((DPRINT_FILESYSTEM, "OpenFile() succeeded. FileHandle: 0x%x\n", FileHandle));
162 }
163 else
164 {
165 DbgPrint((DPRINT_FILESYSTEM, "OpenFile() failed.\n"));
166 }
167 #endif // defined DEBUG
168
169 return FileHandle;
170 }
171
172 VOID CloseFile(PFILE FileHandle)
173 {
174 }
175
176 /*
177 * ReadFile()
178 * returns number of bytes read or EOF
179 */
180 BOOL ReadFile(PFILE FileHandle, ULONG BytesToRead, PULONG BytesRead, PVOID Buffer)
181 {
182 //
183 // Set the number of bytes read equal to zero
184 //
185 if (BytesRead !=NULL)
186 {
187 *BytesRead = 0;
188 }
189
190 switch (FileSystemType)
191 {
192 case FS_FAT:
193
194 return FatReadFile(FileHandle, BytesToRead, BytesRead, Buffer);
195
196 default:
197
198 FileSystemError("Unknown file system.");
199 return FALSE;
200 }
201
202 return FALSE;
203 }
204
205 ULONG GetFileSize(PFILE FileHandle)
206 {
207 switch (FileSystemType)
208 {
209 case FS_FAT:
210
211 return FatGetFileSize(FileHandle);
212
213 default:
214 FileSystemError("Unknown file system.");
215 break;
216 }
217
218 return 0;
219 }
220
221 VOID SetFilePointer(PFILE FileHandle, ULONG NewFilePointer)
222 {
223 switch (FileSystemType)
224 {
225 case FS_FAT:
226
227 FatSetFilePointer(FileHandle, NewFilePointer);
228 break;
229
230 default:
231 FileSystemError("Unknown file system.");
232 break;
233 }
234 }
235
236 ULONG GetFilePointer(PFILE FileHandle)
237 {
238 switch (FileSystemType)
239 {
240 case FS_FAT:
241
242 return FatGetFilePointer(FileHandle);
243 break;
244
245 default:
246 FileSystemError("Unknown file system.");
247 break;
248 }
249
250 return 0;
251 }
252
253 BOOL IsEndOfFile(PFILE FileHandle)
254 {
255 if (GetFilePointer(FileHandle) >= GetFileSize(FileHandle))
256 {
257 return TRUE;
258 }
259 else
260 {
261 return FALSE;
262 }
263 }