ReactOS  0.4.13-dev-455-g28ed234
udmihelp.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS User-mode DMI/SMBIOS Helper Functions
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: SMBIOS table parsing functions
5  * COPYRIGHT: Copyright 2018 Stanislav Motylkov
6  */
7 
8 #include "precomp.h"
9 
10 static UINT (WINAPI * pGetSystemFirmwareTable)(DWORD, DWORD, PVOID, DWORD);
11 static BOOL bInitAPI = FALSE;
12 
13 static
14 VOID
16 {
17  HANDLE hKernel;
18 
19  pGetSystemFirmwareTable = NULL;
20 
21  hKernel = GetModuleHandleW(L"kernel32.dll");
22  if (!hKernel)
23  return;
24 
25  pGetSystemFirmwareTable = (void *)GetProcAddress(hKernel, "GetSystemFirmwareTable");
26 }
27 
28 /* Load SMBIOS Data */
29 PVOID
32 {
33  PVOID pBuffer = NULL;
34  HKEY hKey;
35  DWORD dwType, dwCheck, dwBytes = 0;
36 
37  if (!bInitAPI)
38  {
39  InitializeAPI();
40  bInitAPI = TRUE;
41  }
42 
43  /* Try using GetSystemFirmwareTable (works on NT 5.2 and higher) */
44  if (pGetSystemFirmwareTable)
45  {
46  dwBytes = pGetSystemFirmwareTable('RSMB', 0, NULL, 0);
47  if (dwBytes > 0)
48  {
49  pBuffer = HeapAlloc(GetProcessHeap(), 0, dwBytes);
50  if (!pBuffer)
51  {
52  return NULL;
53  }
54  dwCheck = pGetSystemFirmwareTable('RSMB', 0, pBuffer, dwBytes);
55  if (dwCheck != dwBytes)
56  {
58  return NULL;
59  }
60  }
61  }
62  if (dwBytes == 0)
63  {
64  /* Try using registry (works on NT 5.1) */
66  L"SYSTEM\\CurrentControlSet\\Services\\mssmbios\\Data",
67  &hKey) != ERROR_SUCCESS)
68  {
69  return NULL;
70  }
71 
72  if (RegQueryValueExW(hKey,
73  L"SMBiosData",
74  NULL,
75  &dwType,
76  NULL,
77  &dwBytes) != ERROR_SUCCESS || dwType != REG_BINARY)
78  {
79  RegCloseKey(hKey);
80  return NULL;
81  }
82 
83  pBuffer = HeapAlloc(GetProcessHeap(), 0, dwBytes);
84  if (!pBuffer)
85  {
86  RegCloseKey(hKey);
87  return NULL;
88  }
89 
90  if (RegQueryValueExW(hKey,
91  L"SMBiosData",
92  NULL,
93  &dwType,
94  pBuffer,
95  &dwBytes) != ERROR_SUCCESS || dwType != REG_BINARY)
96  {
98  RegCloseKey(hKey);
99  return NULL;
100  }
101 
102  RegCloseKey(hKey);
103  }
104  ParseSMBiosTables(pBuffer, dwBytes, Strings);
105  return pBuffer;
106 }
107 
108 /* Trim converted DMI string */
109 VOID
111  _Inout_ PWSTR pStr)
112 {
113  SIZE_T Length;
114  UINT i = 0;
115 
116  if (!pStr)
117  return;
118 
119  Length = wcslen(pStr);
120  if (Length == 0)
121  return;
122 
123  /* Trim leading spaces */
124  while (i < Length && pStr[i] <= L' ')
125  {
126  i++;
127  }
128 
129  if (i > 0)
130  {
131  Length -= i;
132  memmove(pStr, pStr + i, (Length + 1) * sizeof(WCHAR));
133  }
134 
135  /* Trim trailing spaces */
136  while (Length && pStr[Length-1] <= L' ')
137  {
138  pStr[Length-1] = L'\0';
139  --Length;
140  }
141 }
142 
143 /* Convert string from SMBIOS */
144 SIZE_T
146  _In_ PCSTR DmiString,
147  _Out_ PWSTR pBuf,
148  _In_ DWORD cchBuf,
149  _In_ BOOL bTrim)
150 {
151  SIZE_T cChars;
152 
153  if (!DmiString)
154  {
155  if (cchBuf >= 1)
156  {
157  *pBuf = 0;
158  }
159  return 0;
160  }
161 
162  cChars = MultiByteToWideChar(CP_OEMCP, 0, DmiString, -1, pBuf, cchBuf);
163 
164  /* NULL-terminate string */
165  pBuf[min(cchBuf-1, cChars)] = L'\0';
166 
167  if (bTrim)
168  {
169  TrimDmiStringW(pBuf);
170  }
171 
172  return wcslen(pBuf);
173 }
174 
175 /* Free SMBIOS Data */
176 VOID
178  _In_ PVOID Buffer)
179 {
180  if (!Buffer)
181  return;
182 
184 }
signed char * PCHAR
Definition: retypes.h:7
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
#define TRUE
Definition: types.h:120
#define ERROR_SUCCESS
Definition: deptool.c:10
#define REG_BINARY
Definition: nt_native.h:1496
VOID ParseSMBiosTables(_In_reads_bytes_(TableSize) PVOID SMBiosTables, _In_ ULONG TableSize, _Inout_updates_(ID_STRINGS_MAX) PCHAR *Strings)
Definition: dmilib.c:41
uint16_t * PWSTR
Definition: typedefs.h:54
SIZE_T GetSMBiosStringW(_In_ PCSTR DmiString, _Out_ PWSTR pBuf, _In_ DWORD cchBuf, _In_ BOOL bTrim)
Definition: udmihelp.c:145
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned int BOOL
Definition: ntddk_ex.h:94
LONG WINAPI RegOpenKeyW(HKEY hKey, LPCWSTR lpSubKey, PHKEY phkResult)
Definition: reg.c:3296
smooth NULL
Definition: ftsmooth.c:416
PVOID pBuffer
#define _Out_
Definition: no_sal2.h:323
Definition: bufpool.h:45
static SCRIPT_CACHE SCRIPT_ANALYSIS OPENTYPE_TAG OPENTYPE_TAG int TEXTRANGE_PROPERTIES int const WCHAR int cChars
Definition: usp10.c:64
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4116
static PVOID
Definition: udmihelp.c:10
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
#define _Inout_
Definition: no_sal2.h:244
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
static const WCHAR Strings[]
Definition: reg.c:35
static DWORD
Definition: udmihelp.c:10
static const WCHAR L[]
Definition: oid.c:1250
#define _In_
Definition: no_sal2.h:204
ULONG_PTR SIZE_T
Definition: typedefs.h:78
VOID FreeSMBiosData(_In_ PVOID Buffer)
Definition: udmihelp.c:177
VOID TrimDmiStringW(_Inout_ PWSTR pStr)
Definition: udmihelp.c:110
#define min(a, b)
Definition: monoChain.cc:55
unsigned int UINT
Definition: ndis.h:50
#define MultiByteToWideChar
Definition: compat.h:100
_In_ ULONG _In_ ULONG _Inout_updates_(8 *8) ULONG *pul)
Definition: winddi.h:3522
static UINT(WINAPI *pGetSystemFirmwareTable)(DWORD
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:845
PVOID LoadSMBiosData(_Inout_updates_(ID_STRINGS_MAX) PCHAR *Strings)
Definition: udmihelp.c:30
#define GetProcAddress(x, y)
Definition: compat.h:410
static BOOL bInitAPI
Definition: udmihelp.c:11
const char * PCSTR
Definition: typedefs.h:51
#define CP_OEMCP
Definition: winnls.h:228
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:394
static VOID InitializeAPI()
Definition: udmihelp.c:15
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12