]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Disk/Partition/Dxe/Mbr.c
add ia32 and x64 direcotry for EdkFvbServiceLib
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / Partition / Dxe / Mbr.c
1 /*++
2
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
8
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.
11
12 Module Name:
13
14 Mbr.c
15
16 Abstract:
17
18 Decode a hard disk partitioned with the legacy MBR found on most PC's
19
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
24
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.
29
30 --*/
31
32 #include "Partition.h"
33
34 STATIC
35 BOOLEAN
36 PartitionValidMbr (
37 IN MASTER_BOOT_RECORD *Mbr,
38 IN EFI_LBA LastLba
39 )
40 /*++
41
42 Routine Description:
43 Test to see if the Mbr buffer is a valid MBR
44
45 Arguments:
46 Mbr - Parent Handle
47 LastLba - Last Lba address on the device.
48
49 Returns:
50 TRUE - Mbr is a Valid MBR
51 FALSE - Mbr is not a Valid MBR
52
53 --*/
54 {
55 UINT32 StartingLBA;
56 UINT32 EndingLBA;
57 UINT32 NewEndingLBA;
58 INTN Index1;
59 INTN Index2;
60 BOOLEAN MbrValid;
61
62 if (Mbr->Signature != MBR_SIGNATURE) {
63 return FALSE;
64 }
65 //
66 // The BPB also has this signature, so it can not be used alone.
67 //
68 MbrValid = FALSE;
69 for (Index1 = 0; Index1 < MAX_MBR_PARTITIONS; Index1++) {
70 if (Mbr->Partition[Index1].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) == 0) {
71 continue;
72 }
73
74 MbrValid = TRUE;
75 StartingLBA = UNPACK_UINT32 (Mbr->Partition[Index1].StartingLBA);
76 EndingLBA = StartingLBA + UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) - 1;
77 if (EndingLBA > LastLba) {
78 //
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.
85 //
86 // return FALSE since no block devices on a system are implemented
87 // with INT 13h
88 //
89 return FALSE;
90 }
91
92 for (Index2 = Index1 + 1; Index2 < MAX_MBR_PARTITIONS; Index2++) {
93 if (Mbr->Partition[Index2].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index2].SizeInLBA) == 0) {
94 continue;
95 }
96
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) {
99 //
100 // This region overlaps with the Index1'th region
101 //
102 return FALSE;
103 }
104 }
105 }
106 //
107 // Non of the regions overlapped so MBR is O.K.
108 //
109 return MbrValid;
110 }
111
112 EFI_STATUS
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
119 )
120 /*++
121
122 Routine Description:
123 Install child handles if the Handle supports MBR format.
124
125 Arguments:
126 This - Calling context.
127 Handle - Parent Handle
128 DiskIo - Parent DiskIo interface
129 BlockIo - Parent BlockIo interface
130 DevicePath - Parent Device Path
131
132 Returns:
133 EFI_SUCCESS - If a child handle was added
134 EFI_MEDIA_CHANGED - Media changed Detected
135 !EFI_SUCCESS - Not found MBR partition.
136
137 --*/
138 {
139 EFI_STATUS Status;
140 MASTER_BOOT_RECORD *Mbr;
141 UINT32 ExtMbrStartingLba;
142 UINTN Index;
143 HARDDRIVE_DEVICE_PATH HdDev;
144 HARDDRIVE_DEVICE_PATH ParentHdDev;
145 EFI_STATUS Found;
146 UINT32 PartitionNumber;
147 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
148 EFI_DEVICE_PATH_PROTOCOL *LastDevicePathNode;
149
150 Mbr = NULL;
151 Found = EFI_NOT_FOUND;
152
153 Mbr = AllocatePool (BlockIo->Media->BlockSize);
154 if (Mbr == NULL) {
155 goto Done;
156 }
157
158 Status = BlockIo->ReadBlocks (
159 BlockIo,
160 BlockIo->Media->MediaId,
161 0,
162 BlockIo->Media->BlockSize,
163 Mbr
164 );
165 if (EFI_ERROR (Status)) {
166 Found = Status;
167 goto Done;
168 }
169 if (!PartitionValidMbr (Mbr, BlockIo->Media->LastBlock)) {
170 goto Done;
171 }
172 //
173 // We have a valid mbr - add each partition
174 //
175 //
176 // Get starting and ending LBA of the parent block device.
177 //
178 LastDevicePathNode = NULL;
179 ZeroMem (&ParentHdDev, sizeof (ParentHdDev));
180 DevicePathNode = DevicePath;
181 while (!EfiIsDevicePathEnd (DevicePathNode)) {
182 LastDevicePathNode = DevicePathNode;
183 DevicePathNode = EfiNextDevicePathNode (DevicePathNode);
184 }
185
186 if (LastDevicePathNode != NULL) {
187 if (DevicePathType (LastDevicePathNode) == MEDIA_DEVICE_PATH &&
188 DevicePathSubType (LastDevicePathNode) == MEDIA_HARDDRIVE_DP
189 ) {
190 CopyMem (&ParentHdDev, LastDevicePathNode, sizeof (ParentHdDev));
191 } else {
192 LastDevicePathNode = NULL;
193 }
194 }
195
196 PartitionNumber = 1;
197
198 ZeroMem (&HdDev, sizeof (HdDev));
199 HdDev.Header.Type = MEDIA_DEVICE_PATH;
200 HdDev.Header.SubType = MEDIA_HARDDRIVE_DP;
201 SetDevicePathNodeLength (&HdDev.Header, sizeof (HdDev));
202 HdDev.MBRType = MBR_TYPE_PCAT;
203 HdDev.SignatureType = SIGNATURE_TYPE_MBR;
204
205 if (LastDevicePathNode == NULL) {
206 //
207 // This is a MBR, add each partition
208 //
209 for (Index = 0; Index < MAX_MBR_PARTITIONS; Index++) {
210 if (Mbr->Partition[Index].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA) == 0) {
211 //
212 // Don't use null MBR entries
213 //
214 continue;
215 }
216
217 if (Mbr->Partition[Index].OSIndicator == PMBR_GPT_PARTITION) {
218 //
219 // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.
220 // We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating
221 // this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format
222 // that corrupted the GPT partition.
223 //
224 continue;
225 }
226
227 HdDev.PartitionNumber = PartitionNumber ++;
228 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[Index].StartingLBA);
229 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA);
230 CopyMem (HdDev.Signature, &(Mbr->UniqueMbrSignature[0]), sizeof (UINT32));
231
232 Status = PartitionInstallChildHandle (
233 This,
234 Handle,
235 DiskIo,
236 BlockIo,
237 DevicePath,
238 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,
239 HdDev.PartitionStart,
240 HdDev.PartitionStart + HdDev.PartitionSize - 1,
241 MBR_SIZE,
242 (BOOLEAN) (Mbr->Partition[Index].OSIndicator == EFI_PARTITION)
243 );
244
245 if (!EFI_ERROR (Status)) {
246 Found = EFI_SUCCESS;
247 }
248 }
249 } else {
250 //
251 // It's an extended partition. Follow the extended partition
252 // chain to get all the logical drives
253 //
254 ExtMbrStartingLba = 0;
255
256 do {
257
258 Status = BlockIo->ReadBlocks (
259 BlockIo,
260 BlockIo->Media->MediaId,
261 ExtMbrStartingLba,
262 BlockIo->Media->BlockSize,
263 Mbr
264 );
265 if (EFI_ERROR (Status)) {
266 Found = Status;
267 goto Done;
268 }
269
270 if (Mbr->Partition[0].OSIndicator == 0) {
271 break;
272 }
273
274 if ((Mbr->Partition[0].OSIndicator == EXTENDED_DOS_PARTITION) ||
275 (Mbr->Partition[0].OSIndicator == EXTENDED_WINDOWS_PARTITION)) {
276 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA);
277 continue;
278 }
279 HdDev.PartitionNumber = PartitionNumber ++;
280 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA) + ExtMbrStartingLba + ParentHdDev.PartitionStart;
281 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA);
282 if ((HdDev.PartitionStart + HdDev.PartitionSize - 1 >= ParentHdDev.PartitionStart + ParentHdDev.PartitionSize) ||
283 (HdDev.PartitionStart <= ParentHdDev.PartitionStart)) {
284 break;
285 }
286
287 //
288 // The signature in EBR(Extended Boot Record) should always be 0.
289 //
290 *((UINT32 *) &HdDev.Signature[0]) = 0;
291
292 Status = PartitionInstallChildHandle (
293 This,
294 Handle,
295 DiskIo,
296 BlockIo,
297 DevicePath,
298 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,
299 HdDev.PartitionStart - ParentHdDev.PartitionStart,
300 HdDev.PartitionStart - ParentHdDev.PartitionStart + HdDev.PartitionSize - 1,
301 MBR_SIZE,
302 (BOOLEAN) (Mbr->Partition[0].OSIndicator == EFI_PARTITION)
303 );
304 if (!EFI_ERROR (Status)) {
305 Found = EFI_SUCCESS;
306 }
307
308 if ((Mbr->Partition[1].OSIndicator != EXTENDED_DOS_PARTITION) &&
309 (Mbr->Partition[1].OSIndicator != EXTENDED_WINDOWS_PARTITION)
310 ) {
311 break;
312 }
313
314 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[1].StartingLBA);
315 //
316 // Don't allow partition to be self referencing
317 //
318 if (ExtMbrStartingLba == 0) {
319 break;
320 }
321 } while (ExtMbrStartingLba < ParentHdDev.PartitionSize);
322 }
323
324 Done:
325 FreePool (Mbr);
326
327 return Found;
328 }