]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Universal/Disk/Partition/Dxe/Mbr.c
Make MDE package pass intel IPF compiler with /W4 /WX switched on.
[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
878ddf1f 33\r
34BOOLEAN\r
35PartitionValidMbr (\r
36 IN MASTER_BOOT_RECORD *Mbr,\r
37 IN EFI_LBA LastLba\r
38 )\r
39/*++\r
40\r
41Routine Description:\r
42 Test to see if the Mbr buffer is a valid MBR\r
43\r
44Arguments: \r
45 Mbr - Parent Handle \r
46 LastLba - Last Lba address on the device.\r
47\r
48Returns:\r
49 TRUE - Mbr is a Valid MBR\r
50 FALSE - Mbr is not a Valid MBR\r
51\r
52--*/\r
53{\r
54 UINT32 StartingLBA;\r
55 UINT32 EndingLBA;\r
56 UINT32 NewEndingLBA;\r
57 INTN Index1;\r
58 INTN Index2;\r
59 BOOLEAN MbrValid;\r
60\r
61 if (Mbr->Signature != MBR_SIGNATURE) {\r
62 return FALSE;\r
63 }\r
64 //\r
65 // The BPB also has this signature, so it can not be used alone.\r
66 //\r
67 MbrValid = FALSE;\r
68 for (Index1 = 0; Index1 < MAX_MBR_PARTITIONS; Index1++) {\r
69 if (Mbr->Partition[Index1].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) == 0) {\r
70 continue;\r
71 }\r
72\r
73 MbrValid = TRUE;\r
74 StartingLBA = UNPACK_UINT32 (Mbr->Partition[Index1].StartingLBA);\r
75 EndingLBA = StartingLBA + UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) - 1;\r
76 if (EndingLBA > LastLba) {\r
77 //\r
78 // Compatibility Errata:\r
79 // Some systems try to hide drive space with their INT 13h driver\r
80 // This does not hide space from the OS driver. This means the MBR\r
81 // that gets created from DOS is smaller than the MBR created from\r
82 // a real OS (NT & Win98). This leads to BlockIo->LastBlock being\r
83 // wrong on some systems FDISKed by the OS.\r
84 //\r
85 // return FALSE since no block devices on a system are implemented\r
86 // with INT 13h\r
87 //\r
88 return FALSE;\r
89 }\r
90\r
91 for (Index2 = Index1 + 1; Index2 < MAX_MBR_PARTITIONS; Index2++) {\r
92 if (Mbr->Partition[Index2].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index2].SizeInLBA) == 0) {\r
93 continue;\r
94 }\r
95\r
96 NewEndingLBA = UNPACK_UINT32 (Mbr->Partition[Index2].StartingLBA) + UNPACK_UINT32 (Mbr->Partition[Index2].SizeInLBA) - 1;\r
97 if (NewEndingLBA >= StartingLBA && UNPACK_UINT32 (Mbr->Partition[Index2].StartingLBA) <= EndingLBA) {\r
98 //\r
99 // This region overlaps with the Index1'th region\r
100 //\r
101 return FALSE;\r
102 }\r
103 }\r
104 }\r
105 //\r
106 // Non of the regions overlapped so MBR is O.K.\r
107 //\r
108 return MbrValid;\r
109}\r
110\r
c7c02fab 111EFI_STATUS\r
878ddf1f 112PartitionInstallMbrChildHandles (\r
113 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
114 IN EFI_HANDLE Handle,\r
115 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
116 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
117 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
118 )\r
119/*++\r
120\r
121Routine Description:\r
122 Install child handles if the Handle supports MBR format.\r
123\r
124Arguments: \r
125 This - Calling context.\r
126 Handle - Parent Handle \r
127 DiskIo - Parent DiskIo interface\r
128 BlockIo - Parent BlockIo interface\r
129 DevicePath - Parent Device Path\r
130\r
131Returns:\r
c7c02fab 132 EFI_SUCCESS - If a child handle was added\r
133 EFI_MEDIA_CHANGED - Media changed Detected\r
134 !EFI_SUCCESS - Not found MBR partition.\r
878ddf1f 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
c7c02fab 144 EFI_STATUS Found;\r
878ddf1f 145 UINT32 PartitionNumber;\r
146 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
147 EFI_DEVICE_PATH_PROTOCOL *LastDevicePathNode;\r
148\r
149 Mbr = NULL;\r
c7c02fab 150 Found = EFI_NOT_FOUND;\r
878ddf1f 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
c7c02fab 165 Found = Status;\r
878ddf1f 166 goto Done;\r
167 }\r
168 //\r
169 // We have a valid mbr - add each partition\r
170 //\r
171 //\r
172 // Get starting and ending LBA of the parent block device.\r
173 //\r
174 LastDevicePathNode = NULL;\r
175 ZeroMem (&ParentHdDev, sizeof (ParentHdDev));\r
176 DevicePathNode = DevicePath;\r
177 while (!EfiIsDevicePathEnd (DevicePathNode)) {\r
178 LastDevicePathNode = DevicePathNode;\r
179 DevicePathNode = EfiNextDevicePathNode (DevicePathNode);\r
180 }\r
181\r
182 if (LastDevicePathNode != NULL) {\r
183 if (DevicePathType (LastDevicePathNode) == MEDIA_DEVICE_PATH &&\r
184 DevicePathSubType (LastDevicePathNode) == MEDIA_HARDDRIVE_DP\r
185 ) {\r
186 gBS->CopyMem (&ParentHdDev, LastDevicePathNode, sizeof (ParentHdDev));\r
187 } else {\r
188 LastDevicePathNode = NULL;\r
189 }\r
190 }\r
191\r
192 PartitionNumber = 1;\r
193\r
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
3114b334 213 if (Mbr->Partition[Index].OSIndicator == PMBR_GPT_PARTITION) {\r
878ddf1f 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
223 HdDev.PartitionNumber = PartitionNumber ++;\r
224 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[Index].StartingLBA);\r
225 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA);\r
226 CopyMem (HdDev.Signature, &(Mbr->UniqueMbrSignature[0]), sizeof (UINT32));\r
227\r
228 Status = PartitionInstallChildHandle (\r
229 This,\r
230 Handle,\r
231 DiskIo,\r
232 BlockIo,\r
233 DevicePath,\r
234 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,\r
235 HdDev.PartitionStart,\r
236 HdDev.PartitionStart + HdDev.PartitionSize - 1,\r
237 MBR_SIZE,\r
238 (BOOLEAN) (Mbr->Partition[Index].OSIndicator == EFI_PARTITION)\r
239 );\r
240\r
241 if (!EFI_ERROR (Status)) {\r
c7c02fab 242 Found = EFI_SUCCESS;\r
878ddf1f 243 }\r
244 }\r
245 } else {\r
246 //\r
247 // It's an extended partition. Follow the extended partition\r
248 // chain to get all the logical drives\r
249 //\r
250 ExtMbrStartingLba = 0;\r
251\r
252 do {\r
253\r
254 Status = BlockIo->ReadBlocks (\r
255 BlockIo,\r
256 BlockIo->Media->MediaId,\r
257 ExtMbrStartingLba,\r
258 BlockIo->Media->BlockSize,\r
259 Mbr\r
260 );\r
261 if (EFI_ERROR (Status)) {\r
c7c02fab 262 Found = Status;\r
878ddf1f 263 goto Done;\r
264 }\r
265\r
266 if (Mbr->Partition[0].OSIndicator == 0) {\r
267 break;\r
268 }\r
269\r
3114b334 270 if ((Mbr->Partition[0].OSIndicator == EXTENDED_DOS_PARTITION) ||\r
271 (Mbr->Partition[0].OSIndicator == EXTENDED_WINDOWS_PARTITION)) {\r
272 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA);\r
273 continue;\r
274 }\r
878ddf1f 275 HdDev.PartitionNumber = PartitionNumber ++;\r
276 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA) + ExtMbrStartingLba + ParentHdDev.PartitionStart;\r
277 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA);\r
278 if (HdDev.PartitionStart + HdDev.PartitionSize - 1 >=\r
279 ParentHdDev.PartitionStart + ParentHdDev.PartitionSize) {\r
280 break;\r
281 }\r
282\r
283 //\r
284 // The signature in EBR(Extended Boot Record) should always be 0.\r
285 //\r
286 *((UINT32 *) &HdDev.Signature[0]) = 0;\r
287\r
288 Status = PartitionInstallChildHandle (\r
289 This,\r
290 Handle,\r
291 DiskIo,\r
292 BlockIo,\r
293 DevicePath,\r
294 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,\r
295 HdDev.PartitionStart - ParentHdDev.PartitionStart,\r
296 HdDev.PartitionStart - ParentHdDev.PartitionStart + HdDev.PartitionSize - 1,\r
297 MBR_SIZE,\r
298 (BOOLEAN) (Mbr->Partition[0].OSIndicator == EFI_PARTITION)\r
299 );\r
300 if (!EFI_ERROR (Status)) {\r
c7c02fab 301 Found = EFI_SUCCESS;\r
878ddf1f 302 }\r
303\r
3114b334 304 if ((Mbr->Partition[1].OSIndicator != EXTENDED_DOS_PARTITION) &&\r
305 (Mbr->Partition[1].OSIndicator != EXTENDED_WINDOWS_PARTITION)\r
878ddf1f 306 ) {\r
307 break;\r
308 }\r
309\r
310 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[1].StartingLBA);\r
311 //\r
312 // Don't allow partition to be self referencing\r
313 //\r
314 if (ExtMbrStartingLba == 0) {\r
315 break;\r
316 }\r
317 } while (ExtMbrStartingLba < ParentHdDev.PartitionSize);\r
318 }\r
319\r
320Done:\r
321 gBS->FreePool (Mbr);\r
322\r
323 return Found;\r
324}\r