0

How do I wait for a key in GNU EFI?
I intend it to wait for a single key, then continue execution.
My code:

#include <efi.h>
#include <efilib.h>
#include <stdlib.h>

EFI_STATUS efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
    EFI_STATUS Status;
    ST = SystemTable;
    Status = uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
    if (EFI_ERROR(Status)){
        ...
        return Status;
    }
    ...
    Status = ST->ConIn->Reset(ST->ConIn,1!=1);
    if (EFI_ERROR(Status)){
        ...
        return Status;
    }
    // wait for key here
    return EFI_SUCCESS;
}
unixsmurf
  • 5,852
  • 1
  • 33
  • 40
randomuser5215
  • 508
  • 1
  • 9
  • 26

2 Answers2

2

You can get some ideas from the EDK2 implementation of UEFI Shell.

The basic principle is that you need to set up an event waiting for a keypress.

See also section 12.3 Simple Text Input Protocol in UEFI specification v2.8.

unixsmurf
  • 5,852
  • 1
  • 33
  • 40
1

TOP OUTSIDE OF efi_main(...){...} YOU SHOULD ADD EFI_INPUT_KEY Key;

INSIDE OF efi_main AFTER,

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

ADD UINTN KeyEvent = 0;

OR WHATEVER YOUR WAY IS...


Print(L"PRESS ANY KEY OR PLEASE PRESS ESC TO EXIT.%N\n");

uefi_call_wrapper(SystemTable->ConOut->OutputString, 1, SystemTable->ConOut, L"START KEY READ\n"); // YES WE COULD DO SIMPLY Print(...);

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

//NOW WE SHOULD READ SOME KEYS
//YOU CAN ADD ANY OTHER OPTION HERE, WHEN CAPSLOCK ON THAT WILL PRINT FIRST CHAR CAPITALIZED BUT NOT REST OF CHARACTERS SINCE WE RESET...
//IF YOU WANT MORE KEYS SIMPLY IN VISUAL STUDIO TYPE SCAN_ AND YOU WILL SEE OTHER KEYS

/*...OTHER...*/

while ((UINTN)Key.ScanCode != SCAN_ESC)
{
    SystemTable->BootServices->WaitForEvent(1, &SystemTable->ConIn->WaitForKey, &KeyEvent);
    SystemTable->ConIn->ReadKeyStroke(SystemTable->ConIn, &Key);
    SystemTable->ConIn->Reset(SystemTable->ConIn, FALSE);
    Print(L"%c", Key.UnicodeChar);
}

/*...OTHER...*/

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