Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 4 | * Copyright (C) 2000-2002 Alan Cox <alan@redhat.com> |
| 5 | * Copyright (C) 2002-2010 Jean Delvare <khali@linux-fr.org> |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009,2010 Michael Karcher |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 7 | * Copyright (C) 2011-2013 Stefan Tauner |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 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. |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Stefan Tauner | dc62793 | 2015-01-27 18:07:50 +0000 | [diff] [blame] | 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 | |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 27 | #include <strings.h> |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 28 | #include <string.h> |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 29 | #include <ctype.h> |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | |
Stefan Tauner | 4f44479 | 2011-06-27 11:44:03 +0000 | [diff] [blame] | 33 | #include "platform.h" |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 34 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 35 | #include "programmer.h" |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 36 | |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 37 | #if defined(__i386__) || defined(__x86_64__) |
| 38 | |
| 39 | /* Enable SMBIOS decoding. Currently legacy DMI decoding is enough. */ |
| 40 | #define SM_SUPPORT 0 |
| 41 | |
| 42 | /* Strings longer than 4096 in DMI are just insane. */ |
| 43 | #define DMI_MAX_ANSWER_LEN 4096 |
| 44 | |
Carl-Daniel Hailfinger | e1fdff4 | 2010-06-23 23:14:44 +0000 | [diff] [blame] | 45 | int has_dmi_support = 0; |
| 46 | |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 47 | static struct { |
| 48 | const char *const keyword; |
| 49 | const uint8_t type; |
| 50 | const uint8_t offset; |
| 51 | char *value; |
| 52 | } dmi_strings[] = { |
| 53 | { "system-manufacturer", 1, 0x04, NULL }, |
| 54 | { "system-product-name", 1, 0x05, NULL }, |
| 55 | { "system-version", 1, 0x06, NULL }, |
| 56 | { "baseboard-manufacturer", 2, 0x04, NULL }, |
| 57 | { "baseboard-product-name", 2, 0x05, NULL }, |
| 58 | { "baseboard-version", 2, 0x06, NULL }, |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Stefan Tauner | a34d719 | 2011-07-26 00:54:42 +0000 | [diff] [blame] | 61 | /* This list is used to identify supposed laptops. The is_laptop field has the |
| 62 | * following meaning: |
Elyes HAOUAS | ac01baa | 2018-05-28 16:52:21 +0200 | [diff] [blame] | 63 | * - 0: in all likelihood not a laptop |
| 64 | * - 1: in all likelihood a laptop |
| 65 | * - 2: chassis-type is not specific enough |
Stefan Tauner | a34d719 | 2011-07-26 00:54:42 +0000 | [diff] [blame] | 66 | * A full list of chassis types can be found in the System Management BIOS |
Carl-Daniel Hailfinger | cb3eb05 | 2010-09-26 21:43:53 +0000 | [diff] [blame] | 67 | * (SMBIOS) Reference Specification 2.7.0 section 7.4.1 "Chassis Types" at |
| 68 | * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf |
| 69 | * The types below are the most common ones. |
| 70 | */ |
| 71 | static const struct { |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 72 | uint8_t type; |
| 73 | uint8_t is_laptop; |
| 74 | char *name; |
Carl-Daniel Hailfinger | cb3eb05 | 2010-09-26 21:43:53 +0000 | [diff] [blame] | 75 | } dmi_chassis_types[] = { |
Stefan Tauner | a34d719 | 2011-07-26 00:54:42 +0000 | [diff] [blame] | 76 | {0x01, 2, "Other"}, |
| 77 | {0x02, 2, "Unknown"}, |
Stefan Tauner | 4c5665f | 2012-02-17 20:03:37 +0000 | [diff] [blame] | 78 | {0x03, 0, "Desktop"}, |
| 79 | {0x04, 0, "Low Profile Desktop"}, |
Stefan Tauner | a34d719 | 2011-07-26 00:54:42 +0000 | [diff] [blame] | 80 | {0x06, 0, "Mini Tower"}, |
| 81 | {0x07, 0, "Tower"}, |
Carl-Daniel Hailfinger | cb3eb05 | 2010-09-26 21:43:53 +0000 | [diff] [blame] | 82 | {0x08, 1, "Portable"}, |
| 83 | {0x09, 1, "Laptop"}, |
| 84 | {0x0a, 1, "Notebook"}, |
| 85 | {0x0b, 1, "Hand Held"}, |
| 86 | {0x0e, 1, "Sub Notebook"}, |
Stefan Tauner | a34d719 | 2011-07-26 00:54:42 +0000 | [diff] [blame] | 87 | {0x11, 0, "Main Server Chassis"}, |
| 88 | {0x17, 0, "Rack Mount Chassis"}, |
Sylvain "ythier" Hitier | 3093f8f | 2011-09-03 11:22:27 +0000 | [diff] [blame] | 89 | {0x18, 0, "Sealed-case PC"}, /* used by Supermicro (X8SIE) */ |
Stefan Tauner | 23e10b8 | 2016-01-23 16:16:49 +0000 | [diff] [blame] | 90 | {0x19, 0, "Multi-system"}, /* used by Supermicro (X7DWT) */ |
Carl-Daniel Hailfinger | cb3eb05 | 2010-09-26 21:43:53 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 93 | #if CONFIG_INTERNAL_DMI == 1 |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 94 | static bool dmi_checksum(const uint8_t * const buf, size_t len) |
| 95 | { |
| 96 | uint8_t sum = 0; |
| 97 | size_t a; |
| 98 | |
| 99 | for (a = 0; a < len; a++) |
| 100 | sum += buf[a]; |
| 101 | return (sum == 0); |
| 102 | } |
| 103 | |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 104 | /** Retrieve a DMI string. |
| 105 | * |
| 106 | * See SMBIOS spec. section 6.1.3 "Text strings". |
| 107 | * The table will be unmapped ASAP, hence return a duplicated & sanitized string that needs to be freed later. |
| 108 | * |
| 109 | * \param buf the buffer to search through (usually appended directly to a DMI structure) |
| 110 | * \param string_id index of the string to look for |
| 111 | * \param limit pointer to the first byte beyond \em buf |
| 112 | */ |
| 113 | static char *dmi_string(const char *buf, uint8_t string_id, const char *limit) |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 114 | { |
| 115 | size_t i, len; |
| 116 | |
| 117 | if (string_id == 0) |
Stefan Tauner | 778c6d8 | 2014-09-05 16:14:11 +0000 | [diff] [blame] | 118 | return strdup("Not Specified"); |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 119 | |
| 120 | while (string_id > 1 && string_id--) { |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 121 | if (buf >= limit) { |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 122 | msg_perr("DMI table is broken (string portion out of bounds)!\n"); |
Stefan Tauner | 778c6d8 | 2014-09-05 16:14:11 +0000 | [diff] [blame] | 123 | return strdup("<OUT OF BOUNDS>"); |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 124 | } |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 125 | buf += strnlen(buf, limit - buf) + 1; |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | if (!*buf) /* as long as the current byte we're on isn't null */ |
Stefan Tauner | 778c6d8 | 2014-09-05 16:14:11 +0000 | [diff] [blame] | 129 | return strdup("<BAD INDEX>"); |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 130 | |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 131 | len = strnlen(buf, limit - buf); |
| 132 | char *newbuf = malloc(len + 1); |
| 133 | if (newbuf == NULL) { |
| 134 | msg_perr("Out of memory!\n"); |
| 135 | return NULL; |
| 136 | } |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 137 | |
| 138 | /* fix junk bytes in the string */ |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 139 | for (i = 0; i < len && buf[i] != '\0'; i++) { |
Stefan Tauner | ff9e6c3 | 2014-10-19 07:54:27 +0000 | [diff] [blame] | 140 | if (isprint((unsigned char)buf[i])) |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 141 | newbuf[i] = buf[i]; |
| 142 | else |
| 143 | newbuf[i] = ' '; |
| 144 | } |
| 145 | newbuf[i] = '\0'; |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 146 | |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 147 | return newbuf; |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static void dmi_chassis_type(uint8_t code) |
| 151 | { |
| 152 | int i; |
| 153 | code &= 0x7f; /* bits 6:0 are chassis type, 7th bit is the lock bit */ |
| 154 | is_laptop = 2; |
| 155 | for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) { |
| 156 | if (code == dmi_chassis_types[i].type) { |
| 157 | msg_pdbg("DMI string chassis-type: \"%s\"\n", dmi_chassis_types[i].name); |
| 158 | is_laptop = dmi_chassis_types[i].is_laptop; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | static void dmi_table(uint32_t base, uint16_t len, uint16_t num) |
| 165 | { |
| 166 | int i = 0, j = 0; |
| 167 | |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 168 | uint8_t *dmi_table_mem = physmap_ro("DMI Table", base, len); |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 169 | if (dmi_table_mem == NULL) { |
| 170 | msg_perr("Unable to access DMI Table\n"); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | uint8_t *data = dmi_table_mem; |
| 175 | uint8_t *limit = dmi_table_mem + len; |
| 176 | |
| 177 | /* SMBIOS structure header is always 4 B long and contains: |
| 178 | * - uint8_t type; // see dmi_chassis_types's type |
| 179 | * - uint8_t length; // data section w/ header w/o strings |
| 180 | * - uint16_t handle; |
| 181 | */ |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 182 | while (i < num && data + 4 < limit) { |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 183 | /* - If a short entry is found (less than 4 bytes), not only it |
| 184 | * is invalid, but we cannot reliably locate the next entry. |
| 185 | * - If the length value indicates that this structure spreads |
Stefan Tauner | 6697f71 | 2014-08-06 15:09:15 +0000 | [diff] [blame] | 186 | * across the table border, something is fishy too. |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 187 | * Better stop at this point, and let the user know his/her |
| 188 | * table is broken. |
| 189 | */ |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 190 | if (data[1] < 4 || data + data[1] >= limit) { |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 191 | msg_perr("DMI table is broken (bogus header)!\n"); |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | if(data[0] == 3) { |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 196 | if (data + 5 < limit) |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 197 | dmi_chassis_type(data[5]); |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 198 | else /* the table is broken, but laptop detection is optional, hence continue. */ |
| 199 | msg_pwarn("DMI table is broken (chassis_type out of bounds)!\n"); |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 200 | } else |
| 201 | for (j = 0; j < ARRAY_SIZE(dmi_strings); j++) { |
| 202 | uint8_t offset = dmi_strings[j].offset; |
| 203 | uint8_t type = dmi_strings[j].type; |
| 204 | |
| 205 | if (data[0] != type) |
| 206 | continue; |
| 207 | |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 208 | if (data[1] <= offset || data + offset >= limit) { |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 209 | msg_perr("DMI table is broken (offset out of bounds)!\n"); |
| 210 | goto out; |
| 211 | } |
| 212 | |
Stefan Tauner | 9972d15 | 2014-07-13 12:52:15 +0000 | [diff] [blame] | 213 | dmi_strings[j].value = dmi_string((const char *)(data + data[1]), data[offset], |
| 214 | (const char *)limit); |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 215 | } |
| 216 | /* Find next structure by skipping data and string sections */ |
| 217 | data += data[1]; |
| 218 | while (data + 1 <= limit) { |
| 219 | if (data[0] == 0 && data[1] == 0) |
| 220 | break; |
| 221 | data++; |
| 222 | } |
| 223 | data += 2; |
| 224 | i++; |
| 225 | } |
| 226 | out: |
| 227 | physunmap(dmi_table_mem, len); |
| 228 | } |
| 229 | |
| 230 | #if SM_SUPPORT |
| 231 | static int smbios_decode(uint8_t *buf) |
| 232 | { |
| 233 | /* TODO: other checks mentioned in the conformance guidelines? */ |
| 234 | if (!dmi_checksum(buf, buf[0x05]) || |
| 235 | (memcmp(buf + 0x10, "_DMI_", 5) != 0) || |
| 236 | !dmi_checksum(buf + 0x10, 0x0F)) |
| 237 | return 0; |
| 238 | |
| 239 | dmi_table(mmio_readl(buf + 0x18), mmio_readw(buf + 0x16), mmio_readw(buf + 0x1C)); |
| 240 | |
| 241 | return 1; |
| 242 | } |
| 243 | #endif |
| 244 | |
| 245 | static int legacy_decode(uint8_t *buf) |
| 246 | { |
| 247 | if (!dmi_checksum(buf, 0x0F)) |
| 248 | return 1; |
| 249 | |
| 250 | dmi_table(mmio_readl(buf + 0x08), mmio_readw(buf + 0x06), mmio_readw(buf + 0x0C)); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | int dmi_fill(void) |
| 256 | { |
| 257 | size_t fp; |
| 258 | uint8_t *dmi_mem; |
| 259 | int ret = 1; |
| 260 | |
| 261 | msg_pdbg("Using Internal DMI decoder.\n"); |
| 262 | /* There are two ways specified to gain access to the SMBIOS table: |
| 263 | * - EFI's configuration table contains a pointer to the SMBIOS table. On linux it can be obtained from |
| 264 | * sysfs. EFI's SMBIOS GUID is: {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,0x0,0x90,0x27,0x3f,0xc1,0x4d} |
| 265 | * - Scanning physical memory address range 0x000F0000h to 0x000FFFFF for the anchor-string(s). */ |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 266 | dmi_mem = physmap_ro("DMI", 0xF0000, 0x10000); |
| 267 | if (dmi_mem == ERROR_PTR) |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 268 | return ret; |
| 269 | |
| 270 | for (fp = 0; fp <= 0xFFF0; fp += 16) { |
| 271 | #if SM_SUPPORT |
| 272 | if (memcmp(dmi_mem + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) { |
| 273 | if (smbios_decode(dmi_mem + fp)) // FIXME: length check |
| 274 | goto out; |
| 275 | } else |
| 276 | #endif |
| 277 | if (memcmp(dmi_mem + fp, "_DMI_", 5) == 0) |
| 278 | if (legacy_decode(dmi_mem + fp) == 0) { |
| 279 | ret = 0; |
| 280 | goto out; |
| 281 | } |
| 282 | } |
| 283 | msg_pinfo("No DMI table found.\n"); |
| 284 | out: |
| 285 | physunmap(dmi_mem, 0x10000); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | #else /* CONFIG_INTERNAL_DMI */ |
| 290 | |
| 291 | #define DMI_COMMAND_LEN_MAX 300 |
Stefan Tauner | 4f44479 | 2011-06-27 11:44:03 +0000 | [diff] [blame] | 292 | #if IS_WINDOWS |
| 293 | static const char *dmidecode_command = "dmidecode.exe 2>NUL"; |
| 294 | #else |
| 295 | static const char *dmidecode_command = "dmidecode 2>/dev/null"; |
| 296 | #endif |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 297 | |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 298 | static char *get_dmi_string(const char *string_name) |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 299 | { |
| 300 | FILE *dmidecode_pipe; |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 301 | char *result; |
| 302 | char answerbuf[DMI_MAX_ANSWER_LEN]; |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 303 | char commandline[DMI_COMMAND_LEN_MAX]; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 304 | |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 305 | snprintf(commandline, sizeof(commandline), |
| 306 | "%s -s %s", dmidecode_command, string_name); |
| 307 | dmidecode_pipe = popen(commandline, "r"); |
| 308 | if (!dmidecode_pipe) { |
Stefan Tauner | c6fa32d | 2013-01-04 22:54:07 +0000 | [diff] [blame] | 309 | msg_perr("Opening DMI pipe failed!\n"); |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 310 | return NULL; |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 311 | } |
Michael Karcher | d9f266d | 2010-08-08 21:56:52 +0000 | [diff] [blame] | 312 | |
| 313 | /* Kill lines starting with '#', as recent dmidecode versions |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 314 | * have the quirk to emit a "# SMBIOS implementations newer..." |
| 315 | * message even on "-s" if the SMBIOS declares a |
| 316 | * newer-than-supported version number, while it *should* only print |
| 317 | * the requested string. |
| 318 | */ |
Michael Karcher | d9f266d | 2010-08-08 21:56:52 +0000 | [diff] [blame] | 319 | do { |
| 320 | if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe)) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 321 | if (ferror(dmidecode_pipe)) { |
Michael Karcher | d9f266d | 2010-08-08 21:56:52 +0000 | [diff] [blame] | 322 | msg_perr("DMI pipe read error\n"); |
| 323 | pclose(dmidecode_pipe); |
| 324 | return NULL; |
| 325 | } else { |
| 326 | answerbuf[0] = 0; /* Hit EOF */ |
| 327 | } |
Michael Karcher | 45f79cb | 2010-03-24 17:55:04 +0000 | [diff] [blame] | 328 | } |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 329 | } while (answerbuf[0] == '#'); |
Michael Karcher | d9f266d | 2010-08-08 21:56:52 +0000 | [diff] [blame] | 330 | |
Stefan Tauner | c6fa32d | 2013-01-04 22:54:07 +0000 | [diff] [blame] | 331 | /* Discard all output exceeding DMI_MAX_ANSWER_LEN to prevent deadlock on pclose. */ |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 332 | while (!feof(dmidecode_pipe)) |
| 333 | getc(dmidecode_pipe); |
| 334 | if (pclose(dmidecode_pipe) != 0) { |
Stefan Tauner | c6fa32d | 2013-01-04 22:54:07 +0000 | [diff] [blame] | 335 | msg_pwarn("dmidecode execution unsuccessful - continuing without DMI info\n"); |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 336 | return NULL; |
| 337 | } |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 338 | |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 339 | /* Chomp trailing newline. */ |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 340 | if (answerbuf[0] != 0 && answerbuf[strlen(answerbuf) - 1] == '\n') |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 341 | answerbuf[strlen(answerbuf) - 1] = 0; |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 342 | |
| 343 | result = strdup(answerbuf); |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 344 | if (result == NULL) |
Stefan Tauner | c6fa32d | 2013-01-04 22:54:07 +0000 | [diff] [blame] | 345 | msg_pwarn("Warning: Out of memory - DMI support fails"); |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 346 | |
| 347 | return result; |
| 348 | } |
| 349 | |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 350 | int dmi_fill(void) |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 351 | { |
| 352 | int i; |
Michael Karcher | 8c1df28 | 2010-02-26 09:51:20 +0000 | [diff] [blame] | 353 | char *chassis_type; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 354 | |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 355 | msg_pdbg("Using External DMI decoder.\n"); |
| 356 | for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) { |
| 357 | dmi_strings[i].value = get_dmi_string(dmi_strings[i].keyword); |
| 358 | if (dmi_strings[i].value == NULL) |
| 359 | return 1; |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 360 | } |
Michael Karcher | 8c1df28 | 2010-02-26 09:51:20 +0000 | [diff] [blame] | 361 | |
| 362 | chassis_type = get_dmi_string("chassis-type"); |
Stefan Tauner | a34d719 | 2011-07-26 00:54:42 +0000 | [diff] [blame] | 363 | if (chassis_type == NULL) |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 364 | return 0; /* chassis-type handling is optional anyway */ |
Stefan Tauner | a34d719 | 2011-07-26 00:54:42 +0000 | [diff] [blame] | 365 | |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 366 | msg_pdbg("DMI string chassis-type: \"%s\"\n", chassis_type); |
Stefan Tauner | a34d719 | 2011-07-26 00:54:42 +0000 | [diff] [blame] | 367 | is_laptop = 2; |
| 368 | for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) { |
| 369 | if (strcasecmp(chassis_type, dmi_chassis_types[i].name) == 0) { |
| 370 | is_laptop = dmi_chassis_types[i].is_laptop; |
| 371 | break; |
Carl-Daniel Hailfinger | cb3eb05 | 2010-09-26 21:43:53 +0000 | [diff] [blame] | 372 | } |
Michael Karcher | 8c1df28 | 2010-02-26 09:51:20 +0000 | [diff] [blame] | 373 | } |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 374 | free(chassis_type); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | #endif /* CONFIG_INTERNAL_DMI */ |
| 379 | |
| 380 | static int dmi_shutdown(void *data) |
| 381 | { |
| 382 | int i; |
| 383 | for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) { |
| 384 | free(dmi_strings[i].value); |
| 385 | dmi_strings[i].value = NULL; |
| 386 | } |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | void dmi_init(void) |
| 391 | { |
| 392 | /* Register shutdown function before we allocate anything. */ |
| 393 | if (register_shutdown(dmi_shutdown, NULL)) { |
| 394 | msg_pwarn("Warning: Could not register DMI shutdown function - continuing without DMI info.\n"); |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | /* dmi_fill fills the dmi_strings array, and if possible sets the global is_laptop variable. */ |
| 399 | if (dmi_fill() != 0) |
| 400 | return; |
Stefan Tauner | a34d719 | 2011-07-26 00:54:42 +0000 | [diff] [blame] | 401 | |
| 402 | switch (is_laptop) { |
| 403 | case 1: |
| 404 | msg_pdbg("Laptop detected via DMI.\n"); |
| 405 | break; |
| 406 | case 2: |
| 407 | msg_pdbg("DMI chassis-type is not specific enough.\n"); |
| 408 | break; |
| 409 | } |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 410 | |
| 411 | has_dmi_support = 1; |
| 412 | int i; |
| 413 | for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) { |
| 414 | msg_pdbg("DMI string %s: \"%s\"\n", dmi_strings[i].keyword, |
| 415 | (dmi_strings[i].value == NULL) ? "" : dmi_strings[i].value); |
| 416 | } |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Does an substring/prefix/postfix/whole-string match. |
| 421 | * |
| 422 | * The pattern is matched as-is. The only metacharacters supported are '^' |
| 423 | * at the beginning and '$' at the end. So you can look for "^prefix", |
| 424 | * "suffix$", "substring" or "^complete string$". |
| 425 | * |
Stefan Tauner | d1045d8 | 2013-10-29 01:38:45 +0000 | [diff] [blame] | 426 | * @param value The non-NULL string to check. |
| 427 | * @param pattern The non-NULL pattern. |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 428 | * @return Nonzero if pattern matches. |
| 429 | */ |
| 430 | static int dmi_compare(const char *value, const char *pattern) |
| 431 | { |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 432 | int anchored = 0; |
| 433 | int patternlen; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 434 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 435 | msg_pspew("matching %s against %s\n", value, pattern); |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 436 | /* The empty string is part of all strings! */ |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 437 | if (pattern[0] == 0) |
| 438 | return 1; |
| 439 | |
| 440 | if (pattern[0] == '^') { |
| 441 | anchored = 1; |
| 442 | pattern++; |
| 443 | } |
| 444 | |
| 445 | patternlen = strlen(pattern); |
| 446 | if (pattern[patternlen - 1] == '$') { |
| 447 | int valuelen = strlen(value); |
| 448 | patternlen--; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 449 | if (patternlen > valuelen) |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 450 | return 0; |
| 451 | |
| 452 | /* full string match: require same length */ |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 453 | if (anchored && (valuelen != patternlen)) |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 454 | return 0; |
| 455 | |
| 456 | /* start character to make ends match */ |
| 457 | value += valuelen - patternlen; |
| 458 | anchored = 1; |
| 459 | } |
| 460 | |
| 461 | if (anchored) |
| 462 | return strncmp(value, pattern, patternlen) == 0; |
| 463 | else |
| 464 | return strstr(value, pattern) != NULL; |
| 465 | } |
| 466 | |
| 467 | int dmi_match(const char *pattern) |
| 468 | { |
| 469 | int i; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 470 | |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 471 | if (!has_dmi_support) |
| 472 | return 0; |
| 473 | |
Stefan Tauner | d1045d8 | 2013-10-29 01:38:45 +0000 | [diff] [blame] | 474 | for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) { |
| 475 | if (dmi_strings[i].value == NULL) |
| 476 | continue; |
| 477 | |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 478 | if (dmi_compare(dmi_strings[i].value, pattern)) |
Michael Karcher | 4dfa093 | 2010-02-12 05:44:18 +0000 | [diff] [blame] | 479 | return 1; |
Stefan Tauner | d1045d8 | 2013-10-29 01:38:45 +0000 | [diff] [blame] | 480 | } |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 481 | |
| 482 | return 0; |
| 483 | } |
Carl-Daniel Hailfinger | e1fdff4 | 2010-06-23 23:14:44 +0000 | [diff] [blame] | 484 | |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 485 | #endif // defined(__i386__) || defined(__x86_64__) |