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_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
= 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
151 EFI_STATUS retStatus
;
154 PCI_EXPANSION_ROM_HEADER
*RomHeader
;
155 PCI_DATA_STRUCTURE
*RomPcir
;
161 RomSize
= PciDevice
->RomSize
;
169 // Get the RomBarIndex
175 RomBarIndex
= PCI_EXPANSION_ROM_BASE
;
176 if (IS_PCI_BRIDGE (&(PciDevice
->Pci
))) {
184 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
187 // Allocate memory for Rom header and PCIR
189 RomHeader
= AllocatePool (sizeof (PCI_EXPANSION_ROM_HEADER
));
190 if (RomHeader
== NULL
) {
191 return EFI_OUT_OF_RESOURCES
;
194 RomPcir
= AllocatePool (sizeof (PCI_DATA_STRUCTURE
));
195 if (RomPcir
== NULL
) {
196 gBS
->FreePool (RomHeader
);
197 return EFI_OUT_OF_RESOURCES
;
200 RomBar
= (UINT32
)ReservedMemoryBase
;
205 RomDecode (PciDevice
, RomBarIndex
, RomBar
, TRUE
);
207 RomBarOffset
= RomBar
;
208 retStatus
= EFI_NOT_FOUND
;
212 PciDevice
->PciRootBridgeIo
->Mem
.Read (
213 PciDevice
->PciRootBridgeIo
,
216 sizeof (PCI_EXPANSION_ROM_HEADER
),
220 if (RomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
221 RomBarOffset
= RomBarOffset
+ 512;
225 RomImageSize
= RomImageSize
+ 512;
231 OffsetPcir
= RomHeader
->PcirOffset
;
232 PciDevice
->PciRootBridgeIo
->Mem
.Read (
233 PciDevice
->PciRootBridgeIo
,
235 RomBarOffset
+ OffsetPcir
,
236 sizeof (PCI_DATA_STRUCTURE
),
239 if (RomPcir
->CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
240 CodeType
= PCI_CODE_TYPE_PCAT_IMAGE
;
242 Indicator
= RomPcir
->Indicator
;
243 RomImageSize
= RomImageSize
+ RomPcir
->ImageLength
* 512;
244 RomBarOffset
= RomBarOffset
+ RomPcir
->ImageLength
* 512;
245 } while (((Indicator
& 0x80) == 0x00) && ((RomBarOffset
- RomBar
) < RomSize
));
248 // Some Legacy Cards do not report the correct ImageLength so used the maximum
249 // of the legacy length and the PCIR Image Length
251 if (CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
252 RomImageSize
= MAX(RomImageSize
, (((EFI_LEGACY_EXPANSION_ROM_HEADER
*)RomHeader
)->Size512
* 512));
255 if (RomImageSize
> 0) {
256 retStatus
= EFI_SUCCESS
;
257 Image
= AllocatePool ((UINT32
) RomImageSize
);
259 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
260 gBS
->FreePool (RomHeader
);
261 gBS
->FreePool (RomPcir
);
262 return EFI_OUT_OF_RESOURCES
;
266 // Copy Rom image into memory
268 PciDevice
->PciRootBridgeIo
->Mem
.Read (
269 PciDevice
->PciRootBridgeIo
,
272 (UINT32
) RomImageSize
,
278 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
280 PciDevice
->PciIo
.RomSize
= RomImageSize
;
281 PciDevice
->PciIo
.RomImage
= RomInMemory
;
284 // Free allocated memory
286 gBS
->FreePool (RomHeader
);
287 gBS
->FreePool (RomPcir
);
294 IN PCI_IO_DEVICE
*PciDevice
,
295 IN UINT8 RomBarIndex
,
313 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
315 PciRootBridgeIo
= PciDevice
->PciRootBridgeIo
;
317 Address
= EFI_PCI_ADDRESS (PciDevice
->BusNumber
, PciDevice
->DeviceNumber
, PciDevice
->FunctionNumber
, RomBarIndex
);
319 // set the Rom base address: now is hardcode
321 PciRootBridgeIo
->Pci
.Write(
329 // enable its decoder
331 Value32
= RomBar
| 0x1;
332 PciRootBridgeIo
->Pci
.Write(
340 //setting the memory space bit in the function's command register
342 Address
= EFI_PCI_ADDRESS (PciDevice
->BusNumber
, PciDevice
->DeviceNumber
, PciDevice
->FunctionNumber
, 0x04);
343 PciRootBridgeIo
->Pci
.Read(
350 CommandValue
= (UINT16
)(CommandValue
| 0x0002); //0x0003
351 PciRootBridgeIo
->Pci
.Write(
359 // disable rom decode
361 Address
= EFI_PCI_ADDRESS (PciDevice
->BusNumber
, PciDevice
->DeviceNumber
, PciDevice
->FunctionNumber
, RomBarIndex
);
362 Value32
= 0xfffffffe;
363 PciRootBridgeIo
->Pci
.Write(
377 PCI_IO_DEVICE
*PciDevice
383 Process the oprom image.
386 PciDevice A pointer to a pci device.
399 EFI_HANDLE ImageHandle
;
401 EFI_STATUS retStatus
;
404 UINT32 DestinationSize
;
408 VOID
*DecompressedImageBuffer
;
410 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
411 EFI_PCI_EXPANSION_ROM_HEADER
*EfiRomHeader
;
412 PCI_DATA_STRUCTURE
*Pcir
;
417 // Get the Address of the Rom image
419 RomBar
= PciDevice
->PciIo
.RomImage
;
420 RomBarOffset
= (UINT8
*) RomBar
;
421 retStatus
= EFI_NOT_FOUND
;
425 EfiRomHeader
= (EFI_PCI_EXPANSION_ROM_HEADER
*) RomBarOffset
;
426 if (EfiRomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
427 RomBarOffset
= RomBarOffset
+ 512;
436 Pcir
= (PCI_DATA_STRUCTURE
*) (RomBarOffset
+ EfiRomHeader
->PcirOffset
);
437 ImageSize
= (UINT32
) (Pcir
->ImageLength
* 512);
438 Indicator
= Pcir
->Indicator
;
440 if ((Pcir
->CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) &&
441 (EfiRomHeader
->EfiSignature
== EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
)) {
443 if ((EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
) ||
444 (EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
)) {
446 ImageOffset
= EfiRomHeader
->EfiImageHeaderOffset
;
447 ImageSize
= (UINT32
) (EfiRomHeader
->InitializationSize
* 512);
449 ImageBuffer
= (VOID
*) (RomBarOffset
+ ImageOffset
);
450 ImageLength
= ImageSize
- (UINT32
)ImageOffset
;
451 DecompressedImageBuffer
= NULL
;
454 // decompress here if needed
457 if (EfiRomHeader
->CompressionType
> EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
461 if (EfiRomHeader
->CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
462 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**) &Decompress
);
463 if (EFI_ERROR (Status
)) {
467 Status
= Decompress
->GetInfo (
474 if (!EFI_ERROR (Status
)) {
475 DecompressedImageBuffer
= NULL
;
476 DecompressedImageBuffer
= AllocatePool (DestinationSize
);
477 if (DecompressedImageBuffer
!= NULL
) {
478 Scratch
= AllocatePool (ScratchSize
);
479 if (Scratch
!= NULL
) {
480 Status
= Decompress
->Decompress (
484 DecompressedImageBuffer
,
489 if (!EFI_ERROR (Status
)) {
490 ImageBuffer
= DecompressedImageBuffer
;
491 ImageLength
= DestinationSize
;
495 gBS
->FreePool (Scratch
);
504 // load image and start image
506 Status
= gBS
->LoadImage (
508 gPciBusDriverBinding
.DriverBindingHandle
,
514 if (!EFI_ERROR (Status
)) {
515 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
516 if (!EFI_ERROR (Status
)) {
517 AddDriver (PciDevice
, ImageHandle
);
518 retStatus
= EFI_SUCCESS
;
523 RomBarOffset
= RomBarOffset
+ ImageSize
;
525 RomBarOffset
= RomBarOffset
+ ImageSize
;
528 RomBarOffset
= RomBarOffset
+ ImageSize
;
531 } while (((Indicator
& 0x80) == 0x00) && ((UINTN
) (RomBarOffset
- (UINT8
*) RomBar
) < PciDevice
->RomSize
));