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