ReactOS 0.4.15-dev-7788-g1ad9096
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
10static UINT (WINAPI * pGetSystemFirmwareTable)(DWORD, DWORD, PVOID, DWORD);
12
13static
14VOID
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 */
32{
34 HKEY hKey;
35 DWORD dwType, dwCheck, dwBytes = 0;
36
37 if (!bInitAPI)
38 {
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",
68 {
69 return NULL;
70 }
71
73 L"SMBiosData",
74 NULL,
75 &dwType,
76 NULL,
77 &dwBytes) != ERROR_SUCCESS || dwType != REG_BINARY)
78 {
80 return NULL;
81 }
82
83 pBuffer = HeapAlloc(GetProcessHeap(), 0, dwBytes);
84 if (!pBuffer)
85 {
87 return NULL;
88 }
89
91 L"SMBiosData",
92 NULL,
93 &dwType,
94 pBuffer,
95 &dwBytes) != ERROR_SUCCESS || dwType != REG_BINARY)
96 {
99 return NULL;
100 }
101
103 }
105 return pBuffer;
106}
107
108/* Trim converted DMI string */
109VOID
111 _Inout_ PWSTR pStr)
112{
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 */
144SIZE_T
146 _In_ PCSTR DmiString,
147 _Out_ PWSTR pBuf,
148 _In_ DWORD cchBuf,
149 _In_ BOOL bTrim)
150{
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 */
176VOID
179{
180 if (!Buffer)
181 return;
182
184}
#define RegCloseKey(hKey)
Definition: registry.h:49
Definition: bufpool.h:45
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
LONG WINAPI RegOpenKeyW(HKEY hKey, LPCWSTR lpSubKey, PHKEY phkResult)
Definition: reg.c:3297
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4132
static const WCHAR Strings[]
Definition: reg.c:35
#define GetProcessHeap()
Definition: compat.h:736
#define GetProcAddress(x, y)
Definition: compat.h:753
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define MultiByteToWideChar
Definition: compat.h:110
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
VOID ParseSMBiosTables(_In_reads_bytes_(TableSize) PVOID SMBiosTables, _In_ ULONG TableSize, _Inout_updates_(ID_STRINGS_MAX) PCHAR *Strings)
Definition: dmilib.c:41
@ ID_STRINGS_MAX
Definition: dmilib.h:28
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
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
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
static SCRIPT_CACHE SCRIPT_ANALYSIS OPENTYPE_TAG OPENTYPE_TAG int TEXTRANGE_PROPERTIES int const WCHAR int cChars
Definition: usp10.c:64
#define min(a, b)
Definition: monoChain.cc:55
#define _Inout_
Definition: ms_sal.h:378
#define _Inout_updates_(size)
Definition: ms_sal.h:387
#define _Out_
Definition: ms_sal.h:345
#define _In_
Definition: ms_sal.h:308
unsigned int UINT
Definition: ndis.h:50
#define REG_BINARY
Definition: nt_native.h:1496
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define L(x)
Definition: ntvdm.h:50
PVOID pBuffer
uint16_t * PWSTR
Definition: typedefs.h:56
ULONG_PTR SIZE_T
Definition: typedefs.h:80
const char * PCSTR
Definition: typedefs.h:52
char * PCHAR
Definition: typedefs.h:51
SIZE_T GetSMBiosStringW(_In_ PCSTR DmiString, _Out_ PWSTR pBuf, _In_ DWORD cchBuf, _In_ BOOL bTrim)
Definition: udmihelp.c:145
static DWORD
Definition: udmihelp.c:10
PVOID LoadSMBiosData(_Inout_updates_(ID_STRINGS_MAX) PCHAR *Strings)
Definition: udmihelp.c:30
static PVOID
Definition: udmihelp.c:10
VOID FreeSMBiosData(_In_ PVOID Buffer)
Definition: udmihelp.c:177
static VOID InitializeAPI()
Definition: udmihelp.c:15
VOID TrimDmiStringW(_Inout_ PWSTR pStr)
Definition: udmihelp.c:110
static BOOL bInitAPI
Definition: udmihelp.c:11
#define WINAPI
Definition: msvc.h:6
#define CP_OEMCP
Definition: winnls.h:231
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
__wchar_t WCHAR
Definition: xmlstorage.h:180