3 Copyright (c) 2006 - 2008, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "PciResourceSupport.h"
17 #include <IndustryStandard/Pci23.h>
20 // Module global for a template of the PCI option ROM Image Device Path Node
22 MEMMAP_DEVICE_PATH mPciOptionRomImageDevicePathNodeTemplate
= {
27 (UINT8
) (sizeof (MEMMAP_DEVICE_PATH
)),
28 (UINT8
) ((sizeof (MEMMAP_DEVICE_PATH
)) >> 8)
37 Get Pci device's oprom infor bits.
39 @param PciIoDevice Pci device instance
41 @retval EFI_NOT_FOUND Pci device has not oprom
42 @retval EFI_SUCCESS Pci device has oprom
46 IN PCI_IO_DEVICE
*PciIoDevice
56 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
58 Bus
= PciIoDevice
->BusNumber
;
59 Device
= PciIoDevice
->DeviceNumber
;
60 Function
= PciIoDevice
->FunctionNumber
;
62 PciRootBridgeIo
= PciIoDevice
->PciRootBridgeIo
;
65 // offset is 0x30 if is not ppb
71 RomBarIndex
= PCI_EXPANSION_ROM_BASE
;
73 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
)) {
81 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
84 // the bit0 is 0 to prevent the enabling of the Rom address decoder
87 Address
= EFI_PCI_ADDRESS (Bus
, Device
, Function
, RomBarIndex
);
89 Status
= PciRootBridgeIoWrite (
97 if (EFI_ERROR (Status
)) {
104 Status
= PciRootBridgeIoRead (
112 if (EFI_ERROR (Status
)) {
116 // Bits [1, 10] are reserved
118 AllOnes
&= 0xFFFFF800;
119 if ((AllOnes
== 0) || (AllOnes
== 0xFFFFF800)) {
120 return EFI_NOT_FOUND
;
123 PciIoDevice
->RomSize
= (UINT64
) ((~AllOnes
) + 1);
128 Load option rom image for specified PCI device
130 @param PciDevice Pci device instance
131 @param RomBase Base address of oprom.
133 @retval EFI_OUT_OF_RESOURCES not enough memory to hold image
134 @retval EFI_SUCESS Success
138 IN PCI_IO_DEVICE
*PciDevice
,
147 EFI_STATUS RetStatus
;
150 PCI_EXPANSION_ROM_HEADER
*RomHeader
;
151 PCI_DATA_STRUCTURE
*RomPcir
;
157 RomSize
= PciDevice
->RomSize
;
165 // Get the RomBarIndex
171 RomBarIndex
= PCI_EXPANSION_ROM_BASE
;
172 if (IS_PCI_BRIDGE (&(PciDevice
->Pci
))) {
180 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
183 // Allocate memory for Rom header and PCIR
185 RomHeader
= AllocatePool (sizeof (PCI_EXPANSION_ROM_HEADER
));
186 if (RomHeader
== NULL
) {
187 return EFI_OUT_OF_RESOURCES
;
190 RomPcir
= AllocatePool (sizeof (PCI_DATA_STRUCTURE
));
191 if (RomPcir
== NULL
) {
192 gBS
->FreePool (RomHeader
);
193 return EFI_OUT_OF_RESOURCES
;
196 RomBar
= (UINT32
) RomBase
;
201 RomDecode (PciDevice
, RomBarIndex
, RomBar
, TRUE
);
203 RomBarOffset
= RomBar
;
204 RetStatus
= EFI_NOT_FOUND
;
208 PciDevice
->PciRootBridgeIo
->Mem
.Read (
209 PciDevice
->PciRootBridgeIo
,
212 sizeof (PCI_EXPANSION_ROM_HEADER
),
216 if (RomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
217 RomBarOffset
= RomBarOffset
+ 512;
221 RomImageSize
= RomImageSize
+ 512;
227 OffsetPcir
= RomHeader
->PcirOffset
;
228 PciDevice
->PciRootBridgeIo
->Mem
.Read (
229 PciDevice
->PciRootBridgeIo
,
231 RomBarOffset
+ OffsetPcir
,
232 sizeof (PCI_DATA_STRUCTURE
),
235 if (RomPcir
->CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
236 CodeType
= PCI_CODE_TYPE_PCAT_IMAGE
;
238 Indicator
= RomPcir
->Indicator
;
239 RomImageSize
= RomImageSize
+ RomPcir
->ImageLength
* 512;
240 RomBarOffset
= RomBarOffset
+ RomPcir
->ImageLength
* 512;
241 } while (((Indicator
& 0x80) == 0x00) && ((RomBarOffset
- RomBar
) < RomSize
));
244 // Some Legacy Cards do not report the correct ImageLength so used the maximum
245 // of the legacy length and the PCIR Image Length
247 if (CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
248 RomImageSize
= MAX(RomImageSize
, (((EFI_LEGACY_EXPANSION_ROM_HEADER
*)RomHeader
)->Size512
* 512));
251 if (RomImageSize
> 0) {
252 RetStatus
= EFI_SUCCESS
;
253 Image
= AllocatePool ((UINT32
) RomImageSize
);
255 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
256 gBS
->FreePool (RomHeader
);
257 gBS
->FreePool (RomPcir
);
258 return EFI_OUT_OF_RESOURCES
;
262 // Copy Rom image into memory
264 PciDevice
->PciRootBridgeIo
->Mem
.Read (
265 PciDevice
->PciRootBridgeIo
,
268 (UINT32
) RomImageSize
,
274 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
276 PciDevice
->PciIo
.RomSize
= RomImageSize
;
277 PciDevice
->PciIo
.RomImage
= RomInMemory
;
280 // For OpROM read from PCI device:
281 // Add the Rom Image to internal database for later PCI light enumeration
283 PciRomAddImageMapping (
285 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
286 PciDevice
->BusNumber
,
287 PciDevice
->DeviceNumber
,
288 PciDevice
->FunctionNumber
,
289 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
290 PciDevice
->PciIo
.RomSize
294 // Free allocated memory
296 gBS
->FreePool (RomHeader
);
297 gBS
->FreePool (RomPcir
);
303 enable/disable oprom decode
305 @param PciDevice pci device instance
306 @param RomBarIndex The BAR index of the standard PCI Configuration header to use as the
307 base address for resource range. The legal range for this field is 0..5.
308 @param RomBar Base address of rom
309 @param Enable Flag for enable/disable decode.
311 @retval EFI_SUCCESS Success
315 IN PCI_IO_DEVICE
*PciDevice
,
316 IN UINT8 RomBarIndex
,
323 EFI_PCI_IO_PROTOCOL
*PciIo
;
325 PciIo
= &PciDevice
->PciIo
;
330 for (Offset
= 0x10; Offset
<= 0x24; Offset
+= sizeof (UINT32
)) {
331 PciIoWrite (PciIo
, EfiPciIoWidthUint32
, Offset
, 1, &gAllZero
);
335 // set the Rom base address: now is hardcode
336 // enable its decoder
338 Value32
= RomBar
| 0x1;
341 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
348 // Programe all upstream bridge
350 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, TRUE
);
353 // Setting the memory space bit in the function's command register
355 PciEnableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
360 // disable command register decode to memory
362 PciDisableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
365 // Destroy the programmed bar in all the upstream bridge.
367 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, FALSE
);
370 // disable rom decode
372 Value32
= 0xFFFFFFFE;
375 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
388 Process the oprom image.
390 @param PciDevice Pci device instance
394 PCI_IO_DEVICE
*PciDevice
402 EFI_HANDLE ImageHandle
;
404 EFI_STATUS RetStatus
;
407 UINT32 DestinationSize
;
411 VOID
*DecompressedImageBuffer
;
413 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
414 EFI_PCI_EXPANSION_ROM_HEADER
*EfiRomHeader
;
415 PCI_DATA_STRUCTURE
*Pcir
;
416 EFI_DEVICE_PATH_PROTOCOL
*PciOptionRomImageDevicePath
;
421 // Get the Address of the Rom image
423 RomBar
= PciDevice
->PciIo
.RomImage
;
424 RomBarOffset
= (UINT8
*) RomBar
;
425 RetStatus
= EFI_NOT_FOUND
;
429 EfiRomHeader
= (EFI_PCI_EXPANSION_ROM_HEADER
*) RomBarOffset
;
430 if (EfiRomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
431 RomBarOffset
= RomBarOffset
+ 512;
440 Pcir
= (PCI_DATA_STRUCTURE
*) (RomBarOffset
+ EfiRomHeader
->PcirOffset
);
441 ImageSize
= (UINT32
) (Pcir
->ImageLength
* 512);
442 Indicator
= Pcir
->Indicator
;
444 if ((Pcir
->CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) &&
445 (EfiRomHeader
->EfiSignature
== EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
)) {
447 if ((EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
) ||
448 (EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
)) {
450 ImageOffset
= EfiRomHeader
->EfiImageHeaderOffset
;
451 ImageSize
= (UINT32
) (EfiRomHeader
->InitializationSize
* 512);
453 ImageBuffer
= (VOID
*) (RomBarOffset
+ ImageOffset
);
454 ImageLength
= ImageSize
- (UINT32
)ImageOffset
;
455 DecompressedImageBuffer
= NULL
;
458 // decompress here if needed
461 if (EfiRomHeader
->CompressionType
> EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
465 if (EfiRomHeader
->CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
466 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**) &Decompress
);
467 if (EFI_ERROR (Status
)) {
471 Status
= Decompress
->GetInfo (
478 if (!EFI_ERROR (Status
)) {
479 DecompressedImageBuffer
= NULL
;
480 DecompressedImageBuffer
= AllocatePool (DestinationSize
);
481 if (DecompressedImageBuffer
!= NULL
) {
482 Scratch
= AllocatePool (ScratchSize
);
483 if (Scratch
!= NULL
) {
484 Status
= Decompress
->Decompress (
488 DecompressedImageBuffer
,
493 if (!EFI_ERROR (Status
)) {
494 ImageBuffer
= DecompressedImageBuffer
;
495 ImageLength
= DestinationSize
;
499 gBS
->FreePool (Scratch
);
508 // Build Memory Mapped device path node to record the image offset into the PCI Option ROM
510 mPciOptionRomImageDevicePathNodeTemplate
.StartingAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) (RomBarOffset
- (UINT8
*) RomBar
);
511 mPciOptionRomImageDevicePathNodeTemplate
.EndingAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) (RomBarOffset
+ ImageSize
- 1 - (UINT8
*) RomBar
);
512 PciOptionRomImageDevicePath
= AppendDevicePathNode (PciDevice
->DevicePath
, (const EFI_DEVICE_PATH_PROTOCOL
*)&mPciOptionRomImageDevicePathNodeTemplate
);
513 ASSERT (PciOptionRomImageDevicePath
!= NULL
);
516 // load image and start image
518 Status
= gBS
->LoadImage (
520 gPciBusDriverBinding
.DriverBindingHandle
,
521 PciOptionRomImageDevicePath
,
528 // Free the device path after it has been used by LoadImage
530 gBS
->FreePool (PciOptionRomImageDevicePath
);
532 if (!EFI_ERROR (Status
)) {
533 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
534 if (!EFI_ERROR (Status
)) {
535 AddDriver (PciDevice
, ImageHandle
);
536 PciRomAddImageMapping (
538 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
539 PciDevice
->BusNumber
,
540 PciDevice
->DeviceNumber
,
541 PciDevice
->FunctionNumber
,
542 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
543 PciDevice
->PciIo
.RomSize
545 RetStatus
= EFI_SUCCESS
;
550 RomBarOffset
= RomBarOffset
+ ImageSize
;
552 RomBarOffset
= RomBarOffset
+ ImageSize
;
555 RomBarOffset
= RomBarOffset
+ ImageSize
;
558 } while (((Indicator
& 0x80) == 0x00) && ((UINTN
) (RomBarOffset
- (UINT8
*) RomBar
) < PciDevice
->RomSize
));