dmi.c 5.68 KB
Newer Older
Michael Karcher's avatar
Michael Karcher committed
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
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2009,2010 Michael Karcher
 *
 * 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
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "flash.h"
26
#include "programmer.h"
Michael Karcher's avatar
Michael Karcher committed
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
int has_dmi_support = 0;

#if STANDALONE

/* Stub to indicate missing DMI functionality.
 * has_dmi_support is 0 by default, so nothing to do here.
 * Because dmidecode is not available on all systems, the goal is to implement
 * the DMI subset we need directly in this file.
 */
void dmi_init(void)
{
}

int dmi_match(const char *pattern)
{
	return 0;
}

#else /* STANDALONE */

48
static const char *dmidecode_names[] = {
Michael Karcher's avatar
Michael Karcher committed
49 50 51 52 53
	"system-manufacturer",
	"system-product-name",
	"system-version",
	"baseboard-manufacturer",
	"baseboard-product-name",
54
	"baseboard-version",
Michael Karcher's avatar
Michael Karcher committed
55 56
};

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/* A full list of chassis types can be found in the System Management BIOS
 * (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 {
	unsigned char type;
	unsigned char is_laptop;
	const char *name;
} dmi_chassis_types[] = {
	{0x01, 0, "Other"},
	{0x02, 0, "Unknown"},
	{0x03, 0, "Desktop",},
	{0x08, 1, "Portable"},
	{0x09, 1, "Laptop"},
	{0x0a, 1, "Notebook"},
	{0x0b, 1, "Hand Held"},
	{0x0e, 1, "Sub Notebook"},
};

Michael Karcher's avatar
Michael Karcher committed
77
#define DMI_COMMAND_LEN_MAX 260
78
static const char *dmidecode_command = "dmidecode";
Michael Karcher's avatar
Michael Karcher committed
79

80
static char *dmistrings[ARRAY_SIZE(dmidecode_names)];
Michael Karcher's avatar
Michael Karcher committed
81

82
/* Strings longer than 4096 in DMI are just insane. */
Michael Karcher's avatar
Michael Karcher committed
83 84
#define DMI_MAX_ANSWER_LEN 4096

85
static char *get_dmi_string(const char *string_name)
Michael Karcher's avatar
Michael Karcher committed
86 87
{
	FILE *dmidecode_pipe;
88 89
	char *result;
	char answerbuf[DMI_MAX_ANSWER_LEN];
90 91
	char commandline[DMI_COMMAND_LEN_MAX + 40];

92 93 94 95
	snprintf(commandline, sizeof(commandline),
		 "%s -s %s", dmidecode_command, string_name);
	dmidecode_pipe = popen(commandline, "r");
	if (!dmidecode_pipe) {
96
		msg_perr("DMI pipe open error\n");
97
		return NULL;
Michael Karcher's avatar
Michael Karcher committed
98
	}
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

	/* Kill lines starting with '#', as recent dmidecode versions
	   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. */
	do {
		if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
			if(ferror(dmidecode_pipe)) {
				msg_perr("DMI pipe read error\n");
				pclose(dmidecode_pipe);
				return NULL;
			} else {
				answerbuf[0] = 0;	/* Hit EOF */
			}
114
		}
115 116
	} while(answerbuf[0] == '#');

117 118 119 120 121
	/* Toss all output above DMI_MAX_ANSWER_LEN away to prevent
	   deadlock on pclose. */
	while (!feof(dmidecode_pipe))
		getc(dmidecode_pipe);
	if (pclose(dmidecode_pipe) != 0) {
122 123
		msg_pinfo("dmidecode execution unsucessfull - continuing "
			"without DMI info\n");
124
		return NULL;
Michael Karcher's avatar
Michael Karcher committed
125
	}
126

127
	/* Chomp trailing newline. */
128 129 130
	if (answerbuf[0] != 0 &&
	    answerbuf[strlen(answerbuf) - 1] == '\n')
		answerbuf[strlen(answerbuf) - 1] = 0;
131
	msg_pdbg("DMI string %s: \"%s\"\n", string_name, answerbuf);
132 133 134 135 136 137 138 139 140 141 142

	result = strdup(answerbuf);
	if (!result)
		puts("WARNING: Out of memory - DMI support fails");

	return result;
}

void dmi_init(void)
{
	int i;
Michael Karcher's avatar
Michael Karcher committed
143
	char *chassis_type;
144

Michael Karcher's avatar
Michael Karcher committed
145
	has_dmi_support = 1;
146
	for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++) {
147 148 149
		dmistrings[i] = get_dmi_string(dmidecode_names[i]);
		if (!dmistrings[i]) {
			has_dmi_support = 0;
150
			return;
151 152
		}
	}
Michael Karcher's avatar
Michael Karcher committed
153 154

	chassis_type = get_dmi_string("chassis-type");
155 156 157 158 159 160 161 162 163
	if (chassis_type) {
		for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
			if (!strcasecmp(chassis_type,
					dmi_chassis_types[i].name) &&
			    dmi_chassis_types[i].is_laptop) {
				msg_pdbg("Laptop detected via DMI\n");
				is_laptop = 1;
			}
		}
Michael Karcher's avatar
Michael Karcher committed
164 165
	}
	free(chassis_type);
Michael Karcher's avatar
Michael Karcher committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}

/**
 * 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$".
 *
 * @param value The string to check.
 * @param pattern The pattern.
 * @return Nonzero if pattern matches.
 */
static int dmi_compare(const char *value, const char *pattern)
{
	int anchored = 0;
	int patternlen;
183

184
	msg_pspew("matching %s against %s\n", value, pattern);
185
	/* The empty string is part of all strings! */
Michael Karcher's avatar
Michael Karcher committed
186 187 188 189 190 191 192 193 194 195 196 197
	if (pattern[0] == 0)
		return 1;

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

	patternlen = strlen(pattern);
	if (pattern[patternlen - 1] == '$') {
		int valuelen = strlen(value);
		patternlen--;
198
		if (patternlen > valuelen)
Michael Karcher's avatar
Michael Karcher committed
199 200 201
			return 0;

		/* full string match: require same length */
202
		if (anchored && (valuelen != patternlen))
Michael Karcher's avatar
Michael Karcher committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
			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;
219

Michael Karcher's avatar
Michael Karcher committed
220 221 222
	if (!has_dmi_support)
		return 0;

223
	for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++)
224
		if (dmi_compare(dmistrings[i], pattern))
Michael Karcher's avatar
Michael Karcher committed
225
			return 1;
Michael Karcher's avatar
Michael Karcher committed
226 227 228

	return 0;
}
229 230

#endif /* STANDALONE */