GNU-EFI how to get keyboard events ?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
gomidas
Posts: 15
Joined: Wed Nov 25, 2020 9:28 pm

GNU-EFI how to get keyboard events ?

Post by gomidas »

Hello, I use uefi on vs2019 project. I based on sample code which found on osdev.org

I test the code on QEMU. My issue is whenever I press a key my program/system shutsdown immediately. Somehow I found a way to wait for a key and print a text but still I am doing it wrong. I want to re-wait for new keys unless that is "ESC" key. If key is esc program will exit. This is my first purpose. Second, if possible I want to print all characters when they are pressed.

Here is code:

Code: Select all

/*
 * UEFI:SIMPLE - UEFI development made easy
 * Copyright © 2014-2018 Pete Batard <[email protected]> - Public Domain
 * See COPYING for the full licensing terms.
 */
#include <efi.h>
#include <efilib.h>
#include <stdio.h>

// Application entrypoint (must be set to 'efi_main' for gnu-efi crt0 compatibility)
EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
	UINTN Event;

#if defined(_GNU_EFI)
	InitializeLib(ImageHandle, SystemTable);
#endif

	/*
	 * In addition to the standard %-based flags, Print() supports the following:
	 *   %N       Set output attribute to normal
	 *   %H       Set output attribute to highlight
	 *   %E       Set output attribute to error
	 *   %B       Set output attribute to blue color
	 *   %V       Set output attribute to green color
	 *   %r       Human readable version of a status code
	 */

	uefi_call_wrapper(SystemTable->ConOut->SetAttribute, 1, SystemTable->ConOut, EFI_BACKGROUND_MAGENTA); //SET BACK COLOR	
	Print(L"\n***DO NOT DISPLAY THIS***%N\n\n");
	uefi_call_wrapper(SystemTable->ConOut->ClearScreen, 1, SystemTable->ConOut); //CLEAR SCREEN

	uefi_call_wrapper(SystemTable->ConOut->SetAttribute, 1, SystemTable->ConOut, EFI_YELLOW); //SET TEXT COLOR

	Print(L"\n*** UEFI:HELLO MY FRIEND, bla bla and some other very necessary messages... ***%N\n\n");
	uefi_call_wrapper(SystemTable->ConOut->SetAttribute, 1, SystemTable->ConOut, EFI_RED); //SET TEXT COLOR
	Print(L"PRESS ESC KEY TO EXIT.%N\n");

	SystemTable->ConIn->Reset(SystemTable->ConIn, FALSE);

#if defined(_DEBUG)
	// If running in debug mode, use the EFI shut down call to close QEMU
	SystemTable->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
#endif

	return EFI_SUCCESS;
}

I found methods but I do not know how to use them, at least it does not work for debug mode but works on release mode:

Code: Select all

SystemTable->BootServices->WaitForEvent(1, &SystemTable->ConIn->WaitForKey, &Event);
SystemTable->BootServices->WaitForEvent(1, &SystemTable->ConIn->ReadKeyStroke, &Event);
uefi_call_wrapper(SystemTable->ConOut->OutputString,1, SystemTable->ConOut , L"test\n");
what should I do ?
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: GNU-EFI how to get keyboard events ?

Post by Octocontrabass »

Are you using GNU-EFI? If you are, you're using uefi_call_wrapper() incorrectly. If you're not, you shouldn't be using uefi_call_wrapper() at all.
User avatar
zaval
Member
Member
Posts: 656
Joined: Fri Feb 17, 2017 4:01 pm
Location: Ukraine, Bachmut
Contact:

Re: GNU-EFI how to get keyboard events ?

Post by zaval »

first, if you use vs2019, then please, don't complicate yourself life and ditch that gnu-efi. use msvc. next, how you can get how to use key event functions? please, read the UEFI specification. it's the easiest way. here is how you could play with key pressing, in this example, the loader shuts down the system if "s" is pressed, you can change it for whatever you want, but you need to read the UEFI specification:

Code: Select all

PEFI_BOOT_SERVICES	pbs;    /* global pointers to the BS/RS, must be initialized */
PEFI_RUNTIME_SERVICES	prs;    /* from the System Table*/

PEFI_SIMPLE_TEXT_OUTPUT_PROTOCOL    pout;    /* global pointers to the I/O protocols, must be initialized */
PEFI_SIMPLE_TEXT_INPUT_PROTOCOL    pin;    /* from the System Table*/

EFI_STATUS EFIAPI SomeFunction(){
    UINTN index, Status;
    EFI_EVENT KeyEvent;
    EFI_INPUT_KEY InputKey;
/* ... */

    KeyEvent = pin->WaitForKey;
    pout->OutputString(pout, L"Press <s> to shutdown or anything else (exc. <Enter>) to exit this rough OSL.");
ZZZ: pbs->WaitForEvent(1, &KeyEvent, &index);
    Status = pin->ReadKeyStroke(pin, &InputKey);
    if(Status == EFI_NOT_READY){
        goto ZZZ;
    }else if(InputKey.UnicodeChar == 's'){
         /* clear screen here */
        pout->OutputString(L"Shutting down the machine...\r\n");
        prs->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
    }

    return pbs->Exit(ImageHandle, EFI_SUCCESS, 0, NULL);
}
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: GNU-EFI how to get keyboard events ?

Post by bzt »

Octocontrabass wrote:Are you using GNU-EFI? If you are, you're using uefi_call_wrapper() incorrectly. If you're not, you shouldn't be using uefi_call_wrapper() at all.
Plus 1. Also there's no need for "#if defined(_GNU_EFI)". If you don't have that define and you don't initialize the GNU-EFI library, then all calls to "Print()" and "uefi_call_wrapper()" should be considered an UB.
zaval wrote:first, if you use vs2019, then please, don't complicate yourself life and ditch that gnu-efi. use msvc.
True, there's no need with MSVC. But if you want to use GNU-EFI, then use a GNU toolchain, like cygwin, mingw or WSL.

Finally, here's a GNU-EFI example that's known to work. It checks the keyboard buffer in a non-blocking manner, because I needed a half-sec timeout. I could have used a timer event, but that would be much more complex.

Code: Select all

        for(i=0;i<500;i++) {
            if(!uefi_call_wrapper(BS->CheckEvent, 1, CI->WaitForKey)) {           // see if there's a key waiting (non-blocking)
                uefi_call_wrapper(CI->ReadKeyStroke, 2, CI, &key);                // read the key (would be blocking, but since there's a key it returns immediately)
                // here you can do whatever you want with the "key"
                break;
            }
            // delay 1ms
            uefi_call_wrapper(BS->Stall, 1, 1000);
        }
Here ReadKeyStroke would block, so I only call it if there's already a key waiting in the input buffer. If you don't need a timeout, and your app doesn't need to do anything while waiting for the user to press a key, then there's no need for the "CheckEvent".

Code: Select all

                uefi_call_wrapper(CI->ReadKeyStroke, 2, CI, &key);                // this blocks until a key is pressed
                // here you can do whatever you want with the "key"
Cheers,
bzt
gomidas
Posts: 15
Joined: Wed Nov 25, 2020 9:28 pm

Re: GNU-EFI how to get keyboard events ?

Post by gomidas »

Alright, found correct way of doing it. Thank you for answers.

Code: Select all

#include ".vs/msvc/efi_reference.h"

//C
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h> //C cstdlib.h for C++

//LOCAL C/C++ myFiles
//#include ".vs/msvc/xxxx.h"

bool keyMap[255];
unsigned short ESC_KEY_DOWN_COUNT=2;

// Application entrypoint (must be set to 'efi_main' for gnu-efi crt0 compatibility)
EFI_STATUS  _cdecl efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable)
{

#if defined(_GNU_EFI)
	InitializeLib(ImageHandle, SystemTable);
#endif
	EFI_INPUT_KEY Key;
	EFI_STATUS Status;
	EFI_SIMPLE_POINTER_PROTOCOL* mouse;
	EFI_SIMPLE_POINTER_STATE     State;
	EFI_EVENT Events[2];

	Key.ScanCode = SCAN_NULL;

	gST->ConOut->SetAttribute(SystemTable->ConOut, EFI_BACKGROUND_MAGENTA);
	gST->ConOut->ClearScreen(SystemTable->ConOut, EFI_BACKGROUND_MAGENTA);

	gST->ConOut->EnableCursor(gST->ConOut, TRUE);

	gST->ConOut->SetCursorPosition(gST->ConOut,55,55);

	Status = gBS->LocateProtocol(&SimplePointerProtocol, NULL, (VOID**)&mouse);

	if (EFI_ERROR(Status))
	{
		Print(L"Error locating Simple Pointer Protocol.\n");
		return Status;
	}
	else
	{
		Print(L"Mouse Found\n");
	}

	Status = mouse->Reset(mouse, TRUE);
	if (EFI_ERROR(Status))
	{
		Print(L"Can not reset.\n");
		return Status;
	}
	else
	{
		Print(L"Mouse Reset\n");
	}

	//Keyboard
	Events[1] = gST->ConIn->WaitForKey;
	Events[0] = mouse->WaitForInput;

	while (1)
	{
		EFI_INPUT_KEY Key = {NULL};
		UINTN index;

		Status = gBS->WaitForEvent(2, Events, &index);

		// INDEX 0 IS KEYBOARD MESSAGE
		if (index == 1)
		{
			Status = gST->ConIn->ReadKeyStroke(gST->ConIn, &Key);

			if ((UINTN)Key.ScanCode == SCAN_ESC)
			{
				if(ESC_KEY_DOWN_COUNT == 0)
				{
					SystemTable->ConIn->Reset(SystemTable->ConIn, FALSE);
					SystemTable->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
					return EFI_SUCCESS;
				}
				ESC_KEY_DOWN_COUNT--;
			}
			else
			{
				ESC_KEY_DOWN_COUNT = 2;
				Print(L"%c", Key.UnicodeChar);
			}
		}
		else
		{
			Status = mouse->GetState(mouse, &State);
			Print(L"X:%d Y:%d Z:%d L:%d R:%d\n",
				State.RelativeMovementX,
				State.RelativeMovementY,
				State.RelativeMovementZ,
				State.LeftButton,
				State.RightButton
			);
		}
	}

	SystemTable->ConIn->Reset(SystemTable->ConIn, FALSE);
	SystemTable->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
	return EFI_SUCCESS;

#if defined(_DEBUG)
	// If running in debug mode, use the EFI shut down call to close QEMU
	SystemTable->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
#endif

	return EFI_SUCCESS;
}
I now get all keyboard events...
I also tested it on a real UEFI hardware that works.
When I first get the sample project, code was given like that I thought that should be like that.

Now I've got another issue I cant see mouse events for QEMU but on real hardware that captures all mouse events. What is the reason of that ?

When my program booted, if I do CTRL+ALT+DELETE that restarts. Is that possible I prevent it or should I prevent it.
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: GNU-EFI how to get keyboard events ?

Post by austanss »

gomidas wrote:Alright, found correct way of doing it. Thank you for answers.

Code: Select all

#include ".vs/msvc/efi_reference.h"

//C
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h> //C cstdlib.h for C++

//LOCAL C/C++ myFiles
//#include ".vs/msvc/xxxx.h"

bool keyMap[255];
unsigned short ESC_KEY_DOWN_COUNT=2;

// Application entrypoint (must be set to 'efi_main' for gnu-efi crt0 compatibility)
EFI_STATUS  _cdecl efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable)
{

#if defined(_GNU_EFI)
	InitializeLib(ImageHandle, SystemTable);
#endif
	EFI_INPUT_KEY Key;
	EFI_STATUS Status;
	EFI_SIMPLE_POINTER_PROTOCOL* mouse;
	EFI_SIMPLE_POINTER_STATE     State;
	EFI_EVENT Events[2];

	Key.ScanCode = SCAN_NULL;

	gST->ConOut->SetAttribute(SystemTable->ConOut, EFI_BACKGROUND_MAGENTA);
	gST->ConOut->ClearScreen(SystemTable->ConOut, EFI_BACKGROUND_MAGENTA);

	gST->ConOut->EnableCursor(gST->ConOut, TRUE);

	gST->ConOut->SetCursorPosition(gST->ConOut,55,55);

	Status = gBS->LocateProtocol(&SimplePointerProtocol, NULL, (VOID**)&mouse);

	if (EFI_ERROR(Status))
	{
		Print(L"Error locating Simple Pointer Protocol.\n");
		return Status;
	}
	else
	{
		Print(L"Mouse Found\n");
	}

	Status = mouse->Reset(mouse, TRUE);
	if (EFI_ERROR(Status))
	{
		Print(L"Can not reset.\n");
		return Status;
	}
	else
	{
		Print(L"Mouse Reset\n");
	}

	//Keyboard
	Events[1] = gST->ConIn->WaitForKey;
	Events[0] = mouse->WaitForInput;

	while (1)
	{
		EFI_INPUT_KEY Key = {NULL};
		UINTN index;

		Status = gBS->WaitForEvent(2, Events, &index);

		// INDEX 0 IS KEYBOARD MESSAGE
		if (index == 1)
		{
			Status = gST->ConIn->ReadKeyStroke(gST->ConIn, &Key);

			if ((UINTN)Key.ScanCode == SCAN_ESC)
			{
				if(ESC_KEY_DOWN_COUNT == 0)
				{
					SystemTable->ConIn->Reset(SystemTable->ConIn, FALSE);
					SystemTable->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
					return EFI_SUCCESS;
				}
				ESC_KEY_DOWN_COUNT--;
			}
			else
			{
				ESC_KEY_DOWN_COUNT = 2;
				Print(L"%c", Key.UnicodeChar);
			}
		}
		else
		{
			Status = mouse->GetState(mouse, &State);
			Print(L"X:%d Y:%d Z:%d L:%d R:%d\n",
				State.RelativeMovementX,
				State.RelativeMovementY,
				State.RelativeMovementZ,
				State.LeftButton,
				State.RightButton
			);
		}
	}

	SystemTable->ConIn->Reset(SystemTable->ConIn, FALSE);
	SystemTable->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
	return EFI_SUCCESS;

#if defined(_DEBUG)
	// If running in debug mode, use the EFI shut down call to close QEMU
	SystemTable->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
#endif

	return EFI_SUCCESS;
}
I now get all keyboard events...
I also tested it on a real UEFI hardware that works.
When I first get the sample project, code was given like that I thought that should be like that.

Now I've got another issue I cant see mouse events for QEMU but on real hardware that captures all mouse events. What is the reason of that ?

When my program booted, if I do CTRL+ALT+DELETE that restarts. Is that possible I prevent it or should I prevent it.
CTRL+ALT+DELETE is the universal keybind for resetting the computer. You should only block it if you intend to replace it with similar functionality. Also, did you configure a mouse in QEMU? It doesn't come by default.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
gomidas
Posts: 15
Joined: Wed Nov 25, 2020 9:28 pm

Re: GNU-EFI how to get keyboard events ?

Post by gomidas »

rizxt wrote: CTRL+ALT+DELETE is the universal keybind for resetting the computer. You should only block it if you intend to replace it with similar functionality. Also, did you configure a mouse in QEMU? It doesn't come by default.
On windows when I do ctrl+alt+delete I get some options like task-manager, reset, turn off, lock etc. I want to do something like that instead of default behaviour, I just confused when I got reset by default since I am very amateur.

For QEMU. The sample I based on uses it for debugging I do not know how to configure it at all. What is the way we configure that ?

Thank you for your help.
User avatar
Minoto
Member
Member
Posts: 89
Joined: Thu May 12, 2011 7:24 pm

Re: GNU-EFI how to get keyboard events ?

Post by Minoto »

rizxt wrote:CTRL+ALT+DELETE is the universal keybind for resetting the computer. You should only block it if you intend to replace it with similar functionality.
Really? I don't recall that being the case on Sun systems, SGI systems, VAXen, Macs, etc...
Those who understand Unix are doomed to copy it, poorly.
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: GNU-EFI how to get keyboard events ?

Post by austanss »

Minoto wrote:
rizxt wrote:CTRL+ALT+DELETE is the universal keybind for resetting the computer. You should only block it if you intend to replace it with similar functionality.
Really? I don't recall that being the case on Sun systems, SGI systems, VAXen, Macs, etc...
Like I said, you can replace it. Most modern computers running on bare metal resets on CTRL+ALT+DELETE.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: GNU-EFI how to get keyboard events ?

Post by nullplan »

rizxt wrote:Like I said, you can replace it. Most modern computers running on bare metal resets on CTRL+ALT+DELETE.
I believe Minoto's point was more that this is specific to PCs, and does not work on systems that are not PCs. That said, there is nothing special about CTRL+ALT+DEL even on a PC until the button is handled in the OS. There is no special interrupt associated with that, no control line that only gets triggered in that case, or anything. If you can talk to the keyboard directly, it is just another key combination. The "bare metal reset" must be implemented in BIOS, and is no longer relevant once BIOS has given up control of the keyboard.
Carpe diem!
Post Reply