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