generated from KNSoft/MSVC-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmbiosDecode.c
300 lines (275 loc) · 9.01 KB
/
SmbiosDecode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "SMBIOS.h"
#include "SMBIOS.TypeInfo.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <locale.h>
bool
GetSmbiosTableData(
PSMBIOS_RAW_DATA* Data,
unsigned int* DataSize);
#if defined(_WINDOWS)
#include <Windows.h>
bool
GetSmbiosTableData(
PSMBIOS_RAW_DATA* Data,
unsigned int* DataSize)
{
UINT BufferSize, BytesWritten;
PVOID Buffer;
DWORD Error;
BufferSize = GetSystemFirmwareTable('RSMB', 0, NULL, 0);
if (BufferSize == 0)
{
printf("GetSystemFirmwareTable failed with: 0x%08lX\n", GetLastError());
return false;
}
Buffer = malloc(BufferSize);
if (Buffer == NULL)
{
printf("malloc failed to allocate %lu bytes buffer\n", BufferSize);
return false;
}
BytesWritten = GetSystemFirmwareTable('RSMB', 0, Buffer, BufferSize);
if (BytesWritten == 0)
{
Error = GetLastError();
} else if (BytesWritten != BufferSize)
{
Error = ERROR_INVALID_DATA;
} else
{
*Data = Buffer;
*DataSize = BufferSize;
return true;
}
printf("GetSystemFirmwareTable failed with: 0x%08lX\n", Error);
return false;
}
#elif defined(_LINUX)
#error Not implemented
#else
#error No OS target specified, currently supports Windows (_WINDOWS) and Linux (_LINUX)
#endif
#define AddPtr(P, I) ((void*)((unsigned char*)(P) + (I))) // aka Add2Ptr
#define SubPtr(B,O) ((unsigned int)((size_t)(O) - (size_t)(B))) // aka PtrOffset
#ifndef NULL
#define NULL ((void *)0)
#endif
static const char* g_Strings[UCHAR_MAX];
static
PSMBIOS_TABLE
PrintSmbiosTable(
PSMBIOS_TABLE Table,
void* StartOfData,
void* EndOfData)
{
PSMBIOS_TABLE NextTable;
void* EndOfTable;
WORD i;
const char *psz;
BYTE StringCount;
size_t MaxSize, Length;
PSMBIOS_TYPE_INFO TypeInfo;
QWORD BitFieldValue, Value;
WORD BitFieldSize = 0, ValueSize;
/* Get type information */
TypeInfo = NULL;
for (i = 0; i < _countof(SmbiosTypeInfo); i++)
{
if (SmbiosTypeInfo[i].Type == Table->Header.Type)
{
TypeInfo = &SmbiosTypeInfo[i];
break;
}
}
/* Build string index array and locate next table address */
psz = AddPtr(Table, Table->Header.Length);
StringCount = 0;
NextTable = NULL;
while (true)
{
if (psz + 2 >= (const char*)EndOfData)
{
break;
}
if (psz[0] == '\0' && psz[1] == '\0')
{
NextTable = (PSMBIOS_TABLE)(psz + 2);
break;
}
MaxSize = SubPtr(psz, EndOfData);
Length = strnlen(psz, MaxSize);
if (Length == MaxSize)
{
break;
}
g_Strings[StringCount++] = psz;
psz += Length + 1;
if (psz[0] == '\0')
{
NextTable = (PSMBIOS_TABLE)(psz + 1);
break;
}
}
if ((void*)NextTable >= EndOfData)
{
NextTable = NULL;
}
EndOfTable = NextTable == NULL ? EndOfData : NextTable;
/* Print table header */
if (TypeInfo != NULL)
{
printf("[Type %hhu: %hs]\n", Table->Header.Type, TypeInfo->Name);
} else
{
printf("[Type %hhu (Unrecognized)]\n", Table->Header.Type);
}
printf("Handle: 0x%04hX, Offset: 0x%08lX, Length: 0x%02hhX bytes, Total: 0x%lX bytes\n",
Table->Header.Handle,
SubPtr(StartOfData, Table),
Table->Header.Length,
SubPtr(Table, EndOfTable));
/* Print table fields */
if (TypeInfo == NULL)
{
goto _Exit;
}
for (i = 0; i < TypeInfo->FieldCount; i++)
{
Value = 0;
if (TypeInfo->Fields[i].IsBitField)
{
if (BitFieldSize == 0)
{
if (i == 0 ||
TypeInfo->Fields[i - 1].IsBitField ||
TypeInfo->Fields[i - 1].Type != SmbiosDataTypeUInt)
{
break;
}
BitFieldSize = TypeInfo->Fields[i - 1].Size;
BitFieldValue = 0;
memcpy(&BitFieldValue, AddPtr(Table, TypeInfo->Fields[i - 1].Offset), TypeInfo->Fields[i - 1].Size);
}
putchar('\t');
if (TypeInfo->Fields[i].Type == SmbiosDataTypeBit)
{
printf("%02hhu [%hc] %hs",
TypeInfo->Fields[i].Offset,
_bittest64(&BitFieldValue, TypeInfo->Fields[i].Offset) ? 'x' : ' ',
TypeInfo->Fields[i].Name);
} else if (TypeInfo->Fields[i].Type == SmbiosDataTypeUInt || TypeInfo->Fields[i].Type == SmbiosDataTypeEnum)
{
printf("%02hhu:%02hhu %hs: ",
TypeInfo->Fields[i].Offset,
TypeInfo->Fields[i].Offset + TypeInfo->Fields[i].Size - 1,
TypeInfo->Fields[i].Name);
Value = BitFieldValue;
Value >>= TypeInfo->Fields[i].Offset;
Value &= ((QWORD)1 << TypeInfo->Fields[i].Size) - 1;
}
} else
{
if (TypeInfo->Fields[i].Offset + TypeInfo->Fields[i].Size > Table->Header.Length)
{
break;
}
BitFieldSize = 0;
printf("0x%04hX %hs: ", TypeInfo->Fields[i].Offset, TypeInfo->Fields[i].Name);
}
if (TypeInfo->Fields[i].Type == SmbiosDataTypeString && TypeInfo->Fields[i].Size == sizeof(BYTE))
{
BYTE Index = *(BYTE*)AddPtr(Table, TypeInfo->Fields[i].Offset);
if (Index != 0 && Index <= StringCount)
{
printf("0x%02hhX \"%hs\"", Index, g_Strings[Index - 1]);
}
} else if (TypeInfo->Fields[i].Type == SmbiosDataTypeUInt || TypeInfo->Fields[i].Type == SmbiosDataTypeEnum)
{
if (TypeInfo->Fields[i].IsBitField)
{
ValueSize = BitFieldSize;
} else
{
Value = 0;
memcpy(&Value, AddPtr(Table, TypeInfo->Fields[i].Offset), TypeInfo->Fields[i].Size);
ValueSize = TypeInfo->Fields[i].Size;
}
if (ValueSize == sizeof(BYTE))
{
printf("0x%02hhX", (BYTE)Value);
} else if (ValueSize == sizeof(WORD))
{
printf("0x%04hX", (WORD)Value);
} else if (ValueSize == sizeof(DWORD))
{
printf("0x%08lX", (DWORD)Value);
} else if (ValueSize == sizeof(QWORD))
{
printf("0x%016llX", Value);
}
if (TypeInfo->Fields[i].Type == SmbiosDataTypeEnum)
{
WORD j;
for (j = 0; j < TypeInfo->Fields[i].AdditionalInfo.Enum.Count; j++)
{
if (TypeInfo->Fields[i].AdditionalInfo.Enum.Values[j].Value == Value)
{
printf(" (%hs)", TypeInfo->Fields[i].AdditionalInfo.Enum.Values[j].Name);
break;
}
}
}
} else if (TypeInfo->Fields[i].Type == SmbiosDataTypeOther)
{
BYTE j, *p;
p = AddPtr(Table, TypeInfo->Fields[i].Offset);
for (j = 0; j < TypeInfo->Fields[i].Size; j++)
{
printf("%02X", p[j]);
if (j != TypeInfo->Fields[i].Size - 1)
{
putchar(' ');
}
}
} else if (TypeInfo->Fields[i].Type == SmbiosDataTypeUuid && TypeInfo->Fields[i].Size == 16)
{
PBYTE Uuid = (PBYTE)AddPtr(Table, TypeInfo->Fields[i].Offset);
printf("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
Uuid[0], Uuid[1], Uuid[2], Uuid[3], Uuid[4], Uuid[5], Uuid[6], Uuid[7],
Uuid[8], Uuid[9], Uuid[10], Uuid[11], Uuid[12], Uuid[13], Uuid[14], Uuid[15]);
}
putchar('\n');
}
_Exit:
putchar('\n');
return NextTable;
}
int
main()
{
PSMBIOS_RAW_DATA Data;
unsigned int DataSize;
PSMBIOS_TABLE Table;
#ifdef _WINDOWS
SetConsoleOutputCP(CP_UTF8);
#endif
setlocale(LC_ALL, ".UTF-8");
if (!GetSmbiosTableData(&Data, &DataSize))
{
puts("Get SMBIOS table failed\n");
return ENODATA;
}
printf("SMBIOS Version: %hhu.%hhu\n", Data->SMBIOSMajorVersion, Data->SMBIOSMinorVersion);
printf("DMI Revision: %hhu\n", Data->DmiRevision);
printf("Data Size: %lu bytes\n\n", Data->Length);
Table = (PSMBIOS_TABLE)Data->SMBIOSTableData;
do
{
Table = PrintSmbiosTable(Table, Data->SMBIOSTableData, AddPtr(Data->SMBIOSTableData, Data->Length));
} while (Table != NULL);
free(Data);
return 0;
}