VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware2/VBoxPkg/PartitionDxe/Mbr.c@ 42653

Last change on this file since 42653 was 42653, checked in by vboxsync, 13 years ago

EFI/OvmfPkg: import PartitionDxe from OvmfPkg.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 KB
Line 
1/* $Id: Mbr.c 42653 2012-08-07 10:32:00Z vboxsync $ */
2/** @file
3 * Mbr.c
4 */
5
6/*
7 * Copyright (C) 2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/** @file
19 Decode a hard disk partitioned with the legacy MBR found on most PC's
20
21 MBR - Master Boot Record is in the first sector of a partitioned hard disk.
22 The MBR supports four partitions per disk. The MBR also contains legacy
23 code that is not run on an EFI system. The legacy code reads the
24 first sector of the active partition into memory and
25
26 BPB - BIOS Parameter Block is in the first sector of a FAT file system.
27 The BPB contains information about the FAT file system. The BPB is
28 always on the first sector of a media. The first sector also contains
29 the legacy boot strap code.
30
31Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
32This program and the accompanying materials
33are licensed and made available under the terms and conditions of the BSD License
34which accompanies this distribution. The full text of the license may be found at
35http://opensource.org/licenses/bsd-license.php
36
37THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
38WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
39
40**/
41
42#include "Partition.h"
43
44/**
45 Test to see if the Mbr buffer is a valid MBR.
46
47 @param Mbr Parent Handle.
48 @param LastLba Last Lba address on the device.
49
50 @retval TRUE Mbr is a Valid MBR.
51 @retval FALSE Mbr is not a Valid MBR.
52
53**/
54BOOLEAN
55PartitionValidMbr (
56 IN MASTER_BOOT_RECORD *Mbr,
57 IN EFI_LBA LastLba
58 )
59{
60 UINT32 StartingLBA;
61 UINT32 EndingLBA;
62 UINT32 NewEndingLBA;
63 INTN Index1;
64 INTN Index2;
65 BOOLEAN MbrValid;
66
67 if (Mbr->Signature != MBR_SIGNATURE) {
68 return FALSE;
69 }
70 //
71 // The BPB also has this signature, so it can not be used alone.
72 //
73 MbrValid = FALSE;
74 for (Index1 = 0; Index1 < MAX_MBR_PARTITIONS; Index1++) {
75 if (Mbr->Partition[Index1].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) == 0) {
76 continue;
77 }
78
79 MbrValid = TRUE;
80 StartingLBA = UNPACK_UINT32 (Mbr->Partition[Index1].StartingLBA);
81 EndingLBA = StartingLBA + UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) - 1;
82 if (EndingLBA > LastLba) {
83 //
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.
90 //
91 // return FALSE since no block devices on a system are implemented
92 // with INT 13h
93 //
94 return FALSE;
95 }
96
97 for (Index2 = Index1 + 1; Index2 < MAX_MBR_PARTITIONS; Index2++) {
98 if (Mbr->Partition[Index2].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index2].SizeInLBA) == 0) {
99 continue;
100 }
101
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) {
104 //
105 // This region overlaps with the Index1'th region
106 //
107 return FALSE;
108 }
109 }
110 }
111 //
112 // None of the regions overlapped so MBR is O.K.
113 //
114 return MbrValid;
115}
116
117
118/**
119 Install child handles if the Handle supports MBR format.
120
121 @param[in] This Calling context.
122 @param[in] Handle Parent Handle.
123 @param[in] DiskIo Parent DiskIo interface.
124 @param[in] BlockIo Parent BlockIo interface.
125 @param[in] BlockIo2 Parent BlockIo2 interface.
126 @param[in] DevicePath Parent Device Path.
127
128 @retval EFI_SUCCESS A child handle was added.
129 @retval EFI_MEDIA_CHANGED Media change was detected.
130 @retval Others MBR partition was not found.
131
132**/
133EFI_STATUS
134PartitionInstallMbrChildHandles (
135 IN EFI_DRIVER_BINDING_PROTOCOL *This,
136 IN EFI_HANDLE Handle,
137 IN EFI_DISK_IO_PROTOCOL *DiskIo,
138 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,
139 IN EFI_BLOCK_IO2_PROTOCOL *BlockIo2,
140 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
141 )
142{
143 EFI_STATUS Status;
144 MASTER_BOOT_RECORD *Mbr;
145 UINT32 ExtMbrStartingLba;
146 UINTN Index;
147 HARDDRIVE_DEVICE_PATH HdDev;
148 HARDDRIVE_DEVICE_PATH ParentHdDev;
149 EFI_STATUS Found;
150 UINT32 PartitionNumber;
151 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
152 EFI_DEVICE_PATH_PROTOCOL *LastDevicePathNode;
153 UINT32 BlockSize;
154 UINT32 MediaId;
155 EFI_LBA LastBlock;
156
157 Found = EFI_NOT_FOUND;
158
159 BlockSize = BlockIo->Media->BlockSize;
160 MediaId = BlockIo->Media->MediaId;
161 LastBlock = BlockIo->Media->LastBlock;
162
163 LogFlowFuncMarkDP(DevicePath);
164 Mbr = AllocatePool (BlockSize);
165 if (Mbr == NULL) {
166 return Found;
167 }
168
169 Status = DiskIo->ReadDisk (
170 DiskIo,
171 MediaId,
172 0,
173 BlockSize,
174 Mbr
175 );
176 if (EFI_ERROR (Status)) {
177 Found = Status;
178 goto Done;
179 }
180 if (!PartitionValidMbr (Mbr, LastBlock)) {
181 goto Done;
182 }
183 //
184 // We have a valid mbr - add each partition
185 //
186 //
187 // Get starting and ending LBA of the parent block device.
188 //
189 LastDevicePathNode = NULL;
190 ZeroMem (&ParentHdDev, sizeof (ParentHdDev));
191 DevicePathNode = DevicePath;
192 while (!IsDevicePathEnd (DevicePathNode)) {
193 LastDevicePathNode = DevicePathNode;
194 DevicePathNode = NextDevicePathNode (DevicePathNode);
195 }
196
197 if (LastDevicePathNode != NULL) {
198 if (DevicePathType (LastDevicePathNode) == MEDIA_DEVICE_PATH &&
199 DevicePathSubType (LastDevicePathNode) == MEDIA_HARDDRIVE_DP
200 ) {
201 CopyMem (&ParentHdDev, LastDevicePathNode, sizeof (ParentHdDev));
202 } else {
203 LastDevicePathNode = NULL;
204 }
205 }
206
207 PartitionNumber = 1;
208
209 ZeroMem (&HdDev, sizeof (HdDev));
210 HdDev.Header.Type = MEDIA_DEVICE_PATH;
211 HdDev.Header.SubType = MEDIA_HARDDRIVE_DP;
212 SetDevicePathNodeLength (&HdDev.Header, sizeof (HdDev));
213 HdDev.MBRType = MBR_TYPE_PCAT;
214 HdDev.SignatureType = SIGNATURE_TYPE_MBR;
215
216 if (LastDevicePathNode == NULL) {
217 //
218 // This is a MBR, add each partition
219 //
220 for (Index = 0; Index < MAX_MBR_PARTITIONS; Index++) {
221 if (Mbr->Partition[Index].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA) == 0) {
222 //
223 // Don't use null MBR entries
224 //
225 continue;
226 }
227
228 if (Mbr->Partition[Index].OSIndicator == PMBR_GPT_PARTITION) {
229 //
230 // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.
231 // We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating
232 // this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format
233 // that corrupted the GPT partition.
234 //
235 continue;
236 }
237
238 HdDev.PartitionNumber = PartitionNumber ++;
239 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[Index].StartingLBA);
240 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA);
241 CopyMem (HdDev.Signature, &(Mbr->UniqueMbrSignature[0]), sizeof (Mbr->UniqueMbrSignature));
242
243 Status = PartitionInstallChildHandle (
244 This,
245 Handle,
246 DiskIo,
247 BlockIo,
248 BlockIo2,
249 DevicePath,
250 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,
251 HdDev.PartitionStart,
252 HdDev.PartitionStart + HdDev.PartitionSize - 1,
253 MBR_SIZE,
254 (BOOLEAN) (Mbr->Partition[Index].OSIndicator == EFI_PARTITION)
255 );
256
257 if (!EFI_ERROR (Status)) {
258 Found = EFI_SUCCESS;
259 }
260 }
261 } else {
262 //
263 // It's an extended partition. Follow the extended partition
264 // chain to get all the logical drives
265 //
266 ExtMbrStartingLba = 0;
267
268 do {
269
270 Status = DiskIo->ReadDisk (
271 DiskIo,
272 MediaId,
273 MultU64x32 (ExtMbrStartingLba, BlockSize),
274 BlockSize,
275 Mbr
276 );
277 if (EFI_ERROR (Status)) {
278 Found = Status;
279 goto Done;
280 }
281
282 if (UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA) == 0) {
283 break;
284 }
285
286 if ((Mbr->Partition[0].OSIndicator == EXTENDED_DOS_PARTITION) ||
287 (Mbr->Partition[0].OSIndicator == EXTENDED_WINDOWS_PARTITION)) {
288 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA);
289 continue;
290 }
291 HdDev.PartitionNumber = PartitionNumber ++;
292 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA) + ExtMbrStartingLba + ParentHdDev.PartitionStart;
293 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA);
294 if ((HdDev.PartitionStart + HdDev.PartitionSize - 1 >= ParentHdDev.PartitionStart + ParentHdDev.PartitionSize) ||
295 (HdDev.PartitionStart <= ParentHdDev.PartitionStart)) {
296 break;
297 }
298
299 //
300 // The signature in EBR(Extended Boot Record) should always be 0.
301 //
302 *((UINT32 *) &HdDev.Signature[0]) = 0;
303
304 Status = PartitionInstallChildHandle (
305 This,
306 Handle,
307 DiskIo,
308 BlockIo,
309 BlockIo2,
310 DevicePath,
311 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,
312 HdDev.PartitionStart - ParentHdDev.PartitionStart,
313 HdDev.PartitionStart - ParentHdDev.PartitionStart + HdDev.PartitionSize - 1,
314 MBR_SIZE,
315 (BOOLEAN) (Mbr->Partition[0].OSIndicator == EFI_PARTITION)
316 );
317 if (!EFI_ERROR (Status)) {
318 Found = EFI_SUCCESS;
319 }
320
321 if ((Mbr->Partition[1].OSIndicator != EXTENDED_DOS_PARTITION) &&
322 (Mbr->Partition[1].OSIndicator != EXTENDED_WINDOWS_PARTITION)
323 ) {
324 break;
325 }
326
327 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[1].StartingLBA);
328 //
329 // Don't allow partition to be self referencing
330 //
331 if (ExtMbrStartingLba == 0) {
332 break;
333 }
334 } while (ExtMbrStartingLba < ParentHdDev.PartitionSize);
335 }
336
337Done:
338 FreePool (Mbr);
339
340 return Found;
341}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette