blob: c80a5683dd41abbbba61565e31fbb6b1f0011308 [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
57void dmi_init(void)
58{
59 FILE *dmidecode_pipe;
60 int i;
61 char *answerbuf = malloc(DMI_MAX_ANSWER_LEN);
62 if(!answerbuf)
63 {
64 fprintf(stderr, "DMI: couldn't alloc answer buffer\n");
65 return;
66 }
67 for (i = 0; i < DMI_ID_INVALID; i++)
68 {
69 char commandline[DMI_COMMAND_LEN_MAX+40];
70 snprintf(commandline, sizeof(commandline),
Michael Karcherf6498d72010-02-24 22:43:44 +000071 "%s -s %s", dmidecode_command, dmidecode_names[i]);
Michael Karcher6701ee82010-01-20 14:14:11 +000072 dmidecode_pipe = popen(commandline, "r");
73 if (!dmidecode_pipe)
74 {
75 printf_debug("DMI pipe open error\n");
76 goto out_free;
77 }
Michael Karcher823dc9d2010-01-27 10:08:33 +000078 if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe) &&
79 ferror(dmidecode_pipe))
Michael Karcher6701ee82010-01-20 14:14:11 +000080 {
81 printf_debug("DMI pipe read error\n");
82 pclose(dmidecode_pipe);
83 goto out_free;
84 }
85 /* Toss all output above DMI_MAX_ANSWER_LEN away to prevent
86 deadlock on pclose. */
87 while (!feof(dmidecode_pipe))
88 getc(dmidecode_pipe);
89 if (pclose(dmidecode_pipe) != 0)
90 {
91 printf_debug("DMI pipe close error\n");
92 goto out_free;
93 }
94
95 /* chomp trailing newline */
96 if (answerbuf[0] != 0 &&
97 answerbuf[strlen(answerbuf) - 1] == '\n')
98 answerbuf[strlen(answerbuf) - 1] = 0;
Michael Karcherf6498d72010-02-24 22:43:44 +000099 printf_debug("DMI string %s: \"%s\"\n", dmidecode_names[i],
100 answerbuf);
Michael Karcher6701ee82010-01-20 14:14:11 +0000101 dmistrings[i] = strdup(answerbuf);
102 }
103 has_dmi_support = 1;
104out_free:
105 free(answerbuf);
106}
107
108/**
109 * Does an substring/prefix/postfix/whole-string match.
110 *
111 * The pattern is matched as-is. The only metacharacters supported are '^'
112 * at the beginning and '$' at the end. So you can look for "^prefix",
113 * "suffix$", "substring" or "^complete string$".
114 *
115 * @param value The string to check.
116 * @param pattern The pattern.
117 * @return Nonzero if pattern matches.
118 */
119static int dmi_compare(const char *value, const char *pattern)
120{
121 int anchored = 0;
122 int patternlen;
123 printf_debug("matching %s against %s\n", value, pattern);
124 /* The empty string is part of all strings */
125 if (pattern[0] == 0)
126 return 1;
127
128 if (pattern[0] == '^') {
129 anchored = 1;
130 pattern++;
131 }
132
133 patternlen = strlen(pattern);
134 if (pattern[patternlen - 1] == '$') {
135 int valuelen = strlen(value);
136 patternlen--;
137 if(patternlen > valuelen)
138 return 0;
139
140 /* full string match: require same length */
141 if(anchored && (valuelen != patternlen))
142 return 0;
143
144 /* start character to make ends match */
145 value += valuelen - patternlen;
146 anchored = 1;
147 }
148
149 if (anchored)
150 return strncmp(value, pattern, patternlen) == 0;
151 else
152 return strstr(value, pattern) != NULL;
153}
154
155int dmi_match(const char *pattern)
156{
157 int i;
158 if (!has_dmi_support)
159 return 0;
160
161 for (i = 0;i < DMI_ID_INVALID; i++)
Michael Karcher4dfa0932010-02-12 05:44:18 +0000162 if(dmi_compare(dmistrings[i], pattern))
163 return 1;
Michael Karcher6701ee82010-01-20 14:14:11 +0000164
165 return 0;
166}