flashrom 

flashrom Svn Source Tree

Root/trunk/dmi.c

1/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2000-2002 Alan Cox <alan@redhat.com>
5 * Copyright (C) 2002-2010 Jean Delvare <khali@linux-fr.org>
6 * Copyright (C) 2009,2010 Michael Karcher
7 * Copyright (C) 2011-2013 Stefan Tauner
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24/* strnlen is in POSIX but was a GNU extension up to glibc 2.10 */
25#if (__GLIBC__ == 2 && __GLIBC_MINOR__ < 10) || __GLIBC__ < 2
26#define _GNU_SOURCE
27#else
28#define _POSIX_C_SOURCE 200809L
29#endif
30
31#include <strings.h>
32#include <string.h>
33#include <ctype.h>
34#include <stdio.h>
35#include <stdlib.h>
36
37#include "flash.h"
38#include "programmer.h"
39
40#if defined(__i386__) || defined(__x86_64__)
41
42/* Enable SMBIOS decoding. Currently legacy DMI decoding is enough. */
43#define SM_SUPPORT 0
44
45/* Strings longer than 4096 in DMI are just insane. */
46#define DMI_MAX_ANSWER_LEN 4096
47
48int has_dmi_support = 0;
49
50static struct {
51const char *const keyword;
52const uint8_t type;
53const uint8_t offset;
54char *value;
55} dmi_strings[] = {
56{ "system-manufacturer", 1, 0x04, NULL },
57{ "system-product-name", 1, 0x05, NULL },
58{ "system-version", 1, 0x06, NULL },
59{ "baseboard-manufacturer", 2, 0x04, NULL },
60{ "baseboard-product-name", 2, 0x05, NULL },
61{ "baseboard-version", 2, 0x06, NULL },
62};
63
64/* This list is used to identify supposed laptops. The is_laptop field has the
65 * following meaning:
66 * - 0: in all likelihood not a laptop
67 * - 1: in all likelihood a laptop
68 * - 2: chassis-type is not specific enough
69 * A full list of chassis types can be found in the System Management BIOS
70 * (SMBIOS) Reference Specification 2.7.0 section 7.4.1 "Chassis Types" at
71 * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf
72 * The types below are the most common ones.
73 */
74static const struct {
75uint8_t type;
76uint8_t is_laptop;
77char *name;
78} dmi_chassis_types[] = {
79{0x01, 2, "Other"},
80{0x02, 2, "Unknown"},
81{0x03, 0, "Desktop"},
82{0x04, 0, "Low Profile Desktop"},
83{0x06, 0, "Mini Tower"},
84{0x07, 0, "Tower"},
85{0x08, 1, "Portable"},
86{0x09, 1, "Laptop"},
87{0x0a, 1, "Notebook"},
88{0x0b, 1, "Hand Held"},
89{0x0e, 1, "Sub Notebook"},
90{0x11, 0, "Main Server Chassis"},
91{0x17, 0, "Rack Mount Chassis"},
92{0x18, 0, "Sealed-case PC"}, /* used by Supermicro (X8SIE) */
93{0x19, 0, "Multi-system"}, /* used by Supermicro (X7DWT) */
94};
95
96#if CONFIG_INTERNAL_DMI == 1
97static bool dmi_checksum(const uint8_t * const buf, size_t len)
98{
99uint8_t sum = 0;
100size_t a;
101
102for (a = 0; a < len; a++)
103sum += buf[a];
104return (sum == 0);
105}
106
107/** Retrieve a DMI string.
108 *
109 * See SMBIOS spec. section 6.1.3 "Text strings".
110 * The table will be unmapped ASAP, hence return a duplicated & sanitized string that needs to be freed later.
111 *
112 * \param bufthe buffer to search through (usually appended directly to a DMI structure)
113 * \param string_idindex of the string to look for
114 * \param limitpointer to the first byte beyond \em buf
115 */
116static char *dmi_string(const char *buf, uint8_t string_id, const char *limit)
117{
118size_t i, len;
119
120if (string_id == 0)
121return strdup("Not Specified");
122
123while (string_id > 1 && string_id--) {
124if (buf >= limit) {
125msg_perr("DMI table is broken (string portion out of bounds)!\n");
126return strdup("<OUT OF BOUNDS>");
127}
128buf += strnlen(buf, limit - buf) + 1;
129}
130
131if (!*buf) /* as long as the current byte we're on isn't null */
132return strdup("<BAD INDEX>");
133
134len = strnlen(buf, limit - buf);
135char *newbuf = malloc(len + 1);
136if (newbuf == NULL) {
137msg_perr("Out of memory!\n");
138return NULL;
139}
140
141/* fix junk bytes in the string */
142for (i = 0; i < len && buf[i] != '\0'; i++) {
143if (isprint((unsigned char)buf[i]))
144newbuf[i] = buf[i];
145else
146newbuf[i] = ' ';
147}
148newbuf[i] = '\0';
149
150return newbuf;
151}
152
153static void dmi_chassis_type(uint8_t code)
154{
155int i;
156code &= 0x7f; /* bits 6:0 are chassis type, 7th bit is the lock bit */
157is_laptop = 2;
158for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
159if (code == dmi_chassis_types[i].type) {
160msg_pdbg("DMI string chassis-type: \"%s\"\n", dmi_chassis_types[i].name);
161is_laptop = dmi_chassis_types[i].is_laptop;
162break;
163}
164}
165}
166
167static void dmi_table(uint32_t base, uint16_t len, uint16_t num)
168{
169int i = 0, j = 0;
170
171uint8_t *dmi_table_mem = physmap_ro("DMI Table", base, len);
172if (dmi_table_mem == NULL) {
173msg_perr("Unable to access DMI Table\n");
174return;
175}
176
177uint8_t *data = dmi_table_mem;
178uint8_t *limit = dmi_table_mem + len;
179
180/* SMBIOS structure header is always 4 B long and contains:
181 * - uint8_t type;// see dmi_chassis_types's type
182 * - uint8_t length;// data section w/ header w/o strings
183 * - uint16_t handle;
184 */
185while (i < num && data + 4 < limit) {
186/* - If a short entry is found (less than 4 bytes), not only it
187 * is invalid, but we cannot reliably locate the next entry.
188 * - If the length value indicates that this structure spreads
189 * across the table border, something is fishy too.
190 * Better stop at this point, and let the user know his/her
191 * table is broken.
192 */
193if (data[1] < 4 || data + data[1] >= limit) {
194msg_perr("DMI table is broken (bogus header)!\n");
195break;
196}
197
198if(data[0] == 3) {
199if (data + 5 < limit)
200dmi_chassis_type(data[5]);
201else /* the table is broken, but laptop detection is optional, hence continue. */
202msg_pwarn("DMI table is broken (chassis_type out of bounds)!\n");
203} else
204for (j = 0; j < ARRAY_SIZE(dmi_strings); j++) {
205uint8_t offset = dmi_strings[j].offset;
206uint8_t type = dmi_strings[j].type;
207
208if (data[0] != type)
209continue;
210
211if (data[1] <= offset || data + offset >= limit) {
212msg_perr("DMI table is broken (offset out of bounds)!\n");
213goto out;
214}
215
216dmi_strings[j].value = dmi_string((const char *)(data + data[1]), data[offset],
217 (const char *)limit);
218}
219/* Find next structure by skipping data and string sections */
220data += data[1];
221while (data + 1 <= limit) {
222if (data[0] == 0 && data[1] == 0)
223break;
224data++;
225}
226data += 2;
227i++;
228}
229out:
230physunmap(dmi_table_mem, len);
231}
232
233#if SM_SUPPORT
234static int smbios_decode(uint8_t *buf)
235{
236/* TODO: other checks mentioned in the conformance guidelines? */
237if (!dmi_checksum(buf, buf[0x05]) ||
238 (memcmp(buf + 0x10, "_DMI_", 5) != 0) ||
239 !dmi_checksum(buf + 0x10, 0x0F))
240return 0;
241
242dmi_table(mmio_readl(buf + 0x18), mmio_readw(buf + 0x16), mmio_readw(buf + 0x1C));
243
244return 1;
245}
246#endif
247
248static int legacy_decode(uint8_t *buf)
249{
250if (!dmi_checksum(buf, 0x0F))
251return 1;
252
253dmi_table(mmio_readl(buf + 0x08), mmio_readw(buf + 0x06), mmio_readw(buf + 0x0C));
254
255return 0;
256}
257
258int dmi_fill(void)
259{
260size_t fp;
261uint8_t *dmi_mem;
262int ret = 1;
263
264msg_pdbg("Using Internal DMI decoder.\n");
265/* There are two ways specified to gain access to the SMBIOS table:
266 * - EFI's configuration table contains a pointer to the SMBIOS table. On linux it can be obtained from
267 * sysfs. EFI's SMBIOS GUID is: {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,0x0,0x90,0x27,0x3f,0xc1,0x4d}
268 * - Scanning physical memory address range 0x000F0000h to 0x000FFFFF for the anchor-string(s). */
269dmi_mem = physmap_ro("DMI", 0xF0000, 0x10000);
270if (dmi_mem == ERROR_PTR)
271return ret;
272
273for (fp = 0; fp <= 0xFFF0; fp += 16) {
274#if SM_SUPPORT
275if (memcmp(dmi_mem + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
276if (smbios_decode(dmi_mem + fp)) // FIXME: length check
277goto out;
278} else
279#endif
280if (memcmp(dmi_mem + fp, "_DMI_", 5) == 0)
281if (legacy_decode(dmi_mem + fp) == 0) {
282ret = 0;
283goto out;
284}
285}
286msg_pinfo("No DMI table found.\n");
287out:
288physunmap(dmi_mem, 0x10000);
289return ret;
290}
291
292#else /* CONFIG_INTERNAL_DMI */
293
294#define DMI_COMMAND_LEN_MAX 300
295static const char *dmidecode_command = "dmidecode";
296
297static char *get_dmi_string(const char *string_name)
298{
299FILE *dmidecode_pipe;
300char *result;
301char answerbuf[DMI_MAX_ANSWER_LEN];
302char commandline[DMI_COMMAND_LEN_MAX];
303
304snprintf(commandline, sizeof(commandline),
305 "%s -s %s", dmidecode_command, string_name);
306dmidecode_pipe = popen(commandline, "r");
307if (!dmidecode_pipe) {
308msg_perr("Opening DMI pipe failed!\n");
309return NULL;
310}
311
312/* Kill lines starting with '#', as recent dmidecode versions
313 * have the quirk to emit a "# SMBIOS implementations newer..."
314 * message even on "-s" if the SMBIOS declares a
315 * newer-than-supported version number, while it *should* only print
316 * the requested string.
317 */
318do {
319if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
320if (ferror(dmidecode_pipe)) {
321msg_perr("DMI pipe read error\n");
322pclose(dmidecode_pipe);
323return NULL;
324} else {
325answerbuf[0] = 0;/* Hit EOF */
326}
327}
328} while (answerbuf[0] == '#');
329
330/* Discard all output exceeding DMI_MAX_ANSWER_LEN to prevent deadlock on pclose. */
331while (!feof(dmidecode_pipe))
332getc(dmidecode_pipe);
333if (pclose(dmidecode_pipe) != 0) {
334msg_pwarn("dmidecode execution unsuccessful - continuing without DMI info\n");
335return NULL;
336}
337
338/* Chomp trailing newline. */
339if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n')
340answerbuf[strlen(answerbuf) - 1] = 0;
341
342result = strdup(answerbuf);
343if (result == NULL)
344msg_pwarn("Warning: Out of memory - DMI support fails");
345
346return result;
347}
348
349int dmi_fill(void)
350{
351int i;
352char *chassis_type;
353
354msg_pdbg("Using External DMI decoder.\n");
355for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
356dmi_strings[i].value = get_dmi_string(dmi_strings[i].keyword);
357if (dmi_strings[i].value == NULL)
358return 1;
359}
360
361chassis_type = get_dmi_string("chassis-type");
362if (chassis_type == NULL)
363return 0; /* chassis-type handling is optional anyway */
364
365msg_pdbg("DMI string chassis-type: \"%s\"\n", chassis_type);
366is_laptop = 2;
367for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
368if (strcasecmp(chassis_type, dmi_chassis_types[i].name) == 0) {
369is_laptop = dmi_chassis_types[i].is_laptop;
370break;
371}
372}
373free(chassis_type);
374return 0;
375}
376
377#endif /* CONFIG_INTERNAL_DMI */
378
379static int dmi_shutdown(void *data)
380{
381int i;
382for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
383free(dmi_strings[i].value);
384dmi_strings[i].value = NULL;
385}
386return 0;
387}
388
389void dmi_init(void)
390{
391/* Register shutdown function before we allocate anything. */
392if (register_shutdown(dmi_shutdown, NULL)) {
393msg_pwarn("Warning: Could not register DMI shutdown function - continuing without DMI info.\n");
394return;
395}
396
397/* dmi_fill fills the dmi_strings array, and if possible sets the global is_laptop variable. */
398if (dmi_fill() != 0)
399return;
400
401switch (is_laptop) {
402case 1:
403msg_pdbg("Laptop detected via DMI.\n");
404break;
405case 2:
406msg_pdbg("DMI chassis-type is not specific enough.\n");
407break;
408}
409
410has_dmi_support = 1;
411int i;
412for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
413msg_pdbg("DMI string %s: \"%s\"\n", dmi_strings[i].keyword,
414 (dmi_strings[i].value == NULL) ? "" : dmi_strings[i].value);
415}
416}
417
418/**
419 * Does an substring/prefix/postfix/whole-string match.
420 *
421 * The pattern is matched as-is. The only metacharacters supported are '^'
422 * at the beginning and '$' at the end. So you can look for "^prefix",
423 * "suffix$", "substring" or "^complete string$".
424 *
425 * @param value The non-NULL string to check.
426 * @param pattern The non-NULL pattern.
427 * @return Nonzero if pattern matches.
428 */
429static int dmi_compare(const char *value, const char *pattern)
430{
431int anchored = 0;
432int patternlen;
433
434msg_pspew("matching %s against %s\n", value, pattern);
435/* The empty string is part of all strings! */
436if (pattern[0] == 0)
437return 1;
438
439if (pattern[0] == '^') {
440anchored = 1;
441pattern++;
442}
443
444patternlen = strlen(pattern);
445if (pattern[patternlen - 1] == '$') {
446int valuelen = strlen(value);
447patternlen--;
448if (patternlen > valuelen)
449return 0;
450
451/* full string match: require same length */
452if (anchored && (valuelen != patternlen))
453return 0;
454
455/* start character to make ends match */
456value += valuelen - patternlen;
457anchored = 1;
458}
459
460if (anchored)
461return strncmp(value, pattern, patternlen) == 0;
462else
463return strstr(value, pattern) != NULL;
464}
465
466int dmi_match(const char *pattern)
467{
468int i;
469
470if (!has_dmi_support)
471return 0;
472
473for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
474if (dmi_strings[i].value == NULL)
475continue;
476
477if (dmi_compare(dmi_strings[i].value, pattern))
478return 1;
479}
480
481return 0;
482}
483
484#endif // defined(__i386__) || defined(__x86_64__)

Archive Download this file

Revision: HEAD