blob: 30a4670360a425a6c476f3d54b3fbf51f075f00a [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 Karcher8c1df282010-02-26 09:51:20 +0000101 char *chassis_type;
Michael Karcher6701ee82010-01-20 14:14:11 +0000102 has_dmi_support = 1;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000103 for (i = 0; i < DMI_ID_INVALID; i++) {
104 dmistrings[i] = get_dmi_string(dmidecode_names[i]);
105 if (!dmistrings[i]) {
106 has_dmi_support = 0;
107 break;
108 }
109 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000110
111 chassis_type = get_dmi_string("chassis-type");
112 if (chassis_type && !strcmp(chassis_type, "Notebook")) {
113 printf_debug("Laptop detected via DMI");
114 is_laptop = 1;
115 }
116 free(chassis_type);
Michael Karcher6701ee82010-01-20 14:14:11 +0000117}
118
119/**
120 * Does an substring/prefix/postfix/whole-string match.
121 *
122 * The pattern is matched as-is. The only metacharacters supported are '^'
123 * at the beginning and '$' at the end. So you can look for "^prefix",
124 * "suffix$", "substring" or "^complete string$".
125 *
126 * @param value The string to check.
127 * @param pattern The pattern.
128 * @return Nonzero if pattern matches.
129 */
130static int dmi_compare(const char *value, const char *pattern)
131{
132 int anchored = 0;
133 int patternlen;
134 printf_debug("matching %s against %s\n", value, pattern);
135 /* The empty string is part of all strings */
136 if (pattern[0] == 0)
137 return 1;
138
139 if (pattern[0] == '^') {
140 anchored = 1;
141 pattern++;
142 }
143
144 patternlen = strlen(pattern);
145 if (pattern[patternlen - 1] == '$') {
146 int valuelen = strlen(value);
147 patternlen--;
148 if(patternlen > valuelen)
149 return 0;
150
151 /* full string match: require same length */
152 if(anchored && (valuelen != patternlen))
153 return 0;
154
155 /* start character to make ends match */
156 value += valuelen - patternlen;
157 anchored = 1;
158 }
159
160 if (anchored)
161 return strncmp(value, pattern, patternlen) == 0;
162 else
163 return strstr(value, pattern) != NULL;
164}
165
166int dmi_match(const char *pattern)
167{
168 int i;
169 if (!has_dmi_support)
170 return 0;
171
172 for (i = 0;i < DMI_ID_INVALID; i++)
Michael Karcher4dfa0932010-02-12 05:44:18 +0000173 if(dmi_compare(dmistrings[i], pattern))
174 return 1;
Michael Karcher6701ee82010-01-20 14:14:11 +0000175
176 return 0;
177}