3 Copyright (c) 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.
15 #include "PciResourceSupport.h"
17 #include <IndustryStandard/Pci23.h>
21 #define EFI_MIN(a, b) (((a) < (b)) ? (a) : (b))
22 #define EFI_MAX(a, b) (((a) > (b)) ? (a) : (b))
27 IN PCI_IO_DEVICE
*PciIoDevice
38 // TODO: PciIoDevice - add argument and description to function comment
39 // TODO: EFI_NOT_FOUND - add return value to function comment
40 // TODO: EFI_SUCCESS - add return value to function comment
49 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
51 Bus
= PciIoDevice
->BusNumber
;
52 Device
= PciIoDevice
->DeviceNumber
;
53 Function
= PciIoDevice
->FunctionNumber
;
55 PciRootBridgeIo
= PciIoDevice
->PciRootBridgeIo
;
58 // offset is 0x30 if is not ppb
64 RomBarIndex
= PCI_DEVICE_ROMBAR
;
66 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
)) {
74 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
77 // the bit0 is 0 to prevent the enabling of the Rom address decoder
80 Address
= EFI_PCI_ADDRESS (Bus
, Device
, Function
, RomBarIndex
);
82 Status
= PciRootBridgeIoWrite (
90 if (EFI_ERROR (Status
)) {
97 Status
= PciRootBridgeIoRead (
105 if (EFI_ERROR (Status
)) {
109 // Bits [1, 10] are reserved
111 AllOnes
&= 0xFFFFF800;
112 if ((AllOnes
== 0) || (AllOnes
== 0xFFFFF800)) {
113 return EFI_NOT_FOUND
;
116 PciIoDevice
->RomSize
= (UINT64
) ((~AllOnes
) + 1);
122 IN PCI_IO_DEVICE
*PciDevice
,
129 Load option rom image for specified PCI device
136 // TODO: PciDevice - add argument and description to function comment
137 // TODO: RomBase - add argument and description to function comment
138 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
139 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
140 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
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_DEVICE_ROMBAR
;
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
= EFI_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
;
279 PciRomAddImageMapping (
281 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
282 PciDevice
->BusNumber
,
283 PciDevice
->DeviceNumber
,
284 PciDevice
->FunctionNumber
,
285 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
286 PciDevice
->PciIo
.RomSize
290 // Free allocated memory
292 gBS
->FreePool (RomHeader
);
293 gBS
->FreePool (RomPcir
);
300 IN PCI_IO_DEVICE
*PciDevice
,
301 IN UINT8 RomBarIndex
,
314 // TODO: PciDevice - add argument and description to function comment
315 // TODO: RomBarIndex - add argument and description to function comment
316 // TODO: RomBar - add argument and description to function comment
317 // TODO: Enable - add argument and description to function comment
318 // TODO: EFI_SUCCESS - add return value to function comment
322 EFI_PCI_IO_PROTOCOL
*PciIo
;
324 PciIo
= &PciDevice
->PciIo
;
329 for (Offset
= 0x10; Offset
<= 0x24; Offset
+= sizeof (UINT32
)) {
330 PciIoWrite (PciIo
, EfiPciIoWidthUint32
, Offset
, 1, &gAllZero
);
334 // set the Rom base address: now is hardcode
335 // enable its decoder
337 Value32
= RomBar
| 0x1;
340 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
347 // Programe all upstream bridge
349 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, TRUE
);
352 // Setting the memory space bit in the function's command register
354 PciEnableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
359 // disable command register decode to memory
361 PciDisableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
364 // Destroy the programmed bar in all the upstream bridge.
366 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, FALSE
);
369 // disable rom decode
371 Value32
= 0xFFFFFFFE;
374 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
388 PCI_IO_DEVICE
*PciDevice
394 Process the oprom image.
397 PciDevice A pointer to a pci device.
410 EFI_HANDLE ImageHandle
;
412 EFI_STATUS retStatus
;
415 UINT32 DestinationSize
;
419 VOID
*DecompressedImageBuffer
;
421 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
422 EFI_PCI_EXPANSION_ROM_HEADER
*EfiRomHeader
;
423 PCI_DATA_STRUCTURE
*Pcir
;
428 // Get the Address of the Rom image
430 RomBar
= PciDevice
->PciIo
.RomImage
;
431 RomBarOffset
= (UINT8
*) RomBar
;
432 retStatus
= EFI_NOT_FOUND
;
436 EfiRomHeader
= (EFI_PCI_EXPANSION_ROM_HEADER
*) RomBarOffset
;
437 if (EfiRomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
438 RomBarOffset
= RomBarOffset
+ 512;
447 Pcir
= (PCI_DATA_STRUCTURE
*) (RomBarOffset
+ EfiRomHeader
->PcirOffset
);
448 ImageSize
= (UINT32
) (Pcir
->ImageLength
* 512);
449 Indicator
= Pcir
->Indicator
;
451 if ((Pcir
->CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) &&
452 (EfiRomHeader
->EfiSignature
== EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
)) {
454 if ((EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
) ||
455 (EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
)) {
457 ImageOffset
= EfiRomHeader
->EfiImageHeaderOffset
;
458 ImageSize
= (UINT32
) (EfiRomHeader
->InitializationSize
* 512);
460 ImageBuffer
= (VOID
*) (RomBarOffset
+ ImageOffset
);
461 ImageLength
= ImageSize
- (UINT32
)ImageOffset
;
462 DecompressedImageBuffer
= NULL
;
465 // decompress here if needed
468 if (EfiRomHeader
->CompressionType
> EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
472 if (EfiRomHeader
->CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
473 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**) &Decompress
);
474 if (EFI_ERROR (Status
)) {
478 Status
= Decompress
->GetInfo (
485 if (!EFI_ERROR (Status
)) {
486 DecompressedImageBuffer
= NULL
;
487 DecompressedImageBuffer
= AllocatePool (DestinationSize
);
488 if (DecompressedImageBuffer
!= NULL
) {
489 Scratch
= AllocatePool (ScratchSize
);
490 if (Scratch
!= NULL
) {
491 Status
= Decompress
->Decompress (
495 DecompressedImageBuffer
,
500 if (!EFI_ERROR (Status
)) {
501 ImageBuffer
= DecompressedImageBuffer
;
502 ImageLength
= DestinationSize
;
506 gBS
->FreePool (Scratch
);
515 // load image and start image
517 Status
= gBS
->LoadImage (
519 gPciBusDriverBinding
.DriverBindingHandle
,
525 if (!EFI_ERROR (Status
)) {
526 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
527 if (!EFI_ERROR (Status
)) {
528 AddDriver (PciDevice
, ImageHandle
);
529 PciRomAddImageMapping (
531 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
532 PciDevice
->BusNumber
,
533 PciDevice
->DeviceNumber
,
534 PciDevice
->FunctionNumber
,
535 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
536 PciDevice
->PciIo
.RomSize
538 retStatus
= EFI_SUCCESS
;
543 RomBarOffset
= RomBarOffset
+ ImageSize
;
545 RomBarOffset
= RomBarOffset
+ ImageSize
;
548 RomBarOffset
= RomBarOffset
+ ImageSize
;
551 } while (((Indicator
& 0x80) == 0x00) && ((UINTN
) (RomBarOffset
- (UINT8
*) RomBar
) < PciDevice
->RomSize
));