blob: 70b117749e3e47316c9bae80107cedcbb0da73ee [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
Stefan Tauner4f444792011-06-27 11:44:03 +000037#include "platform.h"
Michael Karcher6701ee82010-01-20 14:14:11 +000038#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000039#include "programmer.h"
Michael Karcher6701ee82010-01-20 14:14:11 +000040
Sean Nelson4c6d3a42013-09-11 23:35:03 +000041#if defined(__i386__) || defined(__x86_64__)
42
43/* Enable SMBIOS decoding. Currently legacy DMI decoding is enough. */
44#define SM_SUPPORT 0
45
46/* Strings longer than 4096 in DMI are just insane. */
47#define DMI_MAX_ANSWER_LEN 4096
48
Carl-Daniel Hailfingere1fdff42010-06-23 23:14:44 +000049int has_dmi_support = 0;
50
Sean Nelson4c6d3a42013-09-11 23:35:03 +000051static struct {
52 const char *const keyword;
53 const uint8_t type;
54 const uint8_t offset;
55 char *value;
56} dmi_strings[] = {
57 { "system-manufacturer", 1, 0x04, NULL },
58 { "system-product-name", 1, 0x05, NULL },
59 { "system-version", 1, 0x06, NULL },
60 { "baseboard-manufacturer", 2, 0x04, NULL },
61 { "baseboard-product-name", 2, 0x05, NULL },
62 { "baseboard-version", 2, 0x06, NULL },
Michael Karcher6701ee82010-01-20 14:14:11 +000063};
64
Stefan Taunera34d7192011-07-26 00:54:42 +000065/* This list is used to identify supposed laptops. The is_laptop field has the
66 * following meaning:
67 * - 0: in all likelihood not a laptop
68 * - 1: in all likelihood a laptop
69 * - 2: chassis-type is not specific enough
70 * A full list of chassis types can be found in the System Management BIOS
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000071 * (SMBIOS) Reference Specification 2.7.0 section 7.4.1 "Chassis Types" at
72 * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf
73 * The types below are the most common ones.
74 */
75static const struct {
Sean Nelson4c6d3a42013-09-11 23:35:03 +000076 uint8_t type;
77 uint8_t is_laptop;
78 char *name;
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000079} dmi_chassis_types[] = {
Stefan Taunera34d7192011-07-26 00:54:42 +000080 {0x01, 2, "Other"},
81 {0x02, 2, "Unknown"},
Stefan Tauner4c5665f2012-02-17 20:03:37 +000082 {0x03, 0, "Desktop"},
83 {0x04, 0, "Low Profile Desktop"},
Stefan Taunera34d7192011-07-26 00:54:42 +000084 {0x06, 0, "Mini Tower"},
85 {0x07, 0, "Tower"},
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000086 {0x08, 1, "Portable"},
87 {0x09, 1, "Laptop"},
88 {0x0a, 1, "Notebook"},
89 {0x0b, 1, "Hand Held"},
90 {0x0e, 1, "Sub Notebook"},
Stefan Taunera34d7192011-07-26 00:54:42 +000091 {0x11, 0, "Main Server Chassis"},
92 {0x17, 0, "Rack Mount Chassis"},
Sylvain "ythier" Hitier3093f8f2011-09-03 11:22:27 +000093 {0x18, 0, "Sealed-case PC"}, /* used by Supermicro (X8SIE) */
Stefan Tauner23e10b82016-01-23 16:16:49 +000094 {0x19, 0, "Multi-system"}, /* used by Supermicro (X7DWT) */
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000095};
96
Sean Nelson4c6d3a42013-09-11 23:35:03 +000097#if CONFIG_INTERNAL_DMI == 1
Sean Nelson4c6d3a42013-09-11 23:35:03 +000098static bool dmi_checksum(const uint8_t * const buf, size_t len)
99{
100 uint8_t sum = 0;
101 size_t a;
102
103 for (a = 0; a < len; a++)
104 sum += buf[a];
105 return (sum == 0);
106}
107
Stefan Tauner9972d152014-07-13 12:52:15 +0000108/** Retrieve a DMI string.
109 *
110 * See SMBIOS spec. section 6.1.3 "Text strings".
111 * The table will be unmapped ASAP, hence return a duplicated & sanitized string that needs to be freed later.
112 *
113 * \param buf the buffer to search through (usually appended directly to a DMI structure)
114 * \param string_id index of the string to look for
115 * \param limit pointer to the first byte beyond \em buf
116 */
117static char *dmi_string(const char *buf, uint8_t string_id, const char *limit)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000118{
119 size_t i, len;
120
121 if (string_id == 0)
Stefan Tauner778c6d82014-09-05 16:14:11 +0000122 return strdup("Not Specified");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000123
124 while (string_id > 1 && string_id--) {
Stefan Tauner9972d152014-07-13 12:52:15 +0000125 if (buf >= limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000126 msg_perr("DMI table is broken (string portion out of bounds)!\n");
Stefan Tauner778c6d82014-09-05 16:14:11 +0000127 return strdup("<OUT OF BOUNDS>");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000128 }
Stefan Tauner9972d152014-07-13 12:52:15 +0000129 buf += strnlen(buf, limit - buf) + 1;
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000130 }
131
132 if (!*buf) /* as long as the current byte we're on isn't null */
Stefan Tauner778c6d82014-09-05 16:14:11 +0000133 return strdup("<BAD INDEX>");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000134
Stefan Tauner9972d152014-07-13 12:52:15 +0000135 len = strnlen(buf, limit - buf);
136 char *newbuf = malloc(len + 1);
137 if (newbuf == NULL) {
138 msg_perr("Out of memory!\n");
139 return NULL;
140 }
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000141
142 /* fix junk bytes in the string */
Stefan Tauner9972d152014-07-13 12:52:15 +0000143 for (i = 0; i < len && buf[i] != '\0'; i++) {
Stefan Taunerff9e6c32014-10-19 07:54:27 +0000144 if (isprint((unsigned char)buf[i]))
Stefan Tauner9972d152014-07-13 12:52:15 +0000145 newbuf[i] = buf[i];
146 else
147 newbuf[i] = ' ';
148 }
149 newbuf[i] = '\0';
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000150
Stefan Tauner9972d152014-07-13 12:52:15 +0000151 return newbuf;
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000152}
153
154static void dmi_chassis_type(uint8_t code)
155{
156 int i;
157 code &= 0x7f; /* bits 6:0 are chassis type, 7th bit is the lock bit */
158 is_laptop = 2;
159 for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
160 if (code == dmi_chassis_types[i].type) {
161 msg_pdbg("DMI string chassis-type: \"%s\"\n", dmi_chassis_types[i].name);
162 is_laptop = dmi_chassis_types[i].is_laptop;
163 break;
164 }
165 }
166}
167
168static void dmi_table(uint32_t base, uint16_t len, uint16_t num)
169{
170 int i = 0, j = 0;
171
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000172 uint8_t *dmi_table_mem = physmap_ro("DMI Table", base, len);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000173 if (dmi_table_mem == NULL) {
174 msg_perr("Unable to access DMI Table\n");
175 return;
176 }
177
178 uint8_t *data = dmi_table_mem;
179 uint8_t *limit = dmi_table_mem + len;
180
181 /* SMBIOS structure header is always 4 B long and contains:
182 * - uint8_t type; // see dmi_chassis_types's type
183 * - uint8_t length; // data section w/ header w/o strings
184 * - uint16_t handle;
185 */
Stefan Tauner9972d152014-07-13 12:52:15 +0000186 while (i < num && data + 4 < limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000187 /* - If a short entry is found (less than 4 bytes), not only it
188 * is invalid, but we cannot reliably locate the next entry.
189 * - If the length value indicates that this structure spreads
Stefan Tauner6697f712014-08-06 15:09:15 +0000190 * across the table border, something is fishy too.
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000191 * Better stop at this point, and let the user know his/her
192 * table is broken.
193 */
Stefan Tauner9972d152014-07-13 12:52:15 +0000194 if (data[1] < 4 || data + data[1] >= limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000195 msg_perr("DMI table is broken (bogus header)!\n");
196 break;
197 }
198
199 if(data[0] == 3) {
Stefan Tauner9972d152014-07-13 12:52:15 +0000200 if (data + 5 < limit)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000201 dmi_chassis_type(data[5]);
Stefan Tauner9972d152014-07-13 12:52:15 +0000202 else /* the table is broken, but laptop detection is optional, hence continue. */
203 msg_pwarn("DMI table is broken (chassis_type out of bounds)!\n");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000204 } else
205 for (j = 0; j < ARRAY_SIZE(dmi_strings); j++) {
206 uint8_t offset = dmi_strings[j].offset;
207 uint8_t type = dmi_strings[j].type;
208
209 if (data[0] != type)
210 continue;
211
Stefan Tauner9972d152014-07-13 12:52:15 +0000212 if (data[1] <= offset || data + offset >= limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000213 msg_perr("DMI table is broken (offset out of bounds)!\n");
214 goto out;
215 }
216
Stefan Tauner9972d152014-07-13 12:52:15 +0000217 dmi_strings[j].value = dmi_string((const char *)(data + data[1]), data[offset],
218 (const char *)limit);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000219 }
220 /* Find next structure by skipping data and string sections */
221 data += data[1];
222 while (data + 1 <= limit) {
223 if (data[0] == 0 && data[1] == 0)
224 break;
225 data++;
226 }
227 data += 2;
228 i++;
229 }
230out:
231 physunmap(dmi_table_mem, len);
232}
233
234#if SM_SUPPORT
235static int smbios_decode(uint8_t *buf)
236{
237 /* TODO: other checks mentioned in the conformance guidelines? */
238 if (!dmi_checksum(buf, buf[0x05]) ||
239 (memcmp(buf + 0x10, "_DMI_", 5) != 0) ||
240 !dmi_checksum(buf + 0x10, 0x0F))
241 return 0;
242
243 dmi_table(mmio_readl(buf + 0x18), mmio_readw(buf + 0x16), mmio_readw(buf + 0x1C));
244
245 return 1;
246}
247#endif
248
249static int legacy_decode(uint8_t *buf)
250{
251 if (!dmi_checksum(buf, 0x0F))
252 return 1;
253
254 dmi_table(mmio_readl(buf + 0x08), mmio_readw(buf + 0x06), mmio_readw(buf + 0x0C));
255
256 return 0;
257}
258
259int dmi_fill(void)
260{
261 size_t fp;
262 uint8_t *dmi_mem;
263 int ret = 1;
264
265 msg_pdbg("Using Internal DMI decoder.\n");
266 /* There are two ways specified to gain access to the SMBIOS table:
267 * - EFI's configuration table contains a pointer to the SMBIOS table. On linux it can be obtained from
268 * sysfs. EFI's SMBIOS GUID is: {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,0x0,0x90,0x27,0x3f,0xc1,0x4d}
269 * - Scanning physical memory address range 0x000F0000h to 0x000FFFFF for the anchor-string(s). */
Niklas Söderlund5d307202013-09-14 09:02:27 +0000270 dmi_mem = physmap_ro("DMI", 0xF0000, 0x10000);
271 if (dmi_mem == ERROR_PTR)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000272 return ret;
273
274 for (fp = 0; fp <= 0xFFF0; fp += 16) {
275#if SM_SUPPORT
276 if (memcmp(dmi_mem + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
277 if (smbios_decode(dmi_mem + fp)) // FIXME: length check
278 goto out;
279 } else
280#endif
281 if (memcmp(dmi_mem + fp, "_DMI_", 5) == 0)
282 if (legacy_decode(dmi_mem + fp) == 0) {
283 ret = 0;
284 goto out;
285 }
286 }
287 msg_pinfo("No DMI table found.\n");
288out:
289 physunmap(dmi_mem, 0x10000);
290 return ret;
291}
292
293#else /* CONFIG_INTERNAL_DMI */
294
295#define DMI_COMMAND_LEN_MAX 300
Stefan Tauner4f444792011-06-27 11:44:03 +0000296#if IS_WINDOWS
297static const char *dmidecode_command = "dmidecode.exe 2>NUL";
298#else
299static const char *dmidecode_command = "dmidecode 2>/dev/null";
300#endif
Michael Karcher6701ee82010-01-20 14:14:11 +0000301
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000302static char *get_dmi_string(const char *string_name)
Michael Karcher6701ee82010-01-20 14:14:11 +0000303{
304 FILE *dmidecode_pipe;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000305 char *result;
306 char answerbuf[DMI_MAX_ANSWER_LEN];
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000307 char commandline[DMI_COMMAND_LEN_MAX];
Uwe Hermann43959702010-03-13 17:28:29 +0000308
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000309 snprintf(commandline, sizeof(commandline),
310 "%s -s %s", dmidecode_command, string_name);
311 dmidecode_pipe = popen(commandline, "r");
312 if (!dmidecode_pipe) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000313 msg_perr("Opening DMI pipe failed!\n");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000314 return NULL;
Michael Karcher6701ee82010-01-20 14:14:11 +0000315 }
Michael Karcherd9f266d2010-08-08 21:56:52 +0000316
317 /* Kill lines starting with '#', as recent dmidecode versions
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000318 * have the quirk to emit a "# SMBIOS implementations newer..."
319 * message even on "-s" if the SMBIOS declares a
320 * newer-than-supported version number, while it *should* only print
321 * the requested string.
322 */
Michael Karcherd9f266d2010-08-08 21:56:52 +0000323 do {
324 if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000325 if (ferror(dmidecode_pipe)) {
Michael Karcherd9f266d2010-08-08 21:56:52 +0000326 msg_perr("DMI pipe read error\n");
327 pclose(dmidecode_pipe);
328 return NULL;
329 } else {
330 answerbuf[0] = 0; /* Hit EOF */
331 }
Michael Karcher45f79cb2010-03-24 17:55:04 +0000332 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000333 } while (answerbuf[0] == '#');
Michael Karcherd9f266d2010-08-08 21:56:52 +0000334
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000335 /* Discard all output exceeding DMI_MAX_ANSWER_LEN to prevent deadlock on pclose. */
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000336 while (!feof(dmidecode_pipe))
337 getc(dmidecode_pipe);
338 if (pclose(dmidecode_pipe) != 0) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000339 msg_pwarn("dmidecode execution unsuccessful - continuing without DMI info\n");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000340 return NULL;
341 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000342
Uwe Hermann43959702010-03-13 17:28:29 +0000343 /* Chomp trailing newline. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000344 if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n')
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000345 answerbuf[strlen(answerbuf) - 1] = 0;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000346
347 result = strdup(answerbuf);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000348 if (result == NULL)
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000349 msg_pwarn("Warning: Out of memory - DMI support fails");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000350
351 return result;
352}
353
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000354int dmi_fill(void)
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000355{
356 int i;
Michael Karcher8c1df282010-02-26 09:51:20 +0000357 char *chassis_type;
Uwe Hermann43959702010-03-13 17:28:29 +0000358
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000359 msg_pdbg("Using External DMI decoder.\n");
360 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
361 dmi_strings[i].value = get_dmi_string(dmi_strings[i].keyword);
362 if (dmi_strings[i].value == NULL)
363 return 1;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000364 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000365
366 chassis_type = get_dmi_string("chassis-type");
Stefan Taunera34d7192011-07-26 00:54:42 +0000367 if (chassis_type == NULL)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000368 return 0; /* chassis-type handling is optional anyway */
Stefan Taunera34d7192011-07-26 00:54:42 +0000369
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000370 msg_pdbg("DMI string chassis-type: \"%s\"\n", chassis_type);
Stefan Taunera34d7192011-07-26 00:54:42 +0000371 is_laptop = 2;
372 for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
373 if (strcasecmp(chassis_type, dmi_chassis_types[i].name) == 0) {
374 is_laptop = dmi_chassis_types[i].is_laptop;
375 break;
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +0000376 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000377 }
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000378 free(chassis_type);
379 return 0;
380}
381
382#endif /* CONFIG_INTERNAL_DMI */
383
384static int dmi_shutdown(void *data)
385{
386 int i;
387 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
388 free(dmi_strings[i].value);
389 dmi_strings[i].value = NULL;
390 }
391 return 0;
392}
393
394void dmi_init(void)
395{
396 /* Register shutdown function before we allocate anything. */
397 if (register_shutdown(dmi_shutdown, NULL)) {
398 msg_pwarn("Warning: Could not register DMI shutdown function - continuing without DMI info.\n");
399 return;
400 }
401
402 /* dmi_fill fills the dmi_strings array, and if possible sets the global is_laptop variable. */
403 if (dmi_fill() != 0)
404 return;
Stefan Taunera34d7192011-07-26 00:54:42 +0000405
406 switch (is_laptop) {
407 case 1:
408 msg_pdbg("Laptop detected via DMI.\n");
409 break;
410 case 2:
411 msg_pdbg("DMI chassis-type is not specific enough.\n");
412 break;
413 }
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000414
415 has_dmi_support = 1;
416 int i;
417 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
418 msg_pdbg("DMI string %s: \"%s\"\n", dmi_strings[i].keyword,
419 (dmi_strings[i].value == NULL) ? "" : dmi_strings[i].value);
420 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000421}
422
423/**
424 * Does an substring/prefix/postfix/whole-string match.
425 *
426 * The pattern is matched as-is. The only metacharacters supported are '^'
427 * at the beginning and '$' at the end. So you can look for "^prefix",
428 * "suffix$", "substring" or "^complete string$".
429 *
Stefan Taunerd1045d82013-10-29 01:38:45 +0000430 * @param value The non-NULL string to check.
431 * @param pattern The non-NULL pattern.
Michael Karcher6701ee82010-01-20 14:14:11 +0000432 * @return Nonzero if pattern matches.
433 */
434static int dmi_compare(const char *value, const char *pattern)
435{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000436 int anchored = 0;
437 int patternlen;
Uwe Hermann43959702010-03-13 17:28:29 +0000438
Sean Nelson316a29f2010-05-07 20:09:04 +0000439 msg_pspew("matching %s against %s\n", value, pattern);
Uwe Hermann43959702010-03-13 17:28:29 +0000440 /* The empty string is part of all strings! */
Michael Karcher6701ee82010-01-20 14:14:11 +0000441 if (pattern[0] == 0)
442 return 1;
443
444 if (pattern[0] == '^') {
445 anchored = 1;
446 pattern++;
447 }
448
449 patternlen = strlen(pattern);
450 if (pattern[patternlen - 1] == '$') {
451 int valuelen = strlen(value);
452 patternlen--;
Uwe Hermann43959702010-03-13 17:28:29 +0000453 if (patternlen > valuelen)
Michael Karcher6701ee82010-01-20 14:14:11 +0000454 return 0;
455
456 /* full string match: require same length */
Uwe Hermann43959702010-03-13 17:28:29 +0000457 if (anchored && (valuelen != patternlen))
Michael Karcher6701ee82010-01-20 14:14:11 +0000458 return 0;
459
460 /* start character to make ends match */
461 value += valuelen - patternlen;
462 anchored = 1;
463 }
464
465 if (anchored)
466 return strncmp(value, pattern, patternlen) == 0;
467 else
468 return strstr(value, pattern) != NULL;
469}
470
471int dmi_match(const char *pattern)
472{
473 int i;
Uwe Hermann43959702010-03-13 17:28:29 +0000474
Michael Karcher6701ee82010-01-20 14:14:11 +0000475 if (!has_dmi_support)
476 return 0;
477
Stefan Taunerd1045d82013-10-29 01:38:45 +0000478 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
479 if (dmi_strings[i].value == NULL)
480 continue;
481
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000482 if (dmi_compare(dmi_strings[i].value, pattern))
Michael Karcher4dfa0932010-02-12 05:44:18 +0000483 return 1;
Stefan Taunerd1045d82013-10-29 01:38:45 +0000484 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000485
486 return 0;
487}
Carl-Daniel Hailfingere1fdff42010-06-23 23:14:44 +0000488
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000489#endif // defined(__i386__) || defined(__x86_64__)