]>
Commit | Line | Data |
---|---|---|
878ddf1f | 1 | /*++\r |
2 | \r | |
3 | Copyright (c) 2006, Intel Corporation \r | |
4 | All rights reserved. This program and the accompanying materials \r | |
5 | are licensed and made available under the terms and conditions of the BSD License \r | |
6 | which accompanies this distribution. The full text of the license may be found at \r | |
7 | http://opensource.org/licenses/bsd-license.php \r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
11 | \r | |
12 | Module Name:\r | |
13 | \r | |
14 | Mbr.c\r | |
15 | \r | |
16 | Abstract:\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 | |
35 | BOOLEAN\r | |
36 | PartitionValidMbr (\r | |
37 | IN MASTER_BOOT_RECORD *Mbr,\r | |
38 | IN EFI_LBA LastLba\r | |
39 | )\r | |
40 | /*++\r | |
41 | \r | |
42 | Routine Description:\r | |
43 | Test to see if the Mbr buffer is a valid MBR\r | |
44 | \r | |
45 | Arguments: \r | |
46 | Mbr - Parent Handle \r | |
47 | LastLba - Last Lba address on the device.\r | |
48 | \r | |
49 | Returns:\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 | |
112 | BOOLEAN\r | |
113 | PartitionInstallMbrChildHandles (\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 | |
122 | Routine Description:\r | |
123 | Install child handles if the Handle supports MBR format.\r | |
124 | \r | |
125 | Arguments: \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 | |
132 | Returns:\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 | |
3114b334 | 212 | if (Mbr->Partition[Index].OSIndicator == PMBR_GPT_PARTITION) {\r |
878ddf1f | 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 | |
3114b334 | 268 | if ((Mbr->Partition[0].OSIndicator == EXTENDED_DOS_PARTITION) ||\r |
269 | (Mbr->Partition[0].OSIndicator == EXTENDED_WINDOWS_PARTITION)) {\r | |
270 | ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA);\r | |
271 | continue;\r | |
272 | }\r | |
878ddf1f | 273 | HdDev.PartitionNumber = PartitionNumber ++;\r |
274 | HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA) + ExtMbrStartingLba + ParentHdDev.PartitionStart;\r | |
275 | HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA);\r | |
276 | if (HdDev.PartitionStart + HdDev.PartitionSize - 1 >=\r | |
277 | ParentHdDev.PartitionStart + ParentHdDev.PartitionSize) {\r | |
278 | break;\r | |
279 | }\r | |
280 | \r | |
281 | //\r | |
282 | // The signature in EBR(Extended Boot Record) should always be 0.\r | |
283 | //\r | |
284 | *((UINT32 *) &HdDev.Signature[0]) = 0;\r | |
285 | \r | |
286 | Status = PartitionInstallChildHandle (\r | |
287 | This,\r | |
288 | Handle,\r | |
289 | DiskIo,\r | |
290 | BlockIo,\r | |
291 | DevicePath,\r | |
292 | (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,\r | |
293 | HdDev.PartitionStart - ParentHdDev.PartitionStart,\r | |
294 | HdDev.PartitionStart - ParentHdDev.PartitionStart + HdDev.PartitionSize - 1,\r | |
295 | MBR_SIZE,\r | |
296 | (BOOLEAN) (Mbr->Partition[0].OSIndicator == EFI_PARTITION)\r | |
297 | );\r | |
298 | if (!EFI_ERROR (Status)) {\r | |
299 | Found = TRUE;\r | |
300 | }\r | |
301 | \r | |
3114b334 | 302 | if ((Mbr->Partition[1].OSIndicator != EXTENDED_DOS_PARTITION) &&\r |
303 | (Mbr->Partition[1].OSIndicator != EXTENDED_WINDOWS_PARTITION)\r | |
878ddf1f | 304 | ) {\r |
305 | break;\r | |
306 | }\r | |
307 | \r | |
308 | ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[1].StartingLBA);\r | |
309 | //\r | |
310 | // Don't allow partition to be self referencing\r | |
311 | //\r | |
312 | if (ExtMbrStartingLba == 0) {\r | |
313 | break;\r | |
314 | }\r | |
315 | } while (ExtMbrStartingLba < ParentHdDev.PartitionSize);\r | |
316 | }\r | |
317 | \r | |
318 | Done:\r | |
319 | gBS->FreePool (Mbr);\r | |
320 | \r | |
321 | return Found;\r | |
322 | }\r |