blob: a68f210bf38bbd2741e7b6d26abd5ed797132591 [file] [log] [blame]
Michael Karcher6701ee82010-01-20 14:14:11 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009,2010 Michael Karcher
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <string.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25#include "flash.h"
26
27enum dmi_strings {
28 DMI_SYS_MANUFACTURER,
29 DMI_SYS_PRODUCT,
30 DMI_SYS_VERSION,
31 DMI_BB_MANUFACTURER,
32 DMI_BB_PRODUCT,
33 DMI_BB_VERSION,
34 DMI_ID_INVALID /* This must always be the last entry */
35};
36
37/* The short_id for baseboard starts with "m" as in mainboard to leave
38 "b" available for BIOS */
Michael Karcherf6498d72010-02-24 22:43:44 +000039const char *dmidecode_names[DMI_ID_INVALID] = {
40 "system-manufacturer",
41 "system-product-name",
42 "system-version",
43 "baseboard-manufacturer",
44 "baseboard-product-name",
45 "baseboard-version"
Michael Karcher6701ee82010-01-20 14:14:11 +000046};
47
48#define DMI_COMMAND_LEN_MAX 260
49const char *dmidecode_command = "dmidecode";
50
51int has_dmi_support = 0;
52char *dmistrings[DMI_ID_INVALID];
53
54/* strings longer than 4096 in DMI are just insane */
55#define DMI_MAX_ANSWER_LEN 4096
56
Michael Karcher0d7fb7c2010-02-26 09:51:16 +000057static char *get_dmi_string(const char *string_name)
Michael Karcher6701ee82010-01-20 14:14:11 +000058{
59 FILE *dmidecode_pipe;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +000060 char *result;
61 char answerbuf[DMI_MAX_ANSWER_LEN];
62 char commandline[DMI_COMMAND_LEN_MAX+40];
63 snprintf(commandline, sizeof(commandline),
64 "%s -s %s", dmidecode_command, string_name);
65 dmidecode_pipe = popen(commandline, "r");
66 if (!dmidecode_pipe) {
67 printf_debug("DMI pipe open error\n");
68 return NULL;
Michael Karcher6701ee82010-01-20 14:14:11 +000069 }
Michael Karcher0d7fb7c2010-02-26 09:51:16 +000070 if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe) &&
71 ferror(dmidecode_pipe)) {
72 printf_debug("DMI pipe read error\n");
73 pclose(dmidecode_pipe);
74 return NULL;
75 }
76 /* Toss all output above DMI_MAX_ANSWER_LEN away to prevent
77 deadlock on pclose. */
78 while (!feof(dmidecode_pipe))
79 getc(dmidecode_pipe);
80 if (pclose(dmidecode_pipe) != 0) {
81 printf_debug("DMI pipe close error\n");
82 return NULL;
83 }
Michael Karcher6701ee82010-01-20 14:14:11 +000084
Michael Karcher0d7fb7c2010-02-26 09:51:16 +000085 /* chomp trailing newline */
86 if (answerbuf[0] != 0 &&
87 answerbuf[strlen(answerbuf) - 1] == '\n')
88 answerbuf[strlen(answerbuf) - 1] = 0;
89 printf_debug("DMI string %s: \"%s\"\n", string_name, answerbuf);
90
91 result = strdup(answerbuf);
92 if (!result)
93 puts("WARNING: Out of memory - DMI support fails");
94
95 return result;
96}
97
98void dmi_init(void)
99{
100 int i;
Michael Karcher6701ee82010-01-20 14:14:11 +0000101 has_dmi_support = 1;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000102 for (i = 0; i < DMI_ID_INVALID; i++) {
103 dmistrings[i] = get_dmi_string(dmidecode_names[i]);
104 if (!dmistrings[i]) {
105 has_dmi_support = 0;
106 break;
107 }
108 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000109}
110
111/**
112 * Does an substring/prefix/postfix/whole-string match.
113 *
114 * The pattern is matched as-is. The only metacharacters supported are '^'
115 * at the beginning and '$' at the end. So you can look for "^prefix",
116 * "suffix$", "substring" or "^complete string$".
117 *
118 * @param value The string to check.
119 * @param pattern The pattern.
120 * @return Nonzero if pattern matches.
121 */
122static int dmi_compare(const char *value, const char *pattern)
123{
124 int anchored = 0;
125 int patternlen;
126 printf_debug("matching %s against %s\n", value, pattern);
127 /* The empty string is part of all strings */
128 if (pattern[0] == 0)
129 return 1;
130
131 if (pattern[0] == '^') {
132 anchored = 1;
133 pattern++;
134 }
135
136 patternlen = strlen(pattern);
137 if (pattern[patternlen - 1] == '$') {
138 int valuelen = strlen(value);
139 patternlen--;
140 if(patternlen > valuelen)
141 return 0;
142
143 /* full string match: require same length */
144 if(anchored && (valuelen != patternlen))
145 return 0;
146
147 /* start character to make ends match */
148 value += valuelen - patternlen;
149 anchored = 1;
150 }
151
152 if (anchored)
153 return strncmp(value, pattern, patternlen) == 0;
154 else
155 return strstr(value, pattern) != NULL;
156}
157
158int dmi_match(const char *pattern)
159{
160 int i;
161 if (!has_dmi_support)
162 return 0;
163
164 for (i = 0;i < DMI_ID_INVALID; i++)
Michael Karcher4dfa0932010-02-12 05:44:18 +0000165 if(dmi_compare(dmistrings[i], pattern))
166 return 1;
Michael Karcher6701ee82010-01-20 14:14:11 +0000167
168 return 0;
169}