]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Disk/PartitionDxe/Mbr.c
Add DiskIo2 protocol definition to MdePkg.
[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 - 2013, Intel Corporation. All rights reserved.<BR>
15 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[in] This Calling context.
105 @param[in] Handle Parent Handle.
106 @param[in] DiskIo Parent DiskIo interface.
107 @param[in] DiskIo2 Parent DiskIo2 interface.
108 @param[in] BlockIo Parent BlockIo interface.
109 @param[in] BlockIo2 Parent BlockIo2 interface.
110 @param[in] DevicePath Parent Device Path.
111
112 @retval EFI_SUCCESS A child handle was added.
113 @retval EFI_MEDIA_CHANGED Media change was detected.
114 @retval Others MBR partition was not found.
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_DISK_IO2_PROTOCOL *DiskIo2,
123 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,
124 IN EFI_BLOCK_IO2_PROTOCOL *BlockIo2,
125 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
126 )
127 {
128 EFI_STATUS Status;
129 MASTER_BOOT_RECORD *Mbr;
130 UINT32 ExtMbrStartingLba;
131 UINTN Index;
132 HARDDRIVE_DEVICE_PATH HdDev;
133 HARDDRIVE_DEVICE_PATH ParentHdDev;
134 EFI_STATUS Found;
135 UINT32 PartitionNumber;
136 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
137 EFI_DEVICE_PATH_PROTOCOL *LastDevicePathNode;
138 UINT32 BlockSize;
139 UINT32 MediaId;
140 EFI_LBA LastBlock;
141
142 Found = EFI_NOT_FOUND;
143
144 BlockSize = BlockIo->Media->BlockSize;
145 MediaId = BlockIo->Media->MediaId;
146 LastBlock = BlockIo->Media->LastBlock;
147
148 Mbr = AllocatePool (BlockSize);
149 if (Mbr == NULL) {
150 return Found;
151 }
152
153 Status = DiskIo->ReadDisk (
154 DiskIo,
155 MediaId,
156 0,
157 BlockSize,
158 Mbr
159 );
160 if (EFI_ERROR (Status)) {
161 Found = Status;
162 goto Done;
163 }
164 if (!PartitionValidMbr (Mbr, LastBlock)) {
165 goto Done;
166 }
167 //
168 // We have a valid mbr - add each partition
169 //
170 //
171 // Get starting and ending LBA of the parent block device.
172 //
173 LastDevicePathNode = NULL;
174 ZeroMem (&ParentHdDev, sizeof (ParentHdDev));
175 DevicePathNode = DevicePath;
176 while (!IsDevicePathEnd (DevicePathNode)) {
177 LastDevicePathNode = DevicePathNode;
178 DevicePathNode = NextDevicePathNode (DevicePathNode);
179 }
180
181 if (LastDevicePathNode != NULL) {
182 if (DevicePathType (LastDevicePathNode) == MEDIA_DEVICE_PATH &&
183 DevicePathSubType (LastDevicePathNode) == MEDIA_HARDDRIVE_DP
184 ) {
185 CopyMem (&ParentHdDev, LastDevicePathNode, sizeof (ParentHdDev));
186 } else {
187 LastDevicePathNode = NULL;
188 }
189 }
190
191 PartitionNumber = 1;
192
193 ZeroMem (&HdDev, sizeof (HdDev));
194 HdDev.Header.Type = MEDIA_DEVICE_PATH;
195 HdDev.Header.SubType = MEDIA_HARDDRIVE_DP;
196 SetDevicePathNodeLength (&HdDev.Header, sizeof (HdDev));
197 HdDev.MBRType = MBR_TYPE_PCAT;
198 HdDev.SignatureType = SIGNATURE_TYPE_MBR;
199
200 if (LastDevicePathNode == NULL) {
201 //
202 // This is a MBR, add each partition
203 //
204 for (Index = 0; Index < MAX_MBR_PARTITIONS; Index++) {
205 if (Mbr->Partition[Index].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA) == 0) {
206 //
207 // Don't use null MBR entries
208 //
209 continue;
210 }
211
212 if (Mbr->Partition[Index].OSIndicator == PMBR_GPT_PARTITION) {
213 //
214 // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.
215 // We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating
216 // this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format
217 // that corrupted the GPT partition.
218 //
219 continue;
220 }
221
222 HdDev.PartitionNumber = PartitionNumber ++;
223 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[Index].StartingLBA);
224 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA);
225 CopyMem (HdDev.Signature, &(Mbr->UniqueMbrSignature[0]), sizeof (Mbr->UniqueMbrSignature));
226
227 Status = PartitionInstallChildHandle (
228 This,
229 Handle,
230 DiskIo,
231 DiskIo2,
232 BlockIo,
233 BlockIo2,
234 DevicePath,
235 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,
236 HdDev.PartitionStart,
237 HdDev.PartitionStart + HdDev.PartitionSize - 1,
238 MBR_SIZE,
239 (BOOLEAN) (Mbr->Partition[Index].OSIndicator == EFI_PARTITION)
240 );
241
242 if (!EFI_ERROR (Status)) {
243 Found = EFI_SUCCESS;
244 }
245 }
246 } else {
247 //
248 // It's an extended partition. Follow the extended partition
249 // chain to get all the logical drives
250 //
251 ExtMbrStartingLba = 0;
252
253 do {
254
255 Status = DiskIo->ReadDisk (
256 DiskIo,
257 MediaId,
258 MultU64x32 (ExtMbrStartingLba, BlockSize),
259 BlockSize,
260 Mbr
261 );
262 if (EFI_ERROR (Status)) {
263 Found = Status;
264 goto Done;
265 }
266
267 if (UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA) == 0) {
268 break;
269 }
270
271 if ((Mbr->Partition[0].OSIndicator == EXTENDED_DOS_PARTITION) ||
272 (Mbr->Partition[0].OSIndicator == EXTENDED_WINDOWS_PARTITION)) {
273 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA);
274 continue;
275 }
276 HdDev.PartitionNumber = PartitionNumber ++;
277 HdDev.PartitionStart = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA) + ExtMbrStartingLba + ParentHdDev.PartitionStart;
278 HdDev.PartitionSize = UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA);
279 if ((HdDev.PartitionStart + HdDev.PartitionSize - 1 >= ParentHdDev.PartitionStart + ParentHdDev.PartitionSize) ||
280 (HdDev.PartitionStart <= ParentHdDev.PartitionStart)) {
281 break;
282 }
283
284 //
285 // The signature in EBR(Extended Boot Record) should always be 0.
286 //
287 *((UINT32 *) &HdDev.Signature[0]) = 0;
288
289 Status = PartitionInstallChildHandle (
290 This,
291 Handle,
292 DiskIo,
293 DiskIo2,
294 BlockIo,
295 BlockIo2,
296 DevicePath,
297 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,
298 HdDev.PartitionStart - ParentHdDev.PartitionStart,
299 HdDev.PartitionStart - ParentHdDev.PartitionStart + HdDev.PartitionSize - 1,
300 MBR_SIZE,
301 (BOOLEAN) (Mbr->Partition[0].OSIndicator == EFI_PARTITION)
302 );
303 if (!EFI_ERROR (Status)) {
304 Found = EFI_SUCCESS;
305 }
306
307 if ((Mbr->Partition[1].OSIndicator != EXTENDED_DOS_PARTITION) &&
308 (Mbr->Partition[1].OSIndicator != EXTENDED_WINDOWS_PARTITION)
309 ) {
310 break;
311 }
312
313 ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[1].StartingLBA);
314 //
315 // Don't allow partition to be self referencing
316 //
317 if (ExtMbrStartingLba == 0) {
318 break;
319 }
320 } while (ExtMbrStartingLba < ParentHdDev.PartitionSize);
321 }
322
323 Done:
324 FreePool (Mbr);
325
326 return Found;
327 }