X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FConsole%2FTerminalDxe%2FTerminal.c;h=b8dcf0c472bd2670d1f5d8a4e51618833418558d;hp=5b8cb8da0b0c3a20e360727ad74b841b2c9055c2;hb=b7cf1c07479bbe5a3d5fd3c944b2ab76f740c072;hpb=6b88ceec9b23366f3a2b5541637a99f03ed7de04 diff --git a/MdeModulePkg/Universal/Console/TerminalDxe/Terminal.c b/MdeModulePkg/Universal/Console/TerminalDxe/Terminal.c index 5b8cb8da0b..b8dcf0c472 100644 --- a/MdeModulePkg/Universal/Console/TerminalDxe/Terminal.c +++ b/MdeModulePkg/Universal/Console/TerminalDxe/Terminal.c @@ -1,7 +1,9 @@ -/*++ +/** @file + Produces Simple Text Input Protocol, Simple Text Input Extended Protocol and + Simple Text Output Protocol upon Serial IO Protocol. -Copyright (c) 2006, Intel Corporation -All rights reserved. This program and the accompanying materials +Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.
+This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php @@ -9,15 +11,7 @@ http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -Module Name: - - Terminal.c - -Abstract: - -Revision History: - ---*/ +**/ #include "Terminal.h" @@ -35,15 +29,24 @@ EFI_DRIVER_BINDING_PROTOCOL gTerminalDriverBinding = { }; -EFI_GUID *gTerminalType[] = { +EFI_GUID *mTerminalType[] = { &gEfiPcAnsiGuid, &gEfiVT100Guid, &gEfiVT100PlusGuid, - &gEfiVTUTF8Guid + &gEfiVTUTF8Guid, + &gEfiTtyTermGuid }; -TERMINAL_DEV gTerminalDevTemplate = { +CHAR16 *mSerialConsoleNames[] = { + L"PC-ANSI Serial Console", + L"VT-100 Serial Console", + L"VT-100+ Serial Console", + L"VT-UTF8 Serial Console", + L"Tty Terminal Serial Console" +}; + +TERMINAL_DEV mTerminalDevTemplate = { TERMINAL_DEV_SIGNATURE, NULL, 0, @@ -68,37 +71,91 @@ TERMINAL_DEV gTerminalDevTemplate = { }, { // SimpleTextOutputMode 1, // MaxMode - 0, // Mode? + 0, // Mode EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK), // Attribute 0, // CursorColumn 0, // CursorRow TRUE // CursorVisible }, - 0, + NULL, // TerminalConsoleModeData + 0, // SerialInTimeOut + + NULL, // RawFifo + NULL, // UnicodeFiFo + NULL, // EfiKeyFiFo + NULL, // EfiKeyFiFoForNotify + + NULL, // ControllerNameTable + NULL, // TimerEvent + NULL, // TwoSecondTimeOut + INPUT_STATE_DEFAULT, + RESET_STATE_DEFAULT, { - 0, - 0, - { 0 } + 0, + 0, + 0 }, - { - 0, - 0, - { 0 } + 0, + FALSE, + { // SimpleTextInputEx + TerminalConInResetEx, + TerminalConInReadKeyStrokeEx, + NULL, + TerminalConInSetState, + TerminalConInRegisterKeyNotify, + TerminalConInUnregisterKeyNotify, }, - { - 0, - 0, - { 0 } + { // NotifyList + NULL, + NULL, }, - NULL, // ControllerNameTable - NULL, - INPUT_STATE_DEFAULT, - RESET_STATE_DEFAULT, - FALSE + NULL // KeyNotifyProcessEvent }; +TERMINAL_CONSOLE_MODE_DATA mTerminalConsoleModeData[] = { + {80, 25}, + {80, 50}, + {100, 31}, + // + // New modes can be added here. + // +}; + +/** + Convert the GUID representation of terminal type to enum type. + @param Guid The GUID representation of terminal type. + @return The terminal type in enum type. +**/ +TERMINAL_TYPE +TerminalTypeFromGuid ( + IN EFI_GUID *Guid +) +{ + TERMINAL_TYPE Type; + + for (Type = 0; Type < ARRAY_SIZE (mTerminalType); Type++) { + if (CompareGuid (Guid, mTerminalType[Type])) { + break; + } + } + return Type; +} + +/** + Test to see if this driver supports Controller. + + @param This Protocol instance pointer. + @param Controller Handle of device to test + @param RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this device. + @retval other This driver does not support this device. + +**/ EFI_STATUS EFIAPI TerminalDriverBindingSupported ( @@ -117,34 +174,42 @@ TerminalDriverBindingSupported ( // device path that describes a terminal communications protocol. // if (RemainingDevicePath != NULL) { - - Node = (VENDOR_DEVICE_PATH *) RemainingDevicePath; - - if (Node->Header.Type != MESSAGING_DEVICE_PATH || - Node->Header.SubType != MSG_VENDOR_DP || - DevicePathNodeLength(&Node->Header) != sizeof(VENDOR_DEVICE_PATH)) { - - return EFI_UNSUPPORTED; - - } // - // only supports PC ANSI, VT100, VT100+ and VT-UTF8 terminal types + // Check if RemainingDevicePath is the End of Device Path Node, + // if yes, go on checking other conditions // - if (!CompareGuid (&Node->Guid, &gEfiPcAnsiGuid) && - !CompareGuid (&Node->Guid, &gEfiVT100Guid) && - !CompareGuid (&Node->Guid, &gEfiVT100PlusGuid) && - !CompareGuid (&Node->Guid, &gEfiVTUTF8Guid)) { + if (!IsDevicePathEnd (RemainingDevicePath)) { + // + // If RemainingDevicePath isn't the End of Device Path Node, + // check its validation + // + Node = (VENDOR_DEVICE_PATH *) RemainingDevicePath; + + if (Node->Header.Type != MESSAGING_DEVICE_PATH || + Node->Header.SubType != MSG_VENDOR_DP || + DevicePathNodeLength(&Node->Header) != sizeof(VENDOR_DEVICE_PATH)) { + + return EFI_UNSUPPORTED; - return EFI_UNSUPPORTED; + } + // + // only supports PC ANSI, VT100, VT100+, VT-UTF8, and TtyTerm terminal types + // + if (TerminalTypeFromGuid (&Node->Guid) == ARRAY_SIZE (mTerminalType)) { + return EFI_UNSUPPORTED; + } } } // // Open the IO Abstraction(s) needed to perform the supported test + // The Controller must support the Serial I/O Protocol. + // This driver is a bus driver with at most 1 child device, so it is + // ok for it to be already started. // Status = gBS->OpenProtocol ( Controller, - &gEfiDevicePathProtocolGuid, - (VOID **) &ParentDevicePath, + &gEfiSerialIoProtocolGuid, + (VOID **) &SerialIo, This->DriverBindingHandle, Controller, EFI_OPEN_PROTOCOL_BY_DRIVER @@ -157,22 +222,23 @@ TerminalDriverBindingSupported ( return Status; } + // + // Close the I/O Abstraction(s) used to perform the supported test + // gBS->CloseProtocol ( Controller, - &gEfiDevicePathProtocolGuid, + &gEfiSerialIoProtocolGuid, This->DriverBindingHandle, Controller ); // - // The Controller must support the Serial I/O Protocol. - // This driver is a bus driver with at most 1 child device, so it is - // ok for it to be already started. + // Open the EFI Device Path protocol needed to perform the supported test // Status = gBS->OpenProtocol ( Controller, - &gEfiSerialIoProtocolGuid, - (VOID **) &SerialIo, + &gEfiDevicePathProtocolGuid, + (VOID **) &ParentDevicePath, This->DriverBindingHandle, Controller, EFI_OPEN_PROTOCOL_BY_DRIVER @@ -184,12 +250,13 @@ TerminalDriverBindingSupported ( if (EFI_ERROR (Status)) { return Status; } + // - // Close the I/O Abstraction(s) used to perform the supported test + // Close protocol, don't use device path protocol in the Support() function // gBS->CloseProtocol ( Controller, - &gEfiSerialIoProtocolGuid, + &gEfiDevicePathProtocolGuid, This->DriverBindingHandle, Controller ); @@ -197,47 +264,383 @@ TerminalDriverBindingSupported ( return Status; } -EFI_STATUS +/** + Build the terminal device path for the child device according to the + terminal type. + + @param ParentDevicePath Parent device path. + @param RemainingDevicePath A specific child device. + + @return The child device path built. + +**/ +EFI_DEVICE_PATH_PROTOCOL* EFIAPI -TerminalDriverBindingStart ( - IN EFI_DRIVER_BINDING_PROTOCOL *This, - IN EFI_HANDLE Controller, +BuildTerminalDevpath ( + IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath, IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath ) -/*++ +{ + EFI_DEVICE_PATH_PROTOCOL *TerminalDevicePath; + TERMINAL_TYPE TerminalType; + VENDOR_DEVICE_PATH *Node; + EFI_STATUS Status; + + TerminalDevicePath = NULL; + + // + // Use the RemainingDevicePath to determine the terminal type + // + Node = (VENDOR_DEVICE_PATH *) RemainingDevicePath; + if (Node == NULL) { + TerminalType = PcdGet8 (PcdDefaultTerminalType); + + } else if (CompareGuid (&Node->Guid, &gEfiPcAnsiGuid)) { + + TerminalType = TerminalTypePcAnsi; + + } else if (CompareGuid (&Node->Guid, &gEfiVT100Guid)) { + + TerminalType = TerminalTypeVt100; + + } else if (CompareGuid (&Node->Guid, &gEfiVT100PlusGuid)) { + + TerminalType = TerminalTypeVt100Plus; + + } else if (CompareGuid (&Node->Guid, &gEfiVTUTF8Guid)) { + + TerminalType = TerminalTypeVtUtf8; + + } else if (CompareGuid (&Node->Guid, &gEfiTtyTermGuid)) { + + TerminalType = TerminalTypeTtyTerm; + + } else { + return NULL; + } + + // + // Build the device path for the child device + // + Status = SetTerminalDevicePath ( + TerminalType, + ParentDevicePath, + &TerminalDevicePath + ); + if (EFI_ERROR (Status)) { + return NULL; + } + return TerminalDevicePath; +} + +/** + Compare a device path data structure to that of all the nodes of a + second device path instance. + + @param Multi A pointer to a multi-instance device path data structure. + @param Single A pointer to a single-instance device path data structure. + + @retval TRUE If the Single is contained within Multi. + @retval FALSE The Single is not match within Multi. + +**/ +BOOLEAN +MatchDevicePaths ( + IN EFI_DEVICE_PATH_PROTOCOL *Multi, + IN EFI_DEVICE_PATH_PROTOCOL *Single + ) +{ + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_DEVICE_PATH_PROTOCOL *DevicePathInst; + UINTN Size; + + DevicePath = Multi; + DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size); + // + // Search for the match of 'Single' in 'Multi' + // + while (DevicePathInst != NULL) { + // + // If the single device path is found in multiple device paths, + // return success + // + if (CompareMem (Single, DevicePathInst, Size) == 0) { + FreePool (DevicePathInst); + return TRUE; + } + + FreePool (DevicePathInst); + DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size); + } + + return FALSE; +} + +/** + Check whether the terminal device path is in the global variable. + + @param VariableName Pointer to one global variable. + @param TerminalDevicePath Pointer to the terminal device's device path. + + @retval TRUE The devcie is in the global variable. + @retval FALSE The devcie is not in the global variable. + +**/ +BOOLEAN +IsTerminalInConsoleVariable ( + IN CHAR16 *VariableName, + IN EFI_DEVICE_PATH_PROTOCOL *TerminalDevicePath + ) +{ + EFI_DEVICE_PATH_PROTOCOL *Variable; + BOOLEAN ReturnFlag; + + // + // Get global variable and its size according to the name given. + // + GetEfiGlobalVariable2 (VariableName, (VOID**)&Variable, NULL); + if (Variable == NULL) { + return FALSE; + } + + // + // Check whether the terminal device path is one of the variable instances. + // + ReturnFlag = MatchDevicePaths (Variable, TerminalDevicePath); + + FreePool (Variable); + + return ReturnFlag; +} + +/** + Free notify functions list. + + @param ListHead The list head + + @retval EFI_SUCCESS Free the notify list successfully. + @retval EFI_INVALID_PARAMETER ListHead is NULL. + +**/ +EFI_STATUS +TerminalFreeNotifyList ( + IN OUT LIST_ENTRY *ListHead + ) +{ + TERMINAL_CONSOLE_IN_EX_NOTIFY *NotifyNode; + + if (ListHead == NULL) { + return EFI_INVALID_PARAMETER; + } + while (!IsListEmpty (ListHead)) { + NotifyNode = CR ( + ListHead->ForwardLink, + TERMINAL_CONSOLE_IN_EX_NOTIFY, + NotifyEntry, + TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE + ); + RemoveEntryList (ListHead->ForwardLink); + FreePool (NotifyNode); + } + + return EFI_SUCCESS; +} + +/** + Initialize all the text modes which the terminal console supports. + + It returns information for available text modes that the terminal can support. + + @param[out] TextModeCount The total number of text modes that terminal console supports. + + @return The buffer to the text modes column and row information. + Caller is responsible to free it when it's non-NULL. + +**/ +TERMINAL_CONSOLE_MODE_DATA * +InitializeTerminalConsoleTextMode ( + OUT INT32 *TextModeCount +) +{ + TERMINAL_CONSOLE_MODE_DATA *TextModeData; + + ASSERT (TextModeCount != NULL); + + // + // Here we make sure that the final mode exposed does not include the duplicated modes, + // and does not include the invalid modes which exceed the max column and row. + // Reserve 2 modes for 80x25, 80x50 of terminal console. + // + TextModeData = AllocateCopyPool (sizeof (mTerminalConsoleModeData), mTerminalConsoleModeData); + if (TextModeData == NULL) { + return NULL; + } + *TextModeCount = ARRAY_SIZE (mTerminalConsoleModeData); + + DEBUG_CODE ( + INT32 Index; + for (Index = 0; Index < *TextModeCount; Index++) { + DEBUG ((DEBUG_INFO, "Terminal - Mode %d, Column = %d, Row = %d\n", + Index, TextModeData[Index].Columns, TextModeData[Index].Rows)); + } + ); + return TextModeData; +} + +/** + Stop the terminal state machine. + + @param TerminalDevice The terminal device. +**/ +VOID +StopTerminalStateMachine ( + TERMINAL_DEV *TerminalDevice + ) +{ + EFI_TPL OriginalTpl; + + OriginalTpl = gBS->RaiseTPL (TPL_NOTIFY); + + gBS->CloseEvent (TerminalDevice->TimerEvent); + gBS->CloseEvent (TerminalDevice->TwoSecondTimeOut); + + gBS->RestoreTPL (OriginalTpl); +} + +/** + Start the terminal state machine. + + @param TerminalDevice The terminal device. +**/ +VOID +StartTerminalStateMachine ( + TERMINAL_DEV *TerminalDevice + ) +{ + EFI_STATUS Status; + Status = gBS->CreateEvent ( + EVT_TIMER | EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + TerminalConInTimerHandler, + TerminalDevice, + &TerminalDevice->TimerEvent + ); + ASSERT_EFI_ERROR (Status); - Routine Description: + Status = gBS->SetTimer ( + TerminalDevice->TimerEvent, + TimerPeriodic, + KEYBOARD_TIMER_INTERVAL + ); + ASSERT_EFI_ERROR (Status); - Start the controller. + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &TerminalDevice->TwoSecondTimeOut + ); + ASSERT_EFI_ERROR (Status); +} - Arguments: +/** + Initialize the controller name table. - This - A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. - Controller - The handle of the controller to start. - RemainingDevicePath - A pointer to the remaining portion of a devcie path. + @param TerminalType The terminal type. + @param ControllerNameTable The controller name table. - Returns: + @retval EFI_SUCCESS The controller name table is initialized successfully. + @retval others Return status of AddUnicodeString2 (). +**/ +EFI_STATUS +InitializeControllerNameTable ( + TERMINAL_TYPE TerminalType, + EFI_UNICODE_STRING_TABLE **ControllerNameTable +) +{ + EFI_STATUS Status; + EFI_UNICODE_STRING_TABLE *Table; + + ASSERT (TerminalType < ARRAY_SIZE (mTerminalType)); + Table = NULL; + Status = AddUnicodeString2 ( + "eng", + gTerminalComponentName.SupportedLanguages, + &Table, + mSerialConsoleNames[TerminalType], + TRUE + ); + if (!EFI_ERROR (Status)) { + Status = AddUnicodeString2 ( + "en", + gTerminalComponentName2.SupportedLanguages, + &Table, + mSerialConsoleNames[TerminalType], + FALSE + ); + if (EFI_ERROR (Status)) { + FreeUnicodeStringTable (Table); + } + } + if (!EFI_ERROR (Status)) { + *ControllerNameTable = Table; + } + return Status; +} + +/** + Start this driver on Controller by opening a Serial IO protocol, + reading Device Path, and creating a child handle with a Simple Text In, + Simple Text In Ex and Simple Text Out protocol, and device path protocol. + And store Console Device Environment Variables. - EFI_SUCCESS. + @param This Protocol instance pointer. + @param Controller Handle of device to bind driver to + @param RemainingDevicePath Optional parameter use to pick a specific child + device to start. ---*/ + @retval EFI_SUCCESS This driver is added to Controller. + @retval EFI_ALREADY_STARTED This driver is already running on Controller. + @retval other This driver does not support this device. + +**/ +EFI_STATUS +EFIAPI +TerminalDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) { EFI_STATUS Status; EFI_SERIAL_IO_PROTOCOL *SerialIo; EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; VENDOR_DEVICE_PATH *Node; - VENDOR_DEVICE_PATH *DefaultNode; EFI_SERIAL_IO_MODE *Mode; UINTN SerialInTimeOut; TERMINAL_DEV *TerminalDevice; - UINT8 TerminalType; + TERMINAL_TYPE TerminalType; EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer; UINTN EntryCount; UINTN Index; EFI_DEVICE_PATH_PROTOCOL *DevicePath; - - TerminalDevice = NULL; - DefaultNode = NULL; + EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *SimpleTextOutput; + EFI_SIMPLE_TEXT_INPUT_PROTOCOL *SimpleTextInput; + BOOLEAN ConInSelected; + BOOLEAN ConOutSelected; + BOOLEAN NullRemaining; + BOOLEAN SimTxtInInstalled; + BOOLEAN SimTxtOutInstalled; + BOOLEAN FirstEnter; + + TerminalDevice = NULL; + ConInSelected = FALSE; + ConOutSelected = FALSE; + NullRemaining = FALSE; + SimTxtInInstalled = FALSE; + SimTxtOutInstalled = FALSE; + FirstEnter = FALSE; // // Get the Device Path Protocol to build the device path of the child device // @@ -270,323 +673,464 @@ TerminalDriverBindingStart ( if (Status != EFI_ALREADY_STARTED) { // - // If Serial I/O is not already open by this driver, then tag the handle - // with the Terminal Driver GUID and update the ConInDev, ConOutDev, and - // StdErrDev variables with the list of possible terminal types on this - // serial port. + // the serial I/O protocol never be opened before, it is the first + // time to start the serial Io controller // - Status = gBS->OpenProtocol ( - Controller, + FirstEnter = TRUE; + } + + // + // Serial I/O is not already open by this driver, then tag the handle + // with the Terminal Driver GUID and update the ConInDev, ConOutDev, and + // StdErrDev variables with the list of possible terminal types on this + // serial port. + // + Status = gBS->OpenProtocol ( + Controller, + &gEfiCallerIdGuid, + NULL, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + Status = gBS->InstallMultipleProtocolInterfaces ( + &Controller, &gEfiCallerIdGuid, - NULL, - This->DriverBindingHandle, - Controller, - EFI_OPEN_PROTOCOL_TEST_PROTOCOL + DuplicateDevicePath (ParentDevicePath), + NULL ); if (EFI_ERROR (Status)) { - Status = gBS->InstallMultipleProtocolInterfaces ( - &Controller, - &gEfiCallerIdGuid, - DuplicateDevicePath (ParentDevicePath), - NULL - ); - if (EFI_ERROR (Status)) { - goto Error; - } + goto Error; + } + + if (!IsHotPlugDevice (ParentDevicePath)) { // // if the serial device is a hot plug device, do not update the // ConInDev, ConOutDev, and StdErrDev variables. // - Status = gBS->OpenProtocol ( - Controller, - &gEfiHotPlugDeviceGuid, - NULL, - This->DriverBindingHandle, - Controller, - EFI_OPEN_PROTOCOL_TEST_PROTOCOL - ); - if (EFI_ERROR (Status)) { - TerminalUpdateConsoleDevVariable ((CHAR16 *)VarConsoleInpDev, ParentDevicePath); - TerminalUpdateConsoleDevVariable ((CHAR16 *)VarConsoleOutDev, ParentDevicePath); - TerminalUpdateConsoleDevVariable ((CHAR16 *)VarErrorOutDev, ParentDevicePath); - } + TerminalUpdateConsoleDevVariable (L"ConInDev", ParentDevicePath); + TerminalUpdateConsoleDevVariable (L"ConOutDev", ParentDevicePath); + TerminalUpdateConsoleDevVariable (L"ErrOutDev", ParentDevicePath); } } + // - // Make sure a child handle does not already exist. This driver can only - // produce one child per serial port. + // Check the requirement for the SimpleTxtIn and SimpleTxtOut protocols // - Status = gBS->OpenProtocolInformation ( - Controller, - &gEfiSerialIoProtocolGuid, - &OpenInfoBuffer, - &EntryCount - ); - if (!EFI_ERROR (Status)) { - Status = EFI_SUCCESS; - for (Index = 0; Index < EntryCount; Index++) { - if (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { - Status = EFI_ALREADY_STARTED; - } - } + // Simple In/Out Protocol will not be installed onto the handle if the + // device path to the handle is not present in the ConIn/ConOut + // environment variable. But If RemainingDevicePath is NULL, then always + // produce both Simple In and Simple Text Output Protocols. This is required + // for the connect all sequences to make sure all possible consoles are + // produced no matter what the current values of ConIn, ConOut, or StdErr are. + // + if (RemainingDevicePath == NULL) { + NullRemaining = TRUE; + } - FreePool (OpenInfoBuffer); - if (EFI_ERROR (Status)) { - return Status; - } + DevicePath = BuildTerminalDevpath (ParentDevicePath, RemainingDevicePath); + if (DevicePath != NULL) { + ConInSelected = IsTerminalInConsoleVariable (L"ConIn", DevicePath); + ConOutSelected = IsTerminalInConsoleVariable (L"ConOut", DevicePath); + FreePool (DevicePath); + } else { + goto Error; } // - // If RemainingDevicePath is NULL, then create default device path node + // Not create the child terminal handle if both Simple In/In Ex and + // Simple text Out protocols are not required to be published // - if (RemainingDevicePath == NULL) { - DefaultNode = AllocateZeroPool (sizeof (VENDOR_DEVICE_PATH)); - if (DefaultNode == NULL) { + if ((!ConInSelected)&&(!ConOutSelected)&&(!NullRemaining)) { + goto Error; + } + + // + // create the child terminal handle during first entry + // + if (FirstEnter) { + // + // First enther the start function + // + FirstEnter = FALSE; + // + // Make sure a child handle does not already exist. This driver can only + // produce one child per serial port. + // + Status = gBS->OpenProtocolInformation ( + Controller, + &gEfiSerialIoProtocolGuid, + &OpenInfoBuffer, + &EntryCount + ); + if (!EFI_ERROR (Status)) { + Status = EFI_SUCCESS; + for (Index = 0; Index < EntryCount; Index++) { + if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) { + Status = EFI_ALREADY_STARTED; + } + } + + FreePool (OpenInfoBuffer); + if (EFI_ERROR (Status)) { + goto Error; + } + } + + // + // If RemainingDevicePath is NULL, use default terminal type + // + if (RemainingDevicePath == NULL) { + TerminalType = PcdGet8 (PcdDefaultTerminalType); + } else if (!IsDevicePathEnd (RemainingDevicePath)) { + // + // If RemainingDevicePath isn't the End of Device Path Node, + // Use the RemainingDevicePath to determine the terminal type + // + Node = (VENDOR_DEVICE_PATH *)RemainingDevicePath; + TerminalType = TerminalTypeFromGuid (&Node->Guid); + } else { + // + // If RemainingDevicePath is the End of Device Path Node, + // skip enumerate any device and return EFI_SUCESSS + // + return EFI_SUCCESS; + } + + ASSERT (TerminalType < ARRAY_SIZE (mTerminalType)); + + // + // Initialize the Terminal Dev + // + TerminalDevice = AllocateCopyPool (sizeof (TERMINAL_DEV), &mTerminalDevTemplate); + if (TerminalDevice == NULL) { Status = EFI_OUT_OF_RESOURCES; goto Error; } - TerminalType = FixedPcdGet8 (PcdDefaultTerminalType); - // must be between PcAnsiType (0) and VTUTF8Type (3) - ASSERT (TerminalType <= VTUTF8Type); + TerminalDevice->TerminalType = TerminalType; + TerminalDevice->SerialIo = SerialIo; - CopyMem (&DefaultNode->Guid, gTerminalType[TerminalType], sizeof (EFI_GUID)); - RemainingDevicePath = (EFI_DEVICE_PATH_PROTOCOL*)DefaultNode; - } else { + InitializeListHead (&TerminalDevice->NotifyList); + Status = gBS->CreateEvent ( + EVT_NOTIFY_WAIT, + TPL_NOTIFY, + TerminalConInWaitForKeyEx, + TerminalDevice, + &TerminalDevice->SimpleInputEx.WaitForKeyEx + ); + if (EFI_ERROR (Status)) { + goto Error; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_WAIT, + TPL_NOTIFY, + TerminalConInWaitForKey, + TerminalDevice, + &TerminalDevice->SimpleInput.WaitForKey + ); + if (EFI_ERROR (Status)) { + goto Error; + } // - // Use the RemainingDevicePath to determine the terminal type + // Allocates and initializes the FIFO buffer to be zero, used for accommodating + // the pre-read pending characters. // - Node = (VENDOR_DEVICE_PATH *)RemainingDevicePath; - if (CompareGuid (&Node->Guid, &gEfiPcAnsiGuid)) { - TerminalType = PcAnsiType; - } else if (CompareGuid (&Node->Guid, &gEfiVT100Guid)) { - TerminalType = VT100Type; - } else if (CompareGuid (&Node->Guid, &gEfiVT100PlusGuid)) { - TerminalType = VT100PlusType; - } else if (CompareGuid (&Node->Guid, &gEfiVTUTF8Guid)) { - TerminalType = VTUTF8Type; - } else { + TerminalDevice->RawFiFo = AllocateZeroPool (sizeof (RAW_DATA_FIFO)); + if (TerminalDevice->RawFiFo == NULL) { + goto Error; + } + TerminalDevice->UnicodeFiFo = AllocateZeroPool (sizeof (UNICODE_FIFO)); + if (TerminalDevice->UnicodeFiFo == NULL) { + goto Error; + } + TerminalDevice->EfiKeyFiFo = AllocateZeroPool (sizeof (EFI_KEY_FIFO)); + if (TerminalDevice->EfiKeyFiFo == NULL) { + goto Error; + } + TerminalDevice->EfiKeyFiFoForNotify = AllocateZeroPool (sizeof (EFI_KEY_FIFO)); + if (TerminalDevice->EfiKeyFiFoForNotify == NULL) { goto Error; } - } - // - // Initialize the Terminal Dev - // - TerminalDevice = AllocateCopyPool (sizeof (TERMINAL_DEV), &gTerminalDevTemplate); - if (TerminalDevice == NULL) { - Status = EFI_OUT_OF_RESOURCES; - goto Error; - } + // + // Set the timeout value of serial buffer for + // keystroke response performance issue + // + Mode = TerminalDevice->SerialIo->Mode; - TerminalDevice->TerminalType = TerminalType; - TerminalDevice->SerialIo = SerialIo; + SerialInTimeOut = 0; + if (Mode->BaudRate != 0) { + SerialInTimeOut = (1 + Mode->DataBits + Mode->StopBits) * 2 * 1000000 / (UINTN) Mode->BaudRate; + } - Status = gBS->CreateEvent ( - EVT_NOTIFY_WAIT, - TPL_NOTIFY, - TerminalConInWaitForKey, - &TerminalDevice->SimpleInput, - &TerminalDevice->SimpleInput.WaitForKey - ); - if (EFI_ERROR (Status)) { - goto Error; - } + Status = TerminalDevice->SerialIo->SetAttributes ( + TerminalDevice->SerialIo, + Mode->BaudRate, + Mode->ReceiveFifoDepth, + (UINT32) SerialInTimeOut, + (EFI_PARITY_TYPE) (Mode->Parity), + (UINT8) Mode->DataBits, + (EFI_STOP_BITS_TYPE) (Mode->StopBits) + ); + if (EFI_ERROR (Status)) { + // + // if set attributes operation fails, invalidate + // the value of SerialInTimeOut,thus make it + // inconsistent with the default timeout value + // of serial buffer. This will invoke the recalculation + // in the readkeystroke routine. + // + TerminalDevice->SerialInTimeOut = 0; + } else { + TerminalDevice->SerialInTimeOut = SerialInTimeOut; + } + // + // Set Simple Text Output Protocol from template. + // + SimpleTextOutput = CopyMem ( + &TerminalDevice->SimpleTextOutput, + &mTerminalDevTemplate.SimpleTextOutput, + sizeof (mTerminalDevTemplate.SimpleTextOutput) + ); + SimpleTextOutput->Mode = &TerminalDevice->SimpleTextOutputMode; + + TerminalDevice->TerminalConsoleModeData = InitializeTerminalConsoleTextMode ( + &SimpleTextOutput->Mode->MaxMode + ); + if (TerminalDevice->TerminalConsoleModeData == NULL) { + goto ReportError; + } - // - // initialize the FIFO buffer used for accommodating - // the pre-read pending characters - // - InitializeRawFiFo (TerminalDevice); - InitializeUnicodeFiFo (TerminalDevice); - InitializeEfiKeyFiFo (TerminalDevice); + // + // For terminal devices, cursor is always visible + // + TerminalDevice->SimpleTextOutputMode.CursorVisible = TRUE; + Status = TerminalConOutSetAttribute ( + SimpleTextOutput, + EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK) + ); + if (EFI_ERROR (Status)) { + goto ReportError; + } - // - // Set the timeout value of serial buffer for - // keystroke response performance issue - // - Mode = TerminalDevice->SerialIo->Mode; - SerialInTimeOut = 0; - if (Mode->BaudRate != 0) { - SerialInTimeOut = (1 + Mode->DataBits + Mode->StopBits) * 2 * 1000000 / (UINTN) Mode->BaudRate; - } + // + // Build the component name for the child device + // + Status = InitializeControllerNameTable (TerminalDevice->TerminalType, &TerminalDevice->ControllerNameTable); + if (EFI_ERROR (Status)) { + goto Error; + } - Status = TerminalDevice->SerialIo->SetAttributes ( - TerminalDevice->SerialIo, - Mode->BaudRate, - Mode->ReceiveFifoDepth, - (UINT32) SerialInTimeOut, - (EFI_PARITY_TYPE) (Mode->Parity), - (UINT8) Mode->DataBits, - (EFI_STOP_BITS_TYPE) (Mode->StopBits) - ); - if (EFI_ERROR (Status)) { // - // if set attributes operation fails, invalidate - // the value of SerialInTimeOut,thus make it - // inconsistent with the default timeout value - // of serial buffer. This will invoke the recalculation - // in the readkeystroke routine. + // Build the device path for the child device // - TerminalDevice->SerialInTimeOut = 0; - } else { - TerminalDevice->SerialInTimeOut = SerialInTimeOut; - } - // - // Build the device path for the child device - // - Status = SetTerminalDevicePath ( - TerminalDevice->TerminalType, - ParentDevicePath, - &TerminalDevice->DevicePath - ); - if (EFI_ERROR (Status)) { - goto Error; - } + Status = SetTerminalDevicePath ( + TerminalDevice->TerminalType, + ParentDevicePath, + &TerminalDevice->DevicePath + ); + if (EFI_ERROR (Status)) { + goto Error; + } + + Status = TerminalConOutReset (SimpleTextOutput, FALSE); + if (EFI_ERROR (Status)) { + goto ReportError; + } + + Status = TerminalConOutSetMode (SimpleTextOutput, 0); + if (EFI_ERROR (Status)) { + goto ReportError; + } + + Status = TerminalConOutEnableCursor (SimpleTextOutput, TRUE); + if (EFI_ERROR (Status)) { + goto ReportError; + } + + StartTerminalStateMachine (TerminalDevice); + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + KeyNotifyProcessHandler, + TerminalDevice, + &TerminalDevice->KeyNotifyProcessEvent + ); + ASSERT_EFI_ERROR (Status); - DevicePath = TerminalDevice->DevicePath; + Status = gBS->InstallProtocolInterface ( + &TerminalDevice->Handle, + &gEfiDevicePathProtocolGuid, + EFI_NATIVE_INTERFACE, + TerminalDevice->DevicePath + ); + if (EFI_ERROR (Status)) { + goto Error; + } - Status = TerminalDevice->SimpleInput.Reset ( - &TerminalDevice->SimpleInput, - FALSE - ); - if (EFI_ERROR (Status)) { // - // Need to report Error Code first + // Register the Parent-Child relationship via + // EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. // - goto ReportError; + Status = gBS->OpenProtocol ( + Controller, + &gEfiSerialIoProtocolGuid, + (VOID **) &TerminalDevice->SerialIo, + This->DriverBindingHandle, + TerminalDevice->Handle, + EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER + ); + if (EFI_ERROR (Status)) { + goto Error; + } } + // - // Simple Text Output Protocol + // Find the child handle, and get its TerminalDevice private data // - TerminalDevice->SimpleTextOutput.Mode = &TerminalDevice->SimpleTextOutputMode; - - Status = TerminalDevice->SimpleTextOutput.Reset ( - &TerminalDevice->SimpleTextOutput, - FALSE - ); - if (EFI_ERROR (Status)) { - goto ReportError; - } + Status = gBS->OpenProtocolInformation ( + Controller, + &gEfiSerialIoProtocolGuid, + &OpenInfoBuffer, + &EntryCount + ); + if (!EFI_ERROR (Status)) { + Status = EFI_NOT_FOUND; + ASSERT (OpenInfoBuffer != NULL); + for (Index = 0; Index < EntryCount; Index++) { + if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) { + // + // Find the child terminal handle. + // Test whether the SimpleTxtIn and SimpleTxtOut have been published + // + Status = gBS->OpenProtocol ( + OpenInfoBuffer[Index].ControllerHandle, + &gEfiSimpleTextInProtocolGuid, + (VOID **) &SimpleTextInput, + This->DriverBindingHandle, + OpenInfoBuffer[Index].ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + SimTxtInInstalled = TRUE; + TerminalDevice = TERMINAL_CON_IN_DEV_FROM_THIS (SimpleTextInput); + } - Status = TerminalDevice->SimpleTextOutput.SetMode ( - &TerminalDevice->SimpleTextOutput, - 0 - ); - if (EFI_ERROR (Status)) { - goto ReportError; - } + Status = gBS->OpenProtocol ( + OpenInfoBuffer[Index].ControllerHandle, + &gEfiSimpleTextOutProtocolGuid, + (VOID **) &SimpleTextOutput, + This->DriverBindingHandle, + OpenInfoBuffer[Index].ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + SimTxtOutInstalled = TRUE; + TerminalDevice = TERMINAL_CON_OUT_DEV_FROM_THIS (SimpleTextOutput); + } + Status = EFI_SUCCESS; + break; + } + } - Status = TerminalDevice->SimpleTextOutput.EnableCursor ( - &TerminalDevice->SimpleTextOutput, - TRUE - ); - if (EFI_ERROR (Status)) { + FreePool (OpenInfoBuffer); + if (EFI_ERROR (Status)) { + goto ReportError; + } + } else { goto ReportError; } - Status = gBS->CreateEvent ( - EVT_TIMER, - TPL_CALLBACK, - NULL, - NULL, - &TerminalDevice->TwoSecondTimeOut - ); - + ASSERT (TerminalDevice != NULL); // - // Build the component name for the child device - // - TerminalDevice->ControllerNameTable = NULL; - switch (TerminalDevice->TerminalType) { - case PcAnsiType: - AddUnicodeString ( - "eng", - gTerminalComponentName.SupportedLanguages, - &TerminalDevice->ControllerNameTable, - (CHAR16 *)L"PC-ANSI Serial Console" - ); - break; - - case VT100Type: - AddUnicodeString ( - "eng", - gTerminalComponentName.SupportedLanguages, - &TerminalDevice->ControllerNameTable, - (CHAR16 *)L"VT-100 Serial Console" - ); - break; - - case VT100PlusType: - AddUnicodeString ( - "eng", - gTerminalComponentName.SupportedLanguages, - &TerminalDevice->ControllerNameTable, - (CHAR16 *)L"VT-100+ Serial Console" - ); - break; - - case VTUTF8Type: - AddUnicodeString ( - "eng", - gTerminalComponentName.SupportedLanguages, - &TerminalDevice->ControllerNameTable, - (CHAR16 *)L"VT-UTF8 Serial Console" - ); - break; + // Only do the reset if the device path is in the Conout variable + // + if (ConInSelected && !SimTxtInInstalled) { + Status = TerminalDevice->SimpleInput.Reset ( + &TerminalDevice->SimpleInput, + FALSE + ); + if (EFI_ERROR (Status)) { + // + // Need to report Error Code first + // + goto ReportError; + } } + // - // Install protocol interfaces for the serial device. + // Only output the configure string to remote terminal if the device path + // is in the Conout variable // - Status = gBS->InstallMultipleProtocolInterfaces ( - &TerminalDevice->Handle, - &gEfiDevicePathProtocolGuid, - TerminalDevice->DevicePath, - &gEfiSimpleTextInProtocolGuid, - &TerminalDevice->SimpleInput, - &gEfiSimpleTextOutProtocolGuid, - &TerminalDevice->SimpleTextOutput, - NULL - ); - if (EFI_ERROR (Status)) { - goto Error; + if (ConOutSelected && !SimTxtOutInstalled) { + Status = TerminalDevice->SimpleTextOutput.SetAttribute ( + &TerminalDevice->SimpleTextOutput, + EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK) + ); + if (EFI_ERROR (Status)) { + goto ReportError; + } + + Status = TerminalDevice->SimpleTextOutput.Reset ( + &TerminalDevice->SimpleTextOutput, + FALSE + ); + if (EFI_ERROR (Status)) { + goto ReportError; + } + + Status = TerminalDevice->SimpleTextOutput.SetMode ( + &TerminalDevice->SimpleTextOutput, + 0 + ); + if (EFI_ERROR (Status)) { + goto ReportError; + } + + Status = TerminalDevice->SimpleTextOutput.EnableCursor ( + &TerminalDevice->SimpleTextOutput, + TRUE + ); + if (EFI_ERROR (Status)) { + goto ReportError; + } } + // - // if the serial device is a hot plug device, attaches the HotPlugGuid - // onto the terminal device handle. + // Simple In/Out Protocol will not be installed onto the handle if the + // device path to the handle is not present in the ConIn/ConOut + // environment variable. But If RemainingDevicePath is NULL, then always + // produce both Simple In and Simple Text Output Protocols. This is required + // for the connect all sequences to make sure all possible consoles are + // produced no matter what the current values of ConIn, ConOut, or StdErr are. // - Status = gBS->OpenProtocol ( - Controller, - &gEfiHotPlugDeviceGuid, - NULL, - This->DriverBindingHandle, - Controller, - EFI_OPEN_PROTOCOL_TEST_PROTOCOL - ); - if (!EFI_ERROR (Status)) { + if (!SimTxtInInstalled && (ConInSelected || NullRemaining)) { Status = gBS->InstallMultipleProtocolInterfaces ( &TerminalDevice->Handle, - &gEfiHotPlugDeviceGuid, - NULL, + &gEfiSimpleTextInProtocolGuid, + &TerminalDevice->SimpleInput, + &gEfiSimpleTextInputExProtocolGuid, + &TerminalDevice->SimpleInputEx, NULL ); - } - // - // Register the Parent-Child relationship via - // EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER. - // - Status = gBS->OpenProtocol ( - Controller, - &gEfiSerialIoProtocolGuid, - (VOID **) &TerminalDevice->SerialIo, - This->DriverBindingHandle, - TerminalDevice->Handle, - EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER - ); - if (EFI_ERROR (Status)) { - goto Error; + if (EFI_ERROR (Status)) { + goto Error; + } } - if (DefaultNode != NULL) { - FreePool (DefaultNode); + if (!SimTxtOutInstalled && (ConOutSelected || NullRemaining)) { + Status = gBS->InstallProtocolInterface ( + &TerminalDevice->Handle, + &gEfiSimpleTextOutProtocolGuid, + EFI_NATIVE_INTERFACE, + &TerminalDevice->SimpleTextOutput + ); + if (EFI_ERROR (Status)) { + goto Error; + } } return EFI_SUCCESS; @@ -595,9 +1139,10 @@ ReportError: // // Report error code before exiting // + DevicePath = ParentDevicePath; REPORT_STATUS_CODE_WITH_DEVICE_PATH ( EFI_ERROR_CODE | EFI_ERROR_MINOR, - PcdGet32 (PcdStatusCodeValueRemoteConsoleError), + (EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_EC_CONTROLLER_ERROR), DevicePath ); @@ -615,10 +1160,33 @@ Error: gBS->CloseEvent (TerminalDevice->TwoSecondTimeOut); } + if (TerminalDevice->TimerEvent != NULL) { + gBS->CloseEvent (TerminalDevice->TimerEvent); + } + if (TerminalDevice->SimpleInput.WaitForKey != NULL) { gBS->CloseEvent (TerminalDevice->SimpleInput.WaitForKey); } + if (TerminalDevice->SimpleInputEx.WaitForKeyEx != NULL) { + gBS->CloseEvent (TerminalDevice->SimpleInputEx.WaitForKeyEx); + } + + TerminalFreeNotifyList (&TerminalDevice->NotifyList); + + if (TerminalDevice->RawFiFo != NULL) { + FreePool (TerminalDevice->RawFiFo); + } + if (TerminalDevice->UnicodeFiFo != NULL) { + FreePool (TerminalDevice->UnicodeFiFo); + } + if (TerminalDevice->EfiKeyFiFo != NULL) { + FreePool (TerminalDevice->EfiKeyFiFo); + } + if (TerminalDevice->EfiKeyFiFoForNotify != NULL) { + FreePool (TerminalDevice->EfiKeyFiFoForNotify); + } + if (TerminalDevice->ControllerNameTable != NULL) { FreeUnicodeStringTable (TerminalDevice->ControllerNameTable); } @@ -627,19 +1195,34 @@ Error: FreePool (TerminalDevice->DevicePath); } + if (TerminalDevice->TerminalConsoleModeData != NULL) { + FreePool (TerminalDevice->TerminalConsoleModeData); + } + FreePool (TerminalDevice); } } - if (DefaultNode != NULL) { - FreePool (DefaultNode); - } - This->Stop (This, Controller, 0, NULL); return Status; } +/** + Stop this driver on Controller by closing Simple Text In, Simple Text + In Ex, Simple Text Out protocol, and removing parent device path from + Console Device Environment Variables. + + @param This Protocol instance pointer. + @param Controller Handle of device to stop driver on + @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed Controller. + @retval other This driver could not be removed from this device. + +**/ EFI_STATUS EFIAPI TerminalDriverBindingStop ( @@ -648,25 +1231,6 @@ TerminalDriverBindingStop ( IN UINTN NumberOfChildren, IN EFI_HANDLE *ChildHandleBuffer ) -/*++ - - Routine Description: - - Stop a device controller. - - Arguments: - - This - A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. - Controller - A handle to the device being stopped. - NumberOfChildren - The number of child device handles in ChildHandleBuffer. - ChildHandleBuffer - An array of child handles to be freed. - - Returns: - - EFI_SUCCESS - Operation successful. - EFI_DEVICE_ERROR - Devices error. - ---*/ { EFI_STATUS Status; UINTN Index; @@ -707,9 +1271,9 @@ TerminalDriverBindingStop ( // Remove Parent Device Path from // the Console Device Environment Variables // - TerminalRemoveConsoleDevVariable ((CHAR16 *)VarConsoleInpDev, ParentDevicePath); - TerminalRemoveConsoleDevVariable ((CHAR16 *)VarConsoleOutDev, ParentDevicePath); - TerminalRemoveConsoleDevVariable ((CHAR16 *)VarErrorOutDev, ParentDevicePath); + TerminalRemoveConsoleDevVariable (L"ConInDev", ParentDevicePath); + TerminalRemoveConsoleDevVariable (L"ConOutDev", ParentDevicePath); + TerminalRemoveConsoleDevVariable (L"ErrOutDev", ParentDevicePath); // // Uninstall the Terminal Driver's GUID Tag from the Serial controller @@ -773,6 +1337,8 @@ TerminalDriverBindingStop ( ChildHandleBuffer[Index], &gEfiSimpleTextInProtocolGuid, &TerminalDevice->SimpleInput, + &gEfiSimpleTextInputExProtocolGuid, + &TerminalDevice->SimpleInputEx, &gEfiSimpleTextOutProtocolGuid, &TerminalDevice->SimpleTextOutput, &gEfiDevicePathProtocolGuid, @@ -794,28 +1360,15 @@ TerminalDriverBindingStop ( FreeUnicodeStringTable (TerminalDevice->ControllerNameTable); } - Status = gBS->OpenProtocol ( - ChildHandleBuffer[Index], - &gEfiHotPlugDeviceGuid, - NULL, - This->DriverBindingHandle, - Controller, - EFI_OPEN_PROTOCOL_TEST_PROTOCOL - ); - if (!EFI_ERROR (Status)) { - Status = gBS->UninstallMultipleProtocolInterfaces ( - ChildHandleBuffer[Index], - &gEfiHotPlugDeviceGuid, - NULL, - NULL - ); - } else { - Status = EFI_SUCCESS; - } - - gBS->CloseEvent (TerminalDevice->TwoSecondTimeOut); + StopTerminalStateMachine (TerminalDevice); gBS->CloseEvent (TerminalDevice->SimpleInput.WaitForKey); + gBS->CloseEvent (TerminalDevice->SimpleInputEx.WaitForKeyEx); + gBS->CloseEvent (TerminalDevice->KeyNotifyProcessEvent); + TerminalFreeNotifyList (&TerminalDevice->NotifyList); FreePool (TerminalDevice->DevicePath); + if (TerminalDevice->TerminalConsoleModeData != NULL) { + FreePool (TerminalDevice->TerminalConsoleModeData); + } FreePool (TerminalDevice); } } @@ -832,6 +1385,13 @@ TerminalDriverBindingStop ( return EFI_SUCCESS; } +/** + Update terminal device path in Console Device Environment Variables. + + @param VariableName The Console Device Environment Variable. + @param ParentDevicePath The terminal device path to be updated. + +**/ VOID TerminalUpdateConsoleDevVariable ( IN CHAR16 *VariableName, @@ -839,28 +1399,29 @@ TerminalUpdateConsoleDevVariable ( ) { EFI_STATUS Status; + UINTN NameSize; UINTN VariableSize; - UINT8 TerminalType; + TERMINAL_TYPE TerminalType; EFI_DEVICE_PATH_PROTOCOL *Variable; EFI_DEVICE_PATH_PROTOCOL *NewVariable; EFI_DEVICE_PATH_PROTOCOL *TempDevicePath; - - Variable = NULL; + EDKII_SET_VARIABLE_STATUS *SetVariableStatus; // // Get global variable and its size according to the name given. // - Variable = TerminalGetVariableAndSize ( - VariableName, - &gEfiGlobalVariableGuid, - &VariableSize - ); + GetEfiGlobalVariable2 (VariableName, (VOID**)&Variable, NULL); + if (Variable == NULL) { + return; + } + // // Append terminal device path onto the variable. // - for (TerminalType = PcAnsiType; TerminalType <= VTUTF8Type; TerminalType++) { + for (TerminalType = 0; TerminalType < ARRAY_SIZE (mTerminalType); TerminalType++) { SetTerminalDevicePath (TerminalType, ParentDevicePath, &TempDevicePath); NewVariable = AppendDevicePathInstance (Variable, TempDevicePath); + ASSERT (NewVariable != NULL); if (Variable != NULL) { FreePool (Variable); } @@ -881,38 +1442,58 @@ TerminalUpdateConsoleDevVariable ( VariableSize, Variable ); - ASSERT_EFI_ERROR (Status); + + if (EFI_ERROR (Status)) { + NameSize = StrSize (VariableName); + SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + VariableSize); + if (SetVariableStatus != NULL) { + CopyGuid (&SetVariableStatus->Guid, &gEfiGlobalVariableGuid); + SetVariableStatus->NameSize = NameSize; + SetVariableStatus->DataSize = VariableSize; + SetVariableStatus->SetStatus = Status; + SetVariableStatus->Attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS; + CopyMem (SetVariableStatus + 1, VariableName, NameSize); + CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Variable, VariableSize); + + REPORT_STATUS_CODE_EX ( + EFI_ERROR_CODE, + PcdGet32 (PcdErrorCodeSetVariable), + 0, + NULL, + &gEdkiiStatusCodeDataTypeVariableGuid, + SetVariableStatus, + sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + VariableSize + ); + + FreePool (SetVariableStatus); + } + } + FreePool (Variable); return ; } + +/** + Remove terminal device path from Console Device Environment Variables. + + @param VariableName Console Device Environment Variables. + @param ParentDevicePath The terminal device path to be updated. + +**/ VOID TerminalRemoveConsoleDevVariable ( IN CHAR16 *VariableName, IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath ) -/*++ - - Routine Description: - - Remove console device variable. - - Arguments: - - VariableName - A pointer to the variable name. - ParentDevicePath - A pointer to the parent device path. - - Returns: - ---*/ { EFI_STATUS Status; BOOLEAN FoundOne; BOOLEAN Match; UINTN VariableSize; UINTN InstanceSize; - UINT8 TerminalType; + TERMINAL_TYPE TerminalType; EFI_DEVICE_PATH_PROTOCOL *Instance; EFI_DEVICE_PATH_PROTOCOL *Variable; EFI_DEVICE_PATH_PROTOCOL *OriginalVariable; @@ -920,17 +1501,12 @@ TerminalRemoveConsoleDevVariable ( EFI_DEVICE_PATH_PROTOCOL *SavedNewVariable; EFI_DEVICE_PATH_PROTOCOL *TempDevicePath; - Variable = NULL; Instance = NULL; // // Get global variable and its size according to the name given. // - Variable = TerminalGetVariableAndSize ( - VariableName, - &gEfiGlobalVariableGuid, - &VariableSize - ); + GetEfiGlobalVariable2 (VariableName, (VOID**)&Variable, NULL); if (Variable == NULL) { return ; } @@ -955,12 +1531,12 @@ TerminalRemoveConsoleDevVariable ( // Loop through all the terminal types that this driver supports // Match = FALSE; - for (TerminalType = PcAnsiType; TerminalType <= VTUTF8Type; TerminalType++) { + for (TerminalType = 0; TerminalType < ARRAY_SIZE (mTerminalType); TerminalType++) { SetTerminalDevicePath (TerminalType, ParentDevicePath, &TempDevicePath); // - // Compare the genterated device path to the current device path instance + // Compare the generated device path to the current device path instance // if (TempDevicePath != NULL) { if (CompareMem (Instance, TempDevicePath, InstanceSize) == 0) { @@ -1000,6 +1576,9 @@ TerminalRemoveConsoleDevVariable ( VariableSize, NewVariable ); + // + // Shrinking variable with existing variable driver implementation shouldn't fail. + // ASSERT_EFI_ERROR (Status); } @@ -1010,148 +1589,35 @@ TerminalRemoveConsoleDevVariable ( return ; } -VOID * -TerminalGetVariableAndSize ( - IN CHAR16 *Name, - IN EFI_GUID *VendorGuid, - OUT UINTN *VariableSize - ) -/*++ - -Routine Description: - Read the EFI variable (VendorGuid/Name) and return a dynamically allocated - buffer, and the size of the buffer. On failure return NULL. - -Arguments: - Name - String part of EFI variable name - - VendorGuid - GUID part of EFI variable name - - VariableSize - Returns the size of the EFI variable that was read - -Returns: - Dynamically allocated memory that contains a copy of the EFI variable. - Caller is repsoncible freeing the buffer. - - NULL - Variable was not read - ---*/ -{ - EFI_STATUS Status; - UINTN BufferSize; - VOID *Buffer; - - Buffer = NULL; - - // - // Pass in a small size buffer to find the actual variable size. - // - BufferSize = 1; - Buffer = AllocatePool (BufferSize); - if (Buffer == NULL) { - *VariableSize = 0; - return NULL; - } - - Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer); - - if (Status == EFI_SUCCESS) { - *VariableSize = BufferSize; - return Buffer; +/** + Build terminal device path according to terminal type. - } else if (Status == EFI_BUFFER_TOO_SMALL) { - // - // Allocate the buffer to return - // - FreePool (Buffer); - Buffer = AllocatePool (BufferSize); - if (Buffer == NULL) { - *VariableSize = 0; - return NULL; - } - // - // Read variable into the allocated buffer. - // - Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer); - if (EFI_ERROR (Status)) { - BufferSize = 0; - FreePool (Buffer); - Buffer = NULL; - } - } else { - // - // Variable not found or other errors met. - // - BufferSize = 0; - FreePool (Buffer); - Buffer = NULL; - } + @param TerminalType The terminal type is PC ANSI, VT100, VT100+ or VT-UTF8. + @param ParentDevicePath Parent device path. + @param TerminalDevicePath Returned terminal device path, if building successfully. - *VariableSize = BufferSize; - return Buffer; -} + @retval EFI_UNSUPPORTED Terminal does not belong to the supported type. + @retval EFI_OUT_OF_RESOURCES Generate terminal device path failed. + @retval EFI_SUCCESS Build terminal device path successfully. +**/ EFI_STATUS SetTerminalDevicePath ( - IN UINT8 TerminalType, + IN TERMINAL_TYPE TerminalType, IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath, OUT EFI_DEVICE_PATH_PROTOCOL **TerminalDevicePath ) { VENDOR_DEVICE_PATH Node; - *TerminalDevicePath = NULL; + ASSERT (TerminalType < ARRAY_SIZE (mTerminalType)); Node.Header.Type = MESSAGING_DEVICE_PATH; Node.Header.SubType = MSG_VENDOR_DP; + SetDevicePathNodeLength (&Node, sizeof (VENDOR_DEVICE_PATH)); + CopyGuid (&Node.Guid, mTerminalType[TerminalType]); // - // generate terminal device path node according to terminal type. - // - switch (TerminalType) { - - case PcAnsiType: - CopyMem ( - &Node.Guid, - &gEfiPcAnsiGuid, - sizeof (EFI_GUID) - ); - break; - - case VT100Type: - CopyMem ( - &Node.Guid, - &gEfiVT100Guid, - sizeof (EFI_GUID) - ); - break; - - case VT100PlusType: - CopyMem ( - &Node.Guid, - &gEfiVT100PlusGuid, - sizeof (EFI_GUID) - ); - break; - - case VTUTF8Type: - CopyMem ( - &Node.Guid, - &gEfiVTUTF8Guid, - sizeof (EFI_GUID) - ); - break; - - default: - return EFI_UNSUPPORTED; - break; - } - - SetDevicePathNodeLength ( - &Node.Header, - sizeof (VENDOR_DEVICE_PATH) - ); - // - // append the terminal node onto parent device path + // Append the terminal node onto parent device path // to generate a complete terminal device path. // *TerminalDevicePath = AppendDevicePathNode ( @@ -1165,46 +1631,12 @@ SetTerminalDevicePath ( return EFI_SUCCESS; } -VOID -InitializeRawFiFo ( - IN TERMINAL_DEV *TerminalDevice - ) -{ - // - // Make the raw fifo empty. - // - TerminalDevice->RawFiFo.Head = TerminalDevice->RawFiFo.Tail; -} - -VOID -InitializeUnicodeFiFo ( - IN TERMINAL_DEV *TerminalDevice - ) -{ - // - // Make the unicode fifo empty - // - TerminalDevice->UnicodeFiFo.Head = TerminalDevice->UnicodeFiFo.Tail; -} - -VOID -InitializeEfiKeyFiFo ( - IN TERMINAL_DEV *TerminalDevice - ) -{ - // - // Make the efi key fifo empty - // - TerminalDevice->EfiKeyFiFo.Head = TerminalDevice->EfiKeyFiFo.Tail; -} - - /** The user Entry Point for module Terminal. The user code starts with this function. - @param[in] ImageHandle The firmware allocated handle for the EFI image. - @param[in] SystemTable A pointer to the EFI System Table. - + @param ImageHandle The firmware allocated handle for the EFI image. + @param SystemTable A pointer to the EFI System Table. + @retval EFI_SUCCESS The entry point is executed successfully. @retval other Some error occurs when executing this entry point. @@ -1221,17 +1653,63 @@ InitializeTerminal( // // Install driver model protocol(s). // - Status = EfiLibInstallAllDriverProtocols ( + Status = EfiLibInstallDriverBindingComponentName2 ( ImageHandle, SystemTable, &gTerminalDriverBinding, ImageHandle, &gTerminalComponentName, - NULL, - NULL + &gTerminalComponentName2 ); ASSERT_EFI_ERROR (Status); - return Status; } + +/** + Check if the device supports hot-plug through its device path. + + This function could be updated to check more types of Hot Plug devices. + Currently, it checks USB and PCCard device. + + @param DevicePath Pointer to device's device path. + + @retval TRUE The devcie is a hot-plug device + @retval FALSE The devcie is not a hot-plug device. + +**/ +BOOLEAN +IsHotPlugDevice ( + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath + ) +{ + EFI_DEVICE_PATH_PROTOCOL *CheckDevicePath; + + CheckDevicePath = DevicePath; + while (!IsDevicePathEnd (CheckDevicePath)) { + // + // Check device whether is hot plug device or not throught Device Path + // + if ((DevicePathType (CheckDevicePath) == MESSAGING_DEVICE_PATH) && + (DevicePathSubType (CheckDevicePath) == MSG_USB_DP || + DevicePathSubType (CheckDevicePath) == MSG_USB_CLASS_DP || + DevicePathSubType (CheckDevicePath) == MSG_USB_WWID_DP)) { + // + // If Device is USB device + // + return TRUE; + } + if ((DevicePathType (CheckDevicePath) == HARDWARE_DEVICE_PATH) && + (DevicePathSubType (CheckDevicePath) == HW_PCCARD_DP)) { + // + // If Device is PCCard + // + return TRUE; + } + + CheckDevicePath = NextDevicePathNode (CheckDevicePath); + } + + return FALSE; +} +