1 | /* $Id: RTSystemFirmware-win.cpp 81063 2019-09-27 21:54:24Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - System firmware information, Win32.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/system.h>
|
---|
33 |
|
---|
34 | #include <iprt/nt/nt-and-windows.h>
|
---|
35 | #include <WinSDKVer.h>
|
---|
36 |
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/mem.h>
|
---|
41 | #include <iprt/ldr.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include <iprt/utf16.h>
|
---|
44 |
|
---|
45 | #include "internal-r3-win.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 | #if _WIN32_MAXVER < 0x0602 /* Windows 7 or older, supply missing GetFirmwareType bits. */
|
---|
52 | typedef enum _FIRMWARE_TYPE
|
---|
53 | {
|
---|
54 | FirmwareTypeUnknown,
|
---|
55 | FirmwareTypeBios,
|
---|
56 | FirmwareTypeUefi,
|
---|
57 | FirmwareTypeMax
|
---|
58 | } FIRMWARE_TYPE;
|
---|
59 | typedef FIRMWARE_TYPE *PFIRMWARE_TYPE;
|
---|
60 | WINBASEAPI BOOL WINAPI GetFirmwareType(PFIRMWARE_TYPE);
|
---|
61 | #endif
|
---|
62 |
|
---|
63 |
|
---|
64 | /*********************************************************************************************************************************
|
---|
65 | * Defined Constants And Macros *
|
---|
66 | *********************************************************************************************************************************/
|
---|
67 | /** Defines the UEFI Globals UUID. */
|
---|
68 | #define VBOX_UEFI_UUID_GLOBALS L"{8BE4DF61-93CA-11D2-AA0D-00E098032B8C}"
|
---|
69 | /** Defines an UEFI dummy UUID, see MSDN docs of the API. */
|
---|
70 | #define VBOX_UEFI_UUID_DUMMY L"{00000000-0000-0000-0000-000000000000}"
|
---|
71 |
|
---|
72 |
|
---|
73 | /*********************************************************************************************************************************
|
---|
74 | * Global Variables *
|
---|
75 | *********************************************************************************************************************************/
|
---|
76 | static volatile bool g_fResolvedApis = false;
|
---|
77 | static decltype(GetFirmwareType) *g_pfnGetFirmwareType;
|
---|
78 | static decltype(GetFirmwareEnvironmentVariableW) *g_pfnGetFirmwareEnvironmentVariableW;
|
---|
79 |
|
---|
80 |
|
---|
81 | static void rtSystemFirmwareResolveApis(void)
|
---|
82 | {
|
---|
83 | FARPROC pfnTmp1 = GetProcAddress(g_hModKernel32, "GetFirmwareType");
|
---|
84 | FARPROC pfnTmp2 = GetProcAddress(g_hModKernel32, "GetFirmwareEnvironmentVariableW");
|
---|
85 | ASMCompilerBarrier(); /* paranoia^2 */
|
---|
86 |
|
---|
87 | g_pfnGetFirmwareType = (decltype(GetFirmwareType) *)pfnTmp1;
|
---|
88 | g_pfnGetFirmwareEnvironmentVariableW = (decltype(GetFirmwareEnvironmentVariableW) *)pfnTmp2;
|
---|
89 | ASMAtomicWriteBool(&g_fResolvedApis, true);
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | static int rtSystemFirmwareGetPrivileges(LPCTSTR pcszPrivilege)
|
---|
94 | {
|
---|
95 | HANDLE hToken;
|
---|
96 | BOOL fRc = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
|
---|
97 | if (!fRc)
|
---|
98 | return RTErrConvertFromWin32(GetLastError());
|
---|
99 |
|
---|
100 | int rc = VINF_SUCCESS;
|
---|
101 |
|
---|
102 | TOKEN_PRIVILEGES tokenPriv;
|
---|
103 | fRc = LookupPrivilegeValue(NULL, pcszPrivilege, &tokenPriv.Privileges[0].Luid);
|
---|
104 | if (fRc)
|
---|
105 | {
|
---|
106 | tokenPriv.PrivilegeCount = 1;
|
---|
107 | tokenPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
---|
108 | fRc = AdjustTokenPrivileges(hToken, FALSE, &tokenPriv, 0, (PTOKEN_PRIVILEGES)NULL, 0);
|
---|
109 | if (!fRc)
|
---|
110 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
111 | }
|
---|
112 | else
|
---|
113 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
114 |
|
---|
115 | CloseHandle(hToken);
|
---|
116 |
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | RTDECL(int) RTSystemFirmwareQueryType(PRTSYSFWTYPE penmFirmwareType)
|
---|
122 | {
|
---|
123 | AssertPtrReturn(penmFirmwareType, VERR_INVALID_POINTER);
|
---|
124 |
|
---|
125 | if (!g_fResolvedApis)
|
---|
126 | rtSystemFirmwareResolveApis();
|
---|
127 |
|
---|
128 | *penmFirmwareType = RTSYSFWTYPE_INVALID;
|
---|
129 | int rc = VERR_NOT_SUPPORTED;
|
---|
130 |
|
---|
131 | /* GetFirmwareType is Windows 8 and later. */
|
---|
132 | if (g_pfnGetFirmwareType)
|
---|
133 | {
|
---|
134 | FIRMWARE_TYPE enmWinFwType;
|
---|
135 | if (g_pfnGetFirmwareType(&enmWinFwType))
|
---|
136 | {
|
---|
137 | switch (enmWinFwType)
|
---|
138 | {
|
---|
139 | case FirmwareTypeBios:
|
---|
140 | *penmFirmwareType = RTSYSFWTYPE_BIOS;
|
---|
141 | break;
|
---|
142 | case FirmwareTypeUefi:
|
---|
143 | *penmFirmwareType = RTSYSFWTYPE_UEFI;
|
---|
144 | break;
|
---|
145 | default:
|
---|
146 | *penmFirmwareType = RTSYSFWTYPE_UNKNOWN;
|
---|
147 | AssertMsgFailed(("%d\n", enmWinFwType));
|
---|
148 | break;
|
---|
149 | }
|
---|
150 | rc = VINF_SUCCESS;
|
---|
151 | }
|
---|
152 | else
|
---|
153 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
154 | }
|
---|
155 | /* GetFirmwareEnvironmentVariableW is XP and later. */
|
---|
156 | else if (g_pfnGetFirmwareEnvironmentVariableW)
|
---|
157 | {
|
---|
158 | rtSystemFirmwareGetPrivileges(SE_SYSTEM_ENVIRONMENT_NAME);
|
---|
159 |
|
---|
160 | /* On a non-UEFI system (or such a system in legacy boot mode), we will get
|
---|
161 | back ERROR_INVALID_FUNCTION when querying any firmware variable. While on a
|
---|
162 | UEFI system we'll typically get ERROR_ACCESS_DENIED or similar as the dummy
|
---|
163 | is a non-exising dummy namespace. See the API docs. */
|
---|
164 | SetLastError(0);
|
---|
165 | uint8_t abWhatever[64];
|
---|
166 | DWORD cbRet = g_pfnGetFirmwareEnvironmentVariableW(L"", VBOX_UEFI_UUID_DUMMY, abWhatever, sizeof(abWhatever));
|
---|
167 | DWORD dwErr = GetLastError();
|
---|
168 | *penmFirmwareType = cbRet != 0 || dwErr != ERROR_INVALID_FUNCTION ? RTSYSFWTYPE_UEFI : RTSYSFWTYPE_BIOS;
|
---|
169 | rc = VINF_SUCCESS;
|
---|
170 | }
|
---|
171 | return rc;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | RTDECL(void) RTSystemFirmwareFreeValue(PRTSYSFWVALUE pValue)
|
---|
176 | {
|
---|
177 | RT_NOREF(pValue);
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | RTDECL(int) RTSystemFirmwareQueryValue(RTSYSFWPROP enmProp, PRTSYSFWVALUE pValue)
|
---|
182 | {
|
---|
183 | RT_ZERO(*pValue);
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * Translate the enmProp to a name and type:
|
---|
187 | */
|
---|
188 | const wchar_t *pwszName = NULL;
|
---|
189 | switch (enmProp)
|
---|
190 | {
|
---|
191 | case RTSYSFWPROP_SECURE_BOOT:
|
---|
192 | pwszName = L"SecureBoot";
|
---|
193 | pValue->enmType = RTSYSFWVALUETYPE_BOOLEAN;
|
---|
194 | break;
|
---|
195 |
|
---|
196 | default:
|
---|
197 | AssertReturn(enmProp > RTSYSFWPROP_INVALID && enmProp < RTSYSFWPROP_END, VERR_INVALID_PARAMETER);
|
---|
198 | return VERR_SYS_UNSUPPORTED_FIRMWARE_PROPERTY;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * Do type specific query.
|
---|
203 | */
|
---|
204 | if (!g_pfnGetFirmwareEnvironmentVariableW)
|
---|
205 | return VERR_NOT_SUPPORTED;
|
---|
206 | rtSystemFirmwareGetPrivileges(SE_SYSTEM_ENVIRONMENT_NAME);
|
---|
207 |
|
---|
208 | int rc;
|
---|
209 | switch (pValue->enmType)
|
---|
210 | {
|
---|
211 | case RTSYSFWVALUETYPE_BOOLEAN:
|
---|
212 | {
|
---|
213 | uint8_t bValue = 0;
|
---|
214 | DWORD cbRet = g_pfnGetFirmwareEnvironmentVariableW(pwszName, VBOX_UEFI_UUID_GLOBALS, &bValue, sizeof(bValue));
|
---|
215 | pValue->u.fVal = cbRet != 0 && bValue != 0;
|
---|
216 | rc = cbRet != 0 || GetLastError() == ERROR_INVALID_FUNCTION ? VINF_SUCCESS : RTErrConvertFromWin32(GetLastError());
|
---|
217 | break;
|
---|
218 | }
|
---|
219 |
|
---|
220 | default:
|
---|
221 | AssertFailedReturn(VERR_INTERNAL_ERROR);
|
---|
222 | }
|
---|
223 |
|
---|
224 | return rc;
|
---|
225 | }
|
---|
226 |
|
---|