dmi.c 12.7 KB
Newer Older
Michael Karcher's avatar
Michael Karcher committed
1 2 3
/*
 * This file is part of the flashrom project.
 *
Sean Nelson's avatar
Sean Nelson committed
4 5
 * Copyright (C) 2000-2002 Alan Cox <alan@redhat.com>
 * Copyright (C) 2002-2010 Jean Delvare <khali@linux-fr.org>
Michael Karcher's avatar
Michael Karcher committed
6
 * Copyright (C) 2009,2010 Michael Karcher
Sean Nelson's avatar
Sean Nelson committed
7
 * Copyright (C) 2011-2013 Stefan Tauner
Michael Karcher's avatar
Michael Karcher committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
24
#include <strings.h>
Michael Karcher's avatar
Michael Karcher committed
25
#include <string.h>
26
#include <ctype.h>
Michael Karcher's avatar
Michael Karcher committed
27 28 29 30
#include <stdio.h>
#include <stdlib.h>

#include "flash.h"
31
#include "programmer.h"
Michael Karcher's avatar
Michael Karcher committed
32

Sean Nelson's avatar
Sean Nelson committed
33
#if defined(__i386__) || defined(__x86_64__)
34

Sean Nelson's avatar
Sean Nelson committed
35 36
/* Enable SMBIOS decoding. Currently legacy DMI decoding is enough. */
#define SM_SUPPORT 0
37

Sean Nelson's avatar
Sean Nelson committed
38 39
/* Strings longer than 4096 in DMI are just insane. */
#define DMI_MAX_ANSWER_LEN 4096
40

Sean Nelson's avatar
Sean Nelson committed
41
int has_dmi_support = 0;
42

Sean Nelson's avatar
Sean Nelson committed
43 44 45 46 47 48 49 50 51 52 53 54
static struct {
	const char *const keyword;
	const uint8_t type;
	const uint8_t offset;
	char *value;
} dmi_strings[] = {
	{ "system-manufacturer", 1, 0x04, NULL },
	{ "system-product-name", 1, 0x05, NULL },
	{ "system-version", 1, 0x06, NULL },
	{ "baseboard-manufacturer", 2, 0x04, NULL },
	{ "baseboard-product-name", 2, 0x05, NULL },
	{ "baseboard-version", 2, 0x06, NULL },
Michael Karcher's avatar
Michael Karcher committed
55 56
};

57 58 59 60 61 62
/* This list is used to identify supposed laptops. The is_laptop field has the
 * following meaning:
 * 	- 0: in all likelihood not a laptop
 * 	- 1: in all likelihood a laptop
 * 	- 2: chassis-type is not specific enough
 * A full list of chassis types can be found in the System Management BIOS
63 64 65 66 67
 * (SMBIOS) Reference Specification 2.7.0 section 7.4.1 "Chassis Types" at
 * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf
 * The types below are the most common ones.
 */
static const struct {
Sean Nelson's avatar
Sean Nelson committed
68 69 70
	uint8_t type;
	uint8_t is_laptop;
	char *name;
71
} dmi_chassis_types[] = {
72 73
	{0x01, 2, "Other"},
	{0x02, 2, "Unknown"},
74 75
	{0x03, 0, "Desktop"},
	{0x04, 0, "Low Profile Desktop"},
76 77
	{0x06, 0, "Mini Tower"},
	{0x07, 0, "Tower"},
78 79 80 81 82
	{0x08, 1, "Portable"},
	{0x09, 1, "Laptop"},
	{0x0a, 1, "Notebook"},
	{0x0b, 1, "Hand Held"},
	{0x0e, 1, "Sub Notebook"},
83 84
	{0x11, 0, "Main Server Chassis"},
	{0x17, 0, "Rack Mount Chassis"},
85
	{0x18, 0, "Sealed-case PC"}, /* used by Supermicro (X8SIE) */
86 87
};

Sean Nelson's avatar
Sean Nelson committed
88 89 90 91 92 93 94 95 96 97
#if CONFIG_INTERNAL_DMI == 1
#ifdef __DJGPP__ /* There is no strnlen in DJGPP. FIXME: Move this to a common utility file. */
size_t strnlen(const char *str, size_t n)
{
	size_t i;
	for (i = 0; i < n && str[i] != '\0'; i++)
		;
	return i;
}
#endif
Michael Karcher's avatar
Michael Karcher committed
98

Sean Nelson's avatar
Sean Nelson committed
99 100 101 102
static bool dmi_checksum(const uint8_t * const buf, size_t len)
{
	uint8_t sum = 0;
	size_t a;
Michael Karcher's avatar
Michael Karcher committed
103

Sean Nelson's avatar
Sean Nelson committed
104 105 106 107 108
	for (a = 0; a < len; a++)
		sum += buf[a];
	return (sum == 0);
}

109 110 111 112 113 114 115 116 117 118
/** Retrieve a DMI string.
 *
 * See SMBIOS spec. section 6.1.3 "Text strings".
 * The table will be unmapped ASAP, hence return a duplicated & sanitized string that needs to be freed later.
 *
 * \param buf		the buffer to search through (usually appended directly to a DMI structure)
 * \param string_id	index of the string to look for
 * \param limit		pointer to the first byte beyond \em buf
 */
static char *dmi_string(const char *buf, uint8_t string_id, const char *limit)
Sean Nelson's avatar
Sean Nelson committed
119 120 121 122
{
	size_t i, len;

	if (string_id == 0)
123
		return strdup("Not Specified");
Sean Nelson's avatar
Sean Nelson committed
124 125

	while (string_id > 1 && string_id--) {
126
		if (buf >= limit) {
Sean Nelson's avatar
Sean Nelson committed
127
			msg_perr("DMI table is broken (string portion out of bounds)!\n");
128
			return strdup("<OUT OF BOUNDS>");
Sean Nelson's avatar
Sean Nelson committed
129
		}
130
		buf += strnlen(buf, limit - buf) + 1;
Sean Nelson's avatar
Sean Nelson committed
131 132 133
	}

	if (!*buf) /* as long as the current byte we're on isn't null */
134
		return strdup("<BAD INDEX>");
Sean Nelson's avatar
Sean Nelson committed
135

136 137 138 139 140 141
	len = strnlen(buf, limit - buf);
	char *newbuf = malloc(len + 1);
	if (newbuf == NULL) {
		msg_perr("Out of memory!\n");
		return NULL;
	}
Sean Nelson's avatar
Sean Nelson committed
142 143

	/* fix junk bytes in the string */
144
	for (i = 0; i < len && buf[i] != '\0'; i++) {
145
		if (isprint((unsigned char)buf[i]))
146 147 148 149 150
			newbuf[i] = buf[i];
		else
			newbuf[i] = ' ';
	}
	newbuf[i] = '\0';
Sean Nelson's avatar
Sean Nelson committed
151

152
	return newbuf;
Sean Nelson's avatar
Sean Nelson committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}

static void dmi_chassis_type(uint8_t code)
{
	int i;
	code &= 0x7f; /* bits 6:0 are chassis type, 7th bit is the lock bit */
	is_laptop = 2;
	for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
		if (code == dmi_chassis_types[i].type) {
			msg_pdbg("DMI string chassis-type: \"%s\"\n", dmi_chassis_types[i].name);
			is_laptop = dmi_chassis_types[i].is_laptop;
			break;
		}
	}
}

static void dmi_table(uint32_t base, uint16_t len, uint16_t num)
{
	int i = 0, j = 0;

173
	uint8_t *dmi_table_mem = physmap_ro("DMI Table", base, len);
Sean Nelson's avatar
Sean Nelson committed
174 175 176 177 178 179 180 181 182 183 184 185 186
	if (dmi_table_mem == NULL) {
		msg_perr("Unable to access DMI Table\n");
		return;
	}

	uint8_t *data = dmi_table_mem;
	uint8_t *limit = dmi_table_mem + len;

	/* SMBIOS structure header is always 4 B long and contains:
	 *  - uint8_t type;	// see dmi_chassis_types's type
	 *  - uint8_t length;	// data section w/ header w/o strings
	 *  - uint16_t handle;
	 */
187
	while (i < num && data + 4 < limit) {
Sean Nelson's avatar
Sean Nelson committed
188 189 190
		/* - If a short entry is found (less than 4 bytes), not only it
		 *   is invalid, but we cannot reliably locate the next entry.
		 * - If the length value indicates that this structure spreads
191
		 *   across the table border, something is fishy too.
Sean Nelson's avatar
Sean Nelson committed
192 193 194
		 * Better stop at this point, and let the user know his/her
		 * table is broken.
		 */
195
		if (data[1] < 4 || data + data[1] >= limit) {
Sean Nelson's avatar
Sean Nelson committed
196 197 198 199 200
			msg_perr("DMI table is broken (bogus header)!\n");
			break;
		}

		if(data[0] == 3) {
201
			if (data + 5 < limit)
Sean Nelson's avatar
Sean Nelson committed
202
				dmi_chassis_type(data[5]);
203 204
			else /* the table is broken, but laptop detection is optional, hence continue. */
				msg_pwarn("DMI table is broken (chassis_type out of bounds)!\n");
Sean Nelson's avatar
Sean Nelson committed
205 206 207 208 209 210 211 212
		} else
			for (j = 0; j < ARRAY_SIZE(dmi_strings); j++) {
				uint8_t offset = dmi_strings[j].offset;
				uint8_t type = dmi_strings[j].type;

				if (data[0] != type)
					continue;

213
				if (data[1] <= offset || data + offset >= limit) {
Sean Nelson's avatar
Sean Nelson committed
214 215 216 217
					msg_perr("DMI table is broken (offset out of bounds)!\n");
					goto out;
				}

218 219
				dmi_strings[j].value = dmi_string((const char *)(data + data[1]), data[offset],
								  (const char *)limit);
Sean Nelson's avatar
Sean Nelson committed
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
			}
		/* Find next structure by skipping data and string sections */
		data += data[1];
		while (data + 1 <= limit) {
			if (data[0] == 0 && data[1] == 0)
				break;
			data++;
		}
		data += 2;
		i++;
	}
out:
	physunmap(dmi_table_mem, len);
}

#if SM_SUPPORT
static int smbios_decode(uint8_t *buf)
{
	/* TODO: other checks mentioned in the conformance guidelines? */
	if (!dmi_checksum(buf, buf[0x05]) ||
	    (memcmp(buf + 0x10, "_DMI_", 5) != 0) ||
	    !dmi_checksum(buf + 0x10, 0x0F))
			return 0;

	dmi_table(mmio_readl(buf + 0x18), mmio_readw(buf + 0x16), mmio_readw(buf + 0x1C));

	return 1;
}
#endif

static int legacy_decode(uint8_t *buf)
{
	if (!dmi_checksum(buf, 0x0F))
		return 1;

	dmi_table(mmio_readl(buf + 0x08), mmio_readw(buf + 0x06), mmio_readw(buf + 0x0C));

	return 0;
}

int dmi_fill(void)
{
	size_t fp;
	uint8_t *dmi_mem;
	int ret = 1;

	msg_pdbg("Using Internal DMI decoder.\n");
	/* There are two ways specified to gain access to the SMBIOS table:
	 * - EFI's configuration table contains a pointer to the SMBIOS table. On linux it can be obtained from
	 *   sysfs. EFI's SMBIOS GUID is: {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,0x0,0x90,0x27,0x3f,0xc1,0x4d}
	 * - Scanning physical memory address range 0x000F0000h to 0x000FFFFF for the anchor-string(s). */
271 272
	dmi_mem = physmap_ro("DMI", 0xF0000, 0x10000);
	if (dmi_mem == ERROR_PTR)
Sean Nelson's avatar
Sean Nelson committed
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
		return ret;

	for (fp = 0; fp <= 0xFFF0; fp += 16) {
#if SM_SUPPORT
		if (memcmp(dmi_mem + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
			if (smbios_decode(dmi_mem + fp)) // FIXME: length check
				goto out;
		} else
#endif
		if (memcmp(dmi_mem + fp, "_DMI_", 5) == 0)
			if (legacy_decode(dmi_mem + fp) == 0) {
				ret = 0;
				goto out;
			}
	}
	msg_pinfo("No DMI table found.\n");
out:
	physunmap(dmi_mem, 0x10000);
	return ret;
}

#else /* CONFIG_INTERNAL_DMI */

#define DMI_COMMAND_LEN_MAX 300
static const char *dmidecode_command = "dmidecode";
Michael Karcher's avatar
Michael Karcher committed
298

299
static char *get_dmi_string(const char *string_name)
Michael Karcher's avatar
Michael Karcher committed
300 301
{
	FILE *dmidecode_pipe;
302 303
	char *result;
	char answerbuf[DMI_MAX_ANSWER_LEN];
Sean Nelson's avatar
Sean Nelson committed
304
	char commandline[DMI_COMMAND_LEN_MAX];
305

306 307 308 309
	snprintf(commandline, sizeof(commandline),
		 "%s -s %s", dmidecode_command, string_name);
	dmidecode_pipe = popen(commandline, "r");
	if (!dmidecode_pipe) {
Stefan Tauner's avatar
Stefan Tauner committed
310
		msg_perr("Opening DMI pipe failed!\n");
311
		return NULL;
Michael Karcher's avatar
Michael Karcher committed
312
	}
313 314

	/* Kill lines starting with '#', as recent dmidecode versions
315 316 317 318 319
	 * have the quirk to emit a "# SMBIOS implementations newer..."
	 * message even on "-s" if the SMBIOS declares a
	 * newer-than-supported version number, while it *should* only print
	 * the requested string.
	 */
320 321
	do {
		if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
322
			if (ferror(dmidecode_pipe)) {
323 324 325 326 327 328
				msg_perr("DMI pipe read error\n");
				pclose(dmidecode_pipe);
				return NULL;
			} else {
				answerbuf[0] = 0;	/* Hit EOF */
			}
329
		}
330
	} while (answerbuf[0] == '#');
331

Stefan Tauner's avatar
Stefan Tauner committed
332
	/* Discard all output exceeding DMI_MAX_ANSWER_LEN to prevent deadlock on pclose. */
333 334 335
	while (!feof(dmidecode_pipe))
		getc(dmidecode_pipe);
	if (pclose(dmidecode_pipe) != 0) {
Stefan Tauner's avatar
Stefan Tauner committed
336
		msg_pwarn("dmidecode execution unsuccessful - continuing without DMI info\n");
337
		return NULL;
Michael Karcher's avatar
Michael Karcher committed
338
	}
339

340
	/* Chomp trailing newline. */
341
	if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n')
342 343 344
		answerbuf[strlen(answerbuf) - 1] = 0;

	result = strdup(answerbuf);
Sean Nelson's avatar
Sean Nelson committed
345
	if (result == NULL)
Stefan Tauner's avatar
Stefan Tauner committed
346
		msg_pwarn("Warning: Out of memory - DMI support fails");
347 348 349 350

	return result;
}

Sean Nelson's avatar
Sean Nelson committed
351
int dmi_fill(void)
352 353
{
	int i;
Michael Karcher's avatar
Michael Karcher committed
354
	char *chassis_type;
355

Sean Nelson's avatar
Sean Nelson committed
356 357 358 359 360
	msg_pdbg("Using External DMI decoder.\n");
	for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
		dmi_strings[i].value = get_dmi_string(dmi_strings[i].keyword);
		if (dmi_strings[i].value == NULL)
			return 1;
361
	}
Michael Karcher's avatar
Michael Karcher committed
362 363

	chassis_type = get_dmi_string("chassis-type");
364
	if (chassis_type == NULL)
Sean Nelson's avatar
Sean Nelson committed
365
		return 0; /* chassis-type handling is optional anyway */
366

Sean Nelson's avatar
Sean Nelson committed
367
	msg_pdbg("DMI string chassis-type: \"%s\"\n", chassis_type);
368 369 370 371 372
	is_laptop = 2;
	for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
		if (strcasecmp(chassis_type, dmi_chassis_types[i].name) == 0) {
			is_laptop = dmi_chassis_types[i].is_laptop;
			break;
373
		}
Michael Karcher's avatar
Michael Karcher committed
374
	}
Sean Nelson's avatar
Sean Nelson committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	free(chassis_type);
	return 0;
}

#endif /* CONFIG_INTERNAL_DMI */

static int dmi_shutdown(void *data)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
		free(dmi_strings[i].value);
		dmi_strings[i].value = NULL;
	}
	return 0;
}

void dmi_init(void)
{
	/* Register shutdown function before we allocate anything. */
	if (register_shutdown(dmi_shutdown, NULL)) {
		msg_pwarn("Warning: Could not register DMI shutdown function - continuing without DMI info.\n");
		return;
	}

	/* dmi_fill fills the dmi_strings array, and if possible sets the global is_laptop variable. */
	if (dmi_fill() != 0)
		return;
402 403 404 405 406 407 408 409 410

	switch (is_laptop) {
	case 1:
		msg_pdbg("Laptop detected via DMI.\n");
		break;
	case 2:
		msg_pdbg("DMI chassis-type is not specific enough.\n");
		break;
	}
Sean Nelson's avatar
Sean Nelson committed
411 412 413 414 415 416 417

	has_dmi_support = 1;
	int i;
	for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
		msg_pdbg("DMI string %s: \"%s\"\n", dmi_strings[i].keyword,
			 (dmi_strings[i].value == NULL) ? "" : dmi_strings[i].value);
	}
Michael Karcher's avatar
Michael Karcher committed
418 419 420 421 422 423 424 425 426
}

/**
 * Does an substring/prefix/postfix/whole-string match.
 *
 * The pattern is matched as-is. The only metacharacters supported are '^'
 * at the beginning and '$' at the end. So you can look for "^prefix",
 * "suffix$", "substring" or "^complete string$".
 *
427 428
 * @param value The non-NULL string to check.
 * @param pattern The non-NULL pattern.
Michael Karcher's avatar
Michael Karcher committed
429 430 431 432
 * @return Nonzero if pattern matches.
 */
static int dmi_compare(const char *value, const char *pattern)
{
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
433 434
	int anchored = 0;
	int patternlen;
435

436
	msg_pspew("matching %s against %s\n", value, pattern);
437
	/* The empty string is part of all strings! */
Michael Karcher's avatar
Michael Karcher committed
438 439 440 441 442 443 444 445 446 447 448 449
	if (pattern[0] == 0)
		return 1;

	if (pattern[0] == '^') {
		anchored = 1;
		pattern++;
	}

	patternlen = strlen(pattern);
	if (pattern[patternlen - 1] == '$') {
		int valuelen = strlen(value);
		patternlen--;
450
		if (patternlen > valuelen)
Michael Karcher's avatar
Michael Karcher committed
451 452 453
			return 0;

		/* full string match: require same length */
454
		if (anchored && (valuelen != patternlen))
Michael Karcher's avatar
Michael Karcher committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
			return 0;

		/* start character to make ends match */
		value += valuelen - patternlen;
		anchored = 1;
	}

	if (anchored)
		return strncmp(value, pattern, patternlen) == 0;
	else
		return strstr(value, pattern) != NULL;
}

int dmi_match(const char *pattern)
{
	int i;
471

Michael Karcher's avatar
Michael Karcher committed
472 473 474
	if (!has_dmi_support)
		return 0;

475 476 477 478
	for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
		if (dmi_strings[i].value == NULL)
			continue;

Sean Nelson's avatar
Sean Nelson committed
479
		if (dmi_compare(dmi_strings[i].value, pattern))
Michael Karcher's avatar
Michael Karcher committed
480
			return 1;
481
	}
Michael Karcher's avatar
Michael Karcher committed
482 483 484

	return 0;
}
485

Sean Nelson's avatar
Sean Nelson committed
486
#endif // defined(__i386__) || defined(__x86_64__)