1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2000-2002 Alan Cox <[email protected]>
5 * Copyright (C) 2002-2010 Jean Delvare <[email protected]>
6 * Copyright (C) 2009,2010 Michael Karcher
7 * Copyright (C) 2011-2013 Stefan Tauner
8 *
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
20 /* 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
27 #include <strings.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include "flash.h"
35 #include "hwaccess_physmap.h"
36 #include "programmer.h"
37
38 /* 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
44 static bool g_has_dmi_support = false;
45
dmi_is_supported(void)46 bool dmi_is_supported(void)
47 {
48 return g_has_dmi_support;
49 }
50
51 static 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 },
63 };
64
65 /* 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
71 * (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 */
75 static const struct {
76 uint8_t type;
77 uint8_t is_laptop;
78 const char *name;
79 } dmi_chassis_types[] = {
80 {0x01, 2, "Other"},
81 {0x02, 2, "Unknown"},
82 {0x03, 0, "Desktop"},
83 {0x04, 0, "Low Profile Desktop"},
84 {0x06, 0, "Mini Tower"},
85 {0x07, 0, "Tower"},
86 {0x08, 1, "Portable"},
87 {0x09, 1, "Laptop"},
88 {0x0a, 1, "Notebook"},
89 {0x0b, 1, "Hand Held"},
90 {0x0e, 1, "Sub Notebook"},
91 {0x11, 0, "Main Server Chassis"},
92 {0x17, 0, "Rack Mount Chassis"},
93 {0x18, 0, "Sealed-case PC"}, /* used by Supermicro (X8SIE) */
94 {0x19, 0, "Multi-system"}, /* used by Supermicro (X7DWT) */
95 };
96
97 #if CONFIG_INTERNAL_DMI == 1
dmi_checksum(const uint8_t * const buf,size_t len)98 static 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
108 /** 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 */
dmi_string(const char * buf,uint8_t string_id,const char * limit)117 static char *dmi_string(const char *buf, uint8_t string_id, const char *limit)
118 {
119 size_t i, len;
120
121 if (string_id == 0)
122 return strdup("Not Specified");
123
124 while (string_id > 1 && string_id--) {
125 if (buf >= limit) {
126 msg_perr("DMI table is broken (string portion out of bounds)!\n");
127 return strdup("<OUT OF BOUNDS>");
128 }
129 buf += strnlen(buf, limit - buf) + 1;
130 }
131
132 if (!*buf) /* as long as the current byte we're on isn't null */
133 return strdup("<BAD INDEX>");
134
135 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 }
141
142 /* fix junk bytes in the string */
143 for (i = 0; i < len && buf[i] != '\0'; i++) {
144 if (isprint((unsigned char)buf[i]))
145 newbuf[i] = buf[i];
146 else
147 newbuf[i] = ' ';
148 }
149 newbuf[i] = '\0';
150
151 return newbuf;
152 }
153
dmi_chassis_type(uint8_t code)154 static int dmi_chassis_type(uint8_t code)
155 {
156 unsigned int i;
157 code &= 0x7f; /* bits 6:0 are chassis type, 7th bit is the lock bit */
158 int 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 return is_laptop;
167 }
168
dmi_table(uint32_t base,uint16_t len,uint16_t num,int * is_laptop)169 static void dmi_table(uint32_t base, uint16_t len, uint16_t num, int *is_laptop)
170 {
171 unsigned int i = 0, j = 0;
172
173 uint8_t *dmi_table_mem = physmap_ro("DMI Table", base, len);
174 if (dmi_table_mem == ERROR_PTR) {
175 msg_perr("Unable to access DMI Table\n");
176 return;
177 }
178
179 uint8_t *data = dmi_table_mem;
180 uint8_t *limit = dmi_table_mem + len;
181
182 /* SMBIOS structure header is always 4 B long and contains:
183 * - uint8_t type; // see dmi_chassis_types's type
184 * - uint8_t length; // data section w/ header w/o strings
185 * - uint16_t handle;
186 */
187 while (i < num && data + 4 < limit) {
188 /* - If a short entry is found (less than 4 bytes), not only it
189 * is invalid, but we cannot reliably locate the next entry.
190 * - If the length value indicates that this structure spreads
191 * across the table border, something is fishy too.
192 * Better stop at this point, and let the user know their
193 * table is broken.
194 */
195 if (data[1] < 4 || data + data[1] >= limit) {
196 msg_perr("DMI table is broken (bogus header)!\n");
197 break;
198 }
199
200 if(data[0] == 3) {
201 if (data + 5 < limit)
202 *is_laptop = dmi_chassis_type(data[5]);
203 else /* the table is broken, but laptop detection is optional, hence continue. */
204 msg_pwarn("DMI table is broken (chassis_type out of bounds)!\n");
205 } else
206 for (j = 0; j < ARRAY_SIZE(dmi_strings); j++) {
207 uint8_t offset = dmi_strings[j].offset;
208 uint8_t type = dmi_strings[j].type;
209
210 if (data[0] != type)
211 continue;
212
213 if (data[1] <= offset || data + offset >= limit) {
214 msg_perr("DMI table is broken (offset out of bounds)!\n");
215 goto out;
216 }
217
218 dmi_strings[j].value = dmi_string((const char *)(data + data[1]), data[offset],
219 (const char *)limit);
220 }
221 /* Find next structure by skipping data and string sections */
222 data += data[1];
223 while (data + 1 <= limit) {
224 if (data[0] == 0 && data[1] == 0)
225 break;
226 data++;
227 }
228 data += 2;
229 i++;
230 }
231 out:
232 physunmap(dmi_table_mem, len);
233 }
234
235 #if SM_SUPPORT
smbios_decode(uint8_t * buf,int * is_laptop)236 static int smbios_decode(uint8_t *buf, int *is_laptop)
237 {
238 /* TODO: other checks mentioned in the conformance guidelines? */
239 if (!dmi_checksum(buf, buf[0x05]) ||
240 (memcmp(buf + 0x10, "_DMI_", 5) != 0) ||
241 !dmi_checksum(buf + 0x10, 0x0F))
242 return 0;
243
244 dmi_table(mmio_readl(buf + 0x18), mmio_readw(buf + 0x16), mmio_readw(buf + 0x1C), is_laptop);
245
246 return 1;
247 }
248 #endif
249
legacy_decode(uint8_t * buf,int * is_laptop)250 static int legacy_decode(uint8_t *buf, int *is_laptop)
251 {
252 if (!dmi_checksum(buf, 0x0F))
253 return 1;
254
255 dmi_table(mmio_readl(buf + 0x08), mmio_readw(buf + 0x06), mmio_readw(buf + 0x0C), is_laptop);
256
257 return 0;
258 }
259
dmi_fill(int * is_laptop)260 static int dmi_fill(int *is_laptop)
261 {
262 size_t fp;
263 uint8_t *dmi_mem;
264 int ret = 1;
265
266 msg_pdbg("Using Internal DMI decoder.\n");
267 /* There are two ways specified to gain access to the SMBIOS table:
268 * - EFI's configuration table contains a pointer to the SMBIOS table. On linux it can be obtained from
269 * sysfs. EFI's SMBIOS GUID is: {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,0x0,0x90,0x27,0x3f,0xc1,0x4d}
270 * - Scanning physical memory address range 0x000F0000h to 0x000FFFFF for the anchor-string(s). */
271 dmi_mem = physmap_ro("DMI", 0xF0000, 0x10000);
272 if (dmi_mem == ERROR_PTR)
273 return ret;
274
275 for (fp = 0; fp <= 0xFFF0; fp += 16) {
276 #if SM_SUPPORT
277 if (memcmp(dmi_mem + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
278 if (smbios_decode(dmi_mem + fp), is_laptop) // FIXME: length check
279 goto out;
280 } else
281 #endif
282 if (memcmp(dmi_mem + fp, "_DMI_", 5) == 0)
283 if (legacy_decode(dmi_mem + fp, is_laptop) == 0) {
284 ret = 0;
285 goto out;
286 }
287 }
288 msg_pinfo("No DMI table found.\n");
289 out:
290 physunmap(dmi_mem, 0x10000);
291 return ret;
292 }
293
294 #else /* CONFIG_INTERNAL_DMI */
295
296 #define DMI_COMMAND_LEN_MAX 300
297 #if IS_WINDOWS
298 static const char *dmidecode_command = "dmidecode.exe 2>NUL";
299 #else
300 static const char *dmidecode_command = "dmidecode 2>/dev/null";
301 #endif
302
get_dmi_string(const char * string_name)303 static char *get_dmi_string(const char *string_name)
304 {
305 FILE *dmidecode_pipe;
306 char *result;
307 char answerbuf[DMI_MAX_ANSWER_LEN];
308 char commandline[DMI_COMMAND_LEN_MAX];
309
310 snprintf(commandline, sizeof(commandline),
311 "%s -s %s", dmidecode_command, string_name);
312 dmidecode_pipe = popen(commandline, "r");
313 if (!dmidecode_pipe) {
314 msg_perr("Opening DMI pipe failed!\n");
315 return NULL;
316 }
317
318 /* Kill lines starting with '#', as recent dmidecode versions
319 * have the quirk to emit a "# SMBIOS implementations newer..."
320 * message even on "-s" if the SMBIOS declares a
321 * newer-than-supported version number, while it *should* only print
322 * the requested string.
323 */
324 do {
325 if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
326 if (ferror(dmidecode_pipe)) {
327 msg_perr("DMI pipe read error\n");
328 pclose(dmidecode_pipe);
329 return NULL;
330 }
331 answerbuf[0] = 0; /* Hit EOF */
332 }
333 } while (answerbuf[0] == '#');
334
335 /* Discard all output exceeding DMI_MAX_ANSWER_LEN to prevent deadlock on pclose. */
336 while (!feof(dmidecode_pipe))
337 getc(dmidecode_pipe);
338 if (pclose(dmidecode_pipe) != 0) {
339 msg_pwarn("dmidecode execution unsuccessful - continuing without DMI info\n");
340 return NULL;
341 }
342
343 /* Chomp trailing newline. */
344 if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n')
345 answerbuf[strlen(answerbuf) - 1] = 0;
346
347 result = strdup(answerbuf);
348 if (result == NULL)
349 msg_pwarn("Warning: Out of memory - DMI support fails");
350
351 return result;
352 }
353
dmi_fill(int * is_laptop)354 static int dmi_fill(int *is_laptop)
355 {
356 unsigned int i;
357 char *chassis_type;
358
359 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;
364 }
365
366 chassis_type = get_dmi_string("chassis-type");
367 if (chassis_type == NULL)
368 return 0; /* chassis-type handling is optional anyway */
369
370 msg_pdbg("DMI string chassis-type: \"%s\"\n", chassis_type);
371 *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;
376 }
377 }
378 free(chassis_type);
379 return 0;
380 }
381
382 #endif /* CONFIG_INTERNAL_DMI */
383
dmi_shutdown(void * data)384 static int dmi_shutdown(void *data)
385 {
386 unsigned 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 g_has_dmi_support = false;
392 return 0;
393 }
394
dmi_init(int * is_laptop)395 void dmi_init(int *is_laptop)
396 {
397 /* Register shutdown function before we allocate anything. */
398 if (register_shutdown(dmi_shutdown, NULL)) {
399 msg_pwarn("Warning: Could not register DMI shutdown function - continuing without DMI info.\n");
400 return;
401 }
402
403 /* dmi_fill fills the dmi_strings array, and if possible set the is_laptop parameter. */
404 if (dmi_fill(is_laptop) != 0)
405 return;
406
407 switch (*is_laptop) {
408 case 1:
409 msg_pdbg("Laptop detected via DMI.\n");
410 break;
411 case 2:
412 msg_pdbg("DMI chassis-type is not specific enough.\n");
413 break;
414 }
415
416 g_has_dmi_support = true;
417 unsigned int i;
418 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
419 msg_pdbg("DMI string %s: \"%s\"\n", dmi_strings[i].keyword,
420 (dmi_strings[i].value == NULL) ? "" : dmi_strings[i].value);
421 }
422 }
423
424 /**
425 * Does an substring/prefix/postfix/whole-string match.
426 *
427 * The pattern is matched as-is. The only metacharacters supported are '^'
428 * at the beginning and '$' at the end. So you can look for "^prefix",
429 * "suffix$", "substring" or "^complete string$".
430 *
431 * @param value The non-NULL string to check.
432 * @param pattern The non-NULL pattern.
433 * @return Nonzero if pattern matches.
434 */
dmi_compare(const char * value,const char * pattern)435 static int dmi_compare(const char *value, const char *pattern)
436 {
437 bool anchored = false;
438 int patternlen;
439
440 msg_pspew("matching %s against %s\n", value, pattern);
441 /* The empty string is part of all strings! */
442 if (pattern[0] == 0)
443 return 1;
444
445 if (pattern[0] == '^') {
446 anchored = true;
447 pattern++;
448 }
449
450 patternlen = strlen(pattern);
451 if (pattern[patternlen - 1] == '$') {
452 int valuelen = strlen(value);
453 patternlen--;
454 if (patternlen > valuelen)
455 return 0;
456
457 /* full string match: require same length */
458 if (anchored && (valuelen != patternlen))
459 return 0;
460
461 /* start character to make ends match */
462 value += valuelen - patternlen;
463 anchored = true;
464 }
465
466 if (anchored)
467 return strncmp(value, pattern, patternlen) == 0;
468 else
469 return strstr(value, pattern) != NULL;
470 }
471
dmi_match(const char * pattern)472 int dmi_match(const char *pattern)
473 {
474 unsigned int i;
475
476 if (!dmi_is_supported())
477 return 0;
478
479 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
480 if (dmi_strings[i].value == NULL)
481 continue;
482
483 if (dmi_compare(dmi_strings[i].value, pattern))
484 return 1;
485 }
486
487 return 0;
488 }
489