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 48 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 AllOnes
&= 0xFFFFFFFC;
117 if ((AllOnes
== 0) || (AllOnes
== 0xFFFFFFFC)) {
118 return EFI_NOT_FOUND
;
121 PciIoDevice
->RomSize
= (UINT64
) ((~AllOnes
) + 1);
128 IN PCI_IO_DEVICE
*PciDevice
,
135 Load option rom image for specified PCI device
142 // TODO: PciDevice - add argument and description to function comment
143 // TODO: RomBase - add argument and description to function comment
144 // TODO: EFI_OUT_OF_RESOURCES - add return value 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
154 EFI_STATUS retStatus
;
157 PCI_EXPANSION_ROM_HEADER
*RomHeader
;
158 PCI_DATA_STRUCTURE
*RomPcir
;
164 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
= EFI_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 PciIo
->Pci
.Write (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
;
436 // Get the Address of the Rom image
438 RomBar
= PciDevice
->PciIo
.RomImage
;
439 RomBarOffset
= (UINT8
*) RomBar
;
440 retStatus
= EFI_NOT_FOUND
;
444 EfiRomHeader
= (EFI_PCI_EXPANSION_ROM_HEADER
*) RomBarOffset
;
445 if (EfiRomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
446 RomBarOffset
= RomBarOffset
+ 512;
455 Pcir
= (PCI_DATA_STRUCTURE
*) (RomBarOffset
+ EfiRomHeader
->PcirOffset
);
456 ImageSize
= (UINT32
) (Pcir
->ImageLength
* 512);
457 Indicator
= Pcir
->Indicator
;
459 if ((Pcir
->CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) &&
460 (EfiRomHeader
->EfiSignature
== EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
)) {
462 if ((EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
) ||
463 (EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
)) {
465 ImageOffset
= EfiRomHeader
->EfiImageHeaderOffset
;
466 ImageSize
= (UINT32
) (EfiRomHeader
->InitializationSize
* 512);
468 ImageBuffer
= (VOID
*) (RomBarOffset
+ ImageOffset
);
469 ImageLength
= ImageSize
- (UINT32
)ImageOffset
;
470 DecompressedImageBuffer
= NULL
;
473 // decompress here if needed
476 if (EfiRomHeader
->CompressionType
> EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
480 if (EfiRomHeader
->CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
481 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**) &Decompress
);
482 if (EFI_ERROR (Status
)) {
486 Status
= Decompress
->GetInfo (
493 if (!EFI_ERROR (Status
)) {
494 DecompressedImageBuffer
= NULL
;
495 DecompressedImageBuffer
= AllocatePool (DestinationSize
);
496 if (DecompressedImageBuffer
!= NULL
) {
497 Scratch
= AllocatePool (ScratchSize
);
498 if (Scratch
!= NULL
) {
499 Status
= Decompress
->Decompress (
503 DecompressedImageBuffer
,
508 if (!EFI_ERROR (Status
)) {
509 ImageBuffer
= DecompressedImageBuffer
;
510 ImageLength
= DestinationSize
;
514 gBS
->FreePool (Scratch
);
523 // load image and start image
525 Status
= gBS
->LoadImage (
527 gPciBusDriverBinding
.DriverBindingHandle
,
533 if (!EFI_ERROR (Status
)) {
534 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
535 if (!EFI_ERROR (Status
)) {
536 AddDriver (PciDevice
, ImageHandle
);
537 PciRomAddImageMapping (
539 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
540 PciDevice
->BusNumber
,
541 PciDevice
->DeviceNumber
,
542 PciDevice
->FunctionNumber
,
543 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
544 PciDevice
->PciIo
.RomSize
546 retStatus
= EFI_SUCCESS
;
551 RomBarOffset
= RomBarOffset
+ ImageSize
;
553 RomBarOffset
= RomBarOffset
+ ImageSize
;
556 RomBarOffset
= RomBarOffset
+ ImageSize
;
559 } while (((Indicator
& 0x80) == 0x00) && ((UINTN
) (RomBarOffset
- (UINT8
*) RomBar
) < PciDevice
->RomSize
));