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 other - A child handle was not added
138 MASTER_BOOT_RECORD
*Mbr
;
139 UINT32 ExtMbrStartingLba
;
141 HARDDRIVE_DEVICE_PATH HdDev
;
142 HARDDRIVE_DEVICE_PATH ParentHdDev
;
144 UINT32 PartitionNumber
;
145 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
146 EFI_DEVICE_PATH_PROTOCOL
*LastDevicePathNode
;
151 Mbr
= AllocatePool (BlockIo
->Media
->BlockSize
);
156 Status
= BlockIo
->ReadBlocks (
158 BlockIo
->Media
->MediaId
,
160 BlockIo
->Media
->BlockSize
,
163 if (EFI_ERROR (Status
) || !PartitionValidMbr (Mbr
, BlockIo
->Media
->LastBlock
)) {
167 // We have a valid mbr - add each partition
170 // Get starting and ending LBA of the parent block device.
172 LastDevicePathNode
= NULL
;
173 ZeroMem (&ParentHdDev
, sizeof (ParentHdDev
));
174 DevicePathNode
= DevicePath
;
175 while (!EfiIsDevicePathEnd (DevicePathNode
)) {
176 LastDevicePathNode
= DevicePathNode
;
177 DevicePathNode
= EfiNextDevicePathNode (DevicePathNode
);
180 if (LastDevicePathNode
!= NULL
) {
181 if (DevicePathType (LastDevicePathNode
) == MEDIA_DEVICE_PATH
&&
182 DevicePathSubType (LastDevicePathNode
) == MEDIA_HARDDRIVE_DP
184 gBS
->CopyMem (&ParentHdDev
, LastDevicePathNode
, sizeof (ParentHdDev
));
186 LastDevicePathNode
= NULL
;
192 ZeroMem (&HdDev
, sizeof (HdDev
));
193 HdDev
.Header
.Type
= MEDIA_DEVICE_PATH
;
194 HdDev
.Header
.SubType
= MEDIA_HARDDRIVE_DP
;
195 SetDevicePathNodeLength (&HdDev
.Header
, sizeof (HdDev
));
196 HdDev
.MBRType
= MBR_TYPE_PCAT
;
197 HdDev
.SignatureType
= SIGNATURE_TYPE_MBR
;
199 if (LastDevicePathNode
== NULL
) {
201 // This is a MBR, add each partition
203 for (Index
= 0; Index
< MAX_MBR_PARTITIONS
; Index
++) {
204 if (Mbr
->Partition
[Index
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index
].SizeInLBA
) == 0) {
206 // Don't use null MBR entries
211 if (Mbr
->Partition
[Index
].OSIndicator
== PMBR_GPT_PARTITION
) {
213 // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.
214 // We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating
215 // this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format
216 // that corrupted the GPT partition.
221 HdDev
.PartitionNumber
= PartitionNumber
++;
222 HdDev
.PartitionStart
= UNPACK_UINT32 (Mbr
->Partition
[Index
].StartingLBA
);
223 HdDev
.PartitionSize
= UNPACK_UINT32 (Mbr
->Partition
[Index
].SizeInLBA
);
224 CopyMem (HdDev
.Signature
, &(Mbr
->UniqueMbrSignature
[0]), sizeof (UINT32
));
226 Status
= PartitionInstallChildHandle (
232 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
233 HdDev
.PartitionStart
,
234 HdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1,
236 (BOOLEAN
) (Mbr
->Partition
[Index
].OSIndicator
== EFI_PARTITION
)
239 if (!EFI_ERROR (Status
)) {
245 // It's an extended partition. Follow the extended partition
246 // chain to get all the logical drives
248 ExtMbrStartingLba
= 0;
252 Status
= BlockIo
->ReadBlocks (
254 BlockIo
->Media
->MediaId
,
256 BlockIo
->Media
->BlockSize
,
259 if (EFI_ERROR (Status
)) {
263 if (Mbr
->Partition
[0].OSIndicator
== 0) {
267 if ((Mbr
->Partition
[0].OSIndicator
== EXTENDED_DOS_PARTITION
) ||
268 (Mbr
->Partition
[0].OSIndicator
== EXTENDED_WINDOWS_PARTITION
)) {
269 ExtMbrStartingLba
= UNPACK_UINT32 (Mbr
->Partition
[0].StartingLBA
);
272 HdDev
.PartitionNumber
= PartitionNumber
++;
273 HdDev
.PartitionStart
= UNPACK_UINT32 (Mbr
->Partition
[0].StartingLBA
) + ExtMbrStartingLba
+ ParentHdDev
.PartitionStart
;
274 HdDev
.PartitionSize
= UNPACK_UINT32 (Mbr
->Partition
[0].SizeInLBA
);
275 if (HdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1 >=
276 ParentHdDev
.PartitionStart
+ ParentHdDev
.PartitionSize
) {
281 // The signature in EBR(Extended Boot Record) should always be 0.
283 *((UINT32
*) &HdDev
.Signature
[0]) = 0;
285 Status
= PartitionInstallChildHandle (
291 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
292 HdDev
.PartitionStart
- ParentHdDev
.PartitionStart
,
293 HdDev
.PartitionStart
- ParentHdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1,
295 (BOOLEAN
) (Mbr
->Partition
[0].OSIndicator
== EFI_PARTITION
)
297 if (!EFI_ERROR (Status
)) {
301 if ((Mbr
->Partition
[1].OSIndicator
!= EXTENDED_DOS_PARTITION
) &&
302 (Mbr
->Partition
[1].OSIndicator
!= EXTENDED_WINDOWS_PARTITION
)
307 ExtMbrStartingLba
= UNPACK_UINT32 (Mbr
->Partition
[1].StartingLBA
);
309 // Don't allow partition to be self referencing
311 if (ExtMbrStartingLba
== 0) {
314 } while (ExtMbrStartingLba
< ParentHdDev
.PartitionSize
);