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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 BOOLEAN
DiskGetActivePartitionEntry(ULONG DriveNumber
,
25 PPARTITION_TABLE_ENTRY PartitionTableEntry
,
26 ULONG
*ActivePartition
)
28 ULONG BootablePartitionCount
= 0;
29 MASTER_BOOT_RECORD MasterBootRecord
;
32 // Read master boot record
33 if (!DiskReadBootRecord(DriveNumber
, 0, &MasterBootRecord
))
38 // Count the bootable partitions
39 if (MasterBootRecord
.PartitionTable
[0].BootIndicator
== 0x80)
41 BootablePartitionCount
++;
44 if (MasterBootRecord
.PartitionTable
[1].BootIndicator
== 0x80)
46 BootablePartitionCount
++;
49 if (MasterBootRecord
.PartitionTable
[2].BootIndicator
== 0x80)
51 BootablePartitionCount
++;
54 if (MasterBootRecord
.PartitionTable
[3].BootIndicator
== 0x80)
56 BootablePartitionCount
++;
60 // Make sure there was only one bootable partition
61 if (BootablePartitionCount
== 0)
63 DPRINTM(DPRINT_DISK
, "No bootable (active) partitions found.\n");
66 else if (BootablePartitionCount
!= 1)
68 DPRINTM(DPRINT_DISK
, "Too many bootable (active) partitions found.\n");
72 // Copy the partition table entry
73 RtlCopyMemory(PartitionTableEntry
,
74 &MasterBootRecord
.PartitionTable
[*ActivePartition
- 1],
75 sizeof(PARTITION_TABLE_ENTRY
));
80 BOOLEAN
DiskGetPartitionEntry(ULONG DriveNumber
, ULONG PartitionNumber
, PPARTITION_TABLE_ENTRY PartitionTableEntry
)
82 MASTER_BOOT_RECORD MasterBootRecord
;
83 PARTITION_TABLE_ENTRY ExtendedPartitionTableEntry
;
84 ULONG ExtendedPartitionNumber
;
85 ULONG ExtendedPartitionOffset
;
88 // Read master boot record
89 if (!DiskReadBootRecord(DriveNumber
, 0, &MasterBootRecord
))
94 // If they are asking for a primary
95 // partition then things are easy
96 if (PartitionNumber
< 5)
98 // PartitionNumber is one-based and we need it zero-based
101 // Copy the partition table entry
102 RtlCopyMemory(PartitionTableEntry
, &MasterBootRecord
.PartitionTable
[PartitionNumber
], sizeof(PARTITION_TABLE_ENTRY
));
108 // They want an extended partition entry so we will need
109 // to loop through all the extended partitions on the disk
110 // and return the one they want.
112 ExtendedPartitionNumber
= PartitionNumber
- 5;
114 // Set the initial relative starting sector to 0
115 // This is because extended partition starting
116 // sectors a numbered relative to their parent
117 ExtendedPartitionOffset
= 0;
119 for (Index
=0; Index
<=ExtendedPartitionNumber
; Index
++)
121 // Get the extended partition table entry
122 if (!DiskGetFirstExtendedPartitionEntry(&MasterBootRecord
, &ExtendedPartitionTableEntry
))
127 // Adjust the relative starting sector of the partition
128 ExtendedPartitionTableEntry
.SectorCountBeforePartition
+= ExtendedPartitionOffset
;
129 if (ExtendedPartitionOffset
== 0)
131 // Set the start of the parrent extended partition
132 ExtendedPartitionOffset
= ExtendedPartitionTableEntry
.SectorCountBeforePartition
;
134 // Read the partition boot record
135 if (!DiskReadBootRecord(DriveNumber
, ExtendedPartitionTableEntry
.SectorCountBeforePartition
, &MasterBootRecord
))
140 // Get the first real partition table entry
141 if (!DiskGetFirstPartitionEntry(&MasterBootRecord
, PartitionTableEntry
))
146 // Now correct the start sector of the partition
147 PartitionTableEntry
->SectorCountBeforePartition
+= ExtendedPartitionTableEntry
.SectorCountBeforePartition
;
150 // When we get here we should have the correct entry
151 // already stored in PartitionTableEntry
152 // so just return TRUE
158 BOOLEAN
DiskGetFirstPartitionEntry(PMASTER_BOOT_RECORD MasterBootRecord
, PPARTITION_TABLE_ENTRY PartitionTableEntry
)
162 for (Index
=0; Index
<4; Index
++)
164 // Check the system indicator
165 // If it's not an extended or unused partition
167 if ((MasterBootRecord
->PartitionTable
[Index
].SystemIndicator
!= PARTITION_ENTRY_UNUSED
) &&
168 (MasterBootRecord
->PartitionTable
[Index
].SystemIndicator
!= PARTITION_EXTENDED
) &&
169 (MasterBootRecord
->PartitionTable
[Index
].SystemIndicator
!= PARTITION_XINT13_EXTENDED
))
171 RtlCopyMemory(PartitionTableEntry
, &MasterBootRecord
->PartitionTable
[Index
], sizeof(PARTITION_TABLE_ENTRY
));
179 BOOLEAN
DiskGetFirstExtendedPartitionEntry(PMASTER_BOOT_RECORD MasterBootRecord
, PPARTITION_TABLE_ENTRY PartitionTableEntry
)
183 for (Index
=0; Index
<4; Index
++)
185 // Check the system indicator
186 // If it an extended partition then we're done
187 if ((MasterBootRecord
->PartitionTable
[Index
].SystemIndicator
== PARTITION_EXTENDED
) ||
188 (MasterBootRecord
->PartitionTable
[Index
].SystemIndicator
== PARTITION_XINT13_EXTENDED
))
190 RtlCopyMemory(PartitionTableEntry
, &MasterBootRecord
->PartitionTable
[Index
], sizeof(PARTITION_TABLE_ENTRY
));
198 BOOLEAN
DiskReadBootRecord(ULONG DriveNumber
, ULONGLONG LogicalSectorNumber
, PMASTER_BOOT_RECORD BootRecord
)
202 // Read master boot record
203 if (!MachDiskReadLogicalSectors(DriveNumber
, LogicalSectorNumber
, 1, (PVOID
)DISKREADBUFFER
))
207 RtlCopyMemory(BootRecord
, (PVOID
)DISKREADBUFFER
, sizeof(MASTER_BOOT_RECORD
));
210 DPRINTM(DPRINT_DISK
, "Dumping partition table for drive 0x%x:\n", DriveNumber
);
211 DPRINTM(DPRINT_DISK
, "Boot record logical start sector = %d\n", LogicalSectorNumber
);
212 DPRINTM(DPRINT_DISK
, "sizeof(MASTER_BOOT_RECORD) = 0x%x.\n", sizeof(MASTER_BOOT_RECORD
));
214 for (Index
=0; Index
<4; Index
++)
216 DPRINTM(DPRINT_DISK
, "-------------------------------------------\n");
217 DPRINTM(DPRINT_DISK
, "Partition %d\n", (Index
+ 1));
218 DPRINTM(DPRINT_DISK
, "BootIndicator: 0x%x\n", BootRecord
->PartitionTable
[Index
].BootIndicator
);
219 DPRINTM(DPRINT_DISK
, "StartHead: 0x%x\n", BootRecord
->PartitionTable
[Index
].StartHead
);
220 DPRINTM(DPRINT_DISK
, "StartSector (Plus 2 cylinder bits): 0x%x\n", BootRecord
->PartitionTable
[Index
].StartSector
);
221 DPRINTM(DPRINT_DISK
, "StartCylinder: 0x%x\n", BootRecord
->PartitionTable
[Index
].StartCylinder
);
222 DPRINTM(DPRINT_DISK
, "SystemIndicator: 0x%x\n", BootRecord
->PartitionTable
[Index
].SystemIndicator
);
223 DPRINTM(DPRINT_DISK
, "EndHead: 0x%x\n", BootRecord
->PartitionTable
[Index
].EndHead
);
224 DPRINTM(DPRINT_DISK
, "EndSector (Plus 2 cylinder bits): 0x%x\n", BootRecord
->PartitionTable
[Index
].EndSector
);
225 DPRINTM(DPRINT_DISK
, "EndCylinder: 0x%x\n", BootRecord
->PartitionTable
[Index
].EndCylinder
);
226 DPRINTM(DPRINT_DISK
, "SectorCountBeforePartition: 0x%x\n", BootRecord
->PartitionTable
[Index
].SectorCountBeforePartition
);
227 DPRINTM(DPRINT_DISK
, "PartitionSectorCount: 0x%x\n", BootRecord
->PartitionTable
[Index
].PartitionSectorCount
);
230 // Check the partition table magic value
231 if (BootRecord
->MasterBootRecordMagic
!= 0xaa55)
242 IN PDEVICE_OBJECT DeviceObject
,
243 IN ULONGLONG LogicalSectorNumber
,
245 OUT PMASTER_BOOT_RECORD BootRecord
)
247 ULONG FileId
= (ULONG
)DeviceObject
;
248 LARGE_INTEGER Position
;
252 Position
.QuadPart
= LogicalSectorNumber
* SectorSize
;
253 Status
= ArcSeek(FileId
, &Position
, SeekAbsolute
);
254 if (Status
!= ESUCCESS
)
255 return STATUS_IO_DEVICE_ERROR
;
257 Status
= ArcRead(FileId
, BootRecord
, SectorSize
, &BytesRead
);
258 if (Status
!= ESUCCESS
|| BytesRead
!= SectorSize
)
259 return STATUS_IO_DEVICE_ERROR
;
261 return STATUS_SUCCESS
;
266 IopCopyPartitionRecord(
267 IN BOOLEAN ReturnRecognizedPartitions
,
269 IN PPARTITION_TABLE_ENTRY PartitionTableEntry
,
270 OUT PARTITION_INFORMATION
*PartitionEntry
)
272 BOOLEAN IsRecognized
;
274 IsRecognized
= TRUE
; /* FIXME */
275 if (!IsRecognized
&& ReturnRecognizedPartitions
)
278 PartitionEntry
->StartingOffset
.QuadPart
= (ULONGLONG
)PartitionTableEntry
->SectorCountBeforePartition
* SectorSize
;
279 PartitionEntry
->PartitionLength
.QuadPart
= (ULONGLONG
)PartitionTableEntry
->PartitionSectorCount
* SectorSize
;
280 PartitionEntry
->HiddenSectors
= 0;
281 PartitionEntry
->PartitionNumber
= 0; /* Will be filled later */
282 PartitionEntry
->PartitionType
= PartitionTableEntry
->SystemIndicator
;
283 PartitionEntry
->BootIndicator
= (PartitionTableEntry
->BootIndicator
& 0x80) ? TRUE
: FALSE
;
284 PartitionEntry
->RecognizedPartition
= IsRecognized
;
285 PartitionEntry
->RewritePartition
= FALSE
;
293 IoReadPartitionTable(
294 IN PDEVICE_OBJECT DeviceObject
,
296 IN BOOLEAN ReturnRecognizedPartitions
,
297 OUT PDRIVE_LAYOUT_INFORMATION
*PartitionBuffer
)
299 PMASTER_BOOT_RECORD MasterBootRecord
;
300 PDRIVE_LAYOUT_INFORMATION Partitions
;
301 ULONG NbPartitions
, i
, Size
;
304 *PartitionBuffer
= NULL
;
306 if (SectorSize
< sizeof(MASTER_BOOT_RECORD
))
307 return STATUS_NOT_SUPPORTED
;
309 MasterBootRecord
= ExAllocatePool(NonPagedPool
, SectorSize
);
310 if (!MasterBootRecord
)
311 return STATUS_NO_MEMORY
;
314 ret
= IopReadBootRecord(DeviceObject
, 0, SectorSize
, MasterBootRecord
);
315 if (!NT_SUCCESS(ret
))
317 ExFreePool(MasterBootRecord
);
321 /* Check validity of boot record */
322 if (MasterBootRecord
->MasterBootRecordMagic
!= 0xaa55)
324 ExFreePool(MasterBootRecord
);
325 return STATUS_NOT_SUPPORTED
;
328 /* Count number of partitions */
330 for (i
= 0; i
< 4; i
++)
334 if (MasterBootRecord
->PartitionTable
[i
].SystemIndicator
== PARTITION_EXTENDED
||
335 MasterBootRecord
->PartitionTable
[i
].SystemIndicator
== PARTITION_XINT13_EXTENDED
)
337 /* FIXME: unhandled case; count number of partitions */
342 if (NbPartitions
== 0)
344 ExFreePool(MasterBootRecord
);
345 return STATUS_NOT_SUPPORTED
;
348 /* Allocation space to store partitions */
349 Size
= FIELD_OFFSET(DRIVE_LAYOUT_INFORMATION
, PartitionEntry
) +
350 NbPartitions
* sizeof(PARTITION_INFORMATION
);
351 Partitions
= ExAllocatePool(NonPagedPool
, Size
);
354 ExFreePool(MasterBootRecord
);
355 return STATUS_NO_MEMORY
;
358 /* Count number of partitions */
360 for (i
= 0; i
< 4; i
++)
362 if (IopCopyPartitionRecord(ReturnRecognizedPartitions
,
364 &MasterBootRecord
->PartitionTable
[i
],
365 &Partitions
->PartitionEntry
[NbPartitions
]))
367 Partitions
->PartitionEntry
[NbPartitions
].PartitionNumber
= NbPartitions
+ 1;
371 if (MasterBootRecord
->PartitionTable
[i
].SystemIndicator
== PARTITION_EXTENDED
||
372 MasterBootRecord
->PartitionTable
[i
].SystemIndicator
== PARTITION_XINT13_EXTENDED
)
374 /* FIXME: unhandled case; copy partitions */
379 Partitions
->PartitionCount
= NbPartitions
;
380 Partitions
->Signature
= MasterBootRecord
->Signature
;
381 ExFreePool(MasterBootRecord
);
383 *PartitionBuffer
= Partitions
;
384 return STATUS_SUCCESS
;