blob: 8f78376a89cf6b7b23fb3a00d61061180b0e8f3c [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"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000026#include "programmer.h"
Michael Karcher6701ee82010-01-20 14:14:11 +000027
Carl-Daniel Hailfingere1fdff42010-06-23 23:14:44 +000028int has_dmi_support = 0;
29
30#if STANDALONE
31
32/* Stub to indicate missing DMI functionality.
33 * has_dmi_support is 0 by default, so nothing to do here.
34 * Because dmidecode is not available on all systems, the goal is to implement
35 * the DMI subset we need directly in this file.
36 */
37void dmi_init(void)
38{
39}
40
41int dmi_match(const char *pattern)
42{
43 return 0;
44}
45
46#else /* STANDALONE */
47
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000048static const char *dmidecode_names[] = {
Michael Karcherf6498d72010-02-24 22:43:44 +000049 "system-manufacturer",
50 "system-product-name",
51 "system-version",
52 "baseboard-manufacturer",
53 "baseboard-product-name",
Uwe Hermann43959702010-03-13 17:28:29 +000054 "baseboard-version",
Michael Karcher6701ee82010-01-20 14:14:11 +000055};
56
Stefan Taunera34d7192011-07-26 00:54:42 +000057/* This list is used to identify supposed laptops. The is_laptop field has the
58 * following meaning:
59 * - 0: in all likelihood not a laptop
60 * - 1: in all likelihood a laptop
61 * - 2: chassis-type is not specific enough
62 * A full list of chassis types can be found in the System Management BIOS
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000063 * (SMBIOS) Reference Specification 2.7.0 section 7.4.1 "Chassis Types" at
64 * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf
65 * The types below are the most common ones.
66 */
67static const struct {
68 unsigned char type;
69 unsigned char is_laptop;
70 const char *name;
71} dmi_chassis_types[] = {
Stefan Taunera34d7192011-07-26 00:54:42 +000072 {0x01, 2, "Other"},
73 {0x02, 2, "Unknown"},
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000074 {0x03, 0, "Desktop",},
Stefan Taunera34d7192011-07-26 00:54:42 +000075 {0x06, 0, "Mini Tower"},
76 {0x07, 0, "Tower"},
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000077 {0x08, 1, "Portable"},
78 {0x09, 1, "Laptop"},
79 {0x0a, 1, "Notebook"},
80 {0x0b, 1, "Hand Held"},
81 {0x0e, 1, "Sub Notebook"},
Stefan Taunera34d7192011-07-26 00:54:42 +000082 {0x11, 0, "Main Server Chassis"},
83 {0x17, 0, "Rack Mount Chassis"},
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000084};
85
Michael Karcher6701ee82010-01-20 14:14:11 +000086#define DMI_COMMAND_LEN_MAX 260
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000087static const char *dmidecode_command = "dmidecode";
Michael Karcher6701ee82010-01-20 14:14:11 +000088
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000089static char *dmistrings[ARRAY_SIZE(dmidecode_names)];
Michael Karcher6701ee82010-01-20 14:14:11 +000090
Uwe Hermann43959702010-03-13 17:28:29 +000091/* Strings longer than 4096 in DMI are just insane. */
Michael Karcher6701ee82010-01-20 14:14:11 +000092#define DMI_MAX_ANSWER_LEN 4096
93
Michael Karcher0d7fb7c2010-02-26 09:51:16 +000094static char *get_dmi_string(const char *string_name)
Michael Karcher6701ee82010-01-20 14:14:11 +000095{
96 FILE *dmidecode_pipe;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +000097 char *result;
98 char answerbuf[DMI_MAX_ANSWER_LEN];
Uwe Hermann43959702010-03-13 17:28:29 +000099 char commandline[DMI_COMMAND_LEN_MAX + 40];
100
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000101 snprintf(commandline, sizeof(commandline),
102 "%s -s %s", dmidecode_command, string_name);
103 dmidecode_pipe = popen(commandline, "r");
104 if (!dmidecode_pipe) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000105 msg_perr("DMI pipe open error\n");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000106 return NULL;
Michael Karcher6701ee82010-01-20 14:14:11 +0000107 }
Michael Karcherd9f266d2010-08-08 21:56:52 +0000108
109 /* Kill lines starting with '#', as recent dmidecode versions
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000110 * have the quirk to emit a "# SMBIOS implementations newer..."
111 * message even on "-s" if the SMBIOS declares a
112 * newer-than-supported version number, while it *should* only print
113 * the requested string.
114 */
Michael Karcherd9f266d2010-08-08 21:56:52 +0000115 do {
116 if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000117 if (ferror(dmidecode_pipe)) {
Michael Karcherd9f266d2010-08-08 21:56:52 +0000118 msg_perr("DMI pipe read error\n");
119 pclose(dmidecode_pipe);
120 return NULL;
121 } else {
122 answerbuf[0] = 0; /* Hit EOF */
123 }
Michael Karcher45f79cb2010-03-24 17:55:04 +0000124 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000125 } while (answerbuf[0] == '#');
Michael Karcherd9f266d2010-08-08 21:56:52 +0000126
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000127 /* Toss all output above DMI_MAX_ANSWER_LEN away to prevent
128 deadlock on pclose. */
129 while (!feof(dmidecode_pipe))
130 getc(dmidecode_pipe);
131 if (pclose(dmidecode_pipe) != 0) {
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000132 msg_pinfo("dmidecode execution unsuccessful - continuing "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000133 "without DMI info\n");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000134 return NULL;
135 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000136
Uwe Hermann43959702010-03-13 17:28:29 +0000137 /* Chomp trailing newline. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000138 if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n')
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000139 answerbuf[strlen(answerbuf) - 1] = 0;
Sean Nelson316a29f2010-05-07 20:09:04 +0000140 msg_pdbg("DMI string %s: \"%s\"\n", string_name, answerbuf);
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000141
142 result = strdup(answerbuf);
143 if (!result)
144 puts("WARNING: Out of memory - DMI support fails");
145
146 return result;
147}
148
149void dmi_init(void)
150{
151 int i;
Michael Karcher8c1df282010-02-26 09:51:20 +0000152 char *chassis_type;
Uwe Hermann43959702010-03-13 17:28:29 +0000153
Michael Karcher6701ee82010-01-20 14:14:11 +0000154 has_dmi_support = 1;
Michael Karcherfd416702010-03-14 17:57:52 +0000155 for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++) {
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000156 dmistrings[i] = get_dmi_string(dmidecode_names[i]);
157 if (!dmistrings[i]) {
158 has_dmi_support = 0;
Michael Karcherfd416702010-03-14 17:57:52 +0000159 return;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000160 }
161 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000162
163 chassis_type = get_dmi_string("chassis-type");
Stefan Taunera34d7192011-07-26 00:54:42 +0000164 if (chassis_type == NULL)
165 return;
166
167 is_laptop = 2;
168 for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
169 if (strcasecmp(chassis_type, dmi_chassis_types[i].name) == 0) {
170 is_laptop = dmi_chassis_types[i].is_laptop;
171 break;
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +0000172 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000173 }
Stefan Taunera34d7192011-07-26 00:54:42 +0000174
175 switch (is_laptop) {
176 case 1:
177 msg_pdbg("Laptop detected via DMI.\n");
178 break;
179 case 2:
180 msg_pdbg("DMI chassis-type is not specific enough.\n");
181 break;
182 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000183 free(chassis_type);
Michael Karcher6701ee82010-01-20 14:14:11 +0000184}
185
186/**
187 * Does an substring/prefix/postfix/whole-string match.
188 *
189 * The pattern is matched as-is. The only metacharacters supported are '^'
190 * at the beginning and '$' at the end. So you can look for "^prefix",
191 * "suffix$", "substring" or "^complete string$".
192 *
193 * @param value The string to check.
194 * @param pattern The pattern.
195 * @return Nonzero if pattern matches.
196 */
197static int dmi_compare(const char *value, const char *pattern)
198{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000199 int anchored = 0;
200 int patternlen;
Uwe Hermann43959702010-03-13 17:28:29 +0000201
Sean Nelson316a29f2010-05-07 20:09:04 +0000202 msg_pspew("matching %s against %s\n", value, pattern);
Uwe Hermann43959702010-03-13 17:28:29 +0000203 /* The empty string is part of all strings! */
Michael Karcher6701ee82010-01-20 14:14:11 +0000204 if (pattern[0] == 0)
205 return 1;
206
207 if (pattern[0] == '^') {
208 anchored = 1;
209 pattern++;
210 }
211
212 patternlen = strlen(pattern);
213 if (pattern[patternlen - 1] == '$') {
214 int valuelen = strlen(value);
215 patternlen--;
Uwe Hermann43959702010-03-13 17:28:29 +0000216 if (patternlen > valuelen)
Michael Karcher6701ee82010-01-20 14:14:11 +0000217 return 0;
218
219 /* full string match: require same length */
Uwe Hermann43959702010-03-13 17:28:29 +0000220 if (anchored && (valuelen != patternlen))
Michael Karcher6701ee82010-01-20 14:14:11 +0000221 return 0;
222
223 /* start character to make ends match */
224 value += valuelen - patternlen;
225 anchored = 1;
226 }
227
228 if (anchored)
229 return strncmp(value, pattern, patternlen) == 0;
230 else
231 return strstr(value, pattern) != NULL;
232}
233
234int dmi_match(const char *pattern)
235{
236 int i;
Uwe Hermann43959702010-03-13 17:28:29 +0000237
Michael Karcher6701ee82010-01-20 14:14:11 +0000238 if (!has_dmi_support)
239 return 0;
240
Michael Karcherfd416702010-03-14 17:57:52 +0000241 for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++)
Uwe Hermann43959702010-03-13 17:28:29 +0000242 if (dmi_compare(dmistrings[i], pattern))
Michael Karcher4dfa0932010-02-12 05:44:18 +0000243 return 1;
Michael Karcher6701ee82010-01-20 14:14:11 +0000244
245 return 0;
246}
Carl-Daniel Hailfingere1fdff42010-06-23 23:14:44 +0000247
248#endif /* STANDALONE */