blob: 3b6823333303d25a8dbc1bb3253295706c84aaf1 [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.
Michael Karcher6701ee82010-01-20 14:14:11 +000018 */
19
Stefan Taunerdc627932015-01-27 18:07:50 +000020/* strnlen is in POSIX but was a GNU extension up to glibc 2.10 */
21#if (__GLIBC__ == 2 && __GLIBC_MINOR__ < 10) || __GLIBC__ < 2
22#define _GNU_SOURCE
23#else
24#define _POSIX_C_SOURCE 200809L
25#endif
26
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000027#include <strings.h>
Michael Karcher6701ee82010-01-20 14:14:11 +000028#include <string.h>
Stefan Tauner9972d152014-07-13 12:52:15 +000029#include <ctype.h>
Felix Singerdd5f6932022-08-19 03:33:27 +020030#include <stdbool.h>
Michael Karcher6701ee82010-01-20 14:14:11 +000031#include <stdio.h>
32#include <stdlib.h>
33
34#include "flash.h"
Thomas Heijligencce1e5b2021-11-02 20:33:35 +010035#include "hwaccess.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000036#include "programmer.h"
Michael Karcher6701ee82010-01-20 14:14:11 +000037
Sean Nelson4c6d3a42013-09-11 23:35:03 +000038/* Enable SMBIOS decoding. Currently legacy DMI decoding is enough. */
39#define SM_SUPPORT 0
40
41/* Strings longer than 4096 in DMI are just insane. */
42#define DMI_MAX_ANSWER_LEN 4096
43
Carl-Daniel Hailfingere1fdff42010-06-23 23:14:44 +000044int has_dmi_support = 0;
45
Sean Nelson4c6d3a42013-09-11 23:35:03 +000046static struct {
47 const char *const keyword;
48 const uint8_t type;
49 const uint8_t offset;
50 char *value;
51} dmi_strings[] = {
52 { "system-manufacturer", 1, 0x04, NULL },
53 { "system-product-name", 1, 0x05, NULL },
54 { "system-version", 1, 0x06, NULL },
55 { "baseboard-manufacturer", 2, 0x04, NULL },
56 { "baseboard-product-name", 2, 0x05, NULL },
57 { "baseboard-version", 2, 0x06, NULL },
Michael Karcher6701ee82010-01-20 14:14:11 +000058};
59
Stefan Taunera34d7192011-07-26 00:54:42 +000060/* This list is used to identify supposed laptops. The is_laptop field has the
61 * following meaning:
Elyes HAOUASac01baa2018-05-28 16:52:21 +020062 * - 0: in all likelihood not a laptop
63 * - 1: in all likelihood a laptop
64 * - 2: chassis-type is not specific enough
Stefan Taunera34d7192011-07-26 00:54:42 +000065 * A full list of chassis types can be found in the System Management BIOS
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000066 * (SMBIOS) Reference Specification 2.7.0 section 7.4.1 "Chassis Types" at
67 * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf
68 * The types below are the most common ones.
69 */
70static const struct {
Sean Nelson4c6d3a42013-09-11 23:35:03 +000071 uint8_t type;
72 uint8_t is_laptop;
Jacob Garber4a84ec22019-07-25 19:12:31 -060073 const char *name;
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000074} dmi_chassis_types[] = {
Stefan Taunera34d7192011-07-26 00:54:42 +000075 {0x01, 2, "Other"},
76 {0x02, 2, "Unknown"},
Stefan Tauner4c5665f2012-02-17 20:03:37 +000077 {0x03, 0, "Desktop"},
78 {0x04, 0, "Low Profile Desktop"},
Stefan Taunera34d7192011-07-26 00:54:42 +000079 {0x06, 0, "Mini Tower"},
80 {0x07, 0, "Tower"},
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000081 {0x08, 1, "Portable"},
82 {0x09, 1, "Laptop"},
83 {0x0a, 1, "Notebook"},
84 {0x0b, 1, "Hand Held"},
85 {0x0e, 1, "Sub Notebook"},
Stefan Taunera34d7192011-07-26 00:54:42 +000086 {0x11, 0, "Main Server Chassis"},
87 {0x17, 0, "Rack Mount Chassis"},
Sylvain "ythier" Hitier3093f8f2011-09-03 11:22:27 +000088 {0x18, 0, "Sealed-case PC"}, /* used by Supermicro (X8SIE) */
Stefan Tauner23e10b82016-01-23 16:16:49 +000089 {0x19, 0, "Multi-system"}, /* used by Supermicro (X7DWT) */
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000090};
91
Sean Nelson4c6d3a42013-09-11 23:35:03 +000092#if CONFIG_INTERNAL_DMI == 1
Sean Nelson4c6d3a42013-09-11 23:35:03 +000093static bool dmi_checksum(const uint8_t * const buf, size_t len)
94{
95 uint8_t sum = 0;
96 size_t a;
97
98 for (a = 0; a < len; a++)
99 sum += buf[a];
100 return (sum == 0);
101}
102
Stefan Tauner9972d152014-07-13 12:52:15 +0000103/** Retrieve a DMI string.
104 *
105 * See SMBIOS spec. section 6.1.3 "Text strings".
106 * The table will be unmapped ASAP, hence return a duplicated & sanitized string that needs to be freed later.
107 *
108 * \param buf the buffer to search through (usually appended directly to a DMI structure)
109 * \param string_id index of the string to look for
110 * \param limit pointer to the first byte beyond \em buf
111 */
112static char *dmi_string(const char *buf, uint8_t string_id, const char *limit)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000113{
114 size_t i, len;
115
116 if (string_id == 0)
Stefan Tauner778c6d82014-09-05 16:14:11 +0000117 return strdup("Not Specified");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000118
119 while (string_id > 1 && string_id--) {
Stefan Tauner9972d152014-07-13 12:52:15 +0000120 if (buf >= limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000121 msg_perr("DMI table is broken (string portion out of bounds)!\n");
Stefan Tauner778c6d82014-09-05 16:14:11 +0000122 return strdup("<OUT OF BOUNDS>");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000123 }
Stefan Tauner9972d152014-07-13 12:52:15 +0000124 buf += strnlen(buf, limit - buf) + 1;
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000125 }
126
127 if (!*buf) /* as long as the current byte we're on isn't null */
Stefan Tauner778c6d82014-09-05 16:14:11 +0000128 return strdup("<BAD INDEX>");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000129
Stefan Tauner9972d152014-07-13 12:52:15 +0000130 len = strnlen(buf, limit - buf);
131 char *newbuf = malloc(len + 1);
132 if (newbuf == NULL) {
133 msg_perr("Out of memory!\n");
134 return NULL;
135 }
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000136
137 /* fix junk bytes in the string */
Stefan Tauner9972d152014-07-13 12:52:15 +0000138 for (i = 0; i < len && buf[i] != '\0'; i++) {
Stefan Taunerff9e6c32014-10-19 07:54:27 +0000139 if (isprint((unsigned char)buf[i]))
Stefan Tauner9972d152014-07-13 12:52:15 +0000140 newbuf[i] = buf[i];
141 else
142 newbuf[i] = ' ';
143 }
144 newbuf[i] = '\0';
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000145
Stefan Tauner9972d152014-07-13 12:52:15 +0000146 return newbuf;
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000147}
148
149static void dmi_chassis_type(uint8_t code)
150{
Nico Huber519be662018-12-23 20:03:35 +0100151 unsigned int i;
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000152 code &= 0x7f; /* bits 6:0 are chassis type, 7th bit is the lock bit */
153 is_laptop = 2;
154 for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
155 if (code == dmi_chassis_types[i].type) {
156 msg_pdbg("DMI string chassis-type: \"%s\"\n", dmi_chassis_types[i].name);
157 is_laptop = dmi_chassis_types[i].is_laptop;
158 break;
159 }
160 }
161}
162
163static void dmi_table(uint32_t base, uint16_t len, uint16_t num)
164{
Nico Huber519be662018-12-23 20:03:35 +0100165 unsigned int i = 0, j = 0;
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000166
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000167 uint8_t *dmi_table_mem = physmap_ro("DMI Table", base, len);
Nico Huber245e5402022-03-06 00:21:52 +0100168 if (dmi_table_mem == ERROR_PTR) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000169 msg_perr("Unable to access DMI Table\n");
170 return;
171 }
172
173 uint8_t *data = dmi_table_mem;
174 uint8_t *limit = dmi_table_mem + len;
175
176 /* SMBIOS structure header is always 4 B long and contains:
177 * - uint8_t type; // see dmi_chassis_types's type
178 * - uint8_t length; // data section w/ header w/o strings
179 * - uint16_t handle;
180 */
Stefan Tauner9972d152014-07-13 12:52:15 +0000181 while (i < num && data + 4 < limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000182 /* - If a short entry is found (less than 4 bytes), not only it
183 * is invalid, but we cannot reliably locate the next entry.
184 * - If the length value indicates that this structure spreads
Stefan Tauner6697f712014-08-06 15:09:15 +0000185 * across the table border, something is fishy too.
Angel Pons0be623c2021-04-17 17:08:44 +0200186 * Better stop at this point, and let the user know their
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000187 * table is broken.
188 */
Stefan Tauner9972d152014-07-13 12:52:15 +0000189 if (data[1] < 4 || data + data[1] >= limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000190 msg_perr("DMI table is broken (bogus header)!\n");
191 break;
192 }
193
194 if(data[0] == 3) {
Stefan Tauner9972d152014-07-13 12:52:15 +0000195 if (data + 5 < limit)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000196 dmi_chassis_type(data[5]);
Stefan Tauner9972d152014-07-13 12:52:15 +0000197 else /* the table is broken, but laptop detection is optional, hence continue. */
198 msg_pwarn("DMI table is broken (chassis_type out of bounds)!\n");
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000199 } else
200 for (j = 0; j < ARRAY_SIZE(dmi_strings); j++) {
201 uint8_t offset = dmi_strings[j].offset;
202 uint8_t type = dmi_strings[j].type;
203
204 if (data[0] != type)
205 continue;
206
Stefan Tauner9972d152014-07-13 12:52:15 +0000207 if (data[1] <= offset || data + offset >= limit) {
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000208 msg_perr("DMI table is broken (offset out of bounds)!\n");
209 goto out;
210 }
211
Stefan Tauner9972d152014-07-13 12:52:15 +0000212 dmi_strings[j].value = dmi_string((const char *)(data + data[1]), data[offset],
213 (const char *)limit);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000214 }
215 /* Find next structure by skipping data and string sections */
216 data += data[1];
217 while (data + 1 <= limit) {
218 if (data[0] == 0 && data[1] == 0)
219 break;
220 data++;
221 }
222 data += 2;
223 i++;
224 }
225out:
226 physunmap(dmi_table_mem, len);
227}
228
229#if SM_SUPPORT
230static int smbios_decode(uint8_t *buf)
231{
232 /* TODO: other checks mentioned in the conformance guidelines? */
233 if (!dmi_checksum(buf, buf[0x05]) ||
234 (memcmp(buf + 0x10, "_DMI_", 5) != 0) ||
235 !dmi_checksum(buf + 0x10, 0x0F))
236 return 0;
237
238 dmi_table(mmio_readl(buf + 0x18), mmio_readw(buf + 0x16), mmio_readw(buf + 0x1C));
239
240 return 1;
241}
242#endif
243
244static int legacy_decode(uint8_t *buf)
245{
246 if (!dmi_checksum(buf, 0x0F))
247 return 1;
248
249 dmi_table(mmio_readl(buf + 0x08), mmio_readw(buf + 0x06), mmio_readw(buf + 0x0C));
250
251 return 0;
252}
253
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600254static int dmi_fill(void)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000255{
256 size_t fp;
257 uint8_t *dmi_mem;
258 int ret = 1;
259
260 msg_pdbg("Using Internal DMI decoder.\n");
261 /* There are two ways specified to gain access to the SMBIOS table:
262 * - EFI's configuration table contains a pointer to the SMBIOS table. On linux it can be obtained from
263 * sysfs. EFI's SMBIOS GUID is: {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,0x0,0x90,0x27,0x3f,0xc1,0x4d}
264 * - Scanning physical memory address range 0x000F0000h to 0x000FFFFF for the anchor-string(s). */
Niklas Söderlund5d307202013-09-14 09:02:27 +0000265 dmi_mem = physmap_ro("DMI", 0xF0000, 0x10000);
266 if (dmi_mem == ERROR_PTR)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000267 return ret;
268
269 for (fp = 0; fp <= 0xFFF0; fp += 16) {
270#if SM_SUPPORT
271 if (memcmp(dmi_mem + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
272 if (smbios_decode(dmi_mem + fp)) // FIXME: length check
273 goto out;
274 } else
275#endif
276 if (memcmp(dmi_mem + fp, "_DMI_", 5) == 0)
277 if (legacy_decode(dmi_mem + fp) == 0) {
278 ret = 0;
279 goto out;
280 }
281 }
282 msg_pinfo("No DMI table found.\n");
283out:
284 physunmap(dmi_mem, 0x10000);
285 return ret;
286}
287
288#else /* CONFIG_INTERNAL_DMI */
289
290#define DMI_COMMAND_LEN_MAX 300
Stefan Tauner4f444792011-06-27 11:44:03 +0000291#if IS_WINDOWS
292static const char *dmidecode_command = "dmidecode.exe 2>NUL";
293#else
294static const char *dmidecode_command = "dmidecode 2>/dev/null";
295#endif
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;
Michael Karcherd9f266d2010-08-08 21:56:52 +0000324 }
Elyes HAOUASa67ac582019-06-09 17:06:50 +0200325 answerbuf[0] = 0; /* Hit EOF */
Michael Karcher45f79cb2010-03-24 17:55:04 +0000326 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000327 } while (answerbuf[0] == '#');
Michael Karcherd9f266d2010-08-08 21:56:52 +0000328
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000329 /* Discard all output exceeding DMI_MAX_ANSWER_LEN to prevent deadlock on pclose. */
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000330 while (!feof(dmidecode_pipe))
331 getc(dmidecode_pipe);
332 if (pclose(dmidecode_pipe) != 0) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000333 msg_pwarn("dmidecode execution unsuccessful - continuing without DMI info\n");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000334 return NULL;
335 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000336
Uwe Hermann43959702010-03-13 17:28:29 +0000337 /* Chomp trailing newline. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000338 if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n')
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000339 answerbuf[strlen(answerbuf) - 1] = 0;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000340
341 result = strdup(answerbuf);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000342 if (result == NULL)
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000343 msg_pwarn("Warning: Out of memory - DMI support fails");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000344
345 return result;
346}
347
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600348static int dmi_fill(void)
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000349{
Nico Huber519be662018-12-23 20:03:35 +0100350 unsigned int i;
Michael Karcher8c1df282010-02-26 09:51:20 +0000351 char *chassis_type;
Uwe Hermann43959702010-03-13 17:28:29 +0000352
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000353 msg_pdbg("Using External DMI decoder.\n");
354 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
355 dmi_strings[i].value = get_dmi_string(dmi_strings[i].keyword);
356 if (dmi_strings[i].value == NULL)
357 return 1;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000358 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000359
360 chassis_type = get_dmi_string("chassis-type");
Stefan Taunera34d7192011-07-26 00:54:42 +0000361 if (chassis_type == NULL)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000362 return 0; /* chassis-type handling is optional anyway */
Stefan Taunera34d7192011-07-26 00:54:42 +0000363
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000364 msg_pdbg("DMI string chassis-type: \"%s\"\n", chassis_type);
Stefan Taunera34d7192011-07-26 00:54:42 +0000365 is_laptop = 2;
366 for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
367 if (strcasecmp(chassis_type, dmi_chassis_types[i].name) == 0) {
368 is_laptop = dmi_chassis_types[i].is_laptop;
369 break;
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +0000370 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000371 }
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000372 free(chassis_type);
373 return 0;
374}
375
376#endif /* CONFIG_INTERNAL_DMI */
377
378static int dmi_shutdown(void *data)
379{
Nico Huber519be662018-12-23 20:03:35 +0100380 unsigned int i;
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000381 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
382 free(dmi_strings[i].value);
383 dmi_strings[i].value = NULL;
384 }
385 return 0;
386}
387
388void dmi_init(void)
389{
390 /* Register shutdown function before we allocate anything. */
391 if (register_shutdown(dmi_shutdown, NULL)) {
392 msg_pwarn("Warning: Could not register DMI shutdown function - continuing without DMI info.\n");
393 return;
394 }
395
396 /* dmi_fill fills the dmi_strings array, and if possible sets the global is_laptop variable. */
397 if (dmi_fill() != 0)
398 return;
Stefan Taunera34d7192011-07-26 00:54:42 +0000399
400 switch (is_laptop) {
401 case 1:
402 msg_pdbg("Laptop detected via DMI.\n");
403 break;
404 case 2:
405 msg_pdbg("DMI chassis-type is not specific enough.\n");
406 break;
407 }
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000408
409 has_dmi_support = 1;
Nico Huber519be662018-12-23 20:03:35 +0100410 unsigned int i;
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000411 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
412 msg_pdbg("DMI string %s: \"%s\"\n", dmi_strings[i].keyword,
413 (dmi_strings[i].value == NULL) ? "" : dmi_strings[i].value);
414 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000415}
416
417/**
418 * Does an substring/prefix/postfix/whole-string match.
419 *
420 * The pattern is matched as-is. The only metacharacters supported are '^'
421 * at the beginning and '$' at the end. So you can look for "^prefix",
422 * "suffix$", "substring" or "^complete string$".
423 *
Stefan Taunerd1045d82013-10-29 01:38:45 +0000424 * @param value The non-NULL string to check.
425 * @param pattern The non-NULL pattern.
Michael Karcher6701ee82010-01-20 14:14:11 +0000426 * @return Nonzero if pattern matches.
427 */
428static int dmi_compare(const char *value, const char *pattern)
429{
Felix Singerdd5f6932022-08-19 03:33:27 +0200430 bool anchored = false;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000431 int patternlen;
Uwe Hermann43959702010-03-13 17:28:29 +0000432
Sean Nelson316a29f2010-05-07 20:09:04 +0000433 msg_pspew("matching %s against %s\n", value, pattern);
Uwe Hermann43959702010-03-13 17:28:29 +0000434 /* The empty string is part of all strings! */
Michael Karcher6701ee82010-01-20 14:14:11 +0000435 if (pattern[0] == 0)
436 return 1;
437
438 if (pattern[0] == '^') {
Felix Singerdd5f6932022-08-19 03:33:27 +0200439 anchored = true;
Michael Karcher6701ee82010-01-20 14:14:11 +0000440 pattern++;
441 }
442
443 patternlen = strlen(pattern);
444 if (pattern[patternlen - 1] == '$') {
445 int valuelen = strlen(value);
446 patternlen--;
Uwe Hermann43959702010-03-13 17:28:29 +0000447 if (patternlen > valuelen)
Michael Karcher6701ee82010-01-20 14:14:11 +0000448 return 0;
449
450 /* full string match: require same length */
Uwe Hermann43959702010-03-13 17:28:29 +0000451 if (anchored && (valuelen != patternlen))
Michael Karcher6701ee82010-01-20 14:14:11 +0000452 return 0;
453
454 /* start character to make ends match */
455 value += valuelen - patternlen;
Felix Singerdd5f6932022-08-19 03:33:27 +0200456 anchored = true;
Michael Karcher6701ee82010-01-20 14:14:11 +0000457 }
458
459 if (anchored)
460 return strncmp(value, pattern, patternlen) == 0;
461 else
462 return strstr(value, pattern) != NULL;
463}
464
465int dmi_match(const char *pattern)
466{
Nico Huber519be662018-12-23 20:03:35 +0100467 unsigned int i;
Uwe Hermann43959702010-03-13 17:28:29 +0000468
Michael Karcher6701ee82010-01-20 14:14:11 +0000469 if (!has_dmi_support)
470 return 0;
471
Stefan Taunerd1045d82013-10-29 01:38:45 +0000472 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
473 if (dmi_strings[i].value == NULL)
474 continue;
475
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000476 if (dmi_compare(dmi_strings[i].value, pattern))
Michael Karcher4dfa0932010-02-12 05:44:18 +0000477 return 1;
Stefan Taunerd1045d82013-10-29 01:38:45 +0000478 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000479
480 return 0;
481}