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.
32 #include "Partition.h"
37 IN MASTER_BOOT_RECORD
*Mbr
,
43 Test to see if the Mbr buffer is a valid MBR
47 LastLba - Last Lba address on the device.
50 TRUE - Mbr is a Valid MBR
51 FALSE - Mbr is not a Valid MBR
62 if (Mbr
->Signature
!= MBR_SIGNATURE
) {
66 // The BPB also has this signature, so it can not be used alone.
69 for (Index1
= 0; Index1
< MAX_MBR_PARTITIONS
; Index1
++) {
70 if (Mbr
->Partition
[Index1
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index1
].SizeInLBA
) == 0) {
75 StartingLBA
= UNPACK_UINT32 (Mbr
->Partition
[Index1
].StartingLBA
);
76 EndingLBA
= StartingLBA
+ UNPACK_UINT32 (Mbr
->Partition
[Index1
].SizeInLBA
) - 1;
77 if (EndingLBA
> LastLba
) {
79 // Compatibility Errata:
80 // Some systems try to hide drive space with their INT 13h driver
81 // This does not hide space from the OS driver. This means the MBR
82 // that gets created from DOS is smaller than the MBR created from
83 // a real OS (NT & Win98). This leads to BlockIo->LastBlock being
84 // wrong on some systems FDISKed by the OS.
86 // return FALSE since no block devices on a system are implemented
92 for (Index2
= Index1
+ 1; Index2
< MAX_MBR_PARTITIONS
; Index2
++) {
93 if (Mbr
->Partition
[Index2
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index2
].SizeInLBA
) == 0) {
97 NewEndingLBA
= UNPACK_UINT32 (Mbr
->Partition
[Index2
].StartingLBA
) + UNPACK_UINT32 (Mbr
->Partition
[Index2
].SizeInLBA
) - 1;
98 if (NewEndingLBA
>= StartingLBA
&& UNPACK_UINT32 (Mbr
->Partition
[Index2
].StartingLBA
) <= EndingLBA
) {
100 // This region overlaps with the Index1'th region
107 // Non of the regions overlapped so MBR is O.K.
113 PartitionInstallMbrChildHandles (
114 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
115 IN EFI_HANDLE Handle
,
116 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
117 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
118 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
123 Install child handles if the Handle supports MBR format.
126 This - Calling context.
127 Handle - Parent Handle
128 DiskIo - Parent DiskIo interface
129 BlockIo - Parent BlockIo interface
130 DevicePath - Parent Device Path
133 EFI_SUCCESS - If a child handle was added
134 EFI_MEDIA_CHANGED - Media changed Detected
135 !EFI_SUCCESS - Not found MBR partition.
140 MASTER_BOOT_RECORD
*Mbr
;
141 UINT32 ExtMbrStartingLba
;
143 HARDDRIVE_DEVICE_PATH HdDev
;
144 HARDDRIVE_DEVICE_PATH ParentHdDev
;
146 UINT32 PartitionNumber
;
147 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
148 EFI_DEVICE_PATH_PROTOCOL
*LastDevicePathNode
;
151 Found
= EFI_NOT_FOUND
;
153 Mbr
= AllocatePool (BlockIo
->Media
->BlockSize
);
158 Status
= BlockIo
->ReadBlocks (
160 BlockIo
->Media
->MediaId
,
162 BlockIo
->Media
->BlockSize
,
165 if (EFI_ERROR (Status
) || !PartitionValidMbr (Mbr
, BlockIo
->Media
->LastBlock
)) {
170 // We have a valid mbr - add each partition
173 // Get starting and ending LBA of the parent block device.
175 LastDevicePathNode
= NULL
;
176 ZeroMem (&ParentHdDev
, sizeof (ParentHdDev
));
177 DevicePathNode
= DevicePath
;
178 while (!EfiIsDevicePathEnd (DevicePathNode
)) {
179 LastDevicePathNode
= DevicePathNode
;
180 DevicePathNode
= EfiNextDevicePathNode (DevicePathNode
);
183 if (LastDevicePathNode
!= NULL
) {
184 if (DevicePathType (LastDevicePathNode
) == MEDIA_DEVICE_PATH
&&
185 DevicePathSubType (LastDevicePathNode
) == MEDIA_HARDDRIVE_DP
187 CopyMem (&ParentHdDev
, LastDevicePathNode
, sizeof (ParentHdDev
));
189 LastDevicePathNode
= NULL
;
195 ZeroMem (&HdDev
, sizeof (HdDev
));
196 HdDev
.Header
.Type
= MEDIA_DEVICE_PATH
;
197 HdDev
.Header
.SubType
= MEDIA_HARDDRIVE_DP
;
198 SetDevicePathNodeLength (&HdDev
.Header
, sizeof (HdDev
));
199 HdDev
.MBRType
= MBR_TYPE_PCAT
;
200 HdDev
.SignatureType
= SIGNATURE_TYPE_MBR
;
202 if (LastDevicePathNode
== NULL
) {
204 // This is a MBR, add each partition
206 for (Index
= 0; Index
< MAX_MBR_PARTITIONS
; Index
++) {
207 if (Mbr
->Partition
[Index
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index
].SizeInLBA
) == 0) {
209 // Don't use null MBR entries
214 if (Mbr
->Partition
[Index
].OSIndicator
== PMBR_GPT_PARTITION
) {
216 // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.
217 // We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating
218 // this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format
219 // that corrupted the GPT partition.
224 HdDev
.PartitionNumber
= PartitionNumber
++;
225 HdDev
.PartitionStart
= UNPACK_UINT32 (Mbr
->Partition
[Index
].StartingLBA
);
226 HdDev
.PartitionSize
= UNPACK_UINT32 (Mbr
->Partition
[Index
].SizeInLBA
);
227 CopyMem (HdDev
.Signature
, &(Mbr
->UniqueMbrSignature
[0]), sizeof (UINT32
));
229 Status
= PartitionInstallChildHandle (
235 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
236 HdDev
.PartitionStart
,
237 HdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1,
239 (BOOLEAN
) (Mbr
->Partition
[Index
].OSIndicator
== EFI_PARTITION
)
242 if (!EFI_ERROR (Status
)) {
248 // It's an extended partition. Follow the extended partition
249 // chain to get all the logical drives
251 ExtMbrStartingLba
= 0;
255 Status
= BlockIo
->ReadBlocks (
257 BlockIo
->Media
->MediaId
,
259 BlockIo
->Media
->BlockSize
,
262 if (EFI_ERROR (Status
)) {
267 if (Mbr
->Partition
[0].OSIndicator
== 0) {
271 if ((Mbr
->Partition
[0].OSIndicator
== EXTENDED_DOS_PARTITION
) ||
272 (Mbr
->Partition
[0].OSIndicator
== EXTENDED_WINDOWS_PARTITION
)) {
273 ExtMbrStartingLba
= UNPACK_UINT32 (Mbr
->Partition
[0].StartingLBA
);
276 HdDev
.PartitionNumber
= PartitionNumber
++;
277 HdDev
.PartitionStart
= UNPACK_UINT32 (Mbr
->Partition
[0].StartingLBA
) + ExtMbrStartingLba
+ ParentHdDev
.PartitionStart
;
278 HdDev
.PartitionSize
= UNPACK_UINT32 (Mbr
->Partition
[0].SizeInLBA
);
279 if ((HdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1 >= ParentHdDev
.PartitionStart
+ ParentHdDev
.PartitionSize
) ||
280 (HdDev
.PartitionStart
<= ParentHdDev
.PartitionStart
)) {
285 // The signature in EBR(Extended Boot Record) should always be 0.
287 *((UINT32
*) &HdDev
.Signature
[0]) = 0;
289 Status
= PartitionInstallChildHandle (
295 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
296 HdDev
.PartitionStart
- ParentHdDev
.PartitionStart
,
297 HdDev
.PartitionStart
- ParentHdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1,
299 (BOOLEAN
) (Mbr
->Partition
[0].OSIndicator
== EFI_PARTITION
)
301 if (!EFI_ERROR (Status
)) {
305 if ((Mbr
->Partition
[1].OSIndicator
!= EXTENDED_DOS_PARTITION
) &&
306 (Mbr
->Partition
[1].OSIndicator
!= EXTENDED_WINDOWS_PARTITION
)
311 ExtMbrStartingLba
= UNPACK_UINT32 (Mbr
->Partition
[1].StartingLBA
);
313 // Don't allow partition to be self referencing
315 if (ExtMbrStartingLba
== 0) {
318 } while (ExtMbrStartingLba
< ParentHdDev
.PartitionSize
);