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>
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
)
35 IN PCI_IO_DEVICE
*PciIoDevice
46 // TODO: PciIoDevice - add argument and description to function comment
47 // TODO: EFI_NOT_FOUND - add return value to function comment
48 // TODO: EFI_SUCCESS - add return value to function comment
57 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
59 Bus
= PciIoDevice
->BusNumber
;
60 Device
= PciIoDevice
->DeviceNumber
;
61 Function
= PciIoDevice
->FunctionNumber
;
63 PciRootBridgeIo
= PciIoDevice
->PciRootBridgeIo
;
66 // offset is 0x30 if is not ppb
72 RomBarIndex
= PCI_DEVICE_ROMBAR
;
74 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
)) {
82 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
85 // the bit0 is 0 to prevent the enabling of the Rom address decoder
88 Address
= EFI_PCI_ADDRESS (Bus
, Device
, Function
, RomBarIndex
);
90 Status
= PciRootBridgeIoWrite (
98 if (EFI_ERROR (Status
)) {
105 Status
= PciRootBridgeIoRead (
113 if (EFI_ERROR (Status
)) {
117 // Bits [1, 10] are reserved
119 AllOnes
&= 0xFFFFF800;
120 if ((AllOnes
== 0) || (AllOnes
== 0xFFFFF800)) {
121 return EFI_NOT_FOUND
;
124 PciIoDevice
->RomSize
= (UINT64
) ((~AllOnes
) + 1);
130 IN PCI_IO_DEVICE
*PciDevice
,
137 Load option rom image for specified PCI device
144 // TODO: PciDevice - add argument and description to function comment
145 // TODO: RomBase - add argument and description to function comment
146 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
147 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
148 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
155 EFI_STATUS retStatus
;
158 PCI_EXPANSION_ROM_HEADER
*RomHeader
;
159 PCI_DATA_STRUCTURE
*RomPcir
;
165 RomSize
= PciDevice
->RomSize
;
173 // Get the RomBarIndex
179 RomBarIndex
= PCI_DEVICE_ROMBAR
;
180 if (IS_PCI_BRIDGE (&(PciDevice
->Pci
))) {
188 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
191 // Allocate memory for Rom header and PCIR
193 RomHeader
= AllocatePool (sizeof (PCI_EXPANSION_ROM_HEADER
));
194 if (RomHeader
== NULL
) {
195 return EFI_OUT_OF_RESOURCES
;
198 RomPcir
= AllocatePool (sizeof (PCI_DATA_STRUCTURE
));
199 if (RomPcir
== NULL
) {
200 gBS
->FreePool (RomHeader
);
201 return EFI_OUT_OF_RESOURCES
;
204 RomBar
= (UINT32
) RomBase
;
209 RomDecode (PciDevice
, RomBarIndex
, RomBar
, TRUE
);
211 RomBarOffset
= RomBar
;
212 retStatus
= EFI_NOT_FOUND
;
216 PciDevice
->PciRootBridgeIo
->Mem
.Read (
217 PciDevice
->PciRootBridgeIo
,
220 sizeof (PCI_EXPANSION_ROM_HEADER
),
224 if (RomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
225 RomBarOffset
= RomBarOffset
+ 512;
229 RomImageSize
= RomImageSize
+ 512;
235 OffsetPcir
= RomHeader
->PcirOffset
;
236 PciDevice
->PciRootBridgeIo
->Mem
.Read (
237 PciDevice
->PciRootBridgeIo
,
239 RomBarOffset
+ OffsetPcir
,
240 sizeof (PCI_DATA_STRUCTURE
),
243 if (RomPcir
->CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
244 CodeType
= PCI_CODE_TYPE_PCAT_IMAGE
;
246 Indicator
= RomPcir
->Indicator
;
247 RomImageSize
= RomImageSize
+ RomPcir
->ImageLength
* 512;
248 RomBarOffset
= RomBarOffset
+ RomPcir
->ImageLength
* 512;
249 } while (((Indicator
& 0x80) == 0x00) && ((RomBarOffset
- RomBar
) < RomSize
));
252 // Some Legacy Cards do not report the correct ImageLength so used the maximum
253 // of the legacy length and the PCIR Image Length
255 if (CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
256 RomImageSize
= MAX(RomImageSize
, (((EFI_LEGACY_EXPANSION_ROM_HEADER
*)RomHeader
)->Size512
* 512));
259 if (RomImageSize
> 0) {
260 retStatus
= EFI_SUCCESS
;
261 Image
= AllocatePool ((UINT32
) RomImageSize
);
263 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
264 gBS
->FreePool (RomHeader
);
265 gBS
->FreePool (RomPcir
);
266 return EFI_OUT_OF_RESOURCES
;
270 // Copy Rom image into memory
272 PciDevice
->PciRootBridgeIo
->Mem
.Read (
273 PciDevice
->PciRootBridgeIo
,
276 (UINT32
) RomImageSize
,
282 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
284 PciDevice
->PciIo
.RomSize
= RomImageSize
;
285 PciDevice
->PciIo
.RomImage
= RomInMemory
;
287 PciRomAddImageMapping (
289 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
290 PciDevice
->BusNumber
,
291 PciDevice
->DeviceNumber
,
292 PciDevice
->FunctionNumber
,
293 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
294 PciDevice
->PciIo
.RomSize
298 // Free allocated memory
300 gBS
->FreePool (RomHeader
);
301 gBS
->FreePool (RomPcir
);
308 IN PCI_IO_DEVICE
*PciDevice
,
309 IN UINT8 RomBarIndex
,
322 // TODO: PciDevice - add argument and description to function comment
323 // TODO: RomBarIndex - add argument and description to function comment
324 // TODO: RomBar - add argument and description to function comment
325 // TODO: Enable - add argument and description to function comment
326 // TODO: EFI_SUCCESS - add return value to function comment
330 EFI_PCI_IO_PROTOCOL
*PciIo
;
332 PciIo
= &PciDevice
->PciIo
;
337 for (Offset
= 0x10; Offset
<= 0x24; Offset
+= sizeof (UINT32
)) {
338 PciIoWrite (PciIo
, EfiPciIoWidthUint32
, Offset
, 1, &gAllZero
);
342 // set the Rom base address: now is hardcode
343 // enable its decoder
345 Value32
= RomBar
| 0x1;
348 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
355 // Programe all upstream bridge
357 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, TRUE
);
360 // Setting the memory space bit in the function's command register
362 PciEnableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
367 // disable command register decode to memory
369 PciDisableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
372 // Destroy the programmed bar in all the upstream bridge.
374 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, FALSE
);
377 // disable rom decode
379 Value32
= 0xFFFFFFFE;
382 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
396 PCI_IO_DEVICE
*PciDevice
402 Process the oprom image.
405 PciDevice A pointer to a pci device.
418 EFI_HANDLE ImageHandle
;
420 EFI_STATUS retStatus
;
423 UINT32 DestinationSize
;
427 VOID
*DecompressedImageBuffer
;
429 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
430 EFI_PCI_EXPANSION_ROM_HEADER
*EfiRomHeader
;
431 PCI_DATA_STRUCTURE
*Pcir
;
432 EFI_DEVICE_PATH_PROTOCOL
*PciOptionRomImageDevicePath
;
437 // Get the Address of the Rom image
439 RomBar
= PciDevice
->PciIo
.RomImage
;
440 RomBarOffset
= (UINT8
*) RomBar
;
441 retStatus
= EFI_NOT_FOUND
;
445 EfiRomHeader
= (EFI_PCI_EXPANSION_ROM_HEADER
*) RomBarOffset
;
446 if (EfiRomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
447 RomBarOffset
= RomBarOffset
+ 512;
456 Pcir
= (PCI_DATA_STRUCTURE
*) (RomBarOffset
+ EfiRomHeader
->PcirOffset
);
457 ImageSize
= (UINT32
) (Pcir
->ImageLength
* 512);
458 Indicator
= Pcir
->Indicator
;
460 if ((Pcir
->CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) &&
461 (EfiRomHeader
->EfiSignature
== EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
)) {
463 if ((EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
) ||
464 (EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
)) {
466 ImageOffset
= EfiRomHeader
->EfiImageHeaderOffset
;
467 ImageSize
= (UINT32
) (EfiRomHeader
->InitializationSize
* 512);
469 ImageBuffer
= (VOID
*) (RomBarOffset
+ ImageOffset
);
470 ImageLength
= ImageSize
- (UINT32
)ImageOffset
;
471 DecompressedImageBuffer
= NULL
;
474 // decompress here if needed
477 if (EfiRomHeader
->CompressionType
> EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
481 if (EfiRomHeader
->CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
482 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**) &Decompress
);
483 if (EFI_ERROR (Status
)) {
487 Status
= Decompress
->GetInfo (
494 if (!EFI_ERROR (Status
)) {
495 DecompressedImageBuffer
= NULL
;
496 DecompressedImageBuffer
= AllocatePool (DestinationSize
);
497 if (DecompressedImageBuffer
!= NULL
) {
498 Scratch
= AllocatePool (ScratchSize
);
499 if (Scratch
!= NULL
) {
500 Status
= Decompress
->Decompress (
504 DecompressedImageBuffer
,
509 if (!EFI_ERROR (Status
)) {
510 ImageBuffer
= DecompressedImageBuffer
;
511 ImageLength
= DestinationSize
;
515 gBS
->FreePool (Scratch
);
524 // Build full device path to the PCI Option ROM Image being loaded
526 mPciOptionRomImageDevicePathNodeTemplate
.StartingAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)RomBarOffset
;
527 mPciOptionRomImageDevicePathNodeTemplate
.EndingAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(RomBarOffset
+ ImageSize
- 1);
528 PciOptionRomImageDevicePath
= AppendDevicePathNode (PciDevice
->DevicePath
, (const EFI_DEVICE_PATH_PROTOCOL
*)&mPciOptionRomImageDevicePathNodeTemplate
);
529 ASSERT (PciOptionRomImageDevicePath
!= NULL
);
532 // load image and start image
534 Status
= gBS
->LoadImage (
536 gPciBusDriverBinding
.DriverBindingHandle
,
537 PciOptionRomImageDevicePath
,
544 // Free the device path after it has been used by LoadImage
546 gBS
->FreePool (PciOptionRomImageDevicePath
);
548 if (!EFI_ERROR (Status
)) {
549 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
550 if (!EFI_ERROR (Status
)) {
551 AddDriver (PciDevice
, ImageHandle
);
552 PciRomAddImageMapping (
554 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
555 PciDevice
->BusNumber
,
556 PciDevice
->DeviceNumber
,
557 PciDevice
->FunctionNumber
,
558 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
559 PciDevice
->PciIo
.RomSize
561 retStatus
= EFI_SUCCESS
;
566 RomBarOffset
= RomBarOffset
+ ImageSize
;
568 RomBarOffset
= RomBarOffset
+ ImageSize
;
571 RomBarOffset
= RomBarOffset
+ ImageSize
;
574 } while (((Indicator
& 0x80) == 0x00) && ((UINTN
) (RomBarOffset
- (UINT8
*) RomBar
) < PciDevice
->RomSize
));