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