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