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