3 Copyright (c) 2006 - 2007, 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
= PciRootBridgeIoWrite (
99 if (EFI_ERROR (Status
)) {
106 Status
= PciRootBridgeIoRead (
114 if (EFI_ERROR (Status
)) {
118 // Bits [1, 10] are reserved
120 AllOnes
&= 0xFFFFF800;
121 if ((AllOnes
== 0) || (AllOnes
== 0xFFFFF800)) {
122 return EFI_NOT_FOUND
;
125 PciIoDevice
->RomSize
= (UINT64
) ((~AllOnes
) + 1);
131 IN PCI_IO_DEVICE
*PciDevice
,
138 Load option rom image for specified PCI device
145 // TODO: PciDevice - add argument and description to function comment
146 // TODO: RomBase - add argument and description 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
149 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
156 EFI_STATUS retStatus
;
159 PCI_EXPANSION_ROM_HEADER
*RomHeader
;
160 PCI_DATA_STRUCTURE
*RomPcir
;
166 RomSize
= PciDevice
->RomSize
;
174 // Get the RomBarIndex
180 RomBarIndex
= PCI_DEVICE_ROMBAR
;
181 if (IS_PCI_BRIDGE (&(PciDevice
->Pci
))) {
189 RomBarIndex
= PCI_BRIDGE_ROMBAR
;
192 // Allocate memory for Rom header and PCIR
194 RomHeader
= AllocatePool (sizeof (PCI_EXPANSION_ROM_HEADER
));
195 if (RomHeader
== NULL
) {
196 return EFI_OUT_OF_RESOURCES
;
199 RomPcir
= AllocatePool (sizeof (PCI_DATA_STRUCTURE
));
200 if (RomPcir
== NULL
) {
201 gBS
->FreePool (RomHeader
);
202 return EFI_OUT_OF_RESOURCES
;
205 RomBar
= (UINT32
) RomBase
;
210 RomDecode (PciDevice
, RomBarIndex
, RomBar
, TRUE
);
212 RomBarOffset
= RomBar
;
213 retStatus
= EFI_NOT_FOUND
;
217 PciDevice
->PciRootBridgeIo
->Mem
.Read (
218 PciDevice
->PciRootBridgeIo
,
221 sizeof (PCI_EXPANSION_ROM_HEADER
),
225 if (RomHeader
->Signature
!= PCI_EXPANSION_ROM_HEADER_SIGNATURE
) {
226 RomBarOffset
= RomBarOffset
+ 512;
230 RomImageSize
= RomImageSize
+ 512;
236 OffsetPcir
= RomHeader
->PcirOffset
;
237 PciDevice
->PciRootBridgeIo
->Mem
.Read (
238 PciDevice
->PciRootBridgeIo
,
240 RomBarOffset
+ OffsetPcir
,
241 sizeof (PCI_DATA_STRUCTURE
),
244 if (RomPcir
->CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
245 CodeType
= PCI_CODE_TYPE_PCAT_IMAGE
;
247 Indicator
= RomPcir
->Indicator
;
248 RomImageSize
= RomImageSize
+ RomPcir
->ImageLength
* 512;
249 RomBarOffset
= RomBarOffset
+ RomPcir
->ImageLength
* 512;
250 } while (((Indicator
& 0x80) == 0x00) && ((RomBarOffset
- RomBar
) < RomSize
));
253 // Some Legacy Cards do not report the correct ImageLength so used the maximum
254 // of the legacy length and the PCIR Image Length
256 if (CodeType
== PCI_CODE_TYPE_PCAT_IMAGE
) {
257 RomImageSize
= EFI_MAX(RomImageSize
, (((EFI_LEGACY_EXPANSION_ROM_HEADER
*)RomHeader
)->Size512
* 512));
260 if (RomImageSize
> 0) {
261 retStatus
= EFI_SUCCESS
;
262 Image
= AllocatePool ((UINT32
) RomImageSize
);
264 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
265 gBS
->FreePool (RomHeader
);
266 gBS
->FreePool (RomPcir
);
267 return EFI_OUT_OF_RESOURCES
;
271 // Copy Rom image into memory
273 PciDevice
->PciRootBridgeIo
->Mem
.Read (
274 PciDevice
->PciRootBridgeIo
,
277 (UINT32
) RomImageSize
,
283 RomDecode (PciDevice
, RomBarIndex
, RomBar
, FALSE
);
285 PciDevice
->PciIo
.RomSize
= RomImageSize
;
286 PciDevice
->PciIo
.RomImage
= RomInMemory
;
288 PciRomAddImageMapping (
290 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
291 PciDevice
->BusNumber
,
292 PciDevice
->DeviceNumber
,
293 PciDevice
->FunctionNumber
,
294 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
295 PciDevice
->PciIo
.RomSize
299 // Free allocated memory
301 gBS
->FreePool (RomHeader
);
302 gBS
->FreePool (RomPcir
);
309 IN PCI_IO_DEVICE
*PciDevice
,
310 IN UINT8 RomBarIndex
,
323 // TODO: PciDevice - add argument and description to function comment
324 // TODO: RomBarIndex - add argument and description to function comment
325 // TODO: RomBar - add argument and description to function comment
326 // TODO: Enable - add argument and description to function comment
327 // TODO: EFI_SUCCESS - add return value to function comment
331 EFI_PCI_IO_PROTOCOL
*PciIo
;
333 PciIo
= &PciDevice
->PciIo
;
338 for (Offset
= 0x10; Offset
<= 0x24; Offset
+= sizeof (UINT32
)) {
339 PciIoWrite (PciIo
, EfiPciIoWidthUint32
, Offset
, 1, &gAllZero
);
343 // set the Rom base address: now is hardcode
344 // enable its decoder
346 Value32
= RomBar
| 0x1;
349 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
356 // Programe all upstream bridge
358 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, TRUE
);
361 // Setting the memory space bit in the function's command register
363 PciEnableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
368 // disable command register decode to memory
370 PciDisableCommandRegister(PciDevice
, EFI_PCI_COMMAND_MEMORY_SPACE
);
373 // Destroy the programmed bar in all the upstream bridge.
375 ProgrameUpstreamBridgeForRom(PciDevice
, RomBar
, FALSE
);
378 // disable rom decode
380 Value32
= 0xFFFFFFFE;
383 (EFI_PCI_IO_PROTOCOL_WIDTH
) EfiPciWidthUint32
,
397 PCI_IO_DEVICE
*PciDevice
403 Process the oprom image.
406 PciDevice A pointer to a pci device.
419 EFI_HANDLE ImageHandle
;
421 EFI_STATUS retStatus
;
424 UINT32 DestinationSize
;
428 VOID
*DecompressedImageBuffer
;
430 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
431 EFI_PCI_EXPANSION_ROM_HEADER
*EfiRomHeader
;
432 PCI_DATA_STRUCTURE
*Pcir
;
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 // load image and start image
526 Status
= gBS
->LoadImage (
528 gPciBusDriverBinding
.DriverBindingHandle
,
534 if (!EFI_ERROR (Status
)) {
535 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
536 if (!EFI_ERROR (Status
)) {
537 AddDriver (PciDevice
, ImageHandle
);
538 PciRomAddImageMapping (
540 PciDevice
->PciRootBridgeIo
->SegmentNumber
,
541 PciDevice
->BusNumber
,
542 PciDevice
->DeviceNumber
,
543 PciDevice
->FunctionNumber
,
544 (UINT64
) (UINTN
) PciDevice
->PciIo
.RomImage
,
545 PciDevice
->PciIo
.RomSize
547 retStatus
= EFI_SUCCESS
;
552 RomBarOffset
= RomBarOffset
+ ImageSize
;
554 RomBarOffset
= RomBarOffset
+ ImageSize
;
557 RomBarOffset
= RomBarOffset
+ ImageSize
;
560 } while (((Indicator
& 0x80) == 0x00) && ((UINTN
) (RomBarOffset
- (UINT8
*) RomBar
) < PciDevice
->RomSize
));