3 Copyright (c) 2005 - 2006, 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.
29 IN PCI_IO_DEVICE
*PciDevice
,
37 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_DEVICE_ROMBAR
;
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
= PciRootBridgeIo
->Pci
.Write (
96 if (EFI_ERROR (Status
)) {
103 Status
= PciRootBridgeIo
->Pci
.Read (
110 if (EFI_ERROR (Status
)) {
115 // Bits [1, 10] are reserved
117 AllOnes
&= 0xFFFFF800;
118 if ((AllOnes
== 0) || (AllOnes
== 0xFFFFF800)) {
119 return EFI_NOT_FOUND
;
122 DEBUG ((EFI_D_ERROR
, "PCIBUS: GetOpRomInfo: OPROM detected!\n"));
123 DEBUG ((EFI_D_ERROR
, "PCIBUS: GetOpRomInfo: B-%x, D-%x, F-%x\n", (UINTN
)Bus
, (UINTN
)Device
, (UINTN
)Function
));
125 PciIoDevice
->RomSize
= (UINT64
) ((~AllOnes
) + 1);
131 IN PCI_IO_DEVICE
*PciDevice
,
132 IN UINT64 ReservedMemoryBase
138 Load option rom image for specified PCI device
152 EFI_STATUS retStatus
;
155 PCI_EXPANSION_ROM_HEADER
*RomHeader
;
156 PCI_DATA_STRUCTURE
*RomPcir
;
162 RomSize
= PciDevice
->RomSize
;
171 // Get the RomBarIndex
177 RomBarIndex
= PCI_DEVICE_ROMBAR
;
178 if (IS_PCI_BRIDGE (&(PciDevice
->Pci
))) {
186 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
189 // Allocate memory for Rom header and PCIR
191 RomHeader
= AllocatePool (sizeof (PCI_EXPANSION_ROM_HEADER
));
192 if (RomHeader
== NULL
) {
193 return EFI_OUT_OF_RESOURCES
;
196 RomPcir
= AllocatePool (sizeof (PCI_DATA_STRUCTURE
));
197 if (RomPcir
== NULL
) {
198 gBS
->FreePool (RomHeader
);
199 return EFI_OUT_OF_RESOURCES
;
202 RomBar
= (UINT32
)ReservedMemoryBase
;
207 RomDecode (PciDevice
, RomBarIndex
, RomBar
, TRUE
);
209 RomBarOffset
= RomBar
;
210 retStatus
= EFI_NOT_FOUND
;
214 PciDevice
->PciRootBridgeIo
->Mem
.Read (
215 PciDevice
->PciRootBridgeIo
,
218 sizeof (PCI_EXPANSION_ROM_HEADER
),
222 if (RomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
223 RomBarOffset
= RomBarOffset
+ 512;
227 RomImageSize
= RomImageSize
+ 512;
233 OffsetPcir
= RomHeader
->PcirOffset
;
234 PciDevice
->PciRootBridgeIo
->Mem
.Read (
235 PciDevice
->PciRootBridgeIo
,
237 RomBarOffset
+ OffsetPcir
,
238 sizeof (PCI_DATA_STRUCTURE
),
241 if (RomPcir
->CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
242 CodeType
= PCI_CODE_TYPE_PCAT_IMAGE
;
244 Indicator
= RomPcir
->Indicator
;
245 RomImageSize
= RomImageSize
+ RomPcir
->ImageLength
* 512;
246 RomBarOffset
= RomBarOffset
+ RomPcir
->ImageLength
* 512;
247 } while (((Indicator
& 0x80) == 0x00) && ((RomBarOffset
- RomBar
) < RomSize
));
250 // Some Legacy Cards do not report the correct ImageLength so used the maximum
251 // of the legacy length and the PCIR Image Length
253 if (CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
254 RomImageSize
= MAX(RomImageSize
, (((EFI_LEGACY_EXPANSION_ROM_HEADER
*)RomHeader
)->Size512
* 512));
257 if (RomImageSize
> 0) {
258 retStatus
= EFI_SUCCESS
;
259 Image
= AllocatePool ((UINT32
) RomImageSize
);
261 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
262 gBS
->FreePool (RomHeader
);
263 gBS
->FreePool (RomPcir
);
264 return EFI_OUT_OF_RESOURCES
;
268 // Copy Rom image into memory
270 PciDevice
->PciRootBridgeIo
->Mem
.Read (
271 PciDevice
->PciRootBridgeIo
,
274 (UINT32
) RomImageSize
,
280 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
282 PciDevice
->PciIo
.RomSize
= RomImageSize
;
283 PciDevice
->PciIo
.RomImage
= RomInMemory
;
286 // Free allocated memory
288 gBS
->FreePool (RomHeader
);
289 gBS
->FreePool (RomPcir
);
296 IN PCI_IO_DEVICE
*PciDevice
,
297 IN UINT8 RomBarIndex
,
315 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
317 PciRootBridgeIo
= PciDevice
->PciRootBridgeIo
;
319 Address
= EFI_PCI_ADDRESS (PciDevice
->BusNumber
, PciDevice
->DeviceNumber
, PciDevice
->FunctionNumber
, RomBarIndex
);
321 // set the Rom base address: now is hardcode
323 PciRootBridgeIo
->Pci
.Write(
331 // enable its decoder
333 Value32
= RomBar
| 0x1;
334 PciRootBridgeIo
->Pci
.Write(
342 //setting the memory space bit in the function's command register
344 Address
= EFI_PCI_ADDRESS (PciDevice
->BusNumber
, PciDevice
->DeviceNumber
, PciDevice
->FunctionNumber
, 0x04);
345 PciRootBridgeIo
->Pci
.Read(
352 CommandValue
= (UINT16
)(CommandValue
| 0x0002); //0x0003
353 PciRootBridgeIo
->Pci
.Write(
361 // disable rom decode
363 Address
= EFI_PCI_ADDRESS (PciDevice
->BusNumber
, PciDevice
->DeviceNumber
, PciDevice
->FunctionNumber
, RomBarIndex
);
364 Value32
= 0xfffffffe;
365 PciRootBridgeIo
->Pci
.Write(
379 PCI_IO_DEVICE
*PciDevice
385 Process the oprom image.
388 PciDevice A pointer to a pci device.
401 EFI_HANDLE ImageHandle
;
403 EFI_STATUS retStatus
;
406 UINT32 DestinationSize
;
410 VOID
*DecompressedImageBuffer
;
412 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
413 EFI_PCI_EXPANSION_ROM_HEADER
*EfiRomHeader
;
414 PCI_DATA_STRUCTURE
*Pcir
;
419 // Get the Address of the Rom image
421 RomBar
= PciDevice
->PciIo
.RomImage
;
422 RomBarOffset
= (UINT8
*) RomBar
;
423 retStatus
= EFI_NOT_FOUND
;
427 EfiRomHeader
= (EFI_PCI_EXPANSION_ROM_HEADER
*) RomBarOffset
;
428 if (EfiRomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
429 RomBarOffset
= RomBarOffset
+ 512;
438 Pcir
= (PCI_DATA_STRUCTURE
*) (RomBarOffset
+ EfiRomHeader
->PcirOffset
);
439 ImageSize
= (UINT32
) (Pcir
->ImageLength
* 512);
440 Indicator
= Pcir
->Indicator
;
442 if ((Pcir
->CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) &&
443 (EfiRomHeader
->EfiSignature
== EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
)) {
445 if ((EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
) ||
446 (EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
)) {
448 ImageOffset
= EfiRomHeader
->EfiImageHeaderOffset
;
449 ImageSize
= (UINT32
) (EfiRomHeader
->InitializationSize
* 512);
451 ImageBuffer
= (VOID
*) (RomBarOffset
+ ImageOffset
);
452 ImageLength
= ImageSize
- (UINT32
)ImageOffset
;
453 DecompressedImageBuffer
= NULL
;
456 // decompress here if needed
459 if (EfiRomHeader
->CompressionType
> EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
463 if (EfiRomHeader
->CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
464 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**) &Decompress
);
465 if (EFI_ERROR (Status
)) {
469 Status
= Decompress
->GetInfo (
476 if (!EFI_ERROR (Status
)) {
477 DecompressedImageBuffer
= NULL
;
478 DecompressedImageBuffer
= AllocatePool (DestinationSize
);
479 if (DecompressedImageBuffer
!= NULL
) {
480 Scratch
= AllocatePool (ScratchSize
);
481 if (Scratch
!= NULL
) {
482 Status
= Decompress
->Decompress (
486 DecompressedImageBuffer
,
491 if (!EFI_ERROR (Status
)) {
492 ImageBuffer
= DecompressedImageBuffer
;
493 ImageLength
= DestinationSize
;
497 gBS
->FreePool (Scratch
);
506 // load image and start image
508 Status
= gBS
->LoadImage (
510 gPciBusDriverBinding
.DriverBindingHandle
,
516 if (!EFI_ERROR (Status
)) {
517 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
518 if (!EFI_ERROR (Status
)) {
519 AddDriver (PciDevice
, ImageHandle
);
520 retStatus
= EFI_SUCCESS
;
525 RomBarOffset
= RomBarOffset
+ ImageSize
;
527 RomBarOffset
= RomBarOffset
+ ImageSize
;
530 RomBarOffset
= RomBarOffset
+ ImageSize
;
533 } while (((Indicator
& 0x80) == 0x00) && ((UINTN
) (RomBarOffset
- (UINT8
*) RomBar
) < PciDevice
->RomSize
));