-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_dmi.c
249 lines (217 loc) · 5.75 KB
/
parse_dmi.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
/*
* Copyright (c) 2014, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "inc/libdmi.h"
#include "parse_dmi.h"
#include "dmi_intel.h"
#define DMISYSFS_PATH "/sys/firmware/dmi/entries/%d-%d/raw"
/*
* Read sysfs file and return size of the file.
*
* If buffer is NULL, then just read using a temporary buffer, else
* copy the file content in buffer.
*
* The caller must allocate the buffer with enough space
* to contain the whole file (read_sysfs should be called one first
* time to compute the file size)
*/
static int read_sysfs(const char *path, unsigned char *buffer)
{
int ret;
int fd;
fd = open(path, O_RDONLY);
if (fd < 0) {
error("Failed to open %s : %s\n", path, strerror(errno));
return fd;
}
int size = 0;
unsigned char tmp[BUFSIZ];
unsigned char *buf = tmp;
if (buffer)
buf = buffer;
do {
ret = read(fd, buf, BUFSIZ);
if (ret == -1)
break;
size += ret;
if (buffer)
buf += ret;
} while (ret > 0);
close(fd);
if (ret < 0) {
error("Failed to read file %s : %s\n", path, strerror(errno));
size = ret;
}
return size;
}
static struct dmi_header *dmisysfs_gettype(enum smbios_type type)
{
char *path;
int ret;
int size;
struct dmi_header *dmi = NULL;
ret = asprintf(&path, DMISYSFS_PATH, type, 0);
if (ret == -1) {
error("Memory allocation failure\n");
goto out;
}
size = read_sysfs(path, NULL);
if (size <= 0)
goto free_path;
dmi = malloc(size);
if (!dmi) {
error("Memory allocation failure");
goto free_path;
}
read_sysfs(path, (unsigned char *)dmi);
free_path:
free(path);
out:
return dmi;
}
struct vendor_parser {
char *vendor;
char *(*parser)(struct dmi_header *, char *);
};
static const struct vendor_parser vendor_parsers[] = {
{"Intel Corp.", intel_dmi_parser},
{"Intel Corporation", intel_dmi_parser},
{"INSYDE Corp.", intel_dmi_parser},
};
static char *dmi_get_vendor(void);
/* Index begins at 1 */
static char *dmi_string(struct dmi_header *dmi, char index)
{
if (index <= 0) {
error("Invalid string index %d\n", index);
return NULL;
}
int i;
char *string = (char *)dmi + dmi->length;
for (i = 1; i < index; i++)
string += strlen(string) + 1;
return string;
}
static char *oem_dmi_getfield(struct dmi_header *dmi, char *field)
{
unsigned int i;
char *vendor = dmi_get_vendor();
if (!vendor) {
error("Failed to retrieve vendor information to decode OEM specific table\n");
return NULL;
}
for (i = 0; i < sizeof(vendor_parsers) / sizeof(*vendor_parsers); i++)
if (!strcmp(vendor, vendor_parsers[i].vendor)) {
free(vendor);
return vendor_parsers[i].parser(dmi, field);
}
error("Unsupported vendor '%s'\n", vendor);
free(vendor);
return NULL;
}
static char *dmi_getfield(struct dmi_header *dmi, char *field)
{
if (dmi->type >= OEM_SPECIFIC)
return oem_dmi_getfield(dmi, field);
switch (dmi->type) {
case BIOS_INFORMATION:
PARSE_FIELD(bios_information, dmi, field);
break;
case SYSTEM_INFORMATION:
PARSE_FIELD(system_information, dmi, field);
break;
default:
error("Table 0x%x not supported\n", dmi->type);
return NULL;
}
}
char *libdmi_getfield(enum smbios_type type, char *field)
{
struct dmi_header *dmi = dmisysfs_gettype(type);
if (!dmi)
return NULL;
char *value = dmi_getfield(dmi, field);
free(dmi);
return value;
}
static char *dmi_get_vendor(void)
{
return libdmi_getfield(0, "bios_vendor");
}
char *dmi_get_product_name(void)
{
return libdmi_getfield(SYSTEM_INFORMATION, "product_name");
}
/*
* Return stringified value of data.
*
* SMBIOS doesn't give information on the type of the data so we
* encode it using type_size.
*
* type_size 0 is special and means data is a string.
*/
char *parse_dmi_field(struct dmi_header *dmi, unsigned char *data, int type_size)
{
char *value = NULL;
int ret;
if (type_size == 0) {
char *tmp;
tmp = dmi_string(dmi, *data);
if (!tmp)
goto out;
ret = asprintf(&value, "%s", tmp);
if (ret == -1) {
error("Memory allocation failure\n");
value = NULL;
}
goto out;
}
__int64_t int_val = 0;
memcpy(&int_val, data, type_size);
ret = asprintf(&value, "%lld", int_val);
if (ret == -1) {
error("Memory allocation failure\n");
value = NULL;
}
out:
return value;
}
struct field_desc *get_field_desc(struct field_desc *list, int list_size, char *field)
{
int i;
for (i = 0; i < list_size ; i++)
if (!strcmp(list[i].field, field))
return &list[i];
return NULL;
}