From ff7ff904990e66e1e0314e97e86e22950b3040f1 Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Tue, 12 Dec 2023 18:35:35 +0100 Subject: [PATCH] Implement BlGetSecureBootStatus() and do some refactoring --- xtldr2/config.c | 10 +++++----- xtldr2/console.c | 2 +- xtldr2/debug.c | 36 ++++++++++++++++++------------------ xtldr2/efiutils.c | 39 +++++++++++++++++++++++++++++++++++---- xtldr2/globals.c | 8 +------- xtldr2/includes/bootman.h | 4 ++++ xtldr2/includes/globals.h | 8 +------- xtldr2/protocol.c | 1 + 8 files changed, 66 insertions(+), 42 deletions(-) diff --git a/xtldr2/config.c b/xtldr2/config.c index e7e0a3e..dfc0c7b 100644 --- a/xtldr2/config.c +++ b/xtldr2/config.c @@ -119,7 +119,7 @@ BlpLoadConfiguration() PCHAR ConfigData; /* Initialize configuration pointer */ - RtlInitializeListHead(&BlpConfiguration); + RtlInitializeListHead(&BlpConfigSections); /* Read data from configuration file */ Status = BlpReadConfigFile(L"\\EFI\\BOOT\\", L"XTLDR.INI", &ConfigData); @@ -131,7 +131,7 @@ BlpLoadConfiguration() } /* Parse configuration data */ - Status = BlpParseConfigFile(ConfigData, &BlpConfiguration); + Status = BlpParseConfigFile(ConfigData, &BlpConfigSections); if(Status != STATUS_EFI_SUCCESS) { /* Failed to parse configuration */ @@ -140,8 +140,8 @@ BlpLoadConfiguration() } /* Iterate through config sections */ - SectionListEntry = BlpConfiguration.Flink; - while(SectionListEntry != &BlpConfiguration) + SectionListEntry = BlpConfigSections.Flink; + while(SectionListEntry != &BlpConfigSections) { /* Get config section */ PXTBL_CONFIG_SECTION Section = CONTAIN_RECORD(SectionListEntry, XTBL_CONFIG_SECTION, Flink); @@ -162,7 +162,7 @@ BlpLoadConfiguration() } /* Update boot menu OS list */ - BlpMenuList = &BlpConfiguration; + BlpMenuList = &BlpConfigSections; /* Return success */ return STATUS_EFI_SUCCESS; diff --git a/xtldr2/console.c b/xtldr2/console.c index 5273f5f..1a5e974 100644 --- a/xtldr2/console.c +++ b/xtldr2/console.c @@ -82,7 +82,7 @@ BlConsolePrint(IN PUINT16 Format, if(RtlCompareWideString(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)) + if(DEBUG && (BlpStatus.SerialPort.Flags & COMPORT_FLAG_INIT)) { /* Format and print the string to the serial console */ BlpStringPrint(BlpDebugPutChar, Format, Arguments); diff --git a/xtldr2/debug.c b/xtldr2/debug.c index b431723..221c649 100644 --- a/xtldr2/debug.c +++ b/xtldr2/debug.c @@ -36,14 +36,14 @@ BlDebugPrint(IN PUINT16 Format, VA_START(Arguments, Format); /* Check if serial debug port is enabled */ - if((BlpDebugPort & XTBL_DEBUGPORT_SERIAL) && (BlpSerialPort.Flags & COMPORT_FLAG_INIT)) + if((BlpStatus.DebugPort & XTBL_DEBUGPORT_SERIAL) && (BlpStatus.SerialPort.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((BlpDebugPort & XTBL_DEBUGPORT_SCREEN) && (BlpStatus.BootServices == TRUE)) + if((BlpStatus.DebugPort & XTBL_DEBUGPORT_SCREEN) && (BlpStatus.BootServices == TRUE)) { /* Format and print the string to the screen */ BlpStringPrint(BlpConsolePrintChar, Format, Arguments); @@ -78,7 +78,7 @@ BlpInitializeDebugConsole() DebugConfiguration = BlGetConfigValue(L"DEBUG"); /* Make sure any debug options are provided and debug console is not initialized yet */ - if(DebugConfiguration && BlpDebugPort == 0) + if(DebugConfiguration && BlpStatus.DebugPort == 0) { /* Find all debug ports */ DebugPort = RtlTokenizeWideString(DebugConfiguration, L";", &LastPort); @@ -141,12 +141,12 @@ BlpInitializeDebugConsole() } /* Enable debug port */ - BlpDebugPort |= XTBL_DEBUGPORT_SERIAL; + BlpStatus.DebugPort |= XTBL_DEBUGPORT_SERIAL; } else if(RtlCompareWideStringInsensitive(DebugPort, L"SCREEN", 5) == 0) { /* Enable debug port */ - BlpDebugPort |= XTBL_DEBUGPORT_SCREEN; + BlpStatus.DebugPort |= XTBL_DEBUGPORT_SCREEN; } else { @@ -158,18 +158,18 @@ BlpInitializeDebugConsole() /* Take next debug port */ DebugPort = RtlTokenizeWideString(NULL, L";", &LastPort); } - } - /* Check if serial debug port is enabled */ - if(BlpDebugPort & XTBL_DEBUGPORT_SERIAL) - { - /* Try to initialize COM port */ - Status = BlpInitializeSerialPort(PortNumber, PortAddress, BaudRate); - if(Status != STATUS_EFI_SUCCESS) + /* Check if serial debug port is enabled */ + if(BlpStatus.DebugPort & XTBL_DEBUGPORT_SERIAL) { - /* Remove serial debug port, as COM port initialization failed and return */ - BlpDebugPort &= ~XTBL_DEBUGPORT_SERIAL; - return Status; + /* Try to initialize COM port */ + Status = BlpInitializeSerialPort(PortNumber, PortAddress, BaudRate); + if(Status != STATUS_EFI_SUCCESS) + { + /* Remove serial debug port, as COM port initialization failed and return */ + BlpStatus.DebugPort &= ~XTBL_DEBUGPORT_SERIAL; + return Status; + } } } @@ -213,7 +213,7 @@ BlpInitializeSerialPort(IN ULONG PortNumber, } /* Initialize COM port */ - Status = HlInitializeComPort(&BlpSerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate); + Status = HlInitializeComPort(&BlpStatus.SerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate); /* Port not found under supplied address */ if(Status == STATUS_NOT_FOUND && PortAddress) @@ -224,7 +224,7 @@ BlpInitializeSerialPort(IN ULONG PortNumber, { /* 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); + Status = HlInitializeComPort(&BlpStatus.SerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate); } } @@ -259,5 +259,5 @@ BlpDebugPutChar(IN USHORT Character) Buffer[0] = Character; Buffer[1] = 0; - HlComPortPutByte(&BlpSerialPort, Buffer[0]); + HlComPortPutByte(&BlpStatus.SerialPort, Buffer[0]); } diff --git a/xtldr2/efiutils.c b/xtldr2/efiutils.c index bc41df4..b385b53 100644 --- a/xtldr2/efiutils.c +++ b/xtldr2/efiutils.c @@ -26,11 +26,11 @@ BlExitBootServices(IN UINT_PTR MapKey) EFI_STATUS Status; /* Attempt to exit boot services */ - Status = EfiSystemTable->BootServices->ExitBootServices(BlpStatus.ImageHandle, MapKey); + Status = EfiSystemTable->BootServices->ExitBootServices(EfiImageHandle, MapKey); if(Status != STATUS_EFI_SUCCESS) { /* Retry as UEFI spec says to do it twice */ - Status = EfiSystemTable->BootServices->ExitBootServices(BlpStatus.ImageHandle, MapKey); + Status = EfiSystemTable->BootServices->ExitBootServices(EfiImageHandle, MapKey); } /* Make sure boot services were successfully exited */ @@ -44,6 +44,39 @@ BlExitBootServices(IN UINT_PTR MapKey) return Status; } +/** + * Checks whether SecureBoot is enabled or not. + * + * @return Numeric representation of SecureBoot status (0 = Disabled, >0 = Enabled, <0 SetupMode). + * + * @since XT 1.0 + */ +XTCDECL +INT_PTR +BlGetSecureBootStatus() +{ + EFI_GUID VarGuid = EFI_GLOBAL_VARIABLE_GUID; + INT_PTR SecureBootStatus = 0; + UCHAR VarValue = 0; + UINT_PTR Size; + + Size = sizeof(VarValue); + if(EfiSystemTable->RuntimeServices->GetVariable(L"SecureBoot", &VarGuid, + NULL, &Size, &VarValue) == STATUS_EFI_SUCCESS) + { + SecureBootStatus = (INT_PTR)VarValue; + + if((EfiSystemTable->RuntimeServices->GetVariable(L"SetupMode", &VarGuid, + NULL, &Size, &VarValue) == STATUS_EFI_SUCCESS) && VarValue != 0) + { + SecureBootStatus = -1; + } + } + + /* Return SecureBoot status */ + return SecureBootStatus; +} + /** * Puts the system to sleep for the specified number of milliseconds. * @@ -74,8 +107,6 @@ BlpInitializeEfiBootLoader() { /* Set current XTLDR status */ BlpStatus.BootServices = TRUE; - BlpStatus.ImageHandle = EfiImageHandle; - BlpStatus.SystemTable = EfiSystemTable; /* Initialize console */ BlpInitializeConsole(); diff --git a/xtldr2/globals.c b/xtldr2/globals.c index 9fe0c5c..bbcc200 100644 --- a/xtldr2/globals.c +++ b/xtldr2/globals.c @@ -13,10 +13,7 @@ PLIST_ENTRY BlpConfig = NULL; /* XT Boot Loader loaded configuration */ -LIST_ENTRY BlpConfiguration; - -/* XT Boot Loader debug port configuration */ -ULONG BlpDebugPort = 0; +LIST_ENTRY BlpConfigSections; /* XT Boot Loader hex table */ STATIC PUINT16 BlpHexTable = L"0123456789ABCDEF"; @@ -24,9 +21,6 @@ STATIC PUINT16 BlpHexTable = L"0123456789ABCDEF"; /* XT Boot Loader menu list */ PLIST_ENTRY BlpMenuList = NULL; -/* Serial port configuration */ -CPPORT BlpSerialPort; - /* XT Boot Loader status data */ XTBL_STATUS BlpStatus = {0}; diff --git a/xtldr2/includes/bootman.h b/xtldr2/includes/bootman.h index 9a16e58..0cef47f 100644 --- a/xtldr2/includes/bootman.h +++ b/xtldr2/includes/bootman.h @@ -69,6 +69,10 @@ XTCDECL PWCHAR BlGetConfigValue(IN CONST PWCHAR ConfigName); +XTCDECL +INT_PTR +BlGetSecureBootStatus(); + XTCDECL EFI_STATUS BlGetVolumeDevicePath(IN PCHAR SystemPath, diff --git a/xtldr2/includes/globals.h b/xtldr2/includes/globals.h index 0b9074a..b913e9e 100644 --- a/xtldr2/includes/globals.h +++ b/xtldr2/includes/globals.h @@ -16,10 +16,7 @@ EXTERN PLIST_ENTRY BlpConfig; /* XT Boot Loader loaded configuration */ -EXTERN LIST_ENTRY BlpConfiguration; - -/* XT Boot Loader debug port configuration */ -EXTERN ULONG BlpDebugPort; +EXTERN LIST_ENTRY BlpConfigSections; /* XT Boot Loader hex table */ EXTERN PUINT16 BlpHexTable; @@ -27,9 +24,6 @@ EXTERN PUINT16 BlpHexTable; /* XT Boot Loader menu list */ EXTERN PLIST_ENTRY BlpMenuList; -/* Serial port configuration */ -EXTERN CPPORT BlpSerialPort; - /* XT Boot Loader status data */ EXTERN XTBL_STATUS BlpStatus; diff --git a/xtldr2/protocol.c b/xtldr2/protocol.c index 941e6dc..73e50ec 100644 --- a/xtldr2/protocol.c +++ b/xtldr2/protocol.c @@ -108,6 +108,7 @@ BlpRegisterXtLoaderProtocol() LdrProtocol.Memory.FreePool = BlMemoryFreePool; LdrProtocol.Protocol.Open = BlOpenXtProtocol; LdrProtocol.Util.ExitBootServices = BlExitBootServices; + LdrProtocol.Util.GetSecureBootStatus = BlGetSecureBootStatus; LdrProtocol.Util.SleepExecution = BlSleepExecution; /* Register XTLDR loader protocol */