]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Disk/PartitionDxe/Mbr.c
MdeModulePkg: Misc comments and DEBUG messages
[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
493d8e3a 15Copyright (c) 2006 - 2013, 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
135 UINTN Index;\r
136 HARDDRIVE_DEVICE_PATH HdDev;\r
137 HARDDRIVE_DEVICE_PATH ParentHdDev;\r
138 EFI_STATUS Found;\r
139 UINT32 PartitionNumber;\r
140 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
141 EFI_DEVICE_PATH_PROTOCOL *LastDevicePathNode;\r
490b5ea1 142 UINT32 BlockSize;\r
143 UINT32 MediaId;\r
144 EFI_LBA LastBlock;\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
195 PartitionNumber = 1;\r
196\r
197 ZeroMem (&HdDev, sizeof (HdDev));\r
198 HdDev.Header.Type = MEDIA_DEVICE_PATH;\r
199 HdDev.Header.SubType = MEDIA_HARDDRIVE_DP;\r
200 SetDevicePathNodeLength (&HdDev.Header, sizeof (HdDev));\r
201 HdDev.MBRType = MBR_TYPE_PCAT;\r
202 HdDev.SignatureType = SIGNATURE_TYPE_MBR;\r
203\r
204 if (LastDevicePathNode == NULL) {\r
205 //\r
206 // This is a MBR, add each partition\r
207 //\r
208 for (Index = 0; Index < MAX_MBR_PARTITIONS; Index++) {\r
209 if (Mbr->Partition[Index].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA) == 0) {\r
210 //\r
211 // Don't use null MBR entries\r
212 //\r
213 continue;\r
214 }\r
215\r
216 if (Mbr->Partition[Index].OSIndicator == PMBR_GPT_PARTITION) {\r
217 //\r
218 // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.\r
219 // We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating \r
220 // this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format\r
221 // that corrupted the GPT partition. \r
222 //\r
223 continue;\r
224 }\r
225\r
226 HdDev.PartitionNumber = PartitionNumber ++;\r
227 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[Index].StartingLBA);\r
228 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA);\r
48557c65 229 CopyMem (HdDev.Signature, &(Mbr->UniqueMbrSignature[0]), sizeof (Mbr->UniqueMbrSignature));\r
adbcbf8f 230\r
231 Status = PartitionInstallChildHandle (\r
232 This,\r
233 Handle,\r
234 DiskIo,\r
493d8e3a 235 DiskIo2,\r
adbcbf8f 236 BlockIo,\r
490b5ea1 237 BlockIo2,\r
adbcbf8f 238 DevicePath,\r
239 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,\r
240 HdDev.PartitionStart,\r
241 HdDev.PartitionStart + HdDev.PartitionSize - 1,\r
242 MBR_SIZE,\r
243 (BOOLEAN) (Mbr->Partition[Index].OSIndicator == EFI_PARTITION)\r
244 );\r
245\r
246 if (!EFI_ERROR (Status)) {\r
247 Found = EFI_SUCCESS;\r
248 }\r
249 }\r
250 } else {\r
251 //\r
252 // It's an extended partition. Follow the extended partition\r
253 // chain to get all the logical drives\r
254 //\r
255 ExtMbrStartingLba = 0;\r
256\r
257 do {\r
258\r
96f99e1d 259 Status = DiskIo->ReadDisk (\r
260 DiskIo,\r
490b5ea1 261 MediaId,\r
262 MultU64x32 (ExtMbrStartingLba, BlockSize),\r
263 BlockSize,\r
96f99e1d 264 Mbr\r
265 );\r
adbcbf8f 266 if (EFI_ERROR (Status)) {\r
267 Found = Status;\r
268 goto Done;\r
269 }\r
270\r
c63cd426 271 if (UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA) == 0) {\r
adbcbf8f 272 break;\r
273 }\r
274\r
275 if ((Mbr->Partition[0].OSIndicator == EXTENDED_DOS_PARTITION) ||\r
276 (Mbr->Partition[0].OSIndicator == EXTENDED_WINDOWS_PARTITION)) {\r
277 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA);\r
278 continue;\r
279 }\r
280 HdDev.PartitionNumber = PartitionNumber ++;\r
281 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA) + ExtMbrStartingLba + ParentHdDev.PartitionStart;\r
282 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA);\r
283 if ((HdDev.PartitionStart + HdDev.PartitionSize - 1 >= ParentHdDev.PartitionStart + ParentHdDev.PartitionSize) ||\r
284 (HdDev.PartitionStart <= ParentHdDev.PartitionStart)) {\r
285 break;\r
286 }\r
287\r
288 //\r
289 // The signature in EBR(Extended Boot Record) should always be 0.\r
290 //\r
291 *((UINT32 *) &HdDev.Signature[0]) = 0;\r
292\r
293 Status = PartitionInstallChildHandle (\r
490b5ea1 294 This,\r
295 Handle,\r
296 DiskIo,\r
493d8e3a 297 DiskIo2,\r
490b5ea1 298 BlockIo,\r
299 BlockIo2,\r
300 DevicePath,\r
301 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,\r
302 HdDev.PartitionStart - ParentHdDev.PartitionStart,\r
303 HdDev.PartitionStart - ParentHdDev.PartitionStart + HdDev.PartitionSize - 1,\r
304 MBR_SIZE,\r
305 (BOOLEAN) (Mbr->Partition[0].OSIndicator == EFI_PARTITION)\r
306 );\r
adbcbf8f 307 if (!EFI_ERROR (Status)) {\r
308 Found = EFI_SUCCESS;\r
309 }\r
310\r
311 if ((Mbr->Partition[1].OSIndicator != EXTENDED_DOS_PARTITION) &&\r
312 (Mbr->Partition[1].OSIndicator != EXTENDED_WINDOWS_PARTITION)\r
313 ) {\r
314 break;\r
315 }\r
316\r
317 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[1].StartingLBA);\r
318 //\r
319 // Don't allow partition to be self referencing\r
320 //\r
321 if (ExtMbrStartingLba == 0) {\r
322 break;\r
323 }\r
324 } while (ExtMbrStartingLba < ParentHdDev.PartitionSize);\r
325 }\r
326\r
327Done:\r
328 FreePool (Mbr);\r
329\r
330 return Found;\r
331}\r