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