Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009,2010 Michael Karcher |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | #include "flash.h" |
| 26 | |
| 27 | enum dmi_strings { |
| 28 | DMI_SYS_MANUFACTURER, |
| 29 | DMI_SYS_PRODUCT, |
| 30 | DMI_SYS_VERSION, |
| 31 | DMI_BB_MANUFACTURER, |
| 32 | DMI_BB_PRODUCT, |
| 33 | DMI_BB_VERSION, |
| 34 | DMI_ID_INVALID /* This must always be the last entry */ |
| 35 | }; |
| 36 | |
| 37 | /* The short_id for baseboard starts with "m" as in mainboard to leave |
| 38 | "b" available for BIOS */ |
Michael Karcher | f6498d7 | 2010-02-24 22:43:44 +0000 | [diff] [blame] | 39 | const char *dmidecode_names[DMI_ID_INVALID] = { |
| 40 | "system-manufacturer", |
| 41 | "system-product-name", |
| 42 | "system-version", |
| 43 | "baseboard-manufacturer", |
| 44 | "baseboard-product-name", |
| 45 | "baseboard-version" |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | #define DMI_COMMAND_LEN_MAX 260 |
| 49 | const char *dmidecode_command = "dmidecode"; |
| 50 | |
| 51 | int has_dmi_support = 0; |
| 52 | char *dmistrings[DMI_ID_INVALID]; |
| 53 | |
| 54 | /* strings longer than 4096 in DMI are just insane */ |
| 55 | #define DMI_MAX_ANSWER_LEN 4096 |
| 56 | |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 57 | static char *get_dmi_string(const char *string_name) |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 58 | { |
| 59 | FILE *dmidecode_pipe; |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 60 | char *result; |
| 61 | char answerbuf[DMI_MAX_ANSWER_LEN]; |
| 62 | char commandline[DMI_COMMAND_LEN_MAX+40]; |
| 63 | snprintf(commandline, sizeof(commandline), |
| 64 | "%s -s %s", dmidecode_command, string_name); |
| 65 | dmidecode_pipe = popen(commandline, "r"); |
| 66 | if (!dmidecode_pipe) { |
| 67 | printf_debug("DMI pipe open error\n"); |
| 68 | return NULL; |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 69 | } |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 70 | if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe) && |
| 71 | ferror(dmidecode_pipe)) { |
| 72 | printf_debug("DMI pipe read error\n"); |
| 73 | pclose(dmidecode_pipe); |
| 74 | return NULL; |
| 75 | } |
| 76 | /* Toss all output above DMI_MAX_ANSWER_LEN away to prevent |
| 77 | deadlock on pclose. */ |
| 78 | while (!feof(dmidecode_pipe)) |
| 79 | getc(dmidecode_pipe); |
| 80 | if (pclose(dmidecode_pipe) != 0) { |
| 81 | printf_debug("DMI pipe close error\n"); |
| 82 | return NULL; |
| 83 | } |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 84 | |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 85 | /* chomp trailing newline */ |
| 86 | if (answerbuf[0] != 0 && |
| 87 | answerbuf[strlen(answerbuf) - 1] == '\n') |
| 88 | answerbuf[strlen(answerbuf) - 1] = 0; |
| 89 | printf_debug("DMI string %s: \"%s\"\n", string_name, answerbuf); |
| 90 | |
| 91 | result = strdup(answerbuf); |
| 92 | if (!result) |
| 93 | puts("WARNING: Out of memory - DMI support fails"); |
| 94 | |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | void dmi_init(void) |
| 99 | { |
| 100 | int i; |
Michael Karcher | 8c1df28 | 2010-02-26 09:51:20 +0000 | [diff] [blame^] | 101 | char *chassis_type; |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 102 | has_dmi_support = 1; |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 103 | for (i = 0; i < DMI_ID_INVALID; i++) { |
| 104 | dmistrings[i] = get_dmi_string(dmidecode_names[i]); |
| 105 | if (!dmistrings[i]) { |
| 106 | has_dmi_support = 0; |
| 107 | break; |
| 108 | } |
| 109 | } |
Michael Karcher | 8c1df28 | 2010-02-26 09:51:20 +0000 | [diff] [blame^] | 110 | |
| 111 | chassis_type = get_dmi_string("chassis-type"); |
| 112 | if (chassis_type && !strcmp(chassis_type, "Notebook")) { |
| 113 | printf_debug("Laptop detected via DMI"); |
| 114 | is_laptop = 1; |
| 115 | } |
| 116 | free(chassis_type); |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Does an substring/prefix/postfix/whole-string match. |
| 121 | * |
| 122 | * The pattern is matched as-is. The only metacharacters supported are '^' |
| 123 | * at the beginning and '$' at the end. So you can look for "^prefix", |
| 124 | * "suffix$", "substring" or "^complete string$". |
| 125 | * |
| 126 | * @param value The string to check. |
| 127 | * @param pattern The pattern. |
| 128 | * @return Nonzero if pattern matches. |
| 129 | */ |
| 130 | static int dmi_compare(const char *value, const char *pattern) |
| 131 | { |
| 132 | int anchored = 0; |
| 133 | int patternlen; |
| 134 | printf_debug("matching %s against %s\n", value, pattern); |
| 135 | /* The empty string is part of all strings */ |
| 136 | if (pattern[0] == 0) |
| 137 | return 1; |
| 138 | |
| 139 | if (pattern[0] == '^') { |
| 140 | anchored = 1; |
| 141 | pattern++; |
| 142 | } |
| 143 | |
| 144 | patternlen = strlen(pattern); |
| 145 | if (pattern[patternlen - 1] == '$') { |
| 146 | int valuelen = strlen(value); |
| 147 | patternlen--; |
| 148 | if(patternlen > valuelen) |
| 149 | return 0; |
| 150 | |
| 151 | /* full string match: require same length */ |
| 152 | if(anchored && (valuelen != patternlen)) |
| 153 | return 0; |
| 154 | |
| 155 | /* start character to make ends match */ |
| 156 | value += valuelen - patternlen; |
| 157 | anchored = 1; |
| 158 | } |
| 159 | |
| 160 | if (anchored) |
| 161 | return strncmp(value, pattern, patternlen) == 0; |
| 162 | else |
| 163 | return strstr(value, pattern) != NULL; |
| 164 | } |
| 165 | |
| 166 | int dmi_match(const char *pattern) |
| 167 | { |
| 168 | int i; |
| 169 | if (!has_dmi_support) |
| 170 | return 0; |
| 171 | |
| 172 | for (i = 0;i < DMI_ID_INVALID; i++) |
Michael Karcher | 4dfa093 | 2010-02-12 05:44:18 +0000 | [diff] [blame] | 173 | if(dmi_compare(dmistrings[i], pattern)) |
| 174 | return 1; |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 175 | |
| 176 | return 0; |
| 177 | } |