2 Decode a hard disk partitioned with the legacy MBR found on most PC's
4 MBR - Master Boot Record is in the first sector of a partitioned hard disk.
5 The MBR supports four partitions per disk. The MBR also contains legacy
6 code that is not run on an EFI system. The legacy code reads the
7 first sector of the active partition into memory and
9 BPB - BIOS Parameter Block is in the first sector of a FAT file system.
10 The BPB contains information about the FAT file system. The BPB is
11 always on the first sector of a media. The first sector also contains
12 the legacy boot strap code.
14 Copyright (c) 2018 Qualcomm Datacenter Technologies, Inc.
15 Copyright (c) 2014, Hewlett-Packard Development Company, L.P.<BR>
16 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
17 This program and the accompanying materials
18 are licensed and made available under the terms and conditions of the BSD License
19 which accompanies this distribution. The full text of the license may be found at
20 http://opensource.org/licenses/bsd-license.php
22 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
23 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
27 #include "Partition.h"
30 Test to see if the Mbr buffer is a valid MBR.
32 @param Mbr Parent Handle.
33 @param LastLba Last Lba address on the device.
35 @retval TRUE Mbr is a Valid MBR.
36 @retval FALSE Mbr is not a Valid MBR.
41 IN MASTER_BOOT_RECORD
*Mbr
,
52 if (Mbr
->Signature
!= MBR_SIGNATURE
) {
56 // The BPB also has this signature, so it can not be used alone.
59 for (Index1
= 0; Index1
< MAX_MBR_PARTITIONS
; Index1
++) {
60 if (Mbr
->Partition
[Index1
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index1
].SizeInLBA
) == 0) {
65 StartingLBA
= UNPACK_UINT32 (Mbr
->Partition
[Index1
].StartingLBA
);
66 EndingLBA
= StartingLBA
+ UNPACK_UINT32 (Mbr
->Partition
[Index1
].SizeInLBA
) - 1;
67 if (EndingLBA
> LastLba
) {
69 // Compatibility Errata:
70 // Some systems try to hide drive space with their INT 13h driver
71 // This does not hide space from the OS driver. This means the MBR
72 // that gets created from DOS is smaller than the MBR created from
73 // a real OS (NT & Win98). This leads to BlockIo->LastBlock being
74 // wrong on some systems FDISKed by the OS.
76 // return FALSE since no block devices on a system are implemented
80 DEBUG((EFI_D_INFO
, "PartitionValidMbr: Bad MBR partition size EndingLBA(%1x) > LastLBA(%1x)\n", EndingLBA
, LastLba
));
85 for (Index2
= Index1
+ 1; Index2
< MAX_MBR_PARTITIONS
; Index2
++) {
86 if (Mbr
->Partition
[Index2
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index2
].SizeInLBA
) == 0) {
90 NewEndingLBA
= UNPACK_UINT32 (Mbr
->Partition
[Index2
].StartingLBA
) + UNPACK_UINT32 (Mbr
->Partition
[Index2
].SizeInLBA
) - 1;
91 if (NewEndingLBA
>= StartingLBA
&& UNPACK_UINT32 (Mbr
->Partition
[Index2
].StartingLBA
) <= EndingLBA
) {
93 // This region overlaps with the Index1'th region
100 // None of the regions overlapped so MBR is O.K.
107 Install child handles if the Handle supports MBR format.
109 @param[in] This Calling context.
110 @param[in] Handle Parent Handle.
111 @param[in] DiskIo Parent DiskIo interface.
112 @param[in] DiskIo2 Parent DiskIo2 interface.
113 @param[in] BlockIo Parent BlockIo interface.
114 @param[in] BlockIo2 Parent BlockIo2 interface.
115 @param[in] DevicePath Parent Device Path.
117 @retval EFI_SUCCESS A child handle was added.
118 @retval EFI_MEDIA_CHANGED Media change was detected.
119 @retval Others MBR partition was not found.
123 PartitionInstallMbrChildHandles (
124 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
125 IN EFI_HANDLE Handle
,
126 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
127 IN EFI_DISK_IO2_PROTOCOL
*DiskIo2
,
128 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
129 IN EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
,
130 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
134 MASTER_BOOT_RECORD
*Mbr
;
135 UINT32 ExtMbrStartingLba
;
137 HARDDRIVE_DEVICE_PATH HdDev
;
138 HARDDRIVE_DEVICE_PATH ParentHdDev
;
140 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
141 EFI_DEVICE_PATH_PROTOCOL
*LastDevicePathNode
;
145 EFI_PARTITION_INFO_PROTOCOL PartitionInfo
;
147 Found
= EFI_NOT_FOUND
;
149 BlockSize
= BlockIo
->Media
->BlockSize
;
150 MediaId
= BlockIo
->Media
->MediaId
;
151 LastBlock
= BlockIo
->Media
->LastBlock
;
153 Mbr
= AllocatePool (BlockSize
);
158 Status
= DiskIo
->ReadDisk (
165 if (EFI_ERROR (Status
)) {
169 if (!PartitionValidMbr (Mbr
, LastBlock
)) {
173 // We have a valid mbr - add each partition
176 // Get starting and ending LBA of the parent block device.
178 LastDevicePathNode
= NULL
;
179 ZeroMem (&ParentHdDev
, sizeof (ParentHdDev
));
180 DevicePathNode
= DevicePath
;
181 while (!IsDevicePathEnd (DevicePathNode
)) {
182 LastDevicePathNode
= DevicePathNode
;
183 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
186 if (LastDevicePathNode
!= NULL
) {
187 if (DevicePathType (LastDevicePathNode
) == MEDIA_DEVICE_PATH
&&
188 DevicePathSubType (LastDevicePathNode
) == MEDIA_HARDDRIVE_DP
190 CopyMem (&ParentHdDev
, LastDevicePathNode
, sizeof (ParentHdDev
));
192 LastDevicePathNode
= NULL
;
196 ZeroMem (&HdDev
, sizeof (HdDev
));
197 HdDev
.Header
.Type
= MEDIA_DEVICE_PATH
;
198 HdDev
.Header
.SubType
= MEDIA_HARDDRIVE_DP
;
199 SetDevicePathNodeLength (&HdDev
.Header
, sizeof (HdDev
));
200 HdDev
.MBRType
= MBR_TYPE_PCAT
;
201 HdDev
.SignatureType
= SIGNATURE_TYPE_MBR
;
203 if (LastDevicePathNode
== NULL
) {
205 // This is a MBR, add each partition
207 for (Index
= 0; Index
< MAX_MBR_PARTITIONS
; Index
++) {
208 if (Mbr
->Partition
[Index
].OSIndicator
== 0x00 || UNPACK_UINT32 (Mbr
->Partition
[Index
].SizeInLBA
) == 0) {
210 // Don't use null MBR entries
215 if (Mbr
->Partition
[Index
].OSIndicator
== PMBR_GPT_PARTITION
) {
217 // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.
218 // We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating
219 // this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format
220 // that corrupted the GPT partition.
225 HdDev
.PartitionNumber
= Index
+ 1;
226 HdDev
.PartitionStart
= UNPACK_UINT32 (Mbr
->Partition
[Index
].StartingLBA
);
227 HdDev
.PartitionSize
= UNPACK_UINT32 (Mbr
->Partition
[Index
].SizeInLBA
);
228 CopyMem (HdDev
.Signature
, &(Mbr
->UniqueMbrSignature
[0]), sizeof (Mbr
->UniqueMbrSignature
));
230 ZeroMem (&PartitionInfo
, sizeof (EFI_PARTITION_INFO_PROTOCOL
));
231 PartitionInfo
.Revision
= EFI_PARTITION_INFO_PROTOCOL_REVISION
;
232 PartitionInfo
.Type
= PARTITION_TYPE_MBR
;
233 if (Mbr
->Partition
[Index
].OSIndicator
== EFI_PARTITION
) {
234 PartitionInfo
.System
= 1;
236 CopyMem (&PartitionInfo
.Info
.Mbr
, &Mbr
->Partition
[Index
], sizeof (MBR_PARTITION_RECORD
));
238 Status
= PartitionInstallChildHandle (
246 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
248 HdDev
.PartitionStart
,
249 HdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1,
251 ((Mbr
->Partition
[Index
].OSIndicator
== EFI_PARTITION
) ? &gEfiPartTypeSystemPartGuid
: NULL
)
254 if (!EFI_ERROR (Status
)) {
260 // It's an extended partition. Follow the extended partition
261 // chain to get all the logical drives
264 ExtMbrStartingLba
= 0;
268 Status
= DiskIo
->ReadDisk (
271 MultU64x32 (ExtMbrStartingLba
, BlockSize
),
275 if (EFI_ERROR (Status
)) {
280 if (UNPACK_UINT32 (Mbr
->Partition
[0].SizeInLBA
) == 0) {
284 if ((Mbr
->Partition
[0].OSIndicator
== EXTENDED_DOS_PARTITION
) ||
285 (Mbr
->Partition
[0].OSIndicator
== EXTENDED_WINDOWS_PARTITION
)) {
286 ExtMbrStartingLba
= UNPACK_UINT32 (Mbr
->Partition
[0].StartingLBA
);
289 HdDev
.PartitionNumber
= ++Index
;
290 HdDev
.PartitionStart
= UNPACK_UINT32 (Mbr
->Partition
[0].StartingLBA
) + ExtMbrStartingLba
+ ParentHdDev
.PartitionStart
;
291 HdDev
.PartitionSize
= UNPACK_UINT32 (Mbr
->Partition
[0].SizeInLBA
);
292 if ((HdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1 >= ParentHdDev
.PartitionStart
+ ParentHdDev
.PartitionSize
) ||
293 (HdDev
.PartitionStart
<= ParentHdDev
.PartitionStart
)) {
298 // The signature in EBR(Extended Boot Record) should always be 0.
300 *((UINT32
*) &HdDev
.Signature
[0]) = 0;
302 ZeroMem (&PartitionInfo
, sizeof (EFI_PARTITION_INFO_PROTOCOL
));
303 PartitionInfo
.Revision
= EFI_PARTITION_INFO_PROTOCOL_REVISION
;
304 PartitionInfo
.Type
= PARTITION_TYPE_MBR
;
305 if (Mbr
->Partition
[0].OSIndicator
== EFI_PARTITION
) {
306 PartitionInfo
.System
= 1;
308 CopyMem (&PartitionInfo
.Info
.Mbr
, &Mbr
->Partition
[0], sizeof (MBR_PARTITION_RECORD
));
310 Status
= PartitionInstallChildHandle (
318 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
320 HdDev
.PartitionStart
- ParentHdDev
.PartitionStart
,
321 HdDev
.PartitionStart
- ParentHdDev
.PartitionStart
+ HdDev
.PartitionSize
- 1,
323 ((Mbr
->Partition
[0].OSIndicator
== EFI_PARTITION
) ? &gEfiPartTypeSystemPartGuid
: NULL
)
325 if (!EFI_ERROR (Status
)) {
329 if ((Mbr
->Partition
[1].OSIndicator
!= EXTENDED_DOS_PARTITION
) &&
330 (Mbr
->Partition
[1].OSIndicator
!= EXTENDED_WINDOWS_PARTITION
)
335 ExtMbrStartingLba
= UNPACK_UINT32 (Mbr
->Partition
[1].StartingLBA
);
337 // Don't allow partition to be self referencing
339 if (ExtMbrStartingLba
== 0) {
342 } while (ExtMbrStartingLba
< ParentHdDev
.PartitionSize
);