forked from acidanthera/OpenCorePkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGptPartitionEntry.c
638 lines (546 loc) · 16.8 KB
/
GptPartitionEntry.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/** @file
Copyright (C) 2019, Download-Fritz. 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 <Uefi.h>
#include <Guid/Gpt.h>
#include <Protocol/BlockIo.h>
#include <Protocol/BlockIo2.h>
#include <Protocol/DiskIo.h>
#include <Protocol/DiskIo2.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/DevicePathLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/OcDebugLogLib.h>
#include <Library/OcDevicePathLib.h>
#include <Library/OcFileLib.h>
#include <Library/OcGuardLib.h>
#include <Library/UefiBootServicesTableLib.h>
STATIC EFI_GUID mInternalDiskPartitionEntriesProtocolGuid = {
0x1A81704, 0x3442, 0x4A7D, { 0x87, 0x40, 0xF4, 0xEC, 0x5B, 0xBE, 0x59, 0x77 }
};
STATIC EFI_GUID mInternalPartitionEntryProtocolGuid = {
0x9FC6B19, 0xB8A1, 0x4A01, { 0x8D, 0xB1, 0x87, 0x94, 0xE7, 0x63, 0x4C, 0xA5 }
};
EFI_STATUS
OcDiskInitializeContext (
OUT OC_DISK_CONTEXT *Context,
IN EFI_HANDLE DiskHandle,
IN BOOLEAN UseBlockIo2
)
{
EFI_STATUS Status;
//
// Retrieve the Block I/O protocol.
//
if (UseBlockIo2) {
Status = gBS->HandleProtocol (
DiskHandle,
&gEfiBlockIo2ProtocolGuid,
(VOID **)&Context->BlockIo2
);
} else {
Context->BlockIo2 = NULL;
Status = EFI_ABORTED;
}
if (EFI_ERROR (Status)) {
Status = gBS->HandleProtocol (
DiskHandle,
&gEfiBlockIoProtocolGuid,
(VOID **)&Context->BlockIo
);
} else {
Context->BlockIo = NULL;
}
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_INFO,
"OCPI: Block I/O (%d/%d) protocols (%d) are not present on %p - %r\n",
Context->BlockIo != NULL,
Context->BlockIo2 != NULL,
UseBlockIo2,
DiskHandle,
Status
));
return Status;
}
if ((Context->BlockIo2 != NULL) && (Context->BlockIo2->Media != NULL)) {
Context->BlockSize = Context->BlockIo2->Media->BlockSize;
Context->MediaId = Context->BlockIo2->Media->MediaId;
} else if ((Context->BlockIo != NULL) && (Context->BlockIo->Media != NULL)) {
Context->BlockSize = Context->BlockIo->Media->BlockSize;
Context->MediaId = Context->BlockIo->Media->MediaId;
} else {
return EFI_UNSUPPORTED;
}
//
// Check that BlockSize is POT.
//
if ((Context->BlockSize == 0) || ((Context->BlockSize & (Context->BlockSize - 1)) != 0)) {
DEBUG ((DEBUG_INFO, "OCPI: Block I/O has invalid block size %u\n", Context->BlockSize));
return EFI_UNSUPPORTED;
}
return EFI_SUCCESS;
}
EFI_STATUS
OcDiskRead (
IN OC_DISK_CONTEXT *Context,
IN UINT64 Lba,
IN UINTN BufferSize,
OUT VOID *Buffer
)
{
EFI_STATUS Status;
ASSERT (Context->BlockIo != NULL || Context->BlockIo2 != NULL);
ASSERT ((BufferSize & (Context->BlockSize - 1)) == 0);
if (Context->BlockIo2 != NULL) {
Status = Context->BlockIo2->ReadBlocksEx (
Context->BlockIo2,
Context->MediaId,
Lba,
NULL,
BufferSize,
Buffer
);
} else {
Status = Context->BlockIo->ReadBlocks (
Context->BlockIo,
Context->MediaId,
Lba,
BufferSize,
Buffer
);
}
return Status;
}
STATIC
VOID
InternalDebugPrintPartitionEntry (
IN UINTN ErrorLevel,
IN CONST CHAR8 *Message,
IN CONST EFI_PARTITION_ENTRY *PartitionEntry
)
{
ASSERT (PartitionEntry != NULL);
DEBUG ((
ErrorLevel,
"%a:\n"
"- PartitionTypeGUID: %g\n"
"- UniquePartitionGUID: %g\n"
"- StartingLBA: %lx\n"
"- EndingLBA: %lx\n"
"- Attributes: %lx\n"
"- PartitionName: %s\n",
Message,
PartitionEntry->PartitionTypeGUID,
PartitionEntry->UniquePartitionGUID,
PartitionEntry->StartingLBA,
PartitionEntry->EndingLBA,
PartitionEntry->Attributes,
PartitionEntry->PartitionName
));
}
STATIC
EFI_HANDLE
InternalPartitionGetDiskHandle (
IN EFI_DEVICE_PATH_PROTOCOL *HdDevicePath,
IN UINTN HdNodeOffset,
OUT BOOLEAN *HasBlockIo2
)
{
EFI_HANDLE DiskHandle;
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *PrefixPath;
EFI_DEVICE_PATH_PROTOCOL *TempPath;
ASSERT (HdDevicePath != NULL);
ASSERT (HdNodeOffset < GetDevicePathSize (HdDevicePath));
ASSERT (HasBlockIo2 != NULL);
PrefixPath = DuplicateDevicePath (HdDevicePath);
if (PrefixPath == NULL) {
DEBUG ((DEBUG_INFO, "OCPI: DP allocation error\n"));
return NULL;
}
//
// Strip the HD node in order to retrieve the last node supporting Block I/O
// before it, which is going to be its disk.
//
TempPath = (EFI_DEVICE_PATH_PROTOCOL *)((UINTN)PrefixPath + HdNodeOffset);
SetDevicePathEndNode (TempPath);
TempPath = PrefixPath;
Status = gBS->LocateDevicePath (
&gEfiBlockIo2ProtocolGuid,
&TempPath,
&DiskHandle
);
*HasBlockIo2 = !EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
TempPath = PrefixPath;
Status = gBS->LocateDevicePath (
&gEfiBlockIoProtocolGuid,
&TempPath,
&DiskHandle
);
}
if (EFI_ERROR (Status)) {
DebugPrintDevicePath (
DEBUG_INFO,
"OCPI: Failed to locate disk",
PrefixPath
);
DiskHandle = NULL;
}
FreePool (PrefixPath);
return DiskHandle;
}
/**
Retrieve the disk's device handle from a partition's Device Path.
@param[in] HdDevicePath The Device Path of the partition.
**/
EFI_HANDLE
OcPartitionGetDiskHandle (
IN EFI_DEVICE_PATH_PROTOCOL *HdDevicePath
)
{
CONST HARDDRIVE_DEVICE_PATH *HdNode;
BOOLEAN Dummy;
ASSERT (HdDevicePath != NULL);
HdNode = (HARDDRIVE_DEVICE_PATH *)(
FindDevicePathNodeWithType (
HdDevicePath,
MEDIA_DEVICE_PATH,
MEDIA_HARDDRIVE_DP
)
);
if (HdNode == NULL) {
return NULL;
}
return InternalPartitionGetDiskHandle (
HdDevicePath,
(UINTN)HdNode - (UINTN)HdDevicePath,
&Dummy
);
}
EFI_DEVICE_PATH_PROTOCOL *
OcDiskFindSystemPartitionPath (
IN CONST EFI_DEVICE_PATH_PROTOCOL *DiskDevicePath,
OUT UINTN *EspDevicePathSize,
OUT EFI_HANDLE *EspDeviceHandle
)
{
EFI_DEVICE_PATH_PROTOCOL *EspDevicePath;
EFI_STATUS Status;
BOOLEAN Result;
INTN CmpResult;
UINTN Index;
UINTN NumHandles;
EFI_HANDLE *Handles;
EFI_HANDLE Handle;
UINTN DiskDpSize;
UINTN DiskDpCmpSize;
EFI_DEVICE_PATH_PROTOCOL *HdDevicePath;
UINTN HdDpSize;
CONST EFI_PARTITION_ENTRY *PartEntry;
ASSERT (DiskDevicePath != NULL);
ASSERT (EspDevicePathSize != NULL);
ASSERT (EspDeviceHandle != NULL);
DebugPrintDevicePath (
DEBUG_INFO,
"OCPI: Locating disk's ESP",
(EFI_DEVICE_PATH_PROTOCOL *)DiskDevicePath
);
Status = gBS->LocateHandleBuffer (
ByProtocol,
&gEfiSimpleFileSystemProtocolGuid,
NULL,
&NumHandles,
&Handles
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCPI: Failed to locate FS handles\n"));
return NULL;
}
EspDevicePath = NULL;
DiskDpSize = GetDevicePathSize (DiskDevicePath);
//
// The partition's Device Path must be at least as big as the disk's (prefix)
// plus an additional HardDrive node.
//
Result = OcOverflowAddUN (
DiskDpSize,
sizeof (HARDDRIVE_DEVICE_PATH),
&DiskDpCmpSize
);
if (Result) {
DEBUG ((DEBUG_INFO, "OCPI: HD node would overflow DP\n"));
return NULL;
}
for (Index = 0; Index < NumHandles; ++Index) {
Handle = Handles[Index];
HdDevicePath = DevicePathFromHandle (Handle);
if (HdDevicePath == NULL) {
continue;
}
HdDpSize = GetDevicePathSize (HdDevicePath);
if (HdDpSize < DiskDpCmpSize) {
continue;
}
//
// Verify the partition's Device Path has the disk's prefixed.
//
CmpResult = CompareMem (
HdDevicePath,
DiskDevicePath,
DiskDpSize - END_DEVICE_PATH_LENGTH
);
if (CmpResult != 0) {
continue;
}
DebugPrintDevicePath (DEBUG_INFO, "OCPI: Discovered HD DP", HdDevicePath);
PartEntry = OcGetGptPartitionEntry (Handle);
if (PartEntry == NULL) {
continue;
}
InternalDebugPrintPartitionEntry (
DEBUG_INFO,
"OCPI: Discovered PartEntry",
PartEntry
);
if (CompareGuid (&PartEntry->PartitionTypeGUID, &gEfiPartTypeSystemPartGuid)) {
EspDevicePath = HdDevicePath;
*EspDevicePathSize = HdDpSize;
*EspDeviceHandle = Handle;
break;
}
}
FreePool (Handles);
return EspDevicePath;
}
CONST OC_PARTITION_ENTRIES *
OcGetDiskPartitions (
IN EFI_HANDLE DiskHandle,
IN BOOLEAN UseBlockIo2
)
{
OC_PARTITION_ENTRIES *PartEntries;
EFI_STATUS Status;
BOOLEAN Result;
OC_DISK_CONTEXT DiskContext;
EFI_LBA PartEntryLBA;
UINT32 NumPartitions;
UINT32 PartEntrySize;
UINTN PartEntriesSize;
UINTN PartEntriesStructSize;
UINTN BufferSize;
EFI_PARTITION_TABLE_HEADER *GptHeader;
ASSERT (DiskHandle != NULL);
Status = gBS->HandleProtocol (
DiskHandle,
&mInternalDiskPartitionEntriesProtocolGuid,
(VOID **)&PartEntries
);
if (!EFI_ERROR (Status)) {
DEBUG ((DEBUG_VERBOSE, "OCPI: Located cached partition entries\n"));
return PartEntries;
}
Status = OcDiskInitializeContext (&DiskContext, DiskHandle, UseBlockIo2);
if (EFI_ERROR (Status)) {
return NULL;
}
//
// Retrieve the GPT header.
//
BufferSize = ALIGN_VALUE (sizeof (*GptHeader), DiskContext.BlockSize);
GptHeader = AllocatePool (BufferSize);
if (GptHeader == NULL) {
DEBUG ((DEBUG_INFO, "OCPI: GPT header allocation error\n"));
return NULL;
}
Status = OcDiskRead (
&DiskContext,
PRIMARY_PART_HEADER_LBA,
BufferSize,
GptHeader
);
if (EFI_ERROR (Status)) {
FreePool (GptHeader);
DEBUG ((
DEBUG_INFO,
"OCPI: ReadDisk1 (block: %u, io1: %d, io2: %d, size: %u) %r\n",
DiskContext.BlockSize,
DiskContext.BlockIo != NULL,
DiskContext.BlockIo2 != NULL,
(UINT32)BufferSize,
Status
));
return NULL;
}
if (GptHeader->Header.Signature != EFI_PTAB_HEADER_ID) {
FreePool (GptHeader);
DEBUG ((DEBUG_INFO, "OCPI: Partition table not supported\n"));
return NULL;
}
PartEntrySize = GptHeader->SizeOfPartitionEntry;
if (PartEntrySize < sizeof (EFI_PARTITION_ENTRY)) {
FreePool (GptHeader);
DEBUG ((DEBUG_INFO, "OCPI: GPT header is malformed\n"));
return NULL;
}
NumPartitions = GptHeader->NumberOfPartitionEntries;
PartEntryLBA = GptHeader->PartitionEntryLBA;
FreePool (GptHeader);
Result = OcOverflowMulUN (NumPartitions, PartEntrySize, &PartEntriesSize);
if (Result || (MAX_UINTN - DiskContext.BlockSize < PartEntriesSize)) {
DEBUG ((DEBUG_INFO, "OCPI: Partition entries size overflows\n"));
return NULL;
}
PartEntriesSize = ALIGN_VALUE (PartEntriesSize, DiskContext.BlockSize);
Result = OcOverflowAddUN (
sizeof (PartEntries),
PartEntriesSize,
&PartEntriesStructSize
);
if (Result) {
DEBUG ((DEBUG_INFO, "OCPI: Partition entries struct size overflows\n"));
return NULL;
}
//
// Retrieve the GPT partition entries.
//
PartEntries = AllocatePool (PartEntriesStructSize);
if (PartEntries == NULL) {
DEBUG ((DEBUG_INFO, "OCPI: Partition entries allocation error\n"));
return NULL;
}
Status = OcDiskRead (
&DiskContext,
PartEntryLBA,
PartEntriesSize,
PartEntries->FirstEntry
);
if (EFI_ERROR (Status)) {
FreePool (PartEntries);
DEBUG ((
DEBUG_INFO,
"OCPI: ReadDisk2 (block: %u, io1: %d, io2: %d, size: %u) %r\n",
DiskContext.BlockSize,
DiskContext.BlockIo != NULL,
DiskContext.BlockIo2 != NULL,
(UINT32)PartEntriesSize,
Status
));
return NULL;
}
PartEntries->NumPartitions = NumPartitions;
PartEntries->PartitionEntrySize = PartEntrySize;
//
// FIXME: This causes the handle to be dangling if the device is detached.
//
Status = gBS->InstallMultipleProtocolInterfaces (
&DiskHandle,
&mInternalDiskPartitionEntriesProtocolGuid,
PartEntries,
NULL
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCPI: Failed to cache partition entries\n"));
FreePool (PartEntries);
return NULL;
}
return PartEntries;
}
/**
Retrieve the partition's GPT information, if applicable
@param[in] FsHandle The device handle of the partition to retrieve info of.
**/
CONST EFI_PARTITION_ENTRY *
OcGetGptPartitionEntry (
IN EFI_HANDLE FsHandle
)
{
CONST EFI_PARTITION_ENTRY *PartEntry;
CONST OC_PARTITION_ENTRIES *Partitions;
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *FsDevicePath;
CONST HARDDRIVE_DEVICE_PATH *HdNode;
EFI_HANDLE DiskHandle;
BOOLEAN HasBlockIo2;
UINTN Offset;
ASSERT (FsHandle != NULL);
Status = gBS->HandleProtocol (
FsHandle,
&mInternalPartitionEntryProtocolGuid,
(VOID **)&PartEntry
);
if (!EFI_ERROR (Status)) {
DEBUG ((DEBUG_VERBOSE, "OCPI: Located cached partition entry\n"));
return PartEntry;
}
//
// Retrieve the partition Device Path information.
//
FsDevicePath = DevicePathFromHandle (FsHandle);
if (FsDevicePath == NULL) {
DEBUG ((DEBUG_INFO, "OCPI: Failed to retrieve Device Path\n"));
return NULL;
}
HdNode = (HARDDRIVE_DEVICE_PATH *)(
FindDevicePathNodeWithType (
FsDevicePath,
MEDIA_DEVICE_PATH,
MEDIA_HARDDRIVE_DP
)
);
if (HdNode == NULL) {
DEBUG ((DEBUG_INFO, "OCPI: Device Path does not describe a partition\n"));
return NULL;
}
DiskHandle = InternalPartitionGetDiskHandle (
FsDevicePath,
(UINTN)HdNode - (UINTN)FsDevicePath,
&HasBlockIo2
);
if (DiskHandle == NULL) {
DebugPrintDevicePath (
DEBUG_INFO,
"OCPI: Could not locate partition's disk",
FsDevicePath
);
return NULL;
}
//
// Get the disk's GPT partition entries.
//
Partitions = OcGetDiskPartitions (DiskHandle, HasBlockIo2);
if (Partitions == NULL) {
DEBUG ((DEBUG_INFO, "OCPI: Failed to retrieve disk info\n"));
return NULL;
}
if (HdNode->PartitionNumber > Partitions->NumPartitions) {
DEBUG ((DEBUG_INFO, "OCPI: Partition is OOB\n"));
return NULL;
}
ASSERT (HdNode->PartitionNumber > 0);
Offset = ((UINTN)(HdNode->PartitionNumber - 1) * Partitions->PartitionEntrySize);
PartEntry = (EFI_PARTITION_ENTRY *)((UINTN)Partitions->FirstEntry + Offset);
//
// FIXME: This causes the handle to be dangling if the device is detached.
//
Status = gBS->InstallMultipleProtocolInterfaces (
&FsHandle,
&mInternalPartitionEntryProtocolGuid,
PartEntry,
NULL
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCPI: Failed to cache partition entry\n"));
return NULL;
}
return PartEntry;
}