]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Console/TerminalDxe/Terminal.c
MdeModulePkg/TerminalDxe: Avoid always append device path to *Dev
[mirror_edk2.git] / MdeModulePkg / Universal / Console / TerminalDxe / Terminal.c
CommitLineData
fb0b259e 1/** @file\r
11baadb6 2 Produces Simple Text Input Protocol, Simple Text Input Extended Protocol and\r
fb0b259e 3 Simple Text Output Protocol upon Serial IO Protocol.\r
95276127 4\r
8ce87fff 5Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 6This program and the accompanying materials\r
95276127 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
16\r
95276127 17#include "Terminal.h"\r
18\r
95276127 19//\r
20// Globals\r
21//\r
22EFI_DRIVER_BINDING_PROTOCOL gTerminalDriverBinding = {\r
23 TerminalDriverBindingSupported,\r
24 TerminalDriverBindingStart,\r
25 TerminalDriverBindingStop,\r
26 0xa,\r
27 NULL,\r
28 NULL\r
29};\r
30\r
31\r
f9163275 32EFI_GUID *mTerminalType[] = {\r
6b88ceec
A
33 &gEfiPcAnsiGuid,\r
34 &gEfiVT100Guid,\r
35 &gEfiVT100PlusGuid,\r
6e3227c8
RF
36 &gEfiVTUTF8Guid,\r
37 &gEfiTtyTermGuid\r
6b88ceec
A
38};\r
39\r
40\r
fa3f99e5
RN
41CHAR16 *mSerialConsoleNames[] = {\r
42 L"PC-ANSI Serial Console",\r
43 L"VT-100 Serial Console",\r
44 L"VT-100+ Serial Console",\r
45 L"VT-UTF8 Serial Console",\r
46 L"Tty Terminal Serial Console"\r
47};\r
48\r
204ba917 49TERMINAL_DEV mTerminalDevTemplate = {\r
6b88ceec
A
50 TERMINAL_DEV_SIGNATURE,\r
51 NULL,\r
52 0,\r
53 NULL,\r
54 NULL,\r
55 { // SimpleTextInput\r
56 TerminalConInReset,\r
57 TerminalConInReadKeyStroke,\r
58 NULL\r
59 },\r
60 { // SimpleTextOutput\r
61 TerminalConOutReset,\r
62 TerminalConOutOutputString,\r
63 TerminalConOutTestString,\r
64 TerminalConOutQueryMode,\r
65 TerminalConOutSetMode,\r
66 TerminalConOutSetAttribute,\r
67 TerminalConOutClearScreen,\r
68 TerminalConOutSetCursorPosition,\r
69 TerminalConOutEnableCursor,\r
70 NULL\r
71 },\r
72 { // SimpleTextOutputMode\r
73 1, // MaxMode\r
11baadb6 74 0, // Mode\r
6b88ceec
A
75 EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK), // Attribute\r
76 0, // CursorColumn\r
77 0, // CursorRow\r
78 TRUE // CursorVisible\r
79 },\r
79d07c66 80 NULL, // TerminalConsoleModeData\r
11baadb6 81 0, // SerialInTimeOut\r
5c998646 82\r
83 NULL, // RawFifo\r
84 NULL, // UnicodeFiFo\r
85 NULL, // EfiKeyFiFo\r
47b612db 86 NULL, // EfiKeyFiFoForNotify\r
5c998646 87\r
6b88ceec 88 NULL, // ControllerNameTable\r
f0368006 89 NULL, // TimerEvent\r
11baadb6 90 NULL, // TwoSecondTimeOut\r
6b88ceec
A
91 INPUT_STATE_DEFAULT,\r
92 RESET_STATE_DEFAULT,\r
014f93ac
RF
93 {\r
94 0,\r
95 0,\r
96 0\r
97 },\r
98 0,\r
66aa04e4 99 FALSE,\r
100 { // SimpleTextInputEx\r
101 TerminalConInResetEx,\r
102 TerminalConInReadKeyStrokeEx,\r
103 NULL,\r
104 TerminalConInSetState,\r
105 TerminalConInRegisterKeyNotify,\r
106 TerminalConInUnregisterKeyNotify,\r
107 },\r
11baadb6 108 { // NotifyList\r
66aa04e4 109 NULL,\r
110 NULL,\r
47b612db
SZ
111 },\r
112 NULL // KeyNotifyProcessEvent\r
6b88ceec
A
113};\r
114\r
79d07c66 115TERMINAL_CONSOLE_MODE_DATA mTerminalConsoleModeData[] = {\r
390b95a4
RN
116 {80, 25},\r
117 {80, 50},\r
79d07c66 118 {100, 31},\r
119 //\r
120 // New modes can be added here.\r
79d07c66 121 //\r
79d07c66 122};\r
123\r
f9163275
RN
124/**\r
125 Convert the GUID representation of terminal type to enum type.\r
126\r
127 @param Guid The GUID representation of terminal type.\r
128\r
129 @return The terminal type in enum type.\r
130**/\r
131TERMINAL_TYPE\r
132TerminalTypeFromGuid (\r
133 IN EFI_GUID *Guid\r
134)\r
135{\r
136 TERMINAL_TYPE Type;\r
137\r
138 for (Type = 0; Type < ARRAY_SIZE (mTerminalType); Type++) {\r
139 if (CompareGuid (Guid, mTerminalType[Type])) {\r
140 break;\r
141 }\r
142 }\r
143 return Type;\r
144}\r
145\r
8fd98315 146/**\r
677fdb90 147 Test to see if this driver supports Controller.\r
8fd98315 148\r
149 @param This Protocol instance pointer.\r
ab76200c 150 @param Controller Handle of device to test\r
8fd98315 151 @param RemainingDevicePath Optional parameter use to pick a specific child\r
152 device to start.\r
153\r
ab76200c 154 @retval EFI_SUCCESS This driver supports this device.\r
155 @retval EFI_ALREADY_STARTED This driver is already running on this device.\r
156 @retval other This driver does not support this device.\r
6b88ceec 157\r
8fd98315 158**/\r
95276127 159EFI_STATUS\r
160EFIAPI\r
161TerminalDriverBindingSupported (\r
162 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
163 IN EFI_HANDLE Controller,\r
164 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
165 )\r
166{\r
167 EFI_STATUS Status;\r
168 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
169 EFI_SERIAL_IO_PROTOCOL *SerialIo;\r
170 VENDOR_DEVICE_PATH *Node;\r
171\r
172 //\r
173 // If remaining device path is not NULL, then make sure it is a\r
174 // device path that describes a terminal communications protocol.\r
175 //\r
176 if (RemainingDevicePath != NULL) {\r
95276127 177 //\r
e3dcffcc 178 // Check if RemainingDevicePath is the End of Device Path Node,\r
af4a6385 179 // if yes, go on checking other conditions\r
95276127 180 //\r
af4a6385 181 if (!IsDevicePathEnd (RemainingDevicePath)) {\r
182 //\r
183 // If RemainingDevicePath isn't the End of Device Path Node,\r
184 // check its validation\r
185 //\r
186 Node = (VENDOR_DEVICE_PATH *) RemainingDevicePath;\r
e3dcffcc 187\r
af4a6385 188 if (Node->Header.Type != MESSAGING_DEVICE_PATH ||\r
189 Node->Header.SubType != MSG_VENDOR_DP ||\r
190 DevicePathNodeLength(&Node->Header) != sizeof(VENDOR_DEVICE_PATH)) {\r
e3dcffcc 191\r
af4a6385 192 return EFI_UNSUPPORTED;\r
e3dcffcc 193\r
af4a6385 194 }\r
195 //\r
6e3227c8 196 // only supports PC ANSI, VT100, VT100+, VT-UTF8, and TtyTerm terminal types\r
af4a6385 197 //\r
f9163275 198 if (TerminalTypeFromGuid (&Node->Guid) == ARRAY_SIZE (mTerminalType)) {\r
af4a6385 199 return EFI_UNSUPPORTED;\r
200 }\r
95276127 201 }\r
202 }\r
203 //\r
204 // Open the IO Abstraction(s) needed to perform the supported test\r
af4a6385 205 // The Controller must support the Serial I/O Protocol.\r
206 // This driver is a bus driver with at most 1 child device, so it is\r
207 // ok for it to be already started.\r
95276127 208 //\r
209 Status = gBS->OpenProtocol (\r
210 Controller,\r
af4a6385 211 &gEfiSerialIoProtocolGuid,\r
212 (VOID **) &SerialIo,\r
95276127 213 This->DriverBindingHandle,\r
214 Controller,\r
215 EFI_OPEN_PROTOCOL_BY_DRIVER\r
216 );\r
217 if (Status == EFI_ALREADY_STARTED) {\r
218 return EFI_SUCCESS;\r
219 }\r
220\r
221 if (EFI_ERROR (Status)) {\r
222 return Status;\r
223 }\r
224\r
af4a6385 225 //\r
226 // Close the I/O Abstraction(s) used to perform the supported test\r
227 //\r
95276127 228 gBS->CloseProtocol (\r
229 Controller,\r
af4a6385 230 &gEfiSerialIoProtocolGuid,\r
95276127 231 This->DriverBindingHandle,\r
232 Controller\r
233 );\r
234\r
235 //\r
af4a6385 236 // Open the EFI Device Path protocol needed to perform the supported test\r
95276127 237 //\r
238 Status = gBS->OpenProtocol (\r
239 Controller,\r
af4a6385 240 &gEfiDevicePathProtocolGuid,\r
241 (VOID **) &ParentDevicePath,\r
95276127 242 This->DriverBindingHandle,\r
243 Controller,\r
244 EFI_OPEN_PROTOCOL_BY_DRIVER\r
245 );\r
246 if (Status == EFI_ALREADY_STARTED) {\r
247 return EFI_SUCCESS;\r
248 }\r
249\r
250 if (EFI_ERROR (Status)) {\r
251 return Status;\r
252 }\r
af4a6385 253\r
95276127 254 //\r
af4a6385 255 // Close protocol, don't use device path protocol in the Support() function\r
95276127 256 //\r
257 gBS->CloseProtocol (\r
258 Controller,\r
af4a6385 259 &gEfiDevicePathProtocolGuid,\r
95276127 260 This->DriverBindingHandle,\r
261 Controller\r
262 );\r
263\r
264 return Status;\r
265}\r
266\r
e3dcffcc 267\r
268/**\r
269 Free notify functions list.\r
270\r
271 @param ListHead The list head\r
272\r
273 @retval EFI_SUCCESS Free the notify list successfully.\r
274 @retval EFI_INVALID_PARAMETER ListHead is NULL.\r
275\r
276**/\r
277EFI_STATUS\r
278TerminalFreeNotifyList (\r
279 IN OUT LIST_ENTRY *ListHead\r
280 )\r
281{\r
282 TERMINAL_CONSOLE_IN_EX_NOTIFY *NotifyNode;\r
283\r
284 if (ListHead == NULL) {\r
285 return EFI_INVALID_PARAMETER;\r
286 }\r
287 while (!IsListEmpty (ListHead)) {\r
288 NotifyNode = CR (\r
289 ListHead->ForwardLink,\r
290 TERMINAL_CONSOLE_IN_EX_NOTIFY,\r
291 NotifyEntry,\r
292 TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
293 );\r
294 RemoveEntryList (ListHead->ForwardLink);\r
295 FreePool (NotifyNode);\r
296 }\r
297\r
298 return EFI_SUCCESS;\r
299}\r
300\r
79d07c66 301/**\r
302 Initialize all the text modes which the terminal console supports.\r
303\r
304 It returns information for available text modes that the terminal can support.\r
305\r
306 @param[out] TextModeCount The total number of text modes that terminal console supports.\r
79d07c66 307\r
390b95a4
RN
308 @return The buffer to the text modes column and row information.\r
309 Caller is responsible to free it when it's non-NULL.\r
79d07c66 310\r
311**/\r
390b95a4 312TERMINAL_CONSOLE_MODE_DATA *\r
79d07c66 313InitializeTerminalConsoleTextMode (\r
390b95a4
RN
314 OUT INT32 *TextModeCount\r
315)\r
79d07c66 316{\r
390b95a4
RN
317 TERMINAL_CONSOLE_MODE_DATA *TextModeData;\r
318\r
319 ASSERT (TextModeCount != NULL);\r
320\r
390b95a4
RN
321 TextModeData = AllocateCopyPool (sizeof (mTerminalConsoleModeData), mTerminalConsoleModeData);\r
322 if (TextModeData == NULL) {\r
323 return NULL;\r
79d07c66 324 }\r
390b95a4
RN
325 *TextModeCount = ARRAY_SIZE (mTerminalConsoleModeData);\r
326\r
79d07c66 327 DEBUG_CODE (\r
390b95a4
RN
328 INT32 Index;\r
329 for (Index = 0; Index < *TextModeCount; Index++) {\r
330 DEBUG ((DEBUG_INFO, "Terminal - Mode %d, Column = %d, Row = %d\n",\r
331 Index, TextModeData[Index].Columns, TextModeData[Index].Rows));\r
79d07c66 332 }\r
333 );\r
390b95a4 334 return TextModeData;\r
79d07c66 335}\r
e3dcffcc 336\r
b7cf1c07
RN
337/**\r
338 Stop the terminal state machine.\r
339\r
340 @param TerminalDevice The terminal device.\r
341**/\r
342VOID\r
343StopTerminalStateMachine (\r
344 TERMINAL_DEV *TerminalDevice\r
345 )\r
346{\r
347 EFI_TPL OriginalTpl;\r
348\r
349 OriginalTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
350\r
351 gBS->CloseEvent (TerminalDevice->TimerEvent);\r
352 gBS->CloseEvent (TerminalDevice->TwoSecondTimeOut);\r
353\r
354 gBS->RestoreTPL (OriginalTpl);\r
355}\r
356\r
357/**\r
358 Start the terminal state machine.\r
359\r
360 @param TerminalDevice The terminal device.\r
361**/\r
362VOID\r
363StartTerminalStateMachine (\r
364 TERMINAL_DEV *TerminalDevice\r
365 )\r
366{\r
367 EFI_STATUS Status;\r
368 Status = gBS->CreateEvent (\r
369 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
370 TPL_NOTIFY,\r
371 TerminalConInTimerHandler,\r
372 TerminalDevice,\r
373 &TerminalDevice->TimerEvent\r
374 );\r
375 ASSERT_EFI_ERROR (Status);\r
376\r
377 Status = gBS->SetTimer (\r
378 TerminalDevice->TimerEvent,\r
379 TimerPeriodic,\r
380 KEYBOARD_TIMER_INTERVAL\r
381 );\r
382 ASSERT_EFI_ERROR (Status);\r
383\r
384 Status = gBS->CreateEvent (\r
385 EVT_TIMER,\r
386 TPL_CALLBACK,\r
387 NULL,\r
388 NULL,\r
389 &TerminalDevice->TwoSecondTimeOut\r
390 );\r
391 ASSERT_EFI_ERROR (Status);\r
392}\r
393\r
fa3f99e5
RN
394/**\r
395 Initialize the controller name table.\r
396\r
397 @param TerminalType The terminal type.\r
398 @param ControllerNameTable The controller name table.\r
399\r
400 @retval EFI_SUCCESS The controller name table is initialized successfully.\r
401 @retval others Return status of AddUnicodeString2 ().\r
402**/\r
403EFI_STATUS\r
404InitializeControllerNameTable (\r
405 TERMINAL_TYPE TerminalType,\r
406 EFI_UNICODE_STRING_TABLE **ControllerNameTable\r
407)\r
408{\r
409 EFI_STATUS Status;\r
410 EFI_UNICODE_STRING_TABLE *Table;\r
411\r
412 ASSERT (TerminalType < ARRAY_SIZE (mTerminalType));\r
413 Table = NULL;\r
414 Status = AddUnicodeString2 (\r
415 "eng",\r
416 gTerminalComponentName.SupportedLanguages,\r
417 &Table,\r
418 mSerialConsoleNames[TerminalType],\r
419 TRUE\r
420 );\r
421 if (!EFI_ERROR (Status)) {\r
422 Status = AddUnicodeString2 (\r
423 "en",\r
424 gTerminalComponentName2.SupportedLanguages,\r
425 &Table,\r
426 mSerialConsoleNames[TerminalType],\r
427 FALSE\r
428 );\r
429 if (EFI_ERROR (Status)) {\r
430 FreeUnicodeStringTable (Table);\r
431 }\r
432 }\r
433 if (!EFI_ERROR (Status)) {\r
434 *ControllerNameTable = Table;\r
435 }\r
436 return Status;\r
437}\r
438\r
e49ef433 439/**\r
ab76200c 440 Start this driver on Controller by opening a Serial IO protocol,\r
8fd98315 441 reading Device Path, and creating a child handle with a Simple Text In,\r
442 Simple Text In Ex and Simple Text Out protocol, and device path protocol.\r
443 And store Console Device Environment Variables.\r
e49ef433 444\r
8fd98315 445 @param This Protocol instance pointer.\r
ab76200c 446 @param Controller Handle of device to bind driver to\r
8fd98315 447 @param RemainingDevicePath Optional parameter use to pick a specific child\r
448 device to start.\r
e49ef433 449\r
ab76200c 450 @retval EFI_SUCCESS This driver is added to Controller.\r
451 @retval EFI_ALREADY_STARTED This driver is already running on Controller.\r
452 @retval other This driver does not support this device.\r
e49ef433 453\r
454**/\r
95276127 455EFI_STATUS\r
456EFIAPI\r
457TerminalDriverBindingStart (\r
458 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
459 IN EFI_HANDLE Controller,\r
460 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
461 )\r
95276127 462{\r
463 EFI_STATUS Status;\r
464 EFI_SERIAL_IO_PROTOCOL *SerialIo;\r
465 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
f7c11d9b
RN
466 EFI_DEVICE_PATH_PROTOCOL *Vendor;\r
467 EFI_HANDLE SerialIoHandle;\r
95276127 468 EFI_SERIAL_IO_MODE *Mode;\r
469 UINTN SerialInTimeOut;\r
470 TERMINAL_DEV *TerminalDevice;\r
f7c11d9b 471 UINT8 TerminalType;\r
95276127 472 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
473 UINTN EntryCount;\r
474 UINTN Index;\r
c4f9201e 475 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *SimpleTextOutput;\r
e3dcffcc 476 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *SimpleTextInput;\r
f7c11d9b
RN
477 EFI_UNICODE_STRING_TABLE *ControllerNameTable;\r
478\r
95276127 479 //\r
480 // Get the Device Path Protocol to build the device path of the child device\r
481 //\r
482 Status = gBS->OpenProtocol (\r
483 Controller,\r
484 &gEfiDevicePathProtocolGuid,\r
485 (VOID **) &ParentDevicePath,\r
486 This->DriverBindingHandle,\r
487 Controller,\r
488 EFI_OPEN_PROTOCOL_BY_DRIVER\r
489 );\r
f7c11d9b 490 ASSERT ((Status == EFI_SUCCESS) || (Status == EFI_ALREADY_STARTED));\r
95276127 491\r
492 //\r
493 // Open the Serial I/O Protocol BY_DRIVER. It might already be started.\r
494 //\r
495 Status = gBS->OpenProtocol (\r
496 Controller,\r
497 &gEfiSerialIoProtocolGuid,\r
498 (VOID **) &SerialIo,\r
499 This->DriverBindingHandle,\r
500 Controller,\r
501 EFI_OPEN_PROTOCOL_BY_DRIVER\r
502 );\r
f7c11d9b 503 ASSERT ((Status == EFI_SUCCESS) || (Status == EFI_ALREADY_STARTED));\r
95276127 504\r
f7c11d9b 505 if (!IsHotPlugDevice (ParentDevicePath)) {\r
95276127 506 //\r
f7c11d9b
RN
507 // if the serial device is a hot plug device, do not update the\r
508 // ConInDev, ConOutDev, and StdErrDev variables.\r
95276127 509 //\r
f7c11d9b
RN
510 TerminalUpdateConsoleDevVariable (EFI_CON_IN_DEV_VARIABLE_NAME, ParentDevicePath);\r
511 TerminalUpdateConsoleDevVariable (EFI_CON_OUT_DEV_VARIABLE_NAME, ParentDevicePath);\r
512 TerminalUpdateConsoleDevVariable (EFI_ERR_OUT_DEV_VARIABLE_NAME, ParentDevicePath);\r
95276127 513 }\r
e3dcffcc 514\r
95276127 515 //\r
f7c11d9b 516 // Do not create any child for END remaining device path.\r
95276127 517 //\r
f7c11d9b
RN
518 if ((RemainingDevicePath != NULL) && IsDevicePathEnd (RemainingDevicePath)) {\r
519 return EFI_SUCCESS;\r
520 }\r
95276127 521\r
f7c11d9b
RN
522 if (Status == EFI_ALREADY_STARTED) {\r
523\r
524 if (RemainingDevicePath == NULL) {\r
e3dcffcc 525 //\r
f7c11d9b 526 // If RemainingDevicePath is NULL or is the End of Device Path Node\r
e3dcffcc 527 //\r
f7c11d9b 528 return EFI_SUCCESS;\r
95276127 529 }\r
e3dcffcc 530\r
e3dcffcc 531 //\r
f7c11d9b
RN
532 // This driver can only produce one child per serial port.\r
533 // Change its terminal type as remaining device path requests.\r
e3dcffcc 534 //\r
535 Status = gBS->OpenProtocolInformation (\r
536 Controller,\r
537 &gEfiSerialIoProtocolGuid,\r
538 &OpenInfoBuffer,\r
539 &EntryCount\r
540 );\r
541 if (!EFI_ERROR (Status)) {\r
f7c11d9b 542 Status = EFI_NOT_FOUND;\r
e3dcffcc 543 for (Index = 0; Index < EntryCount; Index++) {\r
544 if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
f7c11d9b
RN
545 Status = gBS->OpenProtocol (\r
546 OpenInfoBuffer[Index].ControllerHandle,\r
547 &gEfiSimpleTextInProtocolGuid,\r
548 (VOID **) &SimpleTextInput,\r
549 This->DriverBindingHandle,\r
550 Controller,\r
551 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
552 );\r
553 if (!EFI_ERROR (Status)) {\r
554 TerminalDevice = TERMINAL_CON_IN_DEV_FROM_THIS (SimpleTextInput);\r
555 TerminalType = TerminalTypeFromGuid (&((VENDOR_DEVICE_PATH *) RemainingDevicePath)->Guid);\r
556 ASSERT (TerminalType < ARRAY_SIZE (mTerminalType));\r
557 if (TerminalDevice->TerminalType != TerminalType) {\r
558 Status = InitializeControllerNameTable (TerminalType, &ControllerNameTable);\r
559 if (!EFI_ERROR (Status)) {\r
560 StopTerminalStateMachine (TerminalDevice);\r
561 //\r
562 // Update the device path\r
563 //\r
564 Vendor = TerminalDevice->DevicePath;\r
565 Status = gBS->LocateDevicePath (&gEfiSerialIoProtocolGuid, &Vendor, &SerialIoHandle);\r
566 ASSERT_EFI_ERROR (Status);\r
567 CopyGuid (&((VENDOR_DEVICE_PATH *) Vendor)->Guid, mTerminalType[TerminalType]);\r
568 Status = gBS->ReinstallProtocolInterface (\r
569 TerminalDevice->Handle,\r
570 &gEfiDevicePathProtocolGuid,\r
571 TerminalDevice->DevicePath,\r
572 TerminalDevice->DevicePath\r
573 );\r
574 if (!EFI_ERROR (Status)) {\r
575 TerminalDevice->TerminalType = TerminalType;\r
576 StartTerminalStateMachine (TerminalDevice);\r
577 FreeUnicodeStringTable (TerminalDevice->ControllerNameTable);\r
578 TerminalDevice->ControllerNameTable = ControllerNameTable;\r
579 } else {\r
580 //\r
581 // Restore the device path on failure\r
582 //\r
583 CopyGuid (&((VENDOR_DEVICE_PATH *) Vendor)->Guid, mTerminalType[TerminalDevice->TerminalType]);\r
584 FreeUnicodeStringTable (ControllerNameTable);\r
585 }\r
586 }\r
587 }\r
588 }\r
589 break;\r
e3dcffcc 590 }\r
591 }\r
e3dcffcc 592 FreePool (OpenInfoBuffer);\r
e3dcffcc 593 }\r
f7c11d9b
RN
594 return Status;\r
595 }\r
e3dcffcc 596\r
f7c11d9b
RN
597 //\r
598 // Initialize the Terminal Dev\r
599 //\r
600 TerminalDevice = AllocateCopyPool (sizeof (TERMINAL_DEV), &mTerminalDevTemplate);\r
601 if (TerminalDevice == NULL) {\r
602 Status = EFI_OUT_OF_RESOURCES;\r
603 goto CloseProtocols;\r
604 }\r
390b95a4 605\r
f7c11d9b 606 if (RemainingDevicePath == NULL) {\r
e3dcffcc 607 //\r
f7c11d9b 608 // If RemainingDevicePath is NULL, use default terminal type\r
e3dcffcc 609 //\r
f7c11d9b
RN
610 TerminalDevice->TerminalType = PcdGet8 (PcdDefaultTerminalType);\r
611 } else {\r
e3dcffcc 612 //\r
f7c11d9b 613 // End of Device Path Node is handled in above.\r
e3dcffcc 614 //\r
f7c11d9b 615 ASSERT (!IsDevicePathEnd (RemainingDevicePath));\r
95276127 616 //\r
f7c11d9b
RN
617 // If RemainingDevicePath isn't the End of Device Path Node,\r
618 // Use the RemainingDevicePath to determine the terminal type\r
95276127 619 //\r
f7c11d9b
RN
620 TerminalDevice->TerminalType = TerminalTypeFromGuid (&((VENDOR_DEVICE_PATH *) RemainingDevicePath)->Guid);\r
621 }\r
622 ASSERT (TerminalDevice->TerminalType < ARRAY_SIZE (mTerminalType));\r
623 TerminalDevice->SerialIo = SerialIo;\r
abef1c7a 624\r
f7c11d9b
RN
625 //\r
626 // Build the component name for the child device\r
627 //\r
628 Status = InitializeControllerNameTable (TerminalDevice->TerminalType, &TerminalDevice->ControllerNameTable);\r
629 if (EFI_ERROR (Status)) {\r
630 goto FreeResources;\r
631 }\r
abef1c7a 632\r
f7c11d9b
RN
633 //\r
634 // Build the device path for the child device\r
635 //\r
636 Status = SetTerminalDevicePath (TerminalDevice->TerminalType, ParentDevicePath, &TerminalDevice->DevicePath);\r
637 if (EFI_ERROR (Status)) {\r
638 goto FreeResources;\r
639 }\r
abef1c7a 640\r
f7c11d9b
RN
641 InitializeListHead (&TerminalDevice->NotifyList);\r
642 Status = gBS->CreateEvent (\r
643 EVT_NOTIFY_WAIT,\r
644 TPL_NOTIFY,\r
645 TerminalConInWaitForKeyEx,\r
646 TerminalDevice,\r
647 &TerminalDevice->SimpleInputEx.WaitForKeyEx\r
648 );\r
649 ASSERT_EFI_ERROR (Status);\r
47b612db 650\r
f7c11d9b
RN
651 Status = gBS->CreateEvent (\r
652 EVT_NOTIFY_WAIT,\r
653 TPL_NOTIFY,\r
654 TerminalConInWaitForKey,\r
655 TerminalDevice,\r
656 &TerminalDevice->SimpleInput.WaitForKey\r
657 );\r
658 ASSERT_EFI_ERROR (Status);\r
659 Status = gBS->CreateEvent (\r
660 EVT_NOTIFY_SIGNAL,\r
661 TPL_CALLBACK,\r
662 KeyNotifyProcessHandler,\r
663 TerminalDevice,\r
664 &TerminalDevice->KeyNotifyProcessEvent\r
665 );\r
666 ASSERT_EFI_ERROR (Status);\r
95276127 667\r
f7c11d9b
RN
668 //\r
669 // Allocates and initializes the FIFO buffer to be zero, used for accommodating\r
670 // the pre-read pending characters.\r
671 //\r
672 TerminalDevice->RawFiFo = AllocateZeroPool (sizeof (RAW_DATA_FIFO));\r
673 if (TerminalDevice->RawFiFo == NULL) {\r
674 goto FreeResources;\r
675 }\r
676 TerminalDevice->UnicodeFiFo = AllocateZeroPool (sizeof (UNICODE_FIFO));\r
677 if (TerminalDevice->UnicodeFiFo == NULL) {\r
678 goto FreeResources;\r
679 }\r
680 TerminalDevice->EfiKeyFiFo = AllocateZeroPool (sizeof (EFI_KEY_FIFO));\r
681 if (TerminalDevice->EfiKeyFiFo == NULL) {\r
682 goto FreeResources;\r
683 }\r
684 TerminalDevice->EfiKeyFiFoForNotify = AllocateZeroPool (sizeof (EFI_KEY_FIFO));\r
685 if (TerminalDevice->EfiKeyFiFoForNotify == NULL) {\r
686 goto FreeResources;\r
95276127 687 }\r
95276127 688\r
3012ce5c 689 //\r
f7c11d9b 690 // Set the timeout value of serial buffer for keystroke response performance issue\r
3012ce5c 691 //\r
f7c11d9b 692 Mode = TerminalDevice->SerialIo->Mode;\r
3012ce5c 693\r
f7c11d9b
RN
694 SerialInTimeOut = 0;\r
695 if (Mode->BaudRate != 0) {\r
696 SerialInTimeOut = (1 + Mode->DataBits + Mode->StopBits) * 2 * 1000000 / (UINTN) Mode->BaudRate;\r
697 }\r
95276127 698\r
f7c11d9b
RN
699 Status = TerminalDevice->SerialIo->SetAttributes (\r
700 TerminalDevice->SerialIo,\r
701 Mode->BaudRate,\r
702 Mode->ReceiveFifoDepth,\r
703 (UINT32) SerialInTimeOut,\r
704 (EFI_PARITY_TYPE) (Mode->Parity),\r
705 (UINT8) Mode->DataBits,\r
706 (EFI_STOP_BITS_TYPE) (Mode->StopBits)\r
707 );\r
708 if (EFI_ERROR (Status)) {\r
709 //\r
710 // if set attributes operation fails, invalidate\r
711 // the value of SerialInTimeOut,thus make it\r
712 // inconsistent with the default timeout value\r
713 // of serial buffer. This will invoke the recalculation\r
714 // in the readkeystroke routine.\r
715 //\r
716 TerminalDevice->SerialInTimeOut = 0;\r
e3dcffcc 717 } else {\r
f7c11d9b 718 TerminalDevice->SerialInTimeOut = SerialInTimeOut;\r
95276127 719 }\r
720\r
f7c11d9b
RN
721 SimpleTextOutput = &TerminalDevice->SimpleTextOutput;\r
722 SimpleTextInput = &TerminalDevice->SimpleInput;\r
723\r
e3dcffcc 724 //\r
f7c11d9b 725 // Initialize SimpleTextOut instance\r
e3dcffcc 726 //\r
f7c11d9b
RN
727 SimpleTextOutput->Mode = &TerminalDevice->SimpleTextOutputMode;\r
728 TerminalDevice->TerminalConsoleModeData = InitializeTerminalConsoleTextMode (\r
729 &SimpleTextOutput->Mode->MaxMode\r
730 );\r
731 if (TerminalDevice->TerminalConsoleModeData == NULL) {\r
732 goto FreeResources;\r
95276127 733 }\r
95276127 734 //\r
f7c11d9b 735 // For terminal devices, cursor is always visible\r
95276127 736 //\r
f7c11d9b
RN
737 SimpleTextOutput->Mode->CursorVisible = TRUE;\r
738 Status = SimpleTextOutput->SetAttribute (SimpleTextOutput, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK));\r
739 if (!EFI_ERROR (Status)) {\r
740 Status = SimpleTextOutput->Reset (SimpleTextOutput, FALSE);\r
741 }\r
742 if (EFI_ERROR (Status)) {\r
743 goto ReportError;\r
95276127 744 }\r
f1aec6cc 745\r
95276127 746 //\r
f7c11d9b 747 // Initialize SimpleTextInput instance\r
95276127 748 //\r
f7c11d9b
RN
749 Status = SimpleTextInput->Reset (SimpleTextInput, FALSE);\r
750 if (EFI_ERROR (Status)) {\r
751 goto ReportError;\r
95276127 752 }\r
753\r
f7c11d9b
RN
754 Status = gBS->InstallMultipleProtocolInterfaces (\r
755 &TerminalDevice->Handle,\r
756 &gEfiSimpleTextInProtocolGuid, &TerminalDevice->SimpleInput,\r
757 &gEfiSimpleTextInputExProtocolGuid, &TerminalDevice->SimpleInputEx,\r
758 &gEfiSimpleTextOutProtocolGuid, &TerminalDevice->SimpleTextOutput,\r
759 &gEfiDevicePathProtocolGuid, TerminalDevice->DevicePath,\r
760 NULL\r
761 );\r
762 if (!EFI_ERROR (Status)) {\r
763 Status = gBS->OpenProtocol (\r
764 Controller,\r
765 &gEfiSerialIoProtocolGuid,\r
766 (VOID **) &TerminalDevice->SerialIo,\r
767 This->DriverBindingHandle,\r
768 TerminalDevice->Handle,\r
769 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
e3dcffcc 770 );\r
f7c11d9b
RN
771 ASSERT_EFI_ERROR (Status);\r
772 StartTerminalStateMachine (TerminalDevice);\r
773 return Status;\r
e3dcffcc 774 }\r
95276127 775\r
95276127 776ReportError:\r
95276127 777 REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
778 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
f9876ecf 779 (EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_EC_CONTROLLER_ERROR),\r
f7c11d9b 780 ParentDevicePath\r
95276127 781 );\r
782\r
f7c11d9b
RN
783FreeResources:\r
784 ASSERT (TerminalDevice != NULL);\r
95276127 785\r
f7c11d9b
RN
786 if (TerminalDevice->SimpleInput.WaitForKey != NULL) {\r
787 gBS->CloseEvent (TerminalDevice->SimpleInput.WaitForKey);\r
788 }\r
789 if (TerminalDevice->SimpleInputEx.WaitForKeyEx != NULL) {\r
790 gBS->CloseEvent (TerminalDevice->SimpleInputEx.WaitForKeyEx);\r
791 }\r
792 if (TerminalDevice->KeyNotifyProcessEvent != NULL) {\r
793 gBS->CloseEvent (TerminalDevice->KeyNotifyProcessEvent);\r
794 }\r
f0368006 795\r
f7c11d9b
RN
796 if (TerminalDevice->RawFiFo != NULL) {\r
797 FreePool (TerminalDevice->RawFiFo);\r
798 }\r
799 if (TerminalDevice->UnicodeFiFo != NULL) {\r
800 FreePool (TerminalDevice->UnicodeFiFo);\r
801 }\r
802 if (TerminalDevice->EfiKeyFiFo != NULL) {\r
803 FreePool (TerminalDevice->EfiKeyFiFo);\r
804 }\r
805 if (TerminalDevice->EfiKeyFiFoForNotify != NULL) {\r
806 FreePool (TerminalDevice->EfiKeyFiFoForNotify);\r
807 }\r
95276127 808\r
f7c11d9b
RN
809 if (TerminalDevice->ControllerNameTable != NULL) {\r
810 FreeUnicodeStringTable (TerminalDevice->ControllerNameTable);\r
811 }\r
66aa04e4 812\r
f7c11d9b
RN
813 if (TerminalDevice->DevicePath != NULL) {\r
814 FreePool (TerminalDevice->DevicePath);\r
815 }\r
66aa04e4 816\r
f7c11d9b
RN
817 if (TerminalDevice->TerminalConsoleModeData != NULL) {\r
818 FreePool (TerminalDevice->TerminalConsoleModeData);\r
819 }\r
95276127 820\r
f7c11d9b 821 FreePool (TerminalDevice);\r
95276127 822\r
f7c11d9b 823CloseProtocols:\r
79d07c66 824\r
f7c11d9b
RN
825 //\r
826 // Remove Parent Device Path from\r
827 // the Console Device Environment Variables\r
828 //\r
829 TerminalRemoveConsoleDevVariable (EFI_CON_IN_DEV_VARIABLE_NAME, ParentDevicePath);\r
830 TerminalRemoveConsoleDevVariable (EFI_CON_OUT_DEV_VARIABLE_NAME, ParentDevicePath);\r
831 TerminalRemoveConsoleDevVariable (EFI_ERR_OUT_DEV_VARIABLE_NAME, ParentDevicePath);\r
95276127 832\r
f7c11d9b
RN
833 Status = gBS->CloseProtocol (\r
834 Controller,\r
835 &gEfiSerialIoProtocolGuid,\r
836 This->DriverBindingHandle,\r
837 Controller\r
838 );\r
839 ASSERT_EFI_ERROR (Status);\r
95276127 840\r
f7c11d9b
RN
841 Status = gBS->CloseProtocol (\r
842 Controller,\r
843 &gEfiDevicePathProtocolGuid,\r
844 This->DriverBindingHandle,\r
845 Controller\r
846 );\r
847 ASSERT_EFI_ERROR (Status);\r
95276127 848 return Status;\r
849}\r
850\r
e49ef433 851/**\r
ab76200c 852 Stop this driver on Controller by closing Simple Text In, Simple Text\r
8fd98315 853 In Ex, Simple Text Out protocol, and removing parent device path from\r
677fdb90 854 Console Device Environment Variables.\r
e49ef433 855\r
8fd98315 856 @param This Protocol instance pointer.\r
ab76200c 857 @param Controller Handle of device to stop driver on\r
8fd98315 858 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
859 children is zero stop the entire bus driver.\r
860 @param ChildHandleBuffer List of Child Handles to Stop.\r
e49ef433 861\r
ab76200c 862 @retval EFI_SUCCESS This driver is removed Controller.\r
8fd98315 863 @retval other This driver could not be removed from this device.\r
e49ef433 864\r
865**/\r
95276127 866EFI_STATUS\r
867EFIAPI\r
868TerminalDriverBindingStop (\r
869 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
870 IN EFI_HANDLE Controller,\r
871 IN UINTN NumberOfChildren,\r
872 IN EFI_HANDLE *ChildHandleBuffer\r
873 )\r
95276127 874{\r
875 EFI_STATUS Status;\r
876 UINTN Index;\r
877 BOOLEAN AllChildrenStopped;\r
878 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *SimpleTextOutput;\r
879 TERMINAL_DEV *TerminalDevice;\r
880 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
881 EFI_SERIAL_IO_PROTOCOL *SerialIo;\r
95276127 882\r
883 //\r
884 // Complete all outstanding transactions to Controller.\r
885 // Don't allow any new transaction to Controller to be started.\r
886 //\r
887 if (NumberOfChildren == 0) {\r
888 //\r
889 // Close the bus driver\r
890 //\r
891 Status = gBS->OpenProtocol (\r
892 Controller,\r
f7c11d9b 893 &gEfiDevicePathProtocolGuid,\r
95276127 894 (VOID **) &ParentDevicePath,\r
895 This->DriverBindingHandle,\r
896 Controller,\r
897 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
898 );\r
f7c11d9b 899 ASSERT_EFI_ERROR (Status);\r
95276127 900\r
f7c11d9b
RN
901 //\r
902 // Remove Parent Device Path from\r
903 // the Console Device Environment Variables\r
904 //\r
905 TerminalRemoveConsoleDevVariable (EFI_CON_IN_DEV_VARIABLE_NAME, ParentDevicePath);\r
906 TerminalRemoveConsoleDevVariable (EFI_CON_OUT_DEV_VARIABLE_NAME, ParentDevicePath);\r
907 TerminalRemoveConsoleDevVariable (EFI_ERR_OUT_DEV_VARIABLE_NAME, ParentDevicePath);\r
95276127 908\r
909 gBS->CloseProtocol (\r
910 Controller,\r
911 &gEfiSerialIoProtocolGuid,\r
912 This->DriverBindingHandle,\r
913 Controller\r
914 );\r
915\r
916 gBS->CloseProtocol (\r
917 Controller,\r
918 &gEfiDevicePathProtocolGuid,\r
919 This->DriverBindingHandle,\r
920 Controller\r
921 );\r
922\r
923 return EFI_SUCCESS;\r
924 }\r
925\r
926 AllChildrenStopped = TRUE;\r
927\r
928 for (Index = 0; Index < NumberOfChildren; Index++) {\r
929\r
930 Status = gBS->OpenProtocol (\r
931 ChildHandleBuffer[Index],\r
932 &gEfiSimpleTextOutProtocolGuid,\r
933 (VOID **) &SimpleTextOutput,\r
934 This->DriverBindingHandle,\r
935 ChildHandleBuffer[Index],\r
936 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
937 );\r
938 if (!EFI_ERROR (Status)) {\r
939\r
940 TerminalDevice = TERMINAL_CON_OUT_DEV_FROM_THIS (SimpleTextOutput);\r
941\r
942 gBS->CloseProtocol (\r
943 Controller,\r
944 &gEfiSerialIoProtocolGuid,\r
945 This->DriverBindingHandle,\r
946 ChildHandleBuffer[Index]\r
947 );\r
948\r
949 Status = gBS->UninstallMultipleProtocolInterfaces (\r
950 ChildHandleBuffer[Index],\r
951 &gEfiSimpleTextInProtocolGuid,\r
952 &TerminalDevice->SimpleInput,\r
66aa04e4 953 &gEfiSimpleTextInputExProtocolGuid,\r
954 &TerminalDevice->SimpleInputEx,\r
95276127 955 &gEfiSimpleTextOutProtocolGuid,\r
956 &TerminalDevice->SimpleTextOutput,\r
957 &gEfiDevicePathProtocolGuid,\r
958 TerminalDevice->DevicePath,\r
959 NULL\r
960 );\r
961 if (EFI_ERROR (Status)) {\r
962 gBS->OpenProtocol (\r
963 Controller,\r
964 &gEfiSerialIoProtocolGuid,\r
965 (VOID **) &SerialIo,\r
966 This->DriverBindingHandle,\r
967 ChildHandleBuffer[Index],\r
968 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
969 );\r
970 } else {\r
971\r
0d8b3f81 972 FreeUnicodeStringTable (TerminalDevice->ControllerNameTable);\r
b7cf1c07 973 StopTerminalStateMachine (TerminalDevice);\r
95276127 974 gBS->CloseEvent (TerminalDevice->SimpleInput.WaitForKey);\r
66aa04e4 975 gBS->CloseEvent (TerminalDevice->SimpleInputEx.WaitForKeyEx);\r
47b612db 976 gBS->CloseEvent (TerminalDevice->KeyNotifyProcessEvent);\r
66aa04e4 977 TerminalFreeNotifyList (&TerminalDevice->NotifyList);\r
95276127 978 FreePool (TerminalDevice->DevicePath);\r
0d8b3f81 979 FreePool (TerminalDevice->TerminalConsoleModeData);\r
95276127 980 FreePool (TerminalDevice);\r
981 }\r
982 }\r
983\r
984 if (EFI_ERROR (Status)) {\r
985 AllChildrenStopped = FALSE;\r
986 }\r
987 }\r
988\r
989 if (!AllChildrenStopped) {\r
990 return EFI_DEVICE_ERROR;\r
991 }\r
992\r
993 return EFI_SUCCESS;\r
994}\r
995\r
b9c04b88
RN
996/**\r
997 Compare a device path data structure to that of all the nodes of a\r
998 second device path instance.\r
999\r
1000 @param Multi A pointer to a multi-instance device path data structure.\r
1001 @param Single A pointer to a single-instance device path data structure.\r
1002\r
1003 @retval TRUE If the Single is contained within Multi.\r
1004 @retval FALSE The Single is not match within Multi.\r
1005\r
1006**/\r
1007BOOLEAN\r
1008MatchDevicePaths (\r
1009 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
1010 IN EFI_DEVICE_PATH_PROTOCOL *Single\r
1011 )\r
1012{\r
1013 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
1014 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;\r
1015 UINTN Size;\r
1016\r
1017 DevicePath = Multi;\r
1018 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
1019 //\r
1020 // Search for the match of 'Single' in 'Multi'\r
1021 //\r
1022 while (DevicePathInst != NULL) {\r
1023 //\r
1024 // If the single device path is found in multiple device paths,\r
1025 // return success\r
1026 //\r
1027 if (CompareMem (Single, DevicePathInst, Size) == 0) {\r
1028 FreePool (DevicePathInst);\r
1029 return TRUE;\r
1030 }\r
1031\r
1032 FreePool (DevicePathInst);\r
1033 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
1034 }\r
1035\r
1036 return FALSE;\r
1037}\r
1038\r
8fd98315 1039/**\r
1040 Update terminal device path in Console Device Environment Variables.\r
1041\r
1042 @param VariableName The Console Device Environment Variable.\r
11baadb6 1043 @param ParentDevicePath The terminal device path to be updated.\r
8fd98315 1044\r
8fd98315 1045**/\r
95276127 1046VOID\r
1047TerminalUpdateConsoleDevVariable (\r
1048 IN CHAR16 *VariableName,\r
1049 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath\r
1050 )\r
1051{\r
1052 EFI_STATUS Status;\r
5070befc 1053 UINTN NameSize;\r
95276127 1054 UINTN VariableSize;\r
8ce87fff 1055 TERMINAL_TYPE TerminalType;\r
95276127 1056 EFI_DEVICE_PATH_PROTOCOL *Variable;\r
1057 EFI_DEVICE_PATH_PROTOCOL *NewVariable;\r
1058 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
5070befc 1059 EDKII_SET_VARIABLE_STATUS *SetVariableStatus;\r
95276127 1060\r
95276127 1061 //\r
1062 // Get global variable and its size according to the name given.\r
1063 //\r
b9c04b88
RN
1064 Status = GetEfiGlobalVariable2 (VariableName, (VOID**)&Variable, NULL);\r
1065 if (Status == EFI_NOT_FOUND) {\r
1066 Status = EFI_SUCCESS;\r
1067 Variable = NULL;\r
1068 }\r
1069 if (EFI_ERROR (Status)) {\r
e3dcffcc 1070 return;\r
1071 }\r
1072\r
95276127 1073 //\r
1074 // Append terminal device path onto the variable.\r
1075 //\r
f9163275 1076 for (TerminalType = 0; TerminalType < ARRAY_SIZE (mTerminalType); TerminalType++) {\r
95276127 1077 SetTerminalDevicePath (TerminalType, ParentDevicePath, &TempDevicePath);\r
95276127 1078\r
1079 if (TempDevicePath != NULL) {\r
b9c04b88
RN
1080 if (!MatchDevicePaths (Variable, TempDevicePath)) {\r
1081 NewVariable = AppendDevicePathInstance (Variable, TempDevicePath);\r
1082 if (NewVariable != NULL) {\r
1083 if (Variable != NULL) {\r
1084 FreePool (Variable);\r
1085 }\r
1086 Variable = NewVariable;\r
1087 }\r
1088 }\r
1089\r
95276127 1090 FreePool (TempDevicePath);\r
1091 }\r
1092\r
95276127 1093 }\r
1094\r
1095 VariableSize = GetDevicePathSize (Variable);\r
1096\r
1097 Status = gRT->SetVariable (\r
1098 VariableName,\r
1099 &gEfiGlobalVariableGuid,\r
1100 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
1101 VariableSize,\r
1102 Variable\r
1103 );\r
5070befc
RN
1104\r
1105 if (EFI_ERROR (Status)) {\r
1106 NameSize = StrSize (VariableName);\r
1107 SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + VariableSize);\r
1108 if (SetVariableStatus != NULL) {\r
1109 CopyGuid (&SetVariableStatus->Guid, &gEfiGlobalVariableGuid);\r
1110 SetVariableStatus->NameSize = NameSize;\r
1111 SetVariableStatus->DataSize = VariableSize;\r
1112 SetVariableStatus->SetStatus = Status;\r
1113 SetVariableStatus->Attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
1114 CopyMem (SetVariableStatus + 1, VariableName, NameSize);\r
1115 CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Variable, VariableSize);\r
1116\r
1117 REPORT_STATUS_CODE_EX (\r
1118 EFI_ERROR_CODE,\r
1119 PcdGet32 (PcdErrorCodeSetVariable),\r
1120 0,\r
1121 NULL,\r
1122 &gEdkiiStatusCodeDataTypeVariableGuid,\r
1123 SetVariableStatus,\r
1124 sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + VariableSize\r
1125 );\r
1126\r
1127 FreePool (SetVariableStatus);\r
1128 }\r
1129 }\r
1130\r
95276127 1131 FreePool (Variable);\r
1132\r
1133 return ;\r
1134}\r
1135\r
e49ef433 1136\r
1137/**\r
8fd98315 1138 Remove terminal device path from Console Device Environment Variables.\r
e49ef433 1139\r
8fd98315 1140 @param VariableName Console Device Environment Variables.\r
11baadb6 1141 @param ParentDevicePath The terminal device path to be updated.\r
e49ef433 1142\r
e49ef433 1143**/\r
95276127 1144VOID\r
1145TerminalRemoveConsoleDevVariable (\r
1146 IN CHAR16 *VariableName,\r
1147 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath\r
1148 )\r
95276127 1149{\r
1150 EFI_STATUS Status;\r
1151 BOOLEAN FoundOne;\r
1152 BOOLEAN Match;\r
1153 UINTN VariableSize;\r
1154 UINTN InstanceSize;\r
8ce87fff 1155 TERMINAL_TYPE TerminalType;\r
95276127 1156 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
1157 EFI_DEVICE_PATH_PROTOCOL *Variable;\r
1158 EFI_DEVICE_PATH_PROTOCOL *OriginalVariable;\r
1159 EFI_DEVICE_PATH_PROTOCOL *NewVariable;\r
1160 EFI_DEVICE_PATH_PROTOCOL *SavedNewVariable;\r
1161 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1162\r
95276127 1163 Instance = NULL;\r
1164\r
1165 //\r
1166 // Get global variable and its size according to the name given.\r
1167 //\r
f01b91ae 1168 GetEfiGlobalVariable2 (VariableName, (VOID**)&Variable, NULL);\r
95276127 1169 if (Variable == NULL) {\r
1170 return ;\r
1171 }\r
1172\r
1173 FoundOne = FALSE;\r
1174 OriginalVariable = Variable;\r
1175 NewVariable = NULL;\r
1176\r
1177 //\r
1178 // Get first device path instance from Variable\r
1179 //\r
1180 Instance = GetNextDevicePathInstance (&Variable, &InstanceSize);\r
1181 if (Instance == NULL) {\r
1182 FreePool (OriginalVariable);\r
1183 return ;\r
1184 }\r
1185 //\r
1186 // Loop through all the device path instances of Variable\r
1187 //\r
1188 do {\r
1189 //\r
1190 // Loop through all the terminal types that this driver supports\r
1191 //\r
1192 Match = FALSE;\r
f9163275 1193 for (TerminalType = 0; TerminalType < ARRAY_SIZE (mTerminalType); TerminalType++) {\r
95276127 1194\r
1195 SetTerminalDevicePath (TerminalType, ParentDevicePath, &TempDevicePath);\r
1196\r
1197 //\r
11baadb6 1198 // Compare the generated device path to the current device path instance\r
95276127 1199 //\r
1200 if (TempDevicePath != NULL) {\r
1201 if (CompareMem (Instance, TempDevicePath, InstanceSize) == 0) {\r
1202 Match = TRUE;\r
1203 FoundOne = TRUE;\r
1204 }\r
1205\r
1206 FreePool (TempDevicePath);\r
1207 }\r
1208 }\r
1209 //\r
1210 // If a match was not found, then keep the current device path instance\r
1211 //\r
1212 if (!Match) {\r
1213 SavedNewVariable = NewVariable;\r
1214 NewVariable = AppendDevicePathInstance (NewVariable, Instance);\r
1215 if (SavedNewVariable != NULL) {\r
1216 FreePool (SavedNewVariable);\r
1217 }\r
1218 }\r
1219 //\r
1220 // Get next device path instance from Variable\r
1221 //\r
1222 FreePool (Instance);\r
1223 Instance = GetNextDevicePathInstance (&Variable, &InstanceSize);\r
1224 } while (Instance != NULL);\r
1225\r
1226 FreePool (OriginalVariable);\r
1227\r
1228 if (FoundOne) {\r
1229 VariableSize = GetDevicePathSize (NewVariable);\r
1230\r
1231 Status = gRT->SetVariable (\r
1232 VariableName,\r
1233 &gEfiGlobalVariableGuid,\r
1234 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
1235 VariableSize,\r
1236 NewVariable\r
1237 );\r
5070befc
RN
1238 //\r
1239 // Shrinking variable with existing variable driver implementation shouldn't fail.\r
1240 //\r
1241 ASSERT_EFI_ERROR (Status);\r
95276127 1242 }\r
1243\r
1244 if (NewVariable != NULL) {\r
1245 FreePool (NewVariable);\r
1246 }\r
1247\r
1248 return ;\r
1249}\r
1250\r
8fd98315 1251/**\r
11baadb6 1252 Build terminal device path according to terminal type.\r
8fd98315 1253\r
1254 @param TerminalType The terminal type is PC ANSI, VT100, VT100+ or VT-UTF8.\r
11baadb6 1255 @param ParentDevicePath Parent device path.\r
8fd98315 1256 @param TerminalDevicePath Returned terminal device path, if building successfully.\r
1257\r
1258 @retval EFI_UNSUPPORTED Terminal does not belong to the supported type.\r
1259 @retval EFI_OUT_OF_RESOURCES Generate terminal device path failed.\r
1260 @retval EFI_SUCCESS Build terminal device path successfully.\r
1261\r
1262**/\r
95276127 1263EFI_STATUS\r
1264SetTerminalDevicePath (\r
8ce87fff 1265 IN TERMINAL_TYPE TerminalType,\r
95276127 1266 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,\r
1267 OUT EFI_DEVICE_PATH_PROTOCOL **TerminalDevicePath\r
1268 )\r
1269{\r
1270 VENDOR_DEVICE_PATH Node;\r
1271\r
802c39b0 1272 ASSERT (TerminalType < ARRAY_SIZE (mTerminalType));\r
95276127 1273 Node.Header.Type = MESSAGING_DEVICE_PATH;\r
1274 Node.Header.SubType = MSG_VENDOR_DP;\r
178b6b5d 1275 SetDevicePathNodeLength (&Node.Header, sizeof (VENDOR_DEVICE_PATH));\r
802c39b0 1276 CopyGuid (&Node.Guid, mTerminalType[TerminalType]);\r
ab76200c 1277\r
95276127 1278 //\r
ab76200c 1279 // Append the terminal node onto parent device path\r
95276127 1280 // to generate a complete terminal device path.\r
1281 //\r
1282 *TerminalDevicePath = AppendDevicePathNode (\r
1283 ParentDevicePath,\r
1284 (EFI_DEVICE_PATH_PROTOCOL *) &Node\r
1285 );\r
1286 if (*TerminalDevicePath == NULL) {\r
1287 return EFI_OUT_OF_RESOURCES;\r
1288 }\r
1289\r
1290 return EFI_SUCCESS;\r
1291}\r
1292\r
97a079ed
A
1293/**\r
1294 The user Entry Point for module Terminal. The user code starts with this function.\r
1295\r
ab76200c 1296 @param ImageHandle The firmware allocated handle for the EFI image.\r
1297 @param SystemTable A pointer to the EFI System Table.\r
fb0b259e 1298\r
97a079ed
A
1299 @retval EFI_SUCCESS The entry point is executed successfully.\r
1300 @retval other Some error occurs when executing this entry point.\r
1301\r
1302**/\r
1303EFI_STATUS\r
1304EFIAPI\r
1305InitializeTerminal(\r
1306 IN EFI_HANDLE ImageHandle,\r
1307 IN EFI_SYSTEM_TABLE *SystemTable\r
1308 )\r
1309{\r
1310 EFI_STATUS Status;\r
1311\r
1312 //\r
1313 // Install driver model protocol(s).\r
1314 //\r
5bca971e 1315 Status = EfiLibInstallDriverBindingComponentName2 (\r
97a079ed
A
1316 ImageHandle,\r
1317 SystemTable,\r
1318 &gTerminalDriverBinding,\r
1319 ImageHandle,\r
1320 &gTerminalComponentName,\r
5bca971e 1321 &gTerminalComponentName2\r
97a079ed
A
1322 );\r
1323 ASSERT_EFI_ERROR (Status);\r
1324\r
97a079ed
A
1325 return Status;\r
1326}\r
f1aec6cc 1327\r
1328/**\r
1329 Check if the device supports hot-plug through its device path.\r
1330\r
1331 This function could be updated to check more types of Hot Plug devices.\r
1332 Currently, it checks USB and PCCard device.\r
1333\r
1334 @param DevicePath Pointer to device's device path.\r
1335\r
1336 @retval TRUE The devcie is a hot-plug device\r
1337 @retval FALSE The devcie is not a hot-plug device.\r
1338\r
1339**/\r
1340BOOLEAN\r
1341IsHotPlugDevice (\r
1342 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1343 )\r
1344{\r
1345 EFI_DEVICE_PATH_PROTOCOL *CheckDevicePath;\r
1346\r
1347 CheckDevicePath = DevicePath;\r
1348 while (!IsDevicePathEnd (CheckDevicePath)) {\r
1349 //\r
1350 // Check device whether is hot plug device or not throught Device Path\r
e3dcffcc 1351 //\r
f1aec6cc 1352 if ((DevicePathType (CheckDevicePath) == MESSAGING_DEVICE_PATH) &&\r
1353 (DevicePathSubType (CheckDevicePath) == MSG_USB_DP ||\r
1354 DevicePathSubType (CheckDevicePath) == MSG_USB_CLASS_DP ||\r
1355 DevicePathSubType (CheckDevicePath) == MSG_USB_WWID_DP)) {\r
1356 //\r
1357 // If Device is USB device\r
1358 //\r
1359 return TRUE;\r
1360 }\r
1361 if ((DevicePathType (CheckDevicePath) == HARDWARE_DEVICE_PATH) &&\r
1362 (DevicePathSubType (CheckDevicePath) == HW_PCCARD_DP)) {\r
1363 //\r
1364 // If Device is PCCard\r
1365 //\r
1366 return TRUE;\r
1367 }\r
e3dcffcc 1368\r
f1aec6cc 1369 CheckDevicePath = NextDevicePathNode (CheckDevicePath);\r
1370 }\r
1371\r
1372 return FALSE;\r
1373}\r
1374\r