3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Decode a hard disk partitioned with the legacy MBR found on most PC's
20 MBR - Master Boot Record is in the first sector of a partitioned hard disk.
21 The MBR supports four partitions per disk. The MBR also contains legacy
22 code that is not run on an EFI system. The legacy code reads the
23 first sector of the active partition into memory and
25 BPB - Boot(?) Parameter Block is in the first sector of a FAT file system.
26 The BPB contains information about the FAT file system. The BPB is
27 always on the first sector of a media. The first sector also contains
28 the legacy boot strap code.
32 #include "Partition.h"
36 IN MASTER_BOOT_RECORD
*Mbr
,
42 Test to see if the Mbr buffer is a valid MBR
46 LastLba - Last Lba address on the device.
49 TRUE - Mbr is a Valid MBR
50 FALSE - Mbr is not a Valid MBR
61 if (Mbr
->Signature
!= MBR_SIGNATURE
) {
65 // The BPB also has this signature, so it can not be used alone.
68 for (Index1
= 0; Index1
< MAX_MBR_PARTITIONS
; Index1
++) {
69 if (Mbr
->Partition
[Index1
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index1
].SizeInLBA
) == 0) {
74 StartingLBA
= UNPACK_UINT32 (Mbr
->Partition
[Index1
].StartingLBA
);
75 EndingLBA
= StartingLBA
+ UNPACK_UINT32 (Mbr
->Partition
[Index1
].SizeInLBA
) - 1;
76 if (EndingLBA
> LastLba
) {
78 // Compatibility Errata:
79 // Some systems try to hide drive space with their INT 13h driver
80 // This does not hide space from the OS driver. This means the MBR
81 // that gets created from DOS is smaller than the MBR created from
82 // a real OS (NT & Win98). This leads to BlockIo->LastBlock being
83 // wrong on some systems FDISKed by the OS.
85 // return FALSE since no block devices on a system are implemented
91 for (Index2
= Index1
+ 1; Index2
< MAX_MBR_PARTITIONS
; Index2
++) {
92 if (Mbr
->Partition
[Index2
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index2
].SizeInLBA
) == 0) {
96 NewEndingLBA
= UNPACK_UINT32 (Mbr
->Partition
[Index2
].StartingLBA
) + UNPACK_UINT32 (Mbr
->Partition
[Index2
].SizeInLBA
) - 1;
97 if (NewEndingLBA
>= StartingLBA
&& UNPACK_UINT32 (Mbr
->Partition
[Index2
].StartingLBA
) <= EndingLBA
) {
99 // This region overlaps with the Index1'th region
106 // Non of the regions overlapped so MBR is O.K.
112 PartitionInstallMbrChildHandles (
113 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
114 IN EFI_HANDLE Handle
,
115 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
116 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
117 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
122 Install child handles if the Handle supports MBR format.
125 This - Calling context.
126 Handle - Parent Handle
127 DiskIo - Parent DiskIo interface
128 BlockIo - Parent BlockIo interface
129 DevicePath - Parent Device Path
132 EFI_SUCCESS - If a child handle was added
133 EFI_MEDIA_CHANGED - Media changed Detected
134 !EFI_SUCCESS - Not found MBR partition.
139 MASTER_BOOT_RECORD
*Mbr
;
140 UINT32 ExtMbrStartingLba
;
142 HARDDRIVE_DEVICE_PATH HdDev
;
143 HARDDRIVE_DEVICE_PATH ParentHdDev
;
145 UINT32 PartitionNumber
;
146 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
147 EFI_DEVICE_PATH_PROTOCOL
*LastDevicePathNode
;
150 Found
= EFI_NOT_FOUND
;
152 Mbr
= AllocatePool (BlockIo
->Media
->BlockSize
);
157 Status
= BlockIo
->ReadBlocks (
159 BlockIo
->Media
->MediaId
,
161 BlockIo
->Media
->BlockSize
,
164 if (EFI_ERROR (Status
) || !PartitionValidMbr (Mbr
, BlockIo
->Media
->LastBlock
)) {
169 // We have a valid mbr - add each partition
172 // Get starting and ending LBA of the parent block device.
174 LastDevicePathNode
= NULL
;
175 ZeroMem (&ParentHdDev
, sizeof (ParentHdDev
));
176 DevicePathNode
= DevicePath
;
177 while (!EfiIsDevicePathEnd (DevicePathNode
)) {
178 LastDevicePathNode
= DevicePathNode
;
179 DevicePathNode
= EfiNextDevicePathNode (DevicePathNode
);
182 if (LastDevicePathNode
!= NULL
) {
183 if (DevicePathType (LastDevicePathNode
) == MEDIA_DEVICE_PATH
&&
184 DevicePathSubType (LastDevicePathNode
) == MEDIA_HARDDRIVE_DP
186 gBS
->CopyMem (&ParentHdDev
, LastDevicePathNode
, sizeof (ParentHdDev
));
188 LastDevicePathNode
= NULL
;
194 ZeroMem (&HdDev
, sizeof (HdDev
));
195 HdDev
.Header
.Type
= MEDIA_DEVICE_PATH
;
196 HdDev
.Header
.SubType
= MEDIA_HARDDRIVE_DP
;
197 SetDevicePathNodeLength (&HdDev
.Header
, sizeof (HdDev
));
198 HdDev
.MBRType
= MBR_TYPE_PCAT
;
199 HdDev
.SignatureType
= SIGNATURE_TYPE_MBR
;
201 if (LastDevicePathNode
== NULL
) {
203 // This is a MBR, add each partition
205 for (Index
= 0; Index
< MAX_MBR_PARTITIONS
; Index
++) {
206 if (Mbr
->Partition
[Index
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index
].SizeInLBA
) == 0) {
208 // Don't use null MBR entries
213 if (Mbr
->Partition
[Index
].OSIndicator
== PMBR_GPT_PARTITION
) {
215 // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.
216 // We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating
217 // this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format
218 // that corrupted the GPT partition.
223 HdDev
.PartitionNumber
= PartitionNumber
++;
224 HdDev
.PartitionStart
= UNPACK_UINT32 (Mbr
->Partition
[Index
].StartingLBA
);
225 HdDev
.PartitionSize
= UNPACK_UINT32 (Mbr
->Partition
[Index
].SizeInLBA
);
226 CopyMem (HdDev
.Signature
, &(Mbr
->UniqueMbrSignature
[0]), sizeof (UINT32
));
228 Status
= PartitionInstallChildHandle (
234 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
235 HdDev
.PartitionStart
,
236 HdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1,
238 (BOOLEAN
) (Mbr
->Partition
[Index
].OSIndicator
== EFI_PARTITION
)
241 if (!EFI_ERROR (Status
)) {
247 // It's an extended partition. Follow the extended partition
248 // chain to get all the logical drives
250 ExtMbrStartingLba
= 0;
254 Status
= BlockIo
->ReadBlocks (
256 BlockIo
->Media
->MediaId
,
258 BlockIo
->Media
->BlockSize
,
261 if (EFI_ERROR (Status
)) {
266 if (Mbr
->Partition
[0].OSIndicator
== 0) {
270 if ((Mbr
->Partition
[0].OSIndicator
== EXTENDED_DOS_PARTITION
) ||
271 (Mbr
->Partition
[0].OSIndicator
== EXTENDED_WINDOWS_PARTITION
)) {
272 ExtMbrStartingLba
= UNPACK_UINT32 (Mbr
->Partition
[0].StartingLBA
);
275 HdDev
.PartitionNumber
= PartitionNumber
++;
276 HdDev
.PartitionStart
= UNPACK_UINT32 (Mbr
->Partition
[0].StartingLBA
) + ExtMbrStartingLba
+ ParentHdDev
.PartitionStart
;
277 HdDev
.PartitionSize
= UNPACK_UINT32 (Mbr
->Partition
[0].SizeInLBA
);
278 if (HdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1 >=
279 ParentHdDev
.PartitionStart
+ ParentHdDev
.PartitionSize
) {
284 // The signature in EBR(Extended Boot Record) should always be 0.
286 *((UINT32
*) &HdDev
.Signature
[0]) = 0;
288 Status
= PartitionInstallChildHandle (
294 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
295 HdDev
.PartitionStart
- ParentHdDev
.PartitionStart
,
296 HdDev
.PartitionStart
- ParentHdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1,
298 (BOOLEAN
) (Mbr
->Partition
[0].OSIndicator
== EFI_PARTITION
)
300 if (!EFI_ERROR (Status
)) {
304 if ((Mbr
->Partition
[1].OSIndicator
!= EXTENDED_DOS_PARTITION
) &&
305 (Mbr
->Partition
[1].OSIndicator
!= EXTENDED_WINDOWS_PARTITION
)
310 ExtMbrStartingLba
= UNPACK_UINT32 (Mbr
->Partition
[1].StartingLBA
);
312 // Don't allow partition to be self referencing
314 if (ExtMbrStartingLba
== 0) {
317 } while (ExtMbrStartingLba
< ParentHdDev
.PartitionSize
);