]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Console/ConPlatformDxe/ConPlatform.c
Improve coding style in MdeModulePkg.
[mirror_edk2.git] / MdeModulePkg / Universal / Console / ConPlatformDxe / ConPlatform.c
... / ...
CommitLineData
1/** @file\r
2 Console Platform DXE Driver, install Console Device Guids and update Console\r
3 Environment Variables.\r
4\r
5Copyright (c) 2006 - 2010, Intel Corporation. <BR>\r
6All rights reserved. This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "ConPlatform.h"\r
17\r
18\r
19EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextInDriverBinding = {\r
20 ConPlatformTextInDriverBindingSupported,\r
21 ConPlatformTextInDriverBindingStart,\r
22 ConPlatformTextInDriverBindingStop,\r
23 0xa,\r
24 NULL,\r
25 NULL\r
26};\r
27\r
28EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextOutDriverBinding = {\r
29 ConPlatformTextOutDriverBindingSupported,\r
30 ConPlatformTextOutDriverBindingStart,\r
31 ConPlatformTextOutDriverBindingStop,\r
32 0xa,\r
33 NULL,\r
34 NULL\r
35};\r
36\r
37/**\r
38 Entrypoint of this module.\r
39\r
40 This function is the entrypoint of this module. It installs Driver Binding\r
41 Protocols together with Component Name Protocols.\r
42\r
43 @param ImageHandle The firmware allocated handle for the EFI image.\r
44 @param SystemTable A pointer to the EFI System Table.\r
45\r
46 @retval EFI_SUCCESS The entry point is executed successfully.\r
47\r
48**/\r
49EFI_STATUS\r
50EFIAPI\r
51InitializeConPlatform(\r
52 IN EFI_HANDLE ImageHandle,\r
53 IN EFI_SYSTEM_TABLE *SystemTable\r
54 )\r
55{\r
56 EFI_STATUS Status;\r
57\r
58 Status = EfiLibInstallDriverBindingComponentName2 (\r
59 ImageHandle,\r
60 SystemTable,\r
61 &gConPlatformTextInDriverBinding,\r
62 ImageHandle,\r
63 &gConPlatformComponentName,\r
64 &gConPlatformComponentName2\r
65 );\r
66 ASSERT_EFI_ERROR (Status);\r
67\r
68 Status = EfiLibInstallDriverBindingComponentName2 (\r
69 ImageHandle,\r
70 SystemTable,\r
71 &gConPlatformTextOutDriverBinding,\r
72 NULL,\r
73 &gConPlatformComponentName,\r
74 &gConPlatformComponentName2\r
75 );\r
76 ASSERT_EFI_ERROR (Status);\r
77\r
78 return EFI_SUCCESS;\r
79}\r
80\r
81\r
82/**\r
83 Test to see if EFI_SIMPLE_TEXT_INPUT_PROTOCOL is supported on ControllerHandle. \r
84\r
85 @param This Protocol instance pointer.\r
86 @param ControllerHandle Handle of device to test.\r
87 @param RemainingDevicePath Optional parameter use to pick a specific child\r
88 device to start.\r
89\r
90 @retval EFI_SUCCESS This driver supports this device.\r
91 @retval other This driver does not support this device.\r
92\r
93**/\r
94EFI_STATUS\r
95EFIAPI\r
96ConPlatformTextInDriverBindingSupported (\r
97 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
98 IN EFI_HANDLE ControllerHandle,\r
99 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
100 )\r
101{\r
102 return ConPlatformDriverBindingSupported (\r
103 This,\r
104 ControllerHandle,\r
105 &gEfiSimpleTextInProtocolGuid\r
106 );\r
107}\r
108\r
109\r
110/**\r
111 Test to see if EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is supported on ControllerHandle. \r
112\r
113 @param This Protocol instance pointer.\r
114 @param ControllerHandle Handle of device to test.\r
115 @param RemainingDevicePath Optional parameter use to pick a specific child\r
116 device to start.\r
117\r
118 @retval EFI_SUCCESS This driver supports this device.\r
119 @retval other This driver does not support this device.\r
120\r
121**/\r
122EFI_STATUS\r
123EFIAPI\r
124ConPlatformTextOutDriverBindingSupported (\r
125 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
126 IN EFI_HANDLE ControllerHandle,\r
127 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
128 )\r
129{\r
130 return ConPlatformDriverBindingSupported (\r
131 This,\r
132 ControllerHandle,\r
133 &gEfiSimpleTextOutProtocolGuid\r
134 );\r
135}\r
136\r
137\r
138/**\r
139 Test to see if the specified protocol is supported on ControllerHandle. \r
140\r
141 @param This Protocol instance pointer.\r
142 @param ControllerHandle Handle of device to test.\r
143 @param ProtocolGuid The specfic protocol.\r
144\r
145 @retval EFI_SUCCESS This driver supports this device.\r
146 @retval other This driver does not support this device.\r
147\r
148**/\r
149EFI_STATUS\r
150ConPlatformDriverBindingSupported (\r
151 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
152 IN EFI_HANDLE ControllerHandle,\r
153 IN EFI_GUID *ProtocolGuid\r
154 )\r
155{\r
156 EFI_STATUS Status;\r
157 VOID *Interface;\r
158\r
159 //\r
160 // Test to see if this is a physical device by checking if\r
161 // it has a Device Path Protocol.\r
162 //\r
163 Status = gBS->OpenProtocol (\r
164 ControllerHandle,\r
165 &gEfiDevicePathProtocolGuid,\r
166 NULL,\r
167 This->DriverBindingHandle,\r
168 ControllerHandle,\r
169 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
170 );\r
171 if (EFI_ERROR (Status)) {\r
172 return Status;\r
173 }\r
174 //\r
175 // Test to see if this device supports the specified Protocol.\r
176 //\r
177 Status = gBS->OpenProtocol (\r
178 ControllerHandle,\r
179 ProtocolGuid,\r
180 (VOID **) &Interface,\r
181 This->DriverBindingHandle,\r
182 ControllerHandle,\r
183 EFI_OPEN_PROTOCOL_BY_DRIVER\r
184 );\r
185 if (EFI_ERROR (Status)) {\r
186 return Status;\r
187 }\r
188\r
189 gBS->CloseProtocol (\r
190 ControllerHandle,\r
191 ProtocolGuid,\r
192 This->DriverBindingHandle,\r
193 ControllerHandle\r
194 );\r
195\r
196 return EFI_SUCCESS;\r
197}\r
198\r
199/**\r
200 Start this driver on the device for console input.\r
201\r
202 Start this driver on ControllerHandle by opening Simple Text Input Protocol,\r
203 reading Device Path, and installing Console In Devcice GUID on ControllerHandle.\r
204\r
205 If this devcie is not one hot-plug devce, append its device path into the \r
206 console environment variables ConInDev.\r
207 \r
208 @param This Protocol instance pointer.\r
209 @param ControllerHandle Handle of device to bind driver to\r
210 @param RemainingDevicePath Optional parameter use to pick a specific child\r
211 device to start.\r
212\r
213 @retval EFI_SUCCESS This driver is added to ControllerHandle\r
214 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle\r
215 @retval other This driver does not support this device.\r
216\r
217**/\r
218EFI_STATUS\r
219EFIAPI\r
220ConPlatformTextInDriverBindingStart (\r
221 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
222 IN EFI_HANDLE ControllerHandle,\r
223 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
224 )\r
225{\r
226 EFI_STATUS Status;\r
227 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
228 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *TextIn;\r
229 BOOLEAN IsInConInVariable;\r
230\r
231 //\r
232 // Get the Device Path Protocol so the environment variables can be updated\r
233 //\r
234 Status = gBS->OpenProtocol (\r
235 ControllerHandle,\r
236 &gEfiDevicePathProtocolGuid,\r
237 (VOID **) &DevicePath,\r
238 This->DriverBindingHandle,\r
239 ControllerHandle,\r
240 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
241 );\r
242 if (EFI_ERROR (Status)) {\r
243 return Status;\r
244 }\r
245 //\r
246 // Open the Simple Text Input Protocol BY_DRIVER\r
247 //\r
248 Status = gBS->OpenProtocol (\r
249 ControllerHandle,\r
250 &gEfiSimpleTextInProtocolGuid,\r
251 (VOID **) &TextIn,\r
252 This->DriverBindingHandle,\r
253 ControllerHandle,\r
254 EFI_OPEN_PROTOCOL_BY_DRIVER\r
255 );\r
256 if (EFI_ERROR (Status)) {\r
257 return Status;\r
258 }\r
259 //\r
260 // Check if the device path is in ConIn Variable\r
261 //\r
262 IsInConInVariable = FALSE;\r
263 Status = ConPlatformUpdateDeviceVariable (\r
264 L"ConIn",\r
265 DevicePath,\r
266 Check\r
267 );\r
268 if (!EFI_ERROR (Status)) {\r
269 IsInConInVariable = TRUE;\r
270 }\r
271\r
272 //\r
273 // Check the device path, if it is a hot plug device,\r
274 // do not put the device path into ConInDev, and install\r
275 // gEfiConsoleInDeviceGuid to the device handle directly.\r
276 // The policy is, make hot plug device plug in and play immediately.\r
277 //\r
278 if (IsHotPlugDevice (DevicePath)) {\r
279 gBS->InstallMultipleProtocolInterfaces (\r
280 &ControllerHandle,\r
281 &gEfiConsoleInDeviceGuid,\r
282 NULL,\r
283 NULL\r
284 );\r
285 //\r
286 // Append the device path to ConInDev only if it is in ConIn variable.\r
287 //\r
288 if (IsInConInVariable) {\r
289 ConPlatformUpdateDeviceVariable (\r
290 L"ConInDev",\r
291 DevicePath,\r
292 Append\r
293 );\r
294 }\r
295 } else {\r
296 //\r
297 // If it is not a hot-plug device, append the device path to the\r
298 // ConInDev environment variable\r
299 //\r
300 ConPlatformUpdateDeviceVariable (\r
301 L"ConInDev",\r
302 DevicePath,\r
303 Append\r
304 );\r
305\r
306 //\r
307 // If the device path is an instance in the ConIn environment variable,\r
308 // then install EfiConsoleInDeviceGuid onto ControllerHandle\r
309 //\r
310 if (IsInConInVariable) {\r
311 gBS->InstallMultipleProtocolInterfaces (\r
312 &ControllerHandle,\r
313 &gEfiConsoleInDeviceGuid,\r
314 NULL,\r
315 NULL\r
316 );\r
317 } else {\r
318 gBS->CloseProtocol (\r
319 ControllerHandle,\r
320 &gEfiSimpleTextInProtocolGuid,\r
321 This->DriverBindingHandle,\r
322 ControllerHandle\r
323 );\r
324 }\r
325 }\r
326\r
327 return EFI_SUCCESS;\r
328}\r
329\r
330/**\r
331 Start this driver on the device for console output and stardard error output.\r
332\r
333 Start this driver on ControllerHandle by opening Simple Text Output Protocol,\r
334 reading Device Path, and installing Console Out Devcic GUID, Standard Error\r
335 Device GUID on ControllerHandle.\r
336\r
337 If this devcie is not one hot-plug devce, append its device path into the \r
338 console environment variables ConOutDev, ErrOutDev.\r
339 \r
340 @param This Protocol instance pointer.\r
341 @param ControllerHandle Handle of device to bind driver to\r
342 @param RemainingDevicePath Optional parameter use to pick a specific child\r
343 device to start.\r
344\r
345 @retval EFI_SUCCESS This driver is added to ControllerHandle\r
346 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle\r
347 @retval other This driver does not support this device\r
348\r
349**/\r
350EFI_STATUS\r
351EFIAPI\r
352ConPlatformTextOutDriverBindingStart (\r
353 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
354 IN EFI_HANDLE ControllerHandle,\r
355 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
356 )\r
357{\r
358 EFI_STATUS Status;\r
359 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
360 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *TextOut;\r
361 BOOLEAN NeedClose;\r
362 BOOLEAN IsInConOutVariable;\r
363 BOOLEAN IsInErrOutVariable;\r
364\r
365 NeedClose = TRUE;\r
366\r
367 //\r
368 // Get the Device Path Protocol so the environment variables can be updated\r
369 //\r
370 Status = gBS->OpenProtocol (\r
371 ControllerHandle,\r
372 &gEfiDevicePathProtocolGuid,\r
373 (VOID **) &DevicePath,\r
374 This->DriverBindingHandle,\r
375 ControllerHandle,\r
376 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
377 );\r
378 if (EFI_ERROR (Status)) {\r
379 return Status;\r
380 }\r
381 //\r
382 // Open the Simple Text Output Protocol BY_DRIVER\r
383 //\r
384 Status = gBS->OpenProtocol (\r
385 ControllerHandle,\r
386 &gEfiSimpleTextOutProtocolGuid,\r
387 (VOID **) &TextOut,\r
388 This->DriverBindingHandle,\r
389 ControllerHandle,\r
390 EFI_OPEN_PROTOCOL_BY_DRIVER\r
391 );\r
392 if (EFI_ERROR (Status)) {\r
393 return Status;\r
394 }\r
395 //\r
396 // Check if the device path is in ConOut & ErrOut Variable\r
397 //\r
398 IsInConOutVariable = FALSE;\r
399 Status = ConPlatformUpdateDeviceVariable (\r
400 L"ConOut",\r
401 DevicePath,\r
402 Check\r
403 );\r
404 if (!EFI_ERROR (Status)) {\r
405 IsInConOutVariable = TRUE;\r
406 }\r
407\r
408 IsInErrOutVariable = FALSE;\r
409 Status = ConPlatformUpdateDeviceVariable (\r
410 L"ErrOut",\r
411 DevicePath,\r
412 Check\r
413 );\r
414 if (!EFI_ERROR (Status)) {\r
415 IsInErrOutVariable = TRUE;\r
416 }\r
417\r
418 //\r
419 // Check the device path, if it is a hot plug device,\r
420 // do not put the device path into ConOutDev and ErrOutDev,\r
421 // and install gEfiConsoleOutDeviceGuid to the device handle directly.\r
422 // The policy is, make hot plug device plug in and play immediately.\r
423 //\r
424 if (IsHotPlugDevice (DevicePath)) {\r
425 gBS->InstallMultipleProtocolInterfaces (\r
426 &ControllerHandle,\r
427 &gEfiConsoleOutDeviceGuid,\r
428 NULL,\r
429 NULL\r
430 );\r
431 //\r
432 // Append the device path to ConOutDev only if it is in ConOut variable.\r
433 //\r
434 if (IsInConOutVariable) {\r
435 ConPlatformUpdateDeviceVariable (\r
436 L"ConOutDev",\r
437 DevicePath,\r
438 Append\r
439 );\r
440 }\r
441 //\r
442 // Append the device path to ErrOutDev only if it is in ErrOut variable.\r
443 //\r
444 if (IsInErrOutVariable) {\r
445 ConPlatformUpdateDeviceVariable (\r
446 L"ErrOutDev",\r
447 DevicePath,\r
448 Append\r
449 );\r
450 }\r
451 } else {\r
452 //\r
453 // If it is not a hot-plug device, first append the device path to the\r
454 // ConOutDev environment variable\r
455 //\r
456 ConPlatformUpdateDeviceVariable (\r
457 L"ConOutDev",\r
458 DevicePath,\r
459 Append\r
460 );\r
461 //\r
462 // Then append the device path to the ErrOutDev environment variable\r
463 //\r
464 ConPlatformUpdateDeviceVariable (\r
465 L"ErrOutDev",\r
466 DevicePath,\r
467 Append\r
468 );\r
469\r
470 //\r
471 // If the device path is an instance in the ConOut environment variable,\r
472 // then install EfiConsoleOutDeviceGuid onto ControllerHandle\r
473 //\r
474 if (IsInConOutVariable) {\r
475 NeedClose = FALSE;\r
476 Status = gBS->InstallMultipleProtocolInterfaces (\r
477 &ControllerHandle,\r
478 &gEfiConsoleOutDeviceGuid,\r
479 NULL,\r
480 NULL\r
481 );\r
482 }\r
483 //\r
484 // If the device path is an instance in the ErrOut environment variable,\r
485 // then install EfiStandardErrorDeviceGuid onto ControllerHandle\r
486 //\r
487 if (IsInErrOutVariable) {\r
488 NeedClose = FALSE;\r
489 gBS->InstallMultipleProtocolInterfaces (\r
490 &ControllerHandle,\r
491 &gEfiStandardErrorDeviceGuid,\r
492 NULL,\r
493 NULL\r
494 );\r
495 }\r
496\r
497 if (NeedClose) {\r
498 gBS->CloseProtocol (\r
499 ControllerHandle,\r
500 &gEfiSimpleTextOutProtocolGuid,\r
501 This->DriverBindingHandle,\r
502 ControllerHandle\r
503 );\r
504 }\r
505 }\r
506\r
507 return EFI_SUCCESS;\r
508}\r
509\r
510/**\r
511 Stop this driver on ControllerHandle by removing Console In Devcice GUID \r
512 and closing the Simple Text Input protocol on ControllerHandle.\r
513\r
514 @param This Protocol instance pointer.\r
515 @param ControllerHandle Handle of device to stop driver on\r
516 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
517 children is zero stop the entire bus driver.\r
518 @param ChildHandleBuffer List of Child Handles to Stop.\r
519\r
520 @retval EFI_SUCCESS This driver is removed ControllerHandle\r
521 @retval other This driver was not removed from this device\r
522\r
523**/\r
524EFI_STATUS\r
525EFIAPI\r
526ConPlatformTextInDriverBindingStop (\r
527 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
528 IN EFI_HANDLE ControllerHandle,\r
529 IN UINTN NumberOfChildren,\r
530 IN EFI_HANDLE *ChildHandleBuffer\r
531 )\r
532{\r
533 EFI_STATUS Status;\r
534 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
535\r
536 //\r
537 // Get the Device Path Protocol firstly\r
538 //\r
539 Status = gBS->OpenProtocol (\r
540 ControllerHandle,\r
541 &gEfiDevicePathProtocolGuid,\r
542 (VOID **) &DevicePath,\r
543 This->DriverBindingHandle,\r
544 ControllerHandle,\r
545 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
546 );\r
547 //\r
548 // If there is device path on ControllerHandle\r
549 //\r
550 if (!EFI_ERROR (Status)) {\r
551 //\r
552 // Remove DevicePath from ConInDev if exists.\r
553 //\r
554 ConPlatformUpdateDeviceVariable (\r
555 L"ConInDev",\r
556 DevicePath,\r
557 Delete\r
558 );\r
559 }\r
560\r
561 //\r
562 // Uninstall the Console Device GUIDs from Controller Handle\r
563 //\r
564 ConPlatformUnInstallProtocol (\r
565 This,\r
566 ControllerHandle,\r
567 &gEfiConsoleInDeviceGuid\r
568 );\r
569\r
570 //\r
571 // Close the Simple Text Input Protocol\r
572 //\r
573 gBS->CloseProtocol (\r
574 ControllerHandle,\r
575 &gEfiSimpleTextInProtocolGuid,\r
576 This->DriverBindingHandle,\r
577 ControllerHandle\r
578 );\r
579\r
580 return EFI_SUCCESS;\r
581}\r
582\r
583\r
584/**\r
585 Stop this driver on ControllerHandle by removing Console Out Devcice GUID \r
586 and closing the Simple Text Output protocol on ControllerHandle.\r
587\r
588 @param This Protocol instance pointer.\r
589 @param ControllerHandle Handle of device to stop driver on\r
590 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
591 children is zero stop the entire bus driver.\r
592 @param ChildHandleBuffer List of Child Handles to Stop.\r
593\r
594 @retval EFI_SUCCESS This driver is removed ControllerHandle\r
595 @retval other This driver was not removed from this device\r
596\r
597**/\r
598EFI_STATUS\r
599EFIAPI\r
600ConPlatformTextOutDriverBindingStop (\r
601 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
602 IN EFI_HANDLE ControllerHandle,\r
603 IN UINTN NumberOfChildren,\r
604 IN EFI_HANDLE *ChildHandleBuffer\r
605 )\r
606{\r
607 EFI_STATUS Status;\r
608 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
609\r
610 //\r
611 // Get the Device Path Protocol firstly\r
612 //\r
613 Status = gBS->OpenProtocol (\r
614 ControllerHandle,\r
615 &gEfiDevicePathProtocolGuid,\r
616 (VOID **) &DevicePath,\r
617 This->DriverBindingHandle,\r
618 ControllerHandle,\r
619 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
620 );\r
621 if (!EFI_ERROR (Status)) {\r
622 //\r
623 // Remove DevicePath from ConOutDev and ErrOutDev if exists.\r
624 //\r
625 ConPlatformUpdateDeviceVariable (\r
626 L"ConOutDev",\r
627 DevicePath,\r
628 Delete\r
629 );\r
630 ConPlatformUpdateDeviceVariable (\r
631 L"ErrOutDev",\r
632 DevicePath,\r
633 Delete\r
634 );\r
635 }\r
636\r
637 //\r
638 // Uninstall the Console Device GUIDs from Controller Handle\r
639 //\r
640 ConPlatformUnInstallProtocol (\r
641 This,\r
642 ControllerHandle,\r
643 &gEfiConsoleOutDeviceGuid\r
644 );\r
645\r
646 ConPlatformUnInstallProtocol (\r
647 This,\r
648 ControllerHandle,\r
649 &gEfiStandardErrorDeviceGuid\r
650 );\r
651\r
652 //\r
653 // Close the Simple Text Output Protocol\r
654 //\r
655 gBS->CloseProtocol (\r
656 ControllerHandle,\r
657 &gEfiSimpleTextOutProtocolGuid,\r
658 This->DriverBindingHandle,\r
659 ControllerHandle\r
660 );\r
661\r
662 return EFI_SUCCESS;\r
663}\r
664\r
665\r
666/**\r
667 Uninstall the specified protocol.\r
668\r
669 @param This Protocol instance pointer.\r
670 @param Handle Handle of device to uninstall protocol on.\r
671 @param ProtocolGuid The specified protocol need to be uninstalled.\r
672\r
673**/\r
674VOID\r
675ConPlatformUnInstallProtocol (\r
676 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
677 IN EFI_HANDLE Handle,\r
678 IN EFI_GUID *ProtocolGuid\r
679 )\r
680{\r
681 EFI_STATUS Status;\r
682\r
683 Status = gBS->OpenProtocol (\r
684 Handle,\r
685 ProtocolGuid,\r
686 NULL,\r
687 This->DriverBindingHandle,\r
688 Handle,\r
689 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
690 );\r
691\r
692 if (!EFI_ERROR (Status)) {\r
693 gBS->UninstallMultipleProtocolInterfaces (\r
694 Handle,\r
695 ProtocolGuid,\r
696 NULL,\r
697 NULL\r
698 );\r
699 }\r
700\r
701 return ;\r
702}\r
703\r
704/**\r
705 Get the necessary size of buffer and read the variable.\r
706\r
707 First get the necessary size of buffer. Then read the\r
708 EFI variable (Name) and return a dynamically allocated\r
709 buffer. On failure return NULL.\r
710\r
711 @param Name String part of EFI variable name\r
712\r
713 @return Dynamically allocated memory that contains a copy of the EFI variable.\r
714 Caller is repsoncible freeing the buffer. Return NULL means Variable \r
715 was not read.\r
716\r
717**/\r
718VOID *\r
719ConPlatformGetVariable (\r
720 IN CHAR16 *Name\r
721 )\r
722{\r
723 EFI_STATUS Status;\r
724 VOID *Buffer;\r
725 UINTN BufferSize;\r
726\r
727 BufferSize = 0;\r
728 Buffer = NULL;\r
729\r
730 //\r
731 // Test to see if the variable exists. If it doesn't, return NULL.\r
732 //\r
733 Status = gRT->GetVariable (\r
734 Name,\r
735 &gEfiGlobalVariableGuid,\r
736 NULL,\r
737 &BufferSize,\r
738 Buffer\r
739 );\r
740\r
741 if (Status == EFI_BUFFER_TOO_SMALL) {\r
742 //\r
743 // Allocate the buffer to return\r
744 //\r
745 Buffer = AllocatePool (BufferSize);\r
746 if (Buffer == NULL) {\r
747 return NULL;\r
748 }\r
749 //\r
750 // Read variable into the allocated buffer.\r
751 //\r
752 Status = gRT->GetVariable (\r
753 Name,\r
754 &gEfiGlobalVariableGuid,\r
755 NULL,\r
756 &BufferSize,\r
757 Buffer\r
758 );\r
759 if (EFI_ERROR (Status)) {\r
760 FreePool (Buffer);\r
761 //\r
762 // To make sure Buffer is NULL if any error occurs.\r
763 //\r
764 Buffer = NULL;\r
765 }\r
766 }\r
767\r
768 return Buffer;\r
769}\r
770\r
771/**\r
772 Function compares a device path data structure to that of all the nodes of a\r
773 second device path instance.\r
774\r
775 @param Multi A pointer to a multi-instance device path data structure.\r
776 @param Single A pointer to a single-instance device path data structure.\r
777 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it\r
778 points to the remaining device path data structure.\r
779 (remaining device path = Multi - Single.)\r
780 @param Delete If TRUE, means removing Single from Multi.\r
781 If FALSE, the routine just check whether Single matches\r
782 with any instance in Multi.\r
783\r
784 @retval EFI_SUCCESS If the Single is contained within Multi.\r
785 @retval EFI_NOT_FOUND If the Single is not contained within Multi.\r
786 @retval EFI_INVALID_PARAMETER Multi is NULL.\r
787 @retval EFI_INVALID_PARAMETER Single is NULL.\r
788 @retval EFI_INVALID_PARAMETER NewDevicePath is NULL when Delete is TRUE.\r
789\r
790**/\r
791EFI_STATUS\r
792ConPlatformMatchDevicePaths (\r
793 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
794 IN EFI_DEVICE_PATH_PROTOCOL *Single,\r
795 OUT EFI_DEVICE_PATH_PROTOCOL **NewDevicePath OPTIONAL,\r
796 IN BOOLEAN Delete\r
797 )\r
798{\r
799 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
800 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath1;\r
801 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath2;\r
802 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;\r
803 UINTN Size;\r
804\r
805 //\r
806 // The passed in DevicePath should not be NULL\r
807 //\r
808 if ((Multi == NULL) || (Single == NULL)) {\r
809 return EFI_INVALID_PARAMETER;\r
810 }\r
811\r
812 //\r
813 // If performing Delete operation, the NewDevicePath must not be NULL.\r
814 //\r
815 if (Delete) {\r
816 if (NewDevicePath == NULL) {\r
817 return EFI_INVALID_PARAMETER;\r
818 }\r
819 }\r
820\r
821 TempDevicePath1 = NULL;\r
822\r
823 DevicePath = Multi;\r
824 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
825\r
826 //\r
827 // Search for the match of 'Single' in 'Multi'\r
828 //\r
829 while (DevicePathInst != NULL) {\r
830 if (CompareMem (Single, DevicePathInst, Size) == 0) {\r
831 if (!Delete) {\r
832 //\r
833 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.\r
834 //\r
835 FreePool (DevicePathInst);\r
836 return EFI_SUCCESS;\r
837 }\r
838 } else {\r
839 if (Delete) {\r
840 //\r
841 // If the node of Multi does not match Single, then added it back to the result.\r
842 // That is, the node matching Single will be dropped and deleted from result.\r
843 //\r
844 TempDevicePath2 = AppendDevicePathInstance (\r
845 TempDevicePath1,\r
846 DevicePathInst\r
847 );\r
848 if (TempDevicePath1 != NULL) {\r
849 FreePool (TempDevicePath1);\r
850 }\r
851 TempDevicePath1 = TempDevicePath2;\r
852 }\r
853 }\r
854\r
855 FreePool (DevicePathInst);\r
856 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
857 }\r
858\r
859 if (Delete) {\r
860 //\r
861 // Return the new device path data structure with specified node deleted.\r
862 //\r
863 *NewDevicePath = TempDevicePath1;\r
864 return EFI_SUCCESS;\r
865 }\r
866\r
867 return EFI_NOT_FOUND;\r
868}\r
869\r
870/**\r
871 Update console environment variables. \r
872\r
873 @param VariableName Console environment variables, ConOutDev, ConInDev\r
874 ErrOutDev, ConIn ,ConOut or ErrOut.\r
875 @param DevicePath Console devcie's device path.\r
876 @param Operation Variable operations, including APPEND, CHECK and DELETE.\r
877\r
878 @retval EFI_SUCCESS Variable operates successfully.\r
879 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.\r
880 @retval other Variable updating failed.\r
881\r
882**/\r
883EFI_STATUS\r
884ConPlatformUpdateDeviceVariable (\r
885 IN CHAR16 *VariableName,\r
886 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
887 IN CONPLATFORM_VAR_OPERATION Operation\r
888 )\r
889{\r
890 EFI_STATUS Status;\r
891 EFI_DEVICE_PATH_PROTOCOL *VariableDevicePath;\r
892 EFI_DEVICE_PATH_PROTOCOL *NewVariableDevicePath;\r
893\r
894 VariableDevicePath = NULL;\r
895 NewVariableDevicePath = NULL;\r
896\r
897 //\r
898 // Get Variable according to variable name.\r
899 // The memory for Variable is allocated within ConPlatformGetVarible(),\r
900 // it is the caller's responsibility to free the memory before return.\r
901 //\r
902 VariableDevicePath = ConPlatformGetVariable (VariableName);\r
903\r
904 if (Operation != Delete) {\r
905 //\r
906 // Match specified DevicePath in Console Variable.\r
907 // \r
908 Status = ConPlatformMatchDevicePaths (\r
909 VariableDevicePath,\r
910 DevicePath,\r
911 NULL,\r
912 FALSE\r
913 );\r
914\r
915 if ((Operation == Check) || (!EFI_ERROR (Status))) {\r
916 //\r
917 // Branch here includes 2 cases:\r
918 // 1. Operation is CHECK, simply return Status.\r
919 // 2. Operation is APPEND, and device path already exists in variable, also return.\r
920 //\r
921 if (VariableDevicePath != NULL) {\r
922 FreePool (VariableDevicePath);\r
923 }\r
924\r
925 return Status;\r
926 }\r
927 //\r
928 // We reach here to append a device path that does not exist in variable.\r
929 //\r
930 Status = EFI_SUCCESS;\r
931 NewVariableDevicePath = AppendDevicePathInstance (\r
932 VariableDevicePath,\r
933 DevicePath\r
934 );\r
935 if (NewVariableDevicePath == NULL) {\r
936 Status = EFI_OUT_OF_RESOURCES;\r
937 }\r
938\r
939 } else {\r
940 //\r
941 // We reach here to remove DevicePath from the environment variable that\r
942 // is a multi-instance device path.\r
943 //\r
944 Status = ConPlatformMatchDevicePaths (\r
945 VariableDevicePath,\r
946 DevicePath,\r
947 &NewVariableDevicePath,\r
948 TRUE\r
949 );\r
950 }\r
951\r
952 if (VariableDevicePath != NULL) {\r
953 FreePool (VariableDevicePath);\r
954 }\r
955\r
956 if (EFI_ERROR (Status)) {\r
957 return Status;\r
958 }\r
959\r
960 if (NewVariableDevicePath != NULL) {\r
961 //\r
962 // Update Console Environment Variable.\r
963 //\r
964 Status = gRT->SetVariable (\r
965 VariableName,\r
966 &gEfiGlobalVariableGuid,\r
967 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
968 GetDevicePathSize (NewVariableDevicePath),\r
969 NewVariableDevicePath\r
970 );\r
971\r
972 FreePool (NewVariableDevicePath);\r
973 }\r
974\r
975 return Status;\r
976}\r
977\r
978/**\r
979 Check if the device supports hot-plug through its device path.\r
980\r
981 This function could be updated to check more types of Hot Plug devices.\r
982 Currently, it checks USB and PCCard device.\r
983\r
984 @param DevicePath Pointer to device's device path.\r
985\r
986 @retval TRUE The devcie is a hot-plug device\r
987 @retval FALSE The devcie is not a hot-plug device.\r
988\r
989**/\r
990BOOLEAN\r
991IsHotPlugDevice (\r
992 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
993 )\r
994{\r
995 EFI_DEVICE_PATH_PROTOCOL *CheckDevicePath;\r
996\r
997 CheckDevicePath = DevicePath;\r
998 while (!IsDevicePathEnd (CheckDevicePath)) {\r
999 //\r
1000 // Check device whether is hot plug device or not throught Device Path\r
1001 // \r
1002 if ((DevicePathType (CheckDevicePath) == MESSAGING_DEVICE_PATH) &&\r
1003 (DevicePathSubType (CheckDevicePath) == MSG_USB_DP ||\r
1004 DevicePathSubType (CheckDevicePath) == MSG_USB_CLASS_DP ||\r
1005 DevicePathSubType (CheckDevicePath) == MSG_USB_WWID_DP)) {\r
1006 //\r
1007 // If Device is USB device\r
1008 //\r
1009 return TRUE;\r
1010 }\r
1011 if ((DevicePathType (CheckDevicePath) == HARDWARE_DEVICE_PATH) &&\r
1012 (DevicePathSubType (CheckDevicePath) == HW_PCCARD_DP)) {\r
1013 //\r
1014 // If Device is PCCard\r
1015 //\r
1016 return TRUE;\r
1017 }\r
1018 \r
1019 CheckDevicePath = NextDevicePathNode (CheckDevicePath);\r
1020 }\r
1021\r
1022 return FALSE;\r
1023}\r
1024\r