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