]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Bus/Pci/PciBus/Dxe/PciDriverOverride.c
70ad48b5ecce272b1a5a9c3af6940119533146e6
[mirror_edk2.git] / EdkModulePkg / Bus / Pci / PciBus / Dxe / PciDriverOverride.c
1 /*++
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 Module Name:
13
14 PciDriverOverride.c
15
16 Abstract:
17
18 PCI Bus Driver
19
20 Revision History
21
22 --*/
23
24 #include "pcibus.h"
25
26 EFI_STATUS
27 InitializePciDriverOverrideInstance (
28 PCI_IO_DEVICE *PciIoDevice
29 )
30 /*++
31
32 Routine Description:
33
34 Initializes a PCI Driver Override Instance
35
36 Arguments:
37
38 Returns:
39
40 None
41
42 --*/
43 // TODO: PciIoDevice - add argument and description to function comment
44 // TODO: EFI_SUCCESS - add return value to function comment
45 {
46 PciIoDevice->PciDriverOverride.GetDriver = GetDriver;
47 return EFI_SUCCESS;
48 }
49
50 EFI_STATUS
51 EFIAPI
52 GetDriver (
53 IN EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL *This,
54 IN OUT EFI_HANDLE *DriverImageHandle
55 )
56 /*++
57
58 Routine Description:
59
60 Get a overriding driver image
61
62 Arguments:
63
64 Returns:
65
66 None
67
68 --*/
69 // TODO: This - add argument and description to function comment
70 // TODO: DriverImageHandle - add argument and description to function comment
71 // TODO: EFI_SUCCESS - add return value to function comment
72 // TODO: EFI_NOT_FOUND - add return value to function comment
73 // TODO: EFI_SUCCESS - add return value to function comment
74 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
75 {
76 PCI_IO_DEVICE *PciIoDevice;
77 LIST_ENTRY *CurrentLink;
78 PCI_DRIVER_OVERRIDE_LIST *Node;
79
80 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_DRIVER_OVERRIDE_THIS (This);
81
82 CurrentLink = PciIoDevice->OptionRomDriverList.ForwardLink;
83
84 while (CurrentLink && CurrentLink != &PciIoDevice->OptionRomDriverList) {
85
86 Node = DRIVER_OVERRIDE_FROM_LINK (CurrentLink);
87
88 if (*DriverImageHandle == NULL) {
89
90 *DriverImageHandle = Node->DriverImageHandle;
91 return EFI_SUCCESS;
92 }
93
94 if (*DriverImageHandle == Node->DriverImageHandle) {
95
96 if (CurrentLink->ForwardLink == &PciIoDevice->OptionRomDriverList ||
97 CurrentLink->ForwardLink == NULL) {
98 return EFI_NOT_FOUND;
99 }
100
101 //
102 // Get next node
103 //
104 Node = DRIVER_OVERRIDE_FROM_LINK (CurrentLink->ForwardLink);
105 *DriverImageHandle = Node->DriverImageHandle;
106 return EFI_SUCCESS;
107 }
108
109 CurrentLink = CurrentLink->ForwardLink;
110 }
111
112 return EFI_INVALID_PARAMETER;
113 }
114
115 EFI_STATUS
116 AddDriver (
117 IN PCI_IO_DEVICE *PciIoDevice,
118 IN EFI_HANDLE DriverImageHandle
119 )
120 /*++
121
122 Routine Description:
123
124 Add a overriding driver image
125
126 Arguments:
127
128 Returns:
129
130 None
131
132 --*/
133 // TODO: PciIoDevice - add argument and description to function comment
134 // TODO: DriverImageHandle - add argument and description to function comment
135 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
136 // TODO: EFI_SUCCESS - add return value to function comment
137 // TODO: EFI_SUCCESS - add return value to function comment
138 // TODO: EFI_SUCCESS - add return value to function comment
139 // TODO: EFI_SUCCESS - add return value to function comment
140 {
141 EFI_STATUS Status;
142 EFI_IMAGE_DOS_HEADER *DosHdr;
143 EFI_IMAGE_NT_HEADERS *PeHdr;
144 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
145 PCI_DRIVER_OVERRIDE_LIST *Node;
146 #if (EFI_SPECIFICATION_VERSION < 0x00020000)
147 EFI_DRIVER_OS_HANDOFF_HEADER *DriverOsHandoffHeader;
148 EFI_DRIVER_OS_HANDOFF_HEADER *NewDriverOsHandoffHeader;
149 EFI_DRIVER_OS_HANDOFF *DriverOsHandoff;
150 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
151 EFI_HANDLE DeviceHandle;
152 UINTN NumberOfEntries;
153 UINTN Size;
154 UINTN Index;
155 #endif
156
157 Status = gBS->HandleProtocol (DriverImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &LoadedImage);
158 if (EFI_ERROR (Status)) {
159 return Status;
160 }
161
162 Node = AllocatePool (sizeof (PCI_DRIVER_OVERRIDE_LIST));
163 if (Node == NULL) {
164 return EFI_OUT_OF_RESOURCES;
165 }
166
167 Node->Signature = DRIVER_OVERRIDE_SIGNATURE;
168 Node->DriverImageHandle = DriverImageHandle;
169
170 InsertTailList (&PciIoDevice->OptionRomDriverList, &(Node->Link));
171
172 PciIoDevice->BusOverride = TRUE;
173
174 DosHdr = (EFI_IMAGE_DOS_HEADER *) LoadedImage->ImageBase;
175 if (DosHdr->e_magic != EFI_IMAGE_DOS_SIGNATURE) {
176 return EFI_SUCCESS;
177 }
178
179 PeHdr = (EFI_IMAGE_NT_HEADERS *) ((UINTN) LoadedImage->ImageBase + DosHdr->e_lfanew);
180
181 if (PeHdr->FileHeader.Machine != EFI_IMAGE_MACHINE_EBC) {
182 return EFI_SUCCESS;
183 }
184
185 #if (EFI_SPECIFICATION_VERSION < 0x00020000)
186 DriverOsHandoffHeader = NULL;
187 Status = EfiGetSystemConfigurationTable (&gEfiUgaIoProtocolGuid, (VOID **) &DriverOsHandoffHeader);
188 if (!EFI_ERROR (Status) && DriverOsHandoffHeader != NULL) {
189 for (Index = 0; Index < DriverOsHandoffHeader->NumberOfEntries; Index++) {
190 DriverOsHandoff = (EFI_DRIVER_OS_HANDOFF *)((UINTN)(DriverOsHandoffHeader) +
191 DriverOsHandoffHeader->HeaderSize +
192 Index * DriverOsHandoffHeader->SizeOfEntries);
193 DevicePath = DriverOsHandoff->DevicePath;
194 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &DevicePath, &DeviceHandle);
195 if (!EFI_ERROR (Status) && DeviceHandle != NULL && IsDevicePathEnd (DevicePath)) {
196 if (DeviceHandle == PciIoDevice->Handle) {
197 return EFI_SUCCESS;
198 }
199 }
200 }
201
202 NumberOfEntries = DriverOsHandoffHeader->NumberOfEntries + 1;
203 } else {
204 NumberOfEntries = 1;
205 }
206
207 Status = gBS->AllocatePool (
208 EfiRuntimeServicesData,
209 sizeof (EFI_DRIVER_OS_HANDOFF_HEADER) + NumberOfEntries * sizeof (EFI_DRIVER_OS_HANDOFF),
210 (VOID **) &NewDriverOsHandoffHeader
211 );
212 if (EFI_ERROR (Status)) {
213 return Status;
214 }
215
216 if (DriverOsHandoffHeader == NULL) {
217 NewDriverOsHandoffHeader->Version = 0;
218 NewDriverOsHandoffHeader->HeaderSize = sizeof (EFI_DRIVER_OS_HANDOFF_HEADER);
219 NewDriverOsHandoffHeader->SizeOfEntries = sizeof (EFI_DRIVER_OS_HANDOFF);
220 NewDriverOsHandoffHeader->NumberOfEntries = (UINT32) NumberOfEntries;
221 } else {
222 gBS->CopyMem (
223 NewDriverOsHandoffHeader,
224 DriverOsHandoffHeader,
225 DriverOsHandoffHeader->HeaderSize + (NumberOfEntries - 1) * DriverOsHandoffHeader->SizeOfEntries
226 );
227 NewDriverOsHandoffHeader->NumberOfEntries = (UINT32) NumberOfEntries;
228 }
229
230 DriverOsHandoff = (EFI_DRIVER_OS_HANDOFF *)((UINTN)NewDriverOsHandoffHeader +
231 NewDriverOsHandoffHeader->HeaderSize +
232 (NumberOfEntries - 1) * NewDriverOsHandoffHeader->SizeOfEntries);
233
234 //
235 // Fill in the EFI_DRIVER_OS_HANDOFF structure
236 //
237 DriverOsHandoff->Type = EfiUgaDriverFromPciRom;
238
239 //
240 // Compute the size of the device path
241 //
242 Size = GetDevicePathSize (PciIoDevice->DevicePath);
243 if (Size == 0) {
244 DriverOsHandoff->DevicePath = NULL;
245 } else {
246
247 //
248 // Allocate space for duplicate device path
249 //
250 Status = gBS->AllocatePool (
251 EfiRuntimeServicesData,
252 Size,
253 (VOID **) &DriverOsHandoff->DevicePath
254 );
255 if (EFI_ERROR (Status)) {
256 gBS->FreePool (NewDriverOsHandoffHeader);
257 return Status;
258 }
259
260 //
261 // Make copy of device path
262 //
263 CopyMem (DriverOsHandoff->DevicePath, PciIoDevice->DevicePath, Size);
264 }
265
266 DriverOsHandoff->PciRomSize = (UINT64) PciIoDevice->PciIo.RomSize;
267 Status = gBS->AllocatePool (
268 EfiRuntimeServicesData,
269 (UINTN) DriverOsHandoff->PciRomSize,
270 (VOID **) &DriverOsHandoff->PciRomImage
271 );
272 if (EFI_ERROR (Status)) {
273 gBS->FreePool (NewDriverOsHandoffHeader);
274 return Status;
275 }
276
277 gBS->CopyMem (
278 DriverOsHandoff->PciRomImage,
279 PciIoDevice->PciIo.RomImage,
280 (UINTN) DriverOsHandoff->PciRomSize
281 );
282
283 Status = gBS->InstallConfigurationTable (&gEfiUgaIoProtocolGuid, NewDriverOsHandoffHeader);
284 if (EFI_ERROR (Status)) {
285 return Status;
286 }
287
288 if (DriverOsHandoffHeader != NULL) {
289 gBS->FreePool (DriverOsHandoffHeader);
290 }
291 #endif
292
293 return EFI_SUCCESS;
294 }