3 Copyright (c) 2006 - 2007, Intel Corporation
\r
4 All rights reserved. This program and the accompanying materials
\r
5 are licensed and made available under the terms and conditions of the BSD License
\r
6 which accompanies this distribution. The full text of the license may be found at
\r
7 http://opensource.org/licenses/bsd-license.php
\r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
\r
18 Decode a hard disk partitioned with the legacy MBR found on most PC's
\r
20 MBR - Master Boot Record is in the first sector of a partitioned hard disk.
\r
21 The MBR supports four partitions per disk. The MBR also contains legacy
\r
22 code that is not run on an EFI system. The legacy code reads the
\r
23 first sector of the active partition into memory and
\r
25 BPB - Boot(?) Parameter Block is in the first sector of a FAT file system.
\r
26 The BPB contains information about the FAT file system. The BPB is
\r
27 always on the first sector of a media. The first sector also contains
\r
28 the legacy boot strap code.
\r
32 #include "Partition.h"
\r
37 IN MASTER_BOOT_RECORD *Mbr,
\r
42 Routine Description:
\r
43 Test to see if the Mbr buffer is a valid MBR
\r
46 Mbr - Parent Handle
\r
47 LastLba - Last Lba address on the device.
\r
50 TRUE - Mbr is a Valid MBR
\r
51 FALSE - Mbr is not a Valid MBR
\r
57 UINT32 NewEndingLBA;
\r
62 if (Mbr->Signature != MBR_SIGNATURE) {
\r
66 // The BPB also has this signature, so it can not be used alone.
\r
69 for (Index1 = 0; Index1 < MAX_MBR_PARTITIONS; Index1++) {
\r
70 if (Mbr->Partition[Index1].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) == 0) {
\r
75 StartingLBA = UNPACK_UINT32 (Mbr->Partition[Index1].StartingLBA);
\r
76 EndingLBA = StartingLBA + UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) - 1;
\r
77 if (EndingLBA > LastLba) {
\r
79 // Compatibility Errata:
\r
80 // Some systems try to hide drive space with their INT 13h driver
\r
81 // This does not hide space from the OS driver. This means the MBR
\r
82 // that gets created from DOS is smaller than the MBR created from
\r
83 // a real OS (NT & Win98). This leads to BlockIo->LastBlock being
\r
84 // wrong on some systems FDISKed by the OS.
\r
86 // return FALSE since no block devices on a system are implemented
\r
92 for (Index2 = Index1 + 1; Index2 < MAX_MBR_PARTITIONS; Index2++) {
\r
93 if (Mbr->Partition[Index2].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index2].SizeInLBA) == 0) {
\r
97 NewEndingLBA = UNPACK_UINT32 (Mbr->Partition[Index2].StartingLBA) + UNPACK_UINT32 (Mbr->Partition[Index2].SizeInLBA) - 1;
\r
98 if (NewEndingLBA >= StartingLBA && UNPACK_UINT32 (Mbr->Partition[Index2].StartingLBA) <= EndingLBA) {
\r
100 // This region overlaps with the Index1'th region
\r
107 // Non of the regions overlapped so MBR is O.K.
\r
113 PartitionInstallMbrChildHandles (
\r
114 IN EFI_DRIVER_BINDING_PROTOCOL *This,
\r
115 IN EFI_HANDLE Handle,
\r
116 IN EFI_DISK_IO_PROTOCOL *DiskIo,
\r
117 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,
\r
118 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
\r
122 Routine Description:
\r
123 Install child handles if the Handle supports MBR format.
\r
126 This - Calling context.
\r
127 Handle - Parent Handle
\r
128 DiskIo - Parent DiskIo interface
\r
129 BlockIo - Parent BlockIo interface
\r
130 DevicePath - Parent Device Path
\r
133 EFI_SUCCESS - If a child handle was added
\r
134 EFI_MEDIA_CHANGED - Media changed Detected
\r
135 !EFI_SUCCESS - Not found MBR partition.
\r
140 MASTER_BOOT_RECORD *Mbr;
\r
141 UINT32 ExtMbrStartingLba;
\r
143 HARDDRIVE_DEVICE_PATH HdDev;
\r
144 HARDDRIVE_DEVICE_PATH ParentHdDev;
\r
146 UINT32 PartitionNumber;
\r
147 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
\r
148 EFI_DEVICE_PATH_PROTOCOL *LastDevicePathNode;
\r
151 Found = EFI_NOT_FOUND;
\r
153 Mbr = AllocatePool (BlockIo->Media->BlockSize);
\r
158 Status = BlockIo->ReadBlocks (
\r
160 BlockIo->Media->MediaId,
\r
162 BlockIo->Media->BlockSize,
\r
165 if (EFI_ERROR (Status)) {
\r
169 if (!PartitionValidMbr (Mbr, BlockIo->Media->LastBlock)) {
\r
173 // We have a valid mbr - add each partition
\r
176 // Get starting and ending LBA of the parent block device.
\r
178 LastDevicePathNode = NULL;
\r
179 ZeroMem (&ParentHdDev, sizeof (ParentHdDev));
\r
180 DevicePathNode = DevicePath;
\r
181 while (!EfiIsDevicePathEnd (DevicePathNode)) {
\r
182 LastDevicePathNode = DevicePathNode;
\r
183 DevicePathNode = EfiNextDevicePathNode (DevicePathNode);
\r
186 if (LastDevicePathNode != NULL) {
\r
187 if (DevicePathType (LastDevicePathNode) == MEDIA_DEVICE_PATH &&
\r
188 DevicePathSubType (LastDevicePathNode) == MEDIA_HARDDRIVE_DP
\r
190 CopyMem (&ParentHdDev, LastDevicePathNode, sizeof (ParentHdDev));
\r
192 LastDevicePathNode = NULL;
\r
196 PartitionNumber = 1;
\r
198 ZeroMem (&HdDev, sizeof (HdDev));
\r
199 HdDev.Header.Type = MEDIA_DEVICE_PATH;
\r
200 HdDev.Header.SubType = MEDIA_HARDDRIVE_DP;
\r
201 SetDevicePathNodeLength (&HdDev.Header, sizeof (HdDev));
\r
202 HdDev.MBRType = MBR_TYPE_PCAT;
\r
203 HdDev.SignatureType = SIGNATURE_TYPE_MBR;
\r
205 if (LastDevicePathNode == NULL) {
\r
207 // This is a MBR, add each partition
\r
209 for (Index = 0; Index < MAX_MBR_PARTITIONS; Index++) {
\r
210 if (Mbr->Partition[Index].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA) == 0) {
\r
212 // Don't use null MBR entries
\r
217 if (Mbr->Partition[Index].OSIndicator == PMBR_GPT_PARTITION) {
\r
219 // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.
\r
220 // We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating
\r
221 // this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format
\r
222 // that corrupted the GPT partition.
\r
227 HdDev.PartitionNumber = PartitionNumber ++;
\r
228 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[Index].StartingLBA);
\r
229 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA);
\r
230 CopyMem (HdDev.Signature, &(Mbr->UniqueMbrSignature[0]), sizeof (UINT32));
\r
232 Status = PartitionInstallChildHandle (
\r
238 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,
\r
239 HdDev.PartitionStart,
\r
240 HdDev.PartitionStart + HdDev.PartitionSize - 1,
\r
242 (BOOLEAN) (Mbr->Partition[Index].OSIndicator == EFI_PARTITION)
\r
245 if (!EFI_ERROR (Status)) {
\r
246 Found = EFI_SUCCESS;
\r
251 // It's an extended partition. Follow the extended partition
\r
252 // chain to get all the logical drives
\r
254 ExtMbrStartingLba = 0;
\r
258 Status = BlockIo->ReadBlocks (
\r
260 BlockIo->Media->MediaId,
\r
262 BlockIo->Media->BlockSize,
\r
265 if (EFI_ERROR (Status)) {
\r
270 if (Mbr->Partition[0].OSIndicator == 0) {
\r
274 if ((Mbr->Partition[0].OSIndicator == EXTENDED_DOS_PARTITION) ||
\r
275 (Mbr->Partition[0].OSIndicator == EXTENDED_WINDOWS_PARTITION)) {
\r
276 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA);
\r
279 HdDev.PartitionNumber = PartitionNumber ++;
\r
280 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA) + ExtMbrStartingLba + ParentHdDev.PartitionStart;
\r
281 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA);
\r
282 if ((HdDev.PartitionStart + HdDev.PartitionSize - 1 >= ParentHdDev.PartitionStart + ParentHdDev.PartitionSize) ||
\r
283 (HdDev.PartitionStart <= ParentHdDev.PartitionStart)) {
\r
288 // The signature in EBR(Extended Boot Record) should always be 0.
\r
290 *((UINT32 *) &HdDev.Signature[0]) = 0;
\r
292 Status = PartitionInstallChildHandle (
\r
298 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,
\r
299 HdDev.PartitionStart - ParentHdDev.PartitionStart,
\r
300 HdDev.PartitionStart - ParentHdDev.PartitionStart + HdDev.PartitionSize - 1,
\r
302 (BOOLEAN) (Mbr->Partition[0].OSIndicator == EFI_PARTITION)
\r
304 if (!EFI_ERROR (Status)) {
\r
305 Found = EFI_SUCCESS;
\r
308 if ((Mbr->Partition[1].OSIndicator != EXTENDED_DOS_PARTITION) &&
\r
309 (Mbr->Partition[1].OSIndicator != EXTENDED_WINDOWS_PARTITION)
\r
314 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[1].StartingLBA);
\r
316 // Don't allow partition to be self referencing
\r
318 if (ExtMbrStartingLba == 0) {
\r
321 } while (ExtMbrStartingLba < ParentHdDev.PartitionSize);
\r