3 Copyright (c) 2006 - 2007, 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
= {
26 sizeof (MEMMAP_DEVICE_PATH
)
34 Get Pci device's oprom infor bits.
36 @retval EFI_NOT_FOUND Pci device has not oprom
37 @retval EFI_SUCCESS Pci device has oprom
41 IN PCI_IO_DEVICE
*PciIoDevice
51 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
53 Bus
= PciIoDevice
->BusNumber
;
54 Device
= PciIoDevice
->DeviceNumber
;
55 Function
= PciIoDevice
->FunctionNumber
;
57 PciRootBridgeIo
= PciIoDevice
->PciRootBridgeIo
;
60 // offset is 0x30 if is not ppb
66 RomBarIndex
= PCI_DEVICE_ROMBAR
;
68 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
)) {
76 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
79 // the bit0 is 0 to prevent the enabling of the Rom address decoder
82 Address
= EFI_PCI_ADDRESS (Bus
, Device
, Function
, RomBarIndex
);
84 Status
= PciRootBridgeIoWrite (
92 if (EFI_ERROR (Status
)) {
99 Status
= PciRootBridgeIoRead (
107 if (EFI_ERROR (Status
)) {
111 // Bits [1, 10] are reserved
113 AllOnes
&= 0xFFFFF800;
114 if ((AllOnes
== 0) || (AllOnes
== 0xFFFFF800)) {
115 return EFI_NOT_FOUND
;
118 PciIoDevice
->RomSize
= (UINT64
) ((~AllOnes
) + 1);
123 Load option rom image for specified PCI device
125 @param PciDevice Pci device instance
126 @param RomBase Base address of oprom.
128 @retval EFI_OUT_OF_RESOURCES not enough memory to hold image
129 @retval EFI_SUCESS Success
133 IN PCI_IO_DEVICE
*PciDevice
,
142 EFI_STATUS retStatus
;
145 PCI_EXPANSION_ROM_HEADER
*RomHeader
;
146 PCI_DATA_STRUCTURE
*RomPcir
;
152 RomSize
= PciDevice
->RomSize
;
160 // Get the RomBarIndex
166 RomBarIndex
= PCI_DEVICE_ROMBAR
;
167 if (IS_PCI_BRIDGE (&(PciDevice
->Pci
))) {
175 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
178 // Allocate memory for Rom header and PCIR
180 RomHeader
= AllocatePool (sizeof (PCI_EXPANSION_ROM_HEADER
));
181 if (RomHeader
== NULL
) {
182 return EFI_OUT_OF_RESOURCES
;
185 RomPcir
= AllocatePool (sizeof (PCI_DATA_STRUCTURE
));
186 if (RomPcir
== NULL
) {
187 gBS
->FreePool (RomHeader
);
188 return EFI_OUT_OF_RESOURCES
;
191 RomBar
= (UINT32
) RomBase
;
196 RomDecode (PciDevice
, RomBarIndex
, RomBar
, TRUE
);
198 RomBarOffset
= RomBar
;
199 retStatus
= EFI_NOT_FOUND
;
203 PciDevice
->PciRootBridgeIo
->Mem
.Read (
204 PciDevice
->PciRootBridgeIo
,
207 sizeof (PCI_EXPANSION_ROM_HEADER
),
211 if (RomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
212 RomBarOffset
= RomBarOffset
+ 512;
216 RomImageSize
= RomImageSize
+ 512;
222 OffsetPcir
= RomHeader
->PcirOffset
;
223 PciDevice
->PciRootBridgeIo
->Mem
.Read (
224 PciDevice
->PciRootBridgeIo
,
226 RomBarOffset
+ OffsetPcir
,
227 sizeof (PCI_DATA_STRUCTURE
),
230 if (RomPcir
->CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
231 CodeType
= PCI_CODE_TYPE_PCAT_IMAGE
;
233 Indicator
= RomPcir
->Indicator
;
234 RomImageSize
= RomImageSize
+ RomPcir
->ImageLength
* 512;
235 RomBarOffset
= RomBarOffset
+ RomPcir
->ImageLength
* 512;
236 } while (((Indicator
& 0x80) == 0x00) && ((RomBarOffset
- RomBar
) < RomSize
));
239 // Some Legacy Cards do not report the correct ImageLength so used the maximum
240 // of the legacy length and the PCIR Image Length
242 if (CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
243 RomImageSize
= MAX(RomImageSize
, (((EFI_LEGACY_EXPANSION_ROM_HEADER
*)RomHeader
)->Size512
* 512));
246 if (RomImageSize
> 0) {
247 retStatus
= EFI_SUCCESS
;
248 Image
= AllocatePool ((UINT32
) RomImageSize
);
250 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
251 gBS
->FreePool (RomHeader
);
252 gBS
->FreePool (RomPcir
);
253 return EFI_OUT_OF_RESOURCES
;
257 // Copy Rom image into memory
259 PciDevice
->PciRootBridgeIo
->Mem
.Read (
260 PciDevice
->PciRootBridgeIo
,
263 (UINT32
) RomImageSize
,
269 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
271 PciDevice
->PciIo
.RomSize
= RomImageSize
;
272 PciDevice
->PciIo
.RomImage
= RomInMemory
;
275 // For OpROM read from PCI device:
276 // Add the Rom Image to internal database for later PCI light enumeration
278 PciRomAddImageMapping (
280 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
281 PciDevice
->BusNumber
,
282 PciDevice
->DeviceNumber
,
283 PciDevice
->FunctionNumber
,
284 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
285 PciDevice
->PciIo
.RomSize
289 // Free allocated memory
291 gBS
->FreePool (RomHeader
);
292 gBS
->FreePool (RomPcir
);
298 enable/disable oprom decode
300 @param PciDevice pci device instance
301 @param RomBarIndex The BAR index of the standard PCI Configuration header to use as the
302 base address for resource range. The legal range for this field is 0..5.
303 @param RomBar Base address of rom
304 @param Enable Flag for enable/disable decode.
306 @retval EFI_SUCCESS Success
310 IN PCI_IO_DEVICE
*PciDevice
,
311 IN UINT8 RomBarIndex
,
318 EFI_PCI_IO_PROTOCOL
*PciIo
;
320 PciIo
= &PciDevice
->PciIo
;
325 for (Offset
= 0x10; Offset
<= 0x24; Offset
+= sizeof (UINT32
)) {
326 PciIoWrite (PciIo
, EfiPciIoWidthUint32
, Offset
, 1, &gAllZero
);
330 // set the Rom base address: now is hardcode
331 // enable its decoder
333 Value32
= RomBar
| 0x1;
336 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
343 // Programe all upstream bridge
345 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, TRUE
);
348 // Setting the memory space bit in the function's command register
350 PciEnableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
355 // disable command register decode to memory
357 PciDisableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
360 // Destroy the programmed bar in all the upstream bridge.
362 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, FALSE
);
365 // disable rom decode
367 Value32
= 0xFFFFFFFE;
370 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
383 Process the oprom image.
385 @param PciDevice Pci device instance
389 PCI_IO_DEVICE
*PciDevice
397 EFI_HANDLE ImageHandle
;
399 EFI_STATUS retStatus
;
402 UINT32 DestinationSize
;
406 VOID
*DecompressedImageBuffer
;
408 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
409 EFI_PCI_EXPANSION_ROM_HEADER
*EfiRomHeader
;
410 PCI_DATA_STRUCTURE
*Pcir
;
411 EFI_DEVICE_PATH_PROTOCOL
*PciOptionRomImageDevicePath
;
416 // Get the Address of the Rom image
418 RomBar
= PciDevice
->PciIo
.RomImage
;
419 RomBarOffset
= (UINT8
*) RomBar
;
420 retStatus
= EFI_NOT_FOUND
;
424 EfiRomHeader
= (EFI_PCI_EXPANSION_ROM_HEADER
*) RomBarOffset
;
425 if (EfiRomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
426 RomBarOffset
= RomBarOffset
+ 512;
435 Pcir
= (PCI_DATA_STRUCTURE
*) (RomBarOffset
+ EfiRomHeader
->PcirOffset
);
436 ImageSize
= (UINT32
) (Pcir
->ImageLength
* 512);
437 Indicator
= Pcir
->Indicator
;
439 if ((Pcir
->CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) &&
440 (EfiRomHeader
->EfiSignature
== EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
)) {
442 if ((EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
) ||
443 (EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
)) {
445 ImageOffset
= EfiRomHeader
->EfiImageHeaderOffset
;
446 ImageSize
= (UINT32
) (EfiRomHeader
->InitializationSize
* 512);
448 ImageBuffer
= (VOID
*) (RomBarOffset
+ ImageOffset
);
449 ImageLength
= ImageSize
- (UINT32
)ImageOffset
;
450 DecompressedImageBuffer
= NULL
;
453 // decompress here if needed
456 if (EfiRomHeader
->CompressionType
> EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
460 if (EfiRomHeader
->CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
461 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**) &Decompress
);
462 if (EFI_ERROR (Status
)) {
466 Status
= Decompress
->GetInfo (
473 if (!EFI_ERROR (Status
)) {
474 DecompressedImageBuffer
= NULL
;
475 DecompressedImageBuffer
= AllocatePool (DestinationSize
);
476 if (DecompressedImageBuffer
!= NULL
) {
477 Scratch
= AllocatePool (ScratchSize
);
478 if (Scratch
!= NULL
) {
479 Status
= Decompress
->Decompress (
483 DecompressedImageBuffer
,
488 if (!EFI_ERROR (Status
)) {
489 ImageBuffer
= DecompressedImageBuffer
;
490 ImageLength
= DestinationSize
;
494 gBS
->FreePool (Scratch
);
503 // Build Memory Mapped device path node to record the image offset into the PCI Option ROM
505 mPciOptionRomImageDevicePathNodeTemplate
.StartingAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) (RomBarOffset
- (UINT8
*) RomBar
);
506 mPciOptionRomImageDevicePathNodeTemplate
.EndingAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) (RomBarOffset
+ ImageSize
- 1 - (UINT8
*) RomBar
);
507 PciOptionRomImageDevicePath
= AppendDevicePathNode (PciDevice
->DevicePath
, (const EFI_DEVICE_PATH_PROTOCOL
*)&mPciOptionRomImageDevicePathNodeTemplate
);
508 ASSERT (PciOptionRomImageDevicePath
!= NULL
);
511 // load image and start image
513 Status
= gBS
->LoadImage (
515 gPciBusDriverBinding
.DriverBindingHandle
,
516 PciOptionRomImageDevicePath
,
523 // Free the device path after it has been used by LoadImage
525 gBS
->FreePool (PciOptionRomImageDevicePath
);
527 if (!EFI_ERROR (Status
)) {
528 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
529 if (!EFI_ERROR (Status
)) {
530 AddDriver (PciDevice
, ImageHandle
);
531 PciRomAddImageMapping (
533 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
534 PciDevice
->BusNumber
,
535 PciDevice
->DeviceNumber
,
536 PciDevice
->FunctionNumber
,
537 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
538 PciDevice
->PciIo
.RomSize
540 retStatus
= EFI_SUCCESS
;
545 RomBarOffset
= RomBarOffset
+ ImageSize
;
547 RomBarOffset
= RomBarOffset
+ ImageSize
;
550 RomBarOffset
= RomBarOffset
+ ImageSize
;
553 } while (((Indicator
& 0x80) == 0x00) && ((UINTN
) (RomBarOffset
- (UINT8
*) RomBar
) < PciDevice
->RomSize
));