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