X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FConsole%2FTerminalDxe%2FTerminalConIn.c;h=016241017a42693ec05ab57fa4d84953256cdd84;hb=47b612db902fcdd133dc69b5db8d304f7498bd10;hp=ae4ea818deb95e63610fe26e5d57ac707c65ee82;hpb=7fc80d44841dbc1f1b68d75312ab7ec9e6d63613;p=mirror_edk2.git diff --git a/MdeModulePkg/Universal/Console/TerminalDxe/TerminalConIn.c b/MdeModulePkg/Universal/Console/TerminalDxe/TerminalConIn.c index ae4ea818de..016241017a 100644 --- a/MdeModulePkg/Universal/Console/TerminalDxe/TerminalConIn.c +++ b/MdeModulePkg/Universal/Console/TerminalDxe/TerminalConIn.c @@ -1,8 +1,10 @@ /** @file Implementation for EFI_SIMPLE_TEXT_INPUT_PROTOCOL protocol. -Copyright (c) 2006 - 2008, Intel Corporation.
-All rights reserved. This program and the accompanying materials +(C) Copyright 2014 Hewlett-Packard Development Company, L.P.
+Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.
+Copyright (C) 2016 Silicon Graphics, Inc. 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 @@ -26,8 +28,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. @retval EFI_SUCCESS The keystroke information was returned. @retval EFI_NOT_READY There was no keystroke data available. - @retval EFI_DEVICE_ERROR The keystroke information was not returned due - to hardware errors. @retval EFI_INVALID_PARAMETER KeyData is NULL. **/ @@ -37,26 +37,10 @@ ReadKeyStrokeWorker ( OUT EFI_KEY_DATA *KeyData ) { - EFI_STATUS Status; - LIST_ENTRY *Link; - LIST_ENTRY *NotifyList; - TERMINAL_CONSOLE_IN_EX_NOTIFY *CurrentNotify; - if (KeyData == NULL) { return EFI_INVALID_PARAMETER; } - // - // Initialize *Key to nonsense value. - // - KeyData->Key.ScanCode = SCAN_NULL; - KeyData->Key.UnicodeChar = 0; - - Status = TerminalConInCheckForKey (&TerminalDevice->SimpleInput); - if (EFI_ERROR (Status)) { - return EFI_NOT_READY; - } - if (!EfiKeyFiFoRemoveOneKey (TerminalDevice, &KeyData->Key)) { return EFI_NOT_READY; } @@ -64,21 +48,6 @@ ReadKeyStrokeWorker ( KeyData->KeyState.KeyShiftState = 0; KeyData->KeyState.KeyToggleState = 0; - // - // Invoke notification functions if exist - // - NotifyList = &TerminalDevice->NotifyList; - for (Link = GetFirstNode (NotifyList); !IsNull (NotifyList,Link); Link = GetNextNode (NotifyList,Link)) { - CurrentNotify = CR ( - Link, - TERMINAL_CONSOLE_IN_EX_NOTIFY, - NotifyEntry, - TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE - ); - if (IsKeyRegistered (&CurrentNotify->KeyData, KeyData)) { - CurrentNotify->KeyNotificationFn (KeyData); - } - } return EFI_SUCCESS; @@ -113,7 +82,7 @@ TerminalConInReset ( // REPORT_STATUS_CODE_WITH_DEVICE_PATH ( EFI_PROGRESS_CODE, - PcdGet32 (PcdStatusCodeValueRemoteConsoleReset), + (EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_PC_RESET), TerminalDevice->DevicePath ); @@ -125,11 +94,12 @@ TerminalConInReset ( TerminalDevice->RawFiFo->Head = TerminalDevice->RawFiFo->Tail; TerminalDevice->UnicodeFiFo->Head = TerminalDevice->UnicodeFiFo->Tail; TerminalDevice->EfiKeyFiFo->Head = TerminalDevice->EfiKeyFiFo->Tail; + TerminalDevice->EfiKeyFiFoForNotify->Head = TerminalDevice->EfiKeyFiFoForNotify->Tail; if (EFI_ERROR (Status)) { REPORT_STATUS_CODE_WITH_DEVICE_PATH ( EFI_ERROR_CODE | EFI_ERROR_MINOR, - PcdGet32 (PcdStatusCodeValueRemoteConsoleError), + (EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_EC_CONTROLLER_ERROR), TerminalDevice->DevicePath ); } @@ -190,7 +160,7 @@ TerminalConInReadKeyStroke ( pressed. @retval TRUE Key be pressed matches a registered key. - @retval FLASE Match failed. + @retval FALSE Match failed. **/ BOOLEAN @@ -226,12 +196,7 @@ TerminalConInWaitForKeyEx ( IN VOID *Context ) { - TERMINAL_DEV *TerminalDevice; - - TerminalDevice = TERMINAL_CON_IN_EX_DEV_FROM_THIS (Context); - - TerminalConInWaitForKey (Event, &TerminalDevice->SimpleInput); - + TerminalConInWaitForKey (Event, Context); } // @@ -333,6 +298,10 @@ TerminalConInSetState ( return EFI_INVALID_PARAMETER; } + if ((*KeyToggleState & EFI_TOGGLE_STATE_VALID) != EFI_TOGGLE_STATE_VALID) { + return EFI_UNSUPPORTED; + } + return EFI_SUCCESS; } @@ -362,7 +331,7 @@ TerminalConInRegisterKeyNotify ( IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, IN EFI_KEY_DATA *KeyData, IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction, - OUT EFI_HANDLE *NotifyHandle + OUT VOID **NotifyHandle ) { TERMINAL_DEV *TerminalDevice; @@ -390,7 +359,7 @@ TerminalConInRegisterKeyNotify ( ); if (IsKeyRegistered (&CurrentNotify->KeyData, KeyData)) { if (CurrentNotify->KeyNotificationFn == KeyNotificationFunction) { - *NotifyHandle = CurrentNotify->NotifyHandle; + *NotifyHandle = CurrentNotify; return EFI_SUCCESS; } } @@ -406,11 +375,10 @@ TerminalConInRegisterKeyNotify ( NewNotify->Signature = TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE; NewNotify->KeyNotificationFn = KeyNotificationFunction; - NewNotify->NotifyHandle = (EFI_HANDLE) NewNotify; - CopyMem (&NewNotify->KeyData, KeyData, sizeof (KeyData)); + CopyMem (&NewNotify->KeyData, KeyData, sizeof (EFI_KEY_DATA)); InsertTailList (&TerminalDevice->NotifyList, &NewNotify->NotifyEntry); - *NotifyHandle = NewNotify->NotifyHandle; + *NotifyHandle = NewNotify; return EFI_SUCCESS; } @@ -426,14 +394,13 @@ TerminalConInRegisterKeyNotify ( @retval EFI_SUCCESS The notification function was unregistered successfully. @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid. - @retval EFI_NOT_FOUND Can not find the matching entry in database. **/ EFI_STATUS EFIAPI TerminalConInUnregisterKeyNotify ( IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, - IN EFI_HANDLE NotificationHandle + IN VOID *NotificationHandle ) { TERMINAL_DEV *TerminalDevice; @@ -455,7 +422,7 @@ TerminalConInUnregisterKeyNotify ( NotifyEntry, TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE ); - if (CurrentNotify->NotifyHandle == NotificationHandle) { + if (CurrentNotify == NotificationHandle) { // // Remove the notification function from NotifyList and free resources // @@ -466,7 +433,10 @@ TerminalConInUnregisterKeyNotify ( } } - return EFI_NOT_FOUND; + // + // Can not find the matching entry in database. + // + return EFI_INVALID_PARAMETER; } /** @@ -486,6 +456,7 @@ TranslateRawDataToEfiKey ( case PCANSITYPE: case VT100TYPE: case VT100PLUSTYPE: + case TTYTERMTYPE: AnsiRawDataToUnicode (TerminalDevice); UnicodeToEfiKey (TerminalDevice); break; @@ -526,28 +497,23 @@ TerminalConInWaitForKey ( // Someone is waiting on the keystroke event, if there's // a key pending, signal the event // - // Context is the pointer to EFI_SIMPLE_TEXT_INPUT_PROTOCOL - // - if (!EFI_ERROR (TerminalConInCheckForKey (Context))) { + if (!IsEfiKeyFiFoEmpty ((TERMINAL_DEV *) Context)) { gBS->SignalEvent (Event); } } - /** - Check for a pending key in the Efi Key FIFO or Serial device buffer. - - @param This Indicates the calling context. - - @retval EFI_SUCCESS There is key pending. - @retval EFI_NOT_READY There is no key pending. - @retval EFI_DEVICE_ERROR If Serial IO is not attached to serial device. + Timer handler to poll the key from serial. + @param Event Indicates the event that invoke this function. + @param Context Indicates the calling context. **/ -EFI_STATUS -TerminalConInCheckForKey ( - IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This +VOID +EFIAPI +TerminalConInTimerHandler ( + IN EFI_EVENT Event, + IN VOID *Context ) { EFI_STATUS Status; @@ -558,11 +524,11 @@ TerminalConInCheckForKey ( EFI_SERIAL_IO_PROTOCOL *SerialIo; UINTN SerialInTimeOut; - TerminalDevice = TERMINAL_CON_IN_DEV_FROM_THIS (This); + TerminalDevice = (TERMINAL_DEV *) Context; SerialIo = TerminalDevice->SerialIo; if (SerialIo == NULL) { - return EFI_DEVICE_ERROR; + return ; } // // if current timeout value for serial device is not identical with @@ -597,60 +563,93 @@ TerminalConInCheckForKey ( } } // - // Check whether serial buffer is empty. + // Check whether serial buffer is empty. + // Skip the key transfer loop only if the SerialIo protocol instance + // successfully reports EFI_SERIAL_INPUT_BUFFER_EMPTY. // Status = SerialIo->GetControl (SerialIo, &Control); - - if ((Control & EFI_SERIAL_INPUT_BUFFER_EMPTY) != 0) { + if (EFI_ERROR (Status) || ((Control & EFI_SERIAL_INPUT_BUFFER_EMPTY) == 0)) { // - // Translate all the raw data in RawFIFO into EFI Key, - // according to different terminal type supported. + // Fetch all the keys in the serial buffer, + // and insert the byte stream into RawFIFO. // - TranslateRawDataToEfiKey (TerminalDevice); + while (!IsRawFiFoFull (TerminalDevice)) { - // - // if there is pre-fetched Efi Key in EfiKeyFIFO buffer, - // return directly. - // - if (!IsEfiKeyFiFoEmpty (TerminalDevice)) { - return EFI_SUCCESS; - } else { - return EFI_NOT_READY; - } - } - // - // Fetch all the keys in the serial buffer, - // and insert the byte stream into RawFIFO. - // - do { + Status = GetOneKeyFromSerial (TerminalDevice->SerialIo, &Input); - Status = GetOneKeyFromSerial (TerminalDevice->SerialIo, &Input); - - if (EFI_ERROR (Status)) { - if (Status == EFI_DEVICE_ERROR) { - REPORT_STATUS_CODE_WITH_DEVICE_PATH ( - EFI_ERROR_CODE | EFI_ERROR_MINOR, - PcdGet32 (PcdStatusCodeValueRemoteConsoleInputError), - TerminalDevice->DevicePath - ); + if (EFI_ERROR (Status)) { + if (Status == EFI_DEVICE_ERROR) { + REPORT_STATUS_CODE_WITH_DEVICE_PATH ( + EFI_ERROR_CODE | EFI_ERROR_MINOR, + (EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_EC_INPUT_ERROR), + TerminalDevice->DevicePath + ); + } + break; } - break; - } - RawFiFoInsertOneKey (TerminalDevice, Input); - } while (TRUE); + RawFiFoInsertOneKey (TerminalDevice, Input); + } + } // // Translate all the raw data in RawFIFO into EFI Key, // according to different terminal type supported. // TranslateRawDataToEfiKey (TerminalDevice); +} - if (IsEfiKeyFiFoEmpty (TerminalDevice)) { - return EFI_NOT_READY; - } +/** + Process key notify. - return EFI_SUCCESS; + @param Event Indicates the event that invoke this function. + @param Context Indicates the calling context. +**/ +VOID +EFIAPI +KeyNotifyProcessHandler ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + BOOLEAN HasKey; + TERMINAL_DEV *TerminalDevice; + EFI_INPUT_KEY Key; + EFI_KEY_DATA KeyData; + LIST_ENTRY *Link; + LIST_ENTRY *NotifyList; + TERMINAL_CONSOLE_IN_EX_NOTIFY *CurrentNotify; + EFI_TPL OldTpl; + + TerminalDevice = (TERMINAL_DEV *) Context; + + // + // Invoke notification functions. + // + NotifyList = &TerminalDevice->NotifyList; + while (TRUE) { + // + // Enter critical section + // + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + HasKey = EfiKeyFiFoForNotifyRemoveOneKey (TerminalDevice->EfiKeyFiFoForNotify, &Key); + CopyMem (&KeyData.Key, &Key, sizeof (EFI_INPUT_KEY)); + KeyData.KeyState.KeyShiftState = 0; + KeyData.KeyState.KeyToggleState = 0; + // + // Leave critical section + // + gBS->RestoreTPL (OldTpl); + if (!HasKey) { + break; + } + for (Link = GetFirstNode (NotifyList); !IsNull (NotifyList, Link); Link = GetNextNode (NotifyList, Link)) { + CurrentNotify = CR (Link, TERMINAL_CONSOLE_IN_EX_NOTIFY, NotifyEntry, TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE); + if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) { + CurrentNotify->KeyNotificationFn (&KeyData); + } + } + } } /** @@ -706,7 +705,7 @@ GetOneKeyFromSerial ( @param Input The key will be input. @retval TRUE If insert successfully. - @retval FLASE If Raw Data buffer is full before key insertion, + @retval FALSE If Raw Data buffer is full before key insertion, and the key is lost. **/ @@ -741,7 +740,7 @@ RawFiFoInsertOneKey ( @param Output The key will be removed. @retval TRUE If insert successfully. - @retval FLASE If Raw Data FIFO buffer is empty before remove operation. + @retval FALSE If Raw Data FIFO buffer is empty before remove operation. **/ BOOLEAN @@ -775,7 +774,7 @@ RawFiFoRemoveOneKey ( @param TerminalDevice Terminal driver private structure @retval TRUE If Raw Data FIFO buffer is empty. - @retval FLASE If Raw Data FIFO buffer is not empty. + @retval FALSE If Raw Data FIFO buffer is not empty. **/ BOOLEAN @@ -796,7 +795,7 @@ IsRawFiFoEmpty ( @param TerminalDevice Terminal driver private structure @retval TRUE If Raw Data FIFO buffer is full. - @retval FLASE If Raw Data FIFO buffer is not full. + @retval FALSE If Raw Data FIFO buffer is not full. **/ BOOLEAN @@ -818,6 +817,126 @@ IsRawFiFoFull ( return FALSE; } +/** + Insert one pre-fetched key into the FIFO buffer. + + @param EfiKeyFiFo Pointer to instance of EFI_KEY_FIFO. + @param Input The key will be input. + + @retval TRUE If insert successfully. + @retval FALSE If FIFO buffer is full before key insertion, + and the key is lost. + +**/ +BOOLEAN +EfiKeyFiFoForNotifyInsertOneKey ( + EFI_KEY_FIFO *EfiKeyFiFo, + EFI_INPUT_KEY *Input + ) +{ + UINT8 Tail; + + Tail = EfiKeyFiFo->Tail; + + if (IsEfiKeyFiFoForNotifyFull (EfiKeyFiFo)) { + // + // FIFO is full + // + return FALSE; + } + + CopyMem (&EfiKeyFiFo->Data[Tail], Input, sizeof (EFI_INPUT_KEY)); + + EfiKeyFiFo->Tail = (UINT8) ((Tail + 1) % (FIFO_MAX_NUMBER + 1)); + + return TRUE; +} + +/** + Remove one pre-fetched key out of the FIFO buffer. + + @param EfiKeyFiFo Pointer to instance of EFI_KEY_FIFO. + @param Output The key will be removed. + + @retval TRUE If remove successfully. + @retval FALSE If FIFO buffer is empty before remove operation. + +**/ +BOOLEAN +EfiKeyFiFoForNotifyRemoveOneKey ( + EFI_KEY_FIFO *EfiKeyFiFo, + EFI_INPUT_KEY *Output + ) +{ + UINT8 Head; + + Head = EfiKeyFiFo->Head; + ASSERT (Head < FIFO_MAX_NUMBER + 1); + + if (IsEfiKeyFiFoForNotifyEmpty (EfiKeyFiFo)) { + // + // FIFO is empty + // + Output->ScanCode = SCAN_NULL; + Output->UnicodeChar = 0; + return FALSE; + } + + CopyMem (Output, &EfiKeyFiFo->Data[Head], sizeof (EFI_INPUT_KEY)); + + EfiKeyFiFo->Head = (UINT8) ((Head + 1) % (FIFO_MAX_NUMBER + 1)); + + return TRUE; +} + +/** + Clarify whether FIFO buffer is empty. + + @param EfiKeyFiFo Pointer to instance of EFI_KEY_FIFO. + + @retval TRUE If FIFO buffer is empty. + @retval FALSE If FIFO buffer is not empty. + +**/ +BOOLEAN +IsEfiKeyFiFoForNotifyEmpty ( + EFI_KEY_FIFO *EfiKeyFiFo + ) +{ + if (EfiKeyFiFo->Head == EfiKeyFiFo->Tail) { + return TRUE; + } else { + return FALSE; + } +} + +/** + Clarify whether FIFO buffer is full. + + @param EfiKeyFiFo Pointer to instance of EFI_KEY_FIFO. + + @retval TRUE If FIFO buffer is full. + @retval FALSE If FIFO buffer is not full. + +**/ +BOOLEAN +IsEfiKeyFiFoForNotifyFull ( + EFI_KEY_FIFO *EfiKeyFiFo + ) +{ + UINT8 Tail; + UINT8 Head; + + Tail = EfiKeyFiFo->Tail; + Head = EfiKeyFiFo->Head; + + if (((Tail + 1) % (FIFO_MAX_NUMBER + 1)) == Head) { + return TRUE; + } + + return FALSE; +} + /** Insert one pre-fetched key into the FIFO buffer. @@ -825,21 +944,49 @@ IsRawFiFoFull ( @param Key The key will be input. @retval TRUE If insert successfully. - @retval FLASE If FIFO buffer is full before key insertion, + @retval FALSE If FIFO buffer is full before key insertion, and the key is lost. **/ BOOLEAN EfiKeyFiFoInsertOneKey ( - TERMINAL_DEV *TerminalDevice, - EFI_INPUT_KEY Key + TERMINAL_DEV *TerminalDevice, + EFI_INPUT_KEY *Key ) { - UINT8 Tail; + UINT8 Tail; + LIST_ENTRY *Link; + LIST_ENTRY *NotifyList; + TERMINAL_CONSOLE_IN_EX_NOTIFY *CurrentNotify; + EFI_KEY_DATA KeyData; Tail = TerminalDevice->EfiKeyFiFo->Tail; - ASSERT (Tail < FIFO_MAX_NUMBER + 1); + CopyMem (&KeyData.Key, Key, sizeof (EFI_INPUT_KEY)); + KeyData.KeyState.KeyShiftState = 0; + KeyData.KeyState.KeyToggleState = 0; + + // + // Signal KeyNotify process event if this key pressed matches any key registered. + // + NotifyList = &TerminalDevice->NotifyList; + for (Link = GetFirstNode (NotifyList); !IsNull (NotifyList,Link); Link = GetNextNode (NotifyList,Link)) { + CurrentNotify = CR ( + Link, + TERMINAL_CONSOLE_IN_EX_NOTIFY, + NotifyEntry, + TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE + ); + if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) { + // + // The key notification function needs to run at TPL_CALLBACK + // while current TPL is TPL_NOTIFY. It will be invoked in + // KeyNotifyProcessHandler() which runs at TPL_CALLBACK. + // + EfiKeyFiFoForNotifyInsertOneKey (TerminalDevice->EfiKeyFiFoForNotify, Key); + gBS->SignalEvent (TerminalDevice->KeyNotifyProcessEvent); + } + } if (IsEfiKeyFiFoFull (TerminalDevice)) { // // Efi Key FIFO is full @@ -847,9 +994,9 @@ EfiKeyFiFoInsertOneKey ( return FALSE; } - TerminalDevice->EfiKeyFiFo->Data[Tail] = Key; + CopyMem (&TerminalDevice->EfiKeyFiFo->Data[Tail], Key, sizeof (EFI_INPUT_KEY)); - TerminalDevice->EfiKeyFiFo->Tail = (UINT8) ((Tail + 1) % (FIFO_MAX_NUMBER + 1)); + TerminalDevice->EfiKeyFiFo->Tail = (UINT8) ((Tail + 1) % (FIFO_MAX_NUMBER + 1)); return TRUE; } @@ -861,7 +1008,7 @@ EfiKeyFiFoInsertOneKey ( @param Output The key will be removed. @retval TRUE If insert successfully. - @retval FLASE If FIFO buffer is empty before remove operation. + @retval FALSE If FIFO buffer is empty before remove operation. **/ BOOLEAN @@ -884,7 +1031,7 @@ EfiKeyFiFoRemoveOneKey ( return FALSE; } - *Output = TerminalDevice->EfiKeyFiFo->Data[Head]; + CopyMem (Output, &TerminalDevice->EfiKeyFiFo->Data[Head], sizeof (EFI_INPUT_KEY)); TerminalDevice->EfiKeyFiFo->Head = (UINT8) ((Head + 1) % (FIFO_MAX_NUMBER + 1)); @@ -897,7 +1044,7 @@ EfiKeyFiFoRemoveOneKey ( @param TerminalDevice Terminal driver private structure @retval TRUE If FIFO buffer is empty. - @retval FLASE If FIFO buffer is not empty. + @retval FALSE If FIFO buffer is not empty. **/ BOOLEAN @@ -918,7 +1065,7 @@ IsEfiKeyFiFoEmpty ( @param TerminalDevice Terminal driver private structure @retval TRUE If FIFO buffer is full. - @retval FLASE If FIFO buffer is not full. + @retval FALSE If FIFO buffer is not full. **/ BOOLEAN @@ -947,7 +1094,7 @@ IsEfiKeyFiFoFull ( @param Input The key will be input. @retval TRUE If insert successfully. - @retval FLASE If Unicode FIFO buffer is full before key insertion, + @retval FALSE If Unicode FIFO buffer is full before key insertion, and the key is lost. **/ @@ -979,15 +1126,14 @@ UnicodeFiFoInsertOneKey ( /** Remove one pre-fetched key out of the Unicode FIFO buffer. + The caller should guarantee that Unicode FIFO buffer is not empty + by IsUnicodeFiFoEmpty (). @param TerminalDevice Terminal driver private structure. @param Output The key will be removed. - @retval TRUE If insert successfully. - @retval FLASE If Unicode FIFO buffer is empty before remove operation. - **/ -BOOLEAN +VOID UnicodeFiFoRemoveOneKey ( TERMINAL_DEV *TerminalDevice, UINT16 *Output @@ -998,19 +1144,9 @@ UnicodeFiFoRemoveOneKey ( Head = TerminalDevice->UnicodeFiFo->Head; ASSERT (Head < FIFO_MAX_NUMBER + 1); - if (IsUnicodeFiFoEmpty (TerminalDevice)) { - // - // FIFO is empty - // - Output = NULL; - return FALSE; - } - *Output = TerminalDevice->UnicodeFiFo->Data[Head]; TerminalDevice->UnicodeFiFo->Head = (UINT8) ((Head + 1) % (FIFO_MAX_NUMBER + 1)); - - return TRUE; } /** @@ -1019,7 +1155,7 @@ UnicodeFiFoRemoveOneKey ( @param TerminalDevice Terminal driver private structure @retval TRUE If Unicode FIFO buffer is empty. - @retval FLASE If Unicode FIFO buffer is not empty. + @retval FALSE If Unicode FIFO buffer is not empty. **/ BOOLEAN @@ -1040,7 +1176,7 @@ IsUnicodeFiFoEmpty ( @param TerminalDevice Terminal driver private structure @retval TRUE If Unicode FIFO buffer is full. - @retval FLASE If Unicode FIFO buffer is not full. + @retval FALSE If Unicode FIFO buffer is not full. **/ BOOLEAN @@ -1104,34 +1240,38 @@ UnicodeToEfiKeyFlushState ( InputState = TerminalDevice->InputState; + if (IsEfiKeyFiFoFull (TerminalDevice)) { + return; + } + if ((InputState & INPUT_STATE_ESC) != 0) { Key.ScanCode = SCAN_ESC; Key.UnicodeChar = 0; - EfiKeyFiFoInsertOneKey (TerminalDevice, Key); + EfiKeyFiFoInsertOneKey (TerminalDevice, &Key); } if ((InputState & INPUT_STATE_CSI) != 0) { Key.ScanCode = SCAN_NULL; Key.UnicodeChar = CSI; - EfiKeyFiFoInsertOneKey (TerminalDevice, Key); + EfiKeyFiFoInsertOneKey (TerminalDevice, &Key); } if ((InputState & INPUT_STATE_LEFTOPENBRACKET) != 0) { Key.ScanCode = SCAN_NULL; Key.UnicodeChar = LEFTOPENBRACKET; - EfiKeyFiFoInsertOneKey (TerminalDevice, Key); + EfiKeyFiFoInsertOneKey (TerminalDevice, &Key); } if ((InputState & INPUT_STATE_O) != 0) { Key.ScanCode = SCAN_NULL; Key.UnicodeChar = 'O'; - EfiKeyFiFoInsertOneKey (TerminalDevice, Key); + EfiKeyFiFoInsertOneKey (TerminalDevice, &Key); } if ((InputState & INPUT_STATE_2) != 0) { Key.ScanCode = SCAN_NULL; Key.UnicodeChar = '2'; - EfiKeyFiFoInsertOneKey (TerminalDevice, Key); + EfiKeyFiFoInsertOneKey (TerminalDevice, &Key); } // @@ -1162,6 +1302,10 @@ UnicodeToEfiKeyFlushState ( There is one special input sequence that will force the system to reset. This is ESC R ESC r ESC R. + Note: current implementation support terminal types include: PC ANSI, VT100+/VTUTF8, VT100. + The table below is not same with UEFI Spec 2.3 Appendix B Table 201(not support ANSI X3.64 / + DEC VT200-500 and extra support PC ANSI, VT100)since UEFI Table 201 is just an example. + Symbols used in table below =========================== ESC = 0x1B @@ -1228,7 +1372,7 @@ UnicodeToEfiKey ( TerminalDevice->ResetState = RESET_STATE_DEFAULT; } - while (!IsUnicodeFiFoEmpty(TerminalDevice)) { + while (!IsUnicodeFiFoEmpty (TerminalDevice) && !IsEfiKeyFiFoFull (TerminalDevice)) { if (TerminalDevice->InputState != INPUT_STATE_DEFAULT) { // @@ -1261,7 +1405,8 @@ UnicodeToEfiKey ( continue; } - if (UnicodeChar == 'O' && TerminalDevice->TerminalType == VT100TYPE) { + if (UnicodeChar == 'O' && (TerminalDevice->TerminalType == VT100TYPE || + TerminalDevice->TerminalType == TTYTERMTYPE)) { TerminalDevice->InputState |= INPUT_STATE_O; TerminalDevice->ResetState = RESET_STATE_DEFAULT; continue; @@ -1336,14 +1481,14 @@ UnicodeToEfiKey ( if (TerminalDevice->ResetState == RESET_STATE_DEFAULT) { TerminalDevice->ResetState = RESET_STATE_ESC_R; SetDefaultResetState = FALSE; - } else if (TerminalDevice->ResetState == RESET_STATE_ESC_R_ESC_r) { + } else if (TerminalDevice->ResetState == RESET_STATE_ESC_R_ESC_R) { gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL); } Key.ScanCode = SCAN_NULL; break; case 'r': if (TerminalDevice->ResetState == RESET_STATE_ESC_R) { - TerminalDevice->ResetState = RESET_STATE_ESC_R_ESC_r; + TerminalDevice->ResetState = RESET_STATE_ESC_R_ESC_R; SetDefaultResetState = FALSE; } Key.ScanCode = SCAN_NULL; @@ -1358,7 +1503,7 @@ UnicodeToEfiKey ( if (Key.ScanCode != SCAN_NULL) { Key.UnicodeChar = 0; - EfiKeyFiFoInsertOneKey (TerminalDevice,Key); + EfiKeyFiFoInsertOneKey (TerminalDevice, &Key); TerminalDevice->InputState = INPUT_STATE_DEFAULT; UnicodeToEfiKeyFlushState (TerminalDevice); continue; @@ -1409,11 +1554,33 @@ UnicodeToEfiKey ( default : break; } + } else if (TerminalDevice->TerminalType == TTYTERMTYPE) { + /* Also accept VT100 escape codes for F1-F4, HOME and END for TTY term */ + switch (UnicodeChar) { + case 'P': + Key.ScanCode = SCAN_F1; + break; + case 'Q': + Key.ScanCode = SCAN_F2; + break; + case 'R': + Key.ScanCode = SCAN_F3; + break; + case 'S': + Key.ScanCode = SCAN_F4; + break; + case 'H': + Key.ScanCode = SCAN_HOME; + break; + case 'F': + Key.ScanCode = SCAN_END; + break; + } } if (Key.ScanCode != SCAN_NULL) { Key.UnicodeChar = 0; - EfiKeyFiFoInsertOneKey (TerminalDevice,Key); + EfiKeyFiFoInsertOneKey (TerminalDevice, &Key); TerminalDevice->InputState = INPUT_STATE_DEFAULT; UnicodeToEfiKeyFlushState (TerminalDevice); continue; @@ -1432,7 +1599,8 @@ UnicodeToEfiKey ( if (TerminalDevice->TerminalType == PCANSITYPE || TerminalDevice->TerminalType == VT100TYPE || TerminalDevice->TerminalType == VT100PLUSTYPE || - TerminalDevice->TerminalType == VTUTF8TYPE) { + TerminalDevice->TerminalType == VTUTF8TYPE || + TerminalDevice->TerminalType == TTYTERMTYPE) { switch (UnicodeChar) { case 'A': Key.ScanCode = SCAN_UP; @@ -1448,12 +1616,14 @@ UnicodeToEfiKey ( break; case 'H': if (TerminalDevice->TerminalType == PCANSITYPE || - TerminalDevice->TerminalType == VT100TYPE) { + TerminalDevice->TerminalType == VT100TYPE || + TerminalDevice->TerminalType == TTYTERMTYPE) { Key.ScanCode = SCAN_HOME; } break; case 'F': - if (TerminalDevice->TerminalType == PCANSITYPE) { + if (TerminalDevice->TerminalType == PCANSITYPE || + TerminalDevice->TerminalType == TTYTERMTYPE) { Key.ScanCode = SCAN_END; } break; @@ -1490,6 +1660,7 @@ UnicodeToEfiKey ( if (TerminalDevice->TerminalType == PCANSITYPE) { Key.ScanCode = SCAN_F10; } + break; case '?': if (TerminalDevice->TerminalType == VT100TYPE) { Key.ScanCode = SCAN_PAGE_UP; @@ -1504,6 +1675,7 @@ UnicodeToEfiKey ( if (TerminalDevice->TerminalType == PCANSITYPE) { Key.ScanCode = SCAN_F9; } + break; case '/': if (TerminalDevice->TerminalType == VT100TYPE) { Key.ScanCode = SCAN_PAGE_DOWN; @@ -1549,9 +1721,24 @@ UnicodeToEfiKey ( } } + /* + * The VT220 escape codes that the TTY terminal accepts all have + * numeric codes, and there are no ambiguous prefixes shared with + * other terminal types. + */ + if (TerminalDevice->TerminalType == TTYTERMTYPE && + Key.ScanCode == SCAN_NULL && + UnicodeChar >= '0' && + UnicodeChar <= '9') { + TerminalDevice->TtyEscapeStr[0] = UnicodeChar; + TerminalDevice->TtyEscapeIndex = 1; + TerminalDevice->InputState |= INPUT_STATE_LEFTOPENBRACKET_2; + continue; + } + if (Key.ScanCode != SCAN_NULL) { Key.UnicodeChar = 0; - EfiKeyFiFoInsertOneKey (TerminalDevice,Key); + EfiKeyFiFoInsertOneKey (TerminalDevice, &Key); TerminalDevice->InputState = INPUT_STATE_DEFAULT; UnicodeToEfiKeyFlushState (TerminalDevice); continue; @@ -1562,6 +1749,74 @@ UnicodeToEfiKey ( break; + case INPUT_STATE_ESC | INPUT_STATE_LEFTOPENBRACKET | INPUT_STATE_LEFTOPENBRACKET_2: + /* + * Here we handle the VT220 escape codes that we accept. This + * state is only used by the TTY terminal type. + */ + Key.ScanCode = SCAN_NULL; + if (TerminalDevice->TerminalType == TTYTERMTYPE) { + + if (UnicodeChar == '~' && TerminalDevice->TtyEscapeIndex <= 2) { + UINT16 EscCode; + TerminalDevice->TtyEscapeStr[TerminalDevice->TtyEscapeIndex] = 0; /* Terminate string */ + EscCode = (UINT16) StrDecimalToUintn(TerminalDevice->TtyEscapeStr); + switch (EscCode) { + case 2: + Key.ScanCode = SCAN_INSERT; + break; + case 3: + Key.ScanCode = SCAN_DELETE; + break; + case 5: + Key.ScanCode = SCAN_PAGE_UP; + break; + case 6: + Key.ScanCode = SCAN_PAGE_DOWN; + break; + case 11: + case 12: + case 13: + case 14: + case 15: + Key.ScanCode = SCAN_F1 + EscCode - 11; + break; + case 17: + case 18: + case 19: + case 20: + case 21: + Key.ScanCode = SCAN_F6 + EscCode - 17; + break; + case 23: + case 24: + Key.ScanCode = SCAN_F11 + EscCode - 23; + break; + default: + break; + } + } else if (TerminalDevice->TtyEscapeIndex == 1){ + /* 2 character escape code */ + TerminalDevice->TtyEscapeStr[TerminalDevice->TtyEscapeIndex++] = UnicodeChar; + continue; + } + else { + DEBUG ((EFI_D_ERROR, "Unexpected state in escape2\n")); + } + } + TerminalDevice->ResetState = RESET_STATE_DEFAULT; + + if (Key.ScanCode != SCAN_NULL) { + Key.UnicodeChar = 0; + EfiKeyFiFoInsertOneKey (TerminalDevice, &Key); + TerminalDevice->InputState = INPUT_STATE_DEFAULT; + UnicodeToEfiKeyFlushState (TerminalDevice); + continue; + } + + UnicodeToEfiKeyFlushState (TerminalDevice); + break; + default: // // Invalid state. This should never happen. @@ -1596,13 +1851,19 @@ UnicodeToEfiKey ( } if (UnicodeChar == DEL) { - Key.ScanCode = SCAN_DELETE; - Key.UnicodeChar = 0; + if (TerminalDevice->TerminalType == TTYTERMTYPE) { + Key.ScanCode = SCAN_NULL; + Key.UnicodeChar = CHAR_BACKSPACE; + } + else { + Key.ScanCode = SCAN_DELETE; + Key.UnicodeChar = 0; + } } else { Key.ScanCode = SCAN_NULL; Key.UnicodeChar = UnicodeChar; } - EfiKeyFiFoInsertOneKey (TerminalDevice,Key); + EfiKeyFiFoInsertOneKey (TerminalDevice, &Key); } }