blob: 729cdb119f5c04e37a1d2add1d932dfa777c8a30 [file] [log] [blame]
Michael Karcher6701ee82010-01-20 14:14:11 +00001/*
2 * This file is part of the flashrom project.
3 *
Sean Nelson4c6d3a42013-09-11 23:35:03 +00004 * Copyright (C) 2000-2002 Alan Cox <alan@redhat.com>
5 * Copyright (C) 2002-2010 Jean Delvare <khali@linux-fr.org>
Michael Karcher6701ee82010-01-20 14:14:11 +00006 * Copyright (C) 2009,2010 Michael Karcher
Sean Nelson4c6d3a42013-09-11 23:35:03 +00007 * Copyright (C) 2011-2013 Stefan Tauner
Michael Karcher6701ee82010-01-20 14:14:11 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
Stefan Taunerdc627932015-01-27 18:07:50 +000024/* strnlen is in POSIX but was a GNU extension up to glibc 2.10 */
25#if (__GLIBC__ == 2 && __GLIBC_MINOR__ < 10) || __GLIBC__ < 2
26#define _GNU_SOURCE
27#else
28#define _POSIX_C_SOURCE 200809L
29#endif
30
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000031#include <strings.h>
Michael Karcher6701ee82010-01-20 14:14:11 +000032#include <string.h>
Stefan Tauner9972d152014-07-13 12:52:15 +000033#include <ctype.h>
Michael Karcher6701ee82010-01-20 14:14:11 +000034#include <stdio.h>
35#include <stdlib.h>
36
37#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000038#include "programmer.h"
Michael Karcher6701ee82010-01-20 14:14:11 +000039
Sean Nelson4c6d3a42013-09-11 23:35:03 +000040#if defined(__i386__) || defined(__x86_64__)
41
42/* Enable SMBIOS decoding. Currently legacy DMI decoding is enough. */
43#define SM_SUPPORT 0
44
45/* Strings longer than 4096 in DMI are just insane. */
46#define DMI_MAX_ANSWER_LEN 4096
47
Carl-Daniel Hailfingere1fdff42010-06-23 23:14:44 +000048int has_dmi_support = 0;
49
Sean Nelson4c6d3a42013-09-11 23:35:03 +000050static struct {
51 const char *const keyword;
52 const uint8_t type;
53 const uint8_t offset;
54 char *value;
55} dmi_strings[] = {
56 { "system-manufacturer", 1, 0x04, NULL },
57 { "system-product-name", 1, 0x05, NULL },
58 { "system-version", 1, 0x06, NULL },
59 { "baseboard-manufacturer", 2, 0x04, NULL },
60 { "baseboard-product-name", 2, 0x05, NULL },
61 { "baseboard-version", 2, 0x06, NULL },
Michael Karcher6701ee82010-01-20 14:14:11 +000062};
63
Stefan Taunera34d7192011-07-26 00:54:42 +000064/* This list is used to identify supposed laptops. The is_laptop field has the
65 * following meaning:
66 * - 0: in all likelihood not a laptop
67 * - 1: in all likelihood a laptop
68 * - 2: chassis-type is not specific enough
69 * A full list of chassis types can be found in the System Management BIOS
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000070 * (SMBIOS) Reference Specification 2.7.0 section 7.4.1 "Chassis Types" at
71 * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf
72 * The types below are the most common ones.
73 */
74static const struct {
Sean Nelson4c6d3a42013-09-11 23:35:03 +000075 uint8_t type;
76 uint8_t is_laptop;
77 char *name;
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000078} dmi_chassis_types[] = {
Stefan Taunera34d7192011-07-26 00:54:42 +000079 {0x01, 2, "Other"},
80 {0x02, 2, "Unknown"},
Stefan Tauner4c5665f2012-02-17 20:03:37 +000081 {0x03, 0, "Desktop"},
82 {0x04, 0, "Low Profile Desktop"},
Stefan Taunera34d7192011-07-26 00:54:42 +000083 {0x06, 0, "Mini Tower"},
84 {0x07, 0, "Tower"},
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000085 {0x08, 1, "Portable"},
86 {0x09, 1, "Laptop"},
87 {0x0a, 1, "Notebook"},
88 {0x0b, 1, "Hand Held"},
89 {0x0e, 1, "Sub Notebook"},
Stefan Taunera34d7192011-07-26 00:54:42 +000090 {0x11, 0, "Main Server Chassis"},
91 {0x17, 0, "Rack Mount Chassis"},
Sylvain "ythier" Hitier3093f8f2011-09-03 11:22:27 +000092 {0x18, 0, "Sealed-case PC"}, /* used by Supermicro (X8SIE) */
Stefan Tauner23e10b82016-01-23 16:16:49 +000093 {0x19, 0, "Multi-system"}, /* used by Supermicro (X7DWT) */
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000094};
95
Sean Nelson4c6d3a42013-09-11 23:35:03 +000096#if CONFIG_INTERNAL_DMI == 1
Sean Nelson4c6d3a42013-09-11 23:35:03 +000097static bool dmi_checksum(const uint8_t * const buf, size_t len)
98{
99 uint8_t sum = 0;
100 size_t a;
101
102 for (a = 0; a < len; a++)
103 sum += buf[a];
104 return (sum == 0);
105}
106
Stefan Tauner9972d152014-07-13 12:52:15 +0000107/** Retrieve a DMI string.
108 *
109 * See SMBIOS spec. section 6.1.3 "Text strings".
110 * The table will be unmapped ASAP, hence return a duplicated & sanitized string that needs to be freed later.
111 *
112 * \param buf the buffer to search through (usually appended directly to a DMI structure)
113 * \param string_id index of the string to look for
114 * \param limit pointer to the first byte beyond \em buf
115 */
116static char *dmi_string(const char *buf, uint8_t string_id, const char *limit)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000117{
118 size_t i, len;
119
120 if (string_id == 0)
Stefan Tauner778c6d82014-09-05 16:14:11 +0000121 return strdup("Not Specified");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000122
123 while (string_id > 1 && string_id--) {
Stefan Tauner9972d152014-07-13 12:52:15 +0000124 if (buf >= limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000125 msg_perr("DMI table is broken (string portion out of bounds)!\n");
Stefan Tauner778c6d82014-09-05 16:14:11 +0000126 return strdup("<OUT OF BOUNDS>");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000127 }
Stefan Tauner9972d152014-07-13 12:52:15 +0000128 buf += strnlen(buf, limit - buf) + 1;
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000129 }
130
131 if (!*buf) /* as long as the current byte we're on isn't null */
Stefan Tauner778c6d82014-09-05 16:14:11 +0000132 return strdup("<BAD INDEX>");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000133
Stefan Tauner9972d152014-07-13 12:52:15 +0000134 len = strnlen(buf, limit - buf);
135 char *newbuf = malloc(len + 1);
136 if (newbuf == NULL) {
137 msg_perr("Out of memory!\n");
138 return NULL;
139 }
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000140
141 /* fix junk bytes in the string */
Stefan Tauner9972d152014-07-13 12:52:15 +0000142 for (i = 0; i < len && buf[i] != '\0'; i++) {
Stefan Taunerff9e6c32014-10-19 07:54:27 +0000143 if (isprint((unsigned char)buf[i]))
Stefan Tauner9972d152014-07-13 12:52:15 +0000144 newbuf[i] = buf[i];
145 else
146 newbuf[i] = ' ';
147 }
148 newbuf[i] = '\0';
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000149
Stefan Tauner9972d152014-07-13 12:52:15 +0000150 return newbuf;
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000151}
152
153static void dmi_chassis_type(uint8_t code)
154{
155 int i;
156 code &= 0x7f; /* bits 6:0 are chassis type, 7th bit is the lock bit */
157 is_laptop = 2;
158 for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
159 if (code == dmi_chassis_types[i].type) {
160 msg_pdbg("DMI string chassis-type: \"%s\"\n", dmi_chassis_types[i].name);
161 is_laptop = dmi_chassis_types[i].is_laptop;
162 break;
163 }
164 }
165}
166
167static void dmi_table(uint32_t base, uint16_t len, uint16_t num)
168{
169 int i = 0, j = 0;
170
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000171 uint8_t *dmi_table_mem = physmap_ro("DMI Table", base, len);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000172 if (dmi_table_mem == NULL) {
173 msg_perr("Unable to access DMI Table\n");
174 return;
175 }
176
177 uint8_t *data = dmi_table_mem;
178 uint8_t *limit = dmi_table_mem + len;
179
180 /* SMBIOS structure header is always 4 B long and contains:
181 * - uint8_t type; // see dmi_chassis_types's type
182 * - uint8_t length; // data section w/ header w/o strings
183 * - uint16_t handle;
184 */
Stefan Tauner9972d152014-07-13 12:52:15 +0000185 while (i < num && data + 4 < limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000186 /* - If a short entry is found (less than 4 bytes), not only it
187 * is invalid, but we cannot reliably locate the next entry.
188 * - If the length value indicates that this structure spreads
Stefan Tauner6697f712014-08-06 15:09:15 +0000189 * across the table border, something is fishy too.
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000190 * Better stop at this point, and let the user know his/her
191 * table is broken.
192 */
Stefan Tauner9972d152014-07-13 12:52:15 +0000193 if (data[1] < 4 || data + data[1] >= limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000194 msg_perr("DMI table is broken (bogus header)!\n");
195 break;
196 }
197
198 if(data[0] == 3) {
Stefan Tauner9972d152014-07-13 12:52:15 +0000199 if (data + 5 < limit)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000200 dmi_chassis_type(data[5]);
Stefan Tauner9972d152014-07-13 12:52:15 +0000201 else /* the table is broken, but laptop detection is optional, hence continue. */
202 msg_pwarn("DMI table is broken (chassis_type out of bounds)!\n");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000203 } else
204 for (j = 0; j < ARRAY_SIZE(dmi_strings); j++) {
205 uint8_t offset = dmi_strings[j].offset;
206 uint8_t type = dmi_strings[j].type;
207
208 if (data[0] != type)
209 continue;
210
Stefan Tauner9972d152014-07-13 12:52:15 +0000211 if (data[1] <= offset || data + offset >= limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000212 msg_perr("DMI table is broken (offset out of bounds)!\n");
213 goto out;
214 }
215
Stefan Tauner9972d152014-07-13 12:52:15 +0000216 dmi_strings[j].value = dmi_string((const char *)(data + data[1]), data[offset],
217 (const char *)limit);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000218 }
219 /* Find next structure by skipping data and string sections */
220 data += data[1];
221 while (data + 1 <= limit) {
222 if (data[0] == 0 && data[1] == 0)
223 break;
224 data++;
225 }
226 data += 2;
227 i++;
228 }
229out:
230 physunmap(dmi_table_mem, len);
231}
232
233#if SM_SUPPORT
234static int smbios_decode(uint8_t *buf)
235{
236 /* TODO: other checks mentioned in the conformance guidelines? */
237 if (!dmi_checksum(buf, buf[0x05]) ||
238 (memcmp(buf + 0x10, "_DMI_", 5) != 0) ||
239 !dmi_checksum(buf + 0x10, 0x0F))
240 return 0;
241
242 dmi_table(mmio_readl(buf + 0x18), mmio_readw(buf + 0x16), mmio_readw(buf + 0x1C));
243
244 return 1;
245}
246#endif
247
248static int legacy_decode(uint8_t *buf)
249{
250 if (!dmi_checksum(buf, 0x0F))
251 return 1;
252
253 dmi_table(mmio_readl(buf + 0x08), mmio_readw(buf + 0x06), mmio_readw(buf + 0x0C));
254
255 return 0;
256}
257
258int dmi_fill(void)
259{
260 size_t fp;
261 uint8_t *dmi_mem;
262 int ret = 1;
263
264 msg_pdbg("Using Internal DMI decoder.\n");
265 /* There are two ways specified to gain access to the SMBIOS table:
266 * - EFI's configuration table contains a pointer to the SMBIOS table. On linux it can be obtained from
267 * sysfs. EFI's SMBIOS GUID is: {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,0x0,0x90,0x27,0x3f,0xc1,0x4d}
268 * - Scanning physical memory address range 0x000F0000h to 0x000FFFFF for the anchor-string(s). */
Niklas Söderlund5d307202013-09-14 09:02:27 +0000269 dmi_mem = physmap_ro("DMI", 0xF0000, 0x10000);
270 if (dmi_mem == ERROR_PTR)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000271 return ret;
272
273 for (fp = 0; fp <= 0xFFF0; fp += 16) {
274#if SM_SUPPORT
275 if (memcmp(dmi_mem + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
276 if (smbios_decode(dmi_mem + fp)) // FIXME: length check
277 goto out;
278 } else
279#endif
280 if (memcmp(dmi_mem + fp, "_DMI_", 5) == 0)
281 if (legacy_decode(dmi_mem + fp) == 0) {
282 ret = 0;
283 goto out;
284 }
285 }
286 msg_pinfo("No DMI table found.\n");
287out:
288 physunmap(dmi_mem, 0x10000);
289 return ret;
290}
291
292#else /* CONFIG_INTERNAL_DMI */
293
294#define DMI_COMMAND_LEN_MAX 300
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000295static const char *dmidecode_command = "dmidecode";
Michael Karcher6701ee82010-01-20 14:14:11 +0000296
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000297static char *get_dmi_string(const char *string_name)
Michael Karcher6701ee82010-01-20 14:14:11 +0000298{
299 FILE *dmidecode_pipe;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000300 char *result;
301 char answerbuf[DMI_MAX_ANSWER_LEN];
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000302 char commandline[DMI_COMMAND_LEN_MAX];
Uwe Hermann43959702010-03-13 17:28:29 +0000303
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000304 snprintf(commandline, sizeof(commandline),
305 "%s -s %s", dmidecode_command, string_name);
306 dmidecode_pipe = popen(commandline, "r");
307 if (!dmidecode_pipe) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000308 msg_perr("Opening DMI pipe failed!\n");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000309 return NULL;
Michael Karcher6701ee82010-01-20 14:14:11 +0000310 }
Michael Karcherd9f266d2010-08-08 21:56:52 +0000311
312 /* Kill lines starting with '#', as recent dmidecode versions
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000313 * have the quirk to emit a "# SMBIOS implementations newer..."
314 * message even on "-s" if the SMBIOS declares a
315 * newer-than-supported version number, while it *should* only print
316 * the requested string.
317 */
Michael Karcherd9f266d2010-08-08 21:56:52 +0000318 do {
319 if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000320 if (ferror(dmidecode_pipe)) {
Michael Karcherd9f266d2010-08-08 21:56:52 +0000321 msg_perr("DMI pipe read error\n");
322 pclose(dmidecode_pipe);
323 return NULL;
324 } else {
325 answerbuf[0] = 0; /* Hit EOF */
326 }
Michael Karcher45f79cb2010-03-24 17:55:04 +0000327 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000328 } while (answerbuf[0] == '#');
Michael Karcherd9f266d2010-08-08 21:56:52 +0000329
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000330 /* Discard all output exceeding DMI_MAX_ANSWER_LEN to prevent deadlock on pclose. */
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000331 while (!feof(dmidecode_pipe))
332 getc(dmidecode_pipe);
333 if (pclose(dmidecode_pipe) != 0) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000334 msg_pwarn("dmidecode execution unsuccessful - continuing without DMI info\n");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000335 return NULL;
336 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000337
Uwe Hermann43959702010-03-13 17:28:29 +0000338 /* Chomp trailing newline. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000339 if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n')
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000340 answerbuf[strlen(answerbuf) - 1] = 0;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000341
342 result = strdup(answerbuf);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000343 if (result == NULL)
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000344 msg_pwarn("Warning: Out of memory - DMI support fails");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000345
346 return result;
347}
348
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000349int dmi_fill(void)
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000350{
351 int i;
Michael Karcher8c1df282010-02-26 09:51:20 +0000352 char *chassis_type;
Uwe Hermann43959702010-03-13 17:28:29 +0000353
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000354 msg_pdbg("Using External DMI decoder.\n");
355 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
356 dmi_strings[i].value = get_dmi_string(dmi_strings[i].keyword);
357 if (dmi_strings[i].value == NULL)
358 return 1;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000359 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000360
361 chassis_type = get_dmi_string("chassis-type");
Stefan Taunera34d7192011-07-26 00:54:42 +0000362 if (chassis_type == NULL)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000363 return 0; /* chassis-type handling is optional anyway */
Stefan Taunera34d7192011-07-26 00:54:42 +0000364
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000365 msg_pdbg("DMI string chassis-type: \"%s\"\n", chassis_type);
Stefan Taunera34d7192011-07-26 00:54:42 +0000366 is_laptop = 2;
367 for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
368 if (strcasecmp(chassis_type, dmi_chassis_types[i].name) == 0) {
369 is_laptop = dmi_chassis_types[i].is_laptop;
370 break;
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +0000371 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000372 }
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000373 free(chassis_type);
374 return 0;
375}
376
377#endif /* CONFIG_INTERNAL_DMI */
378
379static int dmi_shutdown(void *data)
380{
381 int i;
382 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
383 free(dmi_strings[i].value);
384 dmi_strings[i].value = NULL;
385 }
386 return 0;
387}
388
389void dmi_init(void)
390{
391 /* Register shutdown function before we allocate anything. */
392 if (register_shutdown(dmi_shutdown, NULL)) {
393 msg_pwarn("Warning: Could not register DMI shutdown function - continuing without DMI info.\n");
394 return;
395 }
396
397 /* dmi_fill fills the dmi_strings array, and if possible sets the global is_laptop variable. */
398 if (dmi_fill() != 0)
399 return;
Stefan Taunera34d7192011-07-26 00:54:42 +0000400
401 switch (is_laptop) {
402 case 1:
403 msg_pdbg("Laptop detected via DMI.\n");
404 break;
405 case 2:
406 msg_pdbg("DMI chassis-type is not specific enough.\n");
407 break;
408 }
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000409
410 has_dmi_support = 1;
411 int i;
412 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
413 msg_pdbg("DMI string %s: \"%s\"\n", dmi_strings[i].keyword,
414 (dmi_strings[i].value == NULL) ? "" : dmi_strings[i].value);
415 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000416}
417
418/**
419 * Does an substring/prefix/postfix/whole-string match.
420 *
421 * The pattern is matched as-is. The only metacharacters supported are '^'
422 * at the beginning and '$' at the end. So you can look for "^prefix",
423 * "suffix$", "substring" or "^complete string$".
424 *
Stefan Taunerd1045d82013-10-29 01:38:45 +0000425 * @param value The non-NULL string to check.
426 * @param pattern The non-NULL pattern.
Michael Karcher6701ee82010-01-20 14:14:11 +0000427 * @return Nonzero if pattern matches.
428 */
429static int dmi_compare(const char *value, const char *pattern)
430{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000431 int anchored = 0;
432 int patternlen;
Uwe Hermann43959702010-03-13 17:28:29 +0000433
Sean Nelson316a29f2010-05-07 20:09:04 +0000434 msg_pspew("matching %s against %s\n", value, pattern);
Uwe Hermann43959702010-03-13 17:28:29 +0000435 /* The empty string is part of all strings! */
Michael Karcher6701ee82010-01-20 14:14:11 +0000436 if (pattern[0] == 0)
437 return 1;
438
439 if (pattern[0] == '^') {
440 anchored = 1;
441 pattern++;
442 }
443
444 patternlen = strlen(pattern);
445 if (pattern[patternlen - 1] == '$') {
446 int valuelen = strlen(value);
447 patternlen--;
Uwe Hermann43959702010-03-13 17:28:29 +0000448 if (patternlen > valuelen)
Michael Karcher6701ee82010-01-20 14:14:11 +0000449 return 0;
450
451 /* full string match: require same length */
Uwe Hermann43959702010-03-13 17:28:29 +0000452 if (anchored && (valuelen != patternlen))
Michael Karcher6701ee82010-01-20 14:14:11 +0000453 return 0;
454
455 /* start character to make ends match */
456 value += valuelen - patternlen;
457 anchored = 1;
458 }
459
460 if (anchored)
461 return strncmp(value, pattern, patternlen) == 0;
462 else
463 return strstr(value, pattern) != NULL;
464}
465
466int dmi_match(const char *pattern)
467{
468 int i;
Uwe Hermann43959702010-03-13 17:28:29 +0000469
Michael Karcher6701ee82010-01-20 14:14:11 +0000470 if (!has_dmi_support)
471 return 0;
472
Stefan Taunerd1045d82013-10-29 01:38:45 +0000473 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
474 if (dmi_strings[i].value == NULL)
475 continue;
476
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000477 if (dmi_compare(dmi_strings[i].value, pattern))
Michael Karcher4dfa0932010-02-12 05:44:18 +0000478 return 1;
Stefan Taunerd1045d82013-10-29 01:38:45 +0000479 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000480
481 return 0;
482}
Carl-Daniel Hailfingere1fdff42010-06-23 23:14:44 +0000483
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000484#endif // defined(__i386__) || defined(__x86_64__)