From ad0b71c49bd8e990a914b39d67c4c78332430eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauri=20Kentt=C3=A4?= Date: Sat, 2 Sep 2023 23:31:02 +0300 Subject: [PATCH] Set EFI ReadKey timeout to 15 seconds --- src/main.c | 17 +++++++++-------- src/util.c | 9 +++++---- src/util.h | 7 +++++-- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index 84b1ad8..894bf02 100644 --- a/src/main.c +++ b/src/main.c @@ -393,14 +393,14 @@ EFI_STATUS EFIAPI EfiMain(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *ST_) { } } Print(L"HackBGRT: Reverting to %s.\n", config.boot_path); - Print(L"Press escape to cancel, any other key to boot.\n"); - if (ReadKey().ScanCode == SCAN_ESC) { + Print(L"Press escape to cancel or any other key (or wait 15 seconds) to boot.\n"); + if (ReadKey(15000).ScanCode == SCAN_ESC) { goto fail; } - } - if (config.debug) { - Print(L"HackBGRT: Ready to boot.\nPress escape to cancel, any other key to boot.\n"); - if (ReadKey().ScanCode == SCAN_ESC) { + } else if (config.debug) { + Print(L"HackBGRT: Ready to boot. Disable debug mode to skip this screen.\n"); + Print(L"Press escape to cancel or any other key (or wait 15 seconds) to boot.\n"); + if (ReadKey(15000).ScanCode == SCAN_ESC) { return 0; } } @@ -409,6 +409,7 @@ EFI_STATUS EFIAPI EfiMain(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *ST_) { goto fail; } Print(L"HackBGRT: Started %s. Why are we still here?!\n", config.boot_path); + Print(L"Please check that %s is not actually HackBGRT!\n", config.boot_path); goto fail; fail: { @@ -419,8 +420,8 @@ EFI_STATUS EFIAPI EfiMain(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *ST_) { #else Print(L"HackBGRT version: unknown; not an official release?\n"); #endif - Print(L"Press any key to exit.\n"); - ReadKey(); + Print(L"Press any key (or wait 15 seconds) to exit.\n"); + ReadKey(15000); return 1; } } diff --git a/src/util.c b/src/util.c index 6e18251..308e660 100644 --- a/src/util.c +++ b/src/util.c @@ -68,14 +68,15 @@ void RandomSeedAuto(void) { RandomSeed(a, b), Random(), Random(); } -void WaitKey(void) { +EFI_STATUS WaitKey(UINT64 timeout_ms) { ST->ConIn->Reset(ST->ConIn, FALSE); - WaitForSingleEvent(ST->ConIn->WaitForKey, 0); + const int ms_to_100ns = 10000; + return WaitForSingleEvent(ST->ConIn->WaitForKey, timeout_ms * ms_to_100ns); } -EFI_INPUT_KEY ReadKey(void) { - WaitKey(); +EFI_INPUT_KEY ReadKey(UINT64 timeout_ms) { EFI_INPUT_KEY key = {0}; + WaitKey(timeout_ms); ST->ConIn->ReadKeyStroke(ST->ConIn, &key); return key; } diff --git a/src/util.h b/src/util.h index b317faa..bb9400b 100644 --- a/src/util.h +++ b/src/util.h @@ -82,15 +82,18 @@ extern void RandomSeedAuto(void); /** * Wait for a key press. It will still remain in the buffer. + * + * @param timeout_ms The timeout in milliseconds, or 0 for no timeout. */ -extern void WaitKey(void); +extern EFI_STATUS WaitKey(UINT64 timeout_ms); /** * Wait for a key press and read it. * + * @param timeout_ms The timeout in milliseconds, or 0 for no timeout. * @return The pressed key. */ -extern EFI_INPUT_KEY ReadKey(void); +extern EFI_INPUT_KEY ReadKey(UINT64 timeout_ms); /** * Load a file, allocate some extra bytes as well.