]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Console/ConPlatformDxe/ConPlatform.c
Update ConPlatform to ensure console dev variable is correct.
[mirror_edk2.git] / MdeModulePkg / Universal / Console / ConPlatformDxe / ConPlatform.c
CommitLineData
fb0b259e 1/** @file\r
dfe687ca 2 Console Platform DXE Driver, install Console Device Guids and update Console\r
9359e53f 3 Environment Variables.\r
95276127 4\r
9359e53f 5Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
95276127 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
fb0b259e 14**/\r
95276127 15\r
0ad02bbd 16#include "ConPlatform.h"\r
95276127 17\r
95276127 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
c5ed97f6 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
95276127 42\r
7b9ff698 43 @param ImageHandle The firmware allocated handle for the EFI image.\r
44 @param SystemTable A pointer to the EFI System Table.\r
c62a593c 45\r
95276127 46 @retval EFI_SUCCESS The entry point is executed successfully.\r
95276127 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
5bca971e 58 Status = EfiLibInstallDriverBindingComponentName2 (\r
95276127 59 ImageHandle,\r
60 SystemTable,\r
61 &gConPlatformTextInDriverBinding,\r
62 ImageHandle,\r
63 &gConPlatformComponentName,\r
5bca971e 64 &gConPlatformComponentName2\r
95276127 65 );\r
66 ASSERT_EFI_ERROR (Status);\r
67\r
5bca971e 68 Status = EfiLibInstallDriverBindingComponentName2 (\r
95276127 69 ImageHandle,\r
70 SystemTable,\r
71 &gConPlatformTextOutDriverBinding,\r
72 NULL,\r
73 &gConPlatformComponentName,\r
5bca971e 74 &gConPlatformComponentName2\r
95276127 75 );\r
76 ASSERT_EFI_ERROR (Status);\r
77\r
c5ed97f6 78 return EFI_SUCCESS;\r
95276127 79}\r
80\r
81\r
9359e53f 82/**\r
c5ed97f6 83 Test to see if EFI_SIMPLE_TEXT_INPUT_PROTOCOL is supported on ControllerHandle. \r
9359e53f 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
7b9ff698 90 @retval EFI_SUCCESS This driver supports this device.\r
91 @retval other This driver does not support this device.\r
9359e53f 92\r
93**/\r
95276127 94EFI_STATUS\r
95EFIAPI\r
96ConPlatformTextInDriverBindingSupported (\r
97 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
98 IN EFI_HANDLE ControllerHandle,\r
c5ed97f6 99 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
95276127 100 )\r
95276127 101{\r
102 return ConPlatformDriverBindingSupported (\r
c5ed97f6 103 This,\r
104 ControllerHandle,\r
105 &gEfiSimpleTextInProtocolGuid\r
106 );\r
95276127 107}\r
108\r
9359e53f 109\r
110/**\r
c5ed97f6 111 Test to see if EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is supported on ControllerHandle. \r
9359e53f 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
7b9ff698 118 @retval EFI_SUCCESS This driver supports this device.\r
119 @retval other This driver does not support this device.\r
9359e53f 120\r
121**/\r
95276127 122EFI_STATUS\r
123EFIAPI\r
124ConPlatformTextOutDriverBindingSupported (\r
125 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
126 IN EFI_HANDLE ControllerHandle,\r
c5ed97f6 127 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
95276127 128 )\r
95276127 129{\r
130 return ConPlatformDriverBindingSupported (\r
c5ed97f6 131 This,\r
132 ControllerHandle,\r
133 &gEfiSimpleTextOutProtocolGuid\r
134 );\r
95276127 135}\r
136\r
9359e53f 137\r
138/**\r
c5ed97f6 139 Test to see if the specified protocol is supported on ControllerHandle. \r
9359e53f 140\r
141 @param This Protocol instance pointer.\r
142 @param ControllerHandle Handle of device to test.\r
9359e53f 143 @param ProtocolGuid The specfic protocol.\r
144\r
7b9ff698 145 @retval EFI_SUCCESS This driver supports this device.\r
146 @retval other This driver does not support this device.\r
9359e53f 147\r
148**/\r
95276127 149EFI_STATUS\r
150ConPlatformDriverBindingSupported (\r
151 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
152 IN EFI_HANDLE ControllerHandle,\r
95276127 153 IN EFI_GUID *ProtocolGuid\r
154 )\r
95276127 155{\r
156 EFI_STATUS Status;\r
157 VOID *Interface;\r
158\r
159 //\r
7b9ff698 160 // Test to see if this is a physical device by checking if\r
161 // it has a Device Path Protocol.\r
95276127 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
7b9ff698 175 // Test to see if this device supports the specified Protocol.\r
95276127 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
c5ed97f6 190 ControllerHandle,\r
191 ProtocolGuid,\r
192 This->DriverBindingHandle,\r
193 ControllerHandle\r
194 );\r
95276127 195\r
196 return EFI_SUCCESS;\r
197}\r
198\r
9359e53f 199/**\r
c5ed97f6 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
24248368 203 reading Device Path, and installing Console In Devcice GUID on ControllerHandle.\r
9359e53f 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
415df2a3 215 @retval other This driver does not support this device.\r
9359e53f 216\r
217**/\r
95276127 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
95276127 225{\r
c5ed97f6 226 EFI_STATUS Status;\r
227 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
95276127 228 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *TextIn;\r
ca6b86ef 229 BOOLEAN IsInConInVariable;\r
95276127 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
c5ed97f6 246 // Open the Simple Text Input Protocol BY_DRIVER\r
95276127 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
ca6b86ef 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
95276127 272 //\r
f1aec6cc 273 // Check the device path, if it is a hot plug device,\r
95276127 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
f1aec6cc 278 if (IsHotPlugDevice (DevicePath)) {\r
95276127 279 gBS->InstallMultipleProtocolInterfaces (\r
c5ed97f6 280 &ControllerHandle,\r
281 &gEfiConsoleInDeviceGuid,\r
282 NULL,\r
283 NULL\r
284 );\r
ca6b86ef 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
95276127 295 } else {\r
296 //\r
c5ed97f6 297 // If it is not a hot-plug device, append the device path to the\r
298 // ConInDev environment variable\r
95276127 299 //\r
300 ConPlatformUpdateDeviceVariable (\r
ff37790d 301 L"ConInDev",\r
95276127 302 DevicePath,\r
303 APPEND\r
304 );\r
305\r
306 //\r
9fdeb098 307 // If the device path is an instance in the ConIn environment variable,\r
95276127 308 // then install EfiConsoleInDeviceGuid onto ControllerHandle\r
309 //\r
ca6b86ef 310 if (IsInConInVariable) {\r
95276127 311 gBS->InstallMultipleProtocolInterfaces (\r
c5ed97f6 312 &ControllerHandle,\r
313 &gEfiConsoleInDeviceGuid,\r
314 NULL,\r
315 NULL\r
316 );\r
95276127 317 } else {\r
318 gBS->CloseProtocol (\r
c5ed97f6 319 ControllerHandle,\r
320 &gEfiSimpleTextInProtocolGuid,\r
321 This->DriverBindingHandle,\r
322 ControllerHandle\r
323 );\r
95276127 324 }\r
325 }\r
326\r
327 return EFI_SUCCESS;\r
328}\r
329\r
9359e53f 330/**\r
c5ed97f6 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
24248368 334 reading Device Path, and installing Console Out Devcic GUID, Standard Error\r
9359e53f 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
7772b176 338 console environment variables ConOutDev, ErrOutDev.\r
9359e53f 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
95276127 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
95276127 357{\r
c5ed97f6 358 EFI_STATUS Status;\r
359 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
95276127 360 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *TextOut;\r
c5ed97f6 361 BOOLEAN NeedClose;\r
ca6b86ef 362 BOOLEAN IsInConOutVariable;\r
363 BOOLEAN IsInErrOutVariable;\r
95276127 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
ca6b86ef 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
95276127 418 //\r
f1aec6cc 419 // Check the device path, if it is a hot plug device,\r
7772b176 420 // do not put the device path into ConOutDev and ErrOutDev,\r
95276127 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
f1aec6cc 424 if (IsHotPlugDevice (DevicePath)) {\r
95276127 425 gBS->InstallMultipleProtocolInterfaces (\r
c5ed97f6 426 &ControllerHandle,\r
427 &gEfiConsoleOutDeviceGuid,\r
428 NULL,\r
429 NULL\r
430 );\r
ca6b86ef 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
95276127 451 } else {\r
452 //\r
c5ed97f6 453 // If it is not a hot-plug device, first append the device path to the\r
454 // ConOutDev environment variable\r
95276127 455 //\r
456 ConPlatformUpdateDeviceVariable (\r
ff37790d 457 L"ConOutDev",\r
95276127 458 DevicePath,\r
459 APPEND\r
460 );\r
461 //\r
7772b176 462 // Then append the device path to the ErrOutDev environment variable\r
95276127 463 //\r
464 ConPlatformUpdateDeviceVariable (\r
ff37790d 465 L"ErrOutDev",\r
95276127 466 DevicePath,\r
467 APPEND\r
468 );\r
469\r
470 //\r
9fdeb098 471 // If the device path is an instance in the ConOut environment variable,\r
95276127 472 // then install EfiConsoleOutDeviceGuid onto ControllerHandle\r
473 //\r
ca6b86ef 474 if (IsInConOutVariable) {\r
95276127 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
9fdeb098 484 // If the device path is an instance in the ErrOut environment variable,\r
95276127 485 // then install EfiStandardErrorDeviceGuid onto ControllerHandle\r
486 //\r
ca6b86ef 487 if (IsInErrOutVariable) {\r
95276127 488 NeedClose = FALSE;\r
489 gBS->InstallMultipleProtocolInterfaces (\r
c5ed97f6 490 &ControllerHandle,\r
491 &gEfiStandardErrorDeviceGuid,\r
492 NULL,\r
493 NULL\r
494 );\r
95276127 495 }\r
496\r
497 if (NeedClose) {\r
498 gBS->CloseProtocol (\r
c5ed97f6 499 ControllerHandle,\r
500 &gEfiSimpleTextOutProtocolGuid,\r
501 This->DriverBindingHandle,\r
502 ControllerHandle\r
503 );\r
95276127 504 }\r
505 }\r
506\r
507 return EFI_SUCCESS;\r
508}\r
509\r
9359e53f 510/**\r
24248368 511 Stop this driver on ControllerHandle by removing Console In Devcice GUID \r
c5ed97f6 512 and closing the Simple Text Input protocol on ControllerHandle.\r
9359e53f 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
95276127 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
95276127 532{\r
533 EFI_STATUS Status;\r
534 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
535\r
536 //\r
f1aec6cc 537 // Get the Device Path Protocol firstly\r
95276127 538 //\r
f1aec6cc 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
95276127 551 //\r
ca6b86ef 552 // Remove DevicePath from ConInDev if exists.\r
4986c561 553 //\r
ca6b86ef 554 ConPlatformUpdateDeviceVariable (\r
555 L"ConInDev",\r
556 DevicePath,\r
557 DELETE\r
558 );\r
95276127 559 }\r
f1aec6cc 560\r
95276127 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
c5ed97f6 571 // Close the Simple Text Input Protocol\r
95276127 572 //\r
573 gBS->CloseProtocol (\r
ca6b86ef 574 ControllerHandle,\r
575 &gEfiSimpleTextInProtocolGuid,\r
576 This->DriverBindingHandle,\r
577 ControllerHandle\r
578 );\r
95276127 579\r
580 return EFI_SUCCESS;\r
581}\r
582\r
9359e53f 583\r
584/**\r
24248368 585 Stop this driver on ControllerHandle by removing Console Out Devcice GUID \r
c5ed97f6 586 and closing the Simple Text Output protocol on ControllerHandle.\r
9359e53f 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
95276127 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
95276127 606{\r
607 EFI_STATUS Status;\r
608 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
609\r
610 //\r
f1aec6cc 611 // Get the Device Path Protocol firstly\r
95276127 612 //\r
f1aec6cc 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
95276127 622 //\r
ca6b86ef 623 // Remove DevicePath from ConOutDev and ErrOutDev if exists.\r
95276127 624 //\r
ca6b86ef 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
95276127 635 }\r
f1aec6cc 636\r
95276127 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
9359e53f 666/**\r
7b9ff698 667 Uninstall the specified protocol.\r
9359e53f 668\r
669 @param This Protocol instance pointer.\r
7b9ff698 670 @param Handle Handle of device to uninstall protocol on.\r
671 @param ProtocolGuid The specified protocol need to be uninstalled.\r
9359e53f 672\r
9359e53f 673**/\r
95276127 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
c5ed97f6 694 Handle,\r
695 ProtocolGuid,\r
696 NULL,\r
697 NULL\r
698 );\r
95276127 699 }\r
700\r
701 return ;\r
702}\r
703\r
9359e53f 704/**\r
7772b176 705 Get the necessary size of buffer and read the variable.\r
c5ed97f6 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
95276127 710\r
9359e53f 711 @param Name String part of EFI variable name\r
95276127 712\r
9359e53f 713 @return Dynamically allocated memory that contains a copy of the EFI variable.\r
7b9ff698 714 Caller is repsoncible freeing the buffer. Return NULL means Variable \r
715 was not read.\r
95276127 716\r
9359e53f 717**/\r
718VOID *\r
719ConPlatformGetVariable (\r
720 IN CHAR16 *Name\r
721 )\r
95276127 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
c5ed97f6 731 // Test to see if the variable exists. If it doesn't, return NULL.\r
95276127 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
7b9ff698 761 //\r
762 // To make sure Buffer is NULL if any error occurs.\r
763 //\r
95276127 764 Buffer = NULL;\r
765 }\r
766 }\r
767\r
768 return Buffer;\r
769}\r
770\r
9359e53f 771/**\r
95276127 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
9359e53f 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
95276127 783\r
c5ed97f6 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
95276127 789\r
9359e53f 790**/\r
791EFI_STATUS\r
792ConPlatformMatchDevicePaths (\r
793 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
794 IN EFI_DEVICE_PATH_PROTOCOL *Single,\r
7b9ff698 795 OUT EFI_DEVICE_PATH_PROTOCOL **NewDevicePath OPTIONAL,\r
9359e53f 796 IN BOOLEAN Delete\r
797 )\r
95276127 798{\r
799 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
7b9ff698 800 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath1;\r
801 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath2;\r
95276127 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
9359e53f 808 if ((Multi == NULL) || (Single == NULL)) {\r
c5ed97f6 809 return EFI_INVALID_PARAMETER;\r
95276127 810 }\r
7b9ff698 811\r
95276127 812 //\r
7b9ff698 813 // If performing Delete operation, the NewDevicePath must not be NULL.\r
95276127 814 //\r
7b9ff698 815 if (Delete) {\r
c5ed97f6 816 if (NewDevicePath == NULL) {\r
817 return EFI_INVALID_PARAMETER;\r
818 }\r
7b9ff698 819 }\r
820\r
821 TempDevicePath1 = NULL;\r
95276127 822\r
823 DevicePath = Multi;\r
824 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
825\r
826 //\r
7b9ff698 827 // Search for the match of 'Single' in 'Multi'\r
95276127 828 //\r
9359e53f 829 while (DevicePathInst != NULL) {\r
95276127 830 if (CompareMem (Single, DevicePathInst, Size) == 0) {\r
831 if (!Delete) {\r
7b9ff698 832 //\r
833 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.\r
834 //\r
95276127 835 FreePool (DevicePathInst);\r
836 return EFI_SUCCESS;\r
837 }\r
838 } else {\r
839 if (Delete) {\r
7b9ff698 840 //\r
c5ed97f6 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
7b9ff698 843 //\r
844 TempDevicePath2 = AppendDevicePathInstance (\r
845 TempDevicePath1,\r
95276127 846 DevicePathInst\r
847 );\r
676df92c 848 if (TempDevicePath1 != NULL) {\r
849 FreePool (TempDevicePath1);\r
850 }\r
7b9ff698 851 TempDevicePath1 = TempDevicePath2;\r
95276127 852 }\r
853 }\r
854\r
855 FreePool (DevicePathInst);\r
856 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
857 }\r
858\r
859 if (Delete) {\r
7b9ff698 860 //\r
c5ed97f6 861 // Return the new device path data structure with specified node deleted.\r
7b9ff698 862 //\r
863 *NewDevicePath = TempDevicePath1;\r
95276127 864 return EFI_SUCCESS;\r
865 }\r
866\r
867 return EFI_NOT_FOUND;\r
868}\r
869\r
9359e53f 870/**\r
7b9ff698 871 Update console environment variables. \r
9359e53f 872\r
873 @param VariableName Console environment variables, ConOutDev, ConInDev\r
7772b176 874 ErrOutDev, ConIn ,ConOut or ErrOut.\r
9359e53f 875 @param DevicePath Console devcie's device path.\r
c5ed97f6 876 @param Operation Variable operations, including APPEND, CHECK and DELETE.\r
9359e53f 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
95276127 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
95276127 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
7b9ff698 905 //\r
906 // Match specified DevicePath in Console Variable.\r
907 // \r
95276127 908 Status = ConPlatformMatchDevicePaths (\r
c5ed97f6 909 VariableDevicePath,\r
910 DevicePath,\r
911 NULL,\r
912 FALSE\r
913 );\r
95276127 914\r
915 if ((Operation == CHECK) || (!EFI_ERROR (Status))) {\r
916 //\r
c5ed97f6 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
95276127 920 //\r
921 if (VariableDevicePath != NULL) {\r
922 FreePool (VariableDevicePath);\r
923 }\r
924\r
925 return Status;\r
926 }\r
927 //\r
c5ed97f6 928 // We reach here to append a device path that does not exist in variable.\r
95276127 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
c5ed97f6 941 // We reach here to remove DevicePath from the environment variable that\r
95276127 942 // is a multi-instance device path.\r
943 //\r
944 Status = ConPlatformMatchDevicePaths (\r
c5ed97f6 945 VariableDevicePath,\r
946 DevicePath,\r
947 &NewVariableDevicePath,\r
948 TRUE\r
949 );\r
95276127 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
c62a593c 960 if (NewVariableDevicePath != NULL) {\r
7b9ff698 961 //\r
962 // Update Console Environment Variable.\r
963 //\r
c62a593c 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
95276127 971\r
c62a593c 972 FreePool (NewVariableDevicePath);\r
973 }\r
95276127 974\r
975 return Status;\r
976}\r
977\r
9359e53f 978/**\r
f1aec6cc 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
9359e53f 983\r
f1aec6cc 984 @param DevicePath Pointer to device's device path.\r
9359e53f 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
95276127 990BOOLEAN\r
991IsHotPlugDevice (\r
f1aec6cc 992 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
95276127 993 )\r
994{\r
f1aec6cc 995 EFI_DEVICE_PATH_PROTOCOL *CheckDevicePath;\r
aa8f4f55 996\r
f1aec6cc 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
95276127 1020 }\r
1021\r
f1aec6cc 1022 return FALSE;\r
95276127 1023}\r
f1aec6cc 1024\r