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.
25 #include "PciResourceSupport.h"
30 #define EFI_MIN(a, b) (((a) < (b)) ? (a) : (b))
31 #define EFI_MAX(a, b) (((a) > (b)) ? (a) : (b))
36 IN PCI_IO_DEVICE
*PciIoDevice
47 // TODO: PciIoDevice - add argument and description to function comment
48 // TODO: EFI_NOT_FOUND - add return value to function comment
49 // TODO: EFI_SUCCESS - add return value to function comment
58 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
60 Bus
= PciIoDevice
->BusNumber
;
61 Device
= PciIoDevice
->DeviceNumber
;
62 Function
= PciIoDevice
->FunctionNumber
;
64 PciRootBridgeIo
= PciIoDevice
->PciRootBridgeIo
;
67 // offset is 0x30 if is not ppb
73 RomBarIndex
= PCI_DEVICE_ROMBAR
;
75 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
)) {
83 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
86 // the bit0 is 0 to prevent the enabling of the Rom address decoder
89 Address
= EFI_PCI_ADDRESS (Bus
, Device
, Function
, RomBarIndex
);
91 Status
= PciRootBridgeIo
->Pci
.Write (
98 if (EFI_ERROR (Status
)) {
105 Status
= PciRootBridgeIo
->Pci
.Read (
112 if (EFI_ERROR (Status
)) {
116 // Bits [1, 10] are reserved
118 AllOnes
&= 0xFFFFF800;
119 if ((AllOnes
== 0) || (AllOnes
== 0xFFFFF800)) {
120 return EFI_NOT_FOUND
;
123 PciIoDevice
->RomSize
= (UINT64
) ((~AllOnes
) + 1);
129 IN PCI_IO_DEVICE
*PciDevice
,
136 Load option rom image for specified PCI device
143 // TODO: PciDevice - add argument and description to function comment
144 // TODO: RomBase - add argument and description to function comment
145 // TODO: EFI_OUT_OF_RESOURCES - add return value 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
154 EFI_STATUS retStatus
;
157 PCI_EXPANSION_ROM_HEADER
*RomHeader
;
158 PCI_DATA_STRUCTURE
*RomPcir
;
164 RomSize
= PciDevice
->RomSize
;
172 // Get the RomBarIndex
178 RomBarIndex
= PCI_DEVICE_ROMBAR
;
179 if (IS_PCI_BRIDGE (&(PciDevice
->Pci
))) {
187 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
190 // Allocate memory for Rom header and PCIR
192 RomHeader
= AllocatePool (sizeof (PCI_EXPANSION_ROM_HEADER
));
193 if (RomHeader
== NULL
) {
194 return EFI_OUT_OF_RESOURCES
;
197 RomPcir
= AllocatePool (sizeof (PCI_DATA_STRUCTURE
));
198 if (RomPcir
== NULL
) {
199 gBS
->FreePool (RomHeader
);
200 return EFI_OUT_OF_RESOURCES
;
203 RomBar
= (UINT32
) RomBase
;
208 RomDecode (PciDevice
, RomBarIndex
, RomBar
, TRUE
);
210 RomBarOffset
= RomBar
;
211 retStatus
= EFI_NOT_FOUND
;
215 PciDevice
->PciRootBridgeIo
->Mem
.Read (
216 PciDevice
->PciRootBridgeIo
,
219 sizeof (PCI_EXPANSION_ROM_HEADER
),
223 if (RomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
224 RomBarOffset
= RomBarOffset
+ 512;
228 RomImageSize
= RomImageSize
+ 512;
234 OffsetPcir
= RomHeader
->PcirOffset
;
235 PciDevice
->PciRootBridgeIo
->Mem
.Read (
236 PciDevice
->PciRootBridgeIo
,
238 RomBarOffset
+ OffsetPcir
,
239 sizeof (PCI_DATA_STRUCTURE
),
242 if (RomPcir
->CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
243 CodeType
= PCI_CODE_TYPE_PCAT_IMAGE
;
245 Indicator
= RomPcir
->Indicator
;
246 RomImageSize
= RomImageSize
+ RomPcir
->ImageLength
* 512;
247 RomBarOffset
= RomBarOffset
+ RomPcir
->ImageLength
* 512;
248 } while (((Indicator
& 0x80) == 0x00) && ((RomBarOffset
- RomBar
) < RomSize
));
251 // Some Legacy Cards do not report the correct ImageLength so used the maximum
252 // of the legacy length and the PCIR Image Length
254 if (CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
255 RomImageSize
= EFI_MAX(RomImageSize
, (((EFI_LEGACY_EXPANSION_ROM_HEADER
*)RomHeader
)->Size512
* 512));
258 if (RomImageSize
> 0) {
259 retStatus
= EFI_SUCCESS
;
260 Image
= AllocatePool ((UINT32
) RomImageSize
);
262 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
263 gBS
->FreePool (RomHeader
);
264 gBS
->FreePool (RomPcir
);
265 return EFI_OUT_OF_RESOURCES
;
269 // Copy Rom image into memory
271 PciDevice
->PciRootBridgeIo
->Mem
.Read (
272 PciDevice
->PciRootBridgeIo
,
275 (UINT32
) RomImageSize
,
281 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
283 PciDevice
->PciIo
.RomSize
= RomImageSize
;
284 PciDevice
->PciIo
.RomImage
= RomInMemory
;
286 PciRomAddImageMapping (
288 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
289 PciDevice
->BusNumber
,
290 PciDevice
->DeviceNumber
,
291 PciDevice
->FunctionNumber
,
292 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
293 PciDevice
->PciIo
.RomSize
297 // Free allocated memory
299 gBS
->FreePool (RomHeader
);
300 gBS
->FreePool (RomPcir
);
307 IN PCI_IO_DEVICE
*PciDevice
,
308 IN UINT8 RomBarIndex
,
321 // TODO: PciDevice - add argument and description to function comment
322 // TODO: RomBarIndex - add argument and description to function comment
323 // TODO: RomBar - add argument and description to function comment
324 // TODO: Enable - add argument and description to function comment
325 // TODO: EFI_SUCCESS - add return value to function comment
329 EFI_PCI_IO_PROTOCOL
*PciIo
;
331 PciIo
= &PciDevice
->PciIo
;
336 for (Offset
= 0x10; Offset
<= 0x24; Offset
+= sizeof (UINT32
)) {
337 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, Offset
, 1, &gAllZero
);
341 // set the Rom base address: now is hardcode
342 // enable its decoder
344 Value32
= RomBar
| 0x1;
347 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
354 // Programe all upstream bridge
356 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, TRUE
);
359 // Setting the memory space bit in the function's command register
361 PciEnableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
366 // disable command register decode to memory
368 PciDisableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
371 // Destroy the programmed bar in all the upstream bridge.
373 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, FALSE
);
376 // disable rom decode
378 Value32
= 0xFFFFFFFE;
381 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
395 PCI_IO_DEVICE
*PciDevice
401 Process the oprom image.
404 PciDevice A pointer to a pci device.
417 EFI_HANDLE ImageHandle
;
419 EFI_STATUS retStatus
;
422 UINT32 DestinationSize
;
426 VOID
*DecompressedImageBuffer
;
428 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
429 EFI_PCI_EXPANSION_ROM_HEADER
*EfiRomHeader
;
430 PCI_DATA_STRUCTURE
*Pcir
;
435 // Get the Address of the Rom image
437 RomBar
= PciDevice
->PciIo
.RomImage
;
438 RomBarOffset
= (UINT8
*) RomBar
;
439 retStatus
= EFI_NOT_FOUND
;
443 EfiRomHeader
= (EFI_PCI_EXPANSION_ROM_HEADER
*) RomBarOffset
;
444 if (EfiRomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
445 RomBarOffset
= RomBarOffset
+ 512;
454 Pcir
= (PCI_DATA_STRUCTURE
*) (RomBarOffset
+ EfiRomHeader
->PcirOffset
);
455 ImageSize
= (UINT32
) (Pcir
->ImageLength
* 512);
456 Indicator
= Pcir
->Indicator
;
458 if ((Pcir
->CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) &&
459 (EfiRomHeader
->EfiSignature
== EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
)) {
461 if ((EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
) ||
462 (EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
)) {
464 ImageOffset
= EfiRomHeader
->EfiImageHeaderOffset
;
465 ImageSize
= (UINT32
) (EfiRomHeader
->InitializationSize
* 512);
467 ImageBuffer
= (VOID
*) (RomBarOffset
+ ImageOffset
);
468 ImageLength
= ImageSize
- (UINT32
)ImageOffset
;
469 DecompressedImageBuffer
= NULL
;
472 // decompress here if needed
475 if (EfiRomHeader
->CompressionType
> EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
479 if (EfiRomHeader
->CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
480 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**) &Decompress
);
481 if (EFI_ERROR (Status
)) {
485 Status
= Decompress
->GetInfo (
492 if (!EFI_ERROR (Status
)) {
493 DecompressedImageBuffer
= NULL
;
494 DecompressedImageBuffer
= AllocatePool (DestinationSize
);
495 if (DecompressedImageBuffer
!= NULL
) {
496 Scratch
= AllocatePool (ScratchSize
);
497 if (Scratch
!= NULL
) {
498 Status
= Decompress
->Decompress (
502 DecompressedImageBuffer
,
507 if (!EFI_ERROR (Status
)) {
508 ImageBuffer
= DecompressedImageBuffer
;
509 ImageLength
= DestinationSize
;
513 gBS
->FreePool (Scratch
);
522 // load image and start image
524 Status
= gBS
->LoadImage (
526 gPciBusDriverBinding
.DriverBindingHandle
,
532 if (!EFI_ERROR (Status
)) {
533 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
534 if (!EFI_ERROR (Status
)) {
535 AddDriver (PciDevice
, ImageHandle
);
536 PciRomAddImageMapping (
538 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
539 PciDevice
->BusNumber
,
540 PciDevice
->DeviceNumber
,
541 PciDevice
->FunctionNumber
,
542 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
543 PciDevice
->PciIo
.RomSize
545 retStatus
= EFI_SUCCESS
;
550 RomBarOffset
= RomBarOffset
+ ImageSize
;
552 RomBarOffset
= RomBarOffset
+ ImageSize
;
555 RomBarOffset
= RomBarOffset
+ ImageSize
;
558 } while (((Indicator
& 0x80) == 0x00) && ((UINTN
) (RomBarOffset
- (UINT8
*) RomBar
) < PciDevice
->RomSize
));