blob: ef7ae2c88e9269d19eead4e0dd2a4a6955216b24 [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
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000024#include <strings.h>
Michael Karcher6701ee82010-01-20 14:14:11 +000025#include <string.h>
26#include <stdio.h>
27#include <stdlib.h>
28
29#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000030#include "programmer.h"
Michael Karcher6701ee82010-01-20 14:14:11 +000031
Sean Nelson4c6d3a42013-09-11 23:35:03 +000032#if defined(__i386__) || defined(__x86_64__)
33
34/* Enable SMBIOS decoding. Currently legacy DMI decoding is enough. */
35#define SM_SUPPORT 0
36
37/* Strings longer than 4096 in DMI are just insane. */
38#define DMI_MAX_ANSWER_LEN 4096
39
Carl-Daniel Hailfingere1fdff42010-06-23 23:14:44 +000040int has_dmi_support = 0;
41
Sean Nelson4c6d3a42013-09-11 23:35:03 +000042static struct {
43 const char *const keyword;
44 const uint8_t type;
45 const uint8_t offset;
46 char *value;
47} dmi_strings[] = {
48 { "system-manufacturer", 1, 0x04, NULL },
49 { "system-product-name", 1, 0x05, NULL },
50 { "system-version", 1, 0x06, NULL },
51 { "baseboard-manufacturer", 2, 0x04, NULL },
52 { "baseboard-product-name", 2, 0x05, NULL },
53 { "baseboard-version", 2, 0x06, NULL },
Michael Karcher6701ee82010-01-20 14:14:11 +000054};
55
Stefan Taunera34d7192011-07-26 00:54:42 +000056/* This list is used to identify supposed laptops. The is_laptop field has the
57 * following meaning:
58 * - 0: in all likelihood not a laptop
59 * - 1: in all likelihood a laptop
60 * - 2: chassis-type is not specific enough
61 * A full list of chassis types can be found in the System Management BIOS
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000062 * (SMBIOS) Reference Specification 2.7.0 section 7.4.1 "Chassis Types" at
63 * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf
64 * The types below are the most common ones.
65 */
66static const struct {
Sean Nelson4c6d3a42013-09-11 23:35:03 +000067 uint8_t type;
68 uint8_t is_laptop;
69 char *name;
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000070} dmi_chassis_types[] = {
Stefan Taunera34d7192011-07-26 00:54:42 +000071 {0x01, 2, "Other"},
72 {0x02, 2, "Unknown"},
Stefan Tauner4c5665f2012-02-17 20:03:37 +000073 {0x03, 0, "Desktop"},
74 {0x04, 0, "Low Profile Desktop"},
Stefan Taunera34d7192011-07-26 00:54:42 +000075 {0x06, 0, "Mini Tower"},
76 {0x07, 0, "Tower"},
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000077 {0x08, 1, "Portable"},
78 {0x09, 1, "Laptop"},
79 {0x0a, 1, "Notebook"},
80 {0x0b, 1, "Hand Held"},
81 {0x0e, 1, "Sub Notebook"},
Stefan Taunera34d7192011-07-26 00:54:42 +000082 {0x11, 0, "Main Server Chassis"},
83 {0x17, 0, "Rack Mount Chassis"},
Sylvain "ythier" Hitier3093f8f2011-09-03 11:22:27 +000084 {0x18, 0, "Sealed-case PC"}, /* used by Supermicro (X8SIE) */
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +000085};
86
Sean Nelson4c6d3a42013-09-11 23:35:03 +000087#if CONFIG_INTERNAL_DMI == 1
88#ifdef __DJGPP__ /* There is no strnlen in DJGPP. FIXME: Move this to a common utility file. */
89size_t strnlen(const char *str, size_t n)
90{
91 size_t i;
92 for (i = 0; i < n && str[i] != '\0'; i++)
93 ;
94 return i;
95}
96#endif
97
98static 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
108static char *dmi_string(uint8_t *buf, uint8_t string_id, uint8_t *limit)
109{
110 size_t i, len;
111
112 if (string_id == 0)
113 return "Not Specified";
114
115 while (string_id > 1 && string_id--) {
116 if (buf > limit) {
117 msg_perr("DMI table is broken (string portion out of bounds)!\n");
118 return "<OUT OF BOUNDS>";
119 }
120 buf += strnlen((char *)buf, limit - buf) + 1;
121 }
122
123 if (!*buf) /* as long as the current byte we're on isn't null */
124 return "<BAD INDEX>";
125
126 len = strnlen((char *)buf, limit - buf);
127 if (len > DMI_MAX_ANSWER_LEN)
128 len = DMI_MAX_ANSWER_LEN;
129
130 /* fix junk bytes in the string */
131 for (i = 0; i < len; i++)
132 if (buf[i] < 32 || buf[i] >= 127)
133 buf[i] = ' ';
134
135 return (char *)buf;
136}
137
138static void dmi_chassis_type(uint8_t code)
139{
140 int i;
141 code &= 0x7f; /* bits 6:0 are chassis type, 7th bit is the lock bit */
142 is_laptop = 2;
143 for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
144 if (code == dmi_chassis_types[i].type) {
145 msg_pdbg("DMI string chassis-type: \"%s\"\n", dmi_chassis_types[i].name);
146 is_laptop = dmi_chassis_types[i].is_laptop;
147 break;
148 }
149 }
150}
151
152static void dmi_table(uint32_t base, uint16_t len, uint16_t num)
153{
154 int i = 0, j = 0;
155
156 uint8_t *dmi_table_mem = physmap_round("DMI Table", base, len);
157 if (dmi_table_mem == NULL) {
158 msg_perr("Unable to access DMI Table\n");
159 return;
160 }
161
162 uint8_t *data = dmi_table_mem;
163 uint8_t *limit = dmi_table_mem + len;
164
165 /* SMBIOS structure header is always 4 B long and contains:
166 * - uint8_t type; // see dmi_chassis_types's type
167 * - uint8_t length; // data section w/ header w/o strings
168 * - uint16_t handle;
169 */
170 while (i < num && data + 4 <= limit) {
171 /* - If a short entry is found (less than 4 bytes), not only it
172 * is invalid, but we cannot reliably locate the next entry.
173 * - If the length value indicates that this structure spreads
174 * accross the table border, something is fishy too.
175 * Better stop at this point, and let the user know his/her
176 * table is broken.
177 */
178 if (data[1] < 4 || data + data[1] > limit) {
179 msg_perr("DMI table is broken (bogus header)!\n");
180 break;
181 }
182
183 if(data[0] == 3) {
184 if (data + 5 <= limit)
185 dmi_chassis_type(data[5]);
186 /* else the table is broken, but laptop detection is optional, hence continue. */
187 } else
188 for (j = 0; j < ARRAY_SIZE(dmi_strings); j++) {
189 uint8_t offset = dmi_strings[j].offset;
190 uint8_t type = dmi_strings[j].type;
191
192 if (data[0] != type)
193 continue;
194
195 if (data[1] <= offset || data+offset > limit) {
196 msg_perr("DMI table is broken (offset out of bounds)!\n");
197 goto out;
198 }
199
200 /* Table will be unmapped, hence fill the struct with duplicated strings. */
201 dmi_strings[j].value = strdup(dmi_string(data + data[1], data[offset], limit));
202 }
203 /* Find next structure by skipping data and string sections */
204 data += data[1];
205 while (data + 1 <= limit) {
206 if (data[0] == 0 && data[1] == 0)
207 break;
208 data++;
209 }
210 data += 2;
211 i++;
212 }
213out:
214 physunmap(dmi_table_mem, len);
215}
216
217#if SM_SUPPORT
218static int smbios_decode(uint8_t *buf)
219{
220 /* TODO: other checks mentioned in the conformance guidelines? */
221 if (!dmi_checksum(buf, buf[0x05]) ||
222 (memcmp(buf + 0x10, "_DMI_", 5) != 0) ||
223 !dmi_checksum(buf + 0x10, 0x0F))
224 return 0;
225
226 dmi_table(mmio_readl(buf + 0x18), mmio_readw(buf + 0x16), mmio_readw(buf + 0x1C));
227
228 return 1;
229}
230#endif
231
232static int legacy_decode(uint8_t *buf)
233{
234 if (!dmi_checksum(buf, 0x0F))
235 return 1;
236
237 dmi_table(mmio_readl(buf + 0x08), mmio_readw(buf + 0x06), mmio_readw(buf + 0x0C));
238
239 return 0;
240}
241
242int dmi_fill(void)
243{
244 size_t fp;
245 uint8_t *dmi_mem;
246 int ret = 1;
247
248 msg_pdbg("Using Internal DMI decoder.\n");
249 /* There are two ways specified to gain access to the SMBIOS table:
250 * - EFI's configuration table contains a pointer to the SMBIOS table. On linux it can be obtained from
251 * sysfs. EFI's SMBIOS GUID is: {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,0x0,0x90,0x27,0x3f,0xc1,0x4d}
252 * - Scanning physical memory address range 0x000F0000h to 0x000FFFFF for the anchor-string(s). */
Niklas Söderlund5d307202013-09-14 09:02:27 +0000253 dmi_mem = physmap_ro("DMI", 0xF0000, 0x10000);
254 if (dmi_mem == ERROR_PTR)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000255 return ret;
256
257 for (fp = 0; fp <= 0xFFF0; fp += 16) {
258#if SM_SUPPORT
259 if (memcmp(dmi_mem + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
260 if (smbios_decode(dmi_mem + fp)) // FIXME: length check
261 goto out;
262 } else
263#endif
264 if (memcmp(dmi_mem + fp, "_DMI_", 5) == 0)
265 if (legacy_decode(dmi_mem + fp) == 0) {
266 ret = 0;
267 goto out;
268 }
269 }
270 msg_pinfo("No DMI table found.\n");
271out:
272 physunmap(dmi_mem, 0x10000);
273 return ret;
274}
275
276#else /* CONFIG_INTERNAL_DMI */
277
278#define DMI_COMMAND_LEN_MAX 300
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000279static const char *dmidecode_command = "dmidecode";
Michael Karcher6701ee82010-01-20 14:14:11 +0000280
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000281static char *get_dmi_string(const char *string_name)
Michael Karcher6701ee82010-01-20 14:14:11 +0000282{
283 FILE *dmidecode_pipe;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000284 char *result;
285 char answerbuf[DMI_MAX_ANSWER_LEN];
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000286 char commandline[DMI_COMMAND_LEN_MAX];
Uwe Hermann43959702010-03-13 17:28:29 +0000287
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000288 snprintf(commandline, sizeof(commandline),
289 "%s -s %s", dmidecode_command, string_name);
290 dmidecode_pipe = popen(commandline, "r");
291 if (!dmidecode_pipe) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000292 msg_perr("Opening DMI pipe failed!\n");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000293 return NULL;
Michael Karcher6701ee82010-01-20 14:14:11 +0000294 }
Michael Karcherd9f266d2010-08-08 21:56:52 +0000295
296 /* Kill lines starting with '#', as recent dmidecode versions
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000297 * have the quirk to emit a "# SMBIOS implementations newer..."
298 * message even on "-s" if the SMBIOS declares a
299 * newer-than-supported version number, while it *should* only print
300 * the requested string.
301 */
Michael Karcherd9f266d2010-08-08 21:56:52 +0000302 do {
303 if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000304 if (ferror(dmidecode_pipe)) {
Michael Karcherd9f266d2010-08-08 21:56:52 +0000305 msg_perr("DMI pipe read error\n");
306 pclose(dmidecode_pipe);
307 return NULL;
308 } else {
309 answerbuf[0] = 0; /* Hit EOF */
310 }
Michael Karcher45f79cb2010-03-24 17:55:04 +0000311 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000312 } while (answerbuf[0] == '#');
Michael Karcherd9f266d2010-08-08 21:56:52 +0000313
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000314 /* Discard all output exceeding DMI_MAX_ANSWER_LEN to prevent deadlock on pclose. */
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000315 while (!feof(dmidecode_pipe))
316 getc(dmidecode_pipe);
317 if (pclose(dmidecode_pipe) != 0) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000318 msg_pwarn("dmidecode execution unsuccessful - continuing without DMI info\n");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000319 return NULL;
320 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000321
Uwe Hermann43959702010-03-13 17:28:29 +0000322 /* Chomp trailing newline. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000323 if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n')
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000324 answerbuf[strlen(answerbuf) - 1] = 0;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000325
326 result = strdup(answerbuf);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000327 if (result == NULL)
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000328 msg_pwarn("Warning: Out of memory - DMI support fails");
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000329
330 return result;
331}
332
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000333int dmi_fill(void)
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000334{
335 int i;
Michael Karcher8c1df282010-02-26 09:51:20 +0000336 char *chassis_type;
Uwe Hermann43959702010-03-13 17:28:29 +0000337
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000338 msg_pdbg("Using External DMI decoder.\n");
339 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
340 dmi_strings[i].value = get_dmi_string(dmi_strings[i].keyword);
341 if (dmi_strings[i].value == NULL)
342 return 1;
Michael Karcher0d7fb7c2010-02-26 09:51:16 +0000343 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000344
345 chassis_type = get_dmi_string("chassis-type");
Stefan Taunera34d7192011-07-26 00:54:42 +0000346 if (chassis_type == NULL)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000347 return 0; /* chassis-type handling is optional anyway */
Stefan Taunera34d7192011-07-26 00:54:42 +0000348
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000349 msg_pdbg("DMI string chassis-type: \"%s\"\n", chassis_type);
Stefan Taunera34d7192011-07-26 00:54:42 +0000350 is_laptop = 2;
351 for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
352 if (strcasecmp(chassis_type, dmi_chassis_types[i].name) == 0) {
353 is_laptop = dmi_chassis_types[i].is_laptop;
354 break;
Carl-Daniel Hailfingercb3eb052010-09-26 21:43:53 +0000355 }
Michael Karcher8c1df282010-02-26 09:51:20 +0000356 }
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000357 free(chassis_type);
358 return 0;
359}
360
361#endif /* CONFIG_INTERNAL_DMI */
362
363static int dmi_shutdown(void *data)
364{
365 int i;
366 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
367 free(dmi_strings[i].value);
368 dmi_strings[i].value = NULL;
369 }
370 return 0;
371}
372
373void dmi_init(void)
374{
375 /* Register shutdown function before we allocate anything. */
376 if (register_shutdown(dmi_shutdown, NULL)) {
377 msg_pwarn("Warning: Could not register DMI shutdown function - continuing without DMI info.\n");
378 return;
379 }
380
381 /* dmi_fill fills the dmi_strings array, and if possible sets the global is_laptop variable. */
382 if (dmi_fill() != 0)
383 return;
Stefan Taunera34d7192011-07-26 00:54:42 +0000384
385 switch (is_laptop) {
386 case 1:
387 msg_pdbg("Laptop detected via DMI.\n");
388 break;
389 case 2:
390 msg_pdbg("DMI chassis-type is not specific enough.\n");
391 break;
392 }
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000393
394 has_dmi_support = 1;
395 int i;
396 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
397 msg_pdbg("DMI string %s: \"%s\"\n", dmi_strings[i].keyword,
398 (dmi_strings[i].value == NULL) ? "" : dmi_strings[i].value);
399 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000400}
401
402/**
403 * Does an substring/prefix/postfix/whole-string match.
404 *
405 * The pattern is matched as-is. The only metacharacters supported are '^'
406 * at the beginning and '$' at the end. So you can look for "^prefix",
407 * "suffix$", "substring" or "^complete string$".
408 *
Stefan Taunerd1045d82013-10-29 01:38:45 +0000409 * @param value The non-NULL string to check.
410 * @param pattern The non-NULL pattern.
Michael Karcher6701ee82010-01-20 14:14:11 +0000411 * @return Nonzero if pattern matches.
412 */
413static int dmi_compare(const char *value, const char *pattern)
414{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000415 int anchored = 0;
416 int patternlen;
Uwe Hermann43959702010-03-13 17:28:29 +0000417
Sean Nelson316a29f2010-05-07 20:09:04 +0000418 msg_pspew("matching %s against %s\n", value, pattern);
Uwe Hermann43959702010-03-13 17:28:29 +0000419 /* The empty string is part of all strings! */
Michael Karcher6701ee82010-01-20 14:14:11 +0000420 if (pattern[0] == 0)
421 return 1;
422
423 if (pattern[0] == '^') {
424 anchored = 1;
425 pattern++;
426 }
427
428 patternlen = strlen(pattern);
429 if (pattern[patternlen - 1] == '$') {
430 int valuelen = strlen(value);
431 patternlen--;
Uwe Hermann43959702010-03-13 17:28:29 +0000432 if (patternlen > valuelen)
Michael Karcher6701ee82010-01-20 14:14:11 +0000433 return 0;
434
435 /* full string match: require same length */
Uwe Hermann43959702010-03-13 17:28:29 +0000436 if (anchored && (valuelen != patternlen))
Michael Karcher6701ee82010-01-20 14:14:11 +0000437 return 0;
438
439 /* start character to make ends match */
440 value += valuelen - patternlen;
441 anchored = 1;
442 }
443
444 if (anchored)
445 return strncmp(value, pattern, patternlen) == 0;
446 else
447 return strstr(value, pattern) != NULL;
448}
449
450int dmi_match(const char *pattern)
451{
452 int i;
Uwe Hermann43959702010-03-13 17:28:29 +0000453
Michael Karcher6701ee82010-01-20 14:14:11 +0000454 if (!has_dmi_support)
455 return 0;
456
Stefan Taunerd1045d82013-10-29 01:38:45 +0000457 for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
458 if (dmi_strings[i].value == NULL)
459 continue;
460
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000461 if (dmi_compare(dmi_strings[i].value, pattern))
Michael Karcher4dfa0932010-02-12 05:44:18 +0000462 return 1;
Stefan Taunerd1045d82013-10-29 01:38:45 +0000463 }
Michael Karcher6701ee82010-01-20 14:14:11 +0000464
465 return 0;
466}
Carl-Daniel Hailfingere1fdff42010-06-23 23:14:44 +0000467
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000468#endif // defined(__i386__) || defined(__x86_64__)