3 Copyright (c) 2006 - 2007, 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.
33 // Include common header file for this module.
35 #include "CommonHeader.h"
37 #include "Partition.h"
42 IN MASTER_BOOT_RECORD
*Mbr
,
48 Test to see if the Mbr buffer is a valid MBR
52 LastLba - Last Lba address on the device.
55 TRUE - Mbr is a Valid MBR
56 FALSE - Mbr is not a Valid MBR
67 if (Mbr
->Signature
!= MBR_SIGNATURE
) {
71 // The BPB also has this signature, so it can not be used alone.
74 for (Index1
= 0; Index1
< MAX_MBR_PARTITIONS
; Index1
++) {
75 if (Mbr
->Partition
[Index1
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index1
].SizeInLBA
) == 0) {
80 StartingLBA
= UNPACK_UINT32 (Mbr
->Partition
[Index1
].StartingLBA
);
81 EndingLBA
= StartingLBA
+ UNPACK_UINT32 (Mbr
->Partition
[Index1
].SizeInLBA
) - 1;
82 if (EndingLBA
> LastLba
) {
84 // Compatibility Errata:
85 // Some systems try to hide drive space with their INT 13h driver
86 // This does not hide space from the OS driver. This means the MBR
87 // that gets created from DOS is smaller than the MBR created from
88 // a real OS (NT & Win98). This leads to BlockIo->LastBlock being
89 // wrong on some systems FDISKed by the OS.
91 // return FALSE since no block devices on a system are implemented
97 for (Index2
= Index1
+ 1; Index2
< MAX_MBR_PARTITIONS
; Index2
++) {
98 if (Mbr
->Partition
[Index2
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index2
].SizeInLBA
) == 0) {
102 NewEndingLBA
= UNPACK_UINT32 (Mbr
->Partition
[Index2
].StartingLBA
) + UNPACK_UINT32 (Mbr
->Partition
[Index2
].SizeInLBA
) - 1;
103 if (NewEndingLBA
>= StartingLBA
&& UNPACK_UINT32 (Mbr
->Partition
[Index2
].StartingLBA
) <= EndingLBA
) {
105 // This region overlaps with the Index1'th region
112 // Non of the regions overlapped so MBR is O.K.
118 PartitionInstallMbrChildHandles (
119 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
120 IN EFI_HANDLE Handle
,
121 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
122 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
123 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
128 Install child handles if the Handle supports MBR format.
131 This - Calling context.
132 Handle - Parent Handle
133 DiskIo - Parent DiskIo interface
134 BlockIo - Parent BlockIo interface
135 DevicePath - Parent Device Path
138 EFI_SUCCESS - If a child handle was added
139 EFI_MEDIA_CHANGED - Media changed Detected
140 !EFI_SUCCESS - Not found MBR partition.
145 MASTER_BOOT_RECORD
*Mbr
;
146 UINT32 ExtMbrStartingLba
;
148 HARDDRIVE_DEVICE_PATH HdDev
;
149 HARDDRIVE_DEVICE_PATH ParentHdDev
;
151 UINT32 PartitionNumber
;
152 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
153 EFI_DEVICE_PATH_PROTOCOL
*LastDevicePathNode
;
156 Found
= EFI_NOT_FOUND
;
158 Mbr
= AllocatePool (BlockIo
->Media
->BlockSize
);
163 Status
= BlockIo
->ReadBlocks (
165 BlockIo
->Media
->MediaId
,
167 BlockIo
->Media
->BlockSize
,
170 if (EFI_ERROR (Status
)) {
174 if (!PartitionValidMbr (Mbr
, BlockIo
->Media
->LastBlock
)) {
178 // We have a valid mbr - add each partition
181 // Get starting and ending LBA of the parent block device.
183 LastDevicePathNode
= NULL
;
184 ZeroMem (&ParentHdDev
, sizeof (ParentHdDev
));
185 DevicePathNode
= DevicePath
;
186 while (!EfiIsDevicePathEnd (DevicePathNode
)) {
187 LastDevicePathNode
= DevicePathNode
;
188 DevicePathNode
= EfiNextDevicePathNode (DevicePathNode
);
191 if (LastDevicePathNode
!= NULL
) {
192 if (DevicePathType (LastDevicePathNode
) == MEDIA_DEVICE_PATH
&&
193 DevicePathSubType (LastDevicePathNode
) == MEDIA_HARDDRIVE_DP
195 CopyMem (&ParentHdDev
, LastDevicePathNode
, sizeof (ParentHdDev
));
197 LastDevicePathNode
= NULL
;
203 ZeroMem (&HdDev
, sizeof (HdDev
));
204 HdDev
.Header
.Type
= MEDIA_DEVICE_PATH
;
205 HdDev
.Header
.SubType
= MEDIA_HARDDRIVE_DP
;
206 SetDevicePathNodeLength (&HdDev
.Header
, sizeof (HdDev
));
207 HdDev
.MBRType
= MBR_TYPE_PCAT
;
208 HdDev
.SignatureType
= SIGNATURE_TYPE_MBR
;
210 if (LastDevicePathNode
== NULL
) {
212 // This is a MBR, add each partition
214 for (Index
= 0; Index
< MAX_MBR_PARTITIONS
; Index
++) {
215 if (Mbr
->Partition
[Index
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index
].SizeInLBA
) == 0) {
217 // Don't use null MBR entries
222 if (Mbr
->Partition
[Index
].OSIndicator
== PMBR_GPT_PARTITION
) {
224 // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.
225 // We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating
226 // this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format
227 // that corrupted the GPT partition.
232 HdDev
.PartitionNumber
= PartitionNumber
++;
233 HdDev
.PartitionStart
= UNPACK_UINT32 (Mbr
->Partition
[Index
].StartingLBA
);
234 HdDev
.PartitionSize
= UNPACK_UINT32 (Mbr
->Partition
[Index
].SizeInLBA
);
235 CopyMem (HdDev
.Signature
, &(Mbr
->UniqueMbrSignature
[0]), sizeof (UINT32
));
237 Status
= PartitionInstallChildHandle (
243 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
244 HdDev
.PartitionStart
,
245 HdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1,
247 (BOOLEAN
) (Mbr
->Partition
[Index
].OSIndicator
== EFI_PARTITION
)
250 if (!EFI_ERROR (Status
)) {
256 // It's an extended partition. Follow the extended partition
257 // chain to get all the logical drives
259 ExtMbrStartingLba
= 0;
263 Status
= BlockIo
->ReadBlocks (
265 BlockIo
->Media
->MediaId
,
267 BlockIo
->Media
->BlockSize
,
270 if (EFI_ERROR (Status
)) {
275 if (Mbr
->Partition
[0].OSIndicator
== 0) {
279 if ((Mbr
->Partition
[0].OSIndicator
== EXTENDED_DOS_PARTITION
) ||
280 (Mbr
->Partition
[0].OSIndicator
== EXTENDED_WINDOWS_PARTITION
)) {
281 ExtMbrStartingLba
= UNPACK_UINT32 (Mbr
->Partition
[0].StartingLBA
);
284 HdDev
.PartitionNumber
= PartitionNumber
++;
285 HdDev
.PartitionStart
= UNPACK_UINT32 (Mbr
->Partition
[0].StartingLBA
) + ExtMbrStartingLba
+ ParentHdDev
.PartitionStart
;
286 HdDev
.PartitionSize
= UNPACK_UINT32 (Mbr
->Partition
[0].SizeInLBA
);
287 if ((HdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1 >= ParentHdDev
.PartitionStart
+ ParentHdDev
.PartitionSize
) ||
288 (HdDev
.PartitionStart
<= ParentHdDev
.PartitionStart
)) {
293 // The signature in EBR(Extended Boot Record) should always be 0.
295 *((UINT32
*) &HdDev
.Signature
[0]) = 0;
297 Status
= PartitionInstallChildHandle (
303 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
304 HdDev
.PartitionStart
- ParentHdDev
.PartitionStart
,
305 HdDev
.PartitionStart
- ParentHdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1,
307 (BOOLEAN
) (Mbr
->Partition
[0].OSIndicator
== EFI_PARTITION
)
309 if (!EFI_ERROR (Status
)) {
313 if ((Mbr
->Partition
[1].OSIndicator
!= EXTENDED_DOS_PARTITION
) &&
314 (Mbr
->Partition
[1].OSIndicator
!= EXTENDED_WINDOWS_PARTITION
)
319 ExtMbrStartingLba
= UNPACK_UINT32 (Mbr
->Partition
[1].StartingLBA
);
321 // Don't allow partition to be self referencing
323 if (ExtMbrStartingLba
== 0) {
326 } while (ExtMbrStartingLba
< ParentHdDev
.PartitionSize
);