Bootmgr (Windows): prefers NT API

This commit is contained in:
李通洲
2026-03-02 10:42:29 +08:00
parent 0a55314d37
commit 70fa1341a8
2 changed files with 35 additions and 8 deletions
+17
View File
@@ -6,6 +6,7 @@
enum {
SystemModuleInformation = 11,
SystemBootEnvironmentInformation = 90,
SystemSecureBootInformation = 146,
};
#define D3DKMT_ALIGN64 __attribute__((aligned(8)))
@@ -378,3 +379,19 @@ typedef struct _RTL_PROCESS_MODULES
ULONG NumberOfModules;
_Field_size_(NumberOfModules) RTL_PROCESS_MODULE_INFORMATION Modules[1];
} RTL_PROCESS_MODULES, *PRTL_PROCESS_MODULES;
NTSTATUS NTAPI NtQuerySystemEnvironmentValueEx(
_In_ PCUNICODE_STRING VariableName,
_In_ const GUID* VendorGuid,
_Out_writes_bytes_opt_(*BufferLength) PVOID Buffer,
_Inout_ PULONG BufferLength,
_Out_opt_ PULONG Attributes // EFI_VARIABLE_*
);
NTSTATUS NTAPI RtlGUIDFromString(IN PCUNICODE_STRING GuidString, OUT GUID* Guid);
typedef struct _SYSTEM_SECUREBOOT_INFORMATION
{
BOOLEAN SecureBootEnabled;
BOOLEAN SecureBootCapable;
} SYSTEM_SECUREBOOT_INFORMATION, *PSYSTEM_SECUREBOOT_INFORMATION;
+18 -8
View File
@@ -3,6 +3,7 @@
#include "common/io.h"
#include "common/windows/nt.h"
#include <ntdef.h>
#include <windows.h>
const char* enablePrivilege(const wchar_t* privilege)
@@ -34,21 +35,30 @@ const char* ffDetectBootmgr(FFBootmgrResult* result)
if (enablePrivilege(L"SeSystemEnvironmentPrivilege") != NULL)
return "Failed to enable SeSystemEnvironmentPrivilege";
if (GetFirmwareEnvironmentVariableW(L"BootCurrent", L"{" FF_EFI_GLOBAL_GUID L"}", &result->order, sizeof(result->order)) != 2)
return "GetFirmwareEnvironmentVariableW(BootCurrent) failed";
GUID efiGlobalGuid;
if (!NT_SUCCESS(RtlGUIDFromString(&(UNICODE_STRING) RTL_CONSTANT_STRING(L"{" FF_EFI_GLOBAL_GUID L"}"), &efiGlobalGuid)))
return "RtlGUIDFromString() failed";
ULONG size = sizeof(result->order);
if (!NT_SUCCESS(NtQuerySystemEnvironmentValueEx(&(UNICODE_STRING) RTL_CONSTANT_STRING(L"BootCurrent"), &efiGlobalGuid, &result->order, &size, NULL)))
return "NtQuerySystemEnvironmentValueEx(BootCurrent) failed";
if (size != sizeof(result->order))
return "NtQuerySystemEnvironmentValueEx(BootCurrent) returned unexpected size";
uint8_t buffer[2048];
wchar_t key[16];
wchar_t key[9];
swprintf(key, ARRAY_SIZE(key), L"Boot%04X", result->order);
uint32_t size = GetFirmwareEnvironmentVariableW(key, L"{" FF_EFI_GLOBAL_GUID L"}", buffer, sizeof(buffer));
size = sizeof(buffer);
if (!NT_SUCCESS(NtQuerySystemEnvironmentValueEx(&(UNICODE_STRING) RTL_CONSTANT_STRING(key), &efiGlobalGuid, buffer, &size, NULL)))
return "NtQuerySystemEnvironmentValueEx(Boot####) failed";
if (size < sizeof(FFEfiLoadOption) || size == ARRAY_SIZE(buffer))
return "GetFirmwareEnvironmentVariableW(Boot####) failed";
return "NtQuerySystemEnvironmentValueEx(Boot####) returned unexpected size";
ffEfiFillLoadOption((FFEfiLoadOption *)buffer, result);
DWORD uefiSecureBootEnabled = 0, bufSize = 0;
if (RegGetValueW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\SecureBoot\\State", L"UEFISecureBootEnabled", RRF_RT_REG_DWORD, NULL, &uefiSecureBootEnabled, &bufSize) == ERROR_SUCCESS)
result->secureBoot = !!uefiSecureBootEnabled;
SYSTEM_SECUREBOOT_INFORMATION ssi;
if (NT_SUCCESS(NtQuerySystemInformation(SystemSecureBootInformation, &ssi, sizeof(ssi), NULL)))
result->secureBoot = ssi.SecureBootEnabled;
return NULL;
}