blob: e449cd9f5ca9d0b18997fe2ff289c491c43611b2 [file] [log] [blame]
mkarcher803b4042010-01-20 14:14:11 +00001/*
2 * This file is part of the flashrom project.
3 *
Kangheui Wonb1798232019-11-27 16:31:54 +11004 * Copyright (C) 2000-2002 Alan Cox <alan@redhat.com>
5 * Copyright (C) 2002-2010 Jean Delvare <khali@linux-fr.org>
mkarcher803b4042010-01-20 14:14:11 +00006 * Copyright (C) 2009,2010 Michael Karcher
Kangheui Wonb1798232019-11-27 16:31:54 +11007 * Copyright (C) 2011-2013 Stefan Tauner
mkarcher803b4042010-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.
mkarcher803b4042010-01-20 14:14:11 +000018 */
19
Kangheui Wonb1798232019-11-27 16:31:54 +110020/* 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
Edward O'Callaghanb4300ca2019-09-03 16:15:21 +100027#include <strings.h>
mkarcher803b4042010-01-20 14:14:11 +000028#include <string.h>
Kangheui Wonb1798232019-11-27 16:31:54 +110029#include <ctype.h>
mkarcher803b4042010-01-20 14:14:11 +000030#include <stdio.h>
31#include <stdlib.h>
32
Kangheui Wonb1798232019-11-27 16:31:54 +110033#include "platform.h"
mkarcher803b4042010-01-20 14:14:11 +000034#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000035#include "programmer.h"
mkarcher803b4042010-01-20 14:14:11 +000036
Kangheui Won0f45dd02019-11-28 15:34:29 +110037/* Enable SMBIOS decoding. Currently legacy DMI decoding is enough. */
38#define SM_SUPPORT 0
hailfingerdd154602010-06-23 23:14:44 +000039
Kangheui Wonb1798232019-11-27 16:31:54 +110040/* Strings longer than 4096 in DMI are just insane. */
41#define DMI_MAX_ANSWER_LEN 4096
42
Kangheui Won0f45dd02019-11-28 15:34:29 +110043int has_dmi_support = 0;
44
Kangheui Wonb1798232019-11-27 16:31:54 +110045static struct {
46 const char *const keyword;
47 const uint8_t type;
48 const uint8_t offset;
49 char *value;
50} dmi_strings[] = {
51 { "system-manufacturer", 1, 0x04, NULL },
52 { "system-product-name", 1, 0x05, NULL },
53 { "system-version", 1, 0x06, NULL },
54 { "baseboard-manufacturer", 2, 0x04, NULL },
55 { "baseboard-product-name", 2, 0x05, NULL },
56 { "baseboard-version", 2, 0x06, NULL },
mkarcher803b4042010-01-20 14:14:11 +000057};
58
stefanct674e81e2011-07-26 00:54:42 +000059/* This list is used to identify supposed laptops. The is_laptop field has the
60 * following meaning:
Kangheui Wonb1798232019-11-27 16:31:54 +110061 * - 0: in all likelihood not a laptop
62 * - 1: in all likelihood a laptop
63 * - 2: chassis-type is not specific enough
stefanct674e81e2011-07-26 00:54:42 +000064 * A full list of chassis types can be found in the System Management BIOS
hailfinger2b11d9d2010-09-26 21:43:53 +000065 * (SMBIOS) Reference Specification 2.7.0 section 7.4.1 "Chassis Types" at
66 * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf
67 * The types below are the most common ones.
68 */
69static const struct {
Kangheui Wonb1798232019-11-27 16:31:54 +110070 uint8_t type;
71 uint8_t is_laptop;
hailfinger2b11d9d2010-09-26 21:43:53 +000072 const char *name;
73} dmi_chassis_types[] = {
stefanct674e81e2011-07-26 00:54:42 +000074 {0x01, 2, "Other"},
75 {0x02, 2, "Unknown"},
Kangheui Wonb1798232019-11-27 16:31:54 +110076 {0x03, 0, "Desktop"},
77 {0x04, 0, "Low Profile Desktop"},
stefanct674e81e2011-07-26 00:54:42 +000078 {0x06, 0, "Mini Tower"},
79 {0x07, 0, "Tower"},
hailfinger2b11d9d2010-09-26 21:43:53 +000080 {0x08, 1, "Portable"},
81 {0x09, 1, "Laptop"},
82 {0x0a, 1, "Notebook"},
83 {0x0b, 1, "Hand Held"},
84 {0x0e, 1, "Sub Notebook"},
stefanct674e81e2011-07-26 00:54:42 +000085 {0x11, 0, "Main Server Chassis"},
86 {0x17, 0, "Rack Mount Chassis"},
stefanctd6efe1a2011-09-03 11:22:27 +000087 {0x18, 0, "Sealed-case PC"}, /* used by Supermicro (X8SIE) */
Kangheui Wonb1798232019-11-27 16:31:54 +110088 {0x19, 0, "Multi-system"}, /* used by Supermicro (X7DWT) */
hailfinger2b11d9d2010-09-26 21:43:53 +000089};
90
Kangheui Won0f45dd02019-11-28 15:34:29 +110091#if CONFIG_INTERNAL_DMI == 1
92static bool dmi_checksum(const uint8_t * const buf, size_t len)
93{
94 uint8_t sum = 0;
95 size_t a;
96
97 for (a = 0; a < len; a++)
98 sum += buf[a];
99 return (sum == 0);
100}
101
102/** Retrieve a DMI string.
103 *
104 * See SMBIOS spec. section 6.1.3 "Text strings".
105 * The table will be unmapped ASAP, hence return a duplicated & sanitized string that needs to be freed later.
106 *
107 * \param buf the buffer to search through (usually appended directly to a DMI structure)
108 * \param string_id index of the string to look for
109 * \param limit pointer to the first byte beyond \em buf
110 */
111static char *dmi_string(const char *buf, uint8_t string_id, const char *limit)
112{
113 size_t i, len;
114
115 if (string_id == 0)
116 return strdup("Not Specified");
117
118 while (string_id > 1 && string_id--) {
119 if (buf >= limit) {
120 msg_perr("DMI table is broken (string portion out of bounds)!\n");
121 return strdup("<OUT OF BOUNDS>");
122 }
123 buf += strnlen(buf, limit - buf) + 1;
124 }
125
126 if (!*buf) /* as long as the current byte we're on isn't null */
127 return strdup("<BAD INDEX>");
128
129 len = strnlen(buf, limit - buf);
130 char *newbuf = malloc(len + 1);
131 if (newbuf == NULL) {
132 msg_perr("Out of memory!\n");
133 return NULL;
134 }
135
136 /* fix junk bytes in the string */
137 for (i = 0; i < len && buf[i] != '\0'; i++) {
138 if (isprint((unsigned char)buf[i]))
139 newbuf[i] = buf[i];
140 else
141 newbuf[i] = ' ';
142 }
143 newbuf[i] = '\0';
144
145 return newbuf;
146}
147
148static void dmi_chassis_type(uint8_t code)
149{
150 unsigned int i;
151 code &= 0x7f; /* bits 6:0 are chassis type, 7th bit is the lock bit */
152 is_laptop = 2;
153 for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
154 if (code == dmi_chassis_types[i].type) {
155 msg_pdbg("DMI string chassis-type: \"%s\"\n", dmi_chassis_types[i].name);
156 is_laptop = dmi_chassis_types[i].is_laptop;
157 break;
158 }
159 }
160}
161
162static void dmi_table(uint32_t base, uint16_t len, uint16_t num)
163{
164 unsigned int i = 0, j = 0;
165
166 uint8_t *dmi_table_mem = physmap_ro("DMI Table", base, len);
167 if (dmi_table_mem == NULL) {
168 msg_perr("Unable to access DMI Table\n");
169 return;
170 }
171
172 uint8_t *data = dmi_table_mem;
173 uint8_t *limit = dmi_table_mem + len;
174
175 /* SMBIOS structure header is always 4 B long and contains:
176 * - uint8_t type; // see dmi_chassis_types's type
177 * - uint8_t length; // data section w/ header w/o strings
178 * - uint16_t handle;
179 */
180 while (i < num && data + 4 < limit) {
181 /* - If a short entry is found (less than 4 bytes), not only it
182 * is invalid, but we cannot reliably locate the next entry.
183 * - If the length value indicates that this structure spreads
184 * across the table border, something is fishy too.
Angel Ponsadc96d52021-04-17 17:08:44 +0200185 * Better stop at this point, and let the user know their
Kangheui Won0f45dd02019-11-28 15:34:29 +1100186 * table is broken.
187 */
188 if (data[1] < 4 || data + data[1] >= limit) {
189 msg_perr("DMI table is broken (bogus header)!\n");
190 break;
191 }
192
193 if(data[0] == 3) {
194 if (data + 5 < limit)
195 dmi_chassis_type(data[5]);
196 else /* the table is broken, but laptop detection is optional, hence continue. */
197 msg_pwarn("DMI table is broken (chassis_type out of bounds)!\n");
198 } else
199 for (j = 0; j < ARRAY_SIZE(dmi_strings); j++) {
200 uint8_t offset = dmi_strings[j].offset;
201 uint8_t type = dmi_strings[j].type;
202
203 if (data[0] != type)
204 continue;
205
206 if (data[1] <= offset || data + offset >= limit) {
207 msg_perr("DMI table is broken (offset out of bounds)!\n");
208 goto out;
209 }
210
211 dmi_strings[j].value = dmi_string((const char *)(data + data[1]), data[offset],
212 (const char *)limit);
213 }
214 /* Find next structure by skipping data and string sections */
215 data += data[1];
216 while (data + 1 <= limit) {
217 if (data[0] == 0 && data[1] == 0)
218 break;
219 data++;
220 }
221 data += 2;
222 i++;
223 }
224out:
225 physunmap(dmi_table_mem, len);
226}
227
228#if SM_SUPPORT
229static int smbios_decode(uint8_t *buf)
230{
231 /* TODO: other checks mentioned in the conformance guidelines? */
232 if (!dmi_checksum(buf, buf[0x05]) ||
233 (memcmp(buf + 0x10, "_DMI_", 5) != 0) ||
234 !dmi_checksum(buf + 0x10, 0x0F))
235 return 0;
236
237 dmi_table(mmio_readl(buf + 0x18), mmio_readw(buf + 0x16), mmio_readw(buf + 0x1C));
238
239 return 1;
240}
241#endif
242
243static int legacy_decode(uint8_t *buf)
244{
245 if (!dmi_checksum(buf, 0x0F))
246 return 1;
247
248 dmi_table(mmio_readl(buf + 0x08), mmio_readw(buf + 0x06), mmio_readw(buf + 0x0C));
249
250 return 0;
251}
252
253static int dmi_fill(void)
254{
255 size_t fp;
256 uint8_t *dmi_mem;
257 int ret = 1;
258
259 msg_pdbg("Using Internal DMI decoder.\n");
260 /* There are two ways specified to gain access to the SMBIOS table:
261 * - EFI's configuration table contains a pointer to the SMBIOS table. On linux it can be obtained from
262 * sysfs. EFI's SMBIOS GUID is: {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,0x0,0x90,0x27,0x3f,0xc1,0x4d}
263 * - Scanning physical memory address range 0x000F0000h to 0x000FFFFF for the anchor-string(s). */
264 dmi_mem = physmap_ro("DMI", 0xF0000, 0x10000);
265 if (dmi_mem == ERROR_PTR)
266 return ret;
267
268 for (fp = 0; fp <= 0xFFF0; fp += 16) {
269#if SM_SUPPORT
270 if (memcmp(dmi_mem + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
271 if (smbios_decode(dmi_mem + fp)) // FIXME: length check
272 goto out;
273 } else
274#endif
275 if (memcmp(dmi_mem + fp, "_DMI_", 5) == 0)
276 if (legacy_decode(dmi_mem + fp) == 0) {
277 ret = 0;
278 goto out;
279 }
280 }
281 msg_pinfo("No DMI table found.\n");
282out:
283 physunmap(dmi_mem, 0x10000);
284 return ret;
285}
286
287#else /* CONFIG_INTERNAL_DMI */
288
Kangheui Wonb1798232019-11-27 16:31:54 +1100289#define DMI_COMMAND_LEN_MAX 300
290#if IS_WINDOWS
291static const char *dmidecode_command = "dmidecode.exe 2>NUL";
292#else
293static const char *dmidecode_command = "dmidecode 2>/dev/null";
294#endif
mkarcher803b4042010-01-20 14:14:11 +0000295
mkarcherd0f757c2010-02-26 09:51:16 +0000296static char *get_dmi_string(const char *string_name)
mkarcher803b4042010-01-20 14:14:11 +0000297{
298 FILE *dmidecode_pipe;
mkarcherd0f757c2010-02-26 09:51:16 +0000299 char *result;
300 char answerbuf[DMI_MAX_ANSWER_LEN];
Kangheui Wonb1798232019-11-27 16:31:54 +1100301 char commandline[DMI_COMMAND_LEN_MAX];
uwef6f94d42010-03-13 17:28:29 +0000302
mkarcherd0f757c2010-02-26 09:51:16 +0000303 snprintf(commandline, sizeof(commandline),
304 "%s -s %s", dmidecode_command, string_name);
305 dmidecode_pipe = popen(commandline, "r");
306 if (!dmidecode_pipe) {
Kangheui Wonb1798232019-11-27 16:31:54 +1100307 msg_perr("Opening DMI pipe failed!\n");
mkarcherd0f757c2010-02-26 09:51:16 +0000308 return NULL;
mkarcher803b4042010-01-20 14:14:11 +0000309 }
mkarcherd0372672010-08-08 21:56:52 +0000310
311 /* Kill lines starting with '#', as recent dmidecode versions
uwe8d342eb2011-07-28 08:13:25 +0000312 * have the quirk to emit a "# SMBIOS implementations newer..."
313 * message even on "-s" if the SMBIOS declares a
314 * newer-than-supported version number, while it *should* only print
315 * the requested string.
316 */
mkarcherd0372672010-08-08 21:56:52 +0000317 do {
318 if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
uwe8d342eb2011-07-28 08:13:25 +0000319 if (ferror(dmidecode_pipe)) {
mkarcherd0372672010-08-08 21:56:52 +0000320 msg_perr("DMI pipe read error\n");
321 pclose(dmidecode_pipe);
322 return NULL;
mkarcherd0372672010-08-08 21:56:52 +0000323 }
Kangheui Wonb1798232019-11-27 16:31:54 +1100324 answerbuf[0] = 0; /* Hit EOF */
mkarcher17103ee2010-03-24 17:55:04 +0000325 }
uwe8d342eb2011-07-28 08:13:25 +0000326 } while (answerbuf[0] == '#');
mkarcherd0372672010-08-08 21:56:52 +0000327
Kangheui Wonb1798232019-11-27 16:31:54 +1100328 /* Discard all output exceeding DMI_MAX_ANSWER_LEN to prevent deadlock on pclose. */
mkarcherd0f757c2010-02-26 09:51:16 +0000329 while (!feof(dmidecode_pipe))
330 getc(dmidecode_pipe);
331 if (pclose(dmidecode_pipe) != 0) {
Kangheui Wonb1798232019-11-27 16:31:54 +1100332 msg_pwarn("dmidecode execution unsuccessful - continuing without DMI info\n");
mkarcherd0f757c2010-02-26 09:51:16 +0000333 return NULL;
334 }
mkarcher803b4042010-01-20 14:14:11 +0000335
uwef6f94d42010-03-13 17:28:29 +0000336 /* Chomp trailing newline. */
uwe8d342eb2011-07-28 08:13:25 +0000337 if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n')
mkarcherd0f757c2010-02-26 09:51:16 +0000338 answerbuf[strlen(answerbuf) - 1] = 0;
mkarcherd0f757c2010-02-26 09:51:16 +0000339
340 result = strdup(answerbuf);
Kangheui Wonb1798232019-11-27 16:31:54 +1100341 if (result == NULL)
342 msg_pwarn("Warning: Out of memory - DMI support fails");
mkarcherd0f757c2010-02-26 09:51:16 +0000343
344 return result;
345}
346
Kangheui Wonb1798232019-11-27 16:31:54 +1100347static int dmi_fill(void)
mkarcherd0f757c2010-02-26 09:51:16 +0000348{
Kangheui Wonb1798232019-11-27 16:31:54 +1100349 unsigned int i;
mkarcher287aa242010-02-26 09:51:20 +0000350 char *chassis_type;
uwef6f94d42010-03-13 17:28:29 +0000351
Kangheui Wonb1798232019-11-27 16:31:54 +1100352 msg_pdbg("Using External DMI decoder.\n");
353 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
354 dmi_strings[i].value = get_dmi_string(dmi_strings[i].keyword);
355 if (dmi_strings[i].value == NULL)
356 return 1;
mkarcherd0f757c2010-02-26 09:51:16 +0000357 }
mkarcher287aa242010-02-26 09:51:20 +0000358
359 chassis_type = get_dmi_string("chassis-type");
stefanct674e81e2011-07-26 00:54:42 +0000360 if (chassis_type == NULL)
Kangheui Wonb1798232019-11-27 16:31:54 +1100361 return 0; /* chassis-type handling is optional anyway */
stefanct674e81e2011-07-26 00:54:42 +0000362
Kangheui Wonb1798232019-11-27 16:31:54 +1100363 msg_pdbg("DMI string chassis-type: \"%s\"\n", chassis_type);
stefanct674e81e2011-07-26 00:54:42 +0000364 is_laptop = 2;
365 for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
366 if (strcasecmp(chassis_type, dmi_chassis_types[i].name) == 0) {
367 is_laptop = dmi_chassis_types[i].is_laptop;
368 break;
hailfinger2b11d9d2010-09-26 21:43:53 +0000369 }
mkarcher287aa242010-02-26 09:51:20 +0000370 }
Kangheui Wonb1798232019-11-27 16:31:54 +1100371 free(chassis_type);
372 return 0;
373}
374
Kangheui Won0f45dd02019-11-28 15:34:29 +1100375#endif /* CONFIG_INTERNAL_DMI */
376
Kangheui Wonb1798232019-11-27 16:31:54 +1100377static int dmi_shutdown(void *data)
378{
379 unsigned int i;
380 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
381 free(dmi_strings[i].value);
382 dmi_strings[i].value = NULL;
383 }
384 return 0;
385}
386
387void dmi_init(void)
388{
389 /* Register shutdown function before we allocate anything. */
390 if (register_shutdown(dmi_shutdown, NULL)) {
391 msg_pwarn("Warning: Could not register DMI shutdown function - continuing without DMI info.\n");
392 return;
393 }
394
395 /* dmi_fill fills the dmi_strings array, and if possible sets the global is_laptop variable. */
396 if (dmi_fill() != 0)
397 return;
stefanct674e81e2011-07-26 00:54:42 +0000398
399 switch (is_laptop) {
400 case 1:
401 msg_pdbg("Laptop detected via DMI.\n");
402 break;
403 case 2:
404 msg_pdbg("DMI chassis-type is not specific enough.\n");
405 break;
406 }
Kangheui Wonb1798232019-11-27 16:31:54 +1100407
408 has_dmi_support = 1;
409 unsigned int i;
410 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
411 msg_pdbg("DMI string %s: \"%s\"\n", dmi_strings[i].keyword,
412 (dmi_strings[i].value == NULL) ? "" : dmi_strings[i].value);
413 }
mkarcher803b4042010-01-20 14:14:11 +0000414}
415
416/**
417 * Does an substring/prefix/postfix/whole-string match.
418 *
419 * The pattern is matched as-is. The only metacharacters supported are '^'
420 * at the beginning and '$' at the end. So you can look for "^prefix",
421 * "suffix$", "substring" or "^complete string$".
422 *
Kangheui Wonb1798232019-11-27 16:31:54 +1100423 * @param value The non-NULL string to check.
424 * @param pattern The non-NULL pattern.
mkarcher803b4042010-01-20 14:14:11 +0000425 * @return Nonzero if pattern matches.
426 */
427static int dmi_compare(const char *value, const char *pattern)
428{
hailfingerb91c08c2011-08-15 19:54:20 +0000429 int anchored = 0;
430 int patternlen;
uwef6f94d42010-03-13 17:28:29 +0000431
snelsone42c3802010-05-07 20:09:04 +0000432 msg_pspew("matching %s against %s\n", value, pattern);
uwef6f94d42010-03-13 17:28:29 +0000433 /* The empty string is part of all strings! */
mkarcher803b4042010-01-20 14:14:11 +0000434 if (pattern[0] == 0)
435 return 1;
436
437 if (pattern[0] == '^') {
438 anchored = 1;
439 pattern++;
440 }
441
442 patternlen = strlen(pattern);
443 if (pattern[patternlen - 1] == '$') {
444 int valuelen = strlen(value);
445 patternlen--;
uwef6f94d42010-03-13 17:28:29 +0000446 if (patternlen > valuelen)
mkarcher803b4042010-01-20 14:14:11 +0000447 return 0;
448
449 /* full string match: require same length */
uwef6f94d42010-03-13 17:28:29 +0000450 if (anchored && (valuelen != patternlen))
mkarcher803b4042010-01-20 14:14:11 +0000451 return 0;
452
453 /* start character to make ends match */
454 value += valuelen - patternlen;
455 anchored = 1;
456 }
457
458 if (anchored)
459 return strncmp(value, pattern, patternlen) == 0;
460 else
461 return strstr(value, pattern) != NULL;
462}
463
464int dmi_match(const char *pattern)
465{
Kangheui Wonb1798232019-11-27 16:31:54 +1100466 unsigned int i;
uwef6f94d42010-03-13 17:28:29 +0000467
mkarcher803b4042010-01-20 14:14:11 +0000468 if (!has_dmi_support)
469 return 0;
470
Kangheui Wonb1798232019-11-27 16:31:54 +1100471 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
472 if (dmi_strings[i].value == NULL)
473 continue;
474
475 if (dmi_compare(dmi_strings[i].value, pattern))
mkarcherdec4dbf2010-02-12 05:44:18 +0000476 return 1;
Kangheui Wonb1798232019-11-27 16:31:54 +1100477 }
mkarcher803b4042010-01-20 14:14:11 +0000478
479 return 0;
480}