]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Disk/PartitionDxe/Mbr.c
BaseTools: GlobalData Add a regular expression for a hex number
[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
3a3d62d2 15Copyright (c) 2006 - 2017, 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
3a3d62d2
HW
132 EFI_STATUS Status;\r
133 MASTER_BOOT_RECORD *Mbr;\r
134 UINT32 ExtMbrStartingLba;\r
135 UINT32 Index;\r
136 HARDDRIVE_DEVICE_PATH HdDev;\r
137 HARDDRIVE_DEVICE_PATH ParentHdDev;\r
138 EFI_STATUS Found;\r
139 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
140 EFI_DEVICE_PATH_PROTOCOL *LastDevicePathNode;\r
141 UINT32 BlockSize;\r
142 UINT32 MediaId;\r
143 EFI_LBA LastBlock;\r
144 EFI_PARTITION_INFO_PROTOCOL PartitionInfo;\r
adbcbf8f 145\r
adbcbf8f 146 Found = EFI_NOT_FOUND;\r
147\r
490b5ea1 148 BlockSize = BlockIo->Media->BlockSize;\r
149 MediaId = BlockIo->Media->MediaId;\r
150 LastBlock = BlockIo->Media->LastBlock;\r
151\r
152 Mbr = AllocatePool (BlockSize);\r
adbcbf8f 153 if (Mbr == NULL) {\r
737dfc36 154 return Found;\r
adbcbf8f 155 }\r
156\r
96f99e1d 157 Status = DiskIo->ReadDisk (\r
158 DiskIo,\r
490b5ea1 159 MediaId,\r
96f99e1d 160 0,\r
490b5ea1 161 BlockSize,\r
96f99e1d 162 Mbr\r
163 );\r
adbcbf8f 164 if (EFI_ERROR (Status)) {\r
165 Found = Status;\r
166 goto Done;\r
167 }\r
490b5ea1 168 if (!PartitionValidMbr (Mbr, LastBlock)) {\r
adbcbf8f 169 goto Done;\r
170 }\r
171 //\r
172 // We have a valid mbr - add each partition\r
173 //\r
174 //\r
175 // Get starting and ending LBA of the parent block device.\r
176 //\r
177 LastDevicePathNode = NULL;\r
178 ZeroMem (&ParentHdDev, sizeof (ParentHdDev));\r
179 DevicePathNode = DevicePath;\r
1232b214 180 while (!IsDevicePathEnd (DevicePathNode)) {\r
adbcbf8f 181 LastDevicePathNode = DevicePathNode;\r
1232b214 182 DevicePathNode = NextDevicePathNode (DevicePathNode);\r
adbcbf8f 183 }\r
184\r
185 if (LastDevicePathNode != NULL) {\r
186 if (DevicePathType (LastDevicePathNode) == MEDIA_DEVICE_PATH &&\r
187 DevicePathSubType (LastDevicePathNode) == MEDIA_HARDDRIVE_DP\r
188 ) {\r
189 CopyMem (&ParentHdDev, LastDevicePathNode, sizeof (ParentHdDev));\r
190 } else {\r
191 LastDevicePathNode = NULL;\r
192 }\r
193 }\r
194\r
adbcbf8f 195 ZeroMem (&HdDev, sizeof (HdDev));\r
196 HdDev.Header.Type = MEDIA_DEVICE_PATH;\r
197 HdDev.Header.SubType = MEDIA_HARDDRIVE_DP;\r
198 SetDevicePathNodeLength (&HdDev.Header, sizeof (HdDev));\r
199 HdDev.MBRType = MBR_TYPE_PCAT;\r
200 HdDev.SignatureType = SIGNATURE_TYPE_MBR;\r
201\r
202 if (LastDevicePathNode == NULL) {\r
203 //\r
204 // This is a MBR, add each partition\r
205 //\r
206 for (Index = 0; Index < MAX_MBR_PARTITIONS; Index++) {\r
207 if (Mbr->Partition[Index].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA) == 0) {\r
208 //\r
209 // Don't use null MBR entries\r
210 //\r
211 continue;\r
212 }\r
213\r
214 if (Mbr->Partition[Index].OSIndicator == PMBR_GPT_PARTITION) {\r
215 //\r
216 // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.\r
217 // We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating \r
218 // this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format\r
219 // that corrupted the GPT partition. \r
220 //\r
221 continue;\r
222 }\r
223\r
e665a69d 224 HdDev.PartitionNumber = Index + 1;\r
adbcbf8f 225 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[Index].StartingLBA);\r
226 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA);\r
48557c65 227 CopyMem (HdDev.Signature, &(Mbr->UniqueMbrSignature[0]), sizeof (Mbr->UniqueMbrSignature));\r
adbcbf8f 228\r
3a3d62d2
HW
229 ZeroMem (&PartitionInfo, sizeof (EFI_PARTITION_INFO_PROTOCOL));\r
230 PartitionInfo.Revision = EFI_PARTITION_INFO_PROTOCOL_REVISION;\r
231 PartitionInfo.Type = PARTITION_TYPE_MBR;\r
232 if (Mbr->Partition[Index].OSIndicator == EFI_PARTITION) {\r
233 PartitionInfo.System = 1;\r
234 }\r
235 CopyMem (&PartitionInfo.Info.Mbr, &Mbr->Partition[Index], sizeof (MBR_PARTITION_RECORD));\r
236\r
adbcbf8f 237 Status = PartitionInstallChildHandle (\r
238 This,\r
239 Handle,\r
240 DiskIo,\r
493d8e3a 241 DiskIo2,\r
adbcbf8f 242 BlockIo,\r
490b5ea1 243 BlockIo2,\r
adbcbf8f 244 DevicePath,\r
245 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,\r
3a3d62d2 246 &PartitionInfo,\r
adbcbf8f 247 HdDev.PartitionStart,\r
248 HdDev.PartitionStart + HdDev.PartitionSize - 1,\r
3a3d62d2 249 MBR_SIZE\r
adbcbf8f 250 );\r
251\r
252 if (!EFI_ERROR (Status)) {\r
253 Found = EFI_SUCCESS;\r
254 }\r
255 }\r
256 } else {\r
257 //\r
258 // It's an extended partition. Follow the extended partition\r
259 // chain to get all the logical drives\r
260 //\r
e665a69d 261 Index = 0;\r
adbcbf8f 262 ExtMbrStartingLba = 0;\r
263\r
264 do {\r
265\r
96f99e1d 266 Status = DiskIo->ReadDisk (\r
267 DiskIo,\r
490b5ea1 268 MediaId,\r
269 MultU64x32 (ExtMbrStartingLba, BlockSize),\r
270 BlockSize,\r
96f99e1d 271 Mbr\r
272 );\r
adbcbf8f 273 if (EFI_ERROR (Status)) {\r
274 Found = Status;\r
275 goto Done;\r
276 }\r
277\r
c63cd426 278 if (UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA) == 0) {\r
adbcbf8f 279 break;\r
280 }\r
281\r
282 if ((Mbr->Partition[0].OSIndicator == EXTENDED_DOS_PARTITION) ||\r
283 (Mbr->Partition[0].OSIndicator == EXTENDED_WINDOWS_PARTITION)) {\r
284 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA);\r
285 continue;\r
286 }\r
e665a69d 287 HdDev.PartitionNumber = ++Index;\r
adbcbf8f 288 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA) + ExtMbrStartingLba + ParentHdDev.PartitionStart;\r
289 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA);\r
290 if ((HdDev.PartitionStart + HdDev.PartitionSize - 1 >= ParentHdDev.PartitionStart + ParentHdDev.PartitionSize) ||\r
291 (HdDev.PartitionStart <= ParentHdDev.PartitionStart)) {\r
292 break;\r
293 }\r
294\r
295 //\r
296 // The signature in EBR(Extended Boot Record) should always be 0.\r
297 //\r
298 *((UINT32 *) &HdDev.Signature[0]) = 0;\r
299\r
3a3d62d2
HW
300 ZeroMem (&PartitionInfo, sizeof (EFI_PARTITION_INFO_PROTOCOL));\r
301 PartitionInfo.Revision = EFI_PARTITION_INFO_PROTOCOL_REVISION;\r
302 PartitionInfo.Type = PARTITION_TYPE_MBR;\r
303 if (Mbr->Partition[0].OSIndicator == EFI_PARTITION) {\r
304 PartitionInfo.System = 1;\r
305 }\r
306 CopyMem (&PartitionInfo.Info.Mbr, &Mbr->Partition[0], sizeof (MBR_PARTITION_RECORD));\r
307\r
adbcbf8f 308 Status = PartitionInstallChildHandle (\r
490b5ea1 309 This,\r
310 Handle,\r
311 DiskIo,\r
493d8e3a 312 DiskIo2,\r
490b5ea1 313 BlockIo,\r
314 BlockIo2,\r
315 DevicePath,\r
316 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,\r
3a3d62d2 317 &PartitionInfo,\r
490b5ea1 318 HdDev.PartitionStart - ParentHdDev.PartitionStart,\r
319 HdDev.PartitionStart - ParentHdDev.PartitionStart + HdDev.PartitionSize - 1,\r
3a3d62d2 320 MBR_SIZE\r
490b5ea1 321 );\r
adbcbf8f 322 if (!EFI_ERROR (Status)) {\r
323 Found = EFI_SUCCESS;\r
324 }\r
325\r
326 if ((Mbr->Partition[1].OSIndicator != EXTENDED_DOS_PARTITION) &&\r
327 (Mbr->Partition[1].OSIndicator != EXTENDED_WINDOWS_PARTITION)\r
328 ) {\r
329 break;\r
330 }\r
331\r
332 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[1].StartingLBA);\r
333 //\r
334 // Don't allow partition to be self referencing\r
335 //\r
336 if (ExtMbrStartingLba == 0) {\r
337 break;\r
338 }\r
339 } while (ExtMbrStartingLba < ParentHdDev.PartitionSize);\r
340 }\r
341\r
342Done:\r
343 FreePool (Mbr);\r
344\r
345 return Found;\r
346}\r