1 /*++
2 
3 Copyright (c) 2004 - 2008, Intel Corporation. All rights reserved.<BR>
4 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   EfiDriverModelLib.c
15 
16 Abstract:
17 
18   Light weight lib to support EFI drivers.
19 
20 --*/
21 
22 #include "Tiano.h"
23 #include "EfiDriverLib.h"
24 
25 EFI_STATUS
EfiLibInstallDriverBinding(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable,IN EFI_DRIVER_BINDING_PROTOCOL * DriverBinding,IN EFI_HANDLE DriverBindingHandle)26 EfiLibInstallDriverBinding (
27   IN EFI_HANDLE                   ImageHandle,
28   IN EFI_SYSTEM_TABLE             *SystemTable,
29   IN EFI_DRIVER_BINDING_PROTOCOL  *DriverBinding,
30   IN EFI_HANDLE                   DriverBindingHandle
31   )
32 /*++
33 
34 Routine Description:
35 
36   Intialize a driver by installing the Driver Binding Protocol onto the
37   driver's DriverBindingHandle.  This is typically the same as the driver's
38   ImageHandle, but it can be different if the driver produces multiple
39   DriverBinding Protocols.  This function also initializes the EFI Driver
40   Library that initializes the global variables gST, gBS, gRT.
41 
42 Arguments:
43 
44   ImageHandle         - The image handle of the driver
45 
46   SystemTable         - The EFI System Table that was passed to the driver's entry point
47 
48   DriverBinding       - A Driver Binding Protocol instance that this driver is producing
49 
50   DriverBindingHandle - The handle that DriverBinding is to be installe onto.  If this
51                         parameter is NULL, then a new handle is created.
52 
53 Returns:
54 
55   EFI_SUCCESS is DriverBinding is installed onto DriverBindingHandle
56 
57   Otherwise, then return status from gBS->InstallProtocolInterface()
58 
59 --*/
60 {
61   EfiInitializeDriverLib (ImageHandle, SystemTable);
62 
63   DriverBinding->ImageHandle          = ImageHandle;
64 
65   DriverBinding->DriverBindingHandle  = DriverBindingHandle;
66 
67   return gBS->InstallProtocolInterface (
68                 &DriverBinding->DriverBindingHandle,
69                 &gEfiDriverBindingProtocolGuid,
70                 EFI_NATIVE_INTERFACE,
71                 DriverBinding
72                 );
73 }
74 
75 EFI_STATUS
InstallAllDriverProtocolsWorker(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable,IN EFI_DRIVER_BINDING_PROTOCOL * DriverBinding,IN EFI_HANDLE DriverBindingHandle,IN EFI_COMPONENT_NAME_PROTOCOL * ComponentName,OPTIONAL IN EFI_COMPONENT_NAME2_PROTOCOL * ComponentName2,OPTIONAL IN EFI_DRIVER_CONFIGURATION_PROTOCOL * DriverConfiguration,OPTIONAL IN EFI_DRIVER_CONFIGURATION2_PROTOCOL * DriverConfiguration2,OPTIONAL IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL * DriverDiagnostics,OPTIONAL IN EFI_DRIVER_DIAGNOSTICS2_PROTOCOL * DriverDiagnostics2 OPTIONAL)76 InstallAllDriverProtocolsWorker (
77   IN EFI_HANDLE                         ImageHandle,
78   IN EFI_SYSTEM_TABLE                   * SystemTable,
79   IN EFI_DRIVER_BINDING_PROTOCOL        * DriverBinding,
80   IN EFI_HANDLE                         DriverBindingHandle,
81   IN EFI_COMPONENT_NAME_PROTOCOL        * ComponentName, OPTIONAL
82   IN EFI_COMPONENT_NAME2_PROTOCOL       * ComponentName2, OPTIONAL
83   IN EFI_DRIVER_CONFIGURATION_PROTOCOL  * DriverConfiguration, OPTIONAL
84   IN EFI_DRIVER_CONFIGURATION2_PROTOCOL * DriverConfiguration2, OPTIONAL
85   IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL    * DriverDiagnostics, OPTIONAL
86   IN EFI_DRIVER_DIAGNOSTICS2_PROTOCOL   * DriverDiagnostics2 OPTIONAL
87   )
88 /*++
89 
90 Routine Description:
91 
92   Intialize a driver by installing the Driver Binding Protocol onto the
93   driver's DriverBindingHandle.  This is typically the same as the driver's
94   ImageHandle, but it can be different if the driver produces multiple
95   DriverBinding Protocols.  This function also initializes the EFI Driver
96   Library that initializes the global variables gST, gBS, gRT.
97 
98 Arguments:
99 
100   ImageHandle         - The image handle of the driver
101 
102   SystemTable         - The EFI System Table that was passed to the driver's entry point
103 
104   DriverBinding       - A Driver Binding Protocol instance that this driver is producing
105 
106   DriverBindingHandle - The handle that DriverBinding is to be installe onto.  If this
107                         parameter is NULL, then a new handle is created.
108 
109   ComponentName       - A Component Name Protocol instance that this driver is producing
110 
111   ComponentName2      - A Component Name2 Protocol instance that this driver is producing
112 
113   DriverConfiguration - A Driver Configuration Protocol instance that this driver is producing
114 
115   DriverConfiguration2- A Driver Configuration2 Protocol instance that this driver is producing
116 
117   DriverDiagnostics   - A Driver Diagnostics Protocol instance that this driver is producing
118 
119   DriverDiagnostics2  - A Driver Diagnostics2 Protocol instance that this driver is producing
120 
121 Returns:
122 
123   EFI_SUCCESS if all the protocols were installed onto DriverBindingHandle
124 
125   Otherwise, then return status from gBS->InstallProtocolInterface()
126 
127 --*/
128 {
129   EFI_STATUS  Status;
130 
131   Status = EfiLibInstallDriverBinding (ImageHandle, SystemTable, DriverBinding, DriverBindingHandle);
132   if (EFI_ERROR (Status)) {
133     return Status;
134   }
135 
136   if (ComponentName != NULL) {
137     Status = gBS->InstallProtocolInterface (
138                     &DriverBinding->DriverBindingHandle,
139                     &gEfiComponentNameProtocolGuid,
140                     EFI_NATIVE_INTERFACE,
141                     ComponentName
142                     );
143     if (EFI_ERROR (Status)) {
144       return Status;
145     }
146   }
147 
148   if (ComponentName2 != NULL) {
149     Status = gBS->InstallProtocolInterface (
150                     &DriverBinding->DriverBindingHandle,
151                     &gEfiComponentName2ProtocolGuid,
152                     EFI_NATIVE_INTERFACE,
153                     ComponentName2
154                     );
155     if (EFI_ERROR (Status)) {
156       return Status;
157     }
158   }
159 
160   if (DriverConfiguration != NULL) {
161     Status = gBS->InstallProtocolInterface (
162                     &DriverBinding->DriverBindingHandle,
163                     &gEfiDriverConfigurationProtocolGuid,
164                     EFI_NATIVE_INTERFACE,
165                     DriverConfiguration
166                     );
167     if (EFI_ERROR (Status)) {
168       return Status;
169     }
170   }
171 
172   if (DriverConfiguration2 != NULL) {
173     Status = gBS->InstallProtocolInterface (
174                     &DriverBinding->DriverBindingHandle,
175                     &gEfiDriverConfiguration2ProtocolGuid,
176                     EFI_NATIVE_INTERFACE,
177                     DriverConfiguration2
178                     );
179     if (EFI_ERROR (Status)) {
180       return Status;
181     }
182   }
183 
184   if (DriverDiagnostics != NULL) {
185     Status = gBS->InstallProtocolInterface (
186                     &DriverBinding->DriverBindingHandle,
187                     &gEfiDriverDiagnosticsProtocolGuid,
188                     EFI_NATIVE_INTERFACE,
189                     DriverDiagnostics
190                     );
191     if (EFI_ERROR (Status)) {
192       return Status;
193     }
194   }
195 
196   if (DriverDiagnostics2 != NULL) {
197     Status = gBS->InstallProtocolInterface (
198                     &DriverBinding->DriverBindingHandle,
199                     &gEfiDriverDiagnostics2ProtocolGuid,
200                     EFI_NATIVE_INTERFACE,
201                     DriverDiagnostics2
202                     );
203     if (EFI_ERROR (Status)) {
204       return Status;
205     }
206   }
207 
208   return EFI_SUCCESS;
209 }
210 
211 EFI_STATUS
EfiLibInstallAllDriverProtocols(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable,IN EFI_DRIVER_BINDING_PROTOCOL * DriverBinding,IN EFI_HANDLE DriverBindingHandle,IN EFI_COMPONENT_NAME_PROTOCOL * ComponentName,OPTIONAL IN EFI_DRIVER_CONFIGURATION_PROTOCOL * DriverConfiguration,OPTIONAL IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL * DriverDiagnostics OPTIONAL)212 EfiLibInstallAllDriverProtocols (
213   IN EFI_HANDLE                         ImageHandle,
214   IN EFI_SYSTEM_TABLE                   * SystemTable,
215   IN EFI_DRIVER_BINDING_PROTOCOL        * DriverBinding,
216   IN EFI_HANDLE                         DriverBindingHandle,
217   IN EFI_COMPONENT_NAME_PROTOCOL        * ComponentName, OPTIONAL
218   IN EFI_DRIVER_CONFIGURATION_PROTOCOL  * DriverConfiguration, OPTIONAL
219   IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL    * DriverDiagnostics OPTIONAL
220   )
221 /*++
222 
223 Routine Description:
224 
225   Intialize a driver by installing the Driver Binding Protocol onto the
226   driver's DriverBindingHandle.  This is typically the same as the driver's
227   ImageHandle, but it can be different if the driver produces multiple
228   DriverBinding Protocols.  This function also initializes the EFI Driver
229   Library that initializes the global variables gST, gBS, gRT.
230 
231 Arguments:
232 
233   ImageHandle         - The image handle of the driver
234 
235   SystemTable         - The EFI System Table that was passed to the driver's entry point
236 
237   DriverBinding       - A Driver Binding Protocol instance that this driver is producing
238 
239   DriverBindingHandle - The handle that DriverBinding is to be installe onto.  If this
240                         parameter is NULL, then a new handle is created.
241 
242   ComponentName       - A Component Name Protocol instance that this driver is producing
243 
244   DriverConfiguration - A Driver Configuration Protocol instance that this driver is producing
245 
246   DriverDiagnostics   - A Driver Diagnostics Protocol instance that this driver is producing
247 
248 Returns:
249 
250   EFI_SUCCESS if all the protocols were installed onto DriverBindingHandle
251 
252   Otherwise, then return status from gBS->InstallProtocolInterface()
253 
254 --*/
255 {
256   return InstallAllDriverProtocolsWorker (
257            ImageHandle,
258            SystemTable,
259            DriverBinding,
260            DriverBindingHandle,
261            ComponentName,
262            NULL,
263            DriverConfiguration,
264            NULL,
265            DriverDiagnostics,
266            NULL
267            );
268 }
269 
270 EFI_STATUS
EfiLibInstallAllDriverProtocols2(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable,IN EFI_DRIVER_BINDING_PROTOCOL * DriverBinding,IN EFI_HANDLE DriverBindingHandle,IN EFI_COMPONENT_NAME2_PROTOCOL * ComponentName2,OPTIONAL IN EFI_DRIVER_CONFIGURATION2_PROTOCOL * DriverConfiguration2,OPTIONAL IN EFI_DRIVER_DIAGNOSTICS2_PROTOCOL * DriverDiagnostics2 OPTIONAL)271 EfiLibInstallAllDriverProtocols2 (
272   IN EFI_HANDLE                         ImageHandle,
273   IN EFI_SYSTEM_TABLE                   * SystemTable,
274   IN EFI_DRIVER_BINDING_PROTOCOL        * DriverBinding,
275   IN EFI_HANDLE                         DriverBindingHandle,
276   IN EFI_COMPONENT_NAME2_PROTOCOL       * ComponentName2, OPTIONAL
277   IN EFI_DRIVER_CONFIGURATION2_PROTOCOL * DriverConfiguration2, OPTIONAL
278   IN EFI_DRIVER_DIAGNOSTICS2_PROTOCOL   * DriverDiagnostics2 OPTIONAL
279   )
280 /*++
281 
282 Routine Description:
283 
284   Intialize a driver by installing the Driver Binding Protocol onto the
285   driver's DriverBindingHandle.  This is typically the same as the driver's
286   ImageHandle, but it can be different if the driver produces multiple
287   DriverBinding Protocols.  This function also initializes the EFI Driver
288   Library that initializes the global variables gST, gBS, gRT.
289 
290 Arguments:
291 
292   ImageHandle         - The image handle of the driver
293 
294   SystemTable         - The EFI System Table that was passed to the driver's entry point
295 
296   DriverBinding       - A Driver Binding Protocol instance that this driver is producing
297 
298   DriverBindingHandle - The handle that DriverBinding is to be installe onto.  If this
299                         parameter is NULL, then a new handle is created.
300 
301   ComponentName2      - A Component Name2 Protocol instance that this driver is producing
302 
303   DriverConfiguration2- A Driver Configuration2 Protocol instance that this driver is producing
304 
305   DriverDiagnostics2  - A Driver Diagnostics2 Protocol instance that this driver is producing
306 
307 Returns:
308 
309   EFI_SUCCESS if all the protocols were installed onto DriverBindingHandle
310 
311   Otherwise, then return status from gBS->InstallProtocolInterface()
312 
313 --*/
314 {
315   return InstallAllDriverProtocolsWorker (
316            ImageHandle,
317            SystemTable,
318            DriverBinding,
319            DriverBindingHandle,
320            NULL,
321            ComponentName2,
322            NULL,
323            DriverConfiguration2,
324            NULL,
325            DriverDiagnostics2
326            );
327 }
328 
329 EFI_STATUS
EfiLibTestManagedDevice(IN EFI_HANDLE ControllerHandle,IN EFI_HANDLE DriverBindingHandle,IN EFI_GUID * ManagedProtocolGuid)330 EfiLibTestManagedDevice (
331   IN EFI_HANDLE       ControllerHandle,
332   IN EFI_HANDLE       DriverBindingHandle,
333   IN EFI_GUID         *ManagedProtocolGuid
334   )
335 /*++
336 
337 Routine Description:
338 
339   Test to see if the controller is managed by a specific driver.
340 
341 Arguments:
342 
343   ControllerHandle          - Handle for controller to test
344 
345   DriverBindingHandle       - Driver binding handle for controller
346 
347   ManagedProtocolGuid       - The protocol guid the driver opens on controller
348 
349 Returns:
350 
351   EFI_SUCCESS     - The controller is managed by the driver
352 
353   EFI_UNSUPPORTED - The controller is not managed by the driver
354 
355 --*/
356 {
357   EFI_STATUS     Status;
358   VOID           *ManagedInterface;
359 
360   Status = gBS->OpenProtocol (
361                   ControllerHandle,
362                   ManagedProtocolGuid,
363                   &ManagedInterface,
364                   DriverBindingHandle,
365                   ControllerHandle,
366                   EFI_OPEN_PROTOCOL_BY_DRIVER
367                   );
368   if (!EFI_ERROR (Status)) {
369     gBS->CloseProtocol (
370            ControllerHandle,
371            ManagedProtocolGuid,
372            DriverBindingHandle,
373            ControllerHandle
374            );
375     return EFI_UNSUPPORTED;
376   }
377 
378   if (Status != EFI_ALREADY_STARTED) {
379     return EFI_UNSUPPORTED;
380   }
381 
382   return EFI_SUCCESS;
383 }
384 
385 EFI_STATUS
EfiLibTestChildHandle(IN EFI_HANDLE ControllerHandle,IN EFI_HANDLE ChildHandle,IN EFI_GUID * ConsumedGuid)386 EfiLibTestChildHandle (
387   IN EFI_HANDLE       ControllerHandle,
388   IN EFI_HANDLE       ChildHandle,
389   IN EFI_GUID         *ConsumedGuid
390   )
391 /*++
392 
393 Routine Description:
394 
395   Test to see if the child handle is the child of the controller
396 
397 Arguments:
398 
399   ControllerHandle          - Handle for controller (parent)
400 
401   ChildHandle               - Child handle to test
402 
403   ConsumsedGuid             - Protocol guid consumed by child from controller
404 
405 Returns:
406 
407   EFI_SUCCESS     - The child handle is the child of the controller
408 
409   EFI_UNSUPPORTED - The child handle is not the child of the controller
410 
411 --*/
412 {
413   EFI_STATUS                            Status;
414   EFI_OPEN_PROTOCOL_INFORMATION_ENTRY   *OpenInfoBuffer;
415   UINTN                                 EntryCount;
416   UINTN                                 Index;
417 
418   //
419   // Retrieve the list of agents that are consuming one of the protocols
420   // on ControllerHandle that the children consume
421   //
422   Status = gBS->OpenProtocolInformation (
423                   ControllerHandle,
424                   ConsumedGuid,
425                   &OpenInfoBuffer,
426                   &EntryCount
427                   );
428   if (EFI_ERROR (Status)) {
429     return EFI_UNSUPPORTED;
430   }
431 
432   //
433   // See if one of the agents is ChildHandle
434   //
435   Status = EFI_UNSUPPORTED;
436   for (Index = 0; Index < EntryCount; Index++) {
437     if (OpenInfoBuffer[Index].ControllerHandle == ChildHandle &&
438         OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
439       Status = EFI_SUCCESS;
440     }
441   }
442   gBS->FreePool (OpenInfoBuffer);
443   return Status;
444 }
445