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