-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddLbaOfs.c
358 lines (308 loc) · 11.5 KB
/
AddLbaOfs.c
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/** @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 - BIOS 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) 2014, Hewlett-Packard Development Company, L.P.<BR>
Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
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"
#if 0
//#include <IndustryStandard/Mbr.h>
///
/// MBR Partition Table
///
typedef struct {
UINT8 BootStrapCode[440];
UINT8 UniqueMbrSignature[4];
UINT8 Unknown[2];
MBR_PARTITION_RECORD Partition[MAX_MBR_PARTITIONS];
UINT16 Signature;
} MASTER_BOOT_RECORD;
#endif
///
/// AddLbaOfs in MBR BootStrapCode[]
///
#define ADDLBAOFS_SIG_STR "AddLbaOfs\0"
#define ADDLBAOFS_SIG_STR_LEN 10
#define ADDLBAOFS_SIG_OFS 0x10
#define ADDLBAOFS_32_OFS 0x28
//#include <Protocol/DevicePath.h>
//#define MBR_TYPE_PCAT 0x01
//#define SIGNATURE_TYPE_MBR 0x01
/**
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
PartitionValidAddLbaOfs (
IN MASTER_BOOT_RECORD *Mbr,
IN EFI_LBA LastLba
)
{
#undef FN
#define FN "PartitionValidMbr"
#define DBG_PartitionValidMbr DL_80 /* DL_DISABLED DL_80 */
#if 0
UINT32 StartingLBA;
UINT32 EndingLBA;
UINT32 NewEndingLBA;
INTN Index1;
INTN Index2;
#endif
BOOLEAN MbrValid;
EFI_LBA AddLbaOfs;
#if 0
/* (MBR_SIGNATURE + 1) OVMF zeroes out 0x55AB?! */
if (Mbr->Signature != MBR_SIGNATURE + 1) {
return FALSE;
}
#endif
DBG_X(DBG_PartitionValidMbr, (PrBufxxdr((UINT8 *)&Mbr->BootStrapCode[ADDLBAOFS_SIG_OFS], ADDLBAOFS_SIG_STR_LEN)));
if (CompareMem(&Mbr->BootStrapCode[ADDLBAOFS_SIG_OFS], ADDLBAOFS_SIG_STR, ADDLBAOFS_SIG_STR_LEN) != 0) {
return FALSE;
}
AddLbaOfs = SwapBytes64(*((UINT64 *)((UINT8 *)&Mbr->BootStrapCode[ADDLBAOFS_32_OFS])));
DBG_PR(DBG_PartitionValidMbr, "AddLbaOfs=%"PRIx64"\n", AddLbaOfs);
MbrValid = TRUE;
return MbrValid;
}
/**
Install child handles if the Handle supports AddLbaOfs format.
@param[in] This Calling context.
@param[in] Handle Parent Handle.
@param[in] DiskIo Parent DiskIo interface.
@param[in] DiskIo2 Parent DiskIo2 interface.
@param[in] BlockIo Parent BlockIo interface.
@param[in] BlockIo2 Parent BlockIo2 interface.
@param[in] 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
PartitionInstallAddLbaOfsChildHandles (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Handle,
IN EFI_DISK_IO_PROTOCOL *DiskIo,
IN EFI_DISK_IO2_PROTOCOL *DiskIo2,
IN EFI_BLOCK_IO_PROTOCOL *BlockIo,
IN EFI_BLOCK_IO2_PROTOCOL *BlockIo2,
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
)
{
#undef FN
#define FN "PartitionInstallAddLbaOfsChildHandles"
#define DBG_PartitionInstallAddLbaOfsChildHandles DL_80 /* DL_DISABLED DL_80 */
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;
UINT32 BlockSize;
UINT32 MediaId;
EFI_LBA LastBlock;
EFI_LBA AddLbaOfs;
EFI_LBA Lba;
DBG_PR(DBG_PartitionInstallAddLbaOfsChildHandles, "entered\n");
Found = EFI_NOT_FOUND;
BlockSize = BlockIo->Media->BlockSize;
MediaId = BlockIo->Media->MediaId;
LastBlock = BlockIo->Media->LastBlock;
DBG_PR(DBG_PartitionInstallAddLbaOfsChildHandles, "BlockSize=%"PRIx64" MediaId=%"PRIx64" LastBlock=%"PRIx64"\n", BlockSize, MediaId, LastBlock);
Mbr = AllocatePool (BlockSize);
if (Mbr == NULL) {
return Found;
}
for (Lba = 0; Lba < 1; Lba++) {
Status = DiskIo->ReadDisk(
DiskIo,
MediaId,
Lba * BlockSize,
BlockSize,
Mbr
);
DBG_PR(DBG_PartitionInstallAddLbaOfsChildHandles, "ReadDisk MediaId=%"PRIx32" Lba*BlockSize=%"PRIx64" %r\n", MediaId, Lba * BlockSize, Status);
if (!EFI_ERROR(Status)) {
DBG_X(DBG_PartitionInstallAddLbaOfsChildHandles, (PrBufxxdr(Mbr, BlockSize)));
if (PartitionValidAddLbaOfs (Mbr, LastBlock)) {
goto ValidMbr;
}
}
#if 0
Status = DiskIo->ReadDisk(
DiskIo,
MediaId,
(LastBlock - Lba) * BlockSize,
BlockSize,
Mbr
);
DBG_PR(DBG_PartitionInstallAddLbaOfsChildHandles, "ReadDisk MediaId=%"PRIx32" (LastBlock-Lba)*BlockSize=%"PRIx64" %r\n", MediaId, (LastBlock - Lba) * BlockSize, Status);
if (!EFI_ERROR(Status)) {
DBG_X(DBG_PartitionInstallAddLbaOfsChildHandles, (PrBufxxdr(Mbr, BlockSize)));
if (PartitionValidAddLbaOfs (Mbr, LastBlock)) {
goto ValidMbr;
}
}
#endif
} /* for */
goto Done;
ValidMbr:
DBG_PR(DBG_PartitionInstallAddLbaOfsChildHandles, "ValidMbr\n");
//
// 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; //TBD MEDIA_VENDOR_DP
SetDevicePathNodeLength (&HdDev.Header, sizeof (HdDev));
HdDev.MBRType = MBR_TYPE_PCAT; //TBD
HdDev.SignatureType = SIGNATURE_TYPE_GUID + 1; //TBD
if (LastDevicePathNode == NULL) {
//
// This is a MBR, add each partition
//
Index = 0;
/* for (Index = 0; Index < MAX_MBR_PARTITIONS; Index++) { */
AddLbaOfs = SwapBytes64(*((UINT64 *)((UINT8 *)&Mbr->BootStrapCode[ADDLBAOFS_32_OFS])));
DBG_PR(DBG_PartitionInstallAddLbaOfsChildHandles, "AddLbaOfs=%"PRIx64"\n", AddLbaOfs);
HdDev.PartitionNumber = PartitionNumber ++;
HdDev.PartitionStart = AddLbaOfs; /* UNPACK_UINT32 (Mbr->Partition[Index].StartingLBA); */
HdDev.PartitionSize = LastBlock + 1 - AddLbaOfs; /* UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA); */
CopyMem (HdDev.Signature, &(Mbr->UniqueMbrSignature[0]), sizeof (Mbr->UniqueMbrSignature));
Status = PartitionInstallChildHandle (
This,
Handle,
DiskIo,
DiskIo2,
BlockIo,
BlockIo2,
DevicePath,
(EFI_DEVICE_PATH_PROTOCOL *) &HdDev,
HdDev.PartitionStart,
HdDev.PartitionStart + HdDev.PartitionSize - 1,
MBR_SIZE,
(BOOLEAN) (Mbr->Partition[Index].OSIndicator == EFI_PARTITION)
);
DBG_PR(DBG_PartitionInstallAddLbaOfsChildHandles, "PartitionInstallChildHandle %r\n", Status);
if (!EFI_ERROR (Status)) {
Found = EFI_SUCCESS;
}
/* } /* for */
} else {
#if 1
//Found = EFI_NOT_FOUND;
goto Done;
#else
//
// It's an extended partition. Follow the extended partition
// chain to get all the logical drives
//
ExtMbrStartingLba = 0;
do {
Status = DiskIo->ReadDisk (
DiskIo,
MediaId,
MultU64x32 (ExtMbrStartingLba, BlockSize),
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,
DiskIo2,
BlockIo,
BlockIo2,
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);
#endif
}
Done:
FreePool (Mbr);
DBG_PR(DBG_PartitionInstallAddLbaOfsChildHandles, "Found=%d\n", Found);
return Found;
}