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