From d2f06a27587dc2a9d8f21b822f5976cd316658f0 Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Sun, 3 Dec 2023 16:44:01 +0100 Subject: [PATCH 01/13] Refactor part 3 --- sdk/xtdk/{xtbmapi.h => xtblapi.h} | 0 xtldr2/config.c | 2 +- xtldr2/console.c | 2 +- xtldr2/globals.c | 2 +- xtldr2/hardware.c | 2 +- xtldr2/includes/bootman.h | 4 ++-- xtldr2/includes/globals.h | 4 ++-- xtldr2/includes/{xtbm.h => xtldr.h} | 10 +++++----- xtldr2/memory.c | 2 +- xtldr2/string.c | 2 +- xtldr2/xtldr.c | 2 +- 11 files changed, 16 insertions(+), 16 deletions(-) rename sdk/xtdk/{xtbmapi.h => xtblapi.h} (100%) rename xtldr2/includes/{xtbm.h => xtldr.h} (66%) diff --git a/sdk/xtdk/xtbmapi.h b/sdk/xtdk/xtblapi.h similarity index 100% rename from sdk/xtdk/xtbmapi.h rename to sdk/xtdk/xtblapi.h diff --git a/xtldr2/config.c b/xtldr2/config.c index 2c8ac23..beb72cc 100644 --- a/xtldr2/config.c +++ b/xtldr2/config.c @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr2/console.c b/xtldr2/console.c index d96ab94..b4d7388 100644 --- a/xtldr2/console.c +++ b/xtldr2/console.c @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr2/globals.c b/xtldr2/globals.c index 14277ac..844833c 100644 --- a/xtldr2/globals.c +++ b/xtldr2/globals.c @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /* XT Boot Loader configuration data */ diff --git a/xtldr2/hardware.c b/xtldr2/hardware.c index 8cf2a47..9824cc2 100644 --- a/xtldr2/hardware.c +++ b/xtldr2/hardware.c @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr2/includes/bootman.h b/xtldr2/includes/bootman.h index a4665fa..73abcb5 100644 --- a/xtldr2/includes/bootman.h +++ b/xtldr2/includes/bootman.h @@ -1,7 +1,7 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/includes/xtbm.h + * FILE: xtldr/includes/bootman.h * DESCRIPTION: XTLDR Boot Loader related structures and routines forward references * DEVELOPERS: Rafal Kupiec */ @@ -9,7 +9,7 @@ #ifndef __XTLDR_BOOTMAN_H #define __XTLDR_BOOTMAN_H -#include +#include /* XTLDR routine callbacks */ diff --git a/xtldr2/includes/globals.h b/xtldr2/includes/globals.h index d9e1419..3ac01d9 100644 --- a/xtldr2/includes/globals.h +++ b/xtldr2/includes/globals.h @@ -1,7 +1,7 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/includes/xtbm.h + * FILE: xtldr/includes/globals.h * DESCRIPTION: XTLDR global variables * DEVELOPERS: Rafal Kupiec */ @@ -9,7 +9,7 @@ #ifndef __XTLDR_GLOBALS_H #define __XTLDR_GLOBALS_H -#include +#include /* XT Boot Loader configuration data */ diff --git a/xtldr2/includes/xtbm.h b/xtldr2/includes/xtldr.h similarity index 66% rename from xtldr2/includes/xtbm.h rename to xtldr2/includes/xtldr.h index 7a9c9d4..497b0b1 100644 --- a/xtldr2/includes/xtbm.h +++ b/xtldr2/includes/xtldr.h @@ -1,18 +1,18 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/includes/xtbm.h + * FILE: xtldr/includes/xtldr.h * DESCRIPTION: Top level header for XTLDR * DEVELOPERS: Rafal Kupiec */ -#ifndef __XTLDR_XTBM_H -#define __XTLDR_XTBM_H +#ifndef __XTLDR_XTLDR_H +#define __XTLDR_XTLDR_H -#include +#include #include #include #include -#endif /* __XTLDR_XTBM_H */ +#endif /* __XTLDR_XTLDR_H */ diff --git a/xtldr2/memory.c b/xtldr2/memory.c index 6ef6998..a91a99e 100644 --- a/xtldr2/memory.c +++ b/xtldr2/memory.c @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr2/string.c b/xtldr2/string.c index 58b0d86..991fbce 100644 --- a/xtldr2/string.c +++ b/xtldr2/string.c @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr2/xtldr.c b/xtldr2/xtldr.c index 8a6bb3f..9a8eeb5 100644 --- a/xtldr2/xtldr.c +++ b/xtldr2/xtldr.c @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** From 023f2c72b34b2774ab344cb03784606daaa95e4b Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Sun, 3 Dec 2023 16:47:03 +0100 Subject: [PATCH 02/13] Refactor part 4 --- sdk/xtdk/{bmtypes.h => bltypes.h} | 0 sdk/xtdk/xtblapi.h | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) rename sdk/xtdk/{bmtypes.h => bltypes.h} (100%) diff --git a/sdk/xtdk/bmtypes.h b/sdk/xtdk/bltypes.h similarity index 100% rename from sdk/xtdk/bmtypes.h rename to sdk/xtdk/bltypes.h diff --git a/sdk/xtdk/xtblapi.h b/sdk/xtdk/xtblapi.h index 35fab90..ececebb 100644 --- a/sdk/xtdk/xtblapi.h +++ b/sdk/xtdk/xtblapi.h @@ -1,8 +1,8 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: sdk/xtdk/xtbmapi.h - * DESCRIPTION: Top level header for the XT Boot Manager API + * FILE: sdk/xtdk/xtblapi.h + * DESCRIPTION: Top level header for the XT Boot Loader API * DEVELOPERS: Rafal Kupiec */ @@ -43,4 +43,4 @@ #include ARCH_HEADER(hlfuncs.h) /* Boot Manager specific structures */ -#include +#include From a101ddf55574f6c6fd75b70e708f47e6a961b3bb Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Sun, 3 Dec 2023 23:14:34 +0100 Subject: [PATCH 03/13] Add debug port type definitions --- sdk/xtdk/bltypes.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sdk/xtdk/bltypes.h b/sdk/xtdk/bltypes.h index 8f75f5a..20c33d2 100644 --- a/sdk/xtdk/bltypes.h +++ b/sdk/xtdk/bltypes.h @@ -12,11 +12,16 @@ #include +/* Debug port type definitions */ +#define XTBL_DEBUGPORT_SCREEN 1 +#define XTBL_DEBUGPORT_SERIAL 2 + /* XTLDR configuration data */ typedef struct _XTBM_CONFIGURATION { PWCHAR Default; PWCHAR Debug; + ULONG DebugPort; BOOLEAN Shell; PWCHAR Theme; ULONG Timeout; From 6a03db6fcd302b98b09e02f5ba80787230226f1c Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Sun, 3 Dec 2023 23:15:10 +0100 Subject: [PATCH 04/13] Include hltypes header --- sdk/xtdk/xtblapi.h | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/xtdk/xtblapi.h b/sdk/xtdk/xtblapi.h index ececebb..ef5260f 100644 --- a/sdk/xtdk/xtblapi.h +++ b/sdk/xtdk/xtblapi.h @@ -36,6 +36,7 @@ #include ARCH_HEADER(hltypes.h) /* XT Kernel runtime routines */ +#include #include /* Architecture specific XT kernel routines */ From 5131a682b87b579553fcc278d6b0df73a5546c17 Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Sun, 3 Dec 2023 23:18:49 +0100 Subject: [PATCH 05/13] Refactor part 5; Implement debugging ports --- xtldr2/CMakeLists.txt | 2 + xtldr2/console.c | 53 +++++--- xtldr2/debug.c | 260 ++++++++++++++++++++++++++++++++++++++ xtldr2/efiutils.c | 27 ++++ xtldr2/hardware.c | 2 +- xtldr2/includes/bootman.h | 61 ++++++--- xtldr2/xtldr.c | 17 ++- 7 files changed, 381 insertions(+), 41 deletions(-) create mode 100644 xtldr2/debug.c create mode 100644 xtldr2/efiutils.c diff --git a/xtldr2/CMakeLists.txt b/xtldr2/CMakeLists.txt index 198e406..465b1c7 100644 --- a/xtldr2/CMakeLists.txt +++ b/xtldr2/CMakeLists.txt @@ -10,6 +10,8 @@ include_directories( list(APPEND XTLDR_SOURCE ${XTLDR_SOURCE_DIR}/config.c ${XTLDR_SOURCE_DIR}/console.c + ${XTLDR_SOURCE_DIR}/debug.c + ${XTLDR_SOURCE_DIR}/efiutils.c ${XTLDR_SOURCE_DIR}/globals.c ${XTLDR_SOURCE_DIR}/hardware.c ${XTLDR_SOURCE_DIR}/memory.c diff --git a/xtldr2/console.c b/xtldr2/console.c index b4d7388..c0911c9 100644 --- a/xtldr2/console.c +++ b/xtldr2/console.c @@ -52,27 +52,6 @@ BlConsoleEnableCursor() EfiSystemTable->ConOut->EnableCursor(EfiSystemTable->ConOut, TRUE); } -/** - * This routine initializes the EFI console. - * - * @return This routine returns status code. - * - * @since XT 1.0 - */ -XTCDECL -VOID -BlConsoleInitialize() -{ - /* Clear console buffers */ - EfiSystemTable->ConIn->Reset(EfiSystemTable->ConIn, TRUE); - EfiSystemTable->ConOut->Reset(EfiSystemTable->ConOut, TRUE); - EfiSystemTable->StdErr->Reset(EfiSystemTable->StdErr, TRUE); - - /* Clear screen and enable cursor */ - BlConsoleClearScreen(); - BlConsoleEnableCursor(); -} - /** * This routine formats the input string and prints it out to the stdout and serial console. * @@ -99,10 +78,42 @@ BlConsolePrint(IN PUINT16 Format, /* Format and print the string to the stdout */ BlpStringPrint(BlpConsolePrintChar, Format, Arguments); + /* Print to serial console only if not running under OVMF */ + if(RtlWideStringCompare(EfiSystemTable->FirmwareVendor, L"EDK II", 6) != 0) + { + /* Check if debugging enabled and if EFI serial port is fully initialized */ + if(DEBUG && (BlpSerialPort.Flags & COMPORT_FLAG_INIT)) + { + /* Format and print the string to the serial console */ + BlpStringPrint(BlpDebugPutChar, Format, Arguments); + } + } + /* Clean up the va_list */ VA_END(Arguments); } +/** + * This routine initializes the EFI console. + * + * @return This routine returns status code. + * + * @since XT 1.0 + */ +XTCDECL +VOID +BlpConsoleInitialize() +{ + /* Clear console buffers */ + EfiSystemTable->ConIn->Reset(EfiSystemTable->ConIn, TRUE); + EfiSystemTable->ConOut->Reset(EfiSystemTable->ConOut, TRUE); + EfiSystemTable->StdErr->Reset(EfiSystemTable->StdErr, TRUE); + + /* Clear screen and enable cursor */ + BlConsoleClearScreen(); + BlConsoleEnableCursor(); +} + /** * Writes a character to the default EFI console. * diff --git a/xtldr2/debug.c b/xtldr2/debug.c new file mode 100644 index 0000000..dcfddc5 --- /dev/null +++ b/xtldr2/debug.c @@ -0,0 +1,260 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/debug.c + * DESCRIPTION: XT Boot Loader debugging support + * DEVELOPERS: Rafal Kupiec + */ + +#include + + +/** + * This routine formats the input string and prints it out to the debug ports. + * + * @param Format + * The formatted string that is to be written to the output. + * + * @param ... + * Depending on the format string, this routine might expect a sequence of additional arguments. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTCDECL +VOID +BlDebugPrint(IN PUINT16 Format, + IN ...) +{ + VA_LIST Arguments; + + /* Check if debugging enabled and if EFI serial port is fully initialized */ + if(DEBUG) + { + /* Initialise the va_list */ + VA_START(Arguments, Format); + + /* Check if serial debug port is enabled */ + if((BlpConfiguration.DebugPort & XTBL_DEBUGPORT_SERIAL) && (BlpSerialPort.Flags & COMPORT_FLAG_INIT)) + { + /* Format and print the string to the serial console */ + BlpStringPrint(BlpDebugPutChar, Format, Arguments); + } + + /* Check if screen debug port is enabled and Boot Services are still available */ + if((BlpConfiguration.DebugPort & XTBL_DEBUGPORT_SCREEN) && (EfiSystemTable->BootServices != 0)) + { + /* Format and print the string to the screen */ + BlpStringPrint(BlpConsolePrintChar, Format, Arguments); + } + + /* Clean up the va_list */ + VA_END(Arguments); + } +} + +/** + * This routine initializes the serial debug console. + * + * @param PortNumber + * Supplies a port number. + * + * @param PortAddress + * Supplies an address of the COM port. + * + * @param BaudRate + * Supplies an optional port baud rate. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +BlpDebugInitializeComPort(IN ULONG PortNumber, + IN ULONG PortAddress, + IN ULONG BaudRate) +{ + EFI_STATUS EfiStatus; + XTSTATUS Status; + + /* Print debug message depending on port settings */ + if(PortAddress) + { + BlConsolePrint(L"Initializing serial console at COM port address: 0x%lx\n", PortAddress); + } + else + { + BlConsolePrint(L"Initializing serial console at port COM%d\n", PortNumber); + } + + /* Initialize COM port */ + Status = HlInitializeComPort(&BlpSerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate); + + /* Port not found under supplied address */ + if(Status == STATUS_NOT_FOUND && PortAddress) + { + /* This might be PCI(E) serial controller, try to activate I/O space access first */ + EfiStatus = BlpActivateSerialIOController(); + if(EfiStatus == STATUS_EFI_SUCCESS) + { + /* Try to reinitialize COM port */ + BlConsolePrint(L"Enabled I/O space access for all PCI(E) serial controllers found\n"); + Status = HlInitializeComPort(&BlpSerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate); + } + } + + /* Check COM port initialization status code */ + if(Status != STATUS_SUCCESS) + { + /* Serial port initialization failed, mark as not ready */ + return STATUS_EFI_NOT_READY; + } + + /* Return success */ + return STATUS_EFI_SUCCESS; +} + +/** + * This routine initializes the XTLDR debug console. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +BlpDebugInitializeConsole() +{ + ULONG PortAddress, PortNumber, BaudRate; + PWCHAR DebugPort, LastPort; + EFI_STATUS Status; + + /* Set default serial port options */ + PortAddress = 0; + PortNumber = 0; + BaudRate = 0; + + /* Make sure any debug options are provided */ + if(BlpConfiguration.Debug) + { + /* Find all debug ports */ + DebugPort = RtlWideStringTokenize(BlpConfiguration.Debug, L";", &LastPort); + + /* Iterate over all debug ports */ + while(DebugPort != NULL) + { + /* Check what port is set for debugging */ + if(RtlWideStringCompare(DebugPort, L"COM", 3) == 0) + { + /* Read COM port number */ + DebugPort += 3; + while(*DebugPort >= '0' && *DebugPort <= '9') + { + /* Get port number */ + PortNumber *= 10; + PortNumber += *DebugPort - '0'; + DebugPort++; + } + + /* Check if custom COM port address supplied */ + if(PortNumber == 0 && RtlWideStringCompare(DebugPort, L":0x", 3) == 0) + { + /* COM port address provided */ + DebugPort += 3; + while((*DebugPort >= '0' && *DebugPort <= '9') || + (*DebugPort >= 'A' && *DebugPort <= 'F') || + (*DebugPort >= 'a' && *DebugPort <= 'f')) + { + /* Get port address */ + PortAddress *= 16; + if(*DebugPort >= '0' && *DebugPort <= '9') + { + PortAddress += *DebugPort - '0'; + } + else if(*DebugPort >= 'A' && *DebugPort <= 'F') + { + PortAddress += *DebugPort - 'A' + 10; + } + else if(*DebugPort >= 'a' && *DebugPort <= 'f') + { + PortAddress += *DebugPort - 'a' + 10; + } + DebugPort++; + } + } + + /* Look for additional COM port parameters */ + if(*DebugPort == ',') + { + /* Baud rate provided */ + DebugPort++; + while(*DebugPort >= '0' && *DebugPort <= '9') + { + /* Get baud rate */ + BaudRate *= 10; + BaudRate += *DebugPort - '0'; + DebugPort++; + } + } + + /* Enable debug port */ + BlpConfiguration.DebugPort |= XTBL_DEBUGPORT_SERIAL; + } + else if(RtlWideStringCompare(DebugPort, L"SCREEN", 5) == 0) + { + /* Enable debug port */ + BlpConfiguration.DebugPort |= XTBL_DEBUGPORT_SCREEN; + } + else + { + /* Unsupported debug port specified */ + BlConsolePrint(L"ERROR: Unsupported debug port ('%S') specified\n", DebugPort); + BlSleepExecution(3000); + } + + /* Take next debug port */ + DebugPort = RtlWideStringTokenize(NULL, L";", &LastPort); + } + } + + /* Check if serial debug port is enabled */ + if(BlpConfiguration.DebugPort & XTBL_DEBUGPORT_SERIAL) + { + /* Try to initialize COM port */ + Status = BlpDebugInitializeComPort(PortNumber, PortAddress, BaudRate); + if(Status != STATUS_EFI_SUCCESS) + { + /* Remove serial debug port, as COM port initialization failed and return */ + BlpConfiguration.DebugPort &= ~XTBL_DEBUGPORT_SERIAL; + return Status; + } + } + + /* Return success */ + return STATUS_EFI_SUCCESS; +} + +/** + * Writes a character to the serial console. + * + * @param Character + * The integer promotion of the character to be written. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTCDECL +VOID +BlpDebugPutChar(IN USHORT Character) +{ + USHORT Buffer[2]; + + /* Write character to the serial console */ + Buffer[0] = Character; + Buffer[1] = 0; + + HlComPortPutByte(&BlpSerialPort, Buffer[0]); +} diff --git a/xtldr2/efiutils.c b/xtldr2/efiutils.c new file mode 100644 index 0000000..3cded88 --- /dev/null +++ b/xtldr2/efiutils.c @@ -0,0 +1,27 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/efiutils.c + * DESCRIPTION: EFI related routines for XT Boot Loader + * DEVELOPERS: Rafal Kupiec + */ + +#include + + +/** + * Puts the system to sleep for the specified number of milliseconds. + * + * @param Milliseconds + * Supplies the number of milliseconds to sleep. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTCDECL +VOID +BlSleepExecution(IN ULONG_PTR Milliseconds) +{ + EfiSystemTable->BootServices->Stall(Milliseconds * 1000); +} diff --git a/xtldr2/hardware.c b/xtldr2/hardware.c index 9824cc2..4b6e6b7 100644 --- a/xtldr2/hardware.c +++ b/xtldr2/hardware.c @@ -18,7 +18,7 @@ */ XTCDECL EFI_STATUS -BlpHwActivateSerialIOController() +BlpActivateSerialIOController() { EFI_GUID PciGuid = EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_GUID; PEFI_PCI_ROOT_BRIDGE_IO_PROTOCOL PciDev; diff --git a/xtldr2/includes/bootman.h b/xtldr2/includes/bootman.h index 73abcb5..3a310ae 100644 --- a/xtldr2/includes/bootman.h +++ b/xtldr2/includes/bootman.h @@ -15,17 +15,17 @@ /* XTLDR routine callbacks */ typedef VOID (BMPRINTCHAR)(IN USHORT Character); +/* XTLDR routines forward references */ XTCDECL EFI_STATUS BlMemoryAllocatePages(IN UINT64 Pages, - OUT PEFI_PHYSICAL_ADDRESS Memory); + OUT PEFI_PHYSICAL_ADDRESS Memory); XTCDECL EFI_STATUS BlMemoryAllocatePool(IN UINT_PTR Size, - OUT PVOID *Memory); + OUT PVOID *Memory); -/* XTLDR routines forward references */ XTCDECL VOID BlConsoleClearScreen(); @@ -38,40 +38,67 @@ XTCDECL VOID BlConsoleEnableCursor(); +XTCDECL +VOID +BlConsolePrint(IN PUINT16 Format, + IN ...); + +XTCDECL +VOID +BlDebugPrint(IN PUINT16 Format, + IN ...); + XTCDECL EFI_STATUS BlMemoreFreePages(IN UINT64 Pages, - IN EFI_PHYSICAL_ADDRESS Memory); + IN EFI_PHYSICAL_ADDRESS Memory); XTCDECL EFI_STATUS BlMemoryFreePool(IN PVOID Memory); +XTCDECL +VOID +BlSleepExecution(IN ULONG_PTR Milliseconds); + +XTCDECL +EFI_STATUS +BlStartXtLoader(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable); + +XTCDECL +EFI_STATUS +BlpActivateSerialIOController(); XTCDECL VOID -BlConsoleInitialize(); +BlpConfigParseCommandLine(VOID); XTCDECL VOID -BlConsolePrint(IN PUINT16 Format, - IN ...); +BlpConfigUpdateGlobalConfiguration(IN PWCHAR Options); + +XTCDECL +VOID +BlpConsoleInitialize(); XTCDECL VOID BlpConsolePrintChar(IN USHORT Character); XTCDECL -VOID -BlpStringPrint(IN IN BMPRINTCHAR PrintCharRoutine, - IN PUINT16 Format, - IN VA_LIST Arguments); +EFI_STATUS +BlpDebugInitializeComPort(IN ULONG PortNumber, + IN ULONG PortAddress, + IN ULONG BaudRate); XTCDECL EFI_STATUS -BmStartXtLoader(IN EFI_HANDLE ImageHandle, - IN PEFI_SYSTEM_TABLE SystemTable); +BlpDebugInitializeConsole(); +XTCDECL +VOID +BlpDebugPutChar(IN USHORT Character); XTCDECL VOID @@ -81,7 +108,9 @@ BlpStringFormat(IN BMPRINTCHAR PrintCharRoutine, XTCDECL VOID -BlpConfigParseCommandLine(VOID); +BlpStringPrint(IN IN BMPRINTCHAR PrintCharRoutine, + IN PUINT16 Format, + IN VA_LIST Arguments); XTCDECL VOID @@ -113,8 +142,4 @@ XTCDECL UINT64 BlpStringReadPadding(IN PUINT16 *Format); -XTCDECL -VOID -BlpConfigUpdateGlobalConfiguration(IN PWCHAR Options); - #endif /* __XTLDR_BOOTMAN_H */ diff --git a/xtldr2/xtldr.c b/xtldr2/xtldr.c index 9a8eeb5..dfa0a03 100644 --- a/xtldr2/xtldr.c +++ b/xtldr2/xtldr.c @@ -34,9 +34,24 @@ BlStartXtLoader(IN EFI_HANDLE ImageHandle, EfiSystemTable = SystemTable; /* Initialize UEFI console and early print XTLDR version */ - BlConsoleInitialize(); + BlpConsoleInitialize(); BlConsolePrint(L"XTLDR boot loader v%s\n", XTOS_VERSION); + /* Parse configuration options passed from UEFI shell */ + BlpConfigParseCommandLine(); + + /* Attempt to early initialize debug console */ + if(DEBUG) + { + Status = BlpDebugInitializeConsole(); + if(Status != STATUS_EFI_SUCCESS) + { + /* Initialization failed, notify user on stdout */ + BlConsolePrint(L"ERROR: Failed to initialize debug console\n"); + BlSleepExecution(3000); + } + } + /* Temporary infinite loop */ for(;;); From 9f36d397f0e5232efa2a400345f8718f5c1a487a Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Sun, 3 Dec 2023 23:42:59 +0100 Subject: [PATCH 06/13] Do not overwrite some options for easy debugging and testing new themes --- xtldr2/config.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/xtldr2/config.c b/xtldr2/config.c index beb72cc..c0b5b13 100644 --- a/xtldr2/config.c +++ b/xtldr2/config.c @@ -78,10 +78,14 @@ BlpConfigUpdateGlobalConfiguration(IN PWCHAR Options) Argument += 6; Length = RtlWideStringLength(Argument, 0); - /* Save debug port in global configuration */ - BlMemoryAllocatePool(Length, (PVOID *)&BlpConfiguration.Debug); - RtlCopyMemory(BlpConfiguration.Debug, Argument, (Length * sizeof(WCHAR)) - 1); - BlpConfiguration.Debug[Length] = '\0'; + /* Store debug port configuration if not set already */ + if(BlpConfiguration.Debug == NULL) + { + /* Save debug port in global configuration */ + BlMemoryAllocatePool(Length, (PVOID *)&BlpConfiguration.Debug); + RtlCopyMemory(BlpConfiguration.Debug, Argument, (Length * sizeof(WCHAR)) - 1); + BlpConfiguration.Debug[Length] = '\0'; + } } else if(RtlWideStringCompare(Argument, L"SHELL", 5) == 0) { @@ -94,10 +98,14 @@ BlpConfigUpdateGlobalConfiguration(IN PWCHAR Options) Argument += 6; Length = RtlWideStringLength(Argument, 0); - /* Save theme in global configuration */ - BlMemoryAllocatePool(Length, (PVOID *)&BlpConfiguration.Theme); - RtlCopyMemory(BlpConfiguration.Theme, Argument, (Length * sizeof(WCHAR)) - 1); - BlpConfiguration.Theme[Length] = '\0'; + /* Store theme configuration if not set already */ + if(BlpConfiguration.Theme == NULL) + { + /* Save theme in global configuration */ + BlMemoryAllocatePool(Length, (PVOID *)&BlpConfiguration.Theme); + RtlCopyMemory(BlpConfiguration.Theme, Argument, (Length * sizeof(WCHAR)) - 1); + BlpConfiguration.Theme[Length] = '\0'; + } } else if(RtlWideStringCompare(Argument, L"TIMEOUT=", 8) == 0) { From 9a3e0f69fa6ea75c324a2f89750579e33284593b Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Sun, 3 Dec 2023 23:46:20 +0100 Subject: [PATCH 07/13] Disable watchdog timer --- xtldr2/xtldr.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/xtldr2/xtldr.c b/xtldr2/xtldr.c index dfa0a03..2c96e1d 100644 --- a/xtldr2/xtldr.c +++ b/xtldr2/xtldr.c @@ -52,6 +52,14 @@ BlStartXtLoader(IN EFI_HANDLE ImageHandle, } } + /* Disable watchdog timer */ + Status = EfiSystemTable->BootServices->SetWatchdogTimer(0, 0x10000, 0, NULL); + if(Status != STATUS_EFI_SUCCESS) + { + /* Failed to disable the timer, print message */ + BlDebugPrint(L"WARNING: Failed to disable watchdog timer\n"); + } + /* Temporary infinite loop */ for(;;); From 2b2efd0dd3548172530ce40447978e9fd99e3930 Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Mon, 4 Dec 2023 16:32:07 +0100 Subject: [PATCH 08/13] Refactor part 6 --- sdk/xtdk/bltypes.h | 15 ++++- sdk/xtdk/xtblapi.h | 10 +-- xtldr2/debug.c | 130 +++++++++++++++++++------------------- xtldr2/efiutils.c | 55 ++++++++++++++++ xtldr2/globals.c | 5 +- xtldr2/includes/bootman.h | 20 ++++-- xtldr2/includes/globals.h | 5 +- xtldr2/xtldr.c | 10 +-- 8 files changed, 162 insertions(+), 88 deletions(-) diff --git a/sdk/xtdk/bltypes.h b/sdk/xtdk/bltypes.h index 20c33d2..59ff5ab 100644 --- a/sdk/xtdk/bltypes.h +++ b/sdk/xtdk/bltypes.h @@ -10,14 +10,15 @@ #define __XTDK_BMTYPES_H #include +#include -/* Debug port type definitions */ +/* XTLDR Debug port type definitions */ #define XTBL_DEBUGPORT_SCREEN 1 #define XTBL_DEBUGPORT_SERIAL 2 /* XTLDR configuration data */ -typedef struct _XTBM_CONFIGURATION +typedef struct _XTBL_CONFIGURATION { PWCHAR Default; PWCHAR Debug; @@ -26,6 +27,14 @@ typedef struct _XTBM_CONFIGURATION PWCHAR Theme; ULONG Timeout; PWCHAR Tune; -} XTBM_CONFIGURATION, *PXTBM_CONFIGURATION; +} XTBL_CONFIGURATION, *PXTBL_CONFIGURATION; + +/* XTLDR status data */ +typedef struct _XTBL_STATUS +{ + BOOLEAN BootServices; + EFI_HANDLE ImageHandle; + PEFI_SYSTEM_TABLE SystemTable; +} XTBL_STATUS, *PXTBL_STATUS; #endif /* __XTDK_BMTYPES_H */ diff --git a/sdk/xtdk/xtblapi.h b/sdk/xtdk/xtblapi.h index ef5260f..f58c149 100644 --- a/sdk/xtdk/xtblapi.h +++ b/sdk/xtdk/xtblapi.h @@ -16,7 +16,7 @@ #include /* Architecture-specific XT forward references */ -#include ARCH_HEADER(xtstruct.h) +// #include ARCH_HEADER(xtstruct.h) /* Architecture-independent XT API */ #include @@ -32,16 +32,16 @@ #include /* Architecture dependent XT kernel data types */ -#include ARCH_HEADER(artypes.h) -#include ARCH_HEADER(hltypes.h) +// #include ARCH_HEADER(artypes.h) +// #include ARCH_HEADER(hltypes.h) /* XT Kernel runtime routines */ #include #include /* Architecture specific XT kernel routines */ -#include ARCH_HEADER(arfuncs.h) -#include ARCH_HEADER(hlfuncs.h) +// #include ARCH_HEADER(arfuncs.h) +// #include ARCH_HEADER(hlfuncs.h) /* Boot Manager specific structures */ #include diff --git a/xtldr2/debug.c b/xtldr2/debug.c index dcfddc5..3a4b689 100644 --- a/xtldr2/debug.c +++ b/xtldr2/debug.c @@ -43,7 +43,7 @@ BlDebugPrint(IN PUINT16 Format, } /* Check if screen debug port is enabled and Boot Services are still available */ - if((BlpConfiguration.DebugPort & XTBL_DEBUGPORT_SCREEN) && (EfiSystemTable->BootServices != 0)) + if((BlpConfiguration.DebugPort & XTBL_DEBUGPORT_SCREEN) && (BlpStatus.BootServices == TRUE)) { /* Format and print the string to the screen */ BlpStringPrint(BlpConsolePrintChar, Format, Arguments); @@ -54,68 +54,6 @@ BlDebugPrint(IN PUINT16 Format, } } -/** - * This routine initializes the serial debug console. - * - * @param PortNumber - * Supplies a port number. - * - * @param PortAddress - * Supplies an address of the COM port. - * - * @param BaudRate - * Supplies an optional port baud rate. - * - * @return This routine returns a status code. - * - * @since XT 1.0 - */ -XTCDECL -EFI_STATUS -BlpDebugInitializeComPort(IN ULONG PortNumber, - IN ULONG PortAddress, - IN ULONG BaudRate) -{ - EFI_STATUS EfiStatus; - XTSTATUS Status; - - /* Print debug message depending on port settings */ - if(PortAddress) - { - BlConsolePrint(L"Initializing serial console at COM port address: 0x%lx\n", PortAddress); - } - else - { - BlConsolePrint(L"Initializing serial console at port COM%d\n", PortNumber); - } - - /* Initialize COM port */ - Status = HlInitializeComPort(&BlpSerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate); - - /* Port not found under supplied address */ - if(Status == STATUS_NOT_FOUND && PortAddress) - { - /* This might be PCI(E) serial controller, try to activate I/O space access first */ - EfiStatus = BlpActivateSerialIOController(); - if(EfiStatus == STATUS_EFI_SUCCESS) - { - /* Try to reinitialize COM port */ - BlConsolePrint(L"Enabled I/O space access for all PCI(E) serial controllers found\n"); - Status = HlInitializeComPort(&BlpSerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate); - } - } - - /* Check COM port initialization status code */ - if(Status != STATUS_SUCCESS) - { - /* Serial port initialization failed, mark as not ready */ - return STATUS_EFI_NOT_READY; - } - - /* Return success */ - return STATUS_EFI_SUCCESS; -} - /** * This routine initializes the XTLDR debug console. * @@ -125,7 +63,7 @@ BlpDebugInitializeComPort(IN ULONG PortNumber, */ XTCDECL EFI_STATUS -BlpDebugInitializeConsole() +BlpInitializeDebugConsole() { ULONG PortAddress, PortNumber, BaudRate; PWCHAR DebugPort, LastPort; @@ -223,7 +161,7 @@ BlpDebugInitializeConsole() if(BlpConfiguration.DebugPort & XTBL_DEBUGPORT_SERIAL) { /* Try to initialize COM port */ - Status = BlpDebugInitializeComPort(PortNumber, PortAddress, BaudRate); + Status = BlpInitializeSerialPort(PortNumber, PortAddress, BaudRate); if(Status != STATUS_EFI_SUCCESS) { /* Remove serial debug port, as COM port initialization failed and return */ @@ -236,6 +174,68 @@ BlpDebugInitializeConsole() return STATUS_EFI_SUCCESS; } +/** + * This routine initializes the serial debug console. + * + * @param PortNumber + * Supplies a port number. + * + * @param PortAddress + * Supplies an address of the COM port. + * + * @param BaudRate + * Supplies an optional port baud rate. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +BlpInitializeSerialPort(IN ULONG PortNumber, + IN ULONG PortAddress, + IN ULONG BaudRate) +{ + EFI_STATUS EfiStatus; + XTSTATUS Status; + + /* Print debug message depending on port settings */ + if(PortAddress) + { + BlConsolePrint(L"Initializing serial console at COM port address: 0x%lx\n", PortAddress); + } + else + { + BlConsolePrint(L"Initializing serial console at port COM%d\n", PortNumber); + } + + /* Initialize COM port */ + Status = HlInitializeComPort(&BlpSerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate); + + /* Port not found under supplied address */ + if(Status == STATUS_NOT_FOUND && PortAddress) + { + /* This might be PCI(E) serial controller, try to activate I/O space access first */ + EfiStatus = BlpActivateSerialIOController(); + if(EfiStatus == STATUS_EFI_SUCCESS) + { + /* Try to reinitialize COM port */ + BlConsolePrint(L"Enabled I/O space access for all PCI(E) serial controllers found\n"); + Status = HlInitializeComPort(&BlpSerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate); + } + } + + /* Check COM port initialization status code */ + if(Status != STATUS_SUCCESS) + { + /* Serial port initialization failed, mark as not ready */ + return STATUS_EFI_NOT_READY; + } + + /* Return success */ + return STATUS_EFI_SUCCESS; +} + /** * Writes a character to the serial console. * diff --git a/xtldr2/efiutils.c b/xtldr2/efiutils.c index 3cded88..104db69 100644 --- a/xtldr2/efiutils.c +++ b/xtldr2/efiutils.c @@ -9,6 +9,41 @@ #include +/** + * Exits EFI boot services. + * + * @param MapKey + * Identifies the current memory map of the system. + * + * @return This routine returns status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +BlExitBootServices(IN UINT_PTR MapKey) +{ + EFI_STATUS Status; + + /* Attempt to exit boot services */ + Status = EfiSystemTable->BootServices->ExitBootServices(BlpStatus.ImageHandle, MapKey); + if(Status != STATUS_EFI_SUCCESS) + { + /* Retry as UEFI spec says to do it twice */ + Status = EfiSystemTable->BootServices->ExitBootServices(BlpStatus.ImageHandle, MapKey); + } + + /* Make sure boot services were successfully exited */ + if(Status == STATUS_EFI_SUCCESS) + { + /* Mark EFI Boot Services as no longer available */ + BlpStatus.BootServices = FALSE; + } + + /* Return EFI status code */ + return Status; +} + /** * Puts the system to sleep for the specified number of milliseconds. * @@ -25,3 +60,23 @@ BlSleepExecution(IN ULONG_PTR Milliseconds) { EfiSystemTable->BootServices->Stall(Milliseconds * 1000); } + +/** + * Initializes EFI Boot Loader (XTLDR). + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTCDECL +VOID +BlpInitializeEfiBootLoader() +{ + /* Set current XTLDR status */ + BlpStatus.BootServices = TRUE; + BlpStatus.ImageHandle = EfiImageHandle; + BlpStatus.SystemTable = EfiSystemTable; + + /* Initialize console */ + BlpConsoleInitialize(); +} diff --git a/xtldr2/globals.c b/xtldr2/globals.c index 844833c..6017862 100644 --- a/xtldr2/globals.c +++ b/xtldr2/globals.c @@ -10,7 +10,7 @@ /* XT Boot Loader configuration data */ -XTBM_CONFIGURATION BlpConfiguration = {0}; +XTBL_CONFIGURATION BlpConfiguration = {0}; /* XT Boot Loader hex table */ STATIC PUINT16 BlpHexTable = L"0123456789ABCDEF"; @@ -18,6 +18,9 @@ STATIC PUINT16 BlpHexTable = L"0123456789ABCDEF"; /* Serial port configuration */ CPPORT BlpSerialPort; +/* XT Boot Loader status data */ +XTBL_STATUS BlpStatus = {0}; + /* EFI Image Handle */ EFI_HANDLE EfiImageHandle; diff --git a/xtldr2/includes/bootman.h b/xtldr2/includes/bootman.h index 3a310ae..5fd9f9b 100644 --- a/xtldr2/includes/bootman.h +++ b/xtldr2/includes/bootman.h @@ -16,6 +16,10 @@ typedef VOID (BMPRINTCHAR)(IN USHORT Character); /* XTLDR routines forward references */ +XTCDECL +EFI_STATUS +BlExitBootServices(IN UINT_PTR MapKey); + XTCDECL EFI_STATUS BlMemoryAllocatePages(IN UINT64 Pages, @@ -87,18 +91,22 @@ VOID BlpConsolePrintChar(IN USHORT Character); XTCDECL -EFI_STATUS -BlpDebugInitializeComPort(IN ULONG PortNumber, - IN ULONG PortAddress, - IN ULONG BaudRate); +VOID +BlpDebugPutChar(IN USHORT Character); XTCDECL EFI_STATUS -BlpDebugInitializeConsole(); +BlpInitializeDebugConsole(); XTCDECL VOID -BlpDebugPutChar(IN USHORT Character); +BlpInitializeEfiBootLoader(); + +XTCDECL +EFI_STATUS +BlpInitializeSerialPort(IN ULONG PortNumber, + IN ULONG PortAddress, + IN ULONG BaudRate); XTCDECL VOID diff --git a/xtldr2/includes/globals.h b/xtldr2/includes/globals.h index 3ac01d9..9d09f76 100644 --- a/xtldr2/includes/globals.h +++ b/xtldr2/includes/globals.h @@ -13,7 +13,7 @@ /* XT Boot Loader configuration data */ -EXTERN XTBM_CONFIGURATION BlpConfiguration; +EXTERN XTBL_CONFIGURATION BlpConfiguration; /* XT Boot Loader hex table */ EXTERN PUINT16 BlpHexTable; @@ -21,6 +21,9 @@ EXTERN PUINT16 BlpHexTable; /* Serial port configuration */ EXTERN CPPORT BlpSerialPort; +/* XT Boot Loader status data */ +EXTERN XTBL_STATUS BlpStatus; + /* EFI Image Handle */ EXTERN EFI_HANDLE EfiImageHandle; diff --git a/xtldr2/xtldr.c b/xtldr2/xtldr.c index 2c96e1d..7a850d7 100644 --- a/xtldr2/xtldr.c +++ b/xtldr2/xtldr.c @@ -29,12 +29,8 @@ BlStartXtLoader(IN EFI_HANDLE ImageHandle, { EFI_STATUS Status; - /* Set the system table and image handle */ - EfiImageHandle = ImageHandle; - EfiSystemTable = SystemTable; - - /* Initialize UEFI console and early print XTLDR version */ - BlpConsoleInitialize(); + /* Initialize XTLDR and early print XTLDR version */ + BlpInitializeEfiBootLoader(); BlConsolePrint(L"XTLDR boot loader v%s\n", XTOS_VERSION); /* Parse configuration options passed from UEFI shell */ @@ -43,7 +39,7 @@ BlStartXtLoader(IN EFI_HANDLE ImageHandle, /* Attempt to early initialize debug console */ if(DEBUG) { - Status = BlpDebugInitializeConsole(); + Status = BlpInitializeDebugConsole(); if(Status != STATUS_EFI_SUCCESS) { /* Initialization failed, notify user on stdout */ From aea69a33b9fb292ebee4627761519798c0fcc35c Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Mon, 4 Dec 2023 17:33:33 +0100 Subject: [PATCH 09/13] Correct typo --- xtldr2/includes/bootman.h | 2 +- xtldr2/memory.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/xtldr2/includes/bootman.h b/xtldr2/includes/bootman.h index 5fd9f9b..961718f 100644 --- a/xtldr2/includes/bootman.h +++ b/xtldr2/includes/bootman.h @@ -54,7 +54,7 @@ BlDebugPrint(IN PUINT16 Format, XTCDECL EFI_STATUS -BlMemoreFreePages(IN UINT64 Pages, +BlMemoryFreePages(IN UINT64 Pages, IN EFI_PHYSICAL_ADDRESS Memory); XTCDECL diff --git a/xtldr2/memory.c b/xtldr2/memory.c index a91a99e..19e9397 100644 --- a/xtldr2/memory.c +++ b/xtldr2/memory.c @@ -67,7 +67,7 @@ BlMemoryAllocatePool(IN UINT_PTR Size, */ XTCDECL EFI_STATUS -BlMemoreFreePages(IN UINT64 Pages, +BlMemoryFreePages(IN UINT64 Pages, IN EFI_PHYSICAL_ADDRESS Memory) { return EfiSystemTable->BootServices->FreePages(Memory, Pages); From 02cd8efde9be09117f716b3b5ed9958b694f13c5 Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Mon, 4 Dec 2023 18:49:34 +0100 Subject: [PATCH 10/13] Add XTLDR protocol support --- sdk/xtdk/bltypes.h | 56 ++++++++++++++++++++++++++++++++++++--- xtldr2/CMakeLists.txt | 1 + xtldr2/efiutils.c | 28 ++++++++++++++++++++ xtldr2/includes/bootman.h | 8 ++++++ 4 files changed, 90 insertions(+), 3 deletions(-) diff --git a/sdk/xtdk/bltypes.h b/sdk/xtdk/bltypes.h index 59ff5ab..e3590e3 100644 --- a/sdk/xtdk/bltypes.h +++ b/sdk/xtdk/bltypes.h @@ -13,11 +13,27 @@ #include -/* XTLDR Debug port type definitions */ +/* XTLDR Debug Port type definitions */ #define XTBL_DEBUGPORT_SCREEN 1 #define XTBL_DEBUGPORT_SERIAL 2 -/* XTLDR configuration data */ +/* Loader protocol routine pointers */ +typedef EFI_STATUS (*PBL_ALLOCATE_PAGES)(IN UINT64 Size, OUT PEFI_PHYSICAL_ADDRESS Memory); +typedef EFI_STATUS (*PBL_ALLOCATE_POOL)(IN UINT_PTR Size, OUT PVOID *Memory); +typedef VOID (*PBL_CONSOLE_CLEAR_SCREEN)(); +typedef VOID (*PBL_CONSOLE_DISABLE_CURSOR)(); +typedef VOID (*PBL_CONSOLE_ENABLE_CURSOR)(); +typedef VOID (*PBL_CONSOLE_PRINT)(IN PUINT16 Format, IN ...); +typedef VOID (*PBL_DEBUG_PRINT)(IN PUINT16 Format, IN ...); +typedef EFI_STATUS (*PBL_EXIT_BOOT_SERVICES)(IN UINT_PTR MapKey); +typedef EFI_STATUS (*PBL_FREE_PAGES)(IN UINT64 Size, IN EFI_PHYSICAL_ADDRESS Memory); +typedef EFI_STATUS (*PBL_FREE_POOL)(IN PVOID Memory); +typedef EFI_HANDLE (*PBL_GET_EFI_IMAGE_HANDLE)(); +typedef PEFI_SYSTEM_TABLE (*PBL_GET_EFI_SYSTEM_TABLE)(); +typedef EFI_STATUS (*PBL_OPEN_XT_PROTOCOL)(OUT PVOID *ProtocolHandler, IN PEFI_GUID ProtocolGuid); +typedef VOID (*PBL_SLEEP_EXECUTION)(IN ULONG_PTR Milliseconds); + +/* XTLDR Configuration data */ typedef struct _XTBL_CONFIGURATION { PWCHAR Default; @@ -29,7 +45,7 @@ typedef struct _XTBL_CONFIGURATION PWCHAR Tune; } XTBL_CONFIGURATION, *PXTBL_CONFIGURATION; -/* XTLDR status data */ +/* XTLDR Status data */ typedef struct _XTBL_STATUS { BOOLEAN BootServices; @@ -37,4 +53,38 @@ typedef struct _XTBL_STATUS PEFI_SYSTEM_TABLE SystemTable; } XTBL_STATUS, *PXTBL_STATUS; +/* XTLDR Loader protocol */ +typedef struct _XTBL_LOADER_PROTOCOL +{ + struct + { + PBL_CONSOLE_CLEAR_SCREEN ClearScreen; + PBL_CONSOLE_DISABLE_CURSOR DisableCursor; + PBL_CONSOLE_ENABLE_CURSOR EnableCursor; + PBL_CONSOLE_PRINT Print; + } Console; + struct + { + PBL_DEBUG_PRINT Print; + } Debug; + struct + { + PBL_ALLOCATE_PAGES AllocatePages; + PBL_ALLOCATE_POOL AllocatePool; + PBL_FREE_PAGES FreePages; + PBL_FREE_POOL FreePool; + } Memory; + struct + { + PBL_OPEN_XT_PROTOCOL Open; + } Protocol; + struct + { + PBL_EXIT_BOOT_SERVICES ExitBootServices; + PBL_GET_EFI_IMAGE_HANDLE GetImageHandle; + PBL_GET_EFI_SYSTEM_TABLE GetSystemTable; + PBL_SLEEP_EXECUTION SleepExecution; + } Util; +} XTBL_LOADER_PROTOCOL, *PXTBL_LOADER_PROTOCOL; + #endif /* __XTDK_BMTYPES_H */ diff --git a/xtldr2/CMakeLists.txt b/xtldr2/CMakeLists.txt index 465b1c7..2004d41 100644 --- a/xtldr2/CMakeLists.txt +++ b/xtldr2/CMakeLists.txt @@ -15,6 +15,7 @@ list(APPEND XTLDR_SOURCE ${XTLDR_SOURCE_DIR}/globals.c ${XTLDR_SOURCE_DIR}/hardware.c ${XTLDR_SOURCE_DIR}/memory.c + ${XTLDR_SOURCE_DIR}/protocol.c ${XTLDR_SOURCE_DIR}/string.c ${XTLDR_SOURCE_DIR}/xtldr.c) diff --git a/xtldr2/efiutils.c b/xtldr2/efiutils.c index 104db69..5c1f3f0 100644 --- a/xtldr2/efiutils.c +++ b/xtldr2/efiutils.c @@ -44,6 +44,34 @@ BlExitBootServices(IN UINT_PTR MapKey) return Status; } +/** + * Returns the EFI image handle. + * + * @return This routine returns the current EFI image handle. + * + * @since XT 1.0 + */ +XTCDECL +EFI_HANDLE +BlGetEfiImageHandle() +{ + return BlpStatus.ImageHandle; +} + +/** + * Returns the EFI system table. + * + * @return This routine returns the current EFI system table. + * + * @since XT 1.0 + */ +XTCDECL +PEFI_SYSTEM_TABLE +BlGetEfiSystemTable() +{ + return BlpStatus.SystemTable; +} + /** * Puts the system to sleep for the specified number of milliseconds. * diff --git a/xtldr2/includes/bootman.h b/xtldr2/includes/bootman.h index 961718f..dea207d 100644 --- a/xtldr2/includes/bootman.h +++ b/xtldr2/includes/bootman.h @@ -52,6 +52,14 @@ VOID BlDebugPrint(IN PUINT16 Format, IN ...); +XTCDECL +EFI_HANDLE +BlGetEfiImageHandle(); + +XTCDECL +PEFI_SYSTEM_TABLE +BlGetEfiSystemTable(); + XTCDECL EFI_STATUS BlMemoryFreePages(IN UINT64 Pages, From 0a1df55a0fe93ab978be897def9ced7357f69e53 Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Mon, 4 Dec 2023 18:53:11 +0100 Subject: [PATCH 11/13] Add XTLDR protocol support, add missing source file --- xtldr2/protocol.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 xtldr2/protocol.c diff --git a/xtldr2/protocol.c b/xtldr2/protocol.c new file mode 100644 index 0000000..6e8d1bb --- /dev/null +++ b/xtldr2/protocol.c @@ -0,0 +1,111 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/protocol.c + * DESCRIPTION: XT Boot Loader protocol support + * DEVELOPERS: Rafal Kupiec + */ + +#include + + +/** + * This routine locates and opens the requested XT boot loader protocol. + * + * @param ProtocolHandler + * Supplies the address where a pointer to the opened protocol is returned. + * + * @param ProtocolGuid + * Supplies a pointer to the unique protocol GUID. + * + * @return This routine returns status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +BlOpenXtProtocol(OUT PVOID *ProtocolHandler, + IN PEFI_GUID ProtocolGuid) +{ + PEFI_HANDLE Handles = NULL; + EFI_STATUS Status; + UINT_PTR Count; + UINT Index; + + /* Try to locate the handles */ + Status = EfiSystemTable->BootServices->LocateHandleBuffer(ByProtocol, ProtocolGuid, NULL, &Count, &Handles); + if(Status != STATUS_EFI_SUCCESS) + { + /* Unable to get handles */ + return Status; + } + + /* Check if any handles returned */ + if(Count > 0) + { + /* Iterate through all given handles */ + for(Index = 0; Index < Count; Index++) + { + /* Try to open protocol */ + Status = EfiSystemTable->BootServices->OpenProtocol(Handles[Index], ProtocolGuid, + ProtocolHandler, EfiImageHandle, NULL, + EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); + + /* Check if successfully opened the loader protocol */ + if(Status == STATUS_EFI_SUCCESS) + { + /* Protocol found and successfully opened */ + break; + } + } + } + + /* Free handles */ + EfiSystemTable->BootServices->FreePool(Handles); + + /* Make sure the loaded protocol has been found */ + if(*ProtocolHandler == NULL) + { + /* Protocol not found */ + return STATUS_EFI_NOT_FOUND; + } + + /* Return success */ + return STATUS_EFI_SUCCESS; +} + +/** + * This routine registers XTLDR protocol for further usage by modules. + * + * @return This routine returns status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +BlpRegisterXtLoaderProtocol() +{ + EFI_GUID Guid = XT_BOOT_LOADER_PROTOCOL_GUID; + XTBL_LOADER_PROTOCOL LdrProtocol; + 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.Print = BlConsolePrint; + LdrProtocol.Debug.Print = BlDebugPrint; + LdrProtocol.Memory.AllocatePages = BlMemoryAllocatePages; + LdrProtocol.Memory.AllocatePool = BlMemoryAllocatePool; + LdrProtocol.Memory.FreePages = BlMemoryFreePages; + LdrProtocol.Memory.FreePool = BlMemoryFreePool; + LdrProtocol.Protocol.Open = BlOpenXtProtocol; + LdrProtocol.Util.ExitBootServices = BlExitBootServices; + LdrProtocol.Util.GetImageHandle = BlGetEfiImageHandle; + LdrProtocol.Util.GetSystemTable = BlGetEfiSystemTable; + LdrProtocol.Util.SleepExecution = BlSleepExecution; + + /* Register XTLDR loader protocol */ + BlDebugPrint(L"Registering XT loader protocol\n"); + return EfiSystemTable->BootServices->InstallProtocolInterface(&Handle, &Guid, EFI_NATIVE_INTERFACE, &LdrProtocol); +} From c4f1429a3b092c419687676e82d115868fbb6adb Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Mon, 4 Dec 2023 18:58:00 +0100 Subject: [PATCH 12/13] Not really needed as each module will get both ImageHandle and SystemTable --- sdk/xtdk/bltypes.h | 4 ---- xtldr2/efiutils.c | 28 ---------------------------- xtldr2/includes/bootman.h | 8 -------- xtldr2/protocol.c | 2 -- 4 files changed, 42 deletions(-) diff --git a/sdk/xtdk/bltypes.h b/sdk/xtdk/bltypes.h index e3590e3..c00a15f 100644 --- a/sdk/xtdk/bltypes.h +++ b/sdk/xtdk/bltypes.h @@ -28,8 +28,6 @@ typedef VOID (*PBL_DEBUG_PRINT)(IN PUINT16 Format, IN ...); typedef EFI_STATUS (*PBL_EXIT_BOOT_SERVICES)(IN UINT_PTR MapKey); typedef EFI_STATUS (*PBL_FREE_PAGES)(IN UINT64 Size, IN EFI_PHYSICAL_ADDRESS Memory); typedef EFI_STATUS (*PBL_FREE_POOL)(IN PVOID Memory); -typedef EFI_HANDLE (*PBL_GET_EFI_IMAGE_HANDLE)(); -typedef PEFI_SYSTEM_TABLE (*PBL_GET_EFI_SYSTEM_TABLE)(); typedef EFI_STATUS (*PBL_OPEN_XT_PROTOCOL)(OUT PVOID *ProtocolHandler, IN PEFI_GUID ProtocolGuid); typedef VOID (*PBL_SLEEP_EXECUTION)(IN ULONG_PTR Milliseconds); @@ -81,8 +79,6 @@ typedef struct _XTBL_LOADER_PROTOCOL struct { PBL_EXIT_BOOT_SERVICES ExitBootServices; - PBL_GET_EFI_IMAGE_HANDLE GetImageHandle; - PBL_GET_EFI_SYSTEM_TABLE GetSystemTable; PBL_SLEEP_EXECUTION SleepExecution; } Util; } XTBL_LOADER_PROTOCOL, *PXTBL_LOADER_PROTOCOL; diff --git a/xtldr2/efiutils.c b/xtldr2/efiutils.c index 5c1f3f0..104db69 100644 --- a/xtldr2/efiutils.c +++ b/xtldr2/efiutils.c @@ -44,34 +44,6 @@ BlExitBootServices(IN UINT_PTR MapKey) return Status; } -/** - * Returns the EFI image handle. - * - * @return This routine returns the current EFI image handle. - * - * @since XT 1.0 - */ -XTCDECL -EFI_HANDLE -BlGetEfiImageHandle() -{ - return BlpStatus.ImageHandle; -} - -/** - * Returns the EFI system table. - * - * @return This routine returns the current EFI system table. - * - * @since XT 1.0 - */ -XTCDECL -PEFI_SYSTEM_TABLE -BlGetEfiSystemTable() -{ - return BlpStatus.SystemTable; -} - /** * Puts the system to sleep for the specified number of milliseconds. * diff --git a/xtldr2/includes/bootman.h b/xtldr2/includes/bootman.h index dea207d..961718f 100644 --- a/xtldr2/includes/bootman.h +++ b/xtldr2/includes/bootman.h @@ -52,14 +52,6 @@ VOID BlDebugPrint(IN PUINT16 Format, IN ...); -XTCDECL -EFI_HANDLE -BlGetEfiImageHandle(); - -XTCDECL -PEFI_SYSTEM_TABLE -BlGetEfiSystemTable(); - XTCDECL EFI_STATUS BlMemoryFreePages(IN UINT64 Pages, diff --git a/xtldr2/protocol.c b/xtldr2/protocol.c index 6e8d1bb..34aa2bb 100644 --- a/xtldr2/protocol.c +++ b/xtldr2/protocol.c @@ -101,8 +101,6 @@ BlpRegisterXtLoaderProtocol() LdrProtocol.Memory.FreePool = BlMemoryFreePool; LdrProtocol.Protocol.Open = BlOpenXtProtocol; LdrProtocol.Util.ExitBootServices = BlExitBootServices; - LdrProtocol.Util.GetImageHandle = BlGetEfiImageHandle; - LdrProtocol.Util.GetSystemTable = BlGetEfiSystemTable; LdrProtocol.Util.SleepExecution = BlSleepExecution; /* Register XTLDR loader protocol */ From 6733146b7124f67e5d45eac0175db234e82ca83a Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Tue, 5 Dec 2023 22:18:25 +0100 Subject: [PATCH 13/13] Refactor, part 7; register XTLDR protocol --- bootdata/xtldr/xtldr.ini | 2 -- sdk/xtdk/bltypes.h | 5 ++++- xtldr2/config.c | 21 +++------------------ xtldr2/includes/bootman.h | 13 +++++++++++-- xtldr2/xtldr.c | 14 +++++++++++++- 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/bootdata/xtldr/xtldr.ini b/bootdata/xtldr/xtldr.ini index 4537da4..8bd454f 100644 --- a/bootdata/xtldr/xtldr.ini +++ b/bootdata/xtldr/xtldr.ini @@ -6,7 +6,6 @@ # Debug - enables the debugging port and consists of two comma-separated parameters: com port and baud rate; # it is also possible to specify custom port address with: COM0:[address],[baud_rate] # Default - specifies which operating system listen in config file will be started if no choice is made -# Theme - allows to set a custom theme to personalize XTLDR's look'n'feel # Timeout - sets the countdown timer (in seconds) before the default OS get started automatically # Tune - plays a tune on the pcspeaker right before the XTLDR loads # @@ -25,7 +24,6 @@ Tune=400 880 2 988 2 783 2 392 2 587 3 Debug=COM1,115200 Timeout=30 -Theme=Fancy Default=ExectOS [ExectOS] diff --git a/sdk/xtdk/bltypes.h b/sdk/xtdk/bltypes.h index c00a15f..60d52e5 100644 --- a/sdk/xtdk/bltypes.h +++ b/sdk/xtdk/bltypes.h @@ -13,6 +13,10 @@ #include +/* XTLDR directories */ +#define XTBL_LOADER_DIRECTORY L"\\EFI\\BOOT\\XTLDR\\" +#define XTBL_THEMES_DIRECTORY L"\\EFI\\BOOT\\XTLDR\\THEMES\\" + /* XTLDR Debug Port type definitions */ #define XTBL_DEBUGPORT_SCREEN 1 #define XTBL_DEBUGPORT_SERIAL 2 @@ -38,7 +42,6 @@ typedef struct _XTBL_CONFIGURATION PWCHAR Debug; ULONG DebugPort; BOOLEAN Shell; - PWCHAR Theme; ULONG Timeout; PWCHAR Tune; } XTBL_CONFIGURATION, *PXTBL_CONFIGURATION; diff --git a/xtldr2/config.c b/xtldr2/config.c index c0b5b13..22c8de4 100644 --- a/xtldr2/config.c +++ b/xtldr2/config.c @@ -18,7 +18,7 @@ */ XTCDECL VOID -BlpConfigParseCommandLine(VOID) +BlpParseCommandLineOptions(VOID) { EFI_GUID LIPGuid = EFI_LOADED_IMAGE_PROTOCOL_GUID; PEFI_LOADED_IMAGE_PROTOCOL LoadedImage; @@ -32,7 +32,7 @@ BlpConfigParseCommandLine(VOID) if(LoadedImage && LoadedImage->LoadOptions) { /* Update global boot loader configuration */ - BlpConfigUpdateGlobalConfiguration(LoadedImage->LoadOptions); + BlpUpdateGlobalConfiguration(LoadedImage->LoadOptions); } } } @@ -49,7 +49,7 @@ BlpConfigParseCommandLine(VOID) */ XTCDECL VOID -BlpConfigUpdateGlobalConfiguration(IN PWCHAR Options) +BlpUpdateGlobalConfiguration(IN PWCHAR Options) { PWCHAR Argument, LastArg; SIZE_T Length; @@ -92,21 +92,6 @@ BlpConfigUpdateGlobalConfiguration(IN PWCHAR Options) /* Force shell mode */ BlpConfiguration.Shell = TRUE; } - else if(RtlWideStringCompare(Argument, L"THEME=", 6) == 0) - { - /* Skip to the argument value */ - Argument += 6; - Length = RtlWideStringLength(Argument, 0); - - /* Store theme configuration if not set already */ - if(BlpConfiguration.Theme == NULL) - { - /* Save theme in global configuration */ - BlMemoryAllocatePool(Length, (PVOID *)&BlpConfiguration.Theme); - RtlCopyMemory(BlpConfiguration.Theme, Argument, (Length * sizeof(WCHAR)) - 1); - BlpConfiguration.Theme[Length] = '\0'; - } - } else if(RtlWideStringCompare(Argument, L"TIMEOUT=", 8) == 0) { /* Skip to the argument value */ diff --git a/xtldr2/includes/bootman.h b/xtldr2/includes/bootman.h index 961718f..e0ad29c 100644 --- a/xtldr2/includes/bootman.h +++ b/xtldr2/includes/bootman.h @@ -61,6 +61,11 @@ XTCDECL EFI_STATUS BlMemoryFreePool(IN PVOID Memory); +XTCDECL +EFI_STATUS +BlOpenXtProtocol(OUT PVOID *ProtocolHandler, + IN PEFI_GUID ProtocolGuid); + XTCDECL VOID BlSleepExecution(IN ULONG_PTR Milliseconds); @@ -76,11 +81,11 @@ BlpActivateSerialIOController(); XTCDECL VOID -BlpConfigParseCommandLine(VOID); +BlpParseCommandLineOptions(VOID); XTCDECL VOID -BlpConfigUpdateGlobalConfiguration(IN PWCHAR Options); +BlpUpdateGlobalConfiguration(IN PWCHAR Options); XTCDECL VOID @@ -108,6 +113,10 @@ BlpInitializeSerialPort(IN ULONG PortNumber, IN ULONG PortAddress, IN ULONG BaudRate); +XTCDECL +EFI_STATUS +BlpRegisterXtLoaderProtocol(); + XTCDECL VOID BlpStringFormat(IN BMPRINTCHAR PrintCharRoutine, diff --git a/xtldr2/xtldr.c b/xtldr2/xtldr.c index 7a850d7..513cca2 100644 --- a/xtldr2/xtldr.c +++ b/xtldr2/xtldr.c @@ -29,12 +29,16 @@ BlStartXtLoader(IN EFI_HANDLE ImageHandle, { EFI_STATUS Status; + /* Set the system table and image handle */ + EfiImageHandle = ImageHandle; + EfiSystemTable = SystemTable; + /* Initialize XTLDR and early print XTLDR version */ BlpInitializeEfiBootLoader(); BlConsolePrint(L"XTLDR boot loader v%s\n", XTOS_VERSION); /* Parse configuration options passed from UEFI shell */ - BlpConfigParseCommandLine(); + BlpParseCommandLineOptions(); /* Attempt to early initialize debug console */ if(DEBUG) @@ -56,6 +60,14 @@ BlStartXtLoader(IN EFI_HANDLE ImageHandle, BlDebugPrint(L"WARNING: Failed to disable watchdog timer\n"); } + /* Register loader protocol */ + Status = BlpRegisterXtLoaderProtocol(); + if(Status != STATUS_EFI_SUCCESS) + { + /* Failed to register loader protocol */ + BlDebugPrint(L"ERROR: Failed to register XTLDR loader protocol\n"); + } + /* Temporary infinite loop */ for(;;);