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 */ |
| 39 | struct { |
| 40 | const char *dmidecode_name; |
| 41 | char short_id[3]; |
| 42 | } dmi_properties[DMI_ID_INVALID] = { |
| 43 | {"system-manufacturer", "sm"}, |
| 44 | {"system-product-name", "sp"}, |
| 45 | {"system-version", "sv"}, |
| 46 | {"baseboard-manufacturer", "mm"}, |
| 47 | {"baseboard-product-name", "mp"}, |
| 48 | {"baseboard-version", "mv"} |
| 49 | }; |
| 50 | |
| 51 | #define DMI_COMMAND_LEN_MAX 260 |
| 52 | const char *dmidecode_command = "dmidecode"; |
| 53 | |
| 54 | int has_dmi_support = 0; |
| 55 | char *dmistrings[DMI_ID_INVALID]; |
| 56 | |
| 57 | /* strings longer than 4096 in DMI are just insane */ |
| 58 | #define DMI_MAX_ANSWER_LEN 4096 |
| 59 | |
| 60 | void dmi_init(void) |
| 61 | { |
| 62 | FILE *dmidecode_pipe; |
| 63 | int i; |
| 64 | char *answerbuf = malloc(DMI_MAX_ANSWER_LEN); |
| 65 | if(!answerbuf) |
| 66 | { |
| 67 | fprintf(stderr, "DMI: couldn't alloc answer buffer\n"); |
| 68 | return; |
| 69 | } |
| 70 | for (i = 0; i < DMI_ID_INVALID; i++) |
| 71 | { |
| 72 | char commandline[DMI_COMMAND_LEN_MAX+40]; |
| 73 | snprintf(commandline, sizeof(commandline), |
| 74 | "%s -s %s", dmidecode_command, |
| 75 | dmi_properties[i].dmidecode_name); |
| 76 | dmidecode_pipe = popen(commandline, "r"); |
| 77 | if (!dmidecode_pipe) |
| 78 | { |
| 79 | printf_debug("DMI pipe open error\n"); |
| 80 | goto out_free; |
| 81 | } |
Michael Karcher | 823dc9d | 2010-01-27 10:08:33 +0000 | [diff] [blame] | 82 | if (!fgets(answerbuf, DMI_MAX_ANSWER_LEN, dmidecode_pipe) && |
| 83 | ferror(dmidecode_pipe)) |
Michael Karcher | 6701ee8 | 2010-01-20 14:14:11 +0000 | [diff] [blame] | 84 | { |
| 85 | printf_debug("DMI pipe read error\n"); |
| 86 | pclose(dmidecode_pipe); |
| 87 | goto out_free; |
| 88 | } |
| 89 | /* Toss all output above DMI_MAX_ANSWER_LEN away to prevent |
| 90 | deadlock on pclose. */ |
| 91 | while (!feof(dmidecode_pipe)) |
| 92 | getc(dmidecode_pipe); |
| 93 | if (pclose(dmidecode_pipe) != 0) |
| 94 | { |
| 95 | printf_debug("DMI pipe close error\n"); |
| 96 | goto out_free; |
| 97 | } |
| 98 | |
| 99 | /* chomp trailing newline */ |
| 100 | if (answerbuf[0] != 0 && |
| 101 | answerbuf[strlen(answerbuf) - 1] == '\n') |
| 102 | answerbuf[strlen(answerbuf) - 1] = 0; |
| 103 | printf_debug("DMI string %d: \"%s\"\n", i, answerbuf); |
| 104 | dmistrings[i] = strdup(answerbuf); |
| 105 | } |
| 106 | has_dmi_support = 1; |
| 107 | out_free: |
| 108 | free(answerbuf); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Does an substring/prefix/postfix/whole-string match. |
| 113 | * |
| 114 | * The pattern is matched as-is. The only metacharacters supported are '^' |
| 115 | * at the beginning and '$' at the end. So you can look for "^prefix", |
| 116 | * "suffix$", "substring" or "^complete string$". |
| 117 | * |
| 118 | * @param value The string to check. |
| 119 | * @param pattern The pattern. |
| 120 | * @return Nonzero if pattern matches. |
| 121 | */ |
| 122 | static int dmi_compare(const char *value, const char *pattern) |
| 123 | { |
| 124 | int anchored = 0; |
| 125 | int patternlen; |
| 126 | printf_debug("matching %s against %s\n", value, pattern); |
| 127 | /* The empty string is part of all strings */ |
| 128 | if (pattern[0] == 0) |
| 129 | return 1; |
| 130 | |
| 131 | if (pattern[0] == '^') { |
| 132 | anchored = 1; |
| 133 | pattern++; |
| 134 | } |
| 135 | |
| 136 | patternlen = strlen(pattern); |
| 137 | if (pattern[patternlen - 1] == '$') { |
| 138 | int valuelen = strlen(value); |
| 139 | patternlen--; |
| 140 | if(patternlen > valuelen) |
| 141 | return 0; |
| 142 | |
| 143 | /* full string match: require same length */ |
| 144 | if(anchored && (valuelen != patternlen)) |
| 145 | return 0; |
| 146 | |
| 147 | /* start character to make ends match */ |
| 148 | value += valuelen - patternlen; |
| 149 | anchored = 1; |
| 150 | } |
| 151 | |
| 152 | if (anchored) |
| 153 | return strncmp(value, pattern, patternlen) == 0; |
| 154 | else |
| 155 | return strstr(value, pattern) != NULL; |
| 156 | } |
| 157 | |
| 158 | int dmi_match(const char *pattern) |
| 159 | { |
| 160 | int i; |
| 161 | if (!has_dmi_support) |
| 162 | return 0; |
| 163 | |
| 164 | for (i = 0;i < DMI_ID_INVALID; i++) |
| 165 | return dmi_compare(dmistrings[i], pattern); |
| 166 | |
| 167 | return 0; |
| 168 | } |