]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Pci/PciBusDxe/PciDriverOverride.c
6c9d6f8079d170e340d0fac3e9cfff52b8f30de6
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Pci / PciBusDxe / PciDriverOverride.c
1 /** @file
2
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
8
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.
11
12 **/
13
14
15 #include "PciBus.h"
16
17 /**
18 Initializes a PCI Driver Override Instance.
19
20 @param PciIoDevice Device instance.
21
22 @retval EFI_SUCCESS Operation success.
23 **/
24 EFI_STATUS
25 InitializePciDriverOverrideInstance (
26 PCI_IO_DEVICE *PciIoDevice
27 )
28 {
29 PciIoDevice->PciDriverOverride.GetDriver = GetDriver;
30 return EFI_SUCCESS;
31 }
32
33 /**
34 Get a overriding driver image.
35 @param This Pointer to instance of EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL.
36 @param DriverImageHandle Override driver image.
37
38 @retval EFI_SUCCESS Success to get driver image handle.
39 @retval EFI_NOT_FOUND can not find override driver image.
40 @retval EFI_INVALID_PARAMETER Invalid parameter.
41 **/
42 EFI_STATUS
43 EFIAPI
44 GetDriver (
45 IN EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL *This,
46 IN OUT EFI_HANDLE *DriverImageHandle
47 )
48 {
49 PCI_IO_DEVICE *PciIoDevice;
50 LIST_ENTRY *CurrentLink;
51 PCI_DRIVER_OVERRIDE_LIST *Node;
52
53 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_DRIVER_OVERRIDE_THIS (This);
54
55 CurrentLink = PciIoDevice->OptionRomDriverList.ForwardLink;
56
57 while (CurrentLink != NULL && CurrentLink != &PciIoDevice->OptionRomDriverList) {
58
59 Node = DRIVER_OVERRIDE_FROM_LINK (CurrentLink);
60
61 if (*DriverImageHandle == NULL) {
62
63 *DriverImageHandle = Node->DriverImageHandle;
64 return EFI_SUCCESS;
65 }
66
67 if (*DriverImageHandle == Node->DriverImageHandle) {
68
69 if (CurrentLink->ForwardLink == &PciIoDevice->OptionRomDriverList ||
70 CurrentLink->ForwardLink == NULL) {
71 return EFI_NOT_FOUND;
72 }
73
74 //
75 // Get next node
76 //
77 Node = DRIVER_OVERRIDE_FROM_LINK (CurrentLink->ForwardLink);
78 *DriverImageHandle = Node->DriverImageHandle;
79 return EFI_SUCCESS;
80 }
81
82 CurrentLink = CurrentLink->ForwardLink;
83 }
84
85 return EFI_INVALID_PARAMETER;
86 }
87
88 /**
89 Add an overriding driver image
90
91 @param PciIoDevice Instance of PciIo device.
92 @param DriverImageHandle new added driver image.
93
94 @retval EFI_OUT_OF_RESOURCES no memory resource for new driver instance.
95 @retval EFI_SUCCESS Success add driver.
96 **/
97 EFI_STATUS
98 AddDriver (
99 IN PCI_IO_DEVICE *PciIoDevice,
100 IN EFI_HANDLE DriverImageHandle
101 )
102 {
103 EFI_STATUS Status;
104 EFI_IMAGE_DOS_HEADER *DosHdr;
105 EFI_IMAGE_NT_HEADERS *PeHdr;
106 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
107 PCI_DRIVER_OVERRIDE_LIST *Node;
108
109 Status = gBS->HandleProtocol (DriverImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &LoadedImage);
110 if (EFI_ERROR (Status)) {
111 return Status;
112 }
113
114 Node = AllocatePool (sizeof (PCI_DRIVER_OVERRIDE_LIST));
115 if (Node == NULL) {
116 return EFI_OUT_OF_RESOURCES;
117 }
118
119 Node->Signature = DRIVER_OVERRIDE_SIGNATURE;
120 Node->DriverImageHandle = DriverImageHandle;
121
122 InsertTailList (&PciIoDevice->OptionRomDriverList, &(Node->Link));
123
124 PciIoDevice->BusOverride = TRUE;
125
126 DosHdr = (EFI_IMAGE_DOS_HEADER *) LoadedImage->ImageBase;
127 if (DosHdr->e_magic != EFI_IMAGE_DOS_SIGNATURE) {
128 return EFI_SUCCESS;
129 }
130
131 PeHdr = (EFI_IMAGE_NT_HEADERS *) ((UINTN) LoadedImage->ImageBase + DosHdr->e_lfanew);
132
133 if (PeHdr->FileHeader.Machine != EFI_IMAGE_MACHINE_EBC) {
134 return EFI_SUCCESS;
135 }
136 return EFI_SUCCESS;
137 }
138