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 | |
Michael Karcher | fd41670 | 2010-03-14 17:57:52 +0000 | [diff] [blame] | 27 | const char *dmidecode_names[] = { |
Michael Karcher | f6498d7 | 2010-02-24 22:43:44 +0000 | [diff] [blame] | 28 | "system-manufacturer", |
| 29 | "system-product-name", |
| 30 | "system-version", |
| 31 | "baseboard-manufacturer", |
| 32 | "baseboard-product-name", |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 33 | "baseboard-version", |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | #define DMI_COMMAND_LEN_MAX 260 |
| 37 | const char *dmidecode_command = "dmidecode"; |
| 38 | |
| 39 | int has_dmi_support = 0; |
Michael Karcher | fd41670 | 2010-03-14 17:57:52 +0000 | [diff] [blame] | 40 | char *dmistrings[ARRAY_SIZE(dmidecode_names)]; |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 41 | |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 42 | /* Strings longer than 4096 in DMI are just insane. */ |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 43 | #define DMI_MAX_ANSWER_LEN 4096 |
| 44 | |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 45 | static char *get_dmi_string(const char *string_name) |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 46 | { |
| 47 | FILE *dmidecode_pipe; |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 48 | char *result; |
| 49 | char answerbuf[DMI_MAX_ANSWER_LEN]; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 50 | char commandline[DMI_COMMAND_LEN_MAX + 40]; |
| 51 | |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 52 | snprintf(commandline, sizeof(commandline), |
| 53 | "%s -s %s", dmidecode_command, string_name); |
| 54 | dmidecode_pipe = popen(commandline, "r"); |
| 55 | if (!dmidecode_pipe) { |
| 56 | printf_debug("DMI pipe open error\n"); |
| 57 | return NULL; |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 58 | } |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 59 | if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe) && |
| 60 | ferror(dmidecode_pipe)) { |
| 61 | printf_debug("DMI pipe read error\n"); |
| 62 | pclose(dmidecode_pipe); |
| 63 | return NULL; |
| 64 | } |
| 65 | /* Toss all output above DMI_MAX_ANSWER_LEN away to prevent |
| 66 | deadlock on pclose. */ |
| 67 | while (!feof(dmidecode_pipe)) |
| 68 | getc(dmidecode_pipe); |
| 69 | if (pclose(dmidecode_pipe) != 0) { |
| 70 | printf_debug("DMI pipe close error\n"); |
| 71 | return NULL; |
| 72 | } |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 73 | |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 74 | /* Chomp trailing newline. */ |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 75 | if (answerbuf[0] != 0 && |
| 76 | answerbuf[strlen(answerbuf) - 1] == '\n') |
| 77 | answerbuf[strlen(answerbuf) - 1] = 0; |
| 78 | printf_debug("DMI string %s: \"%s\"\n", string_name, answerbuf); |
| 79 | |
| 80 | result = strdup(answerbuf); |
| 81 | if (!result) |
| 82 | puts("WARNING: Out of memory - DMI support fails"); |
| 83 | |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | void dmi_init(void) |
| 88 | { |
| 89 | int i; |
Michael Karcher | 8c1df28 | 2010-02-26 09:51:20 +0000 | [diff] [blame] | 90 | char *chassis_type; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 91 | |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 92 | has_dmi_support = 1; |
Michael Karcher | fd41670 | 2010-03-14 17:57:52 +0000 | [diff] [blame] | 93 | for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++) { |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 94 | dmistrings[i] = get_dmi_string(dmidecode_names[i]); |
| 95 | if (!dmistrings[i]) { |
| 96 | has_dmi_support = 0; |
Michael Karcher | fd41670 | 2010-03-14 17:57:52 +0000 | [diff] [blame] | 97 | return; |
Michael Karcher | 0d7fb7c | 2010-02-26 09:51:16 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
Michael Karcher | 8c1df28 | 2010-02-26 09:51:20 +0000 | [diff] [blame] | 100 | |
| 101 | chassis_type = get_dmi_string("chassis-type"); |
| 102 | if (chassis_type && !strcmp(chassis_type, "Notebook")) { |
Michael Karcher | fd41670 | 2010-03-14 17:57:52 +0000 | [diff] [blame] | 103 | printf_debug("Laptop detected via DMI\n"); |
Michael Karcher | 8c1df28 | 2010-02-26 09:51:20 +0000 | [diff] [blame] | 104 | is_laptop = 1; |
| 105 | } |
| 106 | free(chassis_type); |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Does an substring/prefix/postfix/whole-string match. |
| 111 | * |
| 112 | * The pattern is matched as-is. The only metacharacters supported are '^' |
| 113 | * at the beginning and '$' at the end. So you can look for "^prefix", |
| 114 | * "suffix$", "substring" or "^complete string$". |
| 115 | * |
| 116 | * @param value The string to check. |
| 117 | * @param pattern The pattern. |
| 118 | * @return Nonzero if pattern matches. |
| 119 | */ |
| 120 | static int dmi_compare(const char *value, const char *pattern) |
| 121 | { |
| 122 | int anchored = 0; |
| 123 | int patternlen; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 124 | |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 125 | printf_debug("matching %s against %s\n", value, pattern); |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 126 | /* The empty string is part of all strings! */ |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 127 | if (pattern[0] == 0) |
| 128 | return 1; |
| 129 | |
| 130 | if (pattern[0] == '^') { |
| 131 | anchored = 1; |
| 132 | pattern++; |
| 133 | } |
| 134 | |
| 135 | patternlen = strlen(pattern); |
| 136 | if (pattern[patternlen - 1] == '$') { |
| 137 | int valuelen = strlen(value); |
| 138 | patternlen--; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 139 | if (patternlen > valuelen) |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 140 | return 0; |
| 141 | |
| 142 | /* full string match: require same length */ |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 143 | if (anchored && (valuelen != patternlen)) |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 144 | return 0; |
| 145 | |
| 146 | /* start character to make ends match */ |
| 147 | value += valuelen - patternlen; |
| 148 | anchored = 1; |
| 149 | } |
| 150 | |
| 151 | if (anchored) |
| 152 | return strncmp(value, pattern, patternlen) == 0; |
| 153 | else |
| 154 | return strstr(value, pattern) != NULL; |
| 155 | } |
| 156 | |
| 157 | int dmi_match(const char *pattern) |
| 158 | { |
| 159 | int i; |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 160 | |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 161 | if (!has_dmi_support) |
| 162 | return 0; |
| 163 | |
Michael Karcher | fd41670 | 2010-03-14 17:57:52 +0000 | [diff] [blame] | 164 | for (i = 0; i < ARRAY_SIZE(dmidecode_names); i++) |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 165 | if (dmi_compare(dmistrings[i], pattern)) |
Michael Karcher | 4dfa093 | 2010-02-12 05:44:18 +0000 | [diff] [blame] | 166 | return 1; |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 167 | |
| 168 | return 0; |
| 169 | } |