hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
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
/** @file
 
  @copyright
  Copyright 2004 - 2021 Intel Corporation. <BR>
 
  SPDX-License-Identifier: BSD-2-Clause-Patent
**/
 
#include <PiDxe.h>
#include "PciPlatform.h"
#include <Library/PcdLib.h>
#include <Library/PlatformOpromPolicyLib.h>
#include <Protocol/DynamicSiLibraryProtocol.h>
#include <Protocol/UbaCfgDb.h>
#ifdef EFI_PCI_IOV_SUPPORT
#include "PciIovPlatformPolicy.h"
#endif
 
#include <Library/UbaPlatLib.h>
 
PCI_PLATFORM_PRIVATE_DATA mPciPrivateData;
 
BOOLEAN                   FirstCall = TRUE;
UINT8                     sSataRaidLoadEfiDriverOption;
UINT8                     SataRaidLoadEfiDriverOption[PCH_MAX_SATA_CONTROLLERS];
UINT8                     BootNetworkOption;
 
STATIC
BOOLEAN
InternalPlatformCheckPcieRootPort (
  IN  UINTN                 Bus,
  IN  UINT32                PcieSlotOpromBitMap
)
{
  EFI_STATUS                              Status;
 
  UBA_CONFIG_DATABASE_PROTOCOL            *UbaConfigProtocol = NULL;
  UINTN                                   DataLength = 0;
  PLATFORM_OPTION_ROM_UPDATE_DATA         OptionRomUpdateTable;
 
  Status = gBS->LocateProtocol (
                  &gUbaConfigDatabaseProtocolGuid,
                  NULL,
                  &UbaConfigProtocol
                  );
  if (EFI_ERROR(Status)) {
    DEBUG ((DEBUG_INFO," InternalPlatformCheckPcieRootPort fail!\n"));
    return TRUE;
  }
 
  DataLength  = sizeof (OptionRomUpdateTable);
  Status = UbaConfigProtocol->GetData (
                                UbaConfigProtocol,
                                &gPlatformOptionRomUpdateConfigDataGuid,
                                &OptionRomUpdateTable,
                                &DataLength
                                );
  if (EFI_ERROR(Status)) {
    DEBUG ((DEBUG_INFO,"InternalPlatformCheckPcieRootPort fail!\n"));
    return TRUE;
  }
 
  ASSERT (OptionRomUpdateTable.Signature == PLATFORM_OPTION_ROM_UPDATE_SIGNATURE);
  ASSERT (OptionRomUpdateTable.Version == PLATFORM_OPTION_ROM_UPDATE_VERSION);
 
  return OptionRomUpdateTable.CallCheckRootPort (Bus, PcieSlotOpromBitMap);
}
 
STATIC
EFI_STATUS
InternalGetSystemBoardInfo (
  IN OUT   DXE_SYSTEM_BOARD_INFO       **SystemboardInfoTableBuffer
  )
{
  EFI_STATUS                        Status;
  UBA_CONFIG_DATABASE_PROTOCOL      *UbaConfigProtocol = NULL;
  UINTN                             DataLength = 0;
  SYSTEM_BOARD_INFO_DATA            SystemBoardInfoData;
 
  Status = gBS->LocateProtocol (
                  &gUbaConfigDatabaseProtocolGuid,
                  NULL,
                  &UbaConfigProtocol
                  );
 
  if (EFI_ERROR(Status)) {
    DEBUG ((EFI_D_ERROR,"  [GetSystemBoardInfo] Locate UbaConfigProtocol fail!\n"));
    return Status;
  }
 
  DataLength = sizeof(SystemBoardInfoData);
  Status = UbaConfigProtocol->GetData (
                                UbaConfigProtocol,
                                &gSystemBoardInfoConfigDataGuid,
                                &SystemBoardInfoData,
                                &DataLength
                                );
 
  if (EFI_ERROR(Status)) {
    DEBUG ((EFI_D_ERROR," [GetSystemBoardInfo] Get Data fail!\n"));
    return Status;
  }
 
  ASSERT (SystemBoardInfoData.Signature == SYSTEM_SYSTEM_BOARD_INFO_SIGNATURE);
  ASSERT (SystemBoardInfoData.Version   == SYSTEM_SYSTEM_BOARD_INFO_VERSION);
 
  *SystemboardInfoTableBuffer = SystemBoardInfoData.CallUpdate ();
 
  return Status;
}
 
/**
 
  Set the PciPolicy as EFI_RESERVE_ISA_IO_NO_ALIAS | EFI_RESERVE_VGA_IO_NO_ALIAS.
 
  @param This       -  The pointer to the Protocol itself.
  PciPolicy  -  the returned Policy.
 
  @retval EFI_UNSUPPORTED        -  Function not supported.
  @retval EFI_INVALID_PARAMETER  -  Invalid PciPolicy value.
 
**/
EFI_STATUS
EFIAPI
GetPlatformPolicy (
  IN  CONST EFI_PCI_PLATFORM_PROTOCOL         *This,
  OUT EFI_PCI_PLATFORM_POLICY           *PciPolicy
  )
{
  if (PciPolicy == NULL) {
    return EFI_INVALID_PARAMETER;
  }
 
  return EFI_UNSUPPORTED;
}
 
/**
 
  Get an indicated image in raw sections.
 
  @param NameGuid  -  NameGuid of the image to get.
  @param Buffer    -  Buffer to store the image get.
  @param Size      -  size of the image get.
 
  @retval EFI_NOT_FOUND   -  Could not find the image.
  @retval EFI_LOAD_ERROR  -  Error occurred during image loading.
  @retval EFI_SUCCESS     -  Image has been successfully loaded.
 
**/
EFI_STATUS
GetRawImage(
   IN     EFI_GUID                       *NameGuid,
   IN OUT VOID                           **Buffer,
   IN OUT UINTN                          *Size
   ) {
  EFI_STATUS                    Status;
  EFI_HANDLE                    *HandleBuffer;
  UINTN                         HandleCount;
  UINTN                         Index;
  EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
  UINT32                        AuthenticationStatus;
 
  Status = gBS->LocateHandleBuffer(
     ByProtocol,
     &gEfiFirmwareVolume2ProtocolGuid,
     NULL,
     &HandleCount,
     &HandleBuffer
     );
  if (EFI_ERROR(Status) || HandleCount == 0) {
    return EFI_NOT_FOUND;
  }
  //
  // Find desired image in all Fvs
  //
  for (Index = 0; Index < HandleCount; Index++) {
    Status = gBS->HandleProtocol(
       HandleBuffer[Index],
       &gEfiFirmwareVolume2ProtocolGuid,
       &Fv
       );
    if (EFI_ERROR(Status)) {
      return EFI_LOAD_ERROR;
    }
    //
    // Try a raw file
    //
    *Buffer = NULL;
    *Size   = 0;
    Status = Fv->ReadSection(
         Fv,
         NameGuid,
         EFI_SECTION_RAW,
         0,
         Buffer,
         Size,
         &AuthenticationStatus
         );
    if (!EFI_ERROR(Status)) {
      DEBUG((EFI_D_INFO, "Read the OROM successfully!\n"));
      break;
    }
  }
 
  if (Index >= HandleCount) {
    return EFI_NOT_FOUND;
  }
 
  return EFI_SUCCESS;
}
 
/**
 
  Return a PCI ROM image for the onboard device represented by PciHandle.
 
  @param This       -  Protocol instance pointer.
  PciHandle  -  PCI device to return the ROM image for.
  RomImage   -  PCI Rom Image for onboard device.
  RomSize    -  Size of RomImage in bytes.
 
  @retval EFI_SUCCESS    -  RomImage is valid.
  @retval EFI_NOT_FOUND  -  No RomImage.
 
**/
EFI_STATUS
EFIAPI
GetPciRom (
  IN  CONST EFI_PCI_PLATFORM_PROTOCOL   *This,
  IN  EFI_HANDLE                        PciHandle,
  OUT VOID                              **RomImage,
  OUT UINTN                             *RomSize
  )
{
  EFI_STATUS                        Status;
  EFI_PCI_IO_PROTOCOL               *PciIo;
  UINTN                             Segment;
  UINTN                             Bus;
  UINTN                             Device;
  UINTN                             Function;
  UINT16                            VendorId;
  UINT16                            DeviceId;
  UINT16                            DeviceClass;
  UINTN                             TableIndex;
  UINTN                             RomImageNumber;
  VOID                              *OpRomBase;
  UINTN                             OpRomSize;
  EFI_PCI_ROM_HEADER                RomHeader;
  PCI_DATA_STRUCTURE                *Pcir;
  OPROM_LOAD_POLICY                 OpromPolicy;
  BOOLEAN                           SlotOptionRomDisabled;
  DXE_SYSTEM_BOARD_INFO             *SystemBoardInfo = NULL;
  UINT32                            Index;
  DYNAMIC_SI_LIBARY_PROTOCOL        *DynamicSiLibraryProtocol = NULL;
 
  OpRomBase = NULL;
  OpRomSize = 0;
 
  Status = gBS->LocateProtocol (&gDynamicSiLibraryProtocolGuid, NULL, &DynamicSiLibraryProtocol);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }
 
  //
  // Get System Board Info
  //
  Status = InternalGetSystemBoardInfo (&SystemBoardInfo);
  ASSERT_EFI_ERROR (Status);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "[GetPciRom] Get system board info fail!\n"));
    return Status;
  }
 
  Status = gBS->HandleProtocol (
                  PciHandle,
                  &gEfiPciIoProtocolGuid,
                  (VOID **) &PciIo
                  );
  if (EFI_ERROR (Status)) {
    return EFI_NOT_FOUND;
  }
 
  if (FirstCall == TRUE) {
    Status = GetOptionData (&gEfiSetupVariableGuid, OFFSET_OF(SYSTEM_CONFIGURATION, BootNetwork), &BootNetworkOption, sizeof(BootNetworkOption));
    if (EFI_ERROR (Status)) {
      BootNetworkOption = 0;
    }
    Status = GetOptionData (&gPchSetupVariableGuid,
                          OFFSET_OF(PCH_SETUP, SataRaidLoadEfiDriver),
                          &SataRaidLoadEfiDriverOption,
                          sizeof(SataRaidLoadEfiDriverOption) * PCH_MAX_SATA_CONTROLLERS);
    if (EFI_ERROR (Status)) {
      for (Index = 0; Index < DynamicSiLibraryProtocol->MaxSataControllerNum(); Index++) {
        SataRaidLoadEfiDriverOption[Index] = 0;
      }
    }
 
    FirstCall = FALSE;
  }
 
  PciIo->GetLocation (PciIo, &Segment, &Bus, &Device, &Function);
  PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_CLASSCODE_OFFSET + 1, 1, &DeviceClass);
 
  //
  // Run PXE ROM only if Boot network is enabled
  //
  if ((BootNetworkOption == 0) &&
      (DeviceClass == ((PCI_CLASS_NETWORK << 8) | PCI_CLASS_NETWORK_ETHERNET))
      ) {
    return EFI_NOT_FOUND;
  }
 
  //
  // Run each PCI-E slot ROM only if PCI-E Slot Oprom is enabled.
  //
  if ( Bus != 0 ) {
    SlotOptionRomDisabled = InternalPlatformCheckPcieRootPort (Bus, PcdGet32(PcdOemSkuPcieSlotOpromBitMap));
 
    if (SlotOptionRomDisabled) {
      return EFI_NOT_FOUND;
    }
  }
 
  PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_VENDOR_ID_OFFSET, 1, &VendorId);
  PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_DEVICE_ID_OFFSET, 1, &DeviceId);
 
  //DEBUG ((EFI_D_INFO, "GetPciRom - VenID:DevID: %04x:%04x\n", (UINTN)VendorId, (UINTN)DeviceId));
 
  //
  // Fix MS-HD5770 video adapter can not work:
  // This device is not a OPROM 3.0 and does not have device id list as well.
  // It only have 1 device id in OPROM.
  // Device Id in OpROM is not same with the value in PCI configuration space
  // it will cause VBIOS fails to start
  //
  if ((VendorId == 0x1002) && (DeviceId == 0x68BE)) {
    DEBUG ((DEBUG_INFO, "MS-HD5770 video adapter\n"));
    RomHeader.Raw = PciIo->RomImage;
    if (RomHeader.Raw != NULL) {
      Pcir = (PCI_DATA_STRUCTURE *)(RomHeader.Raw + RomHeader.Generic->PcirOffset);
      if ((Pcir->VendorId == 0x1002) && (Pcir->DeviceId == 0x68B8)) {
        //
        // Assign same device id in PCI configuration space
        //
        Pcir->DeviceId = DeviceId;
      }
    } else {
      DEBUG ((EFI_D_ERROR, "MS-HD5770 video adapter detected but PciIo->RomImage == NULL!\n"));
    }
  }
 
  //Check if user disables the option rom loading for this device.
  if (!PlatformOpromLoadDevicePolicy(PciIo)) {
    return EFI_NOT_FOUND;
  }
 
   // If setup value requested EFI, we don't load the RAID OROM.
  if (VendorId == V_SATA_CFG_VENDOR_ID) {
    for (Index = 0; Index < DynamicSiLibraryProtocol->MaxSataControllerNum(); Index++) {
      if (Bus == DEFAULT_PCI_BUS_NUMBER_PCH &&
          Device == DynamicSiLibraryProtocol->SataDevNumber (Index) &&
          Function == DynamicSiLibraryProtocol->SataFuncNumber (Index) &&
          SataRaidLoadEfiDriverOption[Index] == 1) {
        return EFI_NOT_FOUND;
      }
    }
  }
 
  //
  // Loop through table of video option rom descriptions
  //
  RomImageNumber = 0;
  for (TableIndex = 0; SystemBoardInfo->PciOptionRomTable[TableIndex].VendorId != 0xffff; TableIndex++) {
    //
    // See if the PCI device specified by PciHandle matches at device in mPciOptionRomTable
    //
    if (VendorId != SystemBoardInfo->PciOptionRomTable[TableIndex].VendorId ||
        DeviceId != SystemBoardInfo->PciOptionRomTable[TableIndex].DeviceId ||
        Device != SystemBoardInfo->PciOptionRomTable[TableIndex].Device ||
        Function != SystemBoardInfo->PciOptionRomTable[TableIndex].Function
        ) {
      continue;
    }
 
    //Check if user wants to exclusively run this option rom for the device.
    OpromPolicy = PlatformOpromLoadTypePolicy(PciHandle, TableIndex);
    if(OpromPolicy == DONT_LOAD) {
      continue;
    }
 
    Status = GetRawImage (
              &SystemBoardInfo->PciOptionRomTable[TableIndex].FileName,
              &OpRomBase,
              &OpRomSize
              );
 
    if (EFI_ERROR (Status)) {
      continue;
    } else {
      RomImageNumber++;
      if (RomImageNumber == PcdGet8(PcdMaxOptionRomNumber) || OpromPolicy == EXCLUSIVE_LOAD) {
        break;
      }
    }
  }
 
  if (RomImageNumber == 0) {
 
    return EFI_NOT_FOUND;
 
  } else {
 
    *RomImage = OpRomBase;
    *RomSize  = OpRomSize;
 
    return EFI_SUCCESS;
  }
}
 
/**
 
  GC_TODO: Add function description
 
  @param This      - GC_TODO: add argument description
  @param Function  - GC_TODO: add argument description
  @param Phase     - GC_TODO: add argument description
 
  @retval EFI_INVALID_PARAMETER - GC_TODO: Add description for return value
  @retval EFI_INVALID_PARAMETER - GC_TODO: Add description for return value
  @retval EFI_UNSUPPORTED - GC_TODO: Add description for return value
  @retval EFI_SUCCESS - GC_TODO: Add description for return value
 
**/
EFI_STATUS
EFIAPI
RegisterPciCallback (
  IN  EFI_PCI_CALLBACK_PROTOCOL               *This,
  IN  EFI_PCI_CALLBACK_FUNC                   Function,
  IN  EFI_PCI_ENUMERATION_PHASE               Phase
  )
{
  LIST_ENTRY     *NodeEntry;
  PCI_CALLBACK_DATA *PciCallbackData;
 
  if (Function == NULL) {
    return EFI_INVALID_PARAMETER;
  }
 
  if ( (Phase &  (EfiPciEnumerationDeviceScanning | EfiPciEnumerationBusNumberAssigned \
              | EfiPciEnumerationResourceAssigned)) == 0) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Check if the node has been added
  //
  NodeEntry = GetFirstNode (&mPciPrivateData.PciCallbackList);
  while (!IsNull (&mPciPrivateData.PciCallbackList, NodeEntry)) {
    PciCallbackData = PCI_CALLBACK_DATA_FROM_LINK (NodeEntry);
    if (PciCallbackData->Function == Function) {
      return EFI_UNSUPPORTED;
    }
 
    NodeEntry = GetNextNode (&mPciPrivateData.PciCallbackList, NodeEntry);
  }
 
  PciCallbackData = NULL;
  PciCallbackData = AllocateZeroPool (sizeof (PCI_CALLBACK_DATA));
  ASSERT (PciCallbackData != NULL);
 
  if(PciCallbackData != NULL){
    PciCallbackData->Signature  = PCI_CALLBACK_DATA_SIGNATURE;
    PciCallbackData->Function   = Function;
    PciCallbackData->Phase      = Phase;
    InsertTailList (&mPciPrivateData.PciCallbackList, &PciCallbackData->Link);
    return EFI_SUCCESS;
  } else {
    return EFI_UNSUPPORTED;
  }
}
 
 
/**
 
  Main Entry point of the Pci Platform Driver.
 
  @param ImageHandle  -  Handle to the image.
  @param SystemTable  -  Handle to System Table.
 
  @retval EFI_STATUS  -  Status of the function calling.
 
**/
EFI_STATUS
EFIAPI
PciPlatformDriverEntry (
  IN EFI_HANDLE                         ImageHandle,
  IN EFI_SYSTEM_TABLE                   *SystemTable
  )
{
  EFI_STATUS  Status;
  EFI_HOB_GUID_TYPE             *GuidHob;
 
 
  GuidHob    = GetFirstGuidHob (&gEfiPlatformInfoGuid);
  ASSERT (GuidHob != NULL);
  if (GuidHob == NULL) {
    return EFI_NOT_FOUND;
  }
  mPciPrivateData.PlatformInfo = GET_GUID_HOB_DATA(GuidHob);
 
  //EDK2_TODO Check if clearing mPciPrivateData.PlatformInfo (got above) is intended.
  ZeroMem (&mPciPrivateData, sizeof (mPciPrivateData));
  InitializeListHead (&mPciPrivateData.PciCallbackList);
 
  mPciPrivateData.PciPlatform.PlatformNotify          = PhaseNotify;
  mPciPrivateData.PciPlatform.PlatformPrepController  = PlatformPrepController;
  mPciPrivateData.PciPlatform.GetPlatformPolicy       = GetPlatformPolicy;
  mPciPrivateData.PciPlatform.GetPciRom               = GetPciRom;
  mPciPrivateData.PciCallback.RegisterPciCallback     = RegisterPciCallback;
#ifdef EFI_PCI_IOV_SUPPORT
  mPciPrivateData.PciIovPlatform.GetSystemLowestPageSize = GetSystemLowestPageSize;
  mPciPrivateData.PciIovPlatform.GetPlatformPolicy       = GetIovPlatformPolicy;
#endif
 
  //
  // Install on a new handle
  //
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &mPciPrivateData.PciPlatformHandle,
                  &gEfiPciPlatformProtocolGuid,
                  &mPciPrivateData.PciPlatform,
                  &gEfiPciCallbackProtocolGuid,
                  &mPciPrivateData.PciCallback,
#ifdef EFI_PCI_IOV_SUPPORT
                  &gEfiPciIovPlatformProtocolGuid,
                  &mPciPrivateData.PciIovPlatform,
#endif
                  NULL
                  );
 
  return Status;
}