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