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.
25 BOOL
DiskGetActivePartitionEntry(ULONG DriveNumber
,
26 PPARTITION_TABLE_ENTRY PartitionTableEntry
,
27 ULONG
*ActivePartition
)
29 ULONG BootablePartitionCount
= 0;
30 MASTER_BOOT_RECORD MasterBootRecord
;
33 // Read master boot record
34 if (!DiskReadBootRecord(DriveNumber
, 0, &MasterBootRecord
))
39 // Count the bootable partitions
40 if (MasterBootRecord
.PartitionTable
[0].BootIndicator
== 0x80)
42 BootablePartitionCount
++;
45 if (MasterBootRecord
.PartitionTable
[1].BootIndicator
== 0x80)
47 BootablePartitionCount
++;
50 if (MasterBootRecord
.PartitionTable
[2].BootIndicator
== 0x80)
52 BootablePartitionCount
++;
55 if (MasterBootRecord
.PartitionTable
[3].BootIndicator
== 0x80)
57 BootablePartitionCount
++;
61 // Make sure there was only one bootable partition
62 if (BootablePartitionCount
== 0)
64 DbgPrint((DPRINT_DISK
, "No bootable (active) partitions found.\n"));
67 else if (BootablePartitionCount
!= 1)
69 DbgPrint((DPRINT_DISK
, "Too many bootable (active) partitions found.\n"));
73 // Copy the partition table entry
74 RtlCopyMemory(PartitionTableEntry
,
75 &MasterBootRecord
.PartitionTable
[*ActivePartition
- 1],
76 sizeof(PARTITION_TABLE_ENTRY
));
81 BOOL
DiskGetPartitionEntry(ULONG DriveNumber
, ULONG PartitionNumber
, PPARTITION_TABLE_ENTRY PartitionTableEntry
)
83 MASTER_BOOT_RECORD MasterBootRecord
;
84 PARTITION_TABLE_ENTRY ExtendedPartitionTableEntry
;
85 ULONG ExtendedPartitionNumber
;
86 ULONG ExtendedPartitionOffset
;
89 // Read master boot record
90 if (!DiskReadBootRecord(DriveNumber
, 0, &MasterBootRecord
))
95 // If they are asking for a primary
96 // partition then things are easy
97 if (PartitionNumber
< 5)
99 // PartitionNumber is one-based and we need it zero-based
102 // Copy the partition table entry
103 RtlCopyMemory(PartitionTableEntry
, &MasterBootRecord
.PartitionTable
[PartitionNumber
], sizeof(PARTITION_TABLE_ENTRY
));
109 // They want an extended partition entry so we will need
110 // to loop through all the extended partitions on the disk
111 // and return the one they want.
113 ExtendedPartitionNumber
= PartitionNumber
- 5;
115 // Set the initial relative starting sector to 0
116 // This is because extended partition starting
117 // sectors a numbered relative to their parent
118 ExtendedPartitionOffset
= 0;
120 for (Index
=0; Index
<=ExtendedPartitionNumber
; Index
++)
122 // Get the extended partition table entry
123 if (!DiskGetFirstExtendedPartitionEntry(&MasterBootRecord
, &ExtendedPartitionTableEntry
))
128 // Adjust the relative starting sector of the partition
129 ExtendedPartitionTableEntry
.SectorCountBeforePartition
+= ExtendedPartitionOffset
;
130 if (ExtendedPartitionOffset
== 0)
132 // Set the start of the parrent extended partition
133 ExtendedPartitionOffset
= ExtendedPartitionTableEntry
.SectorCountBeforePartition
;
135 // Read the partition boot record
136 if (!DiskReadBootRecord(DriveNumber
, ExtendedPartitionTableEntry
.SectorCountBeforePartition
, &MasterBootRecord
))
141 // Get the first real partition table entry
142 if (!DiskGetFirstPartitionEntry(&MasterBootRecord
, PartitionTableEntry
))
147 // Now correct the start sector of the partition
148 PartitionTableEntry
->SectorCountBeforePartition
+= ExtendedPartitionTableEntry
.SectorCountBeforePartition
;
151 // When we get here we should have the correct entry
152 // already stored in PartitionTableEntry
153 // so just return TRUE
159 BOOL
DiskGetFirstPartitionEntry(PMASTER_BOOT_RECORD MasterBootRecord
, PPARTITION_TABLE_ENTRY PartitionTableEntry
)
163 for (Index
=0; Index
<4; Index
++)
165 // Check the system indicator
166 // If it's not an extended or unused partition
168 if ((MasterBootRecord
->PartitionTable
[Index
].SystemIndicator
!= PARTITION_ENTRY_UNUSED
) &&
169 (MasterBootRecord
->PartitionTable
[Index
].SystemIndicator
!= PARTITION_EXTENDED
) &&
170 (MasterBootRecord
->PartitionTable
[Index
].SystemIndicator
!= PARTITION_XINT13_EXTENDED
))
172 RtlCopyMemory(PartitionTableEntry
, &MasterBootRecord
->PartitionTable
[Index
], sizeof(PARTITION_TABLE_ENTRY
));
180 BOOL
DiskGetFirstExtendedPartitionEntry(PMASTER_BOOT_RECORD MasterBootRecord
, PPARTITION_TABLE_ENTRY PartitionTableEntry
)
184 for (Index
=0; Index
<4; Index
++)
186 // Check the system indicator
187 // If it an extended partition then we're done
188 if ((MasterBootRecord
->PartitionTable
[Index
].SystemIndicator
== PARTITION_EXTENDED
) ||
189 (MasterBootRecord
->PartitionTable
[Index
].SystemIndicator
== PARTITION_XINT13_EXTENDED
))
191 RtlCopyMemory(PartitionTableEntry
, &MasterBootRecord
->PartitionTable
[Index
], sizeof(PARTITION_TABLE_ENTRY
));
199 BOOL
DiskReadBootRecord(ULONG DriveNumber
, ULONGLONG LogicalSectorNumber
, PMASTER_BOOT_RECORD BootRecord
)
206 // Read master boot record
207 if (!MachDiskReadLogicalSectors(DriveNumber
, LogicalSectorNumber
, 1, (PVOID
)DISKREADBUFFER
))
211 RtlCopyMemory(BootRecord
, (PVOID
)DISKREADBUFFER
, sizeof(MASTER_BOOT_RECORD
));
216 DbgPrint((DPRINT_DISK
, "Dumping partition table for drive 0x%x:\n", DriveNumber
));
217 DbgPrint((DPRINT_DISK
, "Boot record logical start sector = %d\n", LogicalSectorNumber
));
218 DbgPrint((DPRINT_DISK
, "sizeof(MASTER_BOOT_RECORD) = 0x%x.\n", sizeof(MASTER_BOOT_RECORD
)));
220 for (Index
=0; Index
<4; Index
++)
222 DbgPrint((DPRINT_DISK
, "-------------------------------------------\n"));
223 DbgPrint((DPRINT_DISK
, "Partition %d\n", (Index
+ 1)));
224 DbgPrint((DPRINT_DISK
, "BootIndicator: 0x%x\n", BootRecord
->PartitionTable
[Index
].BootIndicator
));
225 DbgPrint((DPRINT_DISK
, "StartHead: 0x%x\n", BootRecord
->PartitionTable
[Index
].StartHead
));
226 DbgPrint((DPRINT_DISK
, "StartSector (Plus 2 cylinder bits): 0x%x\n", BootRecord
->PartitionTable
[Index
].StartSector
));
227 DbgPrint((DPRINT_DISK
, "StartCylinder: 0x%x\n", BootRecord
->PartitionTable
[Index
].StartCylinder
));
228 DbgPrint((DPRINT_DISK
, "SystemIndicator: 0x%x\n", BootRecord
->PartitionTable
[Index
].SystemIndicator
));
229 DbgPrint((DPRINT_DISK
, "EndHead: 0x%x\n", BootRecord
->PartitionTable
[Index
].EndHead
));
230 DbgPrint((DPRINT_DISK
, "EndSector (Plus 2 cylinder bits): 0x%x\n", BootRecord
->PartitionTable
[Index
].EndSector
));
231 DbgPrint((DPRINT_DISK
, "EndCylinder: 0x%x\n", BootRecord
->PartitionTable
[Index
].EndCylinder
));
232 DbgPrint((DPRINT_DISK
, "SectorCountBeforePartition: 0x%x\n", BootRecord
->PartitionTable
[Index
].SectorCountBeforePartition
));
233 DbgPrint((DPRINT_DISK
, "PartitionSectorCount: 0x%x\n", BootRecord
->PartitionTable
[Index
].PartitionSectorCount
));
236 #endif // defined DEBUG
238 // Check the partition table magic value
239 if (BootRecord
->MasterBootRecordMagic
!= 0xaa55)
241 sprintf(ErrMsg
, "Invalid partition table magic 0x%x found on drive 0x%x",
242 BootRecord
->MasterBootRecordMagic
, DriveNumber
);
243 DiskError(ErrMsg
, 0);