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