Skip to content

Commit

Permalink
EmulatorPkg: Fix Terminal Issues
Browse files Browse the repository at this point in the history
After running EmulatorPkg, one will notice that their terminal acts
strangely. This is caused by the EmulatorPkg Host changing the terminal
mode and not restoring the original mode, which is now fixed.

Reviewed-by: Michael D Kinney <[email protected]>
Cc: Andrew Fish <[email protected]>
Cc: Ray Ni <[email protected]>
Cc: Chasel Chiu <[email protected]>
Signed-off-by: Nate DeSimone <[email protected]>
  • Loading branch information
nate-desimone authored and mergify[bot] committed Sep 27, 2023
1 parent be971fc commit ad1c039
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
18 changes: 17 additions & 1 deletion EmulatorPkg/Unix/Host/EmuThunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
it may cause the table to be initialized with the members at the end being
set to zero. This is bad as jumping to zero will crash.
Copyright (c) 2004 - 2019, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2004 - 2023, Intel Corporation. All rights reserved.<BR>
Portions copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
Expand All @@ -34,6 +34,9 @@ UINTN settimer_callback = 0;

BOOLEAN gEmulatorInterruptEnabled = FALSE;

STATIC BOOLEAN mEmulatorStdInConfigured = FALSE;
STATIC struct termios mOldTty;

UINTN
SecWriteStdErr (
IN UINT8 *Buffer,
Expand All @@ -58,8 +61,16 @@ SecConfigStdIn (
// Need to turn off line buffering, ECHO, and make it unbuffered.
//
tcgetattr (STDIN_FILENO, &tty);
if (!mEmulatorStdInConfigured) {
//
// Save the original state of the TTY so it can be restored on exit
//
CopyMem (&mOldTty, &tty, sizeof (struct termios));
}

tty.c_lflag &= ~(ICANON | ECHO);
tcsetattr (STDIN_FILENO, TCSANOW, &tty);
mEmulatorStdInConfigured = TRUE;

// setvbuf (STDIN_FILENO, NULL, _IONBF, 0);

Expand Down Expand Up @@ -338,6 +349,11 @@ SecExit (
UINTN Status
)
{
// Reset the TTY back to its original state
if (mEmulatorStdInConfigured) {
tcsetattr (STDIN_FILENO, TCSANOW, &mOldTty);
}

exit (Status);
}

Expand Down
41 changes: 40 additions & 1 deletion EmulatorPkg/Win/Host/WinThunk.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**@file
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
Module Name:
Expand Down Expand Up @@ -30,6 +30,12 @@ Module Name:

#include "WinHost.h"

STATIC BOOLEAN mEmulatorStdInConfigured = FALSE;
STATIC DWORD mOldStdInMode;
#if defined (NTDDI_VERSION) && defined (NTDDI_WIN10_TH2) && (NTDDI_VERSION > NTDDI_WIN10_TH2)
STATIC DWORD mOldStdOutMode;
#endif

UINTN
SecWriteStdErr (
IN UINT8 *Buffer,
Expand Down Expand Up @@ -61,6 +67,13 @@ SecConfigStdIn (

Success = GetConsoleMode (GetStdHandle (STD_INPUT_HANDLE), &Mode);
if (Success) {
if (!mEmulatorStdInConfigured) {
//
// Save the original state of the console so it can be restored on exit
//
mOldStdInMode = Mode;
}

//
// Disable buffer (line input), echo, mouse, window
//
Expand All @@ -82,6 +95,13 @@ SecConfigStdIn (
//
if (Success) {
Success = GetConsoleMode (GetStdHandle (STD_OUTPUT_HANDLE), &Mode);
if (!mEmulatorStdInConfigured) {
//
// Save the original state of the console so it can be restored on exit
//
mOldStdOutMode = Mode;
}

if (Success) {
Success = SetConsoleMode (
GetStdHandle (STD_OUTPUT_HANDLE),
Expand All @@ -91,6 +111,10 @@ SecConfigStdIn (
}

#endif
if (Success) {
mEmulatorStdInConfigured = TRUE;
}

return Success ? EFI_SUCCESS : EFI_DEVICE_ERROR;
}

Expand Down Expand Up @@ -467,6 +491,21 @@ SecExit (
UINTN Status
)
{
if (mEmulatorStdInConfigured) {
//
// Reset the console back to its original state
//
#if defined (NTDDI_VERSION) && defined (NTDDI_WIN10_TH2) && (NTDDI_VERSION > NTDDI_WIN10_TH2)
BOOL Success = SetConsoleMode (GetStdHandle (STD_INPUT_HANDLE), mOldStdInMode);
if (Success) {
SetConsoleMode (GetStdHandle (STD_OUTPUT_HANDLE), mOldStdOutMode);
}

#else
SetConsoleMode (GetStdHandle (STD_INPUT_HANDLE), mOldStdInMode);
#endif
}

exit ((int)Status);
}

Expand Down

0 comments on commit ad1c039

Please sign in to comment.