]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/PciPlatform/PciPlatform.c
IntelFrameworkModulePkg: Refine casting expression result to bigger size
[mirror_edk2.git] / Vlv2TbltDevicePkg / PciPlatform / PciPlatform.c
1 /** @file
2
3 Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
4
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13
14 Module Name:
15
16
17 PciPlatform.c
18
19 Abstract:
20 --*/
21
22
23 #include "PciPlatform.h"
24 #include "PchRegs.h"
25 #include "PchAccess.h"
26 #include "VlvCommonDefinitions.h"
27 #include "PlatformBootMode.h"
28
29 #include <Library/BaseLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Protocol/CpuIo.h>
32 #include <Protocol/PciIo.h>
33 #include <Guid/SetupVariable.h>
34 #include <Protocol/PciRootBridgeIo.h>
35 #include "SetupMode.h"
36 #include <Library/UefiBootServicesTableLib.h>
37 #include <Library/UefiRuntimeServicesTableLib.h>
38 #include <Library/DebugLib.h>
39 #include <Protocol/FirmwareVolume.h>
40 #include <Library/HobLib.h>
41 #include <IndustryStandard/Pci22.h>
42
43 extern PCI_OPTION_ROM_TABLE mPciOptionRomTable[];
44 extern UINTN mSizeOptionRomTable;
45
46 EFI_PCI_PLATFORM_PROTOCOL mPciPlatform = {
47 PhaseNotify,
48 PlatformPrepController,
49 GetPlatformPolicy,
50 GetPciRom
51 };
52
53 EFI_HANDLE mPciPlatformHandle = NULL;
54
55
56 SYSTEM_CONFIGURATION mSystemConfiguration;
57
58 EFI_STATUS
59 GetRawImage (
60 IN EFI_GUID *NameGuid,
61 IN OUT VOID **Buffer,
62 IN OUT UINTN *Size
63 )
64 {
65 EFI_STATUS Status;
66 EFI_HANDLE *HandleBuffer;
67 UINTN HandleCount;
68 UINTN Index;
69 EFI_FIRMWARE_VOLUME_PROTOCOL *Fv;
70 UINT32 AuthenticationStatus;
71
72 Status = gBS->LocateHandleBuffer (
73 ByProtocol,
74 &gEfiFirmwareVolumeProtocolGuid,
75 NULL,
76 &HandleCount,
77 &HandleBuffer
78 );
79 if (EFI_ERROR (Status) || HandleCount == 0) {
80 return EFI_NOT_FOUND;
81 }
82
83 //
84 // Find desired image in all Fvs
85 //
86 for (Index = 0; Index < HandleCount; Index++) {
87 Status = gBS->HandleProtocol(
88 HandleBuffer[Index],
89 &gEfiFirmwareVolumeProtocolGuid,
90 (VOID **) &Fv
91 );
92
93 if ( EFI_ERROR ( Status ) ) {
94 return EFI_LOAD_ERROR;
95 }
96
97 //
98 // Try a raw file
99 //
100 *Buffer = NULL;
101 *Size = 0;
102 Status = Fv->ReadSection (
103 Fv,
104 NameGuid,
105 EFI_SECTION_RAW,
106 0,
107 Buffer,
108 Size,
109 &AuthenticationStatus
110 );
111
112 if ( !EFI_ERROR ( Status )) {
113 break;
114 }
115 }
116
117 if ( Index >= HandleCount ) {
118 return EFI_NOT_FOUND;
119 }
120
121 return EFI_SUCCESS;
122 }
123
124 EFI_STATUS
125 EFIAPI
126 PhaseNotify (
127 IN EFI_PCI_PLATFORM_PROTOCOL *This,
128 IN EFI_HANDLE HostBridge,
129 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PHASE Phase,
130 IN EFI_PCI_CHIPSET_EXECUTION_PHASE ChipsetPhase
131 )
132 {
133 return EFI_UNSUPPORTED;
134 }
135
136
137 EFI_STATUS
138 EFIAPI
139 PlatformPrepController (
140 IN EFI_PCI_PLATFORM_PROTOCOL *This,
141 IN EFI_HANDLE HostBridge,
142 IN EFI_HANDLE RootBridge,
143 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS PciAddress,
144 IN EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE Phase,
145 IN EFI_PCI_CHIPSET_EXECUTION_PHASE ChipsetPhase
146 )
147 {
148 return EFI_UNSUPPORTED;
149 }
150
151 EFI_STATUS
152 EFIAPI
153 GetPlatformPolicy (
154 IN CONST EFI_PCI_PLATFORM_PROTOCOL *This,
155 OUT EFI_PCI_PLATFORM_POLICY *PciPolicy
156 )
157 {
158 *PciPolicy = EFI_RESERVE_VGA_IO_ALIAS;
159 return EFI_SUCCESS;
160 }
161
162 /**
163 GetPciRom from platform specific location for specific PCI device
164
165 @param This Protocol instance
166 @param PciHandle Identify the specific PCI devic
167 @param RomImage Returns the ROM Image memory location
168 @param RomSize Returns Rom Image size
169
170 @retval EFI_SUCCESS
171 @retval EFI_NOT_FOUND
172 @retval EFI_OUT_OF_RESOURCES
173
174 **/
175 EFI_STATUS
176 EFIAPI
177 GetPciRom (
178 IN CONST EFI_PCI_PLATFORM_PROTOCOL *This,
179 IN EFI_HANDLE PciHandle,
180 OUT VOID **RomImage,
181 OUT UINTN *RomSize
182 )
183 {
184 EFI_STATUS Status;
185 EFI_PCI_IO_PROTOCOL *PciIo;
186 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
187 UINTN Segment;
188 UINTN Bus;
189 UINTN Device;
190 UINTN Function;
191 UINT16 VendorId;
192 UINT16 DeviceId;
193 UINT16 DeviceClass;
194 UINTN TableIndex;
195 UINT8 Data8;
196 BOOLEAN MfgMode;
197 EFI_PLATFORM_SETUP_ID *BootModeBuffer;
198
199 EFI_PEI_HOB_POINTERS GuidHob;
200
201 MfgMode = FALSE;
202
203 //
204 // Check if system is in manufacturing mode.
205 //
206 GuidHob.Raw = GetHobList ();
207 if (GuidHob.Raw == NULL) {
208 return EFI_NOT_FOUND;
209 }
210
211 if ((GuidHob.Raw = GetNextGuidHob (&gEfiPlatformBootModeGuid, GuidHob.Raw)) != NULL) {
212 BootModeBuffer = GET_GUID_HOB_DATA (GuidHob.Guid);
213 if (!CompareMem (&BootModeBuffer->SetupName, MANUFACTURE_SETUP_NAME,
214 StrSize (MANUFACTURE_SETUP_NAME)))
215 {
216 //
217 // System is in manufacturing mode.
218 //
219 MfgMode = TRUE;
220 }
221 }
222
223 Status = gBS->HandleProtocol (
224 PciHandle,
225 &gEfiPciIoProtocolGuid,
226 (void **)&PciIo
227 );
228 if (EFI_ERROR (Status)) {
229 return EFI_NOT_FOUND;
230 }
231
232 Status = gBS->LocateProtocol (
233 &gEfiPciRootBridgeIoProtocolGuid,
234 NULL,
235 (void **)&PciRootBridgeIo
236 );
237
238 if (EFI_ERROR (Status)) {
239 return EFI_NOT_FOUND;
240 }
241
242 PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, 0x0A, 1, &DeviceClass);
243
244 PciIo->GetLocation (PciIo, &Segment, &Bus, &Device, &Function);
245
246 PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, 0, 1, &VendorId);
247
248 PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, 2, 1, &DeviceId);
249
250 //
251 // WA for PCIe SATA card (SYBA SY-PEX400-40)
252 //
253 if ((VendorId == 0x1B21) && (DeviceId == 0x0612)) {
254 Data8 = 0x07;
255 PciIo->Pci.Write (PciIo, EfiPciIoWidthUint8, 4, 1, &Data8);
256 }
257
258 //
259 // Do not run RAID or AHCI Option ROM if IDE
260 //
261 if ( (DeviceClass == ((PCI_CLASS_MASS_STORAGE << 8 ) | PCI_CLASS_MASS_STORAGE_IDE)) ) {
262 return EFI_NOT_FOUND;
263 }
264
265 //
266 // Run PXE ROM only if Boot network is enabled and not in MFG mode
267 //
268 if (DeviceClass == ((PCI_CLASS_NETWORK << 8 ) | PCI_CLASS_NETWORK_ETHERNET)) {
269 if (((mSystemConfiguration.BootNetwork == 0) && (MfgMode == FALSE )) || (mSystemConfiguration.FastBoot == 1)) {
270 return EFI_NOT_FOUND;
271 }
272 }
273
274 //
275 // Loop through table of Onboard option rom descriptions
276 //
277 for (TableIndex = 0; mPciOptionRomTable[TableIndex].VendorId != 0xffff; TableIndex++) {
278
279 //
280 // See if the PCI device specified by PciHandle matches at device in mPciOptionRomTable
281 //
282 if (VendorId != mPciOptionRomTable[TableIndex].VendorId ||
283 DeviceId != mPciOptionRomTable[TableIndex].DeviceId ||
284 ((DeviceClass == ((PCI_CLASS_NETWORK << 8 ) | PCI_CLASS_NETWORK_ETHERNET)) &&
285 (mPciOptionRomTable[TableIndex].Flag != mSystemConfiguration.BootNetwork)) ) {
286 continue;
287 }
288
289 Status = GetRawImage(
290 &mPciOptionRomTable[TableIndex].FileName,
291 RomImage,
292 RomSize
293 );
294
295 if ((VendorId == IGD_VID) && (DeviceId == IGD_DID_VLV_A0)) {
296 *(UINT16 *)(((UINTN) *RomImage) + OPROM_DID_OFFSET) = IGD_DID_VLV_A0;
297 }
298
299 if ((VendorId == IGD_VID) && (DeviceId == IGD_DID_II)) {
300 *(UINT16 *)(((UINTN) *RomImage) + OPROM_DID_OFFSET) = IGD_DID_II;
301 }
302
303 if ((VendorId == IGD_VID) && (DeviceId == IGD_DID_0BE4)) {
304 *(UINT16 *)(((UINTN) *RomImage) + OPROM_DID_OFFSET) = IGD_DID_0BE4;
305 }
306
307 if ((VendorId == IGD_VID) && (DeviceId == IGD_DID_QS)) {
308 *(UINT16 *)(((UINTN) *RomImage) + OPROM_DID_OFFSET) = IGD_DID_QS;
309 }
310
311
312 if (EFI_ERROR (Status)) {
313 continue;
314 }
315 return EFI_SUCCESS;
316 }
317
318 return EFI_NOT_FOUND;
319 }
320
321 /**
322
323 @param (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
324
325 @retval EFI_STATUS
326
327 **/
328 EFI_STATUS
329 EFIAPI
330 PciPlatformDriverEntry (
331 IN EFI_HANDLE ImageHandle,
332 IN EFI_SYSTEM_TABLE *SystemTable
333 )
334 {
335 EFI_STATUS Status;
336 UINTN VarSize;
337
338 VarSize = sizeof(SYSTEM_CONFIGURATION);
339 Status = gRT->GetVariable(
340 L"Setup",
341 &gEfiNormalSetupGuid,
342 NULL,
343 &VarSize,
344 &mSystemConfiguration
345 );
346 if (EFI_ERROR (Status) || VarSize != sizeof(SYSTEM_CONFIGURATION)) {
347 //The setup variable is corrupted
348 VarSize = sizeof(SYSTEM_CONFIGURATION);
349 Status = gRT->GetVariable(
350 L"SetupRecovery",
351 &gEfiNormalSetupGuid,
352 NULL,
353 &VarSize,
354 &mSystemConfiguration
355 );
356 ASSERT_EFI_ERROR (Status);
357 }
358
359 //
360 // Install on a new handle
361 //
362 Status = gBS->InstallProtocolInterface (
363 &mPciPlatformHandle,
364 &gEfiPciPlatformProtocolGuid,
365 EFI_NATIVE_INTERFACE,
366 &mPciPlatform
367 );
368
369 return Status;
370 }
371
372