blob: f18907f115debbb0c931cfedcac1f934667bc7a3 [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
57#define DMI_COMMAND_LEN_MAX 260
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000058static const char *dmidecode_command = "dmidecode";
Michael Karcher6701ee82010-01-20 14:14:11 +000059
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000060static char *dmistrings[ARRAY_SIZE(dmidecode_names)];
Michael Karcher6701ee82010-01-20 14:14:11 +000061
Uwe Hermann43959702010-03-13 17:28:29 +000062/* Strings longer than 4096 in DMI are just insane. */
Michael Karcher6701ee82010-01-20 14:14:11 +000063#define DMI_MAX_ANSWER_LEN 4096
64
Michael Karcher0d7fb7c2010-02-26 09:51:16 +000065static char *get_dmi_string(const char *string_name)
Michael Karcher6701ee82010-01-20 14:14:11 +000066{
67 FILE *dmidecode_pipe;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +000068 char *result;
69 char answerbuf[DMI_MAX_ANSWER_LEN];
Uwe Hermann43959702010-03-13 17:28:29 +000070 char commandline[DMI_COMMAND_LEN_MAX + 40];
71
Michael Karcher0d7fb7c2010-02-26 09:51:16 +000072 snprintf(commandline, sizeof(commandline),
73 "%s -s %s", dmidecode_command, string_name);
74 dmidecode_pipe = popen(commandline, "r");
75 if (!dmidecode_pipe) {
Sean Nelson316a29f2010-05-07 20:09:04 +000076 msg_perr("DMI pipe open error\n");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +000077 return NULL;
Michael Karcher6701ee82010-01-20 14:14:11 +000078 }
Michael Karcherd9f266d2010-08-08 21:56:52 +000079
80 /* Kill lines starting with '#', as recent dmidecode versions
81 have the quirk to emit a "# SMBIOS implementations newer..."
82 message even on "-s" if the SMBIOS declares a
83 newer-than-supported version number, while it *should* only print
84 the requested string. */
85 do {
86 if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
87 if(ferror(dmidecode_pipe)) {
88 msg_perr("DMI pipe read error\n");
89 pclose(dmidecode_pipe);
90 return NULL;
91 } else {
92 answerbuf[0] = 0; /* Hit EOF */
93 }
Michael Karcher45f79cb2010-03-24 17:55:04 +000094 }
Michael Karcherd9f266d2010-08-08 21:56:52 +000095 } while(answerbuf[0] == '#');
96
Michael Karcher0d7fb7c2010-02-26 09:51:16 +000097 /* Toss all output above DMI_MAX_ANSWER_LEN away to prevent
98 deadlock on pclose. */
99 while (!feof(dmidecode_pipe))
100 getc(dmidecode_pipe);
101 if (pclose(dmidecode_pipe) != 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000102 msg_pinfo("dmidecode execution unsucessfull - continuing "
103 "without DMI info\n");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000104 return NULL;
105 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000106
Uwe Hermann43959702010-03-13 17:28:29 +0000107 /* Chomp trailing newline. */
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000108 if (answerbuf[0] != 0 &&
109 answerbuf[strlen(answerbuf) - 1] == '\n')
110 answerbuf[strlen(answerbuf) - 1] = 0;
Sean Nelson316a29f2010-05-07 20:09:04 +0000111 msg_pdbg("DMI string %s: \"%s\"\n", string_name, answerbuf);
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000112
113 result = strdup(answerbuf);
114 if (!result)
115 puts("WARNING: Out of memory - DMI support fails");
116
117 return result;
118}
119
120void dmi_init(void)
121{
122 int i;
Michael Karcher8c1df282010-02-26 09:51:20 +0000123 char *chassis_type;
Uwe Hermann43959702010-03-13 17:28:29 +0000124
Michael Karcher6701ee82010-01-20 14:14:11 +0000125 has_dmi_support = 1;
Michael Karcherfd416702010-03-14 17:57:52 +0000126 for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++) {
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000127 dmistrings[i] = get_dmi_string(dmidecode_names[i]);
128 if (!dmistrings[i]) {
129 has_dmi_support = 0;
Michael Karcherfd416702010-03-14 17:57:52 +0000130 return;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000131 }
132 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000133
134 chassis_type = get_dmi_string("chassis-type");
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000135 if (chassis_type && (!strcmp(chassis_type, "Notebook") ||
136 !strcmp(chassis_type, "Portable"))) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000137 msg_pdbg("Laptop detected via DMI\n");
Michael Karcher8c1df282010-02-26 09:51:20 +0000138 is_laptop = 1;
139 }
140 free(chassis_type);
Michael Karcher6701ee82010-01-20 14:14:11 +0000141}
142
143/**
144 * Does an substring/prefix/postfix/whole-string match.
145 *
146 * The pattern is matched as-is. The only metacharacters supported are '^'
147 * at the beginning and '$' at the end. So you can look for "^prefix",
148 * "suffix$", "substring" or "^complete string$".
149 *
150 * @param value The string to check.
151 * @param pattern The pattern.
152 * @return Nonzero if pattern matches.
153 */
154static int dmi_compare(const char *value, const char *pattern)
155{
156 int anchored = 0;
157 int patternlen;
Uwe Hermann43959702010-03-13 17:28:29 +0000158
Sean Nelson316a29f2010-05-07 20:09:04 +0000159 msg_pspew("matching %s against %s\n", value, pattern);
Uwe Hermann43959702010-03-13 17:28:29 +0000160 /* The empty string is part of all strings! */
Michael Karcher6701ee82010-01-20 14:14:11 +0000161 if (pattern[0] == 0)
162 return 1;
163
164 if (pattern[0] == '^') {
165 anchored = 1;
166 pattern++;
167 }
168
169 patternlen = strlen(pattern);
170 if (pattern[patternlen - 1] == '$') {
171 int valuelen = strlen(value);
172 patternlen--;
Uwe Hermann43959702010-03-13 17:28:29 +0000173 if (patternlen > valuelen)
Michael Karcher6701ee82010-01-20 14:14:11 +0000174 return 0;
175
176 /* full string match: require same length */
Uwe Hermann43959702010-03-13 17:28:29 +0000177 if (anchored && (valuelen != patternlen))
Michael Karcher6701ee82010-01-20 14:14:11 +0000178 return 0;
179
180 /* start character to make ends match */
181 value += valuelen - patternlen;
182 anchored = 1;
183 }
184
185 if (anchored)
186 return strncmp(value, pattern, patternlen) == 0;
187 else
188 return strstr(value, pattern) != NULL;
189}
190
191int dmi_match(const char *pattern)
192{
193 int i;
Uwe Hermann43959702010-03-13 17:28:29 +0000194
Michael Karcher6701ee82010-01-20 14:14:11 +0000195 if (!has_dmi_support)
196 return 0;
197
Michael Karcherfd416702010-03-14 17:57:52 +0000198 for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++)
Uwe Hermann43959702010-03-13 17:28:29 +0000199 if (dmi_compare(dmistrings[i], pattern))
Michael Karcher4dfa0932010-02-12 05:44:18 +0000200 return 1;
Michael Karcher6701ee82010-01-20 14:14:11 +0000201
202 return 0;
203}
Carl-Daniel Hailfingere1fdff42010-06-23 23:14:44 +0000204
205#endif /* STANDALONE */