dmi.c 4.16 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 26
/*
 * 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"

27
const char *dmidecode_names[] = {
Michael Karcher's avatar
Michael Karcher committed
28 29 30 31 32
	"system-manufacturer",
	"system-product-name",
	"system-version",
	"baseboard-manufacturer",
	"baseboard-product-name",
33
	"baseboard-version",
Michael Karcher's avatar
Michael Karcher committed
34 35 36 37 38 39
};

#define DMI_COMMAND_LEN_MAX 260
const char *dmidecode_command = "dmidecode";

int has_dmi_support = 0;
40
char *dmistrings[ARRAY_SIZE(dmidecode_names)];
Michael Karcher's avatar
Michael Karcher committed
41

42
/* Strings longer than 4096 in DMI are just insane. */
Michael Karcher's avatar
Michael Karcher committed
43 44
#define DMI_MAX_ANSWER_LEN 4096

45
static char *get_dmi_string(const char *string_name)
Michael Karcher's avatar
Michael Karcher committed
46 47
{
	FILE *dmidecode_pipe;
48 49
	char *result;
	char answerbuf[DMI_MAX_ANSWER_LEN];
50 51
	char commandline[DMI_COMMAND_LEN_MAX + 40];

52 53 54 55 56 57
	snprintf(commandline, sizeof(commandline),
		 "%s -s %s", dmidecode_command, string_name);
	dmidecode_pipe = popen(commandline, "r");
	if (!dmidecode_pipe) {
		printf_debug("DMI pipe open error\n");
		return NULL;
Michael Karcher's avatar
Michael Karcher committed
58
	}
59 60 61 62 63 64 65 66 67 68 69 70 71
	if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe) &&
	    ferror(dmidecode_pipe)) {
		printf_debug("DMI pipe read error\n");
		pclose(dmidecode_pipe);
		return NULL;
	}
	/* 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) {
		printf_debug("DMI pipe close error\n");
		return NULL;
Michael Karcher's avatar
Michael Karcher committed
72
	}
73

74
	/* Chomp trailing newline. */
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	if (answerbuf[0] != 0 &&
	    answerbuf[strlen(answerbuf) - 1] == '\n')
		answerbuf[strlen(answerbuf) - 1] = 0;
	printf_debug("DMI string %s: \"%s\"\n", string_name, answerbuf);

	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
90
	char *chassis_type;
91

Michael Karcher's avatar
Michael Karcher committed
92
	has_dmi_support = 1;
93
	for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++) {
94 95 96
		dmistrings[i] = get_dmi_string(dmidecode_names[i]);
		if (!dmistrings[i]) {
			has_dmi_support = 0;
97
			return;
98 99
		}
	}
Michael Karcher's avatar
Michael Karcher committed
100 101 102

	chassis_type = get_dmi_string("chassis-type");
	if (chassis_type && !strcmp(chassis_type, "Notebook")) {
103
		printf_debug("Laptop detected via DMI\n");
Michael Karcher's avatar
Michael Karcher committed
104 105 106
		is_laptop = 1;
	}
	free(chassis_type);
Michael Karcher's avatar
Michael Karcher committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}

/**
 * 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;
124

Michael Karcher's avatar
Michael Karcher committed
125
	printf_debug("matching %s against %s\n", value, pattern);
126
	/* The empty string is part of all strings! */
Michael Karcher's avatar
Michael Karcher committed
127 128 129 130 131 132 133 134 135 136 137 138
	if (pattern[0] == 0)
		return 1;

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

	patternlen = strlen(pattern);
	if (pattern[patternlen - 1] == '$') {
		int valuelen = strlen(value);
		patternlen--;
139
		if (patternlen > valuelen)
Michael Karcher's avatar
Michael Karcher committed
140 141 142
			return 0;

		/* full string match: require same length */
143
		if (anchored && (valuelen != patternlen))
Michael Karcher's avatar
Michael Karcher committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
			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;
160

Michael Karcher's avatar
Michael Karcher committed
161 162 163
	if (!has_dmi_support)
		return 0;

164
	for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++)
165
		if (dmi_compare(dmistrings[i], pattern))
Michael Karcher's avatar
Michael Karcher committed
166
			return 1;
Michael Karcher's avatar
Michael Karcher committed
167 168 169

	return 0;
}