9b96f28fb1ccf019200856ba42494f7c2a71fd32
[mirror_edk2.git] / MdePkg / Library / UefiDriverModelLib / UefiDriverModelLib.c
1 /** @file
2 EFI Driver Model Library.
3
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name: EfiDriverModelLib.c
14
15 **/
16
17
18
19 /**
20 **/
21 EFI_STATUS
22 UefiDriverModelLibConstructor (
23 IN EFI_HANDLE ImageHandle,
24 IN EFI_SYSTEM_TABLE *SystemTable
25 )
26 {
27 EFI_STATUS Status;
28 UINTN Index;
29 EFI_HANDLE DriverBindingHandle;
30 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
31
32 //
33 // If no Driver Binding Protocols are advertised by the driver then simply return
34 //
35 if (_gDriverModelProtocolListEntries == 0) {
36 return EFI_SUCCESS;
37 }
38
39 //
40 // Install the first Driver Bindng Protocol onto ImageHandle
41 //
42 DriverBindingHandle = ImageHandle;
43
44 //
45 // See if onle one Driver Binding Protocol is advertised by the driver
46 //
47 if (_gDriverModelProtocolListEntries == 1) {
48 //
49 // The Driver Binding Protocol must never be NULL
50 //
51 ASSERT(_gDriverModelProtocolList[0].DriverBinding != NULL);
52
53 //
54 // Check for all 8 possible combinations of the ComponentName, DriverConfiguration, and DriverDiagnostics Protocol
55 // These are all checks against const pointers, so the optimizing compiler will only select one of the
56 // calls to InstallMultipleProtocolInterfaces()
57 //
58 if (_gDriverModelProtocolList[0].DriverDiagnostics == NULL) {
59 if (_gDriverModelProtocolList[0].DriverConfiguration == NULL) {
60 if (_gDriverModelProtocolList[0].ComponentName == NULL) {
61 Status = gBS->InstallMultipleProtocolInterfaces (
62 &DriverBindingHandle,
63 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
64 NULL
65 );
66 } else {
67 Status = gBS->InstallMultipleProtocolInterfaces (
68 &DriverBindingHandle,
69 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
70 &gEfiComponentNameProtocolGuid, (EFI_COMPONENT_NAME_PROTOCOL *)_gDriverModelProtocolList[0].ComponentName,
71 NULL
72 );
73 }
74 } else {
75 if (_gDriverModelProtocolList[0].ComponentName == NULL) {
76 Status = gBS->InstallMultipleProtocolInterfaces (
77 &DriverBindingHandle,
78 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
79 &gEfiDriverConfigurationProtocolGuid, (EFI_DRIVER_CONFIGURATION_PROTOCOL *)_gDriverModelProtocolList[0].DriverConfiguration,
80 NULL
81 );
82 } else {
83 Status = gBS->InstallMultipleProtocolInterfaces (
84 &DriverBindingHandle,
85 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
86 &gEfiComponentNameProtocolGuid, (EFI_COMPONENT_NAME_PROTOCOL *)_gDriverModelProtocolList[0].ComponentName,
87 &gEfiDriverConfigurationProtocolGuid, (EFI_DRIVER_CONFIGURATION_PROTOCOL *)_gDriverModelProtocolList[0].DriverConfiguration,
88 NULL
89 );
90 }
91 }
92 } else {
93 if (_gDriverModelProtocolList[0].DriverConfiguration == NULL) {
94 if (_gDriverModelProtocolList[0].ComponentName == NULL) {
95 Status = gBS->InstallMultipleProtocolInterfaces (
96 &DriverBindingHandle,
97 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
98 &gEfiDriverDiagnosticsProtocolGuid, (EFI_DRIVER_DIAGNOSTICS_PROTOCOL *)_gDriverModelProtocolList[0].DriverDiagnostics,
99 NULL
100 );
101 } else {
102 Status = gBS->InstallMultipleProtocolInterfaces (
103 &DriverBindingHandle,
104 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
105 &gEfiComponentNameProtocolGuid, (EFI_COMPONENT_NAME_PROTOCOL *)_gDriverModelProtocolList[0].ComponentName,
106 &gEfiDriverDiagnosticsProtocolGuid, (EFI_DRIVER_DIAGNOSTICS_PROTOCOL *)_gDriverModelProtocolList[0].DriverDiagnostics,
107 NULL
108 );
109 }
110 } else {
111 if (_gDriverModelProtocolList[0].ComponentName == NULL) {
112 Status = gBS->InstallMultipleProtocolInterfaces (
113 &DriverBindingHandle,
114 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
115 &gEfiDriverConfigurationProtocolGuid, (EFI_DRIVER_CONFIGURATION_PROTOCOL *)_gDriverModelProtocolList[0].DriverConfiguration,
116 &gEfiDriverDiagnosticsProtocolGuid, (EFI_DRIVER_DIAGNOSTICS_PROTOCOL *)_gDriverModelProtocolList[0].DriverDiagnostics,
117 NULL
118 );
119 } else {
120 Status = gBS->InstallMultipleProtocolInterfaces (
121 &DriverBindingHandle,
122 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
123 &gEfiComponentNameProtocolGuid, (EFI_COMPONENT_NAME_PROTOCOL *)_gDriverModelProtocolList[0].ComponentName,
124 &gEfiDriverConfigurationProtocolGuid, (EFI_DRIVER_CONFIGURATION_PROTOCOL *)_gDriverModelProtocolList[0].DriverConfiguration,
125 &gEfiDriverDiagnosticsProtocolGuid, (EFI_DRIVER_DIAGNOSTICS_PROTOCOL *)_gDriverModelProtocolList[0].DriverDiagnostics,
126 NULL
127 );
128 }
129 }
130 }
131
132 //
133 // ASSERT if the call to InstallMultipleProtocolInterfaces() failed
134 //
135 ASSERT_EFI_ERROR (Status);
136
137 //
138 // Update the ImageHandle and DriverBindingHandle fields of the Driver Binding Protocol
139 //
140 DriverBinding = (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding;
141 DriverBinding->ImageHandle = ImageHandle;
142 DriverBinding->DriverBindingHandle = DriverBindingHandle;
143
144 } else {
145 for (Index = 0; Index < _gDriverModelProtocolListEntries; Index++) {
146 //
147 // The Driver Binding Protocol must never be NULL
148 //
149 ASSERT(_gDriverModelProtocolList[Index].DriverBinding != NULL);
150
151 //
152 // Install the Driver Binding Protocol and ASSERT() if the installation fails
153 //
154 Status = gBS->InstallProtocolInterface (
155 &DriverBindingHandle,
156 &gEfiDriverBindingProtocolGuid,
157 EFI_NATIVE_INTERFACE,
158 (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[Index].DriverBinding
159 );
160 ASSERT_EFI_ERROR (Status);
161
162 //
163 // Update the ImageHandle and DriverBindingHandle fields of the Driver Binding Protocol
164 //
165 DriverBinding = (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[Index].DriverBinding;
166 DriverBinding->ImageHandle = ImageHandle;
167 DriverBinding->DriverBindingHandle = DriverBindingHandle;
168
169 //
170 // If Component Name Protocol is specified then install it and ASSERT() if the installation fails
171 //
172 if ((_gDriverModelProtocolBitmask & UEFI_DRIVER_MODEL_LIBRARY_COMPONENT_NAME_PROTOCOL_ENABLED) != 0) {
173 if (_gDriverModelProtocolList[Index].ComponentName != NULL) {
174 Status = gBS->InstallProtocolInterface (
175 &DriverBindingHandle,
176 &gEfiComponentNameProtocolGuid,
177 EFI_NATIVE_INTERFACE,
178 (EFI_COMPONENT_NAME_PROTOCOL *)_gDriverModelProtocolList[Index].ComponentName
179 );
180 ASSERT_EFI_ERROR (Status);
181 }
182 }
183
184 //
185 // If Driver Configuration Protocol is specified then install it and ASSERT() if the installation fails
186 //
187 if ((_gDriverModelProtocolBitmask & UEFI_DRIVER_MODEL_LIBRARY_DRIVER_CONFIGURATION_PROTOCOL_ENABLED) != 0) {
188 if (_gDriverModelProtocolList[Index].DriverConfiguration != NULL) {
189 Status = gBS->InstallProtocolInterface (
190 &DriverBindingHandle,
191 &gEfiDriverConfigurationProtocolGuid,
192 EFI_NATIVE_INTERFACE,
193 (EFI_DRIVER_CONFIGURATION_PROTOCOL *)_gDriverModelProtocolList[Index].DriverConfiguration
194 );
195 ASSERT_EFI_ERROR (Status);
196 }
197 }
198
199 //
200 // If Driver Diagnostics Protocol is specified then install it and ASSERT() if the installation fails
201 //
202 if ((_gDriverModelProtocolBitmask & UEFI_DRIVER_MODEL_LIBRARY_DRIVER_DIAGNOSTICS_PROTOCOL_ENABLED) != 0) {
203 if (_gDriverModelProtocolList[Index].DriverDiagnostics != NULL) {
204 Status = gBS->InstallProtocolInterface (
205 &DriverBindingHandle,
206 &gEfiDriverDiagnosticsProtocolGuid,
207 EFI_NATIVE_INTERFACE,
208 (EFI_DRIVER_DIAGNOSTICS_PROTOCOL *)_gDriverModelProtocolList[Index].DriverDiagnostics
209 );
210 ASSERT_EFI_ERROR (Status);
211 }
212 }
213
214 //
215 // Install subsequent Driver Bindng Protocols onto new handles
216 //
217 DriverBindingHandle = NULL;
218 }
219 }
220 return EFI_SUCCESS;
221 }
222
223 /**
224 **/
225 EFI_STATUS
226 UefiDriverModelLibDestructor (
227 IN EFI_HANDLE ImageHandle,
228 IN EFI_SYSTEM_TABLE *SystemTable
229 )
230 {
231 EFI_STATUS Status;
232 UINTN Index;
233 EFI_HANDLE DriverBindingHandle;
234
235 //
236 // If no Driver Binding Protocols are advertised by the driver then simply return
237 //
238 if (_gDriverModelProtocolListEntries == 0) {
239 return EFI_SUCCESS;
240 }
241
242 //
243 // See if onle one Driver Binding Protocol is advertised by the driver
244 //
245 if (_gDriverModelProtocolListEntries == 1) {
246 //
247 // The Driver Binding Protocol must never be NULL
248 //
249 ASSERT(_gDriverModelProtocolList[0].DriverBinding != NULL);
250
251 //
252 // Retrieve the DriverBindingHandle from the Driver Binding Protocol
253 //
254 DriverBindingHandle = _gDriverModelProtocolList[0].DriverBinding->DriverBindingHandle;
255
256 //
257 // Check for all 8 possible combinations of the ComponentName, DriverConfiguration, and DriverDiagnostics Protocol
258 // These are all checks against const pointers, so the optimizing compiler will only select one of the
259 // calls to InstallMultipleProtocolInterfaces()
260 //
261 if (_gDriverModelProtocolList[0].DriverDiagnostics == NULL) {
262 if (_gDriverModelProtocolList[0].DriverConfiguration == NULL) {
263 if (_gDriverModelProtocolList[0].ComponentName == NULL) {
264 Status = gBS->UninstallMultipleProtocolInterfaces (
265 DriverBindingHandle,
266 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
267 NULL
268 );
269 } else {
270 Status = gBS->UninstallMultipleProtocolInterfaces (
271 &DriverBindingHandle,
272 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
273 &gEfiComponentNameProtocolGuid, (EFI_COMPONENT_NAME_PROTOCOL *)_gDriverModelProtocolList[0].ComponentName,
274 NULL
275 );
276 }
277 } else {
278 if (_gDriverModelProtocolList[0].ComponentName == NULL) {
279 Status = gBS->UninstallMultipleProtocolInterfaces (
280 &DriverBindingHandle,
281 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
282 &gEfiDriverConfigurationProtocolGuid, (EFI_DRIVER_CONFIGURATION_PROTOCOL *)_gDriverModelProtocolList[0].DriverConfiguration,
283 NULL
284 );
285 } else {
286 Status = gBS->UninstallMultipleProtocolInterfaces (
287 &DriverBindingHandle,
288 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
289 &gEfiComponentNameProtocolGuid, (EFI_COMPONENT_NAME_PROTOCOL *)_gDriverModelProtocolList[0].ComponentName,
290 &gEfiDriverConfigurationProtocolGuid, (EFI_DRIVER_CONFIGURATION_PROTOCOL *)_gDriverModelProtocolList[0].DriverConfiguration,
291 NULL
292 );
293 }
294 }
295 } else {
296 if (_gDriverModelProtocolList[0].DriverConfiguration == NULL) {
297 if (_gDriverModelProtocolList[0].ComponentName == NULL) {
298 Status = gBS->UninstallMultipleProtocolInterfaces (
299 &DriverBindingHandle,
300 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
301 &gEfiDriverDiagnosticsProtocolGuid, (EFI_DRIVER_DIAGNOSTICS_PROTOCOL *)_gDriverModelProtocolList[0].DriverDiagnostics,
302 NULL
303 );
304 } else {
305 Status = gBS->UninstallMultipleProtocolInterfaces (
306 &DriverBindingHandle,
307 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
308 &gEfiComponentNameProtocolGuid, (EFI_COMPONENT_NAME_PROTOCOL *)_gDriverModelProtocolList[0].ComponentName,
309 &gEfiDriverDiagnosticsProtocolGuid, (EFI_DRIVER_DIAGNOSTICS_PROTOCOL *)_gDriverModelProtocolList[0].DriverDiagnostics,
310 NULL
311 );
312 }
313 } else {
314 if (_gDriverModelProtocolList[0].ComponentName == NULL) {
315 Status = gBS->UninstallMultipleProtocolInterfaces (
316 &DriverBindingHandle,
317 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
318 &gEfiDriverConfigurationProtocolGuid, (EFI_DRIVER_CONFIGURATION_PROTOCOL *)_gDriverModelProtocolList[0].DriverConfiguration,
319 &gEfiDriverDiagnosticsProtocolGuid, (EFI_DRIVER_DIAGNOSTICS_PROTOCOL *)_gDriverModelProtocolList[0].DriverDiagnostics,
320 NULL
321 );
322 } else {
323 Status = gBS->UninstallMultipleProtocolInterfaces (
324 &DriverBindingHandle,
325 &gEfiDriverBindingProtocolGuid, (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[0].DriverBinding,
326 &gEfiComponentNameProtocolGuid, (EFI_COMPONENT_NAME_PROTOCOL *)_gDriverModelProtocolList[0].ComponentName,
327 &gEfiDriverConfigurationProtocolGuid, (EFI_DRIVER_CONFIGURATION_PROTOCOL *)_gDriverModelProtocolList[0].DriverConfiguration,
328 &gEfiDriverDiagnosticsProtocolGuid, (EFI_DRIVER_DIAGNOSTICS_PROTOCOL *)_gDriverModelProtocolList[0].DriverDiagnostics,
329 NULL
330 );
331 }
332 }
333 }
334
335 //
336 // ASSERT if the call to UninstallMultipleProtocolInterfaces() failed
337 //
338 ASSERT_EFI_ERROR (Status);
339 } else {
340 for (Index = 0; Index < _gDriverModelProtocolListEntries; Index++) {
341 //
342 // The Driver Binding Protocol must never be NULL
343 //
344 ASSERT(_gDriverModelProtocolList[Index].DriverBinding != NULL);
345
346 //
347 // Retrieve the DriverBindingHandle from the Driver Binding Protocol
348 //
349 DriverBindingHandle = _gDriverModelProtocolList[0].DriverBinding->DriverBindingHandle;
350
351 //
352 // Uninstall the Driver Binding Protocol and ASSERT() if the installation fails
353 //
354 Status = gBS->UninstallProtocolInterface (
355 DriverBindingHandle,
356 &gEfiDriverBindingProtocolGuid,
357 (EFI_DRIVER_BINDING_PROTOCOL *)_gDriverModelProtocolList[Index].DriverBinding
358 );
359 ASSERT_EFI_ERROR (Status);
360
361 //
362 // If Component Name Protocol is specified then uninstall it and ASSERT() if the uninstallation fails
363 //
364 if ((_gDriverModelProtocolBitmask & UEFI_DRIVER_MODEL_LIBRARY_COMPONENT_NAME_PROTOCOL_ENABLED) != 0) {
365 if (_gDriverModelProtocolList[Index].ComponentName != NULL) {
366 Status = gBS->UninstallProtocolInterface (
367 DriverBindingHandle,
368 &gEfiComponentNameProtocolGuid,
369 (EFI_COMPONENT_NAME_PROTOCOL *)_gDriverModelProtocolList[Index].ComponentName
370 );
371 ASSERT_EFI_ERROR (Status);
372 }
373 }
374
375 //
376 // If Driver Configuration Protocol is specified then uninstall it and ASSERT() if the uninstallation fails
377 //
378 if ((_gDriverModelProtocolBitmask & UEFI_DRIVER_MODEL_LIBRARY_DRIVER_CONFIGURATION_PROTOCOL_ENABLED) != 0) {
379 if (_gDriverModelProtocolList[Index].DriverConfiguration != NULL) {
380 Status = gBS->UninstallProtocolInterface (
381 DriverBindingHandle,
382 &gEfiDriverConfigurationProtocolGuid,
383 (EFI_DRIVER_CONFIGURATION_PROTOCOL *)_gDriverModelProtocolList[Index].DriverConfiguration
384 );
385 ASSERT_EFI_ERROR (Status);
386 }
387 }
388
389 //
390 // If Driver Diagnostics Protocol is specified then uninstall it and ASSERT() if the uninstallation fails
391 //
392 if ((_gDriverModelProtocolBitmask & UEFI_DRIVER_MODEL_LIBRARY_DRIVER_DIAGNOSTICS_PROTOCOL_ENABLED) != 0) {
393 if (_gDriverModelProtocolList[Index].DriverDiagnostics != NULL) {
394 Status = gBS->UninstallProtocolInterface (
395 DriverBindingHandle,
396 &gEfiDriverDiagnosticsProtocolGuid,
397 (EFI_DRIVER_DIAGNOSTICS_PROTOCOL *)_gDriverModelProtocolList[Index].DriverDiagnostics
398 );
399 ASSERT_EFI_ERROR (Status);
400 }
401 }
402 }
403 }
404 return EFI_SUCCESS;
405 }