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