From 74cac842a5620bffb29ee292b7f96dbc67f1a42d Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Sat, 16 Dec 2023 12:44:18 +0100 Subject: [PATCH] Implement more wrappers and refactoring --- sdk/xtdk/bltypes.h | 6 +++ xtldr2/console.c | 83 +++++++++++++++++++++++++++------------ xtldr2/efiutils.c | 25 ++++++++++++ xtldr2/includes/bootman.h | 48 ++++++++++++++-------- xtldr2/protocol.c | 11 ++++-- xtldr2/textui.c | 56 +++++++++++++++----------- 6 files changed, 160 insertions(+), 69 deletions(-) diff --git a/sdk/xtdk/bltypes.h b/sdk/xtdk/bltypes.h index 30f6e44..6954fb7 100644 --- a/sdk/xtdk/bltypes.h +++ b/sdk/xtdk/bltypes.h @@ -39,6 +39,8 @@ typedef VOID (*PBL_CONSOLE_DISABLE_CURSOR)(); typedef VOID (*PBL_CONSOLE_ENABLE_CURSOR)(); typedef VOID (*PBL_CONSOLE_PRINT)(IN PUINT16 Format, IN ...); typedef VOID (*PBL_CONSOLE_QUERY_MODE)(OUT PUINT_PTR ResX, OUT PUINT_PTR ResY); +typedef VOID (*PBL_CONSOLE_READ_KEY_STROKE)(OUT PEFI_INPUT_KEY Key); +typedef VOID (*PBL_CONSOLE_RESET_INPUT_BUFFER)(); typedef VOID (*PBL_CONSOLE_SET_ATTRIBUTES)(IN ULONGLONG Attributes); typedef VOID (*PBL_CONSOLE_SET_CURSOR_POSITION)(IN ULONGLONG PosX, IN ULONGLONG PosY); typedef VOID (*PBL_CONSOLE_WRITE)(IN PUSHORT String); @@ -51,6 +53,7 @@ typedef EFI_STATUS (*PBL_OPEN_VOLUME)(IN PEFI_DEVICE_PATH_PROTOCOL DevicePath, O typedef EFI_STATUS (*PBL_OPEN_XT_PROTOCOL)(OUT PVOID *ProtocolHandler, IN PEFI_GUID ProtocolGuid); typedef EFI_STATUS (*PBL_READ_FILE)(IN PEFI_FILE_HANDLE DirHandle, IN CONST PWCHAR FileName, OUT PVOID *FileData, OUT PSIZE_T FileSize); typedef VOID (*PBL_SLEEP_EXECUTION)(IN ULONG_PTR Milliseconds); +typedef EFI_STATUS (*PBL_WAIT_FOR_EFI_EVENT)(IN UINT_PTR NumberOfEvents, IN PEFI_EVENT Event, OUT PUINT_PTR Index); /* XTLDR Configuration data */ typedef struct _XTBL_CONFIG_ENTRY @@ -86,6 +89,8 @@ typedef struct _XTBL_LOADER_PROTOCOL PBL_CONSOLE_ENABLE_CURSOR EnableCursor; PBL_CONSOLE_PRINT Print; PBL_CONSOLE_QUERY_MODE QueryMode; + PBL_CONSOLE_READ_KEY_STROKE ReadKeyStroke; + PBL_CONSOLE_RESET_INPUT_BUFFER ResetInputBuffer; PBL_CONSOLE_SET_ATTRIBUTES SetAttributes; PBL_CONSOLE_SET_CURSOR_POSITION SetCursorPosition; PBL_CONSOLE_WRITE Write; @@ -116,6 +121,7 @@ typedef struct _XTBL_LOADER_PROTOCOL PBL_EXIT_BOOT_SERVICES ExitBootServices; PBL_GET_SECURE_BOOT_STATUS GetSecureBootStatus; PBL_SLEEP_EXECUTION SleepExecution; + PBL_WAIT_FOR_EFI_EVENT WaitForEfiEvent; } Util; } XTBL_LOADER_PROTOCOL, *PXTBL_LOADER_PROTOCOL; diff --git a/xtldr2/console.c b/xtldr2/console.c index 1a5e974..1e7a277 100644 --- a/xtldr2/console.c +++ b/xtldr2/console.c @@ -18,7 +18,7 @@ */ XTCDECL VOID -BlConsoleClearScreen() +BlClearConsoleScreen() { /* Clear screen */ EfiSystemTable->ConOut->ClearScreen(EfiSystemTable->ConOut); @@ -33,7 +33,7 @@ BlConsoleClearScreen() */ XTCDECL VOID -BlConsoleDisableCursor() +BlDisableConsoleCursor() { EfiSystemTable->ConOut->EnableCursor(EfiSystemTable->ConOut, FALSE); } @@ -47,7 +47,7 @@ BlConsoleDisableCursor() */ XTCDECL VOID -BlConsoleEnableCursor() +BlEnableConsoleCursor() { EfiSystemTable->ConOut->EnableCursor(EfiSystemTable->ConOut, TRUE); } @@ -93,27 +93,6 @@ BlConsolePrint(IN PUINT16 Format, VA_END(Arguments); } -/** - * Queries information concerning the output device’s supported text mode. - * - * @param ResX - * Supplies a buffer to receive the horizontal resolution. - * - * @param ResY - * Supplies a buffer to receive the vertical resolution. - * - * @return This routine does not return any value. - * - * @since XT 1.0 - */ -XTCDECL -VOID -BlConsoleQueryMode(OUT PUINT_PTR ResX, - OUT PUINT_PTR ResY) -{ - EfiSystemTable->ConOut->QueryMode(EfiSystemTable->ConOut, EfiSystemTable->ConOut->Mode->Mode, ResX, ResY); -} - /** * Displays the string on the device at the current cursor location. * @@ -131,6 +110,58 @@ BlConsoleWrite(IN PUSHORT String) EfiSystemTable->ConOut->OutputString(EfiSystemTable->ConOut, String); } +/** + * Queries information concerning the output device’s supported text mode. + * + * @param ResX + * Supplies a buffer to receive the horizontal resolution. + * + * @param ResY + * Supplies a buffer to receive the vertical resolution. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTCDECL +VOID +BlQueryConsoleMode(OUT PUINT_PTR ResX, + OUT PUINT_PTR ResY) +{ + EfiSystemTable->ConOut->QueryMode(EfiSystemTable->ConOut, EfiSystemTable->ConOut->Mode->Mode, ResX, ResY); +} + +/** + * Reads a keystroke from the input device. + * + * @param Key + * Supplies a pointer to the EFI_INPUT_KEY structure that will receive the keystroke. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTCDECL +VOID +BlReadKeyStroke(OUT PEFI_INPUT_KEY Key) +{ + EfiSystemTable->ConIn->ReadKeyStroke(EfiSystemTable->ConIn, Key); +} + +/** + * Resets the console input device and clears its input buffer. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTCDECL +VOID +BlResetConsoleInputBuffer() +{ + EfiSystemTable->ConIn->Reset(EfiSystemTable->ConIn, FALSE); +} + /** * Sets the foreground and background colors. * @@ -208,6 +239,6 @@ BlpInitializeConsole() EfiSystemTable->StdErr->Reset(EfiSystemTable->StdErr, TRUE); /* Clear screen and enable cursor */ - BlConsoleClearScreen(); - BlConsoleEnableCursor(); + BlClearConsoleScreen(); + BlEnableConsoleCursor(); } diff --git a/xtldr2/efiutils.c b/xtldr2/efiutils.c index b385b53..28bdf7f 100644 --- a/xtldr2/efiutils.c +++ b/xtldr2/efiutils.c @@ -94,6 +94,31 @@ BlSleepExecution(IN ULONG_PTR Milliseconds) EfiSystemTable->BootServices->Stall(Milliseconds * 1000); } +/** + * Waits for one or more EFI events. + * + * @param NumberOfEvents + * Supplies the number of events to wait for. + * + * @param Event + * Supplies the array of events to wait for. + * + * @param Index + * Receives the index of the event that was signaled. + * + * @return This routine returns status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +BlWaitForEfiEvent(IN UINT_PTR NumberOfEvents, + IN PEFI_EVENT Event, + OUT PUINT_PTR Index) +{ + return EfiSystemTable->BootServices->WaitForEvent(NumberOfEvents, Event, Index); +} + /** * Initializes EFI Boot Loader (XTLDR). * diff --git a/xtldr2/includes/bootman.h b/xtldr2/includes/bootman.h index 0cef47f..c4ef222 100644 --- a/xtldr2/includes/bootman.h +++ b/xtldr2/includes/bootman.h @@ -16,32 +16,19 @@ typedef VOID (BMPRINTCHAR)(IN USHORT Character); /* XTLDR routines forward references */ +XTCDECL +VOID +BlClearConsoleScreen(); + XTCDECL EFI_STATUS BlCloseVolume(IN PEFI_HANDLE VolumeHandle); -XTCDECL -VOID -BlConsoleClearScreen(); - -XTCDECL -VOID -BlConsoleDisableCursor(); - -XTCDECL -VOID -BlConsoleEnableCursor(); - XTCDECL VOID BlConsolePrint(IN PUINT16 Format, IN ...); -XTCDECL -VOID -BlConsoleQueryMode(OUT PUINT_PTR ResX, - OUT PUINT_PTR ResY); - XTCDECL VOID BlConsoleWrite(IN PUSHORT String); @@ -51,6 +38,14 @@ VOID BlDebugPrint(IN PUINT16 Format, IN ...); +XTCDECL +VOID +BlDisableConsoleCursor(); + +XTCDECL +VOID +BlEnableConsoleCursor(); + XTCDECL EFI_STATUS BlEnumerateBlockDevices(); @@ -96,6 +91,11 @@ BlOpenVolume(IN PEFI_DEVICE_PATH_PROTOCOL DevicePath, OUT PEFI_HANDLE DiskHandle, OUT PEFI_FILE_HANDLE *FsHandle); +XTCDECL +VOID +BlQueryConsoleMode(OUT PUINT_PTR ResX, + OUT PUINT_PTR ResY); + XTCDECL EFI_STATUS BlReadFile(IN PEFI_FILE_HANDLE DirHandle, @@ -103,6 +103,14 @@ BlReadFile(IN PEFI_FILE_HANDLE DirHandle, OUT PVOID *FileData, OUT PSIZE_T FileSize); +XTCDECL +VOID +BlReadKeyStroke(OUT PEFI_INPUT_KEY Key); + +XTCDECL +VOID +BlResetConsoleInputBuffer(); + XTCDECL EFI_STATUS BlMemoryFreePages(IN UINT64 Pages, @@ -140,6 +148,12 @@ EFI_STATUS BlStartXtLoader(IN EFI_HANDLE ImageHandle, IN PEFI_SYSTEM_TABLE SystemTable); +XTCDECL +EFI_STATUS +BlWaitForEfiEvent(IN UINT_PTR NumberOfEvents, + IN PEFI_EVENT Event, + OUT PUINT_PTR Index); + XTCDECL EFI_STATUS BlpActivateSerialIOController(); diff --git a/xtldr2/protocol.c b/xtldr2/protocol.c index 73e50ec..2ac749a 100644 --- a/xtldr2/protocol.c +++ b/xtldr2/protocol.c @@ -90,11 +90,13 @@ BlpRegisterXtLoaderProtocol() EFI_HANDLE Handle = NULL; /* Set all routines available via loader protocol */ - LdrProtocol.Console.ClearScreen = BlConsoleClearScreen; - LdrProtocol.Console.DisableCursor = BlConsoleDisableCursor; - LdrProtocol.Console.EnableCursor = BlConsoleEnableCursor; + LdrProtocol.Console.ClearScreen = BlClearConsoleScreen; + LdrProtocol.Console.DisableCursor = BlDisableConsoleCursor; + LdrProtocol.Console.EnableCursor = BlEnableConsoleCursor; LdrProtocol.Console.Print = BlConsolePrint; - LdrProtocol.Console.QueryMode = BlConsoleQueryMode; + LdrProtocol.Console.QueryMode = BlQueryConsoleMode; + LdrProtocol.Console.ReadKeyStroke = BlReadKeyStroke; + LdrProtocol.Console.ResetInputBuffer = BlResetConsoleInputBuffer; LdrProtocol.Console.SetAttributes = BlSetConsoleAttributes; LdrProtocol.Console.SetCursorPosition = BlSetCursorPosition; LdrProtocol.Console.Write = BlConsoleWrite; @@ -110,6 +112,7 @@ BlpRegisterXtLoaderProtocol() LdrProtocol.Util.ExitBootServices = BlExitBootServices; LdrProtocol.Util.GetSecureBootStatus = BlGetSecureBootStatus; LdrProtocol.Util.SleepExecution = BlSleepExecution; + LdrProtocol.Util.WaitForEfiEvent = BlWaitForEfiEvent; /* Register XTLDR loader protocol */ BlDebugPrint(L"Registering XT loader protocol\n"); diff --git a/xtldr2/textui.c b/xtldr2/textui.c index 2959e47..ef9fa97 100644 --- a/xtldr2/textui.c +++ b/xtldr2/textui.c @@ -122,7 +122,7 @@ BlpDetermineDialogBoxSize(IN OUT PXTBL_DIALOG_HANDLE Handle, Width += 4; /* Get console resolution */ - BlConsoleQueryMode(&Handle->ResX, &Handle->ResY); + BlQueryConsoleMode(&Handle->ResX, &Handle->ResY); /* Make sure dialog window fits in the buffer */ if(Width > TUI_MAX_DIALOG_WIDTH) @@ -328,7 +328,7 @@ BlpDrawDialogButton(IN PXTBL_DIALOG_HANDLE Handle) } /* Disable cursor and draw dialog button */ - BlConsoleDisableCursor(); + BlDisableConsoleCursor(); BlSetConsoleAttributes(ButtonColor | TextColor); BlSetCursorPosition(Handle->ResX / 2 - 4, Handle->PosY + Handle->Height - 2); BlConsolePrint(L"[ OK ]"); @@ -393,7 +393,7 @@ BlpDrawDialogInputField(IN PXTBL_DIALOG_HANDLE Handle, } /* Disable cursor and write input field to console */ - BlConsoleDisableCursor(); + BlDisableConsoleCursor(); BlConsoleWrite(InputField); /* Write input field text */ @@ -404,7 +404,7 @@ BlpDrawDialogInputField(IN PXTBL_DIALOG_HANDLE Handle, if(Handle->Attributes & TUI_DIALOG_ACTIVE_INPUT_FIELD) { /* Enable cursor for active input field */ - BlConsoleEnableCursor(); + BlEnableConsoleCursor(); } } @@ -459,14 +459,26 @@ BlpDrawDialogProgressBar(IN PXTBL_DIALOG_HANDLE Handle, ProgressBar[Index] = 0; /* Disable cursor and write progress bar to console */ - BlConsoleDisableCursor(); + BlDisableConsoleCursor(); BlConsoleWrite(ProgressBar); } - +/** + * Displays a red error dialog box with the specified caption and message. + * + * @param Caption + * Supplies a caption string put on the dialog box. + * + * @param Message + * Supplies a message string put on the dialog box. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ XTCDECL VOID BlDisplayErrorDialog(IN PWCHAR Caption, @@ -483,7 +495,7 @@ BlDisplayErrorDialog(IN PWCHAR Caption, BlpDetermineDialogBoxSize(&Handle, Message); /* Disable cursor and draw dialog box */ - BlConsoleDisableCursor(); + BlDisableConsoleCursor(); BlpDrawDialogBox(&Handle, Caption, Message); /* Draw active button */ @@ -497,14 +509,14 @@ BlDisplayErrorDialog(IN PWCHAR Caption, while(Key.ScanCode != 0x17 && Key.UnicodeChar != 0x0D) { /* Wait for key press and read key stroke */ - EfiSystemTable->BootServices->WaitForEvent(1, &EfiSystemTable->ConIn->WaitForKey, &Index); - EfiSystemTable->ConIn->ReadKeyStroke(EfiSystemTable->ConIn, &Key); - EfiSystemTable->ConIn->Reset(EfiSystemTable->ConIn, FALSE); + BlWaitForEfiEvent(1, &EfiSystemTable->ConIn->WaitForKey, &Index); + BlReadKeyStroke(&Key); + BlResetConsoleInputBuffer(); } /* Clear screen to remove dialog box */ BlSetConsoleAttributes(EFI_TEXT_BGCOLOR_BLACK | EFI_TEXT_FGCOLOR_LIGHTGRAY); - BlConsoleClearScreen(); + BlClearConsoleScreen(); } XTCDECL @@ -523,7 +535,7 @@ BlDisplayInfoDialog(IN PWCHAR Caption, BlpDetermineDialogBoxSize(&Handle, Message); /* Disable cursor and draw dialog box */ - BlConsoleDisableCursor(); + BlDisableConsoleCursor(); BlpDrawDialogBox(&Handle, Caption, Message); /* Draw active button */ @@ -537,14 +549,14 @@ BlDisplayInfoDialog(IN PWCHAR Caption, while(Key.ScanCode != 0x17 && Key.UnicodeChar != 0x0D) { /* Wait for key press and read key stroke */ - EfiSystemTable->BootServices->WaitForEvent(1, &EfiSystemTable->ConIn->WaitForKey, &Index); - EfiSystemTable->ConIn->ReadKeyStroke(EfiSystemTable->ConIn, &Key); - EfiSystemTable->ConIn->Reset(EfiSystemTable->ConIn, FALSE); + BlWaitForEfiEvent(1, &EfiSystemTable->ConIn->WaitForKey, &Index); + BlReadKeyStroke(&Key); + BlResetConsoleInputBuffer(); } /* Clear screen to remove dialog box */ BlSetConsoleAttributes(EFI_TEXT_BGCOLOR_BLACK | EFI_TEXT_FGCOLOR_LIGHTGRAY); - BlConsoleClearScreen(); + BlClearConsoleScreen(); } XTCDECL @@ -565,7 +577,7 @@ BlDisplayInputDialog(IN PWCHAR Caption, BlpDetermineDialogBoxSize(&Handle, Message); /* Disable cursor and draw dialog box */ - BlConsoleDisableCursor(); + BlDisableConsoleCursor(); BlpDrawDialogBox(&Handle, Caption, Message); /* Draw inactive button */ @@ -593,9 +605,9 @@ BlDisplayInputDialog(IN PWCHAR Caption, while(TRUE) { /* Wait for key press and read key stroke */ - EfiSystemTable->BootServices->WaitForEvent(1, &EfiSystemTable->ConIn->WaitForKey, &Index); - EfiSystemTable->ConIn->ReadKeyStroke(EfiSystemTable->ConIn, &Key); - EfiSystemTable->ConIn->Reset(EfiSystemTable->ConIn, FALSE); + BlWaitForEfiEvent(1, &EfiSystemTable->ConIn->WaitForKey, &Index); + BlReadKeyStroke(&Key); + BlResetConsoleInputBuffer(); /* Check key press scan code */ if(Key.ScanCode == 0x17) @@ -711,7 +723,7 @@ BlDisplayInputDialog(IN PWCHAR Caption, /* Clear screen to remove dialog box */ BlSetConsoleAttributes(EFI_TEXT_BGCOLOR_BLACK | EFI_TEXT_FGCOLOR_LIGHTGRAY); - BlConsoleClearScreen(); + BlClearConsoleScreen(); } XTCDECL @@ -729,7 +741,7 @@ BlDisplayProgressDialog(IN PWCHAR Caption, BlpDetermineDialogBoxSize(&Handle, Message); /* Disable cursor and draw dialog box */ - BlConsoleDisableCursor(); + BlDisableConsoleCursor(); BlpDrawDialogBox(&Handle, Caption, Message); /* Draw active button */