ntdll: - `NtQuerySystemInformation` `SystemBootEnvironmentInformation` - `NtQuerySystemEnvironmentValueEx`
kernel32: - `GetFirmwareType` - `GetFirmwareEnvironmentVariableExA` - `GetFirmwareEnvironmentVariableExW` - `GetFirmwareEnvironmentVariableA` - `GetFirmwareEnvironmentVariableW`.
The code is needed to check SecureBoot status, security systems and for example to check the licenses of software that can use efi variables. The behavior matches the behavior of Windows 11. And the output is no different on wine and Windows 11.
Example:
```C #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #include <winnt.h> #include <winternl.h> #include <winbase.h> #include <shlwapi.h> #include <securitybaseapi.h>
/* x86_64-w64-mingw32-gcc -O2 firmware.c -o firmware.exe -Wl,--subsystem,console -lole32 */ #define SystemBootEnvironmentInformation 90
#define GUID_FORMAT "%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX" #define GUID_ARG(guid) \ (guid).Data1, (guid).Data2, (guid).Data3, (guid).Data4[0], (guid).Data4[1], (guid).Data4[2], (guid).Data4[3], \ (guid).Data4[4], (guid).Data4[5], (guid).Data4[6], (guid).Data4[7]
typedef struct _SYSTEM_BOOT_ENVIRONMENT_INFORMATION { GUID BootIdentifier; FIRMWARE_TYPE FirmwareType; union { ULONGLONG BootFlags; struct { ULONGLONG DbgMenuOsSelection : 1; ULONGLONG DbgHiberBoot : 1; ULONGLONG DbgSoftBoot : 1; ULONGLONG DbgMeasuredLaunch : 1; ULONGLONG DbgMeasuredLaunchCapable : 1; ULONGLONG DbgSystemHiveReplace : 1; ULONGLONG DbgMeasuredLaunchSmmProtections : 1; ULONGLONG DbgMeasuredLaunchSmmLevel : 7; }; }; } SYSTEM_BOOT_ENVIRONMENT_INFORMATION, *PSYSTEM_BOOT_ENVIRONMENT_INFORMATION;
typedef NTSTATUS(NTAPI *LPFNZwInitUnicodeString)(IN OUT PUNICODE_STRING DestinationString, IN PCWSTR SourceString);
typedef NTSTATUS(NTAPI *LPFNZwQuerySystemInformation)( IN SYSTEM_INFORMATION_CLASS SystemInformationClass, IN OUT PVOID SystemInformation, IN ULONG SystemInformationLength, OUT PULONG ReturnLength );
typedef NTSTATUS(NTAPI *LPFNZwQuerySystemEnvironmentValueEx)( IN PUNICODE_STRING VariableName, IN LPGUID VendorGuid, IN PVOID Value, IN OUT PULONG ReturnLength, OUT PULONG Attributes );
BOOL EnableTokenPrivilege(HANDLE hToken, LPCTSTR pszPrivilegesName, BOOL bEnabled) { LUID luid; TOKEN_PRIVILEGES tp;
if (!LookupPrivilegeValue(NULL, pszPrivilegesName, &luid)) { return FALSE; }
tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; tp.Privileges[0].Attributes = bEnabled ? SE_PRIVILEGE_ENABLED : 0;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) { return FALSE; }
return TRUE; }
int main(int argc, char **argv) { DWORD ret; ULONG ret_size; HANDLE hToken; HANDLE hProcess; HINSTANCE hNTdll; DWORD attributes; BOOLEAN secureboot = 0; FIRMWARE_TYPE type = FirmwareTypeUnknown;
UNICODE_STRING asus_name; char asus_license[2048] = {0};
UNICODE_STRING secureboot_name;
GUID asus_guid; GUID secureboot_guid;
SYSTEM_BOOT_ENVIRONMENT_INFORMATION boot_info = {0};
LPFNZwInitUnicodeString ZwInitUnicodeString; LPFNZwQuerySystemInformation ZwQuerySystemInformation; LPFNZwQuerySystemEnvironmentValueEx ZwQuerySystemEnvironmentValueEx;
if (CLSIDFromString(L"{02076249-a52b-420e-bd53-aed044349379}", &asus_guid) != NOERROR) { printf("CLSIDFromString error: %d!\n", GetLastError()); return -1; }
if (CLSIDFromString(L"{8be4df61-93ca-11d2-aa0d-00e098032b8c}", &secureboot_guid) != NOERROR) { printf("CLSIDFromString error: %d!\n", GetLastError()); return -1; }
hNTdll = LoadLibraryW(L"Ntdll.dll");
if (!hNTdll) { printf("Can't load ntdll liabrary!\n"); return -1; }
ZwInitUnicodeString = (LPFNZwInitUnicodeString) GetProcAddress(hNTdll, "RtlInitUnicodeString"); if (!ZwInitUnicodeString) { printf("Can't get GetProcAddress RtlInitUnicodeString error: %d!\n", GetLastError()); return -1; }
ZwInitUnicodeString(&secureboot_name, L"SecureBoot"); ZwInitUnicodeString(&asus_name, L"AsusOnboardToolLicense");
ZwQuerySystemInformation = (LPFNZwQuerySystemInformation) GetProcAddress(hNTdll, "ZwQuerySystemInformation"); if (!ZwQuerySystemInformation) { printf("Can't get GetProcAddress ZwQuerySystemInformation error: %d!\n", GetLastError()); return -1; }
ZwQuerySystemEnvironmentValueEx = (LPFNZwQuerySystemEnvironmentValueEx) GetProcAddress(hNTdll, "ZwQuerySystemEnvironmentValueEx"); if (!ZwQuerySystemEnvironmentValueEx) { printf("Can't get GetProcAddress ZwQuerySystemEnvironmentValueEx error: %d!\n", GetLastError()); return -1; }
hProcess = GetCurrentProcess(); if (!hProcess) { printf("GetCurrentProcessToken error: %d!\n", GetLastError()); return -1; }
if (!OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { printf("OpenProcessToken error: %d!\n", GetLastError()); return -1; }
if (!EnableTokenPrivilege(hToken, SE_SYSTEM_ENVIRONMENT_NAME, TRUE)) { printf("EnableTokenPrivilege error: %d!\n", GetLastError()); return -1; }
if (GetFirmwareType(&type)) printf("GetFirmwareType: %s\n\n", type == FirmwareTypeUefi ? "UEFI" : "BIOS");
ret = GetFirmwareEnvironmentVariableA( "SecureBoot", "{8be4df61-93ca-11d2-aa0d-00e098032b8c}", &secureboot, sizeof(BOOLEAN) ); if (!ret) { printf("GetFirmwareEnvironmentVariableA error: % d!\n\n", GetLastError()); return -1; } printf("GetFirmwareEnvironmentVariableA\nBytes: %d\nSecureBoot: %s\n\n", ret, secureboot ? "enabled" : "disabled");
secureboot = 0; ret = GetFirmwareEnvironmentVariableW( L"SecureBoot", L"{8be4df61-93ca-11d2-aa0d-00e098032b8c}", &secureboot, sizeof(BOOLEAN) ); if (!ret) { printf("GetFirmwareEnvironmentVariableW error: % d!\n\n", GetLastError()); return -1; } printf("GetFirmwareEnvironmentVariableW\nBytes: %d\nSecureBoot: %s\n\n", ret, secureboot ? "enabled" : "disabled");
attributes = 0; secureboot = 0; ret = GetFirmwareEnvironmentVariableExA( "SecureBoot", "{8be4df61-93ca-11d2-aa0d-00e098032b8c}", &secureboot, sizeof(BOOLEAN), &attributes ); if (!ret) { printf("GetFirmwareEnvironmentVariableExA error: % d!\n\n", GetLastError()); return -1; } printf( "GetFirmwareEnvironmentVariableExA\nBytes: %d\nAttributes: 0x%08x\nSecureBoot: %s\n\n", ret, attributes, secureboot ? "enabled" : "disabled" );
attributes = 0; ret = GetFirmwareEnvironmentVariableExW( L"AsusOnboardToolLicense", L"{02076249-a52b-420e-bd53-aed044349379}", asus_license, sizeof(asus_license), &attributes ); if (!ret) { printf("GetFirmwareEnvironmentVariableExW error: % d!\n\n", GetLastError()); return -1; } printf( "GetFirmwareEnvironmentVariableExW\nBytes: %d\nAttributes: 0x%08x\nAsus License: \n%s\n\n", ret, attributes, asus_license );
ret_size = sizeof(secureboot); attributes = 0; secureboot = 0; if (ZwQuerySystemEnvironmentValueEx(&secureboot_name, &secureboot_guid, &secureboot, &ret_size, &attributes) != NOERROR) { printf("ZwQuerySystemEnvironmentValueEx error: % d!\n\n", GetLastError()); FreeLibrary(hNTdll); return -1; } printf( "NtQuerySystemEnvironmentValueEx\nBytes: %d\nAttributes: 0x%08x\nSecureBoot: %s\n\n", ret_size, attributes, secureboot ? "enabled" : "disabled" );
ret_size = sizeof(asus_license); attributes = 0; for (size_t i = 0; i < ret_size; i++) { asus_license[i] = '\0'; } if (ZwQuerySystemEnvironmentValueEx(&asus_name, &asus_guid, asus_license, &ret_size, &attributes) != NOERROR) { printf("ZwQuerySystemEnvironmentValueEx error: % d!\n\n", GetLastError()); FreeLibrary(hNTdll); return -1; } printf( "NtQuerySystemEnvironmentValueEx\nBytes: %d\nAttributes: 0x%08x\nAsus License: \n%s\n\n", ret_size, attributes, asus_license );
if (ZwQuerySystemInformation( (SYSTEM_INFORMATION_CLASS) SystemBootEnvironmentInformation, &boot_info, sizeof(boot_info), &ret_size ) != NOERROR) { printf("ZwQuerySystemInformation error: % d!\n\n", GetLastError()); FreeLibrary(hNTdll); return -1; }
printf("SystemBootEnvironmentInformation:\nBootIdentifier: {" GUID_FORMAT "}", GUID_ARG(boot_info.BootIdentifier)); printf( "\nFirmwareType: %s\nBootFlags: 0x%llX\n\n", (boot_info.FirmwareType == FirmwareTypeUefi ? "UEFI" : "BIOS"), boot_info.BootFlags );
FreeLibrary(hNTdll);
return 0; } ```
Output: ``` GetFirmwareType: UEFI
GetFirmwareEnvironmentVariableA Bytes: 1 SecureBoot: enabled
GetFirmwareEnvironmentVariableW Bytes: 1 SecureBoot: enabled
GetFirmwareEnvironmentVariableExA Bytes: 1 Attributes: 0x00000006 SecureBoot: enabled
GetFirmwareEnvironmentVariableExW Bytes: 1570 Attributes: 0x00000006 Asus License: BASE64_BIG_LICENSE_TEXT== [LicenseID]:FED7A1 [Model]:Dummy-BaseBoard-Id [Tools]:WAsusDmiS,LogoFlashS,ASUSFwConfigS,AsusEventLogS,AsusNvLockS [Customer]:DummyLic [Expire]:2022-11-24
NtQuerySystemEnvironmentValueEx Bytes: 1 Attributes: 0x00000006 SecureBoot: enabled
NtQuerySystemEnvironmentValueEx Bytes: 1570 Attributes: 0x00000006 Asus License: BASE64_BIG_LICENSE_TEXT== [LicenseID]:FED7A1 [Model]:Dummy-BaseBoard-Id [Tools]:WAsusDmiS,LogoFlashS,ASUSFwConfigS,AsusEventLogS,AsusNvLockS [Customer]:DummyLic [Expire]:2022-11-24
SystemBootEnvironmentInformation: BootIdentifier: {8522FFCA-6A31-11EF-952A-966164983DCB} FirmwareType: UEFI BootFlags: 0x0 ```
-- v11: kernel32: Implement GetFirmwareEnvironmentVariableExA, GetFirmwareEnvironmentVariableExW, GetFirmwareEnvironmentVariableA, GetFirmwareEnvironmentVariableW. kernel32: Implement GetFirmwareType. ntdll: Implement NtQuerySystemInformation SystemBootEnvironmentInformation. ntdll: Implement NtQuerySystemEnvironmentValueEx.
From: Grigory Vasilyev h0tc0d3@gmail.com
--- dlls/ntdll/unix/system.c | 73 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-)
diff --git a/dlls/ntdll/unix/system.c b/dlls/ntdll/unix/system.c index f47925018b8..bf6f2815021 100644 --- a/dlls/ntdll/unix/system.c +++ b/dlls/ntdll/unix/system.c @@ -30,6 +30,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <errno.h> @@ -3682,9 +3683,77 @@ NTSTATUS WINAPI NtQuerySystemEnvironmentValue( UNICODE_STRING *name, WCHAR *buff NTSTATUS WINAPI NtQuerySystemEnvironmentValueEx( UNICODE_STRING *name, GUID *vendor, void *buffer, ULONG *retlen, ULONG *attrib ) { - FIXME( "(%s, %s, %p, %p, %p), stub\n", debugstr_us(name), - debugstr_guid(vendor), buffer, retlen, attrib ); +#if defined(__linux__) || defined(__gnu_linux__) + int fd, rc; + size_t bytes, pos = 0; + ssize_t ssz; + char filename[256]; + char *cname; + struct stat stat_info = {0}; + + if(!name || !vendor || !attrib) + { + return ERROR_INVALID_PARAMETER; + } + + cname = (char *) malloc(name->Length); + ntdll_wcstoumbs(name->Buffer, name->Length, cname, name->Length, FALSE); + + snprintf(filename, sizeof(filename), + "/sys/firmware/efi/efivars/%s-%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", cname, + (unsigned long)vendor->Data1, vendor->Data2, vendor->Data3, vendor->Data4[0], vendor->Data4[1], + vendor->Data4[2], vendor->Data4[3], vendor->Data4[4], vendor->Data4[5], + vendor->Data4[6], vendor->Data4[7]); + + fd = open(filename, O_RDONLY | O_NONBLOCK); + if (fd < 0) + goto done; + + rc = fstat(fd, &stat_info); + if (rc < 0 || stat_info.st_size == 0) + goto done; + +try_read_attributes: + ssz = read(fd, attrib, 4); + if (ssz < 0) + { + if (errno == EAGAIN || errno == EINTR) + goto try_read_attributes; + goto done; + } + + if(buffer && retlen) + { + if (stat_info.st_size - 4 < *retlen) + bytes = stat_info.st_size - 4; + else + bytes = *retlen; + while (pos < bytes) + { + ssz = read(fd, (char *) buffer + pos, bytes - pos); + if (ssz < 0) + { + if (errno == EAGAIN || errno == EINTR) + continue; + goto done; + } + pos += ssz; + } + *retlen = ssz & 0xFFFFFFFF; + } + + close(fd); + return STATUS_SUCCESS; + +done: + if (fd >= 0) + close(fd); + return STATUS_UNSUCCESSFUL; +#else + FIXME("(%s, %s, %p, %p, %p), stub\n", debugstr_us(name), debugstr_guid(vendor), buffer, retlen, + attrib); return STATUS_NOT_IMPLEMENTED; +#endif }
From: Grigory Vasilyev h0tc0d3@gmail.com
--- dlls/ntdll/unix/system.c | 142 +++++++++++++++++++++++++++++++++++++++ dlls/wow64/system.c | 3 + include/winternl.h | 22 ++++++ 3 files changed, 167 insertions(+)
diff --git a/dlls/ntdll/unix/system.c b/dlls/ntdll/unix/system.c index bf6f2815021..a3449049553 100644 --- a/dlls/ntdll/unix/system.c +++ b/dlls/ntdll/unix/system.c @@ -2763,6 +2763,65 @@ C_ASSERT( sizeof(struct process_info) <= sizeof(SYSTEM_PROCESS_INFORMATION) ); return ret; }
+/****************************************************************************** + * __wine_hexstr32_to_guid (NTDLL.@) + */ +static inline BOOL __wine_hexstr32_to_guid(const char *s, GUID *id) +{ + int i; + if (!s) { + memset(id, 0, sizeof(*id)); + return FALSE; + } + + id->Data1 = 0; + for (i = 0; i < 8; ++i) { + if (!(((s[i] >= '0') && (s[i] <= '9')) + || ((s[i] >= 'a') && (s[i] <= 'f')) + || ((s[i] >= 'A') && (s[i] <= 'F')))) + return FALSE; + id->Data1 = (id->Data1 << 4) + | ((s[i] & 0xF) + (s[i] >> 6)) + | ((s[i] >> 3) & 0x8); + } + + id->Data2 = 0; + for (i = 8; i < 12; ++i) { + if (!(((s[i] >= '0') && (s[i] <= '9')) + || ((s[i] >= 'a') && (s[i] <= 'f')) + || ((s[i] >= 'A') && (s[i] <= 'F')))) + return FALSE; + id->Data2 = (id->Data2 << 4) + | ((s[i] & 0xF) + (s[i] >> 6)) + | ((s[i] >> 3) & 0x8); + } + + id->Data3 = 0; + for (i = 12; i < 16; ++i) { + if (!(((s[i] >= '0') && (s[i] <= '9')) + || ((s[i] >= 'a') && (s[i] <= 'f')) + || ((s[i] >= 'A') && (s[i] <= 'F')))) + return FALSE; + id->Data3 = (id->Data3 << 4) + | ((s[i] & 0xF) + (s[i] >> 6)) + | ((s[i] >> 3) & 0x8); + } + + for (i = 16; i < 32; i += 2) { + if (!(((s[i] >= '0') && (s[i] <= '9')) + || ((s[i] >= 'a') && (s[i] <= 'f')) + || ((s[i] >= 'A') && (s[i] <= 'F')) + || ((s[i + 1] >= '0') && (s[i + 1] <= '9')) + || ((s[i + 1] >= 'a') && (s[i + 1] <= 'f')) + || ((s[i + 1] >= 'A') && (s[i + 1] <= 'F')))) + return FALSE; + id->Data4[(i - 16) / 2] = ((((s[i] & 0xF) + (s[i] >> 6)) | ((s[i] >> 3) & 0x8)) << 4) + | (((s[i + 1] & 0xF) + (s[i + 1] >> 6)) | ((s[i + 1] >> 3) & 0x8)); + } + + return TRUE; +} + /****************************************************************************** * NtQuerySystemInformation (NTDLL.@) */ @@ -2794,6 +2853,89 @@ NTSTATUS WINAPI NtQuerySystemInformation( SYSTEM_INFORMATION_CLASS class, break; }
+ case SystemBootEnvironmentInformation: /* 90 */ + { + static SYSTEM_BOOT_ENVIRONMENT_INFORMATION boot_info = {0}; + struct stat stat_info = {0}; + ssize_t ssz; + len = sizeof(boot_info); + if (size == len) + { +#if defined(__linux__) || defined(__gnu_linux__) + int fd; + char buffer[32]; + + if (boot_info.FirmwareType == FirmwareTypeUnknown) + { + if (!stat("/sys/firmware/efi", &stat_info)) + boot_info.FirmwareType = FirmwareTypeUefi; + else + boot_info.FirmwareType = FirmwareTypeBios; + } +#else + boot_info.FirmwareType = FirmwareTypeBios; +#endif + if (boot_info.BootIdentifier.Data1 == 0) + { +#if defined(__linux__) || defined(__gnu_linux__) + if (!stat("/etc/machine-id", &stat_info) && stat_info.st_size >= 32) + { + fd = open("/etc/machine-id", O_RDONLY | O_NONBLOCK); + if (fd >= 0) + { + try_read_guid: + ssz = read(fd, &buffer, 32); + if (ssz < 0 && (errno == EAGAIN || errno == EINTR)) + goto try_read_guid; + close(fd); + if (!__wine_hexstr32_to_guid(buffer, &boot_info.BootIdentifier)) + goto try_read_hostid; + } + else goto try_read_hostid; + } + else + { + try_read_hostid: +#endif + if (!stat("/home", &stat_info) || !stat("/usr", &stat_info)) + { + boot_info.BootIdentifier.Data1 = gethostid() & 0xFFFFFFFF; + boot_info.BootIdentifier.Data2 = stat_info.st_dev & 0xFFFF; + boot_info.BootIdentifier.Data3 = stat_info.st_ino & 0xFFFF; + boot_info.BootIdentifier.Data4[0] = 'W' + 136; + boot_info.BootIdentifier.Data4[1] = '1' + 142; + boot_info.BootIdentifier.Data4[2] = 'n' + 136; + boot_info.BootIdentifier.Data4[3] = '3' + 136; + boot_info.BootIdentifier.Data4[4] = 'G' + 128; + boot_info.BootIdentifier.Data4[5] = 'u' + 136; + boot_info.BootIdentifier.Data4[6] = '1' + 128; + boot_info.BootIdentifier.Data4[7] = 'd' + 136; + } + else + { + try_read_random: + ssz = getrandom((void *)&boot_info.BootIdentifier, sizeof(boot_info.BootIdentifier), 0); + if (ssz < 0 && (errno == EAGAIN || errno == EINTR )) + goto try_read_random; + } +#if defined(__linux__) || defined(__gnu_linux__) + } +#endif + boot_info.BootIdentifier.Data3 &= 0x0fff; + boot_info.BootIdentifier.Data3 |= (4 << 12); + /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as + * specified in RFC 4122, section 4.4. + */ + boot_info.BootIdentifier.Data4[0] &= 0x3f; + boot_info.BootIdentifier.Data4[0] |= 0x80; + } + memcpy(info, &boot_info, len); + } + else ret = STATUS_INFO_LENGTH_MISMATCH; + if (ret_size) *ret_size = len; + break; + } + case SystemCpuInformation: /* 1 */ if (size >= (len = sizeof(cpu_info))) memcpy(info, &cpu_info, len); else ret = STATUS_INFO_LENGTH_MISMATCH; diff --git a/dlls/wow64/system.c b/dlls/wow64/system.c index 5f3056a4179..402b53e1eda 100644 --- a/dlls/wow64/system.c +++ b/dlls/wow64/system.c @@ -356,6 +356,9 @@ NTSTATUS WINAPI wow64_NtQuerySystemInformation( UINT *args ) if (retlen) *retlen = sizeof(SYSTEM_BASIC_INFORMATION32); return status;
+ case SystemBootEnvironmentInformation: /* SYSTEM_BOOT_ENVIRONMENT_INFORMATION */ + return NtQuerySystemInformation(class, ptr, len, retlen); + case SystemProcessInformation: /* SYSTEM_PROCESS_INFORMATION */ case SystemExtendedProcessInformation: /* SYSTEM_PROCESS_INFORMATION */ { diff --git a/include/winternl.h b/include/winternl.h index 828fbc6e915..510bba09042 100644 --- a/include/winternl.h +++ b/include/winternl.h @@ -2724,6 +2724,28 @@ typedef struct _SYSTEM_BASIC_INFORMATION { #endif } SYSTEM_BASIC_INFORMATION, *PSYSTEM_BASIC_INFORMATION;
+/* System Information Class 0x90 */ +typedef struct _SYSTEM_BOOT_ENVIRONMENT_INFORMATION +{ + GUID BootIdentifier; + FIRMWARE_TYPE FirmwareType; + union + { + ULONGLONG BootFlags; + struct + { + ULONGLONG DbgMenuOsSelection : 1; + ULONGLONG DbgHiberBoot : 1; + ULONGLONG DbgSoftBoot : 1; + ULONGLONG DbgMeasuredLaunch : 1; + ULONGLONG DbgMeasuredLaunchCapable : 1; + ULONGLONG DbgSystemHiveReplace : 1; + ULONGLONG DbgMeasuredLaunchSmmProtections : 1; + ULONGLONG DbgMeasuredLaunchSmmLevel : 7; + }; + }; +} SYSTEM_BOOT_ENVIRONMENT_INFORMATION, *PSYSTEM_BOOT_ENVIRONMENT_INFORMATION; + /* System Information Class 0x01 */
typedef struct _SYSTEM_CPU_INFORMATION {
From: Grigory Vasilyev h0tc0d3@gmail.com
--- dlls/kernel32/process.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/dlls/kernel32/process.c b/dlls/kernel32/process.c index 2a4ddc68f02..33baa61b3a6 100644 --- a/dlls/kernel32/process.c +++ b/dlls/kernel32/process.c @@ -763,10 +763,17 @@ BOOL WINAPI SetFirmwareEnvironmentVariableW(const WCHAR *name, const WCHAR *guid */ BOOL WINAPI GetFirmwareType(FIRMWARE_TYPE *type) { + ULONG ret_size; + SYSTEM_BOOT_ENVIRONMENT_INFORMATION boot_info = {0}; + if (!type) return FALSE;
- *type = FirmwareTypeUnknown; + NtQuerySystemInformation(SystemBootEnvironmentInformation, &boot_info, sizeof(boot_info), + &ret_size); + + *type = boot_info.FirmwareType; + return TRUE; }
From: Grigory Vasilyev h0tc0d3@gmail.com
--- dlls/kernel32/kernel32.spec | 2 + dlls/kernel32/process.c | 164 ++++++++++++++++++++++++++++++++++-- 2 files changed, 160 insertions(+), 6 deletions(-)
diff --git a/dlls/kernel32/kernel32.spec b/dlls/kernel32/kernel32.spec index 7ed8048f5d0..5d3556a2078 100644 --- a/dlls/kernel32/kernel32.spec +++ b/dlls/kernel32/kernel32.spec @@ -695,7 +695,9 @@ @ stdcall -import GetFileType(long) @ stdcall -import GetFinalPathNameByHandleA(long ptr long long) @ stdcall -import GetFinalPathNameByHandleW(long ptr long long) +@ stdcall GetFirmwareEnvironmentVariableExA(str str ptr long ptr) @ stdcall GetFirmwareEnvironmentVariableA(str str ptr long) +@ stdcall GetFirmwareEnvironmentVariableExW(wstr wstr ptr long ptr) @ stdcall GetFirmwareEnvironmentVariableW(wstr wstr ptr long) @ stdcall GetFirmwareType(ptr) @ stdcall -import GetFullPathNameA(str long ptr ptr) diff --git a/dlls/kernel32/process.c b/dlls/kernel32/process.c index 33baa61b3a6..0cd11f927aa 100644 --- a/dlls/kernel32/process.c +++ b/dlls/kernel32/process.c @@ -718,14 +718,167 @@ WORD WINAPI GetMaximumProcessorGroupCount(void) return groups; }
+/*********************************************************************** + * __wine_string_to_guid (KERNEL32.@) + */ +static inline BOOL __wine_string_to_guid(LPCSTR s, GUID *id) +{ + int i; + if (!s || s[0] != '{') { + memset(id, 0, sizeof(*id)); + return FALSE; + } + + id->Data1 = 0; + for (i = 1; i < 9; ++i) { + if (!(((s[i] >= '0') && (s[i] <= '9')) + || ((s[i] >= 'a') && (s[i] <= 'f')) + || ((s[i] >= 'A') && (s[i] <= 'F')))) + return FALSE; + id->Data1 = (id->Data1 << 4) + | ((s[i] & 0xF) + (s[i] >> 6)) + | ((s[i] >> 3) & 0x8); + } + + if (s[9] != '-') + return FALSE; + + id->Data2 = 0; + for (i = 10; i < 14; ++i) { + if (!(((s[i] >= '0') && (s[i] <= '9')) + || ((s[i] >= 'a') && (s[i] <= 'f')) + || ((s[i] >= 'A') && (s[i] <= 'F')))) + return FALSE; + id->Data2 = (id->Data2 << 4) + | ((s[i] & 0xF) + (s[i] >> 6)) + | ((s[i] >> 3) & 0x8); + } + + if (s[14] != '-') + return FALSE; + + id->Data3 = 0; + for (i = 15; i < 19; ++i) { + if (!(((s[i] >= '0') && (s[i] <= '9')) + || ((s[i] >= 'a') && (s[i] <= 'f')) + || ((s[i] >= 'A') && (s[i] <= 'F')))) + return FALSE; + id->Data3 = (id->Data3 << 4) + | ((s[i] & 0xF) + (s[i] >> 6)) + | ((s[i] >> 3) & 0x8); + } + + if (s[19] != '-') + return FALSE; + + for (i = 20; i < 37; i += 2) { + if (i == 24) { + if (s[i] != '-') + return FALSE; + i++; + } + if (!(((s[i] >= '0') && (s[i] <= '9')) + || ((s[i] >= 'a') && (s[i] <= 'f')) + || ((s[i] >= 'A') && (s[i] <= 'F')) + || ((s[i + 1] >= '0') && (s[i + 1] <= '9')) + || ((s[i + 1] >= 'a') && (s[i + 1] <= 'f')) + || ((s[i + 1] >= 'A') && (s[i + 1] <= 'F')))) + return FALSE; + id->Data4[(i - 20) / 2] = ((((s[i] & 0xF) + (s[i] >> 6)) | ((s[i] >> 3) & 0x8)) << 4) + | (((s[i + 1] & 0xF) + (s[i + 1] >> 6)) | ((s[i + 1] >> 3) & 0x8)); + } + + if (s[37] == '}' && s[38] == '\0') + return TRUE; + + return FALSE; +} + +/*********************************************************************** + * GetFirmwareEnvironmentVariableExA (KERNEL32.@) + */ +DWORD WINAPI GetFirmwareEnvironmentVariableExA(LPCSTR name, LPCSTR guid, PVOID buffer, DWORD size, PDWORD attributes) +{ + int nsize; + GUID vendor = {0}; + LPWSTR wname; + UNICODE_STRING uname; + DWORD ret_size = size; + + if(!name || !guid || !attributes) + { + SetLastError(ERROR_INVALID_PARAMETER); + return 0; + } + + if (!__wine_string_to_guid(guid, &vendor)) + { + SetLastError(CO_E_CLASSSTRING); + return 0; + } + + nsize = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0); + + if (!(wname = HeapAlloc(GetProcessHeap(), 0, nsize * sizeof(wname)))) + { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return 0; + } + + MultiByteToWideChar(CP_ACP, 0, name, -1, wname, nsize); + + RtlInitUnicodeString(&uname, wname); + + if (!set_ntstatus( + NtQuerySystemEnvironmentValueEx(&uname, &vendor, buffer, &ret_size, attributes))) + { + ret_size = 0; + } + + HeapFree(GetProcessHeap(), 0, wname); + + return ret_size; +} + /*********************************************************************** * GetFirmwareEnvironmentVariableA (KERNEL32.@) */ DWORD WINAPI GetFirmwareEnvironmentVariableA(LPCSTR name, LPCSTR guid, PVOID buffer, DWORD size) { - FIXME("stub: %s %s %p %lu\n", debugstr_a(name), debugstr_a(guid), buffer, size); - SetLastError(ERROR_INVALID_FUNCTION); - return 0; + DWORD attributes; + return GetFirmwareEnvironmentVariableExA(name, guid, buffer, size, &attributes); +} + +/*********************************************************************** + * GetFirmwareEnvironmentVariableExW (KERNEL32.@) + */ +DWORD WINAPI GetFirmwareEnvironmentVariableExW(LPCWSTR name, LPCWSTR guid, PVOID buffer, DWORD size, PDWORD attributes) +{ + GUID vendor = {0}; + UNICODE_STRING uname; + UNICODE_STRING uguid; + DWORD ret_size = size; + + if(!name || !guid || !attributes) + { + SetLastError(ERROR_INVALID_PARAMETER); + return 0; + } + + RtlInitUnicodeString(&uguid, guid); + + if (!set_ntstatus(RtlGUIDFromString(&uguid, &vendor))) + return 0; + + RtlInitUnicodeString(&uname, name); + + if (!set_ntstatus( + NtQuerySystemEnvironmentValueEx(&uname, &vendor, buffer, &ret_size, attributes))) + { + return 0; + } + + return ret_size; }
/*********************************************************************** @@ -733,9 +886,8 @@ DWORD WINAPI GetFirmwareEnvironmentVariableA(LPCSTR name, LPCSTR guid, PVOID buf */ DWORD WINAPI GetFirmwareEnvironmentVariableW(LPCWSTR name, LPCWSTR guid, PVOID buffer, DWORD size) { - FIXME("stub: %s %s %p %lu\n", debugstr_w(name), debugstr_w(guid), buffer, size); - SetLastError(ERROR_INVALID_FUNCTION); - return 0; + DWORD attributes; + return GetFirmwareEnvironmentVariableExW(name, guid, buffer, size, &attributes); }
/***********************************************************************
On Thu Sep 5 18:52:07 2024 +0000, Alfred Agrell wrote:
This throws a warning on 32bit builds.
../dlls/ntdll/unix/system.c:3698:47: error: format '%X' expects argument of type 'unsigned int', but argument 5 has type 'long unsigned int' [-Werror=format=] 3698 | "/sys/firmware/efi/efivars/%s-%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", cname, | ~~~^ | | | unsigned int | %08lX 3699 | vendor->Data1, vendor->Data2, vendor->Data3, vendor->Data4[0], vendor->Data4[1], | ~~~~~~~~~~~~~ | | | long unsigned int
They're both uint32, so I'd prefer if compiler could just shut up. But we can't really affect that on our end. Let's just explicitly cast to (unsigned).
Done.
On Thu Sep 5 13:54:50 2024 +0000, Nikolay Sivov wrote:
Yes, what I would do probably is "if (!set_ntstatus()) return FALSE; *type = ...; return TRUE;". We can't really test a case when nt call fails, so it's fine to assume it doesn't.
@zfigura already have fallback. @nsivov I think this is unnecessary.
On Thu Sep 5 18:53:50 2024 +0000, Grigory Vasilyev wrote:
@zfigura already have fallback. @nsivov I think this is unnecessary.
I am not sure if I am the one to tell this, but sharing my Wine development experience, if you are will argue with maintainers about trivial (and, actually, in this case obvious) things it is not going to be merged any time soon. Also, these functions really can use a unit test, it would probably catch some implementation issues at once.